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_hub.h>
64 #include <dev/usb/usb_controller.h>
65 #include <dev/usb/usb_bus.h>
66 #include <dev/usb/usb_pf.h>
68 #endif /* USB_GLOBAL_INCLUDE_FILE */
70 /* function prototypes */
72 static device_probe_t usb_probe;
73 static device_attach_t usb_attach;
74 static device_detach_t usb_detach;
75 static device_suspend_t usb_suspend;
76 static device_resume_t usb_resume;
77 static device_shutdown_t usb_shutdown;
79 static void usb_attach_sub(device_t, struct usb_bus *);
81 /* static variables */
84 static int usb_ctrl_debug = 0;
86 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
87 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
91 #if USB_HAVE_ROOT_MOUNT_HOLD
92 static int usb_no_boot_wait = 0;
93 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
94 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RD|CTLFLAG_TUN, &usb_no_boot_wait, 0,
95 "No USB device enumerate waiting at boot.");
98 static int usb_no_suspend_wait = 0;
99 TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait);
100 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN,
101 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
103 static int usb_no_shutdown_wait = 0;
104 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
105 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN,
106 &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
108 static devclass_t usb_devclass;
110 static device_method_t usb_methods[] = {
111 DEVMETHOD(device_probe, usb_probe),
112 DEVMETHOD(device_attach, usb_attach),
113 DEVMETHOD(device_detach, usb_detach),
114 DEVMETHOD(device_suspend, usb_suspend),
115 DEVMETHOD(device_resume, usb_resume),
116 DEVMETHOD(device_shutdown, usb_shutdown),
121 static driver_t usb_driver = {
123 .methods = usb_methods,
127 /* Host Only Drivers */
128 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
129 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
130 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
131 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
133 /* Device Only Drivers */
134 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
135 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
136 DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
137 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
139 /* Dual Mode Drivers */
140 DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0);
142 /*------------------------------------------------------------------------*
145 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
146 *------------------------------------------------------------------------*/
148 usb_probe(device_t dev)
154 #if USB_HAVE_ROOT_MOUNT_HOLD
156 usb_root_mount_rel(struct usb_bus *bus)
158 if (bus->bus_roothold != NULL) {
159 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
160 root_mount_rel(bus->bus_roothold);
161 bus->bus_roothold = NULL;
166 /*------------------------------------------------------------------------*
168 *------------------------------------------------------------------------*/
170 usb_attach(device_t dev)
172 struct usb_bus *bus = device_get_ivars(dev);
177 device_printf(dev, "USB device has no ivars\n");
181 #if USB_HAVE_ROOT_MOUNT_HOLD
182 if (usb_no_boot_wait == 0) {
183 /* delay vfs_mountroot until the bus is explored */
184 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
187 usb_attach_sub(dev, bus);
189 return (0); /* return success */
192 /*------------------------------------------------------------------------*
194 *------------------------------------------------------------------------*/
196 usb_detach(device_t dev)
198 struct usb_bus *bus = device_get_softc(dev);
203 /* was never setup properly */
206 /* Stop power watchdog */
207 usb_callout_drain(&bus->power_wdog);
209 #if USB_HAVE_ROOT_MOUNT_HOLD
210 /* Let the USB explore process detach all devices. */
211 usb_root_mount_rel(bus);
216 /* Queue detach job */
217 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
218 &bus->detach_msg[0], &bus->detach_msg[1]);
220 /* Wait for detach to complete */
221 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
222 &bus->detach_msg[0], &bus->detach_msg[1]);
226 #if USB_HAVE_PER_BUS_PROCESS
227 /* Get rid of USB callback processes */
229 usb_proc_free(USB_BUS_GIANT_PROC(bus));
230 usb_proc_free(USB_BUS_NON_GIANT_PROC(bus));
232 /* Get rid of USB explore process */
234 usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
236 /* Get rid of control transfer process */
238 usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
247 /*------------------------------------------------------------------------*
249 *------------------------------------------------------------------------*/
251 usb_suspend(device_t dev)
253 struct usb_bus *bus = device_get_softc(dev);
258 /* was never setup properly */
263 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
264 &bus->suspend_msg[0], &bus->suspend_msg[1]);
265 if (usb_no_suspend_wait == 0) {
266 /* wait for suspend callback to be executed */
267 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
268 &bus->suspend_msg[0], &bus->suspend_msg[1]);
275 /*------------------------------------------------------------------------*
277 *------------------------------------------------------------------------*/
279 usb_resume(device_t dev)
281 struct usb_bus *bus = device_get_softc(dev);
286 /* was never setup properly */
291 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
292 &bus->resume_msg[0], &bus->resume_msg[1]);
298 /*------------------------------------------------------------------------*
299 * usb_bus_reset_async_locked
300 *------------------------------------------------------------------------*/
302 usb_bus_reset_async_locked(struct usb_bus *bus)
304 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
308 if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
309 bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
310 DPRINTF("Reset already pending\n");
314 device_printf(bus->parent, "Resetting controller\n");
316 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
317 &bus->reset_msg[0], &bus->reset_msg[1]);
320 /*------------------------------------------------------------------------*
322 *------------------------------------------------------------------------*/
324 usb_shutdown(device_t dev)
326 struct usb_bus *bus = device_get_softc(dev);
331 /* was never setup properly */
335 DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
338 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
339 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
340 if (usb_no_shutdown_wait == 0) {
341 /* wait for shutdown callback to be executed */
342 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
343 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
347 DPRINTF("%s: Controller shutdown complete\n",
348 device_get_nameunit(bus->bdev));
353 /*------------------------------------------------------------------------*
356 * This function is used to explore the device tree from the root.
357 *------------------------------------------------------------------------*/
359 usb_bus_explore(struct usb_proc_msg *pm)
362 struct usb_device *udev;
364 bus = ((struct usb_bus_msg *)pm)->bus;
365 udev = bus->devices[USB_ROOT_HUB_ADDR];
367 if (bus->no_explore != 0)
372 uhub_explore_handle_re_enumerate(udev);
376 if (udev != NULL && udev->hub != NULL) {
380 bus->driver_added_refcount++;
382 if (bus->driver_added_refcount == 0) {
383 /* avoid zero, hence that is memory default */
384 bus->driver_added_refcount = 1;
389 * The following three lines of code are only here to
392 usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
393 usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
394 usb_proc_rewakeup(USB_BUS_NON_GIANT_PROC(bus));
401 * First update the USB power state!
405 /* Explore the Root USB HUB. */
406 (udev->hub->explore) (udev);
409 #if USB_HAVE_ROOT_MOUNT_HOLD
410 usb_root_mount_rel(bus);
414 /*------------------------------------------------------------------------*
417 * This function is used to detach the device tree from the root.
418 *------------------------------------------------------------------------*/
420 usb_bus_detach(struct usb_proc_msg *pm)
423 struct usb_device *udev;
426 bus = ((struct usb_bus_msg *)pm)->bus;
427 udev = bus->devices[USB_ROOT_HUB_ADDR];
429 /* clear the softc */
430 device_set_softc(dev, NULL);
433 /* detach children first */
435 bus_generic_detach(dev);
439 * Free USB device and all subdevices, if any.
441 usb_free_device(udev, 0);
444 /* clear bdev variable last */
448 /*------------------------------------------------------------------------*
451 * This function is used to suspend the USB controller.
452 *------------------------------------------------------------------------*/
454 usb_bus_suspend(struct usb_proc_msg *pm)
457 struct usb_device *udev;
463 bus = ((struct usb_bus_msg *)pm)->bus;
464 udev = bus->devices[USB_ROOT_HUB_ADDR];
466 if (udev == NULL || bus->bdev == NULL)
472 * We use the shutdown event here because the suspend and
473 * resume events are reserved for the USB port suspend and
474 * resume. The USB system suspend is implemented like full
475 * shutdown and all connected USB devices will be disconnected
476 * subsequently. At resume all USB devices will be
477 * re-connected again.
480 bus_generic_shutdown(bus->bdev);
482 do_unlock = usbd_enum_lock(udev);
484 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
486 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
489 bus->hw_power_state = 0;
493 if (bus->methods->set_hw_power != NULL)
494 (bus->methods->set_hw_power) (bus);
496 if (bus->methods->set_hw_power_sleep != NULL)
497 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
500 usbd_enum_unlock(udev);
505 /*------------------------------------------------------------------------*
508 * This function is used to resume the USB controller.
509 *------------------------------------------------------------------------*/
511 usb_bus_resume(struct usb_proc_msg *pm)
514 struct usb_device *udev;
520 bus = ((struct usb_bus_msg *)pm)->bus;
521 udev = bus->devices[USB_ROOT_HUB_ADDR];
523 if (udev == NULL || bus->bdev == NULL)
528 do_unlock = usbd_enum_lock(udev);
530 DEVMETHOD(usb_take_controller, NULL); /* dummy */
532 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
535 bus->hw_power_state =
536 USB_HW_POWER_CONTROL |
538 USB_HW_POWER_INTERRUPT |
540 USB_HW_POWER_NON_ROOT_HUB;
544 if (bus->methods->set_hw_power_sleep != NULL)
545 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
547 if (bus->methods->set_hw_power != NULL)
548 (bus->methods->set_hw_power) (bus);
550 /* restore USB configuration to index 0 */
551 err = usbd_set_config_index(udev, 0);
553 device_printf(bus->bdev, "Could not configure root HUB\n");
555 /* probe and attach */
556 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
558 device_printf(bus->bdev, "Could not probe and "
559 "attach root HUB\n");
563 usbd_enum_unlock(udev);
568 /*------------------------------------------------------------------------*
571 * This function is used to reset the USB controller.
572 *------------------------------------------------------------------------*/
574 usb_bus_reset(struct usb_proc_msg *pm)
580 bus = ((struct usb_bus_msg *)pm)->bus;
582 if (bus->bdev == NULL || bus->no_explore != 0)
585 /* a suspend and resume will reset the USB controller */
590 /*------------------------------------------------------------------------*
593 * This function is used to shutdown the USB controller.
594 *------------------------------------------------------------------------*/
596 usb_bus_shutdown(struct usb_proc_msg *pm)
599 struct usb_device *udev;
603 bus = ((struct usb_bus_msg *)pm)->bus;
604 udev = bus->devices[USB_ROOT_HUB_ADDR];
606 if (udev == NULL || bus->bdev == NULL)
611 bus_generic_shutdown(bus->bdev);
613 do_unlock = usbd_enum_lock(udev);
615 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
617 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
620 bus->hw_power_state = 0;
624 if (bus->methods->set_hw_power != NULL)
625 (bus->methods->set_hw_power) (bus);
627 if (bus->methods->set_hw_power_sleep != NULL)
628 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
631 usbd_enum_unlock(udev);
637 usb_power_wdog(void *arg)
639 struct usb_bus *bus = arg;
641 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
643 usb_callout_reset(&bus->power_wdog,
644 4 * hz, usb_power_wdog, arg);
648 * The following line of code is only here to recover from
651 usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */
657 usb_bus_power_update(bus);
663 /*------------------------------------------------------------------------*
666 * This function attaches USB in context of the explore thread.
667 *------------------------------------------------------------------------*/
669 usb_bus_attach(struct usb_proc_msg *pm)
672 struct usb_device *child;
675 enum usb_dev_speed speed;
677 bus = ((struct usb_bus_msg *)pm)->bus;
682 switch (bus->usbrev) {
684 speed = USB_SPEED_FULL;
685 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
689 speed = USB_SPEED_FULL;
690 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
694 speed = USB_SPEED_HIGH;
695 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
699 speed = USB_SPEED_VARIABLE;
700 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
704 speed = USB_SPEED_SUPER;
705 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
709 device_printf(bus->bdev, "Unsupported USB revision\n");
710 #if USB_HAVE_ROOT_MOUNT_HOLD
711 usb_root_mount_rel(bus);
716 /* default power_mask value */
717 bus->hw_power_state =
718 USB_HW_POWER_CONTROL |
720 USB_HW_POWER_INTERRUPT |
722 USB_HW_POWER_NON_ROOT_HUB;
726 /* make sure power is set at least once */
728 if (bus->methods->set_hw_power != NULL) {
729 (bus->methods->set_hw_power) (bus);
732 /* allocate the Root USB device */
734 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
735 speed, USB_MODE_HOST);
737 err = usb_probe_and_attach(child,
738 USB_IFACE_INDEX_ANY);
740 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
741 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
742 err = USB_ERR_NO_ROOT_HUB;
752 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
754 #if USB_HAVE_ROOT_MOUNT_HOLD
755 usb_root_mount_rel(bus);
759 /* set softc - we are ready */
760 device_set_softc(dev, bus);
766 /*------------------------------------------------------------------------*
769 * This function creates a thread which runs the USB attach code.
770 *------------------------------------------------------------------------*/
772 usb_attach_sub(device_t dev, struct usb_bus *bus)
775 if (usb_devclass_ptr == NULL)
776 usb_devclass_ptr = devclass_find("usbus");
782 /* Initialise USB process messages */
783 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
784 bus->explore_msg[0].bus = bus;
785 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
786 bus->explore_msg[1].bus = bus;
788 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
789 bus->detach_msg[0].bus = bus;
790 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
791 bus->detach_msg[1].bus = bus;
793 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
794 bus->attach_msg[0].bus = bus;
795 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
796 bus->attach_msg[1].bus = bus;
798 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
799 bus->suspend_msg[0].bus = bus;
800 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
801 bus->suspend_msg[1].bus = bus;
803 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
804 bus->resume_msg[0].bus = bus;
805 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
806 bus->resume_msg[1].bus = bus;
808 bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
809 bus->reset_msg[0].bus = bus;
810 bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
811 bus->reset_msg[1].bus = bus;
813 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
814 bus->shutdown_msg[0].bus = bus;
815 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
816 bus->shutdown_msg[1].bus = bus;
818 #if USB_HAVE_PER_BUS_PROCESS
819 /* Create USB explore and callback processes */
821 if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
822 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
823 device_printf(dev, "WARNING: Creation of USB Giant "
824 "callback process failed.\n");
825 } else if (usb_proc_create(USB_BUS_NON_GIANT_PROC(bus),
826 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) {
827 device_printf(dev, "WARNING: Creation of USB non-Giant "
828 "callback process failed.\n");
829 } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
830 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
831 device_printf(dev, "WARNING: Creation of USB explore "
832 "process failed.\n");
833 } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
834 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
835 device_printf(dev, "WARNING: Creation of USB control transfer "
836 "process failed.\n");
840 /* Get final attach going */
842 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
843 &bus->attach_msg[0], &bus->attach_msg[1]);
846 /* Do initial explore */
847 usb_needs_explore(bus, 1);
850 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
852 /*------------------------------------------------------------------------*
853 * usb_bus_mem_flush_all_cb
854 *------------------------------------------------------------------------*/
857 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
858 struct usb_page *pg, usb_size_t size, usb_size_t align)
860 usb_pc_cpu_flush(pc);
864 /*------------------------------------------------------------------------*
865 * usb_bus_mem_flush_all - factored out code
866 *------------------------------------------------------------------------*/
869 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
872 cb(bus, &usb_bus_mem_flush_all_cb);
877 /*------------------------------------------------------------------------*
878 * usb_bus_mem_alloc_all_cb
879 *------------------------------------------------------------------------*/
882 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
883 struct usb_page *pg, usb_size_t size, usb_size_t align)
885 /* need to initialize the page cache */
886 pc->tag_parent = bus->dma_parent_tag;
888 if (usb_pc_alloc_mem(pc, pg, size, align)) {
889 bus->alloc_failed = 1;
894 /*------------------------------------------------------------------------*
895 * usb_bus_mem_alloc_all - factored out code
900 *------------------------------------------------------------------------*/
902 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
903 usb_bus_mem_cb_t *cb)
905 bus->alloc_failed = 0;
907 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
908 "usb_def_mtx", MTX_DEF | MTX_RECURSE);
910 mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
911 "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
913 usb_callout_init_mtx(&bus->power_wdog,
916 TAILQ_INIT(&bus->intr_q.head);
919 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
920 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
922 if ((bus->devices_max > USB_MAX_DEVICES) ||
923 (bus->devices_max < USB_MIN_DEVICES) ||
924 (bus->devices == NULL)) {
925 DPRINTFN(0, "Devices field has not been "
926 "initialised properly\n");
927 bus->alloc_failed = 1; /* failure */
931 cb(bus, &usb_bus_mem_alloc_all_cb);
934 if (bus->alloc_failed) {
935 usb_bus_mem_free_all(bus, cb);
937 return (bus->alloc_failed);
940 /*------------------------------------------------------------------------*
941 * usb_bus_mem_free_all_cb
942 *------------------------------------------------------------------------*/
945 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
946 struct usb_page *pg, usb_size_t size, usb_size_t align)
952 /*------------------------------------------------------------------------*
953 * usb_bus_mem_free_all - factored out code
954 *------------------------------------------------------------------------*/
956 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
960 cb(bus, &usb_bus_mem_free_all_cb);
962 usb_dma_tag_unsetup(bus->dma_parent_tag);
965 mtx_destroy(&bus->bus_mtx);
966 mtx_destroy(&bus->bus_spin_lock);
969 /* convenience wrappers */
971 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
973 usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
977 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
979 return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
983 usb_proc_explore_lock(struct usb_device *udev)
985 USB_BUS_LOCK(udev->bus);
989 usb_proc_explore_unlock(struct usb_device *udev)
991 USB_BUS_UNLOCK(udev->bus);