2/*- 3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29/* 30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 31 */ 32 33#include <dev/usb2/include/usb2_defs.h> 34#include <dev/usb2/include/usb2_mfunc.h> 35#include <dev/usb2/include/usb2_error.h> 36#include <dev/usb2/include/usb2_standard.h> 37#include <dev/usb2/include/usb2_ioctl.h> 38 39#define USB_DEBUG_VAR uhub_debug 40 41#include <dev/usb2/core/usb2_core.h> 42#include <dev/usb2/core/usb2_process.h> 43#include <dev/usb2/core/usb2_device.h> 44#include <dev/usb2/core/usb2_request.h> 45#include <dev/usb2/core/usb2_debug.h> 46#include <dev/usb2/core/usb2_hub.h> 47#include <dev/usb2/core/usb2_util.h> 48#include <dev/usb2/core/usb2_busdma.h> 49#include <dev/usb2/core/usb2_transfer.h> 50#include <dev/usb2/core/usb2_dynamic.h> 51 52#include <dev/usb2/controller/usb2_controller.h> 53#include <dev/usb2/controller/usb2_bus.h> 54 55#define UHUB_INTR_INTERVAL 250 /* ms */ 56 57#if USB_DEBUG 58static int uhub_debug = 0; 59 60SYSCTL_NODE(_hw_usb2, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB"); 61SYSCTL_INT(_hw_usb2_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0, 62 "Debug level"); 63#endif 64 65static int usb2_power_timeout = 30; /* seconds */ 66 67SYSCTL_INT(_hw_usb2, OID_AUTO, power_timeout, CTLFLAG_RW, 68 &usb2_power_timeout, 0, "USB power timeout"); 69 70struct uhub_current_state { 71 uint16_t port_change; 72 uint16_t port_status; 73}; 74 75struct uhub_softc { 76 struct uhub_current_state sc_st;/* current state */ 77 device_t sc_dev; /* base device */ 78 struct usb2_device *sc_udev; /* USB device */ 79 struct usb2_xfer *sc_xfer[2]; /* interrupt xfer */ 80 uint8_t sc_flags; 81#define UHUB_FLAG_DID_EXPLORE 0x01 82#define UHUB_FLAG_INTR_STALL 0x02 83 char sc_name[32]; 84}; 85 86#define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol) 87#define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB) 88#define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT) 89 90/* prototypes for type checking: */ 91 92static device_probe_t uhub_probe; 93static device_attach_t uhub_attach; 94static device_detach_t uhub_detach; 95static device_suspend_t uhub_suspend; 96static device_resume_t uhub_resume; 97 98static bus_driver_added_t uhub_driver_added; 99static bus_child_location_str_t uhub_child_location_string; 100static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string; 101 102static usb2_callback_t uhub_intr_callback; 103static usb2_callback_t uhub_intr_clear_stall_callback; 104 105static void usb2_dev_resume_peer(struct usb2_device *udev); 106static void usb2_dev_suspend_peer(struct usb2_device *udev); 107 108static const struct usb2_config uhub_config[2] = { 109 110 [0] = { 111 .type = UE_INTERRUPT, 112 .endpoint = UE_ADDR_ANY, 113 .direction = UE_DIR_ANY, 114 .mh.timeout = 0, 115 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 116 .mh.bufsize = 0, /* use wMaxPacketSize */ 117 .mh.callback = &uhub_intr_callback, 118 .mh.interval = UHUB_INTR_INTERVAL, 119 }, 120 121 [1] = { 122 .type = UE_CONTROL, 123 .endpoint = 0, 124 .direction = UE_DIR_ANY, 125 .mh.timeout = 1000, /* 1 second */ 126 .mh.interval = 50, /* 50ms */ 127 .mh.flags = {}, 128 .mh.bufsize = sizeof(struct usb2_device_request), 129 .mh.callback = &uhub_intr_clear_stall_callback, 130 }, 131}; 132 133/* 134 * driver instance for "hub" connected to "usb" 135 * and "hub" connected to "hub" 136 */ 137static devclass_t uhub_devclass; 138 139static driver_t uhub_driver = 140{ 141 .name = "ushub", 142 .methods = (device_method_t[]){ 143 DEVMETHOD(device_probe, uhub_probe), 144 DEVMETHOD(device_attach, uhub_attach), 145 DEVMETHOD(device_detach, uhub_detach), 146 147 DEVMETHOD(device_suspend, uhub_suspend), 148 DEVMETHOD(device_resume, uhub_resume), 149 DEVMETHOD(device_shutdown, bus_generic_shutdown), 150 151 DEVMETHOD(bus_child_location_str, uhub_child_location_string), 152 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string), 153 DEVMETHOD(bus_driver_added, uhub_driver_added), 154 {0, 0} 155 }, 156 .size = sizeof(struct uhub_softc) 157}; 158 159DRIVER_MODULE(ushub, usbus, uhub_driver, uhub_devclass, 0, 0); 160DRIVER_MODULE(ushub, ushub, uhub_driver, uhub_devclass, NULL, 0); 161 162static void 163uhub_intr_clear_stall_callback(struct usb2_xfer *xfer) 164{ 165 struct uhub_softc *sc = xfer->priv_sc; 166 struct usb2_xfer *xfer_other = sc->sc_xfer[0]; 167 168 if (usb2_clear_stall_callback(xfer, xfer_other)) { 169 DPRINTF("stall cleared\n"); 170 sc->sc_flags &= ~UHUB_FLAG_INTR_STALL; 171 usb2_transfer_start(xfer_other); 172 } 173} 174 175static void 176uhub_intr_callback(struct usb2_xfer *xfer) 177{ 178 struct uhub_softc *sc = xfer->priv_sc; 179 180 switch (USB_GET_STATE(xfer)) { 181 case USB_ST_TRANSFERRED: 182 DPRINTFN(2, "\n"); 183 /* 184 * This is an indication that some port 185 * has changed status. Notify the bus 186 * event handler thread that we need 187 * to be explored again: 188 */ 189 usb2_needs_explore(sc->sc_udev->bus, 0); 190 191 case USB_ST_SETUP: 192 if (sc->sc_flags & UHUB_FLAG_INTR_STALL) { 193 usb2_transfer_start(sc->sc_xfer[1]); 194 } else { 195 xfer->frlengths[0] = xfer->max_data_length; 196 usb2_start_hardware(xfer); 197 } 198 return; 199 200 default: /* Error */ 201 if (xfer->error != USB_ERR_CANCELLED) { 202 /* start clear stall */ 203 sc->sc_flags |= UHUB_FLAG_INTR_STALL; 204 usb2_transfer_start(sc->sc_xfer[1]); 205 } 206 return; 207 } 208} 209 210/*------------------------------------------------------------------------* 211 * uhub_explore_sub - subroutine 212 * 213 * Return values: 214 * 0: Success 215 * Else: A control transaction failed 216 *------------------------------------------------------------------------*/ 217static usb2_error_t 218uhub_explore_sub(struct uhub_softc *sc, struct usb2_port *up) 219{ 220 struct usb2_bus *bus; 221 struct usb2_device *child; 222 uint8_t refcount; 223 usb2_error_t err; 224 225 bus = sc->sc_udev->bus; 226 err = 0; 227 228 /* get driver added refcount from USB bus */ 229 refcount = bus->driver_added_refcount; 230 231 /* get device assosiated with the given port */ 232 child = usb2_bus_port_get_device(bus, up); 233 if (child == NULL) { 234 /* nothing to do */ 235 goto done; 236 } 237 /* check if probe and attach should be done */ 238 239 if (child->driver_added_refcount != refcount) { 240 child->driver_added_refcount = refcount; 241 err = usb2_probe_and_attach(child, 242 USB_IFACE_INDEX_ANY); 243 if (err) { 244 goto done; 245 } 246 } 247 /* start control transfer, if device mode */ 248 249 if (child->flags.usb2_mode == USB_MODE_DEVICE) { 250 usb2_default_transfer_setup(child); 251 } 252 /* if a HUB becomes present, do a recursive HUB explore */ 253 254 if (child->hub) { 255 err = (child->hub->explore) (child); 256 } 257done: 258 return (err); 259} 260 261/*------------------------------------------------------------------------* 262 * uhub_read_port_status - factored out code 263 *------------------------------------------------------------------------*/ 264static usb2_error_t 265uhub_read_port_status(struct uhub_softc *sc, uint8_t portno) 266{ 267 struct usb2_port_status ps; 268 usb2_error_t err; 269 270 err = usb2_req_get_port_status( 271 sc->sc_udev, &Giant, &ps, portno); 272 273 /* update status regardless of error */ 274 275 sc->sc_st.port_status = UGETW(ps.wPortStatus); 276 sc->sc_st.port_change = UGETW(ps.wPortChange); 277 278 /* debugging print */ 279 280 DPRINTFN(4, "port %d, wPortStatus=0x%04x, " 281 "wPortChange=0x%04x, err=%s\n", 282 portno, sc->sc_st.port_status, 283 sc->sc_st.port_change, usb2_errstr(err)); 284 return (err); 285} 286 287/*------------------------------------------------------------------------* 288 * uhub_reattach_port 289 * 290 * Returns: 291 * 0: Success 292 * Else: A control transaction failed 293 *------------------------------------------------------------------------*/ 294static usb2_error_t 295uhub_reattach_port(struct uhub_softc *sc, uint8_t portno) 296{ 297 struct usb2_device *child; 298 struct usb2_device *udev; 299 usb2_error_t err; 300 uint8_t timeout; 301 uint8_t speed; 302 uint8_t usb2_mode; 303 304 DPRINTF("reattaching port %d\n", portno); 305 306 err = 0; 307 timeout = 0; 308 udev = sc->sc_udev; 309 child = usb2_bus_port_get_device(udev->bus, 310 udev->hub->ports + portno - 1); 311 312repeat: 313 314 /* first clear the port connection change bit */ 315 316 err = usb2_req_clear_port_feature(udev, &Giant, 317 portno, UHF_C_PORT_CONNECTION); 318 319 if (err) { 320 goto error; 321 } 322 /* detach any existing devices */ 323 324 if (child) { 325 usb2_detach_device(child, USB_IFACE_INDEX_ANY, 1); 326 usb2_free_device(child); 327 child = NULL; 328 } 329 /* get fresh status */ 330 331 err = uhub_read_port_status(sc, portno); 332 if (err) { 333 goto error; 334 } 335 /* check if nothing is connected to the port */ 336 337 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) { 338 goto error; 339 } 340 /* check if there is no power on the port and print a warning */ 341 342 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) { 343 DPRINTF("WARNING: strange, connected port %d " 344 "has no power\n", portno); 345 } 346 /* check if the device is in Host Mode */ 347 348 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) { 349 350 DPRINTF("Port %d is in Host Mode\n", portno); 351 352 if (sc->sc_st.port_status & UPS_SUSPEND) { 353 DPRINTF("Port %d was still " 354 "suspended, clearing.\n", portno); 355 err = usb2_req_clear_port_feature(sc->sc_udev, 356 &Giant, portno, UHF_PORT_SUSPEND); 357 } 358 /* USB Host Mode */ 359 360 /* wait for maximum device power up time */ 361 362 usb2_pause_mtx(&Giant, USB_PORT_POWERUP_DELAY); 363 364 /* reset port, which implies enabling it */ 365 366 err = usb2_req_reset_port(udev, &Giant, portno); 367 368 if (err) { 369 DPRINTFN(0, "port %d reset " 370 "failed, error=%s\n", 371 portno, usb2_errstr(err)); 372 goto error; 373 } 374 /* get port status again, it might have changed during reset */ 375 376 err = uhub_read_port_status(sc, portno); 377 if (err) { 378 goto error; 379 } 380 /* check if something changed during port reset */ 381 382 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) || 383 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) { 384 if (timeout) { 385 DPRINTFN(0, "giving up port reset " 386 "- device vanished!\n"); 387 goto error; 388 } 389 timeout = 1; 390 goto repeat; 391 } 392 } else { 393 DPRINTF("Port %d is in Device Mode\n", portno); 394 } 395 396 /* 397 * Figure out the device speed 398 */ 399 speed = 400 (sc->sc_st.port_status & UPS_HIGH_SPEED) ? USB_SPEED_HIGH : 401 (sc->sc_st.port_status & UPS_LOW_SPEED) ? USB_SPEED_LOW : USB_SPEED_FULL; 402 403 /* 404 * Figure out the device mode 405 * 406 * NOTE: This part is currently FreeBSD specific. 407 */ 408 usb2_mode = 409 (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE) ? 410 USB_MODE_DEVICE : USB_MODE_HOST; 411 412 /* need to create a new child */ 413 414 child = usb2_alloc_device(sc->sc_dev, udev->bus, udev, 415 udev->depth + 1, portno - 1, portno, speed, usb2_mode); 416 if (child == NULL) { 417 DPRINTFN(0, "could not allocate new device!\n"); 418 goto error; 419 } 420 return (0); /* success */ 421 422error: 423 if (child) { 424 usb2_detach_device(child, USB_IFACE_INDEX_ANY, 1); 425 usb2_free_device(child); 426 child = NULL; 427 } 428 if (err == 0) { 429 if (sc->sc_st.port_status & UPS_PORT_ENABLED) { 430 err = usb2_req_clear_port_feature( 431 sc->sc_udev, &Giant, 432 portno, UHF_PORT_ENABLE); 433 } 434 } 435 if (err) { 436 DPRINTFN(0, "device problem (%s), " 437 "disabling port %d\n", usb2_errstr(err), portno); 438 } 439 return (err); 440} 441 442/*------------------------------------------------------------------------* 443 * uhub_suspend_resume_port 444 * 445 * Returns: 446 * 0: Success 447 * Else: A control transaction failed 448 *------------------------------------------------------------------------*/ 449static usb2_error_t 450uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno) 451{ 452 struct usb2_device *child; 453 struct usb2_device *udev; 454 uint8_t is_suspend; 455 usb2_error_t err; 456 457 DPRINTF("port %d\n", portno); 458 459 udev = sc->sc_udev; 460 child = usb2_bus_port_get_device(udev->bus, 461 udev->hub->ports + portno - 1); 462 463 /* first clear the port suspend change bit */ 464 465 err = usb2_req_clear_port_feature(udev, &Giant, 466 portno, UHF_C_PORT_SUSPEND); 467 if (err) { 468 DPRINTF("clearing suspend failed.\n"); 469 goto done; 470 } 471 /* get fresh status */ 472 473 err = uhub_read_port_status(sc, portno); 474 if (err) { 475 DPRINTF("reading port status failed.\n"); 476 goto done; 477 } 478 /* get current state */ 479 480 if (sc->sc_st.port_status & UPS_SUSPEND) { 481 is_suspend = 1; 482 } else { 483 is_suspend = 0; 484 } 485 486 DPRINTF("suspended=%u\n", is_suspend); 487 488 /* do the suspend or resume */ 489 490 if (child) { 491 /* 492 * This code handle two cases: 1) Host Mode - we can only 493 * receive resume here 2) Device Mode - we can receive 494 * suspend and resume here 495 */ 496 if (is_suspend == 0) 497 usb2_dev_resume_peer(child); 498 else if (child->flags.usb2_mode == USB_MODE_DEVICE) 499 usb2_dev_suspend_peer(child); 500 } 501done: 502 return (err); 503} 504 505/*------------------------------------------------------------------------* 506 * uhub_explore 507 * 508 * Returns: 509 * 0: Success 510 * Else: Failure 511 *------------------------------------------------------------------------*/ 512static usb2_error_t 513uhub_explore(struct usb2_device *udev) 514{ 515 struct usb2_hub *hub; 516 struct uhub_softc *sc; 517 struct usb2_port *up; 518 usb2_error_t err; 519 uint8_t portno; 520 uint8_t x; 521 522 hub = udev->hub; 523 sc = hub->hubsoftc; 524 525 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address); 526 527 /* ignore hubs that are too deep */ 528 if (udev->depth > USB_HUB_MAX_DEPTH) { 529 return (USB_ERR_TOO_DEEP); 530 } 531 if (udev->pwr_save.suspended) { 532 /* need to wait until the child signals resume */ 533 DPRINTF("Device is suspended!\n"); 534 return (0); 535 } 536 for (x = 0; x != hub->nports; x++) { 537 up = hub->ports + x; 538 portno = x + 1; 539 540 err = uhub_read_port_status(sc, portno); 541 if (err) { 542 /* most likely the HUB is gone */ 543 break; 544 } 545 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) { 546 DPRINTF("Overcurrent on port %u.\n", portno); 547 err = usb2_req_clear_port_feature( 548 udev, &Giant, portno, UHF_C_PORT_OVER_CURRENT); 549 if (err) { 550 /* most likely the HUB is gone */ 551 break; 552 } 553 } 554 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) { 555 /* 556 * Fake a connect status change so that the 557 * status gets checked initially! 558 */ 559 sc->sc_st.port_change |= 560 UPS_C_CONNECT_STATUS; 561 } 562 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) { 563 err = usb2_req_clear_port_feature( 564 udev, &Giant, portno, UHF_C_PORT_ENABLE); 565 if (err) { 566 /* most likely the HUB is gone */ 567 break; 568 } 569 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) { 570 /* 571 * Ignore the port error if the device 572 * has vanished ! 573 */ 574 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) { 575 DPRINTFN(0, "illegal enable change, " 576 "port %d\n", portno); 577 } else { 578 579 if (up->restartcnt == USB_RESTART_MAX) { 580 /* XXX could try another speed ? */ 581 DPRINTFN(0, "port error, giving up " 582 "port %d\n", portno); 583 } else { 584 sc->sc_st.port_change |= 585 UPS_C_CONNECT_STATUS; 586 up->restartcnt++; 587 } 588 } 589 } 590 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) { 591 err = uhub_reattach_port(sc, portno); 592 if (err) { 593 /* most likely the HUB is gone */ 594 break; 595 } 596 } 597 if (sc->sc_st.port_change & UPS_C_SUSPEND) { 598 err = uhub_suspend_resume_port(sc, portno); 599 if (err) { 600 /* most likely the HUB is gone */ 601 break; 602 } 603 } 604 err = uhub_explore_sub(sc, up); 605 if (err) { 606 /* no device(s) present */ 607 continue; 608 } 609 /* explore succeeded - reset restart counter */ 610 up->restartcnt = 0; 611 } 612 613 /* initial status checked */ 614 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE; 615 616 /* return success */ 617 return (USB_ERR_NORMAL_COMPLETION); 618} 619 620static int 621uhub_probe(device_t dev) 622{ 623 struct usb2_attach_arg *uaa = device_get_ivars(dev); 624 625 if (uaa->usb2_mode != USB_MODE_HOST) { 626 return (ENXIO); 627 } 628 /* 629 * The subclass for USB HUBs is ignored because it is 0 for 630 * some and 1 for others. 631 */ 632 if ((uaa->info.bConfigIndex == 0) && 633 (uaa->info.bDeviceClass == UDCLASS_HUB)) { 634 return (0); 635 } 636 return (ENXIO); 637} 638 639static int 640uhub_attach(device_t dev) 641{ 642 struct uhub_softc *sc = device_get_softc(dev); 643 struct usb2_attach_arg *uaa = device_get_ivars(dev); 644 struct usb2_device *udev = uaa->device; 645 struct usb2_device *parent_hub = udev->parent_hub; 646 struct usb2_hub *hub; 647 struct usb2_hub_descriptor hubdesc; 648 uint16_t pwrdly; 649 uint8_t x; 650 uint8_t nports; 651 uint8_t portno; 652 uint8_t removable; 653 uint8_t iface_index; 654 usb2_error_t err; 655 656 if (sc == NULL) { 657 return (ENOMEM); 658 } 659 sc->sc_udev = udev; 660 sc->sc_dev = dev; 661 662 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 663 device_get_nameunit(dev)); 664 665 device_set_usb2_desc(dev); 666 667 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, " 668 "parent->selfpowered=%d\n", 669 udev->depth, 670 udev->flags.self_powered, 671 parent_hub, 672 parent_hub ? 673 parent_hub->flags.self_powered : 0); 674 675 if (udev->depth > USB_HUB_MAX_DEPTH) { 676 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored!\n", 677 USB_HUB_MAX_DEPTH); 678 goto error; 679 } 680 if (!udev->flags.self_powered && parent_hub && 681 (!parent_hub->flags.self_powered)) { 682 DPRINTFN(0, "bus powered HUB connected to " 683 "bus powered HUB. HUB ignored!\n"); 684 goto error; 685 } 686 /* get HUB descriptor */ 687 688 DPRINTFN(2, "getting HUB descriptor\n"); 689 690 /* assuming that there is one port */ 691 err = usb2_req_get_hub_descriptor(udev, &Giant, &hubdesc, 1); 692 693 nports = hubdesc.bNbrPorts; 694 695 if (!err && (nports >= 8)) { 696 /* get complete HUB descriptor */ 697 err = usb2_req_get_hub_descriptor(udev, &Giant, &hubdesc, nports); 698 } 699 if (err) { 700 DPRINTFN(0, "getting hub descriptor failed," 701 "error=%s\n", usb2_errstr(err)); 702 goto error; 703 } 704 if (hubdesc.bNbrPorts != nports) { 705 DPRINTFN(0, "number of ports changed!\n"); 706 goto error; 707 } 708 if (nports == 0) { 709 DPRINTFN(0, "portless HUB!\n"); 710 goto error; 711 } 712 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports), 713 M_USBDEV, M_WAITOK | M_ZERO); 714 715 if (hub == NULL) { 716 goto error; 717 } 718 udev->hub = hub; 719 720 /* init FULL-speed ISOCHRONOUS schedule */ 721 usb2_fs_isoc_schedule_init_all(hub->fs_isoc_schedule); 722 723 /* initialize HUB structure */ 724 hub->hubsoftc = sc; 725 hub->explore = &uhub_explore; 726 hub->nports = hubdesc.bNbrPorts; 727 hub->hubudev = udev; 728 729 /* if self powered hub, give ports maximum current */ 730 if (udev->flags.self_powered) { 731 hub->portpower = USB_MAX_POWER; 732 } else { 733 hub->portpower = USB_MIN_POWER; 734 } 735 736 /* set up interrupt pipe */ 737 iface_index = 0; 738 err = usb2_transfer_setup(udev, &iface_index, sc->sc_xfer, 739 uhub_config, 2, sc, &Giant); 740 if (err) { 741 DPRINTFN(0, "cannot setup interrupt transfer, " 742 "errstr=%s!\n", usb2_errstr(err)); 743 goto error; 744 } 745 /* wait with power off for a while */ 746 usb2_pause_mtx(&Giant, USB_POWER_DOWN_TIME); 747 748 /* 749 * To have the best chance of success we do things in the exact same 750 * order as Windoze98. This should not be necessary, but some 751 * devices do not follow the USB specs to the letter. 752 * 753 * These are the events on the bus when a hub is attached: 754 * Get device and config descriptors (see attach code) 755 * Get hub descriptor (see above) 756 * For all ports 757 * turn on power 758 * wait for power to become stable 759 * (all below happens in explore code) 760 * For all ports 761 * clear C_PORT_CONNECTION 762 * For all ports 763 * get port status 764 * if device connected 765 * wait 100 ms 766 * turn on reset 767 * wait 768 * clear C_PORT_RESET 769 * get port status 770 * proceed with device attachment 771 */ 772 773 /* XXX should check for none, individual, or ganged power? */ 774 775 removable = 0; 776 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) + 777 USB_EXTRA_POWER_UP_TIME); 778 779 for (x = 0; x != nports; x++) { 780 /* set up data structures */ 781 struct usb2_port *up = hub->ports + x; 782 783 up->device_index = 0; 784 up->restartcnt = 0; 785 portno = x + 1; 786 787 /* check if port is removable */ 788 if (!UHD_NOT_REMOV(&hubdesc, portno)) { 789 removable++; 790 } 791 if (!err) { 792 /* turn the power on */ 793 err = usb2_req_set_port_feature(udev, &Giant, 794 portno, UHF_PORT_POWER); 795 } 796 if (err) { 797 DPRINTFN(0, "port %d power on failed, %s\n", 798 portno, usb2_errstr(err)); 799 } 800 DPRINTF("turn on port %d power\n", 801 portno); 802 803 /* wait for stable power */ 804 usb2_pause_mtx(&Giant, pwrdly); 805 } 806 807 device_printf(dev, "%d port%s with %d " 808 "removable, %s powered\n", nports, (nports != 1) ? "s" : "", 809 removable, udev->flags.self_powered ? "self" : "bus"); 810 811 /* start the interrupt endpoint */ 812 813 USB_XFER_LOCK(sc->sc_xfer[0]); 814 usb2_transfer_start(sc->sc_xfer[0]); 815 USB_XFER_UNLOCK(sc->sc_xfer[0]); 816 817 /* Enable automatic power save on all USB HUBs */ 818 819 usb2_set_power_mode(udev, USB_POWER_MODE_SAVE); 820 821 return (0); 822 823error: 824 usb2_transfer_unsetup(sc->sc_xfer, 2); 825 826 if (udev->hub) { 827 free(udev->hub, M_USBDEV); 828 udev->hub = NULL; 829 } 830 return (ENXIO); 831} 832 833/* 834 * Called from process context when the hub is gone. 835 * Detach all devices on active ports. 836 */ 837static int 838uhub_detach(device_t dev) 839{ 840 struct uhub_softc *sc = device_get_softc(dev); 841 struct usb2_hub *hub = sc->sc_udev->hub; 842 struct usb2_device *child; 843 uint8_t x; 844 845 /* detach all children first */ 846 bus_generic_detach(dev); 847 848 if (hub == NULL) { /* must be partially working */ 849 return (0); 850 } 851 for (x = 0; x != hub->nports; x++) { 852 853 child = usb2_bus_port_get_device(sc->sc_udev->bus, hub->ports + x); 854 855 if (child == NULL) { 856 continue; 857 } 858 /* 859 * Subdevices are not freed, because the caller of 860 * uhub_detach() will do that. 861 */ 862 usb2_detach_device(child, USB_IFACE_INDEX_ANY, 0); 863 usb2_free_device(child); 864 child = NULL; 865 } 866 867 usb2_transfer_unsetup(sc->sc_xfer, 2); 868 869 free(hub, M_USBDEV); 870 sc->sc_udev->hub = NULL; 871 return (0); 872} 873 874static int 875uhub_suspend(device_t dev) 876{ 877 DPRINTF("\n"); 878 /* Sub-devices are not suspended here! */ 879 return (0); 880} 881 882static int 883uhub_resume(device_t dev) 884{ 885 DPRINTF("\n"); 886 /* Sub-devices are not resumed here! */ 887 return (0); 888} 889 890static void 891uhub_driver_added(device_t dev, driver_t *driver) 892{ 893 usb2_needs_explore_all(); 894} 895 896struct hub_result { 897 struct usb2_device *udev; 898 uint8_t portno; 899 uint8_t iface_index; 900}; 901 902static void 903uhub_find_iface_index(struct usb2_hub *hub, device_t child, 904 struct hub_result *res) 905{ 906 struct usb2_interface *iface; 907 struct usb2_device *udev; 908 uint8_t nports; 909 uint8_t x; 910 uint8_t i; 911 912 nports = hub->nports; 913 for (x = 0; x != nports; x++) { 914 udev = usb2_bus_port_get_device(hub->hubudev->bus, 915 hub->ports + x); 916 if (!udev) { 917 continue; 918 } 919 for (i = 0; i != USB_IFACE_MAX; i++) { 920 iface = usb2_get_iface(udev, i); 921 if (iface && 922 (iface->subdev == child)) { 923 res->iface_index = i; 924 res->udev = udev; 925 res->portno = x + 1; 926 return; 927 } 928 } 929 } 930 res->iface_index = 0; 931 res->udev = NULL; 932 res->portno = 0; 933} 934 935static int 936uhub_child_location_string(device_t parent, device_t child, 937 char *buf, size_t buflen) 938{ 939 struct uhub_softc *sc = device_get_softc(parent); 940 struct usb2_hub *hub = sc->sc_udev->hub; 941 struct hub_result res; 942 943 mtx_lock(&Giant); 944 uhub_find_iface_index(hub, child, &res); 945 if (!res.udev) { 946 DPRINTF("device not on hub\n"); 947 if (buflen) { 948 buf[0] = '\0'; 949 } 950 goto done; 951 } 952 snprintf(buf, buflen, "port=%u interface=%u", 953 res.portno, res.iface_index); 954done: 955 mtx_unlock(&Giant); 956 957 return (0); 958} 959 960static int 961uhub_child_pnpinfo_string(device_t parent, device_t child, 962 char *buf, size_t buflen) 963{ 964 struct uhub_softc *sc = device_get_softc(parent); 965 struct usb2_hub *hub = sc->sc_udev->hub; 966 struct usb2_interface *iface; 967 struct hub_result res; 968 969 mtx_lock(&Giant); 970 uhub_find_iface_index(hub, child, &res); 971 if (!res.udev) { 972 DPRINTF("device not on hub\n"); 973 if (buflen) { 974 buf[0] = '\0'; 975 } 976 goto done; 977 } 978 iface = usb2_get_iface(res.udev, res.iface_index); 979 if (iface && iface->idesc) { 980 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x " 981 "devclass=0x%02x devsubclass=0x%02x " 982 "sernum=\"%s\" " 983 "intclass=0x%02x intsubclass=0x%02x", 984 UGETW(res.udev->ddesc.idVendor), 985 UGETW(res.udev->ddesc.idProduct), 986 res.udev->ddesc.bDeviceClass, 987 res.udev->ddesc.bDeviceSubClass, 988 res.udev->serial, 989 iface->idesc->bInterfaceClass, 990 iface->idesc->bInterfaceSubClass); 991 } else { 992 if (buflen) { 993 buf[0] = '\0'; 994 } 995 goto done; 996 } 997done: 998 mtx_unlock(&Giant); 999 1000 return (0); 1001} 1002 1003/* 1004 * The USB Transaction Translator: 1005 * =============================== 1006 * 1007 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed 1008 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT 1009 * USB transfers. To utilize bandwidth dynamically the "scatter and 1010 * gather" principle must be applied. This means that bandwidth must 1011 * be divided into equal parts of bandwidth. With regard to USB all 1012 * data is transferred in smaller packets with length 1013 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is 1014 * not a constant! 1015 * 1016 * The bandwidth scheduler which I have implemented will simply pack 1017 * the USB transfers back to back until there is no more space in the 1018 * schedule. Out of the 8 microframes which the USB 2.0 standard 1019 * provides, only 6 are available for non-HIGH-speed devices. I have 1020 * reserved the first 4 microframes for ISOCHRONOUS transfers. The 1021 * last 2 microframes I have reserved for INTERRUPT transfers. Without 1022 * this division, it is very difficult to allocate and free bandwidth 1023 * dynamically. 1024 * 1025 * NOTE about the Transaction Translator in USB HUBs: 1026 * 1027 * USB HUBs have a very simple Transaction Translator, that will 1028 * simply pipeline all the SPLIT transactions. That means that the 1029 * transactions will be executed in the order they are queued! 1030 * 1031 */ 1032 1033/*------------------------------------------------------------------------* 1034 * usb2_intr_find_best_slot 1035 * 1036 * Return value: 1037 * The best Transaction Translation slot for an interrupt endpoint. 1038 *------------------------------------------------------------------------*/ 1039static uint8_t 1040usb2_intr_find_best_slot(uint32_t *ptr, uint8_t start, uint8_t end) 1041{ 1042 uint32_t max = 0xffffffff; 1043 uint8_t x; 1044 uint8_t y; 1045 1046 y = 0; 1047 1048 /* find the last slot with lesser used bandwidth */ 1049 1050 for (x = start; x < end; x++) { 1051 if (max >= ptr[x]) { 1052 max = ptr[x]; 1053 y = x; 1054 } 1055 } 1056 return (y); 1057} 1058 1059/*------------------------------------------------------------------------* 1060 * usb2_intr_schedule_adjust 1061 * 1062 * This function will update the bandwith usage for the microframe 1063 * having index "slot" by "len" bytes. "len" can be negative. If the 1064 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX" 1065 * the "slot" argument will be replaced by the slot having least used 1066 * bandwidth. 1067 * 1068 * Returns: 1069 * The slot on which the bandwidth update was done. 1070 *------------------------------------------------------------------------*/ 1071uint8_t 1072usb2_intr_schedule_adjust(struct usb2_device *udev, int16_t len, uint8_t slot) 1073{ 1074 struct usb2_bus *bus = udev->bus; 1075 struct usb2_hub *hub; 1076 1077 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 1078 1079 if (usb2_get_speed(udev) == USB_SPEED_HIGH) { 1080 if (slot >= USB_HS_MICRO_FRAMES_MAX) { 1081 slot = usb2_intr_find_best_slot(bus->uframe_usage, 0, 1082 USB_HS_MICRO_FRAMES_MAX); 1083 } 1084 bus->uframe_usage[slot] += len; 1085 } else { 1086 if (usb2_get_speed(udev) == USB_SPEED_LOW) { 1087 len *= 8; 1088 } 1089 /* 1090 * The Host Controller Driver should have 1091 * performed checks so that the lookup 1092 * below does not result in a NULL pointer 1093 * access. 1094 */ 1095 1096 hub = bus->devices[udev->hs_hub_addr]->hub; 1097 if (slot >= USB_HS_MICRO_FRAMES_MAX) { 1098 slot = usb2_intr_find_best_slot(hub->uframe_usage, 1099 USB_FS_ISOC_UFRAME_MAX, 6); 1100 } 1101 hub->uframe_usage[slot] += len; 1102 bus->uframe_usage[slot] += len; 1103 } 1104 return (slot); 1105} 1106 1107/*------------------------------------------------------------------------* 1108 * usb2_fs_isoc_schedule_init_sub 1109 * 1110 * This function initialises an USB FULL speed isochronous schedule 1111 * entry. 1112 *------------------------------------------------------------------------*/ 1113static void 1114usb2_fs_isoc_schedule_init_sub(struct usb2_fs_isoc_schedule *fss) 1115{ 1116 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX * 1117 USB_FS_BYTES_PER_HS_UFRAME); 1118 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME); 1119 fss->frame_slot = 0; 1120} 1121 1122/*------------------------------------------------------------------------* 1123 * usb2_fs_isoc_schedule_init_all 1124 * 1125 * This function will reset the complete USB FULL speed isochronous 1126 * bandwidth schedule. 1127 *------------------------------------------------------------------------*/ 1128void 1129usb2_fs_isoc_schedule_init_all(struct usb2_fs_isoc_schedule *fss) 1130{ 1131 struct usb2_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX; 1132 1133 while (fss != fss_end) { 1134 usb2_fs_isoc_schedule_init_sub(fss); 1135 fss++; 1136 } 1137} 1138 1139/*------------------------------------------------------------------------* 1140 * usb2_isoc_time_expand 1141 * 1142 * This function will expand the time counter from 7-bit to 16-bit. 1143 * 1144 * Returns: 1145 * 16-bit isochronous time counter. 1146 *------------------------------------------------------------------------*/ 1147uint16_t 1148usb2_isoc_time_expand(struct usb2_bus *bus, uint16_t isoc_time_curr) 1149{ 1150 uint16_t rem; 1151 1152 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 1153 1154 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1); 1155 1156 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1); 1157 1158 if (isoc_time_curr < rem) { 1159 /* the time counter wrapped around */ 1160 bus->isoc_time_last += USB_ISOC_TIME_MAX; 1161 } 1162 /* update the remainder */ 1163 1164 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1); 1165 bus->isoc_time_last |= isoc_time_curr; 1166 1167 return (bus->isoc_time_last); 1168} 1169 1170/*------------------------------------------------------------------------* 1171 * usb2_fs_isoc_schedule_isoc_time_expand 1172 * 1173 * This function does multiple things. First of all it will expand the 1174 * passed isochronous time, which is the return value. Then it will 1175 * store where the current FULL speed isochronous schedule is 1176 * positioned in time and where the end is. See "pp_start" and 1177 * "pp_end" arguments. 1178 * 1179 * Returns: 1180 * Expanded version of "isoc_time". 1181 * 1182 * NOTE: This function depends on being called regularly with 1183 * intervals less than "USB_ISOC_TIME_MAX". 1184 *------------------------------------------------------------------------*/ 1185uint16_t 1186usb2_fs_isoc_schedule_isoc_time_expand(struct usb2_device *udev, 1187 struct usb2_fs_isoc_schedule **pp_start, 1188 struct usb2_fs_isoc_schedule **pp_end, 1189 uint16_t isoc_time) 1190{ 1191 struct usb2_fs_isoc_schedule *fss_end; 1192 struct usb2_fs_isoc_schedule *fss_a; 1193 struct usb2_fs_isoc_schedule *fss_b; 1194 struct usb2_hub *hs_hub; 1195 1196 isoc_time = usb2_isoc_time_expand(udev->bus, isoc_time); 1197 1198 hs_hub = udev->bus->devices[udev->hs_hub_addr]->hub; 1199 1200 if (hs_hub != NULL) { 1201 1202 fss_a = hs_hub->fs_isoc_schedule + 1203 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX); 1204 1205 hs_hub->isoc_last_time = isoc_time; 1206 1207 fss_b = hs_hub->fs_isoc_schedule + 1208 (isoc_time % USB_ISOC_TIME_MAX); 1209 1210 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX; 1211 1212 *pp_start = hs_hub->fs_isoc_schedule; 1213 *pp_end = fss_end; 1214 1215 while (fss_a != fss_b) { 1216 if (fss_a == fss_end) { 1217 fss_a = hs_hub->fs_isoc_schedule; 1218 continue; 1219 } 1220 usb2_fs_isoc_schedule_init_sub(fss_a); 1221 fss_a++; 1222 } 1223 1224 } else { 1225 1226 *pp_start = NULL; 1227 *pp_end = NULL; 1228 } 1229 return (isoc_time); 1230} 1231 1232/*------------------------------------------------------------------------* 1233 * usb2_fs_isoc_schedule_alloc 1234 * 1235 * This function will allocate bandwidth for an isochronous FULL speed 1236 * transaction in the FULL speed schedule. The microframe slot where 1237 * the transaction should be started is stored in the byte pointed to 1238 * by "pstart". The "len" argument specifies the length of the 1239 * transaction in bytes. 1240 * 1241 * Returns: 1242 * 0: Success 1243 * Else: Error 1244 *------------------------------------------------------------------------*/ 1245uint8_t 1246usb2_fs_isoc_schedule_alloc(struct usb2_fs_isoc_schedule *fss, 1247 uint8_t *pstart, uint16_t len) 1248{ 1249 uint8_t slot = fss->frame_slot; 1250 1251 /* Compute overhead and bit-stuffing */ 1252 1253 len += 8; 1254 1255 len *= 7; 1256 len /= 6; 1257 1258 if (len > fss->total_bytes) { 1259 *pstart = 0; /* set some dummy value */ 1260 return (1); /* error */ 1261 } 1262 if (len > 0) { 1263 1264 fss->total_bytes -= len; 1265 1266 while (len >= fss->frame_bytes) { 1267 len -= fss->frame_bytes; 1268 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME; 1269 fss->frame_slot++; 1270 } 1271 1272 fss->frame_bytes -= len; 1273 } 1274 *pstart = slot; 1275 return (0); /* success */ 1276} 1277 1278/*------------------------------------------------------------------------* 1279 * usb2_bus_port_get_device 1280 * 1281 * This function is NULL safe. 1282 *------------------------------------------------------------------------*/ 1283struct usb2_device * 1284usb2_bus_port_get_device(struct usb2_bus *bus, struct usb2_port *up) 1285{ 1286 if ((bus == NULL) || (up == NULL)) { 1287 /* be NULL safe */ 1288 return (NULL); 1289 } 1290 if (up->device_index == 0) { 1291 /* nothing to do */ 1292 return (NULL); 1293 } 1294 return (bus->devices[up->device_index]); 1295} 1296 1297/*------------------------------------------------------------------------* 1298 * usb2_bus_port_set_device 1299 * 1300 * This function is NULL safe. 1301 *------------------------------------------------------------------------*/ 1302void 1303usb2_bus_port_set_device(struct usb2_bus *bus, struct usb2_port *up, 1304 struct usb2_device *udev, uint8_t device_index) 1305{ 1306 if (bus == NULL) { 1307 /* be NULL safe */ 1308 return; 1309 } 1310 /* 1311 * There is only one case where we don't 1312 * have an USB port, and that is the Root Hub! 1313 */ 1314 if (up) { 1315 if (udev) { 1316 up->device_index = device_index; 1317 } else { 1318 device_index = up->device_index; 1319 up->device_index = 0; 1320 } 1321 } 1322 /* 1323 * Make relationships to our new device 1324 */ 1325 if (device_index != 0) { 1326 mtx_lock(&usb2_ref_lock); 1327 bus->devices[device_index] = udev; 1328 mtx_unlock(&usb2_ref_lock); 1329 } 1330 /* 1331 * Debug print 1332 */ 1333 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev); 1334} 1335 1336/*------------------------------------------------------------------------* 1337 * usb2_needs_explore 1338 * 1339 * This functions is called when the USB event thread needs to run. 1340 *------------------------------------------------------------------------*/ 1341void 1342usb2_needs_explore(struct usb2_bus *bus, uint8_t do_probe) 1343{ 1344 DPRINTF("\n"); 1345 1346 if (bus == NULL) { 1347 DPRINTF("No bus pointer!\n"); 1348 return; 1349 } 1350 USB_BUS_LOCK(bus); 1351 if (do_probe) { 1352 bus->do_probe = 1; 1353 } 1354 if (usb2_proc_msignal(&bus->explore_proc, 1355 &bus->explore_msg[0], &bus->explore_msg[1])) { 1356 /* ignore */ 1357 } 1358 USB_BUS_UNLOCK(bus); 1359} 1360 1361/*------------------------------------------------------------------------* 1362 * usb2_needs_explore_all 1363 * 1364 * This function is called whenever a new driver is loaded and will 1365 * cause that all USB busses are re-explored. 1366 *------------------------------------------------------------------------*/ 1367void 1368usb2_needs_explore_all(void) 1369{ 1370 struct usb2_bus *bus; 1371 devclass_t dc; 1372 device_t dev; 1373 int max; 1374 1375 DPRINTFN(3, "\n"); 1376 1377 dc = usb2_devclass_ptr; 1378 if (dc == NULL) { 1379 DPRINTFN(0, "no devclass\n"); 1380 return; 1381 } 1382 /* 1383 * Explore all USB busses in parallell. 1384 */ 1385 max = devclass_get_maxunit(dc); 1386 while (max >= 0) { 1387 dev = devclass_get_device(dc, max); 1388 if (dev) { 1389 bus = device_get_softc(dev); 1390 if (bus) { 1391 usb2_needs_explore(bus, 1); 1392 } 1393 } 1394 max--; 1395 } 1396} 1397 1398/*------------------------------------------------------------------------* 1399 * usb2_bus_power_update 1400 * 1401 * This function will ensure that all USB devices on the given bus are 1402 * properly suspended or resumed according to the device transfer 1403 * state. 1404 *------------------------------------------------------------------------*/ 1405void 1406usb2_bus_power_update(struct usb2_bus *bus) 1407{ 1408 usb2_needs_explore(bus, 0 /* no probe */ ); 1409} 1410 1411/*------------------------------------------------------------------------* 1412 * usb2_transfer_power_ref 1413 * 1414 * This function will modify the power save reference counts and 1415 * wakeup the USB device associated with the given USB transfer, if 1416 * needed. 1417 *------------------------------------------------------------------------*/ 1418void 1419usb2_transfer_power_ref(struct usb2_xfer *xfer, int val) 1420{ 1421 static const uint32_t power_mask[4] = { 1422 [UE_CONTROL] = USB_HW_POWER_CONTROL, 1423 [UE_BULK] = USB_HW_POWER_BULK, 1424 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT, 1425 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC, 1426 }; 1427 struct usb2_device *udev; 1428 uint8_t needs_explore; 1429 uint8_t needs_hw_power; 1430 uint8_t xfer_type; 1431
| 2/*- 3 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 4 * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29/* 30 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 31 */ 32 33#include <dev/usb2/include/usb2_defs.h> 34#include <dev/usb2/include/usb2_mfunc.h> 35#include <dev/usb2/include/usb2_error.h> 36#include <dev/usb2/include/usb2_standard.h> 37#include <dev/usb2/include/usb2_ioctl.h> 38 39#define USB_DEBUG_VAR uhub_debug 40 41#include <dev/usb2/core/usb2_core.h> 42#include <dev/usb2/core/usb2_process.h> 43#include <dev/usb2/core/usb2_device.h> 44#include <dev/usb2/core/usb2_request.h> 45#include <dev/usb2/core/usb2_debug.h> 46#include <dev/usb2/core/usb2_hub.h> 47#include <dev/usb2/core/usb2_util.h> 48#include <dev/usb2/core/usb2_busdma.h> 49#include <dev/usb2/core/usb2_transfer.h> 50#include <dev/usb2/core/usb2_dynamic.h> 51 52#include <dev/usb2/controller/usb2_controller.h> 53#include <dev/usb2/controller/usb2_bus.h> 54 55#define UHUB_INTR_INTERVAL 250 /* ms */ 56 57#if USB_DEBUG 58static int uhub_debug = 0; 59 60SYSCTL_NODE(_hw_usb2, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB"); 61SYSCTL_INT(_hw_usb2_uhub, OID_AUTO, debug, CTLFLAG_RW, &uhub_debug, 0, 62 "Debug level"); 63#endif 64 65static int usb2_power_timeout = 30; /* seconds */ 66 67SYSCTL_INT(_hw_usb2, OID_AUTO, power_timeout, CTLFLAG_RW, 68 &usb2_power_timeout, 0, "USB power timeout"); 69 70struct uhub_current_state { 71 uint16_t port_change; 72 uint16_t port_status; 73}; 74 75struct uhub_softc { 76 struct uhub_current_state sc_st;/* current state */ 77 device_t sc_dev; /* base device */ 78 struct usb2_device *sc_udev; /* USB device */ 79 struct usb2_xfer *sc_xfer[2]; /* interrupt xfer */ 80 uint8_t sc_flags; 81#define UHUB_FLAG_DID_EXPLORE 0x01 82#define UHUB_FLAG_INTR_STALL 0x02 83 char sc_name[32]; 84}; 85 86#define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol) 87#define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB) 88#define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT) 89 90/* prototypes for type checking: */ 91 92static device_probe_t uhub_probe; 93static device_attach_t uhub_attach; 94static device_detach_t uhub_detach; 95static device_suspend_t uhub_suspend; 96static device_resume_t uhub_resume; 97 98static bus_driver_added_t uhub_driver_added; 99static bus_child_location_str_t uhub_child_location_string; 100static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string; 101 102static usb2_callback_t uhub_intr_callback; 103static usb2_callback_t uhub_intr_clear_stall_callback; 104 105static void usb2_dev_resume_peer(struct usb2_device *udev); 106static void usb2_dev_suspend_peer(struct usb2_device *udev); 107 108static const struct usb2_config uhub_config[2] = { 109 110 [0] = { 111 .type = UE_INTERRUPT, 112 .endpoint = UE_ADDR_ANY, 113 .direction = UE_DIR_ANY, 114 .mh.timeout = 0, 115 .mh.flags = {.pipe_bof = 1,.short_xfer_ok = 1,}, 116 .mh.bufsize = 0, /* use wMaxPacketSize */ 117 .mh.callback = &uhub_intr_callback, 118 .mh.interval = UHUB_INTR_INTERVAL, 119 }, 120 121 [1] = { 122 .type = UE_CONTROL, 123 .endpoint = 0, 124 .direction = UE_DIR_ANY, 125 .mh.timeout = 1000, /* 1 second */ 126 .mh.interval = 50, /* 50ms */ 127 .mh.flags = {}, 128 .mh.bufsize = sizeof(struct usb2_device_request), 129 .mh.callback = &uhub_intr_clear_stall_callback, 130 }, 131}; 132 133/* 134 * driver instance for "hub" connected to "usb" 135 * and "hub" connected to "hub" 136 */ 137static devclass_t uhub_devclass; 138 139static driver_t uhub_driver = 140{ 141 .name = "ushub", 142 .methods = (device_method_t[]){ 143 DEVMETHOD(device_probe, uhub_probe), 144 DEVMETHOD(device_attach, uhub_attach), 145 DEVMETHOD(device_detach, uhub_detach), 146 147 DEVMETHOD(device_suspend, uhub_suspend), 148 DEVMETHOD(device_resume, uhub_resume), 149 DEVMETHOD(device_shutdown, bus_generic_shutdown), 150 151 DEVMETHOD(bus_child_location_str, uhub_child_location_string), 152 DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string), 153 DEVMETHOD(bus_driver_added, uhub_driver_added), 154 {0, 0} 155 }, 156 .size = sizeof(struct uhub_softc) 157}; 158 159DRIVER_MODULE(ushub, usbus, uhub_driver, uhub_devclass, 0, 0); 160DRIVER_MODULE(ushub, ushub, uhub_driver, uhub_devclass, NULL, 0); 161 162static void 163uhub_intr_clear_stall_callback(struct usb2_xfer *xfer) 164{ 165 struct uhub_softc *sc = xfer->priv_sc; 166 struct usb2_xfer *xfer_other = sc->sc_xfer[0]; 167 168 if (usb2_clear_stall_callback(xfer, xfer_other)) { 169 DPRINTF("stall cleared\n"); 170 sc->sc_flags &= ~UHUB_FLAG_INTR_STALL; 171 usb2_transfer_start(xfer_other); 172 } 173} 174 175static void 176uhub_intr_callback(struct usb2_xfer *xfer) 177{ 178 struct uhub_softc *sc = xfer->priv_sc; 179 180 switch (USB_GET_STATE(xfer)) { 181 case USB_ST_TRANSFERRED: 182 DPRINTFN(2, "\n"); 183 /* 184 * This is an indication that some port 185 * has changed status. Notify the bus 186 * event handler thread that we need 187 * to be explored again: 188 */ 189 usb2_needs_explore(sc->sc_udev->bus, 0); 190 191 case USB_ST_SETUP: 192 if (sc->sc_flags & UHUB_FLAG_INTR_STALL) { 193 usb2_transfer_start(sc->sc_xfer[1]); 194 } else { 195 xfer->frlengths[0] = xfer->max_data_length; 196 usb2_start_hardware(xfer); 197 } 198 return; 199 200 default: /* Error */ 201 if (xfer->error != USB_ERR_CANCELLED) { 202 /* start clear stall */ 203 sc->sc_flags |= UHUB_FLAG_INTR_STALL; 204 usb2_transfer_start(sc->sc_xfer[1]); 205 } 206 return; 207 } 208} 209 210/*------------------------------------------------------------------------* 211 * uhub_explore_sub - subroutine 212 * 213 * Return values: 214 * 0: Success 215 * Else: A control transaction failed 216 *------------------------------------------------------------------------*/ 217static usb2_error_t 218uhub_explore_sub(struct uhub_softc *sc, struct usb2_port *up) 219{ 220 struct usb2_bus *bus; 221 struct usb2_device *child; 222 uint8_t refcount; 223 usb2_error_t err; 224 225 bus = sc->sc_udev->bus; 226 err = 0; 227 228 /* get driver added refcount from USB bus */ 229 refcount = bus->driver_added_refcount; 230 231 /* get device assosiated with the given port */ 232 child = usb2_bus_port_get_device(bus, up); 233 if (child == NULL) { 234 /* nothing to do */ 235 goto done; 236 } 237 /* check if probe and attach should be done */ 238 239 if (child->driver_added_refcount != refcount) { 240 child->driver_added_refcount = refcount; 241 err = usb2_probe_and_attach(child, 242 USB_IFACE_INDEX_ANY); 243 if (err) { 244 goto done; 245 } 246 } 247 /* start control transfer, if device mode */ 248 249 if (child->flags.usb2_mode == USB_MODE_DEVICE) { 250 usb2_default_transfer_setup(child); 251 } 252 /* if a HUB becomes present, do a recursive HUB explore */ 253 254 if (child->hub) { 255 err = (child->hub->explore) (child); 256 } 257done: 258 return (err); 259} 260 261/*------------------------------------------------------------------------* 262 * uhub_read_port_status - factored out code 263 *------------------------------------------------------------------------*/ 264static usb2_error_t 265uhub_read_port_status(struct uhub_softc *sc, uint8_t portno) 266{ 267 struct usb2_port_status ps; 268 usb2_error_t err; 269 270 err = usb2_req_get_port_status( 271 sc->sc_udev, &Giant, &ps, portno); 272 273 /* update status regardless of error */ 274 275 sc->sc_st.port_status = UGETW(ps.wPortStatus); 276 sc->sc_st.port_change = UGETW(ps.wPortChange); 277 278 /* debugging print */ 279 280 DPRINTFN(4, "port %d, wPortStatus=0x%04x, " 281 "wPortChange=0x%04x, err=%s\n", 282 portno, sc->sc_st.port_status, 283 sc->sc_st.port_change, usb2_errstr(err)); 284 return (err); 285} 286 287/*------------------------------------------------------------------------* 288 * uhub_reattach_port 289 * 290 * Returns: 291 * 0: Success 292 * Else: A control transaction failed 293 *------------------------------------------------------------------------*/ 294static usb2_error_t 295uhub_reattach_port(struct uhub_softc *sc, uint8_t portno) 296{ 297 struct usb2_device *child; 298 struct usb2_device *udev; 299 usb2_error_t err; 300 uint8_t timeout; 301 uint8_t speed; 302 uint8_t usb2_mode; 303 304 DPRINTF("reattaching port %d\n", portno); 305 306 err = 0; 307 timeout = 0; 308 udev = sc->sc_udev; 309 child = usb2_bus_port_get_device(udev->bus, 310 udev->hub->ports + portno - 1); 311 312repeat: 313 314 /* first clear the port connection change bit */ 315 316 err = usb2_req_clear_port_feature(udev, &Giant, 317 portno, UHF_C_PORT_CONNECTION); 318 319 if (err) { 320 goto error; 321 } 322 /* detach any existing devices */ 323 324 if (child) { 325 usb2_detach_device(child, USB_IFACE_INDEX_ANY, 1); 326 usb2_free_device(child); 327 child = NULL; 328 } 329 /* get fresh status */ 330 331 err = uhub_read_port_status(sc, portno); 332 if (err) { 333 goto error; 334 } 335 /* check if nothing is connected to the port */ 336 337 if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS)) { 338 goto error; 339 } 340 /* check if there is no power on the port and print a warning */ 341 342 if (!(sc->sc_st.port_status & UPS_PORT_POWER)) { 343 DPRINTF("WARNING: strange, connected port %d " 344 "has no power\n", portno); 345 } 346 /* check if the device is in Host Mode */ 347 348 if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) { 349 350 DPRINTF("Port %d is in Host Mode\n", portno); 351 352 if (sc->sc_st.port_status & UPS_SUSPEND) { 353 DPRINTF("Port %d was still " 354 "suspended, clearing.\n", portno); 355 err = usb2_req_clear_port_feature(sc->sc_udev, 356 &Giant, portno, UHF_PORT_SUSPEND); 357 } 358 /* USB Host Mode */ 359 360 /* wait for maximum device power up time */ 361 362 usb2_pause_mtx(&Giant, USB_PORT_POWERUP_DELAY); 363 364 /* reset port, which implies enabling it */ 365 366 err = usb2_req_reset_port(udev, &Giant, portno); 367 368 if (err) { 369 DPRINTFN(0, "port %d reset " 370 "failed, error=%s\n", 371 portno, usb2_errstr(err)); 372 goto error; 373 } 374 /* get port status again, it might have changed during reset */ 375 376 err = uhub_read_port_status(sc, portno); 377 if (err) { 378 goto error; 379 } 380 /* check if something changed during port reset */ 381 382 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) || 383 (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) { 384 if (timeout) { 385 DPRINTFN(0, "giving up port reset " 386 "- device vanished!\n"); 387 goto error; 388 } 389 timeout = 1; 390 goto repeat; 391 } 392 } else { 393 DPRINTF("Port %d is in Device Mode\n", portno); 394 } 395 396 /* 397 * Figure out the device speed 398 */ 399 speed = 400 (sc->sc_st.port_status & UPS_HIGH_SPEED) ? USB_SPEED_HIGH : 401 (sc->sc_st.port_status & UPS_LOW_SPEED) ? USB_SPEED_LOW : USB_SPEED_FULL; 402 403 /* 404 * Figure out the device mode 405 * 406 * NOTE: This part is currently FreeBSD specific. 407 */ 408 usb2_mode = 409 (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE) ? 410 USB_MODE_DEVICE : USB_MODE_HOST; 411 412 /* need to create a new child */ 413 414 child = usb2_alloc_device(sc->sc_dev, udev->bus, udev, 415 udev->depth + 1, portno - 1, portno, speed, usb2_mode); 416 if (child == NULL) { 417 DPRINTFN(0, "could not allocate new device!\n"); 418 goto error; 419 } 420 return (0); /* success */ 421 422error: 423 if (child) { 424 usb2_detach_device(child, USB_IFACE_INDEX_ANY, 1); 425 usb2_free_device(child); 426 child = NULL; 427 } 428 if (err == 0) { 429 if (sc->sc_st.port_status & UPS_PORT_ENABLED) { 430 err = usb2_req_clear_port_feature( 431 sc->sc_udev, &Giant, 432 portno, UHF_PORT_ENABLE); 433 } 434 } 435 if (err) { 436 DPRINTFN(0, "device problem (%s), " 437 "disabling port %d\n", usb2_errstr(err), portno); 438 } 439 return (err); 440} 441 442/*------------------------------------------------------------------------* 443 * uhub_suspend_resume_port 444 * 445 * Returns: 446 * 0: Success 447 * Else: A control transaction failed 448 *------------------------------------------------------------------------*/ 449static usb2_error_t 450uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno) 451{ 452 struct usb2_device *child; 453 struct usb2_device *udev; 454 uint8_t is_suspend; 455 usb2_error_t err; 456 457 DPRINTF("port %d\n", portno); 458 459 udev = sc->sc_udev; 460 child = usb2_bus_port_get_device(udev->bus, 461 udev->hub->ports + portno - 1); 462 463 /* first clear the port suspend change bit */ 464 465 err = usb2_req_clear_port_feature(udev, &Giant, 466 portno, UHF_C_PORT_SUSPEND); 467 if (err) { 468 DPRINTF("clearing suspend failed.\n"); 469 goto done; 470 } 471 /* get fresh status */ 472 473 err = uhub_read_port_status(sc, portno); 474 if (err) { 475 DPRINTF("reading port status failed.\n"); 476 goto done; 477 } 478 /* get current state */ 479 480 if (sc->sc_st.port_status & UPS_SUSPEND) { 481 is_suspend = 1; 482 } else { 483 is_suspend = 0; 484 } 485 486 DPRINTF("suspended=%u\n", is_suspend); 487 488 /* do the suspend or resume */ 489 490 if (child) { 491 /* 492 * This code handle two cases: 1) Host Mode - we can only 493 * receive resume here 2) Device Mode - we can receive 494 * suspend and resume here 495 */ 496 if (is_suspend == 0) 497 usb2_dev_resume_peer(child); 498 else if (child->flags.usb2_mode == USB_MODE_DEVICE) 499 usb2_dev_suspend_peer(child); 500 } 501done: 502 return (err); 503} 504 505/*------------------------------------------------------------------------* 506 * uhub_explore 507 * 508 * Returns: 509 * 0: Success 510 * Else: Failure 511 *------------------------------------------------------------------------*/ 512static usb2_error_t 513uhub_explore(struct usb2_device *udev) 514{ 515 struct usb2_hub *hub; 516 struct uhub_softc *sc; 517 struct usb2_port *up; 518 usb2_error_t err; 519 uint8_t portno; 520 uint8_t x; 521 522 hub = udev->hub; 523 sc = hub->hubsoftc; 524 525 DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address); 526 527 /* ignore hubs that are too deep */ 528 if (udev->depth > USB_HUB_MAX_DEPTH) { 529 return (USB_ERR_TOO_DEEP); 530 } 531 if (udev->pwr_save.suspended) { 532 /* need to wait until the child signals resume */ 533 DPRINTF("Device is suspended!\n"); 534 return (0); 535 } 536 for (x = 0; x != hub->nports; x++) { 537 up = hub->ports + x; 538 portno = x + 1; 539 540 err = uhub_read_port_status(sc, portno); 541 if (err) { 542 /* most likely the HUB is gone */ 543 break; 544 } 545 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) { 546 DPRINTF("Overcurrent on port %u.\n", portno); 547 err = usb2_req_clear_port_feature( 548 udev, &Giant, portno, UHF_C_PORT_OVER_CURRENT); 549 if (err) { 550 /* most likely the HUB is gone */ 551 break; 552 } 553 } 554 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) { 555 /* 556 * Fake a connect status change so that the 557 * status gets checked initially! 558 */ 559 sc->sc_st.port_change |= 560 UPS_C_CONNECT_STATUS; 561 } 562 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) { 563 err = usb2_req_clear_port_feature( 564 udev, &Giant, portno, UHF_C_PORT_ENABLE); 565 if (err) { 566 /* most likely the HUB is gone */ 567 break; 568 } 569 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) { 570 /* 571 * Ignore the port error if the device 572 * has vanished ! 573 */ 574 } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) { 575 DPRINTFN(0, "illegal enable change, " 576 "port %d\n", portno); 577 } else { 578 579 if (up->restartcnt == USB_RESTART_MAX) { 580 /* XXX could try another speed ? */ 581 DPRINTFN(0, "port error, giving up " 582 "port %d\n", portno); 583 } else { 584 sc->sc_st.port_change |= 585 UPS_C_CONNECT_STATUS; 586 up->restartcnt++; 587 } 588 } 589 } 590 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) { 591 err = uhub_reattach_port(sc, portno); 592 if (err) { 593 /* most likely the HUB is gone */ 594 break; 595 } 596 } 597 if (sc->sc_st.port_change & UPS_C_SUSPEND) { 598 err = uhub_suspend_resume_port(sc, portno); 599 if (err) { 600 /* most likely the HUB is gone */ 601 break; 602 } 603 } 604 err = uhub_explore_sub(sc, up); 605 if (err) { 606 /* no device(s) present */ 607 continue; 608 } 609 /* explore succeeded - reset restart counter */ 610 up->restartcnt = 0; 611 } 612 613 /* initial status checked */ 614 sc->sc_flags |= UHUB_FLAG_DID_EXPLORE; 615 616 /* return success */ 617 return (USB_ERR_NORMAL_COMPLETION); 618} 619 620static int 621uhub_probe(device_t dev) 622{ 623 struct usb2_attach_arg *uaa = device_get_ivars(dev); 624 625 if (uaa->usb2_mode != USB_MODE_HOST) { 626 return (ENXIO); 627 } 628 /* 629 * The subclass for USB HUBs is ignored because it is 0 for 630 * some and 1 for others. 631 */ 632 if ((uaa->info.bConfigIndex == 0) && 633 (uaa->info.bDeviceClass == UDCLASS_HUB)) { 634 return (0); 635 } 636 return (ENXIO); 637} 638 639static int 640uhub_attach(device_t dev) 641{ 642 struct uhub_softc *sc = device_get_softc(dev); 643 struct usb2_attach_arg *uaa = device_get_ivars(dev); 644 struct usb2_device *udev = uaa->device; 645 struct usb2_device *parent_hub = udev->parent_hub; 646 struct usb2_hub *hub; 647 struct usb2_hub_descriptor hubdesc; 648 uint16_t pwrdly; 649 uint8_t x; 650 uint8_t nports; 651 uint8_t portno; 652 uint8_t removable; 653 uint8_t iface_index; 654 usb2_error_t err; 655 656 if (sc == NULL) { 657 return (ENOMEM); 658 } 659 sc->sc_udev = udev; 660 sc->sc_dev = dev; 661 662 snprintf(sc->sc_name, sizeof(sc->sc_name), "%s", 663 device_get_nameunit(dev)); 664 665 device_set_usb2_desc(dev); 666 667 DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, " 668 "parent->selfpowered=%d\n", 669 udev->depth, 670 udev->flags.self_powered, 671 parent_hub, 672 parent_hub ? 673 parent_hub->flags.self_powered : 0); 674 675 if (udev->depth > USB_HUB_MAX_DEPTH) { 676 DPRINTFN(0, "hub depth, %d, exceeded. HUB ignored!\n", 677 USB_HUB_MAX_DEPTH); 678 goto error; 679 } 680 if (!udev->flags.self_powered && parent_hub && 681 (!parent_hub->flags.self_powered)) { 682 DPRINTFN(0, "bus powered HUB connected to " 683 "bus powered HUB. HUB ignored!\n"); 684 goto error; 685 } 686 /* get HUB descriptor */ 687 688 DPRINTFN(2, "getting HUB descriptor\n"); 689 690 /* assuming that there is one port */ 691 err = usb2_req_get_hub_descriptor(udev, &Giant, &hubdesc, 1); 692 693 nports = hubdesc.bNbrPorts; 694 695 if (!err && (nports >= 8)) { 696 /* get complete HUB descriptor */ 697 err = usb2_req_get_hub_descriptor(udev, &Giant, &hubdesc, nports); 698 } 699 if (err) { 700 DPRINTFN(0, "getting hub descriptor failed," 701 "error=%s\n", usb2_errstr(err)); 702 goto error; 703 } 704 if (hubdesc.bNbrPorts != nports) { 705 DPRINTFN(0, "number of ports changed!\n"); 706 goto error; 707 } 708 if (nports == 0) { 709 DPRINTFN(0, "portless HUB!\n"); 710 goto error; 711 } 712 hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports), 713 M_USBDEV, M_WAITOK | M_ZERO); 714 715 if (hub == NULL) { 716 goto error; 717 } 718 udev->hub = hub; 719 720 /* init FULL-speed ISOCHRONOUS schedule */ 721 usb2_fs_isoc_schedule_init_all(hub->fs_isoc_schedule); 722 723 /* initialize HUB structure */ 724 hub->hubsoftc = sc; 725 hub->explore = &uhub_explore; 726 hub->nports = hubdesc.bNbrPorts; 727 hub->hubudev = udev; 728 729 /* if self powered hub, give ports maximum current */ 730 if (udev->flags.self_powered) { 731 hub->portpower = USB_MAX_POWER; 732 } else { 733 hub->portpower = USB_MIN_POWER; 734 } 735 736 /* set up interrupt pipe */ 737 iface_index = 0; 738 err = usb2_transfer_setup(udev, &iface_index, sc->sc_xfer, 739 uhub_config, 2, sc, &Giant); 740 if (err) { 741 DPRINTFN(0, "cannot setup interrupt transfer, " 742 "errstr=%s!\n", usb2_errstr(err)); 743 goto error; 744 } 745 /* wait with power off for a while */ 746 usb2_pause_mtx(&Giant, USB_POWER_DOWN_TIME); 747 748 /* 749 * To have the best chance of success we do things in the exact same 750 * order as Windoze98. This should not be necessary, but some 751 * devices do not follow the USB specs to the letter. 752 * 753 * These are the events on the bus when a hub is attached: 754 * Get device and config descriptors (see attach code) 755 * Get hub descriptor (see above) 756 * For all ports 757 * turn on power 758 * wait for power to become stable 759 * (all below happens in explore code) 760 * For all ports 761 * clear C_PORT_CONNECTION 762 * For all ports 763 * get port status 764 * if device connected 765 * wait 100 ms 766 * turn on reset 767 * wait 768 * clear C_PORT_RESET 769 * get port status 770 * proceed with device attachment 771 */ 772 773 /* XXX should check for none, individual, or ganged power? */ 774 775 removable = 0; 776 pwrdly = ((hubdesc.bPwrOn2PwrGood * UHD_PWRON_FACTOR) + 777 USB_EXTRA_POWER_UP_TIME); 778 779 for (x = 0; x != nports; x++) { 780 /* set up data structures */ 781 struct usb2_port *up = hub->ports + x; 782 783 up->device_index = 0; 784 up->restartcnt = 0; 785 portno = x + 1; 786 787 /* check if port is removable */ 788 if (!UHD_NOT_REMOV(&hubdesc, portno)) { 789 removable++; 790 } 791 if (!err) { 792 /* turn the power on */ 793 err = usb2_req_set_port_feature(udev, &Giant, 794 portno, UHF_PORT_POWER); 795 } 796 if (err) { 797 DPRINTFN(0, "port %d power on failed, %s\n", 798 portno, usb2_errstr(err)); 799 } 800 DPRINTF("turn on port %d power\n", 801 portno); 802 803 /* wait for stable power */ 804 usb2_pause_mtx(&Giant, pwrdly); 805 } 806 807 device_printf(dev, "%d port%s with %d " 808 "removable, %s powered\n", nports, (nports != 1) ? "s" : "", 809 removable, udev->flags.self_powered ? "self" : "bus"); 810 811 /* start the interrupt endpoint */ 812 813 USB_XFER_LOCK(sc->sc_xfer[0]); 814 usb2_transfer_start(sc->sc_xfer[0]); 815 USB_XFER_UNLOCK(sc->sc_xfer[0]); 816 817 /* Enable automatic power save on all USB HUBs */ 818 819 usb2_set_power_mode(udev, USB_POWER_MODE_SAVE); 820 821 return (0); 822 823error: 824 usb2_transfer_unsetup(sc->sc_xfer, 2); 825 826 if (udev->hub) { 827 free(udev->hub, M_USBDEV); 828 udev->hub = NULL; 829 } 830 return (ENXIO); 831} 832 833/* 834 * Called from process context when the hub is gone. 835 * Detach all devices on active ports. 836 */ 837static int 838uhub_detach(device_t dev) 839{ 840 struct uhub_softc *sc = device_get_softc(dev); 841 struct usb2_hub *hub = sc->sc_udev->hub; 842 struct usb2_device *child; 843 uint8_t x; 844 845 /* detach all children first */ 846 bus_generic_detach(dev); 847 848 if (hub == NULL) { /* must be partially working */ 849 return (0); 850 } 851 for (x = 0; x != hub->nports; x++) { 852 853 child = usb2_bus_port_get_device(sc->sc_udev->bus, hub->ports + x); 854 855 if (child == NULL) { 856 continue; 857 } 858 /* 859 * Subdevices are not freed, because the caller of 860 * uhub_detach() will do that. 861 */ 862 usb2_detach_device(child, USB_IFACE_INDEX_ANY, 0); 863 usb2_free_device(child); 864 child = NULL; 865 } 866 867 usb2_transfer_unsetup(sc->sc_xfer, 2); 868 869 free(hub, M_USBDEV); 870 sc->sc_udev->hub = NULL; 871 return (0); 872} 873 874static int 875uhub_suspend(device_t dev) 876{ 877 DPRINTF("\n"); 878 /* Sub-devices are not suspended here! */ 879 return (0); 880} 881 882static int 883uhub_resume(device_t dev) 884{ 885 DPRINTF("\n"); 886 /* Sub-devices are not resumed here! */ 887 return (0); 888} 889 890static void 891uhub_driver_added(device_t dev, driver_t *driver) 892{ 893 usb2_needs_explore_all(); 894} 895 896struct hub_result { 897 struct usb2_device *udev; 898 uint8_t portno; 899 uint8_t iface_index; 900}; 901 902static void 903uhub_find_iface_index(struct usb2_hub *hub, device_t child, 904 struct hub_result *res) 905{ 906 struct usb2_interface *iface; 907 struct usb2_device *udev; 908 uint8_t nports; 909 uint8_t x; 910 uint8_t i; 911 912 nports = hub->nports; 913 for (x = 0; x != nports; x++) { 914 udev = usb2_bus_port_get_device(hub->hubudev->bus, 915 hub->ports + x); 916 if (!udev) { 917 continue; 918 } 919 for (i = 0; i != USB_IFACE_MAX; i++) { 920 iface = usb2_get_iface(udev, i); 921 if (iface && 922 (iface->subdev == child)) { 923 res->iface_index = i; 924 res->udev = udev; 925 res->portno = x + 1; 926 return; 927 } 928 } 929 } 930 res->iface_index = 0; 931 res->udev = NULL; 932 res->portno = 0; 933} 934 935static int 936uhub_child_location_string(device_t parent, device_t child, 937 char *buf, size_t buflen) 938{ 939 struct uhub_softc *sc = device_get_softc(parent); 940 struct usb2_hub *hub = sc->sc_udev->hub; 941 struct hub_result res; 942 943 mtx_lock(&Giant); 944 uhub_find_iface_index(hub, child, &res); 945 if (!res.udev) { 946 DPRINTF("device not on hub\n"); 947 if (buflen) { 948 buf[0] = '\0'; 949 } 950 goto done; 951 } 952 snprintf(buf, buflen, "port=%u interface=%u", 953 res.portno, res.iface_index); 954done: 955 mtx_unlock(&Giant); 956 957 return (0); 958} 959 960static int 961uhub_child_pnpinfo_string(device_t parent, device_t child, 962 char *buf, size_t buflen) 963{ 964 struct uhub_softc *sc = device_get_softc(parent); 965 struct usb2_hub *hub = sc->sc_udev->hub; 966 struct usb2_interface *iface; 967 struct hub_result res; 968 969 mtx_lock(&Giant); 970 uhub_find_iface_index(hub, child, &res); 971 if (!res.udev) { 972 DPRINTF("device not on hub\n"); 973 if (buflen) { 974 buf[0] = '\0'; 975 } 976 goto done; 977 } 978 iface = usb2_get_iface(res.udev, res.iface_index); 979 if (iface && iface->idesc) { 980 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x " 981 "devclass=0x%02x devsubclass=0x%02x " 982 "sernum=\"%s\" " 983 "intclass=0x%02x intsubclass=0x%02x", 984 UGETW(res.udev->ddesc.idVendor), 985 UGETW(res.udev->ddesc.idProduct), 986 res.udev->ddesc.bDeviceClass, 987 res.udev->ddesc.bDeviceSubClass, 988 res.udev->serial, 989 iface->idesc->bInterfaceClass, 990 iface->idesc->bInterfaceSubClass); 991 } else { 992 if (buflen) { 993 buf[0] = '\0'; 994 } 995 goto done; 996 } 997done: 998 mtx_unlock(&Giant); 999 1000 return (0); 1001} 1002 1003/* 1004 * The USB Transaction Translator: 1005 * =============================== 1006 * 1007 * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed 1008 * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT 1009 * USB transfers. To utilize bandwidth dynamically the "scatter and 1010 * gather" principle must be applied. This means that bandwidth must 1011 * be divided into equal parts of bandwidth. With regard to USB all 1012 * data is transferred in smaller packets with length 1013 * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is 1014 * not a constant! 1015 * 1016 * The bandwidth scheduler which I have implemented will simply pack 1017 * the USB transfers back to back until there is no more space in the 1018 * schedule. Out of the 8 microframes which the USB 2.0 standard 1019 * provides, only 6 are available for non-HIGH-speed devices. I have 1020 * reserved the first 4 microframes for ISOCHRONOUS transfers. The 1021 * last 2 microframes I have reserved for INTERRUPT transfers. Without 1022 * this division, it is very difficult to allocate and free bandwidth 1023 * dynamically. 1024 * 1025 * NOTE about the Transaction Translator in USB HUBs: 1026 * 1027 * USB HUBs have a very simple Transaction Translator, that will 1028 * simply pipeline all the SPLIT transactions. That means that the 1029 * transactions will be executed in the order they are queued! 1030 * 1031 */ 1032 1033/*------------------------------------------------------------------------* 1034 * usb2_intr_find_best_slot 1035 * 1036 * Return value: 1037 * The best Transaction Translation slot for an interrupt endpoint. 1038 *------------------------------------------------------------------------*/ 1039static uint8_t 1040usb2_intr_find_best_slot(uint32_t *ptr, uint8_t start, uint8_t end) 1041{ 1042 uint32_t max = 0xffffffff; 1043 uint8_t x; 1044 uint8_t y; 1045 1046 y = 0; 1047 1048 /* find the last slot with lesser used bandwidth */ 1049 1050 for (x = start; x < end; x++) { 1051 if (max >= ptr[x]) { 1052 max = ptr[x]; 1053 y = x; 1054 } 1055 } 1056 return (y); 1057} 1058 1059/*------------------------------------------------------------------------* 1060 * usb2_intr_schedule_adjust 1061 * 1062 * This function will update the bandwith usage for the microframe 1063 * having index "slot" by "len" bytes. "len" can be negative. If the 1064 * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX" 1065 * the "slot" argument will be replaced by the slot having least used 1066 * bandwidth. 1067 * 1068 * Returns: 1069 * The slot on which the bandwidth update was done. 1070 *------------------------------------------------------------------------*/ 1071uint8_t 1072usb2_intr_schedule_adjust(struct usb2_device *udev, int16_t len, uint8_t slot) 1073{ 1074 struct usb2_bus *bus = udev->bus; 1075 struct usb2_hub *hub; 1076 1077 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 1078 1079 if (usb2_get_speed(udev) == USB_SPEED_HIGH) { 1080 if (slot >= USB_HS_MICRO_FRAMES_MAX) { 1081 slot = usb2_intr_find_best_slot(bus->uframe_usage, 0, 1082 USB_HS_MICRO_FRAMES_MAX); 1083 } 1084 bus->uframe_usage[slot] += len; 1085 } else { 1086 if (usb2_get_speed(udev) == USB_SPEED_LOW) { 1087 len *= 8; 1088 } 1089 /* 1090 * The Host Controller Driver should have 1091 * performed checks so that the lookup 1092 * below does not result in a NULL pointer 1093 * access. 1094 */ 1095 1096 hub = bus->devices[udev->hs_hub_addr]->hub; 1097 if (slot >= USB_HS_MICRO_FRAMES_MAX) { 1098 slot = usb2_intr_find_best_slot(hub->uframe_usage, 1099 USB_FS_ISOC_UFRAME_MAX, 6); 1100 } 1101 hub->uframe_usage[slot] += len; 1102 bus->uframe_usage[slot] += len; 1103 } 1104 return (slot); 1105} 1106 1107/*------------------------------------------------------------------------* 1108 * usb2_fs_isoc_schedule_init_sub 1109 * 1110 * This function initialises an USB FULL speed isochronous schedule 1111 * entry. 1112 *------------------------------------------------------------------------*/ 1113static void 1114usb2_fs_isoc_schedule_init_sub(struct usb2_fs_isoc_schedule *fss) 1115{ 1116 fss->total_bytes = (USB_FS_ISOC_UFRAME_MAX * 1117 USB_FS_BYTES_PER_HS_UFRAME); 1118 fss->frame_bytes = (USB_FS_BYTES_PER_HS_UFRAME); 1119 fss->frame_slot = 0; 1120} 1121 1122/*------------------------------------------------------------------------* 1123 * usb2_fs_isoc_schedule_init_all 1124 * 1125 * This function will reset the complete USB FULL speed isochronous 1126 * bandwidth schedule. 1127 *------------------------------------------------------------------------*/ 1128void 1129usb2_fs_isoc_schedule_init_all(struct usb2_fs_isoc_schedule *fss) 1130{ 1131 struct usb2_fs_isoc_schedule *fss_end = fss + USB_ISOC_TIME_MAX; 1132 1133 while (fss != fss_end) { 1134 usb2_fs_isoc_schedule_init_sub(fss); 1135 fss++; 1136 } 1137} 1138 1139/*------------------------------------------------------------------------* 1140 * usb2_isoc_time_expand 1141 * 1142 * This function will expand the time counter from 7-bit to 16-bit. 1143 * 1144 * Returns: 1145 * 16-bit isochronous time counter. 1146 *------------------------------------------------------------------------*/ 1147uint16_t 1148usb2_isoc_time_expand(struct usb2_bus *bus, uint16_t isoc_time_curr) 1149{ 1150 uint16_t rem; 1151 1152 USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 1153 1154 rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1); 1155 1156 isoc_time_curr &= (USB_ISOC_TIME_MAX - 1); 1157 1158 if (isoc_time_curr < rem) { 1159 /* the time counter wrapped around */ 1160 bus->isoc_time_last += USB_ISOC_TIME_MAX; 1161 } 1162 /* update the remainder */ 1163 1164 bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1); 1165 bus->isoc_time_last |= isoc_time_curr; 1166 1167 return (bus->isoc_time_last); 1168} 1169 1170/*------------------------------------------------------------------------* 1171 * usb2_fs_isoc_schedule_isoc_time_expand 1172 * 1173 * This function does multiple things. First of all it will expand the 1174 * passed isochronous time, which is the return value. Then it will 1175 * store where the current FULL speed isochronous schedule is 1176 * positioned in time and where the end is. See "pp_start" and 1177 * "pp_end" arguments. 1178 * 1179 * Returns: 1180 * Expanded version of "isoc_time". 1181 * 1182 * NOTE: This function depends on being called regularly with 1183 * intervals less than "USB_ISOC_TIME_MAX". 1184 *------------------------------------------------------------------------*/ 1185uint16_t 1186usb2_fs_isoc_schedule_isoc_time_expand(struct usb2_device *udev, 1187 struct usb2_fs_isoc_schedule **pp_start, 1188 struct usb2_fs_isoc_schedule **pp_end, 1189 uint16_t isoc_time) 1190{ 1191 struct usb2_fs_isoc_schedule *fss_end; 1192 struct usb2_fs_isoc_schedule *fss_a; 1193 struct usb2_fs_isoc_schedule *fss_b; 1194 struct usb2_hub *hs_hub; 1195 1196 isoc_time = usb2_isoc_time_expand(udev->bus, isoc_time); 1197 1198 hs_hub = udev->bus->devices[udev->hs_hub_addr]->hub; 1199 1200 if (hs_hub != NULL) { 1201 1202 fss_a = hs_hub->fs_isoc_schedule + 1203 (hs_hub->isoc_last_time % USB_ISOC_TIME_MAX); 1204 1205 hs_hub->isoc_last_time = isoc_time; 1206 1207 fss_b = hs_hub->fs_isoc_schedule + 1208 (isoc_time % USB_ISOC_TIME_MAX); 1209 1210 fss_end = hs_hub->fs_isoc_schedule + USB_ISOC_TIME_MAX; 1211 1212 *pp_start = hs_hub->fs_isoc_schedule; 1213 *pp_end = fss_end; 1214 1215 while (fss_a != fss_b) { 1216 if (fss_a == fss_end) { 1217 fss_a = hs_hub->fs_isoc_schedule; 1218 continue; 1219 } 1220 usb2_fs_isoc_schedule_init_sub(fss_a); 1221 fss_a++; 1222 } 1223 1224 } else { 1225 1226 *pp_start = NULL; 1227 *pp_end = NULL; 1228 } 1229 return (isoc_time); 1230} 1231 1232/*------------------------------------------------------------------------* 1233 * usb2_fs_isoc_schedule_alloc 1234 * 1235 * This function will allocate bandwidth for an isochronous FULL speed 1236 * transaction in the FULL speed schedule. The microframe slot where 1237 * the transaction should be started is stored in the byte pointed to 1238 * by "pstart". The "len" argument specifies the length of the 1239 * transaction in bytes. 1240 * 1241 * Returns: 1242 * 0: Success 1243 * Else: Error 1244 *------------------------------------------------------------------------*/ 1245uint8_t 1246usb2_fs_isoc_schedule_alloc(struct usb2_fs_isoc_schedule *fss, 1247 uint8_t *pstart, uint16_t len) 1248{ 1249 uint8_t slot = fss->frame_slot; 1250 1251 /* Compute overhead and bit-stuffing */ 1252 1253 len += 8; 1254 1255 len *= 7; 1256 len /= 6; 1257 1258 if (len > fss->total_bytes) { 1259 *pstart = 0; /* set some dummy value */ 1260 return (1); /* error */ 1261 } 1262 if (len > 0) { 1263 1264 fss->total_bytes -= len; 1265 1266 while (len >= fss->frame_bytes) { 1267 len -= fss->frame_bytes; 1268 fss->frame_bytes = USB_FS_BYTES_PER_HS_UFRAME; 1269 fss->frame_slot++; 1270 } 1271 1272 fss->frame_bytes -= len; 1273 } 1274 *pstart = slot; 1275 return (0); /* success */ 1276} 1277 1278/*------------------------------------------------------------------------* 1279 * usb2_bus_port_get_device 1280 * 1281 * This function is NULL safe. 1282 *------------------------------------------------------------------------*/ 1283struct usb2_device * 1284usb2_bus_port_get_device(struct usb2_bus *bus, struct usb2_port *up) 1285{ 1286 if ((bus == NULL) || (up == NULL)) { 1287 /* be NULL safe */ 1288 return (NULL); 1289 } 1290 if (up->device_index == 0) { 1291 /* nothing to do */ 1292 return (NULL); 1293 } 1294 return (bus->devices[up->device_index]); 1295} 1296 1297/*------------------------------------------------------------------------* 1298 * usb2_bus_port_set_device 1299 * 1300 * This function is NULL safe. 1301 *------------------------------------------------------------------------*/ 1302void 1303usb2_bus_port_set_device(struct usb2_bus *bus, struct usb2_port *up, 1304 struct usb2_device *udev, uint8_t device_index) 1305{ 1306 if (bus == NULL) { 1307 /* be NULL safe */ 1308 return; 1309 } 1310 /* 1311 * There is only one case where we don't 1312 * have an USB port, and that is the Root Hub! 1313 */ 1314 if (up) { 1315 if (udev) { 1316 up->device_index = device_index; 1317 } else { 1318 device_index = up->device_index; 1319 up->device_index = 0; 1320 } 1321 } 1322 /* 1323 * Make relationships to our new device 1324 */ 1325 if (device_index != 0) { 1326 mtx_lock(&usb2_ref_lock); 1327 bus->devices[device_index] = udev; 1328 mtx_unlock(&usb2_ref_lock); 1329 } 1330 /* 1331 * Debug print 1332 */ 1333 DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev); 1334} 1335 1336/*------------------------------------------------------------------------* 1337 * usb2_needs_explore 1338 * 1339 * This functions is called when the USB event thread needs to run. 1340 *------------------------------------------------------------------------*/ 1341void 1342usb2_needs_explore(struct usb2_bus *bus, uint8_t do_probe) 1343{ 1344 DPRINTF("\n"); 1345 1346 if (bus == NULL) { 1347 DPRINTF("No bus pointer!\n"); 1348 return; 1349 } 1350 USB_BUS_LOCK(bus); 1351 if (do_probe) { 1352 bus->do_probe = 1; 1353 } 1354 if (usb2_proc_msignal(&bus->explore_proc, 1355 &bus->explore_msg[0], &bus->explore_msg[1])) { 1356 /* ignore */ 1357 } 1358 USB_BUS_UNLOCK(bus); 1359} 1360 1361/*------------------------------------------------------------------------* 1362 * usb2_needs_explore_all 1363 * 1364 * This function is called whenever a new driver is loaded and will 1365 * cause that all USB busses are re-explored. 1366 *------------------------------------------------------------------------*/ 1367void 1368usb2_needs_explore_all(void) 1369{ 1370 struct usb2_bus *bus; 1371 devclass_t dc; 1372 device_t dev; 1373 int max; 1374 1375 DPRINTFN(3, "\n"); 1376 1377 dc = usb2_devclass_ptr; 1378 if (dc == NULL) { 1379 DPRINTFN(0, "no devclass\n"); 1380 return; 1381 } 1382 /* 1383 * Explore all USB busses in parallell. 1384 */ 1385 max = devclass_get_maxunit(dc); 1386 while (max >= 0) { 1387 dev = devclass_get_device(dc, max); 1388 if (dev) { 1389 bus = device_get_softc(dev); 1390 if (bus) { 1391 usb2_needs_explore(bus, 1); 1392 } 1393 } 1394 max--; 1395 } 1396} 1397 1398/*------------------------------------------------------------------------* 1399 * usb2_bus_power_update 1400 * 1401 * This function will ensure that all USB devices on the given bus are 1402 * properly suspended or resumed according to the device transfer 1403 * state. 1404 *------------------------------------------------------------------------*/ 1405void 1406usb2_bus_power_update(struct usb2_bus *bus) 1407{ 1408 usb2_needs_explore(bus, 0 /* no probe */ ); 1409} 1410 1411/*------------------------------------------------------------------------* 1412 * usb2_transfer_power_ref 1413 * 1414 * This function will modify the power save reference counts and 1415 * wakeup the USB device associated with the given USB transfer, if 1416 * needed. 1417 *------------------------------------------------------------------------*/ 1418void 1419usb2_transfer_power_ref(struct usb2_xfer *xfer, int val) 1420{ 1421 static const uint32_t power_mask[4] = { 1422 [UE_CONTROL] = USB_HW_POWER_CONTROL, 1423 [UE_BULK] = USB_HW_POWER_BULK, 1424 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT, 1425 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC, 1426 }; 1427 struct usb2_device *udev; 1428 uint8_t needs_explore; 1429 uint8_t needs_hw_power; 1430 uint8_t xfer_type; 1431
|