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
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
51 #define USB_DEBUG_VAR usb_ctrl_debug
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_debug.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_dynamic.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_dev.h>
60 #include <dev/usb/usb_hub.h>
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
64 #include <dev/usb/usb_pf.h>
67 /* function prototypes */
69 static device_probe_t usb_probe;
70 static device_attach_t usb_attach;
71 static device_detach_t usb_detach;
72 static device_suspend_t usb_suspend;
73 static device_resume_t usb_resume;
74 static device_shutdown_t usb_shutdown;
76 static void usb_attach_sub(device_t, struct usb_bus *);
78 /* static variables */
81 static int usb_ctrl_debug = 0;
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
84 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
88 static int usb_no_boot_wait = 0;
89 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
90 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RD|CTLFLAG_TUN, &usb_no_boot_wait, 0,
91 "No USB device enumerate waiting at boot.");
93 static int usb_no_suspend_wait = 0;
94 TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait);
95 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN,
96 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
98 static int usb_no_shutdown_wait = 0;
99 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
100 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN,
101 &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
103 static devclass_t usb_devclass;
105 static device_method_t usb_methods[] = {
106 DEVMETHOD(device_probe, usb_probe),
107 DEVMETHOD(device_attach, usb_attach),
108 DEVMETHOD(device_detach, usb_detach),
109 DEVMETHOD(device_suspend, usb_suspend),
110 DEVMETHOD(device_resume, usb_resume),
111 DEVMETHOD(device_shutdown, usb_shutdown),
115 static driver_t usb_driver = {
117 .methods = usb_methods,
121 /* Host Only Drivers */
122 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
123 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
124 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
125 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
127 /* Device Only Drivers */
128 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
129 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
130 DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
132 /*------------------------------------------------------------------------*
135 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
136 *------------------------------------------------------------------------*/
138 usb_probe(device_t dev)
145 usb_root_mount_rel(struct usb_bus *bus)
147 if (bus->bus_roothold != NULL) {
148 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
149 root_mount_rel(bus->bus_roothold);
150 bus->bus_roothold = NULL;
154 /*------------------------------------------------------------------------*
156 *------------------------------------------------------------------------*/
158 usb_attach(device_t dev)
160 struct usb_bus *bus = device_get_ivars(dev);
165 device_printf(dev, "USB device has no ivars\n");
169 if (usb_no_boot_wait == 0) {
170 /* delay vfs_mountroot until the bus is explored */
171 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
174 usb_attach_sub(dev, bus);
176 return (0); /* return success */
179 /*------------------------------------------------------------------------*
181 *------------------------------------------------------------------------*/
183 usb_detach(device_t dev)
185 struct usb_bus *bus = device_get_softc(dev);
190 /* was never setup properly */
193 /* Stop power watchdog */
194 usb_callout_drain(&bus->power_wdog);
196 /* Let the USB explore process detach all devices. */
197 usb_root_mount_rel(bus);
201 /* Queue detach job */
202 usb_proc_msignal(&bus->explore_proc,
203 &bus->detach_msg[0], &bus->detach_msg[1]);
205 /* Wait for detach to complete */
206 usb_proc_mwait(&bus->explore_proc,
207 &bus->detach_msg[0], &bus->detach_msg[1]);
210 /* Wait for cleanup to complete */
211 usb_proc_mwait(&bus->explore_proc,
212 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
216 /* Get rid of USB callback processes */
218 usb_proc_free(&bus->giant_callback_proc);
219 usb_proc_free(&bus->non_giant_callback_proc);
221 /* Get rid of USB explore process */
223 usb_proc_free(&bus->explore_proc);
225 /* Get rid of control transfer process */
227 usb_proc_free(&bus->control_xfer_proc);
235 /*------------------------------------------------------------------------*
237 *------------------------------------------------------------------------*/
239 usb_suspend(device_t dev)
241 struct usb_bus *bus = device_get_softc(dev);
246 /* was never setup properly */
251 usb_proc_msignal(&bus->explore_proc,
252 &bus->suspend_msg[0], &bus->suspend_msg[1]);
253 if (usb_no_suspend_wait == 0) {
254 /* wait for suspend callback to be executed */
255 usb_proc_mwait(&bus->explore_proc,
256 &bus->suspend_msg[0], &bus->suspend_msg[1]);
263 /*------------------------------------------------------------------------*
265 *------------------------------------------------------------------------*/
267 usb_resume(device_t dev)
269 struct usb_bus *bus = device_get_softc(dev);
274 /* was never setup properly */
279 usb_proc_msignal(&bus->explore_proc,
280 &bus->resume_msg[0], &bus->resume_msg[1]);
286 /*------------------------------------------------------------------------*
287 * usb_bus_reset_async_locked
288 *------------------------------------------------------------------------*/
290 usb_bus_reset_async_locked(struct usb_bus *bus)
292 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
296 if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
297 bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
298 DPRINTF("Reset already pending\n");
302 device_printf(bus->parent, "Resetting controller\n");
304 usb_proc_msignal(&bus->explore_proc,
305 &bus->reset_msg[0], &bus->reset_msg[1]);
308 /*------------------------------------------------------------------------*
310 *------------------------------------------------------------------------*/
312 usb_shutdown(device_t dev)
314 struct usb_bus *bus = device_get_softc(dev);
319 /* was never setup properly */
323 DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
326 usb_proc_msignal(&bus->explore_proc,
327 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
328 if (usb_no_shutdown_wait == 0) {
329 /* wait for shutdown callback to be executed */
330 usb_proc_mwait(&bus->explore_proc,
331 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
335 DPRINTF("%s: Controller shutdown complete\n",
336 device_get_nameunit(bus->bdev));
341 /*------------------------------------------------------------------------*
344 * This function is used to explore the device tree from the root.
345 *------------------------------------------------------------------------*/
347 usb_bus_explore(struct usb_proc_msg *pm)
350 struct usb_device *udev;
352 bus = ((struct usb_bus_msg *)pm)->bus;
353 udev = bus->devices[USB_ROOT_HUB_ADDR];
355 if (bus->no_explore != 0)
360 uhub_explore_handle_re_enumerate(udev);
364 if (udev != NULL && udev->hub != NULL) {
368 bus->driver_added_refcount++;
370 if (bus->driver_added_refcount == 0) {
371 /* avoid zero, hence that is memory default */
372 bus->driver_added_refcount = 1;
377 * The following three lines of code are only here to
380 usb_proc_rewakeup(&bus->control_xfer_proc);
381 usb_proc_rewakeup(&bus->giant_callback_proc);
382 usb_proc_rewakeup(&bus->non_giant_callback_proc);
389 * First update the USB power state!
393 /* Explore the Root USB HUB. */
394 (udev->hub->explore) (udev);
397 usb_root_mount_rel(bus);
400 /*------------------------------------------------------------------------*
403 * This function is used to detach the device tree from the root.
404 *------------------------------------------------------------------------*/
406 usb_bus_detach(struct usb_proc_msg *pm)
409 struct usb_device *udev;
412 bus = ((struct usb_bus_msg *)pm)->bus;
413 udev = bus->devices[USB_ROOT_HUB_ADDR];
415 /* clear the softc */
416 device_set_softc(dev, NULL);
419 /* detach children first */
421 bus_generic_detach(dev);
425 * Free USB device and all subdevices, if any.
427 usb_free_device(udev, 0);
430 /* clear bdev variable last */
434 /*------------------------------------------------------------------------*
437 * This function is used to suspend the USB controller.
438 *------------------------------------------------------------------------*/
440 usb_bus_suspend(struct usb_proc_msg *pm)
443 struct usb_device *udev;
449 bus = ((struct usb_bus_msg *)pm)->bus;
450 udev = bus->devices[USB_ROOT_HUB_ADDR];
452 if (udev == NULL || bus->bdev == NULL)
458 * We use the shutdown event here because the suspend and
459 * resume events are reserved for the USB port suspend and
460 * resume. The USB system suspend is implemented like full
461 * shutdown and all connected USB devices will be disconnected
462 * subsequently. At resume all USB devices will be
463 * re-connected again.
466 bus_generic_shutdown(bus->bdev);
468 do_unlock = usbd_enum_lock(udev);
470 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
472 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
475 bus->hw_power_state = 0;
479 if (bus->methods->set_hw_power != NULL)
480 (bus->methods->set_hw_power) (bus);
482 if (bus->methods->set_hw_power_sleep != NULL)
483 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
486 usbd_enum_unlock(udev);
491 /*------------------------------------------------------------------------*
494 * This function is used to resume the USB controller.
495 *------------------------------------------------------------------------*/
497 usb_bus_resume(struct usb_proc_msg *pm)
500 struct usb_device *udev;
506 bus = ((struct usb_bus_msg *)pm)->bus;
507 udev = bus->devices[USB_ROOT_HUB_ADDR];
509 if (udev == NULL || bus->bdev == NULL)
514 do_unlock = usbd_enum_lock(udev);
516 DEVMETHOD(usb_take_controller, NULL); /* dummy */
518 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
521 bus->hw_power_state =
522 USB_HW_POWER_CONTROL |
524 USB_HW_POWER_INTERRUPT |
526 USB_HW_POWER_NON_ROOT_HUB;
530 if (bus->methods->set_hw_power_sleep != NULL)
531 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
533 if (bus->methods->set_hw_power != NULL)
534 (bus->methods->set_hw_power) (bus);
536 /* restore USB configuration to index 0 */
537 err = usbd_set_config_index(udev, 0);
539 device_printf(bus->bdev, "Could not configure root HUB\n");
541 /* probe and attach */
542 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
544 device_printf(bus->bdev, "Could not probe and "
545 "attach root HUB\n");
549 usbd_enum_unlock(udev);
554 /*------------------------------------------------------------------------*
557 * This function is used to reset the USB controller.
558 *------------------------------------------------------------------------*/
560 usb_bus_reset(struct usb_proc_msg *pm)
566 bus = ((struct usb_bus_msg *)pm)->bus;
568 if (bus->bdev == NULL || bus->no_explore != 0)
571 /* a suspend and resume will reset the USB controller */
576 /*------------------------------------------------------------------------*
579 * This function is used to shutdown the USB controller.
580 *------------------------------------------------------------------------*/
582 usb_bus_shutdown(struct usb_proc_msg *pm)
585 struct usb_device *udev;
589 bus = ((struct usb_bus_msg *)pm)->bus;
590 udev = bus->devices[USB_ROOT_HUB_ADDR];
592 if (udev == NULL || bus->bdev == NULL)
597 bus_generic_shutdown(bus->bdev);
599 do_unlock = usbd_enum_lock(udev);
601 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
603 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
606 bus->hw_power_state = 0;
610 if (bus->methods->set_hw_power != NULL)
611 (bus->methods->set_hw_power) (bus);
613 if (bus->methods->set_hw_power_sleep != NULL)
614 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
617 usbd_enum_unlock(udev);
622 /*------------------------------------------------------------------------*
625 * This function is used to cleanup leftover USB character devices.
626 *------------------------------------------------------------------------*/
629 usb_bus_cleanup(struct usb_proc_msg *pm)
632 struct usb_fs_privdata *pd;
634 bus = ((struct usb_bus_msg *)pm)->bus;
636 while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
638 LIST_REMOVE(pd, pd_next);
641 usb_destroy_dev_sync(pd);
649 usb_power_wdog(void *arg)
651 struct usb_bus *bus = arg;
653 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
655 usb_callout_reset(&bus->power_wdog,
656 4 * hz, usb_power_wdog, arg);
660 * The following line of code is only here to recover from
663 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */
669 usb_bus_power_update(bus);
675 /*------------------------------------------------------------------------*
678 * This function attaches USB in context of the explore thread.
679 *------------------------------------------------------------------------*/
681 usb_bus_attach(struct usb_proc_msg *pm)
684 struct usb_device *child;
687 enum usb_dev_speed speed;
689 bus = ((struct usb_bus_msg *)pm)->bus;
694 switch (bus->usbrev) {
696 speed = USB_SPEED_FULL;
697 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
701 speed = USB_SPEED_FULL;
702 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
706 speed = USB_SPEED_HIGH;
707 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
711 speed = USB_SPEED_VARIABLE;
712 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
716 speed = USB_SPEED_SUPER;
717 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
721 device_printf(bus->bdev, "Unsupported USB revision\n");
722 usb_root_mount_rel(bus);
726 /* default power_mask value */
727 bus->hw_power_state =
728 USB_HW_POWER_CONTROL |
730 USB_HW_POWER_INTERRUPT |
732 USB_HW_POWER_NON_ROOT_HUB;
736 /* make sure power is set at least once */
738 if (bus->methods->set_hw_power != NULL) {
739 (bus->methods->set_hw_power) (bus);
742 /* allocate the Root USB device */
744 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
745 speed, USB_MODE_HOST);
747 err = usb_probe_and_attach(child,
748 USB_IFACE_INDEX_ANY);
750 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
751 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
752 err = USB_ERR_NO_ROOT_HUB;
762 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
764 usb_root_mount_rel(bus);
767 /* set softc - we are ready */
768 device_set_softc(dev, bus);
774 /*------------------------------------------------------------------------*
777 * This function creates a thread which runs the USB attach code.
778 *------------------------------------------------------------------------*/
780 usb_attach_sub(device_t dev, struct usb_bus *bus)
782 const char *pname = device_get_nameunit(dev);
785 if (usb_devclass_ptr == NULL)
786 usb_devclass_ptr = devclass_find("usbus");
792 /* Initialise USB process messages */
793 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
794 bus->explore_msg[0].bus = bus;
795 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
796 bus->explore_msg[1].bus = bus;
798 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
799 bus->detach_msg[0].bus = bus;
800 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
801 bus->detach_msg[1].bus = bus;
803 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
804 bus->attach_msg[0].bus = bus;
805 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
806 bus->attach_msg[1].bus = bus;
808 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
809 bus->suspend_msg[0].bus = bus;
810 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
811 bus->suspend_msg[1].bus = bus;
813 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
814 bus->resume_msg[0].bus = bus;
815 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
816 bus->resume_msg[1].bus = bus;
818 bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
819 bus->reset_msg[0].bus = bus;
820 bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
821 bus->reset_msg[1].bus = bus;
823 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
824 bus->shutdown_msg[0].bus = bus;
825 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
826 bus->shutdown_msg[1].bus = bus;
829 LIST_INIT(&bus->pd_cleanup_list);
830 bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup;
831 bus->cleanup_msg[0].bus = bus;
832 bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup;
833 bus->cleanup_msg[1].bus = bus;
836 /* Create USB explore and callback processes */
838 if (usb_proc_create(&bus->giant_callback_proc,
839 &bus->bus_mtx, pname, USB_PRI_MED)) {
840 device_printf(dev, "WARNING: Creation of USB Giant "
841 "callback process failed.\n");
842 } else if (usb_proc_create(&bus->non_giant_callback_proc,
843 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
844 device_printf(dev, "WARNING: Creation of USB non-Giant "
845 "callback process failed.\n");
846 } else if (usb_proc_create(&bus->explore_proc,
847 &bus->bus_mtx, pname, USB_PRI_MED)) {
848 device_printf(dev, "WARNING: Creation of USB explore "
849 "process failed.\n");
850 } else if (usb_proc_create(&bus->control_xfer_proc,
851 &bus->bus_mtx, pname, USB_PRI_MED)) {
852 device_printf(dev, "WARNING: Creation of USB control transfer "
853 "process failed.\n");
855 /* Get final attach going */
857 usb_proc_msignal(&bus->explore_proc,
858 &bus->attach_msg[0], &bus->attach_msg[1]);
861 /* Do initial explore */
862 usb_needs_explore(bus, 1);
866 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
868 /*------------------------------------------------------------------------*
869 * usb_bus_mem_flush_all_cb
870 *------------------------------------------------------------------------*/
873 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
874 struct usb_page *pg, usb_size_t size, usb_size_t align)
876 usb_pc_cpu_flush(pc);
880 /*------------------------------------------------------------------------*
881 * usb_bus_mem_flush_all - factored out code
882 *------------------------------------------------------------------------*/
885 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
888 cb(bus, &usb_bus_mem_flush_all_cb);
893 /*------------------------------------------------------------------------*
894 * usb_bus_mem_alloc_all_cb
895 *------------------------------------------------------------------------*/
898 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
899 struct usb_page *pg, usb_size_t size, usb_size_t align)
901 /* need to initialize the page cache */
902 pc->tag_parent = bus->dma_parent_tag;
904 if (usb_pc_alloc_mem(pc, pg, size, align)) {
905 bus->alloc_failed = 1;
910 /*------------------------------------------------------------------------*
911 * usb_bus_mem_alloc_all - factored out code
916 *------------------------------------------------------------------------*/
918 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
919 usb_bus_mem_cb_t *cb)
921 bus->alloc_failed = 0;
923 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
924 NULL, MTX_DEF | MTX_RECURSE);
926 usb_callout_init_mtx(&bus->power_wdog,
929 TAILQ_INIT(&bus->intr_q.head);
932 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
933 dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
935 if ((bus->devices_max > USB_MAX_DEVICES) ||
936 (bus->devices_max < USB_MIN_DEVICES) ||
937 (bus->devices == NULL)) {
938 DPRINTFN(0, "Devices field has not been "
939 "initialised properly\n");
940 bus->alloc_failed = 1; /* failure */
944 cb(bus, &usb_bus_mem_alloc_all_cb);
947 if (bus->alloc_failed) {
948 usb_bus_mem_free_all(bus, cb);
950 return (bus->alloc_failed);
953 /*------------------------------------------------------------------------*
954 * usb_bus_mem_free_all_cb
955 *------------------------------------------------------------------------*/
958 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
959 struct usb_page *pg, usb_size_t size, usb_size_t align)
965 /*------------------------------------------------------------------------*
966 * usb_bus_mem_free_all - factored out code
967 *------------------------------------------------------------------------*/
969 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
973 cb(bus, &usb_bus_mem_free_all_cb);
975 usb_dma_tag_unsetup(bus->dma_parent_tag);
978 mtx_destroy(&bus->bus_mtx);
981 /* convenience wrappers */
983 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
985 usb_proc_mwait(&udev->bus->explore_proc, pm1, pm2);
989 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
991 return (usb_proc_msignal(&udev->bus->explore_proc, pm1, pm2));
995 usb_proc_explore_lock(struct usb_device *udev)
997 USB_BUS_LOCK(udev->bus);
1001 usb_proc_explore_unlock(struct usb_device *udev)
1003 USB_BUS_UNLOCK(udev->bus);