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/config.h> 12 #ifdef CONFIG_USB_DEBUG 13 #define DEBUG 14 #else 15 #undef DEBUG 16 #endif 17 #include <linux/kernel.h> 18 #include <linux/errno.h> 19 #include <linux/module.h> 20 #include <linux/moduleparam.h> 21 #include <linux/completion.h> 22 #include <linux/sched.h> 23 #include <linux/list.h> 24 #include <linux/slab.h> 25 #include <linux/smp_lock.h> 26 #include <linux/ioctl.h> 27 #include <linux/usb.h> 28 #include <linux/usbdevice_fs.h> 29 30 #include <asm/semaphore.h> 31 #include <asm/uaccess.h> 32 #include <asm/byteorder.h> 33 34 #include "usb.h" 35 #include "hcd.h" 36 #include "hub.h" 37 38 /* Protect struct usb_device->state and ->children members 39 * Note: Both are also protected by ->serialize, except that ->state can 40 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ 41 static DEFINE_SPINLOCK(device_state_lock); 42 43 /* khubd's worklist and its lock */ 44 static DEFINE_SPINLOCK(hub_event_lock); 45 static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ 46 47 /* Wakes up khubd */ 48 static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); 49 50 static pid_t khubd_pid = 0; /* PID of khubd */ 51 static DECLARE_COMPLETION(khubd_exited); 52 53 /* cycle leds on hubs that aren't blinking for attention */ 54 static int blinkenlights = 0; 55 module_param (blinkenlights, bool, S_IRUGO); 56 MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); 57 58 /* 59 * As of 2.6.10 we introduce a new USB device initialization scheme which 60 * closely resembles the way Windows works. Hopefully it will be compatible 61 * with a wider range of devices than the old scheme. However some previously 62 * working devices may start giving rise to "device not accepting address" 63 * errors; if that happens the user can try the old scheme by adjusting the 64 * following module parameters. 65 * 66 * For maximum flexibility there are two boolean parameters to control the 67 * hub driver's behavior. On the first initialization attempt, if the 68 * "old_scheme_first" parameter is set then the old scheme will be used, 69 * otherwise the new scheme is used. If that fails and "use_both_schemes" 70 * is set, then the driver will make another attempt, using the other scheme. 71 */ 72 static int old_scheme_first = 0; 73 module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); 74 MODULE_PARM_DESC(old_scheme_first, 75 "start with the old device initialization scheme"); 76 77 static int use_both_schemes = 1; 78 module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); 79 MODULE_PARM_DESC(use_both_schemes, 80 "try the other device initialization scheme if the " 81 "first one fails"); 82 83 84 #ifdef DEBUG 85 static inline char *portspeed (int portstatus) 86 { 87 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED)) 88 return "480 Mb/s"; 89 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED)) 90 return "1.5 Mb/s"; 91 else 92 return "12 Mb/s"; 93 } 94 #endif 95 96 /* Note that hdev or one of its children must be locked! */ 97 static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev) 98 { 99 return usb_get_intfdata(hdev->actconfig->interface[0]); 100 } 101 102 /* USB 2.0 spec Section 11.24.4.5 */ 103 static int get_hub_descriptor(struct usb_device *hdev, void *data, int size) 104 { 105 int i, ret; 106 107 for (i = 0; i < 3; i++) { 108 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 109 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, 110 USB_DT_HUB << 8, 0, data, size, 111 USB_CTRL_GET_TIMEOUT); 112 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) 113 return ret; 114 } 115 return -EINVAL; 116 } 117 118 /* 119 * USB 2.0 spec Section 11.24.2.1 120 */ 121 static int clear_hub_feature(struct usb_device *hdev, int feature) 122 { 123 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 124 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); 125 } 126 127 /* 128 * USB 2.0 spec Section 11.24.2.2 129 */ 130 static int clear_port_feature(struct usb_device *hdev, int port1, int feature) 131 { 132 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 133 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, 134 NULL, 0, 1000); 135 } 136 137 /* 138 * USB 2.0 spec Section 11.24.2.13 139 */ 140 static int set_port_feature(struct usb_device *hdev, int port1, int feature) 141 { 142 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), 143 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, 144 NULL, 0, 1000); 145 } 146 147 /* 148 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 149 * for info about using port indicators 150 */ 151 static void set_port_led( 152 struct usb_hub *hub, 153 int port1, 154 int selector 155 ) 156 { 157 int status = set_port_feature(hub->hdev, (selector << 8) | port1, 158 USB_PORT_FEAT_INDICATOR); 159 if (status < 0) 160 dev_dbg (hub->intfdev, 161 "port %d indicator %s status %d\n", 162 port1, 163 ({ char *s; switch (selector) { 164 case HUB_LED_AMBER: s = "amber"; break; 165 case HUB_LED_GREEN: s = "green"; break; 166 case HUB_LED_OFF: s = "off"; break; 167 case HUB_LED_AUTO: s = "auto"; break; 168 default: s = "??"; break; 169 }; s; }), 170 status); 171 } 172 173 #define LED_CYCLE_PERIOD ((2*HZ)/3) 174 175 static void led_work (void *__hub) 176 { 177 struct usb_hub *hub = __hub; 178 struct usb_device *hdev = hub->hdev; 179 unsigned i; 180 unsigned changed = 0; 181 int cursor = -1; 182 183 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) 184 return; 185 186 for (i = 0; i < hub->descriptor->bNbrPorts; i++) { 187 unsigned selector, mode; 188 189 /* 30%-50% duty cycle */ 190 191 switch (hub->indicator[i]) { 192 /* cycle marker */ 193 case INDICATOR_CYCLE: 194 cursor = i; 195 selector = HUB_LED_AUTO; 196 mode = INDICATOR_AUTO; 197 break; 198 /* blinking green = sw attention */ 199 case INDICATOR_GREEN_BLINK: 200 selector = HUB_LED_GREEN; 201 mode = INDICATOR_GREEN_BLINK_OFF; 202 break; 203 case INDICATOR_GREEN_BLINK_OFF: 204 selector = HUB_LED_OFF; 205 mode = INDICATOR_GREEN_BLINK; 206 break; 207 /* blinking amber = hw attention */ 208 case INDICATOR_AMBER_BLINK: 209 selector = HUB_LED_AMBER; 210 mode = INDICATOR_AMBER_BLINK_OFF; 211 break; 212 case INDICATOR_AMBER_BLINK_OFF: 213 selector = HUB_LED_OFF; 214 mode = INDICATOR_AMBER_BLINK; 215 break; 216 /* blink green/amber = reserved */ 217 case INDICATOR_ALT_BLINK: 218 selector = HUB_LED_GREEN; 219 mode = INDICATOR_ALT_BLINK_OFF; 220 break; 221 case INDICATOR_ALT_BLINK_OFF: 222 selector = HUB_LED_AMBER; 223 mode = INDICATOR_ALT_BLINK; 224 break; 225 default: 226 continue; 227 } 228 if (selector != HUB_LED_AUTO) 229 changed = 1; 230 set_port_led(hub, i + 1, selector); 231 hub->indicator[i] = mode; 232 } 233 if (!changed && blinkenlights) { 234 cursor++; 235 cursor %= hub->descriptor->bNbrPorts; 236 set_port_led(hub, cursor + 1, HUB_LED_GREEN); 237 hub->indicator[cursor] = INDICATOR_CYCLE; 238 changed++; 239 } 240 if (changed) 241 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 242 } 243 244 /* use a short timeout for hub/port status fetches */ 245 #define USB_STS_TIMEOUT 1000 246 #define USB_STS_RETRIES 5 247 248 /* 249 * USB 2.0 spec Section 11.24.2.6 250 */ 251 static int get_hub_status(struct usb_device *hdev, 252 struct usb_hub_status *data) 253 { 254 int i, status = -ETIMEDOUT; 255 256 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) { 257 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 258 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, 259 data, sizeof(*data), USB_STS_TIMEOUT); 260 } 261 return status; 262 } 263 264 /* 265 * USB 2.0 spec Section 11.24.2.7 266 */ 267 static int get_port_status(struct usb_device *hdev, int port1, 268 struct usb_port_status *data) 269 { 270 int i, status = -ETIMEDOUT; 271 272 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) { 273 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 274 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, 275 data, sizeof(*data), USB_STS_TIMEOUT); 276 } 277 return status; 278 } 279 280 static void kick_khubd(struct usb_hub *hub) 281 { 282 unsigned long flags; 283 284 spin_lock_irqsave(&hub_event_lock, flags); 285 if (list_empty(&hub->event_list)) { 286 list_add_tail(&hub->event_list, &hub_event_list); 287 wake_up(&khubd_wait); 288 } 289 spin_unlock_irqrestore(&hub_event_lock, flags); 290 } 291 292 void usb_kick_khubd(struct usb_device *hdev) 293 { 294 kick_khubd(hdev_to_hub(hdev)); 295 } 296 297 298 /* completion function, fires on port status changes and various faults */ 299 static void hub_irq(struct urb *urb, struct pt_regs *regs) 300 { 301 struct usb_hub *hub = (struct usb_hub *)urb->context; 302 int status; 303 int i; 304 unsigned long bits; 305 306 switch (urb->status) { 307 case -ENOENT: /* synchronous unlink */ 308 case -ECONNRESET: /* async unlink */ 309 case -ESHUTDOWN: /* hardware going away */ 310 return; 311 312 default: /* presumably an error */ 313 /* Cause a hub reset after 10 consecutive errors */ 314 dev_dbg (hub->intfdev, "transfer --> %d\n", urb->status); 315 if ((++hub->nerrors < 10) || hub->error) 316 goto resubmit; 317 hub->error = urb->status; 318 /* FALL THROUGH */ 319 320 /* let khubd handle things */ 321 case 0: /* we got data: port status changed */ 322 bits = 0; 323 for (i = 0; i < urb->actual_length; ++i) 324 bits |= ((unsigned long) ((*hub->buffer)[i])) 325 << (i*8); 326 hub->event_bits[0] = bits; 327 break; 328 } 329 330 hub->nerrors = 0; 331 332 /* Something happened, let khubd figure it out */ 333 kick_khubd(hub); 334 335 resubmit: 336 if (hub->quiescing) 337 return; 338 339 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 340 && status != -ENODEV && status != -EPERM) 341 dev_err (hub->intfdev, "resubmit --> %d\n", status); 342 } 343 344 /* USB 2.0 spec Section 11.24.2.3 */ 345 static inline int 346 hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) 347 { 348 return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), 349 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, 350 tt, NULL, 0, 1000); 351 } 352 353 /* 354 * enumeration blocks khubd for a long time. we use keventd instead, since 355 * long blocking there is the exception, not the rule. accordingly, HCDs 356 * talking to TTs must queue control transfers (not just bulk and iso), so 357 * both can talk to the same hub concurrently. 358 */ 359 static void hub_tt_kevent (void *arg) 360 { 361 struct usb_hub *hub = arg; 362 unsigned long flags; 363 364 spin_lock_irqsave (&hub->tt.lock, flags); 365 while (!list_empty (&hub->tt.clear_list)) { 366 struct list_head *temp; 367 struct usb_tt_clear *clear; 368 struct usb_device *hdev = hub->hdev; 369 int status; 370 371 temp = hub->tt.clear_list.next; 372 clear = list_entry (temp, struct usb_tt_clear, clear_list); 373 list_del (&clear->clear_list); 374 375 /* drop lock so HCD can concurrently report other TT errors */ 376 spin_unlock_irqrestore (&hub->tt.lock, flags); 377 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); 378 spin_lock_irqsave (&hub->tt.lock, flags); 379 380 if (status) 381 dev_err (&hdev->dev, 382 "clear tt %d (%04x) error %d\n", 383 clear->tt, clear->devinfo, status); 384 kfree(clear); 385 } 386 spin_unlock_irqrestore (&hub->tt.lock, flags); 387 } 388 389 /** 390 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub 391 * @udev: the device whose split transaction failed 392 * @pipe: identifies the endpoint of the failed transaction 393 * 394 * High speed HCDs use this to tell the hub driver that some split control or 395 * bulk transaction failed in a way that requires clearing internal state of 396 * a transaction translator. This is normally detected (and reported) from 397 * interrupt context. 398 * 399 * It may not be possible for that hub to handle additional full (or low) 400 * speed transactions until that state is fully cleared out. 401 */ 402 void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe) 403 { 404 struct usb_tt *tt = udev->tt; 405 unsigned long flags; 406 struct usb_tt_clear *clear; 407 408 /* we've got to cope with an arbitrary number of pending TT clears, 409 * since each TT has "at least two" buffers that can need it (and 410 * there can be many TTs per hub). even if they're uncommon. 411 */ 412 if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == NULL) { 413 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); 414 /* FIXME recover somehow ... RESET_TT? */ 415 return; 416 } 417 418 /* info that CLEAR_TT_BUFFER needs */ 419 clear->tt = tt->multi ? udev->ttport : 1; 420 clear->devinfo = usb_pipeendpoint (pipe); 421 clear->devinfo |= udev->devnum << 4; 422 clear->devinfo |= usb_pipecontrol (pipe) 423 ? (USB_ENDPOINT_XFER_CONTROL << 11) 424 : (USB_ENDPOINT_XFER_BULK << 11); 425 if (usb_pipein (pipe)) 426 clear->devinfo |= 1 << 15; 427 428 /* tell keventd to clear state for this TT */ 429 spin_lock_irqsave (&tt->lock, flags); 430 list_add_tail (&clear->clear_list, &tt->clear_list); 431 schedule_work (&tt->kevent); 432 spin_unlock_irqrestore (&tt->lock, flags); 433 } 434 435 static void hub_power_on(struct usb_hub *hub) 436 { 437 int port1; 438 439 /* if hub supports power switching, enable power on each port */ 440 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) < 2) { 441 dev_dbg(hub->intfdev, "enabling power on all ports\n"); 442 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) 443 set_port_feature(hub->hdev, port1, 444 USB_PORT_FEAT_POWER); 445 } 446 447 /* Wait for power to be enabled */ 448 msleep(hub->descriptor->bPwrOn2PwrGood * 2); 449 } 450 451 static void hub_quiesce(struct usb_hub *hub) 452 { 453 /* stop khubd and related activity */ 454 hub->quiescing = 1; 455 usb_kill_urb(hub->urb); 456 if (hub->has_indicators) 457 cancel_delayed_work(&hub->leds); 458 if (hub->has_indicators || hub->tt.hub) 459 flush_scheduled_work(); 460 } 461 462 static void hub_activate(struct usb_hub *hub) 463 { 464 int status; 465 466 hub->quiescing = 0; 467 hub->activating = 1; 468 status = usb_submit_urb(hub->urb, GFP_NOIO); 469 if (status < 0) 470 dev_err(hub->intfdev, "activate --> %d\n", status); 471 if (hub->has_indicators && blinkenlights) 472 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); 473 474 /* scan all ports ASAP */ 475 kick_khubd(hub); 476 } 477 478 static int hub_hub_status(struct usb_hub *hub, 479 u16 *status, u16 *change) 480 { 481 int ret; 482 483 ret = get_hub_status(hub->hdev, &hub->status->hub); 484 if (ret < 0) 485 dev_err (hub->intfdev, 486 "%s failed (err = %d)\n", __FUNCTION__, ret); 487 else { 488 *status = le16_to_cpu(hub->status->hub.wHubStatus); 489 *change = le16_to_cpu(hub->status->hub.wHubChange); 490 ret = 0; 491 } 492 return ret; 493 } 494 495 static int hub_configure(struct usb_hub *hub, 496 struct usb_endpoint_descriptor *endpoint) 497 { 498 struct usb_device *hdev = hub->hdev; 499 struct device *hub_dev = hub->intfdev; 500 u16 hubstatus, hubchange; 501 unsigned int pipe; 502 int maxp, ret; 503 char *message; 504 505 hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL, 506 &hub->buffer_dma); 507 if (!hub->buffer) { 508 message = "can't allocate hub irq buffer"; 509 ret = -ENOMEM; 510 goto fail; 511 } 512 513 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); 514 if (!hub->status) { 515 message = "can't kmalloc hub status buffer"; 516 ret = -ENOMEM; 517 goto fail; 518 } 519 520 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); 521 if (!hub->descriptor) { 522 message = "can't kmalloc hub descriptor"; 523 ret = -ENOMEM; 524 goto fail; 525 } 526 527 /* Request the entire hub descriptor. 528 * hub->descriptor can handle USB_MAXCHILDREN ports, 529 * but the hub can/will return fewer bytes here. 530 */ 531 ret = get_hub_descriptor(hdev, hub->descriptor, 532 sizeof(*hub->descriptor)); 533 if (ret < 0) { 534 message = "can't read hub descriptor"; 535 goto fail; 536 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { 537 message = "hub has too many ports!"; 538 ret = -ENODEV; 539 goto fail; 540 } 541 542 hdev->maxchild = hub->descriptor->bNbrPorts; 543 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, 544 (hdev->maxchild == 1) ? "" : "s"); 545 546 le16_to_cpus(&hub->descriptor->wHubCharacteristics); 547 548 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) { 549 int i; 550 char portstr [USB_MAXCHILDREN + 1]; 551 552 for (i = 0; i < hdev->maxchild; i++) 553 portstr[i] = hub->descriptor->DeviceRemovable 554 [((i + 1) / 8)] & (1 << ((i + 1) % 8)) 555 ? 'F' : 'R'; 556 portstr[hdev->maxchild] = 0; 557 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); 558 } else 559 dev_dbg(hub_dev, "standalone hub\n"); 560 561 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) { 562 case 0x00: 563 dev_dbg(hub_dev, "ganged power switching\n"); 564 break; 565 case 0x01: 566 dev_dbg(hub_dev, "individual port power switching\n"); 567 break; 568 case 0x02: 569 case 0x03: 570 dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); 571 break; 572 } 573 574 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) { 575 case 0x00: 576 dev_dbg(hub_dev, "global over-current protection\n"); 577 break; 578 case 0x08: 579 dev_dbg(hub_dev, "individual port over-current protection\n"); 580 break; 581 case 0x10: 582 case 0x18: 583 dev_dbg(hub_dev, "no over-current protection\n"); 584 break; 585 } 586 587 spin_lock_init (&hub->tt.lock); 588 INIT_LIST_HEAD (&hub->tt.clear_list); 589 INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub); 590 switch (hdev->descriptor.bDeviceProtocol) { 591 case 0: 592 break; 593 case 1: 594 dev_dbg(hub_dev, "Single TT\n"); 595 hub->tt.hub = hdev; 596 break; 597 case 2: 598 ret = usb_set_interface(hdev, 0, 1); 599 if (ret == 0) { 600 dev_dbg(hub_dev, "TT per port\n"); 601 hub->tt.multi = 1; 602 } else 603 dev_err(hub_dev, "Using single TT (err %d)\n", 604 ret); 605 hub->tt.hub = hdev; 606 break; 607 default: 608 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", 609 hdev->descriptor.bDeviceProtocol); 610 break; 611 } 612 613 switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) { 614 case 0x00: 615 if (hdev->descriptor.bDeviceProtocol != 0) 616 dev_dbg(hub_dev, "TT requires at most 8 FS bit times\n"); 617 break; 618 case 0x20: 619 dev_dbg(hub_dev, "TT requires at most 16 FS bit times\n"); 620 break; 621 case 0x40: 622 dev_dbg(hub_dev, "TT requires at most 24 FS bit times\n"); 623 break; 624 case 0x60: 625 dev_dbg(hub_dev, "TT requires at most 32 FS bit times\n"); 626 break; 627 } 628 629 /* probe() zeroes hub->indicator[] */ 630 if (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND) { 631 hub->has_indicators = 1; 632 dev_dbg(hub_dev, "Port indicators are supported\n"); 633 } 634 635 dev_dbg(hub_dev, "power on to power good time: %dms\n", 636 hub->descriptor->bPwrOn2PwrGood * 2); 637 638 /* power budgeting mostly matters with bus-powered hubs, 639 * and battery-powered root hubs (may provide just 8 mA). 640 */ 641 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); 642 if (ret < 0) { 643 message = "can't get hub status"; 644 goto fail; 645 } 646 cpu_to_le16s(&hubstatus); 647 if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 648 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", 649 hub->descriptor->bHubContrCurrent); 650 hub->power_budget = (501 - hub->descriptor->bHubContrCurrent) 651 / 2; 652 dev_dbg(hub_dev, "%dmA bus power budget for children\n", 653 hub->power_budget * 2); 654 } 655 656 657 ret = hub_hub_status(hub, &hubstatus, &hubchange); 658 if (ret < 0) { 659 message = "can't get hub status"; 660 goto fail; 661 } 662 663 /* local power status reports aren't always correct */ 664 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) 665 dev_dbg(hub_dev, "local power source is %s\n", 666 (hubstatus & HUB_STATUS_LOCAL_POWER) 667 ? "lost (inactive)" : "good"); 668 669 if ((hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) == 0) 670 dev_dbg(hub_dev, "%sover-current condition exists\n", 671 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); 672 673 /* set up the interrupt endpoint */ 674 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); 675 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); 676 677 if (maxp > sizeof(*hub->buffer)) 678 maxp = sizeof(*hub->buffer); 679 680 hub->urb = usb_alloc_urb(0, GFP_KERNEL); 681 if (!hub->urb) { 682 message = "couldn't allocate interrupt urb"; 683 ret = -ENOMEM; 684 goto fail; 685 } 686 687 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, 688 hub, endpoint->bInterval); 689 hub->urb->transfer_dma = hub->buffer_dma; 690 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 691 692 /* maybe cycle the hub leds */ 693 if (hub->has_indicators && blinkenlights) 694 hub->indicator [0] = INDICATOR_CYCLE; 695 696 hub_power_on(hub); 697 hub_activate(hub); 698 return 0; 699 700 fail: 701 dev_err (hub_dev, "config failed, %s (err %d)\n", 702 message, ret); 703 /* hub_disconnect() frees urb and descriptor */ 704 return ret; 705 } 706 707 static unsigned highspeed_hubs; 708 709 static void hub_disconnect(struct usb_interface *intf) 710 { 711 struct usb_hub *hub = usb_get_intfdata (intf); 712 struct usb_device *hdev; 713 714 if (!hub) 715 return; 716 hdev = hub->hdev; 717 718 if (hdev->speed == USB_SPEED_HIGH) 719 highspeed_hubs--; 720 721 usb_set_intfdata (intf, NULL); 722 723 hub_quiesce(hub); 724 usb_free_urb(hub->urb); 725 hub->urb = NULL; 726 727 spin_lock_irq(&hub_event_lock); 728 list_del_init(&hub->event_list); 729 spin_unlock_irq(&hub_event_lock); 730 731 kfree(hub->descriptor); 732 hub->descriptor = NULL; 733 734 kfree(hub->status); 735 hub->status = NULL; 736 737 if (hub->buffer) { 738 usb_buffer_free(hdev, sizeof(*hub->buffer), hub->buffer, 739 hub->buffer_dma); 740 hub->buffer = NULL; 741 } 742 743 /* Free the memory */ 744 kfree(hub); 745 } 746 747 static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) 748 { 749 struct usb_host_interface *desc; 750 struct usb_endpoint_descriptor *endpoint; 751 struct usb_device *hdev; 752 struct usb_hub *hub; 753 754 desc = intf->cur_altsetting; 755 hdev = interface_to_usbdev(intf); 756 757 /* Some hubs have a subclass of 1, which AFAICT according to the */ 758 /* specs is not defined, but it works */ 759 if ((desc->desc.bInterfaceSubClass != 0) && 760 (desc->desc.bInterfaceSubClass != 1)) { 761 descriptor_error: 762 dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); 763 return -EIO; 764 } 765 766 /* Multiple endpoints? What kind of mutant ninja-hub is this? */ 767 if (desc->desc.bNumEndpoints != 1) 768 goto descriptor_error; 769 770 endpoint = &desc->endpoint[0].desc; 771 772 /* Output endpoint? Curiouser and curiouser.. */ 773 if (!(endpoint->bEndpointAddress & USB_DIR_IN)) 774 goto descriptor_error; 775 776 /* If it's not an interrupt endpoint, we'd better punt! */ 777 if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 778 != USB_ENDPOINT_XFER_INT) 779 goto descriptor_error; 780 781 /* We found a hub */ 782 dev_info (&intf->dev, "USB hub found\n"); 783 784 hub = kmalloc(sizeof(*hub), GFP_KERNEL); 785 if (!hub) { 786 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); 787 return -ENOMEM; 788 } 789 790 memset(hub, 0, sizeof(*hub)); 791 792 INIT_LIST_HEAD(&hub->event_list); 793 hub->intfdev = &intf->dev; 794 hub->hdev = hdev; 795 INIT_WORK(&hub->leds, led_work, hub); 796 797 usb_set_intfdata (intf, hub); 798 799 if (hdev->speed == USB_SPEED_HIGH) 800 highspeed_hubs++; 801 802 if (hub_configure(hub, endpoint) >= 0) 803 return 0; 804 805 hub_disconnect (intf); 806 return -ENODEV; 807 } 808 809 static int 810 hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) 811 { 812 struct usb_device *hdev = interface_to_usbdev (intf); 813 814 /* assert ifno == 0 (part of hub spec) */ 815 switch (code) { 816 case USBDEVFS_HUB_PORTINFO: { 817 struct usbdevfs_hub_portinfo *info = user_data; 818 int i; 819 820 spin_lock_irq(&device_state_lock); 821 if (hdev->devnum <= 0) 822 info->nports = 0; 823 else { 824 info->nports = hdev->maxchild; 825 for (i = 0; i < info->nports; i++) { 826 if (hdev->children[i] == NULL) 827 info->port[i] = 0; 828 else 829 info->port[i] = 830 hdev->children[i]->devnum; 831 } 832 } 833 spin_unlock_irq(&device_state_lock); 834 835 return info->nports + 1; 836 } 837 838 default: 839 return -ENOSYS; 840 } 841 } 842 843 /* caller has locked the hub device */ 844 static void hub_pre_reset(struct usb_hub *hub) 845 { 846 struct usb_device *hdev = hub->hdev; 847 int i; 848 849 for (i = 0; i < hdev->maxchild; ++i) { 850 if (hdev->children[i]) 851 usb_disconnect(&hdev->children[i]); 852 } 853 hub_quiesce(hub); 854 } 855 856 /* caller has locked the hub device */ 857 static void hub_post_reset(struct usb_hub *hub) 858 { 859 hub_activate(hub); 860 hub_power_on(hub); 861 } 862 863 864 /* grab device/port lock, returning index of that port (zero based). 865 * protects the upstream link used by this device from concurrent 866 * tree operations like suspend, resume, reset, and disconnect, which 867 * apply to everything downstream of a given port. 868 */ 869 static int locktree(struct usb_device *udev) 870 { 871 int t; 872 struct usb_device *hdev; 873 874 if (!udev) 875 return -ENODEV; 876 877 /* root hub is always the first lock in the series */ 878 hdev = udev->parent; 879 if (!hdev) { 880 usb_lock_device(udev); 881 return 0; 882 } 883 884 /* on the path from root to us, lock everything from 885 * top down, dropping parent locks when not needed 886 */ 887 t = locktree(hdev); 888 if (t < 0) 889 return t; 890 for (t = 0; t < hdev->maxchild; t++) { 891 if (hdev->children[t] == udev) { 892 /* everything is fail-fast once disconnect 893 * processing starts 894 */ 895 if (udev->state == USB_STATE_NOTATTACHED) 896 break; 897 898 /* when everyone grabs locks top->bottom, 899 * non-overlapping work may be concurrent 900 */ 901 down(&udev->serialize); 902 up(&hdev->serialize); 903 return t + 1; 904 } 905 } 906 usb_unlock_device(hdev); 907 return -ENODEV; 908 } 909 910 static void recursively_mark_NOTATTACHED(struct usb_device *udev) 911 { 912 int i; 913 914 for (i = 0; i < udev->maxchild; ++i) { 915 if (udev->children[i]) 916 recursively_mark_NOTATTACHED(udev->children[i]); 917 } 918 udev->state = USB_STATE_NOTATTACHED; 919 } 920 921 /** 922 * usb_set_device_state - change a device's current state (usbcore, hcds) 923 * @udev: pointer to device whose state should be changed 924 * @new_state: new state value to be stored 925 * 926 * udev->state is _not_ fully protected by the device lock. Although 927 * most transitions are made only while holding the lock, the state can 928 * can change to USB_STATE_NOTATTACHED at almost any time. This 929 * is so that devices can be marked as disconnected as soon as possible, 930 * without having to wait for any semaphores to be released. As a result, 931 * all changes to any device's state must be protected by the 932 * device_state_lock spinlock. 933 * 934 * Once a device has been added to the device tree, all changes to its state 935 * should be made using this routine. The state should _not_ be set directly. 936 * 937 * If udev->state is already USB_STATE_NOTATTACHED then no change is made. 938 * Otherwise udev->state is set to new_state, and if new_state is 939 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set 940 * to USB_STATE_NOTATTACHED. 941 */ 942 void usb_set_device_state(struct usb_device *udev, 943 enum usb_device_state new_state) 944 { 945 unsigned long flags; 946 947 spin_lock_irqsave(&device_state_lock, flags); 948 if (udev->state == USB_STATE_NOTATTACHED) 949 ; /* do nothing */ 950 else if (new_state != USB_STATE_NOTATTACHED) 951 udev->state = new_state; 952 else 953 recursively_mark_NOTATTACHED(udev); 954 spin_unlock_irqrestore(&device_state_lock, flags); 955 } 956 EXPORT_SYMBOL(usb_set_device_state); 957 958 959 static void choose_address(struct usb_device *udev) 960 { 961 int devnum; 962 struct usb_bus *bus = udev->bus; 963 964 /* If khubd ever becomes multithreaded, this will need a lock */ 965 966 /* Try to allocate the next devnum beginning at bus->devnum_next. */ 967 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 968 bus->devnum_next); 969 if (devnum >= 128) 970 devnum = find_next_zero_bit(bus->devmap.devicemap, 128, 1); 971 972 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); 973 974 if (devnum < 128) { 975 set_bit(devnum, bus->devmap.devicemap); 976 udev->devnum = devnum; 977 } 978 } 979 980 static void release_address(struct usb_device *udev) 981 { 982 if (udev->devnum > 0) { 983 clear_bit(udev->devnum, udev->bus->devmap.devicemap); 984 udev->devnum = -1; 985 } 986 } 987 988 /** 989 * usb_disconnect - disconnect a device (usbcore-internal) 990 * @pdev: pointer to device being disconnected 991 * Context: !in_interrupt () 992 * 993 * Something got disconnected. Get rid of it and all of its children. 994 * 995 * If *pdev is a normal device then the parent hub must already be locked. 996 * If *pdev is a root hub then this routine will acquire the 997 * usb_bus_list_lock on behalf of the caller. 998 * 999 * Only hub drivers (including virtual root hub drivers for host 1000 * controllers) should ever call this. 1001 * 1002 * This call is synchronous, and may not be used in an interrupt context. 1003 */ 1004 void usb_disconnect(struct usb_device **pdev) 1005 { 1006 struct usb_device *udev = *pdev; 1007 int i; 1008 1009 if (!udev) { 1010 pr_debug ("%s nodev\n", __FUNCTION__); 1011 return; 1012 } 1013 1014 /* mark the device as inactive, so any further urb submissions for 1015 * this device (and any of its children) will fail immediately. 1016 * this quiesces everyting except pending urbs. 1017 */ 1018 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1019 1020 /* lock the bus list on behalf of HCDs unregistering their root hubs */ 1021 if (!udev->parent) { 1022 down(&usb_bus_list_lock); 1023 usb_lock_device(udev); 1024 } else 1025 down(&udev->serialize); 1026 1027 dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum); 1028 1029 /* Free up all the children before we remove this device */ 1030 for (i = 0; i < USB_MAXCHILDREN; i++) { 1031 if (udev->children[i]) 1032 usb_disconnect(&udev->children[i]); 1033 } 1034 1035 /* deallocate hcd/hardware state ... nuking all pending urbs and 1036 * cleaning up all state associated with the current configuration 1037 * so that the hardware is now fully quiesced. 1038 */ 1039 usb_disable_device(udev, 0); 1040 1041 /* Free the device number, remove the /proc/bus/usb entry and 1042 * the sysfs attributes, and delete the parent's children[] 1043 * (or root_hub) pointer. 1044 */ 1045 dev_dbg (&udev->dev, "unregistering device\n"); 1046 release_address(udev); 1047 usbfs_remove_device(udev); 1048 usb_remove_sysfs_dev_files(udev); 1049 1050 /* Avoid races with recursively_mark_NOTATTACHED() */ 1051 spin_lock_irq(&device_state_lock); 1052 *pdev = NULL; 1053 spin_unlock_irq(&device_state_lock); 1054 1055 if (!udev->parent) { 1056 usb_unlock_device(udev); 1057 up(&usb_bus_list_lock); 1058 } else 1059 up(&udev->serialize); 1060 1061 device_unregister(&udev->dev); 1062 } 1063 1064 static int choose_configuration(struct usb_device *udev) 1065 { 1066 int c, i; 1067 1068 /* NOTE: this should interact with hub power budgeting */ 1069 1070 c = udev->config[0].desc.bConfigurationValue; 1071 if (udev->descriptor.bNumConfigurations != 1) { 1072 for (i = 0; i < udev->descriptor.bNumConfigurations; i++) { 1073 struct usb_interface_descriptor *desc; 1074 1075 /* heuristic: Linux is more likely to have class 1076 * drivers, so avoid vendor-specific interfaces. 1077 */ 1078 desc = &udev->config[i].intf_cache[0] 1079 ->altsetting->desc; 1080 if (desc->bInterfaceClass == USB_CLASS_VENDOR_SPEC) 1081 continue; 1082 /* COMM/2/all is CDC ACM, except 0xff is MSFT RNDIS. 1083 * MSFT needs this to be the first config; never use 1084 * it as the default unless Linux has host-side RNDIS. 1085 * A second config would ideally be CDC-Ethernet, but 1086 * may instead be the "vendor specific" CDC subset 1087 * long used by ARM Linux for sa1100 or pxa255. 1088 */ 1089 if (desc->bInterfaceClass == USB_CLASS_COMM 1090 && desc->bInterfaceSubClass == 2 1091 && desc->bInterfaceProtocol == 0xff) { 1092 c = udev->config[1].desc.bConfigurationValue; 1093 continue; 1094 } 1095 c = udev->config[i].desc.bConfigurationValue; 1096 break; 1097 } 1098 dev_info(&udev->dev, 1099 "configuration #%d chosen from %d choices\n", 1100 c, udev->descriptor.bNumConfigurations); 1101 } 1102 return c; 1103 } 1104 1105 #ifdef DEBUG 1106 static void show_string(struct usb_device *udev, char *id, char *string) 1107 { 1108 if (!string) 1109 return; 1110 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string); 1111 } 1112 1113 #else 1114 static inline void show_string(struct usb_device *udev, char *id, char *string) 1115 {} 1116 #endif 1117 1118 static void get_string(struct usb_device *udev, char **string, int index) 1119 { 1120 char *buf; 1121 1122 if (!index) 1123 return; 1124 buf = kmalloc(256, GFP_KERNEL); 1125 if (!buf) 1126 return; 1127 if (usb_string(udev, index, buf, 256) > 0) 1128 *string = buf; 1129 else 1130 kfree(buf); 1131 } 1132 1133 1134 #ifdef CONFIG_USB_OTG 1135 #include "otg_whitelist.h" 1136 #endif 1137 1138 /** 1139 * usb_new_device - perform initial device setup (usbcore-internal) 1140 * @udev: newly addressed device (in ADDRESS state) 1141 * 1142 * This is called with devices which have been enumerated, but not yet 1143 * configured. The device descriptor is available, but not descriptors 1144 * for any device configuration. The caller must have locked udev and 1145 * either the parent hub (if udev is a normal device) or else the 1146 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to 1147 * udev has already been installed, but udev is not yet visible through 1148 * sysfs or other filesystem code. 1149 * 1150 * Returns 0 for success (device is configured and listed, with its 1151 * interfaces, in sysfs); else a negative errno value. 1152 * 1153 * This call is synchronous, and may not be used in an interrupt context. 1154 * 1155 * Only the hub driver should ever call this; root hub registration 1156 * uses it indirectly. 1157 */ 1158 int usb_new_device(struct usb_device *udev) 1159 { 1160 int err; 1161 int c; 1162 1163 err = usb_get_configuration(udev); 1164 if (err < 0) { 1165 dev_err(&udev->dev, "can't read configurations, error %d\n", 1166 err); 1167 goto fail; 1168 } 1169 1170 /* read the standard strings and cache them if present */ 1171 get_string(udev, &udev->product, udev->descriptor.iProduct); 1172 get_string(udev, &udev->manufacturer, udev->descriptor.iManufacturer); 1173 get_string(udev, &udev->serial, udev->descriptor.iSerialNumber); 1174 1175 /* Tell the world! */ 1176 dev_dbg(&udev->dev, "new device strings: Mfr=%d, Product=%d, " 1177 "SerialNumber=%d\n", 1178 udev->descriptor.iManufacturer, 1179 udev->descriptor.iProduct, 1180 udev->descriptor.iSerialNumber); 1181 show_string(udev, "Product", udev->product); 1182 show_string(udev, "Manufacturer", udev->manufacturer); 1183 show_string(udev, "SerialNumber", udev->serial); 1184 1185 #ifdef CONFIG_USB_OTG 1186 /* 1187 * OTG-aware devices on OTG-capable root hubs may be able to use SRP, 1188 * to wake us after we've powered off VBUS; and HNP, switching roles 1189 * "host" to "peripheral". The OTG descriptor helps figure this out. 1190 */ 1191 if (!udev->bus->is_b_host 1192 && udev->config 1193 && udev->parent == udev->bus->root_hub) { 1194 struct usb_otg_descriptor *desc = 0; 1195 struct usb_bus *bus = udev->bus; 1196 1197 /* descriptor may appear anywhere in config */ 1198 if (__usb_get_extra_descriptor (udev->rawdescriptors[0], 1199 le16_to_cpu(udev->config[0].desc.wTotalLength), 1200 USB_DT_OTG, (void **) &desc) == 0) { 1201 if (desc->bmAttributes & USB_OTG_HNP) { 1202 unsigned port1; 1203 struct usb_device *root = udev->parent; 1204 1205 for (port1 = 1; port1 <= root->maxchild; 1206 port1++) { 1207 if (root->children[port1-1] == udev) 1208 break; 1209 } 1210 1211 dev_info(&udev->dev, 1212 "Dual-Role OTG device on %sHNP port\n", 1213 (port1 == bus->otg_port) 1214 ? "" : "non-"); 1215 1216 /* enable HNP before suspend, it's simpler */ 1217 if (port1 == bus->otg_port) 1218 bus->b_hnp_enable = 1; 1219 err = usb_control_msg(udev, 1220 usb_sndctrlpipe(udev, 0), 1221 USB_REQ_SET_FEATURE, 0, 1222 bus->b_hnp_enable 1223 ? USB_DEVICE_B_HNP_ENABLE 1224 : USB_DEVICE_A_ALT_HNP_SUPPORT, 1225 0, NULL, 0, USB_CTRL_SET_TIMEOUT); 1226 if (err < 0) { 1227 /* OTG MESSAGE: report errors here, 1228 * customize to match your product. 1229 */ 1230 dev_info(&udev->dev, 1231 "can't set HNP mode; %d\n", 1232 err); 1233 bus->b_hnp_enable = 0; 1234 } 1235 } 1236 } 1237 } 1238 1239 if (!is_targeted(udev)) { 1240 1241 /* Maybe it can talk to us, though we can't talk to it. 1242 * (Includes HNP test device.) 1243 */ 1244 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { 1245 static int __usb_suspend_device (struct usb_device *, 1246 int port1, pm_message_t state); 1247 err = __usb_suspend_device(udev, 1248 udev->bus->otg_port, 1249 PMSG_SUSPEND); 1250 if (err < 0) 1251 dev_dbg(&udev->dev, "HNP fail, %d\n", err); 1252 } 1253 err = -ENODEV; 1254 goto fail; 1255 } 1256 #endif 1257 1258 /* put device-specific files into sysfs */ 1259 err = device_add (&udev->dev); 1260 if (err) { 1261 dev_err(&udev->dev, "can't device_add, error %d\n", err); 1262 goto fail; 1263 } 1264 usb_create_sysfs_dev_files (udev); 1265 1266 /* choose and set the configuration. that registers the interfaces 1267 * with the driver core, and lets usb device drivers bind to them. 1268 */ 1269 c = choose_configuration(udev); 1270 if (c < 0) 1271 dev_warn(&udev->dev, 1272 "can't choose an initial configuration\n"); 1273 else { 1274 err = usb_set_configuration(udev, c); 1275 if (err) { 1276 dev_err(&udev->dev, "can't set config #%d, error %d\n", 1277 c, err); 1278 usb_remove_sysfs_dev_files(udev); 1279 device_del(&udev->dev); 1280 goto fail; 1281 } 1282 } 1283 1284 /* USB device state == configured ... usable */ 1285 1286 /* add a /proc/bus/usb entry */ 1287 usbfs_add_device(udev); 1288 return 0; 1289 1290 fail: 1291 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1292 return err; 1293 } 1294 1295 1296 static int hub_port_status(struct usb_hub *hub, int port1, 1297 u16 *status, u16 *change) 1298 { 1299 int ret; 1300 1301 ret = get_port_status(hub->hdev, port1, &hub->status->port); 1302 if (ret < 0) 1303 dev_err (hub->intfdev, 1304 "%s failed (err = %d)\n", __FUNCTION__, ret); 1305 else { 1306 *status = le16_to_cpu(hub->status->port.wPortStatus); 1307 *change = le16_to_cpu(hub->status->port.wPortChange); 1308 ret = 0; 1309 } 1310 return ret; 1311 } 1312 1313 #define PORT_RESET_TRIES 5 1314 #define SET_ADDRESS_TRIES 2 1315 #define GET_DESCRIPTOR_TRIES 2 1316 #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) 1317 #define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first) 1318 1319 #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ 1320 #define HUB_SHORT_RESET_TIME 10 1321 #define HUB_LONG_RESET_TIME 200 1322 #define HUB_RESET_TIMEOUT 500 1323 1324 static int hub_port_wait_reset(struct usb_hub *hub, int port1, 1325 struct usb_device *udev, unsigned int delay) 1326 { 1327 int delay_time, ret; 1328 u16 portstatus; 1329 u16 portchange; 1330 1331 for (delay_time = 0; 1332 delay_time < HUB_RESET_TIMEOUT; 1333 delay_time += delay) { 1334 /* wait to give the device a chance to reset */ 1335 msleep(delay); 1336 1337 /* read and decode port status */ 1338 ret = hub_port_status(hub, port1, &portstatus, &portchange); 1339 if (ret < 0) 1340 return ret; 1341 1342 /* Device went away? */ 1343 if (!(portstatus & USB_PORT_STAT_CONNECTION)) 1344 return -ENOTCONN; 1345 1346 /* bomb out completely if something weird happened */ 1347 if ((portchange & USB_PORT_STAT_C_CONNECTION)) 1348 return -EINVAL; 1349 1350 /* if we`ve finished resetting, then break out of the loop */ 1351 if (!(portstatus & USB_PORT_STAT_RESET) && 1352 (portstatus & USB_PORT_STAT_ENABLE)) { 1353 if (portstatus & USB_PORT_STAT_HIGH_SPEED) 1354 udev->speed = USB_SPEED_HIGH; 1355 else if (portstatus & USB_PORT_STAT_LOW_SPEED) 1356 udev->speed = USB_SPEED_LOW; 1357 else 1358 udev->speed = USB_SPEED_FULL; 1359 return 0; 1360 } 1361 1362 /* switch to the long delay after two short delay failures */ 1363 if (delay_time >= 2 * HUB_SHORT_RESET_TIME) 1364 delay = HUB_LONG_RESET_TIME; 1365 1366 dev_dbg (hub->intfdev, 1367 "port %d not reset yet, waiting %dms\n", 1368 port1, delay); 1369 } 1370 1371 return -EBUSY; 1372 } 1373 1374 static int hub_port_reset(struct usb_hub *hub, int port1, 1375 struct usb_device *udev, unsigned int delay) 1376 { 1377 int i, status; 1378 1379 /* Reset the port */ 1380 for (i = 0; i < PORT_RESET_TRIES; i++) { 1381 status = set_port_feature(hub->hdev, 1382 port1, USB_PORT_FEAT_RESET); 1383 if (status) 1384 dev_err(hub->intfdev, 1385 "cannot reset port %d (err = %d)\n", 1386 port1, status); 1387 else { 1388 status = hub_port_wait_reset(hub, port1, udev, delay); 1389 if (status) 1390 dev_dbg(hub->intfdev, 1391 "port_wait_reset: err = %d\n", 1392 status); 1393 } 1394 1395 /* return on disconnect or reset */ 1396 switch (status) { 1397 case 0: 1398 /* TRSTRCY = 10 ms */ 1399 msleep(10); 1400 /* FALL THROUGH */ 1401 case -ENOTCONN: 1402 case -ENODEV: 1403 clear_port_feature(hub->hdev, 1404 port1, USB_PORT_FEAT_C_RESET); 1405 /* FIXME need disconnect() for NOTATTACHED device */ 1406 usb_set_device_state(udev, status 1407 ? USB_STATE_NOTATTACHED 1408 : USB_STATE_DEFAULT); 1409 return status; 1410 } 1411 1412 dev_dbg (hub->intfdev, 1413 "port %d not enabled, trying reset again...\n", 1414 port1); 1415 delay = HUB_LONG_RESET_TIME; 1416 } 1417 1418 dev_err (hub->intfdev, 1419 "Cannot enable port %i. Maybe the USB cable is bad?\n", 1420 port1); 1421 1422 return status; 1423 } 1424 1425 static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) 1426 { 1427 struct usb_device *hdev = hub->hdev; 1428 int ret; 1429 1430 if (hdev->children[port1-1] && set_state) { 1431 usb_set_device_state(hdev->children[port1-1], 1432 USB_STATE_NOTATTACHED); 1433 } 1434 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); 1435 if (ret) 1436 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", 1437 port1, ret); 1438 1439 return ret; 1440 } 1441 1442 /* 1443 * Disable a port and mark a logical connnect-change event, so that some 1444 * time later khubd will disconnect() any existing usb_device on the port 1445 * and will re-enumerate if there actually is a device attached. 1446 */ 1447 static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) 1448 { 1449 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); 1450 hub_port_disable(hub, port1, 1); 1451 1452 /* FIXME let caller ask to power down the port: 1453 * - some devices won't enumerate without a VBUS power cycle 1454 * - SRP saves power that way 1455 * - usb_suspend_device(dev, PMSG_SUSPEND) 1456 * That's easy if this hub can switch power per-port, and 1457 * khubd reactivates the port later (timer, SRP, etc). 1458 * Powerdown must be optional, because of reset/DFU. 1459 */ 1460 1461 set_bit(port1, hub->change_bits); 1462 kick_khubd(hub); 1463 } 1464 1465 1466 #ifdef CONFIG_USB_SUSPEND 1467 1468 /* 1469 * Selective port suspend reduces power; most suspended devices draw 1470 * less than 500 uA. It's also used in OTG, along with remote wakeup. 1471 * All devices below the suspended port are also suspended. 1472 * 1473 * Devices leave suspend state when the host wakes them up. Some devices 1474 * also support "remote wakeup", where the device can activate the USB 1475 * tree above them to deliver data, such as a keypress or packet. In 1476 * some cases, this wakes the USB host. 1477 */ 1478 static int hub_port_suspend(struct usb_hub *hub, int port1, 1479 struct usb_device *udev) 1480 { 1481 int status; 1482 1483 // dev_dbg(hub->intfdev, "suspend port %d\n", port1); 1484 1485 /* enable remote wakeup when appropriate; this lets the device 1486 * wake up the upstream hub (including maybe the root hub). 1487 * 1488 * NOTE: OTG devices may issue remote wakeup (or SRP) even when 1489 * we don't explicitly enable it here. 1490 */ 1491 if (udev->actconfig 1492 // && FIXME (remote wakeup enabled on this bus) 1493 // ... currently assuming it's always appropriate 1494 && (udev->actconfig->desc.bmAttributes 1495 & USB_CONFIG_ATT_WAKEUP) != 0) { 1496 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 1497 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, 1498 USB_DEVICE_REMOTE_WAKEUP, 0, 1499 NULL, 0, 1500 USB_CTRL_SET_TIMEOUT); 1501 if (status) 1502 dev_dbg(&udev->dev, 1503 "won't remote wakeup, status %d\n", 1504 status); 1505 } 1506 1507 /* see 7.1.7.6 */ 1508 status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND); 1509 if (status) { 1510 dev_dbg(hub->intfdev, 1511 "can't suspend port %d, status %d\n", 1512 port1, status); 1513 /* paranoia: "should not happen" */ 1514 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 1515 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, 1516 USB_DEVICE_REMOTE_WAKEUP, 0, 1517 NULL, 0, 1518 USB_CTRL_SET_TIMEOUT); 1519 } else { 1520 /* device has up to 10 msec to fully suspend */ 1521 dev_dbg(&udev->dev, "usb suspend\n"); 1522 usb_set_device_state(udev, USB_STATE_SUSPENDED); 1523 msleep(10); 1524 } 1525 return status; 1526 } 1527 1528 /* 1529 * Devices on USB hub ports have only one "suspend" state, corresponding 1530 * to ACPI D2, "may cause the device to lose some context". 1531 * State transitions include: 1532 * 1533 * - suspend, resume ... when the VBUS power link stays live 1534 * - suspend, disconnect ... VBUS lost 1535 * 1536 * Once VBUS drop breaks the circuit, the port it's using has to go through 1537 * normal re-enumeration procedures, starting with enabling VBUS power. 1538 * Other than re-initializing the hub (plug/unplug, except for root hubs), 1539 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd 1540 * timer, no SRP, no requests through sysfs. 1541 */ 1542 static int __usb_suspend_device (struct usb_device *udev, int port1, 1543 pm_message_t state) 1544 { 1545 int status; 1546 1547 /* caller owns the udev device lock */ 1548 if (port1 < 0) 1549 return port1; 1550 1551 if (udev->state == USB_STATE_SUSPENDED 1552 || udev->state == USB_STATE_NOTATTACHED) { 1553 return 0; 1554 } 1555 1556 /* suspend interface drivers; if this is a hub, it 1557 * suspends the child devices 1558 */ 1559 if (udev->actconfig) { 1560 int i; 1561 1562 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1563 struct usb_interface *intf; 1564 struct usb_driver *driver; 1565 1566 intf = udev->actconfig->interface[i]; 1567 if (state <= intf->dev.power.power_state) 1568 continue; 1569 if (!intf->dev.driver) 1570 continue; 1571 driver = to_usb_driver(intf->dev.driver); 1572 1573 if (driver->suspend) { 1574 status = driver->suspend(intf, state); 1575 if (intf->dev.power.power_state != state 1576 || status) 1577 dev_err(&intf->dev, 1578 "suspend %d fail, code %d\n", 1579 state, status); 1580 } 1581 1582 /* only drivers with suspend() can ever resume(); 1583 * and after power loss, even they won't. 1584 * bus_rescan_devices() can rebind drivers later. 1585 * 1586 * FIXME the PM core self-deadlocks when unbinding 1587 * drivers during suspend/resume ... everything grabs 1588 * dpm_sem (not a spinlock, ugh). we want to unbind, 1589 * since we know every driver's probe/disconnect works 1590 * even for drivers that can't suspend. 1591 */ 1592 if (!driver->suspend || state > PM_SUSPEND_MEM) { 1593 #if 1 1594 dev_warn(&intf->dev, "resume is unsafe!\n"); 1595 #else 1596 down_write(&usb_bus_type.rwsem); 1597 device_release_driver(&intf->dev); 1598 up_write(&usb_bus_type.rwsem); 1599 #endif 1600 } 1601 } 1602 } 1603 1604 /* 1605 * FIXME this needs port power off call paths too, to help force 1606 * USB into the "generic" PM model. At least for devices on 1607 * ports that aren't using ganged switching (usually root hubs). 1608 * 1609 * NOTE: SRP-capable links should adopt more aggressive poweroff 1610 * policies (when HNP doesn't apply) once we have mechanisms to 1611 * turn power back on! (Likely not before 2.7...) 1612 */ 1613 if (state > PM_SUSPEND_MEM) { 1614 dev_warn(&udev->dev, "no poweroff yet, suspending instead\n"); 1615 } 1616 1617 /* "global suspend" of the HC-to-USB interface (root hub), or 1618 * "selective suspend" of just one hub-device link. 1619 */ 1620 if (!udev->parent) { 1621 struct usb_bus *bus = udev->bus; 1622 if (bus && bus->op->hub_suspend) { 1623 status = bus->op->hub_suspend (bus); 1624 if (status == 0) { 1625 dev_dbg(&udev->dev, "usb suspend\n"); 1626 usb_set_device_state(udev, 1627 USB_STATE_SUSPENDED); 1628 } 1629 } else 1630 status = -EOPNOTSUPP; 1631 } else 1632 status = hub_port_suspend(hdev_to_hub(udev->parent), port1, 1633 udev); 1634 1635 if (status == 0) 1636 udev->dev.power.power_state = state; 1637 return status; 1638 } 1639 1640 /** 1641 * usb_suspend_device - suspend a usb device 1642 * @udev: device that's no longer in active use 1643 * @state: PMSG_SUSPEND to suspend 1644 * Context: must be able to sleep; device not locked 1645 * 1646 * Suspends a USB device that isn't in active use, conserving power. 1647 * Devices may wake out of a suspend, if anything important happens, 1648 * using the remote wakeup mechanism. They may also be taken out of 1649 * suspend by the host, using usb_resume_device(). It's also routine 1650 * to disconnect devices while they are suspended. 1651 * 1652 * Suspending OTG devices may trigger HNP, if that's been enabled 1653 * between a pair of dual-role devices. That will change roles, such 1654 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. 1655 * 1656 * Returns 0 on success, else negative errno. 1657 */ 1658 int usb_suspend_device(struct usb_device *udev, pm_message_t state) 1659 { 1660 int port1, status; 1661 1662 port1 = locktree(udev); 1663 if (port1 < 0) 1664 return port1; 1665 1666 status = __usb_suspend_device(udev, port1, state); 1667 usb_unlock_device(udev); 1668 return status; 1669 } 1670 1671 /* 1672 * hardware resume signaling is finished, either because of selective 1673 * resume (by host) or remote wakeup (by device) ... now see what changed 1674 * in the tree that's rooted at this device. 1675 */ 1676 static int finish_port_resume(struct usb_device *udev) 1677 { 1678 int status; 1679 u16 devstatus; 1680 1681 /* caller owns the udev device lock */ 1682 dev_dbg(&udev->dev, "usb resume\n"); 1683 1684 /* usb ch9 identifies four variants of SUSPENDED, based on what 1685 * state the device resumes to. Linux currently won't see the 1686 * first two on the host side; they'd be inside hub_port_init() 1687 * during many timeouts, but khubd can't suspend until later. 1688 */ 1689 usb_set_device_state(udev, udev->actconfig 1690 ? USB_STATE_CONFIGURED 1691 : USB_STATE_ADDRESS); 1692 udev->dev.power.power_state = PMSG_ON; 1693 1694 /* 10.5.4.5 says be sure devices in the tree are still there. 1695 * For now let's assume the device didn't go crazy on resume, 1696 * and device drivers will know about any resume quirks. 1697 */ 1698 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); 1699 if (status < 0) 1700 dev_dbg(&udev->dev, 1701 "gone after usb resume? status %d\n", 1702 status); 1703 else if (udev->actconfig) { 1704 unsigned i; 1705 1706 le16_to_cpus(&devstatus); 1707 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { 1708 status = usb_control_msg(udev, 1709 usb_sndctrlpipe(udev, 0), 1710 USB_REQ_CLEAR_FEATURE, 1711 USB_RECIP_DEVICE, 1712 USB_DEVICE_REMOTE_WAKEUP, 0, 1713 NULL, 0, 1714 USB_CTRL_SET_TIMEOUT); 1715 if (status) { 1716 dev_dbg(&udev->dev, "disable remote " 1717 "wakeup, status %d\n", status); 1718 status = 0; 1719 } 1720 } 1721 1722 /* resume interface drivers; if this is a hub, it 1723 * resumes the child devices 1724 */ 1725 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1726 struct usb_interface *intf; 1727 struct usb_driver *driver; 1728 1729 intf = udev->actconfig->interface[i]; 1730 if (intf->dev.power.power_state == PMSG_SUSPEND) 1731 continue; 1732 if (!intf->dev.driver) { 1733 /* FIXME maybe force to alt 0 */ 1734 continue; 1735 } 1736 driver = to_usb_driver(intf->dev.driver); 1737 1738 /* bus_rescan_devices() may rebind drivers */ 1739 if (!driver->resume) 1740 continue; 1741 1742 /* can we do better than just logging errors? */ 1743 status = driver->resume(intf); 1744 if (intf->dev.power.power_state != PMSG_ON 1745 || status) 1746 dev_dbg(&intf->dev, 1747 "resume fail, state %d code %d\n", 1748 intf->dev.power.power_state, status); 1749 } 1750 status = 0; 1751 1752 } else if (udev->devnum <= 0) { 1753 dev_dbg(&udev->dev, "bogus resume!\n"); 1754 status = -EINVAL; 1755 } 1756 return status; 1757 } 1758 1759 static int 1760 hub_port_resume(struct usb_hub *hub, int port1, struct usb_device *udev) 1761 { 1762 int status; 1763 1764 // dev_dbg(hub->intfdev, "resume port %d\n", port1); 1765 1766 /* see 7.1.7.7; affects power usage, but not budgeting */ 1767 status = clear_port_feature(hub->hdev, 1768 port1, USB_PORT_FEAT_SUSPEND); 1769 if (status) { 1770 dev_dbg(hub->intfdev, 1771 "can't resume port %d, status %d\n", 1772 port1, status); 1773 } else { 1774 u16 devstatus; 1775 u16 portchange; 1776 1777 /* drive resume for at least 20 msec */ 1778 if (udev) 1779 dev_dbg(&udev->dev, "RESUME\n"); 1780 msleep(25); 1781 1782 #define LIVE_FLAGS ( USB_PORT_STAT_POWER \ 1783 | USB_PORT_STAT_ENABLE \ 1784 | USB_PORT_STAT_CONNECTION) 1785 1786 /* Virtual root hubs can trigger on GET_PORT_STATUS to 1787 * stop resume signaling. Then finish the resume 1788 * sequence. 1789 */ 1790 devstatus = portchange = 0; 1791 status = hub_port_status(hub, port1, 1792 &devstatus, &portchange); 1793 if (status < 0 1794 || (devstatus & LIVE_FLAGS) != LIVE_FLAGS 1795 || (devstatus & USB_PORT_STAT_SUSPEND) != 0 1796 ) { 1797 dev_dbg(hub->intfdev, 1798 "port %d status %04x.%04x after resume, %d\n", 1799 port1, portchange, devstatus, status); 1800 } else { 1801 /* TRSMRCY = 10 msec */ 1802 msleep(10); 1803 if (udev) 1804 status = finish_port_resume(udev); 1805 } 1806 } 1807 if (status < 0) 1808 hub_port_logical_disconnect(hub, port1); 1809 1810 return status; 1811 } 1812 1813 static int hub_resume (struct usb_interface *intf); 1814 1815 /** 1816 * usb_resume_device - re-activate a suspended usb device 1817 * @udev: device to re-activate 1818 * Context: must be able to sleep; device not locked 1819 * 1820 * This will re-activate the suspended device, increasing power usage 1821 * while letting drivers communicate again with its endpoints. 1822 * USB resume explicitly guarantees that the power session between 1823 * the host and the device is the same as it was when the device 1824 * suspended. 1825 * 1826 * Returns 0 on success, else negative errno. 1827 */ 1828 int usb_resume_device(struct usb_device *udev) 1829 { 1830 int port1, status; 1831 1832 port1 = locktree(udev); 1833 if (port1 < 0) 1834 return port1; 1835 1836 /* "global resume" of the HC-to-USB interface (root hub), or 1837 * selective resume of one hub-to-device port 1838 */ 1839 if (!udev->parent) { 1840 struct usb_bus *bus = udev->bus; 1841 if (bus && bus->op->hub_resume) { 1842 status = bus->op->hub_resume (bus); 1843 } else 1844 status = -EOPNOTSUPP; 1845 if (status == 0) { 1846 dev_dbg(&udev->dev, "usb resume\n"); 1847 /* TRSMRCY = 10 msec */ 1848 msleep(10); 1849 usb_set_device_state (udev, USB_STATE_CONFIGURED); 1850 udev->dev.power.power_state = PMSG_ON; 1851 status = hub_resume (udev 1852 ->actconfig->interface[0]); 1853 } 1854 } else if (udev->state == USB_STATE_SUSPENDED) { 1855 // NOTE this fails if parent is also suspended... 1856 status = hub_port_resume(hdev_to_hub(udev->parent), 1857 port1, udev); 1858 } else { 1859 status = 0; 1860 } 1861 if (status < 0) { 1862 dev_dbg(&udev->dev, "can't resume, status %d\n", 1863 status); 1864 } 1865 1866 usb_unlock_device(udev); 1867 1868 /* rebind drivers that had no suspend() */ 1869 if (status == 0) { 1870 usb_lock_all_devices(); 1871 bus_rescan_devices(&usb_bus_type); 1872 usb_unlock_all_devices(); 1873 } 1874 return status; 1875 } 1876 1877 static int remote_wakeup(struct usb_device *udev) 1878 { 1879 int status = 0; 1880 1881 /* don't repeat RESUME sequence if this device 1882 * was already woken up by some other task 1883 */ 1884 down(&udev->serialize); 1885 if (udev->state == USB_STATE_SUSPENDED) { 1886 dev_dbg(&udev->dev, "RESUME (wakeup)\n"); 1887 /* TRSMRCY = 10 msec */ 1888 msleep(10); 1889 status = finish_port_resume(udev); 1890 } 1891 up(&udev->serialize); 1892 return status; 1893 } 1894 1895 static int hub_suspend(struct usb_interface *intf, pm_message_t state) 1896 { 1897 struct usb_hub *hub = usb_get_intfdata (intf); 1898 struct usb_device *hdev = hub->hdev; 1899 unsigned port1; 1900 int status; 1901 1902 /* stop khubd and related activity */ 1903 hub_quiesce(hub); 1904 1905 /* then suspend every port */ 1906 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 1907 struct usb_device *udev; 1908 1909 udev = hdev->children [port1-1]; 1910 if (!udev) 1911 continue; 1912 down(&udev->serialize); 1913 status = __usb_suspend_device(udev, port1, state); 1914 up(&udev->serialize); 1915 if (status < 0) 1916 dev_dbg(&intf->dev, "suspend port %d --> %d\n", 1917 port1, status); 1918 } 1919 1920 intf->dev.power.power_state = state; 1921 return 0; 1922 } 1923 1924 static int hub_resume(struct usb_interface *intf) 1925 { 1926 struct usb_device *hdev = interface_to_usbdev(intf); 1927 struct usb_hub *hub = usb_get_intfdata (intf); 1928 unsigned port1; 1929 int status; 1930 1931 if (intf->dev.power.power_state == PM_SUSPEND_ON) 1932 return 0; 1933 1934 for (port1 = 1; port1 <= hdev->maxchild; port1++) { 1935 struct usb_device *udev; 1936 u16 portstat, portchange; 1937 1938 udev = hdev->children [port1-1]; 1939 status = hub_port_status(hub, port1, &portstat, &portchange); 1940 if (status == 0) { 1941 if (portchange & USB_PORT_STAT_C_SUSPEND) { 1942 clear_port_feature(hdev, port1, 1943 USB_PORT_FEAT_C_SUSPEND); 1944 portchange &= ~USB_PORT_STAT_C_SUSPEND; 1945 } 1946 1947 /* let khubd handle disconnects etc */ 1948 if (portchange) 1949 continue; 1950 } 1951 1952 if (!udev || status < 0) 1953 continue; 1954 down (&udev->serialize); 1955 if (portstat & USB_PORT_STAT_SUSPEND) 1956 status = hub_port_resume(hub, port1, udev); 1957 else { 1958 status = finish_port_resume(udev); 1959 if (status < 0) { 1960 dev_dbg(&intf->dev, "resume port %d --> %d\n", 1961 port1, status); 1962 hub_port_logical_disconnect(hub, port1); 1963 } 1964 } 1965 up(&udev->serialize); 1966 } 1967 intf->dev.power.power_state = PMSG_ON; 1968 1969 hub->resume_root_hub = 0; 1970 hub_activate(hub); 1971 return 0; 1972 } 1973 1974 void usb_resume_root_hub(struct usb_device *hdev) 1975 { 1976 struct usb_hub *hub = hdev_to_hub(hdev); 1977 1978 hub->resume_root_hub = 1; 1979 kick_khubd(hub); 1980 } 1981 1982 #else /* !CONFIG_USB_SUSPEND */ 1983 1984 int usb_suspend_device(struct usb_device *udev, pm_message_t state) 1985 { 1986 return 0; 1987 } 1988 1989 int usb_resume_device(struct usb_device *udev) 1990 { 1991 return 0; 1992 } 1993 1994 #define hub_suspend NULL 1995 #define hub_resume NULL 1996 #define remote_wakeup(x) 0 1997 1998 #endif /* CONFIG_USB_SUSPEND */ 1999 2000 EXPORT_SYMBOL(usb_suspend_device); 2001 EXPORT_SYMBOL(usb_resume_device); 2002 2003 2004 2005 /* USB 2.0 spec, 7.1.7.3 / fig 7-29: 2006 * 2007 * Between connect detection and reset signaling there must be a delay 2008 * of 100ms at least for debounce and power-settling. The corresponding 2009 * timer shall restart whenever the downstream port detects a disconnect. 2010 * 2011 * Apparently there are some bluetooth and irda-dongles and a number of 2012 * low-speed devices for which this debounce period may last over a second. 2013 * Not covered by the spec - but easy to deal with. 2014 * 2015 * This implementation uses a 1500ms total debounce timeout; if the 2016 * connection isn't stable by then it returns -ETIMEDOUT. It checks 2017 * every 25ms for transient disconnects. When the port status has been 2018 * unchanged for 100ms it returns the port status. 2019 */ 2020 2021 #define HUB_DEBOUNCE_TIMEOUT 1500 2022 #define HUB_DEBOUNCE_STEP 25 2023 #define HUB_DEBOUNCE_STABLE 100 2024 2025 static int hub_port_debounce(struct usb_hub *hub, int port1) 2026 { 2027 int ret; 2028 int total_time, stable_time = 0; 2029 u16 portchange, portstatus; 2030 unsigned connection = 0xffff; 2031 2032 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { 2033 ret = hub_port_status(hub, port1, &portstatus, &portchange); 2034 if (ret < 0) 2035 return ret; 2036 2037 if (!(portchange & USB_PORT_STAT_C_CONNECTION) && 2038 (portstatus & USB_PORT_STAT_CONNECTION) == connection) { 2039 stable_time += HUB_DEBOUNCE_STEP; 2040 if (stable_time >= HUB_DEBOUNCE_STABLE) 2041 break; 2042 } else { 2043 stable_time = 0; 2044 connection = portstatus & USB_PORT_STAT_CONNECTION; 2045 } 2046 2047 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2048 clear_port_feature(hub->hdev, port1, 2049 USB_PORT_FEAT_C_CONNECTION); 2050 } 2051 2052 if (total_time >= HUB_DEBOUNCE_TIMEOUT) 2053 break; 2054 msleep(HUB_DEBOUNCE_STEP); 2055 } 2056 2057 dev_dbg (hub->intfdev, 2058 "debounce: port %d: total %dms stable %dms status 0x%x\n", 2059 port1, total_time, stable_time, portstatus); 2060 2061 if (stable_time < HUB_DEBOUNCE_STABLE) 2062 return -ETIMEDOUT; 2063 return portstatus; 2064 } 2065 2066 static void ep0_reinit(struct usb_device *udev) 2067 { 2068 usb_disable_endpoint(udev, 0 + USB_DIR_IN); 2069 usb_disable_endpoint(udev, 0 + USB_DIR_OUT); 2070 udev->ep_in[0] = udev->ep_out[0] = &udev->ep0; 2071 } 2072 2073 #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) 2074 #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) 2075 2076 static int hub_set_address(struct usb_device *udev) 2077 { 2078 int retval; 2079 2080 if (udev->devnum == 0) 2081 return -EINVAL; 2082 if (udev->state == USB_STATE_ADDRESS) 2083 return 0; 2084 if (udev->state != USB_STATE_DEFAULT) 2085 return -EINVAL; 2086 retval = usb_control_msg(udev, usb_sndaddr0pipe(), 2087 USB_REQ_SET_ADDRESS, 0, udev->devnum, 0, 2088 NULL, 0, USB_CTRL_SET_TIMEOUT); 2089 if (retval == 0) { 2090 usb_set_device_state(udev, USB_STATE_ADDRESS); 2091 ep0_reinit(udev); 2092 } 2093 return retval; 2094 } 2095 2096 /* Reset device, (re)assign address, get device descriptor. 2097 * Device connection must be stable, no more debouncing needed. 2098 * Returns device in USB_STATE_ADDRESS, except on error. 2099 * 2100 * If this is called for an already-existing device (as part of 2101 * usb_reset_device), the caller must own the device lock. For a 2102 * newly detected device that is not accessible through any global 2103 * pointers, it's not necessary to lock the device. 2104 */ 2105 static int 2106 hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, 2107 int retry_counter) 2108 { 2109 static DECLARE_MUTEX(usb_address0_sem); 2110 2111 struct usb_device *hdev = hub->hdev; 2112 int i, j, retval; 2113 unsigned delay = HUB_SHORT_RESET_TIME; 2114 enum usb_device_speed oldspeed = udev->speed; 2115 2116 /* root hub ports have a slightly longer reset period 2117 * (from USB 2.0 spec, section 7.1.7.5) 2118 */ 2119 if (!hdev->parent) { 2120 delay = HUB_ROOT_RESET_TIME; 2121 if (port1 == hdev->bus->otg_port) 2122 hdev->bus->b_hnp_enable = 0; 2123 } 2124 2125 /* Some low speed devices have problems with the quick delay, so */ 2126 /* be a bit pessimistic with those devices. RHbug #23670 */ 2127 if (oldspeed == USB_SPEED_LOW) 2128 delay = HUB_LONG_RESET_TIME; 2129 2130 down(&usb_address0_sem); 2131 2132 /* Reset the device; full speed may morph to high speed */ 2133 retval = hub_port_reset(hub, port1, udev, delay); 2134 if (retval < 0) /* error or disconnect */ 2135 goto fail; 2136 /* success, speed is known */ 2137 retval = -ENODEV; 2138 2139 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { 2140 dev_dbg(&udev->dev, "device reset changed speed!\n"); 2141 goto fail; 2142 } 2143 oldspeed = udev->speed; 2144 2145 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... 2146 * it's fixed size except for full speed devices. 2147 */ 2148 switch (udev->speed) { 2149 case USB_SPEED_HIGH: /* fixed at 64 */ 2150 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 2151 break; 2152 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ 2153 /* to determine the ep0 maxpacket size, try to read 2154 * the device descriptor to get bMaxPacketSize0 and 2155 * then correct our initial guess. 2156 */ 2157 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64); 2158 break; 2159 case USB_SPEED_LOW: /* fixed at 8 */ 2160 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(8); 2161 break; 2162 default: 2163 goto fail; 2164 } 2165 2166 dev_info (&udev->dev, 2167 "%s %s speed USB device using %s and address %d\n", 2168 (udev->config) ? "reset" : "new", 2169 ({ char *speed; switch (udev->speed) { 2170 case USB_SPEED_LOW: speed = "low"; break; 2171 case USB_SPEED_FULL: speed = "full"; break; 2172 case USB_SPEED_HIGH: speed = "high"; break; 2173 default: speed = "?"; break; 2174 }; speed;}), 2175 udev->bus->controller->driver->name, 2176 udev->devnum); 2177 2178 /* Set up TT records, if needed */ 2179 if (hdev->tt) { 2180 udev->tt = hdev->tt; 2181 udev->ttport = hdev->ttport; 2182 } else if (udev->speed != USB_SPEED_HIGH 2183 && hdev->speed == USB_SPEED_HIGH) { 2184 udev->tt = &hub->tt; 2185 udev->ttport = port1; 2186 } 2187 2188 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? 2189 * Because device hardware and firmware is sometimes buggy in 2190 * this area, and this is how Linux has done it for ages. 2191 * Change it cautiously. 2192 * 2193 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing 2194 * a 64-byte GET_DESCRIPTOR request. This is what Windows does, 2195 * so it may help with some non-standards-compliant devices. 2196 * Otherwise we start with SET_ADDRESS and then try to read the 2197 * first 8 bytes of the device descriptor to get the ep0 maxpacket 2198 * value. 2199 */ 2200 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { 2201 if (USE_NEW_SCHEME(retry_counter)) { 2202 struct usb_device_descriptor *buf; 2203 int r = 0; 2204 2205 #define GET_DESCRIPTOR_BUFSIZE 64 2206 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); 2207 if (!buf) { 2208 retval = -ENOMEM; 2209 continue; 2210 } 2211 2212 /* Use a short timeout the first time through, 2213 * so that recalcitrant full-speed devices with 2214 * 8- or 16-byte ep0-maxpackets won't slow things 2215 * down tremendously by NAKing the unexpectedly 2216 * early status stage. Also, retry on all errors; 2217 * some devices are flakey. 2218 */ 2219 for (j = 0; j < 3; ++j) { 2220 buf->bMaxPacketSize0 = 0; 2221 r = usb_control_msg(udev, usb_rcvaddr0pipe(), 2222 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, 2223 USB_DT_DEVICE << 8, 0, 2224 buf, GET_DESCRIPTOR_BUFSIZE, 2225 (i ? USB_CTRL_GET_TIMEOUT : 1000)); 2226 switch (buf->bMaxPacketSize0) { 2227 case 8: case 16: case 32: case 64: 2228 if (buf->bDescriptorType == 2229 USB_DT_DEVICE) { 2230 r = 0; 2231 break; 2232 } 2233 /* FALL THROUGH */ 2234 default: 2235 if (r == 0) 2236 r = -EPROTO; 2237 break; 2238 } 2239 if (r == 0) 2240 break; 2241 } 2242 udev->descriptor.bMaxPacketSize0 = 2243 buf->bMaxPacketSize0; 2244 kfree(buf); 2245 2246 retval = hub_port_reset(hub, port1, udev, delay); 2247 if (retval < 0) /* error or disconnect */ 2248 goto fail; 2249 if (oldspeed != udev->speed) { 2250 dev_dbg(&udev->dev, 2251 "device reset changed speed!\n"); 2252 retval = -ENODEV; 2253 goto fail; 2254 } 2255 if (r) { 2256 dev_err(&udev->dev, "device descriptor " 2257 "read/%s, error %d\n", 2258 "64", r); 2259 retval = -EMSGSIZE; 2260 continue; 2261 } 2262 #undef GET_DESCRIPTOR_BUFSIZE 2263 } 2264 2265 for (j = 0; j < SET_ADDRESS_TRIES; ++j) { 2266 retval = hub_set_address(udev); 2267 if (retval >= 0) 2268 break; 2269 msleep(200); 2270 } 2271 if (retval < 0) { 2272 dev_err(&udev->dev, 2273 "device not accepting address %d, error %d\n", 2274 udev->devnum, retval); 2275 goto fail; 2276 } 2277 2278 /* cope with hardware quirkiness: 2279 * - let SET_ADDRESS settle, some device hardware wants it 2280 * - read ep0 maxpacket even for high and low speed, 2281 */ 2282 msleep(10); 2283 if (USE_NEW_SCHEME(retry_counter)) 2284 break; 2285 2286 retval = usb_get_device_descriptor(udev, 8); 2287 if (retval < 8) { 2288 dev_err(&udev->dev, "device descriptor " 2289 "read/%s, error %d\n", 2290 "8", retval); 2291 if (retval >= 0) 2292 retval = -EMSGSIZE; 2293 } else { 2294 retval = 0; 2295 break; 2296 } 2297 } 2298 if (retval) 2299 goto fail; 2300 2301 i = udev->descriptor.bMaxPacketSize0; 2302 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) { 2303 if (udev->speed != USB_SPEED_FULL || 2304 !(i == 8 || i == 16 || i == 32 || i == 64)) { 2305 dev_err(&udev->dev, "ep0 maxpacket = %d\n", i); 2306 retval = -EMSGSIZE; 2307 goto fail; 2308 } 2309 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); 2310 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); 2311 ep0_reinit(udev); 2312 } 2313 2314 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); 2315 if (retval < (signed)sizeof(udev->descriptor)) { 2316 dev_err(&udev->dev, "device descriptor read/%s, error %d\n", 2317 "all", retval); 2318 if (retval >= 0) 2319 retval = -ENOMSG; 2320 goto fail; 2321 } 2322 2323 retval = 0; 2324 2325 fail: 2326 if (retval) 2327 hub_port_disable(hub, port1, 0); 2328 up(&usb_address0_sem); 2329 return retval; 2330 } 2331 2332 static void 2333 check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) 2334 { 2335 struct usb_qualifier_descriptor *qual; 2336 int status; 2337 2338 qual = kmalloc (sizeof *qual, SLAB_KERNEL); 2339 if (qual == NULL) 2340 return; 2341 2342 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, 2343 qual, sizeof *qual); 2344 if (status == sizeof *qual) { 2345 dev_info(&udev->dev, "not running at top speed; " 2346 "connect to a high speed hub\n"); 2347 /* hub LEDs are probably harder to miss than syslog */ 2348 if (hub->has_indicators) { 2349 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; 2350 schedule_work (&hub->leds); 2351 } 2352 } 2353 kfree(qual); 2354 } 2355 2356 static unsigned 2357 hub_power_remaining (struct usb_hub *hub) 2358 { 2359 struct usb_device *hdev = hub->hdev; 2360 int remaining; 2361 unsigned i; 2362 2363 remaining = hub->power_budget; 2364 if (!remaining) /* self-powered */ 2365 return 0; 2366 2367 for (i = 0; i < hdev->maxchild; i++) { 2368 struct usb_device *udev = hdev->children[i]; 2369 int delta, ceiling; 2370 2371 if (!udev) 2372 continue; 2373 2374 /* 100mA per-port ceiling, or 8mA for OTG ports */ 2375 if (i != (udev->bus->otg_port - 1) || hdev->parent) 2376 ceiling = 50; 2377 else 2378 ceiling = 4; 2379 2380 if (udev->actconfig) 2381 delta = udev->actconfig->desc.bMaxPower; 2382 else 2383 delta = ceiling; 2384 // dev_dbg(&udev->dev, "budgeted %dmA\n", 2 * delta); 2385 if (delta > ceiling) 2386 dev_warn(&udev->dev, "%dmA over %dmA budget!\n", 2387 2 * (delta - ceiling), 2 * ceiling); 2388 remaining -= delta; 2389 } 2390 if (remaining < 0) { 2391 dev_warn(hub->intfdev, 2392 "%dmA over power budget!\n", 2393 -2 * remaining); 2394 remaining = 0; 2395 } 2396 return remaining; 2397 } 2398 2399 /* Handle physical or logical connection change events. 2400 * This routine is called when: 2401 * a port connection-change occurs; 2402 * a port enable-change occurs (often caused by EMI); 2403 * usb_reset_device() encounters changed descriptors (as from 2404 * a firmware download) 2405 * caller already locked the hub 2406 */ 2407 static void hub_port_connect_change(struct usb_hub *hub, int port1, 2408 u16 portstatus, u16 portchange) 2409 { 2410 struct usb_device *hdev = hub->hdev; 2411 struct device *hub_dev = hub->intfdev; 2412 int status, i; 2413 2414 dev_dbg (hub_dev, 2415 "port %d, status %04x, change %04x, %s\n", 2416 port1, portstatus, portchange, portspeed (portstatus)); 2417 2418 if (hub->has_indicators) { 2419 set_port_led(hub, port1, HUB_LED_AUTO); 2420 hub->indicator[port1-1] = INDICATOR_AUTO; 2421 } 2422 2423 /* Disconnect any existing devices under this port */ 2424 if (hdev->children[port1-1]) 2425 usb_disconnect(&hdev->children[port1-1]); 2426 clear_bit(port1, hub->change_bits); 2427 2428 #ifdef CONFIG_USB_OTG 2429 /* during HNP, don't repeat the debounce */ 2430 if (hdev->bus->is_b_host) 2431 portchange &= ~USB_PORT_STAT_C_CONNECTION; 2432 #endif 2433 2434 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2435 status = hub_port_debounce(hub, port1); 2436 if (status < 0) { 2437 dev_err (hub_dev, 2438 "connect-debounce failed, port %d disabled\n", 2439 port1); 2440 goto done; 2441 } 2442 portstatus = status; 2443 } 2444 2445 /* Return now if nothing is connected */ 2446 if (!(portstatus & USB_PORT_STAT_CONNECTION)) { 2447 2448 /* maybe switch power back on (e.g. root hub was reset) */ 2449 if ((hub->descriptor->wHubCharacteristics 2450 & HUB_CHAR_LPSM) < 2 2451 && !(portstatus & (1 << USB_PORT_FEAT_POWER))) 2452 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); 2453 2454 if (portstatus & USB_PORT_STAT_ENABLE) 2455 goto done; 2456 return; 2457 } 2458 2459 #ifdef CONFIG_USB_SUSPEND 2460 /* If something is connected, but the port is suspended, wake it up. */ 2461 if (portstatus & USB_PORT_STAT_SUSPEND) { 2462 status = hub_port_resume(hub, port1, NULL); 2463 if (status < 0) { 2464 dev_dbg(hub_dev, 2465 "can't clear suspend on port %d; %d\n", 2466 port1, status); 2467 goto done; 2468 } 2469 } 2470 #endif 2471 2472 for (i = 0; i < SET_CONFIG_TRIES; i++) { 2473 struct usb_device *udev; 2474 2475 /* reallocate for each attempt, since references 2476 * to the previous one can escape in various ways 2477 */ 2478 udev = usb_alloc_dev(hdev, hdev->bus, port1); 2479 if (!udev) { 2480 dev_err (hub_dev, 2481 "couldn't allocate port %d usb_device\n", 2482 port1); 2483 goto done; 2484 } 2485 2486 usb_set_device_state(udev, USB_STATE_POWERED); 2487 udev->speed = USB_SPEED_UNKNOWN; 2488 2489 /* set the address */ 2490 choose_address(udev); 2491 if (udev->devnum <= 0) { 2492 status = -ENOTCONN; /* Don't retry */ 2493 goto loop; 2494 } 2495 2496 /* reset and get descriptor */ 2497 status = hub_port_init(hub, udev, port1, i); 2498 if (status < 0) 2499 goto loop; 2500 2501 /* consecutive bus-powered hubs aren't reliable; they can 2502 * violate the voltage drop budget. if the new child has 2503 * a "powered" LED, users should notice we didn't enable it 2504 * (without reading syslog), even without per-port LEDs 2505 * on the parent. 2506 */ 2507 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB 2508 && hub->power_budget) { 2509 u16 devstat; 2510 2511 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, 2512 &devstat); 2513 if (status < 0) { 2514 dev_dbg(&udev->dev, "get status %d ?\n", status); 2515 goto loop_disable; 2516 } 2517 cpu_to_le16s(&devstat); 2518 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { 2519 dev_err(&udev->dev, 2520 "can't connect bus-powered hub " 2521 "to this port\n"); 2522 if (hub->has_indicators) { 2523 hub->indicator[port1-1] = 2524 INDICATOR_AMBER_BLINK; 2525 schedule_work (&hub->leds); 2526 } 2527 status = -ENOTCONN; /* Don't retry */ 2528 goto loop_disable; 2529 } 2530 } 2531 2532 /* check for devices running slower than they could */ 2533 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 2534 && udev->speed == USB_SPEED_FULL 2535 && highspeed_hubs != 0) 2536 check_highspeed (hub, udev, port1); 2537 2538 /* Store the parent's children[] pointer. At this point 2539 * udev becomes globally accessible, although presumably 2540 * no one will look at it until hdev is unlocked. 2541 */ 2542 down (&udev->serialize); 2543 status = 0; 2544 2545 /* We mustn't add new devices if the parent hub has 2546 * been disconnected; we would race with the 2547 * recursively_mark_NOTATTACHED() routine. 2548 */ 2549 spin_lock_irq(&device_state_lock); 2550 if (hdev->state == USB_STATE_NOTATTACHED) 2551 status = -ENOTCONN; 2552 else 2553 hdev->children[port1-1] = udev; 2554 spin_unlock_irq(&device_state_lock); 2555 2556 /* Run it through the hoops (find a driver, etc) */ 2557 if (!status) { 2558 status = usb_new_device(udev); 2559 if (status) { 2560 spin_lock_irq(&device_state_lock); 2561 hdev->children[port1-1] = NULL; 2562 spin_unlock_irq(&device_state_lock); 2563 } 2564 } 2565 2566 up (&udev->serialize); 2567 if (status) 2568 goto loop_disable; 2569 2570 status = hub_power_remaining(hub); 2571 if (status) 2572 dev_dbg(hub_dev, 2573 "%dmA power budget left\n", 2574 2 * status); 2575 2576 return; 2577 2578 loop_disable: 2579 hub_port_disable(hub, port1, 1); 2580 loop: 2581 ep0_reinit(udev); 2582 release_address(udev); 2583 usb_put_dev(udev); 2584 if (status == -ENOTCONN) 2585 break; 2586 } 2587 2588 done: 2589 hub_port_disable(hub, port1, 1); 2590 } 2591 2592 static void hub_events(void) 2593 { 2594 struct list_head *tmp; 2595 struct usb_device *hdev; 2596 struct usb_interface *intf; 2597 struct usb_hub *hub; 2598 struct device *hub_dev; 2599 u16 hubstatus; 2600 u16 hubchange; 2601 u16 portstatus; 2602 u16 portchange; 2603 int i, ret; 2604 int connect_change; 2605 2606 /* 2607 * We restart the list every time to avoid a deadlock with 2608 * deleting hubs downstream from this one. This should be 2609 * safe since we delete the hub from the event list. 2610 * Not the most efficient, but avoids deadlocks. 2611 */ 2612 while (1) { 2613 2614 /* Grab the first entry at the beginning of the list */ 2615 spin_lock_irq(&hub_event_lock); 2616 if (list_empty(&hub_event_list)) { 2617 spin_unlock_irq(&hub_event_lock); 2618 break; 2619 } 2620 2621 tmp = hub_event_list.next; 2622 list_del_init(tmp); 2623 2624 hub = list_entry(tmp, struct usb_hub, event_list); 2625 hdev = hub->hdev; 2626 intf = to_usb_interface(hub->intfdev); 2627 hub_dev = &intf->dev; 2628 2629 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", 2630 hdev->state, hub->descriptor 2631 ? hub->descriptor->bNbrPorts 2632 : 0, 2633 /* NOTE: expects max 15 ports... */ 2634 (u16) hub->change_bits[0], 2635 (u16) hub->event_bits[0]); 2636 2637 usb_get_intf(intf); 2638 i = hub->resume_root_hub; 2639 spin_unlock_irq(&hub_event_lock); 2640 2641 /* Is this is a root hub wanting to be resumed? */ 2642 if (i) 2643 usb_resume_device(hdev); 2644 2645 /* Lock the device, then check to see if we were 2646 * disconnected while waiting for the lock to succeed. */ 2647 if (locktree(hdev) < 0) { 2648 usb_put_intf(intf); 2649 continue; 2650 } 2651 if (hub != usb_get_intfdata(intf)) 2652 goto loop; 2653 2654 /* If the hub has died, clean up after it */ 2655 if (hdev->state == USB_STATE_NOTATTACHED) { 2656 hub_pre_reset(hub); 2657 goto loop; 2658 } 2659 2660 /* If this is an inactive or suspended hub, do nothing */ 2661 if (hub->quiescing) 2662 goto loop; 2663 2664 if (hub->error) { 2665 dev_dbg (hub_dev, "resetting for error %d\n", 2666 hub->error); 2667 2668 ret = usb_reset_device(hdev); 2669 if (ret) { 2670 dev_dbg (hub_dev, 2671 "error resetting hub: %d\n", ret); 2672 goto loop; 2673 } 2674 2675 hub->nerrors = 0; 2676 hub->error = 0; 2677 } 2678 2679 /* deal with port status changes */ 2680 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { 2681 if (test_bit(i, hub->busy_bits)) 2682 continue; 2683 connect_change = test_bit(i, hub->change_bits); 2684 if (!test_and_clear_bit(i, hub->event_bits) && 2685 !connect_change && !hub->activating) 2686 continue; 2687 2688 ret = hub_port_status(hub, i, 2689 &portstatus, &portchange); 2690 if (ret < 0) 2691 continue; 2692 2693 if (hub->activating && !hdev->children[i-1] && 2694 (portstatus & 2695 USB_PORT_STAT_CONNECTION)) 2696 connect_change = 1; 2697 2698 if (portchange & USB_PORT_STAT_C_CONNECTION) { 2699 clear_port_feature(hdev, i, 2700 USB_PORT_FEAT_C_CONNECTION); 2701 connect_change = 1; 2702 } 2703 2704 if (portchange & USB_PORT_STAT_C_ENABLE) { 2705 if (!connect_change) 2706 dev_dbg (hub_dev, 2707 "port %d enable change, " 2708 "status %08x\n", 2709 i, portstatus); 2710 clear_port_feature(hdev, i, 2711 USB_PORT_FEAT_C_ENABLE); 2712 2713 /* 2714 * EM interference sometimes causes badly 2715 * shielded USB devices to be shutdown by 2716 * the hub, this hack enables them again. 2717 * Works at least with mouse driver. 2718 */ 2719 if (!(portstatus & USB_PORT_STAT_ENABLE) 2720 && !connect_change 2721 && hdev->children[i-1]) { 2722 dev_err (hub_dev, 2723 "port %i " 2724 "disabled by hub (EMI?), " 2725 "re-enabling...\n", 2726 i); 2727 connect_change = 1; 2728 } 2729 } 2730 2731 if (portchange & USB_PORT_STAT_C_SUSPEND) { 2732 clear_port_feature(hdev, i, 2733 USB_PORT_FEAT_C_SUSPEND); 2734 if (hdev->children[i-1]) { 2735 ret = remote_wakeup(hdev-> 2736 children[i-1]); 2737 if (ret < 0) 2738 connect_change = 1; 2739 } else { 2740 ret = -ENODEV; 2741 hub_port_disable(hub, i, 1); 2742 } 2743 dev_dbg (hub_dev, 2744 "resume on port %d, status %d\n", 2745 i, ret); 2746 } 2747 2748 if (portchange & USB_PORT_STAT_C_OVERCURRENT) { 2749 dev_err (hub_dev, 2750 "over-current change on port %d\n", 2751 i); 2752 clear_port_feature(hdev, i, 2753 USB_PORT_FEAT_C_OVER_CURRENT); 2754 hub_power_on(hub); 2755 } 2756 2757 if (portchange & USB_PORT_STAT_C_RESET) { 2758 dev_dbg (hub_dev, 2759 "reset change on port %d\n", 2760 i); 2761 clear_port_feature(hdev, i, 2762 USB_PORT_FEAT_C_RESET); 2763 } 2764 2765 if (connect_change) 2766 hub_port_connect_change(hub, i, 2767 portstatus, portchange); 2768 } /* end for i */ 2769 2770 /* deal with hub status changes */ 2771 if (test_and_clear_bit(0, hub->event_bits) == 0) 2772 ; /* do nothing */ 2773 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) 2774 dev_err (hub_dev, "get_hub_status failed\n"); 2775 else { 2776 if (hubchange & HUB_CHANGE_LOCAL_POWER) { 2777 dev_dbg (hub_dev, "power change\n"); 2778 clear_hub_feature(hdev, C_HUB_LOCAL_POWER); 2779 } 2780 if (hubchange & HUB_CHANGE_OVERCURRENT) { 2781 dev_dbg (hub_dev, "overcurrent change\n"); 2782 msleep(500); /* Cool down */ 2783 clear_hub_feature(hdev, C_HUB_OVER_CURRENT); 2784 hub_power_on(hub); 2785 } 2786 } 2787 2788 hub->activating = 0; 2789 2790 loop: 2791 usb_unlock_device(hdev); 2792 usb_put_intf(intf); 2793 2794 } /* end while (1) */ 2795 } 2796 2797 static int hub_thread(void *__unused) 2798 { 2799 /* 2800 * This thread doesn't need any user-level access, 2801 * so get rid of all our resources 2802 */ 2803 2804 daemonize("khubd"); 2805 allow_signal(SIGKILL); 2806 2807 /* Send me a signal to get me die (for debugging) */ 2808 do { 2809 hub_events(); 2810 wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list)); 2811 try_to_freeze(PF_FREEZE); 2812 } while (!signal_pending(current)); 2813 2814 pr_debug ("%s: khubd exiting\n", usbcore_name); 2815 complete_and_exit(&khubd_exited, 0); 2816 } 2817 2818 static struct usb_device_id hub_id_table [] = { 2819 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, 2820 .bDeviceClass = USB_CLASS_HUB}, 2821 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, 2822 .bInterfaceClass = USB_CLASS_HUB}, 2823 { } /* Terminating entry */ 2824 }; 2825 2826 MODULE_DEVICE_TABLE (usb, hub_id_table); 2827 2828 static struct usb_driver hub_driver = { 2829 .owner = THIS_MODULE, 2830 .name = "hub", 2831 .probe = hub_probe, 2832 .disconnect = hub_disconnect, 2833 .suspend = hub_suspend, 2834 .resume = hub_resume, 2835 .ioctl = hub_ioctl, 2836 .id_table = hub_id_table, 2837 }; 2838 2839 int usb_hub_init(void) 2840 { 2841 pid_t pid; 2842 2843 if (usb_register(&hub_driver) < 0) { 2844 printk(KERN_ERR "%s: can't register hub driver\n", 2845 usbcore_name); 2846 return -1; 2847 } 2848 2849 pid = kernel_thread(hub_thread, NULL, CLONE_KERNEL); 2850 if (pid >= 0) { 2851 khubd_pid = pid; 2852 2853 return 0; 2854 } 2855 2856 /* Fall through if kernel_thread failed */ 2857 usb_deregister(&hub_driver); 2858 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); 2859 2860 return -1; 2861 } 2862 2863 void usb_hub_cleanup(void) 2864 { 2865 int ret; 2866 2867 /* Kill the thread */ 2868 ret = kill_proc(khubd_pid, SIGKILL, 1); 2869 2870 wait_for_completion(&khubd_exited); 2871 2872 /* 2873 * Hub resources are freed for us by usb_deregister. It calls 2874 * usb_driver_purge on every device which in turn calls that 2875 * devices disconnect function if it is using this driver. 2876 * The hub_disconnect function takes care of releasing the 2877 * individual hub resources. -greg 2878 */ 2879 usb_deregister(&hub_driver); 2880 } /* usb_hub_cleanup() */ 2881 2882 2883 static int config_descriptors_changed(struct usb_device *udev) 2884 { 2885 unsigned index; 2886 unsigned len = 0; 2887 struct usb_config_descriptor *buf; 2888 2889 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 2890 if (len < le16_to_cpu(udev->config[index].desc.wTotalLength)) 2891 len = le16_to_cpu(udev->config[index].desc.wTotalLength); 2892 } 2893 buf = kmalloc (len, SLAB_KERNEL); 2894 if (buf == NULL) { 2895 dev_err(&udev->dev, "no mem to re-read configs after reset\n"); 2896 /* assume the worst */ 2897 return 1; 2898 } 2899 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { 2900 int length; 2901 int old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); 2902 2903 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, 2904 old_length); 2905 if (length < old_length) { 2906 dev_dbg(&udev->dev, "config index %d, error %d\n", 2907 index, length); 2908 break; 2909 } 2910 if (memcmp (buf, udev->rawdescriptors[index], old_length) 2911 != 0) { 2912 dev_dbg(&udev->dev, "config index %d changed (#%d)\n", 2913 index, buf->bConfigurationValue); 2914 break; 2915 } 2916 } 2917 kfree(buf); 2918 return index != udev->descriptor.bNumConfigurations; 2919 } 2920 2921 /** 2922 * usb_reset_device - perform a USB port reset to reinitialize a device 2923 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) 2924 * 2925 * WARNING - don't reset any device unless drivers for all of its 2926 * interfaces are expecting that reset! Maybe some driver->reset() 2927 * method should eventually help ensure sufficient cooperation. 2928 * 2929 * Do a port reset, reassign the device's address, and establish its 2930 * former operating configuration. If the reset fails, or the device's 2931 * descriptors change from their values before the reset, or the original 2932 * configuration and altsettings cannot be restored, a flag will be set 2933 * telling khubd to pretend the device has been disconnected and then 2934 * re-connected. All drivers will be unbound, and the device will be 2935 * re-enumerated and probed all over again. 2936 * 2937 * Returns 0 if the reset succeeded, -ENODEV if the device has been 2938 * flagged for logical disconnection, or some other negative error code 2939 * if the reset wasn't even attempted. 2940 * 2941 * The caller must own the device lock. For example, it's safe to use 2942 * this from a driver probe() routine after downloading new firmware. 2943 * For calls that might not occur during probe(), drivers should lock 2944 * the device using usb_lock_device_for_reset(). 2945 */ 2946 int usb_reset_device(struct usb_device *udev) 2947 { 2948 struct usb_device *parent_hdev = udev->parent; 2949 struct usb_hub *parent_hub; 2950 struct usb_device_descriptor descriptor = udev->descriptor; 2951 struct usb_hub *hub = NULL; 2952 int i, ret = 0, port1 = -1; 2953 2954 if (udev->state == USB_STATE_NOTATTACHED || 2955 udev->state == USB_STATE_SUSPENDED) { 2956 dev_dbg(&udev->dev, "device reset not allowed in state %d\n", 2957 udev->state); 2958 return -EINVAL; 2959 } 2960 2961 if (!parent_hdev) { 2962 /* this requires hcd-specific logic; see OHCI hc_restart() */ 2963 dev_dbg(&udev->dev, "%s for root hub!\n", __FUNCTION__); 2964 return -EISDIR; 2965 } 2966 2967 for (i = 0; i < parent_hdev->maxchild; i++) 2968 if (parent_hdev->children[i] == udev) { 2969 port1 = i + 1; 2970 break; 2971 } 2972 2973 if (port1 < 0) { 2974 /* If this ever happens, it's very bad */ 2975 dev_err(&udev->dev, "Can't locate device's port!\n"); 2976 return -ENOENT; 2977 } 2978 parent_hub = hdev_to_hub(parent_hdev); 2979 2980 /* If we're resetting an active hub, take some special actions */ 2981 if (udev->actconfig && 2982 udev->actconfig->interface[0]->dev.driver == 2983 &hub_driver.driver && 2984 (hub = hdev_to_hub(udev)) != NULL) { 2985 hub_pre_reset(hub); 2986 } 2987 2988 set_bit(port1, parent_hub->busy_bits); 2989 for (i = 0; i < SET_CONFIG_TRIES; ++i) { 2990 2991 /* ep0 maxpacket size may change; let the HCD know about it. 2992 * Other endpoints will be handled by re-enumeration. */ 2993 ep0_reinit(udev); 2994 ret = hub_port_init(parent_hub, udev, port1, i); 2995 if (ret >= 0) 2996 break; 2997 } 2998 clear_bit(port1, parent_hub->busy_bits); 2999 if (ret < 0) 3000 goto re_enumerate; 3001 3002 /* Device might have changed firmware (DFU or similar) */ 3003 if (memcmp(&udev->descriptor, &descriptor, sizeof descriptor) 3004 || config_descriptors_changed (udev)) { 3005 dev_info(&udev->dev, "device firmware changed\n"); 3006 udev->descriptor = descriptor; /* for disconnect() calls */ 3007 goto re_enumerate; 3008 } 3009 3010 if (!udev->actconfig) 3011 goto done; 3012 3013 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 3014 USB_REQ_SET_CONFIGURATION, 0, 3015 udev->actconfig->desc.bConfigurationValue, 0, 3016 NULL, 0, USB_CTRL_SET_TIMEOUT); 3017 if (ret < 0) { 3018 dev_err(&udev->dev, 3019 "can't restore configuration #%d (error=%d)\n", 3020 udev->actconfig->desc.bConfigurationValue, ret); 3021 goto re_enumerate; 3022 } 3023 usb_set_device_state(udev, USB_STATE_CONFIGURED); 3024 3025 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 3026 struct usb_interface *intf = udev->actconfig->interface[i]; 3027 struct usb_interface_descriptor *desc; 3028 3029 /* set_interface resets host side toggle even 3030 * for altsetting zero. the interface may have no driver. 3031 */ 3032 desc = &intf->cur_altsetting->desc; 3033 ret = usb_set_interface(udev, desc->bInterfaceNumber, 3034 desc->bAlternateSetting); 3035 if (ret < 0) { 3036 dev_err(&udev->dev, "failed to restore interface %d " 3037 "altsetting %d (error=%d)\n", 3038 desc->bInterfaceNumber, 3039 desc->bAlternateSetting, 3040 ret); 3041 goto re_enumerate; 3042 } 3043 } 3044 3045 done: 3046 if (hub) 3047 hub_post_reset(hub); 3048 return 0; 3049 3050 re_enumerate: 3051 hub_port_logical_disconnect(parent_hub, port1); 3052 return -ENODEV; 3053 } 3054