Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * USB hub driver. |
| 3 | * |
| 4 | * (C) Copyright 1999 Linus Torvalds |
| 5 | * (C) Copyright 1999 Johannes Erdfelt |
| 6 | * (C) Copyright 1999 Gregory P. Smith |
| 7 | * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) |
| 8 | * |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/moduleparam.h> |
| 15 | #include <linux/completion.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/list.h> |
| 18 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/ioctl.h> |
| 20 | #include <linux/usb.h> |
| 21 | #include <linux/usbdevice_fs.h> |
Eric Lescouet | 27729aa | 2010-04-24 23:21:52 +0200 | [diff] [blame] | 22 | #include <linux/usb/hcd.h> |
Richard Zhao | 925aa46 | 2012-07-11 11:09:28 +0800 | [diff] [blame] | 23 | #include <linux/usb/otg.h> |
Phil Dibowitz | 93362a8 | 2010-07-22 00:05:01 +0200 | [diff] [blame] | 24 | #include <linux/usb/quirks.h> |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Arjan van de Ven | 4186ecf | 2006-01-11 15:55:29 +0100 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 27 | #include <linux/freezer.h> |
Theodore Ts'o | b04b315 | 2012-07-04 11:22:20 -0400 | [diff] [blame] | 28 | #include <linux/random.h> |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 29 | #include <linux/pm_qos.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/byteorder.h> |
| 33 | |
Lan Tianyu | 6e30d7c | 2013-01-11 20:10:38 +0800 | [diff] [blame] | 34 | #include "hub.h" |
Peter Chen | 026f3fc | 2014-08-19 09:51:53 +0800 | [diff] [blame] | 35 | #include "otg_whitelist.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Ming Lei | e6f30de | 2012-10-24 11:59:24 +0800 | [diff] [blame] | 37 | #define USB_VENDOR_GENESYS_LOGIC 0x05e3 |
| 38 | #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01 |
| 39 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 40 | /* Protect struct usb_device->state and ->children members |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 41 | * Note: Both are also protected by ->dev.sem, except that ->state can |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ |
| 43 | static DEFINE_SPINLOCK(device_state_lock); |
| 44 | |
| 45 | /* khubd's worklist and its lock */ |
| 46 | static DEFINE_SPINLOCK(hub_event_lock); |
| 47 | static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ |
| 48 | |
| 49 | /* Wakes up khubd */ |
| 50 | static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); |
| 51 | |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 52 | static struct task_struct *khubd_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 54 | /* synchronize hub-port add/remove and peering operations */ |
| 55 | DEFINE_MUTEX(usb_port_peer_mutex); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* cycle leds on hubs that aren't blinking for attention */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 58 | static bool blinkenlights = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | module_param (blinkenlights, bool, S_IRUGO); |
| 60 | MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); |
| 61 | |
| 62 | /* |
Jaroslav Kysela | fd7c519 | 2008-10-10 16:24:45 +0200 | [diff] [blame] | 63 | * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about |
| 64 | * 10 seconds to send reply for the initial 64-byte descriptor request. |
| 65 | */ |
| 66 | /* define initial 64-byte descriptor request timeout in milliseconds */ |
| 67 | static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; |
| 68 | module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 69 | MODULE_PARM_DESC(initial_descriptor_timeout, |
| 70 | "initial 64-byte descriptor request timeout in milliseconds " |
| 71 | "(default 5000 - 5.0 seconds)"); |
Jaroslav Kysela | fd7c519 | 2008-10-10 16:24:45 +0200 | [diff] [blame] | 72 | |
| 73 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | * As of 2.6.10 we introduce a new USB device initialization scheme which |
| 75 | * closely resembles the way Windows works. Hopefully it will be compatible |
| 76 | * with a wider range of devices than the old scheme. However some previously |
| 77 | * working devices may start giving rise to "device not accepting address" |
| 78 | * errors; if that happens the user can try the old scheme by adjusting the |
| 79 | * following module parameters. |
| 80 | * |
| 81 | * For maximum flexibility there are two boolean parameters to control the |
| 82 | * hub driver's behavior. On the first initialization attempt, if the |
| 83 | * "old_scheme_first" parameter is set then the old scheme will be used, |
| 84 | * otherwise the new scheme is used. If that fails and "use_both_schemes" |
| 85 | * is set, then the driver will make another attempt, using the other scheme. |
| 86 | */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 87 | static bool old_scheme_first = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); |
| 89 | MODULE_PARM_DESC(old_scheme_first, |
| 90 | "start with the old device initialization scheme"); |
| 91 | |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 92 | static bool use_both_schemes = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); |
| 94 | MODULE_PARM_DESC(use_both_schemes, |
| 95 | "try the other device initialization scheme if the " |
| 96 | "first one fails"); |
| 97 | |
Alan Stern | 32fe019 | 2007-10-10 16:27:07 -0400 | [diff] [blame] | 98 | /* Mutual exclusion for EHCI CF initialization. This interferes with |
| 99 | * port reset on some companion controllers. |
| 100 | */ |
| 101 | DECLARE_RWSEM(ehci_cf_port_reset_rwsem); |
| 102 | EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); |
| 103 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 104 | #define HUB_DEBOUNCE_TIMEOUT 2000 |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 105 | #define HUB_DEBOUNCE_STEP 25 |
| 106 | #define HUB_DEBOUNCE_STABLE 100 |
| 107 | |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 108 | static int usb_reset_and_verify_device(struct usb_device *udev); |
| 109 | |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 110 | static inline char *portspeed(struct usb_hub *hub, int portstatus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 112 | if (hub_is_superspeed(hub->hdev)) |
| 113 | return "5.0 Gb/s"; |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 114 | if (portstatus & USB_PORT_STAT_HIGH_SPEED) |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 115 | return "480 Mb/s"; |
Alan Stern | 288ead4 | 2010-03-04 11:32:30 -0500 | [diff] [blame] | 116 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return "1.5 Mb/s"; |
| 118 | else |
| 119 | return "12 Mb/s"; |
| 120 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* Note that hdev or one of its children must be locked! */ |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 123 | struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 125 | if (!hdev || !hdev->actconfig || !hdev->maxchild) |
Alan Stern | 25118084 | 2009-07-22 14:41:18 -0400 | [diff] [blame] | 126 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return usb_get_intfdata(hdev->actconfig->interface[0]); |
| 128 | } |
| 129 | |
Sarah Sharp | 140e302 | 2014-01-22 13:35:02 -0800 | [diff] [blame] | 130 | static int usb_device_supports_lpm(struct usb_device *udev) |
Sarah Sharp | d9b2099 | 2012-02-20 08:31:26 -0800 | [diff] [blame] | 131 | { |
| 132 | /* USB 2.1 (and greater) devices indicate LPM support through |
| 133 | * their USB 2.0 Extended Capabilities BOS descriptor. |
| 134 | */ |
| 135 | if (udev->speed == USB_SPEED_HIGH) { |
| 136 | if (udev->bos->ext_cap && |
| 137 | (USB_LPM_SUPPORT & |
| 138 | le32_to_cpu(udev->bos->ext_cap->bmAttributes))) |
| 139 | return 1; |
| 140 | return 0; |
| 141 | } |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 142 | |
Sarah Sharp | 25cd288 | 2014-01-17 14:15:44 -0800 | [diff] [blame] | 143 | /* |
| 144 | * According to the USB 3.0 spec, all USB 3.0 devices must support LPM. |
| 145 | * However, there are some that don't, and they set the U1/U2 exit |
| 146 | * latencies to zero. |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 147 | */ |
| 148 | if (!udev->bos->ss_cap) { |
Sarah Sharp | 25cd288 | 2014-01-17 14:15:44 -0800 | [diff] [blame] | 149 | dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n"); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 150 | return 0; |
| 151 | } |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 152 | |
Sarah Sharp | 25cd288 | 2014-01-17 14:15:44 -0800 | [diff] [blame] | 153 | if (udev->bos->ss_cap->bU1devExitLat == 0 && |
| 154 | udev->bos->ss_cap->bU2DevExitLat == 0) { |
| 155 | if (udev->parent) |
| 156 | dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n"); |
| 157 | else |
| 158 | dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n"); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | if (!udev->parent || udev->parent->lpm_capable) |
| 163 | return 1; |
Sarah Sharp | d9b2099 | 2012-02-20 08:31:26 -0800 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 167 | /* |
| 168 | * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from |
| 169 | * either U1 or U2. |
| 170 | */ |
| 171 | static void usb_set_lpm_mel(struct usb_device *udev, |
| 172 | struct usb3_lpm_parameters *udev_lpm_params, |
| 173 | unsigned int udev_exit_latency, |
| 174 | struct usb_hub *hub, |
| 175 | struct usb3_lpm_parameters *hub_lpm_params, |
| 176 | unsigned int hub_exit_latency) |
| 177 | { |
| 178 | unsigned int total_mel; |
| 179 | unsigned int device_mel; |
| 180 | unsigned int hub_mel; |
| 181 | |
| 182 | /* |
| 183 | * Calculate the time it takes to transition all links from the roothub |
| 184 | * to the parent hub into U0. The parent hub must then decode the |
| 185 | * packet (hub header decode latency) to figure out which port it was |
| 186 | * bound for. |
| 187 | * |
| 188 | * The Hub Header decode latency is expressed in 0.1us intervals (0x1 |
| 189 | * means 0.1us). Multiply that by 100 to get nanoseconds. |
| 190 | */ |
| 191 | total_mel = hub_lpm_params->mel + |
| 192 | (hub->descriptor->u.ss.bHubHdrDecLat * 100); |
| 193 | |
| 194 | /* |
| 195 | * How long will it take to transition the downstream hub's port into |
| 196 | * U0? The greater of either the hub exit latency or the device exit |
| 197 | * latency. |
| 198 | * |
| 199 | * The BOS U1/U2 exit latencies are expressed in 1us intervals. |
| 200 | * Multiply that by 1000 to get nanoseconds. |
| 201 | */ |
| 202 | device_mel = udev_exit_latency * 1000; |
| 203 | hub_mel = hub_exit_latency * 1000; |
| 204 | if (device_mel > hub_mel) |
| 205 | total_mel += device_mel; |
| 206 | else |
| 207 | total_mel += hub_mel; |
| 208 | |
| 209 | udev_lpm_params->mel = total_mel; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate |
| 214 | * a transition from either U1 or U2. |
| 215 | */ |
| 216 | static void usb_set_lpm_pel(struct usb_device *udev, |
| 217 | struct usb3_lpm_parameters *udev_lpm_params, |
| 218 | unsigned int udev_exit_latency, |
| 219 | struct usb_hub *hub, |
| 220 | struct usb3_lpm_parameters *hub_lpm_params, |
| 221 | unsigned int hub_exit_latency, |
| 222 | unsigned int port_to_port_exit_latency) |
| 223 | { |
| 224 | unsigned int first_link_pel; |
| 225 | unsigned int hub_pel; |
| 226 | |
| 227 | /* |
| 228 | * First, the device sends an LFPS to transition the link between the |
| 229 | * device and the parent hub into U0. The exit latency is the bigger of |
| 230 | * the device exit latency or the hub exit latency. |
| 231 | */ |
| 232 | if (udev_exit_latency > hub_exit_latency) |
| 233 | first_link_pel = udev_exit_latency * 1000; |
| 234 | else |
| 235 | first_link_pel = hub_exit_latency * 1000; |
| 236 | |
| 237 | /* |
| 238 | * When the hub starts to receive the LFPS, there is a slight delay for |
| 239 | * it to figure out that one of the ports is sending an LFPS. Then it |
| 240 | * will forward the LFPS to its upstream link. The exit latency is the |
| 241 | * delay, plus the PEL that we calculated for this hub. |
| 242 | */ |
| 243 | hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; |
| 244 | |
| 245 | /* |
| 246 | * According to figure C-7 in the USB 3.0 spec, the PEL for this device |
| 247 | * is the greater of the two exit latencies. |
| 248 | */ |
| 249 | if (first_link_pel > hub_pel) |
| 250 | udev_lpm_params->pel = first_link_pel; |
| 251 | else |
| 252 | udev_lpm_params->pel = hub_pel; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Set the System Exit Latency (SEL) to indicate the total worst-case time from |
| 257 | * when a device initiates a transition to U0, until when it will receive the |
| 258 | * first packet from the host controller. |
| 259 | * |
| 260 | * Section C.1.5.1 describes the four components to this: |
| 261 | * - t1: device PEL |
| 262 | * - t2: time for the ERDY to make it from the device to the host. |
| 263 | * - t3: a host-specific delay to process the ERDY. |
| 264 | * - t4: time for the packet to make it from the host to the device. |
| 265 | * |
| 266 | * t3 is specific to both the xHCI host and the platform the host is integrated |
| 267 | * into. The Intel HW folks have said it's negligible, FIXME if a different |
| 268 | * vendor says otherwise. |
| 269 | */ |
| 270 | static void usb_set_lpm_sel(struct usb_device *udev, |
| 271 | struct usb3_lpm_parameters *udev_lpm_params) |
| 272 | { |
| 273 | struct usb_device *parent; |
| 274 | unsigned int num_hubs; |
| 275 | unsigned int total_sel; |
| 276 | |
| 277 | /* t1 = device PEL */ |
| 278 | total_sel = udev_lpm_params->pel; |
| 279 | /* How many external hubs are in between the device & the root port. */ |
| 280 | for (parent = udev->parent, num_hubs = 0; parent->parent; |
| 281 | parent = parent->parent) |
| 282 | num_hubs++; |
| 283 | /* t2 = 2.1us + 250ns * (num_hubs - 1) */ |
| 284 | if (num_hubs > 0) |
| 285 | total_sel += 2100 + 250 * (num_hubs - 1); |
| 286 | |
| 287 | /* t4 = 250ns * num_hubs */ |
| 288 | total_sel += 250 * num_hubs; |
| 289 | |
| 290 | udev_lpm_params->sel = total_sel; |
| 291 | } |
| 292 | |
| 293 | static void usb_set_lpm_parameters(struct usb_device *udev) |
| 294 | { |
| 295 | struct usb_hub *hub; |
| 296 | unsigned int port_to_port_delay; |
| 297 | unsigned int udev_u1_del; |
| 298 | unsigned int udev_u2_del; |
| 299 | unsigned int hub_u1_del; |
| 300 | unsigned int hub_u2_del; |
| 301 | |
| 302 | if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER) |
| 303 | return; |
| 304 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 305 | hub = usb_hub_to_struct_hub(udev->parent); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 306 | /* It doesn't take time to transition the roothub into U0, since it |
| 307 | * doesn't have an upstream link. |
| 308 | */ |
| 309 | if (!hub) |
| 310 | return; |
| 311 | |
| 312 | udev_u1_del = udev->bos->ss_cap->bU1devExitLat; |
Xenia Ragiadakou | 4d96799 | 2013-08-31 18:09:13 +0300 | [diff] [blame] | 313 | udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 314 | hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; |
Xenia Ragiadakou | 4d96799 | 2013-08-31 18:09:13 +0300 | [diff] [blame] | 315 | hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 316 | |
| 317 | usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, |
| 318 | hub, &udev->parent->u1_params, hub_u1_del); |
| 319 | |
| 320 | usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, |
| 321 | hub, &udev->parent->u2_params, hub_u2_del); |
| 322 | |
| 323 | /* |
| 324 | * Appendix C, section C.2.2.2, says that there is a slight delay from |
| 325 | * when the parent hub notices the downstream port is trying to |
| 326 | * transition to U0 to when the hub initiates a U0 transition on its |
| 327 | * upstream port. The section says the delays are tPort2PortU1EL and |
| 328 | * tPort2PortU2EL, but it doesn't define what they are. |
| 329 | * |
| 330 | * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking |
| 331 | * about the same delays. Use the maximum delay calculations from those |
| 332 | * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For |
| 333 | * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I |
| 334 | * assume the device exit latencies they are talking about are the hub |
| 335 | * exit latencies. |
| 336 | * |
| 337 | * What do we do if the U2 exit latency is less than the U1 exit |
| 338 | * latency? It's possible, although not likely... |
| 339 | */ |
| 340 | port_to_port_delay = 1; |
| 341 | |
| 342 | usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, |
| 343 | hub, &udev->parent->u1_params, hub_u1_del, |
| 344 | port_to_port_delay); |
| 345 | |
| 346 | if (hub_u2_del > hub_u1_del) |
| 347 | port_to_port_delay = 1 + hub_u2_del - hub_u1_del; |
| 348 | else |
| 349 | port_to_port_delay = 1 + hub_u1_del; |
| 350 | |
| 351 | usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, |
| 352 | hub, &udev->parent->u2_params, hub_u2_del, |
| 353 | port_to_port_delay); |
| 354 | |
| 355 | /* Now that we've got PEL, calculate SEL. */ |
| 356 | usb_set_lpm_sel(udev, &udev->u1_params); |
| 357 | usb_set_lpm_sel(udev, &udev->u2_params); |
| 358 | } |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | /* USB 2.0 spec Section 11.24.4.5 */ |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 361 | static int get_hub_descriptor(struct usb_device *hdev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 363 | int i, ret, size; |
| 364 | unsigned dtype; |
| 365 | |
| 366 | if (hub_is_superspeed(hdev)) { |
| 367 | dtype = USB_DT_SS_HUB; |
| 368 | size = USB_DT_SS_HUB_SIZE; |
| 369 | } else { |
| 370 | dtype = USB_DT_HUB; |
| 371 | size = sizeof(struct usb_hub_descriptor); |
| 372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | for (i = 0; i < 3; i++) { |
| 375 | ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
| 376 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 377 | dtype << 8, 0, data, size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | USB_CTRL_GET_TIMEOUT); |
| 379 | if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) |
| 380 | return ret; |
| 381 | } |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | /* |
| 386 | * USB 2.0 spec Section 11.24.2.1 |
| 387 | */ |
| 388 | static int clear_hub_feature(struct usb_device *hdev, int feature) |
| 389 | { |
| 390 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 391 | USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * USB 2.0 spec Section 11.24.2.2 |
| 396 | */ |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 397 | int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
| 399 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 400 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, |
| 401 | NULL, 0, 1000); |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * USB 2.0 spec Section 11.24.2.13 |
| 406 | */ |
| 407 | static int set_port_feature(struct usb_device *hdev, int port1, int feature) |
| 408 | { |
| 409 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 410 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, |
| 411 | NULL, 0, 1000); |
| 412 | } |
| 413 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 414 | static char *to_led_name(int selector) |
| 415 | { |
| 416 | switch (selector) { |
| 417 | case HUB_LED_AMBER: |
| 418 | return "amber"; |
| 419 | case HUB_LED_GREEN: |
| 420 | return "green"; |
| 421 | case HUB_LED_OFF: |
| 422 | return "off"; |
| 423 | case HUB_LED_AUTO: |
| 424 | return "auto"; |
| 425 | default: |
| 426 | return "??"; |
| 427 | } |
| 428 | } |
| 429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | /* |
| 431 | * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 |
| 432 | * for info about using port indicators |
| 433 | */ |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 434 | static void set_port_led(struct usb_hub *hub, int port1, int selector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 436 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 437 | int status; |
| 438 | |
| 439 | status = set_port_feature(hub->hdev, (selector << 8) | port1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | USB_PORT_FEAT_INDICATOR); |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 441 | dev_dbg(&port_dev->dev, "indicator %s status %d\n", |
| 442 | to_led_name(selector), status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | #define LED_CYCLE_PERIOD ((2*HZ)/3) |
| 446 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 447 | static void led_work (struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 449 | struct usb_hub *hub = |
| 450 | container_of(work, struct usb_hub, leds.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | struct usb_device *hdev = hub->hdev; |
| 452 | unsigned i; |
| 453 | unsigned changed = 0; |
| 454 | int cursor = -1; |
| 455 | |
| 456 | if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) |
| 457 | return; |
| 458 | |
Krzysztof Mazur | 3bbc47d | 2013-08-22 14:49:40 +0200 | [diff] [blame] | 459 | for (i = 0; i < hdev->maxchild; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | unsigned selector, mode; |
| 461 | |
| 462 | /* 30%-50% duty cycle */ |
| 463 | |
| 464 | switch (hub->indicator[i]) { |
| 465 | /* cycle marker */ |
| 466 | case INDICATOR_CYCLE: |
| 467 | cursor = i; |
| 468 | selector = HUB_LED_AUTO; |
| 469 | mode = INDICATOR_AUTO; |
| 470 | break; |
| 471 | /* blinking green = sw attention */ |
| 472 | case INDICATOR_GREEN_BLINK: |
| 473 | selector = HUB_LED_GREEN; |
| 474 | mode = INDICATOR_GREEN_BLINK_OFF; |
| 475 | break; |
| 476 | case INDICATOR_GREEN_BLINK_OFF: |
| 477 | selector = HUB_LED_OFF; |
| 478 | mode = INDICATOR_GREEN_BLINK; |
| 479 | break; |
| 480 | /* blinking amber = hw attention */ |
| 481 | case INDICATOR_AMBER_BLINK: |
| 482 | selector = HUB_LED_AMBER; |
| 483 | mode = INDICATOR_AMBER_BLINK_OFF; |
| 484 | break; |
| 485 | case INDICATOR_AMBER_BLINK_OFF: |
| 486 | selector = HUB_LED_OFF; |
| 487 | mode = INDICATOR_AMBER_BLINK; |
| 488 | break; |
| 489 | /* blink green/amber = reserved */ |
| 490 | case INDICATOR_ALT_BLINK: |
| 491 | selector = HUB_LED_GREEN; |
| 492 | mode = INDICATOR_ALT_BLINK_OFF; |
| 493 | break; |
| 494 | case INDICATOR_ALT_BLINK_OFF: |
| 495 | selector = HUB_LED_AMBER; |
| 496 | mode = INDICATOR_ALT_BLINK; |
| 497 | break; |
| 498 | default: |
| 499 | continue; |
| 500 | } |
| 501 | if (selector != HUB_LED_AUTO) |
| 502 | changed = 1; |
| 503 | set_port_led(hub, i + 1, selector); |
| 504 | hub->indicator[i] = mode; |
| 505 | } |
| 506 | if (!changed && blinkenlights) { |
| 507 | cursor++; |
Krzysztof Mazur | 3bbc47d | 2013-08-22 14:49:40 +0200 | [diff] [blame] | 508 | cursor %= hdev->maxchild; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | set_port_led(hub, cursor + 1, HUB_LED_GREEN); |
| 510 | hub->indicator[cursor] = INDICATOR_CYCLE; |
| 511 | changed++; |
| 512 | } |
| 513 | if (changed) |
Shaibal Dutta | 22f6a0f | 2014-02-01 19:16:46 -0800 | [diff] [blame] | 514 | queue_delayed_work(system_power_efficient_wq, |
| 515 | &hub->leds, LED_CYCLE_PERIOD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /* use a short timeout for hub/port status fetches */ |
| 519 | #define USB_STS_TIMEOUT 1000 |
| 520 | #define USB_STS_RETRIES 5 |
| 521 | |
| 522 | /* |
| 523 | * USB 2.0 spec Section 11.24.2.6 |
| 524 | */ |
| 525 | static int get_hub_status(struct usb_device *hdev, |
| 526 | struct usb_hub_status *data) |
| 527 | { |
| 528 | int i, status = -ETIMEDOUT; |
| 529 | |
Libor Pechacek | 3824c1d | 2011-05-20 14:53:25 +0200 | [diff] [blame] | 530 | for (i = 0; i < USB_STS_RETRIES && |
| 531 | (status == -ETIMEDOUT || status == -EPIPE); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
| 533 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, |
| 534 | data, sizeof(*data), USB_STS_TIMEOUT); |
| 535 | } |
| 536 | return status; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * USB 2.0 spec Section 11.24.2.7 |
| 541 | */ |
| 542 | static int get_port_status(struct usb_device *hdev, int port1, |
| 543 | struct usb_port_status *data) |
| 544 | { |
| 545 | int i, status = -ETIMEDOUT; |
| 546 | |
Libor Pechacek | 3824c1d | 2011-05-20 14:53:25 +0200 | [diff] [blame] | 547 | for (i = 0; i < USB_STS_RETRIES && |
| 548 | (status == -ETIMEDOUT || status == -EPIPE); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
| 550 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, |
| 551 | data, sizeof(*data), USB_STS_TIMEOUT); |
| 552 | } |
| 553 | return status; |
| 554 | } |
| 555 | |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 556 | static int hub_port_status(struct usb_hub *hub, int port1, |
| 557 | u16 *status, u16 *change) |
| 558 | { |
| 559 | int ret; |
| 560 | |
| 561 | mutex_lock(&hub->status_mutex); |
| 562 | ret = get_port_status(hub->hdev, port1, &hub->status->port); |
| 563 | if (ret < 4) { |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 564 | if (ret != -ENODEV) |
| 565 | dev_err(hub->intfdev, |
| 566 | "%s failed (err = %d)\n", __func__, ret); |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 567 | if (ret >= 0) |
| 568 | ret = -EIO; |
| 569 | } else { |
| 570 | *status = le16_to_cpu(hub->status->port.wPortStatus); |
| 571 | *change = le16_to_cpu(hub->status->port.wPortChange); |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 572 | |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 573 | ret = 0; |
| 574 | } |
| 575 | mutex_unlock(&hub->status_mutex); |
| 576 | return ret; |
| 577 | } |
| 578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | static void kick_khubd(struct usb_hub *hub) |
| 580 | { |
| 581 | unsigned long flags; |
| 582 | |
| 583 | spin_lock_irqsave(&hub_event_lock, flags); |
Roel Kluin | 2e2c5ee | 2007-10-26 23:54:35 +0200 | [diff] [blame] | 584 | if (!hub->disconnected && list_empty(&hub->event_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | list_add_tail(&hub->event_list, &hub_event_list); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 586 | |
| 587 | /* Suppress autosuspend until khubd runs */ |
| 588 | usb_autopm_get_interface_no_resume( |
| 589 | to_usb_interface(hub->intfdev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | wake_up(&khubd_wait); |
| 591 | } |
| 592 | spin_unlock_irqrestore(&hub_event_lock, flags); |
| 593 | } |
| 594 | |
| 595 | void usb_kick_khubd(struct usb_device *hdev) |
| 596 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 597 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Alan Stern | 25118084 | 2009-07-22 14:41:18 -0400 | [diff] [blame] | 598 | |
| 599 | if (hub) |
| 600 | kick_khubd(hub); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 603 | /* |
| 604 | * Let the USB core know that a USB 3.0 device has sent a Function Wake Device |
| 605 | * Notification, which indicates it had initiated remote wakeup. |
| 606 | * |
| 607 | * USB 3.0 hubs do not report the port link state change from U3 to U0 when the |
| 608 | * device initiates resume, so the USB core will not receive notice of the |
| 609 | * resume through the normal hub interrupt URB. |
| 610 | */ |
| 611 | void usb_wakeup_notification(struct usb_device *hdev, |
| 612 | unsigned int portnum) |
| 613 | { |
| 614 | struct usb_hub *hub; |
| 615 | |
| 616 | if (!hdev) |
| 617 | return; |
| 618 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 619 | hub = usb_hub_to_struct_hub(hdev); |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 620 | if (hub) { |
| 621 | set_bit(portnum, hub->wakeup_bits); |
| 622 | kick_khubd(hub); |
| 623 | } |
| 624 | } |
| 625 | EXPORT_SYMBOL_GPL(usb_wakeup_notification); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
| 627 | /* completion function, fires on port status changes and various faults */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 628 | static void hub_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 630 | struct usb_hub *hub = urb->context; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 631 | int status = urb->status; |
Roel Kluin | 71d2718 | 2009-03-13 12:19:18 +0100 | [diff] [blame] | 632 | unsigned i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | unsigned long bits; |
| 634 | |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 635 | switch (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | case -ENOENT: /* synchronous unlink */ |
| 637 | case -ECONNRESET: /* async unlink */ |
| 638 | case -ESHUTDOWN: /* hardware going away */ |
| 639 | return; |
| 640 | |
| 641 | default: /* presumably an error */ |
| 642 | /* Cause a hub reset after 10 consecutive errors */ |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 643 | dev_dbg (hub->intfdev, "transfer --> %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if ((++hub->nerrors < 10) || hub->error) |
| 645 | goto resubmit; |
Alan Stern | e015268 | 2007-08-24 15:42:52 -0400 | [diff] [blame] | 646 | hub->error = status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | /* FALL THROUGH */ |
Tobias Klauser | ec17cf1 | 2006-09-13 21:38:41 +0200 | [diff] [blame] | 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | /* let khubd handle things */ |
| 650 | case 0: /* we got data: port status changed */ |
| 651 | bits = 0; |
| 652 | for (i = 0; i < urb->actual_length; ++i) |
| 653 | bits |= ((unsigned long) ((*hub->buffer)[i])) |
| 654 | << (i*8); |
| 655 | hub->event_bits[0] = bits; |
| 656 | break; |
| 657 | } |
| 658 | |
| 659 | hub->nerrors = 0; |
| 660 | |
| 661 | /* Something happened, let khubd figure it out */ |
| 662 | kick_khubd(hub); |
| 663 | |
| 664 | resubmit: |
| 665 | if (hub->quiescing) |
| 666 | return; |
| 667 | |
| 668 | if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 |
| 669 | && status != -ENODEV && status != -EPERM) |
| 670 | dev_err (hub->intfdev, "resubmit --> %d\n", status); |
| 671 | } |
| 672 | |
| 673 | /* USB 2.0 spec Section 11.24.2.3 */ |
| 674 | static inline int |
| 675 | hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) |
| 676 | { |
William Gulland | 2c7b871 | 2013-06-27 16:10:20 -0700 | [diff] [blame] | 677 | /* Need to clear both directions for control ep */ |
| 678 | if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) == |
| 679 | USB_ENDPOINT_XFER_CONTROL) { |
| 680 | int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 681 | HUB_CLEAR_TT_BUFFER, USB_RT_PORT, |
| 682 | devinfo ^ 0x8000, tt, NULL, 0, 1000); |
| 683 | if (status) |
| 684 | return status; |
| 685 | } |
Alan Stern | c2f6595 | 2009-11-18 11:37:15 -0500 | [diff] [blame] | 686 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, |
| 688 | tt, NULL, 0, 1000); |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * enumeration blocks khubd for a long time. we use keventd instead, since |
| 693 | * long blocking there is the exception, not the rule. accordingly, HCDs |
| 694 | * talking to TTs must queue control transfers (not just bulk and iso), so |
| 695 | * both can talk to the same hub concurrently. |
| 696 | */ |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 697 | static void hub_tt_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 699 | struct usb_hub *hub = |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 700 | container_of(work, struct usb_hub, tt.clear_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | unsigned long flags; |
| 702 | |
| 703 | spin_lock_irqsave (&hub->tt.lock, flags); |
Octavian Purdila | 3b6054d | 2012-10-01 22:21:12 +0300 | [diff] [blame] | 704 | while (!list_empty(&hub->tt.clear_list)) { |
H Hartley Sweeten | d0f830d | 2009-04-22 16:03:05 -0400 | [diff] [blame] | 705 | struct list_head *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | struct usb_tt_clear *clear; |
| 707 | struct usb_device *hdev = hub->hdev; |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 708 | const struct hc_driver *drv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | int status; |
| 710 | |
H Hartley Sweeten | d0f830d | 2009-04-22 16:03:05 -0400 | [diff] [blame] | 711 | next = hub->tt.clear_list.next; |
| 712 | clear = list_entry (next, struct usb_tt_clear, clear_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | list_del (&clear->clear_list); |
| 714 | |
| 715 | /* drop lock so HCD can concurrently report other TT errors */ |
| 716 | spin_unlock_irqrestore (&hub->tt.lock, flags); |
| 717 | status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 718 | if (status && status != -ENODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | dev_err (&hdev->dev, |
| 720 | "clear tt %d (%04x) error %d\n", |
| 721 | clear->tt, clear->devinfo, status); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 722 | |
| 723 | /* Tell the HCD, even if the operation failed */ |
| 724 | drv = clear->hcd->driver; |
| 725 | if (drv->clear_tt_buffer_complete) |
| 726 | (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); |
| 727 | |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 728 | kfree(clear); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 729 | spin_lock_irqsave(&hub->tt.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | } |
| 731 | spin_unlock_irqrestore (&hub->tt.lock, flags); |
| 732 | } |
| 733 | |
| 734 | /** |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 735 | * usb_hub_set_port_power - control hub port's power state |
Mathias Nyman | 41341261 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 736 | * @hdev: USB device belonging to the usb hub |
| 737 | * @hub: target hub |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 738 | * @port1: port index |
| 739 | * @set: expected status |
| 740 | * |
| 741 | * call this function to control port's power via setting or |
| 742 | * clearing the port's PORT_POWER feature. |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 743 | * |
| 744 | * Return: 0 if successful. A negative error code otherwise. |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 745 | */ |
Mathias Nyman | 41341261 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 746 | int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, |
| 747 | int port1, bool set) |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 748 | { |
| 749 | int ret; |
| 750 | |
| 751 | if (set) |
| 752 | ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
| 753 | else |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 754 | ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
| 755 | |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 756 | if (ret) |
| 757 | return ret; |
| 758 | |
| 759 | if (set) |
| 760 | set_bit(port1, hub->power_bits); |
| 761 | else |
| 762 | clear_bit(port1, hub->power_bits); |
| 763 | return 0; |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /** |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 767 | * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub |
| 768 | * @urb: an URB associated with the failed or incomplete split transaction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | * |
| 770 | * High speed HCDs use this to tell the hub driver that some split control or |
| 771 | * bulk transaction failed in a way that requires clearing internal state of |
| 772 | * a transaction translator. This is normally detected (and reported) from |
| 773 | * interrupt context. |
| 774 | * |
| 775 | * It may not be possible for that hub to handle additional full (or low) |
| 776 | * speed transactions until that state is fully cleared out. |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 777 | * |
| 778 | * Return: 0 if successful. A negative error code otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | */ |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 780 | int usb_hub_clear_tt_buffer(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 782 | struct usb_device *udev = urb->dev; |
| 783 | int pipe = urb->pipe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | struct usb_tt *tt = udev->tt; |
| 785 | unsigned long flags; |
| 786 | struct usb_tt_clear *clear; |
| 787 | |
| 788 | /* we've got to cope with an arbitrary number of pending TT clears, |
| 789 | * since each TT has "at least two" buffers that can need it (and |
| 790 | * there can be many TTs per hub). even if they're uncommon. |
| 791 | */ |
Christoph Lameter | 54e6ecb | 2006-12-06 20:33:16 -0800 | [diff] [blame] | 792 | if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); |
| 794 | /* FIXME recover somehow ... RESET_TT? */ |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 795 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /* info that CLEAR_TT_BUFFER needs */ |
| 799 | clear->tt = tt->multi ? udev->ttport : 1; |
| 800 | clear->devinfo = usb_pipeendpoint (pipe); |
| 801 | clear->devinfo |= udev->devnum << 4; |
| 802 | clear->devinfo |= usb_pipecontrol (pipe) |
| 803 | ? (USB_ENDPOINT_XFER_CONTROL << 11) |
| 804 | : (USB_ENDPOINT_XFER_BULK << 11); |
| 805 | if (usb_pipein (pipe)) |
| 806 | clear->devinfo |= 1 << 15; |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 807 | |
| 808 | /* info for completion callback */ |
| 809 | clear->hcd = bus_to_hcd(udev->bus); |
| 810 | clear->ep = urb->ep; |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | /* tell keventd to clear state for this TT */ |
| 813 | spin_lock_irqsave (&tt->lock, flags); |
| 814 | list_add_tail (&clear->clear_list, &tt->clear_list); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 815 | schedule_work(&tt->clear_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | spin_unlock_irqrestore (&tt->lock, flags); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 817 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 819 | EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 821 | static void hub_power_on(struct usb_hub *hub, bool do_delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | { |
| 823 | int port1; |
| 824 | |
Alan Stern | 4489a57 | 2006-04-27 15:54:22 -0400 | [diff] [blame] | 825 | /* Enable power on each port. Some hubs have reserved values |
| 826 | * of LPSM (> 2) in their descriptors, even though they are |
| 827 | * USB 2.0 hubs. Some hubs do not implement port-power switching |
| 828 | * but only emulate it. In all cases, the ports won't work |
| 829 | * unless we send these messages to the hub. |
| 830 | */ |
Dan Williams | 9262c19 | 2014-05-20 18:08:12 -0700 | [diff] [blame] | 831 | if (hub_is_port_power_switchable(hub)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | dev_dbg(hub->intfdev, "enabling power on all ports\n"); |
Alan Stern | 4489a57 | 2006-04-27 15:54:22 -0400 | [diff] [blame] | 833 | else |
| 834 | dev_dbg(hub->intfdev, "trying to enable port power on " |
| 835 | "non-switchable hub\n"); |
Krzysztof Mazur | 3bbc47d | 2013-08-22 14:49:40 +0200 | [diff] [blame] | 836 | for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 837 | if (test_bit(port1, hub->power_bits)) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 838 | set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); |
| 839 | else |
| 840 | usb_clear_port_feature(hub->hdev, port1, |
| 841 | USB_PORT_FEAT_POWER); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 842 | if (do_delay) |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 843 | msleep(hub_power_on_good_delay(hub)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | static int hub_hub_status(struct usb_hub *hub, |
| 847 | u16 *status, u16 *change) |
| 848 | { |
| 849 | int ret; |
| 850 | |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 851 | mutex_lock(&hub->status_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | ret = get_hub_status(hub->hdev, &hub->status->hub); |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 853 | if (ret < 0) { |
| 854 | if (ret != -ENODEV) |
| 855 | dev_err(hub->intfdev, |
| 856 | "%s failed (err = %d)\n", __func__, ret); |
| 857 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | *status = le16_to_cpu(hub->status->hub.wHubStatus); |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 859 | *change = le16_to_cpu(hub->status->hub.wHubChange); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | ret = 0; |
| 861 | } |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 862 | mutex_unlock(&hub->status_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return ret; |
| 864 | } |
| 865 | |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 866 | static int hub_set_port_link_state(struct usb_hub *hub, int port1, |
| 867 | unsigned int link_status) |
| 868 | { |
| 869 | return set_port_feature(hub->hdev, |
| 870 | port1 | (link_status << 3), |
| 871 | USB_PORT_FEAT_LINK_STATE); |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * If USB 3.0 ports are placed into the Disabled state, they will no longer |
| 876 | * detect any device connects or disconnects. This is generally not what the |
| 877 | * USB core wants, since it expects a disabled port to produce a port status |
| 878 | * change event when a new device connects. |
| 879 | * |
| 880 | * Instead, set the link state to Disabled, wait for the link to settle into |
| 881 | * that state, clear any change bits, and then put the port into the RxDetect |
| 882 | * state. |
| 883 | */ |
| 884 | static int hub_usb3_port_disable(struct usb_hub *hub, int port1) |
| 885 | { |
| 886 | int ret; |
| 887 | int total_time; |
| 888 | u16 portchange, portstatus; |
| 889 | |
| 890 | if (!hub_is_superspeed(hub->hdev)) |
| 891 | return -EINVAL; |
| 892 | |
Gavin Guo | bb86cf5 | 2014-07-18 01:12:13 +0800 | [diff] [blame] | 893 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
| 894 | if (ret < 0) |
| 895 | return ret; |
| 896 | |
| 897 | /* |
| 898 | * USB controller Advanced Micro Devices, Inc. [AMD] FCH USB XHCI |
| 899 | * Controller [1022:7814] will have spurious result making the following |
| 900 | * usb 3.0 device hotplugging route to the 2.0 root hub and recognized |
| 901 | * as high-speed device if we set the usb 3.0 port link state to |
| 902 | * Disabled. Since it's already in USB_SS_PORT_LS_RX_DETECT state, we |
| 903 | * check the state here to avoid the bug. |
| 904 | */ |
| 905 | if ((portstatus & USB_PORT_STAT_LINK_STATE) == |
| 906 | USB_SS_PORT_LS_RX_DETECT) { |
| 907 | dev_dbg(&hub->ports[port1 - 1]->dev, |
| 908 | "Not disabling port; link state is RxDetect\n"); |
| 909 | return ret; |
| 910 | } |
| 911 | |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 912 | ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED); |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 913 | if (ret) |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 914 | return ret; |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 915 | |
| 916 | /* Wait for the link to enter the disabled state. */ |
| 917 | for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { |
| 918 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
| 919 | if (ret < 0) |
| 920 | return ret; |
| 921 | |
| 922 | if ((portstatus & USB_PORT_STAT_LINK_STATE) == |
| 923 | USB_SS_PORT_LS_SS_DISABLED) |
| 924 | break; |
| 925 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) |
| 926 | break; |
| 927 | msleep(HUB_DEBOUNCE_STEP); |
| 928 | } |
| 929 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 930 | dev_warn(&hub->ports[port1 - 1]->dev, |
| 931 | "Could not disable after %d ms\n", total_time); |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 932 | |
| 933 | return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT); |
| 934 | } |
| 935 | |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 936 | static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) |
| 937 | { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 938 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 939 | struct usb_device *hdev = hub->hdev; |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 940 | int ret = 0; |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 941 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 942 | if (port_dev->child && set_state) |
| 943 | usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 944 | if (!hub->error) { |
| 945 | if (hub_is_superspeed(hub->hdev)) |
| 946 | ret = hub_usb3_port_disable(hub, port1); |
| 947 | else |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 948 | ret = usb_clear_port_feature(hdev, port1, |
Sarah Sharp | 41e7e05 | 2012-11-14 16:42:32 -0800 | [diff] [blame] | 949 | USB_PORT_FEAT_ENABLE); |
| 950 | } |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 951 | if (ret && ret != -ENODEV) |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 952 | dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); |
Alan Stern | 8b28c75 | 2005-08-10 17:04:13 -0400 | [diff] [blame] | 953 | return ret; |
| 954 | } |
| 955 | |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 956 | /* |
Justin P. Mattock | 6d42fcd | 2011-02-26 20:33:56 -0800 | [diff] [blame] | 957 | * Disable a port and mark a logical connect-change event, so that some |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 958 | * time later khubd will disconnect() any existing usb_device on the port |
| 959 | * and will re-enumerate if there actually is a device attached. |
| 960 | */ |
| 961 | static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) |
| 962 | { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 963 | dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n"); |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 964 | hub_port_disable(hub, port1, 1); |
| 965 | |
| 966 | /* FIXME let caller ask to power down the port: |
| 967 | * - some devices won't enumerate without a VBUS power cycle |
| 968 | * - SRP saves power that way |
| 969 | * - ... new call, TBD ... |
| 970 | * That's easy if this hub can switch power per-port, and |
| 971 | * khubd reactivates the port later (timer, SRP, etc). |
| 972 | * Powerdown must be optional, because of reset/DFU. |
| 973 | */ |
| 974 | |
| 975 | set_bit(port1, hub->change_bits); |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 976 | kick_khubd(hub); |
Alan Stern | 0458d5b | 2007-05-04 11:52:20 -0400 | [diff] [blame] | 977 | } |
| 978 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 979 | /** |
| 980 | * usb_remove_device - disable a device's port on its parent hub |
| 981 | * @udev: device to be disabled and removed |
| 982 | * Context: @udev locked, must be able to sleep. |
| 983 | * |
| 984 | * After @udev's port has been disabled, khubd is notified and it will |
| 985 | * see that the device has been disconnected. When the device is |
| 986 | * physically unplugged and something is plugged in, the events will |
| 987 | * be received and processed normally. |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 988 | * |
| 989 | * Return: 0 if successful. A negative error code otherwise. |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 990 | */ |
| 991 | int usb_remove_device(struct usb_device *udev) |
| 992 | { |
| 993 | struct usb_hub *hub; |
| 994 | struct usb_interface *intf; |
| 995 | |
| 996 | if (!udev->parent) /* Can't remove a root hub */ |
| 997 | return -EINVAL; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 998 | hub = usb_hub_to_struct_hub(udev->parent); |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 999 | intf = to_usb_interface(hub->intfdev); |
| 1000 | |
| 1001 | usb_autopm_get_interface(intf); |
| 1002 | set_bit(udev->portnum, hub->removed_bits); |
| 1003 | hub_port_logical_disconnect(hub, udev->portnum); |
| 1004 | usb_autopm_put_interface(intf); |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1008 | enum hub_activation_type { |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 1009 | HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1010 | HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1011 | }; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1012 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1013 | static void hub_init_func2(struct work_struct *ws); |
| 1014 | static void hub_init_func3(struct work_struct *ws); |
| 1015 | |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1016 | static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1017 | { |
| 1018 | struct usb_device *hdev = hub->hdev; |
Sarah Sharp | 653a39d | 2010-12-23 11:12:42 -0800 | [diff] [blame] | 1019 | struct usb_hcd *hcd; |
| 1020 | int ret; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1021 | int port1; |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1022 | int status; |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1023 | bool need_debounce_delay = false; |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1024 | unsigned delay; |
| 1025 | |
| 1026 | /* Continue a partial initialization */ |
| 1027 | if (type == HUB_INIT2) |
| 1028 | goto init2; |
| 1029 | if (type == HUB_INIT3) |
| 1030 | goto init3; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1031 | |
Elric Fu | a45aa3b | 2012-02-18 13:32:27 +0800 | [diff] [blame] | 1032 | /* The superspeed hub except for root hub has to use Hub Depth |
| 1033 | * value as an offset into the route string to locate the bits |
| 1034 | * it uses to determine the downstream port number. So hub driver |
| 1035 | * should send a set hub depth request to superspeed hub after |
| 1036 | * the superspeed hub is set configuration in initialization or |
| 1037 | * reset procedure. |
| 1038 | * |
| 1039 | * After a resume, port power should still be on. |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1040 | * For any other type of activation, turn it on. |
| 1041 | */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1042 | if (type != HUB_RESUME) { |
Elric Fu | a45aa3b | 2012-02-18 13:32:27 +0800 | [diff] [blame] | 1043 | if (hdev->parent && hub_is_superspeed(hdev)) { |
| 1044 | ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
| 1045 | HUB_SET_DEPTH, USB_RT_HUB, |
| 1046 | hdev->level - 1, 0, NULL, 0, |
| 1047 | USB_CTRL_SET_TIMEOUT); |
| 1048 | if (ret < 0) |
| 1049 | dev_err(hub->intfdev, |
| 1050 | "set hub depth failed\n"); |
| 1051 | } |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1052 | |
| 1053 | /* Speed up system boot by using a delayed_work for the |
| 1054 | * hub's initial power-up delays. This is pretty awkward |
| 1055 | * and the implementation looks like a home-brewed sort of |
| 1056 | * setjmp/longjmp, but it saves at least 100 ms for each |
| 1057 | * root hub (assuming usbcore is compiled into the kernel |
| 1058 | * rather than as a module). It adds up. |
| 1059 | * |
| 1060 | * This can't be done for HUB_RESUME or HUB_RESET_RESUME |
| 1061 | * because for those activation types the ports have to be |
| 1062 | * operational when we return. In theory this could be done |
| 1063 | * for HUB_POST_RESET, but it's easier not to. |
| 1064 | */ |
| 1065 | if (type == HUB_INIT) { |
Dan Williams | 7ad3c47 | 2014-05-20 18:08:57 -0700 | [diff] [blame] | 1066 | unsigned delay = hub_power_on_good_delay(hub); |
| 1067 | |
| 1068 | hub_power_on(hub, false); |
Tejun Heo | 77fa83c | 2014-03-07 10:24:48 -0500 | [diff] [blame] | 1069 | INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); |
Shaibal Dutta | 22f6a0f | 2014-02-01 19:16:46 -0800 | [diff] [blame] | 1070 | queue_delayed_work(system_power_efficient_wq, |
| 1071 | &hub->init_work, |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1072 | msecs_to_jiffies(delay)); |
Alan Stern | 61fbeba | 2008-10-27 12:07:44 -0400 | [diff] [blame] | 1073 | |
| 1074 | /* Suppress autosuspend until init is done */ |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 1075 | usb_autopm_get_interface_no_resume( |
| 1076 | to_usb_interface(hub->intfdev)); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1077 | return; /* Continues at init2: below */ |
Sarah Sharp | 653a39d | 2010-12-23 11:12:42 -0800 | [diff] [blame] | 1078 | } else if (type == HUB_RESET_RESUME) { |
| 1079 | /* The internal host controller state for the hub device |
| 1080 | * may be gone after a host power loss on system resume. |
| 1081 | * Update the device's info so the HW knows it's a hub. |
| 1082 | */ |
| 1083 | hcd = bus_to_hcd(hdev->bus); |
| 1084 | if (hcd->driver->update_hub_device) { |
| 1085 | ret = hcd->driver->update_hub_device(hcd, hdev, |
| 1086 | &hub->tt, GFP_NOIO); |
| 1087 | if (ret < 0) { |
| 1088 | dev_err(hub->intfdev, "Host not " |
| 1089 | "accepting hub info " |
| 1090 | "update.\n"); |
| 1091 | dev_err(hub->intfdev, "LS/FS devices " |
| 1092 | "and hubs may not work " |
| 1093 | "under this hub\n."); |
| 1094 | } |
| 1095 | } |
| 1096 | hub_power_on(hub, true); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1097 | } else { |
| 1098 | hub_power_on(hub, true); |
| 1099 | } |
| 1100 | } |
| 1101 | init2: |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1102 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 1103 | /* |
| 1104 | * Check each port and set hub->change_bits to let khubd know |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1105 | * which ports need attention. |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1106 | */ |
| 1107 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 1108 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 1109 | struct usb_device *udev = port_dev->child; |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1110 | u16 portstatus, portchange; |
| 1111 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1112 | portstatus = portchange = 0; |
| 1113 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
| 1114 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 1115 | dev_dbg(&port_dev->dev, "status %04x change %04x\n", |
| 1116 | portstatus, portchange); |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1117 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 1118 | /* |
| 1119 | * After anything other than HUB_RESUME (i.e., initialization |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1120 | * or any sort of reset), every port should be disabled. |
| 1121 | * Unconnected ports should likewise be disabled (paranoia), |
| 1122 | * and so should ports for which we have no usb_device. |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1123 | */ |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1124 | if ((portstatus & USB_PORT_STAT_ENABLE) && ( |
| 1125 | type != HUB_RESUME || |
| 1126 | !(portstatus & USB_PORT_STAT_CONNECTION) || |
| 1127 | !udev || |
| 1128 | udev->state == USB_STATE_NOTATTACHED)) { |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1129 | /* |
| 1130 | * USB3 protocol ports will automatically transition |
| 1131 | * to Enabled state when detect an USB3.0 device attach. |
Dan Williams | fd1ac4c | 2013-10-07 11:58:20 -0700 | [diff] [blame] | 1132 | * Do not disable USB3 protocol ports, just pretend |
| 1133 | * power was lost |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1134 | */ |
Dan Williams | fd1ac4c | 2013-10-07 11:58:20 -0700 | [diff] [blame] | 1135 | portstatus &= ~USB_PORT_STAT_ENABLE; |
| 1136 | if (!hub_is_superspeed(hdev)) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1137 | usb_clear_port_feature(hdev, port1, |
Andiry Xu | 9f0a6cd | 2010-05-07 18:09:27 +0800 | [diff] [blame] | 1138 | USB_PORT_FEAT_ENABLE); |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1139 | } |
| 1140 | |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1141 | /* Clear status-change flags; we'll debounce later */ |
| 1142 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
| 1143 | need_debounce_delay = true; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1144 | usb_clear_port_feature(hub->hdev, port1, |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1145 | USB_PORT_FEAT_C_CONNECTION); |
| 1146 | } |
| 1147 | if (portchange & USB_PORT_STAT_C_ENABLE) { |
| 1148 | need_debounce_delay = true; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1149 | usb_clear_port_feature(hub->hdev, port1, |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1150 | USB_PORT_FEAT_C_ENABLE); |
| 1151 | } |
Julius Werner | e92aee3 | 2013-10-15 17:45:00 -0700 | [diff] [blame] | 1152 | if (portchange & USB_PORT_STAT_C_RESET) { |
| 1153 | need_debounce_delay = true; |
| 1154 | usb_clear_port_feature(hub->hdev, port1, |
| 1155 | USB_PORT_FEAT_C_RESET); |
| 1156 | } |
Don Zickus | 79c3dd8 | 2011-11-03 09:07:18 -0400 | [diff] [blame] | 1157 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
| 1158 | hub_is_superspeed(hub->hdev)) { |
| 1159 | need_debounce_delay = true; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1160 | usb_clear_port_feature(hub->hdev, port1, |
Don Zickus | 79c3dd8 | 2011-11-03 09:07:18 -0400 | [diff] [blame] | 1161 | USB_PORT_FEAT_C_BH_PORT_RESET); |
| 1162 | } |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 1163 | /* We can forget about a "removed" device when there's a |
| 1164 | * physical disconnect or the connect status changes. |
| 1165 | */ |
| 1166 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || |
| 1167 | (portchange & USB_PORT_STAT_C_CONNECTION)) |
| 1168 | clear_bit(port1, hub->removed_bits); |
| 1169 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1170 | if (!udev || udev->state == USB_STATE_NOTATTACHED) { |
| 1171 | /* Tell khubd to disconnect the device or |
| 1172 | * check for a new connection |
| 1173 | */ |
Shen Guang | 08d1dec | 2014-01-08 14:45:42 +0800 | [diff] [blame] | 1174 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION) || |
| 1175 | (portstatus & USB_PORT_STAT_OVERCURRENT)) |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1176 | set_bit(port1, hub->change_bits); |
| 1177 | |
| 1178 | } else if (portstatus & USB_PORT_STAT_ENABLE) { |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 1179 | bool port_resumed = (portstatus & |
| 1180 | USB_PORT_STAT_LINK_STATE) == |
| 1181 | USB_SS_PORT_LS_U0; |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1182 | /* The power session apparently survived the resume. |
| 1183 | * If there was an overcurrent or suspend change |
| 1184 | * (i.e., remote wakeup request), have khubd |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 1185 | * take care of it. Look at the port link state |
| 1186 | * for USB 3.0 hubs, since they don't have a suspend |
| 1187 | * change bit, and they don't set the port link change |
| 1188 | * bit on device-initiated resume. |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1189 | */ |
Sarah Sharp | 72937e1 | 2012-01-24 11:46:50 -0800 | [diff] [blame] | 1190 | if (portchange || (hub_is_superspeed(hub->hdev) && |
| 1191 | port_resumed)) |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1192 | set_bit(port1, hub->change_bits); |
| 1193 | |
| 1194 | } else if (udev->persist_enabled) { |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1195 | #ifdef CONFIG_PM |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1196 | udev->reset_resume = 1; |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1197 | #endif |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1198 | /* Don't set the change_bits when the device |
| 1199 | * was powered off. |
| 1200 | */ |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 1201 | if (test_bit(port1, hub->power_bits)) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1202 | set_bit(port1, hub->change_bits); |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 1203 | |
Alan Stern | 6ee0b27 | 2008-04-28 11:06:42 -0400 | [diff] [blame] | 1204 | } else { |
| 1205 | /* The power session is gone; tell khubd */ |
| 1206 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); |
| 1207 | set_bit(port1, hub->change_bits); |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1208 | } |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1209 | } |
| 1210 | |
Alan Stern | 948fea3 | 2008-04-28 11:07:07 -0400 | [diff] [blame] | 1211 | /* If no port-status-change flags were set, we don't need any |
| 1212 | * debouncing. If flags were set we can try to debounce the |
| 1213 | * ports all at once right now, instead of letting khubd do them |
| 1214 | * one at a time later on. |
| 1215 | * |
| 1216 | * If any port-status changes do occur during this delay, khubd |
| 1217 | * will see them later and handle them normally. |
| 1218 | */ |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1219 | if (need_debounce_delay) { |
| 1220 | delay = HUB_DEBOUNCE_STABLE; |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1221 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1222 | /* Don't do a long sleep inside a workqueue routine */ |
| 1223 | if (type == HUB_INIT2) { |
Tejun Heo | 77fa83c | 2014-03-07 10:24:48 -0500 | [diff] [blame] | 1224 | INIT_DELAYED_WORK(&hub->init_work, hub_init_func3); |
Shaibal Dutta | 22f6a0f | 2014-02-01 19:16:46 -0800 | [diff] [blame] | 1225 | queue_delayed_work(system_power_efficient_wq, |
| 1226 | &hub->init_work, |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1227 | msecs_to_jiffies(delay)); |
| 1228 | return; /* Continues at init3: below */ |
| 1229 | } else { |
| 1230 | msleep(delay); |
| 1231 | } |
| 1232 | } |
| 1233 | init3: |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1234 | hub->quiescing = 0; |
| 1235 | |
| 1236 | status = usb_submit_urb(hub->urb, GFP_NOIO); |
| 1237 | if (status < 0) |
| 1238 | dev_err(hub->intfdev, "activate --> %d\n", status); |
| 1239 | if (hub->has_indicators && blinkenlights) |
Shaibal Dutta | 22f6a0f | 2014-02-01 19:16:46 -0800 | [diff] [blame] | 1240 | queue_delayed_work(system_power_efficient_wq, |
| 1241 | &hub->leds, LED_CYCLE_PERIOD); |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1242 | |
| 1243 | /* Scan all ports that need attention */ |
| 1244 | kick_khubd(hub); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 1245 | |
| 1246 | /* Allow autosuspend if it was suppressed */ |
| 1247 | if (type <= HUB_INIT3) |
| 1248 | usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 1249 | } |
| 1250 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1251 | /* Implement the continuations for the delays above */ |
| 1252 | static void hub_init_func2(struct work_struct *ws) |
| 1253 | { |
| 1254 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); |
| 1255 | |
| 1256 | hub_activate(hub, HUB_INIT2); |
| 1257 | } |
| 1258 | |
| 1259 | static void hub_init_func3(struct work_struct *ws) |
| 1260 | { |
| 1261 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); |
| 1262 | |
| 1263 | hub_activate(hub, HUB_INIT3); |
| 1264 | } |
| 1265 | |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1266 | enum hub_quiescing_type { |
| 1267 | HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND |
| 1268 | }; |
| 1269 | |
| 1270 | static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) |
| 1271 | { |
| 1272 | struct usb_device *hdev = hub->hdev; |
| 1273 | int i; |
| 1274 | |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1275 | cancel_delayed_work_sync(&hub->init_work); |
| 1276 | |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1277 | /* khubd and related activity won't re-trigger */ |
| 1278 | hub->quiescing = 1; |
| 1279 | |
| 1280 | if (type != HUB_SUSPEND) { |
| 1281 | /* Disconnect all the children */ |
| 1282 | for (i = 0; i < hdev->maxchild; ++i) { |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 1283 | if (hub->ports[i]->child) |
| 1284 | usb_disconnect(&hub->ports[i]->child); |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | /* Stop khubd and related activity */ |
| 1289 | usb_kill_urb(hub->urb); |
| 1290 | if (hub->has_indicators) |
| 1291 | cancel_delayed_work_sync(&hub->leds); |
| 1292 | if (hub->tt.hub) |
Octavian Purdila | 036546b | 2012-10-23 11:33:12 +0300 | [diff] [blame] | 1293 | flush_work(&hub->tt.clear_work); |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1294 | } |
| 1295 | |
Alan Stern | 600856c | 2014-05-20 18:08:07 -0700 | [diff] [blame] | 1296 | static void hub_pm_barrier_for_all_ports(struct usb_hub *hub) |
| 1297 | { |
| 1298 | int i; |
| 1299 | |
| 1300 | for (i = 0; i < hub->hdev->maxchild; ++i) |
| 1301 | pm_runtime_barrier(&hub->ports[i]->dev); |
| 1302 | } |
| 1303 | |
Alan Stern | 3eb1491 | 2008-03-03 15:15:43 -0500 | [diff] [blame] | 1304 | /* caller has locked the hub device */ |
| 1305 | static int hub_pre_reset(struct usb_interface *intf) |
| 1306 | { |
| 1307 | struct usb_hub *hub = usb_get_intfdata(intf); |
| 1308 | |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1309 | hub_quiesce(hub, HUB_PRE_RESET); |
Alan Stern | 600856c | 2014-05-20 18:08:07 -0700 | [diff] [blame] | 1310 | hub->in_reset = 1; |
| 1311 | hub_pm_barrier_for_all_ports(hub); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1312 | return 0; |
Alan Stern | 7d069b7 | 2005-11-18 12:06:34 -0500 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | /* caller has locked the hub device */ |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1316 | static int hub_post_reset(struct usb_interface *intf) |
Alan Stern | 7d069b7 | 2005-11-18 12:06:34 -0500 | [diff] [blame] | 1317 | { |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 1318 | struct usb_hub *hub = usb_get_intfdata(intf); |
| 1319 | |
Alan Stern | 600856c | 2014-05-20 18:08:07 -0700 | [diff] [blame] | 1320 | hub->in_reset = 0; |
| 1321 | hub_pm_barrier_for_all_ports(hub); |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1322 | hub_activate(hub, HUB_POST_RESET); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 1323 | return 0; |
Alan Stern | 7d069b7 | 2005-11-18 12:06:34 -0500 | [diff] [blame] | 1324 | } |
| 1325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | static int hub_configure(struct usb_hub *hub, |
| 1327 | struct usb_endpoint_descriptor *endpoint) |
| 1328 | { |
Sarah Sharp | b356b7c | 2009-09-04 10:53:24 -0700 | [diff] [blame] | 1329 | struct usb_hcd *hcd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | struct usb_device *hdev = hub->hdev; |
| 1331 | struct device *hub_dev = hub->intfdev; |
| 1332 | u16 hubstatus, hubchange; |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1333 | u16 wHubCharacteristics; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | unsigned int pipe; |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1335 | int maxp, ret, i; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1336 | char *message = "out of memory"; |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1337 | unsigned unit_load; |
| 1338 | unsigned full_load; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1339 | unsigned maxchild; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1340 | |
Alan Stern | d697cdd | 2009-10-27 15:18:46 -0400 | [diff] [blame] | 1341 | hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | if (!hub->buffer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | ret = -ENOMEM; |
| 1344 | goto fail; |
| 1345 | } |
| 1346 | |
| 1347 | hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); |
| 1348 | if (!hub->status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | ret = -ENOMEM; |
| 1350 | goto fail; |
| 1351 | } |
Alan Stern | db90e7a | 2007-02-05 09:56:15 -0500 | [diff] [blame] | 1352 | mutex_init(&hub->status_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | |
| 1354 | hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); |
| 1355 | if (!hub->descriptor) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | ret = -ENOMEM; |
| 1357 | goto fail; |
| 1358 | } |
| 1359 | |
| 1360 | /* Request the entire hub descriptor. |
| 1361 | * hub->descriptor can handle USB_MAXCHILDREN ports, |
| 1362 | * but the hub can/will return fewer bytes here. |
| 1363 | */ |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1364 | ret = get_hub_descriptor(hdev, hub->descriptor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | if (ret < 0) { |
| 1366 | message = "can't read hub descriptor"; |
| 1367 | goto fail; |
| 1368 | } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { |
| 1369 | message = "hub has too many ports!"; |
| 1370 | ret = -ENODEV; |
| 1371 | goto fail; |
David Linares | 769d7368 | 2013-03-25 10:50:27 +0000 | [diff] [blame] | 1372 | } else if (hub->descriptor->bNbrPorts == 0) { |
| 1373 | message = "hub doesn't have any ports!"; |
| 1374 | ret = -ENODEV; |
| 1375 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1378 | maxchild = hub->descriptor->bNbrPorts; |
| 1379 | dev_info(hub_dev, "%d port%s detected\n", maxchild, |
| 1380 | (maxchild == 1) ? "" : "s"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1382 | hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL); |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 1383 | if (!hub->ports) { |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1384 | ret = -ENOMEM; |
| 1385 | goto fail; |
| 1386 | } |
| 1387 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1388 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1389 | if (hub_is_superspeed(hdev)) { |
| 1390 | unit_load = 150; |
| 1391 | full_load = 900; |
| 1392 | } else { |
| 1393 | unit_load = 100; |
| 1394 | full_load = 500; |
| 1395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1397 | /* FIXME for USB 3.0, skip for now */ |
| 1398 | if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && |
| 1399 | !(hub_is_superspeed(hdev))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | int i; |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 1401 | char portstr[USB_MAXCHILDREN + 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1403 | for (i = 0; i < maxchild; i++) |
John Youn | dbe79bb | 2001-09-17 00:00:00 -0700 | [diff] [blame] | 1404 | portstr[i] = hub->descriptor->u.hs.DeviceRemovable |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | [((i + 1) / 8)] & (1 << ((i + 1) % 8)) |
| 1406 | ? 'F' : 'R'; |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1407 | portstr[maxchild] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); |
| 1409 | } else |
| 1410 | dev_dbg(hub_dev, "standalone hub\n"); |
| 1411 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1412 | switch (wHubCharacteristics & HUB_CHAR_LPSM) { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 1413 | case HUB_CHAR_COMMON_LPSM: |
| 1414 | dev_dbg(hub_dev, "ganged power switching\n"); |
| 1415 | break; |
| 1416 | case HUB_CHAR_INDV_PORT_LPSM: |
| 1417 | dev_dbg(hub_dev, "individual port power switching\n"); |
| 1418 | break; |
| 1419 | case HUB_CHAR_NO_LPSM: |
| 1420 | case HUB_CHAR_LPSM: |
| 1421 | dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); |
| 1422 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1425 | switch (wHubCharacteristics & HUB_CHAR_OCPM) { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 1426 | case HUB_CHAR_COMMON_OCPM: |
| 1427 | dev_dbg(hub_dev, "global over-current protection\n"); |
| 1428 | break; |
| 1429 | case HUB_CHAR_INDV_PORT_OCPM: |
| 1430 | dev_dbg(hub_dev, "individual port over-current protection\n"); |
| 1431 | break; |
| 1432 | case HUB_CHAR_NO_OCPM: |
| 1433 | case HUB_CHAR_OCPM: |
| 1434 | dev_dbg(hub_dev, "no over-current protection\n"); |
| 1435 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | spin_lock_init (&hub->tt.lock); |
| 1439 | INIT_LIST_HEAD (&hub->tt.clear_list); |
Alan Stern | cb88a1b | 2009-06-29 10:43:32 -0400 | [diff] [blame] | 1440 | INIT_WORK(&hub->tt.clear_work, hub_tt_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | switch (hdev->descriptor.bDeviceProtocol) { |
Aman Deep | 7bf0118 | 2011-12-08 12:05:22 +0530 | [diff] [blame] | 1442 | case USB_HUB_PR_FS: |
| 1443 | break; |
| 1444 | case USB_HUB_PR_HS_SINGLE_TT: |
| 1445 | dev_dbg(hub_dev, "Single TT\n"); |
| 1446 | hub->tt.hub = hdev; |
| 1447 | break; |
| 1448 | case USB_HUB_PR_HS_MULTI_TT: |
| 1449 | ret = usb_set_interface(hdev, 0, 1); |
| 1450 | if (ret == 0) { |
| 1451 | dev_dbg(hub_dev, "TT per port\n"); |
| 1452 | hub->tt.multi = 1; |
| 1453 | } else |
| 1454 | dev_err(hub_dev, "Using single TT (err %d)\n", |
| 1455 | ret); |
| 1456 | hub->tt.hub = hdev; |
| 1457 | break; |
| 1458 | case USB_HUB_PR_SS: |
| 1459 | /* USB 3.0 hubs don't have a TT */ |
| 1460 | break; |
| 1461 | default: |
| 1462 | dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", |
| 1463 | hdev->descriptor.bDeviceProtocol); |
| 1464 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1467 | /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1468 | switch (wHubCharacteristics & HUB_CHAR_TTTT) { |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 1469 | case HUB_TTTT_8_BITS: |
| 1470 | if (hdev->descriptor.bDeviceProtocol != 0) { |
| 1471 | hub->tt.think_time = 666; |
david-b@pacbell.net | e09711a | 2005-08-13 18:41:04 -0700 | [diff] [blame] | 1472 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1473 | "FS bit times (%d ns)\n", |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 1474 | 8, hub->tt.think_time); |
| 1475 | } |
| 1476 | break; |
| 1477 | case HUB_TTTT_16_BITS: |
| 1478 | hub->tt.think_time = 666 * 2; |
| 1479 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1480 | "FS bit times (%d ns)\n", |
| 1481 | 16, hub->tt.think_time); |
| 1482 | break; |
| 1483 | case HUB_TTTT_24_BITS: |
| 1484 | hub->tt.think_time = 666 * 3; |
| 1485 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1486 | "FS bit times (%d ns)\n", |
| 1487 | 24, hub->tt.think_time); |
| 1488 | break; |
| 1489 | case HUB_TTTT_32_BITS: |
| 1490 | hub->tt.think_time = 666 * 4; |
| 1491 | dev_dbg(hub_dev, "TT requires at most %d " |
| 1492 | "FS bit times (%d ns)\n", |
| 1493 | 32, hub->tt.think_time); |
| 1494 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | /* probe() zeroes hub->indicator[] */ |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1498 | if (wHubCharacteristics & HUB_CHAR_PORTIND) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | hub->has_indicators = 1; |
| 1500 | dev_dbg(hub_dev, "Port indicators are supported\n"); |
| 1501 | } |
| 1502 | |
| 1503 | dev_dbg(hub_dev, "power on to power good time: %dms\n", |
| 1504 | hub->descriptor->bPwrOn2PwrGood * 2); |
| 1505 | |
| 1506 | /* power budgeting mostly matters with bus-powered hubs, |
| 1507 | * and battery-powered root hubs (may provide just 8 mA). |
| 1508 | */ |
| 1509 | ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); |
Alan Stern | 15b7336 | 2013-07-30 15:35:40 -0400 | [diff] [blame] | 1510 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | message = "can't get hub status"; |
| 1512 | goto fail; |
| 1513 | } |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1514 | hcd = bus_to_hcd(hdev->bus); |
Alan Stern | 7d35b92 | 2005-04-25 11:18:32 -0400 | [diff] [blame] | 1515 | if (hdev == hdev->bus->root_hub) { |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1516 | if (hcd->power_budget > 0) |
| 1517 | hdev->bus_mA = hcd->power_budget; |
| 1518 | else |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1519 | hdev->bus_mA = full_load * maxchild; |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1520 | if (hdev->bus_mA >= full_load) |
| 1521 | hub->mA_per_port = full_load; |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1522 | else { |
| 1523 | hub->mA_per_port = hdev->bus_mA; |
| 1524 | hub->limited_power = 1; |
| 1525 | } |
Alan Stern | 7d35b92 | 2005-04-25 11:18:32 -0400 | [diff] [blame] | 1526 | } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1527 | int remaining = hdev->bus_mA - |
| 1528 | hub->descriptor->bHubContrCurrent; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", |
| 1531 | hub->descriptor->bHubContrCurrent); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1532 | hub->limited_power = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1534 | if (remaining < maxchild * unit_load) |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1535 | dev_warn(hub_dev, |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1536 | "insufficient power available " |
| 1537 | "to use all downstream ports\n"); |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1538 | hub->mA_per_port = unit_load; /* 7.2.1 */ |
| 1539 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1540 | } else { /* Self-powered external hub */ |
| 1541 | /* FIXME: What about battery-powered external hubs that |
| 1542 | * provide less current per port? */ |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1543 | hub->mA_per_port = full_load; |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1544 | } |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 1545 | if (hub->mA_per_port < full_load) |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 1546 | dev_dbg(hub_dev, "%umA bus power budget for each child\n", |
| 1547 | hub->mA_per_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | |
| 1549 | ret = hub_hub_status(hub, &hubstatus, &hubchange); |
| 1550 | if (ret < 0) { |
| 1551 | message = "can't get hub status"; |
| 1552 | goto fail; |
| 1553 | } |
| 1554 | |
| 1555 | /* local power status reports aren't always correct */ |
| 1556 | if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) |
| 1557 | dev_dbg(hub_dev, "local power source is %s\n", |
| 1558 | (hubstatus & HUB_STATUS_LOCAL_POWER) |
| 1559 | ? "lost (inactive)" : "good"); |
| 1560 | |
Greg Kroah-Hartman | 74ad9bd | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 1561 | if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | dev_dbg(hub_dev, "%sover-current condition exists\n", |
| 1563 | (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); |
| 1564 | |
inaky@linux.intel.com | 88fafff | 2006-10-11 20:05:59 -0700 | [diff] [blame] | 1565 | /* set up the interrupt endpoint |
| 1566 | * We use the EP's maxpacket size instead of (PORTS+1+7)/8 |
| 1567 | * bytes as USB2.0[11.12.3] says because some hubs are known |
| 1568 | * to send more data (and thus cause overflow). For root hubs, |
| 1569 | * maxpktsize is defined in hcd.c's fake endpoint descriptors |
| 1570 | * to be big enough for at least USB_MAXCHILDREN ports. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); |
| 1572 | maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); |
| 1573 | |
| 1574 | if (maxp > sizeof(*hub->buffer)) |
| 1575 | maxp = sizeof(*hub->buffer); |
| 1576 | |
| 1577 | hub->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1578 | if (!hub->urb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | ret = -ENOMEM; |
| 1580 | goto fail; |
| 1581 | } |
| 1582 | |
| 1583 | usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, |
| 1584 | hub, endpoint->bInterval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | |
| 1586 | /* maybe cycle the hub leds */ |
| 1587 | if (hub->has_indicators && blinkenlights) |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 1588 | hub->indicator[0] = INDICATOR_CYCLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1590 | mutex_lock(&usb_port_peer_mutex); |
| 1591 | for (i = 0; i < maxchild; i++) { |
Krzysztof Mazur | e58547e | 2013-08-22 14:49:39 +0200 | [diff] [blame] | 1592 | ret = usb_hub_create_port_device(hub, i + 1); |
| 1593 | if (ret < 0) { |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1594 | dev_err(hub->intfdev, |
| 1595 | "couldn't create port%d device.\n", i + 1); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1596 | break; |
Krzysztof Mazur | e58547e | 2013-08-22 14:49:39 +0200 | [diff] [blame] | 1597 | } |
| 1598 | } |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1599 | hdev->maxchild = i; |
Dan Williams | e3d1050 | 2014-06-17 16:16:32 -0700 | [diff] [blame] | 1600 | for (i = 0; i < hdev->maxchild; i++) { |
| 1601 | struct usb_port *port_dev = hub->ports[i]; |
| 1602 | |
| 1603 | pm_runtime_put(&port_dev->dev); |
| 1604 | } |
| 1605 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1606 | mutex_unlock(&usb_port_peer_mutex); |
| 1607 | if (ret < 0) |
| 1608 | goto fail; |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1609 | |
Dan Williams | e3d9558 | 2014-06-05 14:23:04 -0700 | [diff] [blame] | 1610 | /* Update the HCD's internal representation of this hub before khubd |
| 1611 | * starts getting port status changes for devices under the hub. |
| 1612 | */ |
| 1613 | if (hcd->driver->update_hub_device) { |
| 1614 | ret = hcd->driver->update_hub_device(hcd, hdev, |
| 1615 | &hub->tt, GFP_KERNEL); |
| 1616 | if (ret < 0) { |
| 1617 | message = "can't update HCD hub info"; |
| 1618 | goto fail; |
| 1619 | } |
| 1620 | } |
| 1621 | |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 1622 | usb_hub_adjust_deviceremovable(hdev, hub->descriptor); |
| 1623 | |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 1624 | hub_activate(hub, HUB_INIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | return 0; |
| 1626 | |
| 1627 | fail: |
| 1628 | dev_err (hub_dev, "config failed, %s (err %d)\n", |
| 1629 | message, ret); |
| 1630 | /* hub_disconnect() frees urb and descriptor */ |
| 1631 | return ret; |
| 1632 | } |
| 1633 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1634 | static void hub_release(struct kref *kref) |
| 1635 | { |
| 1636 | struct usb_hub *hub = container_of(kref, struct usb_hub, kref); |
| 1637 | |
| 1638 | usb_put_intf(to_usb_interface(hub->intfdev)); |
| 1639 | kfree(hub); |
| 1640 | } |
| 1641 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | static unsigned highspeed_hubs; |
| 1643 | |
| 1644 | static void hub_disconnect(struct usb_interface *intf) |
| 1645 | { |
Huajun Li | 8816230 | 2012-03-12 21:00:19 +0800 | [diff] [blame] | 1646 | struct usb_hub *hub = usb_get_intfdata(intf); |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 1647 | struct usb_device *hdev = interface_to_usbdev(intf); |
Alan Stern | 543d778 | 2014-01-07 10:43:02 -0500 | [diff] [blame] | 1648 | int port1; |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1649 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1650 | /* Take the hub off the event list and don't let it be added again */ |
| 1651 | spin_lock_irq(&hub_event_lock); |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 1652 | if (!list_empty(&hub->event_list)) { |
| 1653 | list_del_init(&hub->event_list); |
| 1654 | usb_autopm_put_interface_no_suspend(intf); |
| 1655 | } |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1656 | hub->disconnected = 1; |
| 1657 | spin_unlock_irq(&hub_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 1659 | /* Disconnect all children and quiesce the hub */ |
| 1660 | hub->error = 0; |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 1661 | hub_quiesce(hub, HUB_DISCONNECT); |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 1662 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1663 | mutex_lock(&usb_port_peer_mutex); |
| 1664 | |
Alan Stern | 543d778 | 2014-01-07 10:43:02 -0500 | [diff] [blame] | 1665 | /* Avoid races with recursively_mark_NOTATTACHED() */ |
| 1666 | spin_lock_irq(&device_state_lock); |
| 1667 | port1 = hdev->maxchild; |
| 1668 | hdev->maxchild = 0; |
| 1669 | usb_set_intfdata(intf, NULL); |
| 1670 | spin_unlock_irq(&device_state_lock); |
Alexander Shishkin | 1f2235b | 2012-09-12 14:48:31 +0300 | [diff] [blame] | 1671 | |
Alan Stern | 543d778 | 2014-01-07 10:43:02 -0500 | [diff] [blame] | 1672 | for (; port1 > 0; --port1) |
| 1673 | usb_hub_remove_port_device(hub, port1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 1675 | mutex_unlock(&usb_port_peer_mutex); |
| 1676 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1677 | if (hub->hdev->speed == USB_SPEED_HIGH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | highspeed_hubs--; |
| 1679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | usb_free_urb(hub->urb); |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1681 | kfree(hub->ports); |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1682 | kfree(hub->descriptor); |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 1683 | kfree(hub->status); |
Alan Stern | d697cdd | 2009-10-27 15:18:46 -0400 | [diff] [blame] | 1684 | kfree(hub->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 1686 | pm_suspend_ignore_children(&intf->dev, false); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1687 | kref_put(&hub->kref, hub_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
| 1690 | static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 1691 | { |
| 1692 | struct usb_host_interface *desc; |
| 1693 | struct usb_endpoint_descriptor *endpoint; |
| 1694 | struct usb_device *hdev; |
| 1695 | struct usb_hub *hub; |
| 1696 | |
| 1697 | desc = intf->cur_altsetting; |
| 1698 | hdev = interface_to_usbdev(intf); |
| 1699 | |
Ming Lei | 596d789 | 2012-10-24 11:59:25 +0800 | [diff] [blame] | 1700 | /* |
| 1701 | * Set default autosuspend delay as 0 to speedup bus suspend, |
| 1702 | * based on the below considerations: |
| 1703 | * |
| 1704 | * - Unlike other drivers, the hub driver does not rely on the |
| 1705 | * autosuspend delay to provide enough time to handle a wakeup |
| 1706 | * event, and the submitted status URB is just to check future |
| 1707 | * change on hub downstream ports, so it is safe to do it. |
| 1708 | * |
| 1709 | * - The patch might cause one or more auto supend/resume for |
| 1710 | * below very rare devices when they are plugged into hub |
| 1711 | * first time: |
| 1712 | * |
| 1713 | * devices having trouble initializing, and disconnect |
| 1714 | * themselves from the bus and then reconnect a second |
| 1715 | * or so later |
| 1716 | * |
| 1717 | * devices just for downloading firmware, and disconnects |
| 1718 | * themselves after completing it |
| 1719 | * |
| 1720 | * For these quite rare devices, their drivers may change the |
| 1721 | * autosuspend delay of their parent hub in the probe() to one |
| 1722 | * appropriate value to avoid the subtle problem if someone |
| 1723 | * does care it. |
| 1724 | * |
| 1725 | * - The patch may cause one or more auto suspend/resume on |
| 1726 | * hub during running 'lsusb', but it is probably too |
| 1727 | * infrequent to worry about. |
| 1728 | * |
| 1729 | * - Change autosuspend delay of hub can avoid unnecessary auto |
| 1730 | * suspend timer for hub, also may decrease power consumption |
| 1731 | * of USB bus. |
Roger Quadros | bdd405d | 2014-08-04 12:44:46 +0300 | [diff] [blame] | 1732 | * |
| 1733 | * - If user has indicated to prevent autosuspend by passing |
| 1734 | * usbcore.autosuspend = -1 then keep autosuspend disabled. |
Ming Lei | 596d789 | 2012-10-24 11:59:25 +0800 | [diff] [blame] | 1735 | */ |
Greg Kroah-Hartman | a9ef803 | 2014-08-27 16:55:29 -0700 | [diff] [blame] | 1736 | #ifdef CONFIG_PM_RUNTIME |
Roger Quadros | bdd405d | 2014-08-04 12:44:46 +0300 | [diff] [blame] | 1737 | if (hdev->dev.power.autosuspend_delay >= 0) |
| 1738 | pm_runtime_set_autosuspend_delay(&hdev->dev, 0); |
Greg Kroah-Hartman | a9ef803 | 2014-08-27 16:55:29 -0700 | [diff] [blame] | 1739 | #endif |
Ming Lei | 596d789 | 2012-10-24 11:59:25 +0800 | [diff] [blame] | 1740 | |
Alan Stern | 8ef42dd | 2014-05-23 10:45:54 -0400 | [diff] [blame] | 1741 | /* |
| 1742 | * Hubs have proper suspend/resume support, except for root hubs |
| 1743 | * where the controller driver doesn't have bus_suspend and |
| 1744 | * bus_resume methods. |
| 1745 | */ |
| 1746 | if (hdev->parent) { /* normal device */ |
| 1747 | usb_enable_autosuspend(hdev); |
| 1748 | } else { /* root hub */ |
| 1749 | const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; |
| 1750 | |
| 1751 | if (drv->bus_suspend && drv->bus_resume) |
| 1752 | usb_enable_autosuspend(hdev); |
| 1753 | } |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 1754 | |
Felipe Balbi | 38f3ad5 | 2008-06-12 10:49:47 +0300 | [diff] [blame] | 1755 | if (hdev->level == MAX_TOPO_LEVEL) { |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 1756 | dev_err(&intf->dev, |
| 1757 | "Unsupported bus topology: hub nested too deep\n"); |
Felipe Balbi | 38f3ad5 | 2008-06-12 10:49:47 +0300 | [diff] [blame] | 1758 | return -E2BIG; |
| 1759 | } |
| 1760 | |
David Brownell | 89ccbdc | 2006-04-02 10:18:09 -0800 | [diff] [blame] | 1761 | #ifdef CONFIG_USB_OTG_BLACKLIST_HUB |
| 1762 | if (hdev->parent) { |
| 1763 | dev_warn(&intf->dev, "ignoring external hub\n"); |
| 1764 | return -ENODEV; |
| 1765 | } |
| 1766 | #endif |
| 1767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
| 1769 | /* specs is not defined, but it works */ |
| 1770 | if ((desc->desc.bInterfaceSubClass != 0) && |
| 1771 | (desc->desc.bInterfaceSubClass != 1)) { |
| 1772 | descriptor_error: |
| 1773 | dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); |
| 1774 | return -EIO; |
| 1775 | } |
| 1776 | |
| 1777 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ |
| 1778 | if (desc->desc.bNumEndpoints != 1) |
| 1779 | goto descriptor_error; |
| 1780 | |
| 1781 | endpoint = &desc->endpoint[0].desc; |
| 1782 | |
Luiz Fernando N. Capitulino | fbf81c2 | 2006-09-27 11:58:53 -0700 | [diff] [blame] | 1783 | /* If it's not an interrupt in endpoint, we'd better punt! */ |
| 1784 | if (!usb_endpoint_is_int_in(endpoint)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | goto descriptor_error; |
| 1786 | |
| 1787 | /* We found a hub */ |
| 1788 | dev_info (&intf->dev, "USB hub found\n"); |
| 1789 | |
Alan Stern | 0a1ef3b | 2005-10-24 15:38:24 -0400 | [diff] [blame] | 1790 | hub = kzalloc(sizeof(*hub), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | if (!hub) { |
| 1792 | dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); |
| 1793 | return -ENOMEM; |
| 1794 | } |
| 1795 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1796 | kref_init(&hub->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | INIT_LIST_HEAD(&hub->event_list); |
| 1798 | hub->intfdev = &intf->dev; |
| 1799 | hub->hdev = hdev; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1800 | INIT_DELAYED_WORK(&hub->leds, led_work); |
Alan Stern | 8520f38 | 2008-09-22 14:44:26 -0400 | [diff] [blame] | 1801 | INIT_DELAYED_WORK(&hub->init_work, NULL); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 1802 | usb_get_intf(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | |
| 1804 | usb_set_intfdata (intf, hub); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 1805 | intf->needs_remote_wakeup = 1; |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 1806 | pm_suspend_ignore_children(&intf->dev, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | |
| 1808 | if (hdev->speed == USB_SPEED_HIGH) |
| 1809 | highspeed_hubs++; |
| 1810 | |
Ming Lei | e6f30de | 2012-10-24 11:59:24 +0800 | [diff] [blame] | 1811 | if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) |
| 1812 | hub->quirk_check_port_auto_suspend = 1; |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | if (hub_configure(hub, endpoint) >= 0) |
| 1815 | return 0; |
| 1816 | |
| 1817 | hub_disconnect (intf); |
| 1818 | return -ENODEV; |
| 1819 | } |
| 1820 | |
| 1821 | static int |
| 1822 | hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) |
| 1823 | { |
| 1824 | struct usb_device *hdev = interface_to_usbdev (intf); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1825 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | |
| 1827 | /* assert ifno == 0 (part of hub spec) */ |
| 1828 | switch (code) { |
| 1829 | case USBDEVFS_HUB_PORTINFO: { |
| 1830 | struct usbdevfs_hub_portinfo *info = user_data; |
| 1831 | int i; |
| 1832 | |
| 1833 | spin_lock_irq(&device_state_lock); |
| 1834 | if (hdev->devnum <= 0) |
| 1835 | info->nports = 0; |
| 1836 | else { |
| 1837 | info->nports = hdev->maxchild; |
| 1838 | for (i = 0; i < info->nports; i++) { |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 1839 | if (hub->ports[i]->child == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | info->port[i] = 0; |
| 1841 | else |
| 1842 | info->port[i] = |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 1843 | hub->ports[i]->child->devnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | spin_unlock_irq(&device_state_lock); |
| 1847 | |
| 1848 | return info->nports + 1; |
| 1849 | } |
| 1850 | |
| 1851 | default: |
| 1852 | return -ENOSYS; |
| 1853 | } |
| 1854 | } |
| 1855 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1856 | /* |
| 1857 | * Allow user programs to claim ports on a hub. When a device is attached |
| 1858 | * to one of these "claimed" ports, the program will "own" the device. |
| 1859 | */ |
| 1860 | static int find_port_owner(struct usb_device *hdev, unsigned port1, |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 1861 | struct usb_dev_state ***ppowner) |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1862 | { |
Mathias Nyman | 41341261 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 1863 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
| 1864 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1865 | if (hdev->state == USB_STATE_NOTATTACHED) |
| 1866 | return -ENODEV; |
| 1867 | if (port1 == 0 || port1 > hdev->maxchild) |
| 1868 | return -EINVAL; |
| 1869 | |
Mathias Nyman | 41341261 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 1870 | /* Devices not managed by the hub driver |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1871 | * will always have maxchild equal to 0. |
| 1872 | */ |
Mathias Nyman | 41341261 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 1873 | *ppowner = &(hub->ports[port1 - 1]->port_owner); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1874 | return 0; |
| 1875 | } |
| 1876 | |
| 1877 | /* In the following three functions, the caller must hold hdev's lock */ |
Lan Tianyu | 336c5c3 | 2012-07-06 14:13:52 +0800 | [diff] [blame] | 1878 | int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 1879 | struct usb_dev_state *owner) |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1880 | { |
| 1881 | int rc; |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 1882 | struct usb_dev_state **powner; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1883 | |
| 1884 | rc = find_port_owner(hdev, port1, &powner); |
| 1885 | if (rc) |
| 1886 | return rc; |
| 1887 | if (*powner) |
| 1888 | return -EBUSY; |
| 1889 | *powner = owner; |
| 1890 | return rc; |
| 1891 | } |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 1892 | EXPORT_SYMBOL_GPL(usb_hub_claim_port); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1893 | |
Lan Tianyu | 336c5c3 | 2012-07-06 14:13:52 +0800 | [diff] [blame] | 1894 | int usb_hub_release_port(struct usb_device *hdev, unsigned port1, |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 1895 | struct usb_dev_state *owner) |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1896 | { |
| 1897 | int rc; |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 1898 | struct usb_dev_state **powner; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1899 | |
| 1900 | rc = find_port_owner(hdev, port1, &powner); |
| 1901 | if (rc) |
| 1902 | return rc; |
| 1903 | if (*powner != owner) |
| 1904 | return -ENOENT; |
| 1905 | *powner = NULL; |
| 1906 | return rc; |
| 1907 | } |
Valentina Manea | 6080cd0 | 2014-03-08 14:53:34 +0200 | [diff] [blame] | 1908 | EXPORT_SYMBOL_GPL(usb_hub_release_port); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1909 | |
Valentina Manea | 9b6f0c4 | 2014-03-10 10:36:40 +0200 | [diff] [blame] | 1910 | void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner) |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1911 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1912 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1913 | int n; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1914 | |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1915 | for (n = 0; n < hdev->maxchild; n++) { |
| 1916 | if (hub->ports[n]->port_owner == owner) |
| 1917 | hub->ports[n]->port_owner = NULL; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1918 | } |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1919 | |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1920 | } |
| 1921 | |
| 1922 | /* The caller must hold udev's lock */ |
| 1923 | bool usb_device_is_owned(struct usb_device *udev) |
| 1924 | { |
| 1925 | struct usb_hub *hub; |
| 1926 | |
| 1927 | if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) |
| 1928 | return false; |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1929 | hub = usb_hub_to_struct_hub(udev->parent); |
Lan Tianyu | fa2a956 | 2012-09-05 13:44:31 +0800 | [diff] [blame] | 1930 | return !!hub->ports[udev->portnum - 1]->port_owner; |
Alan Stern | 7cbe5dc | 2009-06-29 10:56:54 -0400 | [diff] [blame] | 1931 | } |
| 1932 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | static void recursively_mark_NOTATTACHED(struct usb_device *udev) |
| 1934 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 1935 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | int i; |
| 1937 | |
| 1938 | for (i = 0; i < udev->maxchild; ++i) { |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 1939 | if (hub->ports[i]->child) |
| 1940 | recursively_mark_NOTATTACHED(hub->ports[i]->child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | } |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 1942 | if (udev->state == USB_STATE_SUSPENDED) |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 1943 | udev->active_duration -= jiffies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | udev->state = USB_STATE_NOTATTACHED; |
| 1945 | } |
| 1946 | |
| 1947 | /** |
| 1948 | * usb_set_device_state - change a device's current state (usbcore, hcds) |
| 1949 | * @udev: pointer to device whose state should be changed |
| 1950 | * @new_state: new state value to be stored |
| 1951 | * |
| 1952 | * udev->state is _not_ fully protected by the device lock. Although |
| 1953 | * most transitions are made only while holding the lock, the state can |
| 1954 | * can change to USB_STATE_NOTATTACHED at almost any time. This |
| 1955 | * is so that devices can be marked as disconnected as soon as possible, |
| 1956 | * without having to wait for any semaphores to be released. As a result, |
| 1957 | * all changes to any device's state must be protected by the |
| 1958 | * device_state_lock spinlock. |
| 1959 | * |
| 1960 | * Once a device has been added to the device tree, all changes to its state |
| 1961 | * should be made using this routine. The state should _not_ be set directly. |
| 1962 | * |
| 1963 | * If udev->state is already USB_STATE_NOTATTACHED then no change is made. |
| 1964 | * Otherwise udev->state is set to new_state, and if new_state is |
| 1965 | * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set |
| 1966 | * to USB_STATE_NOTATTACHED. |
| 1967 | */ |
| 1968 | void usb_set_device_state(struct usb_device *udev, |
| 1969 | enum usb_device_state new_state) |
| 1970 | { |
| 1971 | unsigned long flags; |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 1972 | int wakeup = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | |
| 1974 | spin_lock_irqsave(&device_state_lock, flags); |
| 1975 | if (udev->state == USB_STATE_NOTATTACHED) |
| 1976 | ; /* do nothing */ |
David Brownell | b94dc6b | 2005-09-12 19:39:39 -0700 | [diff] [blame] | 1977 | else if (new_state != USB_STATE_NOTATTACHED) { |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 1978 | |
| 1979 | /* root hub wakeup capabilities are managed out-of-band |
| 1980 | * and may involve silicon errata ... ignore them here. |
| 1981 | */ |
| 1982 | if (udev->parent) { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1983 | if (udev->state == USB_STATE_SUSPENDED |
| 1984 | || new_state == USB_STATE_SUSPENDED) |
| 1985 | ; /* No change to wakeup settings */ |
| 1986 | else if (new_state == USB_STATE_CONFIGURED) |
Lu Baolu | ddbe1fc | 2014-09-19 10:13:50 +0800 | [diff] [blame^] | 1987 | wakeup = (udev->quirks & |
| 1988 | USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 : |
| 1989 | udev->actconfig->desc.bmAttributes & |
| 1990 | USB_CONFIG_ATT_WAKEUP; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 1991 | else |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 1992 | wakeup = 0; |
David Brownell | fb669cc | 2006-01-24 08:40:27 -0800 | [diff] [blame] | 1993 | } |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 1994 | if (udev->state == USB_STATE_SUSPENDED && |
| 1995 | new_state != USB_STATE_SUSPENDED) |
| 1996 | udev->active_duration -= jiffies; |
| 1997 | else if (new_state == USB_STATE_SUSPENDED && |
| 1998 | udev->state != USB_STATE_SUSPENDED) |
| 1999 | udev->active_duration += jiffies; |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 2000 | udev->state = new_state; |
David Brownell | b94dc6b | 2005-09-12 19:39:39 -0700 | [diff] [blame] | 2001 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | recursively_mark_NOTATTACHED(udev); |
| 2003 | spin_unlock_irqrestore(&device_state_lock, flags); |
Rafael J. Wysocki | 4681b17 | 2011-02-08 23:25:48 +0100 | [diff] [blame] | 2004 | if (wakeup >= 0) |
| 2005 | device_set_wakeup_capable(&udev->dev, wakeup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | } |
David Vrabel | 6da9c99 | 2009-02-18 14:43:47 +0000 | [diff] [blame] | 2007 | EXPORT_SYMBOL_GPL(usb_set_device_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 2009 | /* |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 2010 | * Choose a device number. |
| 2011 | * |
| 2012 | * Device numbers are used as filenames in usbfs. On USB-1.1 and |
| 2013 | * USB-2.0 buses they are also used as device addresses, however on |
| 2014 | * USB-3.0 buses the address is assigned by the controller hardware |
| 2015 | * and it usually is not the same as the device number. |
| 2016 | * |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 2017 | * WUSB devices are simple: they have no hubs behind, so the mapping |
| 2018 | * device <-> virtual port number becomes 1:1. Why? to simplify the |
| 2019 | * life of the device connection logic in |
| 2020 | * drivers/usb/wusbcore/devconnect.c. When we do the initial secret |
| 2021 | * handshake we need to assign a temporary address in the unauthorized |
| 2022 | * space. For simplicity we use the first virtual port number found to |
| 2023 | * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] |
| 2024 | * and that becomes it's address [X < 128] or its unauthorized address |
| 2025 | * [X | 0x80]. |
| 2026 | * |
| 2027 | * We add 1 as an offset to the one-based USB-stack port number |
| 2028 | * (zero-based wusb virtual port index) for two reasons: (a) dev addr |
| 2029 | * 0 is reserved by USB for default address; (b) Linux's USB stack |
| 2030 | * uses always #1 for the root hub of the controller. So USB stack's |
| 2031 | * port #1, which is wusb virtual-port #0 has address #2. |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 2032 | * |
| 2033 | * Devices connected under xHCI are not as simple. The host controller |
| 2034 | * supports virtualization, so the hardware assigns device addresses and |
| 2035 | * the HCD must setup data structures before issuing a set address |
| 2036 | * command to the hardware. |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 2037 | */ |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 2038 | static void choose_devnum(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | { |
| 2040 | int devnum; |
| 2041 | struct usb_bus *bus = udev->bus; |
| 2042 | |
| 2043 | /* If khubd ever becomes multithreaded, this will need a lock */ |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 2044 | if (udev->wusb) { |
| 2045 | devnum = udev->portnum + 1; |
| 2046 | BUG_ON(test_bit(devnum, bus->devmap.devicemap)); |
| 2047 | } else { |
| 2048 | /* Try to allocate the next devnum beginning at |
| 2049 | * bus->devnum_next. */ |
| 2050 | devnum = find_next_zero_bit(bus->devmap.devicemap, 128, |
| 2051 | bus->devnum_next); |
| 2052 | if (devnum >= 128) |
| 2053 | devnum = find_next_zero_bit(bus->devmap.devicemap, |
| 2054 | 128, 1); |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 2055 | bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 2056 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | if (devnum < 128) { |
| 2058 | set_bit(devnum, bus->devmap.devicemap); |
| 2059 | udev->devnum = devnum; |
| 2060 | } |
| 2061 | } |
| 2062 | |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 2063 | static void release_devnum(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | { |
| 2065 | if (udev->devnum > 0) { |
| 2066 | clear_bit(udev->devnum, udev->bus->devmap.devicemap); |
| 2067 | udev->devnum = -1; |
| 2068 | } |
| 2069 | } |
| 2070 | |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 2071 | static void update_devnum(struct usb_device *udev, int devnum) |
David Vrabel | 4953d14 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 2072 | { |
| 2073 | /* The address for a WUSB device is managed by wusbcore. */ |
| 2074 | if (!udev->wusb) |
| 2075 | udev->devnum = devnum; |
| 2076 | } |
| 2077 | |
Herbert Xu | f7410ce | 2010-01-10 20:15:03 +1100 | [diff] [blame] | 2078 | static void hub_free_dev(struct usb_device *udev) |
| 2079 | { |
| 2080 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 2081 | |
| 2082 | /* Root hubs aren't real devices, so don't free HCD resources */ |
| 2083 | if (hcd->driver->free_dev && udev->parent) |
| 2084 | hcd->driver->free_dev(hcd, udev); |
| 2085 | } |
| 2086 | |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 2087 | static void hub_disconnect_children(struct usb_device *udev) |
| 2088 | { |
| 2089 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); |
| 2090 | int i; |
| 2091 | |
| 2092 | /* Free up all the children before we remove this device */ |
| 2093 | for (i = 0; i < udev->maxchild; i++) { |
| 2094 | if (hub->ports[i]->child) |
| 2095 | usb_disconnect(&hub->ports[i]->child); |
| 2096 | } |
| 2097 | } |
| 2098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | /** |
| 2100 | * usb_disconnect - disconnect a device (usbcore-internal) |
| 2101 | * @pdev: pointer to device being disconnected |
| 2102 | * Context: !in_interrupt () |
| 2103 | * |
| 2104 | * Something got disconnected. Get rid of it and all of its children. |
| 2105 | * |
| 2106 | * If *pdev is a normal device then the parent hub must already be locked. |
Bjorn Helgaas | db8f2aa | 2013-09-13 13:57:34 -0600 | [diff] [blame] | 2107 | * If *pdev is a root hub then the caller must hold the usb_bus_list_lock, |
| 2108 | * which protects the set of root hubs as well as the list of buses. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | * |
| 2110 | * Only hub drivers (including virtual root hub drivers for host |
| 2111 | * controllers) should ever call this. |
| 2112 | * |
| 2113 | * This call is synchronous, and may not be used in an interrupt context. |
| 2114 | */ |
| 2115 | void usb_disconnect(struct usb_device **pdev) |
| 2116 | { |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 2117 | struct usb_port *port_dev = NULL; |
| 2118 | struct usb_device *udev = *pdev; |
Peter Chen | 5b1dc20 | 2014-08-19 08:56:21 +0800 | [diff] [blame] | 2119 | struct usb_hub *hub = NULL; |
| 2120 | int port1 = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | /* mark the device as inactive, so any further urb submissions for |
| 2123 | * this device (and any of its children) will fail immediately. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2124 | * this quiesces everything except pending urbs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | */ |
| 2126 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 2127 | dev_info(&udev->dev, "USB disconnect, device number %d\n", |
| 2128 | udev->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 2130 | usb_lock_device(udev); |
| 2131 | |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 2132 | hub_disconnect_children(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | |
| 2134 | /* deallocate hcd/hardware state ... nuking all pending urbs and |
| 2135 | * cleaning up all state associated with the current configuration |
| 2136 | * so that the hardware is now fully quiesced. |
| 2137 | */ |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2138 | dev_dbg (&udev->dev, "unregistering device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | usb_disable_device(udev, 0); |
Alan Stern | cde217a | 2008-10-21 15:28:46 -0400 | [diff] [blame] | 2140 | usb_hcd_synchronize_unlinks(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | |
Lan Tianyu | fde2638 | 2013-01-20 01:53:33 +0800 | [diff] [blame] | 2142 | if (udev->parent) { |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 2143 | port1 = udev->portnum; |
| 2144 | hub = usb_hub_to_struct_hub(udev->parent); |
| 2145 | port_dev = hub->ports[port1 - 1]; |
Lan Tianyu | fde2638 | 2013-01-20 01:53:33 +0800 | [diff] [blame] | 2146 | |
| 2147 | sysfs_remove_link(&udev->dev.kobj, "port"); |
| 2148 | sysfs_remove_link(&port_dev->dev.kobj, "device"); |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 2149 | |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 2150 | /* |
| 2151 | * As usb_port_runtime_resume() de-references udev, make |
| 2152 | * sure no resumes occur during removal |
| 2153 | */ |
| 2154 | if (!test_and_set_bit(port1, hub->child_usage_bits)) |
| 2155 | pm_runtime_get_sync(&port_dev->dev); |
Lan Tianyu | fde2638 | 2013-01-20 01:53:33 +0800 | [diff] [blame] | 2156 | } |
| 2157 | |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 2158 | usb_remove_ep_devs(&udev->ep0); |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2159 | usb_unlock_device(udev); |
Greg Kroah-Hartman | 3099e75 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 2160 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2161 | /* Unregister the device. The device driver is responsible |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 2162 | * for de-configuring the device and invoking the remove-device |
| 2163 | * notifier chain (used by usbfs and possibly others). |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2164 | */ |
| 2165 | device_del(&udev->dev); |
| 2166 | |
| 2167 | /* Free the device number and delete the parent's children[] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | * (or root_hub) pointer. |
| 2169 | */ |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 2170 | release_devnum(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
| 2172 | /* Avoid races with recursively_mark_NOTATTACHED() */ |
| 2173 | spin_lock_irq(&device_state_lock); |
| 2174 | *pdev = NULL; |
| 2175 | spin_unlock_irq(&device_state_lock); |
| 2176 | |
Dan Williams | 7027df3 | 2014-05-20 18:09:36 -0700 | [diff] [blame] | 2177 | if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) |
| 2178 | pm_runtime_put(&port_dev->dev); |
| 2179 | |
Herbert Xu | f7410ce | 2010-01-10 20:15:03 +1100 | [diff] [blame] | 2180 | hub_free_dev(udev); |
| 2181 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2182 | put_device(&udev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | } |
| 2184 | |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 2185 | #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | static void show_string(struct usb_device *udev, char *id, char *string) |
| 2187 | { |
| 2188 | if (!string) |
| 2189 | return; |
Joe Perches | f2ec522 | 2012-10-28 01:05:51 -0700 | [diff] [blame] | 2190 | dev_info(&udev->dev, "%s: %s\n", id, string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 2193 | static void announce_device(struct usb_device *udev) |
| 2194 | { |
| 2195 | dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", |
| 2196 | le16_to_cpu(udev->descriptor.idVendor), |
| 2197 | le16_to_cpu(udev->descriptor.idProduct)); |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 2198 | dev_info(&udev->dev, |
| 2199 | "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 2200 | udev->descriptor.iManufacturer, |
| 2201 | udev->descriptor.iProduct, |
| 2202 | udev->descriptor.iSerialNumber); |
| 2203 | show_string(udev, "Product", udev->product); |
| 2204 | show_string(udev, "Manufacturer", udev->manufacturer); |
| 2205 | show_string(udev, "SerialNumber", udev->serial); |
| 2206 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | #else |
Greg Kroah-Hartman | f2a383e | 2007-11-26 22:11:55 -0800 | [diff] [blame] | 2208 | static inline void announce_device(struct usb_device *udev) { } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | #endif |
| 2210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | |
Oliver Neukum | 3ede760 | 2007-01-11 14:35:50 +0100 | [diff] [blame] | 2212 | /** |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2213 | * usb_enumerate_device_otg - FIXME (usbcore-internal) |
Oliver Neukum | 3ede760 | 2007-01-11 14:35:50 +0100 | [diff] [blame] | 2214 | * @udev: newly addressed device (in ADDRESS state) |
| 2215 | * |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2216 | * Finish enumeration for On-The-Go devices |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 2217 | * |
| 2218 | * Return: 0 if successful. A negative error code otherwise. |
Oliver Neukum | 3ede760 | 2007-01-11 14:35:50 +0100 | [diff] [blame] | 2219 | */ |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2220 | static int usb_enumerate_device_otg(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | { |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2222 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | |
| 2224 | #ifdef CONFIG_USB_OTG |
| 2225 | /* |
| 2226 | * OTG-aware devices on OTG-capable root hubs may be able to use SRP, |
| 2227 | * to wake us after we've powered off VBUS; and HNP, switching roles |
| 2228 | * "host" to "peripheral". The OTG descriptor helps figure this out. |
| 2229 | */ |
| 2230 | if (!udev->bus->is_b_host |
| 2231 | && udev->config |
| 2232 | && udev->parent == udev->bus->root_hub) { |
Felipe Balbi | 2eb5052 | 2009-12-04 15:47:43 +0200 | [diff] [blame] | 2233 | struct usb_otg_descriptor *desc = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | struct usb_bus *bus = udev->bus; |
| 2235 | |
| 2236 | /* descriptor may appear anywhere in config */ |
| 2237 | if (__usb_get_extra_descriptor (udev->rawdescriptors[0], |
| 2238 | le16_to_cpu(udev->config[0].desc.wTotalLength), |
| 2239 | USB_DT_OTG, (void **) &desc) == 0) { |
| 2240 | if (desc->bmAttributes & USB_OTG_HNP) { |
Alan Stern | 12c3da3 | 2005-11-23 12:09:52 -0500 | [diff] [blame] | 2241 | unsigned port1 = udev->portnum; |
David Brownell | fc849b8 | 2006-09-18 16:53:26 -0700 | [diff] [blame] | 2242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2243 | dev_info(&udev->dev, |
| 2244 | "Dual-Role OTG device on %sHNP port\n", |
| 2245 | (port1 == bus->otg_port) |
| 2246 | ? "" : "non-"); |
| 2247 | |
| 2248 | /* enable HNP before suspend, it's simpler */ |
| 2249 | if (port1 == bus->otg_port) |
| 2250 | bus->b_hnp_enable = 1; |
| 2251 | err = usb_control_msg(udev, |
| 2252 | usb_sndctrlpipe(udev, 0), |
| 2253 | USB_REQ_SET_FEATURE, 0, |
| 2254 | bus->b_hnp_enable |
| 2255 | ? USB_DEVICE_B_HNP_ENABLE |
| 2256 | : USB_DEVICE_A_ALT_HNP_SUPPORT, |
| 2257 | 0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 2258 | if (err < 0) { |
| 2259 | /* OTG MESSAGE: report errors here, |
| 2260 | * customize to match your product. |
| 2261 | */ |
| 2262 | dev_info(&udev->dev, |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 2263 | "can't set HNP mode: %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | err); |
| 2265 | bus->b_hnp_enable = 0; |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | #endif |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2271 | return err; |
| 2272 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2274 | |
| 2275 | /** |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2276 | * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2277 | * @udev: newly addressed device (in ADDRESS state) |
| 2278 | * |
| 2279 | * This is only called by usb_new_device() and usb_authorize_device() |
| 2280 | * and FIXME -- all comments that apply to them apply here wrt to |
| 2281 | * environment. |
| 2282 | * |
| 2283 | * If the device is WUSB and not authorized, we don't attempt to read |
| 2284 | * the string descriptors, as they will be errored out by the device |
| 2285 | * until it has been authorized. |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 2286 | * |
| 2287 | * Return: 0 if successful. A negative error code otherwise. |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2288 | */ |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2289 | static int usb_enumerate_device(struct usb_device *udev) |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2290 | { |
| 2291 | int err; |
Peter Chen | 026f3fc | 2014-08-19 09:51:53 +0800 | [diff] [blame] | 2292 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2293 | |
| 2294 | if (udev->config == NULL) { |
| 2295 | err = usb_get_configuration(udev); |
| 2296 | if (err < 0) { |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 2297 | if (err != -ENODEV) |
| 2298 | dev_err(&udev->dev, "can't read configurations, error %d\n", |
| 2299 | err); |
Laurent Pinchart | 80da2e0 | 2012-07-19 12:39:13 +0200 | [diff] [blame] | 2300 | return err; |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2301 | } |
| 2302 | } |
Thomas Pugliese | 83e83ec | 2013-12-09 13:40:29 -0600 | [diff] [blame] | 2303 | |
| 2304 | /* read the standard strings and cache them if present */ |
| 2305 | udev->product = usb_cache_string(udev, udev->descriptor.iProduct); |
| 2306 | udev->manufacturer = usb_cache_string(udev, |
| 2307 | udev->descriptor.iManufacturer); |
| 2308 | udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); |
| 2309 | |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2310 | err = usb_enumerate_device_otg(udev); |
Laurent Pinchart | 80da2e0 | 2012-07-19 12:39:13 +0200 | [diff] [blame] | 2311 | if (err < 0) |
| 2312 | return err; |
| 2313 | |
Peter Chen | 026f3fc | 2014-08-19 09:51:53 +0800 | [diff] [blame] | 2314 | if (IS_ENABLED(CONFIG_USB_OTG_WHITELIST) && hcd->tpl_support && |
| 2315 | !is_targeted(udev) && IS_ENABLED(CONFIG_USB_OTG)) { |
| 2316 | /* Maybe it can talk to us, though we can't talk to it. |
| 2317 | * (Includes HNP test device.) |
| 2318 | */ |
| 2319 | if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { |
| 2320 | err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND); |
| 2321 | if (err < 0) |
| 2322 | dev_dbg(&udev->dev, "HNP fail, %d\n", err); |
| 2323 | } |
| 2324 | return -ENOTSUPP; |
| 2325 | } |
| 2326 | |
Laurent Pinchart | 80da2e0 | 2012-07-19 12:39:13 +0200 | [diff] [blame] | 2327 | usb_detect_interface_quirks(udev); |
| 2328 | |
| 2329 | return 0; |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2330 | } |
| 2331 | |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2332 | static void set_usb_port_removable(struct usb_device *udev) |
| 2333 | { |
| 2334 | struct usb_device *hdev = udev->parent; |
| 2335 | struct usb_hub *hub; |
| 2336 | u8 port = udev->portnum; |
| 2337 | u16 wHubCharacteristics; |
| 2338 | bool removable = true; |
| 2339 | |
| 2340 | if (!hdev) |
| 2341 | return; |
| 2342 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2343 | hub = usb_hub_to_struct_hub(udev->parent); |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2344 | |
| 2345 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); |
| 2346 | |
| 2347 | if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) |
| 2348 | return; |
| 2349 | |
| 2350 | if (hub_is_superspeed(hdev)) { |
Lan Tianyu | ca3c153 | 2012-09-11 04:22:36 +0800 | [diff] [blame] | 2351 | if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) |
| 2352 | & (1 << port)) |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2353 | removable = false; |
| 2354 | } else { |
| 2355 | if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) |
| 2356 | removable = false; |
| 2357 | } |
| 2358 | |
| 2359 | if (removable) |
| 2360 | udev->removable = USB_DEVICE_REMOVABLE; |
| 2361 | else |
| 2362 | udev->removable = USB_DEVICE_FIXED; |
Dan Williams | a4204ff | 2014-05-20 18:08:22 -0700 | [diff] [blame] | 2363 | |
| 2364 | /* |
| 2365 | * Platform firmware may have populated an alternative value for |
| 2366 | * removable. If the parent port has a known connect_type use |
| 2367 | * that instead. |
| 2368 | */ |
| 2369 | switch (hub->ports[udev->portnum - 1]->connect_type) { |
| 2370 | case USB_PORT_CONNECT_TYPE_HOT_PLUG: |
| 2371 | udev->removable = USB_DEVICE_REMOVABLE; |
| 2372 | break; |
| 2373 | case USB_PORT_CONNECT_TYPE_HARD_WIRED: |
| 2374 | udev->removable = USB_DEVICE_FIXED; |
| 2375 | break; |
| 2376 | default: /* use what was set above */ |
| 2377 | break; |
| 2378 | } |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2379 | } |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2380 | |
| 2381 | /** |
| 2382 | * usb_new_device - perform initial device setup (usbcore-internal) |
| 2383 | * @udev: newly addressed device (in ADDRESS state) |
| 2384 | * |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2385 | * This is called with devices which have been detected but not fully |
| 2386 | * enumerated. The device descriptor is available, but not descriptors |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2387 | * for any device configuration. The caller must have locked either |
| 2388 | * the parent hub (if udev is a normal device) or else the |
| 2389 | * usb_bus_list_lock (if udev is a root hub). The parent's pointer to |
| 2390 | * udev has already been installed, but udev is not yet visible through |
| 2391 | * sysfs or other filesystem code. |
| 2392 | * |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2393 | * This call is synchronous, and may not be used in an interrupt context. |
| 2394 | * |
| 2395 | * Only the hub driver or root-hub registrar should ever call this. |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 2396 | * |
| 2397 | * Return: Whether the device is configured properly or not. Zero if the |
| 2398 | * interface was registered with the driver core; else a negative errno |
| 2399 | * value. |
| 2400 | * |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2401 | */ |
| 2402 | int usb_new_device(struct usb_device *udev) |
| 2403 | { |
| 2404 | int err; |
| 2405 | |
Dan Streetman | 1698540 | 2010-01-06 09:56:53 -0500 | [diff] [blame] | 2406 | if (udev->parent) { |
Dan Streetman | 1698540 | 2010-01-06 09:56:53 -0500 | [diff] [blame] | 2407 | /* Initialize non-root-hub device wakeup to disabled; |
| 2408 | * device (un)configuration controls wakeup capable |
| 2409 | * sysfs power/wakeup controls wakeup enabled/disabled |
| 2410 | */ |
| 2411 | device_init_wakeup(&udev->dev, 0); |
Dan Streetman | 1698540 | 2010-01-06 09:56:53 -0500 | [diff] [blame] | 2412 | } |
| 2413 | |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2414 | /* Tell the runtime-PM framework the device is active */ |
| 2415 | pm_runtime_set_active(&udev->dev); |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2416 | pm_runtime_get_noresume(&udev->dev); |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 2417 | pm_runtime_use_autosuspend(&udev->dev); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2418 | pm_runtime_enable(&udev->dev); |
| 2419 | |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2420 | /* By default, forbid autosuspend for all devices. It will be |
| 2421 | * allowed for hubs during binding. |
| 2422 | */ |
| 2423 | usb_disable_autosuspend(udev); |
| 2424 | |
Alan Stern | 8d8558d | 2009-12-08 15:50:41 -0500 | [diff] [blame] | 2425 | err = usb_enumerate_device(udev); /* Read descriptors */ |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2426 | if (err < 0) |
| 2427 | goto fail; |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 2428 | dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", |
| 2429 | udev->devnum, udev->bus->busnum, |
| 2430 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2431 | /* export the usbdev device-node for libusb */ |
| 2432 | udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, |
| 2433 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); |
| 2434 | |
Alan Stern | 6cd1320 | 2008-11-13 15:08:30 -0500 | [diff] [blame] | 2435 | /* Tell the world! */ |
| 2436 | announce_device(udev); |
Alan Stern | 195af2c | 2007-07-16 15:28:19 -0400 | [diff] [blame] | 2437 | |
Theodore Ts'o | b04b315 | 2012-07-04 11:22:20 -0400 | [diff] [blame] | 2438 | if (udev->serial) |
| 2439 | add_device_randomness(udev->serial, strlen(udev->serial)); |
| 2440 | if (udev->product) |
| 2441 | add_device_randomness(udev->product, strlen(udev->product)); |
| 2442 | if (udev->manufacturer) |
| 2443 | add_device_randomness(udev->manufacturer, |
| 2444 | strlen(udev->manufacturer)); |
| 2445 | |
Rafael J. Wysocki | 927bc91 | 2010-02-08 19:18:16 +0100 | [diff] [blame] | 2446 | device_enable_async_suspend(&udev->dev); |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2447 | |
Dan Williams | a4204ff | 2014-05-20 18:08:22 -0700 | [diff] [blame] | 2448 | /* check whether the hub or firmware marks this port as non-removable */ |
Matthew Garrett | d35e70d | 2012-02-03 17:11:55 -0500 | [diff] [blame] | 2449 | if (udev->parent) |
| 2450 | set_usb_port_removable(udev); |
| 2451 | |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2452 | /* Register the device. The device driver is responsible |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 2453 | * for configuring the device and invoking the add-device |
| 2454 | * notifier chain (used by usbfs and possibly others). |
Alan Stern | 782da72 | 2006-07-01 22:09:35 -0400 | [diff] [blame] | 2455 | */ |
Kay Sievers | 9f8b17e | 2007-03-13 15:59:31 +0100 | [diff] [blame] | 2456 | err = device_add(&udev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | if (err) { |
| 2458 | dev_err(&udev->dev, "can't device_add, error %d\n", err); |
| 2459 | goto fail; |
| 2460 | } |
Alan Stern | 9ad3d6c | 2005-11-17 17:10:32 -0500 | [diff] [blame] | 2461 | |
Lan Tianyu | fde2638 | 2013-01-20 01:53:33 +0800 | [diff] [blame] | 2462 | /* Create link files between child device and usb port device. */ |
| 2463 | if (udev->parent) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2464 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 2465 | int port1 = udev->portnum; |
| 2466 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
Lan Tianyu | fde2638 | 2013-01-20 01:53:33 +0800 | [diff] [blame] | 2467 | |
| 2468 | err = sysfs_create_link(&udev->dev.kobj, |
| 2469 | &port_dev->dev.kobj, "port"); |
| 2470 | if (err) |
| 2471 | goto fail; |
| 2472 | |
| 2473 | err = sysfs_create_link(&port_dev->dev.kobj, |
| 2474 | &udev->dev.kobj, "device"); |
| 2475 | if (err) { |
| 2476 | sysfs_remove_link(&udev->dev.kobj, "port"); |
| 2477 | goto fail; |
| 2478 | } |
Lan Tianyu | 971fcd4 | 2013-01-23 04:26:29 +0800 | [diff] [blame] | 2479 | |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 2480 | if (!test_and_set_bit(port1, hub->child_usage_bits)) |
| 2481 | pm_runtime_get_sync(&port_dev->dev); |
Lan Tianyu | fde2638 | 2013-01-20 01:53:33 +0800 | [diff] [blame] | 2482 | } |
| 2483 | |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 2484 | (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 2485 | usb_mark_last_busy(udev); |
| 2486 | pm_runtime_put_sync_autosuspend(&udev->dev); |
Greg Kroah-Hartman | c066475 | 2006-08-11 01:55:12 -0700 | [diff] [blame] | 2487 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | |
| 2489 | fail: |
| 2490 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 2491 | pm_runtime_disable(&udev->dev); |
| 2492 | pm_runtime_set_suspended(&udev->dev); |
Inaky Perez-Gonzalez | d9d16e8 | 2007-07-31 20:34:05 -0700 | [diff] [blame] | 2493 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2496 | |
| 2497 | /** |
Randy Dunlap | fd39c86 | 2007-10-15 17:30:02 -0700 | [diff] [blame] | 2498 | * usb_deauthorize_device - deauthorize a device (usbcore-internal) |
| 2499 | * @usb_dev: USB device |
| 2500 | * |
| 2501 | * Move the USB device to a very basic state where interfaces are disabled |
| 2502 | * and the device is in fact unconfigured and unusable. |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2503 | * |
| 2504 | * We share a lock (that we have) with device_del(), so we need to |
| 2505 | * defer its call. |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 2506 | * |
| 2507 | * Return: 0. |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2508 | */ |
| 2509 | int usb_deauthorize_device(struct usb_device *usb_dev) |
| 2510 | { |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2511 | usb_lock_device(usb_dev); |
| 2512 | if (usb_dev->authorized == 0) |
| 2513 | goto out_unauthorized; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2514 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2515 | usb_dev->authorized = 0; |
| 2516 | usb_set_configuration(usb_dev, -1); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2517 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2518 | out_unauthorized: |
| 2519 | usb_unlock_device(usb_dev); |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
| 2523 | |
| 2524 | int usb_authorize_device(struct usb_device *usb_dev) |
| 2525 | { |
| 2526 | int result = 0, c; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2527 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2528 | usb_lock_device(usb_dev); |
| 2529 | if (usb_dev->authorized == 1) |
| 2530 | goto out_authorized; |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2531 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2532 | result = usb_autoresume_device(usb_dev); |
| 2533 | if (result < 0) { |
| 2534 | dev_err(&usb_dev->dev, |
| 2535 | "can't autoresume for authorization: %d\n", result); |
| 2536 | goto error_autoresume; |
| 2537 | } |
| 2538 | result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); |
| 2539 | if (result < 0) { |
| 2540 | dev_err(&usb_dev->dev, "can't re-read device descriptor for " |
| 2541 | "authorization: %d\n", result); |
| 2542 | goto error_device_descriptor; |
| 2543 | } |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2544 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2545 | usb_dev->authorized = 1; |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2546 | /* Choose and set the configuration. This registers the interfaces |
| 2547 | * with the driver core and lets interface drivers bind to them. |
| 2548 | */ |
Greg Kroah-Hartman | b5ea060 | 2007-08-02 22:44:27 -0600 | [diff] [blame] | 2549 | c = usb_choose_configuration(usb_dev); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2550 | if (c >= 0) { |
| 2551 | result = usb_set_configuration(usb_dev, c); |
| 2552 | if (result) { |
| 2553 | dev_err(&usb_dev->dev, |
| 2554 | "can't set config #%d, error %d\n", c, result); |
| 2555 | /* This need not be fatal. The user can try to |
| 2556 | * set other configurations. */ |
| 2557 | } |
| 2558 | } |
| 2559 | dev_info(&usb_dev->dev, "authorized to connect\n"); |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2560 | |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2561 | error_device_descriptor: |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 2562 | usb_autosuspend_device(usb_dev); |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2563 | error_autoresume: |
| 2564 | out_authorized: |
Matthias Beyer | 781b213 | 2013-10-10 23:41:29 +0200 | [diff] [blame] | 2565 | usb_unlock_device(usb_dev); /* complements locktree */ |
Inaky Perez-Gonzalez | 93993a0 | 2007-07-31 20:34:06 -0700 | [diff] [blame] | 2566 | return result; |
| 2567 | } |
| 2568 | |
| 2569 | |
Inaky Perez-Gonzalez | 0165de0 | 2006-08-25 19:35:29 -0700 | [diff] [blame] | 2570 | /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ |
| 2571 | static unsigned hub_is_wusb(struct usb_hub *hub) |
| 2572 | { |
| 2573 | struct usb_hcd *hcd; |
| 2574 | if (hub->hdev->parent != NULL) /* not a root hub? */ |
| 2575 | return 0; |
| 2576 | hcd = container_of(hub->hdev->bus, struct usb_hcd, self); |
| 2577 | return hcd->wireless; |
| 2578 | } |
| 2579 | |
| 2580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | #define PORT_RESET_TRIES 5 |
| 2582 | #define SET_ADDRESS_TRIES 2 |
| 2583 | #define GET_DESCRIPTOR_TRIES 2 |
| 2584 | #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 2585 | #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | |
| 2587 | #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ |
| 2588 | #define HUB_SHORT_RESET_TIME 10 |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2589 | #define HUB_BH_RESET_TIME 50 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | #define HUB_LONG_RESET_TIME 200 |
Sarah Sharp | 77c7f07 | 2012-11-14 17:16:52 -0800 | [diff] [blame] | 2591 | #define HUB_RESET_TIMEOUT 800 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 2593 | /* |
| 2594 | * "New scheme" enumeration causes an extra state transition to be |
| 2595 | * exposed to an xhci host and causes USB3 devices to receive control |
| 2596 | * commands in the default state. This has been seen to cause |
| 2597 | * enumeration failures, so disable this enumeration scheme for USB3 |
| 2598 | * devices. |
| 2599 | */ |
| 2600 | static bool use_new_scheme(struct usb_device *udev, int retry) |
| 2601 | { |
| 2602 | if (udev->speed == USB_SPEED_SUPER) |
| 2603 | return false; |
| 2604 | |
| 2605 | return USE_NEW_SCHEME(retry); |
| 2606 | } |
| 2607 | |
Sarah Sharp | 10d674a | 2011-09-14 14:24:52 -0700 | [diff] [blame] | 2608 | static int hub_port_reset(struct usb_hub *hub, int port1, |
| 2609 | struct usb_device *udev, unsigned int delay, bool warm); |
| 2610 | |
Rahul Bedarkar | 025d443 | 2014-01-04 11:24:41 +0530 | [diff] [blame] | 2611 | /* Is a USB 3.0 port in the Inactive or Compliance Mode state? |
Stanislaw Ledwon | 8bea2bd | 2012-06-18 15:20:00 +0200 | [diff] [blame] | 2612 | * Port worm reset is required to recover |
| 2613 | */ |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2614 | static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, |
| 2615 | u16 portstatus) |
Sarah Sharp | 10d674a | 2011-09-14 14:24:52 -0700 | [diff] [blame] | 2616 | { |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2617 | u16 link_state; |
| 2618 | |
| 2619 | if (!hub_is_superspeed(hub->hdev)) |
| 2620 | return false; |
| 2621 | |
| 2622 | if (test_bit(port1, hub->warm_reset_bits)) |
| 2623 | return true; |
| 2624 | |
| 2625 | link_state = portstatus & USB_PORT_STAT_LINK_STATE; |
| 2626 | return link_state == USB_SS_PORT_LS_SS_INACTIVE |
| 2627 | || link_state == USB_SS_PORT_LS_COMP_MOD; |
Sarah Sharp | 10d674a | 2011-09-14 14:24:52 -0700 | [diff] [blame] | 2628 | } |
| 2629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | static int hub_port_wait_reset(struct usb_hub *hub, int port1, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2631 | struct usb_device *udev, unsigned int delay, bool warm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | { |
| 2633 | int delay_time, ret; |
| 2634 | u16 portstatus; |
| 2635 | u16 portchange; |
| 2636 | |
| 2637 | for (delay_time = 0; |
| 2638 | delay_time < HUB_RESET_TIMEOUT; |
| 2639 | delay_time += delay) { |
| 2640 | /* wait to give the device a chance to reset */ |
| 2641 | msleep(delay); |
| 2642 | |
| 2643 | /* read and decode port status */ |
| 2644 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
| 2645 | if (ret < 0) |
| 2646 | return ret; |
| 2647 | |
Sarah Sharp | 4f43447 | 2012-11-15 14:58:04 -0800 | [diff] [blame] | 2648 | /* The port state is unknown until the reset completes. */ |
Sarah Sharp | 470f0be | 2012-12-11 10:14:03 -0800 | [diff] [blame] | 2649 | if (!(portstatus & USB_PORT_STAT_RESET)) |
| 2650 | break; |
Sarah Sharp | 4f43447 | 2012-11-15 14:58:04 -0800 | [diff] [blame] | 2651 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | /* switch to the long delay after two short delay failures */ |
| 2653 | if (delay_time >= 2 * HUB_SHORT_RESET_TIME) |
| 2654 | delay = HUB_LONG_RESET_TIME; |
| 2655 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2656 | dev_dbg(&hub->ports[port1 - 1]->dev, |
| 2657 | "not %sreset yet, waiting %dms\n", |
| 2658 | warm ? "warm " : "", delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
| 2660 | |
Sarah Sharp | 470f0be | 2012-12-11 10:14:03 -0800 | [diff] [blame] | 2661 | if ((portstatus & USB_PORT_STAT_RESET)) |
| 2662 | return -EBUSY; |
| 2663 | |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2664 | if (hub_port_warm_reset_required(hub, port1, portstatus)) |
Sarah Sharp | 470f0be | 2012-12-11 10:14:03 -0800 | [diff] [blame] | 2665 | return -ENOTCONN; |
| 2666 | |
| 2667 | /* Device went away? */ |
| 2668 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) |
| 2669 | return -ENOTCONN; |
| 2670 | |
| 2671 | /* bomb out completely if the connection bounced. A USB 3.0 |
| 2672 | * connection may bounce if multiple warm resets were issued, |
| 2673 | * but the device may have successfully re-connected. Ignore it. |
| 2674 | */ |
| 2675 | if (!hub_is_superspeed(hub->hdev) && |
| 2676 | (portchange & USB_PORT_STAT_C_CONNECTION)) |
| 2677 | return -ENOTCONN; |
| 2678 | |
| 2679 | if (!(portstatus & USB_PORT_STAT_ENABLE)) |
| 2680 | return -EBUSY; |
| 2681 | |
| 2682 | if (!udev) |
| 2683 | return 0; |
| 2684 | |
| 2685 | if (hub_is_wusb(hub)) |
| 2686 | udev->speed = USB_SPEED_WIRELESS; |
| 2687 | else if (hub_is_superspeed(hub->hdev)) |
| 2688 | udev->speed = USB_SPEED_SUPER; |
| 2689 | else if (portstatus & USB_PORT_STAT_HIGH_SPEED) |
| 2690 | udev->speed = USB_SPEED_HIGH; |
| 2691 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) |
| 2692 | udev->speed = USB_SPEED_LOW; |
| 2693 | else |
| 2694 | udev->speed = USB_SPEED_FULL; |
| 2695 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | } |
| 2697 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2698 | static void hub_port_finish_reset(struct usb_hub *hub, int port1, |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2699 | struct usb_device *udev, int *status) |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2700 | { |
| 2701 | switch (*status) { |
| 2702 | case 0: |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2703 | /* TRSTRCY = 10 ms; plus some extra */ |
| 2704 | msleep(10 + 40); |
| 2705 | if (udev) { |
| 2706 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 2707 | |
| 2708 | update_devnum(udev, 0); |
| 2709 | /* The xHC may think the device is already reset, |
| 2710 | * so ignore the status. |
| 2711 | */ |
| 2712 | if (hcd->driver->reset_device) |
| 2713 | hcd->driver->reset_device(hcd, udev); |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2714 | } |
| 2715 | /* FALL THROUGH */ |
| 2716 | case -ENOTCONN: |
| 2717 | case -ENODEV: |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2718 | usb_clear_port_feature(hub->hdev, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2719 | port1, USB_PORT_FEAT_C_RESET); |
Sarah Sharp | 1c7439c6 | 2012-11-14 15:58:52 -0800 | [diff] [blame] | 2720 | if (hub_is_superspeed(hub->hdev)) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2721 | usb_clear_port_feature(hub->hdev, port1, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2722 | USB_PORT_FEAT_C_BH_PORT_RESET); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2723 | usb_clear_port_feature(hub->hdev, port1, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2724 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2725 | usb_clear_port_feature(hub->hdev, port1, |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2726 | USB_PORT_FEAT_C_CONNECTION); |
Sarah Sharp | 1c7439c6 | 2012-11-14 15:58:52 -0800 | [diff] [blame] | 2727 | } |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2728 | if (udev) |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2729 | usb_set_device_state(udev, *status |
| 2730 | ? USB_STATE_NOTATTACHED |
| 2731 | : USB_STATE_DEFAULT); |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2732 | break; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | static int hub_port_reset(struct usb_hub *hub, int port1, |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2738 | struct usb_device *udev, unsigned int delay, bool warm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | { |
| 2740 | int i, status; |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2741 | u16 portchange, portstatus; |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2742 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | |
Sarah Sharp | 0fe51aa | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2744 | if (!hub_is_superspeed(hub->hdev)) { |
| 2745 | if (warm) { |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2746 | dev_err(hub->intfdev, "only USB3 hub support " |
| 2747 | "warm reset\n"); |
| 2748 | return -EINVAL; |
| 2749 | } |
Sarah Sharp | 0fe51aa | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2750 | /* Block EHCI CF initialization during the port reset. |
| 2751 | * Some companion controllers don't like it when they mix. |
| 2752 | */ |
| 2753 | down_read(&ehci_cf_port_reset_rwsem); |
Sarah Sharp | d3b9d7a | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2754 | } else if (!warm) { |
| 2755 | /* |
| 2756 | * If the caller hasn't explicitly requested a warm reset, |
| 2757 | * double check and see if one is needed. |
| 2758 | */ |
| 2759 | status = hub_port_status(hub, port1, |
| 2760 | &portstatus, &portchange); |
| 2761 | if (status < 0) |
| 2762 | goto done; |
| 2763 | |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2764 | if (hub_port_warm_reset_required(hub, port1, portstatus)) |
Sarah Sharp | d3b9d7a | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2765 | warm = true; |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2766 | } |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2767 | clear_bit(port1, hub->warm_reset_bits); |
Alan Stern | 32fe019 | 2007-10-10 16:27:07 -0400 | [diff] [blame] | 2768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | /* Reset the port */ |
| 2770 | for (i = 0; i < PORT_RESET_TRIES; i++) { |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2771 | status = set_port_feature(hub->hdev, port1, (warm ? |
| 2772 | USB_PORT_FEAT_BH_PORT_RESET : |
| 2773 | USB_PORT_FEAT_RESET)); |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 2774 | if (status == -ENODEV) { |
| 2775 | ; /* The hub is gone */ |
| 2776 | } else if (status) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2777 | dev_err(&port_dev->dev, |
| 2778 | "cannot %sreset (err = %d)\n", |
| 2779 | warm ? "warm " : "", status); |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2780 | } else { |
| 2781 | status = hub_port_wait_reset(hub, port1, udev, delay, |
| 2782 | warm); |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 2783 | if (status && status != -ENOTCONN && status != -ENODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | dev_dbg(hub->intfdev, |
| 2785 | "port_wait_reset: err = %d\n", |
| 2786 | status); |
| 2787 | } |
| 2788 | |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2789 | /* Check for disconnect or reset */ |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2790 | if (status == 0 || status == -ENOTCONN || status == -ENODEV) { |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2791 | hub_port_finish_reset(hub, port1, udev, &status); |
| 2792 | |
| 2793 | if (!hub_is_superspeed(hub->hdev)) |
| 2794 | goto done; |
| 2795 | |
| 2796 | /* |
| 2797 | * If a USB 3.0 device migrates from reset to an error |
| 2798 | * state, re-issue the warm reset. |
| 2799 | */ |
| 2800 | if (hub_port_status(hub, port1, |
| 2801 | &portstatus, &portchange) < 0) |
| 2802 | goto done; |
| 2803 | |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2804 | if (!hub_port_warm_reset_required(hub, port1, |
| 2805 | portstatus)) |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2806 | goto done; |
| 2807 | |
| 2808 | /* |
| 2809 | * If the port is in SS.Inactive or Compliance Mode, the |
| 2810 | * hot or warm reset failed. Try another warm reset. |
| 2811 | */ |
| 2812 | if (!warm) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2813 | dev_dbg(&port_dev->dev, |
| 2814 | "hot reset failed, warm reset\n"); |
Sarah Sharp | a24a607 | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2815 | warm = true; |
| 2816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2819 | dev_dbg(&port_dev->dev, |
| 2820 | "not enabled, trying %sreset again...\n", |
| 2821 | warm ? "warm " : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | delay = HUB_LONG_RESET_TIME; |
| 2823 | } |
| 2824 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2825 | dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2827 | done: |
Sarah Sharp | 0fe51aa | 2012-11-01 11:20:44 -0700 | [diff] [blame] | 2828 | if (!hub_is_superspeed(hub->hdev)) |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 2829 | up_read(&ehci_cf_port_reset_rwsem); |
| 2830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2831 | return status; |
| 2832 | } |
| 2833 | |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2834 | /* Check if a port is power on */ |
| 2835 | static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) |
| 2836 | { |
| 2837 | int ret = 0; |
| 2838 | |
| 2839 | if (hub_is_superspeed(hub->hdev)) { |
| 2840 | if (portstatus & USB_SS_PORT_STAT_POWER) |
| 2841 | ret = 1; |
| 2842 | } else { |
| 2843 | if (portstatus & USB_PORT_STAT_POWER) |
| 2844 | ret = 1; |
| 2845 | } |
| 2846 | |
| 2847 | return ret; |
| 2848 | } |
| 2849 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 2850 | static void usb_lock_port(struct usb_port *port_dev) |
| 2851 | __acquires(&port_dev->status_lock) |
| 2852 | { |
| 2853 | mutex_lock(&port_dev->status_lock); |
| 2854 | __acquire(&port_dev->status_lock); |
| 2855 | } |
| 2856 | |
| 2857 | static void usb_unlock_port(struct usb_port *port_dev) |
| 2858 | __releases(&port_dev->status_lock) |
| 2859 | { |
| 2860 | mutex_unlock(&port_dev->status_lock); |
| 2861 | __release(&port_dev->status_lock); |
| 2862 | } |
| 2863 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 2864 | #ifdef CONFIG_PM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2866 | /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ |
| 2867 | static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) |
| 2868 | { |
| 2869 | int ret = 0; |
| 2870 | |
| 2871 | if (hub_is_superspeed(hub->hdev)) { |
| 2872 | if ((portstatus & USB_PORT_STAT_LINK_STATE) |
| 2873 | == USB_SS_PORT_LS_U3) |
| 2874 | ret = 1; |
| 2875 | } else { |
| 2876 | if (portstatus & USB_PORT_STAT_SUSPEND) |
| 2877 | ret = 1; |
| 2878 | } |
| 2879 | |
| 2880 | return ret; |
| 2881 | } |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2882 | |
| 2883 | /* Determine whether the device on a port is ready for a normal resume, |
| 2884 | * is ready for a reset-resume, or should be disconnected. |
| 2885 | */ |
| 2886 | static int check_port_resume_type(struct usb_device *udev, |
| 2887 | struct usb_hub *hub, int port1, |
| 2888 | int status, unsigned portchange, unsigned portstatus) |
| 2889 | { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2890 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 2891 | |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2892 | /* Is a warm reset needed to recover the connection? */ |
| 2893 | if (status == 0 && udev->reset_resume |
| 2894 | && hub_port_warm_reset_required(hub, port1, portstatus)) { |
| 2895 | /* pass */; |
| 2896 | } |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2897 | /* Is the device still present? */ |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 2898 | else if (status || port_is_suspended(hub, portstatus) || |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 2899 | !port_is_power_on(hub, portstatus) || |
| 2900 | !(portstatus & USB_PORT_STAT_CONNECTION)) { |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2901 | if (status >= 0) |
| 2902 | status = -ENODEV; |
| 2903 | } |
| 2904 | |
Alan Stern | 86c57ed | 2008-06-30 11:14:43 -0400 | [diff] [blame] | 2905 | /* Can't do a normal resume if the port isn't enabled, |
| 2906 | * so try a reset-resume instead. |
| 2907 | */ |
| 2908 | else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { |
| 2909 | if (udev->persist_enabled) |
| 2910 | udev->reset_resume = 1; |
| 2911 | else |
| 2912 | status = -ENODEV; |
| 2913 | } |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2914 | |
| 2915 | if (status) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 2916 | dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", |
| 2917 | portchange, portstatus, status); |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2918 | } else if (udev->reset_resume) { |
| 2919 | |
| 2920 | /* Late port handoff can set status-change bits */ |
| 2921 | if (portchange & USB_PORT_STAT_C_CONNECTION) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2922 | usb_clear_port_feature(hub->hdev, port1, |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2923 | USB_PORT_FEAT_C_CONNECTION); |
| 2924 | if (portchange & USB_PORT_STAT_C_ENABLE) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 2925 | usb_clear_port_feature(hub->hdev, port1, |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 2926 | USB_PORT_FEAT_C_ENABLE); |
| 2927 | } |
| 2928 | |
| 2929 | return status; |
| 2930 | } |
| 2931 | |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 2932 | int usb_disable_ltm(struct usb_device *udev) |
| 2933 | { |
| 2934 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 2935 | |
| 2936 | /* Check if the roothub and device supports LTM. */ |
| 2937 | if (!usb_device_supports_ltm(hcd->self.root_hub) || |
| 2938 | !usb_device_supports_ltm(udev)) |
| 2939 | return 0; |
| 2940 | |
| 2941 | /* Clear Feature LTM Enable can only be sent if the device is |
| 2942 | * configured. |
| 2943 | */ |
| 2944 | if (!udev->actconfig) |
| 2945 | return 0; |
| 2946 | |
| 2947 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 2948 | USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, |
| 2949 | USB_DEVICE_LTM_ENABLE, 0, NULL, 0, |
| 2950 | USB_CTRL_SET_TIMEOUT); |
| 2951 | } |
| 2952 | EXPORT_SYMBOL_GPL(usb_disable_ltm); |
| 2953 | |
| 2954 | void usb_enable_ltm(struct usb_device *udev) |
| 2955 | { |
| 2956 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 2957 | |
| 2958 | /* Check if the roothub and device supports LTM. */ |
| 2959 | if (!usb_device_supports_ltm(hcd->self.root_hub) || |
| 2960 | !usb_device_supports_ltm(udev)) |
| 2961 | return; |
| 2962 | |
| 2963 | /* Set Feature LTM Enable can only be sent if the device is |
| 2964 | * configured. |
| 2965 | */ |
| 2966 | if (!udev->actconfig) |
| 2967 | return; |
| 2968 | |
| 2969 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 2970 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, |
| 2971 | USB_DEVICE_LTM_ENABLE, 0, NULL, 0, |
| 2972 | USB_CTRL_SET_TIMEOUT); |
| 2973 | } |
| 2974 | EXPORT_SYMBOL_GPL(usb_enable_ltm); |
| 2975 | |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 2976 | /* |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 2977 | * usb_enable_remote_wakeup - enable remote wakeup for a device |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 2978 | * @udev: target device |
| 2979 | * |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 2980 | * For USB-2 devices: Set the device's remote wakeup feature. |
| 2981 | * |
| 2982 | * For USB-3 devices: Assume there's only one function on the device and |
| 2983 | * enable remote wake for the first interface. FIXME if the interface |
| 2984 | * association descriptor shows there's more than one function. |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 2985 | */ |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 2986 | static int usb_enable_remote_wakeup(struct usb_device *udev) |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 2987 | { |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 2988 | if (udev->speed < USB_SPEED_SUPER) |
| 2989 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 2990 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, |
| 2991 | USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, |
| 2992 | USB_CTRL_SET_TIMEOUT); |
| 2993 | else |
| 2994 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 2995 | USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, |
| 2996 | USB_INTRF_FUNC_SUSPEND, |
| 2997 | USB_INTRF_FUNC_SUSPEND_RW | |
| 2998 | USB_INTRF_FUNC_SUSPEND_LP, |
| 2999 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 3000 | } |
| 3001 | |
| 3002 | /* |
| 3003 | * usb_disable_remote_wakeup - disable remote wakeup for a device |
| 3004 | * @udev: target device |
| 3005 | * |
| 3006 | * For USB-2 devices: Clear the device's remote wakeup feature. |
| 3007 | * |
| 3008 | * For USB-3 devices: Assume there's only one function on the device and |
| 3009 | * disable remote wake for the first interface. FIXME if the interface |
| 3010 | * association descriptor shows there's more than one function. |
| 3011 | */ |
| 3012 | static int usb_disable_remote_wakeup(struct usb_device *udev) |
| 3013 | { |
| 3014 | if (udev->speed < USB_SPEED_SUPER) |
| 3015 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3016 | USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, |
| 3017 | USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, |
| 3018 | USB_CTRL_SET_TIMEOUT); |
| 3019 | else |
| 3020 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 3021 | USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE, |
| 3022 | USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, |
| 3023 | USB_CTRL_SET_TIMEOUT); |
| 3024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3026 | /* Count of wakeup-enabled devices at or below udev */ |
| 3027 | static unsigned wakeup_enabled_descendants(struct usb_device *udev) |
| 3028 | { |
| 3029 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); |
| 3030 | |
| 3031 | return udev->do_remote_wakeup + |
| 3032 | (hub ? hub->wakeup_enabled_descendants : 0); |
| 3033 | } |
| 3034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3035 | /* |
Alan Stern | 140d8f6 | 2006-07-01 22:07:21 -0400 | [diff] [blame] | 3036 | * usb_port_suspend - suspend a usb device's upstream port |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3037 | * @udev: device that's no longer in active use, not a root hub |
David Brownell | 5edbfb7 | 2005-09-22 22:45:26 -0700 | [diff] [blame] | 3038 | * Context: must be able to sleep; device not locked; pm locks held |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | * |
| 3040 | * Suspends a USB device that isn't in active use, conserving power. |
| 3041 | * Devices may wake out of a suspend, if anything important happens, |
| 3042 | * using the remote wakeup mechanism. They may also be taken out of |
Alan Stern | 140d8f6 | 2006-07-01 22:07:21 -0400 | [diff] [blame] | 3043 | * suspend by the host, using usb_port_resume(). It's also routine |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | * to disconnect devices while they are suspended. |
| 3045 | * |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 3046 | * This only affects the USB hardware for a device; its interfaces |
| 3047 | * (and, for hubs, child devices) must already have been suspended. |
| 3048 | * |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3049 | * Selective port suspend reduces power; most suspended devices draw |
| 3050 | * less than 500 uA. It's also used in OTG, along with remote wakeup. |
| 3051 | * All devices below the suspended port are also suspended. |
| 3052 | * |
| 3053 | * Devices leave suspend state when the host wakes them up. Some devices |
| 3054 | * also support "remote wakeup", where the device can activate the USB |
| 3055 | * tree above them to deliver data, such as a keypress or packet. In |
| 3056 | * some cases, this wakes the USB host. |
| 3057 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | * Suspending OTG devices may trigger HNP, if that's been enabled |
| 3059 | * between a pair of dual-role devices. That will change roles, such |
| 3060 | * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. |
| 3061 | * |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 3062 | * Devices on USB hub ports have only one "suspend" state, corresponding |
| 3063 | * to ACPI D2, "may cause the device to lose some context". |
| 3064 | * State transitions include: |
| 3065 | * |
| 3066 | * - suspend, resume ... when the VBUS power link stays live |
| 3067 | * - suspend, disconnect ... VBUS lost |
| 3068 | * |
| 3069 | * Once VBUS drop breaks the circuit, the port it's using has to go through |
| 3070 | * normal re-enumeration procedures, starting with enabling VBUS power. |
| 3071 | * Other than re-initializing the hub (plug/unplug, except for root hubs), |
| 3072 | * Linux (2.6) currently has NO mechanisms to initiate that: no khubd |
| 3073 | * timer, no SRP, no requests through sysfs. |
| 3074 | * |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3075 | * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get |
| 3076 | * suspended until their bus goes into global suspend (i.e., the root |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3077 | * hub is suspended). Nevertheless, we change @udev->state to |
| 3078 | * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual |
| 3079 | * upstream port setting is stored in @udev->port_is_suspended. |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 3080 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3081 | * Returns 0 on success, else negative errno. |
| 3082 | */ |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 3083 | int usb_port_suspend(struct usb_device *udev, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3084 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3085 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
| 3086 | struct usb_port *port_dev = hub->ports[udev->portnum - 1]; |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3087 | int port1 = udev->portnum; |
| 3088 | int status; |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3089 | bool really_suspend = true; |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 3090 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 3091 | usb_lock_port(port_dev); |
| 3092 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3093 | /* enable remote wakeup when appropriate; this lets the device |
| 3094 | * wake up the upstream hub (including maybe the root hub). |
| 3095 | * |
| 3096 | * NOTE: OTG devices may issue remote wakeup (or SRP) even when |
| 3097 | * we don't explicitly enable it here. |
| 3098 | */ |
| 3099 | if (udev->do_remote_wakeup) { |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 3100 | status = usb_enable_remote_wakeup(udev); |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 3101 | if (status) { |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3102 | dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", |
| 3103 | status); |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 3104 | /* bail if autosuspend is requested */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 3105 | if (PMSG_IS_AUTO(msg)) |
Alan Stern | 4fae6f0 | 2013-07-30 15:39:02 -0400 | [diff] [blame] | 3106 | goto err_wakeup; |
Oliver Neukum | 0c48720 | 2009-10-19 13:19:41 +0200 | [diff] [blame] | 3107 | } |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3108 | } |
| 3109 | |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 3110 | /* disable USB2 hardware LPM */ |
| 3111 | if (udev->usb2_hw_lpm_enabled == 1) |
| 3112 | usb_set_usb2_hardware_lpm(udev, 0); |
| 3113 | |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 3114 | if (usb_disable_ltm(udev)) { |
Alan Stern | 4fae6f0 | 2013-07-30 15:39:02 -0400 | [diff] [blame] | 3115 | dev_err(&udev->dev, "Failed to disable LTM before suspend\n."); |
| 3116 | status = -ENOMEM; |
| 3117 | if (PMSG_IS_AUTO(msg)) |
| 3118 | goto err_ltm; |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 3119 | } |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 3120 | if (usb_unlocked_disable_lpm(udev)) { |
Alan Stern | 4fae6f0 | 2013-07-30 15:39:02 -0400 | [diff] [blame] | 3121 | dev_err(&udev->dev, "Failed to disable LPM before suspend\n."); |
| 3122 | status = -ENOMEM; |
| 3123 | if (PMSG_IS_AUTO(msg)) |
| 3124 | goto err_lpm3; |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3127 | /* see 7.1.7.6 */ |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3128 | if (hub_is_superspeed(hub->hdev)) |
Sarah Sharp | c2f60db | 2012-12-10 15:43:08 -0800 | [diff] [blame] | 3129 | status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3130 | |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3131 | /* |
| 3132 | * For system suspend, we do not need to enable the suspend feature |
| 3133 | * on individual USB-2 ports. The devices will automatically go |
| 3134 | * into suspend a few ms after the root hub stops sending packets. |
| 3135 | * The USB 2.0 spec calls this "global suspend". |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3136 | * |
| 3137 | * However, many USB hubs have a bug: They don't relay wakeup requests |
| 3138 | * from a downstream port if the port's suspend feature isn't on. |
| 3139 | * Therefore we will turn on the suspend feature if udev or any of its |
| 3140 | * descendants is enabled for remote wakeup. |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3141 | */ |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3142 | else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0) |
| 3143 | status = set_port_feature(hub->hdev, port1, |
| 3144 | USB_PORT_FEAT_SUSPEND); |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3145 | else { |
| 3146 | really_suspend = false; |
| 3147 | status = 0; |
| 3148 | } |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3149 | if (status) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 3150 | dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3151 | |
Alan Stern | 4fae6f0 | 2013-07-30 15:39:02 -0400 | [diff] [blame] | 3152 | /* Try to enable USB3 LPM and LTM again */ |
| 3153 | usb_unlocked_enable_lpm(udev); |
| 3154 | err_lpm3: |
| 3155 | usb_enable_ltm(udev); |
| 3156 | err_ltm: |
Andiry Xu | c3e751e | 2012-05-05 00:50:10 +0800 | [diff] [blame] | 3157 | /* Try to enable USB2 hardware LPM again */ |
| 3158 | if (udev->usb2_hw_lpm_capable == 1) |
| 3159 | usb_set_usb2_hardware_lpm(udev, 1); |
| 3160 | |
Alan Stern | 4fae6f0 | 2013-07-30 15:39:02 -0400 | [diff] [blame] | 3161 | if (udev->do_remote_wakeup) |
| 3162 | (void) usb_disable_remote_wakeup(udev); |
| 3163 | err_wakeup: |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 3164 | |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3165 | /* System sleep transitions should never fail */ |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 3166 | if (!PMSG_IS_AUTO(msg)) |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3167 | status = 0; |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3168 | } else { |
Alan Stern | 30b1a7a | 2011-09-27 21:54:22 +0200 | [diff] [blame] | 3169 | dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", |
| 3170 | (PMSG_IS_AUTO(msg) ? "auto-" : ""), |
| 3171 | udev->do_remote_wakeup); |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3172 | if (really_suspend) { |
| 3173 | udev->port_is_suspended = 1; |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3174 | |
| 3175 | /* device has up to 10 msec to fully suspend */ |
Alan Stern | 0aa2832 | 2013-03-27 16:14:19 -0400 | [diff] [blame] | 3176 | msleep(10); |
| 3177 | } |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3178 | usb_set_device_state(udev, USB_STATE_SUSPENDED); |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3179 | } |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3180 | |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 3181 | if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled |
| 3182 | && test_and_clear_bit(port1, hub->child_usage_bits)) |
Lan Tianyu | 98a4f1f | 2013-07-03 22:17:54 +0800 | [diff] [blame] | 3183 | pm_runtime_put_sync(&port_dev->dev); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3184 | |
Alan Stern | c08512c | 2010-11-15 15:57:58 -0500 | [diff] [blame] | 3185 | usb_mark_last_busy(hub->hdev); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 3186 | |
| 3187 | usb_unlock_port(port_dev); |
Alan Stern | 4956ecc | 2007-05-30 16:51:28 -0400 | [diff] [blame] | 3188 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3189 | } |
David Brownell | f3f3253 | 2005-09-22 22:37:29 -0700 | [diff] [blame] | 3190 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | /* |
David Brownell | 390a8c3 | 2005-09-13 19:57:27 -0700 | [diff] [blame] | 3192 | * If the USB "suspend" state is in use (rather than "global suspend"), |
| 3193 | * many devices will be individually taken out of suspend state using |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3194 | * special "resume" signaling. This routine kicks in shortly after |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | * hardware resume signaling is finished, either because of selective |
| 3196 | * resume (by host) or remote wakeup (by device) ... now see what changed |
| 3197 | * in the tree that's rooted at this device. |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3198 | * |
| 3199 | * If @udev->reset_resume is set then the device is reset before the |
| 3200 | * status check is done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | */ |
Alan Stern | 140d8f6 | 2006-07-01 22:07:21 -0400 | [diff] [blame] | 3202 | static int finish_port_resume(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | { |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3204 | int status = 0; |
Oliver Neukum | 07e72b9 | 2012-11-29 15:05:57 +0100 | [diff] [blame] | 3205 | u16 devstatus = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | |
| 3207 | /* caller owns the udev device lock */ |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 3208 | dev_dbg(&udev->dev, "%s\n", |
| 3209 | udev->reset_resume ? "finish reset-resume" : "finish resume"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | |
| 3211 | /* usb ch9 identifies four variants of SUSPENDED, based on what |
| 3212 | * state the device resumes to. Linux currently won't see the |
| 3213 | * first two on the host side; they'd be inside hub_port_init() |
| 3214 | * during many timeouts, but khubd can't suspend until later. |
| 3215 | */ |
| 3216 | usb_set_device_state(udev, udev->actconfig |
| 3217 | ? USB_STATE_CONFIGURED |
| 3218 | : USB_STATE_ADDRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3220 | /* 10.5.4.5 says not to reset a suspended port if the attached |
| 3221 | * device is enabled for remote wakeup. Hence the reset |
| 3222 | * operation is carried out here, after the port has been |
| 3223 | * resumed. |
| 3224 | */ |
Alan Stern | 1d10255 | 2014-03-18 10:39:05 -0400 | [diff] [blame] | 3225 | if (udev->reset_resume) { |
| 3226 | /* |
| 3227 | * If the device morphs or switches modes when it is reset, |
| 3228 | * we don't want to perform a reset-resume. We'll fail the |
| 3229 | * resume, which will cause a logical disconnect, and then |
| 3230 | * the device will be rediscovered. |
| 3231 | */ |
Alan Stern | 86c57ed | 2008-06-30 11:14:43 -0400 | [diff] [blame] | 3232 | retry_reset_resume: |
Alan Stern | 1d10255 | 2014-03-18 10:39:05 -0400 | [diff] [blame] | 3233 | if (udev->quirks & USB_QUIRK_RESET) |
| 3234 | status = -ENODEV; |
| 3235 | else |
| 3236 | status = usb_reset_and_verify_device(udev); |
| 3237 | } |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3238 | |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 3239 | /* 10.5.4.5 says be sure devices in the tree are still there. |
| 3240 | * For now let's assume the device didn't go crazy on resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | * and device drivers will know about any resume quirks. |
| 3242 | */ |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3243 | if (status == 0) { |
Alan Stern | 46dede4 | 2007-08-14 10:56:10 -0400 | [diff] [blame] | 3244 | devstatus = 0; |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3245 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); |
Alan Stern | 86c57ed | 2008-06-30 11:14:43 -0400 | [diff] [blame] | 3246 | |
| 3247 | /* If a normal resume failed, try doing a reset-resume */ |
| 3248 | if (status && !udev->reset_resume && udev->persist_enabled) { |
| 3249 | dev_dbg(&udev->dev, "retry with reset-resume\n"); |
| 3250 | udev->reset_resume = 1; |
| 3251 | goto retry_reset_resume; |
| 3252 | } |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3253 | } |
Alan Stern | b40b7a9 | 2006-06-19 15:12:38 -0400 | [diff] [blame] | 3254 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3255 | if (status) { |
| 3256 | dev_dbg(&udev->dev, "gone after usb resume? status %d\n", |
| 3257 | status); |
Oliver Neukum | 07e72b9 | 2012-11-29 15:05:57 +0100 | [diff] [blame] | 3258 | /* |
| 3259 | * There are a few quirky devices which violate the standard |
| 3260 | * by claiming to have remote wakeup enabled after a reset, |
| 3261 | * which crash if the feature is cleared, hence check for |
| 3262 | * udev->reset_resume |
| 3263 | */ |
| 3264 | } else if (udev->actconfig && !udev->reset_resume) { |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 3265 | if (udev->speed < USB_SPEED_SUPER) { |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 3266 | if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 3267 | status = usb_disable_remote_wakeup(udev); |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 3268 | } else { |
| 3269 | status = usb_get_status(udev, USB_RECIP_INTERFACE, 0, |
| 3270 | &devstatus); |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 3271 | if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP |
| 3272 | | USB_INTRF_STAT_FUNC_RW)) |
Alan Stern | 28e86165 | 2013-07-30 15:37:33 -0400 | [diff] [blame] | 3273 | status = usb_disable_remote_wakeup(udev); |
Alan Stern | 4bf0ba8 | 2005-11-21 11:58:07 -0500 | [diff] [blame] | 3274 | } |
Lan Tianyu | 54a3ac0 | 2013-01-24 10:31:28 +0800 | [diff] [blame] | 3275 | |
| 3276 | if (status) |
| 3277 | dev_dbg(&udev->dev, |
| 3278 | "disable remote wakeup, status %d\n", |
| 3279 | status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3280 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | } |
| 3282 | return status; |
| 3283 | } |
| 3284 | |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3285 | /* |
Pratyush Anand | a40178b | 2014-07-18 12:37:10 +0530 | [diff] [blame] | 3286 | * There are some SS USB devices which take longer time for link training. |
| 3287 | * XHCI specs 4.19.4 says that when Link training is successful, port |
| 3288 | * sets CSC bit to 1. So if SW reads port status before successful link |
| 3289 | * training, then it will not find device to be present. |
| 3290 | * USB Analyzer log with such buggy devices show that in some cases |
| 3291 | * device switch on the RX termination after long delay of host enabling |
| 3292 | * the VBUS. In few other cases it has been seen that device fails to |
| 3293 | * negotiate link training in first attempt. It has been |
| 3294 | * reported till now that few devices take as long as 2000 ms to train |
| 3295 | * the link after host enabling its VBUS and termination. Following |
| 3296 | * routine implements a 2000 ms timeout for link training. If in a case |
| 3297 | * link trains before timeout, loop will exit earlier. |
| 3298 | * |
| 3299 | * FIXME: If a device was connected before suspend, but was removed |
| 3300 | * while system was asleep, then the loop in the following routine will |
| 3301 | * only exit at timeout. |
| 3302 | * |
| 3303 | * This routine should only be called when persist is enabled for a SS |
| 3304 | * device. |
| 3305 | */ |
| 3306 | static int wait_for_ss_port_enable(struct usb_device *udev, |
| 3307 | struct usb_hub *hub, int *port1, |
| 3308 | u16 *portchange, u16 *portstatus) |
| 3309 | { |
| 3310 | int status = 0, delay_ms = 0; |
| 3311 | |
| 3312 | while (delay_ms < 2000) { |
| 3313 | if (status || *portstatus & USB_PORT_STAT_CONNECTION) |
| 3314 | break; |
| 3315 | msleep(20); |
| 3316 | delay_ms += 20; |
| 3317 | status = hub_port_status(hub, *port1, portstatus, portchange); |
| 3318 | } |
| 3319 | return status; |
| 3320 | } |
| 3321 | |
| 3322 | /* |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3323 | * usb_port_resume - re-activate a suspended usb device's upstream port |
| 3324 | * @udev: device to re-activate, not a root hub |
| 3325 | * Context: must be able to sleep; device not locked; pm locks held |
| 3326 | * |
| 3327 | * This will re-activate the suspended device, increasing power usage |
| 3328 | * while letting drivers communicate again with its endpoints. |
| 3329 | * USB resume explicitly guarantees that the power session between |
| 3330 | * the host and the device is the same as it was when the device |
| 3331 | * suspended. |
| 3332 | * |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 3333 | * If @udev->reset_resume is set then this routine won't check that the |
| 3334 | * port is still enabled. Furthermore, finish_port_resume() above will |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3335 | * reset @udev. The end result is that a broken power session can be |
| 3336 | * recovered and @udev will appear to persist across a loss of VBUS power. |
| 3337 | * |
| 3338 | * For example, if a host controller doesn't maintain VBUS suspend current |
| 3339 | * during a system sleep or is reset when the system wakes up, all the USB |
| 3340 | * power sessions below it will be broken. This is especially troublesome |
| 3341 | * for mass-storage devices containing mounted filesystems, since the |
| 3342 | * device will appear to have disconnected and all the memory mappings |
| 3343 | * to it will be lost. Using the USB_PERSIST facility, the device can be |
| 3344 | * made to appear as if it had not disconnected. |
| 3345 | * |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 3346 | * This facility can be dangerous. Although usb_reset_and_verify_device() makes |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 3347 | * every effort to insure that the same device is present after the |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3348 | * reset as before, it cannot provide a 100% guarantee. Furthermore it's |
| 3349 | * quite possible for a device to remain unaltered but its media to be |
| 3350 | * changed. If the user replaces a flash memory card while the system is |
| 3351 | * asleep, he will have only himself to blame when the filesystem on the |
| 3352 | * new card is corrupted and the system crashes. |
| 3353 | * |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3354 | * Returns 0 on success, else negative errno. |
| 3355 | */ |
Alan Stern | 65bfd29 | 2008-11-25 16:39:18 -0500 | [diff] [blame] | 3356 | int usb_port_resume(struct usb_device *udev, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3357 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3358 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
| 3359 | struct usb_port *port_dev = hub->ports[udev->portnum - 1]; |
Alan Stern | 624d6c0 | 2007-05-30 15:35:16 -0400 | [diff] [blame] | 3360 | int port1 = udev->portnum; |
| 3361 | int status; |
| 3362 | u16 portchange, portstatus; |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 3363 | |
Dan Williams | d5c3834 | 2014-05-20 18:08:52 -0700 | [diff] [blame] | 3364 | if (!test_and_set_bit(port1, hub->child_usage_bits)) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3365 | status = pm_runtime_get_sync(&port_dev->dev); |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3366 | if (status < 0) { |
| 3367 | dev_dbg(&udev->dev, "can't resume usb port, status %d\n", |
| 3368 | status); |
| 3369 | return status; |
| 3370 | } |
| 3371 | } |
| 3372 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 3373 | usb_lock_port(port_dev); |
| 3374 | |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 3375 | /* Skip the initial Clear-Suspend step for a remote wakeup */ |
| 3376 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 3377 | if (status == 0 && !port_is_suspended(hub, portstatus)) |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 3378 | goto SuspendCleared; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | /* see 7.1.7.7; affects power usage, but not budgeting */ |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3381 | if (hub_is_superspeed(hub->hdev)) |
Sarah Sharp | c2f60db | 2012-12-10 15:43:08 -0800 | [diff] [blame] | 3382 | status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3383 | else |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3384 | status = usb_clear_port_feature(hub->hdev, |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3385 | port1, USB_PORT_FEAT_SUSPEND); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3386 | if (status) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 3387 | dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | /* drive resume for at least 20 msec */ |
Alan Stern | 686314c | 2007-05-30 15:34:36 -0400 | [diff] [blame] | 3390 | dev_dbg(&udev->dev, "usb %sresume\n", |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 3391 | (PMSG_IS_AUTO(msg) ? "auto-" : "")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3392 | msleep(25); |
| 3393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | /* Virtual root hubs can trigger on GET_PORT_STATUS to |
| 3395 | * stop resume signaling. Then finish the resume |
| 3396 | * sequence. |
| 3397 | */ |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 3398 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3399 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 3400 | /* TRSMRCY = 10 msec */ |
| 3401 | msleep(10); |
| 3402 | } |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3403 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 3404 | SuspendCleared: |
| 3405 | if (status == 0) { |
Alan Stern | bfd1e91 | 2012-10-19 11:03:39 -0400 | [diff] [blame] | 3406 | udev->port_is_suspended = 0; |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3407 | if (hub_is_superspeed(hub->hdev)) { |
| 3408 | if (portchange & USB_PORT_STAT_C_LINK_STATE) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3409 | usb_clear_port_feature(hub->hdev, port1, |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3410 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
| 3411 | } else { |
| 3412 | if (portchange & USB_PORT_STAT_C_SUSPEND) |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 3413 | usb_clear_port_feature(hub->hdev, port1, |
Andiry Xu | a711423 | 2011-04-27 18:07:50 +0800 | [diff] [blame] | 3414 | USB_PORT_FEAT_C_SUSPEND); |
| 3415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
Pratyush Anand | a40178b | 2014-07-18 12:37:10 +0530 | [diff] [blame] | 3418 | if (udev->persist_enabled && hub_is_superspeed(hub->hdev)) |
| 3419 | status = wait_for_ss_port_enable(udev, hub, &port1, &portchange, |
| 3420 | &portstatus); |
| 3421 | |
Alan Stern | b01b03f | 2008-04-28 11:06:11 -0400 | [diff] [blame] | 3422 | status = check_port_resume_type(udev, |
| 3423 | hub, port1, status, portchange, portstatus); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3424 | if (status == 0) |
| 3425 | status = finish_port_resume(udev); |
| 3426 | if (status < 0) { |
| 3427 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); |
| 3428 | hub_port_logical_disconnect(hub, port1); |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 3429 | } else { |
| 3430 | /* Try to enable USB2 hardware LPM */ |
| 3431 | if (udev->usb2_hw_lpm_capable == 1) |
| 3432 | usb_set_usb2_hardware_lpm(udev, 1); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 3433 | |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 3434 | /* Try to enable USB3 LTM and LPM */ |
| 3435 | usb_enable_ltm(udev); |
Sarah Sharp | 8306095 | 2012-05-02 14:25:52 -0700 | [diff] [blame] | 3436 | usb_unlocked_enable_lpm(udev); |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3437 | } |
Andiry Xu | 65580b43 | 2011-09-23 14:19:52 -0700 | [diff] [blame] | 3438 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 3439 | usb_unlock_port(port_dev); |
| 3440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | return status; |
| 3442 | } |
| 3443 | |
Alan Stern | 84ebc10 | 2013-03-27 16:14:46 -0400 | [diff] [blame] | 3444 | #ifdef CONFIG_PM_RUNTIME |
| 3445 | |
Alan Stern | 0534d46 | 2010-01-08 12:56:30 -0500 | [diff] [blame] | 3446 | int usb_remote_wakeup(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | { |
| 3448 | int status = 0; |
| 3449 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 3450 | usb_lock_device(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3451 | if (udev->state == USB_STATE_SUSPENDED) { |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 3452 | dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 3453 | status = usb_autoresume_device(udev); |
| 3454 | if (status == 0) { |
| 3455 | /* Let the drivers do their thing, then... */ |
| 3456 | usb_autosuspend_device(udev); |
| 3457 | } |
Alan Stern | d25450c | 2006-11-20 11:14:30 -0500 | [diff] [blame] | 3458 | } |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 3459 | usb_unlock_device(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | return status; |
| 3461 | } |
| 3462 | |
Dan Williams | 7e73be2 | 2014-05-20 18:09:31 -0700 | [diff] [blame] | 3463 | /* Returns 1 if there was a remote wakeup and a connect status change. */ |
| 3464 | static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, |
| 3465 | u16 portstatus, u16 portchange) |
| 3466 | __must_hold(&port_dev->status_lock) |
| 3467 | { |
| 3468 | struct usb_port *port_dev = hub->ports[port - 1]; |
| 3469 | struct usb_device *hdev; |
| 3470 | struct usb_device *udev; |
| 3471 | int connect_change = 0; |
| 3472 | int ret; |
| 3473 | |
| 3474 | hdev = hub->hdev; |
| 3475 | udev = port_dev->child; |
| 3476 | if (!hub_is_superspeed(hdev)) { |
| 3477 | if (!(portchange & USB_PORT_STAT_C_SUSPEND)) |
| 3478 | return 0; |
| 3479 | usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); |
| 3480 | } else { |
| 3481 | if (!udev || udev->state != USB_STATE_SUSPENDED || |
| 3482 | (portstatus & USB_PORT_STAT_LINK_STATE) != |
| 3483 | USB_SS_PORT_LS_U0) |
| 3484 | return 0; |
| 3485 | } |
| 3486 | |
| 3487 | if (udev) { |
| 3488 | /* TRSMRCY = 10 msec */ |
| 3489 | msleep(10); |
| 3490 | |
| 3491 | usb_unlock_port(port_dev); |
| 3492 | ret = usb_remote_wakeup(udev); |
| 3493 | usb_lock_port(port_dev); |
| 3494 | if (ret < 0) |
| 3495 | connect_change = 1; |
| 3496 | } else { |
| 3497 | ret = -ENODEV; |
| 3498 | hub_port_disable(hub, port, 1); |
| 3499 | } |
| 3500 | dev_dbg(&port_dev->dev, "resume, status %d\n", ret); |
| 3501 | return connect_change; |
| 3502 | } |
| 3503 | |
| 3504 | #else |
| 3505 | |
| 3506 | static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, |
| 3507 | u16 portstatus, u16 portchange) |
| 3508 | { |
| 3509 | return 0; |
| 3510 | } |
| 3511 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 3512 | #endif |
| 3513 | |
Ming Lei | e6f30de | 2012-10-24 11:59:24 +0800 | [diff] [blame] | 3514 | static int check_ports_changed(struct usb_hub *hub) |
| 3515 | { |
| 3516 | int port1; |
| 3517 | |
| 3518 | for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { |
| 3519 | u16 portstatus, portchange; |
| 3520 | int status; |
| 3521 | |
| 3522 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
| 3523 | if (!status && portchange) |
| 3524 | return 1; |
| 3525 | } |
| 3526 | return 0; |
| 3527 | } |
| 3528 | |
David Brownell | db69087 | 2005-09-13 19:56:33 -0700 | [diff] [blame] | 3529 | static int hub_suspend(struct usb_interface *intf, pm_message_t msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | { |
| 3531 | struct usb_hub *hub = usb_get_intfdata (intf); |
| 3532 | struct usb_device *hdev = hub->hdev; |
| 3533 | unsigned port1; |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 3534 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3536 | /* |
| 3537 | * Warn if children aren't already suspended. |
| 3538 | * Also, add up the number of wakeup-enabled descendants. |
| 3539 | */ |
| 3540 | hub->wakeup_enabled_descendants = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 3542 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 3543 | struct usb_device *udev = port_dev->child; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | |
Alan Stern | 6840d25 | 2007-09-10 11:34:26 -0400 | [diff] [blame] | 3545 | if (udev && udev->can_submit) { |
Dan Williams | b658b8f | 2014-06-17 16:16:22 -0700 | [diff] [blame] | 3546 | dev_warn(&port_dev->dev, "device %s not suspended yet\n", |
| 3547 | dev_name(&udev->dev)); |
Alan Stern | 5b1b0b8 | 2011-08-19 23:49:48 +0200 | [diff] [blame] | 3548 | if (PMSG_IS_AUTO(msg)) |
Alan Stern | cbb3300 | 2011-06-15 16:29:16 -0400 | [diff] [blame] | 3549 | return -EBUSY; |
David Brownell | c9f89fa | 2005-09-13 19:57:04 -0700 | [diff] [blame] | 3550 | } |
Alan Stern | e583d9d | 2013-07-11 14:58:04 -0400 | [diff] [blame] | 3551 | if (udev) |
| 3552 | hub->wakeup_enabled_descendants += |
| 3553 | wakeup_enabled_descendants(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | } |
Ming Lei | e6f30de | 2012-10-24 11:59:24 +0800 | [diff] [blame] | 3555 | |
| 3556 | if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { |
| 3557 | /* check if there are changes pending on hub ports */ |
| 3558 | if (check_ports_changed(hub)) { |
| 3559 | if (PMSG_IS_AUTO(msg)) |
| 3560 | return -EBUSY; |
| 3561 | pm_wakeup_event(&hdev->dev, 2000); |
| 3562 | } |
| 3563 | } |
| 3564 | |
Sarah Sharp | 4296c70a | 2012-01-06 10:34:31 -0800 | [diff] [blame] | 3565 | if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { |
| 3566 | /* Enable hub to send remote wakeup for all ports. */ |
| 3567 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
| 3568 | status = set_port_feature(hdev, |
| 3569 | port1 | |
| 3570 | USB_PORT_FEAT_REMOTE_WAKE_CONNECT | |
| 3571 | USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | |
| 3572 | USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, |
| 3573 | USB_PORT_FEAT_REMOTE_WAKE_MASK); |
| 3574 | } |
| 3575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3576 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 3577 | dev_dbg(&intf->dev, "%s\n", __func__); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 3578 | |
David Brownell | c9f89fa | 2005-09-13 19:57:04 -0700 | [diff] [blame] | 3579 | /* stop khubd and related activity */ |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 3580 | hub_quiesce(hub, HUB_SUSPEND); |
Alan Stern | b6f6436 | 2007-05-04 11:51:54 -0400 | [diff] [blame] | 3581 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | } |
| 3583 | |
| 3584 | static int hub_resume(struct usb_interface *intf) |
| 3585 | { |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 3586 | struct usb_hub *hub = usb_get_intfdata(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3587 | |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 3588 | dev_dbg(&intf->dev, "%s\n", __func__); |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 3589 | hub_activate(hub, HUB_RESUME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | return 0; |
| 3591 | } |
| 3592 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 3593 | static int hub_reset_resume(struct usb_interface *intf) |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3594 | { |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 3595 | struct usb_hub *hub = usb_get_intfdata(intf); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3596 | |
Alan Stern | 5e6effa | 2008-03-03 15:15:51 -0500 | [diff] [blame] | 3597 | dev_dbg(&intf->dev, "%s\n", __func__); |
Alan Stern | f283521 | 2008-04-28 11:07:17 -0400 | [diff] [blame] | 3598 | hub_activate(hub, HUB_RESET_RESUME); |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 3599 | return 0; |
| 3600 | } |
| 3601 | |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3602 | /** |
| 3603 | * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power |
| 3604 | * @rhdev: struct usb_device for the root hub |
| 3605 | * |
| 3606 | * The USB host controller driver calls this function when its root hub |
| 3607 | * is resumed and Vbus power has been interrupted or the controller |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 3608 | * has been reset. The routine marks @rhdev as having lost power. |
| 3609 | * When the hub driver is resumed it will take notice and carry out |
| 3610 | * power-session recovery for all the "USB-PERSIST"-enabled child devices; |
| 3611 | * the others will be disconnected. |
Alan Stern | 54515fe | 2007-05-30 15:38:58 -0400 | [diff] [blame] | 3612 | */ |
| 3613 | void usb_root_hub_lost_power(struct usb_device *rhdev) |
| 3614 | { |
| 3615 | dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); |
| 3616 | rhdev->reset_resume = 1; |
| 3617 | } |
| 3618 | EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); |
| 3619 | |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3620 | static const char * const usb3_lpm_names[] = { |
| 3621 | "U0", |
| 3622 | "U1", |
| 3623 | "U2", |
| 3624 | "U3", |
| 3625 | }; |
| 3626 | |
| 3627 | /* |
| 3628 | * Send a Set SEL control transfer to the device, prior to enabling |
| 3629 | * device-initiated U1 or U2. This lets the device know the exit latencies from |
| 3630 | * the time the device initiates a U1 or U2 exit, to the time it will receive a |
| 3631 | * packet from the host. |
| 3632 | * |
| 3633 | * This function will fail if the SEL or PEL values for udev are greater than |
| 3634 | * the maximum allowed values for the link state to be enabled. |
| 3635 | */ |
| 3636 | static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) |
| 3637 | { |
| 3638 | struct usb_set_sel_req *sel_values; |
| 3639 | unsigned long long u1_sel; |
| 3640 | unsigned long long u1_pel; |
| 3641 | unsigned long long u2_sel; |
| 3642 | unsigned long long u2_pel; |
| 3643 | int ret; |
| 3644 | |
Xenia Ragiadakou | 38d7f68 | 2013-09-04 17:24:45 +0300 | [diff] [blame] | 3645 | if (udev->state != USB_STATE_CONFIGURED) |
| 3646 | return 0; |
| 3647 | |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3648 | /* Convert SEL and PEL stored in ns to us */ |
| 3649 | u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); |
| 3650 | u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); |
| 3651 | u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); |
| 3652 | u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); |
| 3653 | |
| 3654 | /* |
| 3655 | * Make sure that the calculated SEL and PEL values for the link |
| 3656 | * state we're enabling aren't bigger than the max SEL/PEL |
| 3657 | * value that will fit in the SET SEL control transfer. |
| 3658 | * Otherwise the device would get an incorrect idea of the exit |
| 3659 | * latency for the link state, and could start a device-initiated |
| 3660 | * U1/U2 when the exit latencies are too high. |
| 3661 | */ |
| 3662 | if ((state == USB3_LPM_U1 && |
| 3663 | (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || |
| 3664 | u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || |
| 3665 | (state == USB3_LPM_U2 && |
| 3666 | (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || |
| 3667 | u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { |
Sarah Sharp | 1510a1a | 2012-10-05 10:30:35 -0700 | [diff] [blame] | 3668 | dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n", |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3669 | usb3_lpm_names[state], u1_sel, u1_pel); |
| 3670 | return -EINVAL; |
| 3671 | } |
| 3672 | |
| 3673 | /* |
| 3674 | * If we're enabling device-initiated LPM for one link state, |
| 3675 | * but the other link state has a too high SEL or PEL value, |
| 3676 | * just set those values to the max in the Set SEL request. |
| 3677 | */ |
| 3678 | if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) |
| 3679 | u1_sel = USB3_LPM_MAX_U1_SEL_PEL; |
| 3680 | |
| 3681 | if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) |
| 3682 | u1_pel = USB3_LPM_MAX_U1_SEL_PEL; |
| 3683 | |
| 3684 | if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) |
| 3685 | u2_sel = USB3_LPM_MAX_U2_SEL_PEL; |
| 3686 | |
| 3687 | if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) |
| 3688 | u2_pel = USB3_LPM_MAX_U2_SEL_PEL; |
| 3689 | |
| 3690 | /* |
| 3691 | * usb_enable_lpm() can be called as part of a failed device reset, |
| 3692 | * which may be initiated by an error path of a mass storage driver. |
| 3693 | * Therefore, use GFP_NOIO. |
| 3694 | */ |
| 3695 | sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); |
| 3696 | if (!sel_values) |
| 3697 | return -ENOMEM; |
| 3698 | |
| 3699 | sel_values->u1_sel = u1_sel; |
| 3700 | sel_values->u1_pel = u1_pel; |
| 3701 | sel_values->u2_sel = cpu_to_le16(u2_sel); |
| 3702 | sel_values->u2_pel = cpu_to_le16(u2_pel); |
| 3703 | |
| 3704 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3705 | USB_REQ_SET_SEL, |
| 3706 | USB_RECIP_DEVICE, |
| 3707 | 0, 0, |
| 3708 | sel_values, sizeof *(sel_values), |
| 3709 | USB_CTRL_SET_TIMEOUT); |
| 3710 | kfree(sel_values); |
| 3711 | return ret; |
| 3712 | } |
| 3713 | |
| 3714 | /* |
| 3715 | * Enable or disable device-initiated U1 or U2 transitions. |
| 3716 | */ |
| 3717 | static int usb_set_device_initiated_lpm(struct usb_device *udev, |
| 3718 | enum usb3_link_state state, bool enable) |
| 3719 | { |
| 3720 | int ret; |
| 3721 | int feature; |
| 3722 | |
| 3723 | switch (state) { |
| 3724 | case USB3_LPM_U1: |
| 3725 | feature = USB_DEVICE_U1_ENABLE; |
| 3726 | break; |
| 3727 | case USB3_LPM_U2: |
| 3728 | feature = USB_DEVICE_U2_ENABLE; |
| 3729 | break; |
| 3730 | default: |
| 3731 | dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", |
| 3732 | __func__, enable ? "enable" : "disable"); |
| 3733 | return -EINVAL; |
| 3734 | } |
| 3735 | |
| 3736 | if (udev->state != USB_STATE_CONFIGURED) { |
| 3737 | dev_dbg(&udev->dev, "%s: Can't %s %s state " |
| 3738 | "for unconfigured device.\n", |
| 3739 | __func__, enable ? "enable" : "disable", |
| 3740 | usb3_lpm_names[state]); |
| 3741 | return 0; |
| 3742 | } |
| 3743 | |
| 3744 | if (enable) { |
| 3745 | /* |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3746 | * Now send the control transfer to enable device-initiated LPM |
| 3747 | * for either U1 or U2. |
| 3748 | */ |
| 3749 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3750 | USB_REQ_SET_FEATURE, |
| 3751 | USB_RECIP_DEVICE, |
| 3752 | feature, |
| 3753 | 0, NULL, 0, |
| 3754 | USB_CTRL_SET_TIMEOUT); |
| 3755 | } else { |
| 3756 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 3757 | USB_REQ_CLEAR_FEATURE, |
| 3758 | USB_RECIP_DEVICE, |
| 3759 | feature, |
| 3760 | 0, NULL, 0, |
| 3761 | USB_CTRL_SET_TIMEOUT); |
| 3762 | } |
| 3763 | if (ret < 0) { |
| 3764 | dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", |
| 3765 | enable ? "Enable" : "Disable", |
| 3766 | usb3_lpm_names[state]); |
| 3767 | return -EBUSY; |
| 3768 | } |
| 3769 | return 0; |
| 3770 | } |
| 3771 | |
| 3772 | static int usb_set_lpm_timeout(struct usb_device *udev, |
| 3773 | enum usb3_link_state state, int timeout) |
| 3774 | { |
| 3775 | int ret; |
| 3776 | int feature; |
| 3777 | |
| 3778 | switch (state) { |
| 3779 | case USB3_LPM_U1: |
| 3780 | feature = USB_PORT_FEAT_U1_TIMEOUT; |
| 3781 | break; |
| 3782 | case USB3_LPM_U2: |
| 3783 | feature = USB_PORT_FEAT_U2_TIMEOUT; |
| 3784 | break; |
| 3785 | default: |
| 3786 | dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", |
| 3787 | __func__); |
| 3788 | return -EINVAL; |
| 3789 | } |
| 3790 | |
| 3791 | if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && |
| 3792 | timeout != USB3_LPM_DEVICE_INITIATED) { |
| 3793 | dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " |
| 3794 | "which is a reserved value.\n", |
| 3795 | usb3_lpm_names[state], timeout); |
| 3796 | return -EINVAL; |
| 3797 | } |
| 3798 | |
| 3799 | ret = set_port_feature(udev->parent, |
| 3800 | USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, |
| 3801 | feature); |
| 3802 | if (ret < 0) { |
| 3803 | dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," |
| 3804 | "error code %i\n", usb3_lpm_names[state], |
| 3805 | timeout, ret); |
| 3806 | return -EBUSY; |
| 3807 | } |
| 3808 | if (state == USB3_LPM_U1) |
| 3809 | udev->u1_params.timeout = timeout; |
| 3810 | else |
| 3811 | udev->u2_params.timeout = timeout; |
| 3812 | return 0; |
| 3813 | } |
| 3814 | |
| 3815 | /* |
| 3816 | * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated |
| 3817 | * U1/U2 entry. |
| 3818 | * |
| 3819 | * We will attempt to enable U1 or U2, but there are no guarantees that the |
| 3820 | * control transfers to set the hub timeout or enable device-initiated U1/U2 |
| 3821 | * will be successful. |
| 3822 | * |
| 3823 | * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI |
| 3824 | * driver know about it. If that call fails, it should be harmless, and just |
| 3825 | * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. |
| 3826 | */ |
| 3827 | static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, |
| 3828 | enum usb3_link_state state) |
| 3829 | { |
Sarah Sharp | 65a95b7 | 2012-10-05 10:32:07 -0700 | [diff] [blame] | 3830 | int timeout, ret; |
Sarah Sharp | ae8963a | 2012-10-03 11:18:05 -0700 | [diff] [blame] | 3831 | __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat; |
| 3832 | __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat; |
| 3833 | |
| 3834 | /* If the device says it doesn't have *any* exit latency to come out of |
| 3835 | * U1 or U2, it's probably lying. Assume it doesn't implement that link |
| 3836 | * state. |
| 3837 | */ |
| 3838 | if ((state == USB3_LPM_U1 && u1_mel == 0) || |
| 3839 | (state == USB3_LPM_U2 && u2_mel == 0)) |
| 3840 | return; |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3841 | |
Sarah Sharp | 65a95b7 | 2012-10-05 10:32:07 -0700 | [diff] [blame] | 3842 | /* |
| 3843 | * First, let the device know about the exit latencies |
| 3844 | * associated with the link state we're about to enable. |
| 3845 | */ |
| 3846 | ret = usb_req_set_sel(udev, state); |
| 3847 | if (ret < 0) { |
| 3848 | dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n", |
| 3849 | usb3_lpm_names[state]); |
| 3850 | return; |
| 3851 | } |
| 3852 | |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3853 | /* We allow the host controller to set the U1/U2 timeout internally |
| 3854 | * first, so that it can change its schedule to account for the |
| 3855 | * additional latency to send data to a device in a lower power |
| 3856 | * link state. |
| 3857 | */ |
| 3858 | timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); |
| 3859 | |
| 3860 | /* xHCI host controller doesn't want to enable this LPM state. */ |
| 3861 | if (timeout == 0) |
| 3862 | return; |
| 3863 | |
| 3864 | if (timeout < 0) { |
| 3865 | dev_warn(&udev->dev, "Could not enable %s link state, " |
| 3866 | "xHCI error %i.\n", usb3_lpm_names[state], |
| 3867 | timeout); |
| 3868 | return; |
| 3869 | } |
| 3870 | |
| 3871 | if (usb_set_lpm_timeout(udev, state, timeout)) |
| 3872 | /* If we can't set the parent hub U1/U2 timeout, |
| 3873 | * device-initiated LPM won't be allowed either, so let the xHCI |
| 3874 | * host know that this link state won't be enabled. |
| 3875 | */ |
| 3876 | hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); |
| 3877 | |
| 3878 | /* Only a configured device will accept the Set Feature U1/U2_ENABLE */ |
| 3879 | else if (udev->actconfig) |
| 3880 | usb_set_device_initiated_lpm(udev, state, true); |
| 3881 | |
| 3882 | } |
| 3883 | |
| 3884 | /* |
| 3885 | * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated |
| 3886 | * U1/U2 entry. |
| 3887 | * |
| 3888 | * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. |
| 3889 | * If zero is returned, the parent will not allow the link to go into U1/U2. |
| 3890 | * |
| 3891 | * If zero is returned, device-initiated U1/U2 entry may still be enabled, but |
| 3892 | * it won't have an effect on the bus link state because the parent hub will |
| 3893 | * still disallow device-initiated U1/U2 entry. |
| 3894 | * |
| 3895 | * If zero is returned, the xHCI host controller may still think U1/U2 entry is |
| 3896 | * possible. The result will be slightly more bus bandwidth will be taken up |
| 3897 | * (to account for U1/U2 exit latency), but it should be harmless. |
| 3898 | */ |
| 3899 | static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, |
| 3900 | enum usb3_link_state state) |
| 3901 | { |
| 3902 | int feature; |
| 3903 | |
| 3904 | switch (state) { |
| 3905 | case USB3_LPM_U1: |
| 3906 | feature = USB_PORT_FEAT_U1_TIMEOUT; |
| 3907 | break; |
| 3908 | case USB3_LPM_U2: |
| 3909 | feature = USB_PORT_FEAT_U2_TIMEOUT; |
| 3910 | break; |
| 3911 | default: |
| 3912 | dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", |
| 3913 | __func__); |
| 3914 | return -EINVAL; |
| 3915 | } |
| 3916 | |
| 3917 | if (usb_set_lpm_timeout(udev, state, 0)) |
| 3918 | return -EBUSY; |
| 3919 | |
| 3920 | usb_set_device_initiated_lpm(udev, state, false); |
| 3921 | |
| 3922 | if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) |
| 3923 | dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " |
| 3924 | "bus schedule bandwidth may be impacted.\n", |
| 3925 | usb3_lpm_names[state]); |
| 3926 | return 0; |
| 3927 | } |
| 3928 | |
| 3929 | /* |
| 3930 | * Disable hub-initiated and device-initiated U1 and U2 entry. |
| 3931 | * Caller must own the bandwidth_mutex. |
| 3932 | * |
| 3933 | * This will call usb_enable_lpm() on failure, which will decrement |
| 3934 | * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. |
| 3935 | */ |
| 3936 | int usb_disable_lpm(struct usb_device *udev) |
| 3937 | { |
| 3938 | struct usb_hcd *hcd; |
| 3939 | |
| 3940 | if (!udev || !udev->parent || |
| 3941 | udev->speed != USB_SPEED_SUPER || |
Pratyush Anand | 51df62f | 2014-07-04 17:01:27 +0300 | [diff] [blame] | 3942 | !udev->lpm_capable || |
| 3943 | udev->state < USB_STATE_DEFAULT) |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3944 | return 0; |
| 3945 | |
| 3946 | hcd = bus_to_hcd(udev->bus); |
| 3947 | if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) |
| 3948 | return 0; |
| 3949 | |
| 3950 | udev->lpm_disable_count++; |
Dan Carpenter | 55558c3 | 2012-05-22 20:54:34 +0300 | [diff] [blame] | 3951 | if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 3952 | return 0; |
| 3953 | |
| 3954 | /* If LPM is enabled, attempt to disable it. */ |
| 3955 | if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) |
| 3956 | goto enable_lpm; |
| 3957 | if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) |
| 3958 | goto enable_lpm; |
| 3959 | |
| 3960 | return 0; |
| 3961 | |
| 3962 | enable_lpm: |
| 3963 | usb_enable_lpm(udev); |
| 3964 | return -EBUSY; |
| 3965 | } |
| 3966 | EXPORT_SYMBOL_GPL(usb_disable_lpm); |
| 3967 | |
| 3968 | /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ |
| 3969 | int usb_unlocked_disable_lpm(struct usb_device *udev) |
| 3970 | { |
| 3971 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 3972 | int ret; |
| 3973 | |
| 3974 | if (!hcd) |
| 3975 | return -EINVAL; |
| 3976 | |
| 3977 | mutex_lock(hcd->bandwidth_mutex); |
| 3978 | ret = usb_disable_lpm(udev); |
| 3979 | mutex_unlock(hcd->bandwidth_mutex); |
| 3980 | |
| 3981 | return ret; |
| 3982 | } |
| 3983 | EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); |
| 3984 | |
| 3985 | /* |
| 3986 | * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The |
| 3987 | * xHCI host policy may prevent U1 or U2 from being enabled. |
| 3988 | * |
| 3989 | * Other callers may have disabled link PM, so U1 and U2 entry will be disabled |
| 3990 | * until the lpm_disable_count drops to zero. Caller must own the |
| 3991 | * bandwidth_mutex. |
| 3992 | */ |
| 3993 | void usb_enable_lpm(struct usb_device *udev) |
| 3994 | { |
| 3995 | struct usb_hcd *hcd; |
| 3996 | |
| 3997 | if (!udev || !udev->parent || |
| 3998 | udev->speed != USB_SPEED_SUPER || |
Pratyush Anand | 51df62f | 2014-07-04 17:01:27 +0300 | [diff] [blame] | 3999 | !udev->lpm_capable || |
| 4000 | udev->state < USB_STATE_DEFAULT) |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 4001 | return; |
| 4002 | |
| 4003 | udev->lpm_disable_count--; |
| 4004 | hcd = bus_to_hcd(udev->bus); |
| 4005 | /* Double check that we can both enable and disable LPM. |
| 4006 | * Device must be configured to accept set feature U1/U2 timeout. |
| 4007 | */ |
| 4008 | if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || |
| 4009 | !hcd->driver->disable_usb3_lpm_timeout) |
| 4010 | return; |
| 4011 | |
| 4012 | if (udev->lpm_disable_count > 0) |
| 4013 | return; |
| 4014 | |
| 4015 | usb_enable_link_state(hcd, udev, USB3_LPM_U1); |
| 4016 | usb_enable_link_state(hcd, udev, USB3_LPM_U2); |
| 4017 | } |
| 4018 | EXPORT_SYMBOL_GPL(usb_enable_lpm); |
| 4019 | |
| 4020 | /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ |
| 4021 | void usb_unlocked_enable_lpm(struct usb_device *udev) |
| 4022 | { |
| 4023 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 4024 | |
| 4025 | if (!hcd) |
| 4026 | return; |
| 4027 | |
| 4028 | mutex_lock(hcd->bandwidth_mutex); |
| 4029 | usb_enable_lpm(udev); |
| 4030 | mutex_unlock(hcd->bandwidth_mutex); |
| 4031 | } |
| 4032 | EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); |
| 4033 | |
| 4034 | |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 4035 | #else /* CONFIG_PM */ |
| 4036 | |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 4037 | #define hub_suspend NULL |
| 4038 | #define hub_resume NULL |
| 4039 | #define hub_reset_resume NULL |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 4040 | |
| 4041 | int usb_disable_lpm(struct usb_device *udev) |
| 4042 | { |
| 4043 | return 0; |
| 4044 | } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 4045 | EXPORT_SYMBOL_GPL(usb_disable_lpm); |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 4046 | |
| 4047 | void usb_enable_lpm(struct usb_device *udev) { } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 4048 | EXPORT_SYMBOL_GPL(usb_enable_lpm); |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 4049 | |
| 4050 | int usb_unlocked_disable_lpm(struct usb_device *udev) |
| 4051 | { |
| 4052 | return 0; |
| 4053 | } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 4054 | EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); |
Sarah Sharp | 1ea7e0e | 2012-04-24 17:21:50 -0700 | [diff] [blame] | 4055 | |
| 4056 | void usb_unlocked_enable_lpm(struct usb_device *udev) { } |
Sarah Sharp | e9261fb | 2012-05-21 08:29:01 -0700 | [diff] [blame] | 4057 | EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 4058 | |
| 4059 | int usb_disable_ltm(struct usb_device *udev) |
| 4060 | { |
| 4061 | return 0; |
| 4062 | } |
| 4063 | EXPORT_SYMBOL_GPL(usb_disable_ltm); |
| 4064 | |
| 4065 | void usb_enable_ltm(struct usb_device *udev) { } |
| 4066 | EXPORT_SYMBOL_GPL(usb_enable_ltm); |
Alan Stern | c4b51a4 | 2013-07-11 14:58:23 -0400 | [diff] [blame] | 4067 | |
Stephen Rothwell | 4a95b1f | 2014-05-29 18:55:06 +1000 | [diff] [blame] | 4068 | static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, |
| 4069 | u16 portstatus, u16 portchange) |
| 4070 | { |
| 4071 | return 0; |
| 4072 | } |
| 4073 | |
Alan Stern | c4b51a4 | 2013-07-11 14:58:23 -0400 | [diff] [blame] | 4074 | #endif /* CONFIG_PM */ |
Alan Stern | d388dab | 2006-07-01 22:14:24 -0400 | [diff] [blame] | 4075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | |
| 4077 | /* USB 2.0 spec, 7.1.7.3 / fig 7-29: |
| 4078 | * |
| 4079 | * Between connect detection and reset signaling there must be a delay |
| 4080 | * of 100ms at least for debounce and power-settling. The corresponding |
| 4081 | * timer shall restart whenever the downstream port detects a disconnect. |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4082 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | * Apparently there are some bluetooth and irda-dongles and a number of |
| 4084 | * low-speed devices for which this debounce period may last over a second. |
| 4085 | * Not covered by the spec - but easy to deal with. |
| 4086 | * |
| 4087 | * This implementation uses a 1500ms total debounce timeout; if the |
| 4088 | * connection isn't stable by then it returns -ETIMEDOUT. It checks |
| 4089 | * every 25ms for transient disconnects. When the port status has been |
| 4090 | * unchanged for 100ms it returns the port status. |
| 4091 | */ |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 4092 | int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
| 4094 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4095 | u16 portchange, portstatus; |
| 4096 | unsigned connection = 0xffff; |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4097 | int total_time, stable_time = 0; |
| 4098 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4099 | |
| 4100 | for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { |
| 4101 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
| 4102 | if (ret < 0) |
| 4103 | return ret; |
| 4104 | |
| 4105 | if (!(portchange & USB_PORT_STAT_C_CONNECTION) && |
| 4106 | (portstatus & USB_PORT_STAT_CONNECTION) == connection) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 4107 | if (!must_be_connected || |
| 4108 | (connection == USB_PORT_STAT_CONNECTION)) |
| 4109 | stable_time += HUB_DEBOUNCE_STEP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4110 | if (stable_time >= HUB_DEBOUNCE_STABLE) |
| 4111 | break; |
| 4112 | } else { |
| 4113 | stable_time = 0; |
| 4114 | connection = portstatus & USB_PORT_STAT_CONNECTION; |
| 4115 | } |
| 4116 | |
| 4117 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 4118 | usb_clear_port_feature(hub->hdev, port1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4119 | USB_PORT_FEAT_C_CONNECTION); |
| 4120 | } |
| 4121 | |
| 4122 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) |
| 4123 | break; |
| 4124 | msleep(HUB_DEBOUNCE_STEP); |
| 4125 | } |
| 4126 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4127 | dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", |
| 4128 | total_time, stable_time, portstatus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | |
| 4130 | if (stable_time < HUB_DEBOUNCE_STABLE) |
| 4131 | return -ETIMEDOUT; |
| 4132 | return portstatus; |
| 4133 | } |
| 4134 | |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4135 | void usb_ep0_reinit(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4136 | { |
Alan Stern | ddeac4e | 2009-01-15 17:03:33 -0500 | [diff] [blame] | 4137 | usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); |
| 4138 | usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); |
Alan Stern | 2caf7fc | 2008-12-31 11:31:33 -0500 | [diff] [blame] | 4139 | usb_enable_endpoint(udev, &udev->ep0, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | } |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4141 | EXPORT_SYMBOL_GPL(usb_ep0_reinit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4142 | |
| 4143 | #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) |
| 4144 | #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) |
| 4145 | |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 4146 | static int hub_set_address(struct usb_device *udev, int devnum) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | { |
| 4148 | int retval; |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4149 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4150 | |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4151 | /* |
| 4152 | * The host controller will choose the device address, |
| 4153 | * instead of the core having chosen it earlier |
| 4154 | */ |
| 4155 | if (!hcd->driver->address_device && devnum <= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | return -EINVAL; |
| 4157 | if (udev->state == USB_STATE_ADDRESS) |
| 4158 | return 0; |
| 4159 | if (udev->state != USB_STATE_DEFAULT) |
| 4160 | return -EINVAL; |
Andiry Xu | c8d4af8 | 2010-10-14 07:22:51 -0700 | [diff] [blame] | 4161 | if (hcd->driver->address_device) |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4162 | retval = hcd->driver->address_device(hcd, udev); |
Andiry Xu | c8d4af8 | 2010-10-14 07:22:51 -0700 | [diff] [blame] | 4163 | else |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4164 | retval = usb_control_msg(udev, usb_sndaddr0pipe(), |
| 4165 | USB_REQ_SET_ADDRESS, 0, devnum, 0, |
| 4166 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4167 | if (retval == 0) { |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4168 | update_devnum(udev, devnum); |
David Vrabel | 4953d14 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4169 | /* Device now using proper address. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | usb_set_device_state(udev, USB_STATE_ADDRESS); |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4171 | usb_ep0_reinit(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | } |
| 4173 | return retval; |
| 4174 | } |
| 4175 | |
Mathias Nyman | 890dae8 | 2013-09-30 17:26:31 +0300 | [diff] [blame] | 4176 | /* |
| 4177 | * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM |
| 4178 | * when they're plugged into a USB 2.0 port, but they don't work when LPM is |
| 4179 | * enabled. |
| 4180 | * |
| 4181 | * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the |
| 4182 | * device says it supports the new USB 2.0 Link PM errata by setting the BESL |
| 4183 | * support bit in the BOS descriptor. |
| 4184 | */ |
| 4185 | static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) |
| 4186 | { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4187 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
| 4188 | int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN; |
Mathias Nyman | 890dae8 | 2013-09-30 17:26:31 +0300 | [diff] [blame] | 4189 | |
| 4190 | if (!udev->usb2_hw_lpm_capable) |
| 4191 | return; |
| 4192 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4193 | if (hub) |
| 4194 | connect_type = hub->ports[udev->portnum - 1]->connect_type; |
Mathias Nyman | 890dae8 | 2013-09-30 17:26:31 +0300 | [diff] [blame] | 4195 | |
Bjørn Mork | 23c0582 | 2014-02-27 14:23:55 +0100 | [diff] [blame] | 4196 | if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || |
Mathias Nyman | 890dae8 | 2013-09-30 17:26:31 +0300 | [diff] [blame] | 4197 | connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { |
| 4198 | udev->usb2_hw_lpm_allowed = 1; |
| 4199 | usb_set_usb2_hardware_lpm(udev, 1); |
| 4200 | } |
| 4201 | } |
| 4202 | |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4203 | static int hub_enable_device(struct usb_device *udev) |
| 4204 | { |
| 4205 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
| 4206 | |
| 4207 | if (!hcd->driver->enable_device) |
| 4208 | return 0; |
| 4209 | if (udev->state == USB_STATE_ADDRESS) |
| 4210 | return 0; |
| 4211 | if (udev->state != USB_STATE_DEFAULT) |
| 4212 | return -EINVAL; |
| 4213 | |
| 4214 | return hcd->driver->enable_device(hcd, udev); |
| 4215 | } |
| 4216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | /* Reset device, (re)assign address, get device descriptor. |
| 4218 | * Device connection must be stable, no more debouncing needed. |
| 4219 | * Returns device in USB_STATE_ADDRESS, except on error. |
| 4220 | * |
| 4221 | * If this is called for an already-existing device (as part of |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4222 | * usb_reset_and_verify_device), the caller must own the device lock and |
| 4223 | * the port lock. For a newly detected device that is not accessible |
| 4224 | * through any global pointers, it's not necessary to lock the device, |
| 4225 | * but it is still necessary to lock the port. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | */ |
| 4227 | static int |
| 4228 | hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, |
| 4229 | int retry_counter) |
| 4230 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | struct usb_device *hdev = hub->hdev; |
Sarah Sharp | e7b7717 | 2009-04-27 19:54:26 -0700 | [diff] [blame] | 4232 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | int i, j, retval; |
| 4234 | unsigned delay = HUB_SHORT_RESET_TIME; |
| 4235 | enum usb_device_speed oldspeed = udev->speed; |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 4236 | const char *speed; |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 4237 | int devnum = udev->devnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | |
| 4239 | /* root hub ports have a slightly longer reset period |
| 4240 | * (from USB 2.0 spec, section 7.1.7.5) |
| 4241 | */ |
| 4242 | if (!hdev->parent) { |
| 4243 | delay = HUB_ROOT_RESET_TIME; |
| 4244 | if (port1 == hdev->bus->otg_port) |
| 4245 | hdev->bus->b_hnp_enable = 0; |
| 4246 | } |
| 4247 | |
| 4248 | /* Some low speed devices have problems with the quick delay, so */ |
| 4249 | /* be a bit pessimistic with those devices. RHbug #23670 */ |
| 4250 | if (oldspeed == USB_SPEED_LOW) |
| 4251 | delay = HUB_LONG_RESET_TIME; |
| 4252 | |
Todd E Brandt | 6fecd4f | 2014-05-19 10:55:32 -0700 | [diff] [blame] | 4253 | mutex_lock(&hdev->bus->usb_address0_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | |
Luben Tuikov | 07194ab | 2011-02-11 11:33:10 -0800 | [diff] [blame] | 4255 | /* Reset the device; full speed may morph to high speed */ |
| 4256 | /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 4257 | retval = hub_port_reset(hub, port1, udev, delay, false); |
Luben Tuikov | 07194ab | 2011-02-11 11:33:10 -0800 | [diff] [blame] | 4258 | if (retval < 0) /* error or disconnect */ |
| 4259 | goto fail; |
| 4260 | /* success, speed is known */ |
| 4261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | retval = -ENODEV; |
| 4263 | |
| 4264 | if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { |
| 4265 | dev_dbg(&udev->dev, "device reset changed speed!\n"); |
| 4266 | goto fail; |
| 4267 | } |
| 4268 | oldspeed = udev->speed; |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 4269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4270 | /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... |
| 4271 | * it's fixed size except for full speed devices. |
Inaky Perez-Gonzalez | 5bb6e0a | 2006-08-25 19:35:30 -0700 | [diff] [blame] | 4272 | * For Wireless USB devices, ep0 max packet is always 512 (tho |
| 4273 | * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4274 | */ |
| 4275 | switch (udev->speed) { |
Sarah Sharp | 6b403b0 | 2009-04-27 19:54:10 -0700 | [diff] [blame] | 4276 | case USB_SPEED_SUPER: |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 4277 | case USB_SPEED_WIRELESS: /* fixed at 512 */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 4278 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); |
Inaky Perez-Gonzalez | 5bb6e0a | 2006-08-25 19:35:30 -0700 | [diff] [blame] | 4279 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | case USB_SPEED_HIGH: /* fixed at 64 */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 4281 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4282 | break; |
| 4283 | case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ |
| 4284 | /* to determine the ep0 maxpacket size, try to read |
| 4285 | * the device descriptor to get bMaxPacketSize0 and |
| 4286 | * then correct our initial guess. |
| 4287 | */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 4288 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4289 | break; |
| 4290 | case USB_SPEED_LOW: /* fixed at 8 */ |
Harvey Harrison | 551509d | 2009-02-11 14:11:36 -0800 | [diff] [blame] | 4291 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | break; |
| 4293 | default: |
| 4294 | goto fail; |
| 4295 | } |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 4296 | |
| 4297 | if (udev->speed == USB_SPEED_WIRELESS) |
| 4298 | speed = "variable speed Wireless"; |
| 4299 | else |
| 4300 | speed = usb_speed_string(udev->speed); |
| 4301 | |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4302 | if (udev->speed != USB_SPEED_SUPER) |
| 4303 | dev_info(&udev->dev, |
Michal Nazarewicz | e538dfd | 2011-08-30 17:11:19 +0200 | [diff] [blame] | 4304 | "%s %s USB device number %d using %s\n", |
| 4305 | (udev->config) ? "reset" : "new", speed, |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4306 | devnum, udev->bus->controller->driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | |
| 4308 | /* Set up TT records, if needed */ |
| 4309 | if (hdev->tt) { |
| 4310 | udev->tt = hdev->tt; |
| 4311 | udev->ttport = hdev->ttport; |
| 4312 | } else if (udev->speed != USB_SPEED_HIGH |
| 4313 | && hdev->speed == USB_SPEED_HIGH) { |
Alan Stern | d199c96 | 2011-01-31 10:56:37 -0500 | [diff] [blame] | 4314 | if (!hub->tt.hub) { |
| 4315 | dev_err(&udev->dev, "parent hub has no TT\n"); |
| 4316 | retval = -EINVAL; |
| 4317 | goto fail; |
| 4318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | udev->tt = &hub->tt; |
| 4320 | udev->ttport = port1; |
| 4321 | } |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? |
| 4324 | * Because device hardware and firmware is sometimes buggy in |
| 4325 | * this area, and this is how Linux has done it for ages. |
| 4326 | * Change it cautiously. |
| 4327 | * |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4328 | * NOTE: If use_new_scheme() is true we will start by issuing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | * a 64-byte GET_DESCRIPTOR request. This is what Windows does, |
| 4330 | * so it may help with some non-standards-compliant devices. |
| 4331 | * Otherwise we start with SET_ADDRESS and then try to read the |
| 4332 | * first 8 bytes of the device descriptor to get the ep0 maxpacket |
| 4333 | * value. |
| 4334 | */ |
| 4335 | for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4336 | bool did_new_scheme = false; |
| 4337 | |
| 4338 | if (use_new_scheme(udev, retry_counter)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | struct usb_device_descriptor *buf; |
| 4340 | int r = 0; |
| 4341 | |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4342 | did_new_scheme = true; |
| 4343 | retval = hub_enable_device(udev); |
Oliver Neukum | 938569e | 2014-02-27 10:57:10 +0100 | [diff] [blame] | 4344 | if (retval < 0) { |
| 4345 | dev_err(&udev->dev, |
| 4346 | "hub failed to enable device, error %d\n", |
| 4347 | retval); |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4348 | goto fail; |
Oliver Neukum | 938569e | 2014-02-27 10:57:10 +0100 | [diff] [blame] | 4349 | } |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | #define GET_DESCRIPTOR_BUFSIZE 64 |
| 4352 | buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); |
| 4353 | if (!buf) { |
| 4354 | retval = -ENOMEM; |
| 4355 | continue; |
| 4356 | } |
| 4357 | |
Alan Stern | b89ee19 | 2007-05-11 10:19:04 -0400 | [diff] [blame] | 4358 | /* Retry on all errors; some devices are flakey. |
| 4359 | * 255 is for WUSB devices, we actually need to use |
| 4360 | * 512 (WUSB1.0[4.8.1]). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | */ |
| 4362 | for (j = 0; j < 3; ++j) { |
| 4363 | buf->bMaxPacketSize0 = 0; |
| 4364 | r = usb_control_msg(udev, usb_rcvaddr0pipe(), |
| 4365 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, |
| 4366 | USB_DT_DEVICE << 8, 0, |
| 4367 | buf, GET_DESCRIPTOR_BUFSIZE, |
Jaroslav Kysela | fd7c519 | 2008-10-10 16:24:45 +0200 | [diff] [blame] | 4368 | initial_descriptor_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | switch (buf->bMaxPacketSize0) { |
Inaky Perez-Gonzalez | 5bb6e0a | 2006-08-25 19:35:30 -0700 | [diff] [blame] | 4370 | case 8: case 16: case 32: case 64: case 255: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | if (buf->bDescriptorType == |
| 4372 | USB_DT_DEVICE) { |
| 4373 | r = 0; |
| 4374 | break; |
| 4375 | } |
| 4376 | /* FALL THROUGH */ |
| 4377 | default: |
| 4378 | if (r == 0) |
| 4379 | r = -EPROTO; |
| 4380 | break; |
| 4381 | } |
| 4382 | if (r == 0) |
| 4383 | break; |
| 4384 | } |
| 4385 | udev->descriptor.bMaxPacketSize0 = |
| 4386 | buf->bMaxPacketSize0; |
| 4387 | kfree(buf); |
| 4388 | |
Andiry Xu | 75d7cf7 | 2011-09-13 16:41:11 -0700 | [diff] [blame] | 4389 | retval = hub_port_reset(hub, port1, udev, delay, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | if (retval < 0) /* error or disconnect */ |
| 4391 | goto fail; |
| 4392 | if (oldspeed != udev->speed) { |
| 4393 | dev_dbg(&udev->dev, |
| 4394 | "device reset changed speed!\n"); |
| 4395 | retval = -ENODEV; |
| 4396 | goto fail; |
| 4397 | } |
| 4398 | if (r) { |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4399 | if (r != -ENODEV) |
| 4400 | dev_err(&udev->dev, "device descriptor read/64, error %d\n", |
| 4401 | r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | retval = -EMSGSIZE; |
| 4403 | continue; |
| 4404 | } |
| 4405 | #undef GET_DESCRIPTOR_BUFSIZE |
| 4406 | } |
| 4407 | |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4408 | /* |
| 4409 | * If device is WUSB, we already assigned an |
| 4410 | * unauthorized address in the Connect Ack sequence; |
| 4411 | * authorization will assign the final address. |
| 4412 | */ |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4413 | if (udev->wusb == 0) { |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4414 | for (j = 0; j < SET_ADDRESS_TRIES; ++j) { |
| 4415 | retval = hub_set_address(udev, devnum); |
| 4416 | if (retval >= 0) |
| 4417 | break; |
| 4418 | msleep(200); |
| 4419 | } |
| 4420 | if (retval < 0) { |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4421 | if (retval != -ENODEV) |
| 4422 | dev_err(&udev->dev, "device not accepting address %d, error %d\n", |
| 4423 | devnum, retval); |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4424 | goto fail; |
| 4425 | } |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4426 | if (udev->speed == USB_SPEED_SUPER) { |
| 4427 | devnum = udev->devnum; |
| 4428 | dev_info(&udev->dev, |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4429 | "%s SuperSpeed USB device number %d using %s\n", |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4430 | (udev->config) ? "reset" : "new", |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4431 | devnum, udev->bus->controller->driver->name); |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4432 | } |
Inaky Perez-Gonzalez | 6c529cd | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4433 | |
| 4434 | /* cope with hardware quirkiness: |
| 4435 | * - let SET_ADDRESS settle, some device hardware wants it |
| 4436 | * - read ep0 maxpacket even for high and low speed, |
| 4437 | */ |
| 4438 | msleep(10); |
Dan Williams | 48fc7db | 2013-12-05 17:07:27 -0800 | [diff] [blame] | 4439 | /* use_new_scheme() checks the speed which may have |
| 4440 | * changed since the initial look so we cache the result |
| 4441 | * in did_new_scheme |
| 4442 | */ |
| 4443 | if (did_new_scheme) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | break; |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | |
| 4447 | retval = usb_get_device_descriptor(udev, 8); |
| 4448 | if (retval < 8) { |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4449 | if (retval != -ENODEV) |
| 4450 | dev_err(&udev->dev, |
Wu Fengguang | b9cef6c | 2008-12-15 15:32:01 +0800 | [diff] [blame] | 4451 | "device descriptor read/8, error %d\n", |
| 4452 | retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4453 | if (retval >= 0) |
| 4454 | retval = -EMSGSIZE; |
| 4455 | } else { |
| 4456 | retval = 0; |
| 4457 | break; |
| 4458 | } |
| 4459 | } |
| 4460 | if (retval) |
| 4461 | goto fail; |
| 4462 | |
Peter Chen | b76baa8 | 2012-11-09 09:44:43 +0800 | [diff] [blame] | 4463 | if (hcd->phy && !hdev->parent) |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 4464 | usb_phy_notify_connect(hcd->phy, udev->speed); |
Peter Chen | b76baa8 | 2012-11-09 09:44:43 +0800 | [diff] [blame] | 4465 | |
Elric Fu | d8aec3d | 2012-03-26 21:16:02 +0800 | [diff] [blame] | 4466 | /* |
| 4467 | * Some superspeed devices have finished the link training process |
| 4468 | * and attached to a superspeed hub port, but the device descriptor |
| 4469 | * got from those devices show they aren't superspeed devices. Warm |
| 4470 | * reset the port attached by the devices can fix them. |
| 4471 | */ |
| 4472 | if ((udev->speed == USB_SPEED_SUPER) && |
| 4473 | (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { |
| 4474 | dev_err(&udev->dev, "got a wrong device descriptor, " |
| 4475 | "warm reset device\n"); |
| 4476 | hub_port_reset(hub, port1, udev, |
| 4477 | HUB_BH_RESET_TIME, true); |
| 4478 | retval = -EINVAL; |
| 4479 | goto fail; |
| 4480 | } |
| 4481 | |
Sarah Sharp | 6b403b0 | 2009-04-27 19:54:10 -0700 | [diff] [blame] | 4482 | if (udev->descriptor.bMaxPacketSize0 == 0xff || |
| 4483 | udev->speed == USB_SPEED_SUPER) |
| 4484 | i = 512; |
| 4485 | else |
| 4486 | i = udev->descriptor.bMaxPacketSize0; |
Kuninori Morimoto | 29cc889 | 2011-08-23 03:12:03 -0700 | [diff] [blame] | 4487 | if (usb_endpoint_maxp(&udev->ep0.desc) != i) { |
Alan Stern | 56626a7 | 2010-10-14 15:25:21 -0400 | [diff] [blame] | 4488 | if (udev->speed == USB_SPEED_LOW || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4489 | !(i == 8 || i == 16 || i == 32 || i == 64)) { |
Alan Stern | 56626a7 | 2010-10-14 15:25:21 -0400 | [diff] [blame] | 4490 | dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | retval = -EMSGSIZE; |
| 4492 | goto fail; |
| 4493 | } |
Alan Stern | 56626a7 | 2010-10-14 15:25:21 -0400 | [diff] [blame] | 4494 | if (udev->speed == USB_SPEED_FULL) |
| 4495 | dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); |
| 4496 | else |
| 4497 | dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4499 | usb_ep0_reinit(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4500 | } |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4502 | retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); |
| 4503 | if (retval < (signed)sizeof(udev->descriptor)) { |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4504 | if (retval != -ENODEV) |
| 4505 | dev_err(&udev->dev, "device descriptor read/all, error %d\n", |
| 4506 | retval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | if (retval >= 0) |
| 4508 | retval = -ENOMSG; |
| 4509 | goto fail; |
| 4510 | } |
| 4511 | |
Andiry Xu | 1ff4df5 | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 4512 | if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { |
| 4513 | retval = usb_get_bos_descriptor(udev); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 4514 | if (!retval) { |
Sarah Sharp | d9b2099 | 2012-02-20 08:31:26 -0800 | [diff] [blame] | 4515 | udev->lpm_capable = usb_device_supports_lpm(udev); |
Sarah Sharp | 51e0a01 | 2012-02-20 12:02:19 -0800 | [diff] [blame] | 4516 | usb_set_lpm_parameters(udev); |
| 4517 | } |
Andiry Xu | 1ff4df5 | 2011-09-23 14:19:48 -0700 | [diff] [blame] | 4518 | } |
Andiry Xu | 3148bf0 | 2011-09-23 14:19:47 -0700 | [diff] [blame] | 4519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | retval = 0; |
Alek Du | 48f2497 | 2010-06-04 15:47:55 +0800 | [diff] [blame] | 4521 | /* notify HCD that we have a device connected and addressed */ |
| 4522 | if (hcd->driver->update_device) |
| 4523 | hcd->driver->update_device(hcd, udev); |
Mathias Nyman | 890dae8 | 2013-09-30 17:26:31 +0300 | [diff] [blame] | 4524 | hub_set_initial_usb2_lpm_policy(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4525 | fail: |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 4526 | if (retval) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | hub_port_disable(hub, port1, 0); |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4528 | update_devnum(udev, devnum); /* for disconnect processing */ |
Alan Stern | 4326ed0 | 2007-07-30 17:08:43 -0400 | [diff] [blame] | 4529 | } |
Todd E Brandt | 6fecd4f | 2014-05-19 10:55:32 -0700 | [diff] [blame] | 4530 | mutex_unlock(&hdev->bus->usb_address0_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | return retval; |
| 4532 | } |
| 4533 | |
| 4534 | static void |
| 4535 | check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) |
| 4536 | { |
| 4537 | struct usb_qualifier_descriptor *qual; |
| 4538 | int status; |
| 4539 | |
Johan Hovold | 2a15938 | 2014-08-25 17:51:26 +0200 | [diff] [blame] | 4540 | if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) |
| 4541 | return; |
| 4542 | |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 4543 | qual = kmalloc (sizeof *qual, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | if (qual == NULL) |
| 4545 | return; |
| 4546 | |
| 4547 | status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, |
| 4548 | qual, sizeof *qual); |
| 4549 | if (status == sizeof *qual) { |
| 4550 | dev_info(&udev->dev, "not running at top speed; " |
| 4551 | "connect to a high speed hub\n"); |
| 4552 | /* hub LEDs are probably harder to miss than syslog */ |
| 4553 | if (hub->has_indicators) { |
| 4554 | hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; |
Shaibal Dutta | 22f6a0f | 2014-02-01 19:16:46 -0800 | [diff] [blame] | 4555 | queue_delayed_work(system_power_efficient_wq, |
| 4556 | &hub->leds, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | } |
| 4558 | } |
Jesper Juhl | 1bc3c9e | 2005-04-18 17:39:34 -0700 | [diff] [blame] | 4559 | kfree(qual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | static unsigned |
| 4563 | hub_power_remaining (struct usb_hub *hub) |
| 4564 | { |
| 4565 | struct usb_device *hdev = hub->hdev; |
| 4566 | int remaining; |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4567 | int port1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4569 | if (!hub->limited_power) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | return 0; |
| 4571 | |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4572 | remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; |
| 4573 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4574 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 4575 | struct usb_device *udev = port_dev->child; |
| 4576 | unsigned unit_load; |
| 4577 | int delta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | |
| 4579 | if (!udev) |
| 4580 | continue; |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 4581 | if (hub_is_superspeed(udev)) |
| 4582 | unit_load = 150; |
| 4583 | else |
| 4584 | unit_load = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 4586 | /* |
| 4587 | * Unconfigured devices may not use more than one unit load, |
| 4588 | * or 8mA for OTG ports |
| 4589 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | if (udev->actconfig) |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 4591 | delta = usb_get_max_power(udev, udev->actconfig); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4592 | else if (port1 != udev->bus->otg_port || hdev->parent) |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 4593 | delta = unit_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4594 | else |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4595 | delta = 8; |
| 4596 | if (delta > hub->mA_per_port) |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4597 | dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", |
| 4598 | delta, hub->mA_per_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | remaining -= delta; |
| 4600 | } |
| 4601 | if (remaining < 0) { |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4602 | dev_warn(hub->intfdev, "%dmA over power budget!\n", |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4603 | -remaining); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | remaining = 0; |
| 4605 | } |
| 4606 | return remaining; |
| 4607 | } |
| 4608 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4609 | static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, |
| 4610 | u16 portchange) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4611 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | int status, i; |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 4613 | unsigned unit_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4614 | struct usb_device *hdev = hub->hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4615 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4616 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4617 | struct usb_device *udev = port_dev->child; |
Oliver Neukum | 5ee0f80 | 2014-07-14 15:39:49 +0200 | [diff] [blame] | 4618 | static int unreliable_port = -1; |
Alan Stern | 8808f00 | 2008-04-28 11:06:55 -0400 | [diff] [blame] | 4619 | |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 4620 | /* Disconnect any existing devices under this port */ |
Peter Chen | b76baa8 | 2012-11-09 09:44:43 +0800 | [diff] [blame] | 4621 | if (udev) { |
| 4622 | if (hcd->phy && !hdev->parent && |
| 4623 | !(portstatus & USB_PORT_STAT_CONNECTION)) |
Peter Chen | ac96511 | 2012-11-09 09:44:44 +0800 | [diff] [blame] | 4624 | usb_phy_notify_disconnect(hcd->phy, udev->speed); |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4625 | usb_disconnect(&port_dev->child); |
Peter Chen | b76baa8 | 2012-11-09 09:44:43 +0800 | [diff] [blame] | 4626 | } |
Alan Stern | 24618b0 | 2008-04-28 11:06:28 -0400 | [diff] [blame] | 4627 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 4628 | /* We can forget about a "removed" device when there's a physical |
| 4629 | * disconnect or the connect status changes. |
| 4630 | */ |
| 4631 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || |
| 4632 | (portchange & USB_PORT_STAT_C_CONNECTION)) |
| 4633 | clear_bit(port1, hub->removed_bits); |
| 4634 | |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4635 | if (portchange & (USB_PORT_STAT_C_CONNECTION | |
| 4636 | USB_PORT_STAT_C_ENABLE)) { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 4637 | status = hub_port_debounce_be_stable(hub, port1); |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4638 | if (status < 0) { |
Oliver Neukum | 5ee0f80 | 2014-07-14 15:39:49 +0200 | [diff] [blame] | 4639 | if (status != -ENODEV && |
| 4640 | port1 != unreliable_port && |
| 4641 | printk_ratelimit()) |
Takashi Iwai | dd5f500 | 2014-08-19 17:37:55 +0200 | [diff] [blame] | 4642 | dev_err(&port_dev->dev, "connect-debounce failed\n"); |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4643 | portstatus &= ~USB_PORT_STAT_CONNECTION; |
Oliver Neukum | 5ee0f80 | 2014-07-14 15:39:49 +0200 | [diff] [blame] | 4644 | unreliable_port = port1; |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4645 | } else { |
| 4646 | portstatus = status; |
| 4647 | } |
| 4648 | } |
| 4649 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 4650 | /* Return now if debouncing failed or nothing is connected or |
| 4651 | * the device was "removed". |
| 4652 | */ |
| 4653 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || |
| 4654 | test_bit(port1, hub->removed_bits)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | |
| 4656 | /* maybe switch power back on (e.g. root hub was reset) */ |
Dan Williams | 9262c19 | 2014-05-20 18:08:12 -0700 | [diff] [blame] | 4657 | if (hub_is_port_power_switchable(hub) |
Andiry Xu | 0ed9a57 | 2011-04-27 18:07:43 +0800 | [diff] [blame] | 4658 | && !port_is_power_on(hub, portstatus)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
Alan Stern | 5257d97 | 2008-09-22 14:43:08 -0400 | [diff] [blame] | 4660 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | if (portstatus & USB_PORT_STAT_ENABLE) |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4662 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4663 | return; |
| 4664 | } |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 4665 | if (hub_is_superspeed(hub->hdev)) |
| 4666 | unit_load = 150; |
| 4667 | else |
| 4668 | unit_load = 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4670 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | for (i = 0; i < SET_CONFIG_TRIES; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | |
| 4673 | /* reallocate for each attempt, since references |
| 4674 | * to the previous one can escape in various ways |
| 4675 | */ |
| 4676 | udev = usb_alloc_dev(hdev, hdev->bus, port1); |
| 4677 | if (!udev) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4678 | dev_err(&port_dev->dev, |
| 4679 | "couldn't allocate usb_device\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | goto done; |
| 4681 | } |
| 4682 | |
| 4683 | usb_set_device_state(udev, USB_STATE_POWERED); |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4684 | udev->bus_mA = hub->mA_per_port; |
Alan Stern | b6956ff | 2006-08-30 15:46:48 -0400 | [diff] [blame] | 4685 | udev->level = hdev->level + 1; |
Inaky Perez-Gonzalez | 8af548d | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4686 | udev->wusb = hub_is_wusb(hub); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 4687 | |
Sarah Sharp | 131dec3 | 2010-12-06 21:00:19 -0800 | [diff] [blame] | 4688 | /* Only USB 3.0 devices are connected to SuperSpeed hubs. */ |
| 4689 | if (hub_is_superspeed(hub->hdev)) |
Sarah Sharp | e7b7717 | 2009-04-27 19:54:26 -0700 | [diff] [blame] | 4690 | udev->speed = USB_SPEED_SUPER; |
| 4691 | else |
| 4692 | udev->speed = USB_SPEED_UNKNOWN; |
| 4693 | |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4694 | choose_devnum(udev); |
Andiry Xu | c8d4af8 | 2010-10-14 07:22:51 -0700 | [diff] [blame] | 4695 | if (udev->devnum <= 0) { |
| 4696 | status = -ENOTCONN; /* Don't retry */ |
| 4697 | goto loop; |
Sarah Sharp | c651527 | 2009-04-27 19:57:26 -0700 | [diff] [blame] | 4698 | } |
| 4699 | |
Sarah Sharp | e7b7717 | 2009-04-27 19:54:26 -0700 | [diff] [blame] | 4700 | /* reset (non-USB 3.0 devices) and get descriptor */ |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4701 | usb_lock_port(port_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | status = hub_port_init(hub, udev, port1, i); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4703 | usb_unlock_port(port_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | if (status < 0) |
| 4705 | goto loop; |
| 4706 | |
Phil Dibowitz | 93362a8 | 2010-07-22 00:05:01 +0200 | [diff] [blame] | 4707 | usb_detect_quirks(udev); |
| 4708 | if (udev->quirks & USB_QUIRK_DELAY_INIT) |
| 4709 | msleep(1000); |
| 4710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4711 | /* consecutive bus-powered hubs aren't reliable; they can |
| 4712 | * violate the voltage drop budget. if the new child has |
| 4713 | * a "powered" LED, users should notice we didn't enable it |
| 4714 | * (without reading syslog), even without per-port LEDs |
| 4715 | * on the parent. |
| 4716 | */ |
| 4717 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB |
Sebastian Andrzej Siewior | 430ee58 | 2012-12-18 15:25:47 +0100 | [diff] [blame] | 4718 | && udev->bus_mA <= unit_load) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4719 | u16 devstat; |
| 4720 | |
| 4721 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, |
| 4722 | &devstat); |
Alan Stern | 15b7336 | 2013-07-30 15:35:40 -0400 | [diff] [blame] | 4723 | if (status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | dev_dbg(&udev->dev, "get status %d ?\n", status); |
| 4725 | goto loop_disable; |
| 4726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
| 4728 | dev_err(&udev->dev, |
| 4729 | "can't connect bus-powered hub " |
| 4730 | "to this port\n"); |
| 4731 | if (hub->has_indicators) { |
| 4732 | hub->indicator[port1-1] = |
| 4733 | INDICATOR_AMBER_BLINK; |
Shaibal Dutta | 22f6a0f | 2014-02-01 19:16:46 -0800 | [diff] [blame] | 4734 | queue_delayed_work( |
| 4735 | system_power_efficient_wq, |
| 4736 | &hub->leds, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | } |
| 4738 | status = -ENOTCONN; /* Don't retry */ |
| 4739 | goto loop_disable; |
| 4740 | } |
| 4741 | } |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | /* check for devices running slower than they could */ |
| 4744 | if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 |
| 4745 | && udev->speed == USB_SPEED_FULL |
| 4746 | && highspeed_hubs != 0) |
| 4747 | check_highspeed (hub, udev, port1); |
| 4748 | |
Greg Kroah-Hartman | fa28618 | 2012-05-14 09:20:37 -0700 | [diff] [blame] | 4749 | /* Store the parent's children[] pointer. At this point |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | * udev becomes globally accessible, although presumably |
| 4751 | * no one will look at it until hdev is unlocked. |
| 4752 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | status = 0; |
| 4754 | |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 4755 | mutex_lock(&usb_port_peer_mutex); |
| 4756 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | /* We mustn't add new devices if the parent hub has |
| 4758 | * been disconnected; we would race with the |
| 4759 | * recursively_mark_NOTATTACHED() routine. |
| 4760 | */ |
| 4761 | spin_lock_irq(&device_state_lock); |
| 4762 | if (hdev->state == USB_STATE_NOTATTACHED) |
| 4763 | status = -ENOTCONN; |
| 4764 | else |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4765 | port_dev->child = udev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4766 | spin_unlock_irq(&device_state_lock); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 4767 | mutex_unlock(&usb_port_peer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4768 | |
| 4769 | /* Run it through the hoops (find a driver, etc) */ |
| 4770 | if (!status) { |
| 4771 | status = usb_new_device(udev); |
| 4772 | if (status) { |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 4773 | mutex_lock(&usb_port_peer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4774 | spin_lock_irq(&device_state_lock); |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4775 | port_dev->child = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | spin_unlock_irq(&device_state_lock); |
Dan Williams | d8521af | 2014-05-20 18:08:28 -0700 | [diff] [blame] | 4777 | mutex_unlock(&usb_port_peer_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | } |
| 4779 | } |
| 4780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | if (status) |
| 4782 | goto loop_disable; |
| 4783 | |
| 4784 | status = hub_power_remaining(hub); |
| 4785 | if (status) |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4786 | dev_dbg(hub->intfdev, "%dmA power budget left\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | |
| 4788 | return; |
| 4789 | |
| 4790 | loop_disable: |
| 4791 | hub_port_disable(hub, port1, 1); |
| 4792 | loop: |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 4793 | usb_ep0_reinit(udev); |
Alan Stern | 3b29b68 | 2011-02-22 09:53:41 -0500 | [diff] [blame] | 4794 | release_devnum(udev); |
Herbert Xu | f7410ce | 2010-01-10 20:15:03 +1100 | [diff] [blame] | 4795 | hub_free_dev(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4796 | usb_put_dev(udev); |
Vikram Pandita | ffcdc18 | 2007-05-25 21:31:07 -0700 | [diff] [blame] | 4797 | if ((status == -ENOTCONN) || (status == -ENOTSUPP)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | break; |
| 4799 | } |
Alan Stern | 3a31155 | 2008-05-20 16:58:29 -0400 | [diff] [blame] | 4800 | if (hub->hdev->parent || |
| 4801 | !hcd->driver->port_handed_over || |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4802 | !(hcd->driver->port_handed_over)(hcd, port1)) { |
| 4803 | if (status != -ENOTCONN && status != -ENODEV) |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 4804 | dev_err(&port_dev->dev, |
| 4805 | "unable to enumerate USB device\n"); |
Alan Stern | e9e88fb | 2013-03-27 16:14:01 -0400 | [diff] [blame] | 4806 | } |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 4807 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | done: |
| 4809 | hub_port_disable(hub, port1, 1); |
Balaji Rao | 90da096 | 2007-11-22 01:58:14 +0530 | [diff] [blame] | 4810 | if (hcd->driver->relinquish_port && !hub->hdev->parent) |
| 4811 | hcd->driver->relinquish_port(hcd, port1); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | } |
| 4814 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4815 | /* Handle physical or logical connection change events. |
| 4816 | * This routine is called when: |
| 4817 | * a port connection-change occurs; |
| 4818 | * a port enable-change occurs (often caused by EMI); |
| 4819 | * usb_reset_and_verify_device() encounters changed descriptors (as from |
| 4820 | * a firmware download) |
| 4821 | * caller already locked the hub |
| 4822 | */ |
| 4823 | static void hub_port_connect_change(struct usb_hub *hub, int port1, |
| 4824 | u16 portstatus, u16 portchange) |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4825 | __must_hold(&port_dev->status_lock) |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4826 | { |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4827 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 4828 | struct usb_device *udev = port_dev->child; |
| 4829 | int status = -ENODEV; |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4830 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4831 | dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, |
| 4832 | portchange, portspeed(hub, portstatus)); |
| 4833 | |
| 4834 | if (hub->has_indicators) { |
| 4835 | set_port_led(hub, port1, HUB_LED_AUTO); |
| 4836 | hub->indicator[port1-1] = INDICATOR_AUTO; |
Sarah Sharp | 4ee823b | 2011-11-14 18:00:01 -0800 | [diff] [blame] | 4837 | } |
| 4838 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4839 | #ifdef CONFIG_USB_OTG |
| 4840 | /* during HNP, don't repeat the debounce */ |
| 4841 | if (hub->hdev->bus->is_b_host) |
| 4842 | portchange &= ~(USB_PORT_STAT_C_CONNECTION | |
| 4843 | USB_PORT_STAT_C_ENABLE); |
| 4844 | #endif |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4845 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4846 | /* Try to resuscitate an existing device */ |
| 4847 | if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && |
| 4848 | udev->state != USB_STATE_NOTATTACHED) { |
| 4849 | if (portstatus & USB_PORT_STAT_ENABLE) { |
| 4850 | status = 0; /* Nothing to do */ |
| 4851 | #ifdef CONFIG_PM_RUNTIME |
| 4852 | } else if (udev->state == USB_STATE_SUSPENDED && |
| 4853 | udev->persist_enabled) { |
| 4854 | /* For a suspended device, treat this as a |
| 4855 | * remote wakeup event. |
| 4856 | */ |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4857 | usb_unlock_port(port_dev); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4858 | status = usb_remote_wakeup(udev); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4859 | usb_lock_port(port_dev); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4860 | #endif |
| 4861 | } else { |
| 4862 | /* Don't resuscitate */; |
| 4863 | } |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4864 | } |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4865 | clear_bit(port1, hub->change_bits); |
| 4866 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4867 | /* successfully revalidated the connection */ |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4868 | if (status == 0) |
| 4869 | return; |
| 4870 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4871 | usb_unlock_port(port_dev); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4872 | hub_port_connect(hub, port1, portstatus, portchange); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4873 | usb_lock_port(port_dev); |
Sarah Sharp | 714b07b | 2012-01-24 13:53:18 -0800 | [diff] [blame] | 4874 | } |
| 4875 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4876 | static void port_event(struct usb_hub *hub, int port1) |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4877 | __must_hold(&port_dev->status_lock) |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4878 | { |
| 4879 | int connect_change, reset_device = 0; |
| 4880 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
| 4881 | struct usb_device *udev = port_dev->child; |
| 4882 | struct usb_device *hdev = hub->hdev; |
| 4883 | u16 portstatus, portchange; |
| 4884 | |
| 4885 | connect_change = test_bit(port1, hub->change_bits); |
| 4886 | clear_bit(port1, hub->event_bits); |
| 4887 | clear_bit(port1, hub->wakeup_bits); |
| 4888 | |
| 4889 | if (hub_port_status(hub, port1, &portstatus, &portchange) < 0) |
| 4890 | return; |
| 4891 | |
| 4892 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
| 4893 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); |
| 4894 | connect_change = 1; |
| 4895 | } |
| 4896 | |
| 4897 | if (portchange & USB_PORT_STAT_C_ENABLE) { |
| 4898 | if (!connect_change) |
| 4899 | dev_dbg(&port_dev->dev, "enable change, status %08x\n", |
| 4900 | portstatus); |
| 4901 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); |
| 4902 | |
| 4903 | /* |
| 4904 | * EM interference sometimes causes badly shielded USB devices |
| 4905 | * to be shutdown by the hub, this hack enables them again. |
| 4906 | * Works at least with mouse driver. |
| 4907 | */ |
| 4908 | if (!(portstatus & USB_PORT_STAT_ENABLE) |
| 4909 | && !connect_change && udev) { |
| 4910 | dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); |
| 4911 | connect_change = 1; |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
| 4916 | u16 status = 0, unused; |
| 4917 | |
| 4918 | dev_dbg(&port_dev->dev, "over-current change\n"); |
| 4919 | usb_clear_port_feature(hdev, port1, |
| 4920 | USB_PORT_FEAT_C_OVER_CURRENT); |
| 4921 | msleep(100); /* Cool down */ |
| 4922 | hub_power_on(hub, true); |
| 4923 | hub_port_status(hub, port1, &status, &unused); |
| 4924 | if (status & USB_PORT_STAT_OVERCURRENT) |
| 4925 | dev_err(&port_dev->dev, "over-current condition\n"); |
| 4926 | } |
| 4927 | |
| 4928 | if (portchange & USB_PORT_STAT_C_RESET) { |
| 4929 | dev_dbg(&port_dev->dev, "reset change\n"); |
| 4930 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET); |
| 4931 | } |
| 4932 | if ((portchange & USB_PORT_STAT_C_BH_RESET) |
| 4933 | && hub_is_superspeed(hdev)) { |
| 4934 | dev_dbg(&port_dev->dev, "warm reset change\n"); |
| 4935 | usb_clear_port_feature(hdev, port1, |
| 4936 | USB_PORT_FEAT_C_BH_PORT_RESET); |
| 4937 | } |
| 4938 | if (portchange & USB_PORT_STAT_C_LINK_STATE) { |
| 4939 | dev_dbg(&port_dev->dev, "link state change\n"); |
| 4940 | usb_clear_port_feature(hdev, port1, |
| 4941 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
| 4942 | } |
| 4943 | if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { |
| 4944 | dev_warn(&port_dev->dev, "config error\n"); |
| 4945 | usb_clear_port_feature(hdev, port1, |
| 4946 | USB_PORT_FEAT_C_PORT_CONFIG_ERROR); |
| 4947 | } |
| 4948 | |
Dan Williams | 097a155 | 2014-05-20 18:09:20 -0700 | [diff] [blame] | 4949 | /* skip port actions that require the port to be powered on */ |
| 4950 | if (!pm_runtime_active(&port_dev->dev)) |
| 4951 | return; |
| 4952 | |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4953 | if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) |
| 4954 | connect_change = 1; |
| 4955 | |
| 4956 | /* |
| 4957 | * Warm reset a USB3 protocol port if it's in |
| 4958 | * SS.Inactive state. |
| 4959 | */ |
Dan Williams | 3cd12f9 | 2014-05-29 12:58:46 -0700 | [diff] [blame] | 4960 | if (hub_port_warm_reset_required(hub, port1, portstatus)) { |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4961 | dev_dbg(&port_dev->dev, "do warm reset\n"); |
| 4962 | if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION) |
| 4963 | || udev->state == USB_STATE_NOTATTACHED) { |
| 4964 | if (hub_port_reset(hub, port1, NULL, |
| 4965 | HUB_BH_RESET_TIME, true) < 0) |
| 4966 | hub_port_disable(hub, port1, 1); |
| 4967 | } else |
| 4968 | reset_device = 1; |
| 4969 | } |
| 4970 | |
| 4971 | /* |
| 4972 | * On disconnect USB3 protocol ports transit from U0 to |
| 4973 | * SS.Inactive to Rx.Detect. If this happens a warm- |
| 4974 | * reset is not needed, but a (re)connect may happen |
| 4975 | * before khubd runs and sees the disconnect, and the |
| 4976 | * device may be an unknown state. |
| 4977 | * |
| 4978 | * If the port went through SS.Inactive without khubd |
| 4979 | * seeing it the C_LINK_STATE change flag will be set, |
| 4980 | * and we reset the dev to put it in a known state. |
| 4981 | */ |
| 4982 | if (reset_device || (udev && hub_is_superspeed(hub->hdev) |
| 4983 | && (portchange & USB_PORT_STAT_C_LINK_STATE) |
| 4984 | && (portstatus & USB_PORT_STAT_CONNECTION))) { |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4985 | usb_unlock_port(port_dev); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4986 | usb_lock_device(udev); |
| 4987 | usb_reset_device(udev); |
| 4988 | usb_unlock_device(udev); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 4989 | usb_lock_port(port_dev); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 4990 | connect_change = 0; |
| 4991 | } |
| 4992 | |
| 4993 | if (connect_change) |
| 4994 | hub_port_connect_change(hub, port1, portstatus, portchange); |
| 4995 | } |
| 4996 | |
| 4997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4998 | static void hub_events(void) |
| 4999 | { |
| 5000 | struct list_head *tmp; |
| 5001 | struct usb_device *hdev; |
| 5002 | struct usb_interface *intf; |
| 5003 | struct usb_hub *hub; |
| 5004 | struct device *hub_dev; |
| 5005 | u16 hubstatus; |
| 5006 | u16 hubchange; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5008 | |
| 5009 | /* |
| 5010 | * We restart the list every time to avoid a deadlock with |
| 5011 | * deleting hubs downstream from this one. This should be |
| 5012 | * safe since we delete the hub from the event list. |
| 5013 | * Not the most efficient, but avoids deadlocks. |
| 5014 | */ |
| 5015 | while (1) { |
| 5016 | |
| 5017 | /* Grab the first entry at the beginning of the list */ |
| 5018 | spin_lock_irq(&hub_event_lock); |
| 5019 | if (list_empty(&hub_event_list)) { |
| 5020 | spin_unlock_irq(&hub_event_lock); |
| 5021 | break; |
| 5022 | } |
| 5023 | |
| 5024 | tmp = hub_event_list.next; |
| 5025 | list_del_init(tmp); |
| 5026 | |
| 5027 | hub = list_entry(tmp, struct usb_hub, event_list); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 5028 | kref_get(&hub->kref); |
Joe Lawrence | c605f3c | 2014-09-10 15:07:50 -0400 | [diff] [blame] | 5029 | hdev = hub->hdev; |
| 5030 | usb_get_dev(hdev); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 5031 | spin_unlock_irq(&hub_event_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5032 | |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 5033 | hub_dev = hub->intfdev; |
| 5034 | intf = to_usb_interface(hub_dev); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 5035 | dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", |
Krzysztof Mazur | 3bbc47d | 2013-08-22 14:49:40 +0200 | [diff] [blame] | 5036 | hdev->state, hdev->maxchild, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5037 | /* NOTE: expects max 15 ports... */ |
| 5038 | (u16) hub->change_bits[0], |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 5039 | (u16) hub->event_bits[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | /* Lock the device, then check to see if we were |
| 5042 | * disconnected while waiting for the lock to succeed. */ |
Alan Stern | 06b84e8 | 2007-05-04 11:54:50 -0400 | [diff] [blame] | 5043 | usb_lock_device(hdev); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 5044 | if (unlikely(hub->disconnected)) |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 5045 | goto loop_disconnected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | |
| 5047 | /* If the hub has died, clean up after it */ |
| 5048 | if (hdev->state == USB_STATE_NOTATTACHED) { |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 5049 | hub->error = -ENODEV; |
Alan Stern | 4330354 | 2008-04-28 11:07:31 -0400 | [diff] [blame] | 5050 | hub_quiesce(hub, HUB_DISCONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | goto loop; |
| 5052 | } |
| 5053 | |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 5054 | /* Autoresume */ |
| 5055 | ret = usb_autopm_get_interface(intf); |
| 5056 | if (ret) { |
| 5057 | dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | goto loop; |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | /* If this is an inactive hub, do nothing */ |
| 5062 | if (hub->quiescing) |
| 5063 | goto loop_autopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5064 | |
| 5065 | if (hub->error) { |
| 5066 | dev_dbg (hub_dev, "resetting for error %d\n", |
| 5067 | hub->error); |
| 5068 | |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5069 | ret = usb_reset_device(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5070 | if (ret) { |
| 5071 | dev_dbg (hub_dev, |
| 5072 | "error resetting hub: %d\n", ret); |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 5073 | goto loop_autopm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5074 | } |
| 5075 | |
| 5076 | hub->nerrors = 0; |
| 5077 | hub->error = 0; |
| 5078 | } |
| 5079 | |
| 5080 | /* deal with port status changes */ |
Krzysztof Mazur | 3bbc47d | 2013-08-22 14:49:40 +0200 | [diff] [blame] | 5081 | for (i = 1; i <= hdev->maxchild; i++) { |
Dan Williams | 097a155 | 2014-05-20 18:09:20 -0700 | [diff] [blame] | 5082 | struct usb_port *port_dev = hub->ports[i - 1]; |
Hans de Goede | a82b76f | 2013-11-08 13:41:05 +0100 | [diff] [blame] | 5083 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5084 | if (test_bit(i, hub->event_bits) |
| 5085 | || test_bit(i, hub->change_bits) |
| 5086 | || test_bit(i, hub->wakeup_bits)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | /* |
Dan Williams | 097a155 | 2014-05-20 18:09:20 -0700 | [diff] [blame] | 5088 | * The get_noresume and barrier ensure that if |
| 5089 | * the port was in the process of resuming, we |
| 5090 | * flush that work and keep the port active for |
| 5091 | * the duration of the port_event(). However, |
| 5092 | * if the port is runtime pm suspended |
| 5093 | * (powered-off), we leave it in that state, run |
| 5094 | * an abbreviated port_event(), and move on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5095 | */ |
Dan Williams | 097a155 | 2014-05-20 18:09:20 -0700 | [diff] [blame] | 5096 | pm_runtime_get_noresume(&port_dev->dev); |
| 5097 | pm_runtime_barrier(&port_dev->dev); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5098 | usb_lock_port(port_dev); |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 5099 | port_event(hub, i); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5100 | usb_unlock_port(port_dev); |
Dan Williams | 097a155 | 2014-05-20 18:09:20 -0700 | [diff] [blame] | 5101 | pm_runtime_put_sync(&port_dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | } |
Dan Williams | af376a4 | 2014-05-20 18:09:15 -0700 | [diff] [blame] | 5103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | |
| 5105 | /* deal with hub status changes */ |
| 5106 | if (test_and_clear_bit(0, hub->event_bits) == 0) |
| 5107 | ; /* do nothing */ |
| 5108 | else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) |
| 5109 | dev_err (hub_dev, "get_hub_status failed\n"); |
| 5110 | else { |
| 5111 | if (hubchange & HUB_CHANGE_LOCAL_POWER) { |
| 5112 | dev_dbg (hub_dev, "power change\n"); |
| 5113 | clear_hub_feature(hdev, C_HUB_LOCAL_POWER); |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 5114 | if (hubstatus & HUB_STATUS_LOCAL_POWER) |
| 5115 | /* FIXME: Is this always true? */ |
Alan Stern | 55c5271 | 2005-11-23 12:03:12 -0500 | [diff] [blame] | 5116 | hub->limited_power = 1; |
jidong xiao | 403fae7 | 2007-09-14 00:08:51 +0800 | [diff] [blame] | 5117 | else |
| 5118 | hub->limited_power = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | } |
| 5120 | if (hubchange & HUB_CHANGE_OVERCURRENT) { |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 5121 | u16 status = 0; |
| 5122 | u16 unused; |
| 5123 | |
| 5124 | dev_dbg(hub_dev, "over-current change\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5125 | clear_hub_feature(hdev, C_HUB_OVER_CURRENT); |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 5126 | msleep(500); /* Cool down */ |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5127 | hub_power_on(hub, true); |
Paul Bolle | 752d57a | 2011-03-11 18:03:52 +0100 | [diff] [blame] | 5128 | hub_hub_status(hub, &status, &unused); |
| 5129 | if (status & HUB_STATUS_OVERCURRENT) |
| 5130 | dev_err(hub_dev, "over-current " |
| 5131 | "condition\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | } |
| 5133 | } |
| 5134 | |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 5135 | loop_autopm: |
| 5136 | /* Balance the usb_autopm_get_interface() above */ |
| 5137 | usb_autopm_put_interface_no_suspend(intf); |
| 5138 | loop: |
| 5139 | /* Balance the usb_autopm_get_interface_no_resume() in |
| 5140 | * kick_khubd() and allow autosuspend. |
| 5141 | */ |
| 5142 | usb_autopm_put_interface(intf); |
Alan Stern | 9bbdf1e | 2010-01-08 12:57:28 -0500 | [diff] [blame] | 5143 | loop_disconnected: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5144 | usb_unlock_device(hdev); |
Joe Lawrence | c605f3c | 2014-09-10 15:07:50 -0400 | [diff] [blame] | 5145 | usb_put_dev(hdev); |
Alan Stern | e805485 | 2007-05-04 11:55:11 -0400 | [diff] [blame] | 5146 | kref_put(&hub->kref, hub_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5148 | } /* end while (1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | static int hub_thread(void *__unused) |
| 5152 | { |
Rahul Bedarkar | 025d443 | 2014-01-04 11:24:41 +0530 | [diff] [blame] | 5153 | /* khubd needs to be freezable to avoid interfering with USB-PERSIST |
Alan Stern | 3bb1af5 | 2008-03-03 15:15:36 -0500 | [diff] [blame] | 5154 | * port handover. Otherwise it might see that a full-speed device |
| 5155 | * was gone before the EHCI controller had handed its port over to |
| 5156 | * the companion full-speed controller. |
| 5157 | */ |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 5158 | set_freezable(); |
Alan Stern | 3bb1af5 | 2008-03-03 15:15:36 -0500 | [diff] [blame] | 5159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5160 | do { |
| 5161 | hub_events(); |
Rafael J. Wysocki | e42837b | 2007-10-18 03:04:45 -0700 | [diff] [blame] | 5162 | wait_event_freezable(khubd_wait, |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 5163 | !list_empty(&hub_event_list) || |
| 5164 | kthread_should_stop()); |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 5165 | } while (!kthread_should_stop() || !list_empty(&hub_event_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5166 | |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 5167 | pr_debug("%s: khubd exiting\n", usbcore_name); |
| 5168 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
Németh Márton | 1e927d9 | 2010-01-10 15:34:53 +0100 | [diff] [blame] | 5171 | static const struct usb_device_id hub_id_table[] = { |
Ming Lei | e6f30de | 2012-10-24 11:59:24 +0800 | [diff] [blame] | 5172 | { .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5173 | | USB_DEVICE_ID_MATCH_INT_CLASS, |
Ming Lei | e6f30de | 2012-10-24 11:59:24 +0800 | [diff] [blame] | 5174 | .idVendor = USB_VENDOR_GENESYS_LOGIC, |
| 5175 | .bInterfaceClass = USB_CLASS_HUB, |
| 5176 | .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, |
| 5178 | .bDeviceClass = USB_CLASS_HUB}, |
| 5179 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, |
| 5180 | .bInterfaceClass = USB_CLASS_HUB}, |
| 5181 | { } /* Terminating entry */ |
| 5182 | }; |
| 5183 | |
| 5184 | MODULE_DEVICE_TABLE (usb, hub_id_table); |
| 5185 | |
| 5186 | static struct usb_driver hub_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | .name = "hub", |
| 5188 | .probe = hub_probe, |
| 5189 | .disconnect = hub_disconnect, |
| 5190 | .suspend = hub_suspend, |
| 5191 | .resume = hub_resume, |
Alan Stern | f07600c | 2007-05-30 15:38:16 -0400 | [diff] [blame] | 5192 | .reset_resume = hub_reset_resume, |
Alan Stern | 7de18d8 | 2006-06-01 13:37:24 -0400 | [diff] [blame] | 5193 | .pre_reset = hub_pre_reset, |
| 5194 | .post_reset = hub_post_reset, |
Andi Kleen | c532b29 | 2010-06-01 23:04:41 +0200 | [diff] [blame] | 5195 | .unlocked_ioctl = hub_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | .id_table = hub_id_table, |
Alan Stern | 40f122f | 2006-11-09 14:44:33 -0500 | [diff] [blame] | 5197 | .supports_autosuspend = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5198 | }; |
| 5199 | |
| 5200 | int usb_hub_init(void) |
| 5201 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | if (usb_register(&hub_driver) < 0) { |
| 5203 | printk(KERN_ERR "%s: can't register hub driver\n", |
| 5204 | usbcore_name); |
| 5205 | return -1; |
| 5206 | } |
| 5207 | |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 5208 | khubd_task = kthread_run(hub_thread, NULL, "khubd"); |
| 5209 | if (!IS_ERR(khubd_task)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5210 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | |
| 5212 | /* Fall through if kernel_thread failed */ |
| 5213 | usb_deregister(&hub_driver); |
| 5214 | printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); |
| 5215 | |
| 5216 | return -1; |
| 5217 | } |
| 5218 | |
| 5219 | void usb_hub_cleanup(void) |
| 5220 | { |
akpm@osdl.org | 9c8d617 | 2005-06-20 14:29:58 -0700 | [diff] [blame] | 5221 | kthread_stop(khubd_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5222 | |
| 5223 | /* |
| 5224 | * Hub resources are freed for us by usb_deregister. It calls |
| 5225 | * usb_driver_purge on every device which in turn calls that |
| 5226 | * devices disconnect function if it is using this driver. |
| 5227 | * The hub_disconnect function takes care of releasing the |
| 5228 | * individual hub resources. -greg |
| 5229 | */ |
| 5230 | usb_deregister(&hub_driver); |
| 5231 | } /* usb_hub_cleanup() */ |
| 5232 | |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5233 | static int descriptors_changed(struct usb_device *udev, |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5234 | struct usb_device_descriptor *old_device_descriptor, |
| 5235 | struct usb_host_bos *old_bos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5236 | { |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5237 | int changed = 0; |
| 5238 | unsigned index; |
| 5239 | unsigned serial_len = 0; |
| 5240 | unsigned len; |
| 5241 | unsigned old_length; |
| 5242 | int length; |
| 5243 | char *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5245 | if (memcmp(&udev->descriptor, old_device_descriptor, |
| 5246 | sizeof(*old_device_descriptor)) != 0) |
| 5247 | return 1; |
| 5248 | |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5249 | if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) |
| 5250 | return 1; |
| 5251 | if (udev->bos) { |
Xenia Ragiadakou | b9a1048 | 2013-08-31 04:40:49 +0300 | [diff] [blame] | 5252 | len = le16_to_cpu(udev->bos->desc->wTotalLength); |
| 5253 | if (len != le16_to_cpu(old_bos->desc->wTotalLength)) |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5254 | return 1; |
Xenia Ragiadakou | b9a1048 | 2013-08-31 04:40:49 +0300 | [diff] [blame] | 5255 | if (memcmp(udev->bos->desc, old_bos->desc, len)) |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5256 | return 1; |
| 5257 | } |
| 5258 | |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5259 | /* Since the idVendor, idProduct, and bcdDevice values in the |
| 5260 | * device descriptor haven't changed, we will assume the |
| 5261 | * Manufacturer and Product strings haven't changed either. |
| 5262 | * But the SerialNumber string could be different (e.g., a |
| 5263 | * different flash card of the same brand). |
| 5264 | */ |
| 5265 | if (udev->serial) |
| 5266 | serial_len = strlen(udev->serial) + 1; |
| 5267 | |
| 5268 | len = serial_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5270 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
| 5271 | len = max(len, old_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | } |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5273 | |
Oliver Neukum | 0cc1a51 | 2008-01-10 10:31:48 +0100 | [diff] [blame] | 5274 | buf = kmalloc(len, GFP_NOIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | if (buf == NULL) { |
| 5276 | dev_err(&udev->dev, "no mem to re-read configs after reset\n"); |
| 5277 | /* assume the worst */ |
| 5278 | return 1; |
| 5279 | } |
| 5280 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5281 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, |
| 5283 | old_length); |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5284 | if (length != old_length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | dev_dbg(&udev->dev, "config index %d, error %d\n", |
| 5286 | index, length); |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5287 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | break; |
| 5289 | } |
| 5290 | if (memcmp (buf, udev->rawdescriptors[index], old_length) |
| 5291 | != 0) { |
| 5292 | dev_dbg(&udev->dev, "config index %d changed (#%d)\n", |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5293 | index, |
| 5294 | ((struct usb_config_descriptor *) buf)-> |
| 5295 | bConfigurationValue); |
| 5296 | changed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | break; |
| 5298 | } |
| 5299 | } |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5300 | |
| 5301 | if (!changed && serial_len) { |
| 5302 | length = usb_string(udev, udev->descriptor.iSerialNumber, |
| 5303 | buf, serial_len); |
| 5304 | if (length + 1 != serial_len) { |
| 5305 | dev_dbg(&udev->dev, "serial string error %d\n", |
| 5306 | length); |
| 5307 | changed = 1; |
| 5308 | } else if (memcmp(buf, udev->serial, length) != 0) { |
| 5309 | dev_dbg(&udev->dev, "serial string changed\n"); |
| 5310 | changed = 1; |
| 5311 | } |
| 5312 | } |
| 5313 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | kfree(buf); |
Alan Stern | eb764c4 | 2008-03-03 15:16:04 -0500 | [diff] [blame] | 5315 | return changed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | /** |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5319 | * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
| 5321 | * |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5322 | * WARNING - don't use this routine to reset a composite device |
| 5323 | * (one with multiple interfaces owned by separate drivers)! |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5324 | * Use usb_reset_device() instead. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | * |
| 5326 | * Do a port reset, reassign the device's address, and establish its |
| 5327 | * former operating configuration. If the reset fails, or the device's |
| 5328 | * descriptors change from their values before the reset, or the original |
| 5329 | * configuration and altsettings cannot be restored, a flag will be set |
| 5330 | * telling khubd to pretend the device has been disconnected and then |
| 5331 | * re-connected. All drivers will be unbound, and the device will be |
| 5332 | * re-enumerated and probed all over again. |
| 5333 | * |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 5334 | * Return: 0 if the reset succeeded, -ENODEV if the device has been |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5335 | * flagged for logical disconnection, or some other negative error code |
| 5336 | * if the reset wasn't even attempted. |
| 5337 | * |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 5338 | * Note: |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5339 | * The caller must own the device lock and the port lock, the latter is |
| 5340 | * taken by usb_reset_device(). For example, it's safe to use |
| 5341 | * usb_reset_device() from a driver probe() routine after downloading |
| 5342 | * new firmware. For calls that might not occur during probe(), drivers |
| 5343 | * should lock the device using usb_lock_device_for_reset(). |
Alan Stern | 6bc6cff | 2007-05-04 11:53:03 -0400 | [diff] [blame] | 5344 | * |
| 5345 | * Locking exception: This routine may also be called from within an |
| 5346 | * autoresume handler. Such usage won't conflict with other tasks |
| 5347 | * holding the device lock because these tasks should always call |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5348 | * usb_autopm_resume_device(), thereby preventing any unwanted |
| 5349 | * autoresume. The autoresume handler is expected to have already |
| 5350 | * acquired the port lock before calling this routine. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5351 | */ |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5352 | static int usb_reset_and_verify_device(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5353 | { |
| 5354 | struct usb_device *parent_hdev = udev->parent; |
| 5355 | struct usb_hub *parent_hub; |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 5356 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | struct usb_device_descriptor descriptor = udev->descriptor; |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5358 | struct usb_host_bos *bos; |
Hans de Goede | 7a7b562 | 2013-11-08 16:37:26 +0100 | [diff] [blame] | 5359 | int i, j, ret = 0; |
Alan Stern | 12c3da3 | 2005-11-23 12:09:52 -0500 | [diff] [blame] | 5360 | int port1 = udev->portnum; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | |
| 5362 | if (udev->state == USB_STATE_NOTATTACHED || |
| 5363 | udev->state == USB_STATE_SUSPENDED) { |
| 5364 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", |
| 5365 | udev->state); |
| 5366 | return -EINVAL; |
| 5367 | } |
| 5368 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5369 | if (!parent_hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5370 | return -EISDIR; |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5371 | |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 5372 | parent_hub = usb_hub_to_struct_hub(parent_hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5373 | |
Sarah Sharp | dcc01c0 | 2013-09-30 17:26:29 +0300 | [diff] [blame] | 5374 | /* Disable USB2 hardware LPM. |
| 5375 | * It will be re-enabled by the enumeration process. |
| 5376 | */ |
| 5377 | if (udev->usb2_hw_lpm_enabled == 1) |
| 5378 | usb_set_usb2_hardware_lpm(udev, 0); |
| 5379 | |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5380 | bos = udev->bos; |
| 5381 | udev->bos = NULL; |
| 5382 | |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 5383 | /* Disable LPM and LTM while we reset the device and reinstall the alt |
| 5384 | * settings. Device-initiated LPM settings, and system exit latency |
| 5385 | * settings are cleared when the device is reset, so we have to set |
| 5386 | * them up again. |
Sarah Sharp | 6d1d051 | 2012-07-03 22:49:04 -0700 | [diff] [blame] | 5387 | */ |
| 5388 | ret = usb_unlocked_disable_lpm(udev); |
| 5389 | if (ret) { |
| 5390 | dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__); |
| 5391 | goto re_enumerate; |
| 5392 | } |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 5393 | ret = usb_disable_ltm(udev); |
| 5394 | if (ret) { |
| 5395 | dev_err(&udev->dev, "%s Failed to disable LTM\n.", |
| 5396 | __func__); |
| 5397 | goto re_enumerate; |
| 5398 | } |
Sarah Sharp | 6d1d051 | 2012-07-03 22:49:04 -0700 | [diff] [blame] | 5399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | for (i = 0; i < SET_CONFIG_TRIES; ++i) { |
| 5401 | |
| 5402 | /* ep0 maxpacket size may change; let the HCD know about it. |
| 5403 | * Other endpoints will be handled by re-enumeration. */ |
Inaky Perez-Gonzalez | fc721f5 | 2008-04-08 13:24:46 -0700 | [diff] [blame] | 5404 | usb_ep0_reinit(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | ret = hub_port_init(parent_hub, udev, port1, i); |
Alan Stern | dd4dd19 | 2007-05-04 11:55:54 -0400 | [diff] [blame] | 5406 | if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | break; |
| 5408 | } |
Alan Stern | d5cbad4 | 2006-08-11 16:52:39 -0400 | [diff] [blame] | 5409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | if (ret < 0) |
| 5411 | goto re_enumerate; |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | /* Device might have changed firmware (DFU or similar) */ |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5414 | if (descriptors_changed(udev, &descriptor, bos)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | dev_info(&udev->dev, "device firmware changed\n"); |
| 5416 | udev->descriptor = descriptor; /* for disconnect() calls */ |
| 5417 | goto re_enumerate; |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5418 | } |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 5419 | |
| 5420 | /* Restore the device's previous configuration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | if (!udev->actconfig) |
| 5422 | goto done; |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 5423 | |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 5424 | mutex_lock(hcd->bandwidth_mutex); |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 5425 | ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); |
| 5426 | if (ret < 0) { |
| 5427 | dev_warn(&udev->dev, |
| 5428 | "Busted HC? Not enough HCD resources for " |
| 5429 | "old configuration.\n"); |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 5430 | mutex_unlock(hcd->bandwidth_mutex); |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 5431 | goto re_enumerate; |
| 5432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5433 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 5434 | USB_REQ_SET_CONFIGURATION, 0, |
| 5435 | udev->actconfig->desc.bConfigurationValue, 0, |
| 5436 | NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 5437 | if (ret < 0) { |
| 5438 | dev_err(&udev->dev, |
| 5439 | "can't restore configuration #%d (error=%d)\n", |
| 5440 | udev->actconfig->desc.bConfigurationValue, ret); |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 5441 | mutex_unlock(hcd->bandwidth_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | goto re_enumerate; |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5443 | } |
Sarah Sharp | d673bfc | 2010-10-15 08:55:24 -0700 | [diff] [blame] | 5444 | mutex_unlock(hcd->bandwidth_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | usb_set_device_state(udev, USB_STATE_CONFIGURED); |
| 5446 | |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 5447 | /* Put interfaces back into the same altsettings as before. |
| 5448 | * Don't bother to send the Set-Interface request for interfaces |
| 5449 | * that were already in altsetting 0; besides being unnecessary, |
| 5450 | * many devices can't handle it. Instead just reset the host-side |
| 5451 | * endpoint state. |
| 5452 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5453 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 5454 | struct usb_host_config *config = udev->actconfig; |
| 5455 | struct usb_interface *intf = config->interface[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5456 | struct usb_interface_descriptor *desc; |
| 5457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | desc = &intf->cur_altsetting->desc; |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 5459 | if (desc->bAlternateSetting == 0) { |
| 5460 | usb_disable_interface(udev, intf, true); |
| 5461 | usb_enable_interface(udev, intf, true); |
| 5462 | ret = 0; |
| 5463 | } else { |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame] | 5464 | /* Let the bandwidth allocation function know that this |
| 5465 | * device has been reset, and it will have to use |
| 5466 | * alternate setting 0 as the current alternate setting. |
Sarah Sharp | 3f0479e | 2009-12-03 09:44:36 -0800 | [diff] [blame] | 5467 | */ |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame] | 5468 | intf->resetting_device = 1; |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 5469 | ret = usb_set_interface(udev, desc->bInterfaceNumber, |
| 5470 | desc->bAlternateSetting); |
Sarah Sharp | 04a723e | 2010-01-06 10:16:51 -0800 | [diff] [blame] | 5471 | intf->resetting_device = 0; |
Alan Stern | 4fe0387 | 2009-02-26 10:21:02 -0500 | [diff] [blame] | 5472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5473 | if (ret < 0) { |
| 5474 | dev_err(&udev->dev, "failed to restore interface %d " |
| 5475 | "altsetting %d (error=%d)\n", |
| 5476 | desc->bInterfaceNumber, |
| 5477 | desc->bAlternateSetting, |
| 5478 | ret); |
| 5479 | goto re_enumerate; |
| 5480 | } |
Hans de Goede | 7a7b562 | 2013-11-08 16:37:26 +0100 | [diff] [blame] | 5481 | /* Resetting also frees any allocated streams */ |
| 5482 | for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) |
| 5483 | intf->cur_altsetting->endpoint[j].streams = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5484 | } |
| 5485 | |
| 5486 | done: |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 5487 | /* Now that the alt settings are re-installed, enable LTM and LPM. */ |
Sarah Sharp | de68bab | 2013-09-30 17:26:28 +0300 | [diff] [blame] | 5488 | usb_set_usb2_hardware_lpm(udev, 1); |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 5489 | usb_unlocked_enable_lpm(udev); |
Sarah Sharp | f74631e | 2012-06-25 12:08:08 -0700 | [diff] [blame] | 5490 | usb_enable_ltm(udev); |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5491 | usb_release_bos_descriptor(udev); |
| 5492 | udev->bos = bos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5493 | return 0; |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 5494 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5495 | re_enumerate: |
Sarah Sharp | 6d1d051 | 2012-07-03 22:49:04 -0700 | [diff] [blame] | 5496 | /* LPM state doesn't matter when we're about to destroy the device. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | hub_port_logical_disconnect(parent_hub, port1); |
Xenia Ragiadakou | e3376d6 | 2013-08-29 21:45:48 +0300 | [diff] [blame] | 5498 | usb_release_bos_descriptor(udev); |
| 5499 | udev->bos = bos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5500 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | } |
| 5502 | |
| 5503 | /** |
| 5504 | * usb_reset_device - warn interface drivers and perform a USB port reset |
| 5505 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
| 5506 | * |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5507 | * Warns all drivers bound to registered interfaces (using their pre_reset |
| 5508 | * method), performs the port reset, and then lets the drivers know that |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5509 | * the reset is over (using their post_reset method). |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5510 | * |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 5511 | * Return: The same as for usb_reset_and_verify_device(). |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5512 | * |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 5513 | * Note: |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5514 | * The caller must own the device lock. For example, it's safe to use |
| 5515 | * this from a driver probe() routine after downloading new firmware. |
| 5516 | * For calls that might not occur during probe(), drivers should lock |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5517 | * the device using usb_lock_device_for_reset(). |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5518 | * |
| 5519 | * If an interface is currently being probed or disconnected, we assume |
| 5520 | * its driver knows how to handle resets. For all other interfaces, |
| 5521 | * if the driver doesn't have pre_reset and post_reset methods then |
| 5522 | * we attempt to unbind it and rebind afterward. |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5523 | */ |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5524 | int usb_reset_device(struct usb_device *udev) |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5525 | { |
| 5526 | int ret; |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 5527 | int i; |
Ming Lei | 4d769de | 2013-02-22 16:34:22 -0800 | [diff] [blame] | 5528 | unsigned int noio_flag; |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5529 | struct usb_port *port_dev; |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5530 | struct usb_host_config *config = udev->actconfig; |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5531 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5532 | |
| 5533 | if (udev->state == USB_STATE_NOTATTACHED || |
| 5534 | udev->state == USB_STATE_SUSPENDED) { |
| 5535 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", |
| 5536 | udev->state); |
| 5537 | return -EINVAL; |
| 5538 | } |
| 5539 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5540 | if (!udev->parent) { |
| 5541 | /* this requires hcd-specific logic; see ohci_restart() */ |
| 5542 | dev_dbg(&udev->dev, "%s for root hub!\n", __func__); |
| 5543 | return -EISDIR; |
| 5544 | } |
| 5545 | |
| 5546 | port_dev = hub->ports[udev->portnum - 1]; |
| 5547 | |
Ming Lei | 4d769de | 2013-02-22 16:34:22 -0800 | [diff] [blame] | 5548 | /* |
| 5549 | * Don't allocate memory with GFP_KERNEL in current |
| 5550 | * context to avoid possible deadlock if usb mass |
| 5551 | * storage interface or usbnet interface(iSCSI case) |
| 5552 | * is included in current configuration. The easist |
| 5553 | * approach is to do it for every device reset, |
| 5554 | * because the device 'memalloc_noio' flag may have |
| 5555 | * not been set before reseting the usb device. |
| 5556 | */ |
| 5557 | noio_flag = memalloc_noio_save(); |
| 5558 | |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 5559 | /* Prevent autosuspend during the reset */ |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 5560 | usb_autoresume_device(udev); |
Alan Stern | 645daaa | 2006-08-30 15:47:02 -0400 | [diff] [blame] | 5561 | |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5562 | if (config) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5563 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 5564 | struct usb_interface *cintf = config->interface[i]; |
| 5565 | struct usb_driver *drv; |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 5566 | int unbind = 0; |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 5567 | |
| 5568 | if (cintf->dev.driver) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5569 | drv = to_usb_driver(cintf->dev.driver); |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 5570 | if (drv->pre_reset && drv->post_reset) |
| 5571 | unbind = (drv->pre_reset)(cintf); |
| 5572 | else if (cintf->condition == |
| 5573 | USB_INTERFACE_BOUND) |
| 5574 | unbind = 1; |
| 5575 | if (unbind) |
| 5576 | usb_forced_unbind_intf(cintf); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5577 | } |
| 5578 | } |
| 5579 | } |
| 5580 | |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5581 | usb_lock_port(port_dev); |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5582 | ret = usb_reset_and_verify_device(udev); |
Dan Williams | 5c79a1e | 2014-05-20 18:09:26 -0700 | [diff] [blame] | 5583 | usb_unlock_port(port_dev); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5584 | |
| 5585 | if (config) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5586 | for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 5587 | struct usb_interface *cintf = config->interface[i]; |
| 5588 | struct usb_driver *drv; |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 5589 | int rebind = cintf->needs_binding; |
Alan Stern | 852c4b4 | 2007-12-03 15:44:29 -0500 | [diff] [blame] | 5590 | |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 5591 | if (!rebind && cintf->dev.driver) { |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5592 | drv = to_usb_driver(cintf->dev.driver); |
| 5593 | if (drv->post_reset) |
Alan Stern | 78d9a48 | 2008-06-23 16:00:40 -0400 | [diff] [blame] | 5594 | rebind = (drv->post_reset)(cintf); |
| 5595 | else if (cintf->condition == |
| 5596 | USB_INTERFACE_BOUND) |
| 5597 | rebind = 1; |
Alan Stern | 6aec044 | 2014-03-12 11:30:38 -0400 | [diff] [blame] | 5598 | if (rebind) |
| 5599 | cintf->needs_binding = 1; |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5600 | } |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5601 | } |
Alan Stern | 6aec044 | 2014-03-12 11:30:38 -0400 | [diff] [blame] | 5602 | usb_unbind_and_rebind_marked_interfaces(udev); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5603 | } |
| 5604 | |
Alan Stern | 94fcda1 | 2006-11-20 11:38:46 -0500 | [diff] [blame] | 5605 | usb_autosuspend_device(udev); |
Ming Lei | 4d769de | 2013-02-22 16:34:22 -0800 | [diff] [blame] | 5606 | memalloc_noio_restore(noio_flag); |
Alan Stern | 79efa09 | 2006-06-01 13:33:42 -0400 | [diff] [blame] | 5607 | return ret; |
| 5608 | } |
Ming Lei | 742120c | 2008-06-18 22:00:29 +0800 | [diff] [blame] | 5609 | EXPORT_SYMBOL_GPL(usb_reset_device); |
Inaky Perez-Gonzalez | dc023dc | 2008-11-13 10:31:35 -0800 | [diff] [blame] | 5610 | |
| 5611 | |
| 5612 | /** |
| 5613 | * usb_queue_reset_device - Reset a USB device from an atomic context |
| 5614 | * @iface: USB interface belonging to the device to reset |
| 5615 | * |
| 5616 | * This function can be used to reset a USB device from an atomic |
| 5617 | * context, where usb_reset_device() won't work (as it blocks). |
| 5618 | * |
| 5619 | * Doing a reset via this method is functionally equivalent to calling |
| 5620 | * usb_reset_device(), except for the fact that it is delayed to a |
| 5621 | * workqueue. This means that any drivers bound to other interfaces |
| 5622 | * might be unbound, as well as users from usbfs in user space. |
| 5623 | * |
| 5624 | * Corner cases: |
| 5625 | * |
| 5626 | * - Scheduling two resets at the same time from two different drivers |
| 5627 | * attached to two different interfaces of the same device is |
| 5628 | * possible; depending on how the driver attached to each interface |
| 5629 | * handles ->pre_reset(), the second reset might happen or not. |
| 5630 | * |
| 5631 | * - If a driver is unbound and it had a pending reset, the reset will |
| 5632 | * be cancelled. |
| 5633 | * |
| 5634 | * - This function can be called during .probe() or .disconnect() |
| 5635 | * times. On return from .disconnect(), any pending resets will be |
| 5636 | * cancelled. |
| 5637 | * |
| 5638 | * There is no no need to lock/unlock the @reset_ws as schedule_work() |
| 5639 | * does its own. |
| 5640 | * |
| 5641 | * NOTE: We don't do any reference count tracking because it is not |
| 5642 | * needed. The lifecycle of the work_struct is tied to the |
| 5643 | * usb_interface. Before destroying the interface we cancel the |
| 5644 | * work_struct, so the fact that work_struct is queued and or |
| 5645 | * running means the interface (and thus, the device) exist and |
| 5646 | * are referenced. |
| 5647 | */ |
| 5648 | void usb_queue_reset_device(struct usb_interface *iface) |
| 5649 | { |
| 5650 | schedule_work(&iface->reset_ws); |
| 5651 | } |
| 5652 | EXPORT_SYMBOL_GPL(usb_queue_reset_device); |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 5653 | |
| 5654 | /** |
| 5655 | * usb_hub_find_child - Get the pointer of child device |
| 5656 | * attached to the port which is specified by @port1. |
| 5657 | * @hdev: USB device belonging to the usb hub |
| 5658 | * @port1: port num to indicate which port the child device |
| 5659 | * is attached to. |
| 5660 | * |
| 5661 | * USB drivers call this function to get hub's child device |
| 5662 | * pointer. |
| 5663 | * |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 5664 | * Return: %NULL if input param is invalid and |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 5665 | * child's usb_device pointer if non-NULL. |
| 5666 | */ |
| 5667 | struct usb_device *usb_hub_find_child(struct usb_device *hdev, |
| 5668 | int port1) |
| 5669 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 5670 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Lan Tianyu | ff823c79 | 2012-09-05 13:44:32 +0800 | [diff] [blame] | 5671 | |
| 5672 | if (port1 < 1 || port1 > hdev->maxchild) |
| 5673 | return NULL; |
| 5674 | return hub->ports[port1 - 1]->child; |
| 5675 | } |
| 5676 | EXPORT_SYMBOL_GPL(usb_hub_find_child); |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 5677 | |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5678 | void usb_hub_adjust_deviceremovable(struct usb_device *hdev, |
| 5679 | struct usb_hub_descriptor *desc) |
| 5680 | { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5681 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5682 | enum usb_port_connect_type connect_type; |
| 5683 | int i; |
| 5684 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5685 | if (!hub) |
| 5686 | return; |
| 5687 | |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5688 | if (!hub_is_superspeed(hdev)) { |
| 5689 | for (i = 1; i <= hdev->maxchild; i++) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5690 | struct usb_port *port_dev = hub->ports[i - 1]; |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5691 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5692 | connect_type = port_dev->connect_type; |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5693 | if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { |
| 5694 | u8 mask = 1 << (i%8); |
| 5695 | |
| 5696 | if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5697 | dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5698 | desc->u.hs.DeviceRemovable[i/8] |= mask; |
| 5699 | } |
| 5700 | } |
| 5701 | } |
| 5702 | } else { |
| 5703 | u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); |
| 5704 | |
| 5705 | for (i = 1; i <= hdev->maxchild; i++) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5706 | struct usb_port *port_dev = hub->ports[i - 1]; |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5707 | |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5708 | connect_type = port_dev->connect_type; |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5709 | if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { |
| 5710 | u16 mask = 1 << i; |
| 5711 | |
| 5712 | if (!(port_removable & mask)) { |
Dan Williams | d99f6b4 | 2014-05-20 18:08:17 -0700 | [diff] [blame] | 5713 | dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); |
Lan Tianyu | d2123fd | 2013-01-21 22:18:00 +0800 | [diff] [blame] | 5714 | port_removable |= mask; |
| 5715 | } |
| 5716 | } |
| 5717 | } |
| 5718 | |
| 5719 | desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); |
| 5720 | } |
| 5721 | } |
| 5722 | |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 5723 | #ifdef CONFIG_ACPI |
| 5724 | /** |
| 5725 | * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle |
| 5726 | * @hdev: USB device belonging to the usb hub |
| 5727 | * @port1: port num of the port |
| 5728 | * |
Yacine Belkadi | 626f090 | 2013-08-02 20:10:04 +0200 | [diff] [blame] | 5729 | * Return: Port's acpi handle if successful, %NULL if params are |
| 5730 | * invalid. |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 5731 | */ |
| 5732 | acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, |
| 5733 | int port1) |
| 5734 | { |
Lan Tianyu | ad493e5 | 2013-01-23 04:26:30 +0800 | [diff] [blame] | 5735 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 5736 | |
Mathias Nyman | 41341261 | 2013-06-18 17:28:48 +0300 | [diff] [blame] | 5737 | if (!hub) |
| 5738 | return NULL; |
| 5739 | |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 5740 | return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); |
Lan Tianyu | d557542 | 2012-09-05 13:44:33 +0800 | [diff] [blame] | 5741 | } |
| 5742 | #endif |