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