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