3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
32 #include <sys/stdint.h>
33 #include <sys/stddef.h>
34 #include <sys/param.h>
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
51 #include <dev/usb/usb.h>
52 #include <dev/usb/usbdi.h>
54 #define USB_DEBUG_VAR usb_ctrl_debug
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_debug.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_busdma.h>
60 #include <dev/usb/usb_dynamic.h>
61 #include <dev/usb/usb_device.h>
62 #include <dev/usb/usb_dev.h>
63 #include <dev/usb/usb_hub.h>
65 #include <dev/usb/usb_controller.h>
66 #include <dev/usb/usb_bus.h>
67 #include <dev/usb/usb_pf.h>
69 #endif /* USB_GLOBAL_INCLUDE_FILE */
71 /* function prototypes */
73 static device_probe_t usb_probe;
74 static device_attach_t usb_attach;
75 static device_detach_t usb_detach;
76 static device_suspend_t usb_suspend;
77 static device_resume_t usb_resume;
78 static device_shutdown_t usb_shutdown;
80 static void usb_attach_sub(device_t, struct usb_bus *);
82 /* static variables */
85 static int usb_ctrl_debug = 0;
87 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
88 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
92 #if USB_HAVE_ROOT_MOUNT_HOLD
93 static int usb_no_boot_wait = 0;
94 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
95 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RD|CTLFLAG_TUN, &usb_no_boot_wait, 0,
96 "No USB device enumerate waiting at boot.");
99 static int usb_no_suspend_wait = 0;
100 TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait);
101 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN,
102 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
104 static int usb_no_shutdown_wait = 0;
105 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
106 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN,
107 &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
109 static devclass_t usb_devclass;
111 static device_method_t usb_methods[] = {
112 DEVMETHOD(device_probe, usb_probe),
113 DEVMETHOD(device_attach, usb_attach),
114 DEVMETHOD(device_detach, usb_detach),
115 DEVMETHOD(device_suspend, usb_suspend),
116 DEVMETHOD(device_resume, usb_resume),
117 DEVMETHOD(device_shutdown, usb_shutdown),
122 static driver_t usb_driver = {
124 .methods = usb_methods,
128 /* Host Only Drivers */
129 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
130 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
131 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
132 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
134 /* Device Only Drivers */
135 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
136 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
137 DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
138 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
140 /* Dual Mode Drivers */
141 DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0);
143 /*------------------------------------------------------------------------*
146 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
147 *------------------------------------------------------------------------*/
149 usb_probe(device_t dev)
155 #if USB_HAVE_ROOT_MOUNT_HOLD
157 usb_root_mount_rel(struct usb_bus *bus)
159 if (bus->bus_roothold != NULL) {
160 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
161 root_mount_rel(bus->bus_roothold);
162 bus->bus_roothold = NULL;
167 /*------------------------------------------------------------------------*
169 *------------------------------------------------------------------------*/
171 usb_attach(device_t dev)
173 struct usb_bus *bus = device_get_ivars(dev);
178 device_printf(dev, "USB device has no ivars\n");
182 #if USB_HAVE_ROOT_MOUNT_HOLD
183 if (usb_no_boot_wait == 0) {
184 /* delay vfs_mountroot until the bus is explored */
185 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
188 usb_attach_sub(dev, bus);
190 return (0); /* return success */
193 /*------------------------------------------------------------------------*
195 *------------------------------------------------------------------------*/
197 usb_detach(device_t dev)
199 struct usb_bus *bus = device_get_softc(dev);
204 /* was never setup properly */
207 /* Stop power watchdog */
208 usb_callout_drain(&bus->power_wdog);
210 #if USB_HAVE_ROOT_MOUNT_HOLD
211 /* Let the USB explore process detach all devices. */
212 usb_root_mount_rel(bus);
217 /* Queue detach job */
218 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
219 &bus->detach_msg[0], &bus->detach_msg[1]);
221 /* Wait for detach to complete */
222 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
223 &bus->detach_msg[0], &bus->detach_msg[1]);
226 /* Wait for cleanup to complete */
227 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
228 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
232 #if USB_HAVE_PER_BUS_PROCESS
233 /* Get rid of USB callback processes */
235 usb_proc_free(USB_BUS_GIANT_PROC(bus));
236 usb_proc_free(USB_BUS_NON_GIANT_ISOC_PROC(bus));
237 usb_proc_free(USB_BUS_NON_GIANT_BULK_PROC(bus));
239 /* Get rid of USB explore process */
241 usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
243 /* Get rid of control transfer process */
245 usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
254 /*------------------------------------------------------------------------*
256 *------------------------------------------------------------------------*/
258 usb_suspend(device_t dev)
260 struct usb_bus *bus = device_get_softc(dev);
265 /* was never setup properly */
270 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
271 &bus->suspend_msg[0], &bus->suspend_msg[1]);
272 if (usb_no_suspend_wait == 0) {
273 /* wait for suspend callback to be executed */
274 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
275 &bus->suspend_msg[0], &bus->suspend_msg[1]);
282 /*------------------------------------------------------------------------*
284 *------------------------------------------------------------------------*/
286 usb_resume(device_t dev)
288 struct usb_bus *bus = device_get_softc(dev);
293 /* was never setup properly */
298 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
299 &bus->resume_msg[0], &bus->resume_msg[1]);
305 /*------------------------------------------------------------------------*
306 * usb_bus_reset_async_locked
307 *------------------------------------------------------------------------*/
309 usb_bus_reset_async_locked(struct usb_bus *bus)
311 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
315 if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
316 bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
317 DPRINTF("Reset already pending\n");
321 device_printf(bus->parent, "Resetting controller\n");
323 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
324 &bus->reset_msg[0], &bus->reset_msg[1]);
327 /*------------------------------------------------------------------------*
329 *------------------------------------------------------------------------*/
331 usb_shutdown(device_t dev)
333 struct usb_bus *bus = device_get_softc(dev);
338 /* was never setup properly */
342 DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
345 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
346 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
347 if (usb_no_shutdown_wait == 0) {
348 /* wait for shutdown callback to be executed */
349 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
350 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
354 DPRINTF("%s: Controller shutdown complete\n",
355 device_get_nameunit(bus->bdev));
360 /*------------------------------------------------------------------------*
363 * This function is used to explore the device tree from the root.
364 *------------------------------------------------------------------------*/
366 usb_bus_explore(struct usb_proc_msg *pm)
369 struct usb_device *udev;
371 bus = ((struct usb_bus_msg *)pm)->bus;
372 udev = bus->devices[USB_ROOT_HUB_ADDR];
374 if (bus->no_explore != 0)
379 uhub_explore_handle_re_enumerate(udev);
383 if (udev != NULL && udev->hub != NULL) {
387 bus->driver_added_refcount++;
389 if (bus->driver_added_refcount == 0) {
390 /* avoid zero, hence that is memory default */
391 bus->driver_added_refcount = 1;
396 * The following three lines of code are only here to
399 usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
400 usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
401 usb_proc_rewakeup(USB_BUS_NON_GIANT_ISOC_PROC(bus));
402 usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_PROC(bus));
409 * First update the USB power state!
413 /* Explore the Root USB HUB. */
414 (udev->hub->explore) (udev);
417 #if USB_HAVE_ROOT_MOUNT_HOLD
418 usb_root_mount_rel(bus);
422 /*------------------------------------------------------------------------*
425 * This function is used to detach the device tree from the root.
426 *------------------------------------------------------------------------*/
428 usb_bus_detach(struct usb_proc_msg *pm)
431 struct usb_device *udev;
434 bus = ((struct usb_bus_msg *)pm)->bus;
435 udev = bus->devices[USB_ROOT_HUB_ADDR];
437 /* clear the softc */
438 device_set_softc(dev, NULL);
441 /* detach children first */
443 bus_generic_detach(dev);
447 * Free USB device and all subdevices, if any.
449 usb_free_device(udev, 0);
452 /* clear bdev variable last */
456 /*------------------------------------------------------------------------*
459 * This function is used to suspend the USB controller.
460 *------------------------------------------------------------------------*/
462 usb_bus_suspend(struct usb_proc_msg *pm)
465 struct usb_device *udev;
471 bus = ((struct usb_bus_msg *)pm)->bus;
472 udev = bus->devices[USB_ROOT_HUB_ADDR];
474 if (udev == NULL || bus->bdev == NULL)
480 * We use the shutdown event here because the suspend and
481 * resume events are reserved for the USB port suspend and
482 * resume. The USB system suspend is implemented like full
483 * shutdown and all connected USB devices will be disconnected
484 * subsequently. At resume all USB devices will be
485 * re-connected again.
488 bus_generic_shutdown(bus->bdev);
490 do_unlock = usbd_enum_lock(udev);
492 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
494 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
497 bus->hw_power_state = 0;
501 if (bus->methods->set_hw_power != NULL)
502 (bus->methods->set_hw_power) (bus);
504 if (bus->methods->set_hw_power_sleep != NULL)
505 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
508 usbd_enum_unlock(udev);
513 /*------------------------------------------------------------------------*
516 * This function is used to resume the USB controller.
517 *------------------------------------------------------------------------*/
519 usb_bus_resume(struct usb_proc_msg *pm)
522 struct usb_device *udev;
528 bus = ((struct usb_bus_msg *)pm)->bus;
529 udev = bus->devices[USB_ROOT_HUB_ADDR];
531 if (udev == NULL || bus->bdev == NULL)
536 do_unlock = usbd_enum_lock(udev);
538 DEVMETHOD(usb_take_controller, NULL); /* dummy */
540 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
543 bus->hw_power_state =
544 USB_HW_POWER_CONTROL |
546 USB_HW_POWER_INTERRUPT |
548 USB_HW_POWER_NON_ROOT_HUB;
552 if (bus->methods->set_hw_power_sleep != NULL)
553 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
555 if (bus->methods->set_hw_power != NULL)
556 (bus->methods->set_hw_power) (bus);
558 /* restore USB configuration to index 0 */
559 err = usbd_set_config_index(udev, 0);
561 device_printf(bus->bdev, "Could not configure root HUB\n");
563 /* probe and attach */
564 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
566 device_printf(bus->bdev, "Could not probe and "
567 "attach root HUB\n");
571 usbd_enum_unlock(udev);
576 /*------------------------------------------------------------------------*
579 * This function is used to reset the USB controller.
580 *------------------------------------------------------------------------*/
582 usb_bus_reset(struct usb_proc_msg *pm)
588 bus = ((struct usb_bus_msg *)pm)->bus;
590 if (bus->bdev == NULL || bus->no_explore != 0)
593 /* a suspend and resume will reset the USB controller */
598 /*------------------------------------------------------------------------*
601 * This function is used to shutdown the USB controller.
602 *------------------------------------------------------------------------*/
604 usb_bus_shutdown(struct usb_proc_msg *pm)
607 struct usb_device *udev;
611 bus = ((struct usb_bus_msg *)pm)->bus;
612 udev = bus->devices[USB_ROOT_HUB_ADDR];
614 if (udev == NULL || bus->bdev == NULL)
619 bus_generic_shutdown(bus->bdev);
621 do_unlock = usbd_enum_lock(udev);
623 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
625 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
628 bus->hw_power_state = 0;
632 if (bus->methods->set_hw_power != NULL)
633 (bus->methods->set_hw_power) (bus);
635 if (bus->methods->set_hw_power_sleep != NULL)
636 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
639 usbd_enum_unlock(udev);
644 /*------------------------------------------------------------------------*
647 * This function is used to cleanup leftover USB character devices.
648 *------------------------------------------------------------------------*/
651 usb_bus_cleanup(struct usb_proc_msg *pm)
654 struct usb_fs_privdata *pd;
656 bus = ((struct usb_bus_msg *)pm)->bus;
658 while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
660 LIST_REMOVE(pd, pd_next);
663 usb_destroy_dev_sync(pd);
671 usb_power_wdog(void *arg)
673 struct usb_bus *bus = arg;
675 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
677 usb_callout_reset(&bus->power_wdog,
678 4 * hz, usb_power_wdog, arg);
682 * The following line of code is only here to recover from
685 usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */
691 usb_bus_power_update(bus);
697 /*------------------------------------------------------------------------*
700 * This function attaches USB in context of the explore thread.
701 *------------------------------------------------------------------------*/
703 usb_bus_attach(struct usb_proc_msg *pm)
706 struct usb_device *child;
709 enum usb_dev_speed speed;
711 bus = ((struct usb_bus_msg *)pm)->bus;
716 switch (bus->usbrev) {
718 speed = USB_SPEED_FULL;
719 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
723 speed = USB_SPEED_FULL;
724 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
728 speed = USB_SPEED_HIGH;
729 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
733 speed = USB_SPEED_VARIABLE;
734 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
738 speed = USB_SPEED_SUPER;
739 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
743 device_printf(bus->bdev, "Unsupported USB revision\n");
744 #if USB_HAVE_ROOT_MOUNT_HOLD
745 usb_root_mount_rel(bus);
750 /* default power_mask value */
751 bus->hw_power_state =
752 USB_HW_POWER_CONTROL |
754 USB_HW_POWER_INTERRUPT |
756 USB_HW_POWER_NON_ROOT_HUB;
760 /* make sure power is set at least once */
762 if (bus->methods->set_hw_power != NULL) {
763 (bus->methods->set_hw_power) (bus);
766 /* allocate the Root USB device */
768 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
769 speed, USB_MODE_HOST);
771 err = usb_probe_and_attach(child,
772 USB_IFACE_INDEX_ANY);
774 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
775 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
776 err = USB_ERR_NO_ROOT_HUB;
786 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
788 #if USB_HAVE_ROOT_MOUNT_HOLD
789 usb_root_mount_rel(bus);
793 /* set softc - we are ready */
794 device_set_softc(dev, bus);
800 /*------------------------------------------------------------------------*
803 * This function creates a thread which runs the USB attach code.
804 *------------------------------------------------------------------------*/
806 usb_attach_sub(device_t dev, struct usb_bus *bus)
809 if (usb_devclass_ptr == NULL)
810 usb_devclass_ptr = devclass_find("usbus");
816 /* Initialise USB process messages */
817 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
818 bus->explore_msg[0].bus = bus;
819 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
820 bus->explore_msg[1].bus = bus;
822 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
823 bus->detach_msg[0].bus = bus;
824 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
825 bus->detach_msg[1].bus = bus;
827 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
828 bus->attach_msg[0].bus = bus;
829 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
830 bus->attach_msg[1].bus = bus;
832 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
833 bus->suspend_msg[0].bus = bus;
834 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
835 bus->suspend_msg[1].bus = bus;
837 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
838 bus->resume_msg[0].bus = bus;
839 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
840 bus->resume_msg[1].bus = bus;
842 bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
843 bus->reset_msg[0].bus = bus;
844 bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
845 bus->reset_msg[1].bus = bus;
847 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
848 bus->shutdown_msg[0].bus = bus;
849 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
850 bus->shutdown_msg[1].bus = bus;
853 LIST_INIT(&bus->pd_cleanup_list);
854 bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup;
855 bus->cleanup_msg[0].bus = bus;
856 bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup;
857 bus->cleanup_msg[1].bus = bus;
860 #if USB_HAVE_PER_BUS_PROCESS
861 /* Create USB explore and callback processes */
863 if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
864 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
865 device_printf(dev, "WARNING: Creation of USB Giant "
866 "callback process failed.\n");
867 } else if (usb_proc_create(USB_BUS_NON_GIANT_ISOC_PROC(bus),
868 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGHEST)) {
869 device_printf(dev, "WARNING: Creation of USB non-Giant ISOC "
870 "callback process failed.\n");
871 } else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus),
872 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) {
873 device_printf(dev, "WARNING: Creation of USB non-Giant BULK "
874 "callback process failed.\n");
875 } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
876 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
877 device_printf(dev, "WARNING: Creation of USB explore "
878 "process failed.\n");
879 } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
880 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
881 device_printf(dev, "WARNING: Creation of USB control transfer "
882 "process failed.\n");
886 /* Get final attach going */
888 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
889 &bus->attach_msg[0], &bus->attach_msg[1]);
892 /* Do initial explore */
893 usb_needs_explore(bus, 1);
896 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
898 /*------------------------------------------------------------------------*
899 * usb_bus_mem_flush_all_cb
900 *------------------------------------------------------------------------*/
903 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
904 struct usb_page *pg, usb_size_t size, usb_size_t align)
906 usb_pc_cpu_flush(pc);
910 /*------------------------------------------------------------------------*
911 * usb_bus_mem_flush_all - factored out code
912 *------------------------------------------------------------------------*/
915 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
918 cb(bus, &usb_bus_mem_flush_all_cb);
923 /*------------------------------------------------------------------------*
924 * usb_bus_mem_alloc_all_cb
925 *------------------------------------------------------------------------*/
928 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
929 struct usb_page *pg, usb_size_t size, usb_size_t align)
931 /* need to initialize the page cache */
932 pc->tag_parent = bus->dma_parent_tag;
934 if (usb_pc_alloc_mem(pc, pg, size, align)) {
935 bus->alloc_failed = 1;
940 /*------------------------------------------------------------------------*
941 * usb_bus_mem_alloc_all - factored out code
946 *------------------------------------------------------------------------*/
948 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
949 usb_bus_mem_cb_t *cb)
951 bus->alloc_failed = 0;
953 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
954 "usb_def_mtx", MTX_DEF | MTX_RECURSE);
956 mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
957 "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
959 usb_callout_init_mtx(&bus->power_wdog,
962 TAILQ_INIT(&bus->intr_q.head);
965 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
966 dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
968 if ((bus->devices_max > USB_MAX_DEVICES) ||
969 (bus->devices_max < USB_MIN_DEVICES) ||
970 (bus->devices == NULL)) {
971 DPRINTFN(0, "Devices field has not been "
972 "initialised properly\n");
973 bus->alloc_failed = 1; /* failure */
977 cb(bus, &usb_bus_mem_alloc_all_cb);
980 if (bus->alloc_failed) {
981 usb_bus_mem_free_all(bus, cb);
983 return (bus->alloc_failed);
986 /*------------------------------------------------------------------------*
987 * usb_bus_mem_free_all_cb
988 *------------------------------------------------------------------------*/
991 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
992 struct usb_page *pg, usb_size_t size, usb_size_t align)
998 /*------------------------------------------------------------------------*
999 * usb_bus_mem_free_all - factored out code
1000 *------------------------------------------------------------------------*/
1002 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
1006 cb(bus, &usb_bus_mem_free_all_cb);
1008 usb_dma_tag_unsetup(bus->dma_parent_tag);
1011 mtx_destroy(&bus->bus_mtx);
1012 mtx_destroy(&bus->bus_spin_lock);
1015 /* convenience wrappers */
1017 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
1019 usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
1023 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
1025 return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
1029 usb_proc_explore_lock(struct usb_device *udev)
1031 USB_BUS_LOCK(udev->bus);
1035 usb_proc_explore_unlock(struct usb_device *udev)
1037 USB_BUS_UNLOCK(udev->bus);