102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 3718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4718cf2ccSPedro F. Giffuni * 502ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 802ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 902ac6454SAndrew Thompson * are met: 1002ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1202ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1402ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1502ac6454SAndrew Thompson * 1602ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1702ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1802ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1902ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2002ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2102ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2202ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2302ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2402ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2502ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2602ac6454SAndrew Thompson * SUCH DAMAGE. 2702ac6454SAndrew Thompson */ 2802ac6454SAndrew Thompson 29d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 31d2b99310SHans Petter Selasky #else 32f0c078e6SAndrew Thompson #include "opt_ddb.h" 33f0c078e6SAndrew Thompson 34ed6d949aSAndrew Thompson #include <sys/stdint.h> 35ed6d949aSAndrew Thompson #include <sys/stddef.h> 36ed6d949aSAndrew Thompson #include <sys/param.h> 37ed6d949aSAndrew Thompson #include <sys/queue.h> 38ed6d949aSAndrew Thompson #include <sys/types.h> 39ed6d949aSAndrew Thompson #include <sys/systm.h> 40ed6d949aSAndrew Thompson #include <sys/kernel.h> 41ed6d949aSAndrew Thompson #include <sys/bus.h> 42ed6d949aSAndrew Thompson #include <sys/module.h> 43ed6d949aSAndrew Thompson #include <sys/lock.h> 44ed6d949aSAndrew Thompson #include <sys/mutex.h> 45ed6d949aSAndrew Thompson #include <sys/condvar.h> 46ed6d949aSAndrew Thompson #include <sys/sysctl.h> 47ed6d949aSAndrew Thompson #include <sys/sx.h> 48ed6d949aSAndrew Thompson #include <sys/unistd.h> 49ed6d949aSAndrew Thompson #include <sys/callout.h> 50ed6d949aSAndrew Thompson #include <sys/malloc.h> 51ed6d949aSAndrew Thompson #include <sys/priv.h> 52ed6d949aSAndrew Thompson 5302ac6454SAndrew Thompson #include <dev/usb/usb.h> 54ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 5502ac6454SAndrew Thompson 56a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_ctrl_debug 5702ac6454SAndrew Thompson 5802ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 5902ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6002ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 6102ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 6202ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 6302ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 64b78e84d1SHans Petter Selasky #include <dev/usb/usb_dev.h> 6502ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 6602ac6454SAndrew Thompson 6702ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 6802ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 6918ec6525SWeongyo Jeong #include <dev/usb/usb_pf.h> 702e141748SHans Petter Selasky #include "usb_if.h" 71d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 7202ac6454SAndrew Thompson 7302ac6454SAndrew Thompson /* function prototypes */ 7402ac6454SAndrew Thompson 75a593f6b8SAndrew Thompson static device_probe_t usb_probe; 76a593f6b8SAndrew Thompson static device_attach_t usb_attach; 77a593f6b8SAndrew Thompson static device_detach_t usb_detach; 782e141748SHans Petter Selasky static device_suspend_t usb_suspend; 792e141748SHans Petter Selasky static device_resume_t usb_resume; 802e141748SHans Petter Selasky static device_shutdown_t usb_shutdown; 8102ac6454SAndrew Thompson 82a593f6b8SAndrew Thompson static void usb_attach_sub(device_t, struct usb_bus *); 8302ac6454SAndrew Thompson 8402ac6454SAndrew Thompson /* static variables */ 8502ac6454SAndrew Thompson 86ed6d949aSAndrew Thompson #ifdef USB_DEBUG 87a593f6b8SAndrew Thompson static int usb_ctrl_debug = 0; 8802ac6454SAndrew Thompson 89*f8d2b1f3SPawel Biernacki static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 90*f8d2b1f3SPawel Biernacki "USB controller"); 91ece4b0bdSHans Petter Selasky SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RWTUN, &usb_ctrl_debug, 0, 9202ac6454SAndrew Thompson "Debug level"); 9302ac6454SAndrew Thompson #endif 9402ac6454SAndrew Thompson 95d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 96a0c61406SAlfred Perlstein static int usb_no_boot_wait = 0; 97af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0, 985ed294aeSHans Petter Selasky "No USB device enumerate waiting at boot."); 99d2b99310SHans Petter Selasky #endif 1005ed294aeSHans Petter Selasky 10102f728afSHans Petter Selasky static int usb_no_suspend_wait = 0; 102af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RWTUN, 10302f728afSHans Petter Selasky &usb_no_suspend_wait, 0, "No USB device waiting at system suspend."); 10402f728afSHans Petter Selasky 1055ed294aeSHans Petter Selasky static int usb_no_shutdown_wait = 0; 106af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RWTUN, 10702f728afSHans Petter Selasky &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown."); 108a0c61406SAlfred Perlstein 109a593f6b8SAndrew Thompson static devclass_t usb_devclass; 11002ac6454SAndrew Thompson 111a593f6b8SAndrew Thompson static device_method_t usb_methods[] = { 112a593f6b8SAndrew Thompson DEVMETHOD(device_probe, usb_probe), 113a593f6b8SAndrew Thompson DEVMETHOD(device_attach, usb_attach), 114a593f6b8SAndrew Thompson DEVMETHOD(device_detach, usb_detach), 1152e141748SHans Petter Selasky DEVMETHOD(device_suspend, usb_suspend), 1162e141748SHans Petter Selasky DEVMETHOD(device_resume, usb_resume), 1172e141748SHans Petter Selasky DEVMETHOD(device_shutdown, usb_shutdown), 11861bfd867SSofian Brabez 11961bfd867SSofian Brabez DEVMETHOD_END 12002ac6454SAndrew Thompson }; 12102ac6454SAndrew Thompson 122a593f6b8SAndrew Thompson static driver_t usb_driver = { 12302ac6454SAndrew Thompson .name = "usbus", 124a593f6b8SAndrew Thompson .methods = usb_methods, 12502ac6454SAndrew Thompson .size = 0, 12602ac6454SAndrew Thompson }; 12702ac6454SAndrew Thompson 128864bc412SHans Petter Selasky /* Host Only Drivers */ 129a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0); 130a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0); 131a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0); 132963169b4SHans Petter Selasky DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0); 133864bc412SHans Petter Selasky 134864bc412SHans Petter Selasky /* Device Only Drivers */ 135864bc412SHans Petter Selasky DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0); 136c6549428SHans Petter Selasky DRIVER_MODULE(usbus, uss820dci, 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 225b78e84d1SHans Petter Selasky #if USB_HAVE_UGEN 226b78e84d1SHans Petter Selasky /* Wait for cleanup to complete */ 227b78e84d1SHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 228b78e84d1SHans Petter Selasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 229b78e84d1SHans Petter Selasky #endif 23002ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 23102ac6454SAndrew Thompson 2329b3a48eeSHans Petter Selasky #if USB_HAVE_PER_BUS_PROCESS 23302ac6454SAndrew Thompson /* Get rid of USB callback processes */ 23402ac6454SAndrew Thompson 2359b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_GIANT_PROC(bus)); 23643ea03d7SHans Petter Selasky usb_proc_free(USB_BUS_NON_GIANT_ISOC_PROC(bus)); 23743ea03d7SHans Petter Selasky usb_proc_free(USB_BUS_NON_GIANT_BULK_PROC(bus)); 23802ac6454SAndrew Thompson 23902ac6454SAndrew Thompson /* Get rid of USB explore process */ 24002ac6454SAndrew Thompson 2419b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_EXPLORE_PROC(bus)); 24202ac6454SAndrew Thompson 243672c9965SAndrew Thompson /* Get rid of control transfer process */ 244672c9965SAndrew Thompson 2459b3a48eeSHans Petter Selasky usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus)); 2469b3a48eeSHans Petter Selasky #endif 247672c9965SAndrew Thompson 2488be09334SHans Petter Selasky #if USB_HAVE_PF 249fe1c24e3SWeongyo Jeong usbpf_detach(bus); 2508be09334SHans Petter Selasky #endif 25102ac6454SAndrew Thompson return (0); 25202ac6454SAndrew Thompson } 25302ac6454SAndrew Thompson 25402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2552e141748SHans Petter Selasky * usb_suspend 2562e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2572e141748SHans Petter Selasky static int 2582e141748SHans Petter Selasky usb_suspend(device_t dev) 2592e141748SHans Petter Selasky { 2602e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2612e141748SHans Petter Selasky 2622e141748SHans Petter Selasky DPRINTF("\n"); 2632e141748SHans Petter Selasky 2642e141748SHans Petter Selasky if (bus == NULL) { 2652e141748SHans Petter Selasky /* was never setup properly */ 2662e141748SHans Petter Selasky return (0); 2672e141748SHans Petter Selasky } 2682e141748SHans Petter Selasky 2692e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2709b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2712e141748SHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 27202f728afSHans Petter Selasky if (usb_no_suspend_wait == 0) { 27302f728afSHans Petter Selasky /* wait for suspend callback to be executed */ 2749b3a48eeSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 27502f728afSHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 27602f728afSHans Petter Selasky } 2772e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2782e141748SHans Petter Selasky 2792e141748SHans Petter Selasky return (0); 2802e141748SHans Petter Selasky } 2812e141748SHans Petter Selasky 2822e141748SHans Petter Selasky /*------------------------------------------------------------------------* 2832e141748SHans Petter Selasky * usb_resume 2842e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2852e141748SHans Petter Selasky static int 2862e141748SHans Petter Selasky usb_resume(device_t dev) 2872e141748SHans Petter Selasky { 2882e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2892e141748SHans Petter Selasky 2902e141748SHans Petter Selasky DPRINTF("\n"); 2912e141748SHans Petter Selasky 2922e141748SHans Petter Selasky if (bus == NULL) { 2932e141748SHans Petter Selasky /* was never setup properly */ 2942e141748SHans Petter Selasky return (0); 2952e141748SHans Petter Selasky } 2962e141748SHans Petter Selasky 2972e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2989b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2992e141748SHans Petter Selasky &bus->resume_msg[0], &bus->resume_msg[1]); 3002e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 3012e141748SHans Petter Selasky 3022e141748SHans Petter Selasky return (0); 3032e141748SHans Petter Selasky } 3042e141748SHans Petter Selasky 3052e141748SHans Petter Selasky /*------------------------------------------------------------------------* 306563ab081SHans Petter Selasky * usb_bus_reset_async_locked 307563ab081SHans Petter Selasky *------------------------------------------------------------------------*/ 308563ab081SHans Petter Selasky void 309563ab081SHans Petter Selasky usb_bus_reset_async_locked(struct usb_bus *bus) 310563ab081SHans Petter Selasky { 311563ab081SHans Petter Selasky USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 312563ab081SHans Petter Selasky 313563ab081SHans Petter Selasky DPRINTF("\n"); 314563ab081SHans Petter Selasky 315563ab081SHans Petter Selasky if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL || 316563ab081SHans Petter Selasky bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) { 317563ab081SHans Petter Selasky DPRINTF("Reset already pending\n"); 318563ab081SHans Petter Selasky return; 319563ab081SHans Petter Selasky } 320563ab081SHans Petter Selasky 321563ab081SHans Petter Selasky device_printf(bus->parent, "Resetting controller\n"); 322563ab081SHans Petter Selasky 323563ab081SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 324563ab081SHans Petter Selasky &bus->reset_msg[0], &bus->reset_msg[1]); 325563ab081SHans Petter Selasky } 326563ab081SHans Petter Selasky 327563ab081SHans Petter Selasky /*------------------------------------------------------------------------* 3282e141748SHans Petter Selasky * usb_shutdown 3292e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 3302e141748SHans Petter Selasky static int 3312e141748SHans Petter Selasky usb_shutdown(device_t dev) 3322e141748SHans Petter Selasky { 3332e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 3342e141748SHans Petter Selasky 3352e141748SHans Petter Selasky DPRINTF("\n"); 3362e141748SHans Petter Selasky 3372e141748SHans Petter Selasky if (bus == NULL) { 3382e141748SHans Petter Selasky /* was never setup properly */ 3392e141748SHans Petter Selasky return (0); 3402e141748SHans Petter Selasky } 3412e141748SHans Petter Selasky 342cabe79d9SMarius Strobl DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev)); 3435ed294aeSHans Petter Selasky 3442e141748SHans Petter Selasky USB_BUS_LOCK(bus); 3459b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 3462e141748SHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 3475ed294aeSHans Petter Selasky if (usb_no_shutdown_wait == 0) { 3485ed294aeSHans Petter Selasky /* wait for shutdown callback to be executed */ 3499b3a48eeSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus), 3505ed294aeSHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 3515ed294aeSHans Petter Selasky } 3522e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 3532e141748SHans Petter Selasky 354cabe79d9SMarius Strobl DPRINTF("%s: Controller shutdown complete\n", 355cabe79d9SMarius Strobl device_get_nameunit(bus->bdev)); 3565ed294aeSHans Petter Selasky 3572e141748SHans Petter Selasky return (0); 3582e141748SHans Petter Selasky } 3592e141748SHans Petter Selasky 3602e141748SHans Petter Selasky /*------------------------------------------------------------------------* 361a593f6b8SAndrew Thompson * usb_bus_explore 36202ac6454SAndrew Thompson * 36302ac6454SAndrew Thompson * This function is used to explore the device tree from the root. 36402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 36502ac6454SAndrew Thompson static void 366a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm) 36702ac6454SAndrew Thompson { 368760bc48eSAndrew Thompson struct usb_bus *bus; 369760bc48eSAndrew Thompson struct usb_device *udev; 37002ac6454SAndrew Thompson 371760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 37202ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 37302ac6454SAndrew Thompson 3742e141748SHans Petter Selasky if (bus->no_explore != 0) 3752e141748SHans Petter Selasky return; 3762e141748SHans Petter Selasky 377d64e9217SHans Petter Selasky if (udev != NULL) { 378d64e9217SHans Petter Selasky USB_BUS_UNLOCK(bus); 379d64e9217SHans Petter Selasky uhub_explore_handle_re_enumerate(udev); 380d64e9217SHans Petter Selasky USB_BUS_LOCK(bus); 381d64e9217SHans Petter Selasky } 382d64e9217SHans Petter Selasky 383f80ccb40SHans Petter Selasky if (udev != NULL && udev->hub != NULL) { 38402ac6454SAndrew Thompson 38502ac6454SAndrew Thompson if (bus->do_probe) { 38602ac6454SAndrew Thompson bus->do_probe = 0; 38702ac6454SAndrew Thompson bus->driver_added_refcount++; 38802ac6454SAndrew Thompson } 38902ac6454SAndrew Thompson if (bus->driver_added_refcount == 0) { 39002ac6454SAndrew Thompson /* avoid zero, hence that is memory default */ 39102ac6454SAndrew Thompson bus->driver_added_refcount = 1; 39202ac6454SAndrew Thompson } 39302ac6454SAndrew Thompson 394f0c078e6SAndrew Thompson #ifdef DDB 39534b48722SAlfred Perlstein /* 39634b48722SAlfred Perlstein * The following three lines of code are only here to 39734b48722SAlfred Perlstein * recover from DDB: 39834b48722SAlfred Perlstein */ 3999b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus)); 4009b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus)); 40143ea03d7SHans Petter Selasky usb_proc_rewakeup(USB_BUS_NON_GIANT_ISOC_PROC(bus)); 40243ea03d7SHans Petter Selasky usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_PROC(bus)); 403f0c078e6SAndrew Thompson #endif 40434b48722SAlfred Perlstein 40534b48722SAlfred Perlstein USB_BUS_UNLOCK(bus); 406a56fe095SJohn Baldwin 407e727a16cSAndrew Thompson #if USB_HAVE_POWERD 40802ac6454SAndrew Thompson /* 40902ac6454SAndrew Thompson * First update the USB power state! 41002ac6454SAndrew Thompson */ 411a593f6b8SAndrew Thompson usb_bus_powerd(bus); 412e727a16cSAndrew Thompson #endif 41334b48722SAlfred Perlstein /* Explore the Root USB HUB. */ 41402ac6454SAndrew Thompson (udev->hub->explore) (udev); 41502ac6454SAndrew Thompson USB_BUS_LOCK(bus); 41602ac6454SAndrew Thompson } 417d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 4183df007ceSHans Petter Selasky usb_root_mount_rel(bus); 419d2b99310SHans Petter Selasky #endif 42002ac6454SAndrew Thompson } 42102ac6454SAndrew Thompson 42202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 423a593f6b8SAndrew Thompson * usb_bus_detach 42402ac6454SAndrew Thompson * 42502ac6454SAndrew Thompson * This function is used to detach the device tree from the root. 42602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 42702ac6454SAndrew Thompson static void 428a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm) 42902ac6454SAndrew Thompson { 430760bc48eSAndrew Thompson struct usb_bus *bus; 431760bc48eSAndrew Thompson struct usb_device *udev; 43202ac6454SAndrew Thompson device_t dev; 43302ac6454SAndrew Thompson 434760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 43502ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 43602ac6454SAndrew Thompson dev = bus->bdev; 43702ac6454SAndrew Thompson /* clear the softc */ 43802ac6454SAndrew Thompson device_set_softc(dev, NULL); 43902ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 44002ac6454SAndrew Thompson 44102ac6454SAndrew Thompson /* detach children first */ 44234b48722SAlfred Perlstein mtx_lock(&Giant); 44302ac6454SAndrew Thompson bus_generic_detach(dev); 44434b48722SAlfred Perlstein mtx_unlock(&Giant); 44502ac6454SAndrew Thompson 44602ac6454SAndrew Thompson /* 447d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 44802ac6454SAndrew Thompson */ 449d88688c7SAndrew Thompson usb_free_device(udev, 0); 45002ac6454SAndrew Thompson 45102ac6454SAndrew Thompson USB_BUS_LOCK(bus); 45202ac6454SAndrew Thompson /* clear bdev variable last */ 45302ac6454SAndrew Thompson bus->bdev = NULL; 45402ac6454SAndrew Thompson } 45502ac6454SAndrew Thompson 4562e141748SHans Petter Selasky /*------------------------------------------------------------------------* 4572e141748SHans Petter Selasky * usb_bus_suspend 4582e141748SHans Petter Selasky * 459468f354bSHans Petter Selasky * This function is used to suspend the USB controller. 4602e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 4612e141748SHans Petter Selasky static void 4622e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm) 4632e141748SHans Petter Selasky { 4642e141748SHans Petter Selasky struct usb_bus *bus; 4652e141748SHans Petter Selasky struct usb_device *udev; 4662e141748SHans Petter Selasky usb_error_t err; 467a18a7a41SHans Petter Selasky uint8_t do_unlock; 4682e141748SHans Petter Selasky 469563ab081SHans Petter Selasky DPRINTF("\n"); 470563ab081SHans Petter Selasky 4712e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4722e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4732e141748SHans Petter Selasky 4742e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 4752e141748SHans Petter Selasky return; 4762e141748SHans Petter Selasky 4776bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 4786bd3e535SHans Petter Selasky 47902f728afSHans Petter Selasky /* 48002f728afSHans Petter Selasky * We use the shutdown event here because the suspend and 48102f728afSHans Petter Selasky * resume events are reserved for the USB port suspend and 48202f728afSHans Petter Selasky * resume. The USB system suspend is implemented like full 48302f728afSHans Petter Selasky * shutdown and all connected USB devices will be disconnected 48402f728afSHans Petter Selasky * subsequently. At resume all USB devices will be 48502f728afSHans Petter Selasky * re-connected again. 48602f728afSHans Petter Selasky */ 48702f728afSHans Petter Selasky 4882e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 4892e141748SHans Petter Selasky 490a18a7a41SHans Petter Selasky do_unlock = usbd_enum_lock(udev); 4912e141748SHans Petter Selasky 4922e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 4932e141748SHans Petter Selasky if (err) 4942e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 4952e141748SHans Petter Selasky 4962e141748SHans Petter Selasky USB_BUS_LOCK(bus); 4972e141748SHans Petter Selasky bus->hw_power_state = 0; 4982e141748SHans Petter Selasky bus->no_explore = 1; 4992e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 5002e141748SHans Petter Selasky 5012e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 5022e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 5032e141748SHans Petter Selasky 5042e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 5052e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 5062e141748SHans Petter Selasky 507a18a7a41SHans Petter Selasky if (do_unlock) 5082e141748SHans Petter Selasky usbd_enum_unlock(udev); 5096bd3e535SHans Petter Selasky 5106bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5112e141748SHans Petter Selasky } 5122e141748SHans Petter Selasky 5132e141748SHans Petter Selasky /*------------------------------------------------------------------------* 5142e141748SHans Petter Selasky * usb_bus_resume 5152e141748SHans Petter Selasky * 516468f354bSHans Petter Selasky * This function is used to resume the USB controller. 5172e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 5182e141748SHans Petter Selasky static void 5192e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm) 5202e141748SHans Petter Selasky { 5212e141748SHans Petter Selasky struct usb_bus *bus; 5222e141748SHans Petter Selasky struct usb_device *udev; 5232e141748SHans Petter Selasky usb_error_t err; 524a18a7a41SHans Petter Selasky uint8_t do_unlock; 5252e141748SHans Petter Selasky 526563ab081SHans Petter Selasky DPRINTF("\n"); 527563ab081SHans Petter Selasky 5282e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 5292e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 5302e141748SHans Petter Selasky 5312e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 5322e141748SHans Petter Selasky return; 5332e141748SHans Petter Selasky 5346bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 5356bd3e535SHans Petter Selasky 536a18a7a41SHans Petter Selasky do_unlock = usbd_enum_lock(udev); 5372e141748SHans Petter Selasky #if 0 5382e141748SHans Petter Selasky DEVMETHOD(usb_take_controller, NULL); /* dummy */ 5392e141748SHans Petter Selasky #endif 5402e141748SHans Petter Selasky USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 5412e141748SHans Petter Selasky 5422e141748SHans Petter Selasky USB_BUS_LOCK(bus); 5432e141748SHans Petter Selasky bus->hw_power_state = 5442e141748SHans Petter Selasky USB_HW_POWER_CONTROL | 5452e141748SHans Petter Selasky USB_HW_POWER_BULK | 5462e141748SHans Petter Selasky USB_HW_POWER_INTERRUPT | 5472e141748SHans Petter Selasky USB_HW_POWER_ISOC | 5482e141748SHans Petter Selasky USB_HW_POWER_NON_ROOT_HUB; 5492e141748SHans Petter Selasky bus->no_explore = 0; 5502e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 5512e141748SHans Petter Selasky 5522e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 5532e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 5542e141748SHans Petter Selasky 5552e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 5562e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 5572e141748SHans Petter Selasky 5586bbe7cdfSHans Petter Selasky /* restore USB configuration to index 0 */ 5592e141748SHans Petter Selasky err = usbd_set_config_index(udev, 0); 5602e141748SHans Petter Selasky if (err) 5612e141748SHans Petter Selasky device_printf(bus->bdev, "Could not configure root HUB\n"); 5622e141748SHans Petter Selasky 5636bbe7cdfSHans Petter Selasky /* probe and attach */ 5646bbe7cdfSHans Petter Selasky err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY); 5656bbe7cdfSHans Petter Selasky if (err) { 5666bbe7cdfSHans Petter Selasky device_printf(bus->bdev, "Could not probe and " 5676bbe7cdfSHans Petter Selasky "attach root HUB\n"); 5686bbe7cdfSHans Petter Selasky } 5696bbe7cdfSHans Petter Selasky 570a18a7a41SHans Petter Selasky if (do_unlock) 5712e141748SHans Petter Selasky usbd_enum_unlock(udev); 5726bd3e535SHans Petter Selasky 5736bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5742e141748SHans Petter Selasky } 5752e141748SHans Petter Selasky 5762e141748SHans Petter Selasky /*------------------------------------------------------------------------* 577563ab081SHans Petter Selasky * usb_bus_reset 578563ab081SHans Petter Selasky * 579468f354bSHans Petter Selasky * This function is used to reset the USB controller. 580563ab081SHans Petter Selasky *------------------------------------------------------------------------*/ 581563ab081SHans Petter Selasky static void 582563ab081SHans Petter Selasky usb_bus_reset(struct usb_proc_msg *pm) 583563ab081SHans Petter Selasky { 584563ab081SHans Petter Selasky struct usb_bus *bus; 585563ab081SHans Petter Selasky 586563ab081SHans Petter Selasky DPRINTF("\n"); 587563ab081SHans Petter Selasky 588563ab081SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 589563ab081SHans Petter Selasky 590563ab081SHans Petter Selasky if (bus->bdev == NULL || bus->no_explore != 0) 591563ab081SHans Petter Selasky return; 592563ab081SHans Petter Selasky 593563ab081SHans Petter Selasky /* a suspend and resume will reset the USB controller */ 594563ab081SHans Petter Selasky usb_bus_suspend(pm); 595563ab081SHans Petter Selasky usb_bus_resume(pm); 596563ab081SHans Petter Selasky } 597563ab081SHans Petter Selasky 598563ab081SHans Petter Selasky /*------------------------------------------------------------------------* 5992e141748SHans Petter Selasky * usb_bus_shutdown 6002e141748SHans Petter Selasky * 601468f354bSHans Petter Selasky * This function is used to shutdown the USB controller. 6022e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 6032e141748SHans Petter Selasky static void 6042e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm) 6052e141748SHans Petter Selasky { 6062e141748SHans Petter Selasky struct usb_bus *bus; 6072e141748SHans Petter Selasky struct usb_device *udev; 6082e141748SHans Petter Selasky usb_error_t err; 609a18a7a41SHans Petter Selasky uint8_t do_unlock; 6102e141748SHans Petter Selasky 6112e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 6122e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 6132e141748SHans Petter Selasky 6142e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 6152e141748SHans Petter Selasky return; 6162e141748SHans Petter Selasky 6176bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 6186bd3e535SHans Petter Selasky 6192e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 6202e141748SHans Petter Selasky 621a18a7a41SHans Petter Selasky do_unlock = usbd_enum_lock(udev); 6222e141748SHans Petter Selasky 6232e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 6242e141748SHans Petter Selasky if (err) 6252e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 6262e141748SHans Petter Selasky 6272e141748SHans Petter Selasky USB_BUS_LOCK(bus); 6282e141748SHans Petter Selasky bus->hw_power_state = 0; 6292e141748SHans Petter Selasky bus->no_explore = 1; 6302e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 6312e141748SHans Petter Selasky 6322e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 6332e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 6342e141748SHans Petter Selasky 6352e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 6362e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 6372e141748SHans Petter Selasky 638a18a7a41SHans Petter Selasky if (do_unlock) 6392e141748SHans Petter Selasky usbd_enum_unlock(udev); 6406bd3e535SHans Petter Selasky 6416bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 6422e141748SHans Petter Selasky } 6432e141748SHans Petter Selasky 644b78e84d1SHans Petter Selasky /*------------------------------------------------------------------------* 645b78e84d1SHans Petter Selasky * usb_bus_cleanup 646b78e84d1SHans Petter Selasky * 647b78e84d1SHans Petter Selasky * This function is used to cleanup leftover USB character devices. 648b78e84d1SHans Petter Selasky *------------------------------------------------------------------------*/ 649b78e84d1SHans Petter Selasky #if USB_HAVE_UGEN 650b78e84d1SHans Petter Selasky static void 651b78e84d1SHans Petter Selasky usb_bus_cleanup(struct usb_proc_msg *pm) 652b78e84d1SHans Petter Selasky { 653b78e84d1SHans Petter Selasky struct usb_bus *bus; 654b78e84d1SHans Petter Selasky struct usb_fs_privdata *pd; 655b78e84d1SHans Petter Selasky 656b78e84d1SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 657b78e84d1SHans Petter Selasky 658b78e84d1SHans Petter Selasky while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) { 659b78e84d1SHans Petter Selasky 660b78e84d1SHans Petter Selasky LIST_REMOVE(pd, pd_next); 661b78e84d1SHans Petter Selasky USB_BUS_UNLOCK(bus); 662b78e84d1SHans Petter Selasky 663b78e84d1SHans Petter Selasky usb_destroy_dev_sync(pd); 664b78e84d1SHans Petter Selasky 665b78e84d1SHans Petter Selasky USB_BUS_LOCK(bus); 666b78e84d1SHans Petter Selasky } 667b78e84d1SHans Petter Selasky } 668b78e84d1SHans Petter Selasky #endif 669b78e84d1SHans Petter Selasky 67002ac6454SAndrew Thompson static void 671a593f6b8SAndrew Thompson usb_power_wdog(void *arg) 67202ac6454SAndrew Thompson { 673760bc48eSAndrew Thompson struct usb_bus *bus = arg; 67402ac6454SAndrew Thompson 67502ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 67602ac6454SAndrew Thompson 677a593f6b8SAndrew Thompson usb_callout_reset(&bus->power_wdog, 678a593f6b8SAndrew Thompson 4 * hz, usb_power_wdog, arg); 67902ac6454SAndrew Thompson 680f0c078e6SAndrew Thompson #ifdef DDB 68134b48722SAlfred Perlstein /* 68234b48722SAlfred Perlstein * The following line of code is only here to recover from 68334b48722SAlfred Perlstein * DDB: 68434b48722SAlfred Perlstein */ 6859b3a48eeSHans Petter Selasky usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */ 686f0c078e6SAndrew Thompson #endif 68734b48722SAlfred Perlstein 688e727a16cSAndrew Thompson #if USB_HAVE_POWERD 68902ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 69002ac6454SAndrew Thompson 691a593f6b8SAndrew Thompson usb_bus_power_update(bus); 69202ac6454SAndrew Thompson 693684e3f22SAndrew Thompson USB_BUS_LOCK(bus); 694e727a16cSAndrew Thompson #endif 69502ac6454SAndrew Thompson } 69602ac6454SAndrew Thompson 69702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 698a593f6b8SAndrew Thompson * usb_bus_attach 69902ac6454SAndrew Thompson * 70002ac6454SAndrew Thompson * This function attaches USB in context of the explore thread. 70102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 70202ac6454SAndrew Thompson static void 703a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm) 70402ac6454SAndrew Thompson { 705760bc48eSAndrew Thompson struct usb_bus *bus; 706760bc48eSAndrew Thompson struct usb_device *child; 70702ac6454SAndrew Thompson device_t dev; 708e0a69b51SAndrew Thompson usb_error_t err; 7098d2dd5ddSAndrew Thompson enum usb_dev_speed speed; 71002ac6454SAndrew Thompson 711760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 71202ac6454SAndrew Thompson dev = bus->bdev; 71302ac6454SAndrew Thompson 71402ac6454SAndrew Thompson DPRINTF("\n"); 71502ac6454SAndrew Thompson 71602ac6454SAndrew Thompson switch (bus->usbrev) { 71702ac6454SAndrew Thompson case USB_REV_1_0: 71802ac6454SAndrew Thompson speed = USB_SPEED_FULL; 71902ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 72002ac6454SAndrew Thompson break; 72102ac6454SAndrew Thompson 72202ac6454SAndrew Thompson case USB_REV_1_1: 72302ac6454SAndrew Thompson speed = USB_SPEED_FULL; 72402ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 72502ac6454SAndrew Thompson break; 72602ac6454SAndrew Thompson 72702ac6454SAndrew Thompson case USB_REV_2_0: 72802ac6454SAndrew Thompson speed = USB_SPEED_HIGH; 72902ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 73002ac6454SAndrew Thompson break; 73102ac6454SAndrew Thompson 73202ac6454SAndrew Thompson case USB_REV_2_5: 73302ac6454SAndrew Thompson speed = USB_SPEED_VARIABLE; 73402ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 73502ac6454SAndrew Thompson break; 73602ac6454SAndrew Thompson 737963169b4SHans Petter Selasky case USB_REV_3_0: 738963169b4SHans Petter Selasky speed = USB_SPEED_SUPER; 739ccac019aSHans Petter Selasky device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 740963169b4SHans Petter Selasky break; 741963169b4SHans Petter Selasky 74202ac6454SAndrew Thompson default: 743767cb2e2SAndrew Thompson device_printf(bus->bdev, "Unsupported USB revision\n"); 744d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 7453df007ceSHans Petter Selasky usb_root_mount_rel(bus); 746d2b99310SHans Petter Selasky #endif 74702ac6454SAndrew Thompson return; 74802ac6454SAndrew Thompson } 74902ac6454SAndrew Thompson 7504eae601eSAndrew Thompson /* default power_mask value */ 7514eae601eSAndrew Thompson bus->hw_power_state = 7524eae601eSAndrew Thompson USB_HW_POWER_CONTROL | 7534eae601eSAndrew Thompson USB_HW_POWER_BULK | 7544eae601eSAndrew Thompson USB_HW_POWER_INTERRUPT | 7554eae601eSAndrew Thompson USB_HW_POWER_ISOC | 7564eae601eSAndrew Thompson USB_HW_POWER_NON_ROOT_HUB; 7574eae601eSAndrew Thompson 7582e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 7592e141748SHans Petter Selasky 7604eae601eSAndrew Thompson /* make sure power is set at least once */ 7614eae601eSAndrew Thompson 7624eae601eSAndrew Thompson if (bus->methods->set_hw_power != NULL) { 7634eae601eSAndrew Thompson (bus->methods->set_hw_power) (bus); 7644eae601eSAndrew Thompson } 7654eae601eSAndrew Thompson 7662e141748SHans Petter Selasky /* allocate the Root USB device */ 76702ac6454SAndrew Thompson 768a593f6b8SAndrew Thompson child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 76902ac6454SAndrew Thompson speed, USB_MODE_HOST); 77002ac6454SAndrew Thompson if (child) { 771a593f6b8SAndrew Thompson err = usb_probe_and_attach(child, 77202ac6454SAndrew Thompson USB_IFACE_INDEX_ANY); 77302ac6454SAndrew Thompson if (!err) { 7741be5bf51SAndrew Thompson if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 7751be5bf51SAndrew Thompson (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 77602ac6454SAndrew Thompson err = USB_ERR_NO_ROOT_HUB; 77702ac6454SAndrew Thompson } 77802ac6454SAndrew Thompson } 77902ac6454SAndrew Thompson } else { 78002ac6454SAndrew Thompson err = USB_ERR_NOMEM; 78102ac6454SAndrew Thompson } 78202ac6454SAndrew Thompson 78302ac6454SAndrew Thompson USB_BUS_LOCK(bus); 78402ac6454SAndrew Thompson 78502ac6454SAndrew Thompson if (err) { 78602ac6454SAndrew Thompson device_printf(bus->bdev, "Root HUB problem, error=%s\n", 787a593f6b8SAndrew Thompson usbd_errstr(err)); 788d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 7893df007ceSHans Petter Selasky usb_root_mount_rel(bus); 790d2b99310SHans Petter Selasky #endif 79102ac6454SAndrew Thompson } 79202ac6454SAndrew Thompson 79302ac6454SAndrew Thompson /* set softc - we are ready */ 79402ac6454SAndrew Thompson device_set_softc(dev, bus); 79502ac6454SAndrew Thompson 796684e3f22SAndrew Thompson /* start watchdog */ 797a593f6b8SAndrew Thompson usb_power_wdog(bus); 79802ac6454SAndrew Thompson } 79902ac6454SAndrew Thompson 80002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 801a593f6b8SAndrew Thompson * usb_attach_sub 80202ac6454SAndrew Thompson * 80334b48722SAlfred Perlstein * This function creates a thread which runs the USB attach code. 80402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 80502ac6454SAndrew Thompson static void 806a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus) 80702ac6454SAndrew Thompson { 80834b48722SAlfred Perlstein mtx_lock(&Giant); 80934b48722SAlfred Perlstein if (usb_devclass_ptr == NULL) 81034b48722SAlfred Perlstein usb_devclass_ptr = devclass_find("usbus"); 81134b48722SAlfred Perlstein mtx_unlock(&Giant); 81234b48722SAlfred Perlstein 8138be09334SHans Petter Selasky #if USB_HAVE_PF 814fe1c24e3SWeongyo Jeong usbpf_attach(bus); 8158be09334SHans Petter Selasky #endif 81602ac6454SAndrew Thompson /* Initialise USB process messages */ 817a593f6b8SAndrew Thompson bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 81802ac6454SAndrew Thompson bus->explore_msg[0].bus = bus; 819a593f6b8SAndrew Thompson bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 82002ac6454SAndrew Thompson bus->explore_msg[1].bus = bus; 82102ac6454SAndrew Thompson 822a593f6b8SAndrew Thompson bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 82302ac6454SAndrew Thompson bus->detach_msg[0].bus = bus; 824a593f6b8SAndrew Thompson bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 82502ac6454SAndrew Thompson bus->detach_msg[1].bus = bus; 82602ac6454SAndrew Thompson 827a593f6b8SAndrew Thompson bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 82802ac6454SAndrew Thompson bus->attach_msg[0].bus = bus; 829a593f6b8SAndrew Thompson bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 83002ac6454SAndrew Thompson bus->attach_msg[1].bus = bus; 83102ac6454SAndrew Thompson 8322e141748SHans Petter Selasky bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 8332e141748SHans Petter Selasky bus->suspend_msg[0].bus = bus; 8342e141748SHans Petter Selasky bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 8352e141748SHans Petter Selasky bus->suspend_msg[1].bus = bus; 8362e141748SHans Petter Selasky 8372e141748SHans Petter Selasky bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 8382e141748SHans Petter Selasky bus->resume_msg[0].bus = bus; 8392e141748SHans Petter Selasky bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 8402e141748SHans Petter Selasky bus->resume_msg[1].bus = bus; 8412e141748SHans Petter Selasky 842563ab081SHans Petter Selasky bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset; 843563ab081SHans Petter Selasky bus->reset_msg[0].bus = bus; 844563ab081SHans Petter Selasky bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset; 845563ab081SHans Petter Selasky bus->reset_msg[1].bus = bus; 846563ab081SHans Petter Selasky 8472e141748SHans Petter Selasky bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 8482e141748SHans Petter Selasky bus->shutdown_msg[0].bus = bus; 8492e141748SHans Petter Selasky bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 8502e141748SHans Petter Selasky bus->shutdown_msg[1].bus = bus; 8512e141748SHans Petter Selasky 852b78e84d1SHans Petter Selasky #if USB_HAVE_UGEN 853b78e84d1SHans Petter Selasky LIST_INIT(&bus->pd_cleanup_list); 854b78e84d1SHans Petter Selasky bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup; 855b78e84d1SHans Petter Selasky bus->cleanup_msg[0].bus = bus; 856b78e84d1SHans Petter Selasky bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup; 857b78e84d1SHans Petter Selasky bus->cleanup_msg[1].bus = bus; 858b78e84d1SHans Petter Selasky #endif 859b78e84d1SHans Petter Selasky 8609b3a48eeSHans Petter Selasky #if USB_HAVE_PER_BUS_PROCESS 86139307315SAndrew Thompson /* Create USB explore and callback processes */ 86202ac6454SAndrew Thompson 8639b3a48eeSHans Petter Selasky if (usb_proc_create(USB_BUS_GIANT_PROC(bus), 8649b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 86588334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB Giant " 86602ac6454SAndrew Thompson "callback process failed.\n"); 86743ea03d7SHans Petter Selasky } else if (usb_proc_create(USB_BUS_NON_GIANT_ISOC_PROC(bus), 86843ea03d7SHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGHEST)) { 86943ea03d7SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB non-Giant ISOC " 87043ea03d7SHans Petter Selasky "callback process failed.\n"); 87143ea03d7SHans Petter Selasky } else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus), 8729b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) { 87343ea03d7SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB non-Giant BULK " 87402ac6454SAndrew Thompson "callback process failed.\n"); 8759b3a48eeSHans Petter Selasky } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus), 8769b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 87788334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB explore " 87802ac6454SAndrew Thompson "process failed.\n"); 8799b3a48eeSHans Petter Selasky } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus), 8809b3a48eeSHans Petter Selasky &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) { 88188334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB control transfer " 882672c9965SAndrew Thompson "process failed.\n"); 8839b3a48eeSHans Petter Selasky } else 8849b3a48eeSHans Petter Selasky #endif 8859b3a48eeSHans Petter Selasky { 88602ac6454SAndrew Thompson /* Get final attach going */ 88702ac6454SAndrew Thompson USB_BUS_LOCK(bus); 8889b3a48eeSHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 8892e141748SHans Petter Selasky &bus->attach_msg[0], &bus->attach_msg[1]); 89002ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 89134b48722SAlfred Perlstein 89234b48722SAlfred Perlstein /* Do initial explore */ 89334b48722SAlfred Perlstein usb_needs_explore(bus, 1); 89402ac6454SAndrew Thompson } 89502ac6454SAndrew Thompson } 896a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 89702ac6454SAndrew Thompson 89802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 899a593f6b8SAndrew Thompson * usb_bus_mem_flush_all_cb 90002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 901bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 90202ac6454SAndrew Thompson static void 903a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 904f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 90502ac6454SAndrew Thompson { 906a593f6b8SAndrew Thompson usb_pc_cpu_flush(pc); 90702ac6454SAndrew Thompson } 908bdc081c6SAndrew Thompson #endif 90902ac6454SAndrew Thompson 91002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 911a593f6b8SAndrew Thompson * usb_bus_mem_flush_all - factored out code 91202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 913bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 91402ac6454SAndrew Thompson void 915a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 91602ac6454SAndrew Thompson { 91702ac6454SAndrew Thompson if (cb) { 918a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_flush_all_cb); 91902ac6454SAndrew Thompson } 92002ac6454SAndrew Thompson } 921bdc081c6SAndrew Thompson #endif 92202ac6454SAndrew Thompson 92302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 924a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all_cb 92502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 926bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 92702ac6454SAndrew Thompson static void 928a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 929f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 93002ac6454SAndrew Thompson { 93102ac6454SAndrew Thompson /* need to initialize the page cache */ 93202ac6454SAndrew Thompson pc->tag_parent = bus->dma_parent_tag; 93302ac6454SAndrew Thompson 934a593f6b8SAndrew Thompson if (usb_pc_alloc_mem(pc, pg, size, align)) { 93502ac6454SAndrew Thompson bus->alloc_failed = 1; 93602ac6454SAndrew Thompson } 93702ac6454SAndrew Thompson } 938bdc081c6SAndrew Thompson #endif 93902ac6454SAndrew Thompson 94002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 941a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all - factored out code 94202ac6454SAndrew Thompson * 94302ac6454SAndrew Thompson * Returns: 94402ac6454SAndrew Thompson * 0: Success 94502ac6454SAndrew Thompson * Else: Failure 94602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 94702ac6454SAndrew Thompson uint8_t 948a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 949e0a69b51SAndrew Thompson usb_bus_mem_cb_t *cb) 95002ac6454SAndrew Thompson { 95102ac6454SAndrew Thompson bus->alloc_failed = 0; 95202ac6454SAndrew Thompson 95302ac6454SAndrew Thompson mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 9544a4da38fSHans Petter Selasky "usb_def_mtx", MTX_DEF | MTX_RECURSE); 95502ac6454SAndrew Thompson 9562fe7ad87SHans Petter Selasky mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent), 9574a4da38fSHans Petter Selasky "usb_spin_mtx", MTX_SPIN | MTX_RECURSE); 9582fe7ad87SHans Petter Selasky 959a593f6b8SAndrew Thompson usb_callout_init_mtx(&bus->power_wdog, 960684e3f22SAndrew Thompson &bus->bus_mtx, 0); 96102ac6454SAndrew Thompson 96202ac6454SAndrew Thompson TAILQ_INIT(&bus->intr_q.head); 96302ac6454SAndrew Thompson 964bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 965a593f6b8SAndrew Thompson usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 966b217d184SHans Petter Selasky dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX); 967bdc081c6SAndrew Thompson #endif 96802ac6454SAndrew Thompson if ((bus->devices_max > USB_MAX_DEVICES) || 96902ac6454SAndrew Thompson (bus->devices_max < USB_MIN_DEVICES) || 97002ac6454SAndrew Thompson (bus->devices == NULL)) { 97102ac6454SAndrew Thompson DPRINTFN(0, "Devices field has not been " 972767cb2e2SAndrew Thompson "initialised properly\n"); 97302ac6454SAndrew Thompson bus->alloc_failed = 1; /* failure */ 97402ac6454SAndrew Thompson } 975bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 97602ac6454SAndrew Thompson if (cb) { 977a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_alloc_all_cb); 97802ac6454SAndrew Thompson } 979bdc081c6SAndrew Thompson #endif 98002ac6454SAndrew Thompson if (bus->alloc_failed) { 981a593f6b8SAndrew Thompson usb_bus_mem_free_all(bus, cb); 98202ac6454SAndrew Thompson } 98302ac6454SAndrew Thompson return (bus->alloc_failed); 98402ac6454SAndrew Thompson } 98502ac6454SAndrew Thompson 98602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 987a593f6b8SAndrew Thompson * usb_bus_mem_free_all_cb 98802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 989bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 99002ac6454SAndrew Thompson static void 991a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 992f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 99302ac6454SAndrew Thompson { 994a593f6b8SAndrew Thompson usb_pc_free_mem(pc); 99502ac6454SAndrew Thompson } 996bdc081c6SAndrew Thompson #endif 99702ac6454SAndrew Thompson 99802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 999a593f6b8SAndrew Thompson * usb_bus_mem_free_all - factored out code 100002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 100102ac6454SAndrew Thompson void 1002a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 100302ac6454SAndrew Thompson { 1004bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 100502ac6454SAndrew Thompson if (cb) { 1006a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_free_all_cb); 100702ac6454SAndrew Thompson } 1008a593f6b8SAndrew Thompson usb_dma_tag_unsetup(bus->dma_parent_tag); 1009bdc081c6SAndrew Thompson #endif 101002ac6454SAndrew Thompson 101102ac6454SAndrew Thompson mtx_destroy(&bus->bus_mtx); 10122fe7ad87SHans Petter Selasky mtx_destroy(&bus->bus_spin_lock); 101302ac6454SAndrew Thompson } 1014751aaf5aSHans Petter Selasky 1015751aaf5aSHans Petter Selasky /* convenience wrappers */ 1016751aaf5aSHans Petter Selasky void 1017751aaf5aSHans Petter Selasky usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2) 1018751aaf5aSHans Petter Selasky { 1019751aaf5aSHans Petter Selasky usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2); 1020751aaf5aSHans Petter Selasky } 1021751aaf5aSHans Petter Selasky 1022751aaf5aSHans Petter Selasky void * 1023751aaf5aSHans Petter Selasky usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2) 1024751aaf5aSHans Petter Selasky { 1025751aaf5aSHans Petter Selasky return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2)); 1026751aaf5aSHans Petter Selasky } 1027751aaf5aSHans Petter Selasky 1028751aaf5aSHans Petter Selasky void 1029751aaf5aSHans Petter Selasky usb_proc_explore_lock(struct usb_device *udev) 1030751aaf5aSHans Petter Selasky { 1031751aaf5aSHans Petter Selasky USB_BUS_LOCK(udev->bus); 1032751aaf5aSHans Petter Selasky } 1033751aaf5aSHans Petter Selasky 1034751aaf5aSHans Petter Selasky void 1035751aaf5aSHans Petter Selasky usb_proc_explore_unlock(struct usb_device *udev) 1036751aaf5aSHans Petter Selasky { 1037751aaf5aSHans Petter Selasky USB_BUS_UNLOCK(udev->bus); 1038751aaf5aSHans Petter Selasky } 1039