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), 117*61bfd867SSofian Brabez 118*61bfd867SSofian 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); 141268ae63aSHans Petter Selasky 14202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 143a593f6b8SAndrew Thompson * usb_probe 14402ac6454SAndrew Thompson * 14502ac6454SAndrew Thompson * This function is called from "{ehci,ohci,uhci}_pci_attach()". 14602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 14702ac6454SAndrew Thompson static int 148a593f6b8SAndrew Thompson usb_probe(device_t dev) 14902ac6454SAndrew Thompson { 15002ac6454SAndrew Thompson DPRINTF("\n"); 15102ac6454SAndrew Thompson return (0); 15202ac6454SAndrew Thompson } 15302ac6454SAndrew Thompson 154d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 1553df007ceSHans Petter Selasky static void 1563df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus) 1573df007ceSHans Petter Selasky { 1583df007ceSHans Petter Selasky if (bus->bus_roothold != NULL) { 1593df007ceSHans Petter Selasky DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold); 1603df007ceSHans Petter Selasky root_mount_rel(bus->bus_roothold); 1613df007ceSHans Petter Selasky bus->bus_roothold = NULL; 1623df007ceSHans Petter Selasky } 1633df007ceSHans Petter Selasky } 164d2b99310SHans Petter Selasky #endif 1653df007ceSHans Petter Selasky 16602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 167a593f6b8SAndrew Thompson * usb_attach 16802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 16902ac6454SAndrew Thompson static int 170a593f6b8SAndrew Thompson usb_attach(device_t dev) 17102ac6454SAndrew Thompson { 172760bc48eSAndrew Thompson struct usb_bus *bus = device_get_ivars(dev); 17302ac6454SAndrew Thompson 17402ac6454SAndrew Thompson DPRINTF("\n"); 17502ac6454SAndrew Thompson 17602ac6454SAndrew Thompson if (bus == NULL) { 177767cb2e2SAndrew Thompson device_printf(dev, "USB device has no ivars\n"); 17802ac6454SAndrew Thompson return (ENXIO); 17902ac6454SAndrew Thompson } 18002ac6454SAndrew Thompson 181d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 182a0c61406SAlfred Perlstein if (usb_no_boot_wait == 0) { 18302ac6454SAndrew Thompson /* delay vfs_mountroot until the bus is explored */ 184853a10a5SAndrew Thompson bus->bus_roothold = root_mount_hold(device_get_nameunit(dev)); 185a0c61406SAlfred Perlstein } 186d2b99310SHans Petter Selasky #endif 187a593f6b8SAndrew Thompson usb_attach_sub(dev, bus); 18834b48722SAlfred Perlstein 18902ac6454SAndrew Thompson return (0); /* return success */ 19002ac6454SAndrew Thompson } 19102ac6454SAndrew Thompson 19202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 193a593f6b8SAndrew Thompson * usb_detach 19402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 19502ac6454SAndrew Thompson static int 196a593f6b8SAndrew Thompson usb_detach(device_t dev) 19702ac6454SAndrew Thompson { 198760bc48eSAndrew Thompson struct usb_bus *bus = device_get_softc(dev); 19902ac6454SAndrew Thompson 20002ac6454SAndrew Thompson DPRINTF("\n"); 20102ac6454SAndrew Thompson 20202ac6454SAndrew Thompson if (bus == NULL) { 20302ac6454SAndrew Thompson /* was never setup properly */ 20402ac6454SAndrew Thompson return (0); 20502ac6454SAndrew Thompson } 20602ac6454SAndrew Thompson /* Stop power watchdog */ 207a593f6b8SAndrew Thompson usb_callout_drain(&bus->power_wdog); 20802ac6454SAndrew Thompson 209d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 21002ac6454SAndrew Thompson /* Let the USB explore process detach all devices. */ 2113df007ceSHans Petter Selasky usb_root_mount_rel(bus); 212d2b99310SHans Petter Selasky #endif 21302ac6454SAndrew Thompson 21402ac6454SAndrew Thompson USB_BUS_LOCK(bus); 21502ac6454SAndrew Thompson 2162e141748SHans Petter Selasky /* Queue detach job */ 2172e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 2182e141748SHans Petter Selasky &bus->detach_msg[0], &bus->detach_msg[1]); 2192e141748SHans Petter Selasky 2202e141748SHans Petter Selasky /* Wait for detach to complete */ 221a593f6b8SAndrew Thompson usb_proc_mwait(&bus->explore_proc, 22202ac6454SAndrew Thompson &bus->detach_msg[0], &bus->detach_msg[1]); 22302ac6454SAndrew Thompson 22402ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 22502ac6454SAndrew Thompson 22602ac6454SAndrew Thompson /* Get rid of USB callback processes */ 22702ac6454SAndrew Thompson 228a593f6b8SAndrew Thompson usb_proc_free(&bus->giant_callback_proc); 229a593f6b8SAndrew Thompson usb_proc_free(&bus->non_giant_callback_proc); 23002ac6454SAndrew Thompson 23102ac6454SAndrew Thompson /* Get rid of USB explore process */ 23202ac6454SAndrew Thompson 233a593f6b8SAndrew Thompson usb_proc_free(&bus->explore_proc); 23402ac6454SAndrew Thompson 235672c9965SAndrew Thompson /* Get rid of control transfer process */ 236672c9965SAndrew Thompson 237a593f6b8SAndrew Thompson usb_proc_free(&bus->control_xfer_proc); 238672c9965SAndrew Thompson 2398be09334SHans Petter Selasky #if USB_HAVE_PF 240fe1c24e3SWeongyo Jeong usbpf_detach(bus); 2418be09334SHans Petter Selasky #endif 24202ac6454SAndrew Thompson return (0); 24302ac6454SAndrew Thompson } 24402ac6454SAndrew Thompson 24502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2462e141748SHans Petter Selasky * usb_suspend 2472e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2482e141748SHans Petter Selasky static int 2492e141748SHans Petter Selasky usb_suspend(device_t dev) 2502e141748SHans Petter Selasky { 2512e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2522e141748SHans Petter Selasky 2532e141748SHans Petter Selasky DPRINTF("\n"); 2542e141748SHans Petter Selasky 2552e141748SHans Petter Selasky if (bus == NULL) { 2562e141748SHans Petter Selasky /* was never setup properly */ 2572e141748SHans Petter Selasky return (0); 2582e141748SHans Petter Selasky } 2592e141748SHans Petter Selasky 2602e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2612e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 2622e141748SHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 26302f728afSHans Petter Selasky if (usb_no_suspend_wait == 0) { 26402f728afSHans Petter Selasky /* wait for suspend callback to be executed */ 26502f728afSHans Petter Selasky usb_proc_mwait(&bus->explore_proc, 26602f728afSHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 26702f728afSHans Petter Selasky } 2682e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2692e141748SHans Petter Selasky 2702e141748SHans Petter Selasky return (0); 2712e141748SHans Petter Selasky } 2722e141748SHans Petter Selasky 2732e141748SHans Petter Selasky /*------------------------------------------------------------------------* 2742e141748SHans Petter Selasky * usb_resume 2752e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2762e141748SHans Petter Selasky static int 2772e141748SHans Petter Selasky usb_resume(device_t dev) 2782e141748SHans Petter Selasky { 2792e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2802e141748SHans Petter Selasky 2812e141748SHans Petter Selasky DPRINTF("\n"); 2822e141748SHans Petter Selasky 2832e141748SHans Petter Selasky if (bus == NULL) { 2842e141748SHans Petter Selasky /* was never setup properly */ 2852e141748SHans Petter Selasky return (0); 2862e141748SHans Petter Selasky } 2872e141748SHans Petter Selasky 2882e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2892e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 2902e141748SHans Petter Selasky &bus->resume_msg[0], &bus->resume_msg[1]); 2912e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2922e141748SHans Petter Selasky 2932e141748SHans Petter Selasky return (0); 2942e141748SHans Petter Selasky } 2952e141748SHans Petter Selasky 2962e141748SHans Petter Selasky /*------------------------------------------------------------------------* 2972e141748SHans Petter Selasky * usb_shutdown 2982e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2992e141748SHans Petter Selasky static int 3002e141748SHans Petter Selasky usb_shutdown(device_t dev) 3012e141748SHans Petter Selasky { 3022e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 3032e141748SHans Petter Selasky 3042e141748SHans Petter Selasky DPRINTF("\n"); 3052e141748SHans Petter Selasky 3062e141748SHans Petter Selasky if (bus == NULL) { 3072e141748SHans Petter Selasky /* was never setup properly */ 3082e141748SHans Petter Selasky return (0); 3092e141748SHans Petter Selasky } 3102e141748SHans Petter Selasky 3115ed294aeSHans Petter Selasky device_printf(bus->bdev, "Controller shutdown\n"); 3125ed294aeSHans Petter Selasky 3132e141748SHans Petter Selasky USB_BUS_LOCK(bus); 3142e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 3152e141748SHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 3165ed294aeSHans Petter Selasky if (usb_no_shutdown_wait == 0) { 3175ed294aeSHans Petter Selasky /* wait for shutdown callback to be executed */ 3185ed294aeSHans Petter Selasky usb_proc_mwait(&bus->explore_proc, 3195ed294aeSHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 3205ed294aeSHans Petter Selasky } 3212e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 3222e141748SHans Petter Selasky 3235ed294aeSHans Petter Selasky device_printf(bus->bdev, "Controller shutdown complete\n"); 3245ed294aeSHans Petter Selasky 3252e141748SHans Petter Selasky return (0); 3262e141748SHans Petter Selasky } 3272e141748SHans Petter Selasky 3282e141748SHans Petter Selasky /*------------------------------------------------------------------------* 329a593f6b8SAndrew Thompson * usb_bus_explore 33002ac6454SAndrew Thompson * 33102ac6454SAndrew Thompson * This function is used to explore the device tree from the root. 33202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 33302ac6454SAndrew Thompson static void 334a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm) 33502ac6454SAndrew Thompson { 336760bc48eSAndrew Thompson struct usb_bus *bus; 337760bc48eSAndrew Thompson struct usb_device *udev; 33802ac6454SAndrew Thompson 339760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 34002ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 34102ac6454SAndrew Thompson 3422e141748SHans Petter Selasky if (bus->no_explore != 0) 3432e141748SHans Petter Selasky return; 3442e141748SHans Petter Selasky 34502ac6454SAndrew Thompson if (udev && udev->hub) { 34602ac6454SAndrew Thompson 34702ac6454SAndrew Thompson if (bus->do_probe) { 34802ac6454SAndrew Thompson bus->do_probe = 0; 34902ac6454SAndrew Thompson bus->driver_added_refcount++; 35002ac6454SAndrew Thompson } 35102ac6454SAndrew Thompson if (bus->driver_added_refcount == 0) { 35202ac6454SAndrew Thompson /* avoid zero, hence that is memory default */ 35302ac6454SAndrew Thompson bus->driver_added_refcount = 1; 35402ac6454SAndrew Thompson } 35502ac6454SAndrew Thompson 356f0c078e6SAndrew Thompson #ifdef DDB 35734b48722SAlfred Perlstein /* 35834b48722SAlfred Perlstein * The following three lines of code are only here to 35934b48722SAlfred Perlstein * recover from DDB: 36034b48722SAlfred Perlstein */ 36134b48722SAlfred Perlstein usb_proc_rewakeup(&bus->control_xfer_proc); 36234b48722SAlfred Perlstein usb_proc_rewakeup(&bus->giant_callback_proc); 36334b48722SAlfred Perlstein usb_proc_rewakeup(&bus->non_giant_callback_proc); 364f0c078e6SAndrew Thompson #endif 36534b48722SAlfred Perlstein 36634b48722SAlfred Perlstein USB_BUS_UNLOCK(bus); 367a56fe095SJohn Baldwin 368e727a16cSAndrew Thompson #if USB_HAVE_POWERD 36902ac6454SAndrew Thompson /* 37002ac6454SAndrew Thompson * First update the USB power state! 37102ac6454SAndrew Thompson */ 372a593f6b8SAndrew Thompson usb_bus_powerd(bus); 373e727a16cSAndrew Thompson #endif 37434b48722SAlfred Perlstein /* Explore the Root USB HUB. */ 37502ac6454SAndrew Thompson (udev->hub->explore) (udev); 37602ac6454SAndrew Thompson USB_BUS_LOCK(bus); 37702ac6454SAndrew Thompson } 378d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 3793df007ceSHans Petter Selasky usb_root_mount_rel(bus); 380d2b99310SHans Petter Selasky #endif 38102ac6454SAndrew Thompson } 38202ac6454SAndrew Thompson 38302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 384a593f6b8SAndrew Thompson * usb_bus_detach 38502ac6454SAndrew Thompson * 38602ac6454SAndrew Thompson * This function is used to detach the device tree from the root. 38702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 38802ac6454SAndrew Thompson static void 389a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm) 39002ac6454SAndrew Thompson { 391760bc48eSAndrew Thompson struct usb_bus *bus; 392760bc48eSAndrew Thompson struct usb_device *udev; 39302ac6454SAndrew Thompson device_t dev; 39402ac6454SAndrew Thompson 395760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 39602ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 39702ac6454SAndrew Thompson dev = bus->bdev; 39802ac6454SAndrew Thompson /* clear the softc */ 39902ac6454SAndrew Thompson device_set_softc(dev, NULL); 40002ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 40102ac6454SAndrew Thompson 40202ac6454SAndrew Thompson /* detach children first */ 40334b48722SAlfred Perlstein mtx_lock(&Giant); 40402ac6454SAndrew Thompson bus_generic_detach(dev); 40534b48722SAlfred Perlstein mtx_unlock(&Giant); 40602ac6454SAndrew Thompson 40702ac6454SAndrew Thompson /* 408d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 40902ac6454SAndrew Thompson */ 410d88688c7SAndrew Thompson usb_free_device(udev, 0); 41102ac6454SAndrew Thompson 41202ac6454SAndrew Thompson USB_BUS_LOCK(bus); 41302ac6454SAndrew Thompson /* clear bdev variable last */ 41402ac6454SAndrew Thompson bus->bdev = NULL; 41502ac6454SAndrew Thompson } 41602ac6454SAndrew Thompson 4172e141748SHans Petter Selasky /*------------------------------------------------------------------------* 4182e141748SHans Petter Selasky * usb_bus_suspend 4192e141748SHans Petter Selasky * 4202e141748SHans Petter Selasky * This function is used to suspend the USB contoller. 4212e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 4222e141748SHans Petter Selasky static void 4232e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm) 4242e141748SHans Petter Selasky { 4252e141748SHans Petter Selasky struct usb_bus *bus; 4262e141748SHans Petter Selasky struct usb_device *udev; 4272e141748SHans Petter Selasky usb_error_t err; 4282e141748SHans Petter Selasky 4292e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4302e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4312e141748SHans Petter Selasky 4322e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 4332e141748SHans Petter Selasky return; 4342e141748SHans Petter Selasky 4356bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 4366bd3e535SHans Petter Selasky 43702f728afSHans Petter Selasky /* 43802f728afSHans Petter Selasky * We use the shutdown event here because the suspend and 43902f728afSHans Petter Selasky * resume events are reserved for the USB port suspend and 44002f728afSHans Petter Selasky * resume. The USB system suspend is implemented like full 44102f728afSHans Petter Selasky * shutdown and all connected USB devices will be disconnected 44202f728afSHans Petter Selasky * subsequently. At resume all USB devices will be 44302f728afSHans Petter Selasky * re-connected again. 44402f728afSHans Petter Selasky */ 44502f728afSHans Petter Selasky 4462e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 4472e141748SHans Petter Selasky 4482e141748SHans Petter Selasky usbd_enum_lock(udev); 4492e141748SHans Petter Selasky 4502e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 4512e141748SHans Petter Selasky if (err) 4522e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 4532e141748SHans Petter Selasky 4542e141748SHans Petter Selasky USB_BUS_LOCK(bus); 4552e141748SHans Petter Selasky bus->hw_power_state = 0; 4562e141748SHans Petter Selasky bus->no_explore = 1; 4572e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 4582e141748SHans Petter Selasky 4592e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 4602e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 4612e141748SHans Petter Selasky 4622e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 4632e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 4642e141748SHans Petter Selasky 4652e141748SHans Petter Selasky usbd_enum_unlock(udev); 4666bd3e535SHans Petter Selasky 4676bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 4682e141748SHans Petter Selasky } 4692e141748SHans Petter Selasky 4702e141748SHans Petter Selasky /*------------------------------------------------------------------------* 4712e141748SHans Petter Selasky * usb_bus_resume 4722e141748SHans Petter Selasky * 4732e141748SHans Petter Selasky * This function is used to resume the USB contoller. 4742e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 4752e141748SHans Petter Selasky static void 4762e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm) 4772e141748SHans Petter Selasky { 4782e141748SHans Petter Selasky struct usb_bus *bus; 4792e141748SHans Petter Selasky struct usb_device *udev; 4802e141748SHans Petter Selasky usb_error_t err; 4812e141748SHans Petter Selasky 4822e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4832e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4842e141748SHans Petter Selasky 4852e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 4862e141748SHans Petter Selasky return; 4872e141748SHans Petter Selasky 4886bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 4896bd3e535SHans Petter Selasky 4902e141748SHans Petter Selasky usbd_enum_lock(udev); 4912e141748SHans Petter Selasky #if 0 4922e141748SHans Petter Selasky DEVMETHOD(usb_take_controller, NULL); /* dummy */ 4932e141748SHans Petter Selasky #endif 4942e141748SHans Petter Selasky USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 4952e141748SHans Petter Selasky 4962e141748SHans Petter Selasky USB_BUS_LOCK(bus); 4972e141748SHans Petter Selasky bus->hw_power_state = 4982e141748SHans Petter Selasky USB_HW_POWER_CONTROL | 4992e141748SHans Petter Selasky USB_HW_POWER_BULK | 5002e141748SHans Petter Selasky USB_HW_POWER_INTERRUPT | 5012e141748SHans Petter Selasky USB_HW_POWER_ISOC | 5022e141748SHans Petter Selasky USB_HW_POWER_NON_ROOT_HUB; 5032e141748SHans Petter Selasky bus->no_explore = 0; 5042e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 5052e141748SHans Petter Selasky 5062e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 5072e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 5082e141748SHans Petter Selasky 5092e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 5102e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 5112e141748SHans Petter Selasky 5126bbe7cdfSHans Petter Selasky /* restore USB configuration to index 0 */ 5132e141748SHans Petter Selasky err = usbd_set_config_index(udev, 0); 5142e141748SHans Petter Selasky if (err) 5152e141748SHans Petter Selasky device_printf(bus->bdev, "Could not configure root HUB\n"); 5162e141748SHans Petter Selasky 5176bbe7cdfSHans Petter Selasky /* probe and attach */ 5186bbe7cdfSHans Petter Selasky err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY); 5196bbe7cdfSHans Petter Selasky if (err) { 5206bbe7cdfSHans Petter Selasky device_printf(bus->bdev, "Could not probe and " 5216bbe7cdfSHans Petter Selasky "attach root HUB\n"); 5226bbe7cdfSHans Petter Selasky } 5236bbe7cdfSHans Petter Selasky 5242e141748SHans Petter Selasky usbd_enum_unlock(udev); 5256bd3e535SHans Petter Selasky 5266bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5272e141748SHans Petter Selasky } 5282e141748SHans Petter Selasky 5292e141748SHans Petter Selasky /*------------------------------------------------------------------------* 5302e141748SHans Petter Selasky * usb_bus_shutdown 5312e141748SHans Petter Selasky * 5322e141748SHans Petter Selasky * This function is used to shutdown the USB contoller. 5332e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 5342e141748SHans Petter Selasky static void 5352e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm) 5362e141748SHans Petter Selasky { 5372e141748SHans Petter Selasky struct usb_bus *bus; 5382e141748SHans Petter Selasky struct usb_device *udev; 5392e141748SHans Petter Selasky usb_error_t err; 5402e141748SHans Petter Selasky 5412e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 5422e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 5432e141748SHans Petter Selasky 5442e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 5452e141748SHans Petter Selasky return; 5462e141748SHans Petter Selasky 5476bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 5486bd3e535SHans Petter Selasky 5492e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 5502e141748SHans Petter Selasky 5512e141748SHans Petter Selasky usbd_enum_lock(udev); 5522e141748SHans Petter Selasky 5532e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 5542e141748SHans Petter Selasky if (err) 5552e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 5562e141748SHans Petter Selasky 5572e141748SHans Petter Selasky USB_BUS_LOCK(bus); 5582e141748SHans Petter Selasky bus->hw_power_state = 0; 5592e141748SHans Petter Selasky bus->no_explore = 1; 5602e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 5612e141748SHans Petter Selasky 5622e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 5632e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 5642e141748SHans Petter Selasky 5652e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 5662e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 5672e141748SHans Petter Selasky 5682e141748SHans Petter Selasky usbd_enum_unlock(udev); 5696bd3e535SHans Petter Selasky 5706bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5712e141748SHans Petter Selasky } 5722e141748SHans Petter Selasky 57302ac6454SAndrew Thompson static void 574a593f6b8SAndrew Thompson usb_power_wdog(void *arg) 57502ac6454SAndrew Thompson { 576760bc48eSAndrew Thompson struct usb_bus *bus = arg; 57702ac6454SAndrew Thompson 57802ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 57902ac6454SAndrew Thompson 580a593f6b8SAndrew Thompson usb_callout_reset(&bus->power_wdog, 581a593f6b8SAndrew Thompson 4 * hz, usb_power_wdog, arg); 58202ac6454SAndrew Thompson 583f0c078e6SAndrew Thompson #ifdef DDB 58434b48722SAlfred Perlstein /* 58534b48722SAlfred Perlstein * The following line of code is only here to recover from 58634b48722SAlfred Perlstein * DDB: 58734b48722SAlfred Perlstein */ 58834b48722SAlfred Perlstein usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 589f0c078e6SAndrew Thompson #endif 59034b48722SAlfred Perlstein 591e727a16cSAndrew Thompson #if USB_HAVE_POWERD 59202ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 59302ac6454SAndrew Thompson 594a593f6b8SAndrew Thompson usb_bus_power_update(bus); 59502ac6454SAndrew Thompson 596684e3f22SAndrew Thompson USB_BUS_LOCK(bus); 597e727a16cSAndrew Thompson #endif 59802ac6454SAndrew Thompson } 59902ac6454SAndrew Thompson 60002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 601a593f6b8SAndrew Thompson * usb_bus_attach 60202ac6454SAndrew Thompson * 60302ac6454SAndrew Thompson * This function attaches USB in context of the explore thread. 60402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 60502ac6454SAndrew Thompson static void 606a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm) 60702ac6454SAndrew Thompson { 608760bc48eSAndrew Thompson struct usb_bus *bus; 609760bc48eSAndrew Thompson struct usb_device *child; 61002ac6454SAndrew Thompson device_t dev; 611e0a69b51SAndrew Thompson usb_error_t err; 6128d2dd5ddSAndrew Thompson enum usb_dev_speed speed; 61302ac6454SAndrew Thompson 614760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 61502ac6454SAndrew Thompson dev = bus->bdev; 61602ac6454SAndrew Thompson 61702ac6454SAndrew Thompson DPRINTF("\n"); 61802ac6454SAndrew Thompson 61902ac6454SAndrew Thompson switch (bus->usbrev) { 62002ac6454SAndrew Thompson case USB_REV_1_0: 62102ac6454SAndrew Thompson speed = USB_SPEED_FULL; 62202ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 62302ac6454SAndrew Thompson break; 62402ac6454SAndrew Thompson 62502ac6454SAndrew Thompson case USB_REV_1_1: 62602ac6454SAndrew Thompson speed = USB_SPEED_FULL; 62702ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 62802ac6454SAndrew Thompson break; 62902ac6454SAndrew Thompson 63002ac6454SAndrew Thompson case USB_REV_2_0: 63102ac6454SAndrew Thompson speed = USB_SPEED_HIGH; 63202ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 63302ac6454SAndrew Thompson break; 63402ac6454SAndrew Thompson 63502ac6454SAndrew Thompson case USB_REV_2_5: 63602ac6454SAndrew Thompson speed = USB_SPEED_VARIABLE; 63702ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 63802ac6454SAndrew Thompson break; 63902ac6454SAndrew Thompson 640963169b4SHans Petter Selasky case USB_REV_3_0: 641963169b4SHans Petter Selasky speed = USB_SPEED_SUPER; 642ccac019aSHans Petter Selasky device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 643963169b4SHans Petter Selasky break; 644963169b4SHans Petter Selasky 64502ac6454SAndrew Thompson default: 646767cb2e2SAndrew Thompson device_printf(bus->bdev, "Unsupported USB revision\n"); 647d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 6483df007ceSHans Petter Selasky usb_root_mount_rel(bus); 649d2b99310SHans Petter Selasky #endif 65002ac6454SAndrew Thompson return; 65102ac6454SAndrew Thompson } 65202ac6454SAndrew Thompson 6534eae601eSAndrew Thompson /* default power_mask value */ 6544eae601eSAndrew Thompson bus->hw_power_state = 6554eae601eSAndrew Thompson USB_HW_POWER_CONTROL | 6564eae601eSAndrew Thompson USB_HW_POWER_BULK | 6574eae601eSAndrew Thompson USB_HW_POWER_INTERRUPT | 6584eae601eSAndrew Thompson USB_HW_POWER_ISOC | 6594eae601eSAndrew Thompson USB_HW_POWER_NON_ROOT_HUB; 6604eae601eSAndrew Thompson 6612e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 6622e141748SHans Petter Selasky 6634eae601eSAndrew Thompson /* make sure power is set at least once */ 6644eae601eSAndrew Thompson 6654eae601eSAndrew Thompson if (bus->methods->set_hw_power != NULL) { 6664eae601eSAndrew Thompson (bus->methods->set_hw_power) (bus); 6674eae601eSAndrew Thompson } 6684eae601eSAndrew Thompson 6692e141748SHans Petter Selasky /* allocate the Root USB device */ 67002ac6454SAndrew Thompson 671a593f6b8SAndrew Thompson child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 67202ac6454SAndrew Thompson speed, USB_MODE_HOST); 67302ac6454SAndrew Thompson if (child) { 674a593f6b8SAndrew Thompson err = usb_probe_and_attach(child, 67502ac6454SAndrew Thompson USB_IFACE_INDEX_ANY); 67602ac6454SAndrew Thompson if (!err) { 6771be5bf51SAndrew Thompson if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 6781be5bf51SAndrew Thompson (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 67902ac6454SAndrew Thompson err = USB_ERR_NO_ROOT_HUB; 68002ac6454SAndrew Thompson } 68102ac6454SAndrew Thompson } 68202ac6454SAndrew Thompson } else { 68302ac6454SAndrew Thompson err = USB_ERR_NOMEM; 68402ac6454SAndrew Thompson } 68502ac6454SAndrew Thompson 68602ac6454SAndrew Thompson USB_BUS_LOCK(bus); 68702ac6454SAndrew Thompson 68802ac6454SAndrew Thompson if (err) { 68902ac6454SAndrew Thompson device_printf(bus->bdev, "Root HUB problem, error=%s\n", 690a593f6b8SAndrew Thompson usbd_errstr(err)); 691d2b99310SHans Petter Selasky #if USB_HAVE_ROOT_MOUNT_HOLD 6923df007ceSHans Petter Selasky usb_root_mount_rel(bus); 693d2b99310SHans Petter Selasky #endif 69402ac6454SAndrew Thompson } 69502ac6454SAndrew Thompson 69602ac6454SAndrew Thompson /* set softc - we are ready */ 69702ac6454SAndrew Thompson device_set_softc(dev, bus); 69802ac6454SAndrew Thompson 699684e3f22SAndrew Thompson /* start watchdog */ 700a593f6b8SAndrew Thompson usb_power_wdog(bus); 70102ac6454SAndrew Thompson } 70202ac6454SAndrew Thompson 70302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 704a593f6b8SAndrew Thompson * usb_attach_sub 70502ac6454SAndrew Thompson * 70634b48722SAlfred Perlstein * This function creates a thread which runs the USB attach code. 70702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 70802ac6454SAndrew Thompson static void 709a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus) 71002ac6454SAndrew Thompson { 71102ac6454SAndrew Thompson const char *pname = device_get_nameunit(dev); 71202ac6454SAndrew Thompson 71334b48722SAlfred Perlstein mtx_lock(&Giant); 71434b48722SAlfred Perlstein if (usb_devclass_ptr == NULL) 71534b48722SAlfred Perlstein usb_devclass_ptr = devclass_find("usbus"); 71634b48722SAlfred Perlstein mtx_unlock(&Giant); 71734b48722SAlfred Perlstein 7188be09334SHans Petter Selasky #if USB_HAVE_PF 719fe1c24e3SWeongyo Jeong usbpf_attach(bus); 7208be09334SHans Petter Selasky #endif 72102ac6454SAndrew Thompson /* Initialise USB process messages */ 722a593f6b8SAndrew Thompson bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 72302ac6454SAndrew Thompson bus->explore_msg[0].bus = bus; 724a593f6b8SAndrew Thompson bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 72502ac6454SAndrew Thompson bus->explore_msg[1].bus = bus; 72602ac6454SAndrew Thompson 727a593f6b8SAndrew Thompson bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 72802ac6454SAndrew Thompson bus->detach_msg[0].bus = bus; 729a593f6b8SAndrew Thompson bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 73002ac6454SAndrew Thompson bus->detach_msg[1].bus = bus; 73102ac6454SAndrew Thompson 732a593f6b8SAndrew Thompson bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 73302ac6454SAndrew Thompson bus->attach_msg[0].bus = bus; 734a593f6b8SAndrew Thompson bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 73502ac6454SAndrew Thompson bus->attach_msg[1].bus = bus; 73602ac6454SAndrew Thompson 7372e141748SHans Petter Selasky bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 7382e141748SHans Petter Selasky bus->suspend_msg[0].bus = bus; 7392e141748SHans Petter Selasky bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 7402e141748SHans Petter Selasky bus->suspend_msg[1].bus = bus; 7412e141748SHans Petter Selasky 7422e141748SHans Petter Selasky bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 7432e141748SHans Petter Selasky bus->resume_msg[0].bus = bus; 7442e141748SHans Petter Selasky bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 7452e141748SHans Petter Selasky bus->resume_msg[1].bus = bus; 7462e141748SHans Petter Selasky 7472e141748SHans Petter Selasky bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 7482e141748SHans Petter Selasky bus->shutdown_msg[0].bus = bus; 7492e141748SHans Petter Selasky bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 7502e141748SHans Petter Selasky bus->shutdown_msg[1].bus = bus; 7512e141748SHans Petter Selasky 75239307315SAndrew Thompson /* Create USB explore and callback processes */ 75302ac6454SAndrew Thompson 754a593f6b8SAndrew Thompson if (usb_proc_create(&bus->giant_callback_proc, 75502ac6454SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_MED)) { 75688334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB Giant " 75702ac6454SAndrew Thompson "callback process failed.\n"); 758a593f6b8SAndrew Thompson } else if (usb_proc_create(&bus->non_giant_callback_proc, 75902ac6454SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_HIGH)) { 76088334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB non-Giant " 76102ac6454SAndrew Thompson "callback process failed.\n"); 762a593f6b8SAndrew Thompson } else if (usb_proc_create(&bus->explore_proc, 76302ac6454SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_MED)) { 76488334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB explore " 76502ac6454SAndrew Thompson "process failed.\n"); 766a593f6b8SAndrew Thompson } else if (usb_proc_create(&bus->control_xfer_proc, 767672c9965SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_MED)) { 76888334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB control transfer " 769672c9965SAndrew Thompson "process failed.\n"); 77002ac6454SAndrew Thompson } else { 77102ac6454SAndrew Thompson /* Get final attach going */ 77202ac6454SAndrew Thompson USB_BUS_LOCK(bus); 7732e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 7742e141748SHans Petter Selasky &bus->attach_msg[0], &bus->attach_msg[1]); 77502ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 77634b48722SAlfred Perlstein 77734b48722SAlfred Perlstein /* Do initial explore */ 77834b48722SAlfred Perlstein usb_needs_explore(bus, 1); 77902ac6454SAndrew Thompson } 78002ac6454SAndrew Thompson } 78102ac6454SAndrew Thompson 782a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 78302ac6454SAndrew Thompson 78402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 785a593f6b8SAndrew Thompson * usb_bus_mem_flush_all_cb 78602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 787bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 78802ac6454SAndrew Thompson static void 789a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 790f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 79102ac6454SAndrew Thompson { 792a593f6b8SAndrew Thompson usb_pc_cpu_flush(pc); 79302ac6454SAndrew Thompson } 794bdc081c6SAndrew Thompson #endif 79502ac6454SAndrew Thompson 79602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 797a593f6b8SAndrew Thompson * usb_bus_mem_flush_all - factored out code 79802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 799bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 80002ac6454SAndrew Thompson void 801a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 80202ac6454SAndrew Thompson { 80302ac6454SAndrew Thompson if (cb) { 804a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_flush_all_cb); 80502ac6454SAndrew Thompson } 80602ac6454SAndrew Thompson } 807bdc081c6SAndrew Thompson #endif 80802ac6454SAndrew Thompson 80902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 810a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all_cb 81102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 812bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 81302ac6454SAndrew Thompson static void 814a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 815f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 81602ac6454SAndrew Thompson { 81702ac6454SAndrew Thompson /* need to initialize the page cache */ 81802ac6454SAndrew Thompson pc->tag_parent = bus->dma_parent_tag; 81902ac6454SAndrew Thompson 820a593f6b8SAndrew Thompson if (usb_pc_alloc_mem(pc, pg, size, align)) { 82102ac6454SAndrew Thompson bus->alloc_failed = 1; 82202ac6454SAndrew Thompson } 82302ac6454SAndrew Thompson } 824bdc081c6SAndrew Thompson #endif 82502ac6454SAndrew Thompson 82602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 827a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all - factored out code 82802ac6454SAndrew Thompson * 82902ac6454SAndrew Thompson * Returns: 83002ac6454SAndrew Thompson * 0: Success 83102ac6454SAndrew Thompson * Else: Failure 83202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 83302ac6454SAndrew Thompson uint8_t 834a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 835e0a69b51SAndrew Thompson usb_bus_mem_cb_t *cb) 83602ac6454SAndrew Thompson { 83702ac6454SAndrew Thompson bus->alloc_failed = 0; 83802ac6454SAndrew Thompson 83902ac6454SAndrew Thompson mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 84002ac6454SAndrew Thompson NULL, MTX_DEF | MTX_RECURSE); 84102ac6454SAndrew Thompson 842a593f6b8SAndrew Thompson usb_callout_init_mtx(&bus->power_wdog, 843684e3f22SAndrew Thompson &bus->bus_mtx, 0); 84402ac6454SAndrew Thompson 84502ac6454SAndrew Thompson TAILQ_INIT(&bus->intr_q.head); 84602ac6454SAndrew Thompson 847bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 848a593f6b8SAndrew Thompson usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 849bdc081c6SAndrew Thompson dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 850bdc081c6SAndrew Thompson #endif 85102ac6454SAndrew Thompson if ((bus->devices_max > USB_MAX_DEVICES) || 85202ac6454SAndrew Thompson (bus->devices_max < USB_MIN_DEVICES) || 85302ac6454SAndrew Thompson (bus->devices == NULL)) { 85402ac6454SAndrew Thompson DPRINTFN(0, "Devices field has not been " 855767cb2e2SAndrew Thompson "initialised properly\n"); 85602ac6454SAndrew Thompson bus->alloc_failed = 1; /* failure */ 85702ac6454SAndrew Thompson } 858bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 85902ac6454SAndrew Thompson if (cb) { 860a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_alloc_all_cb); 86102ac6454SAndrew Thompson } 862bdc081c6SAndrew Thompson #endif 86302ac6454SAndrew Thompson if (bus->alloc_failed) { 864a593f6b8SAndrew Thompson usb_bus_mem_free_all(bus, cb); 86502ac6454SAndrew Thompson } 86602ac6454SAndrew Thompson return (bus->alloc_failed); 86702ac6454SAndrew Thompson } 86802ac6454SAndrew Thompson 86902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 870a593f6b8SAndrew Thompson * usb_bus_mem_free_all_cb 87102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 872bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 87302ac6454SAndrew Thompson static void 874a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 875f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 87602ac6454SAndrew Thompson { 877a593f6b8SAndrew Thompson usb_pc_free_mem(pc); 87802ac6454SAndrew Thompson } 879bdc081c6SAndrew Thompson #endif 88002ac6454SAndrew Thompson 88102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 882a593f6b8SAndrew Thompson * usb_bus_mem_free_all - factored out code 88302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 88402ac6454SAndrew Thompson void 885a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 88602ac6454SAndrew Thompson { 887bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 88802ac6454SAndrew Thompson if (cb) { 889a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_free_all_cb); 89002ac6454SAndrew Thompson } 891a593f6b8SAndrew Thompson usb_dma_tag_unsetup(bus->dma_parent_tag); 892bdc081c6SAndrew Thompson #endif 89302ac6454SAndrew Thompson 89402ac6454SAndrew Thompson mtx_destroy(&bus->bus_mtx); 89502ac6454SAndrew Thompson } 896