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) {
147 usb_attach_sub(dev, bus);
149 usb_needs_explore(bus, 1);
151 return (0); /* return success */
154 /*------------------------------------------------------------------------*
156 *------------------------------------------------------------------------*/
158 usb_detach(device_t dev)
160 struct usb_bus *bus = device_get_softc(dev);
165 /* was never setup properly */
168 /* Stop power watchdog */
169 usb_callout_drain(&bus->power_wdog);
171 /* Let the USB explore process detach all devices. */
172 if (bus->bus_roothold != NULL) {
173 root_mount_rel(bus->bus_roothold);
174 bus->bus_roothold = NULL;
178 if (usb_proc_msignal(&bus->explore_proc,
179 &bus->detach_msg[0], &bus->detach_msg[1])) {
182 /* Wait for detach to complete */
184 usb_proc_mwait(&bus->explore_proc,
185 &bus->detach_msg[0], &bus->detach_msg[1]);
189 /* Get rid of USB callback processes */
191 usb_proc_free(&bus->giant_callback_proc);
192 usb_proc_free(&bus->non_giant_callback_proc);
194 /* Get rid of USB explore process */
196 usb_proc_free(&bus->explore_proc);
198 /* Get rid of control transfer process */
200 usb_proc_free(&bus->control_xfer_proc);
205 /*------------------------------------------------------------------------*
208 * This function is used to explore the device tree from the root.
209 *------------------------------------------------------------------------*/
211 usb_bus_explore(struct usb_proc_msg *pm)
214 struct usb_device *udev;
216 bus = ((struct usb_bus_msg *)pm)->bus;
217 udev = bus->devices[USB_ROOT_HUB_ADDR];
219 if (udev && udev->hub) {
223 bus->driver_added_refcount++;
225 if (bus->driver_added_refcount == 0) {
226 /* avoid zero, hence that is memory default */
227 bus->driver_added_refcount = 1;
234 * First update the USB power state!
238 * Explore the Root USB HUB. This call can sleep,
239 * exiting Giant, which is actually Giant.
241 (udev->hub->explore) (udev);
247 if (bus->bus_roothold != NULL) {
248 root_mount_rel(bus->bus_roothold);
249 bus->bus_roothold = NULL;
253 /*------------------------------------------------------------------------*
256 * This function is used to detach the device tree from the root.
257 *------------------------------------------------------------------------*/
259 usb_bus_detach(struct usb_proc_msg *pm)
262 struct usb_device *udev;
265 bus = ((struct usb_bus_msg *)pm)->bus;
266 udev = bus->devices[USB_ROOT_HUB_ADDR];
268 /* clear the softc */
269 device_set_softc(dev, NULL);
274 /* detach children first */
275 bus_generic_detach(dev);
278 * Free USB Root device, but not any sub-devices, hence they
279 * are freed by the caller of this function:
281 usb_free_device(udev,
282 USB_UNCFG_FLAG_FREE_EP0);
286 /* clear bdev variable last */
291 usb_power_wdog(void *arg)
293 struct usb_bus *bus = arg;
295 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
297 usb_callout_reset(&bus->power_wdog,
298 4 * hz, usb_power_wdog, arg);
302 usb_bus_power_update(bus);
307 /*------------------------------------------------------------------------*
310 * This function attaches USB in context of the explore thread.
311 *------------------------------------------------------------------------*/
313 usb_bus_attach(struct usb_proc_msg *pm)
316 struct usb_device *child;
319 enum usb_dev_speed speed;
321 bus = ((struct usb_bus_msg *)pm)->bus;
326 switch (bus->usbrev) {
328 speed = USB_SPEED_FULL;
329 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
333 speed = USB_SPEED_FULL;
334 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
338 speed = USB_SPEED_HIGH;
339 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
343 speed = USB_SPEED_VARIABLE;
344 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
348 device_printf(bus->bdev, "Unsupported USB revision!\n");
353 mtx_lock(&Giant); /* XXX not required by USB */
355 /* default power_mask value */
356 bus->hw_power_state =
357 USB_HW_POWER_CONTROL |
359 USB_HW_POWER_INTERRUPT |
361 USB_HW_POWER_NON_ROOT_HUB;
363 /* make sure power is set at least once */
365 if (bus->methods->set_hw_power != NULL) {
366 (bus->methods->set_hw_power) (bus);
369 /* Allocate the Root USB device */
371 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
372 speed, USB_MODE_HOST);
374 err = usb_probe_and_attach(child,
375 USB_IFACE_INDEX_ANY);
377 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
378 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
379 err = USB_ERR_NO_ROOT_HUB;
390 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
394 /* set softc - we are ready */
395 device_set_softc(dev, bus);
401 /*------------------------------------------------------------------------*
404 * This function creates a thread which runs the USB attach code. It
405 * is factored out, hence it can be called at two different places in
406 * time. During bootup this function is called from
407 * "usb_post_init". During hot-plug it is called directly from the
408 * "usb_attach()" method.
409 *------------------------------------------------------------------------*/
411 usb_attach_sub(device_t dev, struct usb_bus *bus)
413 const char *pname = device_get_nameunit(dev);
415 /* Initialise USB process messages */
416 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
417 bus->explore_msg[0].bus = bus;
418 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
419 bus->explore_msg[1].bus = bus;
421 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
422 bus->detach_msg[0].bus = bus;
423 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
424 bus->detach_msg[1].bus = bus;
426 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
427 bus->attach_msg[0].bus = bus;
428 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
429 bus->attach_msg[1].bus = bus;
431 /* Create USB explore and callback processes */
433 if (usb_proc_create(&bus->giant_callback_proc,
434 &bus->bus_mtx, pname, USB_PRI_MED)) {
435 printf("WARNING: Creation of USB Giant "
436 "callback process failed.\n");
437 } else if (usb_proc_create(&bus->non_giant_callback_proc,
438 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
439 printf("WARNING: Creation of USB non-Giant "
440 "callback process failed.\n");
441 } else if (usb_proc_create(&bus->explore_proc,
442 &bus->bus_mtx, pname, USB_PRI_MED)) {
443 printf("WARNING: Creation of USB explore "
444 "process failed.\n");
445 } else if (usb_proc_create(&bus->control_xfer_proc,
446 &bus->bus_mtx, pname, USB_PRI_MED)) {
447 printf("WARNING: Creation of USB control transfer "
448 "process failed.\n");
450 /* Get final attach going */
452 if (usb_proc_msignal(&bus->explore_proc,
453 &bus->attach_msg[0], &bus->attach_msg[1])) {
460 /*------------------------------------------------------------------------*
463 * This function is called to attach all USB busses that were found
465 *------------------------------------------------------------------------*/
467 usb_post_init(void *arg)
477 usb_devclass_ptr = devclass_find("usbus");
479 dc = usb_devclass_ptr;
481 max = devclass_get_maxunit(dc) + 1;
482 for (n = 0; n != max; n++) {
483 dev = devclass_get_device(dc, n);
484 if (dev && device_is_attached(dev)) {
485 bus = device_get_ivars(dev);
488 usb_attach_sub(dev, bus);
494 DPRINTFN(0, "no devclass\n");
496 usb_post_init_called = 1;
498 /* explore all USB busses in parallell */
500 usb_needs_explore_all();
505 SYSINIT(usb_post_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_ANY, usb_post_init, NULL);
506 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
508 /*------------------------------------------------------------------------*
509 * usb_bus_mem_flush_all_cb
510 *------------------------------------------------------------------------*/
513 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
514 struct usb_page *pg, usb_size_t size, usb_size_t align)
516 usb_pc_cpu_flush(pc);
520 /*------------------------------------------------------------------------*
521 * usb_bus_mem_flush_all - factored out code
522 *------------------------------------------------------------------------*/
525 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
528 cb(bus, &usb_bus_mem_flush_all_cb);
533 /*------------------------------------------------------------------------*
534 * usb_bus_mem_alloc_all_cb
535 *------------------------------------------------------------------------*/
538 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
539 struct usb_page *pg, usb_size_t size, usb_size_t align)
541 /* need to initialize the page cache */
542 pc->tag_parent = bus->dma_parent_tag;
544 if (usb_pc_alloc_mem(pc, pg, size, align)) {
545 bus->alloc_failed = 1;
550 /*------------------------------------------------------------------------*
551 * usb_bus_mem_alloc_all - factored out code
556 *------------------------------------------------------------------------*/
558 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
559 usb_bus_mem_cb_t *cb)
561 bus->alloc_failed = 0;
563 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
564 NULL, MTX_DEF | MTX_RECURSE);
566 usb_callout_init_mtx(&bus->power_wdog,
569 TAILQ_INIT(&bus->intr_q.head);
572 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
573 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
575 if ((bus->devices_max > USB_MAX_DEVICES) ||
576 (bus->devices_max < USB_MIN_DEVICES) ||
577 (bus->devices == NULL)) {
578 DPRINTFN(0, "Devices field has not been "
579 "initialised properly!\n");
580 bus->alloc_failed = 1; /* failure */
584 cb(bus, &usb_bus_mem_alloc_all_cb);
587 if (bus->alloc_failed) {
588 usb_bus_mem_free_all(bus, cb);
590 return (bus->alloc_failed);
593 /*------------------------------------------------------------------------*
594 * usb_bus_mem_free_all_cb
595 *------------------------------------------------------------------------*/
598 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
599 struct usb_page *pg, usb_size_t size, usb_size_t align)
605 /*------------------------------------------------------------------------*
606 * usb_bus_mem_free_all - factored out code
607 *------------------------------------------------------------------------*/
609 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
613 cb(bus, &usb_bus_mem_free_all_cb);
615 usb_dma_tag_unsetup(bus->dma_parent_tag);
618 mtx_destroy(&bus->bus_mtx);