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 if (!hdev) 2438 return; 2439 2440 hub = usb_hub_to_struct_hub(udev->parent); 2441 2442 /* 2443 * If the platform firmware has provided information about a port, 2444 * use that to determine whether it's removable. 2445 */ 2446 switch (hub->ports[udev->portnum - 1]->connect_type) { 2447 case USB_PORT_CONNECT_TYPE_HOT_PLUG: 2448 udev->removable = USB_DEVICE_REMOVABLE; 2449 return; 2450 case USB_PORT_CONNECT_TYPE_HARD_WIRED: 2451 case USB_PORT_NOT_USED: 2452 udev->removable = USB_DEVICE_FIXED; 2453 return; 2454 default: 2455 break; 2456 } 2457 2458 /* 2459 * Otherwise, check whether the hub knows whether a port is removable 2460 * or not 2461 */ 2462 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); 2463 2464 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) 2465 return; 2466 2467 if (hub_is_superspeed(hdev)) { 2468 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) 2469 & (1 << port)) 2470 removable = false; 2471 } else { 2472 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) 2473 removable = false; 2474 } 2475 2476 if (removable) 2477 udev->removable = USB_DEVICE_REMOVABLE; 2478 else 2479 udev->removable = USB_DEVICE_FIXED; 2480 2481 } 2482 2483 /** 2484 * usb_new_device - perform initial device setup (usbcore-internal) 2485 * @udev: newly addressed device (in ADDRESS state) 2486 * 2487 * This is called with devices which have been detected but not fully 2488 * enumerated. The device descriptor is available, but not descriptors 2489 * for any device configuration. The caller must have locked either 2490 * the parent hub (if udev is a normal device) or else the 2491 * usb_bus_idr_lock (if udev is a root hub). The parent's pointer to 2492 * udev has already been installed, but udev is not yet visible through 2493 * sysfs or other filesystem code. 2494 * 2495 * This call is synchronous, and may not be used in an interrupt context. 2496 * 2497 * Only the hub driver or root-hub registrar should ever call this. 2498 * 2499 * Return: Whether the device is configured properly or not. Zero if the 2500 * interface was registered with the driver core; else a negative errno 2501 * value. 2502 * 2503 */ 2504 int usb_new_device(struct usb_device *udev) 2505 { 2506 int err; 2507 2508 if (udev->parent) { 2509 /* Initialize non-root-hub device wakeup to disabled; 2510 * device (un)configuration controls wakeup capable 2511 * sysfs power/wakeup controls wakeup enabled/disabled 2512 */ 2513 device_init_wakeup(&udev->dev, 0); 2514 } 2515 2516 /* Tell the runtime-PM framework the device is active */ 2517 pm_runtime_set_active(&udev->dev); 2518 pm_runtime_get_noresume(&udev->dev); 2519 pm_runtime_use_autosuspend(&udev->dev); 2520 pm_runtime_enable(&udev->dev); 2521 2522 /* By default, forbid autosuspend for all devices. It will be 2523 * allowed for hubs during binding. 2524 */ 2525 usb_disable_autosuspend(udev); 2526 2527 err = usb_enumerate_device(udev); /* Read descriptors */ 2528 if (err < 0) 2529 goto fail; 2530 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", 2531 udev->devnum, udev->bus->busnum, 2532 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 2533 /* export the usbdev device-node for libusb */ 2534 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, 2535 (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); 2536 2537 /* Tell the world! */ 2538 announce_device(udev); 2539 2540 if (udev->serial) 2541 add_device_randomness(udev->serial, strlen(udev->serial)); 2542 if (udev->product) 2543 add_device_randomness(udev->product, strlen(udev->product)); 2544 if (udev->manufacturer) 2545 add_device_randomness(udev->manufacturer, 2546 strlen(udev->manufacturer)); 2547 2548 device_enable_async_suspend(&udev->dev); 2549 2550 /* check whether the hub or firmware marks this port as non-removable */ 2551 if (udev->parent) 2552 set_usb_port_removable(udev); 2553 2554 /* Register the device. The device driver is responsible 2555 * for configuring the device and invoking the add-device 2556 * notifier chain (used by usbfs and possibly others). 2557 */ 2558 err = device_add(&udev->dev); 2559 if (err) { 2560 dev_err(&udev->dev, "can't device_add, error %d\n", err); 2561 goto fail; 2562 } 2563 2564 /* Create link files between child device and usb port device. */ 2565 if (udev->parent) { 2566 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 2567 int port1 = udev->portnum; 2568 struct usb_port *port_dev = hub->ports[port1 - 1]; 2569 2570 err = sysfs_create_link(&udev->dev.kobj, 2571 &port_dev->dev.kobj, "port"); 2572 if (err) 2573 goto fail; 2574 2575 err = sysfs_create_link(&port_dev->dev.kobj, 2576 &udev->dev.kobj, "device"); 2577 if (err) { 2578 sysfs_remove_link(&udev->dev.kobj, "port"); 2579 goto fail; 2580 } 2581 2582 if (!test_and_set_bit(port1, hub->child_usage_bits)) 2583 pm_runtime_get_sync(&port_dev->dev); 2584 } 2585 2586 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); 2587 usb_mark_last_busy(udev); 2588 pm_runtime_put_sync_autosuspend(&udev->dev); 2589 return err; 2590 2591 fail: 2592 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 2593 pm_runtime_disable(&udev->dev); 2594 pm_runtime_set_suspended(&udev->dev); 2595 return err; 2596 } 2597 2598 2599 /** 2600 * usb_deauthorize_device - deauthorize a device (usbcore-internal) 2601 * @usb_dev: USB device 2602 * 2603 * Move the USB device to a very basic state where interfaces are disabled 2604 * and the device is in fact unconfigured and unusable. 2605 * 2606 * We share a lock (that we have) with device_del(), so we need to 2607 * defer its call. 2608 * 2609 * Return: 0. 2610 */ 2611 int usb_deauthorize_device(struct usb_device *usb_dev) 2612 { 2613 usb_lock_device(usb_dev); 2614 if (usb_dev->authorized == 0) 2615 goto out_unauthorized; 2616 2617 usb_dev->authorized = 0; 2618 usb_set_configuration(usb_dev, -1); 2619 2620 out_unauthorized: 2621 usb_unlock_device(usb_dev); 2622 return 0; 2623 } 2624 2625 2626 int usb_authorize_device(struct usb_device *usb_dev) 2627 { 2628 int result = 0, c; 2629 2630 usb_lock_device(usb_dev); 2631 if (usb_dev->authorized == 1) 2632 goto out_authorized; 2633 2634 result = usb_autoresume_device(usb_dev); 2635 if (result < 0) { 2636 dev_err(&usb_dev->dev, 2637 "can't autoresume for authorization: %d\n", result); 2638 goto error_autoresume; 2639 } 2640 2641 if (usb_dev->wusb) { 2642 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); 2643 if (result < 0) { 2644 dev_err(&usb_dev->dev, "can't re-read device descriptor for " 2645 "authorization: %d\n", result); 2646 goto error_device_descriptor; 2647 } 2648 } 2649 2650 usb_dev->authorized = 1; 2651 /* Choose and set the configuration. This registers the interfaces 2652 * with the driver core and lets interface drivers bind to them. 2653 */ 2654 c = usb_choose_configuration(usb_dev); 2655 if (c >= 0) { 2656 result = usb_set_configuration(usb_dev, c); 2657 if (result) { 2658 dev_err(&usb_dev->dev, 2659 "can't set config #%d, error %d\n", c, result); 2660 /* This need not be fatal. The user can try to 2661 * set other configurations. */ 2662 } 2663 } 2664 dev_info(&usb_dev->dev, "authorized to connect\n"); 2665 2666 error_device_descriptor: 2667 usb_autosuspend_device(usb_dev); 2668 error_autoresume: 2669 out_authorized: 2670 usb_unlock_device(usb_dev); /* complements locktree */ 2671 return result; 2672 } 2673 2674 /** 2675 * get_port_ssp_rate - Match the extended port status to SSP rate 2676 * @hdev: The hub device 2677 * @ext_portstatus: extended port status 2678 * 2679 * Match the extended port status speed id to the SuperSpeed Plus sublink speed 2680 * capability attributes. Base on the number of connected lanes and speed, 2681 * return the corresponding enum usb_ssp_rate. 2682 */ 2683 static enum usb_ssp_rate get_port_ssp_rate(struct usb_device *hdev, 2684 u32 ext_portstatus) 2685 { 2686 struct usb_ssp_cap_descriptor *ssp_cap = hdev->bos->ssp_cap; 2687 u32 attr; 2688 u8 speed_id; 2689 u8 ssac; 2690 u8 lanes; 2691 int i; 2692 2693 if (!ssp_cap) 2694 goto out; 2695 2696 speed_id = ext_portstatus & USB_EXT_PORT_STAT_RX_SPEED_ID; 2697 lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 2698 2699 ssac = le32_to_cpu(ssp_cap->bmAttributes) & 2700 USB_SSP_SUBLINK_SPEED_ATTRIBS; 2701 2702 for (i = 0; i <= ssac; i++) { 2703 u8 ssid; 2704 2705 attr = le32_to_cpu(ssp_cap->bmSublinkSpeedAttr[i]); 2706 ssid = FIELD_GET(USB_SSP_SUBLINK_SPEED_SSID, attr); 2707 if (speed_id == ssid) { 2708 u16 mantissa; 2709 u8 lse; 2710 u8 type; 2711 2712 /* 2713 * Note: currently asymmetric lane types are only 2714 * applicable for SSIC operate in SuperSpeed protocol 2715 */ 2716 type = FIELD_GET(USB_SSP_SUBLINK_SPEED_ST, attr); 2717 if (type == USB_SSP_SUBLINK_SPEED_ST_ASYM_RX || 2718 type == USB_SSP_SUBLINK_SPEED_ST_ASYM_TX) 2719 goto out; 2720 2721 if (FIELD_GET(USB_SSP_SUBLINK_SPEED_LP, attr) != 2722 USB_SSP_SUBLINK_SPEED_LP_SSP) 2723 goto out; 2724 2725 lse = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSE, attr); 2726 mantissa = FIELD_GET(USB_SSP_SUBLINK_SPEED_LSM, attr); 2727 2728 /* Convert to Gbps */ 2729 for (; lse < USB_SSP_SUBLINK_SPEED_LSE_GBPS; lse++) 2730 mantissa /= 1000; 2731 2732 if (mantissa >= 10 && lanes == 1) 2733 return USB_SSP_GEN_2x1; 2734 2735 if (mantissa >= 10 && lanes == 2) 2736 return USB_SSP_GEN_2x2; 2737 2738 if (mantissa >= 5 && lanes == 2) 2739 return USB_SSP_GEN_1x2; 2740 2741 goto out; 2742 } 2743 } 2744 2745 out: 2746 return USB_SSP_GEN_UNKNOWN; 2747 } 2748 2749 /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ 2750 static unsigned hub_is_wusb(struct usb_hub *hub) 2751 { 2752 struct usb_hcd *hcd; 2753 if (hub->hdev->parent != NULL) /* not a root hub? */ 2754 return 0; 2755 hcd = bus_to_hcd(hub->hdev->bus); 2756 return hcd->wireless; 2757 } 2758 2759 2760 #ifdef CONFIG_USB_FEW_INIT_RETRIES 2761 #define PORT_RESET_TRIES 2 2762 #define SET_ADDRESS_TRIES 1 2763 #define GET_DESCRIPTOR_TRIES 1 2764 #define GET_MAXPACKET0_TRIES 1 2765 #define PORT_INIT_TRIES 4 2766 2767 #else 2768 #define PORT_RESET_TRIES 5 2769 #define SET_ADDRESS_TRIES 2 2770 #define GET_DESCRIPTOR_TRIES 2 2771 #define GET_MAXPACKET0_TRIES 3 2772 #define PORT_INIT_TRIES 4 2773 #endif /* CONFIG_USB_FEW_INIT_RETRIES */ 2774 2775 #define HUB_ROOT_RESET_TIME 60 /* times are in msec */ 2776 #define HUB_SHORT_RESET_TIME 10 2777 #define HUB_BH_RESET_TIME 50 2778 #define HUB_LONG_RESET_TIME 200 2779 #define HUB_RESET_TIMEOUT 800 2780 2781 static bool use_new_scheme(struct usb_device *udev, int retry, 2782 struct usb_port *port_dev) 2783 { 2784 int old_scheme_first_port = 2785 (port_dev->quirks & USB_PORT_QUIRK_OLD_SCHEME) || 2786 old_scheme_first; 2787 2788 /* 2789 * "New scheme" enumeration causes an extra state transition to be 2790 * exposed to an xhci host and causes USB3 devices to receive control 2791 * commands in the default state. This has been seen to cause 2792 * enumeration failures, so disable this enumeration scheme for USB3 2793 * devices. 2794 */ 2795 if (udev->speed >= USB_SPEED_SUPER) 2796 return false; 2797 2798 /* 2799 * If use_both_schemes is set, use the first scheme (whichever 2800 * it is) for the larger half of the retries, then use the other 2801 * scheme. Otherwise, use the first scheme for all the retries. 2802 */ 2803 if (use_both_schemes && retry >= (PORT_INIT_TRIES + 1) / 2) 2804 return old_scheme_first_port; /* Second half */ 2805 return !old_scheme_first_port; /* First half or all */ 2806 } 2807 2808 /* Is a USB 3.0 port in the Inactive or Compliance Mode state? 2809 * Port warm reset is required to recover 2810 */ 2811 static bool hub_port_warm_reset_required(struct usb_hub *hub, int port1, 2812 u16 portstatus) 2813 { 2814 u16 link_state; 2815 2816 if (!hub_is_superspeed(hub->hdev)) 2817 return false; 2818 2819 if (test_bit(port1, hub->warm_reset_bits)) 2820 return true; 2821 2822 link_state = portstatus & USB_PORT_STAT_LINK_STATE; 2823 return link_state == USB_SS_PORT_LS_SS_INACTIVE 2824 || link_state == USB_SS_PORT_LS_COMP_MOD; 2825 } 2826 2827 static int hub_port_wait_reset(struct usb_hub *hub, int port1, 2828 struct usb_device *udev, unsigned int delay, bool warm) 2829 { 2830 int delay_time, ret; 2831 u16 portstatus; 2832 u16 portchange; 2833 u32 ext_portstatus = 0; 2834 2835 for (delay_time = 0; 2836 delay_time < HUB_RESET_TIMEOUT; 2837 delay_time += delay) { 2838 /* wait to give the device a chance to reset */ 2839 msleep(delay); 2840 2841 /* read and decode port status */ 2842 if (hub_is_superspeedplus(hub->hdev)) 2843 ret = hub_ext_port_status(hub, port1, 2844 HUB_EXT_PORT_STATUS, 2845 &portstatus, &portchange, 2846 &ext_portstatus); 2847 else 2848 ret = hub_port_status(hub, port1, &portstatus, 2849 &portchange); 2850 if (ret < 0) 2851 return ret; 2852 2853 /* 2854 * The port state is unknown until the reset completes. 2855 * 2856 * On top of that, some chips may require additional time 2857 * to re-establish a connection after the reset is complete, 2858 * so also wait for the connection to be re-established. 2859 */ 2860 if (!(portstatus & USB_PORT_STAT_RESET) && 2861 (portstatus & USB_PORT_STAT_CONNECTION)) 2862 break; 2863 2864 /* switch to the long delay after two short delay failures */ 2865 if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 2866 delay = HUB_LONG_RESET_TIME; 2867 2868 dev_dbg(&hub->ports[port1 - 1]->dev, 2869 "not %sreset yet, waiting %dms\n", 2870 warm ? "warm " : "", delay); 2871 } 2872 2873 if ((portstatus & USB_PORT_STAT_RESET)) 2874 return -EBUSY; 2875 2876 if (hub_port_warm_reset_required(hub, port1, portstatus)) 2877 return -ENOTCONN; 2878 2879 /* Device went away? */ 2880 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 2881 return -ENOTCONN; 2882 2883 /* Retry if connect change is set but status is still connected. 2884 * A USB 3.0 connection may bounce if multiple warm resets were issued, 2885 * but the device may have successfully re-connected. Ignore it. 2886 */ 2887 if (!hub_is_superspeed(hub->hdev) && 2888 (portchange & USB_PORT_STAT_C_CONNECTION)) { 2889 usb_clear_port_feature(hub->hdev, port1, 2890 USB_PORT_FEAT_C_CONNECTION); 2891 return -EAGAIN; 2892 } 2893 2894 if (!(portstatus & USB_PORT_STAT_ENABLE)) 2895 return -EBUSY; 2896 2897 if (!udev) 2898 return 0; 2899 2900 if (hub_is_superspeedplus(hub->hdev)) { 2901 /* extended portstatus Rx and Tx lane count are zero based */ 2902 udev->rx_lanes = USB_EXT_PORT_RX_LANES(ext_portstatus) + 1; 2903 udev->tx_lanes = USB_EXT_PORT_TX_LANES(ext_portstatus) + 1; 2904 udev->ssp_rate = get_port_ssp_rate(hub->hdev, ext_portstatus); 2905 } else { 2906 udev->rx_lanes = 1; 2907 udev->tx_lanes = 1; 2908 udev->ssp_rate = USB_SSP_GEN_UNKNOWN; 2909 } 2910 if (hub_is_wusb(hub)) 2911 udev->speed = USB_SPEED_WIRELESS; 2912 else if (udev->ssp_rate != USB_SSP_GEN_UNKNOWN) 2913 udev->speed = USB_SPEED_SUPER_PLUS; 2914 else if (hub_is_superspeed(hub->hdev)) 2915 udev->speed = USB_SPEED_SUPER; 2916 else if (portstatus & USB_PORT_STAT_HIGH_SPEED) 2917 udev->speed = USB_SPEED_HIGH; 2918 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 2919 udev->speed = USB_SPEED_LOW; 2920 else 2921 udev->speed = USB_SPEED_FULL; 2922 return 0; 2923 } 2924 2925 /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ 2926 static int hub_port_reset(struct usb_hub *hub, int port1, 2927 struct usb_device *udev, unsigned int delay, bool warm) 2928 { 2929 int i, status; 2930 u16 portchange, portstatus; 2931 struct usb_port *port_dev = hub->ports[port1 - 1]; 2932 int reset_recovery_time; 2933 2934 if (!hub_is_superspeed(hub->hdev)) { 2935 if (warm) { 2936 dev_err(hub->intfdev, "only USB3 hub support " 2937 "warm reset\n"); 2938 return -EINVAL; 2939 } 2940 /* Block EHCI CF initialization during the port reset. 2941 * Some companion controllers don't like it when they mix. 2942 */ 2943 down_read(&ehci_cf_port_reset_rwsem); 2944 } else if (!warm) { 2945 /* 2946 * If the caller hasn't explicitly requested a warm reset, 2947 * double check and see if one is needed. 2948 */ 2949 if (hub_port_status(hub, port1, &portstatus, &portchange) == 0) 2950 if (hub_port_warm_reset_required(hub, port1, 2951 portstatus)) 2952 warm = true; 2953 } 2954 clear_bit(port1, hub->warm_reset_bits); 2955 2956 /* Reset the port */ 2957 for (i = 0; i < PORT_RESET_TRIES; i++) { 2958 status = set_port_feature(hub->hdev, port1, (warm ? 2959 USB_PORT_FEAT_BH_PORT_RESET : 2960 USB_PORT_FEAT_RESET)); 2961 if (status == -ENODEV) { 2962 ; /* The hub is gone */ 2963 } else if (status) { 2964 dev_err(&port_dev->dev, 2965 "cannot %sreset (err = %d)\n", 2966 warm ? "warm " : "", status); 2967 } else { 2968 status = hub_port_wait_reset(hub, port1, udev, delay, 2969 warm); 2970 if (status && status != -ENOTCONN && status != -ENODEV) 2971 dev_dbg(hub->intfdev, 2972 "port_wait_reset: err = %d\n", 2973 status); 2974 } 2975 2976 /* Check for disconnect or reset */ 2977 if (status == 0 || status == -ENOTCONN || status == -ENODEV) { 2978 usb_clear_port_feature(hub->hdev, port1, 2979 USB_PORT_FEAT_C_RESET); 2980 2981 if (!hub_is_superspeed(hub->hdev)) 2982 goto done; 2983 2984 usb_clear_port_feature(hub->hdev, port1, 2985 USB_PORT_FEAT_C_BH_PORT_RESET); 2986 usb_clear_port_feature(hub->hdev, port1, 2987 USB_PORT_FEAT_C_PORT_LINK_STATE); 2988 2989 if (udev) 2990 usb_clear_port_feature(hub->hdev, port1, 2991 USB_PORT_FEAT_C_CONNECTION); 2992 2993 /* 2994 * If a USB 3.0 device migrates from reset to an error 2995 * state, re-issue the warm reset. 2996 */ 2997 if (hub_port_status(hub, port1, 2998 &portstatus, &portchange) < 0) 2999 goto done; 3000 3001 if (!hub_port_warm_reset_required(hub, port1, 3002 portstatus)) 3003 goto done; 3004 3005 /* 3006 * If the port is in SS.Inactive or Compliance Mode, the 3007 * hot or warm reset failed. Try another warm reset. 3008 */ 3009 if (!warm) { 3010 dev_dbg(&port_dev->dev, 3011 "hot reset failed, warm reset\n"); 3012 warm = true; 3013 } 3014 } 3015 3016 dev_dbg(&port_dev->dev, 3017 "not enabled, trying %sreset again...\n", 3018 warm ? "warm " : ""); 3019 delay = HUB_LONG_RESET_TIME; 3020 } 3021 3022 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n"); 3023 3024 done: 3025 if (status == 0) { 3026 if (port_dev->quirks & USB_PORT_QUIRK_FAST_ENUM) 3027 usleep_range(10000, 12000); 3028 else { 3029 /* TRSTRCY = 10 ms; plus some extra */ 3030 reset_recovery_time = 10 + 40; 3031 3032 /* Hub needs extra delay after resetting its port. */ 3033 if (hub->hdev->quirks & USB_QUIRK_HUB_SLOW_RESET) 3034 reset_recovery_time += 100; 3035 3036 msleep(reset_recovery_time); 3037 } 3038 3039 if (udev) { 3040 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3041 3042 update_devnum(udev, 0); 3043 /* The xHC may think the device is already reset, 3044 * so ignore the status. 3045 */ 3046 if (hcd->driver->reset_device) 3047 hcd->driver->reset_device(hcd, udev); 3048 3049 usb_set_device_state(udev, USB_STATE_DEFAULT); 3050 } 3051 } else { 3052 if (udev) 3053 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 3054 } 3055 3056 if (!hub_is_superspeed(hub->hdev)) 3057 up_read(&ehci_cf_port_reset_rwsem); 3058 3059 return status; 3060 } 3061 3062 /* Check if a port is power on */ 3063 static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) 3064 { 3065 int ret = 0; 3066 3067 if (hub_is_superspeed(hub->hdev)) { 3068 if (portstatus & USB_SS_PORT_STAT_POWER) 3069 ret = 1; 3070 } else { 3071 if (portstatus & USB_PORT_STAT_POWER) 3072 ret = 1; 3073 } 3074 3075 return ret; 3076 } 3077 3078 static void usb_lock_port(struct usb_port *port_dev) 3079 __acquires(&port_dev->status_lock) 3080 { 3081 mutex_lock(&port_dev->status_lock); 3082 __acquire(&port_dev->status_lock); 3083 } 3084 3085 static void usb_unlock_port(struct usb_port *port_dev) 3086 __releases(&port_dev->status_lock) 3087 { 3088 mutex_unlock(&port_dev->status_lock); 3089 __release(&port_dev->status_lock); 3090 } 3091 3092 #ifdef CONFIG_PM 3093 3094 /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ 3095 static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) 3096 { 3097 int ret = 0; 3098 3099 if (hub_is_superspeed(hub->hdev)) { 3100 if ((portstatus & USB_PORT_STAT_LINK_STATE) 3101 == USB_SS_PORT_LS_U3) 3102 ret = 1; 3103 } else { 3104 if (portstatus & USB_PORT_STAT_SUSPEND) 3105 ret = 1; 3106 } 3107 3108 return ret; 3109 } 3110 3111 /* Determine whether the device on a port is ready for a normal resume, 3112 * is ready for a reset-resume, or should be disconnected. 3113 */ 3114 static int check_port_resume_type(struct usb_device *udev, 3115 struct usb_hub *hub, int port1, 3116 int status, u16 portchange, u16 portstatus) 3117 { 3118 struct usb_port *port_dev = hub->ports[port1 - 1]; 3119 int retries = 3; 3120 3121 retry: 3122 /* Is a warm reset needed to recover the connection? */ 3123 if (status == 0 && udev->reset_resume 3124 && hub_port_warm_reset_required(hub, port1, portstatus)) { 3125 /* pass */; 3126 } 3127 /* Is the device still present? */ 3128 else if (status || port_is_suspended(hub, portstatus) || 3129 !port_is_power_on(hub, portstatus)) { 3130 if (status >= 0) 3131 status = -ENODEV; 3132 } else if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 3133 if (retries--) { 3134 usleep_range(200, 300); 3135 status = hub_port_status(hub, port1, &portstatus, 3136 &portchange); 3137 goto retry; 3138 } 3139 status = -ENODEV; 3140 } 3141 3142 /* Can't do a normal resume if the port isn't enabled, 3143 * so try a reset-resume instead. 3144 */ 3145 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { 3146 if (udev->persist_enabled) 3147 udev->reset_resume = 1; 3148 else 3149 status = -ENODEV; 3150 } 3151 3152 if (status) { 3153 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n", 3154 portchange, portstatus, status); 3155 } else if (udev->reset_resume) { 3156 3157 /* Late port handoff can set status-change bits */ 3158 if (portchange & USB_PORT_STAT_C_CONNECTION) 3159 usb_clear_port_feature(hub->hdev, port1, 3160 USB_PORT_FEAT_C_CONNECTION); 3161 if (portchange & USB_PORT_STAT_C_ENABLE) 3162 usb_clear_port_feature(hub->hdev, port1, 3163 USB_PORT_FEAT_C_ENABLE); 3164 3165 /* 3166 * Whatever made this reset-resume necessary may have 3167 * turned on the port1 bit in hub->change_bits. But after 3168 * a successful reset-resume we want the bit to be clear; 3169 * if it was on it would indicate that something happened 3170 * following the reset-resume. 3171 */ 3172 clear_bit(port1, hub->change_bits); 3173 } 3174 3175 return status; 3176 } 3177 3178 int usb_disable_ltm(struct usb_device *udev) 3179 { 3180 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3181 3182 /* Check if the roothub and device supports LTM. */ 3183 if (!usb_device_supports_ltm(hcd->self.root_hub) || 3184 !usb_device_supports_ltm(udev)) 3185 return 0; 3186 3187 /* Clear Feature LTM Enable can only be sent if the device is 3188 * configured. 3189 */ 3190 if (!udev->actconfig) 3191 return 0; 3192 3193 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3194 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 3195 USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 3196 USB_CTRL_SET_TIMEOUT); 3197 } 3198 EXPORT_SYMBOL_GPL(usb_disable_ltm); 3199 3200 void usb_enable_ltm(struct usb_device *udev) 3201 { 3202 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 3203 3204 /* Check if the roothub and device supports LTM. */ 3205 if (!usb_device_supports_ltm(hcd->self.root_hub) || 3206 !usb_device_supports_ltm(udev)) 3207 return; 3208 3209 /* Set Feature LTM Enable can only be sent if the device is 3210 * configured. 3211 */ 3212 if (!udev->actconfig) 3213 return; 3214 3215 usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3216 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 3217 USB_DEVICE_LTM_ENABLE, 0, NULL, 0, 3218 USB_CTRL_SET_TIMEOUT); 3219 } 3220 EXPORT_SYMBOL_GPL(usb_enable_ltm); 3221 3222 /* 3223 * usb_enable_remote_wakeup - enable remote wakeup for a device 3224 * @udev: target device 3225 * 3226 * For USB-2 devices: Set the device's remote wakeup feature. 3227 * 3228 * For USB-3 devices: Assume there's only one function on the device and 3229 * enable remote wake for the first interface. FIXME if the interface 3230 * association descriptor shows there's more than one function. 3231 */ 3232 static int usb_enable_remote_wakeup(struct usb_device *udev) 3233 { 3234 if (udev->speed < USB_SPEED_SUPER) 3235 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3236 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 3237 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 3238 USB_CTRL_SET_TIMEOUT); 3239 else 3240 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3241 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 3242 USB_INTRF_FUNC_SUSPEND, 3243 USB_INTRF_FUNC_SUSPEND_RW | 3244 USB_INTRF_FUNC_SUSPEND_LP, 3245 NULL, 0, USB_CTRL_SET_TIMEOUT); 3246 } 3247 3248 /* 3249 * usb_disable_remote_wakeup - disable remote wakeup for a device 3250 * @udev: target device 3251 * 3252 * For USB-2 devices: Clear the device's remote wakeup feature. 3253 * 3254 * For USB-3 devices: Assume there's only one function on the device and 3255 * disable remote wake for the first interface. FIXME if the interface 3256 * association descriptor shows there's more than one function. 3257 */ 3258 static int usb_disable_remote_wakeup(struct usb_device *udev) 3259 { 3260 if (udev->speed < USB_SPEED_SUPER) 3261 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3262 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 3263 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0, 3264 USB_CTRL_SET_TIMEOUT); 3265 else 3266 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3267 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE, 3268 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, 3269 USB_CTRL_SET_TIMEOUT); 3270 } 3271 3272 /* Count of wakeup-enabled devices at or below udev */ 3273 unsigned usb_wakeup_enabled_descendants(struct usb_device *udev) 3274 { 3275 struct usb_hub *hub = usb_hub_to_struct_hub(udev); 3276 3277 return udev->do_remote_wakeup + 3278 (hub ? hub->wakeup_enabled_descendants : 0); 3279 } 3280 EXPORT_SYMBOL_GPL(usb_wakeup_enabled_descendants); 3281 3282 /* 3283 * usb_port_suspend - suspend a usb device's upstream port 3284 * @udev: device that's no longer in active use, not a root hub 3285 * Context: must be able to sleep; device not locked; pm locks held 3286 * 3287 * Suspends a USB device that isn't in active use, conserving power. 3288 * Devices may wake out of a suspend, if anything important happens, 3289 * using the remote wakeup mechanism. They may also be taken out of 3290 * suspend by the host, using usb_port_resume(). It's also routine 3291 * to disconnect devices while they are suspended. 3292 * 3293 * This only affects the USB hardware for a device; its interfaces 3294 * (and, for hubs, child devices) must already have been suspended. 3295 * 3296 * Selective port suspend reduces power; most suspended devices draw 3297 * less than 500 uA. It's also used in OTG, along with remote wakeup. 3298 * All devices below the suspended port are also suspended. 3299 * 3300 * Devices leave suspend state when the host wakes them up. Some devices 3301 * also support "remote wakeup", where the device can activate the USB 3302 * tree above them to deliver data, such as a keypress or packet. In 3303 * some cases, this wakes the USB host. 3304 * 3305 * Suspending OTG devices may trigger HNP, if that's been enabled 3306 * between a pair of dual-role devices. That will change roles, such 3307 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 3308 * 3309 * Devices on USB hub ports have only one "suspend" state, corresponding 3310 * to ACPI D2, "may cause the device to lose some context". 3311 * State transitions include: 3312 * 3313 * - suspend, resume ... when the VBUS power link stays live 3314 * - suspend, disconnect ... VBUS lost 3315 * 3316 * Once VBUS drop breaks the circuit, the port it's using has to go through 3317 * normal re-enumeration procedures, starting with enabling VBUS power. 3318 * Other than re-initializing the hub (plug/unplug, except for root hubs), 3319 * Linux (2.6) currently has NO mechanisms to initiate that: no hub_wq 3320 * timer, no SRP, no requests through sysfs. 3321 * 3322 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get 3323 * suspended until their bus goes into global suspend (i.e., the root 3324 * hub is suspended). Nevertheless, we change @udev->state to 3325 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual 3326 * upstream port setting is stored in @udev->port_is_suspended. 3327 * 3328 * Returns 0 on success, else negative errno. 3329 */ 3330 int usb_port_suspend(struct usb_device *udev, pm_message_t msg) 3331 { 3332 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3333 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3334 int port1 = udev->portnum; 3335 int status; 3336 bool really_suspend = true; 3337 3338 usb_lock_port(port_dev); 3339 3340 /* enable remote wakeup when appropriate; this lets the device 3341 * wake up the upstream hub (including maybe the root hub). 3342 * 3343 * NOTE: OTG devices may issue remote wakeup (or SRP) even when 3344 * we don't explicitly enable it here. 3345 */ 3346 if (udev->do_remote_wakeup) { 3347 status = usb_enable_remote_wakeup(udev); 3348 if (status) { 3349 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", 3350 status); 3351 /* bail if autosuspend is requested */ 3352 if (PMSG_IS_AUTO(msg)) 3353 goto err_wakeup; 3354 } 3355 } 3356 3357 /* disable USB2 hardware LPM */ 3358 usb_disable_usb2_hardware_lpm(udev); 3359 3360 if (usb_disable_ltm(udev)) { 3361 dev_err(&udev->dev, "Failed to disable LTM before suspend\n"); 3362 status = -ENOMEM; 3363 if (PMSG_IS_AUTO(msg)) 3364 goto err_ltm; 3365 } 3366 3367 /* see 7.1.7.6 */ 3368 if (hub_is_superspeed(hub->hdev)) 3369 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); 3370 3371 /* 3372 * For system suspend, we do not need to enable the suspend feature 3373 * on individual USB-2 ports. The devices will automatically go 3374 * into suspend a few ms after the root hub stops sending packets. 3375 * The USB 2.0 spec calls this "global suspend". 3376 * 3377 * However, many USB hubs have a bug: They don't relay wakeup requests 3378 * from a downstream port if the port's suspend feature isn't on. 3379 * Therefore we will turn on the suspend feature if udev or any of its 3380 * descendants is enabled for remote wakeup. 3381 */ 3382 else if (PMSG_IS_AUTO(msg) || usb_wakeup_enabled_descendants(udev) > 0) 3383 status = set_port_feature(hub->hdev, port1, 3384 USB_PORT_FEAT_SUSPEND); 3385 else { 3386 really_suspend = false; 3387 status = 0; 3388 } 3389 if (status) { 3390 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status); 3391 3392 /* Try to enable USB3 LTM again */ 3393 usb_enable_ltm(udev); 3394 err_ltm: 3395 /* Try to enable USB2 hardware LPM again */ 3396 usb_enable_usb2_hardware_lpm(udev); 3397 3398 if (udev->do_remote_wakeup) 3399 (void) usb_disable_remote_wakeup(udev); 3400 err_wakeup: 3401 3402 /* System sleep transitions should never fail */ 3403 if (!PMSG_IS_AUTO(msg)) 3404 status = 0; 3405 } else { 3406 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", 3407 (PMSG_IS_AUTO(msg) ? "auto-" : ""), 3408 udev->do_remote_wakeup); 3409 if (really_suspend) { 3410 udev->port_is_suspended = 1; 3411 3412 /* device has up to 10 msec to fully suspend */ 3413 msleep(10); 3414 } 3415 usb_set_device_state(udev, USB_STATE_SUSPENDED); 3416 } 3417 3418 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled 3419 && test_and_clear_bit(port1, hub->child_usage_bits)) 3420 pm_runtime_put_sync(&port_dev->dev); 3421 3422 usb_mark_last_busy(hub->hdev); 3423 3424 usb_unlock_port(port_dev); 3425 return status; 3426 } 3427 3428 /* 3429 * If the USB "suspend" state is in use (rather than "global suspend"), 3430 * many devices will be individually taken out of suspend state using 3431 * special "resume" signaling. This routine kicks in shortly after 3432 * hardware resume signaling is finished, either because of selective 3433 * resume (by host) or remote wakeup (by device) ... now see what changed 3434 * in the tree that's rooted at this device. 3435 * 3436 * If @udev->reset_resume is set then the device is reset before the 3437 * status check is done. 3438 */ 3439 static int finish_port_resume(struct usb_device *udev) 3440 { 3441 int status = 0; 3442 u16 devstatus = 0; 3443 3444 /* caller owns the udev device lock */ 3445 dev_dbg(&udev->dev, "%s\n", 3446 udev->reset_resume ? "finish reset-resume" : "finish resume"); 3447 3448 /* usb ch9 identifies four variants of SUSPENDED, based on what 3449 * state the device resumes to. Linux currently won't see the 3450 * first two on the host side; they'd be inside hub_port_init() 3451 * during many timeouts, but hub_wq can't suspend until later. 3452 */ 3453 usb_set_device_state(udev, udev->actconfig 3454 ? USB_STATE_CONFIGURED 3455 : USB_STATE_ADDRESS); 3456 3457 /* 10.5.4.5 says not to reset a suspended port if the attached 3458 * device is enabled for remote wakeup. Hence the reset 3459 * operation is carried out here, after the port has been 3460 * resumed. 3461 */ 3462 if (udev->reset_resume) { 3463 /* 3464 * If the device morphs or switches modes when it is reset, 3465 * we don't want to perform a reset-resume. We'll fail the 3466 * resume, which will cause a logical disconnect, and then 3467 * the device will be rediscovered. 3468 */ 3469 retry_reset_resume: 3470 if (udev->quirks & USB_QUIRK_RESET) 3471 status = -ENODEV; 3472 else 3473 status = usb_reset_and_verify_device(udev); 3474 } 3475 3476 /* 10.5.4.5 says be sure devices in the tree are still there. 3477 * For now let's assume the device didn't go crazy on resume, 3478 * and device drivers will know about any resume quirks. 3479 */ 3480 if (status == 0) { 3481 devstatus = 0; 3482 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 3483 3484 /* If a normal resume failed, try doing a reset-resume */ 3485 if (status && !udev->reset_resume && udev->persist_enabled) { 3486 dev_dbg(&udev->dev, "retry with reset-resume\n"); 3487 udev->reset_resume = 1; 3488 goto retry_reset_resume; 3489 } 3490 } 3491 3492 if (status) { 3493 dev_dbg(&udev->dev, "gone after usb resume? status %d\n", 3494 status); 3495 /* 3496 * There are a few quirky devices which violate the standard 3497 * by claiming to have remote wakeup enabled after a reset, 3498 * which crash if the feature is cleared, hence check for 3499 * udev->reset_resume 3500 */ 3501 } else if (udev->actconfig && !udev->reset_resume) { 3502 if (udev->speed < USB_SPEED_SUPER) { 3503 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) 3504 status = usb_disable_remote_wakeup(udev); 3505 } else { 3506 status = usb_get_std_status(udev, USB_RECIP_INTERFACE, 0, 3507 &devstatus); 3508 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP 3509 | USB_INTRF_STAT_FUNC_RW)) 3510 status = usb_disable_remote_wakeup(udev); 3511 } 3512 3513 if (status) 3514 dev_dbg(&udev->dev, 3515 "disable remote wakeup, status %d\n", 3516 status); 3517 status = 0; 3518 } 3519 return status; 3520 } 3521 3522 /* 3523 * There are some SS USB devices which take longer time for link training. 3524 * XHCI specs 4.19.4 says that when Link training is successful, port 3525 * sets CCS bit to 1. So if SW reads port status before successful link 3526 * training, then it will not find device to be present. 3527 * USB Analyzer log with such buggy devices show that in some cases 3528 * device switch on the RX termination after long delay of host enabling 3529 * the VBUS. In few other cases it has been seen that device fails to 3530 * negotiate link training in first attempt. It has been 3531 * reported till now that few devices take as long as 2000 ms to train 3532 * the link after host enabling its VBUS and termination. Following 3533 * routine implements a 2000 ms timeout for link training. If in a case 3534 * link trains before timeout, loop will exit earlier. 3535 * 3536 * There are also some 2.0 hard drive based devices and 3.0 thumb 3537 * drives that, when plugged into a 2.0 only port, take a long 3538 * time to set CCS after VBUS enable. 3539 * 3540 * FIXME: If a device was connected before suspend, but was removed 3541 * while system was asleep, then the loop in the following routine will 3542 * only exit at timeout. 3543 * 3544 * This routine should only be called when persist is enabled. 3545 */ 3546 static int wait_for_connected(struct usb_device *udev, 3547 struct usb_hub *hub, int *port1, 3548 u16 *portchange, u16 *portstatus) 3549 { 3550 int status = 0, delay_ms = 0; 3551 3552 while (delay_ms < 2000) { 3553 if (status || *portstatus & USB_PORT_STAT_CONNECTION) 3554 break; 3555 if (!port_is_power_on(hub, *portstatus)) { 3556 status = -ENODEV; 3557 break; 3558 } 3559 msleep(20); 3560 delay_ms += 20; 3561 status = hub_port_status(hub, *port1, portstatus, portchange); 3562 } 3563 dev_dbg(&udev->dev, "Waited %dms for CONNECT\n", delay_ms); 3564 return status; 3565 } 3566 3567 /* 3568 * usb_port_resume - re-activate a suspended usb device's upstream port 3569 * @udev: device to re-activate, not a root hub 3570 * Context: must be able to sleep; device not locked; pm locks held 3571 * 3572 * This will re-activate the suspended device, increasing power usage 3573 * while letting drivers communicate again with its endpoints. 3574 * USB resume explicitly guarantees that the power session between 3575 * the host and the device is the same as it was when the device 3576 * suspended. 3577 * 3578 * If @udev->reset_resume is set then this routine won't check that the 3579 * port is still enabled. Furthermore, finish_port_resume() above will 3580 * reset @udev. The end result is that a broken power session can be 3581 * recovered and @udev will appear to persist across a loss of VBUS power. 3582 * 3583 * For example, if a host controller doesn't maintain VBUS suspend current 3584 * during a system sleep or is reset when the system wakes up, all the USB 3585 * power sessions below it will be broken. This is especially troublesome 3586 * for mass-storage devices containing mounted filesystems, since the 3587 * device will appear to have disconnected and all the memory mappings 3588 * to it will be lost. Using the USB_PERSIST facility, the device can be 3589 * made to appear as if it had not disconnected. 3590 * 3591 * This facility can be dangerous. Although usb_reset_and_verify_device() makes 3592 * every effort to insure that the same device is present after the 3593 * reset as before, it cannot provide a 100% guarantee. Furthermore it's 3594 * quite possible for a device to remain unaltered but its media to be 3595 * changed. If the user replaces a flash memory card while the system is 3596 * asleep, he will have only himself to blame when the filesystem on the 3597 * new card is corrupted and the system crashes. 3598 * 3599 * Returns 0 on success, else negative errno. 3600 */ 3601 int usb_port_resume(struct usb_device *udev, pm_message_t msg) 3602 { 3603 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 3604 struct usb_port *port_dev = hub->ports[udev->portnum - 1]; 3605 int port1 = udev->portnum; 3606 int status; 3607 u16 portchange, portstatus; 3608 3609 if (!test_and_set_bit(port1, hub->child_usage_bits)) { 3610 status = pm_runtime_resume_and_get(&port_dev->dev); 3611 if (status < 0) { 3612 dev_dbg(&udev->dev, "can't resume usb port, status %d\n", 3613 status); 3614 return status; 3615 } 3616 } 3617 3618 usb_lock_port(port_dev); 3619 3620 /* Skip the initial Clear-Suspend step for a remote wakeup */ 3621 status = hub_port_status(hub, port1, &portstatus, &portchange); 3622 if (status == 0 && !port_is_suspended(hub, portstatus)) { 3623 if (portchange & USB_PORT_STAT_C_SUSPEND) 3624 pm_wakeup_event(&udev->dev, 0); 3625 goto SuspendCleared; 3626 } 3627 3628 /* see 7.1.7.7; affects power usage, but not budgeting */ 3629 if (hub_is_superspeed(hub->hdev)) 3630 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); 3631 else 3632 status = usb_clear_port_feature(hub->hdev, 3633 port1, USB_PORT_FEAT_SUSPEND); 3634 if (status) { 3635 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status); 3636 } else { 3637 /* drive resume for USB_RESUME_TIMEOUT msec */ 3638 dev_dbg(&udev->dev, "usb %sresume\n", 3639 (PMSG_IS_AUTO(msg) ? "auto-" : "")); 3640 msleep(USB_RESUME_TIMEOUT); 3641 3642 /* Virtual root hubs can trigger on GET_PORT_STATUS to 3643 * stop resume signaling. Then finish the resume 3644 * sequence. 3645 */ 3646 status = hub_port_status(hub, port1, &portstatus, &portchange); 3647 } 3648 3649 SuspendCleared: 3650 if (status == 0) { 3651 udev->port_is_suspended = 0; 3652 if (hub_is_superspeed(hub->hdev)) { 3653 if (portchange & USB_PORT_STAT_C_LINK_STATE) 3654 usb_clear_port_feature(hub->hdev, port1, 3655 USB_PORT_FEAT_C_PORT_LINK_STATE); 3656 } else { 3657 if (portchange & USB_PORT_STAT_C_SUSPEND) 3658 usb_clear_port_feature(hub->hdev, port1, 3659 USB_PORT_FEAT_C_SUSPEND); 3660 } 3661 3662 /* TRSMRCY = 10 msec */ 3663 msleep(10); 3664 } 3665 3666 if (udev->persist_enabled) 3667 status = wait_for_connected(udev, hub, &port1, &portchange, 3668 &portstatus); 3669 3670 status = check_port_resume_type(udev, 3671 hub, port1, status, portchange, portstatus); 3672 if (status == 0) 3673 status = finish_port_resume(udev); 3674 if (status < 0) { 3675 dev_dbg(&udev->dev, "can't resume, status %d\n", status); 3676 hub_port_logical_disconnect(hub, port1); 3677 } else { 3678 /* Try to enable USB2 hardware LPM */ 3679 usb_enable_usb2_hardware_lpm(udev); 3680 3681 /* Try to enable USB3 LTM */ 3682 usb_enable_ltm(udev); 3683 } 3684 3685 usb_unlock_port(port_dev); 3686 3687 return status; 3688 } 3689 3690 int usb_remote_wakeup(struct usb_device *udev) 3691 { 3692 int status = 0; 3693 3694 usb_lock_device(udev); 3695 if (udev->state == USB_STATE_SUSPENDED) { 3696 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 3697 status = usb_autoresume_device(udev); 3698 if (status == 0) { 3699 /* Let the drivers do their thing, then... */ 3700 usb_autosuspend_device(udev); 3701 } 3702 } 3703 usb_unlock_device(udev); 3704 return status; 3705 } 3706 3707 /* Returns 1 if there was a remote wakeup and a connect status change. */ 3708 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 3709 u16 portstatus, u16 portchange) 3710 __must_hold(&port_dev->status_lock) 3711 { 3712 struct usb_port *port_dev = hub->ports[port - 1]; 3713 struct usb_device *hdev; 3714 struct usb_device *udev; 3715 int connect_change = 0; 3716 u16 link_state; 3717 int ret; 3718 3719 hdev = hub->hdev; 3720 udev = port_dev->child; 3721 if (!hub_is_superspeed(hdev)) { 3722 if (!(portchange & USB_PORT_STAT_C_SUSPEND)) 3723 return 0; 3724 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); 3725 } else { 3726 link_state = portstatus & USB_PORT_STAT_LINK_STATE; 3727 if (!udev || udev->state != USB_STATE_SUSPENDED || 3728 (link_state != USB_SS_PORT_LS_U0 && 3729 link_state != USB_SS_PORT_LS_U1 && 3730 link_state != USB_SS_PORT_LS_U2)) 3731 return 0; 3732 } 3733 3734 if (udev) { 3735 /* TRSMRCY = 10 msec */ 3736 msleep(10); 3737 3738 usb_unlock_port(port_dev); 3739 ret = usb_remote_wakeup(udev); 3740 usb_lock_port(port_dev); 3741 if (ret < 0) 3742 connect_change = 1; 3743 } else { 3744 ret = -ENODEV; 3745 hub_port_disable(hub, port, 1); 3746 } 3747 dev_dbg(&port_dev->dev, "resume, status %d\n", ret); 3748 return connect_change; 3749 } 3750 3751 static int check_ports_changed(struct usb_hub *hub) 3752 { 3753 int port1; 3754 3755 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { 3756 u16 portstatus, portchange; 3757 int status; 3758 3759 status = hub_port_status(hub, port1, &portstatus, &portchange); 3760 if (!status && portchange) 3761 return 1; 3762 } 3763 return 0; 3764 } 3765 3766 static int hub_suspend(struct usb_interface *intf, pm_message_t msg) 3767 { 3768 struct usb_hub *hub = usb_get_intfdata(intf); 3769 struct usb_device *hdev = hub->hdev; 3770 unsigned port1; 3771 3772 /* 3773 * Warn if children aren't already suspended. 3774 * Also, add up the number of wakeup-enabled descendants. 3775 */ 3776 hub->wakeup_enabled_descendants = 0; 3777 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3778 struct usb_port *port_dev = hub->ports[port1 - 1]; 3779 struct usb_device *udev = port_dev->child; 3780 3781 if (udev && udev->can_submit) { 3782 dev_warn(&port_dev->dev, "device %s not suspended yet\n", 3783 dev_name(&udev->dev)); 3784 if (PMSG_IS_AUTO(msg)) 3785 return -EBUSY; 3786 } 3787 if (udev) 3788 hub->wakeup_enabled_descendants += 3789 usb_wakeup_enabled_descendants(udev); 3790 } 3791 3792 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { 3793 /* check if there are changes pending on hub ports */ 3794 if (check_ports_changed(hub)) { 3795 if (PMSG_IS_AUTO(msg)) 3796 return -EBUSY; 3797 pm_wakeup_event(&hdev->dev, 2000); 3798 } 3799 } 3800 3801 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { 3802 /* Enable hub to send remote wakeup for all ports. */ 3803 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 3804 set_port_feature(hdev, 3805 port1 | 3806 USB_PORT_FEAT_REMOTE_WAKE_CONNECT | 3807 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | 3808 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, 3809 USB_PORT_FEAT_REMOTE_WAKE_MASK); 3810 } 3811 } 3812 3813 dev_dbg(&intf->dev, "%s\n", __func__); 3814 3815 /* stop hub_wq and related activity */ 3816 hub_quiesce(hub, HUB_SUSPEND); 3817 return 0; 3818 } 3819 3820 /* Report wakeup requests from the ports of a resuming root hub */ 3821 static void report_wakeup_requests(struct usb_hub *hub) 3822 { 3823 struct usb_device *hdev = hub->hdev; 3824 struct usb_device *udev; 3825 struct usb_hcd *hcd; 3826 unsigned long resuming_ports; 3827 int i; 3828 3829 if (hdev->parent) 3830 return; /* Not a root hub */ 3831 3832 hcd = bus_to_hcd(hdev->bus); 3833 if (hcd->driver->get_resuming_ports) { 3834 3835 /* 3836 * The get_resuming_ports() method returns a bitmap (origin 0) 3837 * of ports which have started wakeup signaling but have not 3838 * yet finished resuming. During system resume we will 3839 * resume all the enabled ports, regardless of any wakeup 3840 * signals, which means the wakeup requests would be lost. 3841 * To prevent this, report them to the PM core here. 3842 */ 3843 resuming_ports = hcd->driver->get_resuming_ports(hcd); 3844 for (i = 0; i < hdev->maxchild; ++i) { 3845 if (test_bit(i, &resuming_ports)) { 3846 udev = hub->ports[i]->child; 3847 if (udev) 3848 pm_wakeup_event(&udev->dev, 0); 3849 } 3850 } 3851 } 3852 } 3853 3854 static int hub_resume(struct usb_interface *intf) 3855 { 3856 struct usb_hub *hub = usb_get_intfdata(intf); 3857 3858 dev_dbg(&intf->dev, "%s\n", __func__); 3859 hub_activate(hub, HUB_RESUME); 3860 3861 /* 3862 * This should be called only for system resume, not runtime resume. 3863 * We can't tell the difference here, so some wakeup requests will be 3864 * reported at the wrong time or more than once. This shouldn't 3865 * matter much, so long as they do get reported. 3866 */ 3867 report_wakeup_requests(hub); 3868 return 0; 3869 } 3870 3871 static int hub_reset_resume(struct usb_interface *intf) 3872 { 3873 struct usb_hub *hub = usb_get_intfdata(intf); 3874 3875 dev_dbg(&intf->dev, "%s\n", __func__); 3876 hub_activate(hub, HUB_RESET_RESUME); 3877 return 0; 3878 } 3879 3880 /** 3881 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power 3882 * @rhdev: struct usb_device for the root hub 3883 * 3884 * The USB host controller driver calls this function when its root hub 3885 * is resumed and Vbus power has been interrupted or the controller 3886 * has been reset. The routine marks @rhdev as having lost power. 3887 * When the hub driver is resumed it will take notice and carry out 3888 * power-session recovery for all the "USB-PERSIST"-enabled child devices; 3889 * the others will be disconnected. 3890 */ 3891 void usb_root_hub_lost_power(struct usb_device *rhdev) 3892 { 3893 dev_notice(&rhdev->dev, "root hub lost power or was reset\n"); 3894 rhdev->reset_resume = 1; 3895 } 3896 EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); 3897 3898 static const char * const usb3_lpm_names[] = { 3899 "U0", 3900 "U1", 3901 "U2", 3902 "U3", 3903 }; 3904 3905 /* 3906 * Send a Set SEL control transfer to the device, prior to enabling 3907 * device-initiated U1 or U2. This lets the device know the exit latencies from 3908 * the time the device initiates a U1 or U2 exit, to the time it will receive a 3909 * packet from the host. 3910 * 3911 * This function will fail if the SEL or PEL values for udev are greater than 3912 * the maximum allowed values for the link state to be enabled. 3913 */ 3914 static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) 3915 { 3916 struct usb_set_sel_req *sel_values; 3917 unsigned long long u1_sel; 3918 unsigned long long u1_pel; 3919 unsigned long long u2_sel; 3920 unsigned long long u2_pel; 3921 int ret; 3922 3923 if (udev->state != USB_STATE_CONFIGURED) 3924 return 0; 3925 3926 /* Convert SEL and PEL stored in ns to us */ 3927 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); 3928 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); 3929 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); 3930 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); 3931 3932 /* 3933 * Make sure that the calculated SEL and PEL values for the link 3934 * state we're enabling aren't bigger than the max SEL/PEL 3935 * value that will fit in the SET SEL control transfer. 3936 * Otherwise the device would get an incorrect idea of the exit 3937 * latency for the link state, and could start a device-initiated 3938 * U1/U2 when the exit latencies are too high. 3939 */ 3940 if ((state == USB3_LPM_U1 && 3941 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || 3942 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || 3943 (state == USB3_LPM_U2 && 3944 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || 3945 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { 3946 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n", 3947 usb3_lpm_names[state], u1_sel, u1_pel); 3948 return -EINVAL; 3949 } 3950 3951 /* 3952 * If we're enabling device-initiated LPM for one link state, 3953 * but the other link state has a too high SEL or PEL value, 3954 * just set those values to the max in the Set SEL request. 3955 */ 3956 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) 3957 u1_sel = USB3_LPM_MAX_U1_SEL_PEL; 3958 3959 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) 3960 u1_pel = USB3_LPM_MAX_U1_SEL_PEL; 3961 3962 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) 3963 u2_sel = USB3_LPM_MAX_U2_SEL_PEL; 3964 3965 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) 3966 u2_pel = USB3_LPM_MAX_U2_SEL_PEL; 3967 3968 /* 3969 * usb_enable_lpm() can be called as part of a failed device reset, 3970 * which may be initiated by an error path of a mass storage driver. 3971 * Therefore, use GFP_NOIO. 3972 */ 3973 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); 3974 if (!sel_values) 3975 return -ENOMEM; 3976 3977 sel_values->u1_sel = u1_sel; 3978 sel_values->u1_pel = u1_pel; 3979 sel_values->u2_sel = cpu_to_le16(u2_sel); 3980 sel_values->u2_pel = cpu_to_le16(u2_pel); 3981 3982 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3983 USB_REQ_SET_SEL, 3984 USB_RECIP_DEVICE, 3985 0, 0, 3986 sel_values, sizeof *(sel_values), 3987 USB_CTRL_SET_TIMEOUT); 3988 kfree(sel_values); 3989 return ret; 3990 } 3991 3992 /* 3993 * Enable or disable device-initiated U1 or U2 transitions. 3994 */ 3995 static int usb_set_device_initiated_lpm(struct usb_device *udev, 3996 enum usb3_link_state state, bool enable) 3997 { 3998 int ret; 3999 int feature; 4000 4001 switch (state) { 4002 case USB3_LPM_U1: 4003 feature = USB_DEVICE_U1_ENABLE; 4004 break; 4005 case USB3_LPM_U2: 4006 feature = USB_DEVICE_U2_ENABLE; 4007 break; 4008 default: 4009 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", 4010 __func__, enable ? "enable" : "disable"); 4011 return -EINVAL; 4012 } 4013 4014 if (udev->state != USB_STATE_CONFIGURED) { 4015 dev_dbg(&udev->dev, "%s: Can't %s %s state " 4016 "for unconfigured device.\n", 4017 __func__, enable ? "enable" : "disable", 4018 usb3_lpm_names[state]); 4019 return 0; 4020 } 4021 4022 if (enable) { 4023 /* 4024 * Now send the control transfer to enable device-initiated LPM 4025 * for either U1 or U2. 4026 */ 4027 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4028 USB_REQ_SET_FEATURE, 4029 USB_RECIP_DEVICE, 4030 feature, 4031 0, NULL, 0, 4032 USB_CTRL_SET_TIMEOUT); 4033 } else { 4034 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 4035 USB_REQ_CLEAR_FEATURE, 4036 USB_RECIP_DEVICE, 4037 feature, 4038 0, NULL, 0, 4039 USB_CTRL_SET_TIMEOUT); 4040 } 4041 if (ret < 0) { 4042 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", 4043 enable ? "Enable" : "Disable", 4044 usb3_lpm_names[state]); 4045 return -EBUSY; 4046 } 4047 return 0; 4048 } 4049 4050 static int usb_set_lpm_timeout(struct usb_device *udev, 4051 enum usb3_link_state state, int timeout) 4052 { 4053 int ret; 4054 int feature; 4055 4056 switch (state) { 4057 case USB3_LPM_U1: 4058 feature = USB_PORT_FEAT_U1_TIMEOUT; 4059 break; 4060 case USB3_LPM_U2: 4061 feature = USB_PORT_FEAT_U2_TIMEOUT; 4062 break; 4063 default: 4064 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", 4065 __func__); 4066 return -EINVAL; 4067 } 4068 4069 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && 4070 timeout != USB3_LPM_DEVICE_INITIATED) { 4071 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " 4072 "which is a reserved value.\n", 4073 usb3_lpm_names[state], timeout); 4074 return -EINVAL; 4075 } 4076 4077 ret = set_port_feature(udev->parent, 4078 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, 4079 feature); 4080 if (ret < 0) { 4081 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," 4082 "error code %i\n", usb3_lpm_names[state], 4083 timeout, ret); 4084 return -EBUSY; 4085 } 4086 if (state == USB3_LPM_U1) 4087 udev->u1_params.timeout = timeout; 4088 else 4089 udev->u2_params.timeout = timeout; 4090 return 0; 4091 } 4092 4093 /* 4094 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated 4095 * U1/U2 entry. 4096 * 4097 * We will attempt to enable U1 or U2, but there are no guarantees that the 4098 * control transfers to set the hub timeout or enable device-initiated U1/U2 4099 * will be successful. 4100 * 4101 * If the control transfer to enable device-initiated U1/U2 entry fails, then 4102 * hub-initiated U1/U2 will be disabled. 4103 * 4104 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI 4105 * driver know about it. If that call fails, it should be harmless, and just 4106 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. 4107 */ 4108 static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 4109 enum usb3_link_state state) 4110 { 4111 int timeout, ret; 4112 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat; 4113 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat; 4114 4115 /* If the device says it doesn't have *any* exit latency to come out of 4116 * U1 or U2, it's probably lying. Assume it doesn't implement that link 4117 * state. 4118 */ 4119 if ((state == USB3_LPM_U1 && u1_mel == 0) || 4120 (state == USB3_LPM_U2 && u2_mel == 0)) 4121 return; 4122 4123 /* 4124 * First, let the device know about the exit latencies 4125 * associated with the link state we're about to enable. 4126 */ 4127 ret = usb_req_set_sel(udev, state); 4128 if (ret < 0) { 4129 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n", 4130 usb3_lpm_names[state]); 4131 return; 4132 } 4133 4134 /* We allow the host controller to set the U1/U2 timeout internally 4135 * first, so that it can change its schedule to account for the 4136 * additional latency to send data to a device in a lower power 4137 * link state. 4138 */ 4139 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); 4140 4141 /* xHCI host controller doesn't want to enable this LPM state. */ 4142 if (timeout == 0) 4143 return; 4144 4145 if (timeout < 0) { 4146 dev_warn(&udev->dev, "Could not enable %s link state, " 4147 "xHCI error %i.\n", usb3_lpm_names[state], 4148 timeout); 4149 return; 4150 } 4151 4152 if (usb_set_lpm_timeout(udev, state, timeout)) { 4153 /* If we can't set the parent hub U1/U2 timeout, 4154 * device-initiated LPM won't be allowed either, so let the xHCI 4155 * host know that this link state won't be enabled. 4156 */ 4157 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 4158 return; 4159 } 4160 4161 /* Only a configured device will accept the Set Feature 4162 * U1/U2_ENABLE 4163 */ 4164 if (udev->actconfig && 4165 usb_set_device_initiated_lpm(udev, state, true) == 0) { 4166 if (state == USB3_LPM_U1) 4167 udev->usb3_lpm_u1_enabled = 1; 4168 else if (state == USB3_LPM_U2) 4169 udev->usb3_lpm_u2_enabled = 1; 4170 } else { 4171 /* Don't request U1/U2 entry if the device 4172 * cannot transition to U1/U2. 4173 */ 4174 usb_set_lpm_timeout(udev, state, 0); 4175 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); 4176 } 4177 } 4178 4179 /* 4180 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated 4181 * U1/U2 entry. 4182 * 4183 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. 4184 * If zero is returned, the parent will not allow the link to go into U1/U2. 4185 * 4186 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but 4187 * it won't have an effect on the bus link state because the parent hub will 4188 * still disallow device-initiated U1/U2 entry. 4189 * 4190 * If zero is returned, the xHCI host controller may still think U1/U2 entry is 4191 * possible. The result will be slightly more bus bandwidth will be taken up 4192 * (to account for U1/U2 exit latency), but it should be harmless. 4193 */ 4194 static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, 4195 enum usb3_link_state state) 4196 { 4197 switch (state) { 4198 case USB3_LPM_U1: 4199 case USB3_LPM_U2: 4200 break; 4201 default: 4202 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", 4203 __func__); 4204 return -EINVAL; 4205 } 4206 4207 if (usb_set_lpm_timeout(udev, state, 0)) 4208 return -EBUSY; 4209 4210 usb_set_device_initiated_lpm(udev, state, false); 4211 4212 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) 4213 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " 4214 "bus schedule bandwidth may be impacted.\n", 4215 usb3_lpm_names[state]); 4216 4217 /* As soon as usb_set_lpm_timeout(0) return 0, hub initiated LPM 4218 * is disabled. Hub will disallows link to enter U1/U2 as well, 4219 * even device is initiating LPM. Hence LPM is disabled if hub LPM 4220 * timeout set to 0, no matter device-initiated LPM is disabled or 4221 * not. 4222 */ 4223 if (state == USB3_LPM_U1) 4224 udev->usb3_lpm_u1_enabled = 0; 4225 else if (state == USB3_LPM_U2) 4226 udev->usb3_lpm_u2_enabled = 0; 4227 4228 return 0; 4229 } 4230 4231 /* 4232 * Disable hub-initiated and device-initiated U1 and U2 entry. 4233 * Caller must own the bandwidth_mutex. 4234 * 4235 * This will call usb_enable_lpm() on failure, which will decrement 4236 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. 4237 */ 4238 int usb_disable_lpm(struct usb_device *udev) 4239 { 4240 struct usb_hcd *hcd; 4241 4242 if (!udev || !udev->parent || 4243 udev->speed < USB_SPEED_SUPER || 4244 !udev->lpm_capable || 4245 udev->state < USB_STATE_CONFIGURED) 4246 return 0; 4247 4248 hcd = bus_to_hcd(udev->bus); 4249 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) 4250 return 0; 4251 4252 udev->lpm_disable_count++; 4253 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) 4254 return 0; 4255 4256 /* If LPM is enabled, attempt to disable it. */ 4257 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) 4258 goto enable_lpm; 4259 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) 4260 goto enable_lpm; 4261 4262 return 0; 4263 4264 enable_lpm: 4265 usb_enable_lpm(udev); 4266 return -EBUSY; 4267 } 4268 EXPORT_SYMBOL_GPL(usb_disable_lpm); 4269 4270 /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ 4271 int usb_unlocked_disable_lpm(struct usb_device *udev) 4272 { 4273 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4274 int ret; 4275 4276 if (!hcd) 4277 return -EINVAL; 4278 4279 mutex_lock(hcd->bandwidth_mutex); 4280 ret = usb_disable_lpm(udev); 4281 mutex_unlock(hcd->bandwidth_mutex); 4282 4283 return ret; 4284 } 4285 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 4286 4287 /* 4288 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The 4289 * xHCI host policy may prevent U1 or U2 from being enabled. 4290 * 4291 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled 4292 * until the lpm_disable_count drops to zero. Caller must own the 4293 * bandwidth_mutex. 4294 */ 4295 void usb_enable_lpm(struct usb_device *udev) 4296 { 4297 struct usb_hcd *hcd; 4298 struct usb_hub *hub; 4299 struct usb_port *port_dev; 4300 4301 if (!udev || !udev->parent || 4302 udev->speed < USB_SPEED_SUPER || 4303 !udev->lpm_capable || 4304 udev->state < USB_STATE_CONFIGURED) 4305 return; 4306 4307 udev->lpm_disable_count--; 4308 hcd = bus_to_hcd(udev->bus); 4309 /* Double check that we can both enable and disable LPM. 4310 * Device must be configured to accept set feature U1/U2 timeout. 4311 */ 4312 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || 4313 !hcd->driver->disable_usb3_lpm_timeout) 4314 return; 4315 4316 if (udev->lpm_disable_count > 0) 4317 return; 4318 4319 hub = usb_hub_to_struct_hub(udev->parent); 4320 if (!hub) 4321 return; 4322 4323 port_dev = hub->ports[udev->portnum - 1]; 4324 4325 if (port_dev->usb3_lpm_u1_permit) 4326 usb_enable_link_state(hcd, udev, USB3_LPM_U1); 4327 4328 if (port_dev->usb3_lpm_u2_permit) 4329 usb_enable_link_state(hcd, udev, USB3_LPM_U2); 4330 } 4331 EXPORT_SYMBOL_GPL(usb_enable_lpm); 4332 4333 /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ 4334 void usb_unlocked_enable_lpm(struct usb_device *udev) 4335 { 4336 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4337 4338 if (!hcd) 4339 return; 4340 4341 mutex_lock(hcd->bandwidth_mutex); 4342 usb_enable_lpm(udev); 4343 mutex_unlock(hcd->bandwidth_mutex); 4344 } 4345 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4346 4347 /* usb3 devices use U3 for disabled, make sure remote wakeup is disabled */ 4348 static void hub_usb3_port_prepare_disable(struct usb_hub *hub, 4349 struct usb_port *port_dev) 4350 { 4351 struct usb_device *udev = port_dev->child; 4352 int ret; 4353 4354 if (udev && udev->port_is_suspended && udev->do_remote_wakeup) { 4355 ret = hub_set_port_link_state(hub, port_dev->portnum, 4356 USB_SS_PORT_LS_U0); 4357 if (!ret) { 4358 msleep(USB_RESUME_TIMEOUT); 4359 ret = usb_disable_remote_wakeup(udev); 4360 } 4361 if (ret) 4362 dev_warn(&udev->dev, 4363 "Port disable: can't disable remote wake\n"); 4364 udev->do_remote_wakeup = 0; 4365 } 4366 } 4367 4368 #else /* CONFIG_PM */ 4369 4370 #define hub_suspend NULL 4371 #define hub_resume NULL 4372 #define hub_reset_resume NULL 4373 4374 static inline void hub_usb3_port_prepare_disable(struct usb_hub *hub, 4375 struct usb_port *port_dev) { } 4376 4377 int usb_disable_lpm(struct usb_device *udev) 4378 { 4379 return 0; 4380 } 4381 EXPORT_SYMBOL_GPL(usb_disable_lpm); 4382 4383 void usb_enable_lpm(struct usb_device *udev) { } 4384 EXPORT_SYMBOL_GPL(usb_enable_lpm); 4385 4386 int usb_unlocked_disable_lpm(struct usb_device *udev) 4387 { 4388 return 0; 4389 } 4390 EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); 4391 4392 void usb_unlocked_enable_lpm(struct usb_device *udev) { } 4393 EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); 4394 4395 int usb_disable_ltm(struct usb_device *udev) 4396 { 4397 return 0; 4398 } 4399 EXPORT_SYMBOL_GPL(usb_disable_ltm); 4400 4401 void usb_enable_ltm(struct usb_device *udev) { } 4402 EXPORT_SYMBOL_GPL(usb_enable_ltm); 4403 4404 static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, 4405 u16 portstatus, u16 portchange) 4406 { 4407 return 0; 4408 } 4409 4410 #endif /* CONFIG_PM */ 4411 4412 /* 4413 * USB-3 does not have a similar link state as USB-2 that will avoid negotiating 4414 * a connection with a plugged-in cable but will signal the host when the cable 4415 * is unplugged. Disable remote wake and set link state to U3 for USB-3 devices 4416 */ 4417 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 4418 { 4419 struct usb_port *port_dev = hub->ports[port1 - 1]; 4420 struct usb_device *hdev = hub->hdev; 4421 int ret = 0; 4422 4423 if (!hub->error) { 4424 if (hub_is_superspeed(hub->hdev)) { 4425 hub_usb3_port_prepare_disable(hub, port_dev); 4426 ret = hub_set_port_link_state(hub, port_dev->portnum, 4427 USB_SS_PORT_LS_U3); 4428 } else { 4429 ret = usb_clear_port_feature(hdev, port1, 4430 USB_PORT_FEAT_ENABLE); 4431 } 4432 } 4433 if (port_dev->child && set_state) 4434 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED); 4435 if (ret && ret != -ENODEV) 4436 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret); 4437 return ret; 4438 } 4439 4440 /* 4441 * usb_port_disable - disable a usb device's upstream port 4442 * @udev: device to disable 4443 * Context: @udev locked, must be able to sleep. 4444 * 4445 * Disables a USB device that isn't in active use. 4446 */ 4447 int usb_port_disable(struct usb_device *udev) 4448 { 4449 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4450 4451 return hub_port_disable(hub, udev->portnum, 0); 4452 } 4453 4454 /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 4455 * 4456 * Between connect detection and reset signaling there must be a delay 4457 * of 100ms at least for debounce and power-settling. The corresponding 4458 * timer shall restart whenever the downstream port detects a disconnect. 4459 * 4460 * Apparently there are some bluetooth and irda-dongles and a number of 4461 * low-speed devices for which this debounce period may last over a second. 4462 * Not covered by the spec - but easy to deal with. 4463 * 4464 * This implementation uses a 1500ms total debounce timeout; if the 4465 * connection isn't stable by then it returns -ETIMEDOUT. It checks 4466 * every 25ms for transient disconnects. When the port status has been 4467 * unchanged for 100ms it returns the port status. 4468 */ 4469 int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) 4470 { 4471 int ret; 4472 u16 portchange, portstatus; 4473 unsigned connection = 0xffff; 4474 int total_time, stable_time = 0; 4475 struct usb_port *port_dev = hub->ports[port1 - 1]; 4476 4477 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 4478 ret = hub_port_status(hub, port1, &portstatus, &portchange); 4479 if (ret < 0) 4480 return ret; 4481 4482 if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 4483 (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 4484 if (!must_be_connected || 4485 (connection == USB_PORT_STAT_CONNECTION)) 4486 stable_time += HUB_DEBOUNCE_STEP; 4487 if (stable_time >= HUB_DEBOUNCE_STABLE) 4488 break; 4489 } else { 4490 stable_time = 0; 4491 connection = portstatus & USB_PORT_STAT_CONNECTION; 4492 } 4493 4494 if (portchange & USB_PORT_STAT_C_CONNECTION) { 4495 usb_clear_port_feature(hub->hdev, port1, 4496 USB_PORT_FEAT_C_CONNECTION); 4497 } 4498 4499 if (total_time >= HUB_DEBOUNCE_TIMEOUT) 4500 break; 4501 msleep(HUB_DEBOUNCE_STEP); 4502 } 4503 4504 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n", 4505 total_time, stable_time, portstatus); 4506 4507 if (stable_time < HUB_DEBOUNCE_STABLE) 4508 return -ETIMEDOUT; 4509 return portstatus; 4510 } 4511 4512 void usb_ep0_reinit(struct usb_device *udev) 4513 { 4514 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); 4515 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); 4516 usb_enable_endpoint(udev, &udev->ep0, true); 4517 } 4518 EXPORT_SYMBOL_GPL(usb_ep0_reinit); 4519 4520 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 4521 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 4522 4523 static int hub_set_address(struct usb_device *udev, int devnum) 4524 { 4525 int retval; 4526 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4527 4528 /* 4529 * The host controller will choose the device address, 4530 * instead of the core having chosen it earlier 4531 */ 4532 if (!hcd->driver->address_device && devnum <= 1) 4533 return -EINVAL; 4534 if (udev->state == USB_STATE_ADDRESS) 4535 return 0; 4536 if (udev->state != USB_STATE_DEFAULT) 4537 return -EINVAL; 4538 if (hcd->driver->address_device) 4539 retval = hcd->driver->address_device(hcd, udev); 4540 else 4541 retval = usb_control_msg(udev, usb_sndaddr0pipe(), 4542 USB_REQ_SET_ADDRESS, 0, devnum, 0, 4543 NULL, 0, USB_CTRL_SET_TIMEOUT); 4544 if (retval == 0) { 4545 update_devnum(udev, devnum); 4546 /* Device now using proper address. */ 4547 usb_set_device_state(udev, USB_STATE_ADDRESS); 4548 usb_ep0_reinit(udev); 4549 } 4550 return retval; 4551 } 4552 4553 /* 4554 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM 4555 * when they're plugged into a USB 2.0 port, but they don't work when LPM is 4556 * enabled. 4557 * 4558 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the 4559 * device says it supports the new USB 2.0 Link PM errata by setting the BESL 4560 * support bit in the BOS descriptor. 4561 */ 4562 static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev) 4563 { 4564 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 4565 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN; 4566 4567 if (!udev->usb2_hw_lpm_capable || !udev->bos) 4568 return; 4569 4570 if (hub) 4571 connect_type = hub->ports[udev->portnum - 1]->connect_type; 4572 4573 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) || 4574 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 4575 udev->usb2_hw_lpm_allowed = 1; 4576 usb_enable_usb2_hardware_lpm(udev); 4577 } 4578 } 4579 4580 static int hub_enable_device(struct usb_device *udev) 4581 { 4582 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 4583 4584 if (!hcd->driver->enable_device) 4585 return 0; 4586 if (udev->state == USB_STATE_ADDRESS) 4587 return 0; 4588 if (udev->state != USB_STATE_DEFAULT) 4589 return -EINVAL; 4590 4591 return hcd->driver->enable_device(hcd, udev); 4592 } 4593 4594 /* Reset device, (re)assign address, get device descriptor. 4595 * Device connection must be stable, no more debouncing needed. 4596 * Returns device in USB_STATE_ADDRESS, except on error. 4597 * 4598 * If this is called for an already-existing device (as part of 4599 * usb_reset_and_verify_device), the caller must own the device lock and 4600 * the port lock. For a newly detected device that is not accessible 4601 * through any global pointers, it's not necessary to lock the device, 4602 * but it is still necessary to lock the port. 4603 */ 4604 static int 4605 hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1, 4606 int retry_counter) 4607 { 4608 struct usb_device *hdev = hub->hdev; 4609 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 4610 struct usb_port *port_dev = hub->ports[port1 - 1]; 4611 int retries, operations, retval, i; 4612 unsigned delay = HUB_SHORT_RESET_TIME; 4613 enum usb_device_speed oldspeed = udev->speed; 4614 const char *speed; 4615 int devnum = udev->devnum; 4616 const char *driver_name; 4617 bool do_new_scheme; 4618 4619 /* root hub ports have a slightly longer reset period 4620 * (from USB 2.0 spec, section 7.1.7.5) 4621 */ 4622 if (!hdev->parent) { 4623 delay = HUB_ROOT_RESET_TIME; 4624 if (port1 == hdev->bus->otg_port) 4625 hdev->bus->b_hnp_enable = 0; 4626 } 4627 4628 /* Some low speed devices have problems with the quick delay, so */ 4629 /* be a bit pessimistic with those devices. RHbug #23670 */ 4630 if (oldspeed == USB_SPEED_LOW) 4631 delay = HUB_LONG_RESET_TIME; 4632 4633 mutex_lock(hcd->address0_mutex); 4634 4635 /* Reset the device; full speed may morph to high speed */ 4636 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ 4637 retval = hub_port_reset(hub, port1, udev, delay, false); 4638 if (retval < 0) /* error or disconnect */ 4639 goto fail; 4640 /* success, speed is known */ 4641 4642 retval = -ENODEV; 4643 4644 /* Don't allow speed changes at reset, except usb 3.0 to faster */ 4645 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed && 4646 !(oldspeed == USB_SPEED_SUPER && udev->speed > oldspeed)) { 4647 dev_dbg(&udev->dev, "device reset changed speed!\n"); 4648 goto fail; 4649 } 4650 oldspeed = udev->speed; 4651 4652 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 4653 * it's fixed size except for full speed devices. 4654 * For Wireless USB devices, ep0 max packet is always 512 (tho 4655 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. 4656 */ 4657 switch (udev->speed) { 4658 case USB_SPEED_SUPER_PLUS: 4659 case USB_SPEED_SUPER: 4660 case USB_SPEED_WIRELESS: /* fixed at 512 */ 4661 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); 4662 break; 4663 case USB_SPEED_HIGH: /* fixed at 64 */ 4664 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4665 break; 4666 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 4667 /* to determine the ep0 maxpacket size, try to read 4668 * the device descriptor to get bMaxPacketSize0 and 4669 * then correct our initial guess. 4670 */ 4671 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); 4672 break; 4673 case USB_SPEED_LOW: /* fixed at 8 */ 4674 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); 4675 break; 4676 default: 4677 goto fail; 4678 } 4679 4680 if (udev->speed == USB_SPEED_WIRELESS) 4681 speed = "variable speed Wireless"; 4682 else 4683 speed = usb_speed_string(udev->speed); 4684 4685 /* 4686 * The controller driver may be NULL if the controller device 4687 * is the middle device between platform device and roothub. 4688 * This middle device may not need a device driver due to 4689 * all hardware control can be at platform device driver, this 4690 * platform device is usually a dual-role USB controller device. 4691 */ 4692 if (udev->bus->controller->driver) 4693 driver_name = udev->bus->controller->driver->name; 4694 else 4695 driver_name = udev->bus->sysdev->driver->name; 4696 4697 if (udev->speed < USB_SPEED_SUPER) 4698 dev_info(&udev->dev, 4699 "%s %s USB device number %d using %s\n", 4700 (udev->config) ? "reset" : "new", speed, 4701 devnum, driver_name); 4702 4703 /* Set up TT records, if needed */ 4704 if (hdev->tt) { 4705 udev->tt = hdev->tt; 4706 udev->ttport = hdev->ttport; 4707 } else if (udev->speed != USB_SPEED_HIGH 4708 && hdev->speed == USB_SPEED_HIGH) { 4709 if (!hub->tt.hub) { 4710 dev_err(&udev->dev, "parent hub has no TT\n"); 4711 retval = -EINVAL; 4712 goto fail; 4713 } 4714 udev->tt = &hub->tt; 4715 udev->ttport = port1; 4716 } 4717 4718 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 4719 * Because device hardware and firmware is sometimes buggy in 4720 * this area, and this is how Linux has done it for ages. 4721 * Change it cautiously. 4722 * 4723 * NOTE: If use_new_scheme() is true we will start by issuing 4724 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 4725 * so it may help with some non-standards-compliant devices. 4726 * Otherwise we start with SET_ADDRESS and then try to read the 4727 * first 8 bytes of the device descriptor to get the ep0 maxpacket 4728 * value. 4729 */ 4730 do_new_scheme = use_new_scheme(udev, retry_counter, port_dev); 4731 4732 for (retries = 0; retries < GET_DESCRIPTOR_TRIES; (++retries, msleep(100))) { 4733 if (do_new_scheme) { 4734 struct usb_device_descriptor *buf; 4735 int r = 0; 4736 4737 retval = hub_enable_device(udev); 4738 if (retval < 0) { 4739 dev_err(&udev->dev, 4740 "hub failed to enable device, error %d\n", 4741 retval); 4742 goto fail; 4743 } 4744 4745 #define GET_DESCRIPTOR_BUFSIZE 64 4746 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 4747 if (!buf) { 4748 retval = -ENOMEM; 4749 continue; 4750 } 4751 4752 /* Retry on all errors; some devices are flakey. 4753 * 255 is for WUSB devices, we actually need to use 4754 * 512 (WUSB1.0[4.8.1]). 4755 */ 4756 for (operations = 0; operations < GET_MAXPACKET0_TRIES; 4757 ++operations) { 4758 buf->bMaxPacketSize0 = 0; 4759 r = usb_control_msg(udev, usb_rcvaddr0pipe(), 4760 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 4761 USB_DT_DEVICE << 8, 0, 4762 buf, GET_DESCRIPTOR_BUFSIZE, 4763 initial_descriptor_timeout); 4764 switch (buf->bMaxPacketSize0) { 4765 case 8: case 16: case 32: case 64: case 255: 4766 if (buf->bDescriptorType == 4767 USB_DT_DEVICE) { 4768 r = 0; 4769 break; 4770 } 4771 fallthrough; 4772 default: 4773 if (r == 0) 4774 r = -EPROTO; 4775 break; 4776 } 4777 /* 4778 * Some devices time out if they are powered on 4779 * when already connected. They need a second 4780 * reset. But only on the first attempt, 4781 * lest we get into a time out/reset loop 4782 */ 4783 if (r == 0 || (r == -ETIMEDOUT && 4784 retries == 0 && 4785 udev->speed > USB_SPEED_FULL)) 4786 break; 4787 } 4788 udev->descriptor.bMaxPacketSize0 = 4789 buf->bMaxPacketSize0; 4790 kfree(buf); 4791 4792 retval = hub_port_reset(hub, port1, udev, delay, false); 4793 if (retval < 0) /* error or disconnect */ 4794 goto fail; 4795 if (oldspeed != udev->speed) { 4796 dev_dbg(&udev->dev, 4797 "device reset changed speed!\n"); 4798 retval = -ENODEV; 4799 goto fail; 4800 } 4801 if (r) { 4802 if (r != -ENODEV) 4803 dev_err(&udev->dev, "device descriptor read/64, error %d\n", 4804 r); 4805 retval = -EMSGSIZE; 4806 continue; 4807 } 4808 #undef GET_DESCRIPTOR_BUFSIZE 4809 } 4810 4811 /* 4812 * If device is WUSB, we already assigned an 4813 * unauthorized address in the Connect Ack sequence; 4814 * authorization will assign the final address. 4815 */ 4816 if (udev->wusb == 0) { 4817 for (operations = 0; operations < SET_ADDRESS_TRIES; ++operations) { 4818 retval = hub_set_address(udev, devnum); 4819 if (retval >= 0) 4820 break; 4821 msleep(200); 4822 } 4823 if (retval < 0) { 4824 if (retval != -ENODEV) 4825 dev_err(&udev->dev, "device not accepting address %d, error %d\n", 4826 devnum, retval); 4827 goto fail; 4828 } 4829 if (udev->speed >= USB_SPEED_SUPER) { 4830 devnum = udev->devnum; 4831 dev_info(&udev->dev, 4832 "%s SuperSpeed%s%s USB device number %d using %s\n", 4833 (udev->config) ? "reset" : "new", 4834 (udev->speed == USB_SPEED_SUPER_PLUS) ? 4835 " Plus" : "", 4836 (udev->ssp_rate == USB_SSP_GEN_2x2) ? 4837 " Gen 2x2" : 4838 (udev->ssp_rate == USB_SSP_GEN_2x1) ? 4839 " Gen 2x1" : 4840 (udev->ssp_rate == USB_SSP_GEN_1x2) ? 4841 " Gen 1x2" : "", 4842 devnum, driver_name); 4843 } 4844 4845 /* cope with hardware quirkiness: 4846 * - let SET_ADDRESS settle, some device hardware wants it 4847 * - read ep0 maxpacket even for high and low speed, 4848 */ 4849 msleep(10); 4850 if (do_new_scheme) 4851 break; 4852 } 4853 4854 retval = usb_get_device_descriptor(udev, 8); 4855 if (retval < 8) { 4856 if (retval != -ENODEV) 4857 dev_err(&udev->dev, 4858 "device descriptor read/8, error %d\n", 4859 retval); 4860 if (retval >= 0) 4861 retval = -EMSGSIZE; 4862 } else { 4863 u32 delay; 4864 4865 retval = 0; 4866 4867 delay = udev->parent->hub_delay; 4868 udev->hub_delay = min_t(u32, delay, 4869 USB_TP_TRANSMISSION_DELAY_MAX); 4870 retval = usb_set_isoch_delay(udev); 4871 if (retval) { 4872 dev_dbg(&udev->dev, 4873 "Failed set isoch delay, error %d\n", 4874 retval); 4875 retval = 0; 4876 } 4877 break; 4878 } 4879 } 4880 if (retval) 4881 goto fail; 4882 4883 /* 4884 * Some superspeed devices have finished the link training process 4885 * and attached to a superspeed hub port, but the device descriptor 4886 * got from those devices show they aren't superspeed devices. Warm 4887 * reset the port attached by the devices can fix them. 4888 */ 4889 if ((udev->speed >= USB_SPEED_SUPER) && 4890 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { 4891 dev_err(&udev->dev, "got a wrong device descriptor, " 4892 "warm reset device\n"); 4893 hub_port_reset(hub, port1, udev, 4894 HUB_BH_RESET_TIME, true); 4895 retval = -EINVAL; 4896 goto fail; 4897 } 4898 4899 if (udev->descriptor.bMaxPacketSize0 == 0xff || 4900 udev->speed >= USB_SPEED_SUPER) 4901 i = 512; 4902 else 4903 i = udev->descriptor.bMaxPacketSize0; 4904 if (usb_endpoint_maxp(&udev->ep0.desc) != i) { 4905 if (udev->speed == USB_SPEED_LOW || 4906 !(i == 8 || i == 16 || i == 32 || i == 64)) { 4907 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); 4908 retval = -EMSGSIZE; 4909 goto fail; 4910 } 4911 if (udev->speed == USB_SPEED_FULL) 4912 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 4913 else 4914 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); 4915 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 4916 usb_ep0_reinit(udev); 4917 } 4918 4919 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 4920 if (retval < (signed)sizeof(udev->descriptor)) { 4921 if (retval != -ENODEV) 4922 dev_err(&udev->dev, "device descriptor read/all, error %d\n", 4923 retval); 4924 if (retval >= 0) 4925 retval = -ENOMSG; 4926 goto fail; 4927 } 4928 4929 usb_detect_quirks(udev); 4930 4931 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { 4932 retval = usb_get_bos_descriptor(udev); 4933 if (!retval) { 4934 udev->lpm_capable = usb_device_supports_lpm(udev); 4935 usb_set_lpm_parameters(udev); 4936 } 4937 } 4938 4939 retval = 0; 4940 /* notify HCD that we have a device connected and addressed */ 4941 if (hcd->driver->update_device) 4942 hcd->driver->update_device(hcd, udev); 4943 hub_set_initial_usb2_lpm_policy(udev); 4944 fail: 4945 if (retval) { 4946 hub_port_disable(hub, port1, 0); 4947 update_devnum(udev, devnum); /* for disconnect processing */ 4948 } 4949 mutex_unlock(hcd->address0_mutex); 4950 return retval; 4951 } 4952 4953 static void 4954 check_highspeed(struct usb_hub *hub, struct usb_device *udev, int port1) 4955 { 4956 struct usb_qualifier_descriptor *qual; 4957 int status; 4958 4959 if (udev->quirks & USB_QUIRK_DEVICE_QUALIFIER) 4960 return; 4961 4962 qual = kmalloc(sizeof *qual, GFP_KERNEL); 4963 if (qual == NULL) 4964 return; 4965 4966 status = usb_get_descriptor(udev, USB_DT_DEVICE_QUALIFIER, 0, 4967 qual, sizeof *qual); 4968 if (status == sizeof *qual) { 4969 dev_info(&udev->dev, "not running at top speed; " 4970 "connect to a high speed hub\n"); 4971 /* hub LEDs are probably harder to miss than syslog */ 4972 if (hub->has_indicators) { 4973 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 4974 queue_delayed_work(system_power_efficient_wq, 4975 &hub->leds, 0); 4976 } 4977 } 4978 kfree(qual); 4979 } 4980 4981 static unsigned 4982 hub_power_remaining(struct usb_hub *hub) 4983 { 4984 struct usb_device *hdev = hub->hdev; 4985 int remaining; 4986 int port1; 4987 4988 if (!hub->limited_power) 4989 return 0; 4990 4991 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; 4992 for (port1 = 1; port1 <= hdev->maxchild; ++port1) { 4993 struct usb_port *port_dev = hub->ports[port1 - 1]; 4994 struct usb_device *udev = port_dev->child; 4995 unsigned unit_load; 4996 int delta; 4997 4998 if (!udev) 4999 continue; 5000 if (hub_is_superspeed(udev)) 5001 unit_load = 150; 5002 else 5003 unit_load = 100; 5004 5005 /* 5006 * Unconfigured devices may not use more than one unit load, 5007 * or 8mA for OTG ports 5008 */ 5009 if (udev->actconfig) 5010 delta = usb_get_max_power(udev, udev->actconfig); 5011 else if (port1 != udev->bus->otg_port || hdev->parent) 5012 delta = unit_load; 5013 else 5014 delta = 8; 5015 if (delta > hub->mA_per_port) 5016 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n", 5017 delta, hub->mA_per_port); 5018 remaining -= delta; 5019 } 5020 if (remaining < 0) { 5021 dev_warn(hub->intfdev, "%dmA over power budget!\n", 5022 -remaining); 5023 remaining = 0; 5024 } 5025 return remaining; 5026 } 5027 5028 5029 static int descriptors_changed(struct usb_device *udev, 5030 struct usb_device_descriptor *old_device_descriptor, 5031 struct usb_host_bos *old_bos) 5032 { 5033 int changed = 0; 5034 unsigned index; 5035 unsigned serial_len = 0; 5036 unsigned len; 5037 unsigned old_length; 5038 int length; 5039 char *buf; 5040 5041 if (memcmp(&udev->descriptor, old_device_descriptor, 5042 sizeof(*old_device_descriptor)) != 0) 5043 return 1; 5044 5045 if ((old_bos && !udev->bos) || (!old_bos && udev->bos)) 5046 return 1; 5047 if (udev->bos) { 5048 len = le16_to_cpu(udev->bos->desc->wTotalLength); 5049 if (len != le16_to_cpu(old_bos->desc->wTotalLength)) 5050 return 1; 5051 if (memcmp(udev->bos->desc, old_bos->desc, len)) 5052 return 1; 5053 } 5054 5055 /* Since the idVendor, idProduct, and bcdDevice values in the 5056 * device descriptor haven't changed, we will assume the 5057 * Manufacturer and Product strings haven't changed either. 5058 * But the SerialNumber string could be different (e.g., a 5059 * different flash card of the same brand). 5060 */ 5061 if (udev->serial) 5062 serial_len = strlen(udev->serial) + 1; 5063 5064 len = serial_len; 5065 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5066 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5067 len = max(len, old_length); 5068 } 5069 5070 buf = kmalloc(len, GFP_NOIO); 5071 if (!buf) 5072 /* assume the worst */ 5073 return 1; 5074 5075 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 5076 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 5077 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 5078 old_length); 5079 if (length != old_length) { 5080 dev_dbg(&udev->dev, "config index %d, error %d\n", 5081 index, length); 5082 changed = 1; 5083 break; 5084 } 5085 if (memcmp(buf, udev->rawdescriptors[index], old_length) 5086 != 0) { 5087 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 5088 index, 5089 ((struct usb_config_descriptor *) buf)-> 5090 bConfigurationValue); 5091 changed = 1; 5092 break; 5093 } 5094 } 5095 5096 if (!changed && serial_len) { 5097 length = usb_string(udev, udev->descriptor.iSerialNumber, 5098 buf, serial_len); 5099 if (length + 1 != serial_len) { 5100 dev_dbg(&udev->dev, "serial string error %d\n", 5101 length); 5102 changed = 1; 5103 } else if (memcmp(buf, udev->serial, length) != 0) { 5104 dev_dbg(&udev->dev, "serial string changed\n"); 5105 changed = 1; 5106 } 5107 } 5108 5109 kfree(buf); 5110 return changed; 5111 } 5112 5113 static void hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus, 5114 u16 portchange) 5115 { 5116 int status = -ENODEV; 5117 int i; 5118 unsigned unit_load; 5119 struct usb_device *hdev = hub->hdev; 5120 struct usb_hcd *hcd = bus_to_hcd(hdev->bus); 5121 struct usb_port *port_dev = hub->ports[port1 - 1]; 5122 struct usb_device *udev = port_dev->child; 5123 static int unreliable_port = -1; 5124 5125 /* Disconnect any existing devices under this port */ 5126 if (udev) { 5127 if (hcd->usb_phy && !hdev->parent) 5128 usb_phy_notify_disconnect(hcd->usb_phy, udev->speed); 5129 usb_disconnect(&port_dev->child); 5130 } 5131 5132 /* We can forget about a "removed" device when there's a physical 5133 * disconnect or the connect status changes. 5134 */ 5135 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 5136 (portchange & USB_PORT_STAT_C_CONNECTION)) 5137 clear_bit(port1, hub->removed_bits); 5138 5139 if (portchange & (USB_PORT_STAT_C_CONNECTION | 5140 USB_PORT_STAT_C_ENABLE)) { 5141 status = hub_port_debounce_be_stable(hub, port1); 5142 if (status < 0) { 5143 if (status != -ENODEV && 5144 port1 != unreliable_port && 5145 printk_ratelimit()) 5146 dev_err(&port_dev->dev, "connect-debounce failed\n"); 5147 portstatus &= ~USB_PORT_STAT_CONNECTION; 5148 unreliable_port = port1; 5149 } else { 5150 portstatus = status; 5151 } 5152 } 5153 5154 /* Return now if debouncing failed or nothing is connected or 5155 * the device was "removed". 5156 */ 5157 if (!(portstatus & USB_PORT_STAT_CONNECTION) || 5158 test_bit(port1, hub->removed_bits)) { 5159 5160 /* 5161 * maybe switch power back on (e.g. root hub was reset) 5162 * but only if the port isn't owned by someone else. 5163 */ 5164 if (hub_is_port_power_switchable(hub) 5165 && !port_is_power_on(hub, portstatus) 5166 && !port_dev->port_owner) 5167 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 5168 5169 if (portstatus & USB_PORT_STAT_ENABLE) 5170 goto done; 5171 return; 5172 } 5173 if (hub_is_superspeed(hub->hdev)) 5174 unit_load = 150; 5175 else 5176 unit_load = 100; 5177 5178 status = 0; 5179 for (i = 0; i < PORT_INIT_TRIES; i++) { 5180 5181 /* reallocate for each attempt, since references 5182 * to the previous one can escape in various ways 5183 */ 5184 udev = usb_alloc_dev(hdev, hdev->bus, port1); 5185 if (!udev) { 5186 dev_err(&port_dev->dev, 5187 "couldn't allocate usb_device\n"); 5188 goto done; 5189 } 5190 5191 usb_set_device_state(udev, USB_STATE_POWERED); 5192 udev->bus_mA = hub->mA_per_port; 5193 udev->level = hdev->level + 1; 5194 udev->wusb = hub_is_wusb(hub); 5195 5196 /* Devices connected to SuperSpeed hubs are USB 3.0 or later */ 5197 if (hub_is_superspeed(hub->hdev)) 5198 udev->speed = USB_SPEED_SUPER; 5199 else 5200 udev->speed = USB_SPEED_UNKNOWN; 5201 5202 choose_devnum(udev); 5203 if (udev->devnum <= 0) { 5204 status = -ENOTCONN; /* Don't retry */ 5205 goto loop; 5206 } 5207 5208 /* reset (non-USB 3.0 devices) and get descriptor */ 5209 usb_lock_port(port_dev); 5210 status = hub_port_init(hub, udev, port1, i); 5211 usb_unlock_port(port_dev); 5212 if (status < 0) 5213 goto loop; 5214 5215 if (udev->quirks & USB_QUIRK_DELAY_INIT) 5216 msleep(2000); 5217 5218 /* consecutive bus-powered hubs aren't reliable; they can 5219 * violate the voltage drop budget. if the new child has 5220 * a "powered" LED, users should notice we didn't enable it 5221 * (without reading syslog), even without per-port LEDs 5222 * on the parent. 5223 */ 5224 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 5225 && udev->bus_mA <= unit_load) { 5226 u16 devstat; 5227 5228 status = usb_get_std_status(udev, USB_RECIP_DEVICE, 0, 5229 &devstat); 5230 if (status) { 5231 dev_dbg(&udev->dev, "get status %d ?\n", status); 5232 goto loop_disable; 5233 } 5234 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 5235 dev_err(&udev->dev, 5236 "can't connect bus-powered hub " 5237 "to this port\n"); 5238 if (hub->has_indicators) { 5239 hub->indicator[port1-1] = 5240 INDICATOR_AMBER_BLINK; 5241 queue_delayed_work( 5242 system_power_efficient_wq, 5243 &hub->leds, 0); 5244 } 5245 status = -ENOTCONN; /* Don't retry */ 5246 goto loop_disable; 5247 } 5248 } 5249 5250 /* check for devices running slower than they could */ 5251 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 5252 && udev->speed == USB_SPEED_FULL 5253 && highspeed_hubs != 0) 5254 check_highspeed(hub, udev, port1); 5255 5256 /* Store the parent's children[] pointer. At this point 5257 * udev becomes globally accessible, although presumably 5258 * no one will look at it until hdev is unlocked. 5259 */ 5260 status = 0; 5261 5262 mutex_lock(&usb_port_peer_mutex); 5263 5264 /* We mustn't add new devices if the parent hub has 5265 * been disconnected; we would race with the 5266 * recursively_mark_NOTATTACHED() routine. 5267 */ 5268 spin_lock_irq(&device_state_lock); 5269 if (hdev->state == USB_STATE_NOTATTACHED) 5270 status = -ENOTCONN; 5271 else 5272 port_dev->child = udev; 5273 spin_unlock_irq(&device_state_lock); 5274 mutex_unlock(&usb_port_peer_mutex); 5275 5276 /* Run it through the hoops (find a driver, etc) */ 5277 if (!status) { 5278 status = usb_new_device(udev); 5279 if (status) { 5280 mutex_lock(&usb_port_peer_mutex); 5281 spin_lock_irq(&device_state_lock); 5282 port_dev->child = NULL; 5283 spin_unlock_irq(&device_state_lock); 5284 mutex_unlock(&usb_port_peer_mutex); 5285 } else { 5286 if (hcd->usb_phy && !hdev->parent) 5287 usb_phy_notify_connect(hcd->usb_phy, 5288 udev->speed); 5289 } 5290 } 5291 5292 if (status) 5293 goto loop_disable; 5294 5295 status = hub_power_remaining(hub); 5296 if (status) 5297 dev_dbg(hub->intfdev, "%dmA power budget left\n", status); 5298 5299 return; 5300 5301 loop_disable: 5302 hub_port_disable(hub, port1, 1); 5303 loop: 5304 usb_ep0_reinit(udev); 5305 release_devnum(udev); 5306 hub_free_dev(udev); 5307 usb_put_dev(udev); 5308 if ((status == -ENOTCONN) || (status == -ENOTSUPP)) 5309 break; 5310 5311 /* When halfway through our retry count, power-cycle the port */ 5312 if (i == (PORT_INIT_TRIES - 1) / 2) { 5313 dev_info(&port_dev->dev, "attempt power cycle\n"); 5314 usb_hub_set_port_power(hdev, hub, port1, false); 5315 msleep(2 * hub_power_on_good_delay(hub)); 5316 usb_hub_set_port_power(hdev, hub, port1, true); 5317 msleep(hub_power_on_good_delay(hub)); 5318 } 5319 } 5320 if (hub->hdev->parent || 5321 !hcd->driver->port_handed_over || 5322 !(hcd->driver->port_handed_over)(hcd, port1)) { 5323 if (status != -ENOTCONN && status != -ENODEV) 5324 dev_err(&port_dev->dev, 5325 "unable to enumerate USB device\n"); 5326 } 5327 5328 done: 5329 hub_port_disable(hub, port1, 1); 5330 if (hcd->driver->relinquish_port && !hub->hdev->parent) { 5331 if (status != -ENOTCONN && status != -ENODEV) 5332 hcd->driver->relinquish_port(hcd, port1); 5333 } 5334 } 5335 5336 /* Handle physical or logical connection change events. 5337 * This routine is called when: 5338 * a port connection-change occurs; 5339 * a port enable-change occurs (often caused by EMI); 5340 * usb_reset_and_verify_device() encounters changed descriptors (as from 5341 * a firmware download) 5342 * caller already locked the hub 5343 */ 5344 static void hub_port_connect_change(struct usb_hub *hub, int port1, 5345 u16 portstatus, u16 portchange) 5346 __must_hold(&port_dev->status_lock) 5347 { 5348 struct usb_port *port_dev = hub->ports[port1 - 1]; 5349 struct usb_device *udev = port_dev->child; 5350 struct usb_device_descriptor descriptor; 5351 int status = -ENODEV; 5352 int retval; 5353 5354 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n", portstatus, 5355 portchange, portspeed(hub, portstatus)); 5356 5357 if (hub->has_indicators) { 5358 set_port_led(hub, port1, HUB_LED_AUTO); 5359 hub->indicator[port1-1] = INDICATOR_AUTO; 5360 } 5361 5362 #ifdef CONFIG_USB_OTG 5363 /* during HNP, don't repeat the debounce */ 5364 if (hub->hdev->bus->is_b_host) 5365 portchange &= ~(USB_PORT_STAT_C_CONNECTION | 5366 USB_PORT_STAT_C_ENABLE); 5367 #endif 5368 5369 /* Try to resuscitate an existing device */ 5370 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && 5371 udev->state != USB_STATE_NOTATTACHED) { 5372 if (portstatus & USB_PORT_STAT_ENABLE) { 5373 /* 5374 * USB-3 connections are initialized automatically by 5375 * the hostcontroller hardware. Therefore check for 5376 * changed device descriptors before resuscitating the 5377 * device. 5378 */ 5379 descriptor = udev->descriptor; 5380 retval = usb_get_device_descriptor(udev, 5381 sizeof(udev->descriptor)); 5382 if (retval < 0) { 5383 dev_dbg(&udev->dev, 5384 "can't read device descriptor %d\n", 5385 retval); 5386 } else { 5387 if (descriptors_changed(udev, &descriptor, 5388 udev->bos)) { 5389 dev_dbg(&udev->dev, 5390 "device descriptor has changed\n"); 5391 /* for disconnect() calls */ 5392 udev->descriptor = descriptor; 5393 } else { 5394 status = 0; /* Nothing to do */ 5395 } 5396 } 5397 #ifdef CONFIG_PM 5398 } else if (udev->state == USB_STATE_SUSPENDED && 5399 udev->persist_enabled) { 5400 /* For a suspended device, treat this as a 5401 * remote wakeup event. 5402 */ 5403 usb_unlock_port(port_dev); 5404 status = usb_remote_wakeup(udev); 5405 usb_lock_port(port_dev); 5406 #endif 5407 } else { 5408 /* Don't resuscitate */; 5409 } 5410 } 5411 clear_bit(port1, hub->change_bits); 5412 5413 /* successfully revalidated the connection */ 5414 if (status == 0) 5415 return; 5416 5417 usb_unlock_port(port_dev); 5418 hub_port_connect(hub, port1, portstatus, portchange); 5419 usb_lock_port(port_dev); 5420 } 5421 5422 /* Handle notifying userspace about hub over-current events */ 5423 static void port_over_current_notify(struct usb_port *port_dev) 5424 { 5425 char *envp[3]; 5426 struct device *hub_dev; 5427 char *port_dev_path; 5428 5429 sysfs_notify(&port_dev->dev.kobj, NULL, "over_current_count"); 5430 5431 hub_dev = port_dev->dev.parent; 5432 5433 if (!hub_dev) 5434 return; 5435 5436 port_dev_path = kobject_get_path(&port_dev->dev.kobj, GFP_KERNEL); 5437 if (!port_dev_path) 5438 return; 5439 5440 envp[0] = kasprintf(GFP_KERNEL, "OVER_CURRENT_PORT=%s", port_dev_path); 5441 if (!envp[0]) 5442 goto exit_path; 5443 5444 envp[1] = kasprintf(GFP_KERNEL, "OVER_CURRENT_COUNT=%u", 5445 port_dev->over_current_count); 5446 if (!envp[1]) 5447 goto exit; 5448 5449 envp[2] = NULL; 5450 kobject_uevent_env(&hub_dev->kobj, KOBJ_CHANGE, envp); 5451 5452 kfree(envp[1]); 5453 exit: 5454 kfree(envp[0]); 5455 exit_path: 5456 kfree(port_dev_path); 5457 } 5458 5459 static void port_event(struct usb_hub *hub, int port1) 5460 __must_hold(&port_dev->status_lock) 5461 { 5462 int connect_change; 5463 struct usb_port *port_dev = hub->ports[port1 - 1]; 5464 struct usb_device *udev = port_dev->child; 5465 struct usb_device *hdev = hub->hdev; 5466 u16 portstatus, portchange; 5467 5468 connect_change = test_bit(port1, hub->change_bits); 5469 clear_bit(port1, hub->event_bits); 5470 clear_bit(port1, hub->wakeup_bits); 5471 5472 if (hub_port_status(hub, port1, &portstatus, &portchange) < 0) 5473 return; 5474 5475 if (portchange & USB_PORT_STAT_C_CONNECTION) { 5476 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); 5477 connect_change = 1; 5478 } 5479 5480 if (portchange & USB_PORT_STAT_C_ENABLE) { 5481 if (!connect_change) 5482 dev_dbg(&port_dev->dev, "enable change, status %08x\n", 5483 portstatus); 5484 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); 5485 5486 /* 5487 * EM interference sometimes causes badly shielded USB devices 5488 * to be shutdown by the hub, this hack enables them again. 5489 * Works at least with mouse driver. 5490 */ 5491 if (!(portstatus & USB_PORT_STAT_ENABLE) 5492 && !connect_change && udev) { 5493 dev_err(&port_dev->dev, "disabled by hub (EMI?), re-enabling...\n"); 5494 connect_change = 1; 5495 } 5496 } 5497 5498 if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 5499 u16 status = 0, unused; 5500 port_dev->over_current_count++; 5501 port_over_current_notify(port_dev); 5502 5503 dev_dbg(&port_dev->dev, "over-current change #%u\n", 5504 port_dev->over_current_count); 5505 usb_clear_port_feature(hdev, port1, 5506 USB_PORT_FEAT_C_OVER_CURRENT); 5507 msleep(100); /* Cool down */ 5508 hub_power_on(hub, true); 5509 hub_port_status(hub, port1, &status, &unused); 5510 if (status & USB_PORT_STAT_OVERCURRENT) 5511 dev_err(&port_dev->dev, "over-current condition\n"); 5512 } 5513 5514 if (portchange & USB_PORT_STAT_C_RESET) { 5515 dev_dbg(&port_dev->dev, "reset change\n"); 5516 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_RESET); 5517 } 5518 if ((portchange & USB_PORT_STAT_C_BH_RESET) 5519 && hub_is_superspeed(hdev)) { 5520 dev_dbg(&port_dev->dev, "warm reset change\n"); 5521 usb_clear_port_feature(hdev, port1, 5522 USB_PORT_FEAT_C_BH_PORT_RESET); 5523 } 5524 if (portchange & USB_PORT_STAT_C_LINK_STATE) { 5525 dev_dbg(&port_dev->dev, "link state change\n"); 5526 usb_clear_port_feature(hdev, port1, 5527 USB_PORT_FEAT_C_PORT_LINK_STATE); 5528 } 5529 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { 5530 dev_warn(&port_dev->dev, "config error\n"); 5531 usb_clear_port_feature(hdev, port1, 5532 USB_PORT_FEAT_C_PORT_CONFIG_ERROR); 5533 } 5534 5535 /* skip port actions that require the port to be powered on */ 5536 if (!pm_runtime_active(&port_dev->dev)) 5537 return; 5538 5539 if (hub_handle_remote_wakeup(hub, port1, portstatus, portchange)) 5540 connect_change = 1; 5541 5542 /* 5543 * Warm reset a USB3 protocol port if it's in 5544 * SS.Inactive state. 5545 */ 5546 if (hub_port_warm_reset_required(hub, port1, portstatus)) { 5547 dev_dbg(&port_dev->dev, "do warm reset\n"); 5548 if (!udev || !(portstatus & USB_PORT_STAT_CONNECTION) 5549 || udev->state == USB_STATE_NOTATTACHED) { 5550 if (hub_port_reset(hub, port1, NULL, 5551 HUB_BH_RESET_TIME, true) < 0) 5552 hub_port_disable(hub, port1, 1); 5553 } else { 5554 usb_unlock_port(port_dev); 5555 usb_lock_device(udev); 5556 usb_reset_device(udev); 5557 usb_unlock_device(udev); 5558 usb_lock_port(port_dev); 5559 connect_change = 0; 5560 } 5561 } 5562 5563 if (connect_change) 5564 hub_port_connect_change(hub, port1, portstatus, portchange); 5565 } 5566 5567 static void hub_event(struct work_struct *work) 5568 { 5569 struct usb_device *hdev; 5570 struct usb_interface *intf; 5571 struct usb_hub *hub; 5572 struct device *hub_dev; 5573 u16 hubstatus; 5574 u16 hubchange; 5575 int i, ret; 5576 5577 hub = container_of(work, struct usb_hub, events); 5578 hdev = hub->hdev; 5579 hub_dev = hub->intfdev; 5580 intf = to_usb_interface(hub_dev); 5581 5582 kcov_remote_start_usb((u64)hdev->bus->busnum); 5583 5584 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 5585 hdev->state, hdev->maxchild, 5586 /* NOTE: expects max 15 ports... */ 5587 (u16) hub->change_bits[0], 5588 (u16) hub->event_bits[0]); 5589 5590 /* Lock the device, then check to see if we were 5591 * disconnected while waiting for the lock to succeed. */ 5592 usb_lock_device(hdev); 5593 if (unlikely(hub->disconnected)) 5594 goto out_hdev_lock; 5595 5596 /* If the hub has died, clean up after it */ 5597 if (hdev->state == USB_STATE_NOTATTACHED) { 5598 hub->error = -ENODEV; 5599 hub_quiesce(hub, HUB_DISCONNECT); 5600 goto out_hdev_lock; 5601 } 5602 5603 /* Autoresume */ 5604 ret = usb_autopm_get_interface(intf); 5605 if (ret) { 5606 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); 5607 goto out_hdev_lock; 5608 } 5609 5610 /* If this is an inactive hub, do nothing */ 5611 if (hub->quiescing) 5612 goto out_autopm; 5613 5614 if (hub->error) { 5615 dev_dbg(hub_dev, "resetting for error %d\n", hub->error); 5616 5617 ret = usb_reset_device(hdev); 5618 if (ret) { 5619 dev_dbg(hub_dev, "error resetting hub: %d\n", ret); 5620 goto out_autopm; 5621 } 5622 5623 hub->nerrors = 0; 5624 hub->error = 0; 5625 } 5626 5627 /* deal with port status changes */ 5628 for (i = 1; i <= hdev->maxchild; i++) { 5629 struct usb_port *port_dev = hub->ports[i - 1]; 5630 5631 if (test_bit(i, hub->event_bits) 5632 || test_bit(i, hub->change_bits) 5633 || test_bit(i, hub->wakeup_bits)) { 5634 /* 5635 * The get_noresume and barrier ensure that if 5636 * the port was in the process of resuming, we 5637 * flush that work and keep the port active for 5638 * the duration of the port_event(). However, 5639 * if the port is runtime pm suspended 5640 * (powered-off), we leave it in that state, run 5641 * an abbreviated port_event(), and move on. 5642 */ 5643 pm_runtime_get_noresume(&port_dev->dev); 5644 pm_runtime_barrier(&port_dev->dev); 5645 usb_lock_port(port_dev); 5646 port_event(hub, i); 5647 usb_unlock_port(port_dev); 5648 pm_runtime_put_sync(&port_dev->dev); 5649 } 5650 } 5651 5652 /* deal with hub status changes */ 5653 if (test_and_clear_bit(0, hub->event_bits) == 0) 5654 ; /* do nothing */ 5655 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 5656 dev_err(hub_dev, "get_hub_status failed\n"); 5657 else { 5658 if (hubchange & HUB_CHANGE_LOCAL_POWER) { 5659 dev_dbg(hub_dev, "power change\n"); 5660 clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 5661 if (hubstatus & HUB_STATUS_LOCAL_POWER) 5662 /* FIXME: Is this always true? */ 5663 hub->limited_power = 1; 5664 else 5665 hub->limited_power = 0; 5666 } 5667 if (hubchange & HUB_CHANGE_OVERCURRENT) { 5668 u16 status = 0; 5669 u16 unused; 5670 5671 dev_dbg(hub_dev, "over-current change\n"); 5672 clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 5673 msleep(500); /* Cool down */ 5674 hub_power_on(hub, true); 5675 hub_hub_status(hub, &status, &unused); 5676 if (status & HUB_STATUS_OVERCURRENT) 5677 dev_err(hub_dev, "over-current condition\n"); 5678 } 5679 } 5680 5681 out_autopm: 5682 /* Balance the usb_autopm_get_interface() above */ 5683 usb_autopm_put_interface_no_suspend(intf); 5684 out_hdev_lock: 5685 usb_unlock_device(hdev); 5686 5687 /* Balance the stuff in kick_hub_wq() and allow autosuspend */ 5688 usb_autopm_put_interface(intf); 5689 kref_put(&hub->kref, hub_release); 5690 5691 kcov_remote_stop(); 5692 } 5693 5694 static const struct usb_device_id hub_id_table[] = { 5695 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5696 | USB_DEVICE_ID_MATCH_PRODUCT 5697 | USB_DEVICE_ID_MATCH_INT_CLASS, 5698 .idVendor = USB_VENDOR_SMSC, 5699 .idProduct = USB_PRODUCT_USB5534B, 5700 .bInterfaceClass = USB_CLASS_HUB, 5701 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5702 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5703 | USB_DEVICE_ID_MATCH_PRODUCT, 5704 .idVendor = USB_VENDOR_CYPRESS, 5705 .idProduct = USB_PRODUCT_CY7C65632, 5706 .driver_info = HUB_QUIRK_DISABLE_AUTOSUSPEND}, 5707 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR 5708 | USB_DEVICE_ID_MATCH_INT_CLASS, 5709 .idVendor = USB_VENDOR_GENESYS_LOGIC, 5710 .bInterfaceClass = USB_CLASS_HUB, 5711 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, 5712 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 5713 .bDeviceClass = USB_CLASS_HUB}, 5714 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 5715 .bInterfaceClass = USB_CLASS_HUB}, 5716 { } /* Terminating entry */ 5717 }; 5718 5719 MODULE_DEVICE_TABLE(usb, hub_id_table); 5720 5721 static struct usb_driver hub_driver = { 5722 .name = "hub", 5723 .probe = hub_probe, 5724 .disconnect = hub_disconnect, 5725 .suspend = hub_suspend, 5726 .resume = hub_resume, 5727 .reset_resume = hub_reset_resume, 5728 .pre_reset = hub_pre_reset, 5729 .post_reset = hub_post_reset, 5730 .unlocked_ioctl = hub_ioctl, 5731 .id_table = hub_id_table, 5732 .supports_autosuspend = 1, 5733 }; 5734 5735 int usb_hub_init(void) 5736 { 5737 if (usb_register(&hub_driver) < 0) { 5738 printk(KERN_ERR "%s: can't register hub driver\n", 5739 usbcore_name); 5740 return -1; 5741 } 5742 5743 /* 5744 * The workqueue needs to be freezable to avoid interfering with 5745 * USB-PERSIST port handover. Otherwise it might see that a full-speed 5746 * device was gone before the EHCI controller had handed its port 5747 * over to the companion full-speed controller. 5748 */ 5749 hub_wq = alloc_workqueue("usb_hub_wq", WQ_FREEZABLE, 0); 5750 if (hub_wq) 5751 return 0; 5752 5753 /* Fall through if kernel_thread failed */ 5754 usb_deregister(&hub_driver); 5755 pr_err("%s: can't allocate workqueue for usb hub\n", usbcore_name); 5756 5757 return -1; 5758 } 5759 5760 void usb_hub_cleanup(void) 5761 { 5762 destroy_workqueue(hub_wq); 5763 5764 /* 5765 * Hub resources are freed for us by usb_deregister. It calls 5766 * usb_driver_purge on every device which in turn calls that 5767 * devices disconnect function if it is using this driver. 5768 * The hub_disconnect function takes care of releasing the 5769 * individual hub resources. -greg 5770 */ 5771 usb_deregister(&hub_driver); 5772 } /* usb_hub_cleanup() */ 5773 5774 /** 5775 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device 5776 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 5777 * 5778 * WARNING - don't use this routine to reset a composite device 5779 * (one with multiple interfaces owned by separate drivers)! 5780 * Use usb_reset_device() instead. 5781 * 5782 * Do a port reset, reassign the device's address, and establish its 5783 * former operating configuration. If the reset fails, or the device's 5784 * descriptors change from their values before the reset, or the original 5785 * configuration and altsettings cannot be restored, a flag will be set 5786 * telling hub_wq to pretend the device has been disconnected and then 5787 * re-connected. All drivers will be unbound, and the device will be 5788 * re-enumerated and probed all over again. 5789 * 5790 * Return: 0 if the reset succeeded, -ENODEV if the device has been 5791 * flagged for logical disconnection, or some other negative error code 5792 * if the reset wasn't even attempted. 5793 * 5794 * Note: 5795 * The caller must own the device lock and the port lock, the latter is 5796 * taken by usb_reset_device(). For example, it's safe to use 5797 * usb_reset_device() from a driver probe() routine after downloading 5798 * new firmware. For calls that might not occur during probe(), drivers 5799 * should lock the device using usb_lock_device_for_reset(). 5800 * 5801 * Locking exception: This routine may also be called from within an 5802 * autoresume handler. Such usage won't conflict with other tasks 5803 * holding the device lock because these tasks should always call 5804 * usb_autopm_resume_device(), thereby preventing any unwanted 5805 * autoresume. The autoresume handler is expected to have already 5806 * acquired the port lock before calling this routine. 5807 */ 5808 static int usb_reset_and_verify_device(struct usb_device *udev) 5809 { 5810 struct usb_device *parent_hdev = udev->parent; 5811 struct usb_hub *parent_hub; 5812 struct usb_hcd *hcd = bus_to_hcd(udev->bus); 5813 struct usb_device_descriptor descriptor = udev->descriptor; 5814 struct usb_host_bos *bos; 5815 int i, j, ret = 0; 5816 int port1 = udev->portnum; 5817 5818 if (udev->state == USB_STATE_NOTATTACHED || 5819 udev->state == USB_STATE_SUSPENDED) { 5820 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 5821 udev->state); 5822 return -EINVAL; 5823 } 5824 5825 if (!parent_hdev) 5826 return -EISDIR; 5827 5828 parent_hub = usb_hub_to_struct_hub(parent_hdev); 5829 5830 /* Disable USB2 hardware LPM. 5831 * It will be re-enabled by the enumeration process. 5832 */ 5833 usb_disable_usb2_hardware_lpm(udev); 5834 5835 /* Disable LPM while we reset the device and reinstall the alt settings. 5836 * Device-initiated LPM, and system exit latency settings are cleared 5837 * when the device is reset, so we have to set them up again. 5838 */ 5839 ret = usb_unlocked_disable_lpm(udev); 5840 if (ret) { 5841 dev_err(&udev->dev, "%s Failed to disable LPM\n", __func__); 5842 goto re_enumerate_no_bos; 5843 } 5844 5845 bos = udev->bos; 5846 udev->bos = NULL; 5847 5848 for (i = 0; i < PORT_INIT_TRIES; ++i) { 5849 5850 /* ep0 maxpacket size may change; let the HCD know about it. 5851 * Other endpoints will be handled by re-enumeration. */ 5852 usb_ep0_reinit(udev); 5853 ret = hub_port_init(parent_hub, udev, port1, i); 5854 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) 5855 break; 5856 } 5857 5858 if (ret < 0) 5859 goto re_enumerate; 5860 5861 /* Device might have changed firmware (DFU or similar) */ 5862 if (descriptors_changed(udev, &descriptor, bos)) { 5863 dev_info(&udev->dev, "device firmware changed\n"); 5864 udev->descriptor = descriptor; /* for disconnect() calls */ 5865 goto re_enumerate; 5866 } 5867 5868 /* Restore the device's previous configuration */ 5869 if (!udev->actconfig) 5870 goto done; 5871 5872 mutex_lock(hcd->bandwidth_mutex); 5873 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); 5874 if (ret < 0) { 5875 dev_warn(&udev->dev, 5876 "Busted HC? Not enough HCD resources for " 5877 "old configuration.\n"); 5878 mutex_unlock(hcd->bandwidth_mutex); 5879 goto re_enumerate; 5880 } 5881 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 5882 USB_REQ_SET_CONFIGURATION, 0, 5883 udev->actconfig->desc.bConfigurationValue, 0, 5884 NULL, 0, USB_CTRL_SET_TIMEOUT); 5885 if (ret < 0) { 5886 dev_err(&udev->dev, 5887 "can't restore configuration #%d (error=%d)\n", 5888 udev->actconfig->desc.bConfigurationValue, ret); 5889 mutex_unlock(hcd->bandwidth_mutex); 5890 goto re_enumerate; 5891 } 5892 mutex_unlock(hcd->bandwidth_mutex); 5893 usb_set_device_state(udev, USB_STATE_CONFIGURED); 5894 5895 /* Put interfaces back into the same altsettings as before. 5896 * Don't bother to send the Set-Interface request for interfaces 5897 * that were already in altsetting 0; besides being unnecessary, 5898 * many devices can't handle it. Instead just reset the host-side 5899 * endpoint state. 5900 */ 5901 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 5902 struct usb_host_config *config = udev->actconfig; 5903 struct usb_interface *intf = config->interface[i]; 5904 struct usb_interface_descriptor *desc; 5905 5906 desc = &intf->cur_altsetting->desc; 5907 if (desc->bAlternateSetting == 0) { 5908 usb_disable_interface(udev, intf, true); 5909 usb_enable_interface(udev, intf, true); 5910 ret = 0; 5911 } else { 5912 /* Let the bandwidth allocation function know that this 5913 * device has been reset, and it will have to use 5914 * alternate setting 0 as the current alternate setting. 5915 */ 5916 intf->resetting_device = 1; 5917 ret = usb_set_interface(udev, desc->bInterfaceNumber, 5918 desc->bAlternateSetting); 5919 intf->resetting_device = 0; 5920 } 5921 if (ret < 0) { 5922 dev_err(&udev->dev, "failed to restore interface %d " 5923 "altsetting %d (error=%d)\n", 5924 desc->bInterfaceNumber, 5925 desc->bAlternateSetting, 5926 ret); 5927 goto re_enumerate; 5928 } 5929 /* Resetting also frees any allocated streams */ 5930 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++) 5931 intf->cur_altsetting->endpoint[j].streams = 0; 5932 } 5933 5934 done: 5935 /* Now that the alt settings are re-installed, enable LTM and LPM. */ 5936 usb_enable_usb2_hardware_lpm(udev); 5937 usb_unlocked_enable_lpm(udev); 5938 usb_enable_ltm(udev); 5939 usb_release_bos_descriptor(udev); 5940 udev->bos = bos; 5941 return 0; 5942 5943 re_enumerate: 5944 usb_release_bos_descriptor(udev); 5945 udev->bos = bos; 5946 re_enumerate_no_bos: 5947 /* LPM state doesn't matter when we're about to destroy the device. */ 5948 hub_port_logical_disconnect(parent_hub, port1); 5949 return -ENODEV; 5950 } 5951 5952 /** 5953 * usb_reset_device - warn interface drivers and perform a USB port reset 5954 * @udev: device to reset (not in NOTATTACHED state) 5955 * 5956 * Warns all drivers bound to registered interfaces (using their pre_reset 5957 * method), performs the port reset, and then lets the drivers know that 5958 * the reset is over (using their post_reset method). 5959 * 5960 * Return: The same as for usb_reset_and_verify_device(). 5961 * 5962 * Note: 5963 * The caller must own the device lock. For example, it's safe to use 5964 * this from a driver probe() routine after downloading new firmware. 5965 * For calls that might not occur during probe(), drivers should lock 5966 * the device using usb_lock_device_for_reset(). 5967 * 5968 * If an interface is currently being probed or disconnected, we assume 5969 * its driver knows how to handle resets. For all other interfaces, 5970 * if the driver doesn't have pre_reset and post_reset methods then 5971 * we attempt to unbind it and rebind afterward. 5972 */ 5973 int usb_reset_device(struct usb_device *udev) 5974 { 5975 int ret; 5976 int i; 5977 unsigned int noio_flag; 5978 struct usb_port *port_dev; 5979 struct usb_host_config *config = udev->actconfig; 5980 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); 5981 5982 if (udev->state == USB_STATE_NOTATTACHED) { 5983 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 5984 udev->state); 5985 return -EINVAL; 5986 } 5987 5988 if (!udev->parent) { 5989 /* this requires hcd-specific logic; see ohci_restart() */ 5990 dev_dbg(&udev->dev, "%s for root hub!\n", __func__); 5991 return -EISDIR; 5992 } 5993 5994 port_dev = hub->ports[udev->portnum - 1]; 5995 5996 /* 5997 * Don't allocate memory with GFP_KERNEL in current 5998 * context to avoid possible deadlock if usb mass 5999 * storage interface or usbnet interface(iSCSI case) 6000 * is included in current configuration. The easist 6001 * approach is to do it for every device reset, 6002 * because the device 'memalloc_noio' flag may have 6003 * not been set before reseting the usb device. 6004 */ 6005 noio_flag = memalloc_noio_save(); 6006 6007 /* Prevent autosuspend during the reset */ 6008 usb_autoresume_device(udev); 6009 6010 if (config) { 6011 for (i = 0; i < config->desc.bNumInterfaces; ++i) { 6012 struct usb_interface *cintf = config->interface[i]; 6013 struct usb_driver *drv; 6014 int unbind = 0; 6015 6016 if (cintf->dev.driver) { 6017 drv = to_usb_driver(cintf->dev.driver); 6018 if (drv->pre_reset && drv->post_reset) 6019 unbind = (drv->pre_reset)(cintf); 6020 else if (cintf->condition == 6021 USB_INTERFACE_BOUND) 6022 unbind = 1; 6023 if (unbind) 6024 usb_forced_unbind_intf(cintf); 6025 } 6026 } 6027 } 6028 6029 usb_lock_port(port_dev); 6030 ret = usb_reset_and_verify_device(udev); 6031 usb_unlock_port(port_dev); 6032 6033 if (config) { 6034 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { 6035 struct usb_interface *cintf = config->interface[i]; 6036 struct usb_driver *drv; 6037 int rebind = cintf->needs_binding; 6038 6039 if (!rebind && cintf->dev.driver) { 6040 drv = to_usb_driver(cintf->dev.driver); 6041 if (drv->post_reset) 6042 rebind = (drv->post_reset)(cintf); 6043 else if (cintf->condition == 6044 USB_INTERFACE_BOUND) 6045 rebind = 1; 6046 if (rebind) 6047 cintf->needs_binding = 1; 6048 } 6049 } 6050 6051 /* If the reset failed, hub_wq will unbind drivers later */ 6052 if (ret == 0) 6053 usb_unbind_and_rebind_marked_interfaces(udev); 6054 } 6055 6056 usb_autosuspend_device(udev); 6057 memalloc_noio_restore(noio_flag); 6058 return ret; 6059 } 6060 EXPORT_SYMBOL_GPL(usb_reset_device); 6061 6062 6063 /** 6064 * usb_queue_reset_device - Reset a USB device from an atomic context 6065 * @iface: USB interface belonging to the device to reset 6066 * 6067 * This function can be used to reset a USB device from an atomic 6068 * context, where usb_reset_device() won't work (as it blocks). 6069 * 6070 * Doing a reset via this method is functionally equivalent to calling 6071 * usb_reset_device(), except for the fact that it is delayed to a 6072 * workqueue. This means that any drivers bound to other interfaces 6073 * might be unbound, as well as users from usbfs in user space. 6074 * 6075 * Corner cases: 6076 * 6077 * - Scheduling two resets at the same time from two different drivers 6078 * attached to two different interfaces of the same device is 6079 * possible; depending on how the driver attached to each interface 6080 * handles ->pre_reset(), the second reset might happen or not. 6081 * 6082 * - If the reset is delayed so long that the interface is unbound from 6083 * its driver, the reset will be skipped. 6084 * 6085 * - This function can be called during .probe(). It can also be called 6086 * during .disconnect(), but doing so is pointless because the reset 6087 * will not occur. If you really want to reset the device during 6088 * .disconnect(), call usb_reset_device() directly -- but watch out 6089 * for nested unbinding issues! 6090 */ 6091 void usb_queue_reset_device(struct usb_interface *iface) 6092 { 6093 if (schedule_work(&iface->reset_ws)) 6094 usb_get_intf(iface); 6095 } 6096 EXPORT_SYMBOL_GPL(usb_queue_reset_device); 6097 6098 /** 6099 * usb_hub_find_child - Get the pointer of child device 6100 * attached to the port which is specified by @port1. 6101 * @hdev: USB device belonging to the usb hub 6102 * @port1: port num to indicate which port the child device 6103 * is attached to. 6104 * 6105 * USB drivers call this function to get hub's child device 6106 * pointer. 6107 * 6108 * Return: %NULL if input param is invalid and 6109 * child's usb_device pointer if non-NULL. 6110 */ 6111 struct usb_device *usb_hub_find_child(struct usb_device *hdev, 6112 int port1) 6113 { 6114 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6115 6116 if (port1 < 1 || port1 > hdev->maxchild) 6117 return NULL; 6118 return hub->ports[port1 - 1]->child; 6119 } 6120 EXPORT_SYMBOL_GPL(usb_hub_find_child); 6121 6122 void usb_hub_adjust_deviceremovable(struct usb_device *hdev, 6123 struct usb_hub_descriptor *desc) 6124 { 6125 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6126 enum usb_port_connect_type connect_type; 6127 int i; 6128 6129 if (!hub) 6130 return; 6131 6132 if (!hub_is_superspeed(hdev)) { 6133 for (i = 1; i <= hdev->maxchild; i++) { 6134 struct usb_port *port_dev = hub->ports[i - 1]; 6135 6136 connect_type = port_dev->connect_type; 6137 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 6138 u8 mask = 1 << (i%8); 6139 6140 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { 6141 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 6142 desc->u.hs.DeviceRemovable[i/8] |= mask; 6143 } 6144 } 6145 } 6146 } else { 6147 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); 6148 6149 for (i = 1; i <= hdev->maxchild; i++) { 6150 struct usb_port *port_dev = hub->ports[i - 1]; 6151 6152 connect_type = port_dev->connect_type; 6153 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { 6154 u16 mask = 1 << i; 6155 6156 if (!(port_removable & mask)) { 6157 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n"); 6158 port_removable |= mask; 6159 } 6160 } 6161 } 6162 6163 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); 6164 } 6165 } 6166 6167 #ifdef CONFIG_ACPI 6168 /** 6169 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle 6170 * @hdev: USB device belonging to the usb hub 6171 * @port1: port num of the port 6172 * 6173 * Return: Port's acpi handle if successful, %NULL if params are 6174 * invalid. 6175 */ 6176 acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, 6177 int port1) 6178 { 6179 struct usb_hub *hub = usb_hub_to_struct_hub(hdev); 6180 6181 if (!hub) 6182 return NULL; 6183 6184 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev); 6185 } 6186 #endif 6187