102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 302ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 602ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 702ac6454SAndrew Thompson * are met: 802ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 902ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1002ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1202ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1502ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1602ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1702ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1802ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1902ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2002ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2102ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2202ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2302ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2402ac6454SAndrew Thompson * SUCH DAMAGE. 2502ac6454SAndrew Thompson */ 2602ac6454SAndrew Thompson 27d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 28d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 29d2b99310SHans Petter Selasky #else 30f0c078e6SAndrew Thompson #include "opt_ddb.h" 31f0c078e6SAndrew Thompson 32ed6d949aSAndrew Thompson #include <sys/stdint.h> 33ed6d949aSAndrew Thompson #include <sys/stddef.h> 34ed6d949aSAndrew Thompson #include <sys/param.h> 35ed6d949aSAndrew Thompson #include <sys/queue.h> 36ed6d949aSAndrew Thompson #include <sys/types.h> 37ed6d949aSAndrew Thompson #include <sys/systm.h> 38ed6d949aSAndrew Thompson #include <sys/kernel.h> 39ed6d949aSAndrew Thompson #include <sys/bus.h> 40ed6d949aSAndrew Thompson #include <sys/module.h> 41ed6d949aSAndrew Thompson #include <sys/lock.h> 42ed6d949aSAndrew Thompson #include <sys/mutex.h> 43ed6d949aSAndrew Thompson #include <sys/condvar.h> 44ed6d949aSAndrew Thompson #include <sys/sysctl.h> 45ed6d949aSAndrew Thompson #include <sys/sx.h> 46ed6d949aSAndrew Thompson #include <sys/unistd.h> 47ed6d949aSAndrew Thompson #include <sys/callout.h> 48ed6d949aSAndrew Thompson #include <sys/malloc.h> 49ed6d949aSAndrew Thompson #include <sys/priv.h> 50ed6d949aSAndrew Thompson 5102ac6454SAndrew Thompson #include <dev/usb/usb.h> 52ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 5302ac6454SAndrew Thompson 54a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_ctrl_debug 5502ac6454SAndrew Thompson 5602ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 5702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 5802ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 5902ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 6002ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 6102ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 6202ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 6302ac6454SAndrew Thompson 6402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 6502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 6618ec6525SWeongyo Jeong #include <dev/usb/usb_pf.h> 672e141748SHans Petter Selasky #include "usb_if.h" 68d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 6902ac6454SAndrew Thompson 7002ac6454SAndrew Thompson /* function prototypes */ 7102ac6454SAndrew Thompson 72a593f6b8SAndrew Thompson static device_probe_t usb_probe; 73a593f6b8SAndrew Thompson static device_attach_t usb_attach; 74a593f6b8SAndrew Thompson static device_detach_t usb_detach; 752e141748SHans Petter Selasky static device_suspend_t usb_suspend; 762e141748SHans Petter Selasky static device_resume_t usb_resume; 772e141748SHans Petter Selasky static device_shutdown_t usb_shutdown; 7802ac6454SAndrew Thompson 79a593f6b8SAndrew Thompson static void usb_attach_sub(device_t, struct usb_bus *); 8002ac6454SAndrew Thompson 8102ac6454SAndrew Thompson /* static variables */ 8202ac6454SAndrew Thompson 83ed6d949aSAndrew Thompson #ifdef USB_DEBUG 84a593f6b8SAndrew Thompson static int usb_ctrl_debug = 0; 8502ac6454SAndrew Thompson 866472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller"); 87a593f6b8SAndrew Thompson SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0, 8802ac6454SAndrew Thompson "Debug level"); 8902ac6454SAndrew Thompson #endif 9002ac6454SAndrew Thompson 91d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 92a0c61406SAlfred Perlstein static int usb_no_boot_wait = 0; 93a0c61406SAlfred Perlstein TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait); 9483cadd7dSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RD|CTLFLAG_TUN, &usb_no_boot_wait, 0, 955ed294aeSHans Petter Selasky "No USB device enumerate waiting at boot."); 96d2b99310SHans Petter Selasky #endif 975ed294aeSHans Petter Selasky 9802f728afSHans Petter Selasky static int usb_no_suspend_wait = 0; 9902f728afSHans Petter Selasky TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait); 10002f728afSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN, 10102f728afSHans Petter Selasky &usb_no_suspend_wait, 0, "No USB device waiting at system suspend."); 10202f728afSHans Petter Selasky 1035ed294aeSHans Petter Selasky static int usb_no_shutdown_wait = 0; 1045ed294aeSHans Petter Selasky TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait); 10502f728afSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN, 10602f728afSHans Petter Selasky &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown."); 107a0c61406SAlfred Perlstein 108a593f6b8SAndrew Thompson static devclass_t usb_devclass; 10902ac6454SAndrew Thompson 110a593f6b8SAndrew Thompson static device_method_t usb_methods[] = { 111a593f6b8SAndrew Thompson DEVMETHOD(device_probe, usb_probe), 112a593f6b8SAndrew Thompson DEVMETHOD(device_attach, usb_attach), 113a593f6b8SAndrew Thompson DEVMETHOD(device_detach, usb_detach), 1142e141748SHans Petter Selasky DEVMETHOD(device_suspend, usb_suspend), 1152e141748SHans Petter Selasky DEVMETHOD(device_resume, usb_resume), 1162e141748SHans Petter Selasky DEVMETHOD(device_shutdown, usb_shutdown), 11761bfd867SSofian Brabez 11861bfd867SSofian Brabez DEVMETHOD_END 11902ac6454SAndrew Thompson }; 12002ac6454SAndrew Thompson 121a593f6b8SAndrew Thompson static driver_t usb_driver = { 12202ac6454SAndrew Thompson .name = "usbus", 123a593f6b8SAndrew Thompson .methods = usb_methods, 12402ac6454SAndrew Thompson .size = 0, 12502ac6454SAndrew Thompson }; 12602ac6454SAndrew Thompson 127864bc412SHans Petter Selasky /* Host Only Drivers */ 128a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0); 129a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0); 130a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0); 131963169b4SHans Petter Selasky DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0); 132864bc412SHans Petter Selasky 133864bc412SHans Petter Selasky /* Device Only Drivers */ 134a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0); 135864bc412SHans Petter Selasky DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0); 136a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0); 137d42cc946SOleksandr Tymoshenko DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0); 13802ac6454SAndrew Thompson 139268ae63aSHans Petter Selasky /* Dual Mode Drivers */ 140268ae63aSHans Petter Selasky DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0); 1410b4dc07dSHans Petter Selasky DRIVER_MODULE(usbus, saf1761otg, usb_driver, usb_devclass, 0, 0); 142268ae63aSHans Petter Selasky 14302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 144a593f6b8SAndrew Thompson * usb_probe 14502ac6454SAndrew Thompson * 14602ac6454SAndrew Thompson * This function is called from "{ehci,ohci,uhci}_pci_attach()". 14702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 14802ac6454SAndrew Thompson static int 149a593f6b8SAndrew Thompson usb_probe(device_t dev) 15002ac6454SAndrew Thompson { 15102ac6454SAndrew Thompson DPRINTF("\n"); 15202ac6454SAndrew Thompson return (0); 15302ac6454SAndrew Thompson } 15402ac6454SAndrew Thompson 155d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 1563df007ceSHans Petter Selasky static void 1573df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus) 1583df007ceSHans Petter Selasky { 1593df007ceSHans Petter Selasky if (bus->bus_roothold != NULL) { 1603df007ceSHans Petter Selasky DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold); 1613df007ceSHans Petter Selasky root_mount_rel(bus->bus_roothold); 1623df007ceSHans Petter Selasky bus->bus_roothold = NULL; 1633df007ceSHans Petter Selasky } 1643df007ceSHans Petter Selasky } 165d2b99310SHans Petter Selasky #endif 1663df007ceSHans Petter Selasky 16702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 168a593f6b8SAndrew Thompson * usb_attach 16902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 17002ac6454SAndrew Thompson static int 171a593f6b8SAndrew Thompson usb_attach(device_t dev) 17202ac6454SAndrew Thompson { 173760bc48eSAndrew Thompson struct usb_bus *bus = device_get_ivars(dev); 17402ac6454SAndrew Thompson 17502ac6454SAndrew Thompson DPRINTF("\n"); 17602ac6454SAndrew Thompson 17702ac6454SAndrew Thompson if (bus == NULL) { 178767cb2e2SAndrew Thompson device_printf(dev, "USB device has no ivars\n"); 17902ac6454SAndrew Thompson return (ENXIO); 18002ac6454SAndrew Thompson } 18102ac6454SAndrew Thompson 182d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 183a0c61406SAlfred Perlstein if (usb_no_boot_wait == 0) { 18402ac6454SAndrew Thompson /* delay vfs_mountroot until the bus is explored */ 185853a10a5SAndrew Thompson bus->bus_roothold = root_mount_hold(device_get_nameunit(dev)); 186a0c61406SAlfred Perlstein } 187d2b99310SHans Petter Selasky #endif 188a593f6b8SAndrew Thompson usb_attach_sub(dev, bus); 18934b48722SAlfred Perlstein 19002ac6454SAndrew Thompson return (0); /* return success */ 19102ac6454SAndrew Thompson } 19202ac6454SAndrew Thompson 19302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 194a593f6b8SAndrew Thompson * usb_detach 19502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 19602ac6454SAndrew Thompson static int 197a593f6b8SAndrew Thompson usb_detach(device_t dev) 19802ac6454SAndrew Thompson { 199760bc48eSAndrew Thompson struct usb_bus *bus = device_get_softc(dev); 20002ac6454SAndrew Thompson 20102ac6454SAndrew Thompson DPRINTF("\n"); 20202ac6454SAndrew Thompson 20302ac6454SAndrew Thompson if (bus == NULL) { 20402ac6454SAndrew Thompson /* was never setup properly */ 20502ac6454SAndrew Thompson return (0); 20602ac6454SAndrew Thompson } 20702ac6454SAndrew Thompson /* Stop power watchdog */ 208a593f6b8SAndrew Thompson usb_callout_drain(&bus->power_wdog); 20902ac6454SAndrew Thompson 210d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 21102ac6454SAndrew Thompson /* Let the USB explore process detach all devices. */ 2123df007ceSHans Petter Selasky usb_root_mount_rel(bus); 213d2b99310SHans Petter Selasky #endif 21402ac6454SAndrew Thompson 21502ac6454SAndrew Thompson USB_BUS_LOCK(bus); 21602ac6454SAndrew Thompson 2172e141748SHans Petter Selasky /* Queue detach job */ 2189b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2192e141748SHans Petter Selasky &bus->detach_msg[0], &bus->detach_msg[1]); 2202e141748SHans Petter Selasky 2212e141748SHans Petter Selasky /* Wait for detach to complete */ 2229b3a48eeSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 22302ac6454SAndrew Thompson &bus->detach_msg[0], &bus->detach_msg[1]); 22402ac6454SAndrew Thompson 22502ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 22602ac6454SAndrew Thompson 2279b3a48eeSHans Petter Selasky #if USB_HAVE_PER_BUS_PROCESS 22802ac6454SAndrew Thompson /* Get rid of USB callback processes */ 22902ac6454SAndrew Thompson 2309b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_GIANT_PROC(bus)); 2319b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_NON_GIANT_PROC(bus)); 23202ac6454SAndrew Thompson 23302ac6454SAndrew Thompson /* Get rid of USB explore process */ 23402ac6454SAndrew Thompson 2359b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_EXPLORE_PROC(bus)); 23602ac6454SAndrew Thompson 237672c9965SAndrew Thompson /* Get rid of control transfer process */ 238672c9965SAndrew Thompson 2399b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus)); 2409b3a48eeSHans Petter Selasky #endif 241672c9965SAndrew Thompson 2428be09334SHans Petter Selasky #if USB_HAVE_PF 243fe1c24e3SWeongyo Jeong usbpf_detach(bus); 2448be09334SHans Petter Selasky #endif 24502ac6454SAndrew Thompson return (0); 24602ac6454SAndrew Thompson } 24702ac6454SAndrew Thompson 24802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2492e141748SHans Petter Selasky * usb_suspend 2502e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2512e141748SHans Petter Selasky static int 2522e141748SHans Petter Selasky usb_suspend(device_t dev) 2532e141748SHans Petter Selasky { 2542e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2552e141748SHans Petter Selasky 2562e141748SHans Petter Selasky DPRINTF("\n"); 2572e141748SHans Petter Selasky 2582e141748SHans Petter Selasky if (bus == NULL) { 2592e141748SHans Petter Selasky /* was never setup properly */ 2602e141748SHans Petter Selasky return (0); 2612e141748SHans Petter Selasky } 2622e141748SHans Petter Selasky 2632e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2649b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2652e141748SHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 26602f728afSHans Petter Selasky if (usb_no_suspend_wait == 0) { 26702f728afSHans Petter Selasky /* wait for suspend callback to be executed */ 2689b3a48eeSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 26902f728afSHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 27002f728afSHans Petter Selasky } 2712e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2722e141748SHans Petter Selasky 2732e141748SHans Petter Selasky return (0); 2742e141748SHans Petter Selasky } 2752e141748SHans Petter Selasky 2762e141748SHans Petter Selasky /*------------------------------------------------------------------------* 2772e141748SHans Petter Selasky * usb_resume 2782e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2792e141748SHans Petter Selasky static int 2802e141748SHans Petter Selasky usb_resume(device_t dev) 2812e141748SHans Petter Selasky { 2822e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2832e141748SHans Petter Selasky 2842e141748SHans Petter Selasky DPRINTF("\n"); 2852e141748SHans Petter Selasky 2862e141748SHans Petter Selasky if (bus == NULL) { 2872e141748SHans Petter Selasky /* was never setup properly */ 2882e141748SHans Petter Selasky return (0); 2892e141748SHans Petter Selasky } 2902e141748SHans Petter Selasky 2912e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2929b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2932e141748SHans Petter Selasky &bus->resume_msg[0], &bus->resume_msg[1]); 2942e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2952e141748SHans Petter Selasky 2962e141748SHans Petter Selasky return (0); 2972e141748SHans Petter Selasky } 2982e141748SHans Petter Selasky 2992e141748SHans Petter Selasky /*------------------------------------------------------------------------* 300563ab081SHans Petter Selasky * usb_bus_reset_async_locked 301563ab081SHans Petter Selasky *------------------------------------------------------------------------*/ 302563ab081SHans Petter Selasky void 303563ab081SHans Petter Selasky usb_bus_reset_async_locked(struct usb_bus *bus) 304563ab081SHans Petter Selasky { 305563ab081SHans Petter Selasky USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 306563ab081SHans Petter Selasky 307563ab081SHans Petter Selasky DPRINTF("\n"); 308563ab081SHans Petter Selasky 309563ab081SHans Petter Selasky if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL || 310563ab081SHans Petter Selasky bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) { 311563ab081SHans Petter Selasky DPRINTF("Reset already pending\n"); 312563ab081SHans Petter Selasky return; 313563ab081SHans Petter Selasky } 314563ab081SHans Petter Selasky 315563ab081SHans Petter Selasky device_printf(bus->parent, "Resetting controller\n"); 316563ab081SHans Petter Selasky 317563ab081SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 318563ab081SHans Petter Selasky &bus->reset_msg[0], &bus->reset_msg[1]); 319563ab081SHans Petter Selasky } 320563ab081SHans Petter Selasky 321563ab081SHans Petter Selasky /*------------------------------------------------------------------------* 3222e141748SHans Petter Selasky * usb_shutdown 3232e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 3242e141748SHans Petter Selasky static int 3252e141748SHans Petter Selasky usb_shutdown(device_t dev) 3262e141748SHans Petter Selasky { 3272e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 3282e141748SHans Petter Selasky 3292e141748SHans Petter Selasky DPRINTF("\n"); 3302e141748SHans Petter Selasky 3312e141748SHans Petter Selasky if (bus == NULL) { 3322e141748SHans Petter Selasky /* was never setup properly */ 3332e141748SHans Petter Selasky return (0); 3342e141748SHans Petter Selasky } 3352e141748SHans Petter Selasky 3365ed294aeSHans Petter Selasky device_printf(bus->bdev, "Controller shutdown\n"); 3375ed294aeSHans Petter Selasky 3382e141748SHans Petter Selasky USB_BUS_LOCK(bus); 3399b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 3402e141748SHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 3415ed294aeSHans Petter Selasky if (usb_no_shutdown_wait == 0) { 3425ed294aeSHans Petter Selasky /* wait for shutdown callback to be executed */ 3439b3a48eeSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 3445ed294aeSHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 3455ed294aeSHans Petter Selasky } 3462e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 3472e141748SHans Petter Selasky 3485ed294aeSHans Petter Selasky device_printf(bus->bdev, "Controller shutdown complete\n"); 3495ed294aeSHans Petter Selasky 3502e141748SHans Petter Selasky return (0); 3512e141748SHans Petter Selasky } 3522e141748SHans Petter Selasky 3532e141748SHans Petter Selasky /*------------------------------------------------------------------------* 354a593f6b8SAndrew Thompson * usb_bus_explore 35502ac6454SAndrew Thompson * 35602ac6454SAndrew Thompson * This function is used to explore the device tree from the root. 35702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 35802ac6454SAndrew Thompson static void 359a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm) 36002ac6454SAndrew Thompson { 361760bc48eSAndrew Thompson struct usb_bus *bus; 362760bc48eSAndrew Thompson struct usb_device *udev; 36302ac6454SAndrew Thompson 364760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 36502ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 36602ac6454SAndrew Thompson 3672e141748SHans Petter Selasky if (bus->no_explore != 0) 3682e141748SHans Petter Selasky return; 3692e141748SHans Petter Selasky 370*d64e9217SHans Petter Selasky if (udev != NULL) { 371*d64e9217SHans Petter Selasky USB_BUS_UNLOCK(bus); 372*d64e9217SHans Petter Selasky uhub_explore_handle_re_enumerate(udev); 373*d64e9217SHans Petter Selasky USB_BUS_LOCK(bus); 374*d64e9217SHans Petter Selasky } 375*d64e9217SHans Petter Selasky 376*d64e9217SHans Petter Selasky if (udev != NULL && udev->hub != NULL) { 37702ac6454SAndrew Thompson 37802ac6454SAndrew Thompson if (bus->do_probe) { 37902ac6454SAndrew Thompson bus->do_probe = 0; 38002ac6454SAndrew Thompson bus->driver_added_refcount++; 38102ac6454SAndrew Thompson } 38202ac6454SAndrew Thompson if (bus->driver_added_refcount == 0) { 38302ac6454SAndrew Thompson /* avoid zero, hence that is memory default */ 38402ac6454SAndrew Thompson bus->driver_added_refcount = 1; 38502ac6454SAndrew Thompson } 38602ac6454SAndrew Thompson 387f0c078e6SAndrew Thompson #ifdef DDB 38834b48722SAlfred Perlstein /* 38934b48722SAlfred Perlstein * The following three lines of code are only here to 39034b48722SAlfred Perlstein * recover from DDB: 39134b48722SAlfred Perlstein */ 3929b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus)); 3939b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus)); 3949b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_NON_GIANT_PROC(bus)); 395f0c078e6SAndrew Thompson #endif 39634b48722SAlfred Perlstein 39734b48722SAlfred Perlstein USB_BUS_UNLOCK(bus); 398a56fe095SJohn Baldwin 399e727a16cSAndrew Thompson #if USB_HAVE_POWERD 40002ac6454SAndrew Thompson /* 40102ac6454SAndrew Thompson * First update the USB power state! 40202ac6454SAndrew Thompson */ 403a593f6b8SAndrew Thompson usb_bus_powerd(bus); 404e727a16cSAndrew Thompson #endif 40534b48722SAlfred Perlstein /* Explore the Root USB HUB. */ 40602ac6454SAndrew Thompson (udev->hub->explore) (udev); 40702ac6454SAndrew Thompson USB_BUS_LOCK(bus); 40802ac6454SAndrew Thompson } 409d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 4103df007ceSHans Petter Selasky usb_root_mount_rel(bus); 411d2b99310SHans Petter Selasky #endif 41202ac6454SAndrew Thompson } 41302ac6454SAndrew Thompson 41402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 415a593f6b8SAndrew Thompson * usb_bus_detach 41602ac6454SAndrew Thompson * 41702ac6454SAndrew Thompson * This function is used to detach the device tree from the root. 41802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 41902ac6454SAndrew Thompson static void 420a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm) 42102ac6454SAndrew Thompson { 422760bc48eSAndrew Thompson struct usb_bus *bus; 423760bc48eSAndrew Thompson struct usb_device *udev; 42402ac6454SAndrew Thompson device_t dev; 42502ac6454SAndrew Thompson 426760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 42702ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 42802ac6454SAndrew Thompson dev = bus->bdev; 42902ac6454SAndrew Thompson /* clear the softc */ 43002ac6454SAndrew Thompson device_set_softc(dev, NULL); 43102ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 43202ac6454SAndrew Thompson 43302ac6454SAndrew Thompson /* detach children first */ 43434b48722SAlfred Perlstein mtx_lock(&Giant); 43502ac6454SAndrew Thompson bus_generic_detach(dev); 43634b48722SAlfred Perlstein mtx_unlock(&Giant); 43702ac6454SAndrew Thompson 43802ac6454SAndrew Thompson /* 439d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 44002ac6454SAndrew Thompson */ 441d88688c7SAndrew Thompson usb_free_device(udev, 0); 44202ac6454SAndrew Thompson 44302ac6454SAndrew Thompson USB_BUS_LOCK(bus); 44402ac6454SAndrew Thompson /* clear bdev variable last */ 44502ac6454SAndrew Thompson bus->bdev = NULL; 44602ac6454SAndrew Thompson } 44702ac6454SAndrew Thompson 4482e141748SHans Petter Selasky /*------------------------------------------------------------------------* 4492e141748SHans Petter Selasky * usb_bus_suspend 4502e141748SHans Petter Selasky * 451468f354bSHans Petter Selasky * This function is used to suspend the USB controller. 4522e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 4532e141748SHans Petter Selasky static void 4542e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm) 4552e141748SHans Petter Selasky { 4562e141748SHans Petter Selasky struct usb_bus *bus; 4572e141748SHans Petter Selasky struct usb_device *udev; 4582e141748SHans Petter Selasky usb_error_t err; 459a18a7a41SHans Petter Selasky uint8_t do_unlock; 4602e141748SHans Petter Selasky 461563ab081SHans Petter Selasky DPRINTF("\n"); 462563ab081SHans Petter Selasky 4632e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4642e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4652e141748SHans Petter Selasky 4662e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 4672e141748SHans Petter Selasky return; 4682e141748SHans Petter Selasky 4696bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 4706bd3e535SHans Petter Selasky 47102f728afSHans Petter Selasky /* 47202f728afSHans Petter Selasky * We use the shutdown event here because the suspend and 47302f728afSHans Petter Selasky * resume events are reserved for the USB port suspend and 47402f728afSHans Petter Selasky * resume. The USB system suspend is implemented like full 47502f728afSHans Petter Selasky * shutdown and all connected USB devices will be disconnected 47602f728afSHans Petter Selasky * subsequently. At resume all USB devices will be 47702f728afSHans Petter Selasky * re-connected again. 47802f728afSHans Petter Selasky */ 47902f728afSHans Petter Selasky 4802e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 4812e141748SHans Petter Selasky 482a18a7a41SHans Petter Selasky do_unlock = usbd_enum_lock(udev); 4832e141748SHans Petter Selasky 4842e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 4852e141748SHans Petter Selasky if (err) 4862e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 4872e141748SHans Petter Selasky 4882e141748SHans Petter Selasky USB_BUS_LOCK(bus); 4892e141748SHans Petter Selasky bus->hw_power_state = 0; 4902e141748SHans Petter Selasky bus->no_explore = 1; 4912e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 4922e141748SHans Petter Selasky 4932e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 4942e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 4952e141748SHans Petter Selasky 4962e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 4972e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 4982e141748SHans Petter Selasky 499a18a7a41SHans Petter Selasky if (do_unlock) 5002e141748SHans Petter Selasky usbd_enum_unlock(udev); 5016bd3e535SHans Petter Selasky 5026bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5032e141748SHans Petter Selasky } 5042e141748SHans Petter Selasky 5052e141748SHans Petter Selasky /*------------------------------------------------------------------------* 5062e141748SHans Petter Selasky * usb_bus_resume 5072e141748SHans Petter Selasky * 508468f354bSHans Petter Selasky * This function is used to resume the USB controller. 5092e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 5102e141748SHans Petter Selasky static void 5112e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm) 5122e141748SHans Petter Selasky { 5132e141748SHans Petter Selasky struct usb_bus *bus; 5142e141748SHans Petter Selasky struct usb_device *udev; 5152e141748SHans Petter Selasky usb_error_t err; 516a18a7a41SHans Petter Selasky uint8_t do_unlock; 5172e141748SHans Petter Selasky 518563ab081SHans Petter Selasky DPRINTF("\n"); 519563ab081SHans Petter Selasky 5202e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 5212e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 5222e141748SHans Petter Selasky 5232e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 5242e141748SHans Petter Selasky return; 5252e141748SHans Petter Selasky 5266bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 5276bd3e535SHans Petter Selasky 528a18a7a41SHans Petter Selasky do_unlock = usbd_enum_lock(udev); 5292e141748SHans Petter Selasky #if 0 5302e141748SHans Petter Selasky DEVMETHOD(usb_take_controller, NULL); /* dummy */ 5312e141748SHans Petter Selasky #endif 5322e141748SHans Petter Selasky USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 5332e141748SHans Petter Selasky 5342e141748SHans Petter Selasky USB_BUS_LOCK(bus); 5352e141748SHans Petter Selasky bus->hw_power_state = 5362e141748SHans Petter Selasky USB_HW_POWER_CONTROL | 5372e141748SHans Petter Selasky USB_HW_POWER_BULK | 5382e141748SHans Petter Selasky USB_HW_POWER_INTERRUPT | 5392e141748SHans Petter Selasky USB_HW_POWER_ISOC | 5402e141748SHans Petter Selasky USB_HW_POWER_NON_ROOT_HUB; 5412e141748SHans Petter Selasky bus->no_explore = 0; 5422e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 5432e141748SHans Petter Selasky 5442e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 5452e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 5462e141748SHans Petter Selasky 5472e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 5482e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 5492e141748SHans Petter Selasky 5506bbe7cdfSHans Petter Selasky /* restore USB configuration to index 0 */ 5512e141748SHans Petter Selasky err = usbd_set_config_index(udev, 0); 5522e141748SHans Petter Selasky if (err) 5532e141748SHans Petter Selasky device_printf(bus->bdev, "Could not configure root HUB\n"); 5542e141748SHans Petter Selasky 5556bbe7cdfSHans Petter Selasky /* probe and attach */ 5566bbe7cdfSHans Petter Selasky err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY); 5576bbe7cdfSHans Petter Selasky if (err) { 5586bbe7cdfSHans Petter Selasky device_printf(bus->bdev, "Could not probe and " 5596bbe7cdfSHans Petter Selasky "attach root HUB\n"); 5606bbe7cdfSHans Petter Selasky } 5616bbe7cdfSHans Petter Selasky 562a18a7a41SHans Petter Selasky if (do_unlock) 5632e141748SHans Petter Selasky usbd_enum_unlock(udev); 5646bd3e535SHans Petter Selasky 5656bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5662e141748SHans Petter Selasky } 5672e141748SHans Petter Selasky 5682e141748SHans Petter Selasky /*------------------------------------------------------------------------* 569563ab081SHans Petter Selasky * usb_bus_reset 570563ab081SHans Petter Selasky * 571468f354bSHans Petter Selasky * This function is used to reset the USB controller. 572563ab081SHans Petter Selasky *------------------------------------------------------------------------*/ 573563ab081SHans Petter Selasky static void 574563ab081SHans Petter Selasky usb_bus_reset(struct usb_proc_msg *pm) 575563ab081SHans Petter Selasky { 576563ab081SHans Petter Selasky struct usb_bus *bus; 577563ab081SHans Petter Selasky 578563ab081SHans Petter Selasky DPRINTF("\n"); 579563ab081SHans Petter Selasky 580563ab081SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 581563ab081SHans Petter Selasky 582563ab081SHans Petter Selasky if (bus->bdev == NULL || bus->no_explore != 0) 583563ab081SHans Petter Selasky return; 584563ab081SHans Petter Selasky 585563ab081SHans Petter Selasky /* a suspend and resume will reset the USB controller */ 586563ab081SHans Petter Selasky usb_bus_suspend(pm); 587563ab081SHans Petter Selasky usb_bus_resume(pm); 588563ab081SHans Petter Selasky } 589563ab081SHans Petter Selasky 590563ab081SHans Petter Selasky /*------------------------------------------------------------------------* 5912e141748SHans Petter Selasky * usb_bus_shutdown 5922e141748SHans Petter Selasky * 593468f354bSHans Petter Selasky * This function is used to shutdown the USB controller. 5942e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 5952e141748SHans Petter Selasky static void 5962e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm) 5972e141748SHans Petter Selasky { 5982e141748SHans Petter Selasky struct usb_bus *bus; 5992e141748SHans Petter Selasky struct usb_device *udev; 6002e141748SHans Petter Selasky usb_error_t err; 601a18a7a41SHans Petter Selasky uint8_t do_unlock; 6022e141748SHans Petter Selasky 6032e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 6042e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 6052e141748SHans Petter Selasky 6062e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 6072e141748SHans Petter Selasky return; 6082e141748SHans Petter Selasky 6096bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 6106bd3e535SHans Petter Selasky 6112e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 6122e141748SHans Petter Selasky 613a18a7a41SHans Petter Selasky do_unlock = usbd_enum_lock(udev); 6142e141748SHans Petter Selasky 6152e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 6162e141748SHans Petter Selasky if (err) 6172e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 6182e141748SHans Petter Selasky 6192e141748SHans Petter Selasky USB_BUS_LOCK(bus); 6202e141748SHans Petter Selasky bus->hw_power_state = 0; 6212e141748SHans Petter Selasky bus->no_explore = 1; 6222e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 6232e141748SHans Petter Selasky 6242e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 6252e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 6262e141748SHans Petter Selasky 6272e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 6282e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 6292e141748SHans Petter Selasky 630a18a7a41SHans Petter Selasky if (do_unlock) 6312e141748SHans Petter Selasky usbd_enum_unlock(udev); 6326bd3e535SHans Petter Selasky 6336bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 6342e141748SHans Petter Selasky } 6352e141748SHans Petter Selasky 63602ac6454SAndrew Thompson static void 637a593f6b8SAndrew Thompson usb_power_wdog(void *arg) 63802ac6454SAndrew Thompson { 639760bc48eSAndrew Thompson struct usb_bus *bus = arg; 64002ac6454SAndrew Thompson 64102ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 64202ac6454SAndrew Thompson 643a593f6b8SAndrew Thompson usb_callout_reset(&bus->power_wdog, 644a593f6b8SAndrew Thompson 4 * hz, usb_power_wdog, arg); 64502ac6454SAndrew Thompson 646f0c078e6SAndrew Thompson #ifdef DDB 64734b48722SAlfred Perlstein /* 64834b48722SAlfred Perlstein * The following line of code is only here to recover from 64934b48722SAlfred Perlstein * DDB: 65034b48722SAlfred Perlstein */ 6519b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */ 652f0c078e6SAndrew Thompson #endif 65334b48722SAlfred Perlstein 654e727a16cSAndrew Thompson #if USB_HAVE_POWERD 65502ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 65602ac6454SAndrew Thompson 657a593f6b8SAndrew Thompson usb_bus_power_update(bus); 65802ac6454SAndrew Thompson 659684e3f22SAndrew Thompson USB_BUS_LOCK(bus); 660e727a16cSAndrew Thompson #endif 66102ac6454SAndrew Thompson } 66202ac6454SAndrew Thompson 66302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 664a593f6b8SAndrew Thompson * usb_bus_attach 66502ac6454SAndrew Thompson * 66602ac6454SAndrew Thompson * This function attaches USB in context of the explore thread. 66702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 66802ac6454SAndrew Thompson static void 669a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm) 67002ac6454SAndrew Thompson { 671760bc48eSAndrew Thompson struct usb_bus *bus; 672760bc48eSAndrew Thompson struct usb_device *child; 67302ac6454SAndrew Thompson device_t dev; 674e0a69b51SAndrew Thompson usb_error_t err; 6758d2dd5ddSAndrew Thompson enum usb_dev_speed speed; 67602ac6454SAndrew Thompson 677760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 67802ac6454SAndrew Thompson dev = bus->bdev; 67902ac6454SAndrew Thompson 68002ac6454SAndrew Thompson DPRINTF("\n"); 68102ac6454SAndrew Thompson 68202ac6454SAndrew Thompson switch (bus->usbrev) { 68302ac6454SAndrew Thompson case USB_REV_1_0: 68402ac6454SAndrew Thompson speed = USB_SPEED_FULL; 68502ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 68602ac6454SAndrew Thompson break; 68702ac6454SAndrew Thompson 68802ac6454SAndrew Thompson case USB_REV_1_1: 68902ac6454SAndrew Thompson speed = USB_SPEED_FULL; 69002ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 69102ac6454SAndrew Thompson break; 69202ac6454SAndrew Thompson 69302ac6454SAndrew Thompson case USB_REV_2_0: 69402ac6454SAndrew Thompson speed = USB_SPEED_HIGH; 69502ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 69602ac6454SAndrew Thompson break; 69702ac6454SAndrew Thompson 69802ac6454SAndrew Thompson case USB_REV_2_5: 69902ac6454SAndrew Thompson speed = USB_SPEED_VARIABLE; 70002ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 70102ac6454SAndrew Thompson break; 70202ac6454SAndrew Thompson 703963169b4SHans Petter Selasky case USB_REV_3_0: 704963169b4SHans Petter Selasky speed = USB_SPEED_SUPER; 705ccac019aSHans Petter Selasky device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 706963169b4SHans Petter Selasky break; 707963169b4SHans Petter Selasky 70802ac6454SAndrew Thompson default: 709767cb2e2SAndrew Thompson device_printf(bus->bdev, "Unsupported USB revision\n"); 710d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 7113df007ceSHans Petter Selasky usb_root_mount_rel(bus); 712d2b99310SHans Petter Selasky #endif 71302ac6454SAndrew Thompson return; 71402ac6454SAndrew Thompson } 71502ac6454SAndrew Thompson 7164eae601eSAndrew Thompson /* default power_mask value */ 7174eae601eSAndrew Thompson bus->hw_power_state = 7184eae601eSAndrew Thompson USB_HW_POWER_CONTROL | 7194eae601eSAndrew Thompson USB_HW_POWER_BULK | 7204eae601eSAndrew Thompson USB_HW_POWER_INTERRUPT | 7214eae601eSAndrew Thompson USB_HW_POWER_ISOC | 7224eae601eSAndrew Thompson USB_HW_POWER_NON_ROOT_HUB; 7234eae601eSAndrew Thompson 7242e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 7252e141748SHans Petter Selasky 7264eae601eSAndrew Thompson /* make sure power is set at least once */ 7274eae601eSAndrew Thompson 7284eae601eSAndrew Thompson if (bus->methods->set_hw_power != NULL) { 7294eae601eSAndrew Thompson (bus->methods->set_hw_power) (bus); 7304eae601eSAndrew Thompson } 7314eae601eSAndrew Thompson 7322e141748SHans Petter Selasky /* allocate the Root USB device */ 73302ac6454SAndrew Thompson 734a593f6b8SAndrew Thompson child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 73502ac6454SAndrew Thompson speed, USB_MODE_HOST); 73602ac6454SAndrew Thompson if (child) { 737a593f6b8SAndrew Thompson err = usb_probe_and_attach(child, 73802ac6454SAndrew Thompson USB_IFACE_INDEX_ANY); 73902ac6454SAndrew Thompson if (!err) { 7401be5bf51SAndrew Thompson if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 7411be5bf51SAndrew Thompson (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 74202ac6454SAndrew Thompson err = USB_ERR_NO_ROOT_HUB; 74302ac6454SAndrew Thompson } 74402ac6454SAndrew Thompson } 74502ac6454SAndrew Thompson } else { 74602ac6454SAndrew Thompson err = USB_ERR_NOMEM; 74702ac6454SAndrew Thompson } 74802ac6454SAndrew Thompson 74902ac6454SAndrew Thompson USB_BUS_LOCK(bus); 75002ac6454SAndrew Thompson 75102ac6454SAndrew Thompson if (err) { 75202ac6454SAndrew Thompson device_printf(bus->bdev, "Root HUB problem, error=%s\n", 753a593f6b8SAndrew Thompson usbd_errstr(err)); 754d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 7553df007ceSHans Petter Selasky usb_root_mount_rel(bus); 756d2b99310SHans Petter Selasky #endif 75702ac6454SAndrew Thompson } 75802ac6454SAndrew Thompson 75902ac6454SAndrew Thompson /* set softc - we are ready */ 76002ac6454SAndrew Thompson device_set_softc(dev, bus); 76102ac6454SAndrew Thompson 762684e3f22SAndrew Thompson /* start watchdog */ 763a593f6b8SAndrew Thompson usb_power_wdog(bus); 76402ac6454SAndrew Thompson } 76502ac6454SAndrew Thompson 76602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 767a593f6b8SAndrew Thompson * usb_attach_sub 76802ac6454SAndrew Thompson * 76934b48722SAlfred Perlstein * This function creates a thread which runs the USB attach code. 77002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 77102ac6454SAndrew Thompson static void 772a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus) 77302ac6454SAndrew Thompson { 77434b48722SAlfred Perlstein mtx_lock(&Giant); 77534b48722SAlfred Perlstein if (usb_devclass_ptr == NULL) 77634b48722SAlfred Perlstein usb_devclass_ptr = devclass_find("usbus"); 77734b48722SAlfred Perlstein mtx_unlock(&Giant); 77834b48722SAlfred Perlstein 7798be09334SHans Petter Selasky #if USB_HAVE_PF 780fe1c24e3SWeongyo Jeong usbpf_attach(bus); 7818be09334SHans Petter Selasky #endif 78202ac6454SAndrew Thompson /* Initialise USB process messages */ 783a593f6b8SAndrew Thompson bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 78402ac6454SAndrew Thompson bus->explore_msg[0].bus = bus; 785a593f6b8SAndrew Thompson bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 78602ac6454SAndrew Thompson bus->explore_msg[1].bus = bus; 78702ac6454SAndrew Thompson 788a593f6b8SAndrew Thompson bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 78902ac6454SAndrew Thompson bus->detach_msg[0].bus = bus; 790a593f6b8SAndrew Thompson bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 79102ac6454SAndrew Thompson bus->detach_msg[1].bus = bus; 79202ac6454SAndrew Thompson 793a593f6b8SAndrew Thompson bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 79402ac6454SAndrew Thompson bus->attach_msg[0].bus = bus; 795a593f6b8SAndrew Thompson bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 79602ac6454SAndrew Thompson bus->attach_msg[1].bus = bus; 79702ac6454SAndrew Thompson 7982e141748SHans Petter Selasky bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 7992e141748SHans Petter Selasky bus->suspend_msg[0].bus = bus; 8002e141748SHans Petter Selasky bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 8012e141748SHans Petter Selasky bus->suspend_msg[1].bus = bus; 8022e141748SHans Petter Selasky 8032e141748SHans Petter Selasky bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 8042e141748SHans Petter Selasky bus->resume_msg[0].bus = bus; 8052e141748SHans Petter Selasky bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 8062e141748SHans Petter Selasky bus->resume_msg[1].bus = bus; 8072e141748SHans Petter Selasky 808563ab081SHans Petter Selasky bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset; 809563ab081SHans Petter Selasky bus->reset_msg[0].bus = bus; 810563ab081SHans Petter Selasky bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset; 811563ab081SHans Petter Selasky bus->reset_msg[1].bus = bus; 812563ab081SHans Petter Selasky 8132e141748SHans Petter Selasky bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 8142e141748SHans Petter Selasky bus->shutdown_msg[0].bus = bus; 8152e141748SHans Petter Selasky bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 8162e141748SHans Petter Selasky bus->shutdown_msg[1].bus = bus; 8172e141748SHans Petter Selasky 8189b3a48eeSHans Petter Selasky #if USB_HAVE_PER_BUS_PROCESS 81939307315SAndrew Thompson /* Create USB explore and callback processes */ 82002ac6454SAndrew Thompson 8219b3a48eeSHans Petter Selasky if (usb_proc_create(USB_BUS_GIANT_PROC(bus), 8229b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 82388334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB Giant " 82402ac6454SAndrew Thompson "callback process failed.\n"); 8259b3a48eeSHans Petter Selasky } else if (usb_proc_create(USB_BUS_NON_GIANT_PROC(bus), 8269b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) { 82788334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB non-Giant " 82802ac6454SAndrew Thompson "callback process failed.\n"); 8299b3a48eeSHans Petter Selasky } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus), 8309b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 83188334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB explore " 83202ac6454SAndrew Thompson "process failed.\n"); 8339b3a48eeSHans Petter Selasky } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus), 8349b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 83588334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB control transfer " 836672c9965SAndrew Thompson "process failed.\n"); 8379b3a48eeSHans Petter Selasky } else 8389b3a48eeSHans Petter Selasky #endif 8399b3a48eeSHans Petter Selasky { 84002ac6454SAndrew Thompson /* Get final attach going */ 84102ac6454SAndrew Thompson USB_BUS_LOCK(bus); 8429b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 8432e141748SHans Petter Selasky &bus->attach_msg[0], &bus->attach_msg[1]); 84402ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 84534b48722SAlfred Perlstein 84634b48722SAlfred Perlstein /* Do initial explore */ 84734b48722SAlfred Perlstein usb_needs_explore(bus, 1); 84802ac6454SAndrew Thompson } 84902ac6454SAndrew Thompson } 850a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 85102ac6454SAndrew Thompson 85202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 853a593f6b8SAndrew Thompson * usb_bus_mem_flush_all_cb 85402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 855bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 85602ac6454SAndrew Thompson static void 857a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 858f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 85902ac6454SAndrew Thompson { 860a593f6b8SAndrew Thompson usb_pc_cpu_flush(pc); 86102ac6454SAndrew Thompson } 862bdc081c6SAndrew Thompson #endif 86302ac6454SAndrew Thompson 86402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 865a593f6b8SAndrew Thompson * usb_bus_mem_flush_all - factored out code 86602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 867bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 86802ac6454SAndrew Thompson void 869a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 87002ac6454SAndrew Thompson { 87102ac6454SAndrew Thompson if (cb) { 872a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_flush_all_cb); 87302ac6454SAndrew Thompson } 87402ac6454SAndrew Thompson } 875bdc081c6SAndrew Thompson #endif 87602ac6454SAndrew Thompson 87702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 878a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all_cb 87902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 880bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 88102ac6454SAndrew Thompson static void 882a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 883f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 88402ac6454SAndrew Thompson { 88502ac6454SAndrew Thompson /* need to initialize the page cache */ 88602ac6454SAndrew Thompson pc->tag_parent = bus->dma_parent_tag; 88702ac6454SAndrew Thompson 888a593f6b8SAndrew Thompson if (usb_pc_alloc_mem(pc, pg, size, align)) { 88902ac6454SAndrew Thompson bus->alloc_failed = 1; 89002ac6454SAndrew Thompson } 89102ac6454SAndrew Thompson } 892bdc081c6SAndrew Thompson #endif 89302ac6454SAndrew Thompson 89402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 895a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all - factored out code 89602ac6454SAndrew Thompson * 89702ac6454SAndrew Thompson * Returns: 89802ac6454SAndrew Thompson * 0: Success 89902ac6454SAndrew Thompson * Else: Failure 90002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 90102ac6454SAndrew Thompson uint8_t 902a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 903e0a69b51SAndrew Thompson usb_bus_mem_cb_t *cb) 90402ac6454SAndrew Thompson { 90502ac6454SAndrew Thompson bus->alloc_failed = 0; 90602ac6454SAndrew Thompson 90702ac6454SAndrew Thompson mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 9084a4da38fSHans Petter Selasky "usb_def_mtx", MTX_DEF | MTX_RECURSE); 90902ac6454SAndrew Thompson 9102fe7ad87SHans Petter Selasky mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent), 9114a4da38fSHans Petter Selasky "usb_spin_mtx", MTX_SPIN | MTX_RECURSE); 9122fe7ad87SHans Petter Selasky 913a593f6b8SAndrew Thompson usb_callout_init_mtx(&bus->power_wdog, 914684e3f22SAndrew Thompson &bus->bus_mtx, 0); 91502ac6454SAndrew Thompson 91602ac6454SAndrew Thompson TAILQ_INIT(&bus->intr_q.head); 91702ac6454SAndrew Thompson 918bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 919a593f6b8SAndrew Thompson usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 920bdc081c6SAndrew Thompson dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 921bdc081c6SAndrew Thompson #endif 92202ac6454SAndrew Thompson if ((bus->devices_max > USB_MAX_DEVICES) || 92302ac6454SAndrew Thompson (bus->devices_max < USB_MIN_DEVICES) || 92402ac6454SAndrew Thompson (bus->devices == NULL)) { 92502ac6454SAndrew Thompson DPRINTFN(0, "Devices field has not been " 926767cb2e2SAndrew Thompson "initialised properly\n"); 92702ac6454SAndrew Thompson bus->alloc_failed = 1; /* failure */ 92802ac6454SAndrew Thompson } 929bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 93002ac6454SAndrew Thompson if (cb) { 931a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_alloc_all_cb); 93202ac6454SAndrew Thompson } 933bdc081c6SAndrew Thompson #endif 93402ac6454SAndrew Thompson if (bus->alloc_failed) { 935a593f6b8SAndrew Thompson usb_bus_mem_free_all(bus, cb); 93602ac6454SAndrew Thompson } 93702ac6454SAndrew Thompson return (bus->alloc_failed); 93802ac6454SAndrew Thompson } 93902ac6454SAndrew Thompson 94002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 941a593f6b8SAndrew Thompson * usb_bus_mem_free_all_cb 94202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 943bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 94402ac6454SAndrew Thompson static void 945a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 946f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 94702ac6454SAndrew Thompson { 948a593f6b8SAndrew Thompson usb_pc_free_mem(pc); 94902ac6454SAndrew Thompson } 950bdc081c6SAndrew Thompson #endif 95102ac6454SAndrew Thompson 95202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 953a593f6b8SAndrew Thompson * usb_bus_mem_free_all - factored out code 95402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 95502ac6454SAndrew Thompson void 956a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 95702ac6454SAndrew Thompson { 958bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 95902ac6454SAndrew Thompson if (cb) { 960a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_free_all_cb); 96102ac6454SAndrew Thompson } 962a593f6b8SAndrew Thompson usb_dma_tag_unsetup(bus->dma_parent_tag); 963bdc081c6SAndrew Thompson #endif 96402ac6454SAndrew Thompson 96502ac6454SAndrew Thompson mtx_destroy(&bus->bus_mtx); 9662fe7ad87SHans Petter Selasky mtx_destroy(&bus->bus_spin_lock); 96702ac6454SAndrew Thompson } 968751aaf5aSHans Petter Selasky 969751aaf5aSHans Petter Selasky /* convenience wrappers */ 970751aaf5aSHans Petter Selasky void 971751aaf5aSHans Petter Selasky usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2) 972751aaf5aSHans Petter Selasky { 973751aaf5aSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2); 974751aaf5aSHans Petter Selasky } 975751aaf5aSHans Petter Selasky 976751aaf5aSHans Petter Selasky void * 977751aaf5aSHans Petter Selasky usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2) 978751aaf5aSHans Petter Selasky { 979751aaf5aSHans Petter Selasky return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2)); 980751aaf5aSHans Petter Selasky } 981751aaf5aSHans Petter Selasky 982751aaf5aSHans Petter Selasky void 983751aaf5aSHans Petter Selasky usb_proc_explore_lock(struct usb_device *udev) 984751aaf5aSHans Petter Selasky { 985751aaf5aSHans Petter Selasky USB_BUS_LOCK(udev->bus); 986751aaf5aSHans Petter Selasky } 987751aaf5aSHans Petter Selasky 988751aaf5aSHans Petter Selasky void 989751aaf5aSHans Petter Selasky usb_proc_explore_unlock(struct usb_device *udev) 990751aaf5aSHans Petter Selasky { 991751aaf5aSHans Petter Selasky USB_BUS_UNLOCK(udev->bus); 992751aaf5aSHans Petter Selasky } 993