1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB hub driver. 4 * 5 * (C) Copyright 1999 Linus Torvalds 6 * (C) Copyright 1999 Johannes Erdfelt 7 * (C) Copyright 1999 Gregory P. Smith 8 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au) 9 * 10 * Released under the GPLv2 only. 11 */ 12 13 #include <linux/kernel.h> 14 #include <linux/errno.h> 15 #include <linux/module.h> 16 #include <linux/moduleparam.h> 17 #include <linux/completion.h> 18 #include <linux/sched/mm.h> 19 #include <linux/list.h> 20 #include <linux/slab.h> 21 #include <linux/string_choices.h> 22 #include <linux/kcov.h> 23 #include <linux/ioctl.h> 24 #include <linux/usb.h> 25 #include <linux/usbdevice_fs.h> 26 #include <linux/usb/hcd.h> 27 #include <linux/usb/onboard_dev.h> 28 #include <linux/usb/otg.h> 29 #include <linux/usb/quirks.h> 30 #include <linux/workqueue.h> 31 #include <linux/mutex.h> 32 #include <linux/random.h> 33 #include <linux/pm_qos.h> 34 #include <linux/kobject.h> 35 36 #include <linux/bitfield.h> 37 #include <linux/uaccess.h> 38 #include <asm/byteorder.h> 39 40 #include "hub.h" 41 #include "phy.h" 42 #include "otg_productlist.h" 43 44 #define USB_VENDOR_GENESYS_LOGIC 0x05e3 45 #define USB_VENDOR_SMSC 0x0424 46 #define USB_PRODUCT_USB5534B 0x5534 47 #define USB_VENDOR_CYPRESS 0x04b4 48 #define USB_PRODUCT_CY7C65632 0x6570 49 #define USB_VENDOR_TEXAS_INSTRUMENTS 0x0451 50 #define USB_PRODUCT_TUSB8041_USB3 0x8140 51 #define USB_PRODUCT_TUSB8041_USB2 0x8142 52 #define USB_VENDOR_MICROCHIP 0x0424 53 #define USB_PRODUCT_USB4913 0x4913 54 #define USB_PRODUCT_USB4914 0x4914 55 #define USB_PRODUCT_USB4915 0x4915 56 #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND BIT(0) 57 #define HUB_QUIRK_DISABLE_AUTOSUSPEND BIT(1) 58 #define HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL BIT(2) 59 60 #define USB_TP_TRANSMISSION_DELAY 40 /* ns */ 61 #define USB_TP_TRANSMISSION_DELAY_MAX 65535 /* ns */ 62 #define USB_PING_RESPONSE_TIME 400 /* ns */ 63 #define USB_REDUCE_FRAME_INTR_BINTERVAL 9 64 65 /* 66 * The SET_ADDRESS request timeout will be 500 ms when 67 * USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT quirk flag is set. 68 */ 69 #define USB_SHORT_SET_ADDRESS_REQ_TIMEOUT 500 /* ms */ 70 71 /* Protect struct usb_device->state and ->children members 72 * Note: Both are also protected by ->dev.sem, except that ->state can 73 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 74 static DEFINE_SPINLOCK(device_state_lock); 75 76 /* workqueue to process hub events */ 77 static struct workqueue_struct *hub_wq; 78 static void hub_event(struct work_struct *work); 79 80 /* synchronize hub-port add/remove and peering operations */ 81 DEFINE_MUTEX(usb_port_peer_mutex); 82 83 /* cycle leds on hubs that aren't blinking for attention */ 84 static bool blinkenlights; 85 module_param(blinkenlights, bool, S_IRUGO); 86 MODULE_PARM_DESC(blinkenlights, "true to cycle leds on hubs"); 87 88 /* 89 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about 90 * 10 seconds to send reply for the initial 64-byte descriptor request. 91 */ 92 /* define initial 64-byte descriptor request timeout in milliseconds */ 93 static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; 94 module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); 95 MODULE_PARM_DESC(initial_descriptor_timeout, 96 "initial 64-byte descriptor request timeout in milliseconds " 97 "(default 5000 - 5.0 seconds)"); 98 99 /* 100 * As of 2.6.10 we introduce a new USB device initialization scheme which 101 * closely resembles the way Windows works. Hopefully it will be compatible 102 * with a wider range of devices than the old scheme. However some previously 103 * working devices may start giving rise to "device not accepting address" 104 * errors; if that happens the user can try the old scheme by adjusting the 105 * following module parameters. 106 * 107 * For maximum flexibility there are two boolean parameters to control the 108 * hub driver's behavior. On the first initialization attempt, if the 109 * "old_scheme_first" parameter is set then the old scheme will be used, 110 * otherwise the new scheme is used. If that fails and "use_both_schemes" 111 * is set, then the driver will make another attempt, using the other scheme. 112 */ 113 static bool old_scheme_first; 114 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 115 MODULE_PARM_DESC(old_scheme_first, 116 "start with the old device initialization scheme"); 117 118 static bool use_both_schemes = true; 119 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 120 MODULE_PARM_DESC(use_both_schemes, 121 "try the other device initialization scheme if the " 122 "first one fails"); 123 124 /* Mutual exclusion for EHCI CF initialization. This interferes with 125 * port reset on some companion controllers. 126 */ 127 DECLARE_RWSEM(ehci_cf_port_reset_rwsem); 128 EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); 129 130 #define HUB_DEBOUNCE_TIMEOUT 2000 131 #define HUB_DEBOUNCE_STEP 25 132 #define HUB_DEBOUNCE_STABLE 100 133 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 hub_get(hub); 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 hub_put(hub); 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 hub_get(hub); 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 hub_put(hub); 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 str_plural(maxchild)); 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 void hub_get(struct usb_hub *hub) 1763 { 1764 kref_get(&hub->kref); 1765 } 1766 1767 void hub_put(struct usb_hub *hub) 1768 { 1769 kref_put(&hub->kref, hub_release); 1770 } 1771 1772 static unsigned highspeed_hubs; 1773 1774 static void hub_disconnect(struct usb_interface *intf) 1775 { 1776 struct usb_hub *hub = usb_get_intfdata(intf); 1777 struct usb_device *hdev = interface_to_usbdev(intf); 1778 int port1; 1779 1780 /* 1781 * Stop adding new hub events. We do not want to block here and thus 1782 * will not try to remove any pending work item. 1783 */ 1784 hub->disconnected = 1; 1785 1786 /* Disconnect all children and quiesce the hub */ 1787 hub->error = 0; 1788 hub_quiesce(hub, HUB_DISCONNECT); 1789 1790 mutex_lock(&usb_port_peer_mutex); 1791 1792 /* Avoid races with recursively_mark_NOTATTACHED() */ 1793 spin_lock_irq(&device_state_lock); 1794 port1 = hdev->maxchild; 1795 hdev->maxchild = 0; 1796 usb_set_intfdata(intf, NULL); 1797 spin_unlock_irq(&device_state_lock); 1798 1799 for (; port1 > 0; --port1) 1800 usb_hub_remove_port_device(hub, port1); 1801 1802 mutex_unlock(&usb_port_peer_mutex); 1803 1804 if (hub->hdev->speed == USB_SPEED_HIGH) 1805 highspeed_hubs--; 1806 1807 usb_free_urb(hub->urb); 1808 kfree(hub->ports); 1809 kfree(hub->descriptor); 1810 kfree(hub->status); 1811 kfree(hub->buffer); 1812 1813 pm_suspend_ignore_children(&intf->dev, false); 1814 1815 if (hub->quirk_disable_autosuspend) 1816 usb_autopm_put_interface(intf); 1817 1818 onboard_dev_destroy_pdevs(&hub->onboard_devs); 1819 1820 hub_put(hub); 1821 } 1822 1823 static bool hub_descriptor_is_sane(struct usb_host_interface *desc) 1824 { 1825 /* Some hubs have a subclass of 1, which AFAICT according to the */ 1826 /* specs is not defined, but it works */ 1827 if (desc->desc.bInterfaceSubClass != 0 && 1828 desc->desc.bInterfaceSubClass != 1) 1829 return false; 1830 1831 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 1832 if (desc->desc.bNumEndpoints != 1) 1833 return false; 1834 1835 /* If the first endpoint is not interrupt IN, we'd better punt! */ 1836 if (!usb_endpoint_is_int_in(&desc->endpoint[0].desc)) 1837 return false; 1838 1839 return true; 1840 } 1841 1842 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 1843 { 1844 struct usb_host_interface *desc; 1845 struct usb_device *hdev; 1846 struct usb_hub *hub; 1847 1848 desc = intf->cur_altsetting; 1849 hdev = interface_to_usbdev(intf); 1850 1851 /* 1852 * Set default autosuspend delay as 0 to speedup bus suspend, 1853 * based on the below considerations: 1854 * 1855 * - Unlike other drivers, the hub driver does not rely on the 1856 * autosuspend delay to provide enough time to handle a wakeup 1857 * event, and the submitted status URB is just to check future 1858 * change on hub downstream ports, so it is safe to do it. 1859 * 1860 * - The patch might cause one or more auto supend/resume for 1861 * below very rare devices when they are plugged into hub 1862 * first time: 1863 * 1864 * devices having trouble initializing, and disconnect 1865 * themselves from the bus and then reconnect a second 1866 * or so later 1867 * 1868 * devices just for downloading firmware, and disconnects 1869 * themselves after completing it 1870 * 1871 * For these quite rare devices, their drivers may change the 1872 * autosuspend delay of their parent hub in the probe() to one 1873 * appropriate value to avoid the subtle problem if someone 1874 * does care it. 1875 * 1876 * - The patch may cause one or more auto suspend/resume on 1877 * hub during running 'lsusb', but it is probably too 1878 * infrequent to worry about. 1879 * 1880 * - Change autosuspend delay of hub can avoid unnecessary auto 1881 * suspend timer for hub, also may decrease power consumption 1882 * of USB bus. 1883 * 1884 * - If user has indicated to prevent autosuspend by passing 1885 * usbcore.autosuspend = -1 then keep autosuspend disabled. 1886 */ 1887 #ifdef CONFIG_PM 1888 if (hdev->dev.power.autosuspend_delay >= 0) 1889 pm_runtime_set_autosuspend_delay(&hdev->dev, 0); 1890 #endif 1891 1892 /* 1893 * Hubs have proper suspend/resume support, except for root hubs 1894 * where the controller driver doesn't have bus_suspend and 1895 * bus_resume methods. 1896 */ 1897 if (hdev->parent) { /* normal device */ 1898 usb_enable_autosuspend(hdev); 1899 } else { /* root hub */ 1900 const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; 1901 1902 if (drv->bus_suspend && drv->bus_resume) 1903 usb_enable_autosuspend(hdev); 1904 } 1905 1906 if (hdev->level == MAX_TOPO_LEVEL) { 1907 dev_err(&intf->dev, 1908 "Unsupported bus topology: hub nested too deep\n"); 1909 return -E2BIG; 1910 } 1911 1912 #ifdef CONFIG_USB_OTG_DISABLE_EXTERNAL_HUB 1913 if (hdev->parent) { 1914 dev_warn(&intf->dev, "ignoring external hub\n"); 1915 return -ENODEV; 1916 } 1917 #endif 1918 1919 if (!hub_descriptor_is_sane(desc)) { 1920 dev_err(&intf->dev, "bad descriptor, ignoring hub\n"); 1921 return -EIO; 1922 } 1923 1924 /* We found a hub */ 1925 dev_info(&intf->dev, "USB hub found\n"); 1926 1927 hub = kzalloc(sizeof(*hub), GFP_KERNEL); 1928 if (!hub) 1929 return -ENOMEM; 1930 1931 kref_init(&hub->kref); 1932 hub->intfdev = &intf->dev; 1933 hub->hdev = hdev; 1934 INIT_DELAYED_WORK(&hub->leds, led_work); 1935 INIT_DELAYED_WORK(&hub->init_work, NULL); 1936 INIT_WORK(&hub->events, hub_event); 1937 INIT_LIST_HEAD(&hub->onboard_devs); 1938 spin_lock_init(&hub->irq_urb_lock); 1939 timer_setup(&hub->irq_urb_retry, hub_retry_irq_urb, 0); 1940 usb_get_intf(intf); 1941 usb_get_dev(hdev); 1942 1943 usb_set_intfdata(intf, hub); 1944 intf->needs_remote_wakeup = 1; 1945 pm_suspend_ignore_children(&intf->dev, true); 1946 1947 if (hdev->speed == USB_SPEED_HIGH) 1948 highspeed_hubs++; 1949 1950 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) 1951 hub->quirk_check_port_auto_suspend = 1; 1952 1953 if (id->driver_info & HUB_QUIRK_DISABLE_AUTOSUSPEND) { 1954 hub->quirk_disable_autosuspend = 1; 1955 usb_autopm_get_interface_no_resume(intf); 1956 } 1957 1958 if ((id->driver_info & HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL) && 1959 desc->endpoint[0].desc.bInterval > USB_REDUCE_FRAME_INTR_BINTERVAL) { 1960 desc->endpoint[0].desc.bInterval = 1961 USB_REDUCE_FRAME_INTR_BINTERVAL; 1962 /* Tell the HCD about the interrupt ep's new bInterval */ 1963 usb_set_interface(hdev, 0, 0); 1964 } 1965 1966 if (hub_configure(hub, &desc->endpoint[0].desc) >= 0) { 1967 onboard_dev_create_pdevs(hdev, &hub->onboard_devs); 1968 1969 return 0; 1970 } 1971 1972 hub_disconnect(intf); 1973 return -ENODEV; 1974 } 1975 1976 static int 1977 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 1978 { 1979 struct usb_device *hdev = interface_to_usbdev(intf); 1980 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 1981 1982 /* assert ifno == 0 (part of hub spec) */ 1983 switch (code) { 1984 case USBDEVFS_HUB_PORTINFO: { 1985 struct usbdevfs_hub_portinfo *info = user_data; 1986 int i; 1987 1988 spin_lock_irq(&device_state_lock); 1989 if (hdev->devnum <= 0) 1990 info->nports = 0; 1991 else { 1992 info->nports = hdev->maxchild; 1993 for (i = 0; i < info->nports; i++) { 1994 if (hub->ports[i]->child == NULL) 1995 info->port[i] = 0; 1996 else 1997 info->port[i] = 1998 hub->ports[i]->child->devnum; 1999 } 2000 } 2001 spin_unlock_irq(&device_state_lock); 2002 2003 return info->nports + 1; 2004 } 2005 2006 default: 2007 return -ENOSYS; 2008 } 2009 } 2010 2011 /* 2012 * Allow user programs to claim ports on a hub. When a device is attached 2013 * to one of these "claimed" ports, the program will "own" the device. 2014 */ 2015 static int find_port_owner(struct usb_device *hdev, unsigned port1, 2016 struct usb_dev_state ***ppowner) 2017 { 2018 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 2019 2020 if (hdev->state == USB_STATE_NOTATTACHED) 2021 return -ENODEV; 2022 if (port1 == 0 || port1 > hdev->maxchild) 2023 return -EINVAL; 2024 2025 /* Devices not managed by the hub driver 2026 * will always have maxchild equal to 0. 2027 */ 2028 *ppowner = &(hub->ports[port1 - 1]->port_owner); 2029 return 0; 2030 } 2031 2032 /* In the following three functions, the caller must hold hdev's lock */ 2033 int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, 2034 struct usb_dev_state *owner) 2035 { 2036 int rc; 2037 struct usb_dev_state **powner; 2038 2039 rc = find_port_owner(hdev, port1, &powner); 2040 if (rc) 2041 return rc; 2042 if (*powner) 2043 return -EBUSY; 2044 *powner = owner; 2045 return rc; 2046 } 2047 EXPORT_SYMBOL_GPL(usb_hub_claim_port); 2048 2049 int usb_hub_release_port(struct usb_device *hdev, unsigned port1, 2050 struct usb_dev_state *owner) 2051 { 2052 int rc; 2053 struct usb_dev_state **powner; 2054 2055 rc = find_port_owner(hdev, port1, &powner); 2056 if (rc) 2057 return rc; 2058 if (*powner != owner) 2059 return -ENOENT; 2060 *powner = NULL; 2061 return rc; 2062 } 2063 EXPORT_SYMBOL_GPL(usb_hub_release_port); 2064 2065 void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner) 2066 { 2067 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 2068 int n; 2069 2070 for (n = 0; n < hdev->maxchild; n++) { 2071 if (hub->ports[n]->port_owner == owner) 2072 hub->ports[n]->port_owner = NULL; 2073 } 2074 2075 } 2076 2077 /* The caller must hold udev's lock */ 2078 bool usb_device_is_owned(struct usb_device *udev) 2079 { 2080 struct usb_hub *hub; 2081 2082 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) 2083 return false; 2084 hub = usb_hub_to_struct_hub(udev->parent); 2085 return !!hub->ports[udev->portnum - 1]->port_owner; 2086 } 2087 2088 static void update_port_device_state(struct usb_device *udev) 2089 { 2090 struct usb_hub *hub; 2091 struct usb_port *port_dev; 2092 2093 if (udev->parent) { 2094 hub = usb_hub_to_struct_hub(udev->parent); 2095 2096 /* 2097 * The Link Layer Validation System Driver (lvstest) 2098 * has a test step to unbind the hub before running the 2099 * rest of the procedure. This triggers hub_disconnect 2100 * which will set the hub's maxchild to 0, further 2101 * resulting in usb_hub_to_struct_hub returning NULL. 2102 */ 2103 if (hub) { 2104 port_dev = hub->ports[udev->portnum - 1]; 2105 WRITE_ONCE(port_dev->state, udev->state); 2106 sysfs_notify_dirent(port_dev->state_kn); 2107 } 2108 } 2109 } 2110 2111 static void recursively_mark_NOTATTACHED(struct usb_device *udev) 2112 { 2113 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 2114 int i; 2115 2116 for (i = 0; i < udev->maxchild; ++i) { 2117 if (hub->ports[i]->child) 2118 recursively_mark_NOTATTACHED(hub->ports[i]->child); 2119 } 2120 if (udev->state == USB_STATE_SUSPENDED) 2121 udev->active_duration -= jiffies; 2122 udev->state = USB_STATE_NOTATTACHED; 2123 update_port_device_state(udev); 2124 } 2125 2126 /** 2127 * usb_set_device_state - change a device's current state (usbcore, hcds) 2128 * @udev: pointer to device whose state should be changed 2129 * @new_state: new state value to be stored 2130 * 2131 * udev->state is _not_ fully protected by the device lock. Although 2132 * most transitions are made only while holding the lock, the state can 2133 * can change to USB_STATE_NOTATTACHED at almost any time. This 2134 * is so that devices can be marked as disconnected as soon as possible, 2135 * without having to wait for any semaphores to be released. As a result, 2136 * all changes to any device's state must be protected by the 2137 * device_state_lock spinlock. 2138 * 2139 * Once a device has been added to the device tree, all changes to its state 2140 * should be made using this routine. The state should _not_ be set directly. 2141 * 2142 * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 2143 * Otherwise udev->state is set to new_state, and if new_state is 2144 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 2145 * to USB_STATE_NOTATTACHED. 2146 */ 2147 void usb_set_device_state(struct usb_device *udev, 2148 enum usb_device_state new_state) 2149 { 2150 unsigned long flags; 2151 int wakeup = -1; 2152 2153 spin_lock_irqsave(&device_state_lock, flags); 2154 if (udev->state == USB_STATE_NOTATTACHED) 2155 ; /* do nothing */ 2156 else if (new_state != USB_STATE_NOTATTACHED) { 2157 2158 /* root hub wakeup capabilities are managed out-of-band 2159 * and may involve silicon errata ... ignore them here. 2160 */ 2161 if (udev->parent) { 2162 if (udev->state == USB_STATE_SUSPENDED 2163 || new_state == USB_STATE_SUSPENDED) 2164 ; /* No change to wakeup settings */ 2165 else if (new_state == USB_STATE_CONFIGURED) 2166 wakeup = (udev->quirks & 2167 USB_QUIRK_IGNORE_REMOTE_WAKEUP) ? 0 : 2168 udev->actconfig->desc.bmAttributes & 2169 USB_CONFIG_ATT_WAKEUP; 2170 else 2171 wakeup = 0; 2172 } 2173 if (udev->state == USB_STATE_SUSPENDED && 2174 new_state != USB_STATE_SUSPENDED) 2175 udev->active_duration -= jiffies; 2176 else if (new_state == USB_STATE_SUSPENDED && 2177 udev->state != USB_STATE_SUSPENDED) 2178 udev->active_duration += jiffies; 2179 udev->state = new_state; 2180 update_port_device_state(udev); 2181 } else 2182 recursively_mark_NOTATTACHED(udev); 2183 spin_unlock_irqrestore(&device_state_lock, flags); 2184 if (wakeup >= 0) 2185 device_set_wakeup_capable(&udev->dev, wakeup); 2186 } 2187 EXPORT_SYMBOL_GPL(usb_set_device_state); 2188 2189 /* 2190 * Choose a device number. 2191 * 2192 * Device numbers are used as filenames in usbfs. On USB-1.1 and 2193 * USB-2.0 buses they are also used as device addresses, however on 2194 * USB-3.0 buses the address is assigned by the controller hardware 2195 * and it usually is not the same as the device number. 2196 * 2197 * Devices connected under xHCI are not as simple. The host controller 2198 * supports virtualization, so the hardware assigns device addresses and 2199 * the HCD must setup data structures before issuing a set address 2200 * command to the hardware. 2201 */ 2202 static void choose_devnum(struct usb_device *udev) 2203 { 2204 int devnum; 2205 struct usb_bus *bus = udev->bus; 2206 2207 /* be safe when more hub events are proceed in parallel */ 2208 mutex_lock(&bus->devnum_next_mutex); 2209 2210 /* Try to allocate the next devnum beginning at bus->devnum_next. */ 2211 devnum = find_next_zero_bit(bus->devmap, 128, bus->devnum_next); 2212 if (devnum >= 128) 2213 devnum = find_next_zero_bit(bus->devmap, 128, 1); 2214 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1); 2215 if (devnum < 128) { 2216 set_bit(devnum, bus->devmap); 2217 udev->devnum = devnum; 2218 } 2219 mutex_unlock(&bus->devnum_next_mutex); 2220 } 2221 2222 static void release_devnum(struct usb_device *udev) 2223 { 2224 if (udev->devnum > 0) { 2225 clear_bit(udev->devnum, udev->bus->devmap); 2226 udev->devnum = -1; 2227 } 2228 } 2229 2230 static void update_devnum(struct usb_device *udev, int devnum) 2231 { 2232 udev->devnum = devnum; 2233 if (!udev->devaddr) 2234 udev->devaddr = (u8)devnum; 2235 } 2236 2237 static void hub_free_dev(struct usb_device *udev) 2238 { 2239 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2240 2241 /* Root hubs aren't real devices, so don't free HCD resources */ 2242 if (hcd->driver->free_dev && udev->parent) 2243 hcd->driver->free_dev(hcd, udev); 2244 } 2245 2246 static void hub_disconnect_children(struct usb_device *udev) 2247 { 2248 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 2249 int i; 2250 2251 /* Free up all the children before we remove this device */ 2252 for (i = 0; i < udev->maxchild; i++) { 2253 if (hub->ports[i]->child) 2254 usb_disconnect(&hub->ports[i]->child); 2255 } 2256 } 2257 2258 /** 2259 * usb_disconnect - disconnect a device (usbcore-internal) 2260 * @pdev: pointer to device being disconnected 2261 * 2262 * Context: task context, might sleep 2263 * 2264 * Something got disconnected. Get rid of it and all of its children. 2265 * 2266 * If *pdev is a normal device then the parent hub must already be locked. 2267 * If *pdev is a root hub then the caller must hold the usb_bus_idr_lock, 2268 * which protects the set of root hubs as well as the list of buses. 2269 * 2270 * Only hub drivers (including virtual root hub drivers for host 2271 * controllers) should ever call this. 2272 * 2273 * This call is synchronous, and may not be used in an interrupt context. 2274 */ 2275 void usb_disconnect(struct usb_device **pdev) 2276 { 2277 struct usb_port *port_dev = NULL; 2278 struct usb_device *udev = *pdev; 2279 struct usb_hub *hub = NULL; 2280 int port1 = 1; 2281 2282 /* mark the device as inactive, so any further urb submissions for 2283 * this device (and any of its children) will fail immediately. 2284 * this quiesces everything except pending urbs. 2285 */ 2286 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 2287 dev_info(&udev->dev, "USB disconnect, device number %d\n", 2288 udev->devnum); 2289 2290 /* 2291 * Ensure that the pm runtime code knows that the USB device 2292 * is in the process of being disconnected. 2293 */ 2294 pm_runtime_barrier(&udev->dev); 2295 2296 usb_lock_device(udev); 2297 2298 hub_disconnect_children(udev); 2299 2300 /* deallocate hcd/hardware state ... nuking all pending urbs and 2301 * cleaning up all state associated with the current configuration 2302 * so that the hardware is now fully quiesced. 2303 */ 2304 dev_dbg(&udev->dev, "unregistering device\n"); 2305 usb_disable_device(udev, 0); 2306 usb_hcd_synchronize_unlinks(udev); 2307 2308 if (udev->parent) { 2309 port1 = udev->portnum; 2310 hub = usb_hub_to_struct_hub(udev->parent); 2311 port_dev = hub->ports[port1 - 1]; 2312 2313 sysfs_remove_link(&udev->dev.kobj, "port"); 2314 sysfs_remove_link(&port_dev->dev.kobj, "device"); 2315 2316 /* 2317 * As usb_port_runtime_resume() de-references udev, make 2318 * sure no resumes occur during removal 2319 */ 2320 if (!test_and_set_bit(port1, hub->child_usage_bits)) 2321 pm_runtime_get_sync(&port_dev->dev); 2322 2323 typec_deattach(port_dev->connector, &udev->dev); 2324 } 2325 2326 usb_remove_ep_devs(&udev->ep0); 2327 usb_unlock_device(udev); 2328 2329 /* Unregister the device. The device driver is responsible 2330 * for de-configuring the device and invoking the remove-device 2331 * notifier chain (used by usbfs and possibly others). 2332 */ 2333 device_del(&udev->dev); 2334 2335 /* Free the device number and delete the parent's children[] 2336 * (or root_hub) pointer. 2337 */ 2338 release_devnum(udev); 2339 2340 /* Avoid races with recursively_mark_NOTATTACHED() */ 2341 spin_lock_irq(&device_state_lock); 2342 *pdev = NULL; 2343 spin_unlock_irq(&device_state_lock); 2344 2345 if (port_dev && test_and_clear_bit(port1, hub->child_usage_bits)) 2346 pm_runtime_put(&port_dev->dev); 2347 2348 hub_free_dev(udev); 2349 2350 put_device(&udev->dev); 2351 } 2352 2353 #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES 2354 static void show_string(struct usb_device *udev, char *id, char *string) 2355 { 2356 if (!string) 2357 return; 2358 dev_info(&udev->dev, "%s: %s\n", id, string); 2359 } 2360 2361 static void announce_device(struct usb_device *udev) 2362 { 2363 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice); 2364 2365 dev_info(&udev->dev, 2366 "New USB device found, idVendor=%04x, idProduct=%04x, bcdDevice=%2x.%02x\n", 2367 le16_to_cpu(udev->descriptor.idVendor), 2368 le16_to_cpu(udev->descriptor.idProduct), 2369 bcdDevice >> 8, bcdDevice & 0xff); 2370 dev_info(&udev->dev, 2371 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", 2372 udev->descriptor.iManufacturer, 2373 udev->descriptor.iProduct, 2374 udev->descriptor.iSerialNumber); 2375 show_string(udev, "Product", udev->product); 2376 show_string(udev, "Manufacturer", udev->manufacturer); 2377 show_string(udev, "SerialNumber", udev->serial); 2378 } 2379 #else 2380 static inline void announce_device(struct usb_device *udev) { } 2381 #endif 2382 2383 2384 /** 2385 * usb_enumerate_device_otg - FIXME (usbcore-internal) 2386 * @udev: newly addressed device (in ADDRESS state) 2387 * 2388 * Finish enumeration for On-The-Go devices 2389 * 2390 * Return: 0 if successful. A negative error code otherwise. 2391 */ 2392 static int usb_enumerate_device_otg(struct usb_device *udev) 2393 { 2394 int err = 0; 2395 2396 #ifdef CONFIG_USB_OTG 2397 /* 2398 * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 2399 * to wake us after we've powered off VBUS; and HNP, switching roles 2400 * "host" to "peripheral". The OTG descriptor helps figure this out. 2401 */ 2402 if (!udev->bus->is_b_host 2403 && udev->config 2404 && udev->parent == udev->bus->root_hub) { 2405 struct usb_otg_descriptor *desc = NULL; 2406 struct usb_bus *bus = udev->bus; 2407 unsigned port1 = udev->portnum; 2408 2409 /* descriptor may appear anywhere in config */ 2410 err = __usb_get_extra_descriptor(udev->rawdescriptors[0], 2411 le16_to_cpu(udev->config[0].desc.wTotalLength), 2412 USB_DT_OTG, (void **) &desc, sizeof(*desc)); 2413 if (err || !(desc->bmAttributes & USB_OTG_HNP)) 2414 return 0; 2415 2416 dev_info(&udev->dev, "Dual-Role OTG device on %sHNP port\n", 2417 (port1 == bus->otg_port) ? "" : "non-"); 2418 2419 /* enable HNP before suspend, it's simpler */ 2420 if (port1 == bus->otg_port) { 2421 bus->b_hnp_enable = 1; 2422 err = usb_control_msg(udev, 2423 usb_sndctrlpipe(udev, 0), 2424 USB_REQ_SET_FEATURE, 0, 2425 USB_DEVICE_B_HNP_ENABLE, 2426 0, NULL, 0, 2427 USB_CTRL_SET_TIMEOUT); 2428 if (err < 0) { 2429 /* 2430 * OTG MESSAGE: report errors here, 2431 * customize to match your product. 2432 */ 2433 dev_err(&udev->dev, "can't set HNP mode: %d\n", 2434 err); 2435 bus->b_hnp_enable = 0; 2436 } 2437 } else if (desc->bLength == sizeof 2438 (struct usb_otg_descriptor)) { 2439 /* 2440 * We are operating on a legacy OTP device 2441 * These should be told that they are operating 2442 * on the wrong port if we have another port that does 2443 * support HNP 2444 */ 2445 if (bus->otg_port != 0) { 2446 /* Set a_alt_hnp_support for legacy otg device */ 2447 err = usb_control_msg(udev, 2448 usb_sndctrlpipe(udev, 0), 2449 USB_REQ_SET_FEATURE, 0, 2450 USB_DEVICE_A_ALT_HNP_SUPPORT, 2451 0, NULL, 0, 2452 USB_CTRL_SET_TIMEOUT); 2453 if (err < 0) 2454 dev_err(&udev->dev, 2455 "set a_alt_hnp_support failed: %d\n", 2456 err); 2457 } 2458 } 2459 } 2460 #endif 2461 return err; 2462 } 2463 2464 2465 /** 2466 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) 2467 * @udev: newly addressed device (in ADDRESS state) 2468 * 2469 * This is only called by usb_new_device() -- all comments that apply there 2470 * apply here wrt to environment. 2471 * 2472 * If the device is WUSB and not authorized, we don't attempt to read 2473 * the string descriptors, as they will be errored out by the device 2474 * until it has been authorized. 2475 * 2476 * Return: 0 if successful. A negative error code otherwise. 2477 */ 2478 static int usb_enumerate_device(struct usb_device *udev) 2479 { 2480 int err; 2481 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 2482 2483 if (udev->config == NULL) { 2484 err = usb_get_configuration(udev); 2485 if (err < 0) { 2486 if (err != -ENODEV) 2487 dev_err(&udev->dev, "can't read configurations, error %d\n", 2488 err); 2489 return err; 2490 } 2491 } 2492 2493 /* read the standard strings and cache them if present */ 2494 udev->product = usb_cache_string(udev, udev->descriptor.iProduct); 2495 udev->manufacturer = usb_cache_string(udev, 2496 udev->descriptor.iManufacturer); 2497 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); 2498 2499 err = usb_enumerate_device_otg(udev); 2500 if (err < 0) 2501 return err; 2502 2503 if (IS_ENABLED(CONFIG_USB_OTG_PRODUCTLIST) && hcd->tpl_support && 2504 !is_targeted(udev)) { 2505 /* Maybe it can talk to us, though we can't talk to it. 2506 * (Includes HNP test device.) 2507 */ 2508 if (IS_ENABLED(CONFIG_USB_OTG) && (udev->bus->b_hnp_enable 2509 || udev->bus->is_b_host)) { 2510 err = usb_port_suspend(udev, PMSG_AUTO_SUSPEND); 2511 if (err < 0) 2512 dev_dbg(&udev->dev, "HNP fail, %d\n", err); 2513 } 2514 return -ENOTSUPP; 2515 } 2516 2517 usb_detect_interface_quirks(udev); 2518 2519 return 0; 2520 } 2521 2522 static void set_usb_port_removable(struct usb_device *udev) 2523 { 2524 struct usb_device *hdev = udev->parent; 2525 struct usb_hub *hub; 2526 u8 port = udev->portnum; 2527 u16 wHubCharacteristics; 2528 bool removable = true; 2529 2530 dev_set_removable(&udev->dev, DEVICE_REMOVABLE_UNKNOWN); 2531 2532 if (!hdev) 2533 return; 2534 2535 hub = usb_hub_to_struct_hub(udev->parent); 2536 2537 /* 2538 * If the platform firmware has provided information about a port, 2539 * use that to determine whether it's removable. 2540 */ 2541 switch (hub->ports[udev->portnum - 1]->connect_type) { 2542 case USB_PORT_CONNECT_TYPE_HOT_PLUG: 2543 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); 2544 return; 2545 case USB_PORT_CONNECT_TYPE_HARD_WIRED: 2546 case USB_PORT_NOT_USED: 2547 dev_set_removable(&udev->dev, DEVICE_FIXED); 2548 return; 2549 default: 2550 break; 2551 } 2552 2553 /* 2554 * Otherwise, check whether the hub knows whether a port is removable 2555 * or not 2556 */ 2557 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2558 2559 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 2560 return; 2561 2562 if (hub_is_superspeed(hdev)) { 2563 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) 2564 & (1 << port)) 2565 removable = false; 2566 } else { 2567 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 2568 removable = false; 2569 } 2570 2571 if (removable) 2572 dev_set_removable(&udev->dev, DEVICE_REMOVABLE); 2573 else 2574 dev_set_removable(&udev->dev, DEVICE_FIXED); 2575 2576 } 2577 2578 /** 2579 * usb_new_device - perform initial device setup (usbcore-internal) 2580 * @udev: newly addressed device (in ADDRESS state) 2581 * 2582 * This is called with devices which have been detected but not fully 2583 * enumerated. The device descriptor is available, but not descriptors 2584 * for any device configuration. The caller must have locked either 2585 * the parent hub (if udev is a normal device) or else the 2586 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to 2587 * udev has already been installed, but udev is not yet visible through 2588 * sysfs or other filesystem code. 2589 * 2590 * This call is synchronous, and may not be used in an interrupt context. 2591 * 2592 * Only the hub driver or root-hub registrar should ever call this. 2593 * 2594 * Return: Whether the device is configured properly or not. Zero if the 2595 * interface was registered with the driver core; else a negative errno 2596 * value. 2597 * 2598 */ 2599 int usb_new_device(struct usb_device *udev) 2600 { 2601 int err; 2602 2603 if (udev->parent) { 2604 /* Initialize non-root-hub device wakeup to disabled; 2605 * device (un)configuration controls wakeup capable 2606 * sysfs power/wakeup controls wakeup enabled/disabled 2607 */ 2608 device_init_wakeup(&udev->dev, 0); 2609 } 2610 2611 /* Tell the runtime-PM framework the device is active */ 2612 pm_runtime_set_active(&udev->dev); 2613 pm_runtime_get_noresume(&udev->dev); 2614 pm_runtime_use_autosuspend(&udev->dev); 2615 pm_runtime_enable(&udev->dev); 2616 2617 /* By default, forbid autosuspend for all devices. It will be 2618 * allowed for hubs during binding. 2619 */ 2620 usb_disable_autosuspend(udev); 2621 2622 err = usb_enumerate_device(udev); /* Read descriptors */ 2623 if (err < 0) 2624 goto fail; 2625 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 2626 udev->devnum, udev->bus->busnum, 2627 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 2628 /* export the usbdev device-node for libusb */ 2629 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 2630 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 2631 2632 /* Tell the world! */ 2633 announce_device(udev); 2634 2635 if (udev->serial) 2636 add_device_randomness(udev->serial, strlen(udev->serial)); 2637 if (udev->product) 2638 add_device_randomness(udev->product, strlen(udev->product)); 2639 if (udev->manufacturer) 2640 add_device_randomness(udev->manufacturer, 2641 strlen(udev->manufacturer)); 2642 2643 device_enable_async_suspend(&udev->dev); 2644 2645 /* check whether the hub or firmware marks this port as non-removable */ 2646 set_usb_port_removable(udev); 2647 2648 /* Register the device. The device driver is responsible 2649 * for configuring the device and invoking the add-device 2650 * notifier chain (used by usbfs and possibly others). 2651 */ 2652 err = device_add(&udev->dev); 2653 if (err) { 2654 dev_err(&udev->dev, "can't device_add, error %d\n", err); 2655 goto fail; 2656 } 2657 2658 /* Create link files between child device and usb port device. */ 2659 if (udev->parent) { 2660 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 2661 int port1 = udev->portnum; 2662 struct usb_port *port_dev = hub->ports[port1 - 1]; 2663 2664 err = sysfs_create_link(&udev->dev.kobj, 2665 &port_dev->dev.kobj, "port"); 2666 if (err) 2667 goto out_del_dev; 2668 2669 err = sysfs_create_link(&port_dev->dev.kobj, 2670 &udev->dev.kobj, "device"); 2671 if (err) { 2672 sysfs_remove_link(&udev->dev.kobj, "port"); 2673 goto out_del_dev; 2674 } 2675 2676 if (!test_and_set_bit(port1, hub->child_usage_bits)) 2677 pm_runtime_get_sync(&port_dev->dev); 2678 2679 typec_attach(port_dev->connector, &udev->dev); 2680 } 2681 2682 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2683 usb_mark_last_busy(udev); 2684 pm_runtime_put_sync_autosuspend(&udev->dev); 2685 return err; 2686 2687 out_del_dev: 2688 device_del(&udev->dev); 2689 fail: 2690 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 2691 pm_runtime_disable(&udev->dev); 2692 pm_runtime_set_suspended(&udev->dev); 2693 return err; 2694 } 2695 2696 2697 /** 2698 * usb_deauthorize_device - deauthorize a device (usbcore-internal) 2699 * @usb_dev: USB device 2700 * 2701 * Move the USB device to a very basic state where interfaces are disabled 2702 * and the device is in fact unconfigured and unusable. 2703 * 2704 * We share a lock (that we have) with device_del(), so we need to 2705 * defer its call. 2706 * 2707 * Return: 0. 2708 */ 2709 int usb_deauthorize_device(struct usb_device *usb_dev) 2710 { 2711 usb_lock_device(usb_dev); 2712 if (usb_dev->authorized == 0) 2713 goto out_unauthorized; 2714 2715 usb_dev->authorized = 0; 2716 usb_set_configuration(usb_dev, -1); 2717 2718 out_unauthorized: 2719 usb_unlock_device(usb_dev); 2720 return 0; 2721 } 2722 2723 2724 int usb_authorize_device(struct usb_device *usb_dev) 2725 { 2726 int result = 0, c; 2727 2728 usb_lock_device(usb_dev); 2729 if (usb_dev->authorized == 1) 2730 goto out_authorized; 2731 2732 result = usb_autoresume_device(usb_dev); 2733 if (result < 0) { 2734 dev_err(&usb_dev->dev, 2735 "can't autoresume for authorization: %d\n", result); 2736 goto error_autoresume; 2737 } 2738 2739 usb_dev->authorized = 1; 2740 /* Choose and set the configuration. This registers the interfaces 2741 * with the driver core and lets interface drivers bind to them. 2742 */ 2743 c = usb_choose_configuration(usb_dev); 2744 if (c >= 0) { 2745 result = usb_set_configuration(usb_dev, c); 2746 if (result) { 2747 dev_err(&usb_dev->dev, 2748 "can't set config #%d, error %d\n", c, result); 2749 /* This need not be fatal. The user can try to 2750 * set other configurations. */ 2751 } 2752 } 2753 dev_info(&usb_dev->dev, "authorized to connect\n"); 2754 2755 usb_autosuspend_device(usb_dev); 2756 error_autoresume: 2757 out_authorized: 2758 usb_unlock_device(usb_dev); /* complements locktree */ 2759 return result; 2760 } 2761 2762 /** 2763 * get_port_ssp_rate - Match the extended port status to SSP rate 2764 * @hdev: The hub device 2765 * @ext_portstatus: extended port status 2766 * 2767 * Match the extended port status speed id to the SuperSpeed Plus sublink speed 2768 * capability attributes. Base on the number of connected lanes and speed, 2769 * return the corresponding enum usb_ssp_rate. 2770 */ 2771 static enum usb_ssp_rate get_port_ssp_rate(struct usb_device *hdev, 2772 u32 ext_portstatus) 2773 { 2774 struct usb_ssp_cap_descriptor *ssp_cap; 2775 u32 attr; 2776 u8 speed_id; 2777 u8 ssac; 2778 u8 lanes; 2779 int i; 2780 2781 if (!hdev->bos) 2782 goto out; 2783 2784 ssp_cap = hdev->bos->ssp_cap; 2785 if (!ssp_cap) 2786 goto out; 2787 2788 speed_id = ext_portstatus & USB_EXT_PORT_STAT_RX_SPEED_ID; 2789 lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 2790 2791 ssac = le32_to_cpu(ssp_cap->bmAttributes) & 2792 USB_SSP_SUBLINK_SPEED_ATTRIBS; 2793 2794 for (i = 0; i <= ssac; i++) { 2795 u8 ssid; 2796 2797 attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); 2798 ssid = FIELD_GET(USB_SSP_SUBLINK_SPEED_SSID, attr); 2799 if (speed_id == ssid) { 2800 u16 mantissa; 2801 u8 lse; 2802 u8 type; 2803 2804 /* 2805 * Note: currently asymmetric lane types are only 2806 * applicable for SSIC operate in SuperSpeed protocol 2807 */ 2808 type = FIELD_GET(USB_SSP_SUBLINK_SPEED_ST, attr); 2809 if (type == USB_SSP_SUBLINK_SPEED_ST_ASYM_RX || 2810 type == USB_SSP_SUBLINK_SPEED_ST_ASYM_TX) 2811 goto out; 2812 2813 if (FIELD_GET(USB_SSP_SUBLINK_SPEED_LP, attr) != 2814 USB_SSP_SUBLINK_SPEED_LP_SSP) 2815 goto out; 2816 2817 lse = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSE, attr); 2818 mantissa = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSM, attr); 2819 2820 /* Convert to Gbps */ 2821 for (; lse < USB_SSP_SUBLINK_SPEED_LSE_GBPS; lse++) 2822 mantissa /= 1000; 2823 2824 if (mantissa >= 10 && lanes == 1) 2825 return USB_SSP_GEN_2x1; 2826 2827 if (mantissa >= 10 && lanes == 2) 2828 return USB_SSP_GEN_2x2; 2829 2830 if (mantissa >= 5 && lanes == 2) 2831 return USB_SSP_GEN_1x2; 2832 2833 goto out; 2834 } 2835 } 2836 2837 out: 2838 return USB_SSP_GEN_UNKNOWN; 2839 } 2840 2841 #ifdef CONFIG_USB_FEW_INIT_RETRIES 2842 #define PORT_RESET_TRIES 2 2843 #define SET_ADDRESS_TRIES 1 2844 #define GET_DESCRIPTOR_TRIES 1 2845 #define GET_MAXPACKET0_TRIES 1 2846 #define PORT_INIT_TRIES 4 2847 2848 #else 2849 #define PORT_RESET_TRIES 5 2850 #define SET_ADDRESS_TRIES 2 2851 #define GET_DESCRIPTOR_TRIES 2 2852 #define GET_MAXPACKET0_TRIES 3 2853 #define PORT_INIT_TRIES 4 2854 #endif /* CONFIG_USB_FEW_INIT_RETRIES */ 2855 2856 #define DETECT_DISCONNECT_TRIES 5 2857 2858 #define HUB_ROOT_RESET_TIME 60 /* times are in msec */ 2859 #define HUB_SHORT_RESET_TIME 10 2860 #define HUB_BH_RESET_TIME 50 2861 #define HUB_LONG_RESET_TIME 200 2862 #define HUB_RESET_TIMEOUT 800 2863 2864 static bool use_new_scheme(struct usb_device *udev, int retry, 2865 struct usb_port *port_dev) 2866 { 2867 int old_scheme_first_port = 2868 (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || 2869 old_scheme_first; 2870 2871 /* 2872 * "New scheme" enumeration causes an extra state transition to be 2873 * exposed to an xhci host and causes USB3 devices to receive control 2874 * commands in the default state. This has been seen to cause 2875 * enumeration failures, so disable this enumeration scheme for USB3 2876 * devices. 2877 */ 2878 if (udev->speed >= USB_SPEED_SUPER) 2879 return false; 2880 2881 /* 2882 * If use_both_schemes is set, use the first scheme (whichever 2883 * it is) for the larger half of the retries, then use the other 2884 * scheme. Otherwise, use the first scheme for all the retries. 2885 */ 2886 if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2) 2887 return old_scheme_first_port; /* Second half */ 2888 return !old_scheme_first_port; /* First half or all */ 2889 } 2890 2891 /* Is a USB 3.0 port in the Inactive or Compliance Mode state? 2892 * Port warm reset is required to recover 2893 */ 2894 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 2895 u16 portstatus) 2896 { 2897 u16 link_state; 2898 2899 if (!hub_is_superspeed(hub->hdev)) 2900 return false; 2901 2902 if (test_bit(port1, hub->warm_reset_bits)) 2903 return true; 2904 2905 link_state = portstatus & USB_PORT_STAT_LINK_STATE; 2906 return link_state == USB_SS_PORT_LS_SS_INACTIVE 2907 || link_state == USB_SS_PORT_LS_COMP_MOD; 2908 } 2909 2910 static int hub_port_wait_reset(struct usb_hub *hub, int port1, 2911 struct usb_device *udev, unsigned int delay, bool warm) 2912 { 2913 int delay_time, ret; 2914 u16 portstatus; 2915 u16 portchange; 2916 u32 ext_portstatus = 0; 2917 2918 for (delay_time = 0; 2919 delay_time < HUB_RESET_TIMEOUT; 2920 delay_time += delay) { 2921 /* wait to give the device a chance to reset */ 2922 msleep(delay); 2923 2924 /* read and decode port status */ 2925 if (hub_is_superspeedplus(hub->hdev)) 2926 ret = hub_ext_port_status(hub, port1, 2927 HUB_EXT_PORT_STATUS, 2928 &portstatus, &portchange, 2929 &ext_portstatus); 2930 else 2931 ret = usb_hub_port_status(hub, port1, &portstatus, 2932 &portchange); 2933 if (ret < 0) 2934 return ret; 2935 2936 /* 2937 * The port state is unknown until the reset completes. 2938 * 2939 * On top of that, some chips may require additional time 2940 * to re-establish a connection after the reset is complete, 2941 * so also wait for the connection to be re-established. 2942 */ 2943 if (!(portstatus & USB_PORT_STAT_RESET) && 2944 (portstatus & USB_PORT_STAT_CONNECTION)) 2945 break; 2946 2947 /* switch to the long delay after two short delay failures */ 2948 if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 2949 delay = HUB_LONG_RESET_TIME; 2950 2951 dev_dbg(&hub->ports[port1 - 1]->dev, 2952 "not %sreset yet, waiting %dms\n", 2953 warm ? "warm " : "", delay); 2954 } 2955 2956 if ((portstatus & USB_PORT_STAT_RESET)) 2957 return -EBUSY; 2958 2959 if (hub_port_warm_reset_required(hub, port1, portstatus)) 2960 return -ENOTCONN; 2961 2962 /* Device went away? */ 2963 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 2964 return -ENOTCONN; 2965 2966 /* Retry if connect change is set but status is still connected. 2967 * A USB 3.0 connection may bounce if multiple warm resets were issued, 2968 * but the device may have successfully re-connected. Ignore it. 2969 */ 2970 if (!hub_is_superspeed(hub->hdev) && 2971 (portchange & USB_PORT_STAT_C_CONNECTION)) { 2972 usb_clear_port_feature(hub->hdev, port1, 2973 USB_PORT_FEAT_C_CONNECTION); 2974 return -EAGAIN; 2975 } 2976 2977 if (!(portstatus & USB_PORT_STAT_ENABLE)) 2978 return -EBUSY; 2979 2980 if (!udev) 2981 return 0; 2982 2983 if (hub_is_superspeedplus(hub->hdev)) { 2984 /* extended portstatus Rx and Tx lane count are zero based */ 2985 udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 2986 udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1; 2987 udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus); 2988 } else { 2989 udev->rx_lanes = 1; 2990 udev->tx_lanes = 1; 2991 udev->ssp_rate = USB_SSP_GEN_UNKNOWN; 2992 } 2993 if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN) 2994 udev->speed = USB_SPEED_SUPER_PLUS; 2995 else if (hub_is_superspeed(hub->hdev)) 2996 udev->speed = USB_SPEED_SUPER; 2997 else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 2998 udev->speed = USB_SPEED_HIGH; 2999 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 3000 udev->speed = USB_SPEED_LOW; 3001 else 3002 udev->speed = USB_SPEED_FULL; 3003 return 0; 3004 } 3005 3006 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 3007 static int hub_port_reset(struct usb_hub *hub, int port1, 3008 struct usb_device *udev, unsigned int delay, bool warm) 3009 { 3010 int i, status; 3011 u16 portchange, portstatus; 3012 struct usb_port *port_dev = hub->ports[port1 - 1]; 3013 int reset_recovery_time; 3014 3015 if (!hub_is_superspeed(hub->hdev)) { 3016 if (warm) { 3017 dev_err(hub->intfdev, "only USB3 hub support " 3018 "warm reset\n"); 3019 return -EINVAL; 3020 } 3021 /* Block EHCI CF initialization during the port reset. 3022 * Some companion controllers don't like it when they mix. 3023 */ 3024 down_read(&ehci_cf_port_reset_rwsem); 3025 } else if (!warm) { 3026 /* 3027 * If the caller hasn't explicitly requested a warm reset, 3028 * double check and see if one is needed. 3029 */ 3030 if (usb_hub_port_status(hub, port1, &portstatus, 3031 &portchange) == 0) 3032 if (hub_port_warm_reset_required(hub, port1, 3033 portstatus)) 3034 warm = true; 3035 } 3036 clear_bit(port1, hub->warm_reset_bits); 3037 3038 /* Reset the port */ 3039 for (i = 0; i < PORT_RESET_TRIES; i++) { 3040 status = set_port_feature(hub->hdev, port1, (warm ? 3041 USB_PORT_FEAT_BH_PORT_RESET : 3042 USB_PORT_FEAT_RESET)); 3043 if (status == -ENODEV) { 3044 ; /* The hub is gone */ 3045 } else if (status) { 3046 dev_err(&port_dev->dev, 3047 "cannot %sreset (err = %d)\n", 3048 warm ? "warm " : "", status); 3049 } else { 3050 status = hub_port_wait_reset(hub, port1, udev, delay, 3051 warm); 3052 if (status && status != -ENOTCONN && status != -ENODEV) 3053 dev_dbg(hub->intfdev, 3054 "port_wait_reset: err = %d\n", 3055 status); 3056 } 3057 3058 /* 3059 * Check for disconnect or reset, and bail out after several 3060 * reset attempts to avoid warm reset loop. 3061 */ 3062 if (status == 0 || status == -ENOTCONN || status == -ENODEV || 3063 (status == -EBUSY && i == PORT_RESET_TRIES - 1)) { 3064 usb_clear_port_feature(hub->hdev, port1, 3065 USB_PORT_FEAT_C_RESET); 3066 3067 if (!hub_is_superspeed(hub->hdev)) 3068 goto done; 3069 3070 usb_clear_port_feature(hub->hdev, port1, 3071 USB_PORT_FEAT_C_BH_PORT_RESET); 3072 usb_clear_port_feature(hub->hdev, port1, 3073 USB_PORT_FEAT_C_PORT_LINK_STATE); 3074 3075 if (udev) 3076 usb_clear_port_feature(hub->hdev, port1, 3077 USB_PORT_FEAT_C_CONNECTION); 3078 3079 /* 3080 * If a USB 3.0 device migrates from reset to an error 3081 * state, re-issue the warm reset. 3082 */ 3083 if (usb_hub_port_status(hub, port1, 3084 &portstatus, &portchange) < 0) 3085 goto done; 3086 3087 if (!hub_port_warm_reset_required(hub, port1, 3088 portstatus)) 3089 goto done; 3090 3091 /* 3092 * If the port is in SS.Inactive or Compliance Mode, the 3093 * hot or warm reset failed. Try another warm reset. 3094 */ 3095 if (!warm) { 3096 dev_dbg(&port_dev->dev, 3097 "hot reset failed, warm reset\n"); 3098 warm = true; 3099 } 3100 } 3101 3102 dev_dbg(&port_dev->dev, 3103 "not enabled, trying %sreset again...\n", 3104 warm ? "warm " : ""); 3105 delay = HUB_LONG_RESET_TIME; 3106 } 3107 3108 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); 3109 3110 done: 3111 if (status == 0) { 3112 if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM) 3113 usleep_range(10000, 12000); 3114 else { 3115 /* TRSTRCY = 10 ms; plus some extra */ 3116 reset_recovery_time = 10 + 40; 3117 3118 /* Hub needs extra delay after resetting its port. */ 3119 if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET) 3120 reset_recovery_time += 100; 3121 3122 msleep(reset_recovery_time); 3123 } 3124 3125 if (udev) { 3126 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3127 3128 update_devnum(udev, 0); 3129 /* The xHC may think the device is already reset, 3130 * so ignore the status. 3131 */ 3132 if (hcd->driver->reset_device) 3133 hcd->driver->reset_device(hcd, udev); 3134 3135 usb_set_device_state(udev, USB_STATE_DEFAULT); 3136 } 3137 } else { 3138 if (udev) 3139 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 3140 } 3141 3142 if (!hub_is_superspeed(hub->hdev)) 3143 up_read(&ehci_cf_port_reset_rwsem); 3144 3145 return status; 3146 } 3147 3148 /* 3149 * hub_port_stop_enumerate - stop USB enumeration or ignore port events 3150 * @hub: target hub 3151 * @port1: port num of the port 3152 * @retries: port retries number of hub_port_init() 3153 * 3154 * Return: 3155 * true: ignore port actions/events or give up connection attempts. 3156 * false: keep original behavior. 3157 * 3158 * This function will be based on retries to check whether the port which is 3159 * marked with early_stop attribute would stop enumeration or ignore events. 3160 * 3161 * Note: 3162 * This function didn't change anything if early_stop is not set, and it will 3163 * prevent all connection attempts when early_stop is set and the attempts of 3164 * the port are more than 1. 3165 */ 3166 static bool hub_port_stop_enumerate(struct usb_hub *hub, int port1, int retries) 3167 { 3168 struct usb_port *port_dev = hub->ports[port1 - 1]; 3169 3170 if (port_dev->early_stop) { 3171 if (port_dev->ignore_event) 3172 return true; 3173 3174 /* 3175 * We want unsuccessful attempts to fail quickly. 3176 * Since some devices may need one failure during 3177 * port initialization, we allow two tries but no 3178 * more. 3179 */ 3180 if (retries < 2) 3181 return false; 3182 3183 port_dev->ignore_event = 1; 3184 } else 3185 port_dev->ignore_event = 0; 3186 3187 return port_dev->ignore_event; 3188 } 3189 3190 /* Check if a port is power on */ 3191 int usb_port_is_power_on(struct usb_hub *hub, unsigned int portstatus) 3192 { 3193 int ret = 0; 3194 3195 if (hub_is_superspeed(hub->hdev)) { 3196 if (portstatus & USB_SS_PORT_STAT_POWER) 3197 ret = 1; 3198 } else { 3199 if (portstatus & USB_PORT_STAT_POWER) 3200 ret = 1; 3201 } 3202 3203 return ret; 3204 } 3205 3206 static void usb_lock_port(struct usb_port *port_dev) 3207 __acquires(&port_dev->status_lock) 3208 { 3209 mutex_lock(&port_dev->status_lock); 3210 __acquire(&port_dev->status_lock); 3211 } 3212 3213 static void usb_unlock_port(struct usb_port *port_dev) 3214 __releases(&port_dev->status_lock) 3215 { 3216 mutex_unlock(&port_dev->status_lock); 3217 __release(&port_dev->status_lock); 3218 } 3219 3220 #ifdef CONFIG_PM 3221 3222 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 3223 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 3224 { 3225 int ret = 0; 3226 3227 if (hub_is_superspeed(hub->hdev)) { 3228 if ((portstatus & USB_PORT_STAT_LINK_STATE) 3229 == USB_SS_PORT_LS_U3) 3230 ret = 1; 3231 } else { 3232 if (portstatus & USB_PORT_STAT_SUSPEND) 3233 ret = 1; 3234 } 3235 3236 return ret; 3237 } 3238 3239 /* Determine whether the device on a port is ready for a normal resume, 3240 * is ready for a reset-resume, or should be disconnected. 3241 */ 3242 static int check_port_resume_type(struct usb_device *udev, 3243 struct usb_hub *hub, int port1, 3244 int status, u16 portchange, u16 portstatus) 3245 { 3246 struct usb_port *port_dev = hub->ports[port1 - 1]; 3247 int retries = 3; 3248 3249 retry: 3250 /* Is a warm reset needed to recover the connection? */ 3251 if (status == 0 && udev->reset_resume 3252 && hub_port_warm_reset_required(hub, port1, portstatus)) { 3253 /* pass */; 3254 } 3255 /* Is the device still present? */ 3256 else if (status || port_is_suspended(hub, portstatus) || 3257 !usb_port_is_power_on(hub, portstatus)) { 3258 if (status >= 0) 3259 status = -ENODEV; 3260 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 3261 if (retries--) { 3262 usleep_range(200, 300); 3263 status = usb_hub_port_status(hub, port1, &portstatus, 3264 &portchange); 3265 goto retry; 3266 } 3267 status = -ENODEV; 3268 } 3269 3270 /* Can't do a normal resume if the port isn't enabled, 3271 * so try a reset-resume instead. 3272 */ 3273 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 3274 if (udev->persist_enabled) 3275 udev->reset_resume = 1; 3276 else 3277 status = -ENODEV; 3278 } 3279 3280 if (status) { 3281 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", 3282 portchange, portstatus, status); 3283 } else if (udev->reset_resume) { 3284 3285 /* Late port handoff can set status-change bits */ 3286 if (portchange & USB_PORT_STAT_C_CONNECTION) 3287 usb_clear_port_feature(hub->hdev, port1, 3288 USB_PORT_FEAT_C_CONNECTION); 3289 if (portchange & USB_PORT_STAT_C_ENABLE) 3290 usb_clear_port_feature(hub->hdev, port1, 3291 USB_PORT_FEAT_C_ENABLE); 3292 3293 /* 3294 * Whatever made this reset-resume necessary may have 3295 * turned on the port1 bit in hub->change_bits. But after 3296 * a successful reset-resume we want the bit to be clear; 3297 * if it was on it would indicate that something happened 3298 * following the reset-resume. 3299 */ 3300 clear_bit(port1, hub->change_bits); 3301 } 3302 3303 return status; 3304 } 3305 3306 int usb_disable_ltm(struct usb_device *udev) 3307 { 3308 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3309 3310 /* Check if the roothub and device supports LTM. */ 3311 if (!usb_device_supports_ltm(hcd->self.root_hub) || 3312 !usb_device_supports_ltm(udev)) 3313 return 0; 3314 3315 /* Clear Feature LTM Enable can only be sent if the device is 3316 * configured. 3317 */ 3318 if (!udev->actconfig) 3319 return 0; 3320 3321 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3322 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 3323 USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 3324 USB_CTRL_SET_TIMEOUT); 3325 } 3326 EXPORT_SYMBOL_GPL(usb_disable_ltm); 3327 3328 void usb_enable_ltm(struct usb_device *udev) 3329 { 3330 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3331 3332 /* Check if the roothub and device supports LTM. */ 3333 if (!usb_device_supports_ltm(hcd->self.root_hub) || 3334 !usb_device_supports_ltm(udev)) 3335 return; 3336 3337 /* Set Feature LTM Enable can only be sent if the device is 3338 * configured. 3339 */ 3340 if (!udev->actconfig) 3341 return; 3342 3343 usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3344 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 3345 USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 3346 USB_CTRL_SET_TIMEOUT); 3347 } 3348 EXPORT_SYMBOL_GPL(usb_enable_ltm); 3349 3350 /* 3351 * usb_enable_remote_wakeup - enable remote wakeup for a device 3352 * @udev: target device 3353 * 3354 * For USB-2 devices: Set the device's remote wakeup feature. 3355 * 3356 * For USB-3 devices: Assume there's only one function on the device and 3357 * enable remote wake for the first interface. FIXME if the interface 3358 * association descriptor shows there's more than one function. 3359 */ 3360 static int usb_enable_remote_wakeup(struct usb_device *udev) 3361 { 3362 if (udev->speed < USB_SPEED_SUPER) 3363 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3364 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 3365 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 3366 USB_CTRL_SET_TIMEOUT); 3367 else 3368 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3369 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 3370 USB_INTRF_FUNC_SUSPEND, 3371 USB_INTRF_FUNC_SUSPEND_RW | 3372 USB_INTRF_FUNC_SUSPEND_LP, 3373 NULL, 0, USB_CTRL_SET_TIMEOUT); 3374 } 3375 3376 /* 3377 * usb_disable_remote_wakeup - disable remote wakeup for a device 3378 * @udev: target device 3379 * 3380 * For USB-2 devices: Clear the device's remote wakeup feature. 3381 * 3382 * For USB-3 devices: Assume there's only one function on the device and 3383 * disable remote wake for the first interface. FIXME if the interface 3384 * association descriptor shows there's more than one function. 3385 */ 3386 static int usb_disable_remote_wakeup(struct usb_device *udev) 3387 { 3388 if (udev->speed < USB_SPEED_SUPER) 3389 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3390 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 3391 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 3392 USB_CTRL_SET_TIMEOUT); 3393 else 3394 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3395 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 3396 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, 3397 USB_CTRL_SET_TIMEOUT); 3398 } 3399 3400 /* Count of wakeup-enabled devices at or below udev */ 3401 unsigned usb_wakeup_enabled_descendants(struct usb_device *udev) 3402 { 3403 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 3404 3405 return udev->do_remote_wakeup + 3406 (hub ? hub->wakeup_enabled_descendants : 0); 3407 } 3408 EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants); 3409 3410 /* 3411 * usb_port_suspend - suspend a usb device's upstream port 3412 * @udev: device that's no longer in active use, not a root hub 3413 * Context: must be able to sleep; device not locked; pm locks held 3414 * 3415 * Suspends a USB device that isn't in active use, conserving power. 3416 * Devices may wake out of a suspend, if anything important happens, 3417 * using the remote wakeup mechanism. They may also be taken out of 3418 * suspend by the host, using usb_port_resume(). It's also routine 3419 * to disconnect devices while they are suspended. 3420 * 3421 * This only affects the USB hardware for a device; its interfaces 3422 * (and, for hubs, child devices) must already have been suspended. 3423 * 3424 * Selective port suspend reduces power; most suspended devices draw 3425 * less than 500 uA. It's also used in OTG, along with remote wakeup. 3426 * All devices below the suspended port are also suspended. 3427 * 3428 * Devices leave suspend state when the host wakes them up. Some devices 3429 * also support "remote wakeup", where the device can activate the USB 3430 * tree above them to deliver data, such as a keypress or packet. In 3431 * some cases, this wakes the USB host. 3432 * 3433 * Suspending OTG devices may trigger HNP, if that's been enabled 3434 * between a pair of dual-role devices. That will change roles, such 3435 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 3436 * 3437 * Devices on USB hub ports have only one "suspend" state, corresponding 3438 * to ACPI D2, "may cause the device to lose some context". 3439 * State transitions include: 3440 * 3441 * - suspend, resume ... when the VBUS power link stays live 3442 * - suspend, disconnect ... VBUS lost 3443 * 3444 * Once VBUS drop breaks the circuit, the port it's using has to go through 3445 * normal re-enumeration procedures, starting with enabling VBUS power. 3446 * Other than re-initializing the hub (plug/unplug, except for root hubs), 3447 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq 3448 * timer, no SRP, no requests through sysfs. 3449 * 3450 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get 3451 * suspended until their bus goes into global suspend (i.e., the root 3452 * hub is suspended). Nevertheless, we change @udev->state to 3453 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual 3454 * upstream port setting is stored in @udev->port_is_suspended. 3455 * 3456 * Returns 0 on success, else negative errno. 3457 */ 3458 int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 3459 { 3460 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3461 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3462 int port1 = udev->portnum; 3463 int status; 3464 bool really_suspend = true; 3465 3466 usb_lock_port(port_dev); 3467 3468 /* enable remote wakeup when appropriate; this lets the device 3469 * wake up the upstream hub (including maybe the root hub). 3470 * 3471 * NOTE: OTG devices may issue remote wakeup (or SRP) even when 3472 * we don't explicitly enable it here. 3473 */ 3474 if (udev->do_remote_wakeup) { 3475 status = usb_enable_remote_wakeup(udev); 3476 if (status) { 3477 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 3478 status); 3479 /* bail if autosuspend is requested */ 3480 if (PMSG_IS_AUTO(msg)) 3481 goto err_wakeup; 3482 } 3483 } 3484 3485 /* disable USB2 hardware LPM */ 3486 usb_disable_usb2_hardware_lpm(udev); 3487 3488 if (usb_disable_ltm(udev)) { 3489 dev_err(&udev->dev, "Failed to disable LTM before suspend\n"); 3490 status = -ENOMEM; 3491 if (PMSG_IS_AUTO(msg)) 3492 goto err_ltm; 3493 } 3494 3495 /* see 7.1.7.6 */ 3496 if (hub_is_superspeed(hub->hdev)) 3497 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); 3498 3499 /* 3500 * For system suspend, we do not need to enable the suspend feature 3501 * on individual USB-2 ports. The devices will automatically go 3502 * into suspend a few ms after the root hub stops sending packets. 3503 * The USB 2.0 spec calls this "global suspend". 3504 * 3505 * However, many USB hubs have a bug: They don't relay wakeup requests 3506 * from a downstream port if the port's suspend feature isn't on. 3507 * Therefore we will turn on the suspend feature if udev or any of its 3508 * descendants is enabled for remote wakeup. 3509 */ 3510 else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0) 3511 status = set_port_feature(hub->hdev, port1, 3512 USB_PORT_FEAT_SUSPEND); 3513 else { 3514 really_suspend = false; 3515 status = 0; 3516 } 3517 if (status) { 3518 /* Check if the port has been suspended for the timeout case 3519 * to prevent the suspended port from incorrect handling. 3520 */ 3521 if (status == -ETIMEDOUT) { 3522 int ret; 3523 u16 portstatus, portchange; 3524 3525 portstatus = portchange = 0; 3526 ret = usb_hub_port_status(hub, port1, &portstatus, 3527 &portchange); 3528 3529 dev_dbg(&port_dev->dev, 3530 "suspend timeout, status %04x\n", portstatus); 3531 3532 if (ret == 0 && port_is_suspended(hub, portstatus)) { 3533 status = 0; 3534 goto suspend_done; 3535 } 3536 } 3537 3538 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); 3539 3540 /* Try to enable USB3 LTM again */ 3541 usb_enable_ltm(udev); 3542 err_ltm: 3543 /* Try to enable USB2 hardware LPM again */ 3544 usb_enable_usb2_hardware_lpm(udev); 3545 3546 if (udev->do_remote_wakeup) 3547 (void) usb_disable_remote_wakeup(udev); 3548 err_wakeup: 3549 3550 /* System sleep transitions should never fail */ 3551 if (!PMSG_IS_AUTO(msg)) 3552 status = 0; 3553 } else { 3554 suspend_done: 3555 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 3556 (PMSG_IS_AUTO(msg) ? "auto-" : ""), 3557 udev->do_remote_wakeup); 3558 if (really_suspend) { 3559 udev->port_is_suspended = 1; 3560 3561 /* device has up to 10 msec to fully suspend */ 3562 msleep(10); 3563 } 3564 usb_set_device_state(udev, USB_STATE_SUSPENDED); 3565 } 3566 3567 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled 3568 && test_and_clear_bit(port1, hub->child_usage_bits)) 3569 pm_runtime_put_sync(&port_dev->dev); 3570 3571 usb_mark_last_busy(hub->hdev); 3572 3573 usb_unlock_port(port_dev); 3574 return status; 3575 } 3576 3577 /* 3578 * If the USB "suspend" state is in use (rather than "global suspend"), 3579 * many devices will be individually taken out of suspend state using 3580 * special "resume" signaling. This routine kicks in shortly after 3581 * hardware resume signaling is finished, either because of selective 3582 * resume (by host) or remote wakeup (by device) ... now see what changed 3583 * in the tree that's rooted at this device. 3584 * 3585 * If @udev->reset_resume is set then the device is reset before the 3586 * status check is done. 3587 */ 3588 static int finish_port_resume(struct usb_device *udev) 3589 { 3590 int status = 0; 3591 u16 devstatus = 0; 3592 3593 /* caller owns the udev device lock */ 3594 dev_dbg(&udev->dev, "%s\n", 3595 udev->reset_resume ? "finish reset-resume" : "finish resume"); 3596 3597 /* usb ch9 identifies four variants of SUSPENDED, based on what 3598 * state the device resumes to. Linux currently won't see the 3599 * first two on the host side; they'd be inside hub_port_init() 3600 * during many timeouts, but hub_wq can't suspend until later. 3601 */ 3602 usb_set_device_state(udev, udev->actconfig 3603 ? USB_STATE_CONFIGURED 3604 : USB_STATE_ADDRESS); 3605 3606 /* 10.5.4.5 says not to reset a suspended port if the attached 3607 * device is enabled for remote wakeup. Hence the reset 3608 * operation is carried out here, after the port has been 3609 * resumed. 3610 */ 3611 if (udev->reset_resume) { 3612 /* 3613 * If the device morphs or switches modes when it is reset, 3614 * we don't want to perform a reset-resume. We'll fail the 3615 * resume, which will cause a logical disconnect, and then 3616 * the device will be rediscovered. 3617 */ 3618 retry_reset_resume: 3619 if (udev->quirks & USB_QUIRK_RESET) 3620 status = -ENODEV; 3621 else 3622 status = usb_reset_and_verify_device(udev); 3623 } 3624 3625 /* 10.5.4.5 says be sure devices in the tree are still there. 3626 * For now let's assume the device didn't go crazy on resume, 3627 * and device drivers will know about any resume quirks. 3628 */ 3629 if (status == 0) { 3630 devstatus = 0; 3631 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 3632 3633 /* If a normal resume failed, try doing a reset-resume */ 3634 if (status && !udev->reset_resume && udev->persist_enabled) { 3635 dev_dbg(&udev->dev, "retry with reset-resume\n"); 3636 udev->reset_resume = 1; 3637 goto retry_reset_resume; 3638 } 3639 } 3640 3641 if (status) { 3642 dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 3643 status); 3644 /* 3645 * There are a few quirky devices which violate the standard 3646 * by claiming to have remote wakeup enabled after a reset, 3647 * which crash if the feature is cleared, hence check for 3648 * udev->reset_resume 3649 */ 3650 } else if (udev->actconfig && !udev->reset_resume) { 3651 if (udev->speed < USB_SPEED_SUPER) { 3652 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) 3653 status = usb_disable_remote_wakeup(udev); 3654 } else { 3655 status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0, 3656 &devstatus); 3657 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP 3658 | USB_INTRF_STAT_FUNC_RW)) 3659 status = usb_disable_remote_wakeup(udev); 3660 } 3661 3662 if (status) 3663 dev_dbg(&udev->dev, 3664 "disable remote wakeup, status %d\n", 3665 status); 3666 status = 0; 3667 } 3668 return status; 3669 } 3670 3671 /* 3672 * There are some SS USB devices which take longer time for link training. 3673 * XHCI specs 4.19.4 says that when Link training is successful, port 3674 * sets CCS bit to 1. So if SW reads port status before successful link 3675 * training, then it will not find device to be present. 3676 * USB Analyzer log with such buggy devices show that in some cases 3677 * device switch on the RX termination after long delay of host enabling 3678 * the VBUS. In few other cases it has been seen that device fails to 3679 * negotiate link training in first attempt. It has been 3680 * reported till now that few devices take as long as 2000 ms to train 3681 * the link after host enabling its VBUS and termination. Following 3682 * routine implements a 2000 ms timeout for link training. If in a case 3683 * link trains before timeout, loop will exit earlier. 3684 * 3685 * There are also some 2.0 hard drive based devices and 3.0 thumb 3686 * drives that, when plugged into a 2.0 only port, take a long 3687 * time to set CCS after VBUS enable. 3688 * 3689 * FIXME: If a device was connected before suspend, but was removed 3690 * while system was asleep, then the loop in the following routine will 3691 * only exit at timeout. 3692 * 3693 * This routine should only be called when persist is enabled. 3694 */ 3695 static int wait_for_connected(struct usb_device *udev, 3696 struct usb_hub *hub, int port1, 3697 u16 *portchange, u16 *portstatus) 3698 { 3699 int status = 0, delay_ms = 0; 3700 3701 while (delay_ms < 2000) { 3702 if (status || *portstatus & USB_PORT_STAT_CONNECTION) 3703 break; 3704 if (!usb_port_is_power_on(hub, *portstatus)) { 3705 status = -ENODEV; 3706 break; 3707 } 3708 msleep(20); 3709 delay_ms += 20; 3710 status = usb_hub_port_status(hub, port1, portstatus, portchange); 3711 } 3712 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); 3713 return status; 3714 } 3715 3716 /* 3717 * usb_port_resume - re-activate a suspended usb device's upstream port 3718 * @udev: device to re-activate, not a root hub 3719 * Context: must be able to sleep; device not locked; pm locks held 3720 * 3721 * This will re-activate the suspended device, increasing power usage 3722 * while letting drivers communicate again with its endpoints. 3723 * USB resume explicitly guarantees that the power session between 3724 * the host and the device is the same as it was when the device 3725 * suspended. 3726 * 3727 * If @udev->reset_resume is set then this routine won't check that the 3728 * port is still enabled. Furthermore, finish_port_resume() above will 3729 * reset @udev. The end result is that a broken power session can be 3730 * recovered and @udev will appear to persist across a loss of VBUS power. 3731 * 3732 * For example, if a host controller doesn't maintain VBUS suspend current 3733 * during a system sleep or is reset when the system wakes up, all the USB 3734 * power sessions below it will be broken. This is especially troublesome 3735 * for mass-storage devices containing mounted filesystems, since the 3736 * device will appear to have disconnected and all the memory mappings 3737 * to it will be lost. Using the USB_PERSIST facility, the device can be 3738 * made to appear as if it had not disconnected. 3739 * 3740 * This facility can be dangerous. Although usb_reset_and_verify_device() makes 3741 * every effort to insure that the same device is present after the 3742 * reset as before, it cannot provide a 100% guarantee. Furthermore it's 3743 * quite possible for a device to remain unaltered but its media to be 3744 * changed. If the user replaces a flash memory card while the system is 3745 * asleep, he will have only himself to blame when the filesystem on the 3746 * new card is corrupted and the system crashes. 3747 * 3748 * Returns 0 on success, else negative errno. 3749 */ 3750 int usb_port_resume(struct usb_device *udev, pm_message_t msg) 3751 { 3752 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3753 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3754 int port1 = udev->portnum; 3755 int status; 3756 u16 portchange, portstatus; 3757 3758 if (!test_and_set_bit(port1, hub->child_usage_bits)) { 3759 status = pm_runtime_resume_and_get(&port_dev->dev); 3760 if (status < 0) { 3761 dev_dbg(&udev->dev, "can't resume usb port, status %d\n", 3762 status); 3763 return status; 3764 } 3765 } 3766 3767 usb_lock_port(port_dev); 3768 3769 /* Skip the initial Clear-Suspend step for a remote wakeup */ 3770 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 3771 if (status == 0 && !port_is_suspended(hub, portstatus)) { 3772 if (portchange & USB_PORT_STAT_C_SUSPEND) 3773 pm_wakeup_event(&udev->dev, 0); 3774 goto SuspendCleared; 3775 } 3776 3777 /* see 7.1.7.7; affects power usage, but not budgeting */ 3778 if (hub_is_superspeed(hub->hdev)) 3779 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); 3780 else 3781 status = usb_clear_port_feature(hub->hdev, 3782 port1, USB_PORT_FEAT_SUSPEND); 3783 if (status) { 3784 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); 3785 } else { 3786 /* drive resume for USB_RESUME_TIMEOUT msec */ 3787 dev_dbg(&udev->dev, "usb %sresume\n", 3788 (PMSG_IS_AUTO(msg) ? "auto-" : "")); 3789 msleep(USB_RESUME_TIMEOUT); 3790 3791 /* Virtual root hubs can trigger on GET_PORT_STATUS to 3792 * stop resume signaling. Then finish the resume 3793 * sequence. 3794 */ 3795 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 3796 } 3797 3798 SuspendCleared: 3799 if (status == 0) { 3800 udev->port_is_suspended = 0; 3801 if (hub_is_superspeed(hub->hdev)) { 3802 if (portchange & USB_PORT_STAT_C_LINK_STATE) 3803 usb_clear_port_feature(hub->hdev, port1, 3804 USB_PORT_FEAT_C_PORT_LINK_STATE); 3805 } else { 3806 if (portchange & USB_PORT_STAT_C_SUSPEND) 3807 usb_clear_port_feature(hub->hdev, port1, 3808 USB_PORT_FEAT_C_SUSPEND); 3809 } 3810 3811 /* TRSMRCY = 10 msec */ 3812 msleep(10); 3813 } 3814 3815 if (udev->persist_enabled) 3816 status = wait_for_connected(udev, hub, port1, &portchange, 3817 &portstatus); 3818 3819 status = check_port_resume_type(udev, 3820 hub, port1, status, portchange, portstatus); 3821 if (status == 0) 3822 status = finish_port_resume(udev); 3823 if (status < 0) { 3824 dev_dbg(&udev->dev, "can't resume, status %d\n", status); 3825 hub_port_logical_disconnect(hub, port1); 3826 } else { 3827 /* Try to enable USB2 hardware LPM */ 3828 usb_enable_usb2_hardware_lpm(udev); 3829 3830 /* Try to enable USB3 LTM */ 3831 usb_enable_ltm(udev); 3832 } 3833 3834 usb_unlock_port(port_dev); 3835 3836 return status; 3837 } 3838 3839 int usb_remote_wakeup(struct usb_device *udev) 3840 { 3841 int status = 0; 3842 3843 usb_lock_device(udev); 3844 if (udev->state == USB_STATE_SUSPENDED) { 3845 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 3846 status = usb_autoresume_device(udev); 3847 if (status == 0) { 3848 /* Let the drivers do their thing, then... */ 3849 usb_autosuspend_device(udev); 3850 } 3851 } 3852 usb_unlock_device(udev); 3853 return status; 3854 } 3855 3856 /* Returns 1 if there was a remote wakeup and a connect status change. */ 3857 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 3858 u16 portstatus, u16 portchange) 3859 __must_hold(&port_dev->status_lock) 3860 { 3861 struct usb_port *port_dev = hub->ports[port - 1]; 3862 struct usb_device *hdev; 3863 struct usb_device *udev; 3864 int connect_change = 0; 3865 u16 link_state; 3866 int ret; 3867 3868 hdev = hub->hdev; 3869 udev = port_dev->child; 3870 if (!hub_is_superspeed(hdev)) { 3871 if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 3872 return 0; 3873 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 3874 } else { 3875 link_state = portstatus & USB_PORT_STAT_LINK_STATE; 3876 if (!udev || udev->state != USB_STATE_SUSPENDED || 3877 (link_state != USB_SS_PORT_LS_U0 && 3878 link_state != USB_SS_PORT_LS_U1 && 3879 link_state != USB_SS_PORT_LS_U2)) 3880 return 0; 3881 } 3882 3883 if (udev) { 3884 /* TRSMRCY = 10 msec */ 3885 msleep(10); 3886 3887 usb_unlock_port(port_dev); 3888 ret = usb_remote_wakeup(udev); 3889 usb_lock_port(port_dev); 3890 if (ret < 0) 3891 connect_change = 1; 3892 } else { 3893 ret = -ENODEV; 3894 hub_port_disable(hub, port, 1); 3895 } 3896 dev_dbg(&port_dev->dev, "resume, status %d\n", ret); 3897 return connect_change; 3898 } 3899 3900 static int check_ports_changed(struct usb_hub *hub) 3901 { 3902 int port1; 3903 3904 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { 3905 u16 portstatus, portchange; 3906 int status; 3907 3908 status = usb_hub_port_status(hub, port1, &portstatus, &portchange); 3909 if (!status && portchange) 3910 return 1; 3911 } 3912 return 0; 3913 } 3914 3915 static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 3916 { 3917 struct usb_hub *hub = usb_get_intfdata(intf); 3918 struct usb_device *hdev = hub->hdev; 3919 unsigned port1; 3920 3921 /* 3922 * Warn if children aren't already suspended. 3923 * Also, add up the number of wakeup-enabled descendants. 3924 */ 3925 hub->wakeup_enabled_descendants = 0; 3926 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3927 struct usb_port *port_dev = hub->ports[port1 - 1]; 3928 struct usb_device *udev = port_dev->child; 3929 3930 if (udev && udev->can_submit) { 3931 dev_warn(&port_dev->dev, "device %s not suspended yet\n", 3932 dev_name(&udev->dev)); 3933 if (PMSG_IS_AUTO(msg)) 3934 return -EBUSY; 3935 } 3936 if (udev) 3937 hub->wakeup_enabled_descendants += 3938 usb_wakeup_enabled_descendants(udev); 3939 } 3940 3941 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { 3942 /* check if there are changes pending on hub ports */ 3943 if (check_ports_changed(hub)) { 3944 if (PMSG_IS_AUTO(msg)) 3945 return -EBUSY; 3946 pm_wakeup_event(&hdev->dev, 2000); 3947 } 3948 } 3949 3950 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 3951 /* Enable hub to send remote wakeup for all ports. */ 3952 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3953 set_port_feature(hdev, 3954 port1 | 3955 USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 3956 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 3957 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 3958 USB_PORT_FEAT_REMOTE_WAKE_MASK); 3959 } 3960 } 3961 3962 dev_dbg(&intf->dev, "%s\n", __func__); 3963 3964 /* stop hub_wq and related activity */ 3965 hub_quiesce(hub, HUB_SUSPEND); 3966 return 0; 3967 } 3968 3969 /* Report wakeup requests from the ports of a resuming root hub */ 3970 static void report_wakeup_requests(struct usb_hub *hub) 3971 { 3972 struct usb_device *hdev = hub->hdev; 3973 struct usb_device *udev; 3974 struct usb_hcd *hcd; 3975 unsigned long resuming_ports; 3976 int i; 3977 3978 if (hdev->parent) 3979 return; /* Not a root hub */ 3980 3981 hcd = bus_to_hcd(hdev->bus); 3982 if (hcd->driver->get_resuming_ports) { 3983 3984 /* 3985 * The get_resuming_ports() method returns a bitmap (origin 0) 3986 * of ports which have started wakeup signaling but have not 3987 * yet finished resuming. During system resume we will 3988 * resume all the enabled ports, regardless of any wakeup 3989 * signals, which means the wakeup requests would be lost. 3990 * To prevent this, report them to the PM core here. 3991 */ 3992 resuming_ports = hcd->driver->get_resuming_ports(hcd); 3993 for (i = 0; i < hdev->maxchild; ++i) { 3994 if (test_bit(i, &resuming_ports)) { 3995 udev = hub->ports[i]->child; 3996 if (udev) 3997 pm_wakeup_event(&udev->dev, 0); 3998 } 3999 } 4000 } 4001 } 4002 4003 static int hub_resume(struct usb_interface *intf) 4004 { 4005 struct usb_hub *hub = usb_get_intfdata(intf); 4006 4007 dev_dbg(&intf->dev, "%s\n", __func__); 4008 hub_activate(hub, HUB_RESUME); 4009 4010 /* 4011 * This should be called only for system resume, not runtime resume. 4012 * We can't tell the difference here, so some wakeup requests will be 4013 * reported at the wrong time or more than once. This shouldn't 4014 * matter much, so long as they do get reported. 4015 */ 4016 report_wakeup_requests(hub); 4017 return 0; 4018 } 4019 4020 static int hub_reset_resume(struct usb_interface *intf) 4021 { 4022 struct usb_hub *hub = usb_get_intfdata(intf); 4023 4024 dev_dbg(&intf->dev, "%s\n", __func__); 4025 hub_activate(hub, HUB_RESET_RESUME); 4026 return 0; 4027 } 4028 4029 /** 4030 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 4031 * @rhdev: struct usb_device for the root hub 4032 * 4033 * The USB host controller driver calls this function when its root hub 4034 * is resumed and Vbus power has been interrupted or the controller 4035 * has been reset. The routine marks @rhdev as having lost power. 4036 * When the hub driver is resumed it will take notice and carry out 4037 * power-session recovery for all the "USB-PERSIST"-enabled child devices; 4038 * the others will be disconnected. 4039 */ 4040 void usb_root_hub_lost_power(struct usb_device *rhdev) 4041 { 4042 dev_notice(&rhdev->dev, "root hub lost power or was reset\n"); 4043 rhdev->reset_resume = 1; 4044 } 4045 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 4046 4047 static const char * const usb3_lpm_names[] = { 4048 "U0", 4049 "U1", 4050 "U2", 4051 "U3", 4052 }; 4053 4054 /* 4055 * Send a Set SEL control transfer to the device, prior to enabling 4056 * device-initiated U1 or U2. This lets the device know the exit latencies from 4057 * the time the device initiates a U1 or U2 exit, to the time it will receive a 4058 * packet from the host. 4059 * 4060 * This function will fail if the SEL or PEL values for udev are greater than 4061 * the maximum allowed values for the link state to be enabled. 4062 */ 4063 static int usb_req_set_sel(struct usb_device *udev) 4064 { 4065 struct usb_set_sel_req *sel_values; 4066 unsigned long long u1_sel; 4067 unsigned long long u1_pel; 4068 unsigned long long u2_sel; 4069 unsigned long long u2_pel; 4070 int ret; 4071 4072 if (!udev->parent || udev->speed < USB_SPEED_SUPER || !udev->lpm_capable) 4073 return 0; 4074 4075 /* Convert SEL and PEL stored in ns to us */ 4076 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 4077 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 4078 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 4079 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 4080 4081 /* 4082 * Make sure that the calculated SEL and PEL values for the link 4083 * state we're enabling aren't bigger than the max SEL/PEL 4084 * value that will fit in the SET SEL control transfer. 4085 * Otherwise the device would get an incorrect idea of the exit 4086 * latency for the link state, and could start a device-initiated 4087 * U1/U2 when the exit latencies are too high. 4088 */ 4089 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 4090 u1_pel > USB3_LPM_MAX_U1_SEL_PEL || 4091 u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 4092 u2_pel > USB3_LPM_MAX_U2_SEL_PEL) { 4093 dev_dbg(&udev->dev, "Device-initiated U1/U2 disabled due to long SEL or PEL\n"); 4094 return -EINVAL; 4095 } 4096 4097 /* 4098 * usb_enable_lpm() can be called as part of a failed device reset, 4099 * which may be initiated by an error path of a mass storage driver. 4100 * Therefore, use GFP_NOIO. 4101 */ 4102 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); 4103 if (!sel_values) 4104 return -ENOMEM; 4105 4106 sel_values->u1_sel = u1_sel; 4107 sel_values->u1_pel = u1_pel; 4108 sel_values->u2_sel = cpu_to_le16(u2_sel); 4109 sel_values->u2_pel = cpu_to_le16(u2_pel); 4110 4111 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4112 USB_REQ_SET_SEL, 4113 USB_RECIP_DEVICE, 4114 0, 0, 4115 sel_values, sizeof *(sel_values), 4116 USB_CTRL_SET_TIMEOUT); 4117 kfree(sel_values); 4118 4119 if (ret > 0) 4120 udev->lpm_devinit_allow = 1; 4121 4122 return ret; 4123 } 4124 4125 /* 4126 * Enable or disable device-initiated U1 or U2 transitions. 4127 */ 4128 static int usb_set_device_initiated_lpm(struct usb_device *udev, 4129 enum usb3_link_state state, bool enable) 4130 { 4131 int ret; 4132 int feature; 4133 4134 switch (state) { 4135 case USB3_LPM_U1: 4136 feature = USB_DEVICE_U1_ENABLE; 4137 break; 4138 case USB3_LPM_U2: 4139 feature = USB_DEVICE_U2_ENABLE; 4140 break; 4141 default: 4142 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 4143 __func__, str_enable_disable(enable)); 4144 return -EINVAL; 4145 } 4146 4147 if (udev->state != USB_STATE_CONFIGURED) { 4148 dev_dbg(&udev->dev, "%s: Can't %s %s state " 4149 "for unconfigured device.\n", 4150 __func__, str_enable_disable(enable), 4151 usb3_lpm_names[state]); 4152 return 0; 4153 } 4154 4155 if (enable) { 4156 /* 4157 * Now send the control transfer to enable device-initiated LPM 4158 * for either U1 or U2. 4159 */ 4160 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4161 USB_REQ_SET_FEATURE, 4162 USB_RECIP_DEVICE, 4163 feature, 4164 0, NULL, 0, 4165 USB_CTRL_SET_TIMEOUT); 4166 } else { 4167 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4168 USB_REQ_CLEAR_FEATURE, 4169 USB_RECIP_DEVICE, 4170 feature, 4171 0, NULL, 0, 4172 USB_CTRL_SET_TIMEOUT); 4173 } 4174 if (ret < 0) { 4175 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 4176 str_enable_disable(enable), usb3_lpm_names[state]); 4177 return -EBUSY; 4178 } 4179 return 0; 4180 } 4181 4182 static int usb_set_lpm_timeout(struct usb_device *udev, 4183 enum usb3_link_state state, int timeout) 4184 { 4185 int ret; 4186 int feature; 4187 4188 switch (state) { 4189 case USB3_LPM_U1: 4190 feature = USB_PORT_FEAT_U1_TIMEOUT; 4191 break; 4192 case USB3_LPM_U2: 4193 feature = USB_PORT_FEAT_U2_TIMEOUT; 4194 break; 4195 default: 4196 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 4197 __func__); 4198 return -EINVAL; 4199 } 4200 4201 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 4202 timeout != USB3_LPM_DEVICE_INITIATED) { 4203 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 4204 "which is a reserved value.\n", 4205 usb3_lpm_names[state], timeout); 4206 return -EINVAL; 4207 } 4208 4209 ret = set_port_feature(udev->parent, 4210 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 4211 feature); 4212 if (ret < 0) { 4213 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 4214 "error code %i\n", usb3_lpm_names[state], 4215 timeout, ret); 4216 return -EBUSY; 4217 } 4218 if (state == USB3_LPM_U1) 4219 udev->u1_params.timeout = timeout; 4220 else 4221 udev->u2_params.timeout = timeout; 4222 return 0; 4223 } 4224 4225 /* 4226 * Don't allow device intiated U1/U2 if the system exit latency + one bus 4227 * interval is greater than the minimum service interval of any active 4228 * periodic endpoint. See USB 3.2 section 9.4.9 4229 */ 4230 static bool usb_device_may_initiate_lpm(struct usb_device *udev, 4231 enum usb3_link_state state) 4232 { 4233 unsigned int sel; /* us */ 4234 int i, j; 4235 4236 if (!udev->lpm_devinit_allow) 4237 return false; 4238 4239 if (state == USB3_LPM_U1) 4240 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 4241 else if (state == USB3_LPM_U2) 4242 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 4243 else 4244 return false; 4245 4246 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 4247 struct usb_interface *intf; 4248 struct usb_endpoint_descriptor *desc; 4249 unsigned int interval; 4250 4251 intf = udev->actconfig->interface[i]; 4252 if (!intf) 4253 continue; 4254 4255 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) { 4256 desc = &intf->cur_altsetting->endpoint[j].desc; 4257 4258 if (usb_endpoint_xfer_int(desc) || 4259 usb_endpoint_xfer_isoc(desc)) { 4260 interval = (1 << (desc->bInterval - 1)) * 125; 4261 if (sel + 125 > interval) 4262 return false; 4263 } 4264 } 4265 } 4266 return true; 4267 } 4268 4269 /* 4270 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 4271 * U1/U2 entry. 4272 * 4273 * We will attempt to enable U1 or U2, but there are no guarantees that the 4274 * control transfers to set the hub timeout or enable device-initiated U1/U2 4275 * will be successful. 4276 * 4277 * If the control transfer to enable device-initiated U1/U2 entry fails, then 4278 * hub-initiated U1/U2 will be disabled. 4279 * 4280 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 4281 * driver know about it. If that call fails, it should be harmless, and just 4282 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 4283 */ 4284 static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 4285 enum usb3_link_state state) 4286 { 4287 int timeout; 4288 __u8 u1_mel; 4289 __le16 u2_mel; 4290 4291 /* Skip if the device BOS descriptor couldn't be read */ 4292 if (!udev->bos) 4293 return; 4294 4295 u1_mel = udev->bos->ss_cap->bU1devExitLat; 4296 u2_mel = udev->bos->ss_cap->bU2DevExitLat; 4297 4298 /* If the device says it doesn't have *any* exit latency to come out of 4299 * U1 or U2, it's probably lying. Assume it doesn't implement that link 4300 * state. 4301 */ 4302 if ((state == USB3_LPM_U1 && u1_mel == 0) || 4303 (state == USB3_LPM_U2 && u2_mel == 0)) 4304 return; 4305 4306 /* We allow the host controller to set the U1/U2 timeout internally 4307 * first, so that it can change its schedule to account for the 4308 * additional latency to send data to a device in a lower power 4309 * link state. 4310 */ 4311 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 4312 4313 /* xHCI host controller doesn't want to enable this LPM state. */ 4314 if (timeout == 0) 4315 return; 4316 4317 if (timeout < 0) { 4318 dev_warn(&udev->dev, "Could not enable %s link state, " 4319 "xHCI error %i.\n", usb3_lpm_names[state], 4320 timeout); 4321 return; 4322 } 4323 4324 if (usb_set_lpm_timeout(udev, state, timeout)) { 4325 /* If we can't set the parent hub U1/U2 timeout, 4326 * device-initiated LPM won't be allowed either, so let the xHCI 4327 * host know that this link state won't be enabled. 4328 */ 4329 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 4330 return; 4331 } 4332 4333 /* Only a configured device will accept the Set Feature 4334 * U1/U2_ENABLE 4335 */ 4336 if (udev->actconfig && 4337 usb_device_may_initiate_lpm(udev, state)) { 4338 if (usb_set_device_initiated_lpm(udev, state, true)) { 4339 /* 4340 * Request to enable device initiated U1/U2 failed, 4341 * better to turn off lpm in this case. 4342 */ 4343 usb_set_lpm_timeout(udev, state, 0); 4344 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 4345 return; 4346 } 4347 } 4348 4349 if (state == USB3_LPM_U1) 4350 udev->usb3_lpm_u1_enabled = 1; 4351 else if (state == USB3_LPM_U2) 4352 udev->usb3_lpm_u2_enabled = 1; 4353 } 4354 /* 4355 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 4356 * U1/U2 entry. 4357 * 4358 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 4359 * If zero is returned, the parent will not allow the link to go into U1/U2. 4360 * 4361 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 4362 * it won't have an effect on the bus link state because the parent hub will 4363 * still disallow device-initiated U1/U2 entry. 4364 * 4365 * If zero is returned, the xHCI host controller may still think U1/U2 entry is 4366 * possible. The result will be slightly more bus bandwidth will be taken up 4367 * (to account for U1/U2 exit latency), but it should be harmless. 4368 */ 4369 static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 4370 enum usb3_link_state state) 4371 { 4372 switch (state) { 4373 case USB3_LPM_U1: 4374 case USB3_LPM_U2: 4375 break; 4376 default: 4377 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 4378 __func__); 4379 return -EINVAL; 4380 } 4381 4382 if (usb_set_lpm_timeout(udev, state, 0)) 4383 return -EBUSY; 4384 4385 usb_set_device_initiated_lpm(udev, state, false); 4386 4387 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 4388 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 4389 "bus schedule bandwidth may be impacted.\n", 4390 usb3_lpm_names[state]); 4391 4392 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM 4393 * is disabled. Hub will disallows link to enter U1/U2 as well, 4394 * even device is initiating LPM. Hence LPM is disabled if hub LPM 4395 * timeout set to 0, no matter device-initiated LPM is disabled or 4396 * not. 4397 */ 4398 if (state == USB3_LPM_U1) 4399 udev->usb3_lpm_u1_enabled = 0; 4400 else if (state == USB3_LPM_U2) 4401 udev->usb3_lpm_u2_enabled = 0; 4402 4403 return 0; 4404 } 4405 4406 /* 4407 * Disable hub-initiated and device-initiated U1 and U2 entry. 4408 * Caller must own the bandwidth_mutex. 4409 * 4410 * This will call usb_enable_lpm() on failure, which will decrement 4411 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 4412 */ 4413 int usb_disable_lpm(struct usb_device *udev) 4414 { 4415 struct usb_hcd *hcd; 4416 4417 if (!udev || !udev->parent || 4418 udev->speed < USB_SPEED_SUPER || 4419 !udev->lpm_capable || 4420 udev->state < USB_STATE_CONFIGURED) 4421 return 0; 4422 4423 hcd = bus_to_hcd(udev->bus); 4424 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 4425 return 0; 4426 4427 udev->lpm_disable_count++; 4428 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 4429 return 0; 4430 4431 /* If LPM is enabled, attempt to disable it. */ 4432 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 4433 goto enable_lpm; 4434 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 4435 goto enable_lpm; 4436 4437 return 0; 4438 4439 enable_lpm: 4440 usb_enable_lpm(udev); 4441 return -EBUSY; 4442 } 4443 EXPORT_SYMBOL_GPL(usb_disable_lpm); 4444 4445 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 4446 int usb_unlocked_disable_lpm(struct usb_device *udev) 4447 { 4448 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4449 int ret; 4450 4451 if (!hcd) 4452 return -EINVAL; 4453 4454 mutex_lock(hcd->bandwidth_mutex); 4455 ret = usb_disable_lpm(udev); 4456 mutex_unlock(hcd->bandwidth_mutex); 4457 4458 return ret; 4459 } 4460 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 4461 4462 /* 4463 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 4464 * xHCI host policy may prevent U1 or U2 from being enabled. 4465 * 4466 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 4467 * until the lpm_disable_count drops to zero. Caller must own the 4468 * bandwidth_mutex. 4469 */ 4470 void usb_enable_lpm(struct usb_device *udev) 4471 { 4472 struct usb_hcd *hcd; 4473 struct usb_hub *hub; 4474 struct usb_port *port_dev; 4475 4476 if (!udev || !udev->parent || 4477 udev->speed < USB_SPEED_SUPER || 4478 !udev->lpm_capable || 4479 udev->state < USB_STATE_CONFIGURED) 4480 return; 4481 4482 udev->lpm_disable_count--; 4483 hcd = bus_to_hcd(udev->bus); 4484 /* Double check that we can both enable and disable LPM. 4485 * Device must be configured to accept set feature U1/U2 timeout. 4486 */ 4487 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 4488 !hcd->driver->disable_usb3_lpm_timeout) 4489 return; 4490 4491 if (udev->lpm_disable_count > 0) 4492 return; 4493 4494 hub = usb_hub_to_struct_hub(udev->parent); 4495 if (!hub) 4496 return; 4497 4498 port_dev = hub->ports[udev->portnum - 1]; 4499 4500 if (port_dev->usb3_lpm_u1_permit) 4501 usb_enable_link_state(hcd, udev, USB3_LPM_U1); 4502 4503 if (port_dev->usb3_lpm_u2_permit) 4504 usb_enable_link_state(hcd, udev, USB3_LPM_U2); 4505 } 4506 EXPORT_SYMBOL_GPL(usb_enable_lpm); 4507 4508 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 4509 void usb_unlocked_enable_lpm(struct usb_device *udev) 4510 { 4511 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4512 4513 if (!hcd) 4514 return; 4515 4516 mutex_lock(hcd->bandwidth_mutex); 4517 usb_enable_lpm(udev); 4518 mutex_unlock(hcd->bandwidth_mutex); 4519 } 4520 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4521 4522 /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */ 4523 static void hub_usb3_port_prepare_disable(struct usb_hub *hub, 4524 struct usb_port *port_dev) 4525 { 4526 struct usb_device *udev = port_dev->child; 4527 int ret; 4528 4529 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { 4530 ret = hub_set_port_link_state(hub, port_dev->portnum, 4531 USB_SS_PORT_LS_U0); 4532 if (!ret) { 4533 msleep(USB_RESUME_TIMEOUT); 4534 ret = usb_disable_remote_wakeup(udev); 4535 } 4536 if (ret) 4537 dev_warn(&udev->dev, 4538 "Port disable: can't disable remote wake\n"); 4539 udev->do_remote_wakeup = 0; 4540 } 4541 } 4542 4543 #else /* CONFIG_PM */ 4544 4545 #define hub_suspend NULL 4546 #define hub_resume NULL 4547 #define hub_reset_resume NULL 4548 4549 static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, 4550 struct usb_port *port_dev) { } 4551 4552 int usb_disable_lpm(struct usb_device *udev) 4553 { 4554 return 0; 4555 } 4556 EXPORT_SYMBOL_GPL(usb_disable_lpm); 4557 4558 void usb_enable_lpm(struct usb_device *udev) { } 4559 EXPORT_SYMBOL_GPL(usb_enable_lpm); 4560 4561 int usb_unlocked_disable_lpm(struct usb_device *udev) 4562 { 4563 return 0; 4564 } 4565 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 4566 4567 void usb_unlocked_enable_lpm(struct usb_device *udev) { } 4568 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4569 4570 int usb_disable_ltm(struct usb_device *udev) 4571 { 4572 return 0; 4573 } 4574 EXPORT_SYMBOL_GPL(usb_disable_ltm); 4575 4576 void usb_enable_ltm(struct usb_device *udev) { } 4577 EXPORT_SYMBOL_GPL(usb_enable_ltm); 4578 4579 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 4580 u16 portstatus, u16 portchange) 4581 { 4582 return 0; 4583 } 4584 4585 static int usb_req_set_sel(struct usb_device *udev) 4586 { 4587 return 0; 4588 } 4589 4590 #endif /* CONFIG_PM */ 4591 4592 /* 4593 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating 4594 * a connection with a plugged-in cable but will signal the host when the cable 4595 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices 4596 */ 4597 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 4598 { 4599 struct usb_port *port_dev = hub->ports[port1 - 1]; 4600 struct usb_device *hdev = hub->hdev; 4601 int ret = 0; 4602 4603 if (!hub->error) { 4604 if (hub_is_superspeed(hub->hdev)) { 4605 hub_usb3_port_prepare_disable(hub, port_dev); 4606 ret = hub_set_port_link_state(hub, port_dev->portnum, 4607 USB_SS_PORT_LS_U3); 4608 } else { 4609 ret = usb_clear_port_feature(hdev, port1, 4610 USB_PORT_FEAT_ENABLE); 4611 } 4612 } 4613 if (port_dev->child && set_state) 4614 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); 4615 if (ret && ret != -ENODEV) 4616 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); 4617 return ret; 4618 } 4619 4620 /* 4621 * usb_port_disable - disable a usb device's upstream port 4622 * @udev: device to disable 4623 * Context: @udev locked, must be able to sleep. 4624 * 4625 * Disables a USB device that isn't in active use. 4626 */ 4627 int usb_port_disable(struct usb_device *udev) 4628 { 4629 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4630 4631 return hub_port_disable(hub, udev->portnum, 0); 4632 } 4633 4634 /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 4635 * 4636 * Between connect detection and reset signaling there must be a delay 4637 * of 100ms at least for debounce and power-settling. The corresponding 4638 * timer shall restart whenever the downstream port detects a disconnect. 4639 * 4640 * Apparently there are some bluetooth and irda-dongles and a number of 4641 * low-speed devices for which this debounce period may last over a second. 4642 * Not covered by the spec - but easy to deal with. 4643 * 4644 * This implementation uses a 1500ms total debounce timeout; if the 4645 * connection isn't stable by then it returns -ETIMEDOUT. It checks 4646 * every 25ms for transient disconnects. When the port status has been 4647 * unchanged for 100ms it returns the port status. 4648 */ 4649 int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) 4650 { 4651 int ret; 4652 u16 portchange, portstatus; 4653 unsigned connection = 0xffff; 4654 int total_time, stable_time = 0; 4655 struct usb_port *port_dev = hub->ports[port1 - 1]; 4656 4657 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 4658 ret = usb_hub_port_status(hub, port1, &portstatus, &portchange); 4659 if (ret < 0) 4660 return ret; 4661 4662 if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 4663 (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 4664 if (!must_be_connected || 4665 (connection == USB_PORT_STAT_CONNECTION)) 4666 stable_time += HUB_DEBOUNCE_STEP; 4667 if (stable_time >= HUB_DEBOUNCE_STABLE) 4668 break; 4669 } else { 4670 stable_time = 0; 4671 connection = portstatus & USB_PORT_STAT_CONNECTION; 4672 } 4673 4674 if (portchange & USB_PORT_STAT_C_CONNECTION) { 4675 usb_clear_port_feature(hub->hdev, port1, 4676 USB_PORT_FEAT_C_CONNECTION); 4677 } 4678 4679 if (total_time >= HUB_DEBOUNCE_TIMEOUT) 4680 break; 4681 msleep(HUB_DEBOUNCE_STEP); 4682 } 4683 4684 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", 4685 total_time, stable_time, portstatus); 4686 4687 if (stable_time < HUB_DEBOUNCE_STABLE) 4688 return -ETIMEDOUT; 4689 return portstatus; 4690 } 4691 4692 void usb_ep0_reinit(struct usb_device *udev) 4693 { 4694 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 4695 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 4696 usb_enable_endpoint(udev, &udev->ep0, true); 4697 } 4698 EXPORT_SYMBOL_GPL(usb_ep0_reinit); 4699 4700 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 4701 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 4702 4703 static int hub_set_address(struct usb_device *udev, int devnum) 4704 { 4705 int retval; 4706 unsigned int timeout_ms = USB_CTRL_SET_TIMEOUT; 4707 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4708 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4709 4710 if (hub->hdev->quirks & USB_QUIRK_SHORT_SET_ADDRESS_REQ_TIMEOUT) 4711 timeout_ms = USB_SHORT_SET_ADDRESS_REQ_TIMEOUT; 4712 4713 /* 4714 * The host controller will choose the device address, 4715 * instead of the core having chosen it earlier 4716 */ 4717 if (!hcd->driver->address_device && devnum <= 1) 4718 return -EINVAL; 4719 if (udev->state == USB_STATE_ADDRESS) 4720 return 0; 4721 if (udev->state != USB_STATE_DEFAULT) 4722 return -EINVAL; 4723 if (hcd->driver->address_device) 4724 retval = hcd->driver->address_device(hcd, udev, timeout_ms); 4725 else 4726 retval = usb_control_msg(udev, usb_sndaddr0pipe(), 4727 USB_REQ_SET_ADDRESS, 0, devnum, 0, 4728 NULL, 0, timeout_ms); 4729 if (retval == 0) { 4730 update_devnum(udev, devnum); 4731 /* Device now using proper address. */ 4732 usb_set_device_state(udev, USB_STATE_ADDRESS); 4733 usb_ep0_reinit(udev); 4734 } 4735 return retval; 4736 } 4737 4738 /* 4739 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM 4740 * when they're plugged into a USB 2.0 port, but they don't work when LPM is 4741 * enabled. 4742 * 4743 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the 4744 * device says it supports the new USB 2.0 Link PM errata by setting the BESL 4745 * support bit in the BOS descriptor. 4746 */ 4747 static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) 4748 { 4749 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4750 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN; 4751 4752 if (!udev->usb2_hw_lpm_capable || !udev->bos) 4753 return; 4754 4755 if (hub) 4756 connect_type = hub->ports[udev->portnum - 1]->connect_type; 4757 4758 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || 4759 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 4760 udev->usb2_hw_lpm_allowed = 1; 4761 usb_enable_usb2_hardware_lpm(udev); 4762 } 4763 } 4764 4765 static int hub_enable_device(struct usb_device *udev) 4766 { 4767 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4768 4769 if (!hcd->driver->enable_device) 4770 return 0; 4771 if (udev->state == USB_STATE_ADDRESS) 4772 return 0; 4773 if (udev->state != USB_STATE_DEFAULT) 4774 return -EINVAL; 4775 4776 return hcd->driver->enable_device(hcd, udev); 4777 } 4778 4779 /* 4780 * Get the bMaxPacketSize0 value during initialization by reading the 4781 * device's device descriptor. Since we don't already know this value, 4782 * the transfer is unsafe and it ignores I/O errors, only testing for 4783 * reasonable received values. 4784 * 4785 * For "old scheme" initialization, size will be 8 so we read just the 4786 * start of the device descriptor, which should work okay regardless of 4787 * the actual bMaxPacketSize0 value. For "new scheme" initialization, 4788 * size will be 64 (and buf will point to a sufficiently large buffer), 4789 * which might not be kosher according to the USB spec but it's what 4790 * Windows does and what many devices expect. 4791 * 4792 * Returns: bMaxPacketSize0 or a negative error code. 4793 */ 4794 static int get_bMaxPacketSize0(struct usb_device *udev, 4795 struct usb_device_descriptor *buf, int size, bool first_time) 4796 { 4797 int i, rc; 4798 4799 /* 4800 * Retry on all errors; some devices are flakey. 4801 * 255 is for WUSB devices, we actually need to use 4802 * 512 (WUSB1.0[4.8.1]). 4803 */ 4804 for (i = 0; i < GET_MAXPACKET0_TRIES; ++i) { 4805 /* Start with invalid values in case the transfer fails */ 4806 buf->bDescriptorType = buf->bMaxPacketSize0 = 0; 4807 rc = usb_control_msg(udev, usb_rcvaddr0pipe(), 4808 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 4809 USB_DT_DEVICE << 8, 0, 4810 buf, size, 4811 initial_descriptor_timeout); 4812 switch (buf->bMaxPacketSize0) { 4813 case 8: case 16: case 32: case 64: case 9: 4814 if (buf->bDescriptorType == USB_DT_DEVICE) { 4815 rc = buf->bMaxPacketSize0; 4816 break; 4817 } 4818 fallthrough; 4819 default: 4820 if (rc >= 0) 4821 rc = -EPROTO; 4822 break; 4823 } 4824 4825 /* 4826 * Some devices time out if they are powered on 4827 * when already connected. They need a second 4828 * reset, so return early. But only on the first 4829 * attempt, lest we get into a time-out/reset loop. 4830 */ 4831 if (rc > 0 || (rc == -ETIMEDOUT && first_time && 4832 udev->speed > USB_SPEED_FULL)) 4833 break; 4834 } 4835 return rc; 4836 } 4837 4838 #define GET_DESCRIPTOR_BUFSIZE 64 4839 4840 /* Reset device, (re)assign address, get device descriptor. 4841 * Device connection must be stable, no more debouncing needed. 4842 * Returns device in USB_STATE_ADDRESS, except on error. 4843 * 4844 * If this is called for an already-existing device (as part of 4845 * usb_reset_and_verify_device), the caller must own the device lock and 4846 * the port lock. For a newly detected device that is not accessible 4847 * through any global pointers, it's not necessary to lock the device, 4848 * but it is still necessary to lock the port. 4849 * 4850 * For a newly detected device, @dev_descr must be NULL. The device 4851 * descriptor retrieved from the device will then be stored in 4852 * @udev->descriptor. For an already existing device, @dev_descr 4853 * must be non-NULL. The device descriptor will be stored there, 4854 * not in @udev->descriptor, because descriptors for registered 4855 * devices are meant to be immutable. 4856 */ 4857 static int 4858 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, 4859 int retry_counter, struct usb_device_descriptor *dev_descr) 4860 { 4861 struct usb_device *hdev = hub->hdev; 4862 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 4863 struct usb_port *port_dev = hub->ports[port1 - 1]; 4864 int retries, operations, retval, i; 4865 unsigned delay = HUB_SHORT_RESET_TIME; 4866 enum usb_device_speed oldspeed = udev->speed; 4867 const char *speed; 4868 int devnum = udev->devnum; 4869 const char *driver_name; 4870 bool do_new_scheme; 4871 const bool initial = !dev_descr; 4872 int maxp0; 4873 struct usb_device_descriptor *buf, *descr; 4874 4875 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 4876 if (!buf) 4877 return -ENOMEM; 4878 4879 /* root hub ports have a slightly longer reset period 4880 * (from USB 2.0 spec, section 7.1.7.5) 4881 */ 4882 if (!hdev->parent) { 4883 delay = HUB_ROOT_RESET_TIME; 4884 if (port1 == hdev->bus->otg_port) 4885 hdev->bus->b_hnp_enable = 0; 4886 } 4887 4888 /* Some low speed devices have problems with the quick delay, so */ 4889 /* be a bit pessimistic with those devices. RHbug #23670 */ 4890 if (oldspeed == USB_SPEED_LOW) 4891 delay = HUB_LONG_RESET_TIME; 4892 4893 /* Reset the device; full speed may morph to high speed */ 4894 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 4895 retval = hub_port_reset(hub, port1, udev, delay, false); 4896 if (retval < 0) /* error or disconnect */ 4897 goto fail; 4898 /* success, speed is known */ 4899 4900 retval = -ENODEV; 4901 4902 /* Don't allow speed changes at reset, except usb 3.0 to faster */ 4903 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && 4904 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { 4905 dev_dbg(&udev->dev, "device reset changed speed!\n"); 4906 goto fail; 4907 } 4908 oldspeed = udev->speed; 4909 4910 if (initial) { 4911 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 4912 * it's fixed size except for full speed devices. 4913 */ 4914 switch (udev->speed) { 4915 case USB_SPEED_SUPER_PLUS: 4916 case USB_SPEED_SUPER: 4917 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 4918 break; 4919 case USB_SPEED_HIGH: /* fixed at 64 */ 4920 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4921 break; 4922 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 4923 /* to determine the ep0 maxpacket size, try to read 4924 * the device descriptor to get bMaxPacketSize0 and 4925 * then correct our initial guess. 4926 */ 4927 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4928 break; 4929 case USB_SPEED_LOW: /* fixed at 8 */ 4930 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 4931 break; 4932 default: 4933 goto fail; 4934 } 4935 } 4936 4937 speed = usb_speed_string(udev->speed); 4938 4939 /* 4940 * The controller driver may be NULL if the controller device 4941 * is the middle device between platform device and roothub. 4942 * This middle device may not need a device driver due to 4943 * all hardware control can be at platform device driver, this 4944 * platform device is usually a dual-role USB controller device. 4945 */ 4946 if (udev->bus->controller->driver) 4947 driver_name = udev->bus->controller->driver->name; 4948 else 4949 driver_name = udev->bus->sysdev->driver->name; 4950 4951 if (udev->speed < USB_SPEED_SUPER) 4952 dev_info(&udev->dev, 4953 "%s %s USB device number %d using %s\n", 4954 (initial ? "new" : "reset"), speed, 4955 devnum, driver_name); 4956 4957 if (initial) { 4958 /* Set up TT records, if needed */ 4959 if (hdev->tt) { 4960 udev->tt = hdev->tt; 4961 udev->ttport = hdev->ttport; 4962 } else if (udev->speed != USB_SPEED_HIGH 4963 && hdev->speed == USB_SPEED_HIGH) { 4964 if (!hub->tt.hub) { 4965 dev_err(&udev->dev, "parent hub has no TT\n"); 4966 retval = -EINVAL; 4967 goto fail; 4968 } 4969 udev->tt = &hub->tt; 4970 udev->ttport = port1; 4971 } 4972 } 4973 4974 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 4975 * Because device hardware and firmware is sometimes buggy in 4976 * this area, and this is how Linux has done it for ages. 4977 * Change it cautiously. 4978 * 4979 * NOTE: If use_new_scheme() is true we will start by issuing 4980 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 4981 * so it may help with some non-standards-compliant devices. 4982 * Otherwise we start with SET_ADDRESS and then try to read the 4983 * first 8 bytes of the device descriptor to get the ep0 maxpacket 4984 * value. 4985 */ 4986 do_new_scheme = use_new_scheme(udev, retry_counter, port_dev); 4987 4988 for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { 4989 if (hub_port_stop_enumerate(hub, port1, retries)) { 4990 retval = -ENODEV; 4991 break; 4992 } 4993 4994 if (do_new_scheme) { 4995 retval = hub_enable_device(udev); 4996 if (retval < 0) { 4997 dev_err(&udev->dev, 4998 "hub failed to enable device, error %d\n", 4999 retval); 5000 goto fail; 5001 } 5002 5003 maxp0 = get_bMaxPacketSize0(udev, buf, 5004 GET_DESCRIPTOR_BUFSIZE, retries == 0); 5005 if (maxp0 > 0 && !initial && 5006 maxp0 != udev->descriptor.bMaxPacketSize0) { 5007 dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 5008 retval = -ENODEV; 5009 goto fail; 5010 } 5011 5012 retval = hub_port_reset(hub, port1, udev, delay, false); 5013 if (retval < 0) /* error or disconnect */ 5014 goto fail; 5015 if (oldspeed != udev->speed) { 5016 dev_dbg(&udev->dev, 5017 "device reset changed speed!\n"); 5018 retval = -ENODEV; 5019 goto fail; 5020 } 5021 if (maxp0 < 0) { 5022 if (maxp0 != -ENODEV) 5023 dev_err(&udev->dev, "device descriptor read/64, error %d\n", 5024 maxp0); 5025 retval = maxp0; 5026 continue; 5027 } 5028 } 5029 5030 for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) { 5031 retval = hub_set_address(udev, devnum); 5032 if (retval >= 0) 5033 break; 5034 msleep(200); 5035 } 5036 if (retval < 0) { 5037 if (retval != -ENODEV) 5038 dev_err(&udev->dev, "device not accepting address %d, error %d\n", 5039 devnum, retval); 5040 goto fail; 5041 } 5042 if (udev->speed >= USB_SPEED_SUPER) { 5043 devnum = udev->devnum; 5044 dev_info(&udev->dev, 5045 "%s SuperSpeed%s%s USB device number %d using %s\n", 5046 (udev->config) ? "reset" : "new", 5047 (udev->speed == USB_SPEED_SUPER_PLUS) ? 5048 " Plus" : "", 5049 (udev->ssp_rate == USB_SSP_GEN_2x2) ? 5050 " Gen 2x2" : 5051 (udev->ssp_rate == USB_SSP_GEN_2x1) ? 5052 " Gen 2x1" : 5053 (udev->ssp_rate == USB_SSP_GEN_1x2) ? 5054 " Gen 1x2" : "", 5055 devnum, driver_name); 5056 } 5057 5058 /* 5059 * cope with hardware quirkiness: 5060 * - let SET_ADDRESS settle, some device hardware wants it 5061 * - read ep0 maxpacket even for high and low speed, 5062 */ 5063 msleep(10); 5064 5065 if (do_new_scheme) 5066 break; 5067 5068 maxp0 = get_bMaxPacketSize0(udev, buf, 8, retries == 0); 5069 if (maxp0 < 0) { 5070 retval = maxp0; 5071 if (retval != -ENODEV) 5072 dev_err(&udev->dev, 5073 "device descriptor read/8, error %d\n", 5074 retval); 5075 } else { 5076 u32 delay; 5077 5078 if (!initial && maxp0 != udev->descriptor.bMaxPacketSize0) { 5079 dev_err(&udev->dev, "device reset changed ep0 maxpacket size!\n"); 5080 retval = -ENODEV; 5081 goto fail; 5082 } 5083 5084 delay = udev->parent->hub_delay; 5085 udev->hub_delay = min_t(u32, delay, 5086 USB_TP_TRANSMISSION_DELAY_MAX); 5087 retval = usb_set_isoch_delay(udev); 5088 if (retval) { 5089 dev_dbg(&udev->dev, 5090 "Failed set isoch delay, error %d\n", 5091 retval); 5092 retval = 0; 5093 } 5094 break; 5095 } 5096 } 5097 if (retval) 5098 goto fail; 5099 5100 /* 5101 * Check the ep0 maxpacket guess and correct it if necessary. 5102 * maxp0 is the value stored in the device descriptor; 5103 * i is the value it encodes (logarithmic for SuperSpeed or greater). 5104 */ 5105 i = maxp0; 5106 if (udev->speed >= USB_SPEED_SUPER) { 5107 if (maxp0 <= 16) 5108 i = 1 << maxp0; 5109 else 5110 i = 0; /* Invalid */ 5111 } 5112 if (usb_endpoint_maxp(&udev->ep0.desc) == i) { 5113 ; /* Initial ep0 maxpacket guess is right */ 5114 } else if (((udev->speed == USB_SPEED_FULL || 5115 udev->speed == USB_SPEED_HIGH) && 5116 (i == 8 || i == 16 || i == 32 || i == 64)) || 5117 (udev->speed >= USB_SPEED_SUPER && i > 0)) { 5118 /* Initial guess is wrong; use the descriptor's value */ 5119 if (udev->speed == USB_SPEED_FULL) 5120 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 5121 else 5122 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 5123 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 5124 usb_ep0_reinit(udev); 5125 } else { 5126 /* Initial guess is wrong and descriptor's value is invalid */ 5127 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", maxp0); 5128 retval = -EMSGSIZE; 5129 goto fail; 5130 } 5131 5132 descr = usb_get_device_descriptor(udev); 5133 if (IS_ERR(descr)) { 5134 retval = PTR_ERR(descr); 5135 if (retval != -ENODEV) 5136 dev_err(&udev->dev, "device descriptor read/all, error %d\n", 5137 retval); 5138 goto fail; 5139 } 5140 if (initial) 5141 udev->descriptor = *descr; 5142 else 5143 *dev_descr = *descr; 5144 kfree(descr); 5145 5146 /* 5147 * Some superspeed devices have finished the link training process 5148 * and attached to a superspeed hub port, but the device descriptor 5149 * got from those devices show they aren't superspeed devices. Warm 5150 * reset the port attached by the devices can fix them. 5151 */ 5152 if ((udev->speed >= USB_SPEED_SUPER) && 5153 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 5154 dev_err(&udev->dev, "got a wrong device descriptor, warm reset device\n"); 5155 hub_port_reset(hub, port1, udev, HUB_BH_RESET_TIME, true); 5156 retval = -EINVAL; 5157 goto fail; 5158 } 5159 5160 usb_detect_quirks(udev); 5161 5162 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 5163 retval = usb_get_bos_descriptor(udev); 5164 if (!retval) { 5165 udev->lpm_capable = usb_device_supports_lpm(udev); 5166 udev->lpm_disable_count = 1; 5167 usb_set_lpm_parameters(udev); 5168 usb_req_set_sel(udev); 5169 } 5170 } 5171 5172 retval = 0; 5173 /* notify HCD that we have a device connected and addressed */ 5174 if (hcd->driver->update_device) 5175 hcd->driver->update_device(hcd, udev); 5176 hub_set_initial_usb2_lpm_policy(udev); 5177 fail: 5178 if (retval) { 5179 hub_port_disable(hub, port1, 0); 5180 update_devnum(udev, devnum); /* for disconnect processing */ 5181 } 5182 kfree(buf); 5183 return retval; 5184 } 5185 5186 static void 5187 check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) 5188 { 5189 struct usb_qualifier_descriptor *qual; 5190 int status; 5191 5192 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) 5193 return; 5194 5195 qual = kmalloc(sizeof *qual, GFP_KERNEL); 5196 if (qual == NULL) 5197 return; 5198 5199 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0, 5200 qual, sizeof *qual); 5201 if (status == sizeof *qual) { 5202 dev_info(&udev->dev, "not running at top speed; " 5203 "connect to a high speed hub\n"); 5204 /* hub LEDs are probably harder to miss than syslog */ 5205 if (hub->has_indicators) { 5206 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 5207 queue_delayed_work(system_power_efficient_wq, 5208 &hub->leds, 0); 5209 } 5210 } 5211 kfree(qual); 5212 } 5213 5214 static unsigned 5215 hub_power_remaining(struct usb_hub *hub) 5216 { 5217 struct usb_device *hdev = hub->hdev; 5218 int remaining; 5219 int port1; 5220 5221 if (!hub->limited_power) 5222 return 0; 5223 5224 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 5225 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 5226 struct usb_port *port_dev = hub->ports[port1 - 1]; 5227 struct usb_device *udev = port_dev->child; 5228 unsigned unit_load; 5229 int delta; 5230 5231 if (!udev) 5232 continue; 5233 if (hub_is_superspeed(udev)) 5234 unit_load = 150; 5235 else 5236 unit_load = 100; 5237 5238 /* 5239 * Unconfigured devices may not use more than one unit load, 5240 * or 8mA for OTG ports 5241 */ 5242 if (udev->actconfig) 5243 delta = usb_get_max_power(udev, udev->actconfig); 5244 else if (port1 != udev->bus->otg_port || hdev->parent) 5245 delta = unit_load; 5246 else 5247 delta = 8; 5248 if (delta > hub->mA_per_port) 5249 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", 5250 delta, hub->mA_per_port); 5251 remaining -= delta; 5252 } 5253 if (remaining < 0) { 5254 dev_warn(hub->intfdev, "%dmA over power budget!\n", 5255 -remaining); 5256 remaining = 0; 5257 } 5258 return remaining; 5259 } 5260 5261 5262 static int descriptors_changed(struct usb_device *udev, 5263 struct usb_device_descriptor *new_device_descriptor, 5264 struct usb_host_bos *old_bos) 5265 { 5266 int changed = 0; 5267 unsigned index; 5268 unsigned serial_len = 0; 5269 unsigned len; 5270 unsigned old_length; 5271 int length; 5272 char *buf; 5273 5274 if (memcmp(&udev->descriptor, new_device_descriptor, 5275 sizeof(*new_device_descriptor)) != 0) 5276 return 1; 5277 5278 if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) 5279 return 1; 5280 if (udev->bos) { 5281 len = le16_to_cpu(udev->bos->desc->wTotalLength); 5282 if (len != le16_to_cpu(old_bos->desc->wTotalLength)) 5283 return 1; 5284 if (memcmp(udev->bos->desc, old_bos->desc, len)) 5285 return 1; 5286 } 5287 5288 /* Since the idVendor, idProduct, and bcdDevice values in the 5289 * device descriptor haven't changed, we will assume the 5290 * Manufacturer and Product strings haven't changed either. 5291 * But the SerialNumber string could be different (e.g., a 5292 * different flash card of the same brand). 5293 */ 5294 if (udev->serial) 5295 serial_len = strlen(udev->serial) + 1; 5296 5297 len = serial_len; 5298 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5299 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5300 len = max(len, old_length); 5301 } 5302 5303 buf = kmalloc(len, GFP_NOIO); 5304 if (!buf) 5305 /* assume the worst */ 5306 return 1; 5307 5308 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5309 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5310 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 5311 old_length); 5312 if (length != old_length) { 5313 dev_dbg(&udev->dev, "config index %d, error %d\n", 5314 index, length); 5315 changed = 1; 5316 break; 5317 } 5318 if (memcmp(buf, udev->rawdescriptors[index], old_length) 5319 != 0) { 5320 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 5321 index, 5322 ((struct usb_config_descriptor *) buf)-> 5323 bConfigurationValue); 5324 changed = 1; 5325 break; 5326 } 5327 } 5328 5329 if (!changed && serial_len) { 5330 length = usb_string(udev, udev->descriptor.iSerialNumber, 5331 buf, serial_len); 5332 if (length + 1 != serial_len) { 5333 dev_dbg(&udev->dev, "serial string error %d\n", 5334 length); 5335 changed = 1; 5336 } else if (memcmp(buf, udev->serial, length) != 0) { 5337 dev_dbg(&udev->dev, "serial string changed\n"); 5338 changed = 1; 5339 } 5340 } 5341 5342 kfree(buf); 5343 return changed; 5344 } 5345 5346 static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, 5347 u16 portchange) 5348 { 5349 int status = -ENODEV; 5350 int i; 5351 unsigned unit_load; 5352 struct usb_device *hdev = hub->hdev; 5353 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 5354 struct usb_port *port_dev = hub->ports[port1 - 1]; 5355 struct usb_device *udev = port_dev->child; 5356 static int unreliable_port = -1; 5357 bool retry_locked; 5358 5359 /* Disconnect any existing devices under this port */ 5360 if (udev) { 5361 if (hcd->usb_phy && !hdev->parent) 5362 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); 5363 usb_disconnect(&port_dev->child); 5364 } 5365 5366 /* We can forget about a "removed" device when there's a physical 5367 * disconnect or the connect status changes. 5368 */ 5369 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 5370 (portchange & USB_PORT_STAT_C_CONNECTION)) 5371 clear_bit(port1, hub->removed_bits); 5372 5373 if (portchange & (USB_PORT_STAT_C_CONNECTION | 5374 USB_PORT_STAT_C_ENABLE)) { 5375 status = hub_port_debounce_be_stable(hub, port1); 5376 if (status < 0) { 5377 if (status != -ENODEV && 5378 port1 != unreliable_port && 5379 printk_ratelimit()) 5380 dev_err(&port_dev->dev, "connect-debounce failed\n"); 5381 portstatus &= ~USB_PORT_STAT_CONNECTION; 5382 unreliable_port = port1; 5383 } else { 5384 portstatus = status; 5385 } 5386 } 5387 5388 /* Return now if debouncing failed or nothing is connected or 5389 * the device was "removed". 5390 */ 5391 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 5392 test_bit(port1, hub->removed_bits)) { 5393 5394 /* 5395 * maybe switch power back on (e.g. root hub was reset) 5396 * but only if the port isn't owned by someone else. 5397 */ 5398 if (hub_is_port_power_switchable(hub) 5399 && !usb_port_is_power_on(hub, portstatus) 5400 && !port_dev->port_owner) 5401 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 5402 5403 if (portstatus & USB_PORT_STAT_ENABLE) 5404 goto done; 5405 return; 5406 } 5407 if (hub_is_superspeed(hub->hdev)) 5408 unit_load = 150; 5409 else 5410 unit_load = 100; 5411 5412 status = 0; 5413 5414 for (i = 0; i < PORT_INIT_TRIES; i++) { 5415 if (hub_port_stop_enumerate(hub, port1, i)) { 5416 status = -ENODEV; 5417 break; 5418 } 5419 5420 usb_lock_port(port_dev); 5421 mutex_lock(hcd->address0_mutex); 5422 retry_locked = true; 5423 /* reallocate for each attempt, since references 5424 * to the previous one can escape in various ways 5425 */ 5426 udev = usb_alloc_dev(hdev, hdev->bus, port1); 5427 if (!udev) { 5428 dev_err(&port_dev->dev, 5429 "couldn't allocate usb_device\n"); 5430 mutex_unlock(hcd->address0_mutex); 5431 usb_unlock_port(port_dev); 5432 goto done; 5433 } 5434 5435 usb_set_device_state(udev, USB_STATE_POWERED); 5436 udev->bus_mA = hub->mA_per_port; 5437 udev->level = hdev->level + 1; 5438 5439 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ 5440 if (hub_is_superspeed(hub->hdev)) 5441 udev->speed = USB_SPEED_SUPER; 5442 else 5443 udev->speed = USB_SPEED_UNKNOWN; 5444 5445 choose_devnum(udev); 5446 if (udev->devnum <= 0) { 5447 status = -ENOTCONN; /* Don't retry */ 5448 goto loop; 5449 } 5450 5451 /* reset (non-USB 3.0 devices) and get descriptor */ 5452 status = hub_port_init(hub, udev, port1, i, NULL); 5453 if (status < 0) 5454 goto loop; 5455 5456 mutex_unlock(hcd->address0_mutex); 5457 usb_unlock_port(port_dev); 5458 retry_locked = false; 5459 5460 if (udev->quirks & USB_QUIRK_DELAY_INIT) 5461 msleep(2000); 5462 5463 /* consecutive bus-powered hubs aren't reliable; they can 5464 * violate the voltage drop budget. if the new child has 5465 * a "powered" LED, users should notice we didn't enable it 5466 * (without reading syslog), even without per-port LEDs 5467 * on the parent. 5468 */ 5469 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 5470 && udev->bus_mA <= unit_load) { 5471 u16 devstat; 5472 5473 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, 5474 &devstat); 5475 if (status) { 5476 dev_dbg(&udev->dev, "get status %d ?\n", status); 5477 goto loop_disable; 5478 } 5479 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 5480 dev_err(&udev->dev, 5481 "can't connect bus-powered hub " 5482 "to this port\n"); 5483 if (hub->has_indicators) { 5484 hub->indicator[port1-1] = 5485 INDICATOR_AMBER_BLINK; 5486 queue_delayed_work( 5487 system_power_efficient_wq, 5488 &hub->leds, 0); 5489 } 5490 status = -ENOTCONN; /* Don't retry */ 5491 goto loop_disable; 5492 } 5493 } 5494 5495 /* check for devices running slower than they could */ 5496 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 5497 && udev->speed == USB_SPEED_FULL 5498 && highspeed_hubs != 0) 5499 check_highspeed(hub, udev, port1); 5500 5501 /* Store the parent's children[] pointer. At this point 5502 * udev becomes globally accessible, although presumably 5503 * no one will look at it until hdev is unlocked. 5504 */ 5505 status = 0; 5506 5507 mutex_lock(&usb_port_peer_mutex); 5508 5509 /* We mustn't add new devices if the parent hub has 5510 * been disconnected; we would race with the 5511 * recursively_mark_NOTATTACHED() routine. 5512 */ 5513 spin_lock_irq(&device_state_lock); 5514 if (hdev->state == USB_STATE_NOTATTACHED) 5515 status = -ENOTCONN; 5516 else 5517 port_dev->child = udev; 5518 spin_unlock_irq(&device_state_lock); 5519 mutex_unlock(&usb_port_peer_mutex); 5520 5521 /* Run it through the hoops (find a driver, etc) */ 5522 if (!status) { 5523 status = usb_new_device(udev); 5524 if (status) { 5525 mutex_lock(&usb_port_peer_mutex); 5526 spin_lock_irq(&device_state_lock); 5527 port_dev->child = NULL; 5528 spin_unlock_irq(&device_state_lock); 5529 mutex_unlock(&usb_port_peer_mutex); 5530 } else { 5531 if (hcd->usb_phy && !hdev->parent) 5532 usb_phy_notify_connect(hcd->usb_phy, 5533 udev->speed); 5534 } 5535 } 5536 5537 if (status) 5538 goto loop_disable; 5539 5540 status = hub_power_remaining(hub); 5541 if (status) 5542 dev_dbg(hub->intfdev, "%dmA power budget left\n", status); 5543 5544 return; 5545 5546 loop_disable: 5547 hub_port_disable(hub, port1, 1); 5548 loop: 5549 usb_ep0_reinit(udev); 5550 release_devnum(udev); 5551 hub_free_dev(udev); 5552 if (retry_locked) { 5553 mutex_unlock(hcd->address0_mutex); 5554 usb_unlock_port(port_dev); 5555 } 5556 usb_put_dev(udev); 5557 if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 5558 break; 5559 5560 /* When halfway through our retry count, power-cycle the port */ 5561 if (i == (PORT_INIT_TRIES - 1) / 2) { 5562 dev_info(&port_dev->dev, "attempt power cycle\n"); 5563 usb_hub_set_port_power(hdev, hub, port1, false); 5564 msleep(2 * hub_power_on_good_delay(hub)); 5565 usb_hub_set_port_power(hdev, hub, port1, true); 5566 msleep(hub_power_on_good_delay(hub)); 5567 } 5568 } 5569 if (hub->hdev->parent || 5570 !hcd->driver->port_handed_over || 5571 !(hcd->driver->port_handed_over)(hcd, port1)) { 5572 if (status != -ENOTCONN && status != -ENODEV) 5573 dev_err(&port_dev->dev, 5574 "unable to enumerate USB device\n"); 5575 } 5576 5577 done: 5578 hub_port_disable(hub, port1, 1); 5579 if (hcd->driver->relinquish_port && !hub->hdev->parent) { 5580 if (status != -ENOTCONN && status != -ENODEV) 5581 hcd->driver->relinquish_port(hcd, port1); 5582 } 5583 } 5584 5585 /* Handle physical or logical connection change events. 5586 * This routine is called when: 5587 * a port connection-change occurs; 5588 * a port enable-change occurs (often caused by EMI); 5589 * usb_reset_and_verify_device() encounters changed descriptors (as from 5590 * a firmware download) 5591 * caller already locked the hub 5592 */ 5593 static void hub_port_connect_change(struct usb_hub *hub, int port1, 5594 u16 portstatus, u16 portchange) 5595 __must_hold(&port_dev->status_lock) 5596 { 5597 struct usb_port *port_dev = hub->ports[port1 - 1]; 5598 struct usb_device *udev = port_dev->child; 5599 struct usb_device_descriptor *descr; 5600 int status = -ENODEV; 5601 5602 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, 5603 portchange, portspeed(hub, portstatus)); 5604 5605 if (hub->has_indicators) { 5606 set_port_led(hub, port1, HUB_LED_AUTO); 5607 hub->indicator[port1-1] = INDICATOR_AUTO; 5608 } 5609 5610 #ifdef CONFIG_USB_OTG 5611 /* during HNP, don't repeat the debounce */ 5612 if (hub->hdev->bus->is_b_host) 5613 portchange &= ~(USB_PORT_STAT_C_CONNECTION | 5614 USB_PORT_STAT_C_ENABLE); 5615 #endif 5616 5617 /* Try to resuscitate an existing device */ 5618 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 5619 udev->state != USB_STATE_NOTATTACHED) { 5620 if (portstatus & USB_PORT_STAT_ENABLE) { 5621 /* 5622 * USB-3 connections are initialized automatically by 5623 * the hostcontroller hardware. Therefore check for 5624 * changed device descriptors before resuscitating the 5625 * device. 5626 */ 5627 descr = usb_get_device_descriptor(udev); 5628 if (IS_ERR(descr)) { 5629 dev_dbg(&udev->dev, 5630 "can't read device descriptor %ld\n", 5631 PTR_ERR(descr)); 5632 } else { 5633 if (descriptors_changed(udev, descr, 5634 udev->bos)) { 5635 dev_dbg(&udev->dev, 5636 "device descriptor has changed\n"); 5637 } else { 5638 status = 0; /* Nothing to do */ 5639 } 5640 kfree(descr); 5641 } 5642 #ifdef CONFIG_PM 5643 } else if (udev->state == USB_STATE_SUSPENDED && 5644 udev->persist_enabled) { 5645 /* For a suspended device, treat this as a 5646 * remote wakeup event. 5647 */ 5648 usb_unlock_port(port_dev); 5649 status = usb_remote_wakeup(udev); 5650 usb_lock_port(port_dev); 5651 #endif 5652 } else { 5653 /* Don't resuscitate */; 5654 } 5655 } 5656 clear_bit(port1, hub->change_bits); 5657 5658 /* successfully revalidated the connection */ 5659 if (status == 0) 5660 return; 5661 5662 usb_unlock_port(port_dev); 5663 hub_port_connect(hub, port1, portstatus, portchange); 5664 usb_lock_port(port_dev); 5665 } 5666 5667 /* Handle notifying userspace about hub over-current events */ 5668 static void port_over_current_notify(struct usb_port *port_dev) 5669 { 5670 char *envp[3] = { NULL, NULL, NULL }; 5671 struct device *hub_dev; 5672 char *port_dev_path; 5673 5674 sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); 5675 5676 hub_dev = port_dev->dev.parent; 5677 5678 if (!hub_dev) 5679 return; 5680 5681 port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); 5682 if (!port_dev_path) 5683 return; 5684 5685 envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path); 5686 if (!envp[0]) 5687 goto exit; 5688 5689 envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u", 5690 port_dev->over_current_count); 5691 if (!envp[1]) 5692 goto exit; 5693 5694 kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); 5695 5696 exit: 5697 kfree(envp[1]); 5698 kfree(envp[0]); 5699 kfree(port_dev_path); 5700 } 5701 5702 static void port_event(struct usb_hub *hub, int port1) 5703 __must_hold(&port_dev->status_lock) 5704 { 5705 int connect_change; 5706 struct usb_port *port_dev = hub->ports[port1 - 1]; 5707 struct usb_device *udev = port_dev->child; 5708 struct usb_device *hdev = hub->hdev; 5709 u16 portstatus, portchange; 5710 int i = 0; 5711 5712 connect_change = test_bit(port1, hub->change_bits); 5713 clear_bit(port1, hub->event_bits); 5714 clear_bit(port1, hub->wakeup_bits); 5715 5716 if (usb_hub_port_status(hub, port1, &portstatus, &portchange) < 0) 5717 return; 5718 5719 if (portchange & USB_PORT_STAT_C_CONNECTION) { 5720 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 5721 connect_change = 1; 5722 } 5723 5724 if (portchange & USB_PORT_STAT_C_ENABLE) { 5725 if (!connect_change) 5726 dev_dbg(&port_dev->dev, "enable change, status %08x\n", 5727 portstatus); 5728 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 5729 5730 /* 5731 * EM interference sometimes causes badly shielded USB devices 5732 * to be shutdown by the hub, this hack enables them again. 5733 * Works at least with mouse driver. 5734 */ 5735 if (!(portstatus & USB_PORT_STAT_ENABLE) 5736 && !connect_change && udev) { 5737 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); 5738 connect_change = 1; 5739 } 5740 } 5741 5742 if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 5743 u16 status = 0, unused; 5744 port_dev->over_current_count++; 5745 port_over_current_notify(port_dev); 5746 5747 dev_dbg(&port_dev->dev, "over-current change #%u\n", 5748 port_dev->over_current_count); 5749 usb_clear_port_feature(hdev, port1, 5750 USB_PORT_FEAT_C_OVER_CURRENT); 5751 msleep(100); /* Cool down */ 5752 hub_power_on(hub, true); 5753 usb_hub_port_status(hub, port1, &status, &unused); 5754 if (status & USB_PORT_STAT_OVERCURRENT) 5755 dev_err(&port_dev->dev, "over-current condition\n"); 5756 } 5757 5758 if (portchange & USB_PORT_STAT_C_RESET) { 5759 dev_dbg(&port_dev->dev, "reset change\n"); 5760 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET); 5761 } 5762 if ((portchange & USB_PORT_STAT_C_BH_RESET) 5763 && hub_is_superspeed(hdev)) { 5764 dev_dbg(&port_dev->dev, "warm reset change\n"); 5765 usb_clear_port_feature(hdev, port1, 5766 USB_PORT_FEAT_C_BH_PORT_RESET); 5767 } 5768 if (portchange & USB_PORT_STAT_C_LINK_STATE) { 5769 dev_dbg(&port_dev->dev, "link state change\n"); 5770 usb_clear_port_feature(hdev, port1, 5771 USB_PORT_FEAT_C_PORT_LINK_STATE); 5772 } 5773 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 5774 dev_warn(&port_dev->dev, "config error\n"); 5775 usb_clear_port_feature(hdev, port1, 5776 USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 5777 } 5778 5779 /* skip port actions that require the port to be powered on */ 5780 if (!pm_runtime_active(&port_dev->dev)) 5781 return; 5782 5783 /* skip port actions if ignore_event and early_stop are true */ 5784 if (port_dev->ignore_event && port_dev->early_stop) 5785 return; 5786 5787 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) 5788 connect_change = 1; 5789 5790 /* 5791 * Avoid trying to recover a USB3 SS.Inactive port with a warm reset if 5792 * the device was disconnected. A 12ms disconnect detect timer in 5793 * SS.Inactive state transitions the port to RxDetect automatically. 5794 * SS.Inactive link error state is common during device disconnect. 5795 */ 5796 while (hub_port_warm_reset_required(hub, port1, portstatus)) { 5797 if ((i++ < DETECT_DISCONNECT_TRIES) && udev) { 5798 u16 unused; 5799 5800 msleep(20); 5801 usb_hub_port_status(hub, port1, &portstatus, &unused); 5802 dev_dbg(&port_dev->dev, "Wait for inactive link disconnect detect\n"); 5803 continue; 5804 } else if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION) 5805 || udev->state == USB_STATE_NOTATTACHED) { 5806 dev_dbg(&port_dev->dev, "do warm reset, port only\n"); 5807 if (hub_port_reset(hub, port1, NULL, 5808 HUB_BH_RESET_TIME, true) < 0) 5809 hub_port_disable(hub, port1, 1); 5810 } else { 5811 dev_dbg(&port_dev->dev, "do warm reset, full device\n"); 5812 usb_unlock_port(port_dev); 5813 usb_lock_device(udev); 5814 usb_reset_device(udev); 5815 usb_unlock_device(udev); 5816 usb_lock_port(port_dev); 5817 connect_change = 0; 5818 } 5819 break; 5820 } 5821 5822 if (connect_change) 5823 hub_port_connect_change(hub, port1, portstatus, portchange); 5824 } 5825 5826 static void hub_event(struct work_struct *work) 5827 { 5828 struct usb_device *hdev; 5829 struct usb_interface *intf; 5830 struct usb_hub *hub; 5831 struct device *hub_dev; 5832 u16 hubstatus; 5833 u16 hubchange; 5834 int i, ret; 5835 5836 hub = container_of(work, struct usb_hub, events); 5837 hdev = hub->hdev; 5838 hub_dev = hub->intfdev; 5839 intf = to_usb_interface(hub_dev); 5840 5841 kcov_remote_start_usb((u64)hdev->bus->busnum); 5842 5843 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 5844 hdev->state, hdev->maxchild, 5845 /* NOTE: expects max 15 ports... */ 5846 (u16) hub->change_bits[0], 5847 (u16) hub->event_bits[0]); 5848 5849 /* Lock the device, then check to see if we were 5850 * disconnected while waiting for the lock to succeed. */ 5851 usb_lock_device(hdev); 5852 if (unlikely(hub->disconnected)) 5853 goto out_hdev_lock; 5854 5855 /* If the hub has died, clean up after it */ 5856 if (hdev->state == USB_STATE_NOTATTACHED) { 5857 hub->error = -ENODEV; 5858 hub_quiesce(hub, HUB_DISCONNECT); 5859 goto out_hdev_lock; 5860 } 5861 5862 /* Autoresume */ 5863 ret = usb_autopm_get_interface(intf); 5864 if (ret) { 5865 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 5866 goto out_hdev_lock; 5867 } 5868 5869 /* If this is an inactive hub, do nothing */ 5870 if (hub->quiescing) 5871 goto out_autopm; 5872 5873 if (hub->error) { 5874 dev_dbg(hub_dev, "resetting for error %d\n", hub->error); 5875 5876 ret = usb_reset_device(hdev); 5877 if (ret) { 5878 dev_dbg(hub_dev, "error resetting hub: %d\n", ret); 5879 goto out_autopm; 5880 } 5881 5882 hub->nerrors = 0; 5883 hub->error = 0; 5884 } 5885 5886 /* deal with port status changes */ 5887 for (i = 1; i <= hdev->maxchild; i++) { 5888 struct usb_port *port_dev = hub->ports[i - 1]; 5889 5890 if (test_bit(i, hub->event_bits) 5891 || test_bit(i, hub->change_bits) 5892 || test_bit(i, hub->wakeup_bits)) { 5893 /* 5894 * The get_noresume and barrier ensure that if 5895 * the port was in the process of resuming, we 5896 * flush that work and keep the port active for 5897 * the duration of the port_event(). However, 5898 * if the port is runtime pm suspended 5899 * (powered-off), we leave it in that state, run 5900 * an abbreviated port_event(), and move on. 5901 */ 5902 pm_runtime_get_noresume(&port_dev->dev); 5903 pm_runtime_barrier(&port_dev->dev); 5904 usb_lock_port(port_dev); 5905 port_event(hub, i); 5906 usb_unlock_port(port_dev); 5907 pm_runtime_put_sync(&port_dev->dev); 5908 } 5909 } 5910 5911 /* deal with hub status changes */ 5912 if (test_and_clear_bit(0, hub->event_bits) == 0) 5913 ; /* do nothing */ 5914 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 5915 dev_err(hub_dev, "get_hub_status failed\n"); 5916 else { 5917 if (hubchange & HUB_CHANGE_LOCAL_POWER) { 5918 dev_dbg(hub_dev, "power change\n"); 5919 clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 5920 if (hubstatus & HUB_STATUS_LOCAL_POWER) 5921 /* FIXME: Is this always true? */ 5922 hub->limited_power = 1; 5923 else 5924 hub->limited_power = 0; 5925 } 5926 if (hubchange & HUB_CHANGE_OVERCURRENT) { 5927 u16 status = 0; 5928 u16 unused; 5929 5930 dev_dbg(hub_dev, "over-current change\n"); 5931 clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 5932 msleep(500); /* Cool down */ 5933 hub_power_on(hub, true); 5934 hub_hub_status(hub, &status, &unused); 5935 if (status & HUB_STATUS_OVERCURRENT) 5936 dev_err(hub_dev, "over-current condition\n"); 5937 } 5938 } 5939 5940 out_autopm: 5941 /* Balance the usb_autopm_get_interface() above */ 5942 usb_autopm_put_interface_no_suspend(intf); 5943 out_hdev_lock: 5944 usb_unlock_device(hdev); 5945 5946 /* Balance the stuff in kick_hub_wq() and allow autosuspend */ 5947 usb_autopm_put_interface(intf); 5948 hub_put(hub); 5949 5950 kcov_remote_stop(); 5951 } 5952 5953 static const struct usb_device_id hub_id_table[] = { 5954 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5955 | USB_DEVICE_ID_MATCH_PRODUCT 5956 | USB_DEVICE_ID_MATCH_INT_CLASS, 5957 .idVendor = USB_VENDOR_SMSC, 5958 .idProduct = USB_PRODUCT_USB5534B, 5959 .bInterfaceClass = USB_CLASS_HUB, 5960 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5961 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5962 | USB_DEVICE_ID_MATCH_PRODUCT, 5963 .idVendor = USB_VENDOR_CYPRESS, 5964 .idProduct = USB_PRODUCT_CY7C65632, 5965 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5966 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5967 | USB_DEVICE_ID_MATCH_INT_CLASS, 5968 .idVendor = USB_VENDOR_GENESYS_LOGIC, 5969 .bInterfaceClass = USB_CLASS_HUB, 5970 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, 5971 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5972 | USB_DEVICE_ID_MATCH_PRODUCT, 5973 .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS, 5974 .idProduct = USB_PRODUCT_TUSB8041_USB2, 5975 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5976 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5977 | USB_DEVICE_ID_MATCH_PRODUCT, 5978 .idVendor = USB_VENDOR_TEXAS_INSTRUMENTS, 5979 .idProduct = USB_PRODUCT_TUSB8041_USB3, 5980 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5981 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5982 | USB_DEVICE_ID_MATCH_PRODUCT, 5983 .idVendor = USB_VENDOR_MICROCHIP, 5984 .idProduct = USB_PRODUCT_USB4913, 5985 .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 5986 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5987 | USB_DEVICE_ID_MATCH_PRODUCT, 5988 .idVendor = USB_VENDOR_MICROCHIP, 5989 .idProduct = USB_PRODUCT_USB4914, 5990 .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 5991 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5992 | USB_DEVICE_ID_MATCH_PRODUCT, 5993 .idVendor = USB_VENDOR_MICROCHIP, 5994 .idProduct = USB_PRODUCT_USB4915, 5995 .driver_info = HUB_QUIRK_REDUCE_FRAME_INTR_BINTERVAL}, 5996 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 5997 .bDeviceClass = USB_CLASS_HUB}, 5998 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 5999 .bInterfaceClass = USB_CLASS_HUB}, 6000 { } /* Terminating entry */ 6001 }; 6002 6003 MODULE_DEVICE_TABLE(usb, hub_id_table); 6004 6005 static struct usb_driver hub_driver = { 6006 .name = "hub", 6007 .probe = hub_probe, 6008 .disconnect = hub_disconnect, 6009 .suspend = hub_suspend, 6010 .resume = hub_resume, 6011 .reset_resume = hub_reset_resume, 6012 .pre_reset = hub_pre_reset, 6013 .post_reset = hub_post_reset, 6014 .unlocked_ioctl = hub_ioctl, 6015 .id_table = hub_id_table, 6016 .supports_autosuspend = 1, 6017 }; 6018 6019 int usb_hub_init(void) 6020 { 6021 if (usb_register(&hub_driver) < 0) { 6022 printk(KERN_ERR "%s: can't register hub driver\n", 6023 usbcore_name); 6024 return -1; 6025 } 6026 6027 /* 6028 * The workqueue needs to be freezable to avoid interfering with 6029 * USB-PERSIST port handover. Otherwise it might see that a full-speed 6030 * device was gone before the EHCI controller had handed its port 6031 * over to the companion full-speed controller. 6032 */ 6033 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0); 6034 if (hub_wq) 6035 return 0; 6036 6037 /* Fall through if kernel_thread failed */ 6038 usb_deregister(&hub_driver); 6039 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); 6040 6041 return -1; 6042 } 6043 6044 void usb_hub_cleanup(void) 6045 { 6046 destroy_workqueue(hub_wq); 6047 6048 /* 6049 * Hub resources are freed for us by usb_deregister. It calls 6050 * usb_driver_purge on every device which in turn calls that 6051 * devices disconnect function if it is using this driver. 6052 * The hub_disconnect function takes care of releasing the 6053 * individual hub resources. -greg 6054 */ 6055 usb_deregister(&hub_driver); 6056 } /* usb_hub_cleanup() */ 6057 6058 /** 6059 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 6060 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 6061 * 6062 * WARNING - don't use this routine to reset a composite device 6063 * (one with multiple interfaces owned by separate drivers)! 6064 * Use usb_reset_device() instead. 6065 * 6066 * Do a port reset, reassign the device's address, and establish its 6067 * former operating configuration. If the reset fails, or the device's 6068 * descriptors change from their values before the reset, or the original 6069 * configuration and altsettings cannot be restored, a flag will be set 6070 * telling hub_wq to pretend the device has been disconnected and then 6071 * re-connected. All drivers will be unbound, and the device will be 6072 * re-enumerated and probed all over again. 6073 * 6074 * Return: 0 if the reset succeeded, -ENODEV if the device has been 6075 * flagged for logical disconnection, or some other negative error code 6076 * if the reset wasn't even attempted. 6077 * 6078 * Note: 6079 * The caller must own the device lock and the port lock, the latter is 6080 * taken by usb_reset_device(). For example, it's safe to use 6081 * usb_reset_device() from a driver probe() routine after downloading 6082 * new firmware. For calls that might not occur during probe(), drivers 6083 * should lock the device using usb_lock_device_for_reset(). 6084 * 6085 * Locking exception: This routine may also be called from within an 6086 * autoresume handler. Such usage won't conflict with other tasks 6087 * holding the device lock because these tasks should always call 6088 * usb_autopm_resume_device(), thereby preventing any unwanted 6089 * autoresume. The autoresume handler is expected to have already 6090 * acquired the port lock before calling this routine. 6091 */ 6092 static int usb_reset_and_verify_device(struct usb_device *udev) 6093 { 6094 struct usb_device *parent_hdev = udev->parent; 6095 struct usb_hub *parent_hub; 6096 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 6097 struct usb_device_descriptor descriptor; 6098 struct usb_host_bos *bos; 6099 int i, j, ret = 0; 6100 int port1 = udev->portnum; 6101 6102 if (udev->state == USB_STATE_NOTATTACHED || 6103 udev->state == USB_STATE_SUSPENDED) { 6104 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 6105 udev->state); 6106 return -EINVAL; 6107 } 6108 6109 if (!parent_hdev) 6110 return -EISDIR; 6111 6112 parent_hub = usb_hub_to_struct_hub(parent_hdev); 6113 6114 /* Disable USB2 hardware LPM. 6115 * It will be re-enabled by the enumeration process. 6116 */ 6117 usb_disable_usb2_hardware_lpm(udev); 6118 6119 bos = udev->bos; 6120 udev->bos = NULL; 6121 6122 mutex_lock(hcd->address0_mutex); 6123 6124 for (i = 0; i < PORT_INIT_TRIES; ++i) { 6125 if (hub_port_stop_enumerate(parent_hub, port1, i)) { 6126 ret = -ENODEV; 6127 break; 6128 } 6129 6130 /* ep0 maxpacket size may change; let the HCD know about it. 6131 * Other endpoints will be handled by re-enumeration. */ 6132 usb_ep0_reinit(udev); 6133 ret = hub_port_init(parent_hub, udev, port1, i, &descriptor); 6134 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 6135 break; 6136 } 6137 mutex_unlock(hcd->address0_mutex); 6138 6139 if (ret < 0) 6140 goto re_enumerate; 6141 6142 /* Device might have changed firmware (DFU or similar) */ 6143 if (descriptors_changed(udev, &descriptor, bos)) { 6144 dev_info(&udev->dev, "device firmware changed\n"); 6145 goto re_enumerate; 6146 } 6147 6148 /* Restore the device's previous configuration */ 6149 if (!udev->actconfig) 6150 goto done; 6151 6152 mutex_lock(hcd->bandwidth_mutex); 6153 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 6154 if (ret < 0) { 6155 dev_warn(&udev->dev, 6156 "Busted HC? Not enough HCD resources for " 6157 "old configuration.\n"); 6158 mutex_unlock(hcd->bandwidth_mutex); 6159 goto re_enumerate; 6160 } 6161 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 6162 USB_REQ_SET_CONFIGURATION, 0, 6163 udev->actconfig->desc.bConfigurationValue, 0, 6164 NULL, 0, USB_CTRL_SET_TIMEOUT); 6165 if (ret < 0) { 6166 dev_err(&udev->dev, 6167 "can't restore configuration #%d (error=%d)\n", 6168 udev->actconfig->desc.bConfigurationValue, ret); 6169 mutex_unlock(hcd->bandwidth_mutex); 6170 goto re_enumerate; 6171 } 6172 mutex_unlock(hcd->bandwidth_mutex); 6173 usb_set_device_state(udev, USB_STATE_CONFIGURED); 6174 6175 /* Put interfaces back into the same altsettings as before. 6176 * Don't bother to send the Set-Interface request for interfaces 6177 * that were already in altsetting 0; besides being unnecessary, 6178 * many devices can't handle it. Instead just reset the host-side 6179 * endpoint state. 6180 */ 6181 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 6182 struct usb_host_config *config = udev->actconfig; 6183 struct usb_interface *intf = config->interface[i]; 6184 struct usb_interface_descriptor *desc; 6185 6186 desc = &intf->cur_altsetting->desc; 6187 if (desc->bAlternateSetting == 0) { 6188 usb_disable_interface(udev, intf, true); 6189 usb_enable_interface(udev, intf, true); 6190 ret = 0; 6191 } else { 6192 /* Let the bandwidth allocation function know that this 6193 * device has been reset, and it will have to use 6194 * alternate setting 0 as the current alternate setting. 6195 */ 6196 intf->resetting_device = 1; 6197 ret = usb_set_interface(udev, desc->bInterfaceNumber, 6198 desc->bAlternateSetting); 6199 intf->resetting_device = 0; 6200 } 6201 if (ret < 0) { 6202 dev_err(&udev->dev, "failed to restore interface %d " 6203 "altsetting %d (error=%d)\n", 6204 desc->bInterfaceNumber, 6205 desc->bAlternateSetting, 6206 ret); 6207 goto re_enumerate; 6208 } 6209 /* Resetting also frees any allocated streams */ 6210 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) 6211 intf->cur_altsetting->endpoint[j].streams = 0; 6212 } 6213 6214 done: 6215 /* Now that the alt settings are re-installed, enable LTM and LPM. */ 6216 usb_enable_usb2_hardware_lpm(udev); 6217 usb_unlocked_enable_lpm(udev); 6218 usb_enable_ltm(udev); 6219 usb_release_bos_descriptor(udev); 6220 udev->bos = bos; 6221 return 0; 6222 6223 re_enumerate: 6224 usb_release_bos_descriptor(udev); 6225 udev->bos = bos; 6226 hub_port_logical_disconnect(parent_hub, port1); 6227 return -ENODEV; 6228 } 6229 6230 /** 6231 * usb_reset_device - warn interface drivers and perform a USB port reset 6232 * @udev: device to reset (not in NOTATTACHED state) 6233 * 6234 * Warns all drivers bound to registered interfaces (using their pre_reset 6235 * method), performs the port reset, and then lets the drivers know that 6236 * the reset is over (using their post_reset method). 6237 * 6238 * Return: The same as for usb_reset_and_verify_device(). 6239 * However, if a reset is already in progress (for instance, if a 6240 * driver doesn't have pre_reset() or post_reset() callbacks, and while 6241 * being unbound or re-bound during the ongoing reset its disconnect() 6242 * or probe() routine tries to perform a second, nested reset), the 6243 * routine returns -EINPROGRESS. 6244 * 6245 * Note: 6246 * The caller must own the device lock. For example, it's safe to use 6247 * this from a driver probe() routine after downloading new firmware. 6248 * For calls that might not occur during probe(), drivers should lock 6249 * the device using usb_lock_device_for_reset(). 6250 * 6251 * If an interface is currently being probed or disconnected, we assume 6252 * its driver knows how to handle resets. For all other interfaces, 6253 * if the driver doesn't have pre_reset and post_reset methods then 6254 * we attempt to unbind it and rebind afterward. 6255 */ 6256 int usb_reset_device(struct usb_device *udev) 6257 { 6258 int ret; 6259 int i; 6260 unsigned int noio_flag; 6261 struct usb_port *port_dev; 6262 struct usb_host_config *config = udev->actconfig; 6263 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 6264 6265 if (udev->state == USB_STATE_NOTATTACHED) { 6266 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 6267 udev->state); 6268 return -EINVAL; 6269 } 6270 6271 if (!udev->parent) { 6272 /* this requires hcd-specific logic; see ohci_restart() */ 6273 dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 6274 return -EISDIR; 6275 } 6276 6277 if (udev->reset_in_progress) 6278 return -EINPROGRESS; 6279 udev->reset_in_progress = 1; 6280 6281 port_dev = hub->ports[udev->portnum - 1]; 6282 6283 /* 6284 * Don't allocate memory with GFP_KERNEL in current 6285 * context to avoid possible deadlock if usb mass 6286 * storage interface or usbnet interface(iSCSI case) 6287 * is included in current configuration. The easist 6288 * approach is to do it for every device reset, 6289 * because the device 'memalloc_noio' flag may have 6290 * not been set before reseting the usb device. 6291 */ 6292 noio_flag = memalloc_noio_save(); 6293 6294 /* Prevent autosuspend during the reset */ 6295 usb_autoresume_device(udev); 6296 6297 if (config) { 6298 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 6299 struct usb_interface *cintf = config->interface[i]; 6300 struct usb_driver *drv; 6301 int unbind = 0; 6302 6303 if (cintf->dev.driver) { 6304 drv = to_usb_driver(cintf->dev.driver); 6305 if (drv->pre_reset && drv->post_reset) 6306 unbind = (drv->pre_reset)(cintf); 6307 else if (cintf->condition == 6308 USB_INTERFACE_BOUND) 6309 unbind = 1; 6310 if (unbind) 6311 usb_forced_unbind_intf(cintf); 6312 } 6313 } 6314 } 6315 6316 usb_lock_port(port_dev); 6317 ret = usb_reset_and_verify_device(udev); 6318 usb_unlock_port(port_dev); 6319 6320 if (config) { 6321 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 6322 struct usb_interface *cintf = config->interface[i]; 6323 struct usb_driver *drv; 6324 int rebind = cintf->needs_binding; 6325 6326 if (!rebind && cintf->dev.driver) { 6327 drv = to_usb_driver(cintf->dev.driver); 6328 if (drv->post_reset) 6329 rebind = (drv->post_reset)(cintf); 6330 else if (cintf->condition == 6331 USB_INTERFACE_BOUND) 6332 rebind = 1; 6333 if (rebind) 6334 cintf->needs_binding = 1; 6335 } 6336 } 6337 6338 /* If the reset failed, hub_wq will unbind drivers later */ 6339 if (ret == 0) 6340 usb_unbind_and_rebind_marked_interfaces(udev); 6341 } 6342 6343 usb_autosuspend_device(udev); 6344 memalloc_noio_restore(noio_flag); 6345 udev->reset_in_progress = 0; 6346 return ret; 6347 } 6348 EXPORT_SYMBOL_GPL(usb_reset_device); 6349 6350 6351 /** 6352 * usb_queue_reset_device - Reset a USB device from an atomic context 6353 * @iface: USB interface belonging to the device to reset 6354 * 6355 * This function can be used to reset a USB device from an atomic 6356 * context, where usb_reset_device() won't work (as it blocks). 6357 * 6358 * Doing a reset via this method is functionally equivalent to calling 6359 * usb_reset_device(), except for the fact that it is delayed to a 6360 * workqueue. This means that any drivers bound to other interfaces 6361 * might be unbound, as well as users from usbfs in user space. 6362 * 6363 * Corner cases: 6364 * 6365 * - Scheduling two resets at the same time from two different drivers 6366 * attached to two different interfaces of the same device is 6367 * possible; depending on how the driver attached to each interface 6368 * handles ->pre_reset(), the second reset might happen or not. 6369 * 6370 * - If the reset is delayed so long that the interface is unbound from 6371 * its driver, the reset will be skipped. 6372 * 6373 * - This function can be called during .probe(). It can also be called 6374 * during .disconnect(), but doing so is pointless because the reset 6375 * will not occur. If you really want to reset the device during 6376 * .disconnect(), call usb_reset_device() directly -- but watch out 6377 * for nested unbinding issues! 6378 */ 6379 void usb_queue_reset_device(struct usb_interface *iface) 6380 { 6381 if (schedule_work(&iface->reset_ws)) 6382 usb_get_intf(iface); 6383 } 6384 EXPORT_SYMBOL_GPL(usb_queue_reset_device); 6385 6386 /** 6387 * usb_hub_find_child - Get the pointer of child device 6388 * attached to the port which is specified by @port1. 6389 * @hdev: USB device belonging to the usb hub 6390 * @port1: port num to indicate which port the child device 6391 * is attached to. 6392 * 6393 * USB drivers call this function to get hub's child device 6394 * pointer. 6395 * 6396 * Return: %NULL if input param is invalid and 6397 * child's usb_device pointer if non-NULL. 6398 */ 6399 struct usb_device *usb_hub_find_child(struct usb_device *hdev, 6400 int port1) 6401 { 6402 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6403 6404 if (port1 < 1 || port1 > hdev->maxchild) 6405 return NULL; 6406 return hub->ports[port1 - 1]->child; 6407 } 6408 EXPORT_SYMBOL_GPL(usb_hub_find_child); 6409 6410 void usb_hub_adjust_deviceremovable(struct usb_device *hdev, 6411 struct usb_hub_descriptor *desc) 6412 { 6413 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6414 enum usb_port_connect_type connect_type; 6415 int i; 6416 6417 if (!hub) 6418 return; 6419 6420 if (!hub_is_superspeed(hdev)) { 6421 for (i = 1; i <= hdev->maxchild; i++) { 6422 struct usb_port *port_dev = hub->ports[i - 1]; 6423 6424 connect_type = port_dev->connect_type; 6425 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 6426 u8 mask = 1 << (i%8); 6427 6428 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { 6429 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 6430 desc->u.hs.DeviceRemovable[i/8] |= mask; 6431 } 6432 } 6433 } 6434 } else { 6435 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); 6436 6437 for (i = 1; i <= hdev->maxchild; i++) { 6438 struct usb_port *port_dev = hub->ports[i - 1]; 6439 6440 connect_type = port_dev->connect_type; 6441 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 6442 u16 mask = 1 << i; 6443 6444 if (!(port_removable & mask)) { 6445 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 6446 port_removable |= mask; 6447 } 6448 } 6449 } 6450 6451 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); 6452 } 6453 } 6454 6455 #ifdef CONFIG_ACPI 6456 /** 6457 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle 6458 * @hdev: USB device belonging to the usb hub 6459 * @port1: port num of the port 6460 * 6461 * Return: Port's acpi handle if successful, %NULL if params are 6462 * invalid. 6463 */ 6464 acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, 6465 int port1) 6466 { 6467 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6468 6469 if (!hub) 6470 return NULL; 6471 6472 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); 6473 } 6474 #endif 6475