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.35 2006/12/23 00:35:04 swildner 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>
46 #include <machine/stdarg.h> /* for device_printf() */
48 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
51 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
52 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
53 #define DRIVERNAME(d) ((d)? d->name : "no driver")
54 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
56 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
57 * prevent syslog from deleting initial spaces
59 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
61 static void print_device_short(device_t dev, int indent);
62 static void print_device(device_t dev, int indent);
63 void print_device_tree_short(device_t dev, int indent);
64 void print_device_tree(device_t dev, int indent);
65 static void print_driver_short(driver_t *driver, int indent);
66 static void print_driver(driver_t *driver, int indent);
67 static void print_driver_list(driver_list_t drivers, int indent);
68 static void print_devclass_short(devclass_t dc, int indent);
69 static void print_devclass(devclass_t dc, int indent);
70 void print_devclass_list_short(void);
71 void print_devclass_list(void);
74 /* Make the compiler ignore the function calls */
75 #define PDEBUG(a) /* nop */
76 #define DEVICENAME(d) /* nop */
77 #define DRIVERNAME(d) /* nop */
78 #define DEVCLANAME(d) /* nop */
80 #define print_device_short(d,i) /* nop */
81 #define print_device(d,i) /* nop */
82 #define print_device_tree_short(d,i) /* nop */
83 #define print_device_tree(d,i) /* nop */
84 #define print_driver_short(d,i) /* nop */
85 #define print_driver(d,i) /* nop */
86 #define print_driver_list(d,i) /* nop */
87 #define print_devclass_short(d,i) /* nop */
88 #define print_devclass(d,i) /* nop */
89 #define print_devclass_list_short() /* nop */
90 #define print_devclass_list() /* nop */
94 static void device_register_oids(device_t dev);
95 static void device_unregister_oids(device_t dev);
98 kobj_method_t null_methods[] = {
102 DEFINE_CLASS(null, null_methods, 0);
105 * Devclass implementation
108 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
111 devclass_find_internal(const char *classname, const char *parentname,
116 PDEBUG(("looking for %s", classname));
117 if (classname == NULL)
120 TAILQ_FOREACH(dc, &devclasses, link)
121 if (!strcmp(dc->name, classname))
125 PDEBUG(("creating %s", classname));
126 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
127 M_BUS, M_INTWAIT | M_ZERO);
131 dc->name = (char*) (dc + 1);
132 strcpy(dc->name, classname);
135 TAILQ_INIT(&dc->drivers);
136 TAILQ_INSERT_TAIL(&devclasses, dc, link);
138 if (parentname && dc && !dc->parent)
139 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
145 devclass_create(const char *classname)
147 return(devclass_find_internal(classname, NULL, TRUE));
151 devclass_find(const char *classname)
153 return(devclass_find_internal(classname, NULL, FALSE));
157 devclass_find_unit(const char *classname, int unit)
161 if ((dc = devclass_find(classname)) != NULL)
162 return(devclass_get_device(dc, unit));
167 devclass_add_driver(devclass_t dc, driver_t *driver)
173 PDEBUG(("%s", DRIVERNAME(driver)));
175 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
180 * Compile the driver's methods. Also increase the reference count
181 * so that the class doesn't get freed when the last instance
182 * goes. This means we can safely use static methods and avoids a
183 * double-free in devclass_delete_driver.
185 kobj_class_instantiate(driver);
188 * Make sure the devclass which the driver is implementing exists.
190 devclass_find_internal(driver->name, NULL, TRUE);
193 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
196 * Call BUS_DRIVER_ADDED for any existing busses in this class,
197 * but only if the bus has already been attached (otherwise we
198 * might probe too early).
200 * This is what will cause a newly loaded module to be associated
201 * with hardware. bus_generic_driver_added() is typically what ends
204 for (i = 0; i < dc->maxunit; i++) {
205 if ((dev = dc->devices[i]) != NULL) {
206 if (dev->state == DS_ATTACHED)
207 BUS_DRIVER_ADDED(dev, driver);
215 devclass_delete_driver(devclass_t busclass, driver_t *driver)
217 devclass_t dc = devclass_find(driver->name);
223 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
229 * Find the link structure in the bus' list of drivers.
231 TAILQ_FOREACH(dl, &busclass->drivers, link)
232 if (dl->driver == driver)
236 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
241 * Disassociate from any devices. We iterate through all the
242 * devices in the devclass of the driver and detach any which are
243 * using the driver and which have a parent in the devclass which
244 * we are deleting from.
246 * Note that since a driver can be in multiple devclasses, we
247 * should not detach devices which are not children of devices in
248 * the affected devclass.
250 for (i = 0; i < dc->maxunit; i++)
251 if (dc->devices[i]) {
252 dev = dc->devices[i];
253 if (dev->driver == driver && dev->parent &&
254 dev->parent->devclass == busclass) {
255 if ((error = device_detach(dev)) != 0)
257 device_set_driver(dev, NULL);
261 TAILQ_REMOVE(&busclass->drivers, dl, link);
264 kobj_class_uninstantiate(driver);
270 devclass_find_driver_internal(devclass_t dc, const char *classname)
274 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
276 TAILQ_FOREACH(dl, &dc->drivers, link)
277 if (!strcmp(dl->driver->name, classname))
280 PDEBUG(("not found"));
285 devclass_find_driver(devclass_t dc, const char *classname)
289 dl = devclass_find_driver_internal(dc, classname);
297 devclass_get_name(devclass_t dc)
303 devclass_get_device(devclass_t dc, int unit)
305 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
307 return(dc->devices[unit]);
311 devclass_get_softc(devclass_t dc, int unit)
315 dev = devclass_get_device(dc, unit);
319 return(device_get_softc(dev));
323 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
330 for (i = 0; i < dc->maxunit; i++)
334 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
339 for (i = 0; i < dc->maxunit; i++)
340 if (dc->devices[i]) {
341 list[count] = dc->devices[i];
352 devclass_get_maxunit(devclass_t dc)
358 devclass_set_parent(devclass_t dc, devclass_t pdc)
364 devclass_get_parent(devclass_t dc)
370 devclass_alloc_unit(devclass_t dc, int *unitp)
374 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
376 /* If we have been given a wired unit number, check for existing device */
378 if (unit >= 0 && unit < dc->maxunit &&
379 dc->devices[unit] != NULL) {
381 kprintf("%s-: %s%d exists, using next available unit number\n",
382 dc->name, dc->name, unit);
383 /* find the next available slot */
384 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
388 /* Unwired device, find the next available slot for it */
390 while (unit < dc->maxunit && dc->devices[unit] != NULL)
395 * We've selected a unit beyond the length of the table, so let's
396 * extend the table to make room for all units up to and including
399 if (unit >= dc->maxunit) {
403 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
404 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
408 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
410 kfree(dc->devices, M_BUS);
411 dc->devices = newlist;
412 dc->maxunit = newsize;
414 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
421 devclass_add_device(devclass_t dc, device_t dev)
425 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
427 buflen = strlen(dc->name) + 5;
428 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
432 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
433 kfree(dev->nameunit, M_BUS);
434 dev->nameunit = NULL;
437 dc->devices[dev->unit] = dev;
439 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
441 #ifdef DEVICE_SYSCTLS
442 device_register_oids(dev);
449 devclass_delete_device(devclass_t dc, device_t dev)
454 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
456 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
457 panic("devclass_delete_device: inconsistent device class");
458 dc->devices[dev->unit] = NULL;
459 if (dev->flags & DF_WILDCARD)
461 dev->devclass = NULL;
462 kfree(dev->nameunit, M_BUS);
463 dev->nameunit = NULL;
465 #ifdef DEVICE_SYSCTLS
466 device_unregister_oids(dev);
473 make_device(device_t parent, const char *name, int unit)
478 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
481 dc = devclass_find_internal(name, NULL, TRUE);
483 kprintf("make_device: can't find device class %s\n", name);
489 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
493 dev->parent = parent;
494 TAILQ_INIT(&dev->children);
495 kobj_init((kobj_t) dev, &null_class);
497 dev->devclass = NULL;
499 dev->nameunit = NULL;
503 dev->flags = DF_ENABLED;
506 dev->flags |= DF_WILDCARD;
508 dev->flags |= DF_FIXEDCLASS;
509 if (devclass_add_device(dc, dev) != 0) {
510 kobj_delete((kobj_t)dev, M_BUS);
517 dev->state = DS_NOTPRESENT;
523 device_print_child(device_t dev, device_t child)
527 if (device_is_alive(child))
528 retval += BUS_PRINT_CHILD(dev, child);
530 retval += device_printf(child, " not found\n");
536 device_add_child(device_t dev, const char *name, int unit)
538 return device_add_child_ordered(dev, 0, name, unit);
542 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
547 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
550 child = make_device(dev, name, unit);
553 child->order = order;
555 TAILQ_FOREACH(place, &dev->children, link)
556 if (place->order > order)
561 * The device 'place' is the first device whose order is
562 * greater than the new child.
564 TAILQ_INSERT_BEFORE(place, child, link);
567 * The new child's order is greater or equal to the order of
568 * any existing device. Add the child to the tail of the list.
570 TAILQ_INSERT_TAIL(&dev->children, child, link);
577 device_delete_child(device_t dev, device_t child)
582 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
584 /* remove children first */
585 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
586 error = device_delete_child(child, grandchild);
591 if ((error = device_detach(child)) != 0)
594 devclass_delete_device(child->devclass, child);
595 TAILQ_REMOVE(&dev->children, child, link);
596 device_set_desc(child, NULL);
597 kobj_delete((kobj_t)child, M_BUS);
603 * Find only devices attached to this bus.
606 device_find_child(device_t dev, const char *classname, int unit)
611 dc = devclass_find(classname);
615 child = devclass_get_device(dc, unit);
616 if (child && child->parent == dev)
622 first_matching_driver(devclass_t dc, device_t dev)
625 return(devclass_find_driver_internal(dc, dev->devclass->name));
627 return(TAILQ_FIRST(&dc->drivers));
631 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
635 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
636 if (!strcmp(dev->devclass->name, dl->driver->name))
640 return(TAILQ_NEXT(last, link));
644 device_probe_child(device_t dev, device_t child)
647 driverlink_t best = 0;
650 int hasclass = (child->devclass != 0);
654 panic("device_probe_child: parent device has no devclass");
656 if (child->state == DS_ALIVE)
659 for (; dc; dc = dc->parent) {
660 for (dl = first_matching_driver(dc, child); dl;
661 dl = next_matching_driver(dc, child, dl)) {
662 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
663 device_set_driver(child, dl->driver);
665 device_set_devclass(child, dl->driver->name);
666 result = DEVICE_PROBE(child);
668 device_set_devclass(child, 0);
671 * If the driver returns SUCCESS, there can be
672 * no higher match for this device.
681 * The driver returned an error so it
682 * certainly doesn't match.
685 device_set_driver(child, 0);
690 * A priority lower than SUCCESS, remember the
691 * best matching driver. Initialise the value
692 * of pri for the first match.
694 if (best == 0 || result > pri) {
701 * If we have unambiguous match in this devclass,
702 * don't look in the parent.
704 if (best && pri == 0)
709 * If we found a driver, change state and initialise the devclass.
712 if (!child->devclass)
713 device_set_devclass(child, best->driver->name);
714 device_set_driver(child, best->driver);
717 * A bit bogus. Call the probe method again to make
718 * sure that we have the right description.
722 child->state = DS_ALIVE;
730 device_get_parent(device_t dev)
736 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
743 TAILQ_FOREACH(child, &dev->children, link)
746 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
751 TAILQ_FOREACH(child, &dev->children, link) {
763 device_get_driver(device_t dev)
769 device_get_devclass(device_t dev)
771 return(dev->devclass);
775 device_get_name(device_t dev)
778 return devclass_get_name(dev->devclass);
783 device_get_nameunit(device_t dev)
785 return(dev->nameunit);
789 device_get_unit(device_t dev)
795 device_get_desc(device_t dev)
801 device_get_flags(device_t dev)
803 return(dev->devflags);
807 device_print_prettyname(device_t dev)
809 const char *name = device_get_name(dev);
812 return kprintf("unknown: ");
814 return kprintf("%s%d: ", name, device_get_unit(dev));
818 device_printf(device_t dev, const char * fmt, ...)
823 retval = device_print_prettyname(dev);
825 retval += kvprintf(fmt, ap);
831 device_set_desc_internal(device_t dev, const char* desc, int copy)
833 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
834 kfree(dev->desc, M_BUS);
835 dev->flags &= ~DF_DESCMALLOCED;
840 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
842 strcpy(dev->desc, desc);
843 dev->flags |= DF_DESCMALLOCED;
846 /* Avoid a -Wcast-qual warning */
847 dev->desc = (char *)(uintptr_t) desc;
849 #ifdef DEVICE_SYSCTLS
851 struct sysctl_oid *oid = &dev->oid[1];
852 oid->oid_arg1 = dev->desc ? dev->desc : "";
853 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
859 device_set_desc(device_t dev, const char* desc)
861 device_set_desc_internal(dev, desc, FALSE);
865 device_set_desc_copy(device_t dev, const char* desc)
867 device_set_desc_internal(dev, desc, TRUE);
871 device_set_flags(device_t dev, uint32_t flags)
873 dev->devflags = flags;
877 device_get_softc(device_t dev)
883 device_set_softc(device_t dev, void *softc)
885 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
886 kfree(dev->softc, M_BUS);
889 dev->flags |= DF_EXTERNALSOFTC;
891 dev->flags &= ~DF_EXTERNALSOFTC;
895 device_get_ivars(device_t dev)
901 device_set_ivars(device_t dev, void * ivars)
910 device_get_state(device_t dev)
916 device_enable(device_t dev)
918 dev->flags |= DF_ENABLED;
922 device_disable(device_t dev)
924 dev->flags &= ~DF_ENABLED;
931 device_busy(device_t dev)
933 if (dev->state < DS_ATTACHED)
934 panic("device_busy: called for unattached device");
935 if (dev->busy == 0 && dev->parent)
936 device_busy(dev->parent);
938 dev->state = DS_BUSY;
945 device_unbusy(device_t dev)
947 if (dev->state != DS_BUSY)
948 panic("device_unbusy: called for non-busy device");
950 if (dev->busy == 0) {
952 device_unbusy(dev->parent);
953 dev->state = DS_ATTACHED;
958 device_quiet(device_t dev)
960 dev->flags |= DF_QUIET;
964 device_verbose(device_t dev)
966 dev->flags &= ~DF_QUIET;
970 device_is_quiet(device_t dev)
972 return((dev->flags & DF_QUIET) != 0);
976 device_is_enabled(device_t dev)
978 return((dev->flags & DF_ENABLED) != 0);
982 device_is_alive(device_t dev)
984 return(dev->state >= DS_ALIVE);
988 device_is_attached(device_t dev)
990 return(dev->state >= DS_ATTACHED);
994 device_set_devclass(device_t dev, const char *classname)
1000 devclass_delete_device(dev->devclass, dev);
1004 if (dev->devclass) {
1005 kprintf("device_set_devclass: device class already set\n");
1009 dc = devclass_find_internal(classname, NULL, TRUE);
1013 return(devclass_add_device(dc, dev));
1017 device_set_driver(device_t dev, driver_t *driver)
1019 if (dev->state >= DS_ATTACHED)
1022 if (dev->driver == driver)
1025 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1026 kfree(dev->softc, M_BUS);
1029 kobj_delete((kobj_t) dev, 0);
1030 dev->driver = driver;
1032 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1033 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1034 dev->softc = kmalloc(driver->size, M_BUS,
1035 M_INTWAIT | M_ZERO);
1037 kobj_delete((kobj_t)dev, 0);
1038 kobj_init((kobj_t) dev, &null_class);
1044 kobj_init((kobj_t) dev, &null_class);
1049 device_probe_and_attach(device_t dev)
1051 device_t bus = dev->parent;
1053 int hasclass = (dev->devclass != 0);
1055 if (dev->state >= DS_ALIVE)
1058 if ((dev->flags & DF_ENABLED) == 0) {
1060 device_print_prettyname(dev);
1061 kprintf("not probed (disabled)\n");
1066 error = device_probe_child(bus, dev);
1068 if (!(dev->flags & DF_DONENOMATCH)) {
1069 BUS_PROBE_NOMATCH(bus, dev);
1070 dev->flags |= DF_DONENOMATCH;
1076 * Output the exact device chain prior to the attach in case the
1077 * system locks up during attach, and generate the full info after
1078 * the attach so correct irq and other information is displayed.
1080 if (bootverbose && !device_is_quiet(dev)) {
1083 kprintf("%s", device_get_nameunit(dev));
1084 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1085 kprintf(".%s", device_get_nameunit(tmp));
1088 if (!device_is_quiet(dev))
1089 device_print_child(bus, dev);
1090 error = DEVICE_ATTACH(dev);
1092 dev->state = DS_ATTACHED;
1093 if (bootverbose && !device_is_quiet(dev))
1094 device_print_child(bus, dev);
1096 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1097 dev->driver->name, dev->unit, error);
1098 /* Unset the class that was set in device_probe_child */
1100 device_set_devclass(dev, 0);
1101 device_set_driver(dev, NULL);
1102 dev->state = DS_NOTPRESENT;
1109 device_detach(device_t dev)
1113 PDEBUG(("%s", DEVICENAME(dev)));
1114 if (dev->state == DS_BUSY)
1116 if (dev->state != DS_ATTACHED)
1119 if ((error = DEVICE_DETACH(dev)) != 0)
1121 device_printf(dev, "detached\n");
1123 BUS_CHILD_DETACHED(dev->parent, dev);
1125 if (!(dev->flags & DF_FIXEDCLASS))
1126 devclass_delete_device(dev->devclass, dev);
1128 dev->state = DS_NOTPRESENT;
1129 device_set_driver(dev, NULL);
1135 device_shutdown(device_t dev)
1137 if (dev->state < DS_ATTACHED)
1139 PDEBUG(("%s", DEVICENAME(dev)));
1140 return DEVICE_SHUTDOWN(dev);
1144 device_set_unit(device_t dev, int unit)
1149 dc = device_get_devclass(dev);
1150 if (unit < dc->maxunit && dc->devices[unit])
1152 err = devclass_delete_device(dc, dev);
1156 err = devclass_add_device(dc, dev);
1160 #ifdef DEVICE_SYSCTLS
1163 * Sysctl nodes for devices.
1166 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1169 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1171 device_t dev = arg1;
1173 int first = 1, error = 0;
1175 TAILQ_FOREACH(child, &dev->children, link)
1176 if (child->nameunit) {
1178 error = SYSCTL_OUT(req, ",", 1);
1183 error = SYSCTL_OUT(req, child->nameunit,
1184 strlen(child->nameunit));
1189 error = SYSCTL_OUT(req, "", 1);
1195 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1197 device_t dev = arg1;
1199 switch (dev->state) {
1201 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1203 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1205 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1207 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1214 device_register_oids(device_t dev)
1216 struct sysctl_oid* oid;
1219 bzero(oid, sizeof(*oid));
1220 oid->oid_parent = &sysctl__hw_devices_children;
1221 oid->oid_number = OID_AUTO;
1222 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1223 oid->oid_arg1 = &dev->oidlist[0];
1225 oid->oid_name = dev->nameunit;
1226 oid->oid_handler = 0;
1228 SLIST_INIT(&dev->oidlist[0]);
1229 sysctl_register_oid(oid);
1232 bzero(oid, sizeof(*oid));
1233 oid->oid_parent = &dev->oidlist[0];
1234 oid->oid_number = OID_AUTO;
1235 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1236 oid->oid_arg1 = dev->desc ? dev->desc : "";
1237 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1238 oid->oid_name = "desc";
1239 oid->oid_handler = sysctl_handle_string;
1241 sysctl_register_oid(oid);
1244 bzero(oid, sizeof(*oid));
1245 oid->oid_parent = &dev->oidlist[0];
1246 oid->oid_number = OID_AUTO;
1247 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1248 oid->oid_arg1 = dev;
1250 oid->oid_name = "children";
1251 oid->oid_handler = sysctl_handle_children;
1253 sysctl_register_oid(oid);
1256 bzero(oid, sizeof(*oid));
1257 oid->oid_parent = &dev->oidlist[0];
1258 oid->oid_number = OID_AUTO;
1259 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1260 oid->oid_arg1 = dev;
1262 oid->oid_name = "state";
1263 oid->oid_handler = sysctl_handle_state;
1265 sysctl_register_oid(oid);
1269 device_unregister_oids(device_t dev)
1271 sysctl_unregister_oid(&dev->oid[0]);
1272 sysctl_unregister_oid(&dev->oid[1]);
1273 sysctl_unregister_oid(&dev->oid[2]);
1278 /*======================================*/
1280 * Access functions for device resources.
1283 /* Supplied by config(8) in ioconf.c */
1284 extern struct config_device config_devtab[];
1285 extern int devtab_count;
1287 /* Runtime version */
1288 struct config_device *devtab = config_devtab;
1291 resource_new_name(const char *name, int unit)
1293 struct config_device *new;
1295 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1296 M_INTWAIT | M_ZERO);
1299 if (devtab && devtab_count > 0)
1300 bcopy(devtab, new, devtab_count * sizeof(*new));
1301 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1302 if (new[devtab_count].name == NULL) {
1306 strcpy(new[devtab_count].name, name);
1307 new[devtab_count].unit = unit;
1308 new[devtab_count].resource_count = 0;
1309 new[devtab_count].resources = NULL;
1310 if (devtab && devtab != config_devtab)
1311 kfree(devtab, M_TEMP);
1313 return devtab_count++;
1317 resource_new_resname(int j, const char *resname, resource_type type)
1319 struct config_resource *new;
1322 i = devtab[j].resource_count;
1323 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1326 if (devtab[j].resources && i > 0)
1327 bcopy(devtab[j].resources, new, i * sizeof(*new));
1328 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1329 if (new[i].name == NULL) {
1333 strcpy(new[i].name, resname);
1335 if (devtab[j].resources)
1336 kfree(devtab[j].resources, M_TEMP);
1337 devtab[j].resources = new;
1338 devtab[j].resource_count = i + 1;
1343 resource_match_string(int i, const char *resname, const char *value)
1346 struct config_resource *res;
1348 for (j = 0, res = devtab[i].resources;
1349 j < devtab[i].resource_count; j++, res++)
1350 if (!strcmp(res->name, resname)
1351 && res->type == RES_STRING
1352 && !strcmp(res->u.stringval, value))
1358 resource_find(const char *name, int unit, const char *resname,
1359 struct config_resource **result)
1362 struct config_resource *res;
1365 * First check specific instances, then generic.
1367 for (i = 0; i < devtab_count; i++) {
1368 if (devtab[i].unit < 0)
1370 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1371 res = devtab[i].resources;
1372 for (j = 0; j < devtab[i].resource_count; j++, res++)
1373 if (!strcmp(res->name, resname)) {
1379 for (i = 0; i < devtab_count; i++) {
1380 if (devtab[i].unit >= 0)
1382 /* XXX should this `&& devtab[i].unit == unit' be here? */
1383 /* XXX if so, then the generic match does nothing */
1384 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1385 res = devtab[i].resources;
1386 for (j = 0; j < devtab[i].resource_count; j++, res++)
1387 if (!strcmp(res->name, resname)) {
1397 resource_int_value(const char *name, int unit, const char *resname, int *result)
1400 struct config_resource *res;
1402 if ((error = resource_find(name, unit, resname, &res)) != 0)
1404 if (res->type != RES_INT)
1406 *result = res->u.intval;
1411 resource_long_value(const char *name, int unit, const char *resname,
1415 struct config_resource *res;
1417 if ((error = resource_find(name, unit, resname, &res)) != 0)
1419 if (res->type != RES_LONG)
1421 *result = res->u.longval;
1426 resource_string_value(const char *name, int unit, const char *resname,
1430 struct config_resource *res;
1432 if ((error = resource_find(name, unit, resname, &res)) != 0)
1434 if (res->type != RES_STRING)
1436 *result = res->u.stringval;
1441 resource_query_string(int i, const char *resname, const char *value)
1447 for (; i < devtab_count; i++)
1448 if (resource_match_string(i, resname, value) >= 0)
1454 resource_locate(int i, const char *resname)
1460 for (; i < devtab_count; i++)
1461 if (!strcmp(devtab[i].name, resname))
1467 resource_count(void)
1469 return(devtab_count);
1473 resource_query_name(int i)
1475 return(devtab[i].name);
1479 resource_query_unit(int i)
1481 return(devtab[i].unit);
1485 resource_create(const char *name, int unit, const char *resname,
1486 resource_type type, struct config_resource **result)
1489 struct config_resource *res = NULL;
1491 for (i = 0; i < devtab_count; i++)
1492 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1493 res = devtab[i].resources;
1497 i = resource_new_name(name, unit);
1500 res = devtab[i].resources;
1502 for (j = 0; j < devtab[i].resource_count; j++, res++)
1503 if (!strcmp(res->name, resname)) {
1507 j = resource_new_resname(i, resname, type);
1510 res = &devtab[i].resources[j];
1516 resource_set_int(const char *name, int unit, const char *resname, int value)
1519 struct config_resource *res;
1521 error = resource_create(name, unit, resname, RES_INT, &res);
1524 if (res->type != RES_INT)
1526 res->u.intval = value;
1531 resource_set_long(const char *name, int unit, const char *resname, long value)
1534 struct config_resource *res;
1536 error = resource_create(name, unit, resname, RES_LONG, &res);
1539 if (res->type != RES_LONG)
1541 res->u.longval = value;
1546 resource_set_string(const char *name, int unit, const char *resname,
1550 struct config_resource *res;
1552 error = resource_create(name, unit, resname, RES_STRING, &res);
1555 if (res->type != RES_STRING)
1557 if (res->u.stringval)
1558 kfree(res->u.stringval, M_TEMP);
1559 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
1560 if (res->u.stringval == NULL)
1562 strcpy(res->u.stringval, value);
1567 resource_cfgload(void *dummy __unused)
1569 struct config_resource *res, *cfgres;
1572 char *name, *resname;
1576 int config_devtab_count;
1578 config_devtab_count = devtab_count;
1582 for (i = 0; i < config_devtab_count; i++) {
1583 name = config_devtab[i].name;
1584 unit = config_devtab[i].unit;
1586 for (j = 0; j < config_devtab[i].resource_count; j++) {
1587 cfgres = config_devtab[i].resources;
1588 resname = cfgres[j].name;
1589 type = cfgres[j].type;
1590 error = resource_create(name, unit, resname, type,
1593 kprintf("create resource %s%d: error %d\n",
1597 if (res->type != type) {
1598 kprintf("type mismatch %s%d: %d != %d\n",
1599 name, unit, res->type, type);
1604 res->u.intval = cfgres[j].u.intval;
1607 res->u.longval = cfgres[j].u.longval;
1610 if (res->u.stringval)
1611 kfree(res->u.stringval, M_TEMP);
1612 stringval = cfgres[j].u.stringval;
1613 res->u.stringval = kmalloc(strlen(stringval) + 1,
1615 if (res->u.stringval == NULL)
1617 strcpy(res->u.stringval, stringval);
1620 panic("unknown resource type %d", type);
1625 SYSINIT(cfgload, SI_SUB_KMEM, SI_ORDER_ANY + 50, resource_cfgload, 0)
1628 /*======================================*/
1630 * Some useful method implementations to make life easier for bus drivers.
1634 resource_list_init(struct resource_list *rl)
1640 resource_list_free(struct resource_list *rl)
1642 struct resource_list_entry *rle;
1644 while ((rle = SLIST_FIRST(rl)) != NULL) {
1646 panic("resource_list_free: resource entry is busy");
1647 SLIST_REMOVE_HEAD(rl, link);
1653 resource_list_add(struct resource_list *rl,
1655 u_long start, u_long end, u_long count)
1657 struct resource_list_entry *rle;
1659 rle = resource_list_find(rl, type, rid);
1661 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
1664 panic("resource_list_add: can't record entry");
1665 SLIST_INSERT_HEAD(rl, rle, link);
1672 panic("resource_list_add: resource entry is busy");
1679 struct resource_list_entry*
1680 resource_list_find(struct resource_list *rl,
1683 struct resource_list_entry *rle;
1685 SLIST_FOREACH(rle, rl, link)
1686 if (rle->type == type && rle->rid == rid)
1692 resource_list_delete(struct resource_list *rl,
1695 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1698 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1704 resource_list_alloc(struct resource_list *rl,
1705 device_t bus, device_t child,
1707 u_long start, u_long end,
1708 u_long count, u_int flags)
1710 struct resource_list_entry *rle = 0;
1711 int passthrough = (device_get_parent(child) != bus);
1712 int isdefault = (start == 0UL && end == ~0UL);
1715 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1717 start, end, count, flags));
1720 rle = resource_list_find(rl, type, *rid);
1723 return(0); /* no resource of that type/rid */
1725 panic("resource_list_alloc: resource entry is busy");
1729 count = max(count, rle->count);
1730 end = max(rle->end, start + count - 1);
1733 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1734 type, rid, start, end, count, flags);
1737 * Record the new range.
1740 rle->start = rman_get_start(rle->res);
1741 rle->end = rman_get_end(rle->res);
1749 resource_list_release(struct resource_list *rl,
1750 device_t bus, device_t child,
1751 int type, int rid, struct resource *res)
1753 struct resource_list_entry *rle = 0;
1754 int passthrough = (device_get_parent(child) != bus);
1758 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1762 rle = resource_list_find(rl, type, rid);
1765 panic("resource_list_release: can't find resource");
1767 panic("resource_list_release: resource entry is not busy");
1769 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1779 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1782 struct resource_list_entry *rle;
1783 int printed, retval;
1787 /* Yes, this is kinda cheating */
1788 SLIST_FOREACH(rle, rl, link) {
1789 if (rle->type == type) {
1791 retval += kprintf(" %s ", name);
1793 retval += kprintf(",");
1795 retval += kprintf(format, rle->start);
1796 if (rle->count > 1) {
1797 retval += kprintf("-");
1798 retval += kprintf(format, rle->start +
1807 * Generic driver/device identify functions. These will install a device
1808 * rendezvous point under the parent using the same name as the driver
1809 * name, which will at a later time be probed and attached.
1811 * These functions are used when the parent does not 'scan' its bus for
1812 * matching devices, or for the particular devices using these functions,
1813 * or when the device is a pseudo or synthesized device (such as can be
1814 * found under firewire and ppbus).
1817 bus_generic_identify(driver_t *driver, device_t parent)
1819 if (parent->state == DS_ATTACHED)
1821 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
1826 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
1828 if (parent->state == DS_ATTACHED)
1830 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
1835 * Call DEVICE_IDENTIFY for each driver.
1838 bus_generic_probe(device_t dev)
1840 devclass_t dc = dev->devclass;
1843 TAILQ_FOREACH(dl, &dc->drivers, link) {
1844 DEVICE_IDENTIFY(dl->driver, dev);
1851 * This is an aweful hack due to the isa bus and autoconf code not
1852 * probing the ISA devices until after everything else has configured.
1853 * The ISA bus did a dummy attach long ago so we have to set it back
1854 * to an earlier state so the probe thinks its the initial probe and
1857 * XXX remove by properly defering the ISA bus scan.
1860 bus_generic_probe_hack(device_t dev)
1862 if (dev->state == DS_ATTACHED) {
1863 dev->state = DS_ALIVE;
1864 bus_generic_probe(dev);
1865 dev->state = DS_ATTACHED;
1871 bus_generic_attach(device_t dev)
1875 TAILQ_FOREACH(child, &dev->children, link) {
1876 device_probe_and_attach(child);
1883 bus_generic_detach(device_t dev)
1888 if (dev->state != DS_ATTACHED)
1891 TAILQ_FOREACH(child, &dev->children, link)
1892 if ((error = device_detach(child)) != 0)
1899 bus_generic_shutdown(device_t dev)
1903 TAILQ_FOREACH(child, &dev->children, link)
1904 device_shutdown(child);
1910 bus_generic_suspend(device_t dev)
1913 device_t child, child2;
1915 TAILQ_FOREACH(child, &dev->children, link) {
1916 error = DEVICE_SUSPEND(child);
1918 for (child2 = TAILQ_FIRST(&dev->children);
1919 child2 && child2 != child;
1920 child2 = TAILQ_NEXT(child2, link))
1921 DEVICE_RESUME(child2);
1929 bus_generic_resume(device_t dev)
1933 TAILQ_FOREACH(child, &dev->children, link)
1934 DEVICE_RESUME(child);
1935 /* if resume fails, there's nothing we can usefully do... */
1941 bus_print_child_header(device_t dev, device_t child)
1945 if (device_get_desc(child))
1946 retval += device_printf(child, "<%s>", device_get_desc(child));
1948 retval += kprintf("%s", device_get_nameunit(child));
1950 if (child->state != DS_ATTACHED)
1951 kprintf(" [tentative]");
1953 kprintf(" [attached!]");
1959 bus_print_child_footer(device_t dev, device_t child)
1961 return(kprintf(" on %s\n", device_get_nameunit(dev)));
1965 bus_generic_add_child(device_t dev, device_t child, int order,
1966 const char *name, int unit)
1969 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
1971 dev = device_add_child_ordered(child, order, name, unit);
1977 bus_generic_print_child(device_t dev, device_t child)
1981 retval += bus_print_child_header(dev, child);
1982 retval += bus_print_child_footer(dev, child);
1988 bus_generic_read_ivar(device_t dev, device_t child, int index,
1994 error = BUS_READ_IVAR(dev->parent, child, index, result);
2001 bus_generic_write_ivar(device_t dev, device_t child, int index,
2007 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2013 struct resource_list *
2014 bus_generic_get_resource_list(device_t dev, device_t child)
2016 struct resource_list *rl;
2019 rl = BUS_GET_RESOURCE_LIST(dev->parent, child);
2026 bus_generic_driver_added(device_t dev, driver_t *driver)
2030 DEVICE_IDENTIFY(driver, dev);
2031 TAILQ_FOREACH(child, &dev->children, link) {
2032 if (child->state == DS_NOTPRESENT)
2033 device_probe_and_attach(child);
2038 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2039 int flags, driver_intr_t *intr, void *arg,
2040 void **cookiep, lwkt_serialize_t serializer)
2042 /* Propagate up the bus hierarchy until someone handles it. */
2044 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2045 intr, arg, cookiep, serializer));
2051 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2054 /* Propagate up the bus hierarchy until someone handles it. */
2056 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2062 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2065 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2071 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2074 BUS_ENABLE_INTR(dev->parent, child, cookie);
2078 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2079 enum intr_polarity pol)
2081 /* Propagate up the bus hierarchy until someone handles it. */
2083 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2089 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2090 u_long start, u_long end, u_long count, u_int flags)
2092 /* Propagate up the bus hierarchy until someone handles it. */
2094 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2095 start, end, count, flags));
2101 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2104 /* Propagate up the bus hierarchy until someone handles it. */
2106 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2112 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2115 /* Propagate up the bus hierarchy until someone handles it. */
2117 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2123 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2124 int rid, struct resource *r)
2126 /* Propagate up the bus hierarchy until someone handles it. */
2128 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2135 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2136 u_long *startp, u_long *countp)
2142 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2149 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2150 u_long start, u_long count)
2156 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2163 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2166 BUS_DELETE_RESOURCE(dev, child, type, rid);
2170 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2171 u_long *startp, u_long *countp)
2173 struct resource_list *rl = NULL;
2174 struct resource_list_entry *rle = NULL;
2176 rl = BUS_GET_RESOURCE_LIST(dev, child);
2180 rle = resource_list_find(rl, type, rid);
2185 *startp = rle->start;
2187 *countp = rle->count;
2193 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2194 u_long start, u_long count)
2196 struct resource_list *rl = NULL;
2198 rl = BUS_GET_RESOURCE_LIST(dev, child);
2202 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2208 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2210 struct resource_list *rl = NULL;
2212 rl = BUS_GET_RESOURCE_LIST(dev, child);
2216 resource_list_delete(rl, type, rid);
2220 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2221 int rid, struct resource *r)
2223 struct resource_list *rl = NULL;
2225 rl = BUS_GET_RESOURCE_LIST(dev, child);
2229 return(resource_list_release(rl, dev, child, type, rid, r));
2233 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2234 int *rid, u_long start, u_long end, u_long count, u_int flags)
2236 struct resource_list *rl = NULL;
2238 rl = BUS_GET_RESOURCE_LIST(dev, child);
2242 return(resource_list_alloc(rl, dev, child, type, rid,
2243 start, end, count, flags));
2247 bus_generic_child_present(device_t bus, device_t child)
2249 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2254 * Some convenience functions to make it easier for drivers to use the
2255 * resource-management functions. All these really do is hide the
2256 * indirection through the parent's method table, making for slightly
2257 * less-wordy code. In the future, it might make sense for this code
2258 * to maintain some sort of a list of resources allocated by each device.
2261 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2262 u_long count, u_int flags)
2264 if (dev->parent == 0)
2266 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2271 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2273 if (dev->parent == 0)
2275 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2279 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2281 if (dev->parent == 0)
2283 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2287 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2289 if (dev->parent == 0)
2291 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2295 bus_setup_intr(device_t dev, struct resource *r, int flags,
2296 driver_intr_t handler, void *arg,
2297 void **cookiep, lwkt_serialize_t serializer)
2299 if (dev->parent == 0)
2301 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2302 cookiep, serializer));
2306 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2308 if (dev->parent == 0)
2310 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2314 bus_enable_intr(device_t dev, void *cookie)
2317 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2321 bus_disable_intr(device_t dev, void *cookie)
2324 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2330 bus_set_resource(device_t dev, int type, int rid,
2331 u_long start, u_long count)
2333 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2338 bus_get_resource(device_t dev, int type, int rid,
2339 u_long *startp, u_long *countp)
2341 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2346 bus_get_resource_start(device_t dev, int type, int rid)
2348 u_long start, count;
2351 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2359 bus_get_resource_count(device_t dev, int type, int rid)
2361 u_long start, count;
2364 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2372 bus_delete_resource(device_t dev, int type, int rid)
2374 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2378 bus_child_present(device_t child)
2380 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2384 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2388 parent = device_get_parent(child);
2389 if (parent == NULL) {
2393 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2397 bus_child_location_str(device_t child, char *buf, size_t buflen)
2401 parent = device_get_parent(child);
2402 if (parent == NULL) {
2406 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2410 root_print_child(device_t dev, device_t child)
2416 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2417 void **cookiep, lwkt_serialize_t serializer)
2420 * If an interrupt mapping gets to here something bad has happened.
2422 panic("root_setup_intr");
2426 * If we get here, assume that the device is permanant and really is
2427 * present in the system. Removable bus drivers are expected to intercept
2428 * this call long before it gets here. We return -1 so that drivers that
2429 * really care can check vs -1 or some ERRNO returned higher in the food
2433 root_child_present(device_t dev, device_t child)
2439 * XXX NOTE! other defaults may be set in bus_if.m
2441 static kobj_method_t root_methods[] = {
2442 /* Device interface */
2443 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2444 KOBJMETHOD(device_suspend, bus_generic_suspend),
2445 KOBJMETHOD(device_resume, bus_generic_resume),
2448 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2449 KOBJMETHOD(bus_print_child, root_print_child),
2450 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2451 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2452 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2453 KOBJMETHOD(bus_child_present, root_child_present),
2458 static driver_t root_driver = {
2465 devclass_t root_devclass;
2468 root_bus_module_handler(module_t mod, int what, void* arg)
2472 root_bus = make_device(NULL, "root", 0);
2473 root_bus->desc = "System root bus";
2474 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2475 root_bus->driver = &root_driver;
2476 root_bus->state = DS_ALIVE;
2477 root_devclass = devclass_find_internal("root", NULL, FALSE);
2481 device_shutdown(root_bus);
2488 static moduledata_t root_bus_mod = {
2490 root_bus_module_handler,
2493 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2496 root_bus_configure(void)
2503 * handle device_identify based device attachments to the root_bus
2504 * (typically nexus).
2506 bus_generic_probe(root_bus);
2509 * Probe and attach the devices under root_bus.
2511 TAILQ_FOREACH(dev, &root_bus->children, link) {
2512 device_probe_and_attach(dev);
2514 root_bus->state = DS_ATTACHED;
2518 driver_module_handler(module_t mod, int what, void *arg)
2521 struct driver_module_data *dmd;
2522 devclass_t bus_devclass;
2523 kobj_class_t driver;
2524 const char *parentname;
2526 dmd = (struct driver_module_data *)arg;
2527 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
2532 if (dmd->dmd_chainevh)
2533 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2535 driver = dmd->dmd_driver;
2536 PDEBUG(("Loading module: driver %s on bus %s",
2537 DRIVERNAME(driver), dmd->dmd_busname));
2538 error = devclass_add_driver(bus_devclass, driver);
2543 * If the driver has any base classes, make the
2544 * devclass inherit from the devclass of the driver's
2545 * first base class. This will allow the system to
2546 * search for drivers in both devclasses for children
2547 * of a device using this driver.
2549 if (driver->baseclasses)
2550 parentname = driver->baseclasses[0]->name;
2553 *dmd->dmd_devclass = devclass_find_internal(driver->name,
2558 PDEBUG(("Unloading module: driver %s from bus %s",
2559 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
2560 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
2562 if (!error && dmd->dmd_chainevh)
2563 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2573 * The _short versions avoid iteration by not calling anything that prints
2574 * more than oneliners. I love oneliners.
2578 print_device_short(device_t dev, int indent)
2583 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2584 dev->unit, dev->desc,
2585 (dev->parent? "":"no "),
2586 (TAILQ_EMPTY(&dev->children)? "no ":""),
2587 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2588 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2589 (dev->flags&DF_WILDCARD? "wildcard,":""),
2590 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2591 (dev->ivars? "":"no "),
2592 (dev->softc? "":"no "),
2597 print_device(device_t dev, int indent)
2602 print_device_short(dev, indent);
2604 indentprintf(("Parent:\n"));
2605 print_device_short(dev->parent, indent+1);
2606 indentprintf(("Driver:\n"));
2607 print_driver_short(dev->driver, indent+1);
2608 indentprintf(("Devclass:\n"));
2609 print_devclass_short(dev->devclass, indent+1);
2613 * Print the device and all its children (indented).
2616 print_device_tree_short(device_t dev, int indent)
2623 print_device_short(dev, indent);
2625 TAILQ_FOREACH(child, &dev->children, link)
2626 print_device_tree_short(child, indent+1);
2630 * Print the device and all its children (indented).
2633 print_device_tree(device_t dev, int indent)
2640 print_device(dev, indent);
2642 TAILQ_FOREACH(child, &dev->children, link)
2643 print_device_tree(child, indent+1);
2647 print_driver_short(driver_t *driver, int indent)
2652 indentprintf(("driver %s: softc size = %d\n",
2653 driver->name, driver->size));
2657 print_driver(driver_t *driver, int indent)
2662 print_driver_short(driver, indent);
2667 print_driver_list(driver_list_t drivers, int indent)
2669 driverlink_t driver;
2671 TAILQ_FOREACH(driver, &drivers, link)
2672 print_driver(driver->driver, indent);
2676 print_devclass_short(devclass_t dc, int indent)
2681 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2685 print_devclass(devclass_t dc, int indent)
2692 print_devclass_short(dc, indent);
2693 indentprintf(("Drivers:\n"));
2694 print_driver_list(dc->drivers, indent+1);
2696 indentprintf(("Devices:\n"));
2697 for (i = 0; i < dc->maxunit; i++)
2699 print_device(dc->devices[i], indent+1);
2703 print_devclass_list_short(void)
2707 kprintf("Short listing of devclasses, drivers & devices:\n");
2708 TAILQ_FOREACH(dc, &devclasses, link) {
2709 print_devclass_short(dc, 0);
2714 print_devclass_list(void)
2718 kprintf("Full listing of devclasses, drivers & devices:\n");
2719 TAILQ_FOREACH(dc, &devclasses, link) {
2720 print_devclass(dc, 0);
2727 * Check to see if a device is disabled via a disabled hint.
2730 resource_disabled(const char *name, int unit)
2734 error = resource_int_value(name, unit, "disabled", &value);