2 * Copyright (c) 1997,1998 Doug Rabson
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
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.27 2005/06/27 12:24:46 joerg Exp $
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>
38 #include <sys/sysctl.h>
41 #include <sys/bus_private.h>
42 #include <sys/systm.h>
43 #include <machine/bus.h>
45 #include <machine/stdarg.h> /* for device_printf() */
47 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
50 #define PDEBUG(a) (printf("%s:%d: ", __func__, __LINE__), printf a, printf("\n"))
51 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
52 #define DRIVERNAME(d) ((d)? d->name : "no driver")
53 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
55 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
56 * prevent syslog from deleting initial spaces
58 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0)
60 static void print_device_short(device_t dev, int indent);
61 static void print_device(device_t dev, int indent);
62 void print_device_tree_short(device_t dev, int indent);
63 void print_device_tree(device_t dev, int indent);
64 static void print_driver_short(driver_t *driver, int indent);
65 static void print_driver(driver_t *driver, int indent);
66 static void print_driver_list(driver_list_t drivers, int indent);
67 static void print_devclass_short(devclass_t dc, int indent);
68 static void print_devclass(devclass_t dc, int indent);
69 void print_devclass_list_short(void);
70 void print_devclass_list(void);
73 /* Make the compiler ignore the function calls */
74 #define PDEBUG(a) /* nop */
75 #define DEVICENAME(d) /* nop */
76 #define DRIVERNAME(d) /* nop */
77 #define DEVCLANAME(d) /* nop */
79 #define print_device_short(d,i) /* nop */
80 #define print_device(d,i) /* nop */
81 #define print_device_tree_short(d,i) /* nop */
82 #define print_device_tree(d,i) /* nop */
83 #define print_driver_short(d,i) /* nop */
84 #define print_driver(d,i) /* nop */
85 #define print_driver_list(d,i) /* nop */
86 #define print_devclass_short(d,i) /* nop */
87 #define print_devclass(d,i) /* nop */
88 #define print_devclass_list_short() /* nop */
89 #define print_devclass_list() /* nop */
93 static void device_register_oids(device_t dev);
94 static void device_unregister_oids(device_t dev);
97 kobj_method_t null_methods[] = {
101 DEFINE_CLASS(null, null_methods, 0);
104 * Devclass implementation
107 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
110 devclass_find_internal(const char *classname, const char *parentname,
115 PDEBUG(("looking for %s", classname));
116 if (classname == NULL)
119 TAILQ_FOREACH(dc, &devclasses, link)
120 if (!strcmp(dc->name, classname))
124 PDEBUG(("creating %s", classname));
125 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
126 M_BUS, M_INTWAIT | M_ZERO);
130 dc->name = (char*) (dc + 1);
131 strcpy(dc->name, classname);
134 TAILQ_INIT(&dc->drivers);
135 TAILQ_INSERT_TAIL(&devclasses, dc, link);
137 if (parentname && dc && !dc->parent)
138 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
144 devclass_create(const char *classname)
146 return(devclass_find_internal(classname, NULL, TRUE));
150 devclass_find(const char *classname)
152 return(devclass_find_internal(classname, NULL, FALSE));
156 devclass_add_driver(devclass_t dc, driver_t *driver)
161 PDEBUG(("%s", DRIVERNAME(driver)));
163 dl = malloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
168 * Compile the driver's methods. Also increase the reference count
169 * so that the class doesn't get freed when the last instance
170 * goes. This means we can safely use static methods and avoids a
171 * double-free in devclass_delete_driver.
173 kobj_class_instantiate(driver);
176 * Make sure the devclass which the driver is implementing exists.
178 devclass_find_internal(driver->name, NULL, TRUE);
181 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
184 * Call BUS_DRIVER_ADDED for any existing busses in this class.
186 for (i = 0; i < dc->maxunit; i++)
188 BUS_DRIVER_ADDED(dc->devices[i], driver);
194 devclass_delete_driver(devclass_t busclass, driver_t *driver)
196 devclass_t dc = devclass_find(driver->name);
202 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
208 * Find the link structure in the bus' list of drivers.
210 TAILQ_FOREACH(dl, &busclass->drivers, link)
211 if (dl->driver == driver)
215 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
220 * Disassociate from any devices. We iterate through all the
221 * devices in the devclass of the driver and detach any which are
222 * using the driver and which have a parent in the devclass which
223 * we are deleting from.
225 * Note that since a driver can be in multiple devclasses, we
226 * should not detach devices which are not children of devices in
227 * the affected devclass.
229 for (i = 0; i < dc->maxunit; i++)
230 if (dc->devices[i]) {
231 dev = dc->devices[i];
232 if (dev->driver == driver && dev->parent &&
233 dev->parent->devclass == busclass) {
234 if ((error = device_detach(dev)) != 0)
236 device_set_driver(dev, NULL);
240 TAILQ_REMOVE(&busclass->drivers, dl, link);
243 kobj_class_uninstantiate(driver);
249 devclass_find_driver_internal(devclass_t dc, const char *classname)
253 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
255 TAILQ_FOREACH(dl, &dc->drivers, link)
256 if (!strcmp(dl->driver->name, classname))
259 PDEBUG(("not found"));
264 devclass_find_driver(devclass_t dc, const char *classname)
268 dl = devclass_find_driver_internal(dc, classname);
276 devclass_get_name(devclass_t dc)
282 devclass_get_device(devclass_t dc, int unit)
284 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
286 return(dc->devices[unit]);
290 devclass_get_softc(devclass_t dc, int unit)
294 dev = devclass_get_device(dc, unit);
298 return(device_get_softc(dev));
302 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
309 for (i = 0; i < dc->maxunit; i++)
313 list = malloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
318 for (i = 0; i < dc->maxunit; i++)
319 if (dc->devices[i]) {
320 list[count] = dc->devices[i];
331 devclass_get_maxunit(devclass_t dc)
337 devclass_set_parent(devclass_t dc, devclass_t pdc)
343 devclass_get_parent(devclass_t dc)
349 devclass_alloc_unit(devclass_t dc, int *unitp)
353 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
355 /* If we have been given a wired unit number, check for existing device */
357 if (unit >= 0 && unit < dc->maxunit &&
358 dc->devices[unit] != NULL) {
360 printf("%s-: %s%d exists, using next available unit number\n",
361 dc->name, dc->name, unit);
362 /* find the next available slot */
363 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
367 /* Unwired device, find the next available slot for it */
369 while (unit < dc->maxunit && dc->devices[unit] != NULL)
374 * We've selected a unit beyond the length of the table, so let's
375 * extend the table to make room for all units up to and including
378 if (unit >= dc->maxunit) {
382 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
383 newlist = malloc(sizeof(device_t) * newsize, M_BUS,
387 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
389 free(dc->devices, M_BUS);
390 dc->devices = newlist;
391 dc->maxunit = newsize;
393 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
400 devclass_add_device(devclass_t dc, device_t dev)
404 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
406 buflen = strlen(dc->name) + 5;
407 dev->nameunit = malloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
411 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
412 free(dev->nameunit, M_BUS);
413 dev->nameunit = NULL;
416 dc->devices[dev->unit] = dev;
418 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
420 #ifdef DEVICE_SYSCTLS
421 device_register_oids(dev);
428 devclass_delete_device(devclass_t dc, device_t dev)
433 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
435 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
436 panic("devclass_delete_device: inconsistent device class");
437 dc->devices[dev->unit] = NULL;
438 if (dev->flags & DF_WILDCARD)
440 dev->devclass = NULL;
441 free(dev->nameunit, M_BUS);
442 dev->nameunit = NULL;
444 #ifdef DEVICE_SYSCTLS
445 device_unregister_oids(dev);
452 make_device(device_t parent, const char *name, int unit)
457 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
460 dc = devclass_find_internal(name, NULL, TRUE);
462 printf("make_device: can't find device class %s\n", name);
468 dev = malloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
472 dev->parent = parent;
473 TAILQ_INIT(&dev->children);
474 kobj_init((kobj_t) dev, &null_class);
476 dev->devclass = NULL;
478 dev->nameunit = NULL;
482 dev->flags = DF_ENABLED;
485 dev->flags |= DF_WILDCARD;
487 dev->flags |= DF_FIXEDCLASS;
488 if (devclass_add_device(dc, dev) != 0) {
489 kobj_delete((kobj_t)dev, M_BUS);
496 dev->state = DS_NOTPRESENT;
502 device_print_child(device_t dev, device_t child)
506 if (device_is_alive(child))
507 retval += BUS_PRINT_CHILD(dev, child);
509 retval += device_printf(child, " not found\n");
515 device_add_child(device_t dev, const char *name, int unit)
517 return device_add_child_ordered(dev, 0, name, unit);
521 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
526 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
529 child = make_device(dev, name, unit);
532 child->order = order;
534 TAILQ_FOREACH(place, &dev->children, link)
535 if (place->order > order)
540 * The device 'place' is the first device whose order is
541 * greater than the new child.
543 TAILQ_INSERT_BEFORE(place, child, link);
546 * The new child's order is greater or equal to the order of
547 * any existing device. Add the child to the tail of the list.
549 TAILQ_INSERT_TAIL(&dev->children, child, link);
556 device_delete_child(device_t dev, device_t child)
561 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
563 /* remove children first */
564 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
565 error = device_delete_child(child, grandchild);
570 if ((error = device_detach(child)) != 0)
573 devclass_delete_device(child->devclass, child);
574 TAILQ_REMOVE(&dev->children, child, link);
575 device_set_desc(child, NULL);
576 kobj_delete((kobj_t)child, M_BUS);
582 * Find only devices attached to this bus.
585 device_find_child(device_t dev, const char *classname, int unit)
590 dc = devclass_find(classname);
594 child = devclass_get_device(dc, unit);
595 if (child && child->parent == dev)
601 first_matching_driver(devclass_t dc, device_t dev)
604 return(devclass_find_driver_internal(dc, dev->devclass->name));
606 return(TAILQ_FIRST(&dc->drivers));
610 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
614 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
615 if (!strcmp(dev->devclass->name, dl->driver->name))
619 return(TAILQ_NEXT(last, link));
623 device_probe_child(device_t dev, device_t child)
626 driverlink_t best = 0;
629 int hasclass = (child->devclass != 0);
633 panic("device_probe_child: parent device has no devclass");
635 if (child->state == DS_ALIVE)
638 for (; dc; dc = dc->parent) {
639 for (dl = first_matching_driver(dc, child); dl;
640 dl = next_matching_driver(dc, child, dl)) {
641 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
642 device_set_driver(child, dl->driver);
644 device_set_devclass(child, dl->driver->name);
645 result = DEVICE_PROBE(child);
647 device_set_devclass(child, 0);
650 * If the driver returns SUCCESS, there can be
651 * no higher match for this device.
660 * The driver returned an error so it
661 * certainly doesn't match.
664 device_set_driver(child, 0);
669 * A priority lower than SUCCESS, remember the
670 * best matching driver. Initialise the value
671 * of pri for the first match.
673 if (best == 0 || result > pri) {
680 * If we have unambiguous match in this devclass,
681 * don't look in the parent.
683 if (best && pri == 0)
688 * If we found a driver, change state and initialise the devclass.
691 if (!child->devclass)
692 device_set_devclass(child, best->driver->name);
693 device_set_driver(child, best->driver);
696 * A bit bogus. Call the probe method again to make
697 * sure that we have the right description.
701 child->state = DS_ALIVE;
709 device_get_parent(device_t dev)
715 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
722 TAILQ_FOREACH(child, &dev->children, link)
725 list = malloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
730 TAILQ_FOREACH(child, &dev->children, link) {
742 device_get_driver(device_t dev)
748 device_get_devclass(device_t dev)
750 return(dev->devclass);
754 device_get_name(device_t dev)
757 return devclass_get_name(dev->devclass);
762 device_get_nameunit(device_t dev)
764 return(dev->nameunit);
768 device_get_unit(device_t dev)
774 device_get_desc(device_t dev)
780 device_get_flags(device_t dev)
782 return(dev->devflags);
786 device_print_prettyname(device_t dev)
788 const char *name = device_get_name(dev);
791 return printf("unknown: ");
793 return printf("%s%d: ", name, device_get_unit(dev));
797 device_printf(device_t dev, const char * fmt, ...)
802 retval = device_print_prettyname(dev);
804 retval += vprintf(fmt, ap);
810 device_set_desc_internal(device_t dev, const char* desc, int copy)
812 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
813 free(dev->desc, M_BUS);
814 dev->flags &= ~DF_DESCMALLOCED;
819 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
821 strcpy(dev->desc, desc);
822 dev->flags |= DF_DESCMALLOCED;
825 /* Avoid a -Wcast-qual warning */
826 dev->desc = (char *)(uintptr_t) desc;
828 #ifdef DEVICE_SYSCTLS
830 struct sysctl_oid *oid = &dev->oid[1];
831 oid->oid_arg1 = dev->desc ? dev->desc : "";
832 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
838 device_set_desc(device_t dev, const char* desc)
840 device_set_desc_internal(dev, desc, FALSE);
844 device_set_desc_copy(device_t dev, const char* desc)
846 device_set_desc_internal(dev, desc, TRUE);
850 device_set_flags(device_t dev, uint32_t flags)
852 dev->devflags = flags;
856 device_get_softc(device_t dev)
862 device_set_softc(device_t dev, void *softc)
864 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
865 free(dev->softc, M_BUS);
868 dev->flags |= DF_EXTERNALSOFTC;
870 dev->flags &= ~DF_EXTERNALSOFTC;
874 device_get_ivars(device_t dev)
880 device_set_ivars(device_t dev, void * ivars)
889 device_get_state(device_t dev)
895 device_enable(device_t dev)
897 dev->flags |= DF_ENABLED;
901 device_disable(device_t dev)
903 dev->flags &= ~DF_ENABLED;
910 device_busy(device_t dev)
912 if (dev->state < DS_ATTACHED)
913 panic("device_busy: called for unattached device");
914 if (dev->busy == 0 && dev->parent)
915 device_busy(dev->parent);
917 dev->state = DS_BUSY;
924 device_unbusy(device_t dev)
926 if (dev->state != DS_BUSY)
927 panic("device_unbusy: called for non-busy device");
929 if (dev->busy == 0) {
931 device_unbusy(dev->parent);
932 dev->state = DS_ATTACHED;
937 device_quiet(device_t dev)
939 dev->flags |= DF_QUIET;
943 device_verbose(device_t dev)
945 dev->flags &= ~DF_QUIET;
949 device_is_quiet(device_t dev)
951 return((dev->flags & DF_QUIET) != 0);
955 device_is_enabled(device_t dev)
957 return((dev->flags & DF_ENABLED) != 0);
961 device_is_alive(device_t dev)
963 return(dev->state >= DS_ALIVE);
967 device_is_attached(device_t dev)
969 return(dev->state >= DS_ATTACHED);
973 device_set_devclass(device_t dev, const char *classname)
979 devclass_delete_device(dev->devclass, dev);
984 printf("device_set_devclass: device class already set\n");
988 dc = devclass_find_internal(classname, NULL, TRUE);
992 return(devclass_add_device(dc, dev));
996 device_set_driver(device_t dev, driver_t *driver)
998 if (dev->state >= DS_ATTACHED)
1001 if (dev->driver == driver)
1004 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1005 free(dev->softc, M_BUS);
1008 kobj_delete((kobj_t) dev, 0);
1009 dev->driver = driver;
1011 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1012 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1013 dev->softc = malloc(driver->size, M_BUS,
1014 M_INTWAIT | M_ZERO);
1016 kobj_delete((kobj_t)dev, 0);
1017 kobj_init((kobj_t) dev, &null_class);
1023 kobj_init((kobj_t) dev, &null_class);
1028 device_probe_and_attach(device_t dev)
1030 device_t bus = dev->parent;
1032 int hasclass = (dev->devclass != 0);
1034 if (dev->state >= DS_ALIVE)
1037 if ((dev->flags & DF_ENABLED) == 0) {
1039 device_print_prettyname(dev);
1040 printf("not probed (disabled)\n");
1045 error = device_probe_child(bus, dev);
1047 if (!(dev->flags & DF_DONENOMATCH)) {
1048 BUS_PROBE_NOMATCH(bus, dev);
1049 dev->flags |= DF_DONENOMATCH;
1055 * Output the exact device chain prior to the attach in case the
1056 * system locks up during attach, and generate the full info after
1057 * the attach so correct irq and other information is displayed.
1059 if (bootverbose && !device_is_quiet(dev)) {
1062 printf("%s", device_get_nameunit(dev));
1063 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1064 printf(".%s", device_get_nameunit(tmp));
1067 if (!device_is_quiet(dev))
1068 device_print_child(bus, dev);
1069 error = DEVICE_ATTACH(dev);
1071 dev->state = DS_ATTACHED;
1072 if (bootverbose && !device_is_quiet(dev))
1073 device_print_child(bus, dev);
1075 printf("device_probe_and_attach: %s%d attach returned %d\n",
1076 dev->driver->name, dev->unit, error);
1077 /* Unset the class that was set in device_probe_child */
1079 device_set_devclass(dev, 0);
1080 device_set_driver(dev, NULL);
1081 dev->state = DS_NOTPRESENT;
1088 device_detach(device_t dev)
1092 PDEBUG(("%s", DEVICENAME(dev)));
1093 if (dev->state == DS_BUSY)
1095 if (dev->state != DS_ATTACHED)
1098 if ((error = DEVICE_DETACH(dev)) != 0)
1100 device_printf(dev, "detached\n");
1102 BUS_CHILD_DETACHED(dev->parent, dev);
1104 if (!(dev->flags & DF_FIXEDCLASS))
1105 devclass_delete_device(dev->devclass, dev);
1107 dev->state = DS_NOTPRESENT;
1108 device_set_driver(dev, NULL);
1114 device_shutdown(device_t dev)
1116 if (dev->state < DS_ATTACHED)
1118 PDEBUG(("%s", DEVICENAME(dev)));
1119 return DEVICE_SHUTDOWN(dev);
1123 device_set_unit(device_t dev, int unit)
1128 dc = device_get_devclass(dev);
1129 if (unit < dc->maxunit && dc->devices[unit])
1131 err = devclass_delete_device(dc, dev);
1135 err = devclass_add_device(dc, dev);
1139 #ifdef DEVICE_SYSCTLS
1142 * Sysctl nodes for devices.
1145 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1148 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1150 device_t dev = arg1;
1152 int first = 1, error = 0;
1154 TAILQ_FOREACH(child, &dev->children, link)
1155 if (child->nameunit) {
1157 error = SYSCTL_OUT(req, ",", 1);
1162 error = SYSCTL_OUT(req, child->nameunit,
1163 strlen(child->nameunit));
1168 error = SYSCTL_OUT(req, "", 1);
1174 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1176 device_t dev = arg1;
1178 switch (dev->state) {
1180 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1182 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1184 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1186 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1193 device_register_oids(device_t dev)
1195 struct sysctl_oid* oid;
1198 bzero(oid, sizeof(*oid));
1199 oid->oid_parent = &sysctl__hw_devices_children;
1200 oid->oid_number = OID_AUTO;
1201 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1202 oid->oid_arg1 = &dev->oidlist[0];
1204 oid->oid_name = dev->nameunit;
1205 oid->oid_handler = 0;
1207 SLIST_INIT(&dev->oidlist[0]);
1208 sysctl_register_oid(oid);
1211 bzero(oid, sizeof(*oid));
1212 oid->oid_parent = &dev->oidlist[0];
1213 oid->oid_number = OID_AUTO;
1214 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1215 oid->oid_arg1 = dev->desc ? dev->desc : "";
1216 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1217 oid->oid_name = "desc";
1218 oid->oid_handler = sysctl_handle_string;
1220 sysctl_register_oid(oid);
1223 bzero(oid, sizeof(*oid));
1224 oid->oid_parent = &dev->oidlist[0];
1225 oid->oid_number = OID_AUTO;
1226 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1227 oid->oid_arg1 = dev;
1229 oid->oid_name = "children";
1230 oid->oid_handler = sysctl_handle_children;
1232 sysctl_register_oid(oid);
1235 bzero(oid, sizeof(*oid));
1236 oid->oid_parent = &dev->oidlist[0];
1237 oid->oid_number = OID_AUTO;
1238 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1239 oid->oid_arg1 = dev;
1241 oid->oid_name = "state";
1242 oid->oid_handler = sysctl_handle_state;
1244 sysctl_register_oid(oid);
1248 device_unregister_oids(device_t dev)
1250 sysctl_unregister_oid(&dev->oid[0]);
1251 sysctl_unregister_oid(&dev->oid[1]);
1252 sysctl_unregister_oid(&dev->oid[2]);
1257 /*======================================*/
1259 * Access functions for device resources.
1262 /* Supplied by config(8) in ioconf.c */
1263 extern struct config_device config_devtab[];
1264 extern int devtab_count;
1266 /* Runtime version */
1267 struct config_device *devtab = config_devtab;
1270 resource_new_name(const char *name, int unit)
1272 struct config_device *new;
1274 new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1275 M_INTWAIT | M_ZERO);
1278 if (devtab && devtab_count > 0)
1279 bcopy(devtab, new, devtab_count * sizeof(*new));
1280 new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1281 if (new[devtab_count].name == NULL) {
1285 strcpy(new[devtab_count].name, name);
1286 new[devtab_count].unit = unit;
1287 new[devtab_count].resource_count = 0;
1288 new[devtab_count].resources = NULL;
1289 if (devtab && devtab != config_devtab)
1290 free(devtab, M_TEMP);
1292 return devtab_count++;
1296 resource_new_resname(int j, const char *resname, resource_type type)
1298 struct config_resource *new;
1301 i = devtab[j].resource_count;
1302 new = malloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1305 if (devtab[j].resources && i > 0)
1306 bcopy(devtab[j].resources, new, i * sizeof(*new));
1307 new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1308 if (new[i].name == NULL) {
1312 strcpy(new[i].name, resname);
1314 if (devtab[j].resources)
1315 free(devtab[j].resources, M_TEMP);
1316 devtab[j].resources = new;
1317 devtab[j].resource_count = i + 1;
1322 resource_match_string(int i, const char *resname, const char *value)
1325 struct config_resource *res;
1327 for (j = 0, res = devtab[i].resources;
1328 j < devtab[i].resource_count; j++, res++)
1329 if (!strcmp(res->name, resname)
1330 && res->type == RES_STRING
1331 && !strcmp(res->u.stringval, value))
1337 resource_find(const char *name, int unit, const char *resname,
1338 struct config_resource **result)
1341 struct config_resource *res;
1344 * First check specific instances, then generic.
1346 for (i = 0; i < devtab_count; i++) {
1347 if (devtab[i].unit < 0)
1349 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1350 res = devtab[i].resources;
1351 for (j = 0; j < devtab[i].resource_count; j++, res++)
1352 if (!strcmp(res->name, resname)) {
1358 for (i = 0; i < devtab_count; i++) {
1359 if (devtab[i].unit >= 0)
1361 /* XXX should this `&& devtab[i].unit == unit' be here? */
1362 /* XXX if so, then the generic match does nothing */
1363 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1364 res = devtab[i].resources;
1365 for (j = 0; j < devtab[i].resource_count; j++, res++)
1366 if (!strcmp(res->name, resname)) {
1376 resource_int_value(const char *name, int unit, const char *resname, int *result)
1379 struct config_resource *res;
1381 if ((error = resource_find(name, unit, resname, &res)) != 0)
1383 if (res->type != RES_INT)
1385 *result = res->u.intval;
1390 resource_long_value(const char *name, int unit, const char *resname,
1394 struct config_resource *res;
1396 if ((error = resource_find(name, unit, resname, &res)) != 0)
1398 if (res->type != RES_LONG)
1400 *result = res->u.longval;
1405 resource_string_value(const char *name, int unit, const char *resname,
1409 struct config_resource *res;
1411 if ((error = resource_find(name, unit, resname, &res)) != 0)
1413 if (res->type != RES_STRING)
1415 *result = res->u.stringval;
1420 resource_query_string(int i, const char *resname, const char *value)
1426 for (; i < devtab_count; i++)
1427 if (resource_match_string(i, resname, value) >= 0)
1433 resource_locate(int i, const char *resname)
1439 for (; i < devtab_count; i++)
1440 if (!strcmp(devtab[i].name, resname))
1446 resource_count(void)
1448 return(devtab_count);
1452 resource_query_name(int i)
1454 return(devtab[i].name);
1458 resource_query_unit(int i)
1460 return(devtab[i].unit);
1464 resource_create(const char *name, int unit, const char *resname,
1465 resource_type type, struct config_resource **result)
1468 struct config_resource *res = NULL;
1470 for (i = 0; i < devtab_count; i++)
1471 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1472 res = devtab[i].resources;
1476 i = resource_new_name(name, unit);
1479 res = devtab[i].resources;
1481 for (j = 0; j < devtab[i].resource_count; j++, res++)
1482 if (!strcmp(res->name, resname)) {
1486 j = resource_new_resname(i, resname, type);
1489 res = &devtab[i].resources[j];
1495 resource_set_int(const char *name, int unit, const char *resname, int value)
1498 struct config_resource *res;
1500 error = resource_create(name, unit, resname, RES_INT, &res);
1503 if (res->type != RES_INT)
1505 res->u.intval = value;
1510 resource_set_long(const char *name, int unit, const char *resname, long value)
1513 struct config_resource *res;
1515 error = resource_create(name, unit, resname, RES_LONG, &res);
1518 if (res->type != RES_LONG)
1520 res->u.longval = value;
1525 resource_set_string(const char *name, int unit, const char *resname,
1529 struct config_resource *res;
1531 error = resource_create(name, unit, resname, RES_STRING, &res);
1534 if (res->type != RES_STRING)
1536 if (res->u.stringval)
1537 free(res->u.stringval, M_TEMP);
1538 res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
1539 if (res->u.stringval == NULL)
1541 strcpy(res->u.stringval, value);
1546 resource_cfgload(void *dummy __unused)
1548 struct config_resource *res, *cfgres;
1551 char *name, *resname;
1555 int config_devtab_count;
1557 config_devtab_count = devtab_count;
1561 for (i = 0; i < config_devtab_count; i++) {
1562 name = config_devtab[i].name;
1563 unit = config_devtab[i].unit;
1565 for (j = 0; j < config_devtab[i].resource_count; j++) {
1566 cfgres = config_devtab[i].resources;
1567 resname = cfgres[j].name;
1568 type = cfgres[j].type;
1569 error = resource_create(name, unit, resname, type,
1572 printf("create resource %s%d: error %d\n",
1576 if (res->type != type) {
1577 printf("type mismatch %s%d: %d != %d\n",
1578 name, unit, res->type, type);
1583 res->u.intval = cfgres[j].u.intval;
1586 res->u.longval = cfgres[j].u.longval;
1589 if (res->u.stringval)
1590 free(res->u.stringval, M_TEMP);
1591 stringval = cfgres[j].u.stringval;
1592 res->u.stringval = malloc(strlen(stringval) + 1,
1594 if (res->u.stringval == NULL)
1596 strcpy(res->u.stringval, stringval);
1599 panic("unknown resource type %d", type);
1604 SYSINIT(cfgload, SI_SUB_KMEM, SI_ORDER_ANY + 50, resource_cfgload, 0)
1607 /*======================================*/
1609 * Some useful method implementations to make life easier for bus drivers.
1613 resource_list_init(struct resource_list *rl)
1619 resource_list_free(struct resource_list *rl)
1621 struct resource_list_entry *rle;
1623 while ((rle = SLIST_FIRST(rl)) != NULL) {
1625 panic("resource_list_free: resource entry is busy");
1626 SLIST_REMOVE_HEAD(rl, link);
1632 resource_list_add(struct resource_list *rl,
1634 u_long start, u_long end, u_long count)
1636 struct resource_list_entry *rle;
1638 rle = resource_list_find(rl, type, rid);
1640 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
1643 panic("resource_list_add: can't record entry");
1644 SLIST_INSERT_HEAD(rl, rle, link);
1651 panic("resource_list_add: resource entry is busy");
1658 struct resource_list_entry*
1659 resource_list_find(struct resource_list *rl,
1662 struct resource_list_entry *rle;
1664 SLIST_FOREACH(rle, rl, link)
1665 if (rle->type == type && rle->rid == rid)
1671 resource_list_delete(struct resource_list *rl,
1674 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1677 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1683 resource_list_alloc(struct resource_list *rl,
1684 device_t bus, device_t child,
1686 u_long start, u_long end,
1687 u_long count, u_int flags)
1689 struct resource_list_entry *rle = 0;
1690 int passthrough = (device_get_parent(child) != bus);
1691 int isdefault = (start == 0UL && end == ~0UL);
1694 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1696 start, end, count, flags));
1699 rle = resource_list_find(rl, type, *rid);
1702 return(0); /* no resource of that type/rid */
1704 panic("resource_list_alloc: resource entry is busy");
1708 count = max(count, rle->count);
1709 end = max(rle->end, start + count - 1);
1712 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1713 type, rid, start, end, count, flags);
1716 * Record the new range.
1719 rle->start = rman_get_start(rle->res);
1720 rle->end = rman_get_end(rle->res);
1728 resource_list_release(struct resource_list *rl,
1729 device_t bus, device_t child,
1730 int type, int rid, struct resource *res)
1732 struct resource_list_entry *rle = 0;
1733 int passthrough = (device_get_parent(child) != bus);
1737 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1741 rle = resource_list_find(rl, type, rid);
1744 panic("resource_list_release: can't find resource");
1746 panic("resource_list_release: resource entry is not busy");
1748 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1758 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1761 struct resource_list_entry *rle;
1762 int printed, retval;
1766 /* Yes, this is kinda cheating */
1767 SLIST_FOREACH(rle, rl, link) {
1768 if (rle->type == type) {
1770 retval += printf(" %s ", name);
1772 retval += printf(",");
1774 retval += printf(format, rle->start);
1775 if (rle->count > 1) {
1776 retval += printf("-");
1777 retval += printf(format, rle->start +
1786 * Call DEVICE_IDENTIFY for each driver.
1789 bus_generic_probe(device_t dev)
1791 devclass_t dc = dev->devclass;
1794 TAILQ_FOREACH(dl, &dc->drivers, link)
1795 DEVICE_IDENTIFY(dl->driver, dev);
1801 bus_generic_attach(device_t dev)
1805 TAILQ_FOREACH(child, &dev->children, link)
1806 device_probe_and_attach(child);
1812 bus_generic_detach(device_t dev)
1817 if (dev->state != DS_ATTACHED)
1820 TAILQ_FOREACH(child, &dev->children, link)
1821 if ((error = device_detach(child)) != 0)
1828 bus_generic_shutdown(device_t dev)
1832 TAILQ_FOREACH(child, &dev->children, link)
1833 device_shutdown(child);
1839 bus_generic_suspend(device_t dev)
1842 device_t child, child2;
1844 TAILQ_FOREACH(child, &dev->children, link) {
1845 error = DEVICE_SUSPEND(child);
1847 for (child2 = TAILQ_FIRST(&dev->children);
1848 child2 && child2 != child;
1849 child2 = TAILQ_NEXT(child2, link))
1850 DEVICE_RESUME(child2);
1858 bus_generic_resume(device_t dev)
1862 TAILQ_FOREACH(child, &dev->children, link)
1863 DEVICE_RESUME(child);
1864 /* if resume fails, there's nothing we can usefully do... */
1870 bus_print_child_header(device_t dev, device_t child)
1874 if (device_get_desc(child))
1875 retval += device_printf(child, "<%s>", device_get_desc(child));
1877 retval += printf("%s", device_get_nameunit(child));
1879 if (child->state != DS_ATTACHED)
1880 printf(" [tentative]");
1882 printf(" [attached!]");
1888 bus_print_child_footer(device_t dev, device_t child)
1890 return(printf(" on %s\n", device_get_nameunit(dev)));
1894 bus_generic_print_child(device_t dev, device_t child)
1898 retval += bus_print_child_header(dev, child);
1899 retval += bus_print_child_footer(dev, child);
1905 bus_generic_read_ivar(device_t dev, device_t child, int index,
1912 bus_generic_write_ivar(device_t dev, device_t child, int index,
1918 struct resource_list *
1919 bus_generic_get_resource_list(device_t dev, device_t child)
1925 bus_generic_driver_added(device_t dev, driver_t *driver)
1929 DEVICE_IDENTIFY(driver, dev);
1930 TAILQ_FOREACH(child, &dev->children, link)
1931 if (child->state == DS_NOTPRESENT)
1932 device_probe_and_attach(child);
1936 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
1937 int flags, driver_intr_t *intr, void *arg,
1938 void **cookiep, lwkt_serialize_t serializer)
1940 /* Propagate up the bus hierarchy until someone handles it. */
1942 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
1943 intr, arg, cookiep, serializer));
1949 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
1952 /* Propagate up the bus hierarchy until someone handles it. */
1954 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
1960 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
1963 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
1969 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
1972 BUS_ENABLE_INTR(dev->parent, child, cookie);
1976 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
1977 u_long start, u_long end, u_long count, u_int flags)
1979 /* Propagate up the bus hierarchy until someone handles it. */
1981 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
1982 start, end, count, flags));
1988 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
1991 /* Propagate up the bus hierarchy until someone handles it. */
1993 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
1999 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2002 /* Propagate up the bus hierarchy until someone handles it. */
2004 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2010 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2011 int rid, struct resource *r)
2013 /* Propagate up the bus hierarchy until someone handles it. */
2015 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2022 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2023 enum intr_polarity pol)
2025 /* Propagate up the bus hierarchy until someone handles it. */
2027 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2033 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2034 u_long *startp, u_long *countp)
2036 struct resource_list *rl = NULL;
2037 struct resource_list_entry *rle = NULL;
2039 rl = BUS_GET_RESOURCE_LIST(dev, child);
2043 rle = resource_list_find(rl, type, rid);
2048 *startp = rle->start;
2050 *countp = rle->count;
2056 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2057 u_long start, u_long count)
2059 struct resource_list *rl = NULL;
2061 rl = BUS_GET_RESOURCE_LIST(dev, child);
2065 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2071 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2073 struct resource_list *rl = NULL;
2075 rl = BUS_GET_RESOURCE_LIST(dev, child);
2079 resource_list_delete(rl, type, rid);
2083 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2084 int rid, struct resource *r)
2086 struct resource_list *rl = NULL;
2088 rl = BUS_GET_RESOURCE_LIST(dev, child);
2092 return(resource_list_release(rl, dev, child, type, rid, r));
2096 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2097 int *rid, u_long start, u_long end, u_long count, u_int flags)
2099 struct resource_list *rl = NULL;
2101 rl = BUS_GET_RESOURCE_LIST(dev, child);
2105 return(resource_list_alloc(rl, dev, child, type, rid,
2106 start, end, count, flags));
2110 bus_generic_child_present(device_t bus, device_t child)
2112 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2117 * Some convenience functions to make it easier for drivers to use the
2118 * resource-management functions. All these really do is hide the
2119 * indirection through the parent's method table, making for slightly
2120 * less-wordy code. In the future, it might make sense for this code
2121 * to maintain some sort of a list of resources allocated by each device.
2124 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2125 u_long count, u_int flags)
2127 if (dev->parent == 0)
2129 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2134 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2136 if (dev->parent == 0)
2138 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2142 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2144 if (dev->parent == 0)
2146 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2150 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2152 if (dev->parent == 0)
2154 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2158 bus_setup_intr(device_t dev, struct resource *r, int flags,
2159 driver_intr_t handler, void *arg,
2160 void **cookiep, lwkt_serialize_t serializer)
2162 if (dev->parent == 0)
2164 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2165 cookiep, serializer));
2169 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2171 if (dev->parent == 0)
2173 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2177 bus_enable_intr(device_t dev, void *cookie)
2180 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2184 bus_disable_intr(device_t dev, void *cookie)
2187 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2193 bus_set_resource(device_t dev, int type, int rid,
2194 u_long start, u_long count)
2196 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2201 bus_get_resource(device_t dev, int type, int rid,
2202 u_long *startp, u_long *countp)
2204 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2209 bus_get_resource_start(device_t dev, int type, int rid)
2211 u_long start, count;
2214 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2222 bus_get_resource_count(device_t dev, int type, int rid)
2224 u_long start, count;
2227 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2235 bus_delete_resource(device_t dev, int type, int rid)
2237 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2241 bus_child_present(device_t child)
2243 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2247 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2251 parent = device_get_parent(child);
2252 if (parent == NULL) {
2256 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2260 bus_child_location_str(device_t child, char *buf, size_t buflen)
2264 parent = device_get_parent(child);
2265 if (parent == NULL) {
2269 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2273 root_print_child(device_t dev, device_t child)
2279 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2280 void **cookiep, lwkt_serialize_t serializer)
2283 * If an interrupt mapping gets to here something bad has happened.
2285 panic("root_setup_intr");
2289 * If we get here, assume that the device is permanant and really is
2290 * present in the system. Removable bus drivers are expected to intercept
2291 * this call long before it gets here. We return -1 so that drivers that
2292 * really care can check vs -1 or some ERRNO returned higher in the food
2296 root_child_present(device_t dev, device_t child)
2302 * XXX NOTE! other defaults may be set in bus_if.m
2304 static kobj_method_t root_methods[] = {
2305 /* Device interface */
2306 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2307 KOBJMETHOD(device_suspend, bus_generic_suspend),
2308 KOBJMETHOD(device_resume, bus_generic_resume),
2311 KOBJMETHOD(bus_print_child, root_print_child),
2312 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2313 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2314 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2315 KOBJMETHOD(bus_child_present, root_child_present),
2320 static driver_t root_driver = {
2327 devclass_t root_devclass;
2330 root_bus_module_handler(module_t mod, int what, void* arg)
2334 root_bus = make_device(NULL, "root", 0);
2335 root_bus->desc = "System root bus";
2336 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2337 root_bus->driver = &root_driver;
2338 root_bus->state = DS_ATTACHED;
2339 root_devclass = devclass_find_internal("root", NULL, FALSE);
2343 device_shutdown(root_bus);
2350 static moduledata_t root_bus_mod = {
2352 root_bus_module_handler,
2355 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2358 root_bus_configure(void)
2364 TAILQ_FOREACH(dev, &root_bus->children, link)
2365 device_probe_and_attach(dev);
2369 driver_module_handler(module_t mod, int what, void *arg)
2372 struct driver_module_data *dmd;
2373 devclass_t bus_devclass;
2374 kobj_class_t driver;
2375 const char *parentname;
2377 dmd = (struct driver_module_data *)arg;
2378 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
2383 if (dmd->dmd_chainevh)
2384 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2386 driver = dmd->dmd_driver;
2387 PDEBUG(("Loading module: driver %s on bus %s",
2388 DRIVERNAME(driver), dmd->dmd_busname));
2389 error = devclass_add_driver(bus_devclass, driver);
2394 * If the driver has any base classes, make the
2395 * devclass inherit from the devclass of the driver's
2396 * first base class. This will allow the system to
2397 * search for drivers in both devclasses for children
2398 * of a device using this driver.
2400 if (driver->baseclasses)
2401 parentname = driver->baseclasses[0]->name;
2404 *dmd->dmd_devclass = devclass_find_internal(driver->name,
2409 PDEBUG(("Unloading module: driver %s from bus %s",
2410 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
2411 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
2413 if (!error && dmd->dmd_chainevh)
2414 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2424 * The _short versions avoid iteration by not calling anything that prints
2425 * more than oneliners. I love oneliners.
2429 print_device_short(device_t dev, int indent)
2434 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2435 dev->unit, dev->desc,
2436 (dev->parent? "":"no "),
2437 (TAILQ_EMPTY(&dev->children)? "no ":""),
2438 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2439 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2440 (dev->flags&DF_WILDCARD? "wildcard,":""),
2441 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2442 (dev->ivars? "":"no "),
2443 (dev->softc? "":"no "),
2448 print_device(device_t dev, int indent)
2453 print_device_short(dev, indent);
2455 indentprintf(("Parent:\n"));
2456 print_device_short(dev->parent, indent+1);
2457 indentprintf(("Driver:\n"));
2458 print_driver_short(dev->driver, indent+1);
2459 indentprintf(("Devclass:\n"));
2460 print_devclass_short(dev->devclass, indent+1);
2464 * Print the device and all its children (indented).
2467 print_device_tree_short(device_t dev, int indent)
2474 print_device_short(dev, indent);
2476 TAILQ_FOREACH(child, &dev->children, link)
2477 print_device_tree_short(child, indent+1);
2481 * Print the device and all its children (indented).
2484 print_device_tree(device_t dev, int indent)
2491 print_device(dev, indent);
2493 TAILQ_FOREACH(child, &dev->children, link)
2494 print_device_tree(child, indent+1);
2498 print_driver_short(driver_t *driver, int indent)
2503 indentprintf(("driver %s: softc size = %d\n",
2504 driver->name, driver->size));
2508 print_driver(driver_t *driver, int indent)
2513 print_driver_short(driver, indent);
2518 print_driver_list(driver_list_t drivers, int indent)
2520 driverlink_t driver;
2522 TAILQ_FOREACH(driver, &drivers, link)
2523 print_driver(driver->driver, indent);
2527 print_devclass_short(devclass_t dc, int indent)
2532 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2536 print_devclass(devclass_t dc, int indent)
2543 print_devclass_short(dc, indent);
2544 indentprintf(("Drivers:\n"));
2545 print_driver_list(dc->drivers, indent+1);
2547 indentprintf(("Devices:\n"));
2548 for (i = 0; i < dc->maxunit; i++)
2550 print_device(dc->devices[i], indent+1);
2554 print_devclass_list_short(void)
2558 printf("Short listing of devclasses, drivers & devices:\n");
2559 TAILQ_FOREACH(dc, &devclasses, link) {
2560 print_devclass_short(dc, 0);
2565 print_devclass_list(void)
2569 printf("Full listing of devclasses, drivers & devices:\n");
2570 TAILQ_FOREACH(dc, &devclasses, link) {
2571 print_devclass(dc, 0);
2578 * Check to see if a device is disabled via a disabled hint.
2581 resource_disabled(const char *name, int unit)
2585 error = resource_int_value(name, unit, "disabled", &value);