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 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 device enumerate waiting at boot.");
92 static devclass_t usb_devclass;
94 static device_method_t usb_methods[] = {
95 DEVMETHOD(device_probe, usb_probe),
96 DEVMETHOD(device_attach, usb_attach),
97 DEVMETHOD(device_detach, usb_detach),
98 DEVMETHOD(device_suspend, usb_suspend),
99 DEVMETHOD(device_resume, usb_resume),
100 DEVMETHOD(device_shutdown, usb_shutdown),
104 static driver_t usb_driver = {
106 .methods = usb_methods,
110 /* Host Only Drivers */
111 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
112 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
113 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
114 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
116 /* Device Only Drivers */
117 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
118 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
119 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
121 /*------------------------------------------------------------------------*
124 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
125 *------------------------------------------------------------------------*/
127 usb_probe(device_t dev)
134 usb_root_mount_rel(struct usb_bus *bus)
136 if (bus->bus_roothold != NULL) {
137 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
138 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 (usb_no_boot_wait == 0) {
159 /* delay vfs_mountroot until the bus is explored */
160 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
163 usb_attach_sub(dev, bus);
165 return (0); /* return success */
168 /*------------------------------------------------------------------------*
170 *------------------------------------------------------------------------*/
172 usb_detach(device_t dev)
174 struct usb_bus *bus = device_get_softc(dev);
179 /* was never setup properly */
182 /* Stop power watchdog */
183 usb_callout_drain(&bus->power_wdog);
185 /* Let the USB explore process detach all devices. */
186 usb_root_mount_rel(bus);
190 /* Queue detach job */
191 usb_proc_msignal(&bus->explore_proc,
192 &bus->detach_msg[0], &bus->detach_msg[1]);
194 /* Wait for detach to complete */
195 usb_proc_mwait(&bus->explore_proc,
196 &bus->detach_msg[0], &bus->detach_msg[1]);
200 /* Get rid of USB callback processes */
202 usb_proc_free(&bus->giant_callback_proc);
203 usb_proc_free(&bus->non_giant_callback_proc);
205 /* Get rid of USB explore process */
207 usb_proc_free(&bus->explore_proc);
209 /* Get rid of control transfer process */
211 usb_proc_free(&bus->control_xfer_proc);
219 /*------------------------------------------------------------------------*
221 *------------------------------------------------------------------------*/
223 usb_suspend(device_t dev)
225 struct usb_bus *bus = device_get_softc(dev);
230 /* was never setup properly */
235 usb_proc_msignal(&bus->explore_proc,
236 &bus->suspend_msg[0], &bus->suspend_msg[1]);
242 /*------------------------------------------------------------------------*
244 *------------------------------------------------------------------------*/
246 usb_resume(device_t dev)
248 struct usb_bus *bus = device_get_softc(dev);
253 /* was never setup properly */
258 usb_proc_msignal(&bus->explore_proc,
259 &bus->resume_msg[0], &bus->resume_msg[1]);
265 /*------------------------------------------------------------------------*
267 *------------------------------------------------------------------------*/
269 usb_shutdown(device_t dev)
271 struct usb_bus *bus = device_get_softc(dev);
276 /* was never setup properly */
281 usb_proc_msignal(&bus->explore_proc,
282 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
288 /*------------------------------------------------------------------------*
291 * This function is used to explore the device tree from the root.
292 *------------------------------------------------------------------------*/
294 usb_bus_explore(struct usb_proc_msg *pm)
297 struct usb_device *udev;
299 bus = ((struct usb_bus_msg *)pm)->bus;
300 udev = bus->devices[USB_ROOT_HUB_ADDR];
302 if (bus->no_explore != 0)
305 if (udev && udev->hub) {
309 bus->driver_added_refcount++;
311 if (bus->driver_added_refcount == 0) {
312 /* avoid zero, hence that is memory default */
313 bus->driver_added_refcount = 1;
318 * The following three lines of code are only here to
321 usb_proc_rewakeup(&bus->control_xfer_proc);
322 usb_proc_rewakeup(&bus->giant_callback_proc);
323 usb_proc_rewakeup(&bus->non_giant_callback_proc);
330 * First update the USB power state!
334 /* Explore the Root USB HUB. */
335 (udev->hub->explore) (udev);
338 usb_root_mount_rel(bus);
341 /*------------------------------------------------------------------------*
344 * This function is used to detach the device tree from the root.
345 *------------------------------------------------------------------------*/
347 usb_bus_detach(struct usb_proc_msg *pm)
350 struct usb_device *udev;
353 bus = ((struct usb_bus_msg *)pm)->bus;
354 udev = bus->devices[USB_ROOT_HUB_ADDR];
356 /* clear the softc */
357 device_set_softc(dev, NULL);
360 /* detach children first */
362 bus_generic_detach(dev);
366 * Free USB device and all subdevices, if any.
368 usb_free_device(udev, 0);
371 /* clear bdev variable last */
375 /*------------------------------------------------------------------------*
378 * This function is used to suspend the USB contoller.
379 *------------------------------------------------------------------------*/
381 usb_bus_suspend(struct usb_proc_msg *pm)
384 struct usb_device *udev;
387 bus = ((struct usb_bus_msg *)pm)->bus;
388 udev = bus->devices[USB_ROOT_HUB_ADDR];
390 if (udev == NULL || bus->bdev == NULL)
393 bus_generic_shutdown(bus->bdev);
395 usbd_enum_lock(udev);
397 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
399 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
402 bus->hw_power_state = 0;
406 if (bus->methods->set_hw_power != NULL)
407 (bus->methods->set_hw_power) (bus);
409 if (bus->methods->set_hw_power_sleep != NULL)
410 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
412 usbd_enum_unlock(udev);
415 /*------------------------------------------------------------------------*
418 * This function is used to resume the USB contoller.
419 *------------------------------------------------------------------------*/
421 usb_bus_resume(struct usb_proc_msg *pm)
424 struct usb_device *udev;
427 bus = ((struct usb_bus_msg *)pm)->bus;
428 udev = bus->devices[USB_ROOT_HUB_ADDR];
430 if (udev == NULL || bus->bdev == NULL)
433 usbd_enum_lock(udev);
435 DEVMETHOD(usb_take_controller, NULL); /* dummy */
437 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
440 bus->hw_power_state =
441 USB_HW_POWER_CONTROL |
443 USB_HW_POWER_INTERRUPT |
445 USB_HW_POWER_NON_ROOT_HUB;
449 if (bus->methods->set_hw_power_sleep != NULL)
450 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
452 if (bus->methods->set_hw_power != NULL)
453 (bus->methods->set_hw_power) (bus);
455 err = usbd_set_config_index(udev, 0);
457 device_printf(bus->bdev, "Could not configure root HUB\n");
459 usbd_enum_unlock(udev);
462 /*------------------------------------------------------------------------*
465 * This function is used to shutdown the USB contoller.
466 *------------------------------------------------------------------------*/
468 usb_bus_shutdown(struct usb_proc_msg *pm)
471 struct usb_device *udev;
474 bus = ((struct usb_bus_msg *)pm)->bus;
475 udev = bus->devices[USB_ROOT_HUB_ADDR];
477 if (udev == NULL || bus->bdev == NULL)
480 bus_generic_shutdown(bus->bdev);
482 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_SHUTDOWN);
499 usbd_enum_unlock(udev);
503 usb_power_wdog(void *arg)
505 struct usb_bus *bus = arg;
507 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
509 usb_callout_reset(&bus->power_wdog,
510 4 * hz, usb_power_wdog, arg);
514 * The following line of code is only here to recover from
517 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */
523 usb_bus_power_update(bus);
529 /*------------------------------------------------------------------------*
532 * This function attaches USB in context of the explore thread.
533 *------------------------------------------------------------------------*/
535 usb_bus_attach(struct usb_proc_msg *pm)
538 struct usb_device *child;
541 enum usb_dev_speed speed;
543 bus = ((struct usb_bus_msg *)pm)->bus;
548 switch (bus->usbrev) {
550 speed = USB_SPEED_FULL;
551 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
555 speed = USB_SPEED_FULL;
556 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
560 speed = USB_SPEED_HIGH;
561 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
565 speed = USB_SPEED_VARIABLE;
566 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
570 speed = USB_SPEED_SUPER;
571 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
575 device_printf(bus->bdev, "Unsupported USB revision\n");
576 usb_root_mount_rel(bus);
580 /* default power_mask value */
581 bus->hw_power_state =
582 USB_HW_POWER_CONTROL |
584 USB_HW_POWER_INTERRUPT |
586 USB_HW_POWER_NON_ROOT_HUB;
590 /* make sure power is set at least once */
592 if (bus->methods->set_hw_power != NULL) {
593 (bus->methods->set_hw_power) (bus);
596 /* allocate the Root USB device */
598 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
599 speed, USB_MODE_HOST);
601 err = usb_probe_and_attach(child,
602 USB_IFACE_INDEX_ANY);
604 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
605 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
606 err = USB_ERR_NO_ROOT_HUB;
616 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
618 usb_root_mount_rel(bus);
621 /* set softc - we are ready */
622 device_set_softc(dev, bus);
628 /*------------------------------------------------------------------------*
631 * This function creates a thread which runs the USB attach code.
632 *------------------------------------------------------------------------*/
634 usb_attach_sub(device_t dev, struct usb_bus *bus)
636 const char *pname = device_get_nameunit(dev);
639 if (usb_devclass_ptr == NULL)
640 usb_devclass_ptr = devclass_find("usbus");
646 /* Initialise USB process messages */
647 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
648 bus->explore_msg[0].bus = bus;
649 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
650 bus->explore_msg[1].bus = bus;
652 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
653 bus->detach_msg[0].bus = bus;
654 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
655 bus->detach_msg[1].bus = bus;
657 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
658 bus->attach_msg[0].bus = bus;
659 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
660 bus->attach_msg[1].bus = bus;
662 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
663 bus->suspend_msg[0].bus = bus;
664 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
665 bus->suspend_msg[1].bus = bus;
667 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
668 bus->resume_msg[0].bus = bus;
669 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
670 bus->resume_msg[1].bus = bus;
672 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
673 bus->shutdown_msg[0].bus = bus;
674 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
675 bus->shutdown_msg[1].bus = bus;
677 /* Create USB explore and callback processes */
679 if (usb_proc_create(&bus->giant_callback_proc,
680 &bus->bus_mtx, pname, USB_PRI_MED)) {
681 device_printf(dev, "WARNING: Creation of USB Giant "
682 "callback process failed.\n");
683 } else if (usb_proc_create(&bus->non_giant_callback_proc,
684 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
685 device_printf(dev, "WARNING: Creation of USB non-Giant "
686 "callback process failed.\n");
687 } else if (usb_proc_create(&bus->explore_proc,
688 &bus->bus_mtx, pname, USB_PRI_MED)) {
689 device_printf(dev, "WARNING: Creation of USB explore "
690 "process failed.\n");
691 } else if (usb_proc_create(&bus->control_xfer_proc,
692 &bus->bus_mtx, pname, USB_PRI_MED)) {
693 device_printf(dev, "WARNING: Creation of USB control transfer "
694 "process failed.\n");
696 /* Get final attach going */
698 usb_proc_msignal(&bus->explore_proc,
699 &bus->attach_msg[0], &bus->attach_msg[1]);
702 /* Do initial explore */
703 usb_needs_explore(bus, 1);
707 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
709 /*------------------------------------------------------------------------*
710 * usb_bus_mem_flush_all_cb
711 *------------------------------------------------------------------------*/
714 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
715 struct usb_page *pg, usb_size_t size, usb_size_t align)
717 usb_pc_cpu_flush(pc);
721 /*------------------------------------------------------------------------*
722 * usb_bus_mem_flush_all - factored out code
723 *------------------------------------------------------------------------*/
726 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
729 cb(bus, &usb_bus_mem_flush_all_cb);
734 /*------------------------------------------------------------------------*
735 * usb_bus_mem_alloc_all_cb
736 *------------------------------------------------------------------------*/
739 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
740 struct usb_page *pg, usb_size_t size, usb_size_t align)
742 /* need to initialize the page cache */
743 pc->tag_parent = bus->dma_parent_tag;
745 if (usb_pc_alloc_mem(pc, pg, size, align)) {
746 bus->alloc_failed = 1;
751 /*------------------------------------------------------------------------*
752 * usb_bus_mem_alloc_all - factored out code
757 *------------------------------------------------------------------------*/
759 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
760 usb_bus_mem_cb_t *cb)
762 bus->alloc_failed = 0;
764 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
765 NULL, MTX_DEF | MTX_RECURSE);
767 usb_callout_init_mtx(&bus->power_wdog,
770 TAILQ_INIT(&bus->intr_q.head);
773 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
774 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
776 if ((bus->devices_max > USB_MAX_DEVICES) ||
777 (bus->devices_max < USB_MIN_DEVICES) ||
778 (bus->devices == NULL)) {
779 DPRINTFN(0, "Devices field has not been "
780 "initialised properly\n");
781 bus->alloc_failed = 1; /* failure */
785 cb(bus, &usb_bus_mem_alloc_all_cb);
788 if (bus->alloc_failed) {
789 usb_bus_mem_free_all(bus, cb);
791 return (bus->alloc_failed);
794 /*------------------------------------------------------------------------*
795 * usb_bus_mem_free_all_cb
796 *------------------------------------------------------------------------*/
799 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
800 struct usb_page *pg, usb_size_t size, usb_size_t align)
806 /*------------------------------------------------------------------------*
807 * usb_bus_mem_free_all - factored out code
808 *------------------------------------------------------------------------*/
810 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
814 cb(bus, &usb_bus_mem_free_all_cb);
816 usb_dma_tag_unsetup(bus->dma_parent_tag);
819 mtx_destroy(&bus->bus_mtx);