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
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <machine/bus.h>
42 #include <machine/stdarg.h> /* for device_printf() */
44 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
48 static int bus_debug = 1;
49 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
52 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a, printf("\n");}
53 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
54 #define DRIVERNAME(d) ((d)? d->name : "no driver")
55 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
57 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
58 * prevent syslog from deleting initial spaces
60 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
62 static void print_device_short(device_t dev, int indent);
63 static void print_device(device_t dev, int indent);
64 void print_device_tree_short(device_t dev, int indent);
65 void print_device_tree(device_t dev, int indent);
66 static void print_driver_short(driver_t *driver, int indent);
67 static void print_driver(driver_t *driver, int indent);
68 static void print_driver_list(driver_list_t drivers, int indent);
69 static void print_devclass_short(devclass_t dc, int indent);
70 static void print_devclass(devclass_t dc, int indent);
71 void print_devclass_list_short(void);
72 void print_devclass_list(void);
75 /* Make the compiler ignore the function calls */
76 #define PDEBUG(a) /* nop */
77 #define DEVICENAME(d) /* nop */
78 #define DRIVERNAME(d) /* nop */
79 #define DEVCLANAME(d) /* nop */
81 #define print_device_short(d,i) /* nop */
82 #define print_device(d,i) /* nop */
83 #define print_device_tree_short(d,i) /* nop */
84 #define print_device_tree(d,i) /* nop */
85 #define print_driver_short(d,i) /* nop */
86 #define print_driver(d,i) /* nop */
87 #define print_driver_list(d,i) /* nop */
88 #define print_devclass_short(d,i) /* nop */
89 #define print_devclass(d,i) /* nop */
90 #define print_devclass_list_short() /* nop */
91 #define print_devclass_list() /* nop */
94 TAILQ_HEAD(,device) bus_data_devices;
95 static int bus_data_generation = 1;
97 kobj_method_t null_methods[] = {
101 DEFINE_CLASS(null, null_methods, 0);
104 * Devclass implementation
107 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
110 devclass_find_internal(const char *classname, int create)
114 PDEBUG(("looking for %s", classname));
118 TAILQ_FOREACH(dc, &devclasses, link) {
119 if (!strcmp(dc->name, classname))
123 PDEBUG(("%s not found%s", classname, (create? ", creating": "")));
125 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
126 M_BUS, M_NOWAIT|M_ZERO);
129 dc->name = (char*) (dc + 1);
130 strcpy(dc->name, classname);
131 TAILQ_INIT(&dc->drivers);
132 TAILQ_INSERT_TAIL(&devclasses, dc, link);
134 bus_data_generation_update();
141 devclass_create(const char *classname)
143 return (devclass_find_internal(classname, TRUE));
147 devclass_find(const char *classname)
149 return (devclass_find_internal(classname, FALSE));
153 devclass_add_driver(devclass_t dc, driver_t *driver)
158 PDEBUG(("%s", DRIVERNAME(driver)));
160 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
165 * Compile the driver's methods. Also increase the reference count
166 * so that the class doesn't get freed when the last instance
167 * goes. This means we can safely use static methods and avoids a
168 * double-free in devclass_delete_driver.
170 kobj_class_compile((kobj_class_t) driver);
173 * Make sure the devclass which the driver is implementing exists.
175 devclass_find_internal(driver->name, TRUE);
178 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
182 * Call BUS_DRIVER_ADDED for any existing busses in this class.
184 for (i = 0; i < dc->maxunit; i++)
186 BUS_DRIVER_ADDED(dc->devices[i], driver);
188 bus_data_generation_update();
193 devclass_delete_driver(devclass_t busclass, driver_t *driver)
195 devclass_t dc = devclass_find(driver->name);
201 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
207 * Find the link structure in the bus' list of drivers.
209 TAILQ_FOREACH(dl, &busclass->drivers, link) {
210 if (dl->driver == driver)
215 PDEBUG(("%s not found in %s list", driver->name,
221 * Disassociate from any devices. We iterate through all the
222 * devices in the devclass of the driver and detach any which are
223 * using the driver and which have a parent in the devclass which
224 * we are deleting from.
226 * Note that since a driver can be in multiple devclasses, we
227 * should not detach devices which are not children of devices in
228 * the affected devclass.
230 for (i = 0; i < dc->maxunit; i++) {
231 if (dc->devices[i]) {
232 dev = dc->devices[i];
233 if (dev->driver == driver && dev->parent &&
234 dev->parent->devclass == busclass) {
235 if ((error = device_detach(dev)) != 0)
237 device_set_driver(dev, NULL);
242 TAILQ_REMOVE(&busclass->drivers, dl, link);
246 if (driver->refs == 0)
247 kobj_class_free((kobj_class_t) driver);
249 bus_data_generation_update();
254 devclass_find_driver_internal(devclass_t dc, const char *classname)
258 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
260 TAILQ_FOREACH(dl, &dc->drivers, link) {
261 if (!strcmp(dl->driver->name, classname))
265 PDEBUG(("not found"));
270 devclass_find_driver(devclass_t dc, const char *classname)
274 dl = devclass_find_driver_internal(dc, classname);
281 devclass_get_name(devclass_t dc)
287 devclass_get_device(devclass_t dc, int unit)
289 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
291 return (dc->devices[unit]);
295 devclass_get_softc(devclass_t dc, int unit)
299 dev = devclass_get_device(dc, unit);
303 return (device_get_softc(dev));
307 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
314 for (i = 0; i < dc->maxunit; i++)
318 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
323 for (i = 0; i < dc->maxunit; i++) {
324 if (dc->devices[i]) {
325 list[count] = dc->devices[i];
337 devclass_get_maxunit(devclass_t dc)
339 return (dc->maxunit);
343 devclass_find_free_unit(devclass_t dc, int unit)
347 while (unit < dc->maxunit && dc->devices[unit] != NULL)
353 devclass_alloc_unit(devclass_t dc, int *unitp)
357 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
359 /* If we were given a wired unit number, check for existing device */
362 if (unit >= 0 && unit < dc->maxunit &&
363 dc->devices[unit] != NULL) {
365 printf("%s: %s%d already exists; skipping it\n",
366 dc->name, dc->name, *unitp);
370 /* Unwired device, find the next available slot for it */
372 while (unit < dc->maxunit && dc->devices[unit] != NULL)
377 * We've selected a unit beyond the length of the table, so let's
378 * extend the table to make room for all units up to and including
381 if (unit >= dc->maxunit) {
385 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
386 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
389 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
390 bzero(newlist + dc->maxunit,
391 sizeof(device_t) * (newsize - dc->maxunit));
393 free(dc->devices, M_BUS);
394 dc->devices = newlist;
395 dc->maxunit = newsize;
397 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
404 devclass_add_device(devclass_t dc, device_t dev)
408 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
410 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
413 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
417 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
418 free(dev->nameunit, M_BUS);
419 dev->nameunit = NULL;
422 dc->devices[dev->unit] = dev;
424 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
430 devclass_delete_device(devclass_t dc, device_t dev)
435 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
437 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
438 panic("devclass_delete_device: inconsistent device class");
439 dc->devices[dev->unit] = NULL;
440 if (dev->flags & DF_WILDCARD)
442 dev->devclass = NULL;
443 free(dev->nameunit, M_BUS);
444 dev->nameunit = NULL;
450 make_device(device_t parent, const char *name, int unit)
455 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
458 dc = devclass_find_internal(name, TRUE);
460 printf("make_device: can't find device class %s\n",
468 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
472 dev->parent = parent;
473 TAILQ_INIT(&dev->children);
474 kobj_init((kobj_t) dev, &null_class);
476 dev->devclass = NULL;
478 dev->nameunit = NULL;
482 dev->flags = DF_ENABLED;
485 dev->flags |= DF_WILDCARD;
487 dev->flags |= DF_FIXEDCLASS;
488 if (devclass_add_device(dc, dev)) {
489 kobj_delete((kobj_t) dev, M_BUS);
496 dev->state = DS_NOTPRESENT;
498 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
499 bus_data_generation_update();
505 device_print_child(device_t dev, device_t child)
509 if (device_is_alive(child))
510 retval += BUS_PRINT_CHILD(dev, child);
512 retval += device_printf(child, " not found\n");
518 device_add_child(device_t dev, const char *name, int unit)
520 return (device_add_child_ordered(dev, 0, name, unit));
524 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
529 PDEBUG(("%s at %s with order %d as unit %d",
530 name, DEVICENAME(dev), order, unit));
532 child = make_device(dev, name, unit);
535 child->order = order;
537 TAILQ_FOREACH(place, &dev->children, link) {
538 if (place->order > order)
544 * The device 'place' is the first device whose order is
545 * greater than the new child.
547 TAILQ_INSERT_BEFORE(place, child, link);
550 * The new child's order is greater or equal to the order of
551 * any existing device. Add the child to the tail of the list.
553 TAILQ_INSERT_TAIL(&dev->children, child, link);
556 bus_data_generation_update();
561 device_delete_child(device_t dev, device_t child)
566 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
568 /* remove children first */
569 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
570 error = device_delete_child(child, grandchild);
575 if ((error = device_detach(child)) != 0)
578 devclass_delete_device(child->devclass, child);
579 TAILQ_REMOVE(&dev->children, child, link);
580 TAILQ_REMOVE(&bus_data_devices, child, devlink);
581 device_set_desc(child, NULL);
584 bus_data_generation_update();
589 * Find only devices attached to this bus.
592 device_find_child(device_t dev, const char *classname, int unit)
597 dc = devclass_find(classname);
601 child = devclass_get_device(dc, unit);
602 if (child && child->parent == dev)
608 first_matching_driver(devclass_t dc, device_t dev)
611 return (devclass_find_driver_internal(dc, dev->devclass->name));
612 return (TAILQ_FIRST(&dc->drivers));
616 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
620 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
621 if (!strcmp(dev->devclass->name, dl->driver->name))
625 return (TAILQ_NEXT(last, link));
629 device_probe_child(device_t dev, device_t child)
632 driverlink_t best = 0;
635 int hasclass = (child->devclass != 0);
639 panic("device_probe_child: parent device has no devclass");
641 if (child->state == DS_ALIVE)
644 for (dl = first_matching_driver(dc, child);
646 dl = next_matching_driver(dc, child, dl)) {
647 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
648 device_set_driver(child, dl->driver);
650 device_set_devclass(child, dl->driver->name);
651 result = DEVICE_PROBE(child);
653 device_set_devclass(child, 0);
656 * If the driver returns SUCCESS, there can be no higher match
666 * The driver returned an error so it certainly doesn't match.
669 device_set_driver(child, 0);
674 * A priority lower than SUCCESS, remember the best matching
675 * driver. Initialise the value of pri for the first match.
677 if (best == 0 || result > pri) {
685 * If we found a driver, change state and initialise the devclass.
688 if (!child->devclass)
689 device_set_devclass(child, best->driver->name);
690 device_set_driver(child, best->driver);
693 * A bit bogus. Call the probe method again to make
694 * sure that we have the right description.
698 child->state = DS_ALIVE;
700 bus_data_generation_update();
708 device_get_parent(device_t dev)
710 return (dev->parent);
714 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
721 TAILQ_FOREACH(child, &dev->children, link) {
725 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
730 TAILQ_FOREACH(child, &dev->children, link) {
742 device_get_driver(device_t dev)
744 return (dev->driver);
748 device_get_devclass(device_t dev)
750 return (dev->devclass);
754 device_get_name(device_t dev)
757 return (devclass_get_name(dev->devclass));
762 device_get_nameunit(device_t dev)
764 return (dev->nameunit);
768 device_get_unit(device_t dev)
774 device_get_desc(device_t dev)
780 device_get_flags(device_t dev)
782 return (dev->devflags);
786 device_print_prettyname(device_t dev)
788 const char *name = device_get_name(dev);
791 return (printf("unknown: "));
792 return (printf("%s%d: ", name, device_get_unit(dev)));
796 device_printf(device_t dev, const char * fmt, ...)
801 retval = device_print_prettyname(dev);
803 retval += vprintf(fmt, ap);
809 device_set_desc_internal(device_t dev, const char* desc, int copy)
811 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
812 free(dev->desc, M_BUS);
813 dev->flags &= ~DF_DESCMALLOCED;
818 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
820 strcpy(dev->desc, desc);
821 dev->flags |= DF_DESCMALLOCED;
824 /* Avoid a -Wcast-qual warning */
825 dev->desc = (char *)(uintptr_t) desc;
828 bus_data_generation_update();
832 device_set_desc(device_t dev, const char* desc)
834 device_set_desc_internal(dev, desc, FALSE);
838 device_set_desc_copy(device_t dev, const char* desc)
840 device_set_desc_internal(dev, desc, TRUE);
844 device_set_flags(device_t dev, u_int32_t flags)
846 dev->devflags = flags;
850 device_get_softc(device_t dev)
856 device_set_softc(device_t dev, void *softc)
858 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
859 free(dev->softc, M_BUS);
862 dev->flags |= DF_EXTERNALSOFTC;
864 dev->flags &= ~DF_EXTERNALSOFTC;
868 device_get_ivars(device_t dev)
874 device_set_ivars(device_t dev, void * ivars)
885 device_get_state(device_t dev)
891 device_enable(device_t dev)
893 dev->flags |= DF_ENABLED;
897 device_disable(device_t dev)
899 dev->flags &= ~DF_ENABLED;
903 device_busy(device_t dev)
905 if (dev->state < DS_ATTACHED)
906 panic("device_busy: called for unattached device");
907 if (dev->busy == 0 && dev->parent)
908 device_busy(dev->parent);
910 dev->state = DS_BUSY;
914 device_unbusy(device_t dev)
916 if (dev->state != DS_BUSY)
917 panic("device_unbusy: called for non-busy device");
919 if (dev->busy == 0) {
921 device_unbusy(dev->parent);
922 dev->state = DS_ATTACHED;
927 device_quiet(device_t dev)
929 dev->flags |= DF_QUIET;
933 device_verbose(device_t dev)
935 dev->flags &= ~DF_QUIET;
939 device_is_quiet(device_t dev)
941 return ((dev->flags & DF_QUIET) != 0);
945 device_is_enabled(device_t dev)
947 return ((dev->flags & DF_ENABLED) != 0);
951 device_is_alive(device_t dev)
953 return (dev->state >= DS_ALIVE);
957 device_set_devclass(device_t dev, const char *classname)
964 devclass_delete_device(dev->devclass, dev);
969 printf("device_set_devclass: device class already set\n");
973 dc = devclass_find_internal(classname, TRUE);
977 error = devclass_add_device(dc, dev);
979 bus_data_generation_update();
984 device_set_driver(device_t dev, driver_t *driver)
986 if (dev->state >= DS_ATTACHED)
989 if (dev->driver == driver)
992 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
993 free(dev->softc, M_BUS);
996 kobj_delete((kobj_t) dev, 0);
997 dev->driver = driver;
999 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1000 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1001 dev->softc = malloc(driver->size, M_BUS,
1004 kobj_init((kobj_t) dev, &null_class);
1010 kobj_init((kobj_t) dev, &null_class);
1013 bus_data_generation_update();
1018 device_probe_and_attach(device_t dev)
1020 device_t bus = dev->parent;
1022 int hasclass = (dev->devclass != 0);
1024 if (dev->state >= DS_ALIVE)
1027 if (dev->flags & DF_ENABLED) {
1028 error = device_probe_child(bus, dev);
1030 if (!device_is_quiet(dev))
1031 device_print_child(bus, dev);
1032 error = DEVICE_ATTACH(dev);
1034 dev->state = DS_ATTACHED;
1036 printf("device_probe_and_attach: %s%d attach returned %d\n",
1037 dev->driver->name, dev->unit, error);
1038 /* Unset the class; set in device_probe_child */
1040 device_set_devclass(dev, 0);
1041 device_set_driver(dev, NULL);
1042 dev->state = DS_NOTPRESENT;
1045 if (!(dev->flags & DF_DONENOMATCH)) {
1046 BUS_PROBE_NOMATCH(bus, dev);
1047 dev->flags |= DF_DONENOMATCH;
1052 device_print_prettyname(dev);
1053 printf("not probed (disabled)\n");
1061 device_detach(device_t dev)
1065 PDEBUG(("%s", DEVICENAME(dev)));
1066 if (dev->state == DS_BUSY)
1068 if (dev->state != DS_ATTACHED)
1071 if ((error = DEVICE_DETACH(dev)) != 0)
1073 device_printf(dev, "detached\n");
1075 BUS_CHILD_DETACHED(dev->parent, dev);
1077 if (!(dev->flags & DF_FIXEDCLASS))
1078 devclass_delete_device(dev->devclass, dev);
1080 dev->state = DS_NOTPRESENT;
1081 device_set_driver(dev, NULL);
1087 device_shutdown(device_t dev)
1089 if (dev->state < DS_ATTACHED)
1091 return (DEVICE_SHUTDOWN(dev));
1095 device_set_unit(device_t dev, int unit)
1100 dc = device_get_devclass(dev);
1101 if (unit < dc->maxunit && dc->devices[unit])
1103 err = devclass_delete_device(dc, dev);
1107 err = devclass_add_device(dc, dev);
1111 bus_data_generation_update();
1115 /*======================================*/
1117 * Some useful method implementations to make life easier for bus drivers.
1121 resource_list_init(struct resource_list *rl)
1127 resource_list_free(struct resource_list *rl)
1129 struct resource_list_entry *rle;
1131 while ((rle = SLIST_FIRST(rl)) != NULL) {
1133 panic("resource_list_free: resource entry is busy");
1134 SLIST_REMOVE_HEAD(rl, link);
1140 resource_list_add_next(struct resource_list *rl, int type,
1141 u_long start, u_long end, u_long count)
1146 while (resource_list_find(rl, type, rid)) rid++;
1147 resource_list_add(rl, type, rid, start, end, count);
1153 resource_list_add(struct resource_list *rl, int type, int rid,
1154 u_long start, u_long end, u_long count)
1156 struct resource_list_entry *rle;
1158 rle = resource_list_find(rl, type, rid);
1160 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
1163 panic("resource_list_add: can't record entry");
1164 SLIST_INSERT_HEAD(rl, rle, link);
1171 panic("resource_list_add: resource entry is busy");
1178 struct resource_list_entry *
1179 resource_list_find(struct resource_list *rl, int type, int rid)
1181 struct resource_list_entry *rle;
1183 SLIST_FOREACH(rle, rl, link) {
1184 if (rle->type == type && rle->rid == rid)
1191 resource_list_delete(struct resource_list *rl, int type, int rid)
1193 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1196 if (rle->res != NULL)
1197 panic("resource_list_delete: resource has not been released");
1198 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1204 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
1205 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
1207 struct resource_list_entry *rle = 0;
1208 int passthrough = (device_get_parent(child) != bus);
1209 int isdefault = (start == 0UL && end == ~0UL);
1212 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1213 type, rid, start, end, count, flags));
1216 rle = resource_list_find(rl, type, *rid);
1219 return (NULL); /* no resource of that type/rid */
1222 panic("resource_list_alloc: resource entry is busy");
1226 count = ulmax(count, rle->count);
1227 end = ulmax(rle->end, start + count - 1);
1230 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1231 type, rid, start, end, count, flags);
1234 * Record the new range.
1237 rle->start = rman_get_start(rle->res);
1238 rle->end = rman_get_end(rle->res);
1246 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
1247 int type, int rid, struct resource *res)
1249 struct resource_list_entry *rle = 0;
1250 int passthrough = (device_get_parent(child) != bus);
1254 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1258 rle = resource_list_find(rl, type, rid);
1261 panic("resource_list_release: can't find resource");
1263 panic("resource_list_release: resource entry is not busy");
1265 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1275 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1278 struct resource_list_entry *rle;
1279 int printed, retval;
1283 /* Yes, this is kinda cheating */
1284 SLIST_FOREACH(rle, rl, link) {
1285 if (rle->type == type) {
1287 retval += printf(" %s ", name);
1289 retval += printf(",");
1291 retval += printf(format, rle->start);
1292 if (rle->count > 1) {
1293 retval += printf("-");
1294 retval += printf(format, rle->start +
1303 * Call DEVICE_IDENTIFY for each driver.
1306 bus_generic_probe(device_t dev)
1308 devclass_t dc = dev->devclass;
1311 TAILQ_FOREACH(dl, &dc->drivers, link) {
1312 DEVICE_IDENTIFY(dl->driver, dev);
1319 bus_generic_attach(device_t dev)
1323 TAILQ_FOREACH(child, &dev->children, link) {
1324 device_probe_and_attach(child);
1331 bus_generic_detach(device_t dev)
1336 if (dev->state != DS_ATTACHED)
1339 TAILQ_FOREACH(child, &dev->children, link) {
1340 if ((error = device_detach(child)) != 0)
1348 bus_generic_shutdown(device_t dev)
1352 TAILQ_FOREACH(child, &dev->children, link) {
1353 device_shutdown(child);
1360 bus_generic_suspend(device_t dev)
1363 device_t child, child2;
1365 TAILQ_FOREACH(child, &dev->children, link) {
1366 error = DEVICE_SUSPEND(child);
1368 for (child2 = TAILQ_FIRST(&dev->children);
1369 child2 && child2 != child;
1370 child2 = TAILQ_NEXT(child2, link))
1371 DEVICE_RESUME(child2);
1379 bus_generic_resume(device_t dev)
1383 TAILQ_FOREACH(child, &dev->children, link) {
1384 DEVICE_RESUME(child);
1385 /* if resume fails, there's nothing we can usefully do... */
1391 bus_print_child_header (device_t dev, device_t child)
1395 if (device_get_desc(child)) {
1396 retval += device_printf(child, "<%s>", device_get_desc(child));
1398 retval += printf("%s", device_get_nameunit(child));
1405 bus_print_child_footer (device_t dev, device_t child)
1407 return (printf(" on %s\n", device_get_nameunit(dev)));
1411 bus_generic_print_child(device_t dev, device_t child)
1415 retval += bus_print_child_header(dev, child);
1416 retval += bus_print_child_footer(dev, child);
1422 bus_generic_read_ivar(device_t dev, device_t child, int index,
1429 bus_generic_write_ivar(device_t dev, device_t child, int index,
1435 struct resource_list *
1436 bus_generic_get_resource_list (device_t dev, device_t child)
1442 bus_generic_driver_added(device_t dev, driver_t *driver)
1446 DEVICE_IDENTIFY(driver, dev);
1447 TAILQ_FOREACH(child, &dev->children, link) {
1448 if (child->state == DS_NOTPRESENT)
1449 device_probe_and_attach(child);
1454 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
1455 int flags, driver_intr_t *intr, void *arg, void **cookiep)
1457 /* Propagate up the bus hierarchy until someone handles it. */
1459 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
1460 intr, arg, cookiep));
1465 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
1468 /* Propagate up the bus hierarchy until someone handles it. */
1470 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
1475 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
1476 u_long start, u_long end, u_long count, u_int flags)
1478 /* Propagate up the bus hierarchy until someone handles it. */
1480 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
1481 start, end, count, flags));
1486 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
1489 /* Propagate up the bus hierarchy until someone handles it. */
1491 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
1497 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
1500 /* Propagate up the bus hierarchy until someone handles it. */
1502 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
1508 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
1509 int rid, struct resource *r)
1511 /* Propagate up the bus hierarchy until someone handles it. */
1513 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
1519 bus_generic_rl_get_resource (device_t dev, device_t child, int type, int rid,
1520 u_long *startp, u_long *countp)
1522 struct resource_list * rl = NULL;
1523 struct resource_list_entry * rle = NULL;
1525 rl = BUS_GET_RESOURCE_LIST(dev, child);
1529 rle = resource_list_find(rl, type, rid);
1534 *startp = rle->start;
1536 *countp = rle->count;
1542 bus_generic_rl_set_resource (device_t dev, device_t child, int type, int rid,
1543 u_long start, u_long count)
1545 struct resource_list * rl = NULL;
1547 rl = BUS_GET_RESOURCE_LIST(dev, child);
1551 resource_list_add(rl, type, rid, start, (start + count - 1), count);
1557 bus_generic_rl_delete_resource (device_t dev, device_t child, int type, int rid)
1559 struct resource_list * rl = NULL;
1561 rl = BUS_GET_RESOURCE_LIST(dev, child);
1565 resource_list_delete(rl, type, rid);
1571 bus_generic_rl_release_resource (device_t dev, device_t child, int type,
1572 int rid, struct resource *r)
1574 struct resource_list * rl = NULL;
1576 rl = BUS_GET_RESOURCE_LIST(dev, child);
1580 return (resource_list_release(rl, dev, child, type, rid, r));
1584 bus_generic_rl_alloc_resource (device_t dev, device_t child, int type,
1585 int *rid, u_long start, u_long end, u_long count, u_int flags)
1587 struct resource_list * rl = NULL;
1589 rl = BUS_GET_RESOURCE_LIST(dev, child);
1593 return (resource_list_alloc(rl, dev, child, type, rid,
1594 start, end, count, flags));
1598 * Some convenience functions to make it easier for drivers to use the
1599 * resource-management functions. All these really do is hide the
1600 * indirection through the parent's method table, making for slightly
1601 * less-wordy code. In the future, it might make sense for this code
1602 * to maintain some sort of a list of resources allocated by each device.
1605 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
1606 u_long count, u_int flags)
1608 if (dev->parent == 0)
1610 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
1615 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
1617 if (dev->parent == 0)
1619 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
1623 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
1625 if (dev->parent == 0)
1627 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
1631 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
1633 if (dev->parent == 0)
1635 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
1639 bus_setup_intr(device_t dev, struct resource *r, int flags,
1640 driver_intr_t handler, void *arg, void **cookiep)
1642 if (dev->parent == 0)
1644 return (BUS_SETUP_INTR(dev->parent, dev, r, flags,
1645 handler, arg, cookiep));
1649 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
1651 if (dev->parent == 0)
1653 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
1657 bus_set_resource(device_t dev, int type, int rid,
1658 u_long start, u_long count)
1660 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
1665 bus_get_resource(device_t dev, int type, int rid,
1666 u_long *startp, u_long *countp)
1668 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
1673 bus_get_resource_start(device_t dev, int type, int rid)
1675 u_long start, count;
1678 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
1686 bus_get_resource_count(device_t dev, int type, int rid)
1688 u_long start, count;
1691 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
1699 bus_delete_resource(device_t dev, int type, int rid)
1701 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
1705 root_print_child(device_t dev, device_t child)
1709 retval += bus_print_child_header(dev, child);
1710 retval += printf("\n");
1716 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
1720 * If an interrupt mapping gets to here something bad has happened.
1722 panic("root_setup_intr");
1725 static kobj_method_t root_methods[] = {
1726 /* Device interface */
1727 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
1728 KOBJMETHOD(device_suspend, bus_generic_suspend),
1729 KOBJMETHOD(device_resume, bus_generic_resume),
1732 KOBJMETHOD(bus_print_child, root_print_child),
1733 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
1734 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
1735 KOBJMETHOD(bus_setup_intr, root_setup_intr),
1740 static driver_t root_driver = {
1747 devclass_t root_devclass;
1750 root_bus_module_handler(module_t mod, int what, void* arg)
1754 TAILQ_INIT(&bus_data_devices);
1755 kobj_class_compile((kobj_class_t) &root_driver);
1756 root_bus = make_device(NULL, "root", 0);
1757 root_bus->desc = "System root bus";
1758 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
1759 root_bus->driver = &root_driver;
1760 root_bus->state = DS_ATTACHED;
1761 root_devclass = devclass_find_internal("root", FALSE);
1765 device_shutdown(root_bus);
1772 static moduledata_t root_bus_mod = {
1774 root_bus_module_handler,
1777 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1780 root_bus_configure(void)
1786 TAILQ_FOREACH(dev, &root_bus->children, link) {
1787 device_probe_and_attach(dev);
1792 driver_module_handler(module_t mod, int what, void *arg)
1795 struct driver_module_data *dmd;
1796 devclass_t bus_devclass;
1798 dmd = (struct driver_module_data *)arg;
1799 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE);
1804 if (dmd->dmd_chainevh)
1805 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
1807 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
1808 PDEBUG(("Loading module: driver %s on bus %s",
1809 DRIVERNAME(dmd->dmd_drivers[i]), dmd->dmd_busname));
1810 error = devclass_add_driver(bus_devclass,
1811 dmd->dmd_drivers[i]);
1817 * The drivers loaded in this way are assumed to all
1818 * implement the same devclass.
1820 *dmd->dmd_devclass =
1821 devclass_find_internal(dmd->dmd_drivers[0]->name, TRUE);
1825 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
1826 PDEBUG(("Unloading module: driver %s from bus %s",
1827 DRIVERNAME(dmd->dmd_drivers[i]),
1829 error = devclass_delete_driver(bus_devclass,
1830 dmd->dmd_drivers[i]);
1833 if (!error && dmd->dmd_chainevh)
1834 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
1843 /* the _short versions avoid iteration by not calling anything that prints
1844 * more than oneliners. I love oneliners.
1848 print_device_short(device_t dev, int indent)
1853 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
1854 dev->unit, dev->desc,
1855 (dev->parent? "":"no "),
1856 (TAILQ_EMPTY(&dev->children)? "no ":""),
1857 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
1858 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
1859 (dev->flags&DF_WILDCARD? "wildcard,":""),
1860 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
1861 (dev->ivars? "":"no "),
1862 (dev->softc? "":"no "),
1867 print_device(device_t dev, int indent)
1872 print_device_short(dev, indent);
1874 indentprintf(("Parent:\n"));
1875 print_device_short(dev->parent, indent+1);
1876 indentprintf(("Driver:\n"));
1877 print_driver_short(dev->driver, indent+1);
1878 indentprintf(("Devclass:\n"));
1879 print_devclass_short(dev->devclass, indent+1);
1883 print_device_tree_short(device_t dev, int indent)
1884 /* print the device and all its children (indented) */
1891 print_device_short(dev, indent);
1893 TAILQ_FOREACH(child, &dev->children, link) {
1894 print_device_tree_short(child, indent+1);
1899 print_device_tree(device_t dev, int indent)
1900 /* print the device and all its children (indented) */
1907 print_device(dev, indent);
1909 TAILQ_FOREACH(child, &dev->children, link) {
1910 print_device_tree(child, indent+1);
1915 print_driver_short(driver_t *driver, int indent)
1920 indentprintf(("driver %s: softc size = %d\n",
1921 driver->name, driver->size));
1925 print_driver(driver_t *driver, int indent)
1930 print_driver_short(driver, indent);
1935 print_driver_list(driver_list_t drivers, int indent)
1937 driverlink_t driver;
1939 TAILQ_FOREACH(driver, &drivers, link) {
1940 print_driver(driver->driver, indent);
1945 print_devclass_short(devclass_t dc, int indent)
1950 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
1954 print_devclass(devclass_t dc, int indent)
1961 print_devclass_short(dc, indent);
1962 indentprintf(("Drivers:\n"));
1963 print_driver_list(dc->drivers, indent+1);
1965 indentprintf(("Devices:\n"));
1966 for (i = 0; i < dc->maxunit; i++)
1968 print_device(dc->devices[i], indent+1);
1972 print_devclass_list_short(void)
1976 printf("Short listing of devclasses, drivers & devices:\n");
1977 TAILQ_FOREACH(dc, &devclasses, link) {
1978 print_devclass_short(dc, 0);
1983 print_devclass_list(void)
1987 printf("Full listing of devclasses, drivers & devices:\n");
1988 TAILQ_FOREACH(dc, &devclasses, link) {
1989 print_devclass(dc, 0);
1996 * User-space access to the device tree.
1998 * We implement a small set of nodes:
2000 * hw.bus Single integer read method to obtain the
2001 * current generation count.
2002 * hw.bus.devices Reads the entire device tree in flat space.
2003 * hw.bus.rman Resource manager interface
2005 * We might like to add the ability to scan devclasses and/or drivers to
2006 * determine what else is currently loaded/available.
2008 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
2011 sysctl_bus(SYSCTL_HANDLER_ARGS)
2013 struct u_businfo ubus;
2015 ubus.ub_version = BUS_USER_VERSION;
2016 ubus.ub_generation = bus_data_generation;
2018 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
2020 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
2021 "bus-related data");
2024 sysctl_devices(SYSCTL_HANDLER_ARGS)
2026 int *name = (int *)arg1;
2027 u_int namelen = arg2;
2030 struct u_device udev; /* XXX this is a bit big */
2036 if (bus_data_generation_check(name[0]))
2042 * Scan the list of devices, looking for the requested index.
2044 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
2052 * Populate the return array.
2054 udev.dv_handle = (uintptr_t)dev;
2055 udev.dv_parent = (uintptr_t)dev->parent;
2056 if (dev->nameunit == NULL) {
2057 udev.dv_name[0] = 0;
2059 snprintf(udev.dv_name, 32, "%s", dev->nameunit);
2061 if (dev->desc == NULL) {
2062 udev.dv_desc[0] = 0;
2064 snprintf(udev.dv_desc, 32, "%s", dev->desc);
2066 if ((dev->driver == NULL) || (dev->driver->name == NULL)) {
2067 udev.dv_drivername[0] = 0;
2069 snprintf(udev.dv_drivername, 32, "%s", dev->driver->name);
2071 error = SYSCTL_OUT(req, &udev, sizeof(udev));
2075 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
2076 "system device tree");
2079 * Sysctl interface for scanning the resource lists.
2081 * We take two input parameters; the index into the list of resource
2082 * managers, and the resource offset into the list.
2085 sysctl_rman(SYSCTL_HANDLER_ARGS)
2087 int *name = (int *)arg1;
2088 u_int namelen = arg2;
2089 int rman_idx, res_idx;
2091 struct resource *res;
2093 struct u_resource ures;
2099 if (bus_data_generation_check(name[0]))
2105 * Find the indexed resource manager
2107 TAILQ_FOREACH(rm, &rman_head, rm_link) {
2108 if (rman_idx-- == 0)
2115 * If the resource index is -1, we want details on the
2118 if (res_idx == -1) {
2119 urm.rm_handle = (uintptr_t)rm;
2120 snprintf(urm.rm_descr, RM_TEXTLEN, "%s", rm->rm_descr);
2121 urm.rm_descr[RM_TEXTLEN - 1] = '\0';
2122 urm.rm_start = rm->rm_start;
2123 urm.rm_size = rm->rm_end - rm->rm_start + 1;
2124 urm.rm_type = rm->rm_type;
2126 error = SYSCTL_OUT(req, &urm, sizeof(urm));
2131 * Find the indexed resource and return it.
2133 TAILQ_FOREACH(res, &rm->rm_list, r_link) {
2134 if (res_idx-- == 0) {
2135 ures.r_handle = (uintptr_t)res;
2136 ures.r_parent = (uintptr_t)res->r_rm;
2137 ures.r_device = (uintptr_t)res->r_dev;
2138 if (res->r_dev != NULL) {
2139 if (device_get_name(res->r_dev) != NULL) {
2140 snprintf(ures.r_devname, RM_TEXTLEN,
2142 device_get_name(res->r_dev),
2143 device_get_unit(res->r_dev));
2145 snprintf(ures.r_devname, RM_TEXTLEN,
2149 ures.r_devname[0] = 0;
2151 ures.r_start = res->r_start;
2152 ures.r_size = res->r_end - res->r_start + 1;
2153 ures.r_flags = res->r_flags;
2155 error = SYSCTL_OUT(req, &ures, sizeof(ures));
2162 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
2163 "kernel resource manager");
2166 bus_data_generation_check(int generation)
2168 if (generation != bus_data_generation)
2171 /* XXX generate optimised lists here? */
2176 bus_data_generation_update(void)
2178 bus_data_generation++;