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 27f0c078e6SAndrew Thompson #include "opt_ddb.h" 28f0c078e6SAndrew Thompson 29ed6d949aSAndrew Thompson #include <sys/stdint.h> 30ed6d949aSAndrew Thompson #include <sys/stddef.h> 31ed6d949aSAndrew Thompson #include <sys/param.h> 32ed6d949aSAndrew Thompson #include <sys/queue.h> 33ed6d949aSAndrew Thompson #include <sys/types.h> 34ed6d949aSAndrew Thompson #include <sys/systm.h> 35ed6d949aSAndrew Thompson #include <sys/kernel.h> 36ed6d949aSAndrew Thompson #include <sys/bus.h> 37ed6d949aSAndrew Thompson #include <sys/module.h> 38ed6d949aSAndrew Thompson #include <sys/lock.h> 39ed6d949aSAndrew Thompson #include <sys/mutex.h> 40ed6d949aSAndrew Thompson #include <sys/condvar.h> 41ed6d949aSAndrew Thompson #include <sys/sysctl.h> 42ed6d949aSAndrew Thompson #include <sys/sx.h> 43ed6d949aSAndrew Thompson #include <sys/unistd.h> 44ed6d949aSAndrew Thompson #include <sys/callout.h> 45ed6d949aSAndrew Thompson #include <sys/malloc.h> 46ed6d949aSAndrew Thompson #include <sys/priv.h> 47ed6d949aSAndrew Thompson 4802ac6454SAndrew Thompson #include <dev/usb/usb.h> 49ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 5002ac6454SAndrew Thompson 51a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_ctrl_debug 5202ac6454SAndrew Thompson 5302ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 5402ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 5502ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 5702ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 5802ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 5902ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 6002ac6454SAndrew Thompson 6102ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 6202ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 6318ec6525SWeongyo Jeong #include <dev/usb/usb_pf.h> 642e141748SHans Petter Selasky #include "usb_if.h" 6502ac6454SAndrew Thompson 6602ac6454SAndrew Thompson /* function prototypes */ 6702ac6454SAndrew Thompson 68a593f6b8SAndrew Thompson static device_probe_t usb_probe; 69a593f6b8SAndrew Thompson static device_attach_t usb_attach; 70a593f6b8SAndrew Thompson static device_detach_t usb_detach; 712e141748SHans Petter Selasky static device_suspend_t usb_suspend; 722e141748SHans Petter Selasky static device_resume_t usb_resume; 732e141748SHans Petter Selasky static device_shutdown_t usb_shutdown; 7402ac6454SAndrew Thompson 75a593f6b8SAndrew Thompson static void usb_attach_sub(device_t, struct usb_bus *); 7602ac6454SAndrew Thompson 7702ac6454SAndrew Thompson /* static variables */ 7802ac6454SAndrew Thompson 79ed6d949aSAndrew Thompson #ifdef USB_DEBUG 80a593f6b8SAndrew Thompson static int usb_ctrl_debug = 0; 8102ac6454SAndrew Thompson 826472ac3dSEd Schouten static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller"); 83a593f6b8SAndrew Thompson SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0, 8402ac6454SAndrew Thompson "Debug level"); 8502ac6454SAndrew Thompson #endif 8602ac6454SAndrew Thompson 87a0c61406SAlfred Perlstein static int usb_no_boot_wait = 0; 88a0c61406SAlfred Perlstein TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait); 89a0c61406SAlfred Perlstein SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0, 90*5ed294aeSHans Petter Selasky "No USB device enumerate waiting at boot."); 91*5ed294aeSHans Petter Selasky 92*5ed294aeSHans Petter Selasky static int usb_no_shutdown_wait = 0; 93*5ed294aeSHans Petter Selasky TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait); 94*5ed294aeSHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RDTUN, &usb_no_shutdown_wait, 0, 95*5ed294aeSHans Petter Selasky "No USB device waiting at system shutdown."); 96a0c61406SAlfred Perlstein 97a593f6b8SAndrew Thompson static devclass_t usb_devclass; 9802ac6454SAndrew Thompson 99a593f6b8SAndrew Thompson static device_method_t usb_methods[] = { 100a593f6b8SAndrew Thompson DEVMETHOD(device_probe, usb_probe), 101a593f6b8SAndrew Thompson DEVMETHOD(device_attach, usb_attach), 102a593f6b8SAndrew Thompson DEVMETHOD(device_detach, usb_detach), 1032e141748SHans Petter Selasky DEVMETHOD(device_suspend, usb_suspend), 1042e141748SHans Petter Selasky DEVMETHOD(device_resume, usb_resume), 1052e141748SHans Petter Selasky DEVMETHOD(device_shutdown, usb_shutdown), 10602ac6454SAndrew Thompson {0, 0} 10702ac6454SAndrew Thompson }; 10802ac6454SAndrew Thompson 109a593f6b8SAndrew Thompson static driver_t usb_driver = { 11002ac6454SAndrew Thompson .name = "usbus", 111a593f6b8SAndrew Thompson .methods = usb_methods, 11202ac6454SAndrew Thompson .size = 0, 11302ac6454SAndrew Thompson }; 11402ac6454SAndrew Thompson 115864bc412SHans Petter Selasky /* Host Only Drivers */ 116a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0); 117a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0); 118a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0); 119963169b4SHans Petter Selasky DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0); 120864bc412SHans Petter Selasky 121864bc412SHans Petter Selasky /* Device Only Drivers */ 122a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0); 123864bc412SHans Petter Selasky DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0); 124a593f6b8SAndrew Thompson DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0); 12502ac6454SAndrew Thompson 12602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 127a593f6b8SAndrew Thompson * usb_probe 12802ac6454SAndrew Thompson * 12902ac6454SAndrew Thompson * This function is called from "{ehci,ohci,uhci}_pci_attach()". 13002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 13102ac6454SAndrew Thompson static int 132a593f6b8SAndrew Thompson usb_probe(device_t dev) 13302ac6454SAndrew Thompson { 13402ac6454SAndrew Thompson DPRINTF("\n"); 13502ac6454SAndrew Thompson return (0); 13602ac6454SAndrew Thompson } 13702ac6454SAndrew Thompson 1383df007ceSHans Petter Selasky static void 1393df007ceSHans Petter Selasky usb_root_mount_rel(struct usb_bus *bus) 1403df007ceSHans Petter Selasky { 1413df007ceSHans Petter Selasky if (bus->bus_roothold != NULL) { 1423df007ceSHans Petter Selasky DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold); 1433df007ceSHans Petter Selasky root_mount_rel(bus->bus_roothold); 1443df007ceSHans Petter Selasky bus->bus_roothold = NULL; 1453df007ceSHans Petter Selasky } 1463df007ceSHans Petter Selasky } 1473df007ceSHans Petter Selasky 14802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 149a593f6b8SAndrew Thompson * usb_attach 15002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 15102ac6454SAndrew Thompson static int 152a593f6b8SAndrew Thompson usb_attach(device_t dev) 15302ac6454SAndrew Thompson { 154760bc48eSAndrew Thompson struct usb_bus *bus = device_get_ivars(dev); 15502ac6454SAndrew Thompson 15602ac6454SAndrew Thompson DPRINTF("\n"); 15702ac6454SAndrew Thompson 15802ac6454SAndrew Thompson if (bus == NULL) { 159767cb2e2SAndrew Thompson device_printf(dev, "USB device has no ivars\n"); 16002ac6454SAndrew Thompson return (ENXIO); 16102ac6454SAndrew Thompson } 16202ac6454SAndrew Thompson 163a0c61406SAlfred Perlstein if (usb_no_boot_wait == 0) { 16402ac6454SAndrew Thompson /* delay vfs_mountroot until the bus is explored */ 165853a10a5SAndrew Thompson bus->bus_roothold = root_mount_hold(device_get_nameunit(dev)); 166a0c61406SAlfred Perlstein } 16702ac6454SAndrew Thompson 168a593f6b8SAndrew Thompson usb_attach_sub(dev, bus); 16934b48722SAlfred Perlstein 17002ac6454SAndrew Thompson return (0); /* return success */ 17102ac6454SAndrew Thompson } 17202ac6454SAndrew Thompson 17302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 174a593f6b8SAndrew Thompson * usb_detach 17502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 17602ac6454SAndrew Thompson static int 177a593f6b8SAndrew Thompson usb_detach(device_t dev) 17802ac6454SAndrew Thompson { 179760bc48eSAndrew Thompson struct usb_bus *bus = device_get_softc(dev); 18002ac6454SAndrew Thompson 18102ac6454SAndrew Thompson DPRINTF("\n"); 18202ac6454SAndrew Thompson 18302ac6454SAndrew Thompson if (bus == NULL) { 18402ac6454SAndrew Thompson /* was never setup properly */ 18502ac6454SAndrew Thompson return (0); 18602ac6454SAndrew Thompson } 18702ac6454SAndrew Thompson /* Stop power watchdog */ 188a593f6b8SAndrew Thompson usb_callout_drain(&bus->power_wdog); 18902ac6454SAndrew Thompson 19002ac6454SAndrew Thompson /* Let the USB explore process detach all devices. */ 1913df007ceSHans Petter Selasky usb_root_mount_rel(bus); 19202ac6454SAndrew Thompson 19302ac6454SAndrew Thompson USB_BUS_LOCK(bus); 19402ac6454SAndrew Thompson 1952e141748SHans Petter Selasky /* Queue detach job */ 1962e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 1972e141748SHans Petter Selasky &bus->detach_msg[0], &bus->detach_msg[1]); 1982e141748SHans Petter Selasky 1992e141748SHans Petter Selasky /* Wait for detach to complete */ 200a593f6b8SAndrew Thompson usb_proc_mwait(&bus->explore_proc, 20102ac6454SAndrew Thompson &bus->detach_msg[0], &bus->detach_msg[1]); 20202ac6454SAndrew Thompson 20302ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 20402ac6454SAndrew Thompson 20502ac6454SAndrew Thompson /* Get rid of USB callback processes */ 20602ac6454SAndrew Thompson 207a593f6b8SAndrew Thompson usb_proc_free(&bus->giant_callback_proc); 208a593f6b8SAndrew Thompson usb_proc_free(&bus->non_giant_callback_proc); 20902ac6454SAndrew Thompson 21002ac6454SAndrew Thompson /* Get rid of USB explore process */ 21102ac6454SAndrew Thompson 212a593f6b8SAndrew Thompson usb_proc_free(&bus->explore_proc); 21302ac6454SAndrew Thompson 214672c9965SAndrew Thompson /* Get rid of control transfer process */ 215672c9965SAndrew Thompson 216a593f6b8SAndrew Thompson usb_proc_free(&bus->control_xfer_proc); 217672c9965SAndrew Thompson 2188be09334SHans Petter Selasky #if USB_HAVE_PF 219fe1c24e3SWeongyo Jeong usbpf_detach(bus); 2208be09334SHans Petter Selasky #endif 22102ac6454SAndrew Thompson return (0); 22202ac6454SAndrew Thompson } 22302ac6454SAndrew Thompson 22402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2252e141748SHans Petter Selasky * usb_suspend 2262e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2272e141748SHans Petter Selasky static int 2282e141748SHans Petter Selasky usb_suspend(device_t dev) 2292e141748SHans Petter Selasky { 2302e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2312e141748SHans Petter Selasky 2322e141748SHans Petter Selasky DPRINTF("\n"); 2332e141748SHans Petter Selasky 2342e141748SHans Petter Selasky if (bus == NULL) { 2352e141748SHans Petter Selasky /* was never setup properly */ 2362e141748SHans Petter Selasky return (0); 2372e141748SHans Petter Selasky } 2382e141748SHans Petter Selasky 2392e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2402e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 2412e141748SHans Petter Selasky &bus->suspend_msg[0], &bus->suspend_msg[1]); 2422e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2432e141748SHans Petter Selasky 2442e141748SHans Petter Selasky return (0); 2452e141748SHans Petter Selasky } 2462e141748SHans Petter Selasky 2472e141748SHans Petter Selasky /*------------------------------------------------------------------------* 2482e141748SHans Petter Selasky * usb_resume 2492e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2502e141748SHans Petter Selasky static int 2512e141748SHans Petter Selasky usb_resume(device_t dev) 2522e141748SHans Petter Selasky { 2532e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2542e141748SHans Petter Selasky 2552e141748SHans Petter Selasky DPRINTF("\n"); 2562e141748SHans Petter Selasky 2572e141748SHans Petter Selasky if (bus == NULL) { 2582e141748SHans Petter Selasky /* was never setup properly */ 2592e141748SHans Petter Selasky return (0); 2602e141748SHans Petter Selasky } 2612e141748SHans Petter Selasky 2622e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2632e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 2642e141748SHans Petter Selasky &bus->resume_msg[0], &bus->resume_msg[1]); 2652e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2662e141748SHans Petter Selasky 2672e141748SHans Petter Selasky return (0); 2682e141748SHans Petter Selasky } 2692e141748SHans Petter Selasky 2702e141748SHans Petter Selasky /*------------------------------------------------------------------------* 2712e141748SHans Petter Selasky * usb_shutdown 2722e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 2732e141748SHans Petter Selasky static int 2742e141748SHans Petter Selasky usb_shutdown(device_t dev) 2752e141748SHans Petter Selasky { 2762e141748SHans Petter Selasky struct usb_bus *bus = device_get_softc(dev); 2772e141748SHans Petter Selasky 2782e141748SHans Petter Selasky DPRINTF("\n"); 2792e141748SHans Petter Selasky 2802e141748SHans Petter Selasky if (bus == NULL) { 2812e141748SHans Petter Selasky /* was never setup properly */ 2822e141748SHans Petter Selasky return (0); 2832e141748SHans Petter Selasky } 2842e141748SHans Petter Selasky 285*5ed294aeSHans Petter Selasky device_printf(bus->bdev, "Controller shutdown\n"); 286*5ed294aeSHans Petter Selasky 2872e141748SHans Petter Selasky USB_BUS_LOCK(bus); 2882e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 2892e141748SHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 290*5ed294aeSHans Petter Selasky if (usb_no_shutdown_wait == 0) { 291*5ed294aeSHans Petter Selasky /* wait for shutdown callback to be executed */ 292*5ed294aeSHans Petter Selasky usb_proc_mwait(&bus->explore_proc, 293*5ed294aeSHans Petter Selasky &bus->shutdown_msg[0], &bus->shutdown_msg[1]); 294*5ed294aeSHans Petter Selasky } 2952e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 2962e141748SHans Petter Selasky 297*5ed294aeSHans Petter Selasky device_printf(bus->bdev, "Controller shutdown complete\n"); 298*5ed294aeSHans Petter Selasky 2992e141748SHans Petter Selasky return (0); 3002e141748SHans Petter Selasky } 3012e141748SHans Petter Selasky 3022e141748SHans Petter Selasky /*------------------------------------------------------------------------* 303a593f6b8SAndrew Thompson * usb_bus_explore 30402ac6454SAndrew Thompson * 30502ac6454SAndrew Thompson * This function is used to explore the device tree from the root. 30602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 30702ac6454SAndrew Thompson static void 308a593f6b8SAndrew Thompson usb_bus_explore(struct usb_proc_msg *pm) 30902ac6454SAndrew Thompson { 310760bc48eSAndrew Thompson struct usb_bus *bus; 311760bc48eSAndrew Thompson struct usb_device *udev; 31202ac6454SAndrew Thompson 313760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 31402ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 31502ac6454SAndrew Thompson 3162e141748SHans Petter Selasky if (bus->no_explore != 0) 3172e141748SHans Petter Selasky return; 3182e141748SHans Petter Selasky 31902ac6454SAndrew Thompson if (udev && udev->hub) { 32002ac6454SAndrew Thompson 32102ac6454SAndrew Thompson if (bus->do_probe) { 32202ac6454SAndrew Thompson bus->do_probe = 0; 32302ac6454SAndrew Thompson bus->driver_added_refcount++; 32402ac6454SAndrew Thompson } 32502ac6454SAndrew Thompson if (bus->driver_added_refcount == 0) { 32602ac6454SAndrew Thompson /* avoid zero, hence that is memory default */ 32702ac6454SAndrew Thompson bus->driver_added_refcount = 1; 32802ac6454SAndrew Thompson } 32902ac6454SAndrew Thompson 330f0c078e6SAndrew Thompson #ifdef DDB 33134b48722SAlfred Perlstein /* 33234b48722SAlfred Perlstein * The following three lines of code are only here to 33334b48722SAlfred Perlstein * recover from DDB: 33434b48722SAlfred Perlstein */ 33534b48722SAlfred Perlstein usb_proc_rewakeup(&bus->control_xfer_proc); 33634b48722SAlfred Perlstein usb_proc_rewakeup(&bus->giant_callback_proc); 33734b48722SAlfred Perlstein usb_proc_rewakeup(&bus->non_giant_callback_proc); 338f0c078e6SAndrew Thompson #endif 33934b48722SAlfred Perlstein 34034b48722SAlfred Perlstein USB_BUS_UNLOCK(bus); 341a56fe095SJohn Baldwin 342e727a16cSAndrew Thompson #if USB_HAVE_POWERD 34302ac6454SAndrew Thompson /* 34402ac6454SAndrew Thompson * First update the USB power state! 34502ac6454SAndrew Thompson */ 346a593f6b8SAndrew Thompson usb_bus_powerd(bus); 347e727a16cSAndrew Thompson #endif 34834b48722SAlfred Perlstein /* Explore the Root USB HUB. */ 34902ac6454SAndrew Thompson (udev->hub->explore) (udev); 35002ac6454SAndrew Thompson USB_BUS_LOCK(bus); 35102ac6454SAndrew Thompson } 3523df007ceSHans Petter Selasky usb_root_mount_rel(bus); 35302ac6454SAndrew Thompson } 35402ac6454SAndrew Thompson 35502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 356a593f6b8SAndrew Thompson * usb_bus_detach 35702ac6454SAndrew Thompson * 35802ac6454SAndrew Thompson * This function is used to detach the device tree from the root. 35902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 36002ac6454SAndrew Thompson static void 361a593f6b8SAndrew Thompson usb_bus_detach(struct usb_proc_msg *pm) 36202ac6454SAndrew Thompson { 363760bc48eSAndrew Thompson struct usb_bus *bus; 364760bc48eSAndrew Thompson struct usb_device *udev; 36502ac6454SAndrew Thompson device_t dev; 36602ac6454SAndrew Thompson 367760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 36802ac6454SAndrew Thompson udev = bus->devices[USB_ROOT_HUB_ADDR]; 36902ac6454SAndrew Thompson dev = bus->bdev; 37002ac6454SAndrew Thompson /* clear the softc */ 37102ac6454SAndrew Thompson device_set_softc(dev, NULL); 37202ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 37302ac6454SAndrew Thompson 37402ac6454SAndrew Thompson /* detach children first */ 37534b48722SAlfred Perlstein mtx_lock(&Giant); 37602ac6454SAndrew Thompson bus_generic_detach(dev); 37734b48722SAlfred Perlstein mtx_unlock(&Giant); 37802ac6454SAndrew Thompson 37902ac6454SAndrew Thompson /* 380d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 38102ac6454SAndrew Thompson */ 382d88688c7SAndrew Thompson usb_free_device(udev, 0); 38302ac6454SAndrew Thompson 38402ac6454SAndrew Thompson USB_BUS_LOCK(bus); 38502ac6454SAndrew Thompson /* clear bdev variable last */ 38602ac6454SAndrew Thompson bus->bdev = NULL; 38702ac6454SAndrew Thompson } 38802ac6454SAndrew Thompson 3892e141748SHans Petter Selasky /*------------------------------------------------------------------------* 3902e141748SHans Petter Selasky * usb_bus_suspend 3912e141748SHans Petter Selasky * 3922e141748SHans Petter Selasky * This function is used to suspend the USB contoller. 3932e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 3942e141748SHans Petter Selasky static void 3952e141748SHans Petter Selasky usb_bus_suspend(struct usb_proc_msg *pm) 3962e141748SHans Petter Selasky { 3972e141748SHans Petter Selasky struct usb_bus *bus; 3982e141748SHans Petter Selasky struct usb_device *udev; 3992e141748SHans Petter Selasky usb_error_t err; 4002e141748SHans Petter Selasky 4012e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4022e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4032e141748SHans Petter Selasky 4042e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 4052e141748SHans Petter Selasky return; 4062e141748SHans Petter Selasky 4076bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 4086bd3e535SHans Petter Selasky 4092e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 4102e141748SHans Petter Selasky 4112e141748SHans Petter Selasky usbd_enum_lock(udev); 4122e141748SHans Petter Selasky 4132e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 4142e141748SHans Petter Selasky if (err) 4152e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 4162e141748SHans Petter Selasky 4172e141748SHans Petter Selasky USB_BUS_LOCK(bus); 4182e141748SHans Petter Selasky bus->hw_power_state = 0; 4192e141748SHans Petter Selasky bus->no_explore = 1; 4202e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 4212e141748SHans Petter Selasky 4222e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 4232e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 4242e141748SHans Petter Selasky 4252e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 4262e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND); 4272e141748SHans Petter Selasky 4282e141748SHans Petter Selasky usbd_enum_unlock(udev); 4296bd3e535SHans Petter Selasky 4306bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 4312e141748SHans Petter Selasky } 4322e141748SHans Petter Selasky 4332e141748SHans Petter Selasky /*------------------------------------------------------------------------* 4342e141748SHans Petter Selasky * usb_bus_resume 4352e141748SHans Petter Selasky * 4362e141748SHans Petter Selasky * This function is used to resume the USB contoller. 4372e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 4382e141748SHans Petter Selasky static void 4392e141748SHans Petter Selasky usb_bus_resume(struct usb_proc_msg *pm) 4402e141748SHans Petter Selasky { 4412e141748SHans Petter Selasky struct usb_bus *bus; 4422e141748SHans Petter Selasky struct usb_device *udev; 4432e141748SHans Petter Selasky usb_error_t err; 4442e141748SHans Petter Selasky 4452e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4462e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4472e141748SHans Petter Selasky 4482e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 4492e141748SHans Petter Selasky return; 4502e141748SHans Petter Selasky 4516bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 4526bd3e535SHans Petter Selasky 4532e141748SHans Petter Selasky usbd_enum_lock(udev); 4542e141748SHans Petter Selasky #if 0 4552e141748SHans Petter Selasky DEVMETHOD(usb_take_controller, NULL); /* dummy */ 4562e141748SHans Petter Selasky #endif 4572e141748SHans Petter Selasky USB_TAKE_CONTROLLER(device_get_parent(bus->bdev)); 4582e141748SHans Petter Selasky 4592e141748SHans Petter Selasky USB_BUS_LOCK(bus); 4602e141748SHans Petter Selasky bus->hw_power_state = 4612e141748SHans Petter Selasky USB_HW_POWER_CONTROL | 4622e141748SHans Petter Selasky USB_HW_POWER_BULK | 4632e141748SHans Petter Selasky USB_HW_POWER_INTERRUPT | 4642e141748SHans Petter Selasky USB_HW_POWER_ISOC | 4652e141748SHans Petter Selasky USB_HW_POWER_NON_ROOT_HUB; 4662e141748SHans Petter Selasky bus->no_explore = 0; 4672e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 4682e141748SHans Petter Selasky 4692e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 4702e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME); 4712e141748SHans Petter Selasky 4722e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 4732e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 4742e141748SHans Petter Selasky 4752e141748SHans Petter Selasky err = usbd_set_config_index(udev, 0); 4762e141748SHans Petter Selasky if (err) 4772e141748SHans Petter Selasky device_printf(bus->bdev, "Could not configure root HUB\n"); 4782e141748SHans Petter Selasky 4792e141748SHans Petter Selasky usbd_enum_unlock(udev); 4806bd3e535SHans Petter Selasky 4816bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 4822e141748SHans Petter Selasky } 4832e141748SHans Petter Selasky 4842e141748SHans Petter Selasky /*------------------------------------------------------------------------* 4852e141748SHans Petter Selasky * usb_bus_shutdown 4862e141748SHans Petter Selasky * 4872e141748SHans Petter Selasky * This function is used to shutdown the USB contoller. 4882e141748SHans Petter Selasky *------------------------------------------------------------------------*/ 4892e141748SHans Petter Selasky static void 4902e141748SHans Petter Selasky usb_bus_shutdown(struct usb_proc_msg *pm) 4912e141748SHans Petter Selasky { 4922e141748SHans Petter Selasky struct usb_bus *bus; 4932e141748SHans Petter Selasky struct usb_device *udev; 4942e141748SHans Petter Selasky usb_error_t err; 4952e141748SHans Petter Selasky 4962e141748SHans Petter Selasky bus = ((struct usb_bus_msg *)pm)->bus; 4972e141748SHans Petter Selasky udev = bus->devices[USB_ROOT_HUB_ADDR]; 4982e141748SHans Petter Selasky 4992e141748SHans Petter Selasky if (udev == NULL || bus->bdev == NULL) 5002e141748SHans Petter Selasky return; 5012e141748SHans Petter Selasky 5026bd3e535SHans Petter Selasky USB_BUS_UNLOCK(bus); 5036bd3e535SHans Petter Selasky 5042e141748SHans Petter Selasky bus_generic_shutdown(bus->bdev); 5052e141748SHans Petter Selasky 5062e141748SHans Petter Selasky usbd_enum_lock(udev); 5072e141748SHans Petter Selasky 5082e141748SHans Petter Selasky err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX); 5092e141748SHans Petter Selasky if (err) 5102e141748SHans Petter Selasky device_printf(bus->bdev, "Could not unconfigure root HUB\n"); 5112e141748SHans Petter Selasky 5122e141748SHans Petter Selasky USB_BUS_LOCK(bus); 5132e141748SHans Petter Selasky bus->hw_power_state = 0; 5142e141748SHans Petter Selasky bus->no_explore = 1; 5152e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 5162e141748SHans Petter Selasky 5172e141748SHans Petter Selasky if (bus->methods->set_hw_power != NULL) 5182e141748SHans Petter Selasky (bus->methods->set_hw_power) (bus); 5192e141748SHans Petter Selasky 5202e141748SHans Petter Selasky if (bus->methods->set_hw_power_sleep != NULL) 5212e141748SHans Petter Selasky (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN); 5222e141748SHans Petter Selasky 5232e141748SHans Petter Selasky usbd_enum_unlock(udev); 5246bd3e535SHans Petter Selasky 5256bd3e535SHans Petter Selasky USB_BUS_LOCK(bus); 5262e141748SHans Petter Selasky } 5272e141748SHans Petter Selasky 52802ac6454SAndrew Thompson static void 529a593f6b8SAndrew Thompson usb_power_wdog(void *arg) 53002ac6454SAndrew Thompson { 531760bc48eSAndrew Thompson struct usb_bus *bus = arg; 53202ac6454SAndrew Thompson 53302ac6454SAndrew Thompson USB_BUS_LOCK_ASSERT(bus, MA_OWNED); 53402ac6454SAndrew Thompson 535a593f6b8SAndrew Thompson usb_callout_reset(&bus->power_wdog, 536a593f6b8SAndrew Thompson 4 * hz, usb_power_wdog, arg); 53702ac6454SAndrew Thompson 538f0c078e6SAndrew Thompson #ifdef DDB 53934b48722SAlfred Perlstein /* 54034b48722SAlfred Perlstein * The following line of code is only here to recover from 54134b48722SAlfred Perlstein * DDB: 54234b48722SAlfred Perlstein */ 54334b48722SAlfred Perlstein usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */ 544f0c078e6SAndrew Thompson #endif 54534b48722SAlfred Perlstein 546e727a16cSAndrew Thompson #if USB_HAVE_POWERD 54702ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 54802ac6454SAndrew Thompson 549a593f6b8SAndrew Thompson usb_bus_power_update(bus); 55002ac6454SAndrew Thompson 551684e3f22SAndrew Thompson USB_BUS_LOCK(bus); 552e727a16cSAndrew Thompson #endif 55302ac6454SAndrew Thompson } 55402ac6454SAndrew Thompson 55502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 556a593f6b8SAndrew Thompson * usb_bus_attach 55702ac6454SAndrew Thompson * 55802ac6454SAndrew Thompson * This function attaches USB in context of the explore thread. 55902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 56002ac6454SAndrew Thompson static void 561a593f6b8SAndrew Thompson usb_bus_attach(struct usb_proc_msg *pm) 56202ac6454SAndrew Thompson { 563760bc48eSAndrew Thompson struct usb_bus *bus; 564760bc48eSAndrew Thompson struct usb_device *child; 56502ac6454SAndrew Thompson device_t dev; 566e0a69b51SAndrew Thompson usb_error_t err; 5678d2dd5ddSAndrew Thompson enum usb_dev_speed speed; 56802ac6454SAndrew Thompson 569760bc48eSAndrew Thompson bus = ((struct usb_bus_msg *)pm)->bus; 57002ac6454SAndrew Thompson dev = bus->bdev; 57102ac6454SAndrew Thompson 57202ac6454SAndrew Thompson DPRINTF("\n"); 57302ac6454SAndrew Thompson 57402ac6454SAndrew Thompson switch (bus->usbrev) { 57502ac6454SAndrew Thompson case USB_REV_1_0: 57602ac6454SAndrew Thompson speed = USB_SPEED_FULL; 57702ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n"); 57802ac6454SAndrew Thompson break; 57902ac6454SAndrew Thompson 58002ac6454SAndrew Thompson case USB_REV_1_1: 58102ac6454SAndrew Thompson speed = USB_SPEED_FULL; 58202ac6454SAndrew Thompson device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n"); 58302ac6454SAndrew Thompson break; 58402ac6454SAndrew Thompson 58502ac6454SAndrew Thompson case USB_REV_2_0: 58602ac6454SAndrew Thompson speed = USB_SPEED_HIGH; 58702ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n"); 58802ac6454SAndrew Thompson break; 58902ac6454SAndrew Thompson 59002ac6454SAndrew Thompson case USB_REV_2_5: 59102ac6454SAndrew Thompson speed = USB_SPEED_VARIABLE; 59202ac6454SAndrew Thompson device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n"); 59302ac6454SAndrew Thompson break; 59402ac6454SAndrew Thompson 595963169b4SHans Petter Selasky case USB_REV_3_0: 596963169b4SHans Petter Selasky speed = USB_SPEED_SUPER; 597ccac019aSHans Petter Selasky device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n"); 598963169b4SHans Petter Selasky break; 599963169b4SHans Petter Selasky 60002ac6454SAndrew Thompson default: 601767cb2e2SAndrew Thompson device_printf(bus->bdev, "Unsupported USB revision\n"); 6023df007ceSHans Petter Selasky usb_root_mount_rel(bus); 60302ac6454SAndrew Thompson return; 60402ac6454SAndrew Thompson } 60502ac6454SAndrew Thompson 6064eae601eSAndrew Thompson /* default power_mask value */ 6074eae601eSAndrew Thompson bus->hw_power_state = 6084eae601eSAndrew Thompson USB_HW_POWER_CONTROL | 6094eae601eSAndrew Thompson USB_HW_POWER_BULK | 6104eae601eSAndrew Thompson USB_HW_POWER_INTERRUPT | 6114eae601eSAndrew Thompson USB_HW_POWER_ISOC | 6124eae601eSAndrew Thompson USB_HW_POWER_NON_ROOT_HUB; 6134eae601eSAndrew Thompson 6142e141748SHans Petter Selasky USB_BUS_UNLOCK(bus); 6152e141748SHans Petter Selasky 6164eae601eSAndrew Thompson /* make sure power is set at least once */ 6174eae601eSAndrew Thompson 6184eae601eSAndrew Thompson if (bus->methods->set_hw_power != NULL) { 6194eae601eSAndrew Thompson (bus->methods->set_hw_power) (bus); 6204eae601eSAndrew Thompson } 6214eae601eSAndrew Thompson 6222e141748SHans Petter Selasky /* allocate the Root USB device */ 62302ac6454SAndrew Thompson 624a593f6b8SAndrew Thompson child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1, 62502ac6454SAndrew Thompson speed, USB_MODE_HOST); 62602ac6454SAndrew Thompson if (child) { 627a593f6b8SAndrew Thompson err = usb_probe_and_attach(child, 62802ac6454SAndrew Thompson USB_IFACE_INDEX_ANY); 62902ac6454SAndrew Thompson if (!err) { 6301be5bf51SAndrew Thompson if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) || 6311be5bf51SAndrew Thompson (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) { 63202ac6454SAndrew Thompson err = USB_ERR_NO_ROOT_HUB; 63302ac6454SAndrew Thompson } 63402ac6454SAndrew Thompson } 63502ac6454SAndrew Thompson } else { 63602ac6454SAndrew Thompson err = USB_ERR_NOMEM; 63702ac6454SAndrew Thompson } 63802ac6454SAndrew Thompson 63902ac6454SAndrew Thompson USB_BUS_LOCK(bus); 64002ac6454SAndrew Thompson 64102ac6454SAndrew Thompson if (err) { 64202ac6454SAndrew Thompson device_printf(bus->bdev, "Root HUB problem, error=%s\n", 643a593f6b8SAndrew Thompson usbd_errstr(err)); 6443df007ceSHans Petter Selasky usb_root_mount_rel(bus); 64502ac6454SAndrew Thompson } 64602ac6454SAndrew Thompson 64702ac6454SAndrew Thompson /* set softc - we are ready */ 64802ac6454SAndrew Thompson device_set_softc(dev, bus); 64902ac6454SAndrew Thompson 650684e3f22SAndrew Thompson /* start watchdog */ 651a593f6b8SAndrew Thompson usb_power_wdog(bus); 65202ac6454SAndrew Thompson } 65302ac6454SAndrew Thompson 65402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 655a593f6b8SAndrew Thompson * usb_attach_sub 65602ac6454SAndrew Thompson * 65734b48722SAlfred Perlstein * This function creates a thread which runs the USB attach code. 65802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 65902ac6454SAndrew Thompson static void 660a593f6b8SAndrew Thompson usb_attach_sub(device_t dev, struct usb_bus *bus) 66102ac6454SAndrew Thompson { 66202ac6454SAndrew Thompson const char *pname = device_get_nameunit(dev); 66302ac6454SAndrew Thompson 66434b48722SAlfred Perlstein mtx_lock(&Giant); 66534b48722SAlfred Perlstein if (usb_devclass_ptr == NULL) 66634b48722SAlfred Perlstein usb_devclass_ptr = devclass_find("usbus"); 66734b48722SAlfred Perlstein mtx_unlock(&Giant); 66834b48722SAlfred Perlstein 6698be09334SHans Petter Selasky #if USB_HAVE_PF 670fe1c24e3SWeongyo Jeong usbpf_attach(bus); 6718be09334SHans Petter Selasky #endif 67202ac6454SAndrew Thompson /* Initialise USB process messages */ 673a593f6b8SAndrew Thompson bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore; 67402ac6454SAndrew Thompson bus->explore_msg[0].bus = bus; 675a593f6b8SAndrew Thompson bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore; 67602ac6454SAndrew Thompson bus->explore_msg[1].bus = bus; 67702ac6454SAndrew Thompson 678a593f6b8SAndrew Thompson bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach; 67902ac6454SAndrew Thompson bus->detach_msg[0].bus = bus; 680a593f6b8SAndrew Thompson bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach; 68102ac6454SAndrew Thompson bus->detach_msg[1].bus = bus; 68202ac6454SAndrew Thompson 683a593f6b8SAndrew Thompson bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach; 68402ac6454SAndrew Thompson bus->attach_msg[0].bus = bus; 685a593f6b8SAndrew Thompson bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach; 68602ac6454SAndrew Thompson bus->attach_msg[1].bus = bus; 68702ac6454SAndrew Thompson 6882e141748SHans Petter Selasky bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend; 6892e141748SHans Petter Selasky bus->suspend_msg[0].bus = bus; 6902e141748SHans Petter Selasky bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend; 6912e141748SHans Petter Selasky bus->suspend_msg[1].bus = bus; 6922e141748SHans Petter Selasky 6932e141748SHans Petter Selasky bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume; 6942e141748SHans Petter Selasky bus->resume_msg[0].bus = bus; 6952e141748SHans Petter Selasky bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume; 6962e141748SHans Petter Selasky bus->resume_msg[1].bus = bus; 6972e141748SHans Petter Selasky 6982e141748SHans Petter Selasky bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown; 6992e141748SHans Petter Selasky bus->shutdown_msg[0].bus = bus; 7002e141748SHans Petter Selasky bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown; 7012e141748SHans Petter Selasky bus->shutdown_msg[1].bus = bus; 7022e141748SHans Petter Selasky 70339307315SAndrew Thompson /* Create USB explore and callback processes */ 70402ac6454SAndrew Thompson 705a593f6b8SAndrew Thompson if (usb_proc_create(&bus->giant_callback_proc, 70602ac6454SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_MED)) { 70788334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB Giant " 70802ac6454SAndrew Thompson "callback process failed.\n"); 709a593f6b8SAndrew Thompson } else if (usb_proc_create(&bus->non_giant_callback_proc, 71002ac6454SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_HIGH)) { 71188334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB non-Giant " 71202ac6454SAndrew Thompson "callback process failed.\n"); 713a593f6b8SAndrew Thompson } else if (usb_proc_create(&bus->explore_proc, 71402ac6454SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_MED)) { 71588334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB explore " 71602ac6454SAndrew Thompson "process failed.\n"); 717a593f6b8SAndrew Thompson } else if (usb_proc_create(&bus->control_xfer_proc, 718672c9965SAndrew Thompson &bus->bus_mtx, pname, USB_PRI_MED)) { 71988334428SHans Petter Selasky device_printf(dev, "WARNING: Creation of USB control transfer " 720672c9965SAndrew Thompson "process failed.\n"); 72102ac6454SAndrew Thompson } else { 72202ac6454SAndrew Thompson /* Get final attach going */ 72302ac6454SAndrew Thompson USB_BUS_LOCK(bus); 7242e141748SHans Petter Selasky usb_proc_msignal(&bus->explore_proc, 7252e141748SHans Petter Selasky &bus->attach_msg[0], &bus->attach_msg[1]); 72602ac6454SAndrew Thompson USB_BUS_UNLOCK(bus); 72734b48722SAlfred Perlstein 72834b48722SAlfred Perlstein /* Do initial explore */ 72934b48722SAlfred Perlstein usb_needs_explore(bus, 1); 73002ac6454SAndrew Thompson } 73102ac6454SAndrew Thompson } 73202ac6454SAndrew Thompson 733a593f6b8SAndrew Thompson SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL); 73402ac6454SAndrew Thompson 73502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 736a593f6b8SAndrew Thompson * usb_bus_mem_flush_all_cb 73702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 738bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 73902ac6454SAndrew Thompson static void 740a593f6b8SAndrew Thompson usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 741f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 74202ac6454SAndrew Thompson { 743a593f6b8SAndrew Thompson usb_pc_cpu_flush(pc); 74402ac6454SAndrew Thompson } 745bdc081c6SAndrew Thompson #endif 74602ac6454SAndrew Thompson 74702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 748a593f6b8SAndrew Thompson * usb_bus_mem_flush_all - factored out code 74902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 750bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 75102ac6454SAndrew Thompson void 752a593f6b8SAndrew Thompson usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 75302ac6454SAndrew Thompson { 75402ac6454SAndrew Thompson if (cb) { 755a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_flush_all_cb); 75602ac6454SAndrew Thompson } 75702ac6454SAndrew Thompson } 758bdc081c6SAndrew Thompson #endif 75902ac6454SAndrew Thompson 76002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 761a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all_cb 76202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 763bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 76402ac6454SAndrew Thompson static void 765a593f6b8SAndrew Thompson usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 766f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 76702ac6454SAndrew Thompson { 76802ac6454SAndrew Thompson /* need to initialize the page cache */ 76902ac6454SAndrew Thompson pc->tag_parent = bus->dma_parent_tag; 77002ac6454SAndrew Thompson 771a593f6b8SAndrew Thompson if (usb_pc_alloc_mem(pc, pg, size, align)) { 77202ac6454SAndrew Thompson bus->alloc_failed = 1; 77302ac6454SAndrew Thompson } 77402ac6454SAndrew Thompson } 775bdc081c6SAndrew Thompson #endif 77602ac6454SAndrew Thompson 77702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 778a593f6b8SAndrew Thompson * usb_bus_mem_alloc_all - factored out code 77902ac6454SAndrew Thompson * 78002ac6454SAndrew Thompson * Returns: 78102ac6454SAndrew Thompson * 0: Success 78202ac6454SAndrew Thompson * Else: Failure 78302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 78402ac6454SAndrew Thompson uint8_t 785a593f6b8SAndrew Thompson usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, 786e0a69b51SAndrew Thompson usb_bus_mem_cb_t *cb) 78702ac6454SAndrew Thompson { 78802ac6454SAndrew Thompson bus->alloc_failed = 0; 78902ac6454SAndrew Thompson 79002ac6454SAndrew Thompson mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent), 79102ac6454SAndrew Thompson NULL, MTX_DEF | MTX_RECURSE); 79202ac6454SAndrew Thompson 793a593f6b8SAndrew Thompson usb_callout_init_mtx(&bus->power_wdog, 794684e3f22SAndrew Thompson &bus->bus_mtx, 0); 79502ac6454SAndrew Thompson 79602ac6454SAndrew Thompson TAILQ_INIT(&bus->intr_q.head); 79702ac6454SAndrew Thompson 798bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 799a593f6b8SAndrew Thompson usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags, 800bdc081c6SAndrew Thompson dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX); 801bdc081c6SAndrew Thompson #endif 80202ac6454SAndrew Thompson if ((bus->devices_max > USB_MAX_DEVICES) || 80302ac6454SAndrew Thompson (bus->devices_max < USB_MIN_DEVICES) || 80402ac6454SAndrew Thompson (bus->devices == NULL)) { 80502ac6454SAndrew Thompson DPRINTFN(0, "Devices field has not been " 806767cb2e2SAndrew Thompson "initialised properly\n"); 80702ac6454SAndrew Thompson bus->alloc_failed = 1; /* failure */ 80802ac6454SAndrew Thompson } 809bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 81002ac6454SAndrew Thompson if (cb) { 811a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_alloc_all_cb); 81202ac6454SAndrew Thompson } 813bdc081c6SAndrew Thompson #endif 81402ac6454SAndrew Thompson if (bus->alloc_failed) { 815a593f6b8SAndrew Thompson usb_bus_mem_free_all(bus, cb); 81602ac6454SAndrew Thompson } 81702ac6454SAndrew Thompson return (bus->alloc_failed); 81802ac6454SAndrew Thompson } 81902ac6454SAndrew Thompson 82002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 821a593f6b8SAndrew Thompson * usb_bus_mem_free_all_cb 82202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 823bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 82402ac6454SAndrew Thompson static void 825a593f6b8SAndrew Thompson usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc, 826f9cb546cSAndrew Thompson struct usb_page *pg, usb_size_t size, usb_size_t align) 82702ac6454SAndrew Thompson { 828a593f6b8SAndrew Thompson usb_pc_free_mem(pc); 82902ac6454SAndrew Thompson } 830bdc081c6SAndrew Thompson #endif 83102ac6454SAndrew Thompson 83202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 833a593f6b8SAndrew Thompson * usb_bus_mem_free_all - factored out code 83402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 83502ac6454SAndrew Thompson void 836a593f6b8SAndrew Thompson usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb) 83702ac6454SAndrew Thompson { 838bdc081c6SAndrew Thompson #if USB_HAVE_BUSDMA 83902ac6454SAndrew Thompson if (cb) { 840a593f6b8SAndrew Thompson cb(bus, &usb_bus_mem_free_all_cb); 84102ac6454SAndrew Thompson } 842a593f6b8SAndrew Thompson usb_dma_tag_unsetup(bus->dma_parent_tag); 843bdc081c6SAndrew Thompson #endif 84402ac6454SAndrew Thompson 84502ac6454SAndrew Thompson mtx_destroy(&bus->bus_mtx); 84602ac6454SAndrew Thompson } 847