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