1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB hub driver. 4 * 5 * (C) Copyright 1999 Linus Torvalds 6 * (C) Copyright 1999 Johannes Erdfelt 7 * (C) Copyright 1999 Gregory P. Smith 8 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) 9 * 10 * Released under the GPLv2 only. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/errno.h> 15 #include <linux/module.h> 16 #include <linux/moduleparam.h> 17 #include <linux/completion.h> 18 #include <linux/sched/mm.h> 19 #include <linux/list.h> 20 #include <linux/slab.h> 21 #include <linux/string_choices.h> 22 #include <linux/kcov.h> 23 #include <linux/ioctl.h> 24 #include <linux/usb.h> 25 #include <linux/usbdevice_fs.h> 26 #include <linux/usb/hcd.h> 27 #include <linux/usb/onboard_dev.h> 28 #include <linux/usb/otg.h> 29 #include <linux/usb/quirks.h> 30 #include <linux/workqueue.h> 31 #include <linux/minmax.h> 32 #include <linux/mutex.h> 33 #include <linux/random.h> 34 #include <linux/pm_qos.h> 35 #include <linux/kobject.h> 36 37 #include <linux/bitfield.h> 38 #include <linux/uaccess.h> 39 #include <asm/byteorder.h> 40 41 #include "hub.h" 42 #include "phy.h" 43 #include "otg_productlist.h" 44 #include "trace.h" 45 46 #define USB_VENDOR_GENESYS_LOGIC 0x05e3 47 #define USB_VENDOR_SMSC 0x0424 48 #define USB_PRODUCT_USB5534B 0x5534 49 #define USB_VENDOR_CYPRESS 0x04b4 50 #define USB_PRODUCT_CY7C65632 0x6570 51 #define USB_VENDOR_TEXAS_INSTRUMENTS 0x0451 52 #define USB_PRODUCT_TUSB8041_USB3 0x8140 53 #define USB_PRODUCT_TUSB8041_USB2 0x8142 54 #define USB_VENDOR_MICROCHIP 0x0424 55 #define USB_PRODUCT_USB4913 0x4913 56 #define USB_PRODUCT_USB4914 0x4914 57 #define USB_PRODUCT_USB4915 0x4915 58 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND BIT(0) 59 #define HUB_QUIRK_DISABLE_AUTOSUSPEND BIT(1) 60 #define HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL BIT(2) 61 62 #define USB_TP_TRANSMISSION_DELAY 40 /* ns */ 63 #define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */ 64 #define USB_PING_RESPONSE_TIME 400 /* ns */ 65 #define USB_REDUCE_FRAME_INTR_BINTERVAL 9 66 67 /* 68 * The SET_ADDRESS request timeout will be 500 ms when 69 * USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT quirk flag is set. 70 */ 71 #define USB_SHORT_SET_ADDRESS_REQ_TIMEOUT 500 /* ms */ 72 73 /* 74 * Give SS hubs 200ms time after wake to train downstream links before 75 * assuming no port activity and allowing hub to runtime suspend back. 76 */ 77 #define USB_SS_PORT_U0_WAKE_TIME 200 /* ms */ 78 79 /* Protect struct usb_device->state and ->children members 80 * Note: Both are also protected by ->dev.sem, except that ->state can 81 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 82 static DEFINE_SPINLOCK(device_state_lock); 83 84 /* workqueue to process hub events */ 85 static struct workqueue_struct *hub_wq; 86 static void hub_event(struct work_struct *work); 87 88 /* synchronize hub-port add/remove and peering operations */ 89 DEFINE_MUTEX(usb_port_peer_mutex); 90 91 /* cycle leds on hubs that aren't blinking for attention */ 92 static bool blinkenlights; 93 module_param(blinkenlights, bool, S_IRUGO); 94 MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs"); 95 96 /* 97 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 98 * 10 seconds to send reply for the initial 64-byte descriptor request. 99 */ 100 /* define initial 64-byte descriptor request timeout in milliseconds */ 101 static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 102 module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 103 MODULE_PARM_DESC(initial_descriptor_timeout, 104 "initial 64-byte descriptor request timeout in milliseconds " 105 "(default 5000 - 5.0 seconds)"); 106 107 /* 108 * As of 2.6.10 we introduce a new USB device initialization scheme which 109 * closely resembles the way Windows works. Hopefully it will be compatible 110 * with a wider range of devices than the old scheme. However some previously 111 * working devices may start giving rise to "device not accepting address" 112 * errors; if that happens the user can try the old scheme by adjusting the 113 * following module parameters. 114 * 115 * For maximum flexibility there are two boolean parameters to control the 116 * hub driver's behavior. On the first initialization attempt, if the 117 * "old_scheme_first" parameter is set then the old scheme will be used, 118 * otherwise the new scheme is used. If that fails and "use_both_schemes" 119 * is set, then the driver will make another attempt, using the other scheme. 120 */ 121 static bool old_scheme_first; 122 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 123 MODULE_PARM_DESC(old_scheme_first, 124 "start with the old device initialization scheme"); 125 126 static bool use_both_schemes = true; 127 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 128 MODULE_PARM_DESC(use_both_schemes, 129 "try the other device initialization scheme if the " 130 "first one fails"); 131 132 /* Mutual exclusion for EHCI CF initialization. This interferes with 133 * port reset on some companion controllers. 134 */ 135 DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 136 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 137 138 #define HUB_DEBOUNCE_TIMEOUT 2000 139 #define HUB_DEBOUNCE_STEP 25 140 #define HUB_DEBOUNCE_STABLE 100 141 142 static int usb_reset_and_verify_device(struct usb_device *udev); 143 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state); 144 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 145 u16 portstatus); 146 147 static inline char *portspeed(struct usb_hub *hub, int portstatus) 148 { 149 if (hub_is_superspeedplus(hub->hdev)) 150 return "10.0 Gb/s"; 151 if (hub_is_superspeed(hub->hdev)) 152 return "5.0 Gb/s"; 153 if (portstatus & USB_PORT_STAT_HIGH_SPEED) 154 return "480 Mb/s"; 155 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 156 return "1.5 Mb/s"; 157 else 158 return "12 Mb/s"; 159 } 160 161 /* Note that hdev or one of its children must be locked! */ 162 struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev) 163 { 164 if (!hdev || !hdev->actconfig || !hdev->maxchild) 165 return NULL; 166 return usb_get_intfdata(hdev->actconfig->interface[0]); 167 } 168 169 int usb_device_supports_lpm(struct usb_device *udev) 170 { 171 /* Some devices have trouble with LPM */ 172 if (udev->quirks & USB_QUIRK_NO_LPM) 173 return 0; 174 175 /* Skip if the device BOS descriptor couldn't be read */ 176 if (!udev->bos) 177 return 0; 178 179 /* USB 2.1 (and greater) devices indicate LPM support through 180 * their USB 2.0 Extended Capabilities BOS descriptor. 181 */ 182 if (udev->speed == USB_SPEED_HIGH || udev->speed == USB_SPEED_FULL) { 183 if (udev->bos->ext_cap && 184 (USB_LPM_SUPPORT & 185 le32_to_cpu(udev->bos->ext_cap->bmAttributes))) 186 return 1; 187 return 0; 188 } 189 190 /* 191 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM. 192 * However, there are some that don't, and they set the U1/U2 exit 193 * latencies to zero. 194 */ 195 if (!udev->bos->ss_cap) { 196 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n"); 197 return 0; 198 } 199 200 if (udev->bos->ss_cap->bU1devExitLat == 0 && 201 udev->bos->ss_cap->bU2DevExitLat == 0) { 202 if (udev->parent) 203 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n"); 204 else 205 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n"); 206 return 0; 207 } 208 209 if (!udev->parent || udev->parent->lpm_capable) 210 return 1; 211 return 0; 212 } 213 214 /* 215 * Set the Maximum Exit Latency (MEL) for the host to wakup up the path from 216 * U1/U2, send a PING to the device and receive a PING_RESPONSE. 217 * See USB 3.1 section C.1.5.2 218 */ 219 static void usb_set_lpm_mel(struct usb_device *udev, 220 struct usb3_lpm_parameters *udev_lpm_params, 221 unsigned int udev_exit_latency, 222 struct usb_hub *hub, 223 struct usb3_lpm_parameters *hub_lpm_params, 224 unsigned int hub_exit_latency) 225 { 226 unsigned int total_mel; 227 228 /* 229 * tMEL1. time to transition path from host to device into U0. 230 * MEL for parent already contains the delay up to parent, so only add 231 * the exit latency for the last link (pick the slower exit latency), 232 * and the hub header decode latency. See USB 3.1 section C 2.2.1 233 * Store MEL in nanoseconds 234 */ 235 total_mel = hub_lpm_params->mel + 236 max(udev_exit_latency, hub_exit_latency) * 1000 + 237 hub->descriptor->u.ss.bHubHdrDecLat * 100; 238 239 /* 240 * tMEL2. Time to submit PING packet. Sum of tTPTransmissionDelay for 241 * each link + wHubDelay for each hub. Add only for last link. 242 * tMEL4, the time for PING_RESPONSE to traverse upstream is similar. 243 * Multiply by 2 to include it as well. 244 */ 245 total_mel += (__le16_to_cpu(hub->descriptor->u.ss.wHubDelay) + 246 USB_TP_TRANSMISSION_DELAY) * 2; 247 248 /* 249 * tMEL3, tPingResponse. Time taken by device to generate PING_RESPONSE 250 * after receiving PING. Also add 2100ns as stated in USB 3.1 C 1.5.2.4 251 * to cover the delay if the PING_RESPONSE is queued behind a Max Packet 252 * Size DP. 253 * Note these delays should be added only once for the entire path, so 254 * add them to the MEL of the device connected to the roothub. 255 */ 256 if (!hub->hdev->parent) 257 total_mel += USB_PING_RESPONSE_TIME + 2100; 258 259 udev_lpm_params->mel = total_mel; 260 } 261 262 /* 263 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate 264 * a transition from either U1 or U2. 265 */ 266 static void usb_set_lpm_pel(struct usb_device *udev, 267 struct usb3_lpm_parameters *udev_lpm_params, 268 unsigned int udev_exit_latency, 269 struct usb_hub *hub, 270 struct usb3_lpm_parameters *hub_lpm_params, 271 unsigned int hub_exit_latency, 272 unsigned int port_to_port_exit_latency) 273 { 274 unsigned int first_link_pel; 275 unsigned int hub_pel; 276 277 /* 278 * First, the device sends an LFPS to transition the link between the 279 * device and the parent hub into U0. The exit latency is the bigger of 280 * the device exit latency or the hub exit latency. 281 */ 282 first_link_pel = max(udev_exit_latency, hub_exit_latency) * 1000; 283 284 /* 285 * When the hub starts to receive the LFPS, there is a slight delay for 286 * it to figure out that one of the ports is sending an LFPS. Then it 287 * will forward the LFPS to its upstream link. The exit latency is the 288 * delay, plus the PEL that we calculated for this hub. 289 */ 290 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; 291 292 /* 293 * According to figure C-7 in the USB 3.0 spec, the PEL for this device 294 * is the greater of the two exit latencies. 295 */ 296 udev_lpm_params->pel = max(first_link_pel, hub_pel); 297 } 298 299 /* 300 * Set the System Exit Latency (SEL) to indicate the total worst-case time from 301 * when a device initiates a transition to U0, until when it will receive the 302 * first packet from the host controller. 303 * 304 * Section C.1.5.1 describes the four components to this: 305 * - t1: device PEL 306 * - t2: time for the ERDY to make it from the device to the host. 307 * - t3: a host-specific delay to process the ERDY. 308 * - t4: time for the packet to make it from the host to the device. 309 * 310 * t3 is specific to both the xHCI host and the platform the host is integrated 311 * into. The Intel HW folks have said it's negligible, FIXME if a different 312 * vendor says otherwise. 313 */ 314 static void usb_set_lpm_sel(struct usb_device *udev, 315 struct usb3_lpm_parameters *udev_lpm_params) 316 { 317 struct usb_device *parent; 318 unsigned int num_hubs; 319 unsigned int total_sel; 320 321 /* t1 = device PEL */ 322 total_sel = udev_lpm_params->pel; 323 /* How many external hubs are in between the device & the root port. */ 324 for (parent = udev->parent, num_hubs = 0; parent->parent; 325 parent = parent->parent) 326 num_hubs++; 327 /* t2 = 2.1us + 250ns * (num_hubs - 1) */ 328 if (num_hubs > 0) 329 total_sel += 2100 + 250 * (num_hubs - 1); 330 331 /* t4 = 250ns * num_hubs */ 332 total_sel += 250 * num_hubs; 333 334 udev_lpm_params->sel = total_sel; 335 } 336 337 static void usb_set_lpm_parameters(struct usb_device *udev) 338 { 339 struct usb_hub *hub; 340 unsigned int port_to_port_delay; 341 unsigned int udev_u1_del; 342 unsigned int udev_u2_del; 343 unsigned int hub_u1_del; 344 unsigned int hub_u2_del; 345 346 if (!udev->lpm_capable || udev->speed < USB_SPEED_SUPER) 347 return; 348 349 /* Skip if the device BOS descriptor couldn't be read */ 350 if (!udev->bos) 351 return; 352 353 hub = usb_hub_to_struct_hub(udev->parent); 354 /* It doesn't take time to transition the roothub into U0, since it 355 * doesn't have an upstream link. 356 */ 357 if (!hub) 358 return; 359 360 udev_u1_del = udev->bos->ss_cap->bU1devExitLat; 361 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat); 362 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; 363 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat); 364 365 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, 366 hub, &udev->parent->u1_params, hub_u1_del); 367 368 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, 369 hub, &udev->parent->u2_params, hub_u2_del); 370 371 /* 372 * Appendix C, section C.2.2.2, says that there is a slight delay from 373 * when the parent hub notices the downstream port is trying to 374 * transition to U0 to when the hub initiates a U0 transition on its 375 * upstream port. The section says the delays are tPort2PortU1EL and 376 * tPort2PortU2EL, but it doesn't define what they are. 377 * 378 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking 379 * about the same delays. Use the maximum delay calculations from those 380 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For 381 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I 382 * assume the device exit latencies they are talking about are the hub 383 * exit latencies. 384 * 385 * What do we do if the U2 exit latency is less than the U1 exit 386 * latency? It's possible, although not likely... 387 */ 388 port_to_port_delay = 1; 389 390 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, 391 hub, &udev->parent->u1_params, hub_u1_del, 392 port_to_port_delay); 393 394 if (hub_u2_del > hub_u1_del) 395 port_to_port_delay = 1 + hub_u2_del - hub_u1_del; 396 else 397 port_to_port_delay = 1 + hub_u1_del; 398 399 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, 400 hub, &udev->parent->u2_params, hub_u2_del, 401 port_to_port_delay); 402 403 /* Now that we've got PEL, calculate SEL. */ 404 usb_set_lpm_sel(udev, &udev->u1_params); 405 usb_set_lpm_sel(udev, &udev->u2_params); 406 } 407 408 /* USB 2.0 spec Section 11.24.4.5 */ 409 static int get_hub_descriptor(struct usb_device *hdev, 410 struct usb_hub_descriptor *desc) 411 { 412 int i, ret, size; 413 unsigned dtype; 414 415 if (hub_is_superspeed(hdev)) { 416 dtype = USB_DT_SS_HUB; 417 size = USB_DT_SS_HUB_SIZE; 418 } else { 419 dtype = USB_DT_HUB; 420 size = sizeof(struct usb_hub_descriptor); 421 } 422 423 for (i = 0; i < 3; i++) { 424 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 425 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 426 dtype << 8, 0, desc, size, 427 USB_CTRL_GET_TIMEOUT); 428 if (hub_is_superspeed(hdev)) { 429 if (ret == size) 430 return ret; 431 } else if (ret >= USB_DT_HUB_NONVAR_SIZE + 2) { 432 /* Make sure we have the DeviceRemovable field. */ 433 size = USB_DT_HUB_NONVAR_SIZE + desc->bNbrPorts / 8 + 1; 434 if (ret < size) 435 return -EMSGSIZE; 436 return ret; 437 } 438 } 439 return -EINVAL; 440 } 441 442 /* 443 * USB 2.0 spec Section 11.24.2.1 444 */ 445 static int clear_hub_feature(struct usb_device *hdev, int feature) 446 { 447 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 448 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 449 } 450 451 /* 452 * USB 2.0 spec Section 11.24.2.2 453 */ 454 int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature) 455 { 456 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 457 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 458 NULL, 0, 1000); 459 } 460 461 /* 462 * USB 2.0 spec Section 11.24.2.13 463 */ 464 static int set_port_feature(struct usb_device *hdev, int port1, int feature) 465 { 466 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 467 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 468 NULL, 0, 1000); 469 } 470 471 static char *to_led_name(int selector) 472 { 473 switch (selector) { 474 case HUB_LED_AMBER: 475 return "amber"; 476 case HUB_LED_GREEN: 477 return "green"; 478 case HUB_LED_OFF: 479 return "off"; 480 case HUB_LED_AUTO: 481 return "auto"; 482 default: 483 return "??"; 484 } 485 } 486 487 /* 488 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 489 * for info about using port indicators 490 */ 491 static void set_port_led(struct usb_hub *hub, int port1, int selector) 492 { 493 struct usb_port *port_dev = hub->ports[port1 - 1]; 494 int status; 495 496 status = set_port_feature(hub->hdev, (selector << 8) | port1, 497 USB_PORT_FEAT_INDICATOR); 498 dev_dbg(&port_dev->dev, "indicator %s status %d\n", 499 to_led_name(selector), status); 500 } 501 502 #define LED_CYCLE_PERIOD ((2*HZ)/3) 503 504 static void led_work(struct work_struct *work) 505 { 506 struct usb_hub *hub = 507 container_of(work, struct usb_hub, leds.work); 508 struct usb_device *hdev = hub->hdev; 509 unsigned i; 510 unsigned changed = 0; 511 int cursor = -1; 512 513 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 514 return; 515 516 for (i = 0; i < hdev->maxchild; i++) { 517 unsigned selector, mode; 518 519 /* 30%-50% duty cycle */ 520 521 switch (hub->indicator[i]) { 522 /* cycle marker */ 523 case INDICATOR_CYCLE: 524 cursor = i; 525 selector = HUB_LED_AUTO; 526 mode = INDICATOR_AUTO; 527 break; 528 /* blinking green = sw attention */ 529 case INDICATOR_GREEN_BLINK: 530 selector = HUB_LED_GREEN; 531 mode = INDICATOR_GREEN_BLINK_OFF; 532 break; 533 case INDICATOR_GREEN_BLINK_OFF: 534 selector = HUB_LED_OFF; 535 mode = INDICATOR_GREEN_BLINK; 536 break; 537 /* blinking amber = hw attention */ 538 case INDICATOR_AMBER_BLINK: 539 selector = HUB_LED_AMBER; 540 mode = INDICATOR_AMBER_BLINK_OFF; 541 break; 542 case INDICATOR_AMBER_BLINK_OFF: 543 selector = HUB_LED_OFF; 544 mode = INDICATOR_AMBER_BLINK; 545 break; 546 /* blink green/amber = reserved */ 547 case INDICATOR_ALT_BLINK: 548 selector = HUB_LED_GREEN; 549 mode = INDICATOR_ALT_BLINK_OFF; 550 break; 551 case INDICATOR_ALT_BLINK_OFF: 552 selector = HUB_LED_AMBER; 553 mode = INDICATOR_ALT_BLINK; 554 break; 555 default: 556 continue; 557 } 558 if (selector != HUB_LED_AUTO) 559 changed = 1; 560 set_port_led(hub, i + 1, selector); 561 hub->indicator[i] = mode; 562 } 563 if (!changed && blinkenlights) { 564 cursor++; 565 cursor %= hdev->maxchild; 566 set_port_led(hub, cursor + 1, HUB_LED_GREEN); 567 hub->indicator[cursor] = INDICATOR_CYCLE; 568 changed++; 569 } 570 if (changed) 571 queue_delayed_work(system_power_efficient_wq, 572 &hub->leds, LED_CYCLE_PERIOD); 573 } 574 575 /* use a short timeout for hub/port status fetches */ 576 #define USB_STS_TIMEOUT 1000 577 #define USB_STS_RETRIES 5 578 579 /* 580 * USB 2.0 spec Section 11.24.2.6 581 */ 582 static int get_hub_status(struct usb_device *hdev, 583 struct usb_hub_status *data) 584 { 585 int i, status = -ETIMEDOUT; 586 587 for (i = 0; i < USB_STS_RETRIES && 588 (status == -ETIMEDOUT || status == -EPIPE); i++) { 589 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 590 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 591 data, sizeof(*data), USB_STS_TIMEOUT); 592 } 593 return status; 594 } 595 596 /* 597 * USB 2.0 spec Section 11.24.2.7 598 * USB 3.1 takes into use the wValue and wLength fields, spec Section 10.16.2.6 599 */ 600 static int get_port_status(struct usb_device *hdev, int port1, 601 void *data, u16 value, u16 length) 602 { 603 int i, status = -ETIMEDOUT; 604 605 for (i = 0; i < USB_STS_RETRIES && 606 (status == -ETIMEDOUT || status == -EPIPE); i++) { 607 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 608 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, value, 609 port1, data, length, USB_STS_TIMEOUT); 610 } 611 return status; 612 } 613 614 static int hub_ext_port_status(struct usb_hub *hub, int port1, int type, 615 u16 *status, u16 *change, u32 *ext_status) 616 { 617 int ret; 618 int len = 4; 619 620 if (type != HUB_PORT_STATUS) 621 len = 8; 622 623 mutex_lock(&hub->status_mutex); 624 ret = get_port_status(hub->hdev, port1, &hub->status->port, type, len); 625 if (ret < len) { 626 if (ret != -ENODEV) 627 dev_err(hub->intfdev, 628 "%s failed (err = %d)\n", __func__, ret); 629 if (ret >= 0) 630 ret = -EIO; 631 } else { 632 *status = le16_to_cpu(hub->status->port.wPortStatus); 633 *change = le16_to_cpu(hub->status->port.wPortChange); 634 if (type != HUB_PORT_STATUS && ext_status) 635 *ext_status = le32_to_cpu( 636 hub->status->port.dwExtPortStatus); 637 ret = 0; 638 } 639 mutex_unlock(&hub->status_mutex); 640 641 /* 642 * There is no need to lock status_mutex here, because status_mutex 643 * protects hub->status, and the phy driver only checks the port 644 * status without changing the status. 645 */ 646 if (!ret) { 647 struct usb_device *hdev = hub->hdev; 648 649 /* 650 * Only roothub will be notified of connection changes, 651 * since the USB PHY only cares about changes at the next 652 * level. 653 */ 654 if (is_root_hub(hdev)) { 655 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 656 bool connect; 657 bool connect_change; 658 659 connect_change = *change & USB_PORT_STAT_C_CONNECTION; 660 connect = *status & USB_PORT_STAT_CONNECTION; 661 if (connect_change && connect) 662 usb_phy_roothub_notify_connect(hcd->phy_roothub, port1 - 1); 663 else if (connect_change) 664 usb_phy_roothub_notify_disconnect(hcd->phy_roothub, port1 - 1); 665 } 666 } 667 668 return ret; 669 } 670 671 int usb_hub_port_status(struct usb_hub *hub, int port1, 672 u16 *status, u16 *change) 673 { 674 return hub_ext_port_status(hub, port1, HUB_PORT_STATUS, 675 status, change, NULL); 676 } 677 678 static void hub_resubmit_irq_urb(struct usb_hub *hub) 679 { 680 unsigned long flags; 681 int status; 682 683 spin_lock_irqsave(&hub->irq_urb_lock, flags); 684 685 if (hub->quiescing) { 686 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); 687 return; 688 } 689 690 status = usb_submit_urb(hub->urb, GFP_ATOMIC); 691 if (status && status != -ENODEV && status != -EPERM && 692 status != -ESHUTDOWN) { 693 dev_err(hub->intfdev, "resubmit --> %d\n", status); 694 mod_timer(&hub->irq_urb_retry, jiffies + HZ); 695 } 696 697 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); 698 } 699 700 static void hub_retry_irq_urb(struct timer_list *t) 701 { 702 struct usb_hub *hub = timer_container_of(hub, t, irq_urb_retry); 703 704 hub_resubmit_irq_urb(hub); 705 } 706 707 708 static void kick_hub_wq(struct usb_hub *hub) 709 { 710 struct usb_interface *intf; 711 712 if (hub->disconnected || work_pending(&hub->events)) 713 return; 714 715 /* 716 * Suppress autosuspend until the event is proceed. 717 * 718 * Be careful and make sure that the symmetric operation is 719 * always called. We are here only when there is no pending 720 * work for this hub. Therefore put the interface either when 721 * the new work is called or when it is canceled. 722 */ 723 intf = to_usb_interface(hub->intfdev); 724 usb_autopm_get_interface_no_resume(intf); 725 hub_get(hub); 726 727 if (queue_work(hub_wq, &hub->events)) 728 return; 729 730 /* the work has already been scheduled */ 731 usb_autopm_put_interface_async(intf); 732 hub_put(hub); 733 } 734 735 void usb_kick_hub_wq(struct usb_device *hdev) 736 { 737 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 738 739 if (hub) 740 kick_hub_wq(hub); 741 } 742 743 /* 744 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device 745 * Notification, which indicates it had initiated remote wakeup. 746 * 747 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the 748 * device initiates resume, so the USB core will not receive notice of the 749 * resume through the normal hub interrupt URB. 750 */ 751 void usb_wakeup_notification(struct usb_device *hdev, 752 unsigned int portnum) 753 { 754 struct usb_hub *hub; 755 struct usb_port *port_dev; 756 757 if (!hdev) 758 return; 759 760 hub = usb_hub_to_struct_hub(hdev); 761 if (hub) { 762 port_dev = hub->ports[portnum - 1]; 763 if (port_dev && port_dev->child) 764 pm_wakeup_event(&port_dev->child->dev, 0); 765 766 set_bit(portnum, hub->wakeup_bits); 767 kick_hub_wq(hub); 768 } 769 } 770 EXPORT_SYMBOL_GPL(usb_wakeup_notification); 771 772 /* completion function, fires on port status changes and various faults */ 773 static void hub_irq(struct urb *urb) 774 { 775 struct usb_hub *hub = urb->context; 776 int status = urb->status; 777 unsigned i; 778 unsigned long bits; 779 780 switch (status) { 781 case -ENOENT: /* synchronous unlink */ 782 case -ECONNRESET: /* async unlink */ 783 case -ESHUTDOWN: /* hardware going away */ 784 return; 785 786 default: /* presumably an error */ 787 /* Cause a hub reset after 10 consecutive errors */ 788 dev_dbg(hub->intfdev, "transfer --> %d\n", status); 789 if ((++hub->nerrors < 10) || hub->error) 790 goto resubmit; 791 hub->error = status; 792 fallthrough; 793 794 /* let hub_wq handle things */ 795 case 0: /* we got data: port status changed */ 796 bits = 0; 797 for (i = 0; i < urb->actual_length; ++i) 798 bits |= ((unsigned long) ((*hub->buffer)[i])) 799 << (i*8); 800 hub->event_bits[0] = bits; 801 break; 802 } 803 804 hub->nerrors = 0; 805 806 /* Something happened, let hub_wq figure it out */ 807 kick_hub_wq(hub); 808 809 resubmit: 810 hub_resubmit_irq_urb(hub); 811 } 812 813 /* USB 2.0 spec Section 11.24.2.3 */ 814 static inline int 815 hub_clear_tt_buffer(struct usb_device *hdev, u16 devinfo, u16 tt) 816 { 817 /* Need to clear both directions for control ep */ 818 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) == 819 USB_ENDPOINT_XFER_CONTROL) { 820 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 821 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, 822 devinfo ^ 0x8000, tt, NULL, 0, 1000); 823 if (status) 824 return status; 825 } 826 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 827 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 828 tt, NULL, 0, 1000); 829 } 830 831 /* 832 * enumeration blocks hub_wq for a long time. we use keventd instead, since 833 * long blocking there is the exception, not the rule. accordingly, HCDs 834 * talking to TTs must queue control transfers (not just bulk and iso), so 835 * both can talk to the same hub concurrently. 836 */ 837 static void hub_tt_work(struct work_struct *work) 838 { 839 struct usb_hub *hub = 840 container_of(work, struct usb_hub, tt.clear_work); 841 unsigned long flags; 842 843 spin_lock_irqsave(&hub->tt.lock, flags); 844 while (!list_empty(&hub->tt.clear_list)) { 845 struct list_head *next; 846 struct usb_tt_clear *clear; 847 struct usb_device *hdev = hub->hdev; 848 const struct hc_driver *drv; 849 int status; 850 851 next = hub->tt.clear_list.next; 852 clear = list_entry(next, struct usb_tt_clear, clear_list); 853 list_del(&clear->clear_list); 854 855 /* drop lock so HCD can concurrently report other TT errors */ 856 spin_unlock_irqrestore(&hub->tt.lock, flags); 857 status = hub_clear_tt_buffer(hdev, clear->devinfo, clear->tt); 858 if (status && status != -ENODEV) 859 dev_err(&hdev->dev, 860 "clear tt %d (%04x) error %d\n", 861 clear->tt, clear->devinfo, status); 862 863 /* Tell the HCD, even if the operation failed */ 864 drv = clear->hcd->driver; 865 if (drv->clear_tt_buffer_complete) 866 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); 867 868 kfree(clear); 869 spin_lock_irqsave(&hub->tt.lock, flags); 870 } 871 spin_unlock_irqrestore(&hub->tt.lock, flags); 872 } 873 874 /** 875 * usb_hub_set_port_power - control hub port's power state 876 * @hdev: USB device belonging to the usb hub 877 * @hub: target hub 878 * @port1: port index 879 * @set: expected status 880 * 881 * call this function to control port's power via setting or 882 * clearing the port's PORT_POWER feature. 883 * 884 * Return: 0 if successful. A negative error code otherwise. 885 */ 886 int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub, 887 int port1, bool set) 888 { 889 int ret; 890 891 if (set) 892 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 893 else 894 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 895 896 if (ret) 897 return ret; 898 899 if (set) 900 set_bit(port1, hub->power_bits); 901 else 902 clear_bit(port1, hub->power_bits); 903 return 0; 904 } 905 906 /** 907 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub 908 * @urb: an URB associated with the failed or incomplete split transaction 909 * 910 * High speed HCDs use this to tell the hub driver that some split control or 911 * bulk transaction failed in a way that requires clearing internal state of 912 * a transaction translator. This is normally detected (and reported) from 913 * interrupt context. 914 * 915 * It may not be possible for that hub to handle additional full (or low) 916 * speed transactions until that state is fully cleared out. 917 * 918 * Return: 0 if successful. A negative error code otherwise. 919 */ 920 int usb_hub_clear_tt_buffer(struct urb *urb) 921 { 922 struct usb_device *udev = urb->dev; 923 int pipe = urb->pipe; 924 struct usb_tt *tt = udev->tt; 925 unsigned long flags; 926 struct usb_tt_clear *clear; 927 928 /* we've got to cope with an arbitrary number of pending TT clears, 929 * since each TT has "at least two" buffers that can need it (and 930 * there can be many TTs per hub). even if they're uncommon. 931 */ 932 clear = kmalloc_obj(*clear, GFP_ATOMIC); 933 if (clear == NULL) { 934 dev_err(&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 935 /* FIXME recover somehow ... RESET_TT? */ 936 return -ENOMEM; 937 } 938 939 /* info that CLEAR_TT_BUFFER needs */ 940 clear->tt = tt->multi ? udev->ttport : 1; 941 clear->devinfo = usb_pipeendpoint (pipe); 942 clear->devinfo |= ((u16)udev->devaddr) << 4; 943 clear->devinfo |= usb_pipecontrol(pipe) 944 ? (USB_ENDPOINT_XFER_CONTROL << 11) 945 : (USB_ENDPOINT_XFER_BULK << 11); 946 if (usb_pipein(pipe)) 947 clear->devinfo |= 1 << 15; 948 949 /* info for completion callback */ 950 clear->hcd = bus_to_hcd(udev->bus); 951 clear->ep = urb->ep; 952 953 /* tell keventd to clear state for this TT */ 954 spin_lock_irqsave(&tt->lock, flags); 955 list_add_tail(&clear->clear_list, &tt->clear_list); 956 schedule_work(&tt->clear_work); 957 spin_unlock_irqrestore(&tt->lock, flags); 958 return 0; 959 } 960 EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); 961 962 static void hub_power_on(struct usb_hub *hub, bool do_delay) 963 { 964 int port1; 965 966 /* Enable power on each port. Some hubs have reserved values 967 * of LPSM (> 2) in their descriptors, even though they are 968 * USB 2.0 hubs. Some hubs do not implement port-power switching 969 * but only emulate it. In all cases, the ports won't work 970 * unless we send these messages to the hub. 971 */ 972 if (hub_is_port_power_switchable(hub)) 973 dev_dbg(hub->intfdev, "enabling power on all ports\n"); 974 else 975 dev_dbg(hub->intfdev, "trying to enable port power on " 976 "non-switchable hub\n"); 977 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++) 978 if (test_bit(port1, hub->power_bits)) 979 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); 980 else 981 usb_clear_port_feature(hub->hdev, port1, 982 USB_PORT_FEAT_POWER); 983 if (do_delay) 984 msleep(hub_power_on_good_delay(hub)); 985 } 986 987 static int hub_hub_status(struct usb_hub *hub, 988 u16 *status, u16 *change) 989 { 990 int ret; 991 992 mutex_lock(&hub->status_mutex); 993 ret = get_hub_status(hub->hdev, &hub->status->hub); 994 if (ret < 0) { 995 if (ret != -ENODEV) 996 dev_err(hub->intfdev, 997 "%s failed (err = %d)\n", __func__, ret); 998 } else { 999 *status = le16_to_cpu(hub->status->hub.wHubStatus); 1000 *change = le16_to_cpu(hub->status->hub.wHubChange); 1001 ret = 0; 1002 } 1003 mutex_unlock(&hub->status_mutex); 1004 return ret; 1005 } 1006 1007 static int hub_set_port_link_state(struct usb_hub *hub, int port1, 1008 unsigned int link_status) 1009 { 1010 return set_port_feature(hub->hdev, 1011 port1 | (link_status << 3), 1012 USB_PORT_FEAT_LINK_STATE); 1013 } 1014 1015 /* 1016 * Disable a port and mark a logical connect-change event, so that some 1017 * time later hub_wq will disconnect() any existing usb_device on the port 1018 * and will re-enumerate if there actually is a device attached. 1019 */ 1020 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 1021 { 1022 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n"); 1023 hub_port_disable(hub, port1, 1); 1024 1025 /* FIXME let caller ask to power down the port: 1026 * - some devices won't enumerate without a VBUS power cycle 1027 * - SRP saves power that way 1028 * - ... new call, TBD ... 1029 * That's easy if this hub can switch power per-port, and 1030 * hub_wq reactivates the port later (timer, SRP, etc). 1031 * Powerdown must be optional, because of reset/DFU. 1032 */ 1033 1034 set_bit(port1, hub->change_bits); 1035 kick_hub_wq(hub); 1036 } 1037 1038 /** 1039 * usb_remove_device - disable a device's port on its parent hub 1040 * @udev: device to be disabled and removed 1041 * Context: @udev locked, must be able to sleep. 1042 * 1043 * After @udev's port has been disabled, hub_wq is notified and it will 1044 * see that the device has been disconnected. When the device is 1045 * physically unplugged and something is plugged in, the events will 1046 * be received and processed normally. 1047 * 1048 * Return: 0 if successful. A negative error code otherwise. 1049 */ 1050 int usb_remove_device(struct usb_device *udev) 1051 { 1052 struct usb_hub *hub; 1053 struct usb_interface *intf; 1054 int ret; 1055 1056 if (!udev->parent) /* Can't remove a root hub */ 1057 return -EINVAL; 1058 hub = usb_hub_to_struct_hub(udev->parent); 1059 intf = to_usb_interface(hub->intfdev); 1060 1061 ret = usb_autopm_get_interface(intf); 1062 if (ret < 0) 1063 return ret; 1064 1065 set_bit(udev->portnum, hub->removed_bits); 1066 hub_port_logical_disconnect(hub, udev->portnum); 1067 usb_autopm_put_interface(intf); 1068 return 0; 1069 } 1070 1071 enum hub_activation_type { 1072 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ 1073 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, 1074 }; 1075 1076 static void hub_init_func2(struct work_struct *ws); 1077 static void hub_init_func3(struct work_struct *ws); 1078 1079 static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) 1080 { 1081 struct usb_device *hdev = hub->hdev; 1082 struct usb_hcd *hcd; 1083 int ret; 1084 int port1; 1085 int status; 1086 bool need_debounce_delay = false; 1087 unsigned delay; 1088 1089 /* Continue a partial initialization */ 1090 if (type == HUB_INIT2 || type == HUB_INIT3) { 1091 device_lock(&hdev->dev); 1092 1093 /* Was the hub disconnected while we were waiting? */ 1094 if (hub->disconnected) 1095 goto disconnected; 1096 if (type == HUB_INIT2) 1097 goto init2; 1098 goto init3; 1099 } 1100 1101 hub_get(hub); 1102 1103 /* The superspeed hub except for root hub has to use Hub Depth 1104 * value as an offset into the route string to locate the bits 1105 * it uses to determine the downstream port number. So hub driver 1106 * should send a set hub depth request to superspeed hub after 1107 * the superspeed hub is set configuration in initialization or 1108 * reset procedure. 1109 * 1110 * After a resume, port power should still be on. 1111 * For any other type of activation, turn it on. 1112 */ 1113 if (type != HUB_RESUME) { 1114 if (hdev->parent && hub_is_superspeed(hdev)) { 1115 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 1116 HUB_SET_DEPTH, USB_RT_HUB, 1117 hdev->level - 1, 0, NULL, 0, 1118 USB_CTRL_SET_TIMEOUT); 1119 if (ret < 0) 1120 dev_err(hub->intfdev, 1121 "set hub depth failed\n"); 1122 } 1123 1124 /* Speed up system boot by using a delayed_work for the 1125 * hub's initial power-up delays. This is pretty awkward 1126 * and the implementation looks like a home-brewed sort of 1127 * setjmp/longjmp, but it saves at least 100 ms for each 1128 * root hub (assuming usbcore is compiled into the kernel 1129 * rather than as a module). It adds up. 1130 * 1131 * This can't be done for HUB_RESUME or HUB_RESET_RESUME 1132 * because for those activation types the ports have to be 1133 * operational when we return. In theory this could be done 1134 * for HUB_POST_RESET, but it's easier not to. 1135 */ 1136 if (type == HUB_INIT) { 1137 delay = hub_power_on_good_delay(hub); 1138 1139 hub_power_on(hub, false); 1140 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2); 1141 queue_delayed_work(system_power_efficient_wq, 1142 &hub->init_work, 1143 msecs_to_jiffies(delay)); 1144 1145 /* Suppress autosuspend until init is done */ 1146 usb_autopm_get_interface_no_resume( 1147 to_usb_interface(hub->intfdev)); 1148 return; /* Continues at init2: below */ 1149 } else if (type == HUB_RESET_RESUME) { 1150 /* The internal host controller state for the hub device 1151 * may be gone after a host power loss on system resume. 1152 * Update the device's info so the HW knows it's a hub. 1153 */ 1154 hcd = bus_to_hcd(hdev->bus); 1155 if (hcd->driver->update_hub_device) { 1156 ret = hcd->driver->update_hub_device(hcd, hdev, 1157 &hub->tt, GFP_NOIO); 1158 if (ret < 0) { 1159 dev_err(hub->intfdev, 1160 "Host not accepting hub info update\n"); 1161 dev_err(hub->intfdev, 1162 "LS/FS devices and hubs may not work under this hub\n"); 1163 } 1164 } 1165 hub_power_on(hub, true); 1166 } else { 1167 hub_power_on(hub, true); 1168 } 1169 /* Give some time on remote wakeup to let links to transit to U0 */ 1170 } else if (hub_is_superspeed(hub->hdev)) 1171 msleep(20); 1172 1173 init2: 1174 1175 /* 1176 * Check each port and set hub->change_bits to let hub_wq know 1177 * which ports need attention. 1178 */ 1179 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 1180 struct usb_port *port_dev = hub->ports[port1 - 1]; 1181 struct usb_device *udev = port_dev->child; 1182 u16 portstatus, portchange; 1183 1184 portstatus = portchange = 0; 1185 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 1186 if (status) 1187 goto abort; 1188 1189 if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) 1190 dev_dbg(&port_dev->dev, "status %04x change %04x\n", 1191 portstatus, portchange); 1192 1193 /* 1194 * After anything other than HUB_RESUME (i.e., initialization 1195 * or any sort of reset), every port should be disabled. 1196 * Unconnected ports should likewise be disabled (paranoia), 1197 * and so should ports for which we have no usb_device. 1198 */ 1199 if ((portstatus & USB_PORT_STAT_ENABLE) && ( 1200 type != HUB_RESUME || 1201 !(portstatus & USB_PORT_STAT_CONNECTION) || 1202 !udev || 1203 udev->state == USB_STATE_NOTATTACHED)) { 1204 /* 1205 * USB3 protocol ports will automatically transition 1206 * to Enabled state when detect an USB3.0 device attach. 1207 * Do not disable USB3 protocol ports, just pretend 1208 * power was lost 1209 */ 1210 portstatus &= ~USB_PORT_STAT_ENABLE; 1211 if (!hub_is_superspeed(hdev)) 1212 usb_clear_port_feature(hdev, port1, 1213 USB_PORT_FEAT_ENABLE); 1214 } 1215 1216 /* Make sure a warm-reset request is handled by port_event */ 1217 if (type == HUB_RESUME && 1218 hub_port_warm_reset_required(hub, port1, portstatus)) 1219 set_bit(port1, hub->event_bits); 1220 1221 /* 1222 * Add debounce if USB3 link is in polling/link training state. 1223 * Link will automatically transition to Enabled state after 1224 * link training completes. 1225 */ 1226 if (hub_is_superspeed(hdev) && 1227 ((portstatus & USB_PORT_STAT_LINK_STATE) == 1228 USB_SS_PORT_LS_POLLING)) 1229 need_debounce_delay = true; 1230 1231 /* Clear status-change flags; we'll debounce later */ 1232 if (portchange & USB_PORT_STAT_C_CONNECTION) { 1233 need_debounce_delay = true; 1234 usb_clear_port_feature(hub->hdev, port1, 1235 USB_PORT_FEAT_C_CONNECTION); 1236 } 1237 if (portchange & USB_PORT_STAT_C_ENABLE) { 1238 need_debounce_delay = true; 1239 usb_clear_port_feature(hub->hdev, port1, 1240 USB_PORT_FEAT_C_ENABLE); 1241 } 1242 if (portchange & USB_PORT_STAT_C_RESET) { 1243 need_debounce_delay = true; 1244 usb_clear_port_feature(hub->hdev, port1, 1245 USB_PORT_FEAT_C_RESET); 1246 } 1247 if ((portchange & USB_PORT_STAT_C_BH_RESET) && 1248 hub_is_superspeed(hub->hdev)) { 1249 need_debounce_delay = true; 1250 usb_clear_port_feature(hub->hdev, port1, 1251 USB_PORT_FEAT_C_BH_PORT_RESET); 1252 } 1253 /* We can forget about a "removed" device when there's a 1254 * physical disconnect or the connect status changes. 1255 */ 1256 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 1257 (portchange & USB_PORT_STAT_C_CONNECTION)) 1258 clear_bit(port1, hub->removed_bits); 1259 1260 if (!udev || udev->state == USB_STATE_NOTATTACHED) { 1261 /* Tell hub_wq to disconnect the device or 1262 * check for a new connection or over current condition. 1263 * Based on USB2.0 Spec Section 11.12.5, 1264 * C_PORT_OVER_CURRENT could be set while 1265 * PORT_OVER_CURRENT is not. So check for any of them. 1266 */ 1267 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) || 1268 (portchange & USB_PORT_STAT_C_CONNECTION) || 1269 (portstatus & USB_PORT_STAT_OVERCURRENT) || 1270 (portchange & USB_PORT_STAT_C_OVERCURRENT)) 1271 set_bit(port1, hub->change_bits); 1272 1273 } else if (portstatus & USB_PORT_STAT_ENABLE) { 1274 bool port_resumed = (portstatus & 1275 USB_PORT_STAT_LINK_STATE) == 1276 USB_SS_PORT_LS_U0; 1277 /* The power session apparently survived the resume. 1278 * If there was an overcurrent or suspend change 1279 * (i.e., remote wakeup request), have hub_wq 1280 * take care of it. Look at the port link state 1281 * for USB 3.0 hubs, since they don't have a suspend 1282 * change bit, and they don't set the port link change 1283 * bit on device-initiated resume. 1284 */ 1285 if (portchange || (hub_is_superspeed(hub->hdev) && 1286 port_resumed)) 1287 set_bit(port1, hub->event_bits); 1288 1289 } else if (udev->persist_enabled) { 1290 #ifdef CONFIG_PM 1291 udev->reset_resume = 1; 1292 #endif 1293 /* Don't set the change_bits when the device 1294 * was powered off. 1295 */ 1296 if (test_bit(port1, hub->power_bits)) 1297 set_bit(port1, hub->change_bits); 1298 1299 } else { 1300 /* The power session is gone; tell hub_wq */ 1301 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1302 set_bit(port1, hub->change_bits); 1303 } 1304 } 1305 1306 /* If no port-status-change flags were set, we don't need any 1307 * debouncing. If flags were set we can try to debounce the 1308 * ports all at once right now, instead of letting hub_wq do them 1309 * one at a time later on. 1310 * 1311 * If any port-status changes do occur during this delay, hub_wq 1312 * will see them later and handle them normally. 1313 */ 1314 if (need_debounce_delay) { 1315 delay = HUB_DEBOUNCE_STABLE; 1316 1317 /* Don't do a long sleep inside a workqueue routine */ 1318 if (type == HUB_INIT2) { 1319 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3); 1320 queue_delayed_work(system_power_efficient_wq, 1321 &hub->init_work, 1322 msecs_to_jiffies(delay)); 1323 device_unlock(&hdev->dev); 1324 return; /* Continues at init3: below */ 1325 } else { 1326 msleep(delay); 1327 } 1328 } 1329 init3: 1330 hub->quiescing = 0; 1331 1332 status = usb_submit_urb(hub->urb, GFP_NOIO); 1333 if (status < 0) 1334 dev_err(hub->intfdev, "activate --> %d\n", status); 1335 if (hub->has_indicators && blinkenlights) 1336 queue_delayed_work(system_power_efficient_wq, 1337 &hub->leds, LED_CYCLE_PERIOD); 1338 1339 /* Scan all ports that need attention */ 1340 kick_hub_wq(hub); 1341 abort: 1342 if (type == HUB_INIT2 || type == HUB_INIT3) { 1343 /* Allow autosuspend if it was suppressed */ 1344 disconnected: 1345 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); 1346 device_unlock(&hdev->dev); 1347 } 1348 1349 if (type == HUB_RESUME && hub_is_superspeed(hub->hdev)) { 1350 /* give usb3 downstream links training time after hub resume */ 1351 usb_autopm_get_interface_no_resume( 1352 to_usb_interface(hub->intfdev)); 1353 1354 queue_delayed_work(system_power_efficient_wq, 1355 &hub->post_resume_work, 1356 msecs_to_jiffies(USB_SS_PORT_U0_WAKE_TIME)); 1357 return; 1358 } 1359 1360 hub_put(hub); 1361 } 1362 1363 /* Implement the continuations for the delays above */ 1364 static void hub_init_func2(struct work_struct *ws) 1365 { 1366 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 1367 1368 hub_activate(hub, HUB_INIT2); 1369 } 1370 1371 static void hub_init_func3(struct work_struct *ws) 1372 { 1373 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); 1374 1375 hub_activate(hub, HUB_INIT3); 1376 } 1377 1378 static void hub_post_resume(struct work_struct *ws) 1379 { 1380 struct usb_hub *hub = container_of(ws, struct usb_hub, post_resume_work.work); 1381 1382 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); 1383 hub_put(hub); 1384 } 1385 1386 enum hub_quiescing_type { 1387 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND 1388 }; 1389 1390 static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) 1391 { 1392 struct usb_device *hdev = hub->hdev; 1393 unsigned long flags; 1394 int i; 1395 1396 /* hub_wq and related activity won't re-trigger */ 1397 spin_lock_irqsave(&hub->irq_urb_lock, flags); 1398 hub->quiescing = 1; 1399 spin_unlock_irqrestore(&hub->irq_urb_lock, flags); 1400 1401 if (type != HUB_SUSPEND) { 1402 /* Disconnect all the children */ 1403 for (i = 0; i < hdev->maxchild; ++i) { 1404 if (hub->ports[i]->child) 1405 usb_disconnect(&hub->ports[i]->child); 1406 } 1407 } 1408 1409 /* Stop hub_wq and related activity */ 1410 timer_delete_sync(&hub->irq_urb_retry); 1411 flush_delayed_work(&hub->post_resume_work); 1412 usb_kill_urb(hub->urb); 1413 if (hub->has_indicators) 1414 cancel_delayed_work_sync(&hub->leds); 1415 if (hub->tt.hub) 1416 flush_work(&hub->tt.clear_work); 1417 } 1418 1419 static void hub_pm_barrier_for_all_ports(struct usb_hub *hub) 1420 { 1421 int i; 1422 1423 for (i = 0; i < hub->hdev->maxchild; ++i) 1424 pm_runtime_barrier(&hub->ports[i]->dev); 1425 } 1426 1427 /* caller has locked the hub device */ 1428 static int hub_pre_reset(struct usb_interface *intf) 1429 { 1430 struct usb_hub *hub = usb_get_intfdata(intf); 1431 1432 hub_quiesce(hub, HUB_PRE_RESET); 1433 hub->in_reset = 1; 1434 hub_pm_barrier_for_all_ports(hub); 1435 return 0; 1436 } 1437 1438 /* caller has locked the hub device */ 1439 static int hub_post_reset(struct usb_interface *intf) 1440 { 1441 struct usb_hub *hub = usb_get_intfdata(intf); 1442 1443 hub->in_reset = 0; 1444 hub_pm_barrier_for_all_ports(hub); 1445 hub_activate(hub, HUB_POST_RESET); 1446 return 0; 1447 } 1448 1449 static int hub_configure(struct usb_hub *hub, 1450 struct usb_endpoint_descriptor *endpoint) 1451 { 1452 struct usb_hcd *hcd; 1453 struct usb_device *hdev = hub->hdev; 1454 struct device *hub_dev = hub->intfdev; 1455 u16 hubstatus, hubchange; 1456 u16 wHubCharacteristics; 1457 unsigned int pipe; 1458 int maxp, ret, i; 1459 char *message = "out of memory"; 1460 unsigned unit_load; 1461 unsigned full_load; 1462 unsigned maxchild; 1463 1464 hub->buffer = kmalloc_obj(*hub->buffer); 1465 if (!hub->buffer) { 1466 ret = -ENOMEM; 1467 goto fail; 1468 } 1469 1470 hub->status = kmalloc_obj(*hub->status); 1471 if (!hub->status) { 1472 ret = -ENOMEM; 1473 goto fail; 1474 } 1475 mutex_init(&hub->status_mutex); 1476 1477 hub->descriptor = kzalloc_obj(*hub->descriptor); 1478 if (!hub->descriptor) { 1479 ret = -ENOMEM; 1480 goto fail; 1481 } 1482 1483 /* Request the entire hub descriptor. 1484 * hub->descriptor can handle USB_MAXCHILDREN ports, 1485 * but a (non-SS) hub can/will return fewer bytes here. 1486 */ 1487 ret = get_hub_descriptor(hdev, hub->descriptor); 1488 if (ret < 0) { 1489 message = "can't read hub descriptor"; 1490 goto fail; 1491 } 1492 1493 maxchild = USB_MAXCHILDREN; 1494 if (hub_is_superspeed(hdev)) 1495 maxchild = min_t(unsigned, maxchild, USB_SS_MAXPORTS); 1496 1497 if (hub->descriptor->bNbrPorts > maxchild) { 1498 message = "hub has too many ports!"; 1499 ret = -ENODEV; 1500 goto fail; 1501 } else if (hub->descriptor->bNbrPorts == 0) { 1502 message = "hub doesn't have any ports!"; 1503 ret = -ENODEV; 1504 goto fail; 1505 } 1506 1507 /* 1508 * Accumulate wHubDelay + 40ns for every hub in the tree of devices. 1509 * The resulting value will be used for SetIsochDelay() request. 1510 */ 1511 if (hub_is_superspeed(hdev) || hub_is_superspeedplus(hdev)) { 1512 u32 delay = __le16_to_cpu(hub->descriptor->u.ss.wHubDelay); 1513 1514 if (hdev->parent) 1515 delay += hdev->parent->hub_delay; 1516 1517 delay += USB_TP_TRANSMISSION_DELAY; 1518 hdev->hub_delay = min_t(u32, delay, USB_TP_TRANSMISSION_DELAY_MAX); 1519 } 1520 1521 maxchild = hub->descriptor->bNbrPorts; 1522 dev_info(hub_dev, "%d port%s detected\n", maxchild, 1523 str_plural(maxchild)); 1524 1525 hub->ports = kzalloc_objs(struct usb_port *, maxchild); 1526 if (!hub->ports) { 1527 ret = -ENOMEM; 1528 goto fail; 1529 } 1530 1531 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 1532 if (hub_is_superspeed(hdev)) { 1533 unit_load = 150; 1534 full_load = 900; 1535 } else { 1536 unit_load = 100; 1537 full_load = 500; 1538 } 1539 1540 /* FIXME for USB 3.0, skip for now */ 1541 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && 1542 !(hub_is_superspeed(hdev))) { 1543 char portstr[USB_MAXCHILDREN + 1]; 1544 1545 for (i = 0; i < maxchild; i++) 1546 portstr[i] = hub->descriptor->u.hs.DeviceRemovable 1547 [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 1548 ? 'F' : 'R'; 1549 portstr[maxchild] = 0; 1550 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 1551 } else 1552 dev_dbg(hub_dev, "standalone hub\n"); 1553 1554 switch (wHubCharacteristics & HUB_CHAR_LPSM) { 1555 case HUB_CHAR_COMMON_LPSM: 1556 dev_dbg(hub_dev, "ganged power switching\n"); 1557 break; 1558 case HUB_CHAR_INDV_PORT_LPSM: 1559 dev_dbg(hub_dev, "individual port power switching\n"); 1560 break; 1561 case HUB_CHAR_NO_LPSM: 1562 case HUB_CHAR_LPSM: 1563 dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 1564 break; 1565 } 1566 1567 switch (wHubCharacteristics & HUB_CHAR_OCPM) { 1568 case HUB_CHAR_COMMON_OCPM: 1569 dev_dbg(hub_dev, "global over-current protection\n"); 1570 break; 1571 case HUB_CHAR_INDV_PORT_OCPM: 1572 dev_dbg(hub_dev, "individual port over-current protection\n"); 1573 break; 1574 case HUB_CHAR_NO_OCPM: 1575 case HUB_CHAR_OCPM: 1576 dev_dbg(hub_dev, "no over-current protection\n"); 1577 break; 1578 } 1579 1580 spin_lock_init(&hub->tt.lock); 1581 INIT_LIST_HEAD(&hub->tt.clear_list); 1582 INIT_WORK(&hub->tt.clear_work, hub_tt_work); 1583 switch (hdev->descriptor.bDeviceProtocol) { 1584 case USB_HUB_PR_FS: 1585 break; 1586 case USB_HUB_PR_HS_SINGLE_TT: 1587 dev_dbg(hub_dev, "Single TT\n"); 1588 hub->tt.hub = hdev; 1589 break; 1590 case USB_HUB_PR_HS_MULTI_TT: 1591 ret = usb_set_interface(hdev, 0, 1); 1592 if (ret == 0) { 1593 dev_dbg(hub_dev, "TT per port\n"); 1594 hub->tt.multi = 1; 1595 } else 1596 dev_err(hub_dev, "Using single TT (err %d)\n", 1597 ret); 1598 hub->tt.hub = hdev; 1599 break; 1600 case USB_HUB_PR_SS: 1601 /* USB 3.0 hubs don't have a TT */ 1602 break; 1603 default: 1604 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 1605 hdev->descriptor.bDeviceProtocol); 1606 break; 1607 } 1608 1609 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ 1610 switch (wHubCharacteristics & HUB_CHAR_TTTT) { 1611 case HUB_TTTT_8_BITS: 1612 if (hdev->descriptor.bDeviceProtocol != 0) { 1613 hub->tt.think_time = 666; 1614 dev_dbg(hub_dev, "TT requires at most %d " 1615 "FS bit times (%d ns)\n", 1616 8, hub->tt.think_time); 1617 } 1618 break; 1619 case HUB_TTTT_16_BITS: 1620 hub->tt.think_time = 666 * 2; 1621 dev_dbg(hub_dev, "TT requires at most %d " 1622 "FS bit times (%d ns)\n", 1623 16, hub->tt.think_time); 1624 break; 1625 case HUB_TTTT_24_BITS: 1626 hub->tt.think_time = 666 * 3; 1627 dev_dbg(hub_dev, "TT requires at most %d " 1628 "FS bit times (%d ns)\n", 1629 24, hub->tt.think_time); 1630 break; 1631 case HUB_TTTT_32_BITS: 1632 hub->tt.think_time = 666 * 4; 1633 dev_dbg(hub_dev, "TT requires at most %d " 1634 "FS bit times (%d ns)\n", 1635 32, hub->tt.think_time); 1636 break; 1637 } 1638 1639 /* probe() zeroes hub->indicator[] */ 1640 if (wHubCharacteristics & HUB_CHAR_PORTIND) { 1641 hub->has_indicators = 1; 1642 dev_dbg(hub_dev, "Port indicators are supported\n"); 1643 } 1644 1645 dev_dbg(hub_dev, "power on to power good time: %dms\n", 1646 hub->descriptor->bPwrOn2PwrGood * 2); 1647 1648 /* power budgeting mostly matters with bus-powered hubs, 1649 * and battery-powered root hubs (may provide just 8 mA). 1650 */ 1651 ret = usb_get_std_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 1652 if (ret) { 1653 message = "can't get hub status"; 1654 goto fail; 1655 } 1656 hcd = bus_to_hcd(hdev->bus); 1657 if (hdev == hdev->bus->root_hub) { 1658 if (hcd->power_budget > 0) 1659 hdev->bus_mA = hcd->power_budget; 1660 else 1661 hdev->bus_mA = full_load * maxchild; 1662 if (hdev->bus_mA >= full_load) 1663 hub->mA_per_port = full_load; 1664 else { 1665 hub->mA_per_port = hdev->bus_mA; 1666 hub->limited_power = 1; 1667 } 1668 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 1669 int remaining = hdev->bus_mA - 1670 hub->descriptor->bHubContrCurrent; 1671 1672 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 1673 hub->descriptor->bHubContrCurrent); 1674 hub->limited_power = 1; 1675 1676 if (remaining < maxchild * unit_load) 1677 dev_warn(hub_dev, 1678 "insufficient power available " 1679 "to use all downstream ports\n"); 1680 hub->mA_per_port = unit_load; /* 7.2.1 */ 1681 1682 } else { /* Self-powered external hub */ 1683 /* FIXME: What about battery-powered external hubs that 1684 * provide less current per port? */ 1685 hub->mA_per_port = full_load; 1686 } 1687 if (hub->mA_per_port < full_load) 1688 dev_dbg(hub_dev, "%umA bus power budget for each child\n", 1689 hub->mA_per_port); 1690 1691 ret = hub_hub_status(hub, &hubstatus, &hubchange); 1692 if (ret < 0) { 1693 message = "can't get hub status"; 1694 goto fail; 1695 } 1696 1697 /* local power status reports aren't always correct */ 1698 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 1699 dev_dbg(hub_dev, "local power source is %s\n", 1700 (hubstatus & HUB_STATUS_LOCAL_POWER) 1701 ? "lost (inactive)" : "good"); 1702 1703 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) 1704 dev_dbg(hub_dev, "%sover-current condition exists\n", 1705 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 1706 1707 /* set up the interrupt endpoint 1708 * We use the EP's maxpacket size instead of (PORTS+1+7)/8 1709 * bytes as USB2.0[11.12.3] says because some hubs are known 1710 * to send more data (and thus cause overflow). For root hubs, 1711 * maxpktsize is defined in hcd.c's fake endpoint descriptors 1712 * to be big enough for at least USB_MAXCHILDREN ports. */ 1713 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 1714 maxp = usb_maxpacket(hdev, pipe); 1715 1716 if (maxp > sizeof(*hub->buffer)) 1717 maxp = sizeof(*hub->buffer); 1718 1719 hub->urb = usb_alloc_urb(0, GFP_KERNEL); 1720 if (!hub->urb) { 1721 ret = -ENOMEM; 1722 goto fail; 1723 } 1724 1725 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 1726 hub, endpoint->bInterval); 1727 1728 /* maybe cycle the hub leds */ 1729 if (hub->has_indicators && blinkenlights) 1730 hub->indicator[0] = INDICATOR_CYCLE; 1731 1732 mutex_lock(&usb_port_peer_mutex); 1733 for (i = 0; i < maxchild; i++) { 1734 ret = usb_hub_create_port_device(hub, i + 1); 1735 if (ret < 0) { 1736 dev_err(hub->intfdev, 1737 "couldn't create port%d device.\n", i + 1); 1738 break; 1739 } 1740 } 1741 hdev->maxchild = i; 1742 for (i = 0; i < hdev->maxchild; i++) { 1743 struct usb_port *port_dev = hub->ports[i]; 1744 1745 pm_runtime_put(&port_dev->dev); 1746 } 1747 1748 mutex_unlock(&usb_port_peer_mutex); 1749 if (ret < 0) 1750 goto fail; 1751 1752 /* Update the HCD's internal representation of this hub before hub_wq 1753 * starts getting port status changes for devices under the hub. 1754 */ 1755 if (hcd->driver->update_hub_device) { 1756 ret = hcd->driver->update_hub_device(hcd, hdev, 1757 &hub->tt, GFP_KERNEL); 1758 if (ret < 0) { 1759 message = "can't update HCD hub info"; 1760 goto fail; 1761 } 1762 } 1763 1764 usb_hub_adjust_deviceremovable(hdev, hub->descriptor); 1765 1766 hub_activate(hub, HUB_INIT); 1767 return 0; 1768 1769 fail: 1770 dev_err(hub_dev, "config failed, %s (err %d)\n", 1771 message, ret); 1772 /* hub_disconnect() frees urb and descriptor */ 1773 return ret; 1774 } 1775 1776 static void hub_release(struct kref *kref) 1777 { 1778 struct usb_hub *hub = container_of(kref, struct usb_hub, kref); 1779 1780 usb_put_dev(hub->hdev); 1781 usb_put_intf(to_usb_interface(hub->intfdev)); 1782 kfree(hub); 1783 } 1784 1785 void hub_get(struct usb_hub *hub) 1786 { 1787 kref_get(&hub->kref); 1788 } 1789 1790 void hub_put(struct usb_hub *hub) 1791 { 1792 kref_put(&hub->kref, hub_release); 1793 } 1794 1795 static unsigned highspeed_hubs; 1796 1797 static void hub_disconnect(struct usb_interface *intf) 1798 { 1799 struct usb_hub *hub = usb_get_intfdata(intf); 1800 struct usb_device *hdev = interface_to_usbdev(intf); 1801 int port1; 1802 1803 /* 1804 * Stop adding new hub events. We do not want to block here and thus 1805 * will not try to remove any pending work item. 1806 */ 1807 hub->disconnected = 1; 1808 1809 /* Disconnect all children and quiesce the hub */ 1810 hub->error = 0; 1811 hub_quiesce(hub, HUB_DISCONNECT); 1812 1813 mutex_lock(&usb_port_peer_mutex); 1814 1815 /* Avoid races with recursively_mark_NOTATTACHED() */ 1816 spin_lock_irq(&device_state_lock); 1817 port1 = hdev->maxchild; 1818 hdev->maxchild = 0; 1819 usb_set_intfdata(intf, NULL); 1820 spin_unlock_irq(&device_state_lock); 1821 1822 for (; port1 > 0; --port1) 1823 usb_hub_remove_port_device(hub, port1); 1824 1825 mutex_unlock(&usb_port_peer_mutex); 1826 1827 if (hub->hdev->speed == USB_SPEED_HIGH) 1828 highspeed_hubs--; 1829 1830 usb_free_urb(hub->urb); 1831 kfree(hub->ports); 1832 kfree(hub->descriptor); 1833 kfree(hub->status); 1834 kfree(hub->buffer); 1835 1836 pm_suspend_ignore_children(&intf->dev, false); 1837 1838 if (hub->quirk_disable_autosuspend) 1839 usb_autopm_put_interface(intf); 1840 1841 onboard_dev_destroy_pdevs(&hub->onboard_devs); 1842 1843 hub_put(hub); 1844 } 1845 1846 static bool hub_descriptor_is_sane(struct usb_host_interface *desc) 1847 { 1848 /* Some hubs have a subclass of 1, which AFAICT according to the */ 1849 /* specs is not defined, but it works */ 1850 if (desc->desc.bInterfaceSubClass != 0 && 1851 desc->desc.bInterfaceSubClass != 1) 1852 return false; 1853 1854 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 1855 if (desc->desc.bNumEndpoints != 1) 1856 return false; 1857 1858 /* If the first endpoint is not interrupt IN, we'd better punt! */ 1859 if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc)) 1860 return false; 1861 1862 return true; 1863 } 1864 1865 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 1866 { 1867 struct usb_host_interface *desc; 1868 struct usb_device *hdev; 1869 struct usb_hub *hub; 1870 1871 desc = intf->cur_altsetting; 1872 hdev = interface_to_usbdev(intf); 1873 1874 /* 1875 * The USB 2.0 spec prohibits hubs from having more than one 1876 * configuration or interface, and we rely on this prohibition. 1877 * Refuse to accept a device that violates it. 1878 */ 1879 if (hdev->descriptor.bNumConfigurations > 1 || 1880 hdev->actconfig->desc.bNumInterfaces > 1) { 1881 dev_err(&intf->dev, "Invalid hub with more than one config or interface\n"); 1882 return -EINVAL; 1883 } 1884 1885 /* 1886 * Set default autosuspend delay as 0 to speedup bus suspend, 1887 * based on the below considerations: 1888 * 1889 * - Unlike other drivers, the hub driver does not rely on the 1890 * autosuspend delay to provide enough time to handle a wakeup 1891 * event, and the submitted status URB is just to check future 1892 * change on hub downstream ports, so it is safe to do it. 1893 * 1894 * - The patch might cause one or more auto supend/resume for 1895 * below very rare devices when they are plugged into hub 1896 * first time: 1897 * 1898 * devices having trouble initializing, and disconnect 1899 * themselves from the bus and then reconnect a second 1900 * or so later 1901 * 1902 * devices just for downloading firmware, and disconnects 1903 * themselves after completing it 1904 * 1905 * For these quite rare devices, their drivers may change the 1906 * autosuspend delay of their parent hub in the probe() to one 1907 * appropriate value to avoid the subtle problem if someone 1908 * does care it. 1909 * 1910 * - The patch may cause one or more auto suspend/resume on 1911 * hub during running 'lsusb', but it is probably too 1912 * infrequent to worry about. 1913 * 1914 * - Change autosuspend delay of hub can avoid unnecessary auto 1915 * suspend timer for hub, also may decrease power consumption 1916 * of USB bus. 1917 * 1918 * - If user has indicated to prevent autosuspend by passing 1919 * usbcore.autosuspend = -1 then keep autosuspend disabled. 1920 */ 1921 #ifdef CONFIG_PM 1922 if (hdev->dev.power.autosuspend_delay >= 0) 1923 pm_runtime_set_autosuspend_delay(&hdev->dev, 0); 1924 #endif 1925 1926 /* 1927 * Hubs have proper suspend/resume support, except for root hubs 1928 * where the controller driver doesn't have bus_suspend and 1929 * bus_resume methods. 1930 */ 1931 if (hdev->parent) { /* normal device */ 1932 usb_enable_autosuspend(hdev); 1933 } else { /* root hub */ 1934 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; 1935 1936 if (drv->bus_suspend && drv->bus_resume) 1937 usb_enable_autosuspend(hdev); 1938 } 1939 1940 if (hdev->level == MAX_TOPO_LEVEL) { 1941 dev_err(&intf->dev, 1942 "Unsupported bus topology: hub nested too deep\n"); 1943 return -E2BIG; 1944 } 1945 1946 #ifdef CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB 1947 if (hdev->parent) { 1948 dev_warn(&intf->dev, "ignoring external hub\n"); 1949 return -ENODEV; 1950 } 1951 #endif 1952 1953 if (!hub_descriptor_is_sane(desc)) { 1954 dev_err(&intf->dev, "bad descriptor, ignoring hub\n"); 1955 return -EIO; 1956 } 1957 1958 /* We found a hub */ 1959 dev_info(&intf->dev, "USB hub found\n"); 1960 1961 hub = kzalloc_obj(*hub); 1962 if (!hub) 1963 return -ENOMEM; 1964 1965 kref_init(&hub->kref); 1966 hub->intfdev = &intf->dev; 1967 hub->hdev = hdev; 1968 INIT_DELAYED_WORK(&hub->leds, led_work); 1969 INIT_DELAYED_WORK(&hub->init_work, NULL); 1970 INIT_DELAYED_WORK(&hub->post_resume_work, hub_post_resume); 1971 INIT_WORK(&hub->events, hub_event); 1972 INIT_LIST_HEAD(&hub->onboard_devs); 1973 spin_lock_init(&hub->irq_urb_lock); 1974 timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0); 1975 usb_get_intf(intf); 1976 usb_get_dev(hdev); 1977 1978 usb_set_intfdata(intf, hub); 1979 intf->needs_remote_wakeup = 1; 1980 pm_suspend_ignore_children(&intf->dev, true); 1981 1982 if (hdev->speed == USB_SPEED_HIGH) 1983 highspeed_hubs++; 1984 1985 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) 1986 hub->quirk_check_port_auto_suspend = 1; 1987 1988 if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) { 1989 hub->quirk_disable_autosuspend = 1; 1990 usb_autopm_get_interface_no_resume(intf); 1991 } 1992 1993 if ((id->driver_info & HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL) && 1994 desc->endpoint[0].desc.bInterval > USB_REDUCE_FRAME_INTR_BINTERVAL) { 1995 desc->endpoint[0].desc.bInterval = 1996 USB_REDUCE_FRAME_INTR_BINTERVAL; 1997 /* Tell the HCD about the interrupt ep's new bInterval */ 1998 usb_set_interface(hdev, 0, 0); 1999 } 2000 2001 if (hub_configure(hub, &desc->endpoint[0].desc) >= 0) { 2002 onboard_dev_create_pdevs(hdev, &hub->onboard_devs); 2003 2004 return 0; 2005 } 2006 2007 hub_disconnect(intf); 2008 return -ENODEV; 2009 } 2010 2011 static int 2012 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 2013 { 2014 struct usb_device *hdev = interface_to_usbdev(intf); 2015 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 2016 2017 /* assert ifno == 0 (part of hub spec) */ 2018 switch (code) { 2019 case USBDEVFS_HUB_PORTINFO: { 2020 struct usbdevfs_hub_portinfo *info = user_data; 2021 int i; 2022 2023 spin_lock_irq(&device_state_lock); 2024 if (hdev->devnum <= 0) 2025 info->nports = 0; 2026 else { 2027 info->nports = hdev->maxchild; 2028 for (i = 0; i < info->nports; i++) { 2029 if (hub->ports[i]->child == NULL) 2030 info->port[i] = 0; 2031 else 2032 info->port[i] = 2033 hub->ports[i]->child->devnum; 2034 } 2035 } 2036 spin_unlock_irq(&device_state_lock); 2037 2038 return info->nports + 1; 2039 } 2040 2041 default: 2042 return -ENOSYS; 2043 } 2044 } 2045 2046 /* 2047 * Allow user programs to claim ports on a hub. When a device is attached 2048 * to one of these "claimed" ports, the program will "own" the device. 2049 */ 2050 static int find_port_owner(struct usb_device *hdev, unsigned port1, 2051 struct usb_dev_state ***ppowner) 2052 { 2053 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 2054 2055 if (hdev->state == USB_STATE_NOTATTACHED) 2056 return -ENODEV; 2057 if (port1 == 0 || port1 > hdev->maxchild) 2058 return -EINVAL; 2059 2060 /* Devices not managed by the hub driver 2061 * will always have maxchild equal to 0. 2062 */ 2063 *ppowner = &(hub->ports[port1 - 1]->port_owner); 2064 return 0; 2065 } 2066 2067 /* In the following three functions, the caller must hold hdev's lock */ 2068 int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 2069 struct usb_dev_state *owner) 2070 { 2071 int rc; 2072 struct usb_dev_state **powner; 2073 2074 rc = find_port_owner(hdev, port1, &powner); 2075 if (rc) 2076 return rc; 2077 if (*powner) 2078 return -EBUSY; 2079 *powner = owner; 2080 return rc; 2081 } 2082 EXPORT_SYMBOL_GPL(usb_hub_claim_port); 2083 2084 int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 2085 struct usb_dev_state *owner) 2086 { 2087 int rc; 2088 struct usb_dev_state **powner; 2089 2090 rc = find_port_owner(hdev, port1, &powner); 2091 if (rc) 2092 return rc; 2093 if (*powner != owner) 2094 return -ENOENT; 2095 *powner = NULL; 2096 return rc; 2097 } 2098 EXPORT_SYMBOL_GPL(usb_hub_release_port); 2099 2100 void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner) 2101 { 2102 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 2103 int n; 2104 2105 for (n = 0; n < hdev->maxchild; n++) { 2106 if (hub->ports[n]->port_owner == owner) 2107 hub->ports[n]->port_owner = NULL; 2108 } 2109 2110 } 2111 2112 /* The caller must hold udev's lock */ 2113 bool usb_device_is_owned(struct usb_device *udev) 2114 { 2115 struct usb_hub *hub; 2116 2117 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) 2118 return false; 2119 hub = usb_hub_to_struct_hub(udev->parent); 2120 return !!hub->ports[udev->portnum - 1]->port_owner; 2121 } 2122 2123 static void update_port_device_state(struct usb_device *udev) 2124 { 2125 struct usb_hub *hub; 2126 struct usb_port *port_dev; 2127 2128 if (udev->parent) { 2129 hub = usb_hub_to_struct_hub(udev->parent); 2130 2131 /* 2132 * The Link Layer Validation System Driver (lvstest) 2133 * has a test step to unbind the hub before running the 2134 * rest of the procedure. This triggers hub_disconnect 2135 * which will set the hub's maxchild to 0, further 2136 * resulting in usb_hub_to_struct_hub returning NULL. 2137 */ 2138 if (hub) { 2139 port_dev = hub->ports[udev->portnum - 1]; 2140 WRITE_ONCE(port_dev->state, udev->state); 2141 sysfs_notify_dirent(port_dev->state_kn); 2142 } 2143 } 2144 } 2145 2146 static void update_usb_device_state(struct usb_device *udev, 2147 enum usb_device_state new_state) 2148 { 2149 if (udev->state == USB_STATE_SUSPENDED && 2150 new_state != USB_STATE_SUSPENDED) 2151 udev->active_duration -= jiffies; 2152 else if (new_state == USB_STATE_SUSPENDED && 2153 udev->state != USB_STATE_SUSPENDED) 2154 udev->active_duration += jiffies; 2155 2156 udev->state = new_state; 2157 update_port_device_state(udev); 2158 trace_usb_set_device_state(udev); 2159 } 2160 2161 static void recursively_mark_NOTATTACHED(struct usb_device *udev) 2162 { 2163 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 2164 int i; 2165 2166 for (i = 0; i < udev->maxchild; ++i) { 2167 if (hub->ports[i]->child) 2168 recursively_mark_NOTATTACHED(hub->ports[i]->child); 2169 } 2170 update_usb_device_state(udev, USB_STATE_NOTATTACHED); 2171 } 2172 2173 /** 2174 * usb_set_device_state - change a device's current state (usbcore, hcds) 2175 * @udev: pointer to device whose state should be changed 2176 * @new_state: new state value to be stored 2177 * 2178 * udev->state is _not_ fully protected by the device lock. Although 2179 * most transitions are made only while holding the lock, the state can 2180 * can change to USB_STATE_NOTATTACHED at almost any time. This 2181 * is so that devices can be marked as disconnected as soon as possible, 2182 * without having to wait for any semaphores to be released. As a result, 2183 * all changes to any device's state must be protected by the 2184 * device_state_lock spinlock. 2185 * 2186 * Once a device has been added to the device tree, all changes to its state 2187 * should be made using this routine. The state should _not_ be set directly. 2188 * 2189 * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 2190 * Otherwise udev->state is set to new_state, and if new_state is 2191 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 2192 * to USB_STATE_NOTATTACHED. 2193 */ 2194 void usb_set_device_state(struct usb_device *udev, 2195 enum usb_device_state new_state) 2196 { 2197 unsigned long flags; 2198 int wakeup = -1; 2199 2200 spin_lock_irqsave(&device_state_lock, flags); 2201 if (udev->state == USB_STATE_NOTATTACHED) 2202 ; /* do nothing */ 2203 else if (new_state != USB_STATE_NOTATTACHED) { 2204 2205 /* root hub wakeup capabilities are managed out-of-band 2206 * and may involve silicon errata ... ignore them here. 2207 */ 2208 if (udev->parent) { 2209 if (udev->state == USB_STATE_SUSPENDED 2210 || new_state == USB_STATE_SUSPENDED) 2211 ; /* No change to wakeup settings */ 2212 else if (new_state == USB_STATE_CONFIGURED) 2213 wakeup = (udev->quirks & 2214 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 : 2215 udev->actconfig->desc.bmAttributes & 2216 USB_CONFIG_ATT_WAKEUP; 2217 else 2218 wakeup = 0; 2219 } 2220 update_usb_device_state(udev, new_state); 2221 } else 2222 recursively_mark_NOTATTACHED(udev); 2223 spin_unlock_irqrestore(&device_state_lock, flags); 2224 if (wakeup >= 0) 2225 device_set_wakeup_capable(&udev->dev, wakeup); 2226 } 2227 EXPORT_SYMBOL_GPL(usb_set_device_state); 2228 2229 /* 2230 * Choose a device number. 2231 * 2232 * Device numbers are used as filenames in usbfs. On USB-1.1 and 2233 * USB-2.0 buses they are also used as device addresses, however on 2234 * USB-3.0 buses the address is assigned by the controller hardware 2235 * and it usually is not the same as the device number. 2236 * 2237 * Devices connected under xHCI are not as simple. The host controller 2238 * supports virtualization, so the hardware assigns device addresses and 2239 * the HCD must setup data structures before issuing a set address 2240 * command to the hardware. 2241 */ 2242 static void choose_devnum(struct usb_device *udev) 2243 { 2244 int devnum; 2245 struct usb_bus *bus = udev->bus; 2246 2247 /* be safe when more hub events are proceed in parallel */ 2248 mutex_lock(&bus->devnum_next_mutex); 2249 2250 /* Try to allocate the next devnum beginning at bus->devnum_next. */ 2251 devnum = find_next_zero_bit(bus->devmap, 128, bus->devnum_next); 2252 if (devnum >= 128) 2253 devnum = find_next_zero_bit(bus->devmap, 128, 1); 2254 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); 2255 if (devnum < 128) { 2256 set_bit(devnum, bus->devmap); 2257 udev->devnum = devnum; 2258 } 2259 mutex_unlock(&bus->devnum_next_mutex); 2260 } 2261 2262 static void release_devnum(struct usb_device *udev) 2263 { 2264 if (udev->devnum > 0) { 2265 clear_bit(udev->devnum, udev->bus->devmap); 2266 udev->devnum = -1; 2267 } 2268 } 2269 2270 static void update_devnum(struct usb_device *udev, int devnum) 2271 { 2272 udev->devnum = devnum; 2273 if (!udev->devaddr) 2274 udev->devaddr = (u8)devnum; 2275 } 2276 2277 static void hub_free_dev(struct usb_device *udev) 2278 { 2279 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2280 2281 /* Root hubs aren't real devices, so don't free HCD resources */ 2282 if (hcd->driver->free_dev && udev->parent) 2283 hcd->driver->free_dev(hcd, udev); 2284 } 2285 2286 static void hub_disconnect_children(struct usb_device *udev) 2287 { 2288 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 2289 int i; 2290 2291 /* Free up all the children before we remove this device */ 2292 for (i = 0; i < udev->maxchild; i++) { 2293 if (hub->ports[i]->child) 2294 usb_disconnect(&hub->ports[i]->child); 2295 } 2296 } 2297 2298 /** 2299 * usb_disconnect - disconnect a device (usbcore-internal) 2300 * @pdev: pointer to device being disconnected 2301 * 2302 * Context: task context, might sleep 2303 * 2304 * Something got disconnected. Get rid of it and all of its children. 2305 * 2306 * If *pdev is a normal device then the parent hub must already be locked. 2307 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock, 2308 * which protects the set of root hubs as well as the list of buses. 2309 * 2310 * Only hub drivers (including virtual root hub drivers for host 2311 * controllers) should ever call this. 2312 * 2313 * This call is synchronous, and may not be used in an interrupt context. 2314 */ 2315 void usb_disconnect(struct usb_device **pdev) 2316 { 2317 struct usb_port *port_dev = NULL; 2318 struct usb_device *udev = *pdev; 2319 struct usb_hub *hub = NULL; 2320 int port1 = 1; 2321 2322 /* mark the device as inactive, so any further urb submissions for 2323 * this device (and any of its children) will fail immediately. 2324 * this quiesces everything except pending urbs. 2325 */ 2326 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 2327 dev_info(&udev->dev, "USB disconnect, device number %d\n", 2328 udev->devnum); 2329 2330 /* 2331 * Ensure that the pm runtime code knows that the USB device 2332 * is in the process of being disconnected. 2333 */ 2334 pm_runtime_barrier(&udev->dev); 2335 2336 usb_lock_device(udev); 2337 2338 hub_disconnect_children(udev); 2339 2340 /* deallocate hcd/hardware state ... nuking all pending urbs and 2341 * cleaning up all state associated with the current configuration 2342 * so that the hardware is now fully quiesced. 2343 */ 2344 dev_dbg(&udev->dev, "unregistering device\n"); 2345 usb_disable_device(udev, 0); 2346 usb_hcd_synchronize_unlinks(udev); 2347 2348 if (udev->parent) { 2349 port1 = udev->portnum; 2350 hub = usb_hub_to_struct_hub(udev->parent); 2351 port_dev = hub->ports[port1 - 1]; 2352 2353 sysfs_remove_link(&udev->dev.kobj, "port"); 2354 sysfs_remove_link(&port_dev->dev.kobj, "device"); 2355 2356 /* 2357 * As usb_port_runtime_resume() de-references udev, make 2358 * sure no resumes occur during removal 2359 */ 2360 if (!test_and_set_bit(port1, hub->child_usage_bits)) 2361 pm_runtime_get_sync(&port_dev->dev); 2362 2363 typec_deattach(port_dev->connector, &udev->dev); 2364 } 2365 2366 usb_remove_ep_devs(&udev->ep0); 2367 usb_unlock_device(udev); 2368 2369 if (udev->usb4_link) 2370 device_link_del(udev->usb4_link); 2371 2372 /* Unregister the device. The device driver is responsible 2373 * for de-configuring the device and invoking the remove-device 2374 * notifier chain (used by usbfs and possibly others). 2375 */ 2376 device_del(&udev->dev); 2377 2378 /* Free the device number and delete the parent's children[] 2379 * (or root_hub) pointer. 2380 */ 2381 release_devnum(udev); 2382 2383 /* Avoid races with recursively_mark_NOTATTACHED() */ 2384 spin_lock_irq(&device_state_lock); 2385 *pdev = NULL; 2386 spin_unlock_irq(&device_state_lock); 2387 2388 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) 2389 pm_runtime_put(&port_dev->dev); 2390 2391 hub_free_dev(udev); 2392 2393 put_device(&udev->dev); 2394 } 2395 2396 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 2397 static void show_string(struct usb_device *udev, char *id, char *string) 2398 { 2399 if (!string) 2400 return; 2401 dev_info(&udev->dev, "%s: %s\n", id, string); 2402 } 2403 2404 static void announce_device_ids(struct usb_device *udev) 2405 { 2406 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice); 2407 2408 dev_info(&udev->dev, 2409 "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n", 2410 le16_to_cpu(udev->descriptor.idVendor), 2411 le16_to_cpu(udev->descriptor.idProduct), 2412 bcdDevice >> 8, bcdDevice & 0xff); 2413 } 2414 2415 static void announce_device_strings(struct usb_device *udev) 2416 { 2417 dev_info(&udev->dev, 2418 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 2419 udev->descriptor.iManufacturer, 2420 udev->descriptor.iProduct, 2421 udev->descriptor.iSerialNumber); 2422 show_string(udev, "Product", udev->product); 2423 show_string(udev, "Manufacturer", udev->manufacturer); 2424 show_string(udev, "SerialNumber", udev->serial); 2425 } 2426 #else 2427 static inline void announce_device_ids(struct usb_device *udev) { } 2428 static inline void announce_device_strings(struct usb_device *udev) { } 2429 #endif 2430 2431 2432 /** 2433 * usb_enumerate_device_otg - FIXME (usbcore-internal) 2434 * @udev: newly addressed device (in ADDRESS state) 2435 * 2436 * Finish enumeration for On-The-Go devices 2437 * 2438 * Return: 0 if successful. A negative error code otherwise. 2439 */ 2440 static int usb_enumerate_device_otg(struct usb_device *udev) 2441 { 2442 int err = 0; 2443 2444 #ifdef CONFIG_USB_OTG 2445 /* 2446 * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 2447 * to wake us after we've powered off VBUS; and HNP, switching roles 2448 * "host" to "peripheral". The OTG descriptor helps figure this out. 2449 */ 2450 if (!udev->bus->is_b_host 2451 && udev->config 2452 && udev->parent == udev->bus->root_hub) { 2453 struct usb_otg_descriptor *desc = NULL; 2454 struct usb_bus *bus = udev->bus; 2455 unsigned port1 = udev->portnum; 2456 2457 /* descriptor may appear anywhere in config */ 2458 err = __usb_get_extra_descriptor(udev->rawdescriptors[0], 2459 le16_to_cpu(udev->config[0].desc.wTotalLength), 2460 USB_DT_OTG, (void **) &desc, sizeof(*desc)); 2461 if (err || !(desc->bmAttributes & USB_OTG_HNP)) 2462 return 0; 2463 2464 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", 2465 (port1 == bus->otg_port) ? "" : "non-"); 2466 2467 /* enable HNP before suspend, it's simpler */ 2468 if (port1 == bus->otg_port) { 2469 bus->b_hnp_enable = 1; 2470 err = usb_control_msg(udev, 2471 usb_sndctrlpipe(udev, 0), 2472 USB_REQ_SET_FEATURE, 0, 2473 USB_DEVICE_B_HNP_ENABLE, 2474 0, NULL, 0, 2475 USB_CTRL_SET_TIMEOUT); 2476 if (err < 0) { 2477 /* 2478 * OTG MESSAGE: report errors here, 2479 * customize to match your product. 2480 */ 2481 dev_err(&udev->dev, "can't set HNP mode: %d\n", 2482 err); 2483 bus->b_hnp_enable = 0; 2484 } 2485 } else if (desc->bLength == sizeof 2486 (struct usb_otg_descriptor)) { 2487 /* 2488 * We are operating on a legacy OTP device 2489 * These should be told that they are operating 2490 * on the wrong port if we have another port that does 2491 * support HNP 2492 */ 2493 if (bus->otg_port != 0) { 2494 /* Set a_alt_hnp_support for legacy otg device */ 2495 err = usb_control_msg(udev, 2496 usb_sndctrlpipe(udev, 0), 2497 USB_REQ_SET_FEATURE, 0, 2498 USB_DEVICE_A_ALT_HNP_SUPPORT, 2499 0, NULL, 0, 2500 USB_CTRL_SET_TIMEOUT); 2501 if (err < 0) 2502 dev_err(&udev->dev, 2503 "set a_alt_hnp_support failed: %d\n", 2504 err); 2505 } 2506 } 2507 } 2508 #endif 2509 return err; 2510 } 2511 2512 2513 /** 2514 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) 2515 * @udev: newly addressed device (in ADDRESS state) 2516 * 2517 * This is only called by usb_new_device() -- all comments that apply there 2518 * apply here wrt to environment. 2519 * 2520 * If the device is WUSB and not authorized, we don't attempt to read 2521 * the string descriptors, as they will be errored out by the device 2522 * until it has been authorized. 2523 * 2524 * Return: 0 if successful. A negative error code otherwise. 2525 */ 2526 static int usb_enumerate_device(struct usb_device *udev) 2527 { 2528 int err; 2529 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2530 2531 if (udev->config == NULL) { 2532 err = usb_get_configuration(udev); 2533 if (err < 0) { 2534 if (err != -ENODEV) 2535 dev_err(&udev->dev, "can't read configurations, error %d\n", 2536 err); 2537 return err; 2538 } 2539 } 2540 2541 /* read the standard strings and cache them if present */ 2542 udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 2543 udev->manufacturer = usb_cache_string(udev, 2544 udev->descriptor.iManufacturer); 2545 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 2546 2547 err = usb_enumerate_device_otg(udev); 2548 if (err < 0) 2549 return err; 2550 2551 if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support && 2552 !is_targeted(udev)) { 2553 /* Maybe it can talk to us, though we can't talk to it. 2554 * (Includes HNP test device.) 2555 */ 2556 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable 2557 || udev->bus->is_b_host)) { 2558 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND); 2559 if (err < 0) 2560 dev_dbg(&udev->dev, "HNP fail, %d\n", err); 2561 } 2562 return -ENOTSUPP; 2563 } 2564 2565 usb_detect_interface_quirks(udev); 2566 2567 return 0; 2568 } 2569 2570 static void set_usb_port_removable(struct usb_device *udev) 2571 { 2572 struct usb_device *hdev = udev->parent; 2573 struct usb_hub *hub; 2574 u8 port = udev->portnum; 2575 u16 wHubCharacteristics; 2576 bool removable = true; 2577 2578 dev_set_removable(&udev->dev, DEVICE_REMOVABLE_UNKNOWN); 2579 2580 if (!hdev) 2581 return; 2582 2583 hub = usb_hub_to_struct_hub(udev->parent); 2584 2585 /* 2586 * If the platform firmware has provided information about a port, 2587 * use that to determine whether it's removable. 2588 */ 2589 switch (hub->ports[udev->portnum - 1]->connect_type) { 2590 case USB_PORT_CONNECT_TYPE_HOT_PLUG: 2591 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); 2592 return; 2593 case USB_PORT_CONNECT_TYPE_HARD_WIRED: 2594 case USB_PORT_NOT_USED: 2595 dev_set_removable(&udev->dev, DEVICE_FIXED); 2596 return; 2597 default: 2598 break; 2599 } 2600 2601 /* 2602 * Otherwise, check whether the hub knows whether a port is removable 2603 * or not 2604 */ 2605 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2606 2607 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 2608 return; 2609 2610 if (hub_is_superspeed(hdev)) { 2611 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) 2612 & (1 << port)) 2613 removable = false; 2614 } else { 2615 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 2616 removable = false; 2617 } 2618 2619 if (removable) 2620 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); 2621 else 2622 dev_set_removable(&udev->dev, DEVICE_FIXED); 2623 2624 } 2625 2626 /** 2627 * usb_new_device - perform initial device setup (usbcore-internal) 2628 * @udev: newly addressed device (in ADDRESS state) 2629 * 2630 * This is called with devices which have been detected but not fully 2631 * enumerated. The device descriptor is available, but not descriptors 2632 * for any device configuration. The caller must have locked either 2633 * the parent hub (if udev is a normal device) or else the 2634 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to 2635 * udev has already been installed, but udev is not yet visible through 2636 * sysfs or other filesystem code. 2637 * 2638 * This call is synchronous, and may not be used in an interrupt context. 2639 * 2640 * Only the hub driver or root-hub registrar should ever call this. 2641 * 2642 * Return: Whether the device is configured properly or not. Zero if the 2643 * interface was registered with the driver core; else a negative errno 2644 * value. 2645 * 2646 */ 2647 int usb_new_device(struct usb_device *udev) 2648 { 2649 int err; 2650 2651 if (udev->parent) { 2652 /* Initialize non-root-hub device wakeup to disabled; 2653 * device (un)configuration controls wakeup capable 2654 * sysfs power/wakeup controls wakeup enabled/disabled 2655 */ 2656 device_init_wakeup(&udev->dev, 0); 2657 } 2658 2659 /* Announce the device identity */ 2660 announce_device_ids(udev); 2661 2662 /* Tell the runtime-PM framework the device is active */ 2663 pm_runtime_set_active(&udev->dev); 2664 pm_runtime_get_noresume(&udev->dev); 2665 pm_runtime_use_autosuspend(&udev->dev); 2666 pm_runtime_enable(&udev->dev); 2667 2668 /* By default, forbid autosuspend for all devices. It will be 2669 * allowed for hubs during binding. 2670 */ 2671 usb_disable_autosuspend(udev); 2672 2673 err = usb_enumerate_device(udev); /* Read descriptors */ 2674 if (err < 0) 2675 goto fail; 2676 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 2677 udev->devnum, udev->bus->busnum, 2678 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 2679 /* export the usbdev device-node for libusb */ 2680 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 2681 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 2682 2683 /* Announce the device's product, manufacturer and serial number */ 2684 announce_device_strings(udev); 2685 2686 if (udev->serial) 2687 add_device_randomness(udev->serial, strlen(udev->serial)); 2688 if (udev->product) 2689 add_device_randomness(udev->product, strlen(udev->product)); 2690 if (udev->manufacturer) 2691 add_device_randomness(udev->manufacturer, 2692 strlen(udev->manufacturer)); 2693 2694 device_enable_async_suspend(&udev->dev); 2695 2696 /* check whether the hub or firmware marks this port as non-removable */ 2697 set_usb_port_removable(udev); 2698 2699 /* Register the device. The device driver is responsible 2700 * for configuring the device and invoking the add-device 2701 * notifier chain (used by usbfs and possibly others). 2702 */ 2703 err = device_add(&udev->dev); 2704 if (err) { 2705 dev_err(&udev->dev, "can't device_add, error %d\n", err); 2706 goto fail; 2707 } 2708 2709 /* Create link files between child device and usb port device. */ 2710 if (udev->parent) { 2711 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 2712 int port1 = udev->portnum; 2713 struct usb_port *port_dev = hub->ports[port1 - 1]; 2714 2715 err = sysfs_create_link(&udev->dev.kobj, 2716 &port_dev->dev.kobj, "port"); 2717 if (err) 2718 goto out_del_dev; 2719 2720 err = sysfs_create_link(&port_dev->dev.kobj, 2721 &udev->dev.kobj, "device"); 2722 if (err) { 2723 sysfs_remove_link(&udev->dev.kobj, "port"); 2724 goto out_del_dev; 2725 } 2726 2727 if (!test_and_set_bit(port1, hub->child_usage_bits)) 2728 pm_runtime_get_sync(&port_dev->dev); 2729 2730 typec_attach(port_dev->connector, &udev->dev); 2731 } 2732 2733 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2734 usb_mark_last_busy(udev); 2735 pm_runtime_put_sync_autosuspend(&udev->dev); 2736 return err; 2737 2738 out_del_dev: 2739 device_del(&udev->dev); 2740 fail: 2741 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 2742 pm_runtime_disable(&udev->dev); 2743 pm_runtime_set_suspended(&udev->dev); 2744 return err; 2745 } 2746 2747 2748 /** 2749 * usb_deauthorize_device - deauthorize a device (usbcore-internal) 2750 * @usb_dev: USB device 2751 * 2752 * Move the USB device to a very basic state where interfaces are disabled 2753 * and the device is in fact unconfigured and unusable. 2754 * 2755 * We share a lock (that we have) with device_del(), so we need to 2756 * defer its call. 2757 * 2758 * Return: 0. 2759 */ 2760 int usb_deauthorize_device(struct usb_device *usb_dev) 2761 { 2762 usb_lock_device(usb_dev); 2763 if (usb_dev->authorized == 0) 2764 goto out_unauthorized; 2765 2766 usb_dev->authorized = 0; 2767 usb_set_configuration(usb_dev, -1); 2768 2769 out_unauthorized: 2770 usb_unlock_device(usb_dev); 2771 return 0; 2772 } 2773 2774 2775 int usb_authorize_device(struct usb_device *usb_dev) 2776 { 2777 int result = 0, c; 2778 2779 usb_lock_device(usb_dev); 2780 if (usb_dev->authorized == 1) 2781 goto out_authorized; 2782 2783 result = usb_autoresume_device(usb_dev); 2784 if (result < 0) { 2785 dev_err(&usb_dev->dev, 2786 "can't autoresume for authorization: %d\n", result); 2787 goto error_autoresume; 2788 } 2789 2790 usb_dev->authorized = 1; 2791 /* Choose and set the configuration. This registers the interfaces 2792 * with the driver core and lets interface drivers bind to them. 2793 */ 2794 c = usb_choose_configuration(usb_dev); 2795 if (c >= 0) { 2796 result = usb_set_configuration(usb_dev, c); 2797 if (result) { 2798 dev_err(&usb_dev->dev, 2799 "can't set config #%d, error %d\n", c, result); 2800 /* This need not be fatal. The user can try to 2801 * set other configurations. */ 2802 } 2803 } 2804 dev_info(&usb_dev->dev, "authorized to connect\n"); 2805 2806 usb_autosuspend_device(usb_dev); 2807 error_autoresume: 2808 out_authorized: 2809 usb_unlock_device(usb_dev); /* complements locktree */ 2810 return result; 2811 } 2812 2813 /** 2814 * get_port_ssp_rate - Match the extended port status to SSP rate 2815 * @hdev: The hub device 2816 * @ext_portstatus: extended port status 2817 * 2818 * Match the extended port status speed id to the SuperSpeed Plus sublink speed 2819 * capability attributes. Base on the number of connected lanes and speed, 2820 * return the corresponding enum usb_ssp_rate. 2821 */ 2822 static enum usb_ssp_rate get_port_ssp_rate(struct usb_device *hdev, 2823 u32 ext_portstatus) 2824 { 2825 struct usb_ssp_cap_descriptor *ssp_cap; 2826 u32 attr; 2827 u8 speed_id; 2828 u8 ssac; 2829 u8 lanes; 2830 int i; 2831 2832 if (!hdev->bos) 2833 goto out; 2834 2835 ssp_cap = hdev->bos->ssp_cap; 2836 if (!ssp_cap) 2837 goto out; 2838 2839 speed_id = ext_portstatus & USB_EXT_PORT_STAT_RX_SPEED_ID; 2840 lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 2841 2842 ssac = le32_to_cpu(ssp_cap->bmAttributes) & 2843 USB_SSP_SUBLINK_SPEED_ATTRIBS; 2844 2845 for (i = 0; i <= ssac; i++) { 2846 u8 ssid; 2847 2848 attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); 2849 ssid = FIELD_GET(USB_SSP_SUBLINK_SPEED_SSID, attr); 2850 if (speed_id == ssid) { 2851 u16 mantissa; 2852 u8 lse; 2853 u8 type; 2854 2855 /* 2856 * Note: currently asymmetric lane types are only 2857 * applicable for SSIC operate in SuperSpeed protocol 2858 */ 2859 type = FIELD_GET(USB_SSP_SUBLINK_SPEED_ST, attr); 2860 if (type == USB_SSP_SUBLINK_SPEED_ST_ASYM_RX || 2861 type == USB_SSP_SUBLINK_SPEED_ST_ASYM_TX) 2862 goto out; 2863 2864 if (FIELD_GET(USB_SSP_SUBLINK_SPEED_LP, attr) != 2865 USB_SSP_SUBLINK_SPEED_LP_SSP) 2866 goto out; 2867 2868 lse = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSE, attr); 2869 mantissa = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSM, attr); 2870 2871 /* Convert to Gbps */ 2872 for (; lse < USB_SSP_SUBLINK_SPEED_LSE_GBPS; lse++) 2873 mantissa /= 1000; 2874 2875 if (mantissa >= 10 && lanes == 1) 2876 return USB_SSP_GEN_2x1; 2877 2878 if (mantissa >= 10 && lanes == 2) 2879 return USB_SSP_GEN_2x2; 2880 2881 if (mantissa >= 5 && lanes == 2) 2882 return USB_SSP_GEN_1x2; 2883 2884 goto out; 2885 } 2886 } 2887 2888 out: 2889 return USB_SSP_GEN_UNKNOWN; 2890 } 2891 2892 #ifdef CONFIG_USB_FEW_INIT_RETRIES 2893 #define PORT_RESET_TRIES 2 2894 #define SET_ADDRESS_TRIES 1 2895 #define GET_DESCRIPTOR_TRIES 1 2896 #define GET_MAXPACKET0_TRIES 1 2897 #define PORT_INIT_TRIES 4 2898 2899 #else 2900 #define PORT_RESET_TRIES 5 2901 #define SET_ADDRESS_TRIES 2 2902 #define GET_DESCRIPTOR_TRIES 2 2903 #define GET_MAXPACKET0_TRIES 3 2904 #define PORT_INIT_TRIES 4 2905 #endif /* CONFIG_USB_FEW_INIT_RETRIES */ 2906 2907 #define DETECT_DISCONNECT_TRIES 5 2908 2909 #define HUB_ROOT_RESET_TIME 60 /* times are in msec */ 2910 #define HUB_SHORT_RESET_TIME 10 2911 #define HUB_BH_RESET_TIME 50 2912 #define HUB_LONG_RESET_TIME 200 2913 #define HUB_RESET_TIMEOUT 800 2914 2915 static bool use_new_scheme(struct usb_device *udev, int retry, 2916 struct usb_port *port_dev) 2917 { 2918 int old_scheme_first_port = 2919 (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || 2920 old_scheme_first; 2921 2922 /* 2923 * "New scheme" enumeration causes an extra state transition to be 2924 * exposed to an xhci host and causes USB3 devices to receive control 2925 * commands in the default state. This has been seen to cause 2926 * enumeration failures, so disable this enumeration scheme for USB3 2927 * devices. 2928 */ 2929 if (udev->speed >= USB_SPEED_SUPER) 2930 return false; 2931 2932 /* 2933 * If use_both_schemes is set, use the first scheme (whichever 2934 * it is) for the larger half of the retries, then use the other 2935 * scheme. Otherwise, use the first scheme for all the retries. 2936 */ 2937 if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2) 2938 return old_scheme_first_port; /* Second half */ 2939 return !old_scheme_first_port; /* First half or all */ 2940 } 2941 2942 /* Is a USB 3.0 port in the Inactive or Compliance Mode state? 2943 * Port warm reset is required to recover 2944 */ 2945 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 2946 u16 portstatus) 2947 { 2948 u16 link_state; 2949 2950 if (!hub_is_superspeed(hub->hdev)) 2951 return false; 2952 2953 if (test_bit(port1, hub->warm_reset_bits)) 2954 return true; 2955 2956 link_state = portstatus & USB_PORT_STAT_LINK_STATE; 2957 return link_state == USB_SS_PORT_LS_SS_INACTIVE 2958 || link_state == USB_SS_PORT_LS_COMP_MOD; 2959 } 2960 2961 static int hub_port_wait_reset(struct usb_hub *hub, int port1, 2962 struct usb_device *udev, unsigned int delay, bool warm) 2963 { 2964 int delay_time, ret; 2965 u16 portstatus; 2966 u16 portchange; 2967 u32 ext_portstatus = 0; 2968 2969 for (delay_time = 0; 2970 delay_time < HUB_RESET_TIMEOUT; 2971 delay_time += delay) { 2972 /* wait to give the device a chance to reset */ 2973 msleep(delay); 2974 2975 /* read and decode port status */ 2976 if (hub_is_superspeedplus(hub->hdev)) 2977 ret = hub_ext_port_status(hub, port1, 2978 HUB_EXT_PORT_STATUS, 2979 &portstatus, &portchange, 2980 &ext_portstatus); 2981 else 2982 ret = usb_hub_port_status(hub, port1, &portstatus, 2983 &portchange); 2984 if (ret < 0) 2985 return ret; 2986 2987 /* 2988 * The port state is unknown until the reset completes. 2989 * 2990 * On top of that, some chips may require additional time 2991 * to re-establish a connection after the reset is complete, 2992 * so also wait for the connection to be re-established. 2993 */ 2994 if (!(portstatus & USB_PORT_STAT_RESET) && 2995 (portstatus & USB_PORT_STAT_CONNECTION)) 2996 break; 2997 2998 /* switch to the long delay after two short delay failures */ 2999 if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 3000 delay = HUB_LONG_RESET_TIME; 3001 3002 dev_dbg(&hub->ports[port1 - 1]->dev, 3003 "not %sreset yet, waiting %dms\n", 3004 warm ? "warm " : "", delay); 3005 } 3006 3007 if ((portstatus & USB_PORT_STAT_RESET)) 3008 return -EBUSY; 3009 3010 if (hub_port_warm_reset_required(hub, port1, portstatus)) 3011 return -ENOTCONN; 3012 3013 /* Device went away? */ 3014 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 3015 return -ENOTCONN; 3016 3017 /* Retry if connect change is set but status is still connected. 3018 * A USB 3.0 connection may bounce if multiple warm resets were issued, 3019 * but the device may have successfully re-connected. Ignore it. 3020 */ 3021 if (!hub_is_superspeed(hub->hdev) && 3022 (portchange & USB_PORT_STAT_C_CONNECTION)) { 3023 usb_clear_port_feature(hub->hdev, port1, 3024 USB_PORT_FEAT_C_CONNECTION); 3025 return -EAGAIN; 3026 } 3027 3028 if (!(portstatus & USB_PORT_STAT_ENABLE)) 3029 return -EBUSY; 3030 3031 if (!udev) 3032 return 0; 3033 3034 if (hub_is_superspeedplus(hub->hdev)) { 3035 /* extended portstatus Rx and Tx lane count are zero based */ 3036 udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 3037 udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1; 3038 udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus); 3039 } else { 3040 udev->rx_lanes = 1; 3041 udev->tx_lanes = 1; 3042 udev->ssp_rate = USB_SSP_GEN_UNKNOWN; 3043 } 3044 if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN) 3045 udev->speed = USB_SPEED_SUPER_PLUS; 3046 else if (hub_is_superspeed(hub->hdev)) 3047 udev->speed = USB_SPEED_SUPER; 3048 else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 3049 udev->speed = USB_SPEED_HIGH; 3050 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 3051 udev->speed = USB_SPEED_LOW; 3052 else 3053 udev->speed = USB_SPEED_FULL; 3054 return 0; 3055 } 3056 3057 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 3058 static int hub_port_reset(struct usb_hub *hub, int port1, 3059 struct usb_device *udev, unsigned int delay, bool warm) 3060 { 3061 int i, status; 3062 u16 portchange, portstatus; 3063 struct usb_port *port_dev = hub->ports[port1 - 1]; 3064 int reset_recovery_time; 3065 3066 if (!hub_is_superspeed(hub->hdev)) { 3067 if (warm) { 3068 dev_err(hub->intfdev, "only USB3 hub support " 3069 "warm reset\n"); 3070 return -EINVAL; 3071 } 3072 /* Block EHCI CF initialization during the port reset. 3073 * Some companion controllers don't like it when they mix. 3074 */ 3075 down_read(&ehci_cf_port_reset_rwsem); 3076 } else if (!warm) { 3077 /* 3078 * If the caller hasn't explicitly requested a warm reset, 3079 * double check and see if one is needed. 3080 */ 3081 if (usb_hub_port_status(hub, port1, &portstatus, 3082 &portchange) == 0) 3083 if (hub_port_warm_reset_required(hub, port1, 3084 portstatus)) 3085 warm = true; 3086 } 3087 clear_bit(port1, hub->warm_reset_bits); 3088 3089 /* Reset the port */ 3090 for (i = 0; i < PORT_RESET_TRIES; i++) { 3091 status = set_port_feature(hub->hdev, port1, (warm ? 3092 USB_PORT_FEAT_BH_PORT_RESET : 3093 USB_PORT_FEAT_RESET)); 3094 if (status == -ENODEV) { 3095 ; /* The hub is gone */ 3096 } else if (status) { 3097 dev_err(&port_dev->dev, 3098 "cannot %sreset (err = %d)\n", 3099 warm ? "warm " : "", status); 3100 } else { 3101 status = hub_port_wait_reset(hub, port1, udev, delay, 3102 warm); 3103 if (status && status != -ENOTCONN && status != -ENODEV) 3104 dev_dbg(hub->intfdev, 3105 "port_wait_reset: err = %d\n", 3106 status); 3107 } 3108 3109 /* 3110 * Check for disconnect or reset, and bail out after several 3111 * reset attempts to avoid warm reset loop. 3112 */ 3113 if (status == 0 || status == -ENOTCONN || status == -ENODEV || 3114 (status == -EBUSY && i == PORT_RESET_TRIES - 1)) { 3115 usb_clear_port_feature(hub->hdev, port1, 3116 USB_PORT_FEAT_C_RESET); 3117 3118 if (!hub_is_superspeed(hub->hdev)) 3119 goto done; 3120 3121 usb_clear_port_feature(hub->hdev, port1, 3122 USB_PORT_FEAT_C_BH_PORT_RESET); 3123 usb_clear_port_feature(hub->hdev, port1, 3124 USB_PORT_FEAT_C_PORT_LINK_STATE); 3125 3126 if (udev) 3127 usb_clear_port_feature(hub->hdev, port1, 3128 USB_PORT_FEAT_C_CONNECTION); 3129 3130 /* 3131 * If a USB 3.0 device migrates from reset to an error 3132 * state, re-issue the warm reset. 3133 */ 3134 if (usb_hub_port_status(hub, port1, 3135 &portstatus, &portchange) < 0) 3136 goto done; 3137 3138 if (!hub_port_warm_reset_required(hub, port1, 3139 portstatus)) 3140 goto done; 3141 3142 /* 3143 * If the port is in SS.Inactive or Compliance Mode, the 3144 * hot or warm reset failed. Try another warm reset. 3145 */ 3146 if (!warm) { 3147 dev_dbg(&port_dev->dev, 3148 "hot reset failed, warm reset\n"); 3149 warm = true; 3150 } 3151 } 3152 3153 dev_dbg(&port_dev->dev, 3154 "not enabled, trying %sreset again...\n", 3155 warm ? "warm " : ""); 3156 delay = HUB_LONG_RESET_TIME; 3157 } 3158 3159 dev_err_ratelimited(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); 3160 3161 done: 3162 if (status == 0) { 3163 if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM) 3164 usleep_range(10000, 12000); 3165 else { 3166 /* TRSTRCY = 10 ms; plus some extra */ 3167 reset_recovery_time = 10 + 40; 3168 3169 /* Hub needs extra delay after resetting its port. */ 3170 if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET) 3171 reset_recovery_time += 100; 3172 3173 msleep(reset_recovery_time); 3174 } 3175 3176 if (udev) { 3177 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3178 3179 update_devnum(udev, 0); 3180 /* The xHC may think the device is already reset, 3181 * so ignore the status. 3182 */ 3183 if (hcd->driver->reset_device) 3184 hcd->driver->reset_device(hcd, udev); 3185 3186 usb_set_device_state(udev, USB_STATE_DEFAULT); 3187 } 3188 } else { 3189 if (udev) 3190 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 3191 } 3192 3193 if (!hub_is_superspeed(hub->hdev)) 3194 up_read(&ehci_cf_port_reset_rwsem); 3195 3196 return status; 3197 } 3198 3199 /* 3200 * hub_port_stop_enumerate - stop USB enumeration or ignore port events 3201 * @hub: target hub 3202 * @port1: port num of the port 3203 * @retries: port retries number of hub_port_init() 3204 * 3205 * Return: 3206 * true: ignore port actions/events or give up connection attempts. 3207 * false: keep original behavior. 3208 * 3209 * This function will be based on retries to check whether the port which is 3210 * marked with early_stop attribute would stop enumeration or ignore events. 3211 * 3212 * Note: 3213 * This function didn't change anything if early_stop is not set, and it will 3214 * prevent all connection attempts when early_stop is set and the attempts of 3215 * the port are more than 1. 3216 */ 3217 static bool hub_port_stop_enumerate(struct usb_hub *hub, int port1, int retries) 3218 { 3219 struct usb_port *port_dev = hub->ports[port1 - 1]; 3220 3221 if (port_dev->early_stop) { 3222 if (port_dev->ignore_event) 3223 return true; 3224 3225 /* 3226 * We want unsuccessful attempts to fail quickly. 3227 * Since some devices may need one failure during 3228 * port initialization, we allow two tries but no 3229 * more. 3230 */ 3231 if (retries < 2) 3232 return false; 3233 3234 port_dev->ignore_event = 1; 3235 } else 3236 port_dev->ignore_event = 0; 3237 3238 return port_dev->ignore_event; 3239 } 3240 3241 /* Check if a port is power on */ 3242 int usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus) 3243 { 3244 int ret = 0; 3245 3246 if (hub_is_superspeed(hub->hdev)) { 3247 if (portstatus & USB_SS_PORT_STAT_POWER) 3248 ret = 1; 3249 } else { 3250 if (portstatus & USB_PORT_STAT_POWER) 3251 ret = 1; 3252 } 3253 3254 return ret; 3255 } 3256 3257 static void usb_lock_port(struct usb_port *port_dev) 3258 __acquires(&port_dev->status_lock) 3259 { 3260 mutex_lock(&port_dev->status_lock); 3261 __acquire(&port_dev->status_lock); 3262 } 3263 3264 static void usb_unlock_port(struct usb_port *port_dev) 3265 __releases(&port_dev->status_lock) 3266 { 3267 mutex_unlock(&port_dev->status_lock); 3268 __release(&port_dev->status_lock); 3269 } 3270 3271 #ifdef CONFIG_PM 3272 3273 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 3274 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 3275 { 3276 int ret = 0; 3277 3278 if (hub_is_superspeed(hub->hdev)) { 3279 if ((portstatus & USB_PORT_STAT_LINK_STATE) 3280 == USB_SS_PORT_LS_U3) 3281 ret = 1; 3282 } else { 3283 if (portstatus & USB_PORT_STAT_SUSPEND) 3284 ret = 1; 3285 } 3286 3287 return ret; 3288 } 3289 3290 /* Determine whether the device on a port is ready for a normal resume, 3291 * is ready for a reset-resume, or should be disconnected. 3292 */ 3293 static int check_port_resume_type(struct usb_device *udev, 3294 struct usb_hub *hub, int port1, 3295 int status, u16 portchange, u16 portstatus) 3296 { 3297 struct usb_port *port_dev = hub->ports[port1 - 1]; 3298 int retries = 3; 3299 3300 retry: 3301 /* Is a warm reset needed to recover the connection? */ 3302 if (status == 0 && udev->reset_resume 3303 && hub_port_warm_reset_required(hub, port1, portstatus)) { 3304 /* pass */; 3305 } 3306 /* Is the device still present? */ 3307 else if (status || port_is_suspended(hub, portstatus) || 3308 !usb_port_is_power_on(hub, portstatus)) { 3309 if (status >= 0) 3310 status = -ENODEV; 3311 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 3312 if (retries--) { 3313 usleep_range(200, 300); 3314 status = usb_hub_port_status(hub, port1, &portstatus, 3315 &portchange); 3316 goto retry; 3317 } 3318 status = -ENODEV; 3319 } 3320 3321 /* Can't do a normal resume if the port isn't enabled, 3322 * so try a reset-resume instead. 3323 */ 3324 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 3325 if (udev->persist_enabled) 3326 udev->reset_resume = 1; 3327 else 3328 status = -ENODEV; 3329 } 3330 3331 if (status) { 3332 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", 3333 portchange, portstatus, status); 3334 } else if (udev->reset_resume) { 3335 3336 /* Late port handoff can set status-change bits */ 3337 if (portchange & USB_PORT_STAT_C_CONNECTION) 3338 usb_clear_port_feature(hub->hdev, port1, 3339 USB_PORT_FEAT_C_CONNECTION); 3340 if (portchange & USB_PORT_STAT_C_ENABLE) 3341 usb_clear_port_feature(hub->hdev, port1, 3342 USB_PORT_FEAT_C_ENABLE); 3343 3344 /* 3345 * Whatever made this reset-resume necessary may have 3346 * turned on the port1 bit in hub->change_bits. But after 3347 * a successful reset-resume we want the bit to be clear; 3348 * if it was on it would indicate that something happened 3349 * following the reset-resume. 3350 */ 3351 clear_bit(port1, hub->change_bits); 3352 } 3353 3354 return status; 3355 } 3356 3357 int usb_disable_ltm(struct usb_device *udev) 3358 { 3359 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3360 3361 /* Check if the roothub and device supports LTM. */ 3362 if (!usb_device_supports_ltm(hcd->self.root_hub) || 3363 !usb_device_supports_ltm(udev)) 3364 return 0; 3365 3366 /* Clear Feature LTM Enable can only be sent if the device is 3367 * configured. 3368 */ 3369 if (!udev->actconfig) 3370 return 0; 3371 3372 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3373 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 3374 USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 3375 USB_CTRL_SET_TIMEOUT); 3376 } 3377 EXPORT_SYMBOL_GPL(usb_disable_ltm); 3378 3379 void usb_enable_ltm(struct usb_device *udev) 3380 { 3381 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3382 3383 /* Check if the roothub and device supports LTM. */ 3384 if (!usb_device_supports_ltm(hcd->self.root_hub) || 3385 !usb_device_supports_ltm(udev)) 3386 return; 3387 3388 /* Set Feature LTM Enable can only be sent if the device is 3389 * configured. 3390 */ 3391 if (!udev->actconfig) 3392 return; 3393 3394 usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3395 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 3396 USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 3397 USB_CTRL_SET_TIMEOUT); 3398 } 3399 EXPORT_SYMBOL_GPL(usb_enable_ltm); 3400 3401 /* 3402 * usb_enable_remote_wakeup - enable remote wakeup for a device 3403 * @udev: target device 3404 * 3405 * For USB-2 devices: Set the device's remote wakeup feature. 3406 * 3407 * For USB-3 devices: Assume there's only one function on the device and 3408 * enable remote wake for the first interface. FIXME if the interface 3409 * association descriptor shows there's more than one function. 3410 */ 3411 static int usb_enable_remote_wakeup(struct usb_device *udev) 3412 { 3413 if (udev->speed < USB_SPEED_SUPER) 3414 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3415 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 3416 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 3417 USB_CTRL_SET_TIMEOUT); 3418 else 3419 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3420 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 3421 USB_INTRF_FUNC_SUSPEND, 3422 USB_INTRF_FUNC_SUSPEND_RW | 3423 USB_INTRF_FUNC_SUSPEND_LP, 3424 NULL, 0, USB_CTRL_SET_TIMEOUT); 3425 } 3426 3427 /* 3428 * usb_disable_remote_wakeup - disable remote wakeup for a device 3429 * @udev: target device 3430 * 3431 * For USB-2 devices: Clear the device's remote wakeup feature. 3432 * 3433 * For USB-3 devices: Assume there's only one function on the device and 3434 * disable remote wake for the first interface. FIXME if the interface 3435 * association descriptor shows there's more than one function. 3436 */ 3437 static int usb_disable_remote_wakeup(struct usb_device *udev) 3438 { 3439 if (udev->speed < USB_SPEED_SUPER) 3440 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3441 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 3442 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 3443 USB_CTRL_SET_TIMEOUT); 3444 else 3445 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3446 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 3447 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, 3448 USB_CTRL_SET_TIMEOUT); 3449 } 3450 3451 /* Count of wakeup-enabled devices at or below udev */ 3452 unsigned usb_wakeup_enabled_descendants(struct usb_device *udev) 3453 { 3454 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 3455 3456 return udev->do_remote_wakeup + 3457 (hub ? hub->wakeup_enabled_descendants : 0); 3458 } 3459 EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants); 3460 3461 /* 3462 * usb_port_suspend - suspend a usb device's upstream port 3463 * @udev: device that's no longer in active use, not a root hub 3464 * Context: must be able to sleep; device not locked; pm locks held 3465 * 3466 * Suspends a USB device that isn't in active use, conserving power. 3467 * Devices may wake out of a suspend, if anything important happens, 3468 * using the remote wakeup mechanism. They may also be taken out of 3469 * suspend by the host, using usb_port_resume(). It's also routine 3470 * to disconnect devices while they are suspended. 3471 * 3472 * This only affects the USB hardware for a device; its interfaces 3473 * (and, for hubs, child devices) must already have been suspended. 3474 * 3475 * Selective port suspend reduces power; most suspended devices draw 3476 * less than 500 uA. It's also used in OTG, along with remote wakeup. 3477 * All devices below the suspended port are also suspended. 3478 * 3479 * Devices leave suspend state when the host wakes them up. Some devices 3480 * also support "remote wakeup", where the device can activate the USB 3481 * tree above them to deliver data, such as a keypress or packet. In 3482 * some cases, this wakes the USB host. 3483 * 3484 * Suspending OTG devices may trigger HNP, if that's been enabled 3485 * between a pair of dual-role devices. That will change roles, such 3486 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 3487 * 3488 * Devices on USB hub ports have only one "suspend" state, corresponding 3489 * to ACPI D2, "may cause the device to lose some context". 3490 * State transitions include: 3491 * 3492 * - suspend, resume ... when the VBUS power link stays live 3493 * - suspend, disconnect ... VBUS lost 3494 * 3495 * Once VBUS drop breaks the circuit, the port it's using has to go through 3496 * normal re-enumeration procedures, starting with enabling VBUS power. 3497 * Other than re-initializing the hub (plug/unplug, except for root hubs), 3498 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq 3499 * timer, no SRP, no requests through sysfs. 3500 * 3501 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get 3502 * suspended until their bus goes into global suspend (i.e., the root 3503 * hub is suspended). Nevertheless, we change @udev->state to 3504 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual 3505 * upstream port setting is stored in @udev->port_is_suspended. 3506 * 3507 * Returns 0 on success, else negative errno. 3508 */ 3509 int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 3510 { 3511 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3512 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3513 int port1 = udev->portnum; 3514 int status; 3515 bool really_suspend = true; 3516 3517 usb_lock_port(port_dev); 3518 3519 /* enable remote wakeup when appropriate; this lets the device 3520 * wake up the upstream hub (including maybe the root hub). 3521 * 3522 * NOTE: OTG devices may issue remote wakeup (or SRP) even when 3523 * we don't explicitly enable it here. 3524 */ 3525 if (udev->do_remote_wakeup) { 3526 status = usb_enable_remote_wakeup(udev); 3527 if (status) { 3528 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 3529 status); 3530 /* bail if autosuspend is requested */ 3531 if (PMSG_IS_AUTO(msg)) 3532 goto err_wakeup; 3533 } 3534 } 3535 3536 /* disable USB2 hardware LPM */ 3537 usb_disable_usb2_hardware_lpm(udev); 3538 3539 if (usb_disable_ltm(udev)) { 3540 dev_err(&udev->dev, "Failed to disable LTM before suspend\n"); 3541 status = -ENOMEM; 3542 if (PMSG_IS_AUTO(msg)) 3543 goto err_ltm; 3544 } 3545 3546 /* see 7.1.7.6 */ 3547 if (hub_is_superspeed(hub->hdev)) 3548 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); 3549 3550 /* 3551 * For system suspend, we do not need to enable the suspend feature 3552 * on individual USB-2 ports. The devices will automatically go 3553 * into suspend a few ms after the root hub stops sending packets. 3554 * The USB 2.0 spec calls this "global suspend". 3555 * 3556 * However, many USB hubs have a bug: They don't relay wakeup requests 3557 * from a downstream port if the port's suspend feature isn't on. 3558 * Therefore we will turn on the suspend feature if udev or any of its 3559 * descendants is enabled for remote wakeup. 3560 */ 3561 else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0) 3562 status = set_port_feature(hub->hdev, port1, 3563 USB_PORT_FEAT_SUSPEND); 3564 else { 3565 really_suspend = false; 3566 status = 0; 3567 } 3568 if (status) { 3569 /* Check if the port has been suspended for the timeout case 3570 * to prevent the suspended port from incorrect handling. 3571 */ 3572 if (status == -ETIMEDOUT) { 3573 int ret; 3574 u16 portstatus, portchange; 3575 3576 portstatus = portchange = 0; 3577 ret = usb_hub_port_status(hub, port1, &portstatus, 3578 &portchange); 3579 3580 dev_dbg(&port_dev->dev, 3581 "suspend timeout, status %04x\n", portstatus); 3582 3583 if (ret == 0 && port_is_suspended(hub, portstatus)) { 3584 status = 0; 3585 goto suspend_done; 3586 } 3587 } 3588 3589 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); 3590 3591 /* Try to enable USB3 LTM again */ 3592 usb_enable_ltm(udev); 3593 err_ltm: 3594 /* Try to enable USB2 hardware LPM again */ 3595 usb_enable_usb2_hardware_lpm(udev); 3596 3597 if (udev->do_remote_wakeup) 3598 (void) usb_disable_remote_wakeup(udev); 3599 err_wakeup: 3600 3601 /* System sleep transitions should never fail */ 3602 if (!PMSG_IS_AUTO(msg)) 3603 status = 0; 3604 } else { 3605 suspend_done: 3606 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 3607 (PMSG_IS_AUTO(msg) ? "auto-" : ""), 3608 udev->do_remote_wakeup); 3609 if (really_suspend) { 3610 udev->port_is_suspended = 1; 3611 3612 /* device has up to 10 msec to fully suspend */ 3613 msleep(10); 3614 } 3615 usb_set_device_state(udev, USB_STATE_SUSPENDED); 3616 } 3617 3618 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled 3619 && test_and_clear_bit(port1, hub->child_usage_bits)) 3620 pm_runtime_put_sync(&port_dev->dev); 3621 3622 usb_mark_last_busy(hub->hdev); 3623 3624 usb_unlock_port(port_dev); 3625 return status; 3626 } 3627 3628 /* 3629 * If the USB "suspend" state is in use (rather than "global suspend"), 3630 * many devices will be individually taken out of suspend state using 3631 * special "resume" signaling. This routine kicks in shortly after 3632 * hardware resume signaling is finished, either because of selective 3633 * resume (by host) or remote wakeup (by device) ... now see what changed 3634 * in the tree that's rooted at this device. 3635 * 3636 * If @udev->reset_resume is set then the device is reset before the 3637 * status check is done. 3638 */ 3639 static int finish_port_resume(struct usb_device *udev) 3640 { 3641 int status = 0; 3642 u16 devstatus = 0; 3643 3644 /* caller owns the udev device lock */ 3645 dev_dbg(&udev->dev, "%s\n", 3646 udev->reset_resume ? "finish reset-resume" : "finish resume"); 3647 3648 /* usb ch9 identifies four variants of SUSPENDED, based on what 3649 * state the device resumes to. Linux currently won't see the 3650 * first two on the host side; they'd be inside hub_port_init() 3651 * during many timeouts, but hub_wq can't suspend until later. 3652 */ 3653 usb_set_device_state(udev, udev->actconfig 3654 ? USB_STATE_CONFIGURED 3655 : USB_STATE_ADDRESS); 3656 3657 /* 10.5.4.5 says not to reset a suspended port if the attached 3658 * device is enabled for remote wakeup. Hence the reset 3659 * operation is carried out here, after the port has been 3660 * resumed. 3661 */ 3662 if (udev->reset_resume) { 3663 /* 3664 * If the device morphs or switches modes when it is reset, 3665 * we don't want to perform a reset-resume. We'll fail the 3666 * resume, which will cause a logical disconnect, and then 3667 * the device will be rediscovered. 3668 */ 3669 retry_reset_resume: 3670 if (udev->quirks & USB_QUIRK_RESET) 3671 status = -ENODEV; 3672 else 3673 status = usb_reset_and_verify_device(udev); 3674 } 3675 3676 /* 10.5.4.5 says be sure devices in the tree are still there. 3677 * For now let's assume the device didn't go crazy on resume, 3678 * and device drivers will know about any resume quirks. 3679 */ 3680 if (status == 0) { 3681 devstatus = 0; 3682 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 3683 3684 /* If a normal resume failed, try doing a reset-resume */ 3685 if (status && !udev->reset_resume && udev->persist_enabled) { 3686 dev_dbg(&udev->dev, "retry with reset-resume\n"); 3687 udev->reset_resume = 1; 3688 goto retry_reset_resume; 3689 } 3690 } 3691 3692 if (status) { 3693 dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 3694 status); 3695 /* 3696 * There are a few quirky devices which violate the standard 3697 * by claiming to have remote wakeup enabled after a reset, 3698 * which crash if the feature is cleared, hence check for 3699 * udev->reset_resume 3700 */ 3701 } else if (udev->actconfig && !udev->reset_resume) { 3702 if (udev->speed < USB_SPEED_SUPER) { 3703 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) 3704 status = usb_disable_remote_wakeup(udev); 3705 } else { 3706 status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0, 3707 &devstatus); 3708 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP 3709 | USB_INTRF_STAT_FUNC_RW)) 3710 status = usb_disable_remote_wakeup(udev); 3711 } 3712 3713 if (status) 3714 dev_dbg(&udev->dev, 3715 "disable remote wakeup, status %d\n", 3716 status); 3717 status = 0; 3718 } 3719 return status; 3720 } 3721 3722 /* 3723 * There are some SS USB devices which take longer time for link training. 3724 * XHCI specs 4.19.4 says that when Link training is successful, port 3725 * sets CCS bit to 1. So if SW reads port status before successful link 3726 * training, then it will not find device to be present. 3727 * USB Analyzer log with such buggy devices show that in some cases 3728 * device switch on the RX termination after long delay of host enabling 3729 * the VBUS. In few other cases it has been seen that device fails to 3730 * negotiate link training in first attempt. It has been 3731 * reported till now that few devices take as long as 2000 ms to train 3732 * the link after host enabling its VBUS and termination. Following 3733 * routine implements a 2000 ms timeout for link training. If in a case 3734 * link trains before timeout, loop will exit earlier. 3735 * 3736 * There are also some 2.0 hard drive based devices and 3.0 thumb 3737 * drives that, when plugged into a 2.0 only port, take a long 3738 * time to set CCS after VBUS enable. 3739 * 3740 * FIXME: If a device was connected before suspend, but was removed 3741 * while system was asleep, then the loop in the following routine will 3742 * only exit at timeout. 3743 * 3744 * This routine should only be called when persist is enabled. 3745 */ 3746 static int wait_for_connected(struct usb_device *udev, 3747 struct usb_hub *hub, int port1, 3748 u16 *portchange, u16 *portstatus) 3749 { 3750 int status = 0, delay_ms = 0; 3751 3752 while (delay_ms < 2000) { 3753 if (status || *portstatus & USB_PORT_STAT_CONNECTION) 3754 break; 3755 if (!usb_port_is_power_on(hub, *portstatus)) { 3756 status = -ENODEV; 3757 break; 3758 } 3759 msleep(20); 3760 delay_ms += 20; 3761 status = usb_hub_port_status(hub, port1, portstatus, portchange); 3762 } 3763 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); 3764 return status; 3765 } 3766 3767 /* 3768 * usb_port_resume - re-activate a suspended usb device's upstream port 3769 * @udev: device to re-activate, not a root hub 3770 * Context: must be able to sleep; device not locked; pm locks held 3771 * 3772 * This will re-activate the suspended device, increasing power usage 3773 * while letting drivers communicate again with its endpoints. 3774 * USB resume explicitly guarantees that the power session between 3775 * the host and the device is the same as it was when the device 3776 * suspended. 3777 * 3778 * If @udev->reset_resume is set then this routine won't check that the 3779 * port is still enabled. Furthermore, finish_port_resume() above will 3780 * reset @udev. The end result is that a broken power session can be 3781 * recovered and @udev will appear to persist across a loss of VBUS power. 3782 * 3783 * For example, if a host controller doesn't maintain VBUS suspend current 3784 * during a system sleep or is reset when the system wakes up, all the USB 3785 * power sessions below it will be broken. This is especially troublesome 3786 * for mass-storage devices containing mounted filesystems, since the 3787 * device will appear to have disconnected and all the memory mappings 3788 * to it will be lost. Using the USB_PERSIST facility, the device can be 3789 * made to appear as if it had not disconnected. 3790 * 3791 * This facility can be dangerous. Although usb_reset_and_verify_device() makes 3792 * every effort to insure that the same device is present after the 3793 * reset as before, it cannot provide a 100% guarantee. Furthermore it's 3794 * quite possible for a device to remain unaltered but its media to be 3795 * changed. If the user replaces a flash memory card while the system is 3796 * asleep, he will have only himself to blame when the filesystem on the 3797 * new card is corrupted and the system crashes. 3798 * 3799 * Returns 0 on success, else negative errno. 3800 */ 3801 int usb_port_resume(struct usb_device *udev, pm_message_t msg) 3802 { 3803 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3804 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3805 int port1 = udev->portnum; 3806 int status; 3807 u16 portchange, portstatus; 3808 3809 if (!test_and_set_bit(port1, hub->child_usage_bits)) { 3810 status = pm_runtime_resume_and_get(&port_dev->dev); 3811 if (status < 0) { 3812 dev_dbg(&udev->dev, "can't resume usb port, status %d\n", 3813 status); 3814 return status; 3815 } 3816 } 3817 3818 usb_lock_port(port_dev); 3819 3820 /* Skip the initial Clear-Suspend step for a remote wakeup */ 3821 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 3822 if (status == 0 && !port_is_suspended(hub, portstatus)) { 3823 if (portchange & USB_PORT_STAT_C_SUSPEND) 3824 pm_wakeup_event(&udev->dev, 0); 3825 goto SuspendCleared; 3826 } 3827 3828 /* see 7.1.7.7; affects power usage, but not budgeting */ 3829 if (hub_is_superspeed(hub->hdev)) 3830 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); 3831 else 3832 status = usb_clear_port_feature(hub->hdev, 3833 port1, USB_PORT_FEAT_SUSPEND); 3834 if (status) { 3835 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); 3836 } else { 3837 /* drive resume for USB_RESUME_TIMEOUT msec */ 3838 dev_dbg(&udev->dev, "usb %sresume\n", 3839 (PMSG_IS_AUTO(msg) ? "auto-" : "")); 3840 msleep(USB_RESUME_TIMEOUT); 3841 3842 /* Virtual root hubs can trigger on GET_PORT_STATUS to 3843 * stop resume signaling. Then finish the resume 3844 * sequence. 3845 */ 3846 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 3847 } 3848 3849 SuspendCleared: 3850 if (status == 0) { 3851 udev->port_is_suspended = 0; 3852 if (hub_is_superspeed(hub->hdev)) { 3853 if (portchange & USB_PORT_STAT_C_LINK_STATE) 3854 usb_clear_port_feature(hub->hdev, port1, 3855 USB_PORT_FEAT_C_PORT_LINK_STATE); 3856 } else { 3857 if (portchange & USB_PORT_STAT_C_SUSPEND) 3858 usb_clear_port_feature(hub->hdev, port1, 3859 USB_PORT_FEAT_C_SUSPEND); 3860 } 3861 3862 /* TRSMRCY = 10 msec */ 3863 msleep(10); 3864 } 3865 3866 if (udev->persist_enabled) 3867 status = wait_for_connected(udev, hub, port1, &portchange, 3868 &portstatus); 3869 3870 status = check_port_resume_type(udev, 3871 hub, port1, status, portchange, portstatus); 3872 if (status == 0) 3873 status = finish_port_resume(udev); 3874 if (status < 0) { 3875 dev_dbg(&udev->dev, "can't resume, status %d\n", status); 3876 hub_port_logical_disconnect(hub, port1); 3877 } else { 3878 /* Try to enable USB2 hardware LPM */ 3879 usb_enable_usb2_hardware_lpm(udev); 3880 3881 /* Try to enable USB3 LTM */ 3882 usb_enable_ltm(udev); 3883 } 3884 3885 usb_unlock_port(port_dev); 3886 3887 return status; 3888 } 3889 3890 int usb_remote_wakeup(struct usb_device *udev) 3891 { 3892 int status = 0; 3893 3894 usb_lock_device(udev); 3895 if (udev->state == USB_STATE_SUSPENDED) { 3896 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 3897 status = usb_autoresume_device(udev); 3898 if (status == 0) { 3899 /* Let the drivers do their thing, then... */ 3900 usb_autosuspend_device(udev); 3901 } 3902 } 3903 usb_unlock_device(udev); 3904 return status; 3905 } 3906 3907 /* Returns 1 if there was a remote wakeup and a connect status change. */ 3908 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 3909 u16 portstatus, u16 portchange) 3910 __must_hold(&port_dev->status_lock) 3911 { 3912 struct usb_port *port_dev = hub->ports[port - 1]; 3913 struct usb_device *hdev; 3914 struct usb_device *udev; 3915 int connect_change = 0; 3916 u16 link_state; 3917 int ret; 3918 3919 hdev = hub->hdev; 3920 udev = port_dev->child; 3921 if (!hub_is_superspeed(hdev)) { 3922 if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 3923 return 0; 3924 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 3925 } else { 3926 link_state = portstatus & USB_PORT_STAT_LINK_STATE; 3927 if (!udev || udev->state != USB_STATE_SUSPENDED || 3928 (link_state != USB_SS_PORT_LS_U0 && 3929 link_state != USB_SS_PORT_LS_U1 && 3930 link_state != USB_SS_PORT_LS_U2)) 3931 return 0; 3932 } 3933 3934 if (udev) { 3935 /* TRSMRCY = 10 msec */ 3936 msleep(10); 3937 3938 usb_unlock_port(port_dev); 3939 ret = usb_remote_wakeup(udev); 3940 usb_lock_port(port_dev); 3941 if (ret < 0) 3942 connect_change = 1; 3943 } else { 3944 ret = -ENODEV; 3945 hub_port_disable(hub, port, 1); 3946 } 3947 dev_dbg(&port_dev->dev, "resume, status %d\n", ret); 3948 return connect_change; 3949 } 3950 3951 static int check_ports_changed(struct usb_hub *hub) 3952 { 3953 int port1; 3954 3955 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { 3956 u16 portstatus, portchange; 3957 int status; 3958 3959 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 3960 if (!status && portchange) 3961 return 1; 3962 } 3963 return 0; 3964 } 3965 3966 static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 3967 { 3968 struct usb_hub *hub = usb_get_intfdata(intf); 3969 struct usb_device *hdev = hub->hdev; 3970 unsigned port1; 3971 3972 /* 3973 * Warn if children aren't already suspended. 3974 * Also, add up the number of wakeup-enabled descendants. 3975 */ 3976 hub->wakeup_enabled_descendants = 0; 3977 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3978 struct usb_port *port_dev = hub->ports[port1 - 1]; 3979 struct usb_device *udev = port_dev->child; 3980 3981 if (udev && udev->can_submit) { 3982 dev_warn(&port_dev->dev, "device %s not suspended yet\n", 3983 dev_name(&udev->dev)); 3984 if (PMSG_IS_AUTO(msg)) 3985 return -EBUSY; 3986 } 3987 if (udev) 3988 hub->wakeup_enabled_descendants += 3989 usb_wakeup_enabled_descendants(udev); 3990 } 3991 3992 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { 3993 /* check if there are changes pending on hub ports */ 3994 if (check_ports_changed(hub)) { 3995 if (PMSG_IS_AUTO(msg)) 3996 return -EBUSY; 3997 pm_wakeup_event(&hdev->dev, 2000); 3998 } 3999 } 4000 4001 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 4002 /* Enable hub to send remote wakeup for all ports. */ 4003 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 4004 set_port_feature(hdev, 4005 port1 | 4006 USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 4007 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 4008 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 4009 USB_PORT_FEAT_REMOTE_WAKE_MASK); 4010 } 4011 } 4012 4013 dev_dbg(&intf->dev, "%s\n", __func__); 4014 4015 /* stop hub_wq and related activity */ 4016 hub_quiesce(hub, HUB_SUSPEND); 4017 return 0; 4018 } 4019 4020 /* Report wakeup requests from the ports of a resuming root hub */ 4021 static void report_wakeup_requests(struct usb_hub *hub) 4022 { 4023 struct usb_device *hdev = hub->hdev; 4024 struct usb_device *udev; 4025 struct usb_hcd *hcd; 4026 unsigned long resuming_ports; 4027 int i; 4028 4029 if (hdev->parent) 4030 return; /* Not a root hub */ 4031 4032 hcd = bus_to_hcd(hdev->bus); 4033 if (hcd->driver->get_resuming_ports) { 4034 4035 /* 4036 * The get_resuming_ports() method returns a bitmap (origin 0) 4037 * of ports which have started wakeup signaling but have not 4038 * yet finished resuming. During system resume we will 4039 * resume all the enabled ports, regardless of any wakeup 4040 * signals, which means the wakeup requests would be lost. 4041 * To prevent this, report them to the PM core here. 4042 */ 4043 resuming_ports = hcd->driver->get_resuming_ports(hcd); 4044 for (i = 0; i < hdev->maxchild; ++i) { 4045 if (test_bit(i, &resuming_ports)) { 4046 udev = hub->ports[i]->child; 4047 if (udev) 4048 pm_wakeup_event(&udev->dev, 0); 4049 } 4050 } 4051 } 4052 } 4053 4054 static int hub_resume(struct usb_interface *intf) 4055 { 4056 struct usb_hub *hub = usb_get_intfdata(intf); 4057 4058 dev_dbg(&intf->dev, "%s\n", __func__); 4059 hub_activate(hub, HUB_RESUME); 4060 4061 /* 4062 * This should be called only for system resume, not runtime resume. 4063 * We can't tell the difference here, so some wakeup requests will be 4064 * reported at the wrong time or more than once. This shouldn't 4065 * matter much, so long as they do get reported. 4066 */ 4067 report_wakeup_requests(hub); 4068 return 0; 4069 } 4070 4071 static int hub_reset_resume(struct usb_interface *intf) 4072 { 4073 struct usb_hub *hub = usb_get_intfdata(intf); 4074 4075 dev_dbg(&intf->dev, "%s\n", __func__); 4076 hub_activate(hub, HUB_RESET_RESUME); 4077 return 0; 4078 } 4079 4080 /** 4081 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 4082 * @rhdev: struct usb_device for the root hub 4083 * 4084 * The USB host controller driver calls this function when its root hub 4085 * is resumed and Vbus power has been interrupted or the controller 4086 * has been reset. The routine marks @rhdev as having lost power. 4087 * When the hub driver is resumed it will take notice and carry out 4088 * power-session recovery for all the "USB-PERSIST"-enabled child devices; 4089 * the others will be disconnected. 4090 */ 4091 void usb_root_hub_lost_power(struct usb_device *rhdev) 4092 { 4093 dev_notice(&rhdev->dev, "root hub lost power or was reset\n"); 4094 rhdev->reset_resume = 1; 4095 } 4096 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 4097 4098 static const char * const usb3_lpm_names[] = { 4099 "U0", 4100 "U1", 4101 "U2", 4102 "U3", 4103 }; 4104 4105 /* 4106 * Send a Set SEL control transfer to the device, prior to enabling 4107 * device-initiated U1 or U2. This lets the device know the exit latencies from 4108 * the time the device initiates a U1 or U2 exit, to the time it will receive a 4109 * packet from the host. 4110 * 4111 * This function will fail if the SEL or PEL values for udev are greater than 4112 * the maximum allowed values for the link state to be enabled. 4113 */ 4114 static int usb_req_set_sel(struct usb_device *udev) 4115 { 4116 struct usb_set_sel_req *sel_values; 4117 unsigned long long u1_sel; 4118 unsigned long long u1_pel; 4119 unsigned long long u2_sel; 4120 unsigned long long u2_pel; 4121 int ret; 4122 4123 if (!udev->parent || udev->speed < USB_SPEED_SUPER || !udev->lpm_capable) 4124 return 0; 4125 4126 /* Convert SEL and PEL stored in ns to us */ 4127 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 4128 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 4129 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 4130 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 4131 4132 /* 4133 * Make sure that the calculated SEL and PEL values for the link 4134 * state we're enabling aren't bigger than the max SEL/PEL 4135 * value that will fit in the SET SEL control transfer. 4136 * Otherwise the device would get an incorrect idea of the exit 4137 * latency for the link state, and could start a device-initiated 4138 * U1/U2 when the exit latencies are too high. 4139 */ 4140 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 4141 u1_pel > USB3_LPM_MAX_U1_SEL_PEL || 4142 u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 4143 u2_pel > USB3_LPM_MAX_U2_SEL_PEL) { 4144 dev_dbg(&udev->dev, "Device-initiated U1/U2 disabled due to long SEL or PEL\n"); 4145 return -EINVAL; 4146 } 4147 4148 /* 4149 * usb_enable_lpm() can be called as part of a failed device reset, 4150 * which may be initiated by an error path of a mass storage driver. 4151 * Therefore, use GFP_NOIO. 4152 */ 4153 sel_values = kmalloc_obj(*(sel_values), GFP_NOIO); 4154 if (!sel_values) 4155 return -ENOMEM; 4156 4157 sel_values->u1_sel = u1_sel; 4158 sel_values->u1_pel = u1_pel; 4159 sel_values->u2_sel = cpu_to_le16(u2_sel); 4160 sel_values->u2_pel = cpu_to_le16(u2_pel); 4161 4162 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4163 USB_REQ_SET_SEL, 4164 USB_RECIP_DEVICE, 4165 0, 0, 4166 sel_values, sizeof *(sel_values), 4167 USB_CTRL_SET_TIMEOUT); 4168 kfree(sel_values); 4169 4170 if (ret > 0) 4171 udev->lpm_devinit_allow = 1; 4172 4173 return ret; 4174 } 4175 4176 /* 4177 * Enable or disable device-initiated U1 or U2 transitions. 4178 */ 4179 static int usb_set_device_initiated_lpm(struct usb_device *udev, 4180 enum usb3_link_state state, bool enable) 4181 { 4182 int ret; 4183 int feature; 4184 4185 switch (state) { 4186 case USB3_LPM_U1: 4187 feature = USB_DEVICE_U1_ENABLE; 4188 break; 4189 case USB3_LPM_U2: 4190 feature = USB_DEVICE_U2_ENABLE; 4191 break; 4192 default: 4193 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 4194 __func__, str_enable_disable(enable)); 4195 return -EINVAL; 4196 } 4197 4198 if (udev->state != USB_STATE_CONFIGURED) { 4199 dev_dbg(&udev->dev, "%s: Can't %s %s state " 4200 "for unconfigured device.\n", 4201 __func__, str_enable_disable(enable), 4202 usb3_lpm_names[state]); 4203 return -EINVAL; 4204 } 4205 4206 if (enable) { 4207 /* 4208 * Now send the control transfer to enable device-initiated LPM 4209 * for either U1 or U2. 4210 */ 4211 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4212 USB_REQ_SET_FEATURE, 4213 USB_RECIP_DEVICE, 4214 feature, 4215 0, NULL, 0, 4216 USB_CTRL_SET_TIMEOUT); 4217 } else { 4218 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4219 USB_REQ_CLEAR_FEATURE, 4220 USB_RECIP_DEVICE, 4221 feature, 4222 0, NULL, 0, 4223 USB_CTRL_SET_TIMEOUT); 4224 } 4225 if (ret < 0) { 4226 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 4227 str_enable_disable(enable), usb3_lpm_names[state]); 4228 return -EBUSY; 4229 } 4230 return 0; 4231 } 4232 4233 static int usb_set_lpm_timeout(struct usb_device *udev, 4234 enum usb3_link_state state, int timeout) 4235 { 4236 int ret; 4237 int feature; 4238 4239 switch (state) { 4240 case USB3_LPM_U1: 4241 feature = USB_PORT_FEAT_U1_TIMEOUT; 4242 break; 4243 case USB3_LPM_U2: 4244 feature = USB_PORT_FEAT_U2_TIMEOUT; 4245 break; 4246 default: 4247 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 4248 __func__); 4249 return -EINVAL; 4250 } 4251 4252 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 4253 timeout != USB3_LPM_DEVICE_INITIATED) { 4254 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 4255 "which is a reserved value.\n", 4256 usb3_lpm_names[state], timeout); 4257 return -EINVAL; 4258 } 4259 4260 ret = set_port_feature(udev->parent, 4261 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 4262 feature); 4263 if (ret < 0) { 4264 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 4265 "error code %i\n", usb3_lpm_names[state], 4266 timeout, ret); 4267 return -EBUSY; 4268 } 4269 if (state == USB3_LPM_U1) 4270 udev->u1_params.timeout = timeout; 4271 else 4272 udev->u2_params.timeout = timeout; 4273 return 0; 4274 } 4275 4276 /* 4277 * Don't allow device intiated U1/U2 if device isn't in the configured state, 4278 * or the system exit latency + one bus interval is greater than the minimum 4279 * service interval of any active periodic endpoint. See USB 3.2 section 9.4.9 4280 */ 4281 static bool usb_device_may_initiate_lpm(struct usb_device *udev, 4282 enum usb3_link_state state) 4283 { 4284 unsigned int sel; /* us */ 4285 int i, j; 4286 4287 if (!udev->lpm_devinit_allow || !udev->actconfig) 4288 return false; 4289 4290 if (state == USB3_LPM_U1) 4291 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 4292 else if (state == USB3_LPM_U2) 4293 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 4294 else 4295 return false; 4296 4297 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 4298 struct usb_interface *intf; 4299 struct usb_endpoint_descriptor *desc; 4300 unsigned int interval; 4301 4302 intf = udev->actconfig->interface[i]; 4303 if (!intf) 4304 continue; 4305 4306 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) { 4307 desc = &intf->cur_altsetting->endpoint[j].desc; 4308 4309 if (usb_endpoint_xfer_int(desc) || 4310 usb_endpoint_xfer_isoc(desc)) { 4311 interval = (1 << (desc->bInterval - 1)) * 125; 4312 if (sel + 125 > interval) 4313 return false; 4314 } 4315 } 4316 } 4317 return true; 4318 } 4319 4320 /* 4321 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 4322 * U1/U2 entry. 4323 * 4324 * We will attempt to enable U1 or U2, but there are no guarantees that the 4325 * control transfers to set the hub timeout or enable device-initiated U1/U2 4326 * will be successful. 4327 * 4328 * If the control transfer to enable device-initiated U1/U2 entry fails, then 4329 * hub-initiated U1/U2 will be disabled. 4330 * 4331 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 4332 * driver know about it. If that call fails, it should be harmless, and just 4333 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 4334 */ 4335 static int usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 4336 enum usb3_link_state state) 4337 { 4338 int timeout; 4339 __u8 u1_mel; 4340 __le16 u2_mel; 4341 4342 /* Skip if the device BOS descriptor couldn't be read */ 4343 if (!udev->bos) 4344 return -EINVAL; 4345 4346 u1_mel = udev->bos->ss_cap->bU1devExitLat; 4347 u2_mel = udev->bos->ss_cap->bU2DevExitLat; 4348 4349 /* If the device says it doesn't have *any* exit latency to come out of 4350 * U1 or U2, it's probably lying. Assume it doesn't implement that link 4351 * state. 4352 */ 4353 if ((state == USB3_LPM_U1 && u1_mel == 0) || 4354 (state == USB3_LPM_U2 && u2_mel == 0)) 4355 return -EINVAL; 4356 4357 /* We allow the host controller to set the U1/U2 timeout internally 4358 * first, so that it can change its schedule to account for the 4359 * additional latency to send data to a device in a lower power 4360 * link state. 4361 */ 4362 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 4363 4364 /* xHCI host controller doesn't want to enable this LPM state. */ 4365 if (timeout == 0) 4366 return -EINVAL; 4367 4368 if (timeout < 0) { 4369 dev_warn(&udev->dev, "Could not enable %s link state, " 4370 "xHCI error %i.\n", usb3_lpm_names[state], 4371 timeout); 4372 return timeout; 4373 } 4374 4375 if (usb_set_lpm_timeout(udev, state, timeout)) { 4376 /* If we can't set the parent hub U1/U2 timeout, 4377 * device-initiated LPM won't be allowed either, so let the xHCI 4378 * host know that this link state won't be enabled. 4379 */ 4380 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 4381 return -EBUSY; 4382 } 4383 4384 if (state == USB3_LPM_U1) 4385 udev->usb3_lpm_u1_enabled = 1; 4386 else if (state == USB3_LPM_U2) 4387 udev->usb3_lpm_u2_enabled = 1; 4388 4389 return 0; 4390 } 4391 /* 4392 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 4393 * U1/U2 entry. 4394 * 4395 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 4396 * If zero is returned, the parent will not allow the link to go into U1/U2. 4397 * 4398 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 4399 * it won't have an effect on the bus link state because the parent hub will 4400 * still disallow device-initiated U1/U2 entry. 4401 * 4402 * If zero is returned, the xHCI host controller may still think U1/U2 entry is 4403 * possible. The result will be slightly more bus bandwidth will be taken up 4404 * (to account for U1/U2 exit latency), but it should be harmless. 4405 */ 4406 static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 4407 enum usb3_link_state state) 4408 { 4409 switch (state) { 4410 case USB3_LPM_U1: 4411 case USB3_LPM_U2: 4412 break; 4413 default: 4414 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 4415 __func__); 4416 return -EINVAL; 4417 } 4418 4419 if (usb_set_lpm_timeout(udev, state, 0)) 4420 return -EBUSY; 4421 4422 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 4423 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 4424 "bus schedule bandwidth may be impacted.\n", 4425 usb3_lpm_names[state]); 4426 4427 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM 4428 * is disabled. Hub will disallows link to enter U1/U2 as well, 4429 * even device is initiating LPM. Hence LPM is disabled if hub LPM 4430 * timeout set to 0, no matter device-initiated LPM is disabled or 4431 * not. 4432 */ 4433 if (state == USB3_LPM_U1) 4434 udev->usb3_lpm_u1_enabled = 0; 4435 else if (state == USB3_LPM_U2) 4436 udev->usb3_lpm_u2_enabled = 0; 4437 4438 return 0; 4439 } 4440 4441 /* 4442 * Disable hub-initiated and device-initiated U1 and U2 entry. 4443 * Caller must own the bandwidth_mutex. 4444 * 4445 * This will call usb_enable_lpm() on failure, which will decrement 4446 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 4447 */ 4448 int usb_disable_lpm(struct usb_device *udev) 4449 { 4450 struct usb_hcd *hcd; 4451 int err; 4452 4453 if (!udev || !udev->parent || 4454 udev->speed < USB_SPEED_SUPER || 4455 !udev->lpm_capable || 4456 udev->state < USB_STATE_CONFIGURED) 4457 return 0; 4458 4459 hcd = bus_to_hcd(udev->bus); 4460 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 4461 return 0; 4462 4463 udev->lpm_disable_count++; 4464 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 4465 return 0; 4466 4467 /* If LPM is enabled, attempt to disable it. */ 4468 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 4469 goto disable_failed; 4470 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 4471 goto disable_failed; 4472 4473 err = usb_set_device_initiated_lpm(udev, USB3_LPM_U1, false); 4474 if (!err) 4475 usb_set_device_initiated_lpm(udev, USB3_LPM_U2, false); 4476 4477 return 0; 4478 4479 disable_failed: 4480 udev->lpm_disable_count--; 4481 4482 return -EBUSY; 4483 } 4484 EXPORT_SYMBOL_GPL(usb_disable_lpm); 4485 4486 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 4487 int usb_unlocked_disable_lpm(struct usb_device *udev) 4488 { 4489 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4490 int ret; 4491 4492 if (!hcd) 4493 return -EINVAL; 4494 4495 mutex_lock(hcd->bandwidth_mutex); 4496 ret = usb_disable_lpm(udev); 4497 mutex_unlock(hcd->bandwidth_mutex); 4498 4499 return ret; 4500 } 4501 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 4502 4503 /* 4504 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 4505 * xHCI host policy may prevent U1 or U2 from being enabled. 4506 * 4507 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 4508 * until the lpm_disable_count drops to zero. Caller must own the 4509 * bandwidth_mutex. 4510 */ 4511 void usb_enable_lpm(struct usb_device *udev) 4512 { 4513 struct usb_hcd *hcd; 4514 struct usb_hub *hub; 4515 struct usb_port *port_dev; 4516 4517 if (!udev || !udev->parent || 4518 udev->speed < USB_SPEED_SUPER || 4519 !udev->lpm_capable || 4520 udev->state < USB_STATE_CONFIGURED) 4521 return; 4522 4523 udev->lpm_disable_count--; 4524 hcd = bus_to_hcd(udev->bus); 4525 /* Double check that we can both enable and disable LPM. 4526 * Device must be configured to accept set feature U1/U2 timeout. 4527 */ 4528 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 4529 !hcd->driver->disable_usb3_lpm_timeout) 4530 return; 4531 4532 if (udev->lpm_disable_count > 0) 4533 return; 4534 4535 hub = usb_hub_to_struct_hub(udev->parent); 4536 if (!hub) 4537 return; 4538 4539 port_dev = hub->ports[udev->portnum - 1]; 4540 4541 if (port_dev->usb3_lpm_u1_permit) 4542 if (usb_enable_link_state(hcd, udev, USB3_LPM_U1)) 4543 return; 4544 4545 if (port_dev->usb3_lpm_u2_permit) 4546 if (usb_enable_link_state(hcd, udev, USB3_LPM_U2)) 4547 return; 4548 4549 /* 4550 * Enable device initiated U1/U2 with a SetFeature(U1/U2_ENABLE) request 4551 * if system exit latency is short enough and device is configured 4552 */ 4553 if (usb_device_may_initiate_lpm(udev, USB3_LPM_U1)) { 4554 if (usb_set_device_initiated_lpm(udev, USB3_LPM_U1, true)) 4555 return; 4556 4557 if (usb_device_may_initiate_lpm(udev, USB3_LPM_U2)) 4558 usb_set_device_initiated_lpm(udev, USB3_LPM_U2, true); 4559 } 4560 } 4561 EXPORT_SYMBOL_GPL(usb_enable_lpm); 4562 4563 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 4564 void usb_unlocked_enable_lpm(struct usb_device *udev) 4565 { 4566 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4567 4568 if (!hcd) 4569 return; 4570 4571 mutex_lock(hcd->bandwidth_mutex); 4572 usb_enable_lpm(udev); 4573 mutex_unlock(hcd->bandwidth_mutex); 4574 } 4575 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4576 4577 /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */ 4578 static void hub_usb3_port_prepare_disable(struct usb_hub *hub, 4579 struct usb_port *port_dev) 4580 { 4581 struct usb_device *udev = port_dev->child; 4582 int ret; 4583 4584 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { 4585 ret = hub_set_port_link_state(hub, port_dev->portnum, 4586 USB_SS_PORT_LS_U0); 4587 if (!ret) { 4588 msleep(USB_RESUME_TIMEOUT); 4589 ret = usb_disable_remote_wakeup(udev); 4590 } 4591 if (ret) 4592 dev_warn(&udev->dev, 4593 "Port disable: can't disable remote wake\n"); 4594 udev->do_remote_wakeup = 0; 4595 } 4596 } 4597 4598 #else /* CONFIG_PM */ 4599 4600 #define hub_suspend NULL 4601 #define hub_resume NULL 4602 #define hub_reset_resume NULL 4603 4604 static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, 4605 struct usb_port *port_dev) { } 4606 4607 int usb_disable_lpm(struct usb_device *udev) 4608 { 4609 return 0; 4610 } 4611 EXPORT_SYMBOL_GPL(usb_disable_lpm); 4612 4613 void usb_enable_lpm(struct usb_device *udev) { } 4614 EXPORT_SYMBOL_GPL(usb_enable_lpm); 4615 4616 int usb_unlocked_disable_lpm(struct usb_device *udev) 4617 { 4618 return 0; 4619 } 4620 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 4621 4622 void usb_unlocked_enable_lpm(struct usb_device *udev) { } 4623 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4624 4625 int usb_disable_ltm(struct usb_device *udev) 4626 { 4627 return 0; 4628 } 4629 EXPORT_SYMBOL_GPL(usb_disable_ltm); 4630 4631 void usb_enable_ltm(struct usb_device *udev) { } 4632 EXPORT_SYMBOL_GPL(usb_enable_ltm); 4633 4634 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 4635 u16 portstatus, u16 portchange) 4636 { 4637 return 0; 4638 } 4639 4640 static int usb_req_set_sel(struct usb_device *udev) 4641 { 4642 return 0; 4643 } 4644 4645 #endif /* CONFIG_PM */ 4646 4647 /* 4648 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating 4649 * a connection with a plugged-in cable but will signal the host when the cable 4650 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices 4651 */ 4652 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 4653 { 4654 struct usb_port *port_dev = hub->ports[port1 - 1]; 4655 struct usb_device *hdev = hub->hdev; 4656 int ret = 0; 4657 4658 if (!hub->error) { 4659 if (hub_is_superspeed(hub->hdev)) { 4660 hub_usb3_port_prepare_disable(hub, port_dev); 4661 ret = hub_set_port_link_state(hub, port_dev->portnum, 4662 USB_SS_PORT_LS_U3); 4663 } else { 4664 ret = usb_clear_port_feature(hdev, port1, 4665 USB_PORT_FEAT_ENABLE); 4666 } 4667 } 4668 if (port_dev->child && set_state) 4669 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); 4670 if (ret && ret != -ENODEV) 4671 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); 4672 return ret; 4673 } 4674 4675 /* 4676 * usb_port_disable - disable a usb device's upstream port 4677 * @udev: device to disable 4678 * Context: @udev locked, must be able to sleep. 4679 * 4680 * Disables a USB device that isn't in active use. 4681 */ 4682 int usb_port_disable(struct usb_device *udev) 4683 { 4684 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4685 4686 return hub_port_disable(hub, udev->portnum, 0); 4687 } 4688 4689 /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 4690 * 4691 * Between connect detection and reset signaling there must be a delay 4692 * of 100ms at least for debounce and power-settling. The corresponding 4693 * timer shall restart whenever the downstream port detects a disconnect. 4694 * 4695 * Apparently there are some bluetooth and irda-dongles and a number of 4696 * low-speed devices for which this debounce period may last over a second. 4697 * Not covered by the spec - but easy to deal with. 4698 * 4699 * This implementation uses a 1500ms total debounce timeout; if the 4700 * connection isn't stable by then it returns -ETIMEDOUT. It checks 4701 * every 25ms for transient disconnects. When the port status has been 4702 * unchanged for 100ms it returns the port status. 4703 */ 4704 int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) 4705 { 4706 int ret; 4707 u16 portchange, portstatus; 4708 unsigned connection = 0xffff; 4709 int total_time, stable_time = 0; 4710 struct usb_port *port_dev = hub->ports[port1 - 1]; 4711 4712 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 4713 ret = usb_hub_port_status(hub, port1, &portstatus, &portchange); 4714 if (ret < 0) 4715 return ret; 4716 4717 if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 4718 (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 4719 if (!must_be_connected || 4720 (connection == USB_PORT_STAT_CONNECTION)) 4721 stable_time += HUB_DEBOUNCE_STEP; 4722 if (stable_time >= HUB_DEBOUNCE_STABLE) 4723 break; 4724 } else { 4725 stable_time = 0; 4726 connection = portstatus & USB_PORT_STAT_CONNECTION; 4727 } 4728 4729 if (portchange & USB_PORT_STAT_C_CONNECTION) { 4730 usb_clear_port_feature(hub->hdev, port1, 4731 USB_PORT_FEAT_C_CONNECTION); 4732 } 4733 4734 if (total_time >= HUB_DEBOUNCE_TIMEOUT) 4735 break; 4736 msleep(HUB_DEBOUNCE_STEP); 4737 } 4738 4739 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", 4740 total_time, stable_time, portstatus); 4741 4742 if (stable_time < HUB_DEBOUNCE_STABLE) 4743 return -ETIMEDOUT; 4744 return portstatus; 4745 } 4746 4747 void usb_ep0_reinit(struct usb_device *udev) 4748 { 4749 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 4750 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 4751 usb_enable_endpoint(udev, &udev->ep0, true); 4752 } 4753 EXPORT_SYMBOL_GPL(usb_ep0_reinit); 4754 4755 static int hub_set_address(struct usb_device *udev, int devnum) 4756 { 4757 int retval; 4758 unsigned int timeout_ms = USB_CTRL_SET_TIMEOUT; 4759 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4760 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4761 4762 if (hub->hdev->quirks & USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT) 4763 timeout_ms = USB_SHORT_SET_ADDRESS_REQ_TIMEOUT; 4764 4765 /* 4766 * The host controller will choose the device address, 4767 * instead of the core having chosen it earlier 4768 */ 4769 if (!hcd->driver->address_device && devnum <= 1) 4770 return -EINVAL; 4771 if (udev->state == USB_STATE_ADDRESS) 4772 return 0; 4773 if (udev->state != USB_STATE_DEFAULT) 4774 return -EINVAL; 4775 if (hcd->driver->address_device) 4776 retval = hcd->driver->address_device(hcd, udev, timeout_ms); 4777 else 4778 retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4779 USB_REQ_SET_ADDRESS, 0, devnum, 0, 4780 NULL, 0, timeout_ms); 4781 if (retval == 0) { 4782 update_devnum(udev, devnum); 4783 /* Device now using proper address. */ 4784 usb_set_device_state(udev, USB_STATE_ADDRESS); 4785 usb_ep0_reinit(udev); 4786 } 4787 return retval; 4788 } 4789 4790 /* 4791 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM 4792 * when they're plugged into a USB 2.0 port, but they don't work when LPM is 4793 * enabled. 4794 * 4795 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the 4796 * device says it supports the new USB 2.0 Link PM errata by setting the BESL 4797 * support bit in the BOS descriptor. 4798 */ 4799 static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) 4800 { 4801 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4802 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN; 4803 4804 if (!udev->usb2_hw_lpm_capable || !udev->bos) 4805 return; 4806 4807 if (hub) 4808 connect_type = hub->ports[udev->portnum - 1]->connect_type; 4809 4810 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || 4811 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 4812 udev->usb2_hw_lpm_allowed = 1; 4813 usb_enable_usb2_hardware_lpm(udev); 4814 } 4815 } 4816 4817 static int hub_enable_device(struct usb_device *udev) 4818 { 4819 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4820 4821 if (!hcd->driver->enable_device) 4822 return 0; 4823 if (udev->state == USB_STATE_ADDRESS) 4824 return 0; 4825 if (udev->state != USB_STATE_DEFAULT) 4826 return -EINVAL; 4827 4828 return hcd->driver->enable_device(hcd, udev); 4829 } 4830 4831 /* 4832 * Get the bMaxPacketSize0 value during initialization by reading the 4833 * device's device descriptor. Since we don't already know this value, 4834 * the transfer is unsafe and it ignores I/O errors, only testing for 4835 * reasonable received values. 4836 * 4837 * For "old scheme" initialization, size will be 8 so we read just the 4838 * start of the device descriptor, which should work okay regardless of 4839 * the actual bMaxPacketSize0 value. For "new scheme" initialization, 4840 * size will be 64 (and buf will point to a sufficiently large buffer), 4841 * which might not be kosher according to the USB spec but it's what 4842 * Windows does and what many devices expect. 4843 * 4844 * Returns: bMaxPacketSize0 or a negative error code. 4845 */ 4846 static int get_bMaxPacketSize0(struct usb_device *udev, 4847 struct usb_device_descriptor *buf, int size, bool first_time) 4848 { 4849 int i, rc; 4850 4851 /* 4852 * Retry on all errors; some devices are flakey. 4853 * 255 is for WUSB devices, we actually need to use 4854 * 512 (WUSB1.0[4.8.1]). 4855 */ 4856 for (i = 0; i < GET_MAXPACKET0_TRIES; ++i) { 4857 /* Start with invalid values in case the transfer fails */ 4858 buf->bDescriptorType = buf->bMaxPacketSize0 = 0; 4859 rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 4860 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 4861 USB_DT_DEVICE << 8, 0, 4862 buf, size, 4863 initial_descriptor_timeout); 4864 switch (buf->bMaxPacketSize0) { 4865 case 8: case 16: case 32: case 64: case 9: 4866 if (buf->bDescriptorType == USB_DT_DEVICE) { 4867 rc = buf->bMaxPacketSize0; 4868 break; 4869 } 4870 fallthrough; 4871 default: 4872 if (rc >= 0) 4873 rc = -EPROTO; 4874 break; 4875 } 4876 4877 /* 4878 * Some devices time out if they are powered on 4879 * when already connected. They need a second 4880 * reset, so return early. But only on the first 4881 * attempt, lest we get into a time-out/reset loop. 4882 */ 4883 if (rc > 0 || (rc == -ETIMEDOUT && first_time && 4884 udev->speed > USB_SPEED_FULL)) 4885 break; 4886 } 4887 return rc; 4888 } 4889 4890 #define GET_DESCRIPTOR_BUFSIZE 64 4891 4892 /* Reset device, (re)assign address, get device descriptor. 4893 * Device connection must be stable, no more debouncing needed. 4894 * Returns device in USB_STATE_ADDRESS, except on error. 4895 * 4896 * If this is called for an already-existing device (as part of 4897 * usb_reset_and_verify_device), the caller must own the device lock and 4898 * the port lock. For a newly detected device that is not accessible 4899 * through any global pointers, it's not necessary to lock the device, 4900 * but it is still necessary to lock the port. 4901 * 4902 * For a newly detected device, @dev_descr must be NULL. The device 4903 * descriptor retrieved from the device will then be stored in 4904 * @udev->descriptor. For an already existing device, @dev_descr 4905 * must be non-NULL. The device descriptor will be stored there, 4906 * not in @udev->descriptor, because descriptors for registered 4907 * devices are meant to be immutable. 4908 */ 4909 static int 4910 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, 4911 int retry_counter, struct usb_device_descriptor *dev_descr) 4912 { 4913 struct usb_device *hdev = hub->hdev; 4914 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 4915 struct usb_port *port_dev = hub->ports[port1 - 1]; 4916 int retries, operations, retval, i; 4917 unsigned delay = HUB_SHORT_RESET_TIME; 4918 enum usb_device_speed oldspeed = udev->speed; 4919 const char *speed; 4920 int devnum = udev->devnum; 4921 const char *driver_name; 4922 bool do_new_scheme; 4923 const bool initial = !dev_descr; 4924 int maxp0; 4925 struct usb_device_descriptor *buf, *descr; 4926 4927 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 4928 if (!buf) 4929 return -ENOMEM; 4930 4931 /* root hub ports have a slightly longer reset period 4932 * (from USB 2.0 spec, section 7.1.7.5) 4933 */ 4934 if (!hdev->parent) { 4935 delay = HUB_ROOT_RESET_TIME; 4936 if (port1 == hdev->bus->otg_port) 4937 hdev->bus->b_hnp_enable = 0; 4938 } 4939 4940 /* Some low speed devices have problems with the quick delay, so */ 4941 /* be a bit pessimistic with those devices. RHbug #23670 */ 4942 if (oldspeed == USB_SPEED_LOW) 4943 delay = HUB_LONG_RESET_TIME; 4944 4945 /* Reset the device; full speed may morph to high speed */ 4946 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 4947 retval = hub_port_reset(hub, port1, udev, delay, false); 4948 if (retval < 0) /* error or disconnect */ 4949 goto fail; 4950 /* success, speed is known */ 4951 4952 retval = -ENODEV; 4953 4954 /* Don't allow speed changes at reset, except usb 3.0 to faster */ 4955 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && 4956 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { 4957 dev_dbg(&udev->dev, "device reset changed speed!\n"); 4958 goto fail; 4959 } 4960 oldspeed = udev->speed; 4961 4962 if (initial) { 4963 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 4964 * it's fixed size except for full speed devices. 4965 */ 4966 switch (udev->speed) { 4967 case USB_SPEED_SUPER_PLUS: 4968 case USB_SPEED_SUPER: 4969 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 4970 break; 4971 case USB_SPEED_HIGH: /* fixed at 64 */ 4972 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4973 break; 4974 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 4975 /* to determine the ep0 maxpacket size, try to read 4976 * the device descriptor to get bMaxPacketSize0 and 4977 * then correct our initial guess. 4978 */ 4979 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4980 break; 4981 case USB_SPEED_LOW: /* fixed at 8 */ 4982 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 4983 break; 4984 default: 4985 goto fail; 4986 } 4987 } 4988 4989 speed = usb_speed_string(udev->speed); 4990 4991 /* 4992 * The controller driver may be NULL if the controller device 4993 * is the middle device between platform device and roothub. 4994 * This middle device may not need a device driver due to 4995 * all hardware control can be at platform device driver, this 4996 * platform device is usually a dual-role USB controller device. 4997 */ 4998 if (udev->bus->controller->driver) 4999 driver_name = udev->bus->controller->driver->name; 5000 else 5001 driver_name = udev->bus->sysdev->driver->name; 5002 5003 if (udev->speed < USB_SPEED_SUPER) 5004 dev_info(&udev->dev, 5005 "%s %s USB device number %d using %s\n", 5006 (initial ? "new" : "reset"), speed, 5007 devnum, driver_name); 5008 5009 if (initial) { 5010 /* Set up TT records, if needed */ 5011 if (hdev->tt) { 5012 udev->tt = hdev->tt; 5013 udev->ttport = hdev->ttport; 5014 } else if (udev->speed != USB_SPEED_HIGH 5015 && hdev->speed == USB_SPEED_HIGH) { 5016 if (!hub->tt.hub) { 5017 dev_err(&udev->dev, "parent hub has no TT\n"); 5018 retval = -EINVAL; 5019 goto fail; 5020 } 5021 udev->tt = &hub->tt; 5022 udev->ttport = port1; 5023 } 5024 } 5025 5026 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 5027 * Because device hardware and firmware is sometimes buggy in 5028 * this area, and this is how Linux has done it for ages. 5029 * Change it cautiously. 5030 * 5031 * NOTE: If use_new_scheme() is true we will start by issuing 5032 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 5033 * so it may help with some non-standards-compliant devices. 5034 * Otherwise we start with SET_ADDRESS and then try to read the 5035 * first 8 bytes of the device descriptor to get the ep0 maxpacket 5036 * value. 5037 */ 5038 do_new_scheme = use_new_scheme(udev, retry_counter, port_dev); 5039 5040 for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { 5041 if (hub_port_stop_enumerate(hub, port1, retries)) { 5042 retval = -ENODEV; 5043 break; 5044 } 5045 5046 if (do_new_scheme) { 5047 retval = hub_enable_device(udev); 5048 if (retval < 0) { 5049 dev_err(&udev->dev, 5050 "hub failed to enable device, error %d\n", 5051 retval); 5052 goto fail; 5053 } 5054 5055 maxp0 = get_bMaxPacketSize0(udev, buf, 5056 GET_DESCRIPTOR_BUFSIZE, retries == 0); 5057 if (maxp0 > 0 && !initial && 5058 maxp0 != udev->descriptor.bMaxPacketSize0) { 5059 dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 5060 retval = -ENODEV; 5061 goto fail; 5062 } 5063 5064 retval = hub_port_reset(hub, port1, udev, delay, false); 5065 if (retval < 0) /* error or disconnect */ 5066 goto fail; 5067 if (oldspeed != udev->speed) { 5068 dev_dbg(&udev->dev, 5069 "device reset changed speed!\n"); 5070 retval = -ENODEV; 5071 goto fail; 5072 } 5073 if (maxp0 < 0) { 5074 if (maxp0 != -ENODEV) 5075 dev_err(&udev->dev, "device descriptor read/64, error %d\n", 5076 maxp0); 5077 retval = maxp0; 5078 continue; 5079 } 5080 } 5081 5082 for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) { 5083 retval = hub_set_address(udev, devnum); 5084 if (retval >= 0) 5085 break; 5086 msleep(200); 5087 } 5088 if (retval < 0) { 5089 if (retval != -ENODEV) 5090 dev_err(&udev->dev, "device not accepting address %d, error %d\n", 5091 devnum, retval); 5092 goto fail; 5093 } 5094 if (udev->speed >= USB_SPEED_SUPER) { 5095 devnum = udev->devnum; 5096 dev_info(&udev->dev, 5097 "%s SuperSpeed%s%s USB device number %d using %s\n", 5098 (udev->config) ? "reset" : "new", 5099 (udev->speed == USB_SPEED_SUPER_PLUS) ? 5100 " Plus" : "", 5101 (udev->ssp_rate == USB_SSP_GEN_2x2) ? 5102 " Gen 2x2" : 5103 (udev->ssp_rate == USB_SSP_GEN_2x1) ? 5104 " Gen 2x1" : 5105 (udev->ssp_rate == USB_SSP_GEN_1x2) ? 5106 " Gen 1x2" : "", 5107 devnum, driver_name); 5108 } 5109 5110 /* 5111 * cope with hardware quirkiness: 5112 * - let SET_ADDRESS settle, some device hardware wants it 5113 * - read ep0 maxpacket even for high and low speed, 5114 */ 5115 msleep(10); 5116 5117 if (do_new_scheme) 5118 break; 5119 5120 maxp0 = get_bMaxPacketSize0(udev, buf, 8, retries == 0); 5121 if (maxp0 < 0) { 5122 retval = maxp0; 5123 if (retval != -ENODEV) 5124 dev_err(&udev->dev, 5125 "device descriptor read/8, error %d\n", 5126 retval); 5127 } else { 5128 u32 delay; 5129 5130 if (!initial && maxp0 != udev->descriptor.bMaxPacketSize0) { 5131 dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 5132 retval = -ENODEV; 5133 goto fail; 5134 } 5135 5136 delay = udev->parent->hub_delay; 5137 udev->hub_delay = min_t(u32, delay, 5138 USB_TP_TRANSMISSION_DELAY_MAX); 5139 retval = usb_set_isoch_delay(udev); 5140 if (retval) { 5141 dev_dbg(&udev->dev, 5142 "Failed set isoch delay, error %d\n", 5143 retval); 5144 retval = 0; 5145 } 5146 break; 5147 } 5148 } 5149 if (retval) 5150 goto fail; 5151 5152 /* 5153 * Check the ep0 maxpacket guess and correct it if necessary. 5154 * maxp0 is the value stored in the device descriptor; 5155 * i is the value it encodes (logarithmic for SuperSpeed or greater). 5156 */ 5157 i = maxp0; 5158 if (udev->speed >= USB_SPEED_SUPER) { 5159 if (maxp0 <= 16) 5160 i = 1 << maxp0; 5161 else 5162 i = 0; /* Invalid */ 5163 } 5164 if (usb_endpoint_maxp(&udev->ep0.desc) == i) { 5165 ; /* Initial ep0 maxpacket guess is right */ 5166 } else if (((udev->speed == USB_SPEED_FULL || 5167 udev->speed == USB_SPEED_HIGH) && 5168 (i == 8 || i == 16 || i == 32 || i == 64)) || 5169 (udev->speed >= USB_SPEED_SUPER && i > 0)) { 5170 /* Initial guess is wrong; use the descriptor's value */ 5171 if (udev->speed == USB_SPEED_FULL) 5172 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 5173 else 5174 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 5175 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 5176 usb_ep0_reinit(udev); 5177 } else { 5178 /* Initial guess is wrong and descriptor's value is invalid */ 5179 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", maxp0); 5180 retval = -EMSGSIZE; 5181 goto fail; 5182 } 5183 5184 descr = usb_get_device_descriptor(udev); 5185 if (IS_ERR(descr)) { 5186 retval = PTR_ERR(descr); 5187 if (retval != -ENODEV) 5188 dev_err(&udev->dev, "device descriptor read/all, error %d\n", 5189 retval); 5190 goto fail; 5191 } 5192 if (initial) 5193 udev->descriptor = *descr; 5194 else 5195 *dev_descr = *descr; 5196 kfree(descr); 5197 5198 /* 5199 * Some superspeed devices have finished the link training process 5200 * and attached to a superspeed hub port, but the device descriptor 5201 * got from those devices show they aren't superspeed devices. Warm 5202 * reset the port attached by the devices can fix them. 5203 */ 5204 if ((udev->speed >= USB_SPEED_SUPER) && 5205 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 5206 dev_err(&udev->dev, "got a wrong device descriptor, warm reset device\n"); 5207 hub_port_reset(hub, port1, udev, HUB_BH_RESET_TIME, true); 5208 retval = -EINVAL; 5209 goto fail; 5210 } 5211 5212 usb_detect_quirks(udev); 5213 5214 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 5215 retval = usb_get_bos_descriptor(udev); 5216 if (!retval) { 5217 udev->lpm_capable = usb_device_supports_lpm(udev); 5218 udev->lpm_disable_count = 1; 5219 usb_set_lpm_parameters(udev); 5220 usb_req_set_sel(udev); 5221 } 5222 } 5223 5224 retval = 0; 5225 /* notify HCD that we have a device connected and addressed */ 5226 if (hcd->driver->update_device) 5227 hcd->driver->update_device(hcd, udev); 5228 hub_set_initial_usb2_lpm_policy(udev); 5229 fail: 5230 if (retval) { 5231 hub_port_disable(hub, port1, 0); 5232 update_devnum(udev, devnum); /* for disconnect processing */ 5233 } 5234 kfree(buf); 5235 return retval; 5236 } 5237 5238 static void 5239 check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) 5240 { 5241 struct usb_qualifier_descriptor *qual; 5242 int status; 5243 5244 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) 5245 return; 5246 5247 qual = kmalloc_obj(*qual); 5248 if (qual == NULL) 5249 return; 5250 5251 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0, 5252 qual, sizeof *qual); 5253 if (status == sizeof *qual) { 5254 dev_info(&udev->dev, "not running at top speed; " 5255 "connect to a high speed hub\n"); 5256 /* hub LEDs are probably harder to miss than syslog */ 5257 if (hub->has_indicators) { 5258 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 5259 queue_delayed_work(system_power_efficient_wq, 5260 &hub->leds, 0); 5261 } 5262 } 5263 kfree(qual); 5264 } 5265 5266 static unsigned 5267 hub_power_remaining(struct usb_hub *hub) 5268 { 5269 struct usb_device *hdev = hub->hdev; 5270 int remaining; 5271 int port1; 5272 5273 if (!hub->limited_power) 5274 return 0; 5275 5276 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 5277 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 5278 struct usb_port *port_dev = hub->ports[port1 - 1]; 5279 struct usb_device *udev = port_dev->child; 5280 unsigned unit_load; 5281 int delta; 5282 5283 if (!udev) 5284 continue; 5285 if (hub_is_superspeed(udev)) 5286 unit_load = 150; 5287 else 5288 unit_load = 100; 5289 5290 /* 5291 * Unconfigured devices may not use more than one unit load, 5292 * or 8mA for OTG ports 5293 */ 5294 if (udev->actconfig) 5295 delta = usb_get_max_power(udev, udev->actconfig); 5296 else if (port1 != udev->bus->otg_port || hdev->parent) 5297 delta = unit_load; 5298 else 5299 delta = 8; 5300 if (delta > hub->mA_per_port) 5301 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", 5302 delta, hub->mA_per_port); 5303 remaining -= delta; 5304 } 5305 if (remaining < 0) { 5306 dev_warn(hub->intfdev, "%dmA over power budget!\n", 5307 -remaining); 5308 remaining = 0; 5309 } 5310 return remaining; 5311 } 5312 5313 5314 static int descriptors_changed(struct usb_device *udev, 5315 struct usb_device_descriptor *new_device_descriptor, 5316 struct usb_host_bos *old_bos) 5317 { 5318 int changed = 0; 5319 unsigned index; 5320 unsigned serial_len = 0; 5321 unsigned len; 5322 unsigned old_length; 5323 int length; 5324 char *buf; 5325 5326 if (memcmp(&udev->descriptor, new_device_descriptor, 5327 sizeof(*new_device_descriptor)) != 0) 5328 return 1; 5329 5330 if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) 5331 return 1; 5332 if (udev->bos) { 5333 len = le16_to_cpu(udev->bos->desc->wTotalLength); 5334 if (len != le16_to_cpu(old_bos->desc->wTotalLength)) 5335 return 1; 5336 if (memcmp(udev->bos->desc, old_bos->desc, len)) 5337 return 1; 5338 } 5339 5340 /* Since the idVendor, idProduct, and bcdDevice values in the 5341 * device descriptor haven't changed, we will assume the 5342 * Manufacturer and Product strings haven't changed either. 5343 * But the SerialNumber string could be different (e.g., a 5344 * different flash card of the same brand). 5345 */ 5346 if (udev->serial) 5347 serial_len = strlen(udev->serial) + 1; 5348 5349 len = serial_len; 5350 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5351 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5352 len = max(len, old_length); 5353 } 5354 5355 buf = kmalloc(len, GFP_NOIO); 5356 if (!buf) 5357 /* assume the worst */ 5358 return 1; 5359 5360 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5361 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5362 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 5363 old_length); 5364 if (length != old_length) { 5365 dev_dbg(&udev->dev, "config index %d, error %d\n", 5366 index, length); 5367 changed = 1; 5368 break; 5369 } 5370 if (memcmp(buf, udev->rawdescriptors[index], old_length) 5371 != 0) { 5372 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 5373 index, 5374 ((struct usb_config_descriptor *) buf)-> 5375 bConfigurationValue); 5376 changed = 1; 5377 break; 5378 } 5379 } 5380 5381 if (!changed && serial_len) { 5382 length = usb_string(udev, udev->descriptor.iSerialNumber, 5383 buf, serial_len); 5384 if (length + 1 != serial_len) { 5385 dev_dbg(&udev->dev, "serial string error %d\n", 5386 length); 5387 changed = 1; 5388 } else if (memcmp(buf, udev->serial, length) != 0) { 5389 dev_dbg(&udev->dev, "serial string changed\n"); 5390 changed = 1; 5391 } 5392 } 5393 5394 kfree(buf); 5395 return changed; 5396 } 5397 5398 static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, 5399 u16 portchange) 5400 { 5401 int status = -ENODEV; 5402 int i; 5403 unsigned unit_load; 5404 struct usb_device *hdev = hub->hdev; 5405 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 5406 struct usb_port *port_dev = hub->ports[port1 - 1]; 5407 struct usb_device *udev = port_dev->child; 5408 static int unreliable_port = -1; 5409 bool retry_locked; 5410 5411 /* Disconnect any existing devices under this port */ 5412 if (udev) { 5413 if (hcd->usb_phy && !hdev->parent) 5414 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); 5415 usb_disconnect(&port_dev->child); 5416 } 5417 5418 /* We can forget about a "removed" device when there's a physical 5419 * disconnect or the connect status changes. 5420 */ 5421 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 5422 (portchange & USB_PORT_STAT_C_CONNECTION)) 5423 clear_bit(port1, hub->removed_bits); 5424 5425 if (portchange & (USB_PORT_STAT_C_CONNECTION | 5426 USB_PORT_STAT_C_ENABLE)) { 5427 status = hub_port_debounce_be_stable(hub, port1); 5428 if (status < 0) { 5429 if (status != -ENODEV && 5430 port1 != unreliable_port && 5431 printk_ratelimit()) 5432 dev_err(&port_dev->dev, "connect-debounce failed\n"); 5433 portstatus &= ~USB_PORT_STAT_CONNECTION; 5434 unreliable_port = port1; 5435 } else { 5436 portstatus = status; 5437 } 5438 } 5439 5440 /* Return now if debouncing failed or nothing is connected or 5441 * the device was "removed". 5442 */ 5443 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 5444 test_bit(port1, hub->removed_bits)) { 5445 5446 /* 5447 * maybe switch power back on (e.g. root hub was reset) 5448 * but only if the port isn't owned by someone else. 5449 */ 5450 if (hub_is_port_power_switchable(hub) 5451 && !usb_port_is_power_on(hub, portstatus) 5452 && !port_dev->port_owner) 5453 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 5454 5455 if (portstatus & USB_PORT_STAT_ENABLE) 5456 goto done; 5457 return; 5458 } 5459 if (hub_is_superspeed(hub->hdev)) 5460 unit_load = 150; 5461 else 5462 unit_load = 100; 5463 5464 status = 0; 5465 5466 for (i = 0; i < PORT_INIT_TRIES; i++) { 5467 if (hub_port_stop_enumerate(hub, port1, i)) { 5468 status = -ENODEV; 5469 break; 5470 } 5471 5472 usb_lock_port(port_dev); 5473 mutex_lock(hcd->address0_mutex); 5474 retry_locked = true; 5475 /* reallocate for each attempt, since references 5476 * to the previous one can escape in various ways 5477 */ 5478 udev = usb_alloc_dev(hdev, hdev->bus, port1); 5479 if (!udev) { 5480 dev_err(&port_dev->dev, 5481 "couldn't allocate usb_device\n"); 5482 mutex_unlock(hcd->address0_mutex); 5483 usb_unlock_port(port_dev); 5484 goto done; 5485 } 5486 5487 usb_set_device_state(udev, USB_STATE_POWERED); 5488 udev->bus_mA = hub->mA_per_port; 5489 udev->level = hdev->level + 1; 5490 5491 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ 5492 if (hub_is_superspeed(hub->hdev)) 5493 udev->speed = USB_SPEED_SUPER; 5494 else 5495 udev->speed = USB_SPEED_UNKNOWN; 5496 5497 choose_devnum(udev); 5498 if (udev->devnum <= 0) { 5499 status = -ENOTCONN; /* Don't retry */ 5500 goto loop; 5501 } 5502 5503 /* reset (non-USB 3.0 devices) and get descriptor */ 5504 status = hub_port_init(hub, udev, port1, i, NULL); 5505 if (status < 0) 5506 goto loop; 5507 5508 mutex_unlock(hcd->address0_mutex); 5509 usb_unlock_port(port_dev); 5510 retry_locked = false; 5511 5512 if (udev->quirks & USB_QUIRK_DELAY_INIT) 5513 msleep(2000); 5514 5515 /* consecutive bus-powered hubs aren't reliable; they can 5516 * violate the voltage drop budget. if the new child has 5517 * a "powered" LED, users should notice we didn't enable it 5518 * (without reading syslog), even without per-port LEDs 5519 * on the parent. 5520 */ 5521 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 5522 && udev->bus_mA <= unit_load) { 5523 u16 devstat; 5524 5525 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, 5526 &devstat); 5527 if (status) { 5528 dev_dbg(&udev->dev, "get status %d ?\n", status); 5529 goto loop_disable; 5530 } 5531 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 5532 dev_err(&udev->dev, 5533 "can't connect bus-powered hub " 5534 "to this port\n"); 5535 if (hub->has_indicators) { 5536 hub->indicator[port1-1] = 5537 INDICATOR_AMBER_BLINK; 5538 queue_delayed_work( 5539 system_power_efficient_wq, 5540 &hub->leds, 0); 5541 } 5542 status = -ENOTCONN; /* Don't retry */ 5543 goto loop_disable; 5544 } 5545 } 5546 5547 /* check for devices running slower than they could */ 5548 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 5549 && udev->speed == USB_SPEED_FULL 5550 && highspeed_hubs != 0) 5551 check_highspeed(hub, udev, port1); 5552 5553 /* Store the parent's children[] pointer. At this point 5554 * udev becomes globally accessible, although presumably 5555 * no one will look at it until hdev is unlocked. 5556 */ 5557 status = 0; 5558 5559 mutex_lock(&usb_port_peer_mutex); 5560 5561 /* We mustn't add new devices if the parent hub has 5562 * been disconnected; we would race with the 5563 * recursively_mark_NOTATTACHED() routine. 5564 */ 5565 spin_lock_irq(&device_state_lock); 5566 if (hdev->state == USB_STATE_NOTATTACHED) 5567 status = -ENOTCONN; 5568 else 5569 port_dev->child = udev; 5570 spin_unlock_irq(&device_state_lock); 5571 mutex_unlock(&usb_port_peer_mutex); 5572 5573 /* Run it through the hoops (find a driver, etc) */ 5574 if (!status) { 5575 status = usb_new_device(udev); 5576 if (status) { 5577 mutex_lock(&usb_port_peer_mutex); 5578 spin_lock_irq(&device_state_lock); 5579 port_dev->child = NULL; 5580 spin_unlock_irq(&device_state_lock); 5581 mutex_unlock(&usb_port_peer_mutex); 5582 } else { 5583 if (hcd->usb_phy && !hdev->parent) 5584 usb_phy_notify_connect(hcd->usb_phy, 5585 udev->speed); 5586 } 5587 } 5588 5589 if (status) 5590 goto loop_disable; 5591 5592 status = hub_power_remaining(hub); 5593 if (status) 5594 dev_dbg(hub->intfdev, "%dmA power budget left\n", status); 5595 5596 return; 5597 5598 loop_disable: 5599 hub_port_disable(hub, port1, 1); 5600 loop: 5601 usb_ep0_reinit(udev); 5602 release_devnum(udev); 5603 hub_free_dev(udev); 5604 if (retry_locked) { 5605 mutex_unlock(hcd->address0_mutex); 5606 usb_unlock_port(port_dev); 5607 } 5608 usb_put_dev(udev); 5609 if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 5610 break; 5611 5612 /* When halfway through our retry count, power-cycle the port */ 5613 if (i == (PORT_INIT_TRIES - 1) / 2) { 5614 dev_info(&port_dev->dev, "attempt power cycle\n"); 5615 usb_hub_set_port_power(hdev, hub, port1, false); 5616 msleep(2 * hub_power_on_good_delay(hub)); 5617 usb_hub_set_port_power(hdev, hub, port1, true); 5618 msleep(hub_power_on_good_delay(hub)); 5619 } 5620 } 5621 if (hub->hdev->parent || 5622 !hcd->driver->port_handed_over || 5623 !(hcd->driver->port_handed_over)(hcd, port1)) { 5624 if (status != -ENOTCONN && status != -ENODEV) 5625 dev_err(&port_dev->dev, 5626 "unable to enumerate USB device\n"); 5627 } 5628 5629 done: 5630 hub_port_disable(hub, port1, 1); 5631 if (hcd->driver->relinquish_port && !hub->hdev->parent) { 5632 if (status != -ENOTCONN && status != -ENODEV) 5633 hcd->driver->relinquish_port(hcd, port1); 5634 } 5635 } 5636 5637 /* Handle physical or logical connection change events. 5638 * This routine is called when: 5639 * a port connection-change occurs; 5640 * a port enable-change occurs (often caused by EMI); 5641 * usb_reset_and_verify_device() encounters changed descriptors (as from 5642 * a firmware download) 5643 * caller already locked the hub 5644 */ 5645 static void hub_port_connect_change(struct usb_hub *hub, int port1, 5646 u16 portstatus, u16 portchange) 5647 __must_hold(&port_dev->status_lock) 5648 { 5649 struct usb_port *port_dev = hub->ports[port1 - 1]; 5650 struct usb_device *udev = port_dev->child; 5651 struct usb_device_descriptor *descr; 5652 int status = -ENODEV; 5653 5654 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, 5655 portchange, portspeed(hub, portstatus)); 5656 5657 if (hub->has_indicators) { 5658 set_port_led(hub, port1, HUB_LED_AUTO); 5659 hub->indicator[port1-1] = INDICATOR_AUTO; 5660 } 5661 5662 #ifdef CONFIG_USB_OTG 5663 /* during HNP, don't repeat the debounce */ 5664 if (hub->hdev->bus->is_b_host) 5665 portchange &= ~(USB_PORT_STAT_C_CONNECTION | 5666 USB_PORT_STAT_C_ENABLE); 5667 #endif 5668 5669 /* Try to resuscitate an existing device */ 5670 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 5671 udev->state != USB_STATE_NOTATTACHED) { 5672 if (portstatus & USB_PORT_STAT_ENABLE) { 5673 /* 5674 * USB-3 connections are initialized automatically by 5675 * the hostcontroller hardware. Therefore check for 5676 * changed device descriptors before resuscitating the 5677 * device. 5678 */ 5679 descr = usb_get_device_descriptor(udev); 5680 if (IS_ERR(descr)) { 5681 dev_dbg(&udev->dev, 5682 "can't read device descriptor %ld\n", 5683 PTR_ERR(descr)); 5684 } else { 5685 if (descriptors_changed(udev, descr, 5686 udev->bos)) { 5687 dev_dbg(&udev->dev, 5688 "device descriptor has changed\n"); 5689 } else { 5690 status = 0; /* Nothing to do */ 5691 } 5692 kfree(descr); 5693 } 5694 #ifdef CONFIG_PM 5695 } else if (udev->state == USB_STATE_SUSPENDED && 5696 udev->persist_enabled) { 5697 /* For a suspended device, treat this as a 5698 * remote wakeup event. 5699 */ 5700 usb_unlock_port(port_dev); 5701 status = usb_remote_wakeup(udev); 5702 usb_lock_port(port_dev); 5703 #endif 5704 } else { 5705 /* Don't resuscitate */; 5706 } 5707 } 5708 clear_bit(port1, hub->change_bits); 5709 5710 /* successfully revalidated the connection */ 5711 if (status == 0) 5712 return; 5713 5714 usb_unlock_port(port_dev); 5715 hub_port_connect(hub, port1, portstatus, portchange); 5716 usb_lock_port(port_dev); 5717 } 5718 5719 /* Handle notifying userspace about hub over-current events */ 5720 static void port_over_current_notify(struct usb_port *port_dev) 5721 { 5722 char *envp[3] = { NULL, NULL, NULL }; 5723 struct device *hub_dev; 5724 char *port_dev_path; 5725 5726 sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); 5727 5728 hub_dev = port_dev->dev.parent; 5729 5730 if (!hub_dev) 5731 return; 5732 5733 port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); 5734 if (!port_dev_path) 5735 return; 5736 5737 envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path); 5738 if (!envp[0]) 5739 goto exit; 5740 5741 envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u", 5742 port_dev->over_current_count); 5743 if (!envp[1]) 5744 goto exit; 5745 5746 kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); 5747 5748 exit: 5749 kfree(envp[1]); 5750 kfree(envp[0]); 5751 kfree(port_dev_path); 5752 } 5753 5754 static void port_event(struct usb_hub *hub, int port1) 5755 __must_hold(&port_dev->status_lock) 5756 { 5757 int connect_change; 5758 struct usb_port *port_dev = hub->ports[port1 - 1]; 5759 struct usb_device *udev = port_dev->child; 5760 struct usb_device *hdev = hub->hdev; 5761 u16 portstatus, portchange; 5762 int i = 0; 5763 int err; 5764 5765 connect_change = test_bit(port1, hub->change_bits); 5766 clear_bit(port1, hub->event_bits); 5767 clear_bit(port1, hub->wakeup_bits); 5768 5769 if (usb_hub_port_status(hub, port1, &portstatus, &portchange) < 0) 5770 return; 5771 5772 if (portchange & USB_PORT_STAT_C_CONNECTION) { 5773 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 5774 connect_change = 1; 5775 } 5776 5777 if (portchange & USB_PORT_STAT_C_ENABLE) { 5778 if (!connect_change) 5779 dev_dbg(&port_dev->dev, "enable change, status %08x\n", 5780 portstatus); 5781 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 5782 5783 /* 5784 * EM interference sometimes causes badly shielded USB devices 5785 * to be shutdown by the hub, this hack enables them again. 5786 * Works at least with mouse driver. 5787 */ 5788 if (!(portstatus & USB_PORT_STAT_ENABLE) 5789 && !connect_change && udev) { 5790 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); 5791 connect_change = 1; 5792 } 5793 } 5794 5795 if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 5796 u16 status = 0, unused; 5797 port_dev->over_current_count++; 5798 port_over_current_notify(port_dev); 5799 5800 dev_dbg(&port_dev->dev, "over-current change #%u\n", 5801 port_dev->over_current_count); 5802 usb_clear_port_feature(hdev, port1, 5803 USB_PORT_FEAT_C_OVER_CURRENT); 5804 msleep(100); /* Cool down */ 5805 hub_power_on(hub, true); 5806 usb_hub_port_status(hub, port1, &status, &unused); 5807 if (status & USB_PORT_STAT_OVERCURRENT) 5808 dev_err(&port_dev->dev, "over-current condition\n"); 5809 } 5810 5811 if (portchange & USB_PORT_STAT_C_RESET) { 5812 dev_dbg(&port_dev->dev, "reset change\n"); 5813 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET); 5814 } 5815 if ((portchange & USB_PORT_STAT_C_BH_RESET) 5816 && hub_is_superspeed(hdev)) { 5817 dev_dbg(&port_dev->dev, "warm reset change\n"); 5818 usb_clear_port_feature(hdev, port1, 5819 USB_PORT_FEAT_C_BH_PORT_RESET); 5820 } 5821 if (portchange & USB_PORT_STAT_C_LINK_STATE) { 5822 dev_dbg(&port_dev->dev, "link state change\n"); 5823 usb_clear_port_feature(hdev, port1, 5824 USB_PORT_FEAT_C_PORT_LINK_STATE); 5825 } 5826 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 5827 dev_warn(&port_dev->dev, "config error\n"); 5828 usb_clear_port_feature(hdev, port1, 5829 USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 5830 } 5831 5832 /* skip port actions that require the port to be powered on */ 5833 if (!pm_runtime_active(&port_dev->dev)) 5834 return; 5835 5836 /* skip port actions if ignore_event and early_stop are true */ 5837 if (port_dev->ignore_event && port_dev->early_stop) 5838 return; 5839 5840 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) 5841 connect_change = 1; 5842 5843 /* 5844 * Avoid trying to recover a USB3 SS.Inactive port with a warm reset if 5845 * the device was disconnected. A 12ms disconnect detect timer in 5846 * SS.Inactive state transitions the port to RxDetect automatically. 5847 * SS.Inactive link error state is common during device disconnect. 5848 */ 5849 while (hub_port_warm_reset_required(hub, port1, portstatus)) { 5850 if ((i++ < DETECT_DISCONNECT_TRIES) && udev) { 5851 u16 unused; 5852 5853 msleep(20); 5854 usb_hub_port_status(hub, port1, &portstatus, &unused); 5855 dev_dbg(&port_dev->dev, "Wait for inactive link disconnect detect\n"); 5856 continue; 5857 } else if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION) 5858 || udev->state == USB_STATE_NOTATTACHED) { 5859 dev_dbg(&port_dev->dev, "do warm reset, port only\n"); 5860 err = hub_port_reset(hub, port1, NULL, 5861 HUB_BH_RESET_TIME, true); 5862 if (!udev && err == -ENOTCONN) 5863 connect_change = 0; 5864 else if (err < 0) 5865 hub_port_disable(hub, port1, 1); 5866 } else { 5867 dev_dbg(&port_dev->dev, "do warm reset, full device\n"); 5868 usb_unlock_port(port_dev); 5869 usb_lock_device(udev); 5870 usb_reset_device(udev); 5871 usb_unlock_device(udev); 5872 usb_lock_port(port_dev); 5873 connect_change = 0; 5874 } 5875 break; 5876 } 5877 5878 if (connect_change) 5879 hub_port_connect_change(hub, port1, portstatus, portchange); 5880 } 5881 5882 static void hub_event(struct work_struct *work) 5883 { 5884 struct usb_device *hdev; 5885 struct usb_interface *intf; 5886 struct usb_hub *hub; 5887 struct device *hub_dev; 5888 u16 hubstatus; 5889 u16 hubchange; 5890 int i, ret; 5891 5892 hub = container_of(work, struct usb_hub, events); 5893 hdev = hub->hdev; 5894 hub_dev = hub->intfdev; 5895 intf = to_usb_interface(hub_dev); 5896 5897 kcov_remote_start_usb((u64)hdev->bus->busnum); 5898 5899 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 5900 hdev->state, hdev->maxchild, 5901 /* NOTE: expects max 15 ports... */ 5902 (u16) hub->change_bits[0], 5903 (u16) hub->event_bits[0]); 5904 5905 /* Lock the device, then check to see if we were 5906 * disconnected while waiting for the lock to succeed. */ 5907 usb_lock_device(hdev); 5908 if (unlikely(hub->disconnected)) 5909 goto out_hdev_lock; 5910 5911 /* If the hub has died, clean up after it */ 5912 if (hdev->state == USB_STATE_NOTATTACHED) { 5913 hub->error = -ENODEV; 5914 hub_quiesce(hub, HUB_DISCONNECT); 5915 goto out_hdev_lock; 5916 } 5917 5918 /* Autoresume */ 5919 ret = usb_autopm_get_interface(intf); 5920 if (ret) { 5921 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 5922 goto out_hdev_lock; 5923 } 5924 5925 /* If this is an inactive hub, do nothing */ 5926 if (hub->quiescing) 5927 goto out_autopm; 5928 5929 if (hub->error) { 5930 dev_dbg(hub_dev, "resetting for error %d\n", hub->error); 5931 5932 ret = usb_reset_device(hdev); 5933 if (ret) { 5934 dev_dbg(hub_dev, "error resetting hub: %d\n", ret); 5935 goto out_autopm; 5936 } 5937 5938 hub->nerrors = 0; 5939 hub->error = 0; 5940 } 5941 5942 /* deal with port status changes */ 5943 for (i = 1; i <= hdev->maxchild; i++) { 5944 struct usb_port *port_dev = hub->ports[i - 1]; 5945 5946 if (test_bit(i, hub->event_bits) 5947 || test_bit(i, hub->change_bits) 5948 || test_bit(i, hub->wakeup_bits)) { 5949 /* 5950 * The get_noresume and barrier ensure that if 5951 * the port was in the process of resuming, we 5952 * flush that work and keep the port active for 5953 * the duration of the port_event(). However, 5954 * if the port is runtime pm suspended 5955 * (powered-off), we leave it in that state, run 5956 * an abbreviated port_event(), and move on. 5957 */ 5958 pm_runtime_get_noresume(&port_dev->dev); 5959 pm_runtime_barrier(&port_dev->dev); 5960 usb_lock_port(port_dev); 5961 port_event(hub, i); 5962 usb_unlock_port(port_dev); 5963 pm_runtime_put_sync(&port_dev->dev); 5964 } 5965 } 5966 5967 /* deal with hub status changes */ 5968 if (test_and_clear_bit(0, hub->event_bits) == 0) 5969 ; /* do nothing */ 5970 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 5971 dev_err(hub_dev, "get_hub_status failed\n"); 5972 else { 5973 if (hubchange & HUB_CHANGE_LOCAL_POWER) { 5974 dev_dbg(hub_dev, "power change\n"); 5975 clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 5976 if (hubstatus & HUB_STATUS_LOCAL_POWER) 5977 /* FIXME: Is this always true? */ 5978 hub->limited_power = 1; 5979 else 5980 hub->limited_power = 0; 5981 } 5982 if (hubchange & HUB_CHANGE_OVERCURRENT) { 5983 u16 status = 0; 5984 u16 unused; 5985 5986 dev_dbg(hub_dev, "over-current change\n"); 5987 clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 5988 msleep(500); /* Cool down */ 5989 hub_power_on(hub, true); 5990 hub_hub_status(hub, &status, &unused); 5991 if (status & HUB_STATUS_OVERCURRENT) 5992 dev_err(hub_dev, "over-current condition\n"); 5993 } 5994 } 5995 5996 out_autopm: 5997 /* Balance the usb_autopm_get_interface() above */ 5998 usb_autopm_put_interface_no_suspend(intf); 5999 out_hdev_lock: 6000 usb_unlock_device(hdev); 6001 6002 /* Balance the stuff in kick_hub_wq() and allow autosuspend */ 6003 usb_autopm_put_interface(intf); 6004 hub_put(hub); 6005 6006 kcov_remote_stop(); 6007 } 6008 6009 static const struct usb_device_id hub_id_table[] = { 6010 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6011 | USB_DEVICE_ID_MATCH_PRODUCT 6012 | USB_DEVICE_ID_MATCH_INT_CLASS, 6013 .idVendor = USB_VENDOR_SMSC, 6014 .idProduct = USB_PRODUCT_USB5534B, 6015 .bInterfaceClass = USB_CLASS_HUB, 6016 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 6017 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6018 | USB_DEVICE_ID_MATCH_PRODUCT, 6019 .idVendor = USB_VENDOR_CYPRESS, 6020 .idProduct = USB_PRODUCT_CY7C65632, 6021 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 6022 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6023 | USB_DEVICE_ID_MATCH_INT_CLASS, 6024 .idVendor = USB_VENDOR_GENESYS_LOGIC, 6025 .bInterfaceClass = USB_CLASS_HUB, 6026 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, 6027 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6028 | USB_DEVICE_ID_MATCH_PRODUCT, 6029 .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS, 6030 .idProduct = USB_PRODUCT_TUSB8041_USB2, 6031 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 6032 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6033 | USB_DEVICE_ID_MATCH_PRODUCT, 6034 .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS, 6035 .idProduct = USB_PRODUCT_TUSB8041_USB3, 6036 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 6037 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6038 | USB_DEVICE_ID_MATCH_PRODUCT, 6039 .idVendor = USB_VENDOR_MICROCHIP, 6040 .idProduct = USB_PRODUCT_USB4913, 6041 .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 6042 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6043 | USB_DEVICE_ID_MATCH_PRODUCT, 6044 .idVendor = USB_VENDOR_MICROCHIP, 6045 .idProduct = USB_PRODUCT_USB4914, 6046 .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 6047 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 6048 | USB_DEVICE_ID_MATCH_PRODUCT, 6049 .idVendor = USB_VENDOR_MICROCHIP, 6050 .idProduct = USB_PRODUCT_USB4915, 6051 .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 6052 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 6053 .bDeviceClass = USB_CLASS_HUB}, 6054 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 6055 .bInterfaceClass = USB_CLASS_HUB}, 6056 { } /* Terminating entry */ 6057 }; 6058 6059 MODULE_DEVICE_TABLE(usb, hub_id_table); 6060 6061 static struct usb_driver hub_driver = { 6062 .name = "hub", 6063 .probe = hub_probe, 6064 .disconnect = hub_disconnect, 6065 .suspend = hub_suspend, 6066 .resume = hub_resume, 6067 .reset_resume = hub_reset_resume, 6068 .pre_reset = hub_pre_reset, 6069 .post_reset = hub_post_reset, 6070 .unlocked_ioctl = hub_ioctl, 6071 .id_table = hub_id_table, 6072 .supports_autosuspend = 1, 6073 }; 6074 6075 int usb_hub_init(void) 6076 { 6077 if (usb_register(&hub_driver) < 0) { 6078 printk(KERN_ERR "%s: can't register hub driver\n", 6079 usbcore_name); 6080 return -1; 6081 } 6082 6083 /* 6084 * The workqueue needs to be freezable to avoid interfering with 6085 * USB-PERSIST port handover. Otherwise it might see that a full-speed 6086 * device was gone before the EHCI controller had handed its port 6087 * over to the companion full-speed controller. 6088 */ 6089 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE | WQ_PERCPU, 0); 6090 if (hub_wq) 6091 return 0; 6092 6093 /* Fall through if kernel_thread failed */ 6094 usb_deregister(&hub_driver); 6095 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); 6096 6097 return -1; 6098 } 6099 6100 void usb_hub_cleanup(void) 6101 { 6102 destroy_workqueue(hub_wq); 6103 6104 /* 6105 * Hub resources are freed for us by usb_deregister. It calls 6106 * usb_driver_purge on every device which in turn calls that 6107 * devices disconnect function if it is using this driver. 6108 * The hub_disconnect function takes care of releasing the 6109 * individual hub resources. -greg 6110 */ 6111 usb_deregister(&hub_driver); 6112 } /* usb_hub_cleanup() */ 6113 6114 /** 6115 * hub_hc_release_resources - clear resources used by host controller 6116 * @udev: pointer to device being released 6117 * 6118 * Context: task context, might sleep 6119 * 6120 * Function releases the host controller resources in correct order before 6121 * making any operation on resuming usb device. The host controller resources 6122 * allocated for devices in tree should be released starting from the last 6123 * usb device in tree toward the root hub. This function is used only during 6124 * resuming device when usb device require reinitialization – that is, when 6125 * flag udev->reset_resume is set. 6126 * 6127 * This call is synchronous, and may not be used in an interrupt context. 6128 */ 6129 static void hub_hc_release_resources(struct usb_device *udev) 6130 { 6131 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 6132 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 6133 int i; 6134 6135 /* Release up resources for all children before this device */ 6136 for (i = 0; i < udev->maxchild; i++) 6137 if (hub->ports[i]->child) 6138 hub_hc_release_resources(hub->ports[i]->child); 6139 6140 if (hcd->driver->reset_device) 6141 hcd->driver->reset_device(hcd, udev); 6142 } 6143 6144 /** 6145 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 6146 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 6147 * 6148 * WARNING - don't use this routine to reset a composite device 6149 * (one with multiple interfaces owned by separate drivers)! 6150 * Use usb_reset_device() instead. 6151 * 6152 * Do a port reset, reassign the device's address, and establish its 6153 * former operating configuration. If the reset fails, or the device's 6154 * descriptors change from their values before the reset, or the original 6155 * configuration and altsettings cannot be restored, a flag will be set 6156 * telling hub_wq to pretend the device has been disconnected and then 6157 * re-connected. All drivers will be unbound, and the device will be 6158 * re-enumerated and probed all over again. 6159 * 6160 * Return: 0 if the reset succeeded, -ENODEV if the device has been 6161 * flagged for logical disconnection, or some other negative error code 6162 * if the reset wasn't even attempted. 6163 * 6164 * Note: 6165 * The caller must own the device lock and the port lock, the latter is 6166 * taken by usb_reset_device(). For example, it's safe to use 6167 * usb_reset_device() from a driver probe() routine after downloading 6168 * new firmware. For calls that might not occur during probe(), drivers 6169 * should lock the device using usb_lock_device_for_reset(). 6170 * 6171 * Locking exception: This routine may also be called from within an 6172 * autoresume handler. Such usage won't conflict with other tasks 6173 * holding the device lock because these tasks should always call 6174 * usb_autopm_resume_device(), thereby preventing any unwanted 6175 * autoresume. The autoresume handler is expected to have already 6176 * acquired the port lock before calling this routine. 6177 */ 6178 static int usb_reset_and_verify_device(struct usb_device *udev) 6179 { 6180 struct usb_device *parent_hdev = udev->parent; 6181 struct usb_hub *parent_hub; 6182 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 6183 struct usb_device_descriptor descriptor; 6184 struct usb_interface *intf; 6185 struct usb_host_bos *bos; 6186 int i, j, ret = 0; 6187 int port1 = udev->portnum; 6188 6189 if (udev->state == USB_STATE_NOTATTACHED || 6190 udev->state == USB_STATE_SUSPENDED) { 6191 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 6192 udev->state); 6193 return -EINVAL; 6194 } 6195 6196 if (!parent_hdev) 6197 return -EISDIR; 6198 6199 parent_hub = usb_hub_to_struct_hub(parent_hdev); 6200 6201 /* Disable USB2 hardware LPM. 6202 * It will be re-enabled by the enumeration process. 6203 */ 6204 usb_disable_usb2_hardware_lpm(udev); 6205 6206 bos = udev->bos; 6207 udev->bos = NULL; 6208 6209 if (udev->reset_resume) 6210 hub_hc_release_resources(udev); 6211 6212 mutex_lock(hcd->address0_mutex); 6213 6214 for (i = 0; i < PORT_INIT_TRIES; ++i) { 6215 if (hub_port_stop_enumerate(parent_hub, port1, i)) { 6216 ret = -ENODEV; 6217 break; 6218 } 6219 6220 /* ep0 maxpacket size may change; let the HCD know about it. 6221 * Other endpoints will be handled by re-enumeration. */ 6222 usb_ep0_reinit(udev); 6223 ret = hub_port_init(parent_hub, udev, port1, i, &descriptor); 6224 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 6225 break; 6226 } 6227 mutex_unlock(hcd->address0_mutex); 6228 6229 if (ret < 0) 6230 goto re_enumerate; 6231 6232 /* Device might have changed firmware (DFU or similar) */ 6233 if (descriptors_changed(udev, &descriptor, bos)) { 6234 dev_info(&udev->dev, "device firmware changed\n"); 6235 goto re_enumerate; 6236 } 6237 6238 /* Restore the device's previous configuration */ 6239 if (!udev->actconfig) 6240 goto done; 6241 6242 /* 6243 * Some devices can't handle setting default altsetting 0 with a 6244 * Set-Interface request. Disable host-side endpoints of those 6245 * interfaces here. Enable and reset them back after host has set 6246 * its internal endpoint structures during usb_hcd_alloc_bandwith() 6247 */ 6248 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 6249 intf = udev->actconfig->interface[i]; 6250 if (intf->cur_altsetting->desc.bAlternateSetting == 0) 6251 usb_disable_interface(udev, intf, true); 6252 } 6253 6254 mutex_lock(hcd->bandwidth_mutex); 6255 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 6256 if (ret < 0) { 6257 dev_warn(&udev->dev, 6258 "Busted HC? Not enough HCD resources for " 6259 "old configuration.\n"); 6260 mutex_unlock(hcd->bandwidth_mutex); 6261 goto re_enumerate; 6262 } 6263 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 6264 USB_REQ_SET_CONFIGURATION, 0, 6265 udev->actconfig->desc.bConfigurationValue, 0, 6266 NULL, 0, USB_CTRL_SET_TIMEOUT); 6267 if (ret < 0) { 6268 dev_err(&udev->dev, 6269 "can't restore configuration #%d (error=%d)\n", 6270 udev->actconfig->desc.bConfigurationValue, ret); 6271 mutex_unlock(hcd->bandwidth_mutex); 6272 goto re_enumerate; 6273 } 6274 mutex_unlock(hcd->bandwidth_mutex); 6275 usb_set_device_state(udev, USB_STATE_CONFIGURED); 6276 6277 /* Put interfaces back into the same altsettings as before. 6278 * Don't bother to send the Set-Interface request for interfaces 6279 * that were already in altsetting 0; besides being unnecessary, 6280 * many devices can't handle it. Instead just reset the host-side 6281 * endpoint state. 6282 */ 6283 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 6284 struct usb_host_config *config = udev->actconfig; 6285 struct usb_interface_descriptor *desc; 6286 6287 intf = config->interface[i]; 6288 desc = &intf->cur_altsetting->desc; 6289 if (desc->bAlternateSetting == 0) { 6290 usb_enable_interface(udev, intf, true); 6291 ret = 0; 6292 } else { 6293 /* Let the bandwidth allocation function know that this 6294 * device has been reset, and it will have to use 6295 * alternate setting 0 as the current alternate setting. 6296 */ 6297 intf->resetting_device = 1; 6298 ret = usb_set_interface(udev, desc->bInterfaceNumber, 6299 desc->bAlternateSetting); 6300 intf->resetting_device = 0; 6301 } 6302 if (ret < 0) { 6303 dev_err(&udev->dev, "failed to restore interface %d " 6304 "altsetting %d (error=%d)\n", 6305 desc->bInterfaceNumber, 6306 desc->bAlternateSetting, 6307 ret); 6308 goto re_enumerate; 6309 } 6310 /* Resetting also frees any allocated streams */ 6311 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) 6312 intf->cur_altsetting->endpoint[j].streams = 0; 6313 } 6314 6315 done: 6316 /* Now that the alt settings are re-installed, enable LTM and LPM. */ 6317 usb_enable_usb2_hardware_lpm(udev); 6318 usb_unlocked_enable_lpm(udev); 6319 usb_enable_ltm(udev); 6320 usb_release_bos_descriptor(udev); 6321 udev->bos = bos; 6322 return 0; 6323 6324 re_enumerate: 6325 usb_release_bos_descriptor(udev); 6326 udev->bos = bos; 6327 hub_port_logical_disconnect(parent_hub, port1); 6328 return -ENODEV; 6329 } 6330 6331 /** 6332 * usb_reset_device - warn interface drivers and perform a USB port reset 6333 * @udev: device to reset (not in NOTATTACHED state) 6334 * 6335 * Warns all drivers bound to registered interfaces (using their pre_reset 6336 * method), performs the port reset, and then lets the drivers know that 6337 * the reset is over (using their post_reset method). 6338 * 6339 * Return: The same as for usb_reset_and_verify_device(). 6340 * However, if a reset is already in progress (for instance, if a 6341 * driver doesn't have pre_reset() or post_reset() callbacks, and while 6342 * being unbound or re-bound during the ongoing reset its disconnect() 6343 * or probe() routine tries to perform a second, nested reset), the 6344 * routine returns -EINPROGRESS. 6345 * 6346 * Note: 6347 * The caller must own the device lock. For example, it's safe to use 6348 * this from a driver probe() routine after downloading new firmware. 6349 * For calls that might not occur during probe(), drivers should lock 6350 * the device using usb_lock_device_for_reset(). 6351 * 6352 * If an interface is currently being probed or disconnected, we assume 6353 * its driver knows how to handle resets. For all other interfaces, 6354 * if the driver doesn't have pre_reset and post_reset methods then 6355 * we attempt to unbind it and rebind afterward. 6356 */ 6357 int usb_reset_device(struct usb_device *udev) 6358 { 6359 int ret; 6360 int i; 6361 unsigned int noio_flag; 6362 struct usb_port *port_dev; 6363 struct usb_host_config *config = udev->actconfig; 6364 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 6365 6366 if (udev->state == USB_STATE_NOTATTACHED) { 6367 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 6368 udev->state); 6369 return -EINVAL; 6370 } 6371 6372 if (!udev->parent) { 6373 /* this requires hcd-specific logic; see ohci_restart() */ 6374 dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 6375 return -EISDIR; 6376 } 6377 6378 if (udev->reset_in_progress) 6379 return -EINPROGRESS; 6380 udev->reset_in_progress = 1; 6381 6382 port_dev = hub->ports[udev->portnum - 1]; 6383 6384 /* 6385 * Don't allocate memory with GFP_KERNEL in current 6386 * context to avoid possible deadlock if usb mass 6387 * storage interface or usbnet interface(iSCSI case) 6388 * is included in current configuration. The easist 6389 * approach is to do it for every device reset, 6390 * because the device 'memalloc_noio' flag may have 6391 * not been set before reseting the usb device. 6392 */ 6393 noio_flag = memalloc_noio_save(); 6394 6395 /* Prevent autosuspend during the reset */ 6396 usb_autoresume_device(udev); 6397 6398 if (config) { 6399 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 6400 struct usb_interface *cintf = config->interface[i]; 6401 struct usb_driver *drv; 6402 int unbind = 0; 6403 6404 if (cintf->dev.driver) { 6405 drv = to_usb_driver(cintf->dev.driver); 6406 if (drv->pre_reset && drv->post_reset) 6407 unbind = (drv->pre_reset)(cintf); 6408 else if (cintf->condition == 6409 USB_INTERFACE_BOUND) 6410 unbind = 1; 6411 if (unbind) 6412 usb_forced_unbind_intf(cintf); 6413 } 6414 } 6415 } 6416 6417 usb_lock_port(port_dev); 6418 ret = usb_reset_and_verify_device(udev); 6419 usb_unlock_port(port_dev); 6420 6421 if (config) { 6422 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 6423 struct usb_interface *cintf = config->interface[i]; 6424 struct usb_driver *drv; 6425 int rebind = cintf->needs_binding; 6426 6427 if (!rebind && cintf->dev.driver) { 6428 drv = to_usb_driver(cintf->dev.driver); 6429 if (drv->post_reset) 6430 rebind = (drv->post_reset)(cintf); 6431 else if (cintf->condition == 6432 USB_INTERFACE_BOUND) 6433 rebind = 1; 6434 if (rebind) 6435 cintf->needs_binding = 1; 6436 } 6437 } 6438 6439 /* If the reset failed, hub_wq will unbind drivers later */ 6440 if (ret == 0) 6441 usb_unbind_and_rebind_marked_interfaces(udev); 6442 } 6443 6444 usb_autosuspend_device(udev); 6445 memalloc_noio_restore(noio_flag); 6446 udev->reset_in_progress = 0; 6447 return ret; 6448 } 6449 EXPORT_SYMBOL_GPL(usb_reset_device); 6450 6451 6452 /** 6453 * usb_queue_reset_device - Reset a USB device from an atomic context 6454 * @iface: USB interface belonging to the device to reset 6455 * 6456 * This function can be used to reset a USB device from an atomic 6457 * context, where usb_reset_device() won't work (as it blocks). 6458 * 6459 * Doing a reset via this method is functionally equivalent to calling 6460 * usb_reset_device(), except for the fact that it is delayed to a 6461 * workqueue. This means that any drivers bound to other interfaces 6462 * might be unbound, as well as users from usbfs in user space. 6463 * 6464 * Corner cases: 6465 * 6466 * - Scheduling two resets at the same time from two different drivers 6467 * attached to two different interfaces of the same device is 6468 * possible; depending on how the driver attached to each interface 6469 * handles ->pre_reset(), the second reset might happen or not. 6470 * 6471 * - If the reset is delayed so long that the interface is unbound from 6472 * its driver, the reset will be skipped. 6473 * 6474 * - This function can be called during .probe(). It can also be called 6475 * during .disconnect(), but doing so is pointless because the reset 6476 * will not occur. If you really want to reset the device during 6477 * .disconnect(), call usb_reset_device() directly -- but watch out 6478 * for nested unbinding issues! 6479 */ 6480 void usb_queue_reset_device(struct usb_interface *iface) 6481 { 6482 if (schedule_work(&iface->reset_ws)) 6483 usb_get_intf(iface); 6484 } 6485 EXPORT_SYMBOL_GPL(usb_queue_reset_device); 6486 6487 /** 6488 * usb_hub_find_child - Get the pointer of child device 6489 * attached to the port which is specified by @port1. 6490 * @hdev: USB device belonging to the usb hub 6491 * @port1: port num to indicate which port the child device 6492 * is attached to. 6493 * 6494 * USB drivers call this function to get hub's child device 6495 * pointer. 6496 * 6497 * Return: %NULL if input param is invalid and 6498 * child's usb_device pointer if non-NULL. 6499 */ 6500 struct usb_device *usb_hub_find_child(struct usb_device *hdev, 6501 int port1) 6502 { 6503 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6504 6505 if (port1 < 1 || port1 > hdev->maxchild) 6506 return NULL; 6507 return hub->ports[port1 - 1]->child; 6508 } 6509 EXPORT_SYMBOL_GPL(usb_hub_find_child); 6510 6511 void usb_hub_adjust_deviceremovable(struct usb_device *hdev, 6512 struct usb_hub_descriptor *desc) 6513 { 6514 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6515 enum usb_port_connect_type connect_type; 6516 int i; 6517 6518 if (!hub) 6519 return; 6520 6521 if (!hub_is_superspeed(hdev)) { 6522 for (i = 1; i <= hdev->maxchild; i++) { 6523 struct usb_port *port_dev = hub->ports[i - 1]; 6524 6525 connect_type = port_dev->connect_type; 6526 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 6527 u8 mask = 1 << (i%8); 6528 6529 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { 6530 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 6531 desc->u.hs.DeviceRemovable[i/8] |= mask; 6532 } 6533 } 6534 } 6535 } else { 6536 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); 6537 6538 for (i = 1; i <= hdev->maxchild; i++) { 6539 struct usb_port *port_dev = hub->ports[i - 1]; 6540 6541 connect_type = port_dev->connect_type; 6542 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 6543 u16 mask = 1 << i; 6544 6545 if (!(port_removable & mask)) { 6546 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 6547 port_removable |= mask; 6548 } 6549 } 6550 } 6551 6552 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); 6553 } 6554 } 6555 6556 #ifdef CONFIG_ACPI 6557 /** 6558 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle 6559 * @hdev: USB device belonging to the usb hub 6560 * @port1: port num of the port 6561 * 6562 * Return: Port's acpi handle if successful, %NULL if params are 6563 * invalid. 6564 */ 6565 acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, 6566 int port1) 6567 { 6568 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6569 6570 if (!hub) 6571 return NULL; 6572 6573 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); 6574 } 6575 #endif 6576