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_RWTUN, &usb_ctrl_debug, 0,
92 #if USB_HAVE_ROOT_MOUNT_HOLD
93 static int usb_no_boot_wait = 0;
94 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
95 "No USB device enumerate waiting at boot.");
98 static int usb_no_suspend_wait = 0;
99 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RWTUN,
100 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
102 static int usb_no_shutdown_wait = 0;
103 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RWTUN,
104 &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
106 static devclass_t usb_devclass;
108 static device_method_t usb_methods[] = {
109 DEVMETHOD(device_probe, usb_probe),
110 DEVMETHOD(device_attach, usb_attach),
111 DEVMETHOD(device_detach, usb_detach),
112 DEVMETHOD(device_suspend, usb_suspend),
113 DEVMETHOD(device_resume, usb_resume),
114 DEVMETHOD(device_shutdown, usb_shutdown),
119 static driver_t usb_driver = {
121 .methods = usb_methods,
125 /* Host Only Drivers */
126 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
127 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
128 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
129 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
131 /* Device Only Drivers */
132 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
133 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
134 DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
135 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
137 /* Dual Mode Drivers */
138 DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0);
139 DRIVER_MODULE(usbus, saf1761otg, usb_driver, usb_devclass, 0, 0);
141 /*------------------------------------------------------------------------*
144 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
145 *------------------------------------------------------------------------*/
147 usb_probe(device_t dev)
153 #if USB_HAVE_ROOT_MOUNT_HOLD
155 usb_root_mount_rel(struct usb_bus *bus)
157 if (bus->bus_roothold != NULL) {
158 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
159 root_mount_rel(bus->bus_roothold);
160 bus->bus_roothold = NULL;
165 /*------------------------------------------------------------------------*
167 *------------------------------------------------------------------------*/
169 usb_attach(device_t dev)
171 struct usb_bus *bus = device_get_ivars(dev);
176 device_printf(dev, "USB device has no ivars\n");
180 #if USB_HAVE_ROOT_MOUNT_HOLD
181 if (usb_no_boot_wait == 0) {
182 /* delay vfs_mountroot until the bus is explored */
183 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
186 usb_attach_sub(dev, bus);
188 return (0); /* return success */
191 /*------------------------------------------------------------------------*
193 *------------------------------------------------------------------------*/
195 usb_detach(device_t dev)
197 struct usb_bus *bus = device_get_softc(dev);
202 /* was never setup properly */
205 /* Stop power watchdog */
206 usb_callout_drain(&bus->power_wdog);
208 #if USB_HAVE_ROOT_MOUNT_HOLD
209 /* Let the USB explore process detach all devices. */
210 usb_root_mount_rel(bus);
215 /* Queue detach job */
216 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
217 &bus->detach_msg[0], &bus->detach_msg[1]);
219 /* Wait for detach to complete */
220 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
221 &bus->detach_msg[0], &bus->detach_msg[1]);
224 /* Wait for cleanup to complete */
225 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
226 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
230 #if USB_HAVE_PER_BUS_PROCESS
231 /* Get rid of USB callback processes */
233 usb_proc_free(USB_BUS_GIANT_PROC(bus));
234 usb_proc_free(USB_BUS_NON_GIANT_ISOC_PROC(bus));
235 usb_proc_free(USB_BUS_NON_GIANT_BULK_PROC(bus));
237 /* Get rid of USB explore process */
239 usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
241 /* Get rid of control transfer process */
243 usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
252 /*------------------------------------------------------------------------*
254 *------------------------------------------------------------------------*/
256 usb_suspend(device_t dev)
258 struct usb_bus *bus = device_get_softc(dev);
263 /* was never setup properly */
268 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
269 &bus->suspend_msg[0], &bus->suspend_msg[1]);
270 if (usb_no_suspend_wait == 0) {
271 /* wait for suspend callback to be executed */
272 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
273 &bus->suspend_msg[0], &bus->suspend_msg[1]);
280 /*------------------------------------------------------------------------*
282 *------------------------------------------------------------------------*/
284 usb_resume(device_t dev)
286 struct usb_bus *bus = device_get_softc(dev);
291 /* was never setup properly */
296 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
297 &bus->resume_msg[0], &bus->resume_msg[1]);
303 /*------------------------------------------------------------------------*
304 * usb_bus_reset_async_locked
305 *------------------------------------------------------------------------*/
307 usb_bus_reset_async_locked(struct usb_bus *bus)
309 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
313 if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
314 bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
315 DPRINTF("Reset already pending\n");
319 device_printf(bus->parent, "Resetting controller\n");
321 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
322 &bus->reset_msg[0], &bus->reset_msg[1]);
325 /*------------------------------------------------------------------------*
327 *------------------------------------------------------------------------*/
329 usb_shutdown(device_t dev)
331 struct usb_bus *bus = device_get_softc(dev);
336 /* was never setup properly */
340 DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
343 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
344 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
345 if (usb_no_shutdown_wait == 0) {
346 /* wait for shutdown callback to be executed */
347 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
348 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
352 DPRINTF("%s: Controller shutdown complete\n",
353 device_get_nameunit(bus->bdev));
358 /*------------------------------------------------------------------------*
361 * This function is used to explore the device tree from the root.
362 *------------------------------------------------------------------------*/
364 usb_bus_explore(struct usb_proc_msg *pm)
367 struct usb_device *udev;
369 bus = ((struct usb_bus_msg *)pm)->bus;
370 udev = bus->devices[USB_ROOT_HUB_ADDR];
372 if (bus->no_explore != 0)
377 uhub_explore_handle_re_enumerate(udev);
381 if (udev != NULL && udev->hub != NULL) {
385 bus->driver_added_refcount++;
387 if (bus->driver_added_refcount == 0) {
388 /* avoid zero, hence that is memory default */
389 bus->driver_added_refcount = 1;
394 * The following three lines of code are only here to
397 usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
398 usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
399 usb_proc_rewakeup(USB_BUS_NON_GIANT_ISOC_PROC(bus));
400 usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_PROC(bus));
407 * First update the USB power state!
411 /* Explore the Root USB HUB. */
412 (udev->hub->explore) (udev);
415 #if USB_HAVE_ROOT_MOUNT_HOLD
416 usb_root_mount_rel(bus);
420 /*------------------------------------------------------------------------*
423 * This function is used to detach the device tree from the root.
424 *------------------------------------------------------------------------*/
426 usb_bus_detach(struct usb_proc_msg *pm)
429 struct usb_device *udev;
432 bus = ((struct usb_bus_msg *)pm)->bus;
433 udev = bus->devices[USB_ROOT_HUB_ADDR];
435 /* clear the softc */
436 device_set_softc(dev, NULL);
439 /* detach children first */
441 bus_generic_detach(dev);
445 * Free USB device and all subdevices, if any.
447 usb_free_device(udev, 0);
450 /* clear bdev variable last */
454 /*------------------------------------------------------------------------*
457 * This function is used to suspend the USB controller.
458 *------------------------------------------------------------------------*/
460 usb_bus_suspend(struct usb_proc_msg *pm)
463 struct usb_device *udev;
469 bus = ((struct usb_bus_msg *)pm)->bus;
470 udev = bus->devices[USB_ROOT_HUB_ADDR];
472 if (udev == NULL || bus->bdev == NULL)
478 * We use the shutdown event here because the suspend and
479 * resume events are reserved for the USB port suspend and
480 * resume. The USB system suspend is implemented like full
481 * shutdown and all connected USB devices will be disconnected
482 * subsequently. At resume all USB devices will be
483 * re-connected again.
486 bus_generic_shutdown(bus->bdev);
488 do_unlock = usbd_enum_lock(udev);
490 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
492 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
495 bus->hw_power_state = 0;
499 if (bus->methods->set_hw_power != NULL)
500 (bus->methods->set_hw_power) (bus);
502 if (bus->methods->set_hw_power_sleep != NULL)
503 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
506 usbd_enum_unlock(udev);
511 /*------------------------------------------------------------------------*
514 * This function is used to resume the USB controller.
515 *------------------------------------------------------------------------*/
517 usb_bus_resume(struct usb_proc_msg *pm)
520 struct usb_device *udev;
526 bus = ((struct usb_bus_msg *)pm)->bus;
527 udev = bus->devices[USB_ROOT_HUB_ADDR];
529 if (udev == NULL || bus->bdev == NULL)
534 do_unlock = usbd_enum_lock(udev);
536 DEVMETHOD(usb_take_controller, NULL); /* dummy */
538 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
541 bus->hw_power_state =
542 USB_HW_POWER_CONTROL |
544 USB_HW_POWER_INTERRUPT |
546 USB_HW_POWER_NON_ROOT_HUB;
550 if (bus->methods->set_hw_power_sleep != NULL)
551 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
553 if (bus->methods->set_hw_power != NULL)
554 (bus->methods->set_hw_power) (bus);
556 /* restore USB configuration to index 0 */
557 err = usbd_set_config_index(udev, 0);
559 device_printf(bus->bdev, "Could not configure root HUB\n");
561 /* probe and attach */
562 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
564 device_printf(bus->bdev, "Could not probe and "
565 "attach root HUB\n");
569 usbd_enum_unlock(udev);
574 /*------------------------------------------------------------------------*
577 * This function is used to reset the USB controller.
578 *------------------------------------------------------------------------*/
580 usb_bus_reset(struct usb_proc_msg *pm)
586 bus = ((struct usb_bus_msg *)pm)->bus;
588 if (bus->bdev == NULL || bus->no_explore != 0)
591 /* a suspend and resume will reset the USB controller */
596 /*------------------------------------------------------------------------*
599 * This function is used to shutdown the USB controller.
600 *------------------------------------------------------------------------*/
602 usb_bus_shutdown(struct usb_proc_msg *pm)
605 struct usb_device *udev;
609 bus = ((struct usb_bus_msg *)pm)->bus;
610 udev = bus->devices[USB_ROOT_HUB_ADDR];
612 if (udev == NULL || bus->bdev == NULL)
617 bus_generic_shutdown(bus->bdev);
619 do_unlock = usbd_enum_lock(udev);
621 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
623 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
626 bus->hw_power_state = 0;
630 if (bus->methods->set_hw_power != NULL)
631 (bus->methods->set_hw_power) (bus);
633 if (bus->methods->set_hw_power_sleep != NULL)
634 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
637 usbd_enum_unlock(udev);
642 /*------------------------------------------------------------------------*
645 * This function is used to cleanup leftover USB character devices.
646 *------------------------------------------------------------------------*/
649 usb_bus_cleanup(struct usb_proc_msg *pm)
652 struct usb_fs_privdata *pd;
654 bus = ((struct usb_bus_msg *)pm)->bus;
656 while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
658 LIST_REMOVE(pd, pd_next);
661 usb_destroy_dev_sync(pd);
669 usb_power_wdog(void *arg)
671 struct usb_bus *bus = arg;
673 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
675 usb_callout_reset(&bus->power_wdog,
676 4 * hz, usb_power_wdog, arg);
680 * The following line of code is only here to recover from
683 usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */
689 usb_bus_power_update(bus);
695 /*------------------------------------------------------------------------*
698 * This function attaches USB in context of the explore thread.
699 *------------------------------------------------------------------------*/
701 usb_bus_attach(struct usb_proc_msg *pm)
704 struct usb_device *child;
707 enum usb_dev_speed speed;
709 bus = ((struct usb_bus_msg *)pm)->bus;
714 switch (bus->usbrev) {
716 speed = USB_SPEED_FULL;
717 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
721 speed = USB_SPEED_FULL;
722 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
726 speed = USB_SPEED_HIGH;
727 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
731 speed = USB_SPEED_VARIABLE;
732 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
736 speed = USB_SPEED_SUPER;
737 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
741 device_printf(bus->bdev, "Unsupported USB revision\n");
742 #if USB_HAVE_ROOT_MOUNT_HOLD
743 usb_root_mount_rel(bus);
748 /* default power_mask value */
749 bus->hw_power_state =
750 USB_HW_POWER_CONTROL |
752 USB_HW_POWER_INTERRUPT |
754 USB_HW_POWER_NON_ROOT_HUB;
758 /* make sure power is set at least once */
760 if (bus->methods->set_hw_power != NULL) {
761 (bus->methods->set_hw_power) (bus);
764 /* allocate the Root USB device */
766 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
767 speed, USB_MODE_HOST);
769 err = usb_probe_and_attach(child,
770 USB_IFACE_INDEX_ANY);
772 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
773 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
774 err = USB_ERR_NO_ROOT_HUB;
784 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
786 #if USB_HAVE_ROOT_MOUNT_HOLD
787 usb_root_mount_rel(bus);
791 /* set softc - we are ready */
792 device_set_softc(dev, bus);
798 /*------------------------------------------------------------------------*
801 * This function creates a thread which runs the USB attach code.
802 *------------------------------------------------------------------------*/
804 usb_attach_sub(device_t dev, struct usb_bus *bus)
807 if (usb_devclass_ptr == NULL)
808 usb_devclass_ptr = devclass_find("usbus");
814 /* Initialise USB process messages */
815 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
816 bus->explore_msg[0].bus = bus;
817 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
818 bus->explore_msg[1].bus = bus;
820 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
821 bus->detach_msg[0].bus = bus;
822 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
823 bus->detach_msg[1].bus = bus;
825 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
826 bus->attach_msg[0].bus = bus;
827 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
828 bus->attach_msg[1].bus = bus;
830 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
831 bus->suspend_msg[0].bus = bus;
832 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
833 bus->suspend_msg[1].bus = bus;
835 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
836 bus->resume_msg[0].bus = bus;
837 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
838 bus->resume_msg[1].bus = bus;
840 bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
841 bus->reset_msg[0].bus = bus;
842 bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
843 bus->reset_msg[1].bus = bus;
845 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
846 bus->shutdown_msg[0].bus = bus;
847 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
848 bus->shutdown_msg[1].bus = bus;
851 LIST_INIT(&bus->pd_cleanup_list);
852 bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup;
853 bus->cleanup_msg[0].bus = bus;
854 bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup;
855 bus->cleanup_msg[1].bus = bus;
858 #if USB_HAVE_PER_BUS_PROCESS
859 /* Create USB explore and callback processes */
861 if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
862 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
863 device_printf(dev, "WARNING: Creation of USB Giant "
864 "callback process failed.\n");
865 } else if (usb_proc_create(USB_BUS_NON_GIANT_ISOC_PROC(bus),
866 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGHEST)) {
867 device_printf(dev, "WARNING: Creation of USB non-Giant ISOC "
868 "callback process failed.\n");
869 } else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus),
870 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) {
871 device_printf(dev, "WARNING: Creation of USB non-Giant BULK "
872 "callback process failed.\n");
873 } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
874 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
875 device_printf(dev, "WARNING: Creation of USB explore "
876 "process failed.\n");
877 } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
878 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
879 device_printf(dev, "WARNING: Creation of USB control transfer "
880 "process failed.\n");
884 /* Get final attach going */
886 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
887 &bus->attach_msg[0], &bus->attach_msg[1]);
890 /* Do initial explore */
891 usb_needs_explore(bus, 1);
894 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
896 /*------------------------------------------------------------------------*
897 * usb_bus_mem_flush_all_cb
898 *------------------------------------------------------------------------*/
901 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
902 struct usb_page *pg, usb_size_t size, usb_size_t align)
904 usb_pc_cpu_flush(pc);
908 /*------------------------------------------------------------------------*
909 * usb_bus_mem_flush_all - factored out code
910 *------------------------------------------------------------------------*/
913 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
916 cb(bus, &usb_bus_mem_flush_all_cb);
921 /*------------------------------------------------------------------------*
922 * usb_bus_mem_alloc_all_cb
923 *------------------------------------------------------------------------*/
926 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
927 struct usb_page *pg, usb_size_t size, usb_size_t align)
929 /* need to initialize the page cache */
930 pc->tag_parent = bus->dma_parent_tag;
932 if (usb_pc_alloc_mem(pc, pg, size, align)) {
933 bus->alloc_failed = 1;
938 /*------------------------------------------------------------------------*
939 * usb_bus_mem_alloc_all - factored out code
944 *------------------------------------------------------------------------*/
946 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
947 usb_bus_mem_cb_t *cb)
949 bus->alloc_failed = 0;
951 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
952 "usb_def_mtx", MTX_DEF | MTX_RECURSE);
954 mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
955 "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
957 usb_callout_init_mtx(&bus->power_wdog,
960 TAILQ_INIT(&bus->intr_q.head);
963 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
964 dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
966 if ((bus->devices_max > USB_MAX_DEVICES) ||
967 (bus->devices_max < USB_MIN_DEVICES) ||
968 (bus->devices == NULL)) {
969 DPRINTFN(0, "Devices field has not been "
970 "initialised properly\n");
971 bus->alloc_failed = 1; /* failure */
975 cb(bus, &usb_bus_mem_alloc_all_cb);
978 if (bus->alloc_failed) {
979 usb_bus_mem_free_all(bus, cb);
981 return (bus->alloc_failed);
984 /*------------------------------------------------------------------------*
985 * usb_bus_mem_free_all_cb
986 *------------------------------------------------------------------------*/
989 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
990 struct usb_page *pg, usb_size_t size, usb_size_t align)
996 /*------------------------------------------------------------------------*
997 * usb_bus_mem_free_all - factored out code
998 *------------------------------------------------------------------------*/
1000 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
1004 cb(bus, &usb_bus_mem_free_all_cb);
1006 usb_dma_tag_unsetup(bus->dma_parent_tag);
1009 mtx_destroy(&bus->bus_mtx);
1010 mtx_destroy(&bus->bus_spin_lock);
1013 /* convenience wrappers */
1015 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
1017 usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
1021 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
1023 return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
1027 usb_proc_explore_lock(struct usb_device *udev)
1029 USB_BUS_LOCK(udev->bus);
1033 usb_proc_explore_unlock(struct usb_device *udev)
1035 USB_BUS_UNLOCK(udev->bus);