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