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