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.2 2003/06/17 04:28:41 dillon 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>
40 #include <sys/bus_private.h>
41 #include <sys/systm.h>
42 #include <machine/bus.h>
44 #include <machine/stdarg.h> /* for device_printf() */
46 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
49 #define PDEBUG(a) (printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n"))
50 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
51 #define DRIVERNAME(d) ((d)? d->name : "no driver")
52 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
54 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
55 * prevent syslog from deleting initial spaces
57 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0)
59 static void print_method_list(device_method_t *m, int indent);
60 static void print_device_ops(device_ops_t ops, int indent);
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_method_list(m,i) /* nop */
81 #define print_device_ops(o,i) /* nop */
82 #define print_device_short(d,i) /* nop */
83 #define print_device(d,i) /* nop */
84 #define print_device_tree_short(d,i) /* nop */
85 #define print_device_tree(d,i) /* nop */
86 #define print_driver_short(d,i) /* nop */
87 #define print_driver(d,i) /* nop */
88 #define print_driver_list(d,i) /* nop */
89 #define print_devclass_short(d,i) /* nop */
90 #define print_devclass(d,i) /* nop */
91 #define print_devclass_list_short() /* nop */
92 #define print_devclass_list() /* nop */
96 static void device_register_oids(device_t dev);
97 static void device_unregister_oids(device_t dev);
101 * Method table handling
103 static int error_method(void);
104 static int next_method_offset = 1;
106 LIST_HEAD(methodlist, method) methods;
108 LIST_ENTRY(method) link; /* linked list of methods */
109 int offset; /* offset in method table */
110 int refs; /* count of device_op_desc users */
111 devop_t deflt; /* default implementation */
112 char* name; /* unique name of method */
116 register_method(struct device_op_desc *desc)
121 desc->method->refs++;
126 * Make sure that desc->deflt is always valid to simplify dispatch.
129 desc->deflt = error_method;
131 for (m = LIST_FIRST(&methods); m; m = LIST_NEXT(m, link)) {
132 if (!strcmp(m->name, desc->name)) {
133 desc->offset = m->offset;
136 PDEBUG(("method %p has the same name, %s, with offset %d",
137 (void *)m, desc->name, desc->offset));
142 m = (struct method *) malloc(sizeof(struct method)
143 + strlen(desc->name) + 1,
146 panic("register_method: out of memory");
147 bzero(m, sizeof(struct method) + strlen(desc->name) + 1);
148 m->offset = next_method_offset++;
150 m->deflt = desc->deflt;
151 m->name = (char*) (m + 1);
152 strcpy(m->name, desc->name);
153 LIST_INSERT_HEAD(&methods, m, link);
155 desc->offset = m->offset;
160 unregister_method(struct device_op_desc *desc)
162 struct method *m = desc->method;
165 PDEBUG(("method %s, reached refcount 0", desc->name));
166 LIST_REMOVE(m, link);
172 static int error_method(void)
177 static struct device_ops null_ops = {
183 compile_methods(driver_t *driver)
186 struct device_method *m;
191 * First register any methods which need it.
193 for (i = 0, m = driver->methods; m->desc; i++, m++)
194 register_method(m->desc);
197 * Then allocate the compiled op table.
199 ops = malloc(sizeof(struct device_ops) + (next_method_offset-1) * sizeof(devop_t),
202 panic("compile_methods: out of memory");
203 bzero(ops, sizeof(struct device_ops) + (next_method_offset-1) * sizeof(devop_t));
205 ops->maxoffset = next_method_offset;
206 /* Fill in default methods and then overwrite with driver methods */
207 for (i = 0; i < next_method_offset; i++)
208 ops->methods[i] = error_method;
209 for (cm = LIST_FIRST(&methods); cm; cm = LIST_NEXT(cm, link)) {
211 ops->methods[cm->offset] = cm->deflt;
213 for (i = 0, m = driver->methods; m->desc; i++, m++)
214 ops->methods[m->desc->offset] = m->func;
215 PDEBUG(("%s has %d method%s, wasting %d bytes",
216 DRIVERNAME(driver), i, (i==1?"":"s"),
217 (next_method_offset-i)*sizeof(devop_t)));
223 free_methods(driver_t *driver)
226 struct device_method *m;
229 * Unregister any methods which are no longer used.
231 for (i = 0, m = driver->methods; m->desc; i++, m++)
232 unregister_method(m->desc);
235 * Free memory and clean up.
237 free(driver->ops, M_BUS);
242 * Devclass implementation
245 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
248 devclass_find_internal(const char *classname, int create)
252 PDEBUG(("looking for %s", classname));
256 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
257 if (!strcmp(dc->name, classname))
260 PDEBUG(("%s not found%s", classname, (create? ", creating": "")));
262 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
266 bzero(dc, sizeof(struct devclass) + strlen(classname) + 1);
267 dc->name = (char*) (dc + 1);
268 strcpy(dc->name, classname);
271 TAILQ_INIT(&dc->drivers);
272 TAILQ_INSERT_TAIL(&devclasses, dc, link);
279 devclass_create(const char *classname)
281 return devclass_find_internal(classname, TRUE);
285 devclass_find(const char *classname)
287 return devclass_find_internal(classname, FALSE);
291 devclass_add_driver(devclass_t dc, driver_t *driver)
296 PDEBUG(("%s", DRIVERNAME(driver)));
298 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT);
301 bzero(dl, sizeof *dl);
304 * Compile the driver's methods.
307 compile_methods(driver);
310 * Make sure the devclass which the driver is implementing exists.
312 devclass_find_internal(driver->name, TRUE);
315 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
319 * Call BUS_DRIVER_ADDED for any existing busses in this class.
321 for (i = 0; i < dc->maxunit; i++)
323 BUS_DRIVER_ADDED(dc->devices[i], driver);
329 devclass_delete_driver(devclass_t busclass, driver_t *driver)
331 devclass_t dc = devclass_find(driver->name);
337 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
343 * Find the link structure in the bus' list of drivers.
345 for (dl = TAILQ_FIRST(&busclass->drivers); dl;
346 dl = TAILQ_NEXT(dl, link)) {
347 if (dl->driver == driver)
352 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
357 * Disassociate from any devices. We iterate through all the
358 * devices in the devclass of the driver and detach any which are
359 * using the driver and which have a parent in the devclass which
360 * we are deleting from.
362 * Note that since a driver can be in multiple devclasses, we
363 * should not detach devices which are not children of devices in
364 * the affected devclass.
366 for (i = 0; i < dc->maxunit; i++) {
367 if (dc->devices[i]) {
368 dev = dc->devices[i];
369 if (dev->driver == driver
370 && dev->parent && dev->parent->devclass == busclass) {
371 if ((error = device_detach(dev)) != 0)
373 device_set_driver(dev, NULL);
378 TAILQ_REMOVE(&busclass->drivers, dl, link);
382 if (driver->refs == 0)
383 free_methods(driver);
389 devclass_find_driver_internal(devclass_t dc, const char *classname)
393 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
395 for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link)) {
396 if (!strcmp(dl->driver->name, classname))
400 PDEBUG(("not found"));
405 devclass_find_driver(devclass_t dc, const char *classname)
409 dl = devclass_find_driver_internal(dc, classname);
417 devclass_get_name(devclass_t dc)
423 devclass_get_device(devclass_t dc, int unit)
425 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
427 return dc->devices[unit];
431 devclass_get_softc(devclass_t dc, int unit)
435 dev = devclass_get_device(dc, unit);
439 return (device_get_softc(dev));
443 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
450 for (i = 0; i < dc->maxunit; i++)
454 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
457 bzero(list, count * sizeof(device_t));
460 for (i = 0; i < dc->maxunit; i++)
461 if (dc->devices[i]) {
462 list[count] = dc->devices[i];
473 devclass_get_maxunit(devclass_t dc)
479 devclass_alloc_unit(devclass_t dc, int *unitp)
483 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
485 /* If we have been given a wired unit number, check for existing device */
487 if (unit >= 0 && unit < dc->maxunit && dc->devices[unit] != NULL) {
489 printf("%s-: %s%d exists, using next available unit number\n",
490 dc->name, dc->name, unit);
491 /* find the next available slot */
492 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
497 /* Unwired device, find the next available slot for it */
499 while (unit < dc->maxunit && dc->devices[unit] != NULL)
504 * We've selected a unit beyond the length of the table, so let's extend
505 * the table to make room for all units up to and including this one.
507 if (unit >= dc->maxunit) {
511 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
512 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
515 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
516 bzero(newlist + dc->maxunit,
517 sizeof(device_t) * (newsize - dc->maxunit));
519 free(dc->devices, M_BUS);
520 dc->devices = newlist;
521 dc->maxunit = newsize;
523 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
530 devclass_add_device(devclass_t dc, device_t dev)
534 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
536 buflen = strlen(dc->name) + 5;
537 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT);
540 bzero(dev->nameunit, buflen);
542 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
543 free(dev->nameunit, M_BUS);
544 dev->nameunit = NULL;
547 dc->devices[dev->unit] = dev;
549 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
551 #ifdef DEVICE_SYSCTLS
552 device_register_oids(dev);
559 devclass_delete_device(devclass_t dc, device_t dev)
564 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
566 if (dev->devclass != dc
567 || dc->devices[dev->unit] != dev)
568 panic("devclass_delete_device: inconsistent device class");
569 dc->devices[dev->unit] = NULL;
570 if (dev->flags & DF_WILDCARD)
572 dev->devclass = NULL;
573 free(dev->nameunit, M_BUS);
574 dev->nameunit = NULL;
576 #ifdef DEVICE_SYSCTLS
577 device_unregister_oids(dev);
584 make_device(device_t parent, const char *name, int unit)
589 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
592 dc = devclass_find_internal(name, TRUE);
594 printf("make_device: can't find device class %s\n", name);
600 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT);
603 bzero(dev, sizeof(struct device));
605 dev->parent = parent;
606 TAILQ_INIT(&dev->children);
607 dev->ops = &null_ops;
609 dev->devclass = NULL;
611 dev->nameunit = NULL;
615 dev->flags = DF_ENABLED;
618 dev->flags |= DF_WILDCARD;
620 dev->flags |= DF_FIXEDCLASS;
621 devclass_add_device(dc, dev);
626 dev->state = DS_NOTPRESENT;
632 device_print_child(device_t dev, device_t child)
636 if (device_is_alive(child)) {
637 retval += BUS_PRINT_CHILD(dev, child);
639 retval += device_printf(child, " not found\n");
645 device_add_child(device_t dev, const char *name, int unit)
647 return device_add_child_ordered(dev, 0, name, unit);
651 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
656 PDEBUG(("%s at %s with order %d as unit %d",
657 name, DEVICENAME(dev), order, unit));
659 child = make_device(dev, name, unit);
662 child->order = order;
664 TAILQ_FOREACH(place, &dev->children, link)
665 if (place->order > order)
670 * The device 'place' is the first device whose order is
671 * greater than the new child.
673 TAILQ_INSERT_BEFORE(place, child, link);
676 * The new child's order is greater or equal to the order of
677 * any existing device. Add the child to the tail of the list.
679 TAILQ_INSERT_TAIL(&dev->children, child, link);
686 device_delete_child(device_t dev, device_t child)
691 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
693 /* remove children first */
694 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
695 error = device_delete_child(child, grandchild);
700 if ((error = device_detach(child)) != 0)
703 devclass_delete_device(child->devclass, child);
704 TAILQ_REMOVE(&dev->children, child, link);
705 device_set_desc(child, NULL);
712 * Find only devices attached to this bus.
715 device_find_child(device_t dev, const char *classname, int unit)
720 dc = devclass_find(classname);
724 child = devclass_get_device(dc, unit);
725 if (child && child->parent == dev)
731 first_matching_driver(devclass_t dc, device_t dev)
734 return devclass_find_driver_internal(dc, dev->devclass->name);
736 return TAILQ_FIRST(&dc->drivers);
740 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
744 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
745 if (!strcmp(dev->devclass->name, dl->driver->name))
749 return TAILQ_NEXT(last, link);
753 device_probe_child(device_t dev, device_t child)
756 driverlink_t best = 0;
759 int hasclass = (child->devclass != 0);
763 panic("device_probe_child: parent device has no devclass");
765 if (child->state == DS_ALIVE)
768 for (dl = first_matching_driver(dc, child);
770 dl = next_matching_driver(dc, child, dl)) {
771 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
772 device_set_driver(child, dl->driver);
774 device_set_devclass(child, dl->driver->name);
775 result = DEVICE_PROBE(child);
777 device_set_devclass(child, 0);
780 * If the driver returns SUCCESS, there can be no higher match
790 * The driver returned an error so it certainly doesn't match.
793 device_set_driver(child, 0);
798 * A priority lower than SUCCESS, remember the best matching
799 * driver. Initialise the value of pri for the first match.
801 if (best == 0 || result > pri) {
809 * If we found a driver, change state and initialise the devclass.
812 if (!child->devclass)
813 device_set_devclass(child, best->driver->name);
814 device_set_driver(child, best->driver);
817 * A bit bogus. Call the probe method again to make sure
818 * that we have the right description.
822 child->state = DS_ALIVE;
830 device_get_parent(device_t dev)
836 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
843 for (child = TAILQ_FIRST(&dev->children); child;
844 child = TAILQ_NEXT(child, link))
847 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
850 bzero(list, count * sizeof(device_t));
853 for (child = TAILQ_FIRST(&dev->children); child;
854 child = TAILQ_NEXT(child, link)) {
866 device_get_driver(device_t dev)
872 device_get_devclass(device_t dev)
874 return dev->devclass;
878 device_get_name(device_t dev)
881 return devclass_get_name(dev->devclass);
886 device_get_nameunit(device_t dev)
888 return dev->nameunit;
892 device_get_unit(device_t dev)
898 device_get_desc(device_t dev)
904 device_get_flags(device_t dev)
906 return dev->devflags;
910 device_print_prettyname(device_t dev)
912 const char *name = device_get_name(dev);
915 return printf("unknown: ");
917 return printf("%s%d: ", name, device_get_unit(dev));
921 device_printf(device_t dev, const char * fmt, ...)
926 retval = device_print_prettyname(dev);
928 retval += vprintf(fmt, ap);
934 device_set_desc_internal(device_t dev, const char* desc, int copy)
936 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
937 free(dev->desc, M_BUS);
938 dev->flags &= ~DF_DESCMALLOCED;
943 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
945 strcpy(dev->desc, desc);
946 dev->flags |= DF_DESCMALLOCED;
949 /* Avoid a -Wcast-qual warning */
950 dev->desc = (char *)(uintptr_t) desc;
952 #ifdef DEVICE_SYSCTLS
954 struct sysctl_oid *oid = &dev->oid[1];
955 oid->oid_arg1 = dev->desc ? dev->desc : "";
956 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
962 device_set_desc(device_t dev, const char* desc)
964 device_set_desc_internal(dev, desc, FALSE);
968 device_set_desc_copy(device_t dev, const char* desc)
970 device_set_desc_internal(dev, desc, TRUE);
974 device_set_flags(device_t dev, u_int32_t flags)
976 dev->devflags = flags;
980 device_get_softc(device_t dev)
986 device_set_softc(device_t dev, void *softc)
988 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
989 free(dev->softc, M_BUS);
992 dev->flags |= DF_EXTERNALSOFTC;
994 dev->flags &= ~DF_EXTERNALSOFTC;
998 device_get_ivars(device_t dev)
1004 device_set_ivars(device_t dev, void * ivars)
1015 device_get_state(device_t dev)
1021 device_enable(device_t dev)
1023 dev->flags |= DF_ENABLED;
1027 device_disable(device_t dev)
1029 dev->flags &= ~DF_ENABLED;
1033 device_busy(device_t dev)
1035 if (dev->state < DS_ATTACHED)
1036 panic("device_busy: called for unattached device");
1037 if (dev->busy == 0 && dev->parent)
1038 device_busy(dev->parent);
1040 dev->state = DS_BUSY;
1044 device_unbusy(device_t dev)
1046 if (dev->state != DS_BUSY)
1047 panic("device_unbusy: called for non-busy device");
1049 if (dev->busy == 0) {
1051 device_unbusy(dev->parent);
1052 dev->state = DS_ATTACHED;
1057 device_quiet(device_t dev)
1059 dev->flags |= DF_QUIET;
1063 device_verbose(device_t dev)
1065 dev->flags &= ~DF_QUIET;
1069 device_is_quiet(device_t dev)
1071 return (dev->flags & DF_QUIET) != 0;
1075 device_is_enabled(device_t dev)
1077 return (dev->flags & DF_ENABLED) != 0;
1081 device_is_alive(device_t dev)
1083 return dev->state >= DS_ALIVE;
1087 device_set_devclass(device_t dev, const char *classname)
1093 devclass_delete_device(dev->devclass, dev);
1097 if (dev->devclass) {
1098 printf("device_set_devclass: device class already set\n");
1102 dc = devclass_find_internal(classname, TRUE);
1106 return devclass_add_device(dc, dev);
1110 device_set_driver(device_t dev, driver_t *driver)
1112 if (dev->state >= DS_ATTACHED)
1115 if (dev->driver == driver)
1118 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1119 free(dev->softc, M_BUS);
1122 dev->ops = &null_ops;
1123 dev->driver = driver;
1125 dev->ops = driver->ops;
1126 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1127 dev->softc = malloc(driver->softc, M_BUS, M_NOWAIT);
1129 dev->ops = &null_ops;
1133 bzero(dev->softc, driver->softc);
1140 device_probe_and_attach(device_t dev)
1142 device_t bus = dev->parent;
1144 int hasclass = (dev->devclass != 0);
1146 if (dev->state >= DS_ALIVE)
1149 if (dev->flags & DF_ENABLED) {
1150 error = device_probe_child(bus, dev);
1152 if (!device_is_quiet(dev))
1153 device_print_child(bus, dev);
1154 error = DEVICE_ATTACH(dev);
1156 dev->state = DS_ATTACHED;
1158 printf("device_probe_and_attach: %s%d attach returned %d\n",
1159 dev->driver->name, dev->unit, error);
1160 /* Unset the class that was set in device_probe_child */
1162 device_set_devclass(dev, 0);
1163 device_set_driver(dev, NULL);
1164 dev->state = DS_NOTPRESENT;
1167 if (!(dev->flags & DF_DONENOMATCH)) {
1168 BUS_PROBE_NOMATCH(bus, dev);
1169 dev->flags |= DF_DONENOMATCH;
1174 device_print_prettyname(dev);
1175 printf("not probed (disabled)\n");
1183 device_detach(device_t dev)
1187 PDEBUG(("%s", DEVICENAME(dev)));
1188 if (dev->state == DS_BUSY)
1190 if (dev->state != DS_ATTACHED)
1193 if ((error = DEVICE_DETACH(dev)) != 0)
1195 device_printf(dev, "detached\n");
1197 BUS_CHILD_DETACHED(dev->parent, dev);
1199 if (!(dev->flags & DF_FIXEDCLASS))
1200 devclass_delete_device(dev->devclass, dev);
1202 dev->state = DS_NOTPRESENT;
1203 device_set_driver(dev, NULL);
1209 device_shutdown(device_t dev)
1211 if (dev->state < DS_ATTACHED)
1213 return DEVICE_SHUTDOWN(dev);
1217 device_set_unit(device_t dev, int unit)
1222 dc = device_get_devclass(dev);
1223 if (unit < dc->maxunit && dc->devices[unit])
1225 err = devclass_delete_device(dc, dev);
1229 err = devclass_add_device(dc, dev);
1235 #ifdef DEVICE_SYSCTLS
1238 * Sysctl nodes for devices.
1241 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1244 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1246 device_t dev = arg1;
1248 int first = 1, error = 0;
1250 for (child = TAILQ_FIRST(&dev->children); child;
1251 child = TAILQ_NEXT(child, link)) {
1252 if (child->nameunit) {
1254 error = SYSCTL_OUT(req, ",", 1);
1255 if (error) return error;
1259 error = SYSCTL_OUT(req, child->nameunit, strlen(child->nameunit));
1260 if (error) return error;
1264 error = SYSCTL_OUT(req, "", 1);
1270 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1272 device_t dev = arg1;
1274 switch (dev->state) {
1276 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1278 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1280 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1282 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1289 device_register_oids(device_t dev)
1291 struct sysctl_oid* oid;
1294 bzero(oid, sizeof(*oid));
1295 oid->oid_parent = &sysctl__hw_devices_children;
1296 oid->oid_number = OID_AUTO;
1297 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1298 oid->oid_arg1 = &dev->oidlist[0];
1300 oid->oid_name = dev->nameunit;
1301 oid->oid_handler = 0;
1303 SLIST_INIT(&dev->oidlist[0]);
1304 sysctl_register_oid(oid);
1307 bzero(oid, sizeof(*oid));
1308 oid->oid_parent = &dev->oidlist[0];
1309 oid->oid_number = OID_AUTO;
1310 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1311 oid->oid_arg1 = dev->desc ? dev->desc : "";
1312 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1313 oid->oid_name = "desc";
1314 oid->oid_handler = sysctl_handle_string;
1316 sysctl_register_oid(oid);
1319 bzero(oid, sizeof(*oid));
1320 oid->oid_parent = &dev->oidlist[0];
1321 oid->oid_number = OID_AUTO;
1322 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1323 oid->oid_arg1 = dev;
1325 oid->oid_name = "children";
1326 oid->oid_handler = sysctl_handle_children;
1328 sysctl_register_oid(oid);
1331 bzero(oid, sizeof(*oid));
1332 oid->oid_parent = &dev->oidlist[0];
1333 oid->oid_number = OID_AUTO;
1334 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1335 oid->oid_arg1 = dev;
1337 oid->oid_name = "state";
1338 oid->oid_handler = sysctl_handle_state;
1340 sysctl_register_oid(oid);
1344 device_unregister_oids(device_t dev)
1346 sysctl_unregister_oid(&dev->oid[0]);
1347 sysctl_unregister_oid(&dev->oid[1]);
1348 sysctl_unregister_oid(&dev->oid[2]);
1353 /*======================================*/
1355 * Access functions for device resources.
1358 /* Supplied by config(8) in ioconf.c */
1359 extern struct config_device config_devtab[];
1360 extern int devtab_count;
1362 /* Runtime version */
1363 struct config_device *devtab = config_devtab;
1366 resource_new_name(const char *name, int unit)
1368 struct config_device *new;
1370 new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
1373 if (devtab && devtab_count > 0)
1374 bcopy(devtab, new, devtab_count * sizeof(*new));
1375 bzero(&new[devtab_count], sizeof(*new));
1376 new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_NOWAIT);
1377 if (new[devtab_count].name == NULL) {
1381 strcpy(new[devtab_count].name, name);
1382 new[devtab_count].unit = unit;
1383 new[devtab_count].resource_count = 0;
1384 new[devtab_count].resources = NULL;
1386 return devtab_count++;
1390 resource_new_resname(int j, const char *resname, resource_type type)
1392 struct config_resource *new;
1395 i = devtab[j].resource_count;
1396 new = malloc((i + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
1399 if (devtab[j].resources && i > 0)
1400 bcopy(devtab[j].resources, new, i * sizeof(*new));
1401 bzero(&new[i], sizeof(*new));
1402 new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_NOWAIT);
1403 if (new[i].name == NULL) {
1407 strcpy(new[i].name, resname);
1409 if (devtab[j].resources)
1410 free(devtab[j].resources, M_TEMP);
1411 devtab[j].resources = new;
1412 devtab[j].resource_count = i + 1;
1417 resource_match_string(int i, const char *resname, const char *value)
1420 struct config_resource *res;
1422 for (j = 0, res = devtab[i].resources;
1423 j < devtab[i].resource_count; j++, res++)
1424 if (!strcmp(res->name, resname)
1425 && res->type == RES_STRING
1426 && !strcmp(res->u.stringval, value))
1432 resource_find(const char *name, int unit, const char *resname,
1433 struct config_resource **result)
1436 struct config_resource *res;
1439 * First check specific instances, then generic.
1441 for (i = 0; i < devtab_count; i++) {
1442 if (devtab[i].unit < 0)
1444 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1445 res = devtab[i].resources;
1446 for (j = 0; j < devtab[i].resource_count; j++, res++)
1447 if (!strcmp(res->name, resname)) {
1453 for (i = 0; i < devtab_count; i++) {
1454 if (devtab[i].unit >= 0)
1456 /* XXX should this `&& devtab[i].unit == unit' be here? */
1457 /* XXX if so, then the generic match does nothing */
1458 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1459 res = devtab[i].resources;
1460 for (j = 0; j < devtab[i].resource_count; j++, res++)
1461 if (!strcmp(res->name, resname)) {
1471 resource_int_value(const char *name, int unit, const char *resname, int *result)
1474 struct config_resource *res;
1476 if ((error = resource_find(name, unit, resname, &res)) != 0)
1478 if (res->type != RES_INT)
1480 *result = res->u.intval;
1485 resource_long_value(const char *name, int unit, const char *resname,
1489 struct config_resource *res;
1491 if ((error = resource_find(name, unit, resname, &res)) != 0)
1493 if (res->type != RES_LONG)
1495 *result = res->u.longval;
1500 resource_string_value(const char *name, int unit, const char *resname,
1504 struct config_resource *res;
1506 if ((error = resource_find(name, unit, resname, &res)) != 0)
1508 if (res->type != RES_STRING)
1510 *result = res->u.stringval;
1515 resource_query_string(int i, const char *resname, const char *value)
1521 for (; i < devtab_count; i++)
1522 if (resource_match_string(i, resname, value) >= 0)
1528 resource_locate(int i, const char *resname)
1534 for (; i < devtab_count; i++)
1535 if (!strcmp(devtab[i].name, resname))
1541 resource_count(void)
1543 return devtab_count;
1547 resource_query_name(int i)
1549 return devtab[i].name;
1553 resource_query_unit(int i)
1555 return devtab[i].unit;
1559 resource_create(const char *name, int unit, const char *resname,
1560 resource_type type, struct config_resource **result)
1563 struct config_resource *res = NULL;
1565 for (i = 0; i < devtab_count; i++) {
1566 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1567 res = devtab[i].resources;
1572 i = resource_new_name(name, unit);
1575 res = devtab[i].resources;
1577 for (j = 0; j < devtab[i].resource_count; j++, res++) {
1578 if (!strcmp(res->name, resname)) {
1583 j = resource_new_resname(i, resname, type);
1586 res = &devtab[i].resources[j];
1592 resource_set_int(const char *name, int unit, const char *resname, int value)
1595 struct config_resource *res;
1597 error = resource_create(name, unit, resname, RES_INT, &res);
1600 if (res->type != RES_INT)
1602 res->u.intval = value;
1607 resource_set_long(const char *name, int unit, const char *resname, long value)
1610 struct config_resource *res;
1612 error = resource_create(name, unit, resname, RES_LONG, &res);
1615 if (res->type != RES_LONG)
1617 res->u.longval = value;
1622 resource_set_string(const char *name, int unit, const char *resname,
1626 struct config_resource *res;
1628 error = resource_create(name, unit, resname, RES_STRING, &res);
1631 if (res->type != RES_STRING)
1633 if (res->u.stringval)
1634 free(res->u.stringval, M_TEMP);
1635 res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_NOWAIT);
1636 if (res->u.stringval == NULL)
1638 strcpy(res->u.stringval, value);
1644 resource_cfgload(void *dummy __unused)
1646 struct config_resource *res, *cfgres;
1649 char *name, *resname;
1653 int config_devtab_count;
1655 config_devtab_count = devtab_count;
1659 for (i = 0; i < config_devtab_count; i++) {
1660 name = config_devtab[i].name;
1661 unit = config_devtab[i].unit;
1663 for (j = 0; j < config_devtab[i].resource_count; j++) {
1664 cfgres = config_devtab[i].resources;
1665 resname = cfgres[j].name;
1666 type = cfgres[j].type;
1667 error = resource_create(name, unit, resname, type,
1670 printf("create resource %s%d: error %d\n",
1674 if (res->type != type) {
1675 printf("type mismatch %s%d: %d != %d\n",
1676 name, unit, res->type, type);
1681 res->u.intval = cfgres[j].u.intval;
1684 res->u.longval = cfgres[j].u.longval;
1687 if (res->u.stringval)
1688 free(res->u.stringval, M_TEMP);
1689 stringval = cfgres[j].u.stringval;
1690 res->u.stringval = malloc(strlen(stringval) + 1,
1692 if (res->u.stringval == NULL)
1694 strcpy(res->u.stringval, stringval);
1697 panic("unknown resource type %d\n", type);
1702 SYSINIT(cfgload, SI_SUB_KMEM, SI_ORDER_ANY + 50, resource_cfgload, 0)
1705 /*======================================*/
1707 * Some useful method implementations to make life easier for bus drivers.
1711 resource_list_init(struct resource_list *rl)
1717 resource_list_free(struct resource_list *rl)
1719 struct resource_list_entry *rle;
1721 while ((rle = SLIST_FIRST(rl)) != NULL) {
1723 panic("resource_list_free: resource entry is busy");
1724 SLIST_REMOVE_HEAD(rl, link);
1730 resource_list_add(struct resource_list *rl,
1732 u_long start, u_long end, u_long count)
1734 struct resource_list_entry *rle;
1736 rle = resource_list_find(rl, type, rid);
1738 rle = malloc(sizeof(struct resource_list_entry), M_BUS, M_NOWAIT);
1740 panic("resource_list_add: can't record entry");
1741 SLIST_INSERT_HEAD(rl, rle, link);
1748 panic("resource_list_add: resource entry is busy");
1755 struct resource_list_entry*
1756 resource_list_find(struct resource_list *rl,
1759 struct resource_list_entry *rle;
1761 SLIST_FOREACH(rle, rl, link)
1762 if (rle->type == type && rle->rid == rid)
1768 resource_list_delete(struct resource_list *rl,
1771 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1774 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1780 resource_list_alloc(struct resource_list *rl,
1781 device_t bus, device_t child,
1783 u_long start, u_long end,
1784 u_long count, u_int flags)
1786 struct resource_list_entry *rle = 0;
1787 int passthrough = (device_get_parent(child) != bus);
1788 int isdefault = (start == 0UL && end == ~0UL);
1791 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1793 start, end, count, flags);
1796 rle = resource_list_find(rl, type, *rid);
1799 return 0; /* no resource of that type/rid */
1801 panic("resource_list_alloc: resource entry is busy");
1805 count = max(count, rle->count);
1806 end = max(rle->end, start + count - 1);
1809 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1810 type, rid, start, end, count, flags);
1813 * Record the new range.
1816 rle->start = rman_get_start(rle->res);
1817 rle->end = rman_get_end(rle->res);
1825 resource_list_release(struct resource_list *rl,
1826 device_t bus, device_t child,
1827 int type, int rid, struct resource *res)
1829 struct resource_list_entry *rle = 0;
1830 int passthrough = (device_get_parent(child) != bus);
1834 return BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1838 rle = resource_list_find(rl, type, rid);
1841 panic("resource_list_release: can't find resource");
1843 panic("resource_list_release: resource entry is not busy");
1845 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1855 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1858 struct resource_list_entry *rle;
1859 int printed, retval;
1863 /* Yes, this is kinda cheating */
1864 SLIST_FOREACH(rle, rl, link) {
1865 if (rle->type == type) {
1867 retval += printf(" %s ", name);
1869 retval += printf(",");
1871 retval += printf(format, rle->start);
1872 if (rle->count > 1) {
1873 retval += printf("-");
1874 retval += printf(format, rle->start +
1883 * Call DEVICE_IDENTIFY for each driver.
1886 bus_generic_probe(device_t dev)
1888 devclass_t dc = dev->devclass;
1891 for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link))
1892 DEVICE_IDENTIFY(dl->driver, dev);
1898 bus_generic_attach(device_t dev)
1902 for (child = TAILQ_FIRST(&dev->children);
1903 child; child = TAILQ_NEXT(child, link))
1904 device_probe_and_attach(child);
1910 bus_generic_detach(device_t dev)
1915 if (dev->state != DS_ATTACHED)
1918 for (child = TAILQ_FIRST(&dev->children);
1919 child; child = TAILQ_NEXT(child, link))
1920 if ((error = device_detach(child)) != 0)
1927 bus_generic_shutdown(device_t dev)
1931 for (child = TAILQ_FIRST(&dev->children);
1932 child; child = TAILQ_NEXT(child, link))
1933 device_shutdown(child);
1939 bus_generic_suspend(device_t dev)
1942 device_t child, child2;
1944 for (child = TAILQ_FIRST(&dev->children);
1945 child; child = TAILQ_NEXT(child, link)) {
1946 error = DEVICE_SUSPEND(child);
1948 for (child2 = TAILQ_FIRST(&dev->children);
1949 child2 && child2 != child;
1950 child2 = TAILQ_NEXT(child2, link))
1951 DEVICE_RESUME(child2);
1959 bus_generic_resume(device_t dev)
1963 for (child = TAILQ_FIRST(&dev->children);
1964 child; child = TAILQ_NEXT(child, link)) {
1965 DEVICE_RESUME(child);
1966 /* if resume fails, there's nothing we can usefully do... */
1972 bus_print_child_header (device_t dev, device_t child)
1976 if (device_get_desc(child)) {
1977 retval += device_printf(child, "<%s>",
1978 device_get_desc(child));
1980 retval += printf("%s", device_get_nameunit(child));
1987 bus_print_child_footer (device_t dev, device_t child)
1989 return(printf(" on %s\n", device_get_nameunit(dev)));
1993 bus_generic_print_child(device_t dev, device_t child)
1997 retval += bus_print_child_header(dev, child);
1998 retval += bus_print_child_footer(dev, child);
2004 bus_generic_read_ivar(device_t dev, device_t child, int index,
2011 bus_generic_write_ivar(device_t dev, device_t child, int index,
2018 bus_generic_driver_added(device_t dev, driver_t *driver)
2022 DEVICE_IDENTIFY(driver, dev);
2023 for (child = TAILQ_FIRST(&dev->children);
2024 child; child = TAILQ_NEXT(child, link))
2025 if (child->state == DS_NOTPRESENT)
2026 device_probe_and_attach(child);
2030 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2031 int flags, driver_intr_t *intr, void *arg,
2034 /* Propagate up the bus hierarchy until someone handles it. */
2036 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
2037 intr, arg, cookiep));
2043 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2046 /* Propagate up the bus hierarchy until someone handles it. */
2048 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2054 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2055 u_long start, u_long end, u_long count, u_int flags)
2057 /* Propagate up the bus hierarchy until someone handles it. */
2059 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2060 start, end, count, flags));
2066 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2069 /* Propagate up the bus hierarchy until someone handles it. */
2071 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
2078 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2081 /* Propagate up the bus hierarchy until someone handles it. */
2083 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
2090 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2091 int rid, struct resource *r)
2093 /* Propagate up the bus hierarchy until someone handles it. */
2095 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2102 * Some convenience functions to make it easier for drivers to use the
2103 * resource-management functions. All these really do is hide the
2104 * indirection through the parent's method table, making for slightly
2105 * less-wordy code. In the future, it might make sense for this code
2106 * to maintain some sort of a list of resources allocated by each device.
2109 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2110 u_long count, u_int flags)
2112 if (dev->parent == 0)
2114 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2119 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2121 if (dev->parent == 0)
2123 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2127 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2129 if (dev->parent == 0)
2131 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2135 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2137 if (dev->parent == 0)
2139 return (BUS_RELEASE_RESOURCE(dev->parent, dev,
2144 bus_setup_intr(device_t dev, struct resource *r, int flags,
2145 driver_intr_t handler, void *arg, void **cookiep)
2147 if (dev->parent == 0)
2149 return (BUS_SETUP_INTR(dev->parent, dev, r, flags,
2150 handler, arg, cookiep));
2154 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2156 if (dev->parent == 0)
2158 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2162 bus_set_resource(device_t dev, int type, int rid,
2163 u_long start, u_long count)
2165 return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2170 bus_get_resource(device_t dev, int type, int rid,
2171 u_long *startp, u_long *countp)
2173 return BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2178 bus_get_resource_start(device_t dev, int type, int rid)
2180 u_long start, count;
2183 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2191 bus_get_resource_count(device_t dev, int type, int rid)
2193 u_long start, count;
2196 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2204 bus_delete_resource(device_t dev, int type, int rid)
2206 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2210 root_print_child(device_t dev, device_t child)
2216 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2220 * If an interrupt mapping gets to here something bad has happened.
2222 panic("root_setup_intr");
2225 static device_method_t root_methods[] = {
2226 /* Device interface */
2227 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2228 DEVMETHOD(device_suspend, bus_generic_suspend),
2229 DEVMETHOD(device_resume, bus_generic_resume),
2232 DEVMETHOD(bus_print_child, root_print_child),
2233 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
2234 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
2235 DEVMETHOD(bus_setup_intr, root_setup_intr),
2240 static driver_t root_driver = {
2247 devclass_t root_devclass;
2250 root_bus_module_handler(module_t mod, int what, void* arg)
2254 compile_methods(&root_driver);
2255 root_bus = make_device(NULL, "root", 0);
2256 root_bus->desc = "System root bus";
2257 root_bus->ops = root_driver.ops;
2258 root_bus->driver = &root_driver;
2259 root_bus->state = DS_ATTACHED;
2260 root_devclass = devclass_find_internal("root", FALSE);
2264 device_shutdown(root_bus);
2271 static moduledata_t root_bus_mod = {
2273 root_bus_module_handler,
2276 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2279 root_bus_configure(void)
2285 for (dev = TAILQ_FIRST(&root_bus->children); dev;
2286 dev = TAILQ_NEXT(dev, link)) {
2287 device_probe_and_attach(dev);
2292 driver_module_handler(module_t mod, int what, void *arg)
2295 struct driver_module_data *dmd;
2296 devclass_t bus_devclass;
2298 dmd = (struct driver_module_data *)arg;
2299 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE);
2304 if (dmd->dmd_chainevh)
2305 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2307 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2308 PDEBUG(("Loading module: driver %s on bus %s",
2309 DRIVERNAME(dmd->dmd_drivers[i]),
2311 error = devclass_add_driver(bus_devclass,
2312 dmd->dmd_drivers[i]);
2318 * The drivers loaded in this way are assumed to all
2319 * implement the same devclass.
2321 *dmd->dmd_devclass =
2322 devclass_find_internal(dmd->dmd_drivers[0]->name,
2327 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2328 PDEBUG(("Unloading module: driver %s from bus %s",
2329 DRIVERNAME(dmd->dmd_drivers[i]),
2331 error = devclass_delete_driver(bus_devclass,
2332 dmd->dmd_drivers[i]);
2335 if (!error && dmd->dmd_chainevh)
2336 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2345 /* the _short versions avoid iteration by not calling anything that prints
2346 * more than oneliners. I love oneliners.
2350 print_method_list(device_method_t *m, int indent)
2357 for (i = 0; m->desc; i++, m++)
2358 indentprintf(("method %d: %s, offset=%d\n",
2359 i, m->desc->name, m->desc->offset));
2363 print_device_ops(device_ops_t ops, int indent)
2371 /* we present a list of the methods that are pointing to the
2372 * error_method, but ignore the 0'th elements; it is always
2375 for (i = 1; i < ops->maxoffset; i++) {
2376 if (ops->methods[i] == error_method) {
2378 indentprintf(("error_method:"));
2386 indentprintf(("(%d method%s, %d valid, %d error_method%s)\n",
2387 ops->maxoffset-1, (ops->maxoffset-1 == 1? "":"s"),
2388 ops->maxoffset-1-count,
2389 count, (count == 1? "":"'s")));
2393 print_device_short(device_t dev, int indent)
2398 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2399 dev->unit, dev->desc,
2400 (dev->parent? "":"no "),
2401 (TAILQ_EMPTY(&dev->children)? "no ":""),
2402 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2403 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2404 (dev->flags&DF_WILDCARD? "wildcard,":""),
2405 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2406 (dev->ivars? "":"no "),
2407 (dev->softc? "":"no "),
2412 print_device(device_t dev, int indent)
2417 print_device_short(dev, indent);
2419 indentprintf(("Parent:\n"));
2420 print_device_short(dev->parent, indent+1);
2421 indentprintf(("Methods:\n"));
2422 print_device_ops(dev->ops, indent+1);
2423 indentprintf(("Driver:\n"));
2424 print_driver_short(dev->driver, indent+1);
2425 indentprintf(("Devclass:\n"));
2426 print_devclass_short(dev->devclass, indent+1);
2430 print_device_tree_short(device_t dev, int indent)
2431 /* print the device and all its children (indented) */
2438 print_device_short(dev, indent);
2440 for (child = TAILQ_FIRST(&dev->children); child;
2441 child = TAILQ_NEXT(child, link))
2442 print_device_tree_short(child, indent+1);
2446 print_device_tree(device_t dev, int indent)
2447 /* print the device and all its children (indented) */
2454 print_device(dev, indent);
2456 for (child = TAILQ_FIRST(&dev->children); child;
2457 child = TAILQ_NEXT(child, link))
2458 print_device_tree(child, indent+1);
2462 print_driver_short(driver_t *driver, int indent)
2467 indentprintf(("driver %s: softc size = %d\n",
2468 driver->name, driver->softc));
2472 print_driver(driver_t *driver, int indent)
2477 print_driver_short(driver, indent);
2478 indentprintf(("Methods:\n"));
2479 print_method_list(driver->methods, indent+1);
2480 indentprintf(("Operations:\n"));
2481 print_device_ops(driver->ops, indent+1);
2486 print_driver_list(driver_list_t drivers, int indent)
2488 driverlink_t driver;
2490 for (driver = TAILQ_FIRST(&drivers); driver;
2491 driver = TAILQ_NEXT(driver, link))
2492 print_driver(driver->driver, indent);
2496 print_devclass_short(devclass_t dc, int indent)
2501 indentprintf(("devclass %s: max units = %d\n",
2502 dc->name, dc->maxunit));
2506 print_devclass(devclass_t dc, int indent)
2513 print_devclass_short(dc, indent);
2514 indentprintf(("Drivers:\n"));
2515 print_driver_list(dc->drivers, indent+1);
2517 indentprintf(("Devices:\n"));
2518 for (i = 0; i < dc->maxunit; i++)
2520 print_device(dc->devices[i], indent+1);
2524 print_devclass_list_short(void)
2528 printf("Short listing of devclasses, drivers & devices:\n");
2529 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
2530 print_devclass_short(dc, 0);
2534 print_devclass_list(void)
2538 printf("Full listing of devclasses, drivers & devices:\n");
2539 for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
2540 print_devclass(dc, 0);