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/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/unistd.h>
41 #include <sys/callout.h>
42 #include <sys/malloc.h>
45 #include <bus/u4b/usb.h>
46 #include <bus/u4b/usbdi.h>
48 #define USB_DEBUG_VAR usb_ctrl_debug
50 #include <bus/u4b/usb_core.h>
51 #include <bus/u4b/usb_debug.h>
52 #include <bus/u4b/usb_process.h>
53 #include <bus/u4b/usb_busdma.h>
54 #include <bus/u4b/usb_dynamic.h>
55 #include <bus/u4b/usb_device.h>
56 #include <bus/u4b/usb_hub.h>
58 #include <bus/u4b/usb_controller.h>
59 #include <bus/u4b/usb_bus.h>
60 #include <bus/u4b/usb_pf.h>
63 /* function prototypes */
65 static device_probe_t usb_probe;
66 static device_attach_t usb_attach;
67 static device_detach_t usb_detach;
68 static device_suspend_t usb_suspend;
69 static device_resume_t usb_resume;
70 static device_shutdown_t usb_shutdown;
72 static void usb_attach_sub(device_t, struct usb_bus *);
74 /* static variables */
77 static int usb_ctrl_debug = 0;
79 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
80 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
84 static int usb_no_boot_wait = 0;
85 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
86 /* XXX freebsd uses CTLFLAG_RDTUN here */
87 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RW, &usb_no_boot_wait, 0,
88 "No USB device enumerate waiting at boot.");
90 static int usb_no_shutdown_wait = 0;
91 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
92 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW, &usb_no_shutdown_wait, 0,
93 "No USB device waiting at system shutdown.");
95 static devclass_t usb_devclass;
97 static device_method_t usb_methods[] = {
98 DEVMETHOD(device_probe, usb_probe),
99 DEVMETHOD(device_attach, usb_attach),
100 DEVMETHOD(device_detach, usb_detach),
101 DEVMETHOD(device_suspend, usb_suspend),
102 DEVMETHOD(device_resume, usb_resume),
103 DEVMETHOD(device_shutdown, usb_shutdown),
107 static driver_t usb_driver = {
109 .methods = usb_methods,
113 /* Host Only Drivers */
114 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, NULL, NULL);
115 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, NULL, NULL);
116 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, NULL, NULL);
117 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, NULL, NULL);
119 /*------------------------------------------------------------------------*
122 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
123 *------------------------------------------------------------------------*/
125 usb_probe(device_t dev)
132 usb_root_mount_rel(struct usb_bus *bus)
134 if (bus->bus_roothold != NULL) {
135 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
136 #if 0 /* XXX Dragonflybsd seems to not have this? */
137 root_mount_rel(bus->bus_roothold);
139 bus->bus_roothold = NULL;
143 /*------------------------------------------------------------------------*
145 *------------------------------------------------------------------------*/
147 usb_attach(device_t dev)
149 struct usb_bus *bus = device_get_ivars(dev);
154 device_printf(dev, "USB device has no ivars\n");
158 #if 0 /* XXX: Dragonfly does not seem to have this mechanism? */
159 if (usb_no_boot_wait == 0) {
160 /* delay vfs_mountroot until the bus is explored */
161 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
165 usb_attach_sub(dev, bus);
167 return (0); /* return success */
170 /*------------------------------------------------------------------------*
172 *------------------------------------------------------------------------*/
174 usb_detach(device_t dev)
176 struct usb_bus *bus = device_get_softc(dev);
181 /* was never setup properly */
184 /* Stop power watchdog */
185 usb_callout_drain(&bus->power_wdog);
187 /* Let the USB explore process detach all devices. */
188 usb_root_mount_rel(bus);
192 /* Queue detach job */
193 usb_proc_msignal(&bus->explore_proc,
194 &bus->detach_msg[0], &bus->detach_msg[1]);
196 /* Wait for detach to complete */
197 usb_proc_mwait(&bus->explore_proc,
198 &bus->detach_msg[0], &bus->detach_msg[1]);
202 /* Get rid of USB callback processes */
204 usb_proc_free(&bus->giant_callback_proc);
205 usb_proc_free(&bus->non_giant_callback_proc);
207 /* Get rid of USB explore process */
209 usb_proc_free(&bus->explore_proc);
211 /* Get rid of control transfer process */
213 usb_proc_free(&bus->control_xfer_proc);
221 /*------------------------------------------------------------------------*
223 *------------------------------------------------------------------------*/
225 usb_suspend(device_t dev)
227 struct usb_bus *bus = device_get_softc(dev);
232 /* was never setup properly */
237 usb_proc_msignal(&bus->explore_proc,
238 &bus->suspend_msg[0], &bus->suspend_msg[1]);
244 /*------------------------------------------------------------------------*
246 *------------------------------------------------------------------------*/
248 usb_resume(device_t dev)
250 struct usb_bus *bus = device_get_softc(dev);
255 /* was never setup properly */
260 usb_proc_msignal(&bus->explore_proc,
261 &bus->resume_msg[0], &bus->resume_msg[1]);
267 /*------------------------------------------------------------------------*
269 *------------------------------------------------------------------------*/
271 usb_shutdown(device_t dev)
273 struct usb_bus *bus = device_get_softc(dev);
278 /* was never setup properly */
282 device_printf(bus->bdev, "Controller shutdown\n");
285 usb_proc_msignal(&bus->explore_proc,
286 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
287 if (usb_no_shutdown_wait == 0) {
288 /* wait for shutdown callback to be executed */
289 usb_proc_mwait(&bus->explore_proc,
290 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
294 device_printf(bus->bdev, "Controller shutdown complete\n");
299 /*------------------------------------------------------------------------*
302 * This function is used to explore the device tree from the root.
303 *------------------------------------------------------------------------*/
305 usb_bus_explore(struct usb_proc_msg *pm)
308 struct usb_device *udev;
310 bus = ((struct usb_bus_msg *)pm)->bus;
311 udev = bus->devices[USB_ROOT_HUB_ADDR];
313 if (bus->no_explore != 0)
316 if (udev && udev->hub) {
320 bus->driver_added_refcount++;
322 if (bus->driver_added_refcount == 0) {
323 /* avoid zero, hence that is memory default */
324 bus->driver_added_refcount = 1;
329 * The following three lines of code are only here to
332 usb_proc_rewakeup(&bus->control_xfer_proc);
333 usb_proc_rewakeup(&bus->giant_callback_proc);
334 usb_proc_rewakeup(&bus->non_giant_callback_proc);
341 * First update the USB power state!
345 /* Explore the Root USB HUB. */
346 (udev->hub->explore) (udev);
349 usb_root_mount_rel(bus);
352 /*------------------------------------------------------------------------*
355 * This function is used to detach the device tree from the root.
356 *------------------------------------------------------------------------*/
358 usb_bus_detach(struct usb_proc_msg *pm)
361 struct usb_device *udev;
364 bus = ((struct usb_bus_msg *)pm)->bus;
365 udev = bus->devices[USB_ROOT_HUB_ADDR];
367 /* clear the softc */
368 device_set_softc(dev, NULL);
371 /* detach children first */
372 bus_generic_detach(dev);
375 * Free USB device and all subdevices, if any.
377 usb_free_device(udev, 0);
380 /* clear bdev variable last */
384 /*------------------------------------------------------------------------*
387 * This function is used to suspend the USB contoller.
388 *------------------------------------------------------------------------*/
390 usb_bus_suspend(struct usb_proc_msg *pm)
393 struct usb_device *udev;
396 bus = ((struct usb_bus_msg *)pm)->bus;
397 udev = bus->devices[USB_ROOT_HUB_ADDR];
399 if (udev == NULL || bus->bdev == NULL)
404 bus_generic_shutdown(bus->bdev);
406 usbd_enum_lock(udev);
408 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
410 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
413 bus->hw_power_state = 0;
417 if (bus->methods->set_hw_power != NULL)
418 (bus->methods->set_hw_power) (bus);
420 if (bus->methods->set_hw_power_sleep != NULL)
421 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
423 usbd_enum_unlock(udev);
428 /*------------------------------------------------------------------------*
431 * This function is used to resume the USB contoller.
432 *------------------------------------------------------------------------*/
434 usb_bus_resume(struct usb_proc_msg *pm)
437 struct usb_device *udev;
440 bus = ((struct usb_bus_msg *)pm)->bus;
441 udev = bus->devices[USB_ROOT_HUB_ADDR];
443 if (udev == NULL || bus->bdev == NULL)
448 usbd_enum_lock(udev);
450 DEVMETHOD(usb_take_controller, NULL); /* dummy */
452 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
455 bus->hw_power_state =
456 USB_HW_POWER_CONTROL |
458 USB_HW_POWER_INTERRUPT |
460 USB_HW_POWER_NON_ROOT_HUB;
464 if (bus->methods->set_hw_power_sleep != NULL)
465 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
467 if (bus->methods->set_hw_power != NULL)
468 (bus->methods->set_hw_power) (bus);
470 /* restore USB configuration to index 0 */
471 err = usbd_set_config_index(udev, 0);
473 device_printf(bus->bdev, "Could not configure root HUB\n");
475 /* probe and attach */
476 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
478 device_printf(bus->bdev, "Could not probe and "
479 "attach root HUB\n");
482 usbd_enum_unlock(udev);
487 /*------------------------------------------------------------------------*
490 * This function is used to shutdown the USB contoller.
491 *------------------------------------------------------------------------*/
493 usb_bus_shutdown(struct usb_proc_msg *pm)
496 struct usb_device *udev;
499 bus = ((struct usb_bus_msg *)pm)->bus;
500 udev = bus->devices[USB_ROOT_HUB_ADDR];
502 if (udev == NULL || bus->bdev == NULL)
507 bus_generic_shutdown(bus->bdev);
509 usbd_enum_lock(udev);
511 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
513 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
516 bus->hw_power_state = 0;
520 if (bus->methods->set_hw_power != NULL)
521 (bus->methods->set_hw_power) (bus);
523 if (bus->methods->set_hw_power_sleep != NULL)
524 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
526 usbd_enum_unlock(udev);
532 usb_power_wdog(void *arg)
534 struct usb_bus *bus = arg;
535 USB_BUS_LOCK_ASSERT(bus);
537 usb_callout_reset(&bus->power_wdog,
538 4 * hz, usb_power_wdog, arg);
542 * The following line of code is only here to recover from
545 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */
551 usb_bus_power_update(bus);
557 /*------------------------------------------------------------------------*
560 * This function attaches USB in context of the explore thread.
561 *------------------------------------------------------------------------*/
563 usb_bus_attach(struct usb_proc_msg *pm)
566 struct usb_device *child;
569 enum usb_dev_speed speed;
571 bus = ((struct usb_bus_msg *)pm)->bus;
576 switch (bus->usbrev) {
578 speed = USB_SPEED_FULL;
579 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
583 speed = USB_SPEED_FULL;
584 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
588 speed = USB_SPEED_HIGH;
589 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
593 speed = USB_SPEED_VARIABLE;
594 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
598 speed = USB_SPEED_SUPER;
599 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
603 device_printf(bus->bdev, "Unsupported USB revision\n");
604 usb_root_mount_rel(bus);
608 /* default power_mask value */
609 bus->hw_power_state =
610 USB_HW_POWER_CONTROL |
612 USB_HW_POWER_INTERRUPT |
614 USB_HW_POWER_NON_ROOT_HUB;
618 /* make sure power is set at least once */
620 if (bus->methods->set_hw_power != NULL) {
621 (bus->methods->set_hw_power) (bus);
624 /* allocate the Root USB device */
626 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
627 speed, USB_MODE_HOST);
629 err = usb_probe_and_attach(child,
630 USB_IFACE_INDEX_ANY);
632 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
633 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
634 err = USB_ERR_NO_ROOT_HUB;
644 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
646 usb_root_mount_rel(bus);
649 /* set softc - we are ready */
650 device_set_softc(dev, bus);
656 /*------------------------------------------------------------------------*
659 * This function creates a thread which runs the USB attach code.
660 *------------------------------------------------------------------------*/
662 usb_attach_sub(device_t dev, struct usb_bus *bus)
664 const char *pname = device_get_nameunit(dev);
666 if (usb_devclass_ptr == NULL)
667 usb_devclass_ptr = devclass_find("usbus");
672 /* Initialise USB process messages */
673 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
674 bus->explore_msg[0].bus = bus;
675 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
676 bus->explore_msg[1].bus = bus;
678 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
679 bus->detach_msg[0].bus = bus;
680 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
681 bus->detach_msg[1].bus = bus;
683 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
684 bus->attach_msg[0].bus = bus;
685 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
686 bus->attach_msg[1].bus = bus;
688 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
689 bus->suspend_msg[0].bus = bus;
690 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
691 bus->suspend_msg[1].bus = bus;
693 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
694 bus->resume_msg[0].bus = bus;
695 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
696 bus->resume_msg[1].bus = bus;
698 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
699 bus->shutdown_msg[0].bus = bus;
700 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
701 bus->shutdown_msg[1].bus = bus;
703 /* Create USB explore and callback processes */
705 if (usb_proc_create(&bus->giant_callback_proc,
706 &bus->bus_lock, pname, USB_PRI_MED)) {
707 device_printf(dev, "WARNING: Creation of USB Giant "
708 "callback process failed.\n");
709 } else if (usb_proc_create(&bus->non_giant_callback_proc,
710 &bus->bus_lock, pname, USB_PRI_HIGH)) {
711 device_printf(dev, "WARNING: Creation of USB non-Giant "
712 "callback process failed.\n");
713 } else if (usb_proc_create(&bus->explore_proc,
714 &bus->bus_lock, pname, USB_PRI_MED)) {
715 device_printf(dev, "WARNING: Creation of USB explore "
716 "process failed.\n");
717 } else if (usb_proc_create(&bus->control_xfer_proc,
718 &bus->bus_lock, pname, USB_PRI_MED)) {
719 device_printf(dev, "WARNING: Creation of USB control transfer "
720 "process failed.\n");
722 /* Get final attach going */
724 usb_proc_msignal(&bus->explore_proc,
725 &bus->attach_msg[0], &bus->attach_msg[1]);
728 /* Do initial explore */
729 usb_needs_explore(bus, 1);
733 SYSUNINIT(usb_bus_unload, SI_SUB_CONFIGURE, SI_ORDER_ANY, usb_bus_unload, NULL);
735 /*------------------------------------------------------------------------*
736 * usb_bus_mem_flush_all_cb
737 *------------------------------------------------------------------------*/
740 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
741 struct usb_page *pg, usb_size_t size, usb_size_t align)
743 usb_pc_cpu_flush(pc);
747 /*------------------------------------------------------------------------*
748 * usb_bus_mem_flush_all - factored out code
749 *------------------------------------------------------------------------*/
752 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
755 cb(bus, &usb_bus_mem_flush_all_cb);
760 /*------------------------------------------------------------------------*
761 * usb_bus_mem_alloc_all_cb
762 *------------------------------------------------------------------------*/
765 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
766 struct usb_page *pg, usb_size_t size, usb_size_t align)
768 /* need to initialize the page cache */
769 pc->tag_parent = bus->dma_parent_tag;
771 if (usb_pc_alloc_mem(pc, pg, size, align)) {
772 bus->alloc_failed = 1;
777 /*------------------------------------------------------------------------*
778 * usb_bus_mem_alloc_all - factored out code
783 *------------------------------------------------------------------------*/
785 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
786 usb_bus_mem_cb_t *cb)
788 bus->alloc_failed = 0;
790 lockinit(&bus->bus_lock, "USB bus mem", 0, LK_CANRECURSE);
792 usb_callout_init_mtx(&bus->power_wdog,
795 TAILQ_INIT(&bus->intr_q.head);
798 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
799 dmat, &bus->bus_lock, NULL, 32, USB_BUS_DMA_TAG_MAX);
801 if ((bus->devices_max > USB_MAX_DEVICES) ||
802 (bus->devices_max < USB_MIN_DEVICES) ||
803 (bus->devices == NULL)) {
804 DPRINTFN(0, "Devices field has not been "
805 "initialised properly\n");
806 bus->alloc_failed = 1; /* failure */
810 cb(bus, &usb_bus_mem_alloc_all_cb);
813 if (bus->alloc_failed) {
814 usb_bus_mem_free_all(bus, cb);
816 return (bus->alloc_failed);
819 /*------------------------------------------------------------------------*
820 * usb_bus_mem_free_all_cb
821 *------------------------------------------------------------------------*/
824 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
825 struct usb_page *pg, usb_size_t size, usb_size_t align)
831 /*------------------------------------------------------------------------*
832 * usb_bus_mem_free_all - factored out code
833 *------------------------------------------------------------------------*/
835 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
839 cb(bus, &usb_bus_mem_free_all_cb);
841 usb_dma_tag_unsetup(bus->dma_parent_tag);
844 lockuninit(&bus->bus_lock);