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