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_hub.h>
61 #include <dev/usb/usb_controller.h>
62 #include <dev/usb/usb_bus.h>
63 #include <dev/usb/usb_pf.h>
66 /* function prototypes */
68 static device_probe_t usb_probe;
69 static device_attach_t usb_attach;
70 static device_detach_t usb_detach;
71 static device_suspend_t usb_suspend;
72 static device_resume_t usb_resume;
73 static device_shutdown_t usb_shutdown;
75 static void usb_attach_sub(device_t, struct usb_bus *);
77 /* static variables */
80 static int usb_ctrl_debug = 0;
82 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
83 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
87 static int usb_no_boot_wait = 0;
88 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
89 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
90 "No USB device enumerate waiting at boot.");
92 static int usb_no_shutdown_wait = 0;
93 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
94 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN, &usb_no_shutdown_wait, 0,
95 "No USB device waiting at system shutdown.");
97 static devclass_t usb_devclass;
99 static device_method_t usb_methods[] = {
100 DEVMETHOD(device_probe, usb_probe),
101 DEVMETHOD(device_attach, usb_attach),
102 DEVMETHOD(device_detach, usb_detach),
103 DEVMETHOD(device_suspend, usb_suspend),
104 DEVMETHOD(device_resume, usb_resume),
105 DEVMETHOD(device_shutdown, usb_shutdown),
109 static driver_t usb_driver = {
111 .methods = usb_methods,
115 /* Host Only Drivers */
116 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
117 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
118 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
119 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
121 /* Device Only Drivers */
122 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
123 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
124 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
125 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
127 /*------------------------------------------------------------------------*
130 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
131 *------------------------------------------------------------------------*/
133 usb_probe(device_t dev)
140 usb_root_mount_rel(struct usb_bus *bus)
142 if (bus->bus_roothold != NULL) {
143 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
144 root_mount_rel(bus->bus_roothold);
145 bus->bus_roothold = NULL;
149 /*------------------------------------------------------------------------*
151 *------------------------------------------------------------------------*/
153 usb_attach(device_t dev)
155 struct usb_bus *bus = device_get_ivars(dev);
160 device_printf(dev, "USB device has no ivars\n");
164 if (usb_no_boot_wait == 0) {
165 /* delay vfs_mountroot until the bus is explored */
166 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
169 usb_attach_sub(dev, bus);
171 return (0); /* return success */
174 /*------------------------------------------------------------------------*
176 *------------------------------------------------------------------------*/
178 usb_detach(device_t dev)
180 struct usb_bus *bus = device_get_softc(dev);
185 /* was never setup properly */
188 /* Stop power watchdog */
189 usb_callout_drain(&bus->power_wdog);
191 /* Let the USB explore process detach all devices. */
192 usb_root_mount_rel(bus);
196 /* Queue detach job */
197 usb_proc_msignal(&bus->explore_proc,
198 &bus->detach_msg[0], &bus->detach_msg[1]);
200 /* Wait for detach to complete */
201 usb_proc_mwait(&bus->explore_proc,
202 &bus->detach_msg[0], &bus->detach_msg[1]);
206 /* Get rid of USB callback processes */
208 usb_proc_free(&bus->giant_callback_proc);
209 usb_proc_free(&bus->non_giant_callback_proc);
211 /* Get rid of USB explore process */
213 usb_proc_free(&bus->explore_proc);
215 /* Get rid of control transfer process */
217 usb_proc_free(&bus->control_xfer_proc);
225 /*------------------------------------------------------------------------*
227 *------------------------------------------------------------------------*/
229 usb_suspend(device_t dev)
231 struct usb_bus *bus = device_get_softc(dev);
236 /* was never setup properly */
241 usb_proc_msignal(&bus->explore_proc,
242 &bus->suspend_msg[0], &bus->suspend_msg[1]);
248 /*------------------------------------------------------------------------*
250 *------------------------------------------------------------------------*/
252 usb_resume(device_t dev)
254 struct usb_bus *bus = device_get_softc(dev);
259 /* was never setup properly */
264 usb_proc_msignal(&bus->explore_proc,
265 &bus->resume_msg[0], &bus->resume_msg[1]);
271 /*------------------------------------------------------------------------*
273 *------------------------------------------------------------------------*/
275 usb_shutdown(device_t dev)
277 struct usb_bus *bus = device_get_softc(dev);
282 /* was never setup properly */
286 device_printf(bus->bdev, "Controller shutdown\n");
289 usb_proc_msignal(&bus->explore_proc,
290 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
291 if (usb_no_shutdown_wait == 0) {
292 /* wait for shutdown callback to be executed */
293 usb_proc_mwait(&bus->explore_proc,
294 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
298 device_printf(bus->bdev, "Controller shutdown complete\n");
303 /*------------------------------------------------------------------------*
306 * This function is used to explore the device tree from the root.
307 *------------------------------------------------------------------------*/
309 usb_bus_explore(struct usb_proc_msg *pm)
312 struct usb_device *udev;
314 bus = ((struct usb_bus_msg *)pm)->bus;
315 udev = bus->devices[USB_ROOT_HUB_ADDR];
317 if (bus->no_explore != 0)
320 if (udev && udev->hub) {
324 bus->driver_added_refcount++;
326 if (bus->driver_added_refcount == 0) {
327 /* avoid zero, hence that is memory default */
328 bus->driver_added_refcount = 1;
333 * The following three lines of code are only here to
336 usb_proc_rewakeup(&bus->control_xfer_proc);
337 usb_proc_rewakeup(&bus->giant_callback_proc);
338 usb_proc_rewakeup(&bus->non_giant_callback_proc);
345 * First update the USB power state!
349 /* Explore the Root USB HUB. */
350 (udev->hub->explore) (udev);
353 usb_root_mount_rel(bus);
356 /*------------------------------------------------------------------------*
359 * This function is used to detach the device tree from the root.
360 *------------------------------------------------------------------------*/
362 usb_bus_detach(struct usb_proc_msg *pm)
365 struct usb_device *udev;
368 bus = ((struct usb_bus_msg *)pm)->bus;
369 udev = bus->devices[USB_ROOT_HUB_ADDR];
371 /* clear the softc */
372 device_set_softc(dev, NULL);
375 /* detach children first */
377 bus_generic_detach(dev);
381 * Free USB device and all subdevices, if any.
383 usb_free_device(udev, 0);
386 /* clear bdev variable last */
390 /*------------------------------------------------------------------------*
393 * This function is used to suspend the USB contoller.
394 *------------------------------------------------------------------------*/
396 usb_bus_suspend(struct usb_proc_msg *pm)
399 struct usb_device *udev;
402 bus = ((struct usb_bus_msg *)pm)->bus;
403 udev = bus->devices[USB_ROOT_HUB_ADDR];
405 if (udev == NULL || bus->bdev == NULL)
410 bus_generic_shutdown(bus->bdev);
412 usbd_enum_lock(udev);
414 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
416 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
419 bus->hw_power_state = 0;
423 if (bus->methods->set_hw_power != NULL)
424 (bus->methods->set_hw_power) (bus);
426 if (bus->methods->set_hw_power_sleep != NULL)
427 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
429 usbd_enum_unlock(udev);
434 /*------------------------------------------------------------------------*
437 * This function is used to resume the USB contoller.
438 *------------------------------------------------------------------------*/
440 usb_bus_resume(struct usb_proc_msg *pm)
443 struct usb_device *udev;
446 bus = ((struct usb_bus_msg *)pm)->bus;
447 udev = bus->devices[USB_ROOT_HUB_ADDR];
449 if (udev == NULL || bus->bdev == NULL)
454 usbd_enum_lock(udev);
456 DEVMETHOD(usb_take_controller, NULL); /* dummy */
458 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
461 bus->hw_power_state =
462 USB_HW_POWER_CONTROL |
464 USB_HW_POWER_INTERRUPT |
466 USB_HW_POWER_NON_ROOT_HUB;
470 if (bus->methods->set_hw_power_sleep != NULL)
471 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
473 if (bus->methods->set_hw_power != NULL)
474 (bus->methods->set_hw_power) (bus);
476 /* restore USB configuration to index 0 */
477 err = usbd_set_config_index(udev, 0);
479 device_printf(bus->bdev, "Could not configure root HUB\n");
481 /* probe and attach */
482 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
484 device_printf(bus->bdev, "Could not probe and "
485 "attach root HUB\n");
488 usbd_enum_unlock(udev);
493 /*------------------------------------------------------------------------*
496 * This function is used to shutdown the USB contoller.
497 *------------------------------------------------------------------------*/
499 usb_bus_shutdown(struct usb_proc_msg *pm)
502 struct usb_device *udev;
505 bus = ((struct usb_bus_msg *)pm)->bus;
506 udev = bus->devices[USB_ROOT_HUB_ADDR];
508 if (udev == NULL || bus->bdev == NULL)
513 bus_generic_shutdown(bus->bdev);
515 usbd_enum_lock(udev);
517 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
519 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
522 bus->hw_power_state = 0;
526 if (bus->methods->set_hw_power != NULL)
527 (bus->methods->set_hw_power) (bus);
529 if (bus->methods->set_hw_power_sleep != NULL)
530 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
532 usbd_enum_unlock(udev);
538 usb_power_wdog(void *arg)
540 struct usb_bus *bus = arg;
542 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
544 usb_callout_reset(&bus->power_wdog,
545 4 * hz, usb_power_wdog, arg);
549 * The following line of code is only here to recover from
552 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */
558 usb_bus_power_update(bus);
564 /*------------------------------------------------------------------------*
567 * This function attaches USB in context of the explore thread.
568 *------------------------------------------------------------------------*/
570 usb_bus_attach(struct usb_proc_msg *pm)
573 struct usb_device *child;
576 enum usb_dev_speed speed;
578 bus = ((struct usb_bus_msg *)pm)->bus;
583 switch (bus->usbrev) {
585 speed = USB_SPEED_FULL;
586 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
590 speed = USB_SPEED_FULL;
591 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
595 speed = USB_SPEED_HIGH;
596 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
600 speed = USB_SPEED_VARIABLE;
601 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
605 speed = USB_SPEED_SUPER;
606 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
610 device_printf(bus->bdev, "Unsupported USB revision\n");
611 usb_root_mount_rel(bus);
615 /* default power_mask value */
616 bus->hw_power_state =
617 USB_HW_POWER_CONTROL |
619 USB_HW_POWER_INTERRUPT |
621 USB_HW_POWER_NON_ROOT_HUB;
625 /* make sure power is set at least once */
627 if (bus->methods->set_hw_power != NULL) {
628 (bus->methods->set_hw_power) (bus);
631 /* allocate the Root USB device */
633 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
634 speed, USB_MODE_HOST);
636 err = usb_probe_and_attach(child,
637 USB_IFACE_INDEX_ANY);
639 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
640 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
641 err = USB_ERR_NO_ROOT_HUB;
651 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
653 usb_root_mount_rel(bus);
656 /* set softc - we are ready */
657 device_set_softc(dev, bus);
663 /*------------------------------------------------------------------------*
666 * This function creates a thread which runs the USB attach code.
667 *------------------------------------------------------------------------*/
669 usb_attach_sub(device_t dev, struct usb_bus *bus)
671 const char *pname = device_get_nameunit(dev);
674 if (usb_devclass_ptr == NULL)
675 usb_devclass_ptr = devclass_find("usbus");
681 /* Initialise USB process messages */
682 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
683 bus->explore_msg[0].bus = bus;
684 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
685 bus->explore_msg[1].bus = bus;
687 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
688 bus->detach_msg[0].bus = bus;
689 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
690 bus->detach_msg[1].bus = bus;
692 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
693 bus->attach_msg[0].bus = bus;
694 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
695 bus->attach_msg[1].bus = bus;
697 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
698 bus->suspend_msg[0].bus = bus;
699 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
700 bus->suspend_msg[1].bus = bus;
702 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
703 bus->resume_msg[0].bus = bus;
704 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
705 bus->resume_msg[1].bus = bus;
707 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
708 bus->shutdown_msg[0].bus = bus;
709 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
710 bus->shutdown_msg[1].bus = bus;
712 /* Create USB explore and callback processes */
714 if (usb_proc_create(&bus->giant_callback_proc,
715 &bus->bus_mtx, pname, USB_PRI_MED)) {
716 device_printf(dev, "WARNING: Creation of USB Giant "
717 "callback process failed.\n");
718 } else if (usb_proc_create(&bus->non_giant_callback_proc,
719 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
720 device_printf(dev, "WARNING: Creation of USB non-Giant "
721 "callback process failed.\n");
722 } else if (usb_proc_create(&bus->explore_proc,
723 &bus->bus_mtx, pname, USB_PRI_MED)) {
724 device_printf(dev, "WARNING: Creation of USB explore "
725 "process failed.\n");
726 } else if (usb_proc_create(&bus->control_xfer_proc,
727 &bus->bus_mtx, pname, USB_PRI_MED)) {
728 device_printf(dev, "WARNING: Creation of USB control transfer "
729 "process failed.\n");
731 /* Get final attach going */
733 usb_proc_msignal(&bus->explore_proc,
734 &bus->attach_msg[0], &bus->attach_msg[1]);
737 /* Do initial explore */
738 usb_needs_explore(bus, 1);
742 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
744 /*------------------------------------------------------------------------*
745 * usb_bus_mem_flush_all_cb
746 *------------------------------------------------------------------------*/
749 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
750 struct usb_page *pg, usb_size_t size, usb_size_t align)
752 usb_pc_cpu_flush(pc);
756 /*------------------------------------------------------------------------*
757 * usb_bus_mem_flush_all - factored out code
758 *------------------------------------------------------------------------*/
761 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
764 cb(bus, &usb_bus_mem_flush_all_cb);
769 /*------------------------------------------------------------------------*
770 * usb_bus_mem_alloc_all_cb
771 *------------------------------------------------------------------------*/
774 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
775 struct usb_page *pg, usb_size_t size, usb_size_t align)
777 /* need to initialize the page cache */
778 pc->tag_parent = bus->dma_parent_tag;
780 if (usb_pc_alloc_mem(pc, pg, size, align)) {
781 bus->alloc_failed = 1;
786 /*------------------------------------------------------------------------*
787 * usb_bus_mem_alloc_all - factored out code
792 *------------------------------------------------------------------------*/
794 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
795 usb_bus_mem_cb_t *cb)
797 bus->alloc_failed = 0;
799 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
800 NULL, MTX_DEF | MTX_RECURSE);
802 usb_callout_init_mtx(&bus->power_wdog,
805 TAILQ_INIT(&bus->intr_q.head);
808 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
809 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
811 if ((bus->devices_max > USB_MAX_DEVICES) ||
812 (bus->devices_max < USB_MIN_DEVICES) ||
813 (bus->devices == NULL)) {
814 DPRINTFN(0, "Devices field has not been "
815 "initialised properly\n");
816 bus->alloc_failed = 1; /* failure */
820 cb(bus, &usb_bus_mem_alloc_all_cb);
823 if (bus->alloc_failed) {
824 usb_bus_mem_free_all(bus, cb);
826 return (bus->alloc_failed);
829 /*------------------------------------------------------------------------*
830 * usb_bus_mem_free_all_cb
831 *------------------------------------------------------------------------*/
834 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
835 struct usb_page *pg, usb_size_t size, usb_size_t align)
841 /*------------------------------------------------------------------------*
842 * usb_bus_mem_free_all - factored out code
843 *------------------------------------------------------------------------*/
845 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
849 cb(bus, &usb_bus_mem_free_all_cb);
851 usb_dma_tag_unsetup(bus->dma_parent_tag);
854 mtx_destroy(&bus->bus_mtx);