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>
65 /* function prototypes */
67 static device_probe_t usb_probe;
68 static device_attach_t usb_attach;
69 static device_detach_t usb_detach;
71 static void usb_attach_sub(device_t, struct usb_bus *);
73 /* static variables */
76 static int usb_ctrl_debug = 0;
78 SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
79 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
83 static int usb_no_boot_wait = 0;
84 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
85 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
86 "No device enumerate waiting at boot.");
88 static devclass_t usb_devclass;
90 static device_method_t usb_methods[] = {
91 DEVMETHOD(device_probe, usb_probe),
92 DEVMETHOD(device_attach, usb_attach),
93 DEVMETHOD(device_detach, usb_detach),
94 DEVMETHOD(device_suspend, bus_generic_suspend),
95 DEVMETHOD(device_resume, bus_generic_resume),
96 DEVMETHOD(device_shutdown, bus_generic_shutdown),
100 static driver_t usb_driver = {
102 .methods = usb_methods,
106 /* Host Only Drivers */
107 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
108 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
109 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
110 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
112 /* Device Only Drivers */
113 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
114 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
115 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
117 /*------------------------------------------------------------------------*
120 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
121 *------------------------------------------------------------------------*/
123 usb_probe(device_t dev)
130 usb_root_mount_rel(struct usb_bus *bus)
132 if (bus->bus_roothold != NULL) {
133 DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
134 root_mount_rel(bus->bus_roothold);
135 bus->bus_roothold = NULL;
139 /*------------------------------------------------------------------------*
141 *------------------------------------------------------------------------*/
143 usb_attach(device_t dev)
145 struct usb_bus *bus = device_get_ivars(dev);
150 device_printf(dev, "USB device has no ivars\n");
154 if (usb_no_boot_wait == 0) {
155 /* delay vfs_mountroot until the bus is explored */
156 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
159 usb_attach_sub(dev, bus);
161 return (0); /* return success */
164 /*------------------------------------------------------------------------*
166 *------------------------------------------------------------------------*/
168 usb_detach(device_t dev)
170 struct usb_bus *bus = device_get_softc(dev);
175 /* was never setup properly */
178 /* Stop power watchdog */
179 usb_callout_drain(&bus->power_wdog);
181 /* Let the USB explore process detach all devices. */
182 usb_root_mount_rel(bus);
185 if (usb_proc_msignal(&bus->explore_proc,
186 &bus->detach_msg[0], &bus->detach_msg[1])) {
189 /* Wait for detach to complete */
191 usb_proc_mwait(&bus->explore_proc,
192 &bus->detach_msg[0], &bus->detach_msg[1]);
196 /* Get rid of USB callback processes */
198 usb_proc_free(&bus->giant_callback_proc);
199 usb_proc_free(&bus->non_giant_callback_proc);
201 /* Get rid of USB explore process */
203 usb_proc_free(&bus->explore_proc);
205 /* Get rid of control transfer process */
207 usb_proc_free(&bus->control_xfer_proc);
215 /*------------------------------------------------------------------------*
218 * This function is used to explore the device tree from the root.
219 *------------------------------------------------------------------------*/
221 usb_bus_explore(struct usb_proc_msg *pm)
224 struct usb_device *udev;
226 bus = ((struct usb_bus_msg *)pm)->bus;
227 udev = bus->devices[USB_ROOT_HUB_ADDR];
229 if (udev && udev->hub) {
233 bus->driver_added_refcount++;
235 if (bus->driver_added_refcount == 0) {
236 /* avoid zero, hence that is memory default */
237 bus->driver_added_refcount = 1;
242 * The following three lines of code are only here to
245 usb_proc_rewakeup(&bus->control_xfer_proc);
246 usb_proc_rewakeup(&bus->giant_callback_proc);
247 usb_proc_rewakeup(&bus->non_giant_callback_proc);
254 * First update the USB power state!
258 /* Explore the Root USB HUB. */
259 (udev->hub->explore) (udev);
262 usb_root_mount_rel(bus);
265 /*------------------------------------------------------------------------*
268 * This function is used to detach the device tree from the root.
269 *------------------------------------------------------------------------*/
271 usb_bus_detach(struct usb_proc_msg *pm)
274 struct usb_device *udev;
277 bus = ((struct usb_bus_msg *)pm)->bus;
278 udev = bus->devices[USB_ROOT_HUB_ADDR];
280 /* clear the softc */
281 device_set_softc(dev, NULL);
284 /* detach children first */
286 bus_generic_detach(dev);
290 * Free USB device and all subdevices, if any.
292 usb_free_device(udev, 0);
295 /* clear bdev variable last */
300 usb_power_wdog(void *arg)
302 struct usb_bus *bus = arg;
304 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
306 usb_callout_reset(&bus->power_wdog,
307 4 * hz, usb_power_wdog, arg);
311 * The following line of code is only here to recover from
314 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */
320 usb_bus_power_update(bus);
326 /*------------------------------------------------------------------------*
329 * This function attaches USB in context of the explore thread.
330 *------------------------------------------------------------------------*/
332 usb_bus_attach(struct usb_proc_msg *pm)
335 struct usb_device *child;
338 enum usb_dev_speed speed;
340 bus = ((struct usb_bus_msg *)pm)->bus;
345 switch (bus->usbrev) {
347 speed = USB_SPEED_FULL;
348 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
352 speed = USB_SPEED_FULL;
353 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
357 speed = USB_SPEED_HIGH;
358 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
362 speed = USB_SPEED_VARIABLE;
363 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
367 speed = USB_SPEED_SUPER;
368 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
372 device_printf(bus->bdev, "Unsupported USB revision\n");
373 usb_root_mount_rel(bus);
379 /* default power_mask value */
380 bus->hw_power_state =
381 USB_HW_POWER_CONTROL |
383 USB_HW_POWER_INTERRUPT |
385 USB_HW_POWER_NON_ROOT_HUB;
387 /* make sure power is set at least once */
389 if (bus->methods->set_hw_power != NULL) {
390 (bus->methods->set_hw_power) (bus);
393 /* Allocate the Root USB device */
395 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
396 speed, USB_MODE_HOST);
398 err = usb_probe_and_attach(child,
399 USB_IFACE_INDEX_ANY);
401 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
402 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
403 err = USB_ERR_NO_ROOT_HUB;
413 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
415 usb_root_mount_rel(bus);
418 /* set softc - we are ready */
419 device_set_softc(dev, bus);
425 /*------------------------------------------------------------------------*
428 * This function creates a thread which runs the USB attach code.
429 *------------------------------------------------------------------------*/
431 usb_attach_sub(device_t dev, struct usb_bus *bus)
433 const char *pname = device_get_nameunit(dev);
436 if (usb_devclass_ptr == NULL)
437 usb_devclass_ptr = devclass_find("usbus");
443 /* Initialise USB process messages */
444 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
445 bus->explore_msg[0].bus = bus;
446 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
447 bus->explore_msg[1].bus = bus;
449 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
450 bus->detach_msg[0].bus = bus;
451 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
452 bus->detach_msg[1].bus = bus;
454 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
455 bus->attach_msg[0].bus = bus;
456 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
457 bus->attach_msg[1].bus = bus;
459 /* Create USB explore and callback processes */
461 if (usb_proc_create(&bus->giant_callback_proc,
462 &bus->bus_mtx, pname, USB_PRI_MED)) {
463 device_printf(dev, "WARNING: Creation of USB Giant "
464 "callback process failed.\n");
465 } else if (usb_proc_create(&bus->non_giant_callback_proc,
466 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
467 device_printf(dev, "WARNING: Creation of USB non-Giant "
468 "callback process failed.\n");
469 } else if (usb_proc_create(&bus->explore_proc,
470 &bus->bus_mtx, pname, USB_PRI_MED)) {
471 device_printf(dev, "WARNING: Creation of USB explore "
472 "process failed.\n");
473 } else if (usb_proc_create(&bus->control_xfer_proc,
474 &bus->bus_mtx, pname, USB_PRI_MED)) {
475 device_printf(dev, "WARNING: Creation of USB control transfer "
476 "process failed.\n");
478 /* Get final attach going */
480 if (usb_proc_msignal(&bus->explore_proc,
481 &bus->attach_msg[0], &bus->attach_msg[1])) {
486 /* Do initial explore */
487 usb_needs_explore(bus, 1);
491 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
493 /*------------------------------------------------------------------------*
494 * usb_bus_mem_flush_all_cb
495 *------------------------------------------------------------------------*/
498 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
499 struct usb_page *pg, usb_size_t size, usb_size_t align)
501 usb_pc_cpu_flush(pc);
505 /*------------------------------------------------------------------------*
506 * usb_bus_mem_flush_all - factored out code
507 *------------------------------------------------------------------------*/
510 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
513 cb(bus, &usb_bus_mem_flush_all_cb);
518 /*------------------------------------------------------------------------*
519 * usb_bus_mem_alloc_all_cb
520 *------------------------------------------------------------------------*/
523 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
524 struct usb_page *pg, usb_size_t size, usb_size_t align)
526 /* need to initialize the page cache */
527 pc->tag_parent = bus->dma_parent_tag;
529 if (usb_pc_alloc_mem(pc, pg, size, align)) {
530 bus->alloc_failed = 1;
535 /*------------------------------------------------------------------------*
536 * usb_bus_mem_alloc_all - factored out code
541 *------------------------------------------------------------------------*/
543 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
544 usb_bus_mem_cb_t *cb)
546 bus->alloc_failed = 0;
548 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
549 NULL, MTX_DEF | MTX_RECURSE);
551 usb_callout_init_mtx(&bus->power_wdog,
554 TAILQ_INIT(&bus->intr_q.head);
557 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
558 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
560 if ((bus->devices_max > USB_MAX_DEVICES) ||
561 (bus->devices_max < USB_MIN_DEVICES) ||
562 (bus->devices == NULL)) {
563 DPRINTFN(0, "Devices field has not been "
564 "initialised properly\n");
565 bus->alloc_failed = 1; /* failure */
569 cb(bus, &usb_bus_mem_alloc_all_cb);
572 if (bus->alloc_failed) {
573 usb_bus_mem_free_all(bus, cb);
575 return (bus->alloc_failed);
578 /*------------------------------------------------------------------------*
579 * usb_bus_mem_free_all_cb
580 *------------------------------------------------------------------------*/
583 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
584 struct usb_page *pg, usb_size_t size, usb_size_t align)
590 /*------------------------------------------------------------------------*
591 * usb_bus_mem_free_all - factored out code
592 *------------------------------------------------------------------------*/
594 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
598 cb(bus, &usb_bus_mem_free_all_cb);
600 usb_dma_tag_unsetup(bus->dma_parent_tag);
603 mtx_destroy(&bus->bus_mtx);