HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / kern / subr_bus.c
blob83ead30cd1c8da525104ae42588cadf6dbb8d0c9
1 /*
2 * Copyright (c) 1997,1998 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
27 * $DragonFly: src/sys/kern/subr_bus.c,v 1.41 2008/01/05 13:30:33 corecode Exp $
30 #include "opt_bus.h"
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #ifdef DEVICE_SYSCTLS
38 #include <sys/sysctl.h>
39 #endif
40 #include <sys/kobj.h>
41 #include <sys/bus_private.h>
42 #include <sys/systm.h>
43 #include <sys/bus.h>
44 #include <sys/rman.h>
46 #include <machine/stdarg.h> /* for device_printf() */
48 #include <sys/thread2.h>
50 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
52 #ifdef BUS_DEBUG
53 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
54 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
55 #define DRIVERNAME(d) ((d)? d->name : "no driver")
56 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
58 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
59 * prevent syslog from deleting initial spaces
61 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
63 static void print_device_short(device_t dev, int indent);
64 static void print_device(device_t dev, int indent);
65 void print_device_tree_short(device_t dev, int indent);
66 void print_device_tree(device_t dev, int indent);
67 static void print_driver_short(driver_t *driver, int indent);
68 static void print_driver(driver_t *driver, int indent);
69 static void print_driver_list(driver_list_t drivers, int indent);
70 static void print_devclass_short(devclass_t dc, int indent);
71 static void print_devclass(devclass_t dc, int indent);
72 void print_devclass_list_short(void);
73 void print_devclass_list(void);
75 #else
76 /* Make the compiler ignore the function calls */
77 #define PDEBUG(a) /* nop */
78 #define DEVICENAME(d) /* nop */
79 #define DRIVERNAME(d) /* nop */
80 #define DEVCLANAME(d) /* nop */
82 #define print_device_short(d,i) /* nop */
83 #define print_device(d,i) /* nop */
84 #define print_device_tree_short(d,i) /* nop */
85 #define print_device_tree(d,i) /* nop */
86 #define print_driver_short(d,i) /* nop */
87 #define print_driver(d,i) /* nop */
88 #define print_driver_list(d,i) /* nop */
89 #define print_devclass_short(d,i) /* nop */
90 #define print_devclass(d,i) /* nop */
91 #define print_devclass_list_short() /* nop */
92 #define print_devclass_list() /* nop */
93 #endif
95 #ifdef DEVICE_SYSCTLS
96 static void device_register_oids(device_t dev);
97 static void device_unregister_oids(device_t dev);
98 #endif
99 static void device_attach_async(device_t dev);
100 static void device_attach_thread(void *arg);
101 static int device_doattach(device_t dev);
103 static int do_async_attach = 0;
104 static int numasyncthreads;
105 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
107 kobj_method_t null_methods[] = {
108 { 0, 0 }
111 DEFINE_CLASS(null, null_methods, 0);
114 * Devclass implementation
117 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
119 static devclass_t
120 devclass_find_internal(const char *classname, const char *parentname,
121 int create)
123 devclass_t dc;
125 PDEBUG(("looking for %s", classname));
126 if (classname == NULL)
127 return(NULL);
129 TAILQ_FOREACH(dc, &devclasses, link)
130 if (!strcmp(dc->name, classname))
131 break;
133 if (create && !dc) {
134 PDEBUG(("creating %s", classname));
135 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
136 M_BUS, M_INTWAIT | M_ZERO);
137 if (!dc)
138 return(NULL);
139 dc->parent = NULL;
140 dc->name = (char*) (dc + 1);
141 strcpy(dc->name, classname);
142 dc->devices = NULL;
143 dc->maxunit = 0;
144 TAILQ_INIT(&dc->drivers);
145 TAILQ_INSERT_TAIL(&devclasses, dc, link);
147 if (parentname && dc && !dc->parent)
148 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
150 return(dc);
153 devclass_t
154 devclass_create(const char *classname)
156 return(devclass_find_internal(classname, NULL, TRUE));
159 devclass_t
160 devclass_find(const char *classname)
162 return(devclass_find_internal(classname, NULL, FALSE));
165 device_t
166 devclass_find_unit(const char *classname, int unit)
168 devclass_t dc;
170 if ((dc = devclass_find(classname)) != NULL)
171 return(devclass_get_device(dc, unit));
172 return (NULL);
176 devclass_add_driver(devclass_t dc, driver_t *driver)
178 driverlink_t dl;
179 device_t dev;
180 int i;
182 PDEBUG(("%s", DRIVERNAME(driver)));
184 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
185 if (!dl)
186 return(ENOMEM);
189 * Compile the driver's methods. Also increase the reference count
190 * so that the class doesn't get freed when the last instance
191 * goes. This means we can safely use static methods and avoids a
192 * double-free in devclass_delete_driver.
194 kobj_class_instantiate(driver);
197 * Make sure the devclass which the driver is implementing exists.
199 devclass_find_internal(driver->name, NULL, TRUE);
201 dl->driver = driver;
202 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
205 * Call BUS_DRIVER_ADDED for any existing busses in this class,
206 * but only if the bus has already been attached (otherwise we
207 * might probe too early).
209 * This is what will cause a newly loaded module to be associated
210 * with hardware. bus_generic_driver_added() is typically what ends
211 * up being called.
213 for (i = 0; i < dc->maxunit; i++) {
214 if ((dev = dc->devices[i]) != NULL) {
215 if (dev->state >= DS_ATTACHED)
216 BUS_DRIVER_ADDED(dev, driver);
220 return(0);
224 devclass_delete_driver(devclass_t busclass, driver_t *driver)
226 devclass_t dc = devclass_find(driver->name);
227 driverlink_t dl;
228 device_t dev;
229 int i;
230 int error;
232 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
234 if (!dc)
235 return(0);
238 * Find the link structure in the bus' list of drivers.
240 TAILQ_FOREACH(dl, &busclass->drivers, link)
241 if (dl->driver == driver)
242 break;
244 if (!dl) {
245 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
246 return(ENOENT);
250 * Disassociate from any devices. We iterate through all the
251 * devices in the devclass of the driver and detach any which are
252 * using the driver and which have a parent in the devclass which
253 * we are deleting from.
255 * Note that since a driver can be in multiple devclasses, we
256 * should not detach devices which are not children of devices in
257 * the affected devclass.
259 for (i = 0; i < dc->maxunit; i++)
260 if (dc->devices[i]) {
261 dev = dc->devices[i];
262 if (dev->driver == driver && dev->parent &&
263 dev->parent->devclass == busclass) {
264 if ((error = device_detach(dev)) != 0)
265 return(error);
266 device_set_driver(dev, NULL);
270 TAILQ_REMOVE(&busclass->drivers, dl, link);
271 kfree(dl, M_BUS);
273 kobj_class_uninstantiate(driver);
275 return(0);
278 static driverlink_t
279 devclass_find_driver_internal(devclass_t dc, const char *classname)
281 driverlink_t dl;
283 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
285 TAILQ_FOREACH(dl, &dc->drivers, link)
286 if (!strcmp(dl->driver->name, classname))
287 return(dl);
289 PDEBUG(("not found"));
290 return(NULL);
293 kobj_class_t
294 devclass_find_driver(devclass_t dc, const char *classname)
296 driverlink_t dl;
298 dl = devclass_find_driver_internal(dc, classname);
299 if (dl)
300 return(dl->driver);
301 else
302 return(NULL);
305 const char *
306 devclass_get_name(devclass_t dc)
308 return(dc->name);
311 device_t
312 devclass_get_device(devclass_t dc, int unit)
314 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
315 return(NULL);
316 return(dc->devices[unit]);
319 void *
320 devclass_get_softc(devclass_t dc, int unit)
322 device_t dev;
324 dev = devclass_get_device(dc, unit);
325 if (!dev)
326 return(NULL);
328 return(device_get_softc(dev));
332 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
334 int i;
335 int count;
336 device_t *list;
338 count = 0;
339 for (i = 0; i < dc->maxunit; i++)
340 if (dc->devices[i])
341 count++;
343 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
344 if (list == NULL)
345 return(ENOMEM);
347 count = 0;
348 for (i = 0; i < dc->maxunit; i++)
349 if (dc->devices[i]) {
350 list[count] = dc->devices[i];
351 count++;
354 *devlistp = list;
355 *devcountp = count;
357 return(0);
361 devclass_get_maxunit(devclass_t dc)
363 return(dc->maxunit);
366 void
367 devclass_set_parent(devclass_t dc, devclass_t pdc)
369 dc->parent = pdc;
372 devclass_t
373 devclass_get_parent(devclass_t dc)
375 return(dc->parent);
378 static int
379 devclass_alloc_unit(devclass_t dc, int *unitp)
381 int unit = *unitp;
383 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
385 /* If we have been given a wired unit number, check for existing device */
386 if (unit != -1) {
387 if (unit >= 0 && unit < dc->maxunit &&
388 dc->devices[unit] != NULL) {
389 if (bootverbose)
390 kprintf("%s-: %s%d exists, using next available unit number\n",
391 dc->name, dc->name, unit);
392 /* find the next available slot */
393 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
396 } else {
397 /* Unwired device, find the next available slot for it */
398 unit = 0;
399 while (unit < dc->maxunit && dc->devices[unit] != NULL)
400 unit++;
404 * We've selected a unit beyond the length of the table, so let's
405 * extend the table to make room for all units up to and including
406 * this one.
408 if (unit >= dc->maxunit) {
409 device_t *newlist;
410 int newsize;
412 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
413 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
414 M_INTWAIT | M_ZERO);
415 if (newlist == NULL)
416 return(ENOMEM);
417 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
418 if (dc->devices)
419 kfree(dc->devices, M_BUS);
420 dc->devices = newlist;
421 dc->maxunit = newsize;
423 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
425 *unitp = unit;
426 return(0);
429 static int
430 devclass_add_device(devclass_t dc, device_t dev)
432 int buflen, error;
434 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
436 buflen = strlen(dc->name) + 5;
437 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
438 if (!dev->nameunit)
439 return(ENOMEM);
441 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
442 kfree(dev->nameunit, M_BUS);
443 dev->nameunit = NULL;
444 return(error);
446 dc->devices[dev->unit] = dev;
447 dev->devclass = dc;
448 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
450 #ifdef DEVICE_SYSCTLS
451 device_register_oids(dev);
452 #endif
454 return(0);
457 static int
458 devclass_delete_device(devclass_t dc, device_t dev)
460 if (!dc || !dev)
461 return(0);
463 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
465 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
466 panic("devclass_delete_device: inconsistent device class");
467 dc->devices[dev->unit] = NULL;
468 if (dev->flags & DF_WILDCARD)
469 dev->unit = -1;
470 dev->devclass = NULL;
471 kfree(dev->nameunit, M_BUS);
472 dev->nameunit = NULL;
474 #ifdef DEVICE_SYSCTLS
475 device_unregister_oids(dev);
476 #endif
478 return(0);
481 static device_t
482 make_device(device_t parent, const char *name, int unit)
484 device_t dev;
485 devclass_t dc;
487 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
489 if (name != NULL) {
490 dc = devclass_find_internal(name, NULL, TRUE);
491 if (!dc) {
492 kprintf("make_device: can't find device class %s\n", name);
493 return(NULL);
495 } else
496 dc = NULL;
498 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
499 if (!dev)
500 return(0);
502 dev->parent = parent;
503 TAILQ_INIT(&dev->children);
504 kobj_init((kobj_t) dev, &null_class);
505 dev->driver = NULL;
506 dev->devclass = NULL;
507 dev->unit = unit;
508 dev->nameunit = NULL;
509 dev->desc = NULL;
510 dev->busy = 0;
511 dev->devflags = 0;
512 dev->flags = DF_ENABLED;
513 dev->order = 0;
514 if (unit == -1)
515 dev->flags |= DF_WILDCARD;
516 if (name) {
517 dev->flags |= DF_FIXEDCLASS;
518 if (devclass_add_device(dc, dev) != 0) {
519 kobj_delete((kobj_t)dev, M_BUS);
520 return(NULL);
523 dev->ivars = NULL;
524 dev->softc = NULL;
526 dev->state = DS_NOTPRESENT;
528 return(dev);
531 static int
532 device_print_child(device_t dev, device_t child)
534 int retval = 0;
536 if (device_is_alive(child))
537 retval += BUS_PRINT_CHILD(dev, child);
538 else
539 retval += device_printf(child, " not found\n");
541 return(retval);
544 device_t
545 device_add_child(device_t dev, const char *name, int unit)
547 return device_add_child_ordered(dev, 0, name, unit);
550 device_t
551 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
553 device_t child;
554 device_t place;
556 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
557 order, unit));
559 child = make_device(dev, name, unit);
560 if (child == NULL)
561 return child;
562 child->order = order;
564 TAILQ_FOREACH(place, &dev->children, link)
565 if (place->order > order)
566 break;
568 if (place) {
570 * The device 'place' is the first device whose order is
571 * greater than the new child.
573 TAILQ_INSERT_BEFORE(place, child, link);
574 } else {
576 * The new child's order is greater or equal to the order of
577 * any existing device. Add the child to the tail of the list.
579 TAILQ_INSERT_TAIL(&dev->children, child, link);
582 return(child);
586 device_delete_child(device_t dev, device_t child)
588 int error;
589 device_t grandchild;
591 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
593 /* remove children first */
594 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
595 error = device_delete_child(child, grandchild);
596 if (error)
597 return(error);
600 if ((error = device_detach(child)) != 0)
601 return(error);
602 if (child->devclass)
603 devclass_delete_device(child->devclass, child);
604 TAILQ_REMOVE(&dev->children, child, link);
605 device_set_desc(child, NULL);
606 kobj_delete((kobj_t)child, M_BUS);
608 return(0);
612 * Find only devices attached to this bus.
614 device_t
615 device_find_child(device_t dev, const char *classname, int unit)
617 devclass_t dc;
618 device_t child;
620 dc = devclass_find(classname);
621 if (!dc)
622 return(NULL);
624 child = devclass_get_device(dc, unit);
625 if (child && child->parent == dev)
626 return(child);
627 return(NULL);
630 static driverlink_t
631 first_matching_driver(devclass_t dc, device_t dev)
633 if (dev->devclass)
634 return(devclass_find_driver_internal(dc, dev->devclass->name));
635 else
636 return(TAILQ_FIRST(&dc->drivers));
639 static driverlink_t
640 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
642 if (dev->devclass) {
643 driverlink_t dl;
644 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
645 if (!strcmp(dev->devclass->name, dl->driver->name))
646 return(dl);
647 return(NULL);
648 } else
649 return(TAILQ_NEXT(last, link));
652 static int
653 device_probe_child(device_t dev, device_t child)
655 devclass_t dc;
656 driverlink_t best = 0;
657 driverlink_t dl;
658 int result, pri = 0;
659 int hasclass = (child->devclass != 0);
661 dc = dev->devclass;
662 if (!dc)
663 panic("device_probe_child: parent device has no devclass");
665 if (child->state == DS_ALIVE)
666 return(0);
668 for (; dc; dc = dc->parent) {
669 for (dl = first_matching_driver(dc, child); dl;
670 dl = next_matching_driver(dc, child, dl)) {
671 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
672 device_set_driver(child, dl->driver);
673 if (!hasclass)
674 device_set_devclass(child, dl->driver->name);
675 result = DEVICE_PROBE(child);
676 if (!hasclass)
677 device_set_devclass(child, 0);
680 * If the driver returns SUCCESS, there can be
681 * no higher match for this device.
683 if (result == 0) {
684 best = dl;
685 pri = 0;
686 break;
690 * The driver returned an error so it
691 * certainly doesn't match.
693 if (result > 0) {
694 device_set_driver(child, 0);
695 continue;
699 * A priority lower than SUCCESS, remember the
700 * best matching driver. Initialise the value
701 * of pri for the first match.
703 if (best == 0 || result > pri) {
704 best = dl;
705 pri = result;
706 continue;
710 * If we have unambiguous match in this devclass,
711 * don't look in the parent.
713 if (best && pri == 0)
714 break;
718 * If we found a driver, change state and initialise the devclass.
720 if (best) {
721 if (!child->devclass)
722 device_set_devclass(child, best->driver->name);
723 device_set_driver(child, best->driver);
724 if (pri < 0) {
726 * A bit bogus. Call the probe method again to make
727 * sure that we have the right description.
729 DEVICE_PROBE(child);
731 child->state = DS_ALIVE;
732 return(0);
735 return(ENXIO);
738 device_t
739 device_get_parent(device_t dev)
741 return dev->parent;
745 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
747 int count;
748 device_t child;
749 device_t *list;
751 count = 0;
752 TAILQ_FOREACH(child, &dev->children, link)
753 count++;
755 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
756 if (!list)
757 return(ENOMEM);
759 count = 0;
760 TAILQ_FOREACH(child, &dev->children, link) {
761 list[count] = child;
762 count++;
765 *devlistp = list;
766 *devcountp = count;
768 return(0);
771 driver_t *
772 device_get_driver(device_t dev)
774 return(dev->driver);
777 devclass_t
778 device_get_devclass(device_t dev)
780 return(dev->devclass);
783 const char *
784 device_get_name(device_t dev)
786 if (dev->devclass)
787 return devclass_get_name(dev->devclass);
788 return(NULL);
791 const char *
792 device_get_nameunit(device_t dev)
794 return(dev->nameunit);
798 device_get_unit(device_t dev)
800 return(dev->unit);
803 const char *
804 device_get_desc(device_t dev)
806 return(dev->desc);
809 uint32_t
810 device_get_flags(device_t dev)
812 return(dev->devflags);
816 device_print_prettyname(device_t dev)
818 const char *name = device_get_name(dev);
820 if (name == 0)
821 return kprintf("unknown: ");
822 else
823 return kprintf("%s%d: ", name, device_get_unit(dev));
827 device_printf(device_t dev, const char * fmt, ...)
829 __va_list ap;
830 int retval;
832 retval = device_print_prettyname(dev);
833 __va_start(ap, fmt);
834 retval += kvprintf(fmt, ap);
835 __va_end(ap);
836 return retval;
839 static void
840 device_set_desc_internal(device_t dev, const char* desc, int copy)
842 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
843 kfree(dev->desc, M_BUS);
844 dev->flags &= ~DF_DESCMALLOCED;
845 dev->desc = NULL;
848 if (copy && desc) {
849 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
850 if (dev->desc) {
851 strcpy(dev->desc, desc);
852 dev->flags |= DF_DESCMALLOCED;
854 } else
855 /* Avoid a -Wcast-qual warning */
856 dev->desc = (char *)(uintptr_t) desc;
858 #ifdef DEVICE_SYSCTLS
860 struct sysctl_oid *oid = &dev->oid[1];
861 oid->oid_arg1 = dev->desc ? dev->desc : "";
862 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
864 #endif
867 void
868 device_set_desc(device_t dev, const char* desc)
870 device_set_desc_internal(dev, desc, FALSE);
873 void
874 device_set_desc_copy(device_t dev, const char* desc)
876 device_set_desc_internal(dev, desc, TRUE);
879 void
880 device_set_flags(device_t dev, uint32_t flags)
882 dev->devflags = flags;
885 void *
886 device_get_softc(device_t dev)
888 return dev->softc;
891 void
892 device_set_softc(device_t dev, void *softc)
894 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
895 kfree(dev->softc, M_BUS);
896 dev->softc = softc;
897 if (dev->softc)
898 dev->flags |= DF_EXTERNALSOFTC;
899 else
900 dev->flags &= ~DF_EXTERNALSOFTC;
903 void
904 device_set_async_attach(device_t dev, int enable)
906 if (enable)
907 dev->flags |= DF_ASYNCPROBE;
908 else
909 dev->flags &= ~DF_ASYNCPROBE;
912 void *
913 device_get_ivars(device_t dev)
915 return dev->ivars;
918 void
919 device_set_ivars(device_t dev, void * ivars)
921 if (!dev)
922 return;
924 dev->ivars = ivars;
927 device_state_t
928 device_get_state(device_t dev)
930 return(dev->state);
933 void
934 device_enable(device_t dev)
936 dev->flags |= DF_ENABLED;
939 void
940 device_disable(device_t dev)
942 dev->flags &= ~DF_ENABLED;
946 * YYY cannot block
948 void
949 device_busy(device_t dev)
951 if (dev->state < DS_ATTACHED)
952 panic("device_busy: called for unattached device");
953 if (dev->busy == 0 && dev->parent)
954 device_busy(dev->parent);
955 dev->busy++;
956 dev->state = DS_BUSY;
960 * YYY cannot block
962 void
963 device_unbusy(device_t dev)
965 if (dev->state != DS_BUSY)
966 panic("device_unbusy: called for non-busy device");
967 dev->busy--;
968 if (dev->busy == 0) {
969 if (dev->parent)
970 device_unbusy(dev->parent);
971 dev->state = DS_ATTACHED;
975 void
976 device_quiet(device_t dev)
978 dev->flags |= DF_QUIET;
981 void
982 device_verbose(device_t dev)
984 dev->flags &= ~DF_QUIET;
988 device_is_quiet(device_t dev)
990 return((dev->flags & DF_QUIET) != 0);
994 device_is_enabled(device_t dev)
996 return((dev->flags & DF_ENABLED) != 0);
1000 device_is_alive(device_t dev)
1002 return(dev->state >= DS_ALIVE);
1006 device_is_attached(device_t dev)
1008 return(dev->state >= DS_ATTACHED);
1012 device_set_devclass(device_t dev, const char *classname)
1014 devclass_t dc;
1016 if (!classname) {
1017 if (dev->devclass)
1018 devclass_delete_device(dev->devclass, dev);
1019 return(0);
1022 if (dev->devclass) {
1023 kprintf("device_set_devclass: device class already set\n");
1024 return(EINVAL);
1027 dc = devclass_find_internal(classname, NULL, TRUE);
1028 if (!dc)
1029 return(ENOMEM);
1031 return(devclass_add_device(dc, dev));
1035 device_set_driver(device_t dev, driver_t *driver)
1037 if (dev->state >= DS_ATTACHED)
1038 return(EBUSY);
1040 if (dev->driver == driver)
1041 return(0);
1043 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1044 kfree(dev->softc, M_BUS);
1045 dev->softc = NULL;
1047 kobj_delete((kobj_t) dev, 0);
1048 dev->driver = driver;
1049 if (driver) {
1050 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1051 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1052 dev->softc = kmalloc(driver->size, M_BUS,
1053 M_INTWAIT | M_ZERO);
1054 if (!dev->softc) {
1055 kobj_delete((kobj_t)dev, 0);
1056 kobj_init((kobj_t) dev, &null_class);
1057 dev->driver = NULL;
1058 return(ENOMEM);
1061 } else
1062 kobj_init((kobj_t) dev, &null_class);
1063 return(0);
1067 device_probe_and_attach(device_t dev)
1069 device_t bus = dev->parent;
1070 int error = 0;
1072 if (dev->state >= DS_ALIVE)
1073 return(0);
1075 if ((dev->flags & DF_ENABLED) == 0) {
1076 if (bootverbose) {
1077 device_print_prettyname(dev);
1078 kprintf("not probed (disabled)\n");
1080 return(0);
1083 error = device_probe_child(bus, dev);
1084 if (error) {
1085 if (!(dev->flags & DF_DONENOMATCH)) {
1086 BUS_PROBE_NOMATCH(bus, dev);
1087 dev->flags |= DF_DONENOMATCH;
1089 return(error);
1093 * Output the exact device chain prior to the attach in case the
1094 * system locks up during attach, and generate the full info after
1095 * the attach so correct irq and other information is displayed.
1097 if (bootverbose && !device_is_quiet(dev)) {
1098 device_t tmp;
1100 kprintf("%s", device_get_nameunit(dev));
1101 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1102 kprintf(".%s", device_get_nameunit(tmp));
1103 kprintf("\n");
1105 if (!device_is_quiet(dev))
1106 device_print_child(bus, dev);
1107 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1108 kprintf("%s: probing asynchronously\n",
1109 device_get_nameunit(dev));
1110 dev->state = DS_INPROGRESS;
1111 device_attach_async(dev);
1112 error = 0;
1113 } else {
1114 error = device_doattach(dev);
1116 return(error);
1120 * Device is known to be alive, do the attach asynchronously.
1122 * The MP lock is held by all threads.
1124 static void
1125 device_attach_async(device_t dev)
1127 thread_t td;
1129 atomic_add_int(&numasyncthreads, 1);
1130 lwkt_create(device_attach_thread, dev, &td, NULL,
1131 0, 0, (dev->desc ? dev->desc : "devattach"));
1134 static void
1135 device_attach_thread(void *arg)
1137 device_t dev = arg;
1139 (void)device_doattach(dev);
1140 atomic_subtract_int(&numasyncthreads, 1);
1141 wakeup(&numasyncthreads);
1145 * Device is known to be alive, do the attach (synchronous or asynchronous)
1147 static int
1148 device_doattach(device_t dev)
1150 device_t bus = dev->parent;
1151 int hasclass = (dev->devclass != 0);
1152 int error;
1154 error = DEVICE_ATTACH(dev);
1155 if (error == 0) {
1156 dev->state = DS_ATTACHED;
1157 if (bootverbose && !device_is_quiet(dev))
1158 device_print_child(bus, dev);
1159 } else {
1160 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1161 dev->driver->name, dev->unit, error);
1162 /* Unset the class that was set in device_probe_child */
1163 if (!hasclass)
1164 device_set_devclass(dev, 0);
1165 device_set_driver(dev, NULL);
1166 dev->state = DS_NOTPRESENT;
1168 return(error);
1172 device_detach(device_t dev)
1174 int error;
1176 PDEBUG(("%s", DEVICENAME(dev)));
1177 if (dev->state == DS_BUSY)
1178 return(EBUSY);
1179 if (dev->state != DS_ATTACHED)
1180 return(0);
1182 if ((error = DEVICE_DETACH(dev)) != 0)
1183 return(error);
1184 device_printf(dev, "detached\n");
1185 if (dev->parent)
1186 BUS_CHILD_DETACHED(dev->parent, dev);
1188 if (!(dev->flags & DF_FIXEDCLASS))
1189 devclass_delete_device(dev->devclass, dev);
1191 dev->state = DS_NOTPRESENT;
1192 device_set_driver(dev, NULL);
1194 return(0);
1198 device_shutdown(device_t dev)
1200 if (dev->state < DS_ATTACHED)
1201 return 0;
1202 PDEBUG(("%s", DEVICENAME(dev)));
1203 return DEVICE_SHUTDOWN(dev);
1207 device_set_unit(device_t dev, int unit)
1209 devclass_t dc;
1210 int err;
1212 dc = device_get_devclass(dev);
1213 if (unit < dc->maxunit && dc->devices[unit])
1214 return(EBUSY);
1215 err = devclass_delete_device(dc, dev);
1216 if (err)
1217 return(err);
1218 dev->unit = unit;
1219 err = devclass_add_device(dc, dev);
1220 return(err);
1223 #ifdef DEVICE_SYSCTLS
1226 * Sysctl nodes for devices.
1229 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1231 static int
1232 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1234 device_t dev = arg1;
1235 device_t child;
1236 int first = 1, error = 0;
1238 TAILQ_FOREACH(child, &dev->children, link)
1239 if (child->nameunit) {
1240 if (!first) {
1241 error = SYSCTL_OUT(req, ",", 1);
1242 if (error)
1243 return error;
1244 } else
1245 first = 0;
1246 error = SYSCTL_OUT(req, child->nameunit,
1247 strlen(child->nameunit));
1248 if (error)
1249 return(error);
1252 error = SYSCTL_OUT(req, "", 1);
1254 return(error);
1257 static int
1258 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1260 device_t dev = arg1;
1262 switch (dev->state) {
1263 case DS_NOTPRESENT:
1264 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1265 case DS_ALIVE:
1266 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1267 case DS_INPROGRESS:
1268 return SYSCTL_OUT(req, "in-progress", sizeof("in-progress"));
1269 case DS_ATTACHED:
1270 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1271 case DS_BUSY:
1272 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1273 default:
1274 return (0);
1278 static void
1279 device_register_oids(device_t dev)
1281 struct sysctl_oid* oid;
1283 oid = &dev->oid[0];
1284 bzero(oid, sizeof(*oid));
1285 oid->oid_parent = &sysctl__hw_devices_children;
1286 oid->oid_number = OID_AUTO;
1287 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1288 oid->oid_arg1 = &dev->oidlist[0];
1289 oid->oid_arg2 = 0;
1290 oid->oid_name = dev->nameunit;
1291 oid->oid_handler = 0;
1292 oid->oid_fmt = "N";
1293 SLIST_INIT(&dev->oidlist[0]);
1294 sysctl_register_oid(oid);
1296 oid = &dev->oid[1];
1297 bzero(oid, sizeof(*oid));
1298 oid->oid_parent = &dev->oidlist[0];
1299 oid->oid_number = OID_AUTO;
1300 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1301 oid->oid_arg1 = dev->desc ? dev->desc : "";
1302 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1303 oid->oid_name = "desc";
1304 oid->oid_handler = sysctl_handle_string;
1305 oid->oid_fmt = "A";
1306 sysctl_register_oid(oid);
1308 oid = &dev->oid[2];
1309 bzero(oid, sizeof(*oid));
1310 oid->oid_parent = &dev->oidlist[0];
1311 oid->oid_number = OID_AUTO;
1312 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1313 oid->oid_arg1 = dev;
1314 oid->oid_arg2 = 0;
1315 oid->oid_name = "children";
1316 oid->oid_handler = sysctl_handle_children;
1317 oid->oid_fmt = "A";
1318 sysctl_register_oid(oid);
1320 oid = &dev->oid[3];
1321 bzero(oid, sizeof(*oid));
1322 oid->oid_parent = &dev->oidlist[0];
1323 oid->oid_number = OID_AUTO;
1324 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1325 oid->oid_arg1 = dev;
1326 oid->oid_arg2 = 0;
1327 oid->oid_name = "state";
1328 oid->oid_handler = sysctl_handle_state;
1329 oid->oid_fmt = "A";
1330 sysctl_register_oid(oid);
1333 static void
1334 device_unregister_oids(device_t dev)
1336 sysctl_unregister_oid(&dev->oid[0]);
1337 sysctl_unregister_oid(&dev->oid[1]);
1338 sysctl_unregister_oid(&dev->oid[2]);
1341 #endif
1343 /*======================================*/
1345 * Access functions for device resources.
1348 /* Supplied by config(8) in ioconf.c */
1349 extern struct config_device config_devtab[];
1350 extern int devtab_count;
1352 /* Runtime version */
1353 struct config_device *devtab = config_devtab;
1355 static int
1356 resource_new_name(const char *name, int unit)
1358 struct config_device *new;
1360 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1361 M_INTWAIT | M_ZERO);
1362 if (new == NULL)
1363 return(-1);
1364 if (devtab && devtab_count > 0)
1365 bcopy(devtab, new, devtab_count * sizeof(*new));
1366 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1367 if (new[devtab_count].name == NULL) {
1368 kfree(new, M_TEMP);
1369 return(-1);
1371 strcpy(new[devtab_count].name, name);
1372 new[devtab_count].unit = unit;
1373 new[devtab_count].resource_count = 0;
1374 new[devtab_count].resources = NULL;
1375 if (devtab && devtab != config_devtab)
1376 kfree(devtab, M_TEMP);
1377 devtab = new;
1378 return devtab_count++;
1381 static int
1382 resource_new_resname(int j, const char *resname, resource_type type)
1384 struct config_resource *new;
1385 int i;
1387 i = devtab[j].resource_count;
1388 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1389 if (new == NULL)
1390 return(-1);
1391 if (devtab[j].resources && i > 0)
1392 bcopy(devtab[j].resources, new, i * sizeof(*new));
1393 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1394 if (new[i].name == NULL) {
1395 kfree(new, M_TEMP);
1396 return(-1);
1398 strcpy(new[i].name, resname);
1399 new[i].type = type;
1400 if (devtab[j].resources)
1401 kfree(devtab[j].resources, M_TEMP);
1402 devtab[j].resources = new;
1403 devtab[j].resource_count = i + 1;
1404 return(i);
1407 static int
1408 resource_match_string(int i, const char *resname, const char *value)
1410 int j;
1411 struct config_resource *res;
1413 for (j = 0, res = devtab[i].resources;
1414 j < devtab[i].resource_count; j++, res++)
1415 if (!strcmp(res->name, resname)
1416 && res->type == RES_STRING
1417 && !strcmp(res->u.stringval, value))
1418 return(j);
1419 return(-1);
1422 static int
1423 resource_find(const char *name, int unit, const char *resname,
1424 struct config_resource **result)
1426 int i, j;
1427 struct config_resource *res;
1430 * First check specific instances, then generic.
1432 for (i = 0; i < devtab_count; i++) {
1433 if (devtab[i].unit < 0)
1434 continue;
1435 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1436 res = devtab[i].resources;
1437 for (j = 0; j < devtab[i].resource_count; j++, res++)
1438 if (!strcmp(res->name, resname)) {
1439 *result = res;
1440 return(0);
1444 for (i = 0; i < devtab_count; i++) {
1445 if (devtab[i].unit >= 0)
1446 continue;
1447 /* XXX should this `&& devtab[i].unit == unit' be here? */
1448 /* XXX if so, then the generic match does nothing */
1449 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1450 res = devtab[i].resources;
1451 for (j = 0; j < devtab[i].resource_count; j++, res++)
1452 if (!strcmp(res->name, resname)) {
1453 *result = res;
1454 return(0);
1458 return(ENOENT);
1462 resource_int_value(const char *name, int unit, const char *resname, int *result)
1464 int error;
1465 struct config_resource *res;
1467 if ((error = resource_find(name, unit, resname, &res)) != 0)
1468 return(error);
1469 if (res->type != RES_INT)
1470 return(EFTYPE);
1471 *result = res->u.intval;
1472 return(0);
1476 resource_long_value(const char *name, int unit, const char *resname,
1477 long *result)
1479 int error;
1480 struct config_resource *res;
1482 if ((error = resource_find(name, unit, resname, &res)) != 0)
1483 return(error);
1484 if (res->type != RES_LONG)
1485 return(EFTYPE);
1486 *result = res->u.longval;
1487 return(0);
1491 resource_string_value(const char *name, int unit, const char *resname,
1492 char **result)
1494 int error;
1495 struct config_resource *res;
1497 if ((error = resource_find(name, unit, resname, &res)) != 0)
1498 return(error);
1499 if (res->type != RES_STRING)
1500 return(EFTYPE);
1501 *result = res->u.stringval;
1502 return(0);
1506 resource_query_string(int i, const char *resname, const char *value)
1508 if (i < 0)
1509 i = 0;
1510 else
1511 i = i + 1;
1512 for (; i < devtab_count; i++)
1513 if (resource_match_string(i, resname, value) >= 0)
1514 return(i);
1515 return(-1);
1519 resource_locate(int i, const char *resname)
1521 if (i < 0)
1522 i = 0;
1523 else
1524 i = i + 1;
1525 for (; i < devtab_count; i++)
1526 if (!strcmp(devtab[i].name, resname))
1527 return(i);
1528 return(-1);
1532 resource_count(void)
1534 return(devtab_count);
1537 char *
1538 resource_query_name(int i)
1540 return(devtab[i].name);
1544 resource_query_unit(int i)
1546 return(devtab[i].unit);
1549 static int
1550 resource_create(const char *name, int unit, const char *resname,
1551 resource_type type, struct config_resource **result)
1553 int i, j;
1554 struct config_resource *res = NULL;
1556 for (i = 0; i < devtab_count; i++)
1557 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1558 res = devtab[i].resources;
1559 break;
1561 if (res == NULL) {
1562 i = resource_new_name(name, unit);
1563 if (i < 0)
1564 return(ENOMEM);
1565 res = devtab[i].resources;
1567 for (j = 0; j < devtab[i].resource_count; j++, res++)
1568 if (!strcmp(res->name, resname)) {
1569 *result = res;
1570 return(0);
1572 j = resource_new_resname(i, resname, type);
1573 if (j < 0)
1574 return(ENOMEM);
1575 res = &devtab[i].resources[j];
1576 *result = res;
1577 return(0);
1581 resource_set_int(const char *name, int unit, const char *resname, int value)
1583 int error;
1584 struct config_resource *res;
1586 error = resource_create(name, unit, resname, RES_INT, &res);
1587 if (error)
1588 return(error);
1589 if (res->type != RES_INT)
1590 return(EFTYPE);
1591 res->u.intval = value;
1592 return(0);
1596 resource_set_long(const char *name, int unit, const char *resname, long value)
1598 int error;
1599 struct config_resource *res;
1601 error = resource_create(name, unit, resname, RES_LONG, &res);
1602 if (error)
1603 return(error);
1604 if (res->type != RES_LONG)
1605 return(EFTYPE);
1606 res->u.longval = value;
1607 return(0);
1611 resource_set_string(const char *name, int unit, const char *resname,
1612 const char *value)
1614 int error;
1615 struct config_resource *res;
1617 error = resource_create(name, unit, resname, RES_STRING, &res);
1618 if (error)
1619 return(error);
1620 if (res->type != RES_STRING)
1621 return(EFTYPE);
1622 if (res->u.stringval)
1623 kfree(res->u.stringval, M_TEMP);
1624 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
1625 if (res->u.stringval == NULL)
1626 return(ENOMEM);
1627 strcpy(res->u.stringval, value);
1628 return(0);
1631 static void
1632 resource_cfgload(void *dummy __unused)
1634 struct config_resource *res, *cfgres;
1635 int i, j;
1636 int error;
1637 char *name, *resname;
1638 int unit;
1639 resource_type type;
1640 char *stringval;
1641 int config_devtab_count;
1643 config_devtab_count = devtab_count;
1644 devtab = NULL;
1645 devtab_count = 0;
1647 for (i = 0; i < config_devtab_count; i++) {
1648 name = config_devtab[i].name;
1649 unit = config_devtab[i].unit;
1651 for (j = 0; j < config_devtab[i].resource_count; j++) {
1652 cfgres = config_devtab[i].resources;
1653 resname = cfgres[j].name;
1654 type = cfgres[j].type;
1655 error = resource_create(name, unit, resname, type,
1656 &res);
1657 if (error) {
1658 kprintf("create resource %s%d: error %d\n",
1659 name, unit, error);
1660 continue;
1662 if (res->type != type) {
1663 kprintf("type mismatch %s%d: %d != %d\n",
1664 name, unit, res->type, type);
1665 continue;
1667 switch (type) {
1668 case RES_INT:
1669 res->u.intval = cfgres[j].u.intval;
1670 break;
1671 case RES_LONG:
1672 res->u.longval = cfgres[j].u.longval;
1673 break;
1674 case RES_STRING:
1675 if (res->u.stringval)
1676 kfree(res->u.stringval, M_TEMP);
1677 stringval = cfgres[j].u.stringval;
1678 res->u.stringval = kmalloc(strlen(stringval) + 1,
1679 M_TEMP, M_INTWAIT);
1680 if (res->u.stringval == NULL)
1681 break;
1682 strcpy(res->u.stringval, stringval);
1683 break;
1684 default:
1685 panic("unknown resource type %d", type);
1690 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
1693 /*======================================*/
1695 * Some useful method implementations to make life easier for bus drivers.
1698 void
1699 resource_list_init(struct resource_list *rl)
1701 SLIST_INIT(rl);
1704 void
1705 resource_list_free(struct resource_list *rl)
1707 struct resource_list_entry *rle;
1709 while ((rle = SLIST_FIRST(rl)) != NULL) {
1710 if (rle->res)
1711 panic("resource_list_free: resource entry is busy");
1712 SLIST_REMOVE_HEAD(rl, link);
1713 kfree(rle, M_BUS);
1717 void
1718 resource_list_add(struct resource_list *rl,
1719 int type, int rid,
1720 u_long start, u_long end, u_long count)
1722 struct resource_list_entry *rle;
1724 rle = resource_list_find(rl, type, rid);
1725 if (rle == NULL) {
1726 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
1727 M_INTWAIT);
1728 if (!rle)
1729 panic("resource_list_add: can't record entry");
1730 SLIST_INSERT_HEAD(rl, rle, link);
1731 rle->type = type;
1732 rle->rid = rid;
1733 rle->res = NULL;
1736 if (rle->res)
1737 panic("resource_list_add: resource entry is busy");
1739 rle->start = start;
1740 rle->end = end;
1741 rle->count = count;
1744 struct resource_list_entry*
1745 resource_list_find(struct resource_list *rl,
1746 int type, int rid)
1748 struct resource_list_entry *rle;
1750 SLIST_FOREACH(rle, rl, link)
1751 if (rle->type == type && rle->rid == rid)
1752 return(rle);
1753 return(NULL);
1756 void
1757 resource_list_delete(struct resource_list *rl,
1758 int type, int rid)
1760 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1762 if (rle) {
1763 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1764 kfree(rle, M_BUS);
1768 struct resource *
1769 resource_list_alloc(struct resource_list *rl,
1770 device_t bus, device_t child,
1771 int type, int *rid,
1772 u_long start, u_long end,
1773 u_long count, u_int flags)
1775 struct resource_list_entry *rle = 0;
1776 int passthrough = (device_get_parent(child) != bus);
1777 int isdefault = (start == 0UL && end == ~0UL);
1779 if (passthrough) {
1780 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1781 type, rid,
1782 start, end, count, flags));
1785 rle = resource_list_find(rl, type, *rid);
1787 if (!rle)
1788 return(0); /* no resource of that type/rid */
1789 if (rle->res)
1790 panic("resource_list_alloc: resource entry is busy");
1792 if (isdefault) {
1793 start = rle->start;
1794 count = max(count, rle->count);
1795 end = max(rle->end, start + count - 1);
1798 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1799 type, rid, start, end, count, flags);
1802 * Record the new range.
1804 if (rle->res) {
1805 rle->start = rman_get_start(rle->res);
1806 rle->end = rman_get_end(rle->res);
1807 rle->count = count;
1810 return(rle->res);
1814 resource_list_release(struct resource_list *rl,
1815 device_t bus, device_t child,
1816 int type, int rid, struct resource *res)
1818 struct resource_list_entry *rle = 0;
1819 int passthrough = (device_get_parent(child) != bus);
1820 int error;
1822 if (passthrough) {
1823 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1824 type, rid, res));
1827 rle = resource_list_find(rl, type, rid);
1829 if (!rle)
1830 panic("resource_list_release: can't find resource");
1831 if (!rle->res)
1832 panic("resource_list_release: resource entry is not busy");
1834 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1835 type, rid, res);
1836 if (error)
1837 return(error);
1839 rle->res = NULL;
1840 return(0);
1844 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1845 const char *format)
1847 struct resource_list_entry *rle;
1848 int printed, retval;
1850 printed = 0;
1851 retval = 0;
1852 /* Yes, this is kinda cheating */
1853 SLIST_FOREACH(rle, rl, link) {
1854 if (rle->type == type) {
1855 if (printed == 0)
1856 retval += kprintf(" %s ", name);
1857 else
1858 retval += kprintf(",");
1859 printed++;
1860 retval += kprintf(format, rle->start);
1861 if (rle->count > 1) {
1862 retval += kprintf("-");
1863 retval += kprintf(format, rle->start +
1864 rle->count - 1);
1868 return(retval);
1872 * Generic driver/device identify functions. These will install a device
1873 * rendezvous point under the parent using the same name as the driver
1874 * name, which will at a later time be probed and attached.
1876 * These functions are used when the parent does not 'scan' its bus for
1877 * matching devices, or for the particular devices using these functions,
1878 * or when the device is a pseudo or synthesized device (such as can be
1879 * found under firewire and ppbus).
1882 bus_generic_identify(driver_t *driver, device_t parent)
1884 if (parent->state == DS_ATTACHED)
1885 return (0);
1886 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
1887 return (0);
1891 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
1893 if (parent->state == DS_ATTACHED)
1894 return (0);
1895 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
1896 return (0);
1900 * Call DEVICE_IDENTIFY for each driver.
1903 bus_generic_probe(device_t dev)
1905 devclass_t dc = dev->devclass;
1906 driverlink_t dl;
1908 TAILQ_FOREACH(dl, &dc->drivers, link) {
1909 DEVICE_IDENTIFY(dl->driver, dev);
1912 return(0);
1916 * This is an aweful hack due to the isa bus and autoconf code not
1917 * probing the ISA devices until after everything else has configured.
1918 * The ISA bus did a dummy attach long ago so we have to set it back
1919 * to an earlier state so the probe thinks its the initial probe and
1920 * not a bus rescan.
1922 * XXX remove by properly defering the ISA bus scan.
1925 bus_generic_probe_hack(device_t dev)
1927 if (dev->state == DS_ATTACHED) {
1928 dev->state = DS_ALIVE;
1929 bus_generic_probe(dev);
1930 dev->state = DS_ATTACHED;
1932 return (0);
1936 bus_generic_attach(device_t dev)
1938 device_t child;
1940 TAILQ_FOREACH(child, &dev->children, link) {
1941 device_probe_and_attach(child);
1944 return(0);
1948 bus_generic_detach(device_t dev)
1950 device_t child;
1951 int error;
1953 if (dev->state != DS_ATTACHED)
1954 return(EBUSY);
1956 TAILQ_FOREACH(child, &dev->children, link)
1957 if ((error = device_detach(child)) != 0)
1958 return(error);
1960 return 0;
1964 bus_generic_shutdown(device_t dev)
1966 device_t child;
1968 TAILQ_FOREACH(child, &dev->children, link)
1969 device_shutdown(child);
1971 return(0);
1975 bus_generic_suspend(device_t dev)
1977 int error;
1978 device_t child, child2;
1980 TAILQ_FOREACH(child, &dev->children, link) {
1981 error = DEVICE_SUSPEND(child);
1982 if (error) {
1983 for (child2 = TAILQ_FIRST(&dev->children);
1984 child2 && child2 != child;
1985 child2 = TAILQ_NEXT(child2, link))
1986 DEVICE_RESUME(child2);
1987 return(error);
1990 return(0);
1994 bus_generic_resume(device_t dev)
1996 device_t child;
1998 TAILQ_FOREACH(child, &dev->children, link)
1999 DEVICE_RESUME(child);
2000 /* if resume fails, there's nothing we can usefully do... */
2002 return(0);
2006 bus_print_child_header(device_t dev, device_t child)
2008 int retval = 0;
2010 if (device_get_desc(child))
2011 retval += device_printf(child, "<%s>", device_get_desc(child));
2012 else
2013 retval += kprintf("%s", device_get_nameunit(child));
2014 if (bootverbose) {
2015 if (child->state != DS_ATTACHED)
2016 kprintf(" [tentative]");
2017 else
2018 kprintf(" [attached!]");
2020 return(retval);
2024 bus_print_child_footer(device_t dev, device_t child)
2026 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2029 device_t
2030 bus_generic_add_child(device_t dev, device_t child, int order,
2031 const char *name, int unit)
2033 if (dev->parent)
2034 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2035 else
2036 dev = device_add_child_ordered(child, order, name, unit);
2037 return(dev);
2042 bus_generic_print_child(device_t dev, device_t child)
2044 int retval = 0;
2046 retval += bus_print_child_header(dev, child);
2047 retval += bus_print_child_footer(dev, child);
2049 return(retval);
2053 bus_generic_read_ivar(device_t dev, device_t child, int index,
2054 uintptr_t * result)
2056 int error;
2058 if (dev->parent)
2059 error = BUS_READ_IVAR(dev->parent, child, index, result);
2060 else
2061 error = ENOENT;
2062 return (error);
2066 bus_generic_write_ivar(device_t dev, device_t child, int index,
2067 uintptr_t value)
2069 int error;
2071 if (dev->parent)
2072 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2073 else
2074 error = ENOENT;
2075 return (error);
2079 * Resource list are used for iterations, do not recurse.
2081 struct resource_list *
2082 bus_generic_get_resource_list(device_t dev, device_t child)
2084 return (NULL);
2087 void
2088 bus_generic_driver_added(device_t dev, driver_t *driver)
2090 device_t child;
2092 DEVICE_IDENTIFY(driver, dev);
2093 TAILQ_FOREACH(child, &dev->children, link) {
2094 if (child->state == DS_NOTPRESENT)
2095 device_probe_and_attach(child);
2100 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2101 int flags, driver_intr_t *intr, void *arg,
2102 void **cookiep, lwkt_serialize_t serializer)
2104 /* Propagate up the bus hierarchy until someone handles it. */
2105 if (dev->parent)
2106 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2107 intr, arg, cookiep, serializer));
2108 else
2109 return(EINVAL);
2113 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2114 void *cookie)
2116 /* Propagate up the bus hierarchy until someone handles it. */
2117 if (dev->parent)
2118 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2119 else
2120 return(EINVAL);
2124 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2126 if (dev->parent)
2127 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2128 else
2129 return(0);
2132 void
2133 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2135 if (dev->parent)
2136 BUS_ENABLE_INTR(dev->parent, child, cookie);
2140 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2141 enum intr_polarity pol)
2143 /* Propagate up the bus hierarchy until someone handles it. */
2144 if (dev->parent)
2145 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2146 else
2147 return(EINVAL);
2150 struct resource *
2151 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2152 u_long start, u_long end, u_long count, u_int flags)
2154 /* Propagate up the bus hierarchy until someone handles it. */
2155 if (dev->parent)
2156 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2157 start, end, count, flags));
2158 else
2159 return(NULL);
2163 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2164 struct resource *r)
2166 /* Propagate up the bus hierarchy until someone handles it. */
2167 if (dev->parent)
2168 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2169 else
2170 return(EINVAL);
2174 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2175 struct resource *r)
2177 /* Propagate up the bus hierarchy until someone handles it. */
2178 if (dev->parent)
2179 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2180 else
2181 return(EINVAL);
2185 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2186 int rid, struct resource *r)
2188 /* Propagate up the bus hierarchy until someone handles it. */
2189 if (dev->parent)
2190 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2191 r));
2192 else
2193 return(EINVAL);
2197 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2198 u_long *startp, u_long *countp)
2200 int error;
2202 error = ENOENT;
2203 if (dev->parent) {
2204 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2205 startp, countp);
2207 return (error);
2211 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2212 u_long start, u_long count)
2214 int error;
2216 error = EINVAL;
2217 if (dev->parent) {
2218 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2219 start, count);
2221 return (error);
2224 void
2225 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2227 if (dev->parent)
2228 BUS_DELETE_RESOURCE(dev, child, type, rid);
2232 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2233 u_long *startp, u_long *countp)
2235 struct resource_list *rl = NULL;
2236 struct resource_list_entry *rle = NULL;
2238 rl = BUS_GET_RESOURCE_LIST(dev, child);
2239 if (!rl)
2240 return(EINVAL);
2242 rle = resource_list_find(rl, type, rid);
2243 if (!rle)
2244 return(ENOENT);
2246 if (startp)
2247 *startp = rle->start;
2248 if (countp)
2249 *countp = rle->count;
2251 return(0);
2255 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2256 u_long start, u_long count)
2258 struct resource_list *rl = NULL;
2260 rl = BUS_GET_RESOURCE_LIST(dev, child);
2261 if (!rl)
2262 return(EINVAL);
2264 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2266 return(0);
2269 void
2270 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2272 struct resource_list *rl = NULL;
2274 rl = BUS_GET_RESOURCE_LIST(dev, child);
2275 if (!rl)
2276 return;
2278 resource_list_delete(rl, type, rid);
2282 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2283 int rid, struct resource *r)
2285 struct resource_list *rl = NULL;
2287 rl = BUS_GET_RESOURCE_LIST(dev, child);
2288 if (!rl)
2289 return(EINVAL);
2291 return(resource_list_release(rl, dev, child, type, rid, r));
2294 struct resource *
2295 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2296 int *rid, u_long start, u_long end, u_long count, u_int flags)
2298 struct resource_list *rl = NULL;
2300 rl = BUS_GET_RESOURCE_LIST(dev, child);
2301 if (!rl)
2302 return(NULL);
2304 return(resource_list_alloc(rl, dev, child, type, rid,
2305 start, end, count, flags));
2309 bus_generic_child_present(device_t bus, device_t child)
2311 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2316 * Some convenience functions to make it easier for drivers to use the
2317 * resource-management functions. All these really do is hide the
2318 * indirection through the parent's method table, making for slightly
2319 * less-wordy code. In the future, it might make sense for this code
2320 * to maintain some sort of a list of resources allocated by each device.
2323 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2324 struct resource **res)
2326 int i;
2328 for (i = 0; rs[i].type != -1; i++)
2329 res[i] = NULL;
2330 for (i = 0; rs[i].type != -1; i++) {
2331 res[i] = bus_alloc_resource_any(dev,
2332 rs[i].type, &rs[i].rid, rs[i].flags);
2333 if (res[i] == NULL) {
2334 bus_release_resources(dev, rs, res);
2335 return (ENXIO);
2338 return (0);
2341 void
2342 bus_release_resources(device_t dev, const struct resource_spec *rs,
2343 struct resource **res)
2345 int i;
2347 for (i = 0; rs[i].type != -1; i++)
2348 if (res[i] != NULL) {
2349 bus_release_resource(
2350 dev, rs[i].type, rs[i].rid, res[i]);
2351 res[i] = NULL;
2355 struct resource *
2356 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2357 u_long count, u_int flags)
2359 if (dev->parent == 0)
2360 return(0);
2361 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2362 count, flags));
2366 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2368 if (dev->parent == 0)
2369 return(EINVAL);
2370 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2374 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2376 if (dev->parent == 0)
2377 return(EINVAL);
2378 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2382 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2384 if (dev->parent == 0)
2385 return(EINVAL);
2386 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2390 bus_setup_intr(device_t dev, struct resource *r, int flags,
2391 driver_intr_t handler, void *arg,
2392 void **cookiep, lwkt_serialize_t serializer)
2394 if (dev->parent == 0)
2395 return(EINVAL);
2396 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2397 cookiep, serializer));
2401 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2403 if (dev->parent == 0)
2404 return(EINVAL);
2405 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2408 void
2409 bus_enable_intr(device_t dev, void *cookie)
2411 if (dev->parent)
2412 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2416 bus_disable_intr(device_t dev, void *cookie)
2418 if (dev->parent)
2419 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2420 else
2421 return(0);
2425 bus_set_resource(device_t dev, int type, int rid,
2426 u_long start, u_long count)
2428 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2429 start, count));
2433 bus_get_resource(device_t dev, int type, int rid,
2434 u_long *startp, u_long *countp)
2436 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2437 startp, countp));
2440 u_long
2441 bus_get_resource_start(device_t dev, int type, int rid)
2443 u_long start, count;
2444 int error;
2446 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2447 &start, &count);
2448 if (error)
2449 return(0);
2450 return(start);
2453 u_long
2454 bus_get_resource_count(device_t dev, int type, int rid)
2456 u_long start, count;
2457 int error;
2459 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2460 &start, &count);
2461 if (error)
2462 return(0);
2463 return(count);
2466 void
2467 bus_delete_resource(device_t dev, int type, int rid)
2469 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2473 bus_child_present(device_t child)
2475 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2479 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2481 device_t parent;
2483 parent = device_get_parent(child);
2484 if (parent == NULL) {
2485 *buf = '\0';
2486 return (0);
2488 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2492 bus_child_location_str(device_t child, char *buf, size_t buflen)
2494 device_t parent;
2496 parent = device_get_parent(child);
2497 if (parent == NULL) {
2498 *buf = '\0';
2499 return (0);
2501 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2504 static int
2505 root_print_child(device_t dev, device_t child)
2507 return(0);
2510 static int
2511 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2512 void **cookiep, lwkt_serialize_t serializer)
2515 * If an interrupt mapping gets to here something bad has happened.
2517 panic("root_setup_intr");
2521 * If we get here, assume that the device is permanant and really is
2522 * present in the system. Removable bus drivers are expected to intercept
2523 * this call long before it gets here. We return -1 so that drivers that
2524 * really care can check vs -1 or some ERRNO returned higher in the food
2525 * chain.
2527 static int
2528 root_child_present(device_t dev, device_t child)
2530 return(-1);
2534 * XXX NOTE! other defaults may be set in bus_if.m
2536 static kobj_method_t root_methods[] = {
2537 /* Device interface */
2538 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2539 KOBJMETHOD(device_suspend, bus_generic_suspend),
2540 KOBJMETHOD(device_resume, bus_generic_resume),
2542 /* Bus interface */
2543 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2544 KOBJMETHOD(bus_print_child, root_print_child),
2545 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2546 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2547 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2548 KOBJMETHOD(bus_child_present, root_child_present),
2550 { 0, 0 }
2553 static driver_t root_driver = {
2554 "root",
2555 root_methods,
2556 1, /* no softc */
2559 device_t root_bus;
2560 devclass_t root_devclass;
2562 static int
2563 root_bus_module_handler(module_t mod, int what, void* arg)
2565 switch (what) {
2566 case MOD_LOAD:
2567 root_bus = make_device(NULL, "root", 0);
2568 root_bus->desc = "System root bus";
2569 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2570 root_bus->driver = &root_driver;
2571 root_bus->state = DS_ALIVE;
2572 root_devclass = devclass_find_internal("root", NULL, FALSE);
2573 return(0);
2575 case MOD_SHUTDOWN:
2576 device_shutdown(root_bus);
2577 return(0);
2578 default:
2579 return(0);
2583 static moduledata_t root_bus_mod = {
2584 "rootbus",
2585 root_bus_module_handler,
2588 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2590 void
2591 root_bus_configure(void)
2593 int warncount;
2594 device_t dev;
2596 PDEBUG(("."));
2599 * handle device_identify based device attachments to the root_bus
2600 * (typically nexus).
2602 bus_generic_probe(root_bus);
2605 * Probe and attach the devices under root_bus.
2607 TAILQ_FOREACH(dev, &root_bus->children, link) {
2608 device_probe_and_attach(dev);
2612 * Wait for all asynchronous attaches to complete. If we don't
2613 * our legacy ISA bus scan could steal device unit numbers or
2614 * even I/O ports.
2616 warncount = 10;
2617 if (numasyncthreads)
2618 kprintf("Waiting for async drivers to attach\n");
2619 while (numasyncthreads > 0) {
2620 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
2621 --warncount;
2622 if (warncount == 0) {
2623 kprintf("Warning: Still waiting for %d "
2624 "drivers to attach\n", numasyncthreads);
2625 } else if (warncount == -30) {
2626 kprintf("Giving up on %d drivers\n", numasyncthreads);
2627 break;
2630 root_bus->state = DS_ATTACHED;
2634 driver_module_handler(module_t mod, int what, void *arg)
2636 int error;
2637 struct driver_module_data *dmd;
2638 devclass_t bus_devclass;
2639 kobj_class_t driver;
2640 const char *parentname;
2642 dmd = (struct driver_module_data *)arg;
2643 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
2644 error = 0;
2646 switch (what) {
2647 case MOD_LOAD:
2648 if (dmd->dmd_chainevh)
2649 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2651 driver = dmd->dmd_driver;
2652 PDEBUG(("Loading module: driver %s on bus %s",
2653 DRIVERNAME(driver), dmd->dmd_busname));
2656 * If the driver has any base classes, make the
2657 * devclass inherit from the devclass of the driver's
2658 * first base class. This will allow the system to
2659 * search for drivers in both devclasses for children
2660 * of a device using this driver.
2662 if (driver->baseclasses)
2663 parentname = driver->baseclasses[0]->name;
2664 else
2665 parentname = NULL;
2666 *dmd->dmd_devclass = devclass_find_internal(driver->name,
2667 parentname, TRUE);
2669 error = devclass_add_driver(bus_devclass, driver);
2670 if (error)
2671 break;
2672 break;
2674 case MOD_UNLOAD:
2675 PDEBUG(("Unloading module: driver %s from bus %s",
2676 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
2677 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
2679 if (!error && dmd->dmd_chainevh)
2680 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2681 break;
2684 return (error);
2687 #ifdef BUS_DEBUG
2690 * The _short versions avoid iteration by not calling anything that prints
2691 * more than oneliners. I love oneliners.
2694 static void
2695 print_device_short(device_t dev, int indent)
2697 if (!dev)
2698 return;
2700 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2701 dev->unit, dev->desc,
2702 (dev->parent? "":"no "),
2703 (TAILQ_EMPTY(&dev->children)? "no ":""),
2704 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2705 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2706 (dev->flags&DF_WILDCARD? "wildcard,":""),
2707 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2708 (dev->ivars? "":"no "),
2709 (dev->softc? "":"no "),
2710 dev->busy));
2713 static void
2714 print_device(device_t dev, int indent)
2716 if (!dev)
2717 return;
2719 print_device_short(dev, indent);
2721 indentprintf(("Parent:\n"));
2722 print_device_short(dev->parent, indent+1);
2723 indentprintf(("Driver:\n"));
2724 print_driver_short(dev->driver, indent+1);
2725 indentprintf(("Devclass:\n"));
2726 print_devclass_short(dev->devclass, indent+1);
2730 * Print the device and all its children (indented).
2732 void
2733 print_device_tree_short(device_t dev, int indent)
2735 device_t child;
2737 if (!dev)
2738 return;
2740 print_device_short(dev, indent);
2742 TAILQ_FOREACH(child, &dev->children, link)
2743 print_device_tree_short(child, indent+1);
2747 * Print the device and all its children (indented).
2749 void
2750 print_device_tree(device_t dev, int indent)
2752 device_t child;
2754 if (!dev)
2755 return;
2757 print_device(dev, indent);
2759 TAILQ_FOREACH(child, &dev->children, link)
2760 print_device_tree(child, indent+1);
2763 static void
2764 print_driver_short(driver_t *driver, int indent)
2766 if (!driver)
2767 return;
2769 indentprintf(("driver %s: softc size = %d\n",
2770 driver->name, driver->size));
2773 static void
2774 print_driver(driver_t *driver, int indent)
2776 if (!driver)
2777 return;
2779 print_driver_short(driver, indent);
2783 static void
2784 print_driver_list(driver_list_t drivers, int indent)
2786 driverlink_t driver;
2788 TAILQ_FOREACH(driver, &drivers, link)
2789 print_driver(driver->driver, indent);
2792 static void
2793 print_devclass_short(devclass_t dc, int indent)
2795 if (!dc)
2796 return;
2798 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2801 static void
2802 print_devclass(devclass_t dc, int indent)
2804 int i;
2806 if (!dc)
2807 return;
2809 print_devclass_short(dc, indent);
2810 indentprintf(("Drivers:\n"));
2811 print_driver_list(dc->drivers, indent+1);
2813 indentprintf(("Devices:\n"));
2814 for (i = 0; i < dc->maxunit; i++)
2815 if (dc->devices[i])
2816 print_device(dc->devices[i], indent+1);
2819 void
2820 print_devclass_list_short(void)
2822 devclass_t dc;
2824 kprintf("Short listing of devclasses, drivers & devices:\n");
2825 TAILQ_FOREACH(dc, &devclasses, link) {
2826 print_devclass_short(dc, 0);
2830 void
2831 print_devclass_list(void)
2833 devclass_t dc;
2835 kprintf("Full listing of devclasses, drivers & devices:\n");
2836 TAILQ_FOREACH(dc, &devclasses, link) {
2837 print_devclass(dc, 0);
2841 #endif
2844 * Check to see if a device is disabled via a disabled hint.
2847 resource_disabled(const char *name, int unit)
2849 int error, value;
2851 error = resource_int_value(name, unit, "disabled", &value);
2852 if (error)
2853 return(0);
2854 return(value);