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
27 #include <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/linker_set.h>
36 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
42 #include <sys/unistd.h>
43 #include <sys/callout.h>
44 #include <sys/malloc.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
50 #define USB_DEBUG_VAR usb_ctrl_debug
52 #include <dev/usb/usb_core.h>
53 #include <dev/usb/usb_debug.h>
54 #include <dev/usb/usb_process.h>
55 #include <dev/usb/usb_busdma.h>
56 #include <dev/usb/usb_dynamic.h>
57 #include <dev/usb/usb_device.h>
58 #include <dev/usb/usb_hub.h>
60 #include <dev/usb/usb_controller.h>
61 #include <dev/usb/usb_bus.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;
69 static void usb_attach_sub(device_t, struct usb_bus *);
70 static void usb_post_init(void *);
72 /* static variables */
75 static int usb_ctrl_debug = 0;
77 SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
78 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
82 static int usb_no_boot_wait = 0;
83 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
84 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
85 "No device enumerate waiting at boot.");
87 static uint8_t usb_post_init_called = 0;
89 static devclass_t usb_devclass;
91 static device_method_t usb_methods[] = {
92 DEVMETHOD(device_probe, usb_probe),
93 DEVMETHOD(device_attach, usb_attach),
94 DEVMETHOD(device_detach, usb_detach),
95 DEVMETHOD(device_suspend, bus_generic_suspend),
96 DEVMETHOD(device_resume, bus_generic_resume),
97 DEVMETHOD(device_shutdown, bus_generic_shutdown),
101 static driver_t usb_driver = {
103 .methods = usb_methods,
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, at91_udp, usb_driver, usb_devclass, 0, 0);
111 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
113 /*------------------------------------------------------------------------*
116 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
117 *------------------------------------------------------------------------*/
119 usb_probe(device_t dev)
125 /*------------------------------------------------------------------------*
127 *------------------------------------------------------------------------*/
129 usb_attach(device_t dev)
131 struct usb_bus *bus = device_get_ivars(dev);
136 DPRINTFN(0, "USB device has no ivars\n");
140 if (usb_no_boot_wait == 0) {
141 /* delay vfs_mountroot until the bus is explored */
142 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
145 if (usb_post_init_called) {
146 usb_attach_sub(dev, bus);
147 usb_needs_explore(bus, 1);
149 return (0); /* return success */
152 /*------------------------------------------------------------------------*
154 *------------------------------------------------------------------------*/
156 usb_detach(device_t dev)
158 struct usb_bus *bus = device_get_softc(dev);
163 /* was never setup properly */
166 /* Stop power watchdog */
167 usb_callout_drain(&bus->power_wdog);
169 /* Let the USB explore process detach all devices. */
170 if (bus->bus_roothold != NULL) {
171 root_mount_rel(bus->bus_roothold);
172 bus->bus_roothold = NULL;
176 if (usb_proc_msignal(&bus->explore_proc,
177 &bus->detach_msg[0], &bus->detach_msg[1])) {
180 /* Wait for detach to complete */
182 usb_proc_mwait(&bus->explore_proc,
183 &bus->detach_msg[0], &bus->detach_msg[1]);
187 /* Get rid of USB callback processes */
189 usb_proc_free(&bus->giant_callback_proc);
190 usb_proc_free(&bus->non_giant_callback_proc);
192 /* Get rid of USB explore process */
194 usb_proc_free(&bus->explore_proc);
196 /* Get rid of control transfer process */
198 usb_proc_free(&bus->control_xfer_proc);
203 /*------------------------------------------------------------------------*
206 * This function is used to explore the device tree from the root.
207 *------------------------------------------------------------------------*/
209 usb_bus_explore(struct usb_proc_msg *pm)
212 struct usb_device *udev;
214 bus = ((struct usb_bus_msg *)pm)->bus;
215 udev = bus->devices[USB_ROOT_HUB_ADDR];
217 if (udev && udev->hub) {
221 bus->driver_added_refcount++;
223 if (bus->driver_added_refcount == 0) {
224 /* avoid zero, hence that is memory default */
225 bus->driver_added_refcount = 1;
230 * First update the USB power state!
234 /* Explore the Root USB HUB. */
235 (udev->hub->explore) (udev);
238 if (bus->bus_roothold != NULL) {
239 root_mount_rel(bus->bus_roothold);
240 bus->bus_roothold = NULL;
244 /*------------------------------------------------------------------------*
247 * This function is used to detach the device tree from the root.
248 *------------------------------------------------------------------------*/
250 usb_bus_detach(struct usb_proc_msg *pm)
253 struct usb_device *udev;
256 bus = ((struct usb_bus_msg *)pm)->bus;
257 udev = bus->devices[USB_ROOT_HUB_ADDR];
259 /* clear the softc */
260 device_set_softc(dev, NULL);
265 /* detach children first */
266 bus_generic_detach(dev);
269 * Free USB Root device, but not any sub-devices, hence they
270 * are freed by the caller of this function:
272 usb_free_device(udev,
273 USB_UNCFG_FLAG_FREE_EP0);
277 /* clear bdev variable last */
282 usb_power_wdog(void *arg)
284 struct usb_bus *bus = arg;
286 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
288 usb_callout_reset(&bus->power_wdog,
289 4 * hz, usb_power_wdog, arg);
293 usb_bus_power_update(bus);
298 /*------------------------------------------------------------------------*
301 * This function attaches USB in context of the explore thread.
302 *------------------------------------------------------------------------*/
304 usb_bus_attach(struct usb_proc_msg *pm)
307 struct usb_device *child;
310 enum usb_dev_speed speed;
312 bus = ((struct usb_bus_msg *)pm)->bus;
317 switch (bus->usbrev) {
319 speed = USB_SPEED_FULL;
320 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
324 speed = USB_SPEED_FULL;
325 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
329 speed = USB_SPEED_HIGH;
330 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
334 speed = USB_SPEED_VARIABLE;
335 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
339 device_printf(bus->bdev, "Unsupported USB revision!\n");
346 /* default power_mask value */
347 bus->hw_power_state =
348 USB_HW_POWER_CONTROL |
350 USB_HW_POWER_INTERRUPT |
352 USB_HW_POWER_NON_ROOT_HUB;
354 /* make sure power is set at least once */
356 if (bus->methods->set_hw_power != NULL) {
357 (bus->methods->set_hw_power) (bus);
360 /* Allocate the Root USB device */
362 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
363 speed, USB_MODE_HOST);
365 err = usb_probe_and_attach(child,
366 USB_IFACE_INDEX_ANY);
368 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
369 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
370 err = USB_ERR_NO_ROOT_HUB;
381 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
385 /* set softc - we are ready */
386 device_set_softc(dev, bus);
392 /*------------------------------------------------------------------------*
395 * This function creates a thread which runs the USB attach code. It
396 * is factored out, hence it can be called at two different places in
397 * time. During bootup this function is called from
398 * "usb_post_init". During hot-plug it is called directly from the
399 * "usb_attach()" method.
400 *------------------------------------------------------------------------*/
402 usb_attach_sub(device_t dev, struct usb_bus *bus)
404 const char *pname = device_get_nameunit(dev);
406 /* Initialise USB process messages */
407 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
408 bus->explore_msg[0].bus = bus;
409 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
410 bus->explore_msg[1].bus = bus;
412 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
413 bus->detach_msg[0].bus = bus;
414 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
415 bus->detach_msg[1].bus = bus;
417 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
418 bus->attach_msg[0].bus = bus;
419 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
420 bus->attach_msg[1].bus = bus;
422 /* Create USB explore and callback processes */
424 if (usb_proc_create(&bus->giant_callback_proc,
425 &bus->bus_mtx, pname, USB_PRI_MED)) {
426 printf("WARNING: Creation of USB Giant "
427 "callback process failed.\n");
428 } else if (usb_proc_create(&bus->non_giant_callback_proc,
429 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
430 printf("WARNING: Creation of USB non-Giant "
431 "callback process failed.\n");
432 } else if (usb_proc_create(&bus->explore_proc,
433 &bus->bus_mtx, pname, USB_PRI_MED)) {
434 printf("WARNING: Creation of USB explore "
435 "process failed.\n");
436 } else if (usb_proc_create(&bus->control_xfer_proc,
437 &bus->bus_mtx, pname, USB_PRI_MED)) {
438 printf("WARNING: Creation of USB control transfer "
439 "process failed.\n");
441 /* Get final attach going */
443 if (usb_proc_msignal(&bus->explore_proc,
444 &bus->attach_msg[0], &bus->attach_msg[1])) {
451 /*------------------------------------------------------------------------*
454 * This function is called to attach all USB busses that were found
456 *------------------------------------------------------------------------*/
458 usb_post_init(void *arg)
468 usb_devclass_ptr = devclass_find("usbus");
470 dc = usb_devclass_ptr;
472 max = devclass_get_maxunit(dc) + 1;
473 for (n = 0; n != max; n++) {
474 dev = devclass_get_device(dc, n);
475 if (dev && device_is_attached(dev)) {
476 bus = device_get_ivars(dev);
478 usb_attach_sub(dev, bus);
482 DPRINTFN(0, "no devclass\n");
484 usb_post_init_called = 1;
486 /* explore all USB busses in parallell */
488 usb_needs_explore_all();
493 SYSINIT(usb_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_post_init, NULL);
494 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
496 /*------------------------------------------------------------------------*
497 * usb_bus_mem_flush_all_cb
498 *------------------------------------------------------------------------*/
501 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
502 struct usb_page *pg, usb_size_t size, usb_size_t align)
504 usb_pc_cpu_flush(pc);
508 /*------------------------------------------------------------------------*
509 * usb_bus_mem_flush_all - factored out code
510 *------------------------------------------------------------------------*/
513 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
516 cb(bus, &usb_bus_mem_flush_all_cb);
521 /*------------------------------------------------------------------------*
522 * usb_bus_mem_alloc_all_cb
523 *------------------------------------------------------------------------*/
526 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
527 struct usb_page *pg, usb_size_t size, usb_size_t align)
529 /* need to initialize the page cache */
530 pc->tag_parent = bus->dma_parent_tag;
532 if (usb_pc_alloc_mem(pc, pg, size, align)) {
533 bus->alloc_failed = 1;
538 /*------------------------------------------------------------------------*
539 * usb_bus_mem_alloc_all - factored out code
544 *------------------------------------------------------------------------*/
546 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
547 usb_bus_mem_cb_t *cb)
549 bus->alloc_failed = 0;
551 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
552 NULL, MTX_DEF | MTX_RECURSE);
554 usb_callout_init_mtx(&bus->power_wdog,
557 TAILQ_INIT(&bus->intr_q.head);
560 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
561 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
563 if ((bus->devices_max > USB_MAX_DEVICES) ||
564 (bus->devices_max < USB_MIN_DEVICES) ||
565 (bus->devices == NULL)) {
566 DPRINTFN(0, "Devices field has not been "
567 "initialised properly!\n");
568 bus->alloc_failed = 1; /* failure */
572 cb(bus, &usb_bus_mem_alloc_all_cb);
575 if (bus->alloc_failed) {
576 usb_bus_mem_free_all(bus, cb);
578 return (bus->alloc_failed);
581 /*------------------------------------------------------------------------*
582 * usb_bus_mem_free_all_cb
583 *------------------------------------------------------------------------*/
586 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
587 struct usb_page *pg, usb_size_t size, usb_size_t align)
593 /*------------------------------------------------------------------------*
594 * usb_bus_mem_free_all - factored out code
595 *------------------------------------------------------------------------*/
597 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
601 cb(bus, &usb_bus_mem_free_all_cb);
603 usb_dma_tag_unsetup(bus->dma_parent_tag);
606 mtx_destroy(&bus->bus_mtx);