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