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 *);
71 /* static variables */
74 static int usb_ctrl_debug = 0;
76 SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
77 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
81 static int usb_no_boot_wait = 0;
82 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
83 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
84 "No device enumerate waiting at boot.");
86 static devclass_t usb_devclass;
88 static device_method_t usb_methods[] = {
89 DEVMETHOD(device_probe, usb_probe),
90 DEVMETHOD(device_attach, usb_attach),
91 DEVMETHOD(device_detach, usb_detach),
92 DEVMETHOD(device_suspend, bus_generic_suspend),
93 DEVMETHOD(device_resume, bus_generic_resume),
94 DEVMETHOD(device_shutdown, bus_generic_shutdown),
98 static driver_t usb_driver = {
100 .methods = usb_methods,
104 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
105 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
106 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
107 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
108 DRIVER_MODULE(usbus, uss820, usb_driver, usb_devclass, 0, 0);
110 /*------------------------------------------------------------------------*
113 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
114 *------------------------------------------------------------------------*/
116 usb_probe(device_t dev)
122 /*------------------------------------------------------------------------*
124 *------------------------------------------------------------------------*/
126 usb_attach(device_t dev)
128 struct usb_bus *bus = device_get_ivars(dev);
133 DPRINTFN(0, "USB device has no ivars\n");
137 if (usb_no_boot_wait == 0) {
138 /* delay vfs_mountroot until the bus is explored */
139 bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
142 usb_attach_sub(dev, bus);
144 return (0); /* return success */
147 /*------------------------------------------------------------------------*
149 *------------------------------------------------------------------------*/
151 usb_detach(device_t dev)
153 struct usb_bus *bus = device_get_softc(dev);
158 /* was never setup properly */
161 /* Stop power watchdog */
162 usb_callout_drain(&bus->power_wdog);
164 /* Let the USB explore process detach all devices. */
165 if (bus->bus_roothold != NULL) {
166 root_mount_rel(bus->bus_roothold);
167 bus->bus_roothold = NULL;
171 if (usb_proc_msignal(&bus->explore_proc,
172 &bus->detach_msg[0], &bus->detach_msg[1])) {
175 /* Wait for detach to complete */
177 usb_proc_mwait(&bus->explore_proc,
178 &bus->detach_msg[0], &bus->detach_msg[1]);
182 /* Get rid of USB callback processes */
184 usb_proc_free(&bus->giant_callback_proc);
185 usb_proc_free(&bus->non_giant_callback_proc);
187 /* Get rid of USB explore process */
189 usb_proc_free(&bus->explore_proc);
191 /* Get rid of control transfer process */
193 usb_proc_free(&bus->control_xfer_proc);
198 /*------------------------------------------------------------------------*
201 * This function is used to explore the device tree from the root.
202 *------------------------------------------------------------------------*/
204 usb_bus_explore(struct usb_proc_msg *pm)
207 struct usb_device *udev;
209 bus = ((struct usb_bus_msg *)pm)->bus;
210 udev = bus->devices[USB_ROOT_HUB_ADDR];
212 if (udev && udev->hub) {
216 bus->driver_added_refcount++;
218 if (bus->driver_added_refcount == 0) {
219 /* avoid zero, hence that is memory default */
220 bus->driver_added_refcount = 1;
224 * The following three lines of code are only here to
227 usb_proc_rewakeup(&bus->control_xfer_proc);
228 usb_proc_rewakeup(&bus->giant_callback_proc);
229 usb_proc_rewakeup(&bus->non_giant_callback_proc);
234 * First update the USB power state!
238 /* Explore the Root USB HUB. */
239 (udev->hub->explore) (udev);
242 if (bus->bus_roothold != NULL) {
243 root_mount_rel(bus->bus_roothold);
244 bus->bus_roothold = NULL;
248 /*------------------------------------------------------------------------*
251 * This function is used to detach the device tree from the root.
252 *------------------------------------------------------------------------*/
254 usb_bus_detach(struct usb_proc_msg *pm)
257 struct usb_device *udev;
260 bus = ((struct usb_bus_msg *)pm)->bus;
261 udev = bus->devices[USB_ROOT_HUB_ADDR];
263 /* clear the softc */
264 device_set_softc(dev, NULL);
267 /* detach children first */
269 bus_generic_detach(dev);
273 * Free USB device and all subdevices, if any.
275 usb_free_device(udev, 0);
278 /* clear bdev variable last */
283 usb_power_wdog(void *arg)
285 struct usb_bus *bus = arg;
287 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
289 usb_callout_reset(&bus->power_wdog,
290 4 * hz, usb_power_wdog, arg);
293 * The following line of code is only here to recover from
296 usb_proc_rewakeup(&bus->explore_proc); /* recover from DDB */
300 usb_bus_power_update(bus);
305 /*------------------------------------------------------------------------*
308 * This function attaches USB in context of the explore thread.
309 *------------------------------------------------------------------------*/
311 usb_bus_attach(struct usb_proc_msg *pm)
314 struct usb_device *child;
317 enum usb_dev_speed speed;
319 bus = ((struct usb_bus_msg *)pm)->bus;
324 switch (bus->usbrev) {
326 speed = USB_SPEED_FULL;
327 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
331 speed = USB_SPEED_FULL;
332 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
336 speed = USB_SPEED_HIGH;
337 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
341 speed = USB_SPEED_VARIABLE;
342 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
346 device_printf(bus->bdev, "Unsupported USB revision!\n");
352 /* default power_mask value */
353 bus->hw_power_state =
354 USB_HW_POWER_CONTROL |
356 USB_HW_POWER_INTERRUPT |
358 USB_HW_POWER_NON_ROOT_HUB;
360 /* make sure power is set at least once */
362 if (bus->methods->set_hw_power != NULL) {
363 (bus->methods->set_hw_power) (bus);
366 /* Allocate the Root USB device */
368 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
369 speed, USB_MODE_HOST);
371 err = usb_probe_and_attach(child,
372 USB_IFACE_INDEX_ANY);
374 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
375 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
376 err = USB_ERR_NO_ROOT_HUB;
386 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
390 /* set softc - we are ready */
391 device_set_softc(dev, bus);
397 /*------------------------------------------------------------------------*
400 * This function creates a thread which runs the USB attach code.
401 *------------------------------------------------------------------------*/
403 usb_attach_sub(device_t dev, struct usb_bus *bus)
405 const char *pname = device_get_nameunit(dev);
408 if (usb_devclass_ptr == NULL)
409 usb_devclass_ptr = devclass_find("usbus");
412 /* Initialise USB process messages */
413 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
414 bus->explore_msg[0].bus = bus;
415 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
416 bus->explore_msg[1].bus = bus;
418 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
419 bus->detach_msg[0].bus = bus;
420 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
421 bus->detach_msg[1].bus = bus;
423 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
424 bus->attach_msg[0].bus = bus;
425 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
426 bus->attach_msg[1].bus = bus;
428 /* Create USB explore and callback processes */
430 if (usb_proc_create(&bus->giant_callback_proc,
431 &bus->bus_mtx, pname, USB_PRI_MED)) {
432 printf("WARNING: Creation of USB Giant "
433 "callback process failed.\n");
434 } else if (usb_proc_create(&bus->non_giant_callback_proc,
435 &bus->bus_mtx, pname, USB_PRI_HIGH)) {
436 printf("WARNING: Creation of USB non-Giant "
437 "callback process failed.\n");
438 } else if (usb_proc_create(&bus->explore_proc,
439 &bus->bus_mtx, pname, USB_PRI_MED)) {
440 printf("WARNING: Creation of USB explore "
441 "process failed.\n");
442 } else if (usb_proc_create(&bus->control_xfer_proc,
443 &bus->bus_mtx, pname, USB_PRI_MED)) {
444 printf("WARNING: Creation of USB control transfer "
445 "process failed.\n");
447 /* Get final attach going */
449 if (usb_proc_msignal(&bus->explore_proc,
450 &bus->attach_msg[0], &bus->attach_msg[1])) {
455 /* Do initial explore */
456 usb_needs_explore(bus, 1);
460 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
462 /*------------------------------------------------------------------------*
463 * usb_bus_mem_flush_all_cb
464 *------------------------------------------------------------------------*/
467 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
468 struct usb_page *pg, usb_size_t size, usb_size_t align)
470 usb_pc_cpu_flush(pc);
474 /*------------------------------------------------------------------------*
475 * usb_bus_mem_flush_all - factored out code
476 *------------------------------------------------------------------------*/
479 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
482 cb(bus, &usb_bus_mem_flush_all_cb);
487 /*------------------------------------------------------------------------*
488 * usb_bus_mem_alloc_all_cb
489 *------------------------------------------------------------------------*/
492 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
493 struct usb_page *pg, usb_size_t size, usb_size_t align)
495 /* need to initialize the page cache */
496 pc->tag_parent = bus->dma_parent_tag;
498 if (usb_pc_alloc_mem(pc, pg, size, align)) {
499 bus->alloc_failed = 1;
504 /*------------------------------------------------------------------------*
505 * usb_bus_mem_alloc_all - factored out code
510 *------------------------------------------------------------------------*/
512 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
513 usb_bus_mem_cb_t *cb)
515 bus->alloc_failed = 0;
517 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
518 NULL, MTX_DEF | MTX_RECURSE);
520 usb_callout_init_mtx(&bus->power_wdog,
523 TAILQ_INIT(&bus->intr_q.head);
526 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
527 dmat, &bus->bus_mtx, NULL, 32, USB_BUS_DMA_TAG_MAX);
529 if ((bus->devices_max > USB_MAX_DEVICES) ||
530 (bus->devices_max < USB_MIN_DEVICES) ||
531 (bus->devices == NULL)) {
532 DPRINTFN(0, "Devices field has not been "
533 "initialised properly!\n");
534 bus->alloc_failed = 1; /* failure */
538 cb(bus, &usb_bus_mem_alloc_all_cb);
541 if (bus->alloc_failed) {
542 usb_bus_mem_free_all(bus, cb);
544 return (bus->alloc_failed);
547 /*------------------------------------------------------------------------*
548 * usb_bus_mem_free_all_cb
549 *------------------------------------------------------------------------*/
552 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
553 struct usb_page *pg, usb_size_t size, usb_size_t align)
559 /*------------------------------------------------------------------------*
560 * usb_bus_mem_free_all - factored out code
561 *------------------------------------------------------------------------*/
563 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
567 cb(bus, &usb_bus_mem_free_all_cb);
569 usb_dma_tag_unsetup(bus->dma_parent_tag);
572 mtx_destroy(&bus->bus_mtx);