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.11 2004/02/16 18:48:03 joerg Exp $
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/sysctl.h>
41 #include <sys/bus_private.h>
42 #include <sys/systm.h>
43 #include <machine/bus.h>
45 #include <machine/stdarg.h> /* for device_printf() */
47 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
50 #define PDEBUG(a) (printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n"))
51 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
52 #define DRIVERNAME(d) ((d)? d->name : "no driver")
53 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
55 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
56 * prevent syslog from deleting initial spaces
58 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0)
60 static void print_device_short(device_t dev, int indent);
61 static void print_device(device_t dev, int indent);
62 void print_device_tree_short(device_t dev, int indent);
63 void print_device_tree(device_t dev, int indent);
64 static void print_driver_short(driver_t *driver, int indent);
65 static void print_driver(driver_t *driver, int indent);
66 static void print_driver_list(driver_list_t drivers, int indent);
67 static void print_devclass_short(devclass_t dc, int indent);
68 static void print_devclass(devclass_t dc, int indent);
69 void print_devclass_list_short(void);
70 void print_devclass_list(void);
73 /* Make the compiler ignore the function calls */
74 #define PDEBUG(a) /* nop */
75 #define DEVICENAME(d) /* nop */
76 #define DRIVERNAME(d) /* nop */
77 #define DEVCLANAME(d) /* nop */
79 #define print_device_short(d,i) /* nop */
80 #define print_device(d,i) /* nop */
81 #define print_device_tree_short(d,i) /* nop */
82 #define print_device_tree(d,i) /* nop */
83 #define print_driver_short(d,i) /* nop */
84 #define print_driver(d,i) /* nop */
85 #define print_driver_list(d,i) /* nop */
86 #define print_devclass_short(d,i) /* nop */
87 #define print_devclass(d,i) /* nop */
88 #define print_devclass_list_short() /* nop */
89 #define print_devclass_list() /* nop */
93 static void device_register_oids(device_t dev);
94 static void device_unregister_oids(device_t dev);
97 kobj_method_t null_methods[] = {
101 DEFINE_CLASS(null, null_methods, 0);
104 * Devclass implementation
107 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
110 devclass_find_internal(const char *classname, 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);
133 TAILQ_INIT(&dc->drivers);
134 TAILQ_INSERT_TAIL(&devclasses, dc, link);
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);
192 devclass_delete_driver(devclass_t busclass, driver_t *driver)
194 devclass_t dc = devclass_find(driver->name);
200 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
206 * Find the link structure in the bus' list of drivers.
208 TAILQ_FOREACH(dl, &busclass->drivers, link) {
209 if (dl->driver == driver)
214 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
219 * Disassociate from any devices. We iterate through all the
220 * devices in the devclass of the driver and detach any which are
221 * using the driver and which have a parent in the devclass which
222 * we are deleting from.
224 * Note that since a driver can be in multiple devclasses, we
225 * should not detach devices which are not children of devices in
226 * the affected devclass.
228 for (i = 0; i < dc->maxunit; i++) {
229 if (dc->devices[i]) {
230 dev = dc->devices[i];
231 if (dev->driver == driver
232 && dev->parent && dev->parent->devclass == busclass) {
233 if ((error = device_detach(dev)) != 0)
235 device_set_driver(dev, NULL);
240 TAILQ_REMOVE(&busclass->drivers, dl, link);
244 if (driver->refs == 0)
245 kobj_class_free((kobj_class_t) driver);
251 devclass_find_driver_internal(devclass_t dc, const char *classname)
255 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
257 TAILQ_FOREACH(dl, &dc->drivers, link) {
258 if (!strcmp(dl->driver->name, classname))
262 PDEBUG(("not found"));
267 devclass_find_driver(devclass_t dc, const char *classname)
271 dl = devclass_find_driver_internal(dc, classname);
279 devclass_get_name(devclass_t dc)
285 devclass_get_device(devclass_t dc, int unit)
287 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
289 return dc->devices[unit];
293 devclass_get_softc(devclass_t dc, int unit)
297 dev = devclass_get_device(dc, unit);
301 return (device_get_softc(dev));
305 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
312 for (i = 0; i < dc->maxunit; i++)
316 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT | M_ZERO);
321 for (i = 0; i < dc->maxunit; i++)
322 if (dc->devices[i]) {
323 list[count] = dc->devices[i];
334 devclass_get_maxunit(devclass_t dc)
340 devclass_alloc_unit(devclass_t dc, int *unitp)
344 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
346 /* If we have been given a wired unit number, check for existing device */
348 if (unit >= 0 && unit < dc->maxunit && dc->devices[unit] != NULL) {
350 printf("%s-: %s%d exists, using next available unit number\n",
351 dc->name, dc->name, unit);
352 /* find the next available slot */
353 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
358 /* Unwired device, find the next available slot for it */
360 while (unit < dc->maxunit && dc->devices[unit] != NULL)
365 * We've selected a unit beyond the length of the table, so let's extend
366 * the table to make room for all units up to and including this one.
368 if (unit >= dc->maxunit) {
372 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
373 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT | M_ZERO);
376 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
378 free(dc->devices, M_BUS);
379 dc->devices = newlist;
380 dc->maxunit = newsize;
382 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
389 devclass_add_device(devclass_t dc, device_t dev)
393 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
395 buflen = strlen(dc->name) + 5;
396 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT | M_ZERO);
400 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
401 free(dev->nameunit, M_BUS);
402 dev->nameunit = NULL;
405 dc->devices[dev->unit] = dev;
407 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
409 #ifdef DEVICE_SYSCTLS
410 device_register_oids(dev);
417 devclass_delete_device(devclass_t dc, device_t dev)
422 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
424 if (dev->devclass != dc
425 || dc->devices[dev->unit] != dev)
426 panic("devclass_delete_device: inconsistent device class");
427 dc->devices[dev->unit] = NULL;
428 if (dev->flags & DF_WILDCARD)
430 dev->devclass = NULL;
431 free(dev->nameunit, M_BUS);
432 dev->nameunit = NULL;
434 #ifdef DEVICE_SYSCTLS
435 device_unregister_oids(dev);
442 make_device(device_t parent, const char *name, int unit)
447 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
450 dc = devclass_find_internal(name, TRUE);
452 printf("make_device: can't find device class %s\n", name);
458 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT | M_ZERO);
462 dev->parent = parent;
463 TAILQ_INIT(&dev->children);
464 kobj_init((kobj_t) dev, &null_class);
466 dev->devclass = NULL;
468 dev->nameunit = NULL;
472 dev->flags = DF_ENABLED;
475 dev->flags |= DF_WILDCARD;
477 dev->flags |= DF_FIXEDCLASS;
478 if (devclass_add_device(dc, dev) != 0) {
479 kobj_delete((kobj_t)dev, M_BUS);
486 dev->state = DS_NOTPRESENT;
492 device_print_child(device_t dev, device_t child)
496 if (device_is_alive(child)) {
497 retval += BUS_PRINT_CHILD(dev, child);
499 retval += device_printf(child, " not found\n");
505 device_add_child(device_t dev, const char *name, int unit)
507 return device_add_child_ordered(dev, 0, name, unit);
511 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
516 PDEBUG(("%s at %s with order %d as unit %d",
517 name, DEVICENAME(dev), order, unit));
519 child = make_device(dev, name, unit);
522 child->order = order;
524 TAILQ_FOREACH(place, &dev->children, link) {
525 if (place->order > order)
531 * The device 'place' is the first device whose order is
532 * greater than the new child.
534 TAILQ_INSERT_BEFORE(place, child, link);
537 * The new child's order is greater or equal to the order of
538 * any existing device. Add the child to the tail of the list.
540 TAILQ_INSERT_TAIL(&dev->children, child, link);
547 device_delete_child(device_t dev, device_t child)
552 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
554 /* remove children first */
555 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
556 error = device_delete_child(child, grandchild);
561 if ((error = device_detach(child)) != 0)
564 devclass_delete_device(child->devclass, child);
565 TAILQ_REMOVE(&dev->children, child, link);
566 device_set_desc(child, NULL);
567 kobj_delete((kobj_t)child, M_BUS);
573 * Find only devices attached to this bus.
576 device_find_child(device_t dev, const char *classname, int unit)
581 dc = devclass_find(classname);
585 child = devclass_get_device(dc, unit);
586 if (child && child->parent == dev)
592 first_matching_driver(devclass_t dc, device_t dev)
595 return devclass_find_driver_internal(dc, dev->devclass->name);
597 return TAILQ_FIRST(&dc->drivers);
601 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
605 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link)) {
606 if (!strcmp(dev->devclass->name, dl->driver->name))
611 return TAILQ_NEXT(last, link);
615 device_probe_child(device_t dev, device_t child)
618 driverlink_t best = 0;
621 int hasclass = (child->devclass != 0);
625 panic("device_probe_child: parent device has no devclass");
627 if (child->state == DS_ALIVE)
630 for (dl = first_matching_driver(dc, child);
632 dl = next_matching_driver(dc, child, dl)) {
633 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
634 device_set_driver(child, dl->driver);
636 device_set_devclass(child, dl->driver->name);
637 result = DEVICE_PROBE(child);
639 device_set_devclass(child, 0);
642 * If the driver returns SUCCESS, there can be no higher match
652 * The driver returned an error so it certainly doesn't match.
655 device_set_driver(child, 0);
660 * A priority lower than SUCCESS, remember the best matching
661 * driver. Initialise the value of pri for the first match.
663 if (best == 0 || result > pri) {
671 * If we found a driver, change state and initialise the devclass.
674 if (!child->devclass)
675 device_set_devclass(child, best->driver->name);
676 device_set_driver(child, best->driver);
679 * A bit bogus. Call the probe method again to make sure
680 * that we have the right description.
684 child->state = DS_ALIVE;
692 device_get_parent(device_t dev)
698 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
705 TAILQ_FOREACH(child, &dev->children, link) {
709 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT | M_ZERO);
714 TAILQ_FOREACH(child, &dev->children, link) {
726 device_get_driver(device_t dev)
732 device_get_devclass(device_t dev)
734 return dev->devclass;
738 device_get_name(device_t dev)
741 return devclass_get_name(dev->devclass);
746 device_get_nameunit(device_t dev)
748 return dev->nameunit;
752 device_get_unit(device_t dev)
758 device_get_desc(device_t dev)
764 device_get_flags(device_t dev)
766 return dev->devflags;
770 device_print_prettyname(device_t dev)
772 const char *name = device_get_name(dev);
775 return printf("unknown: ");
777 return printf("%s%d: ", name, device_get_unit(dev));
781 device_printf(device_t dev, const char * fmt, ...)
786 retval = device_print_prettyname(dev);
788 retval += vprintf(fmt, ap);
794 device_set_desc_internal(device_t dev, const char* desc, int copy)
796 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
797 free(dev->desc, M_BUS);
798 dev->flags &= ~DF_DESCMALLOCED;
803 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
805 strcpy(dev->desc, desc);
806 dev->flags |= DF_DESCMALLOCED;
809 /* Avoid a -Wcast-qual warning */
810 dev->desc = (char *)(uintptr_t) desc;
812 #ifdef DEVICE_SYSCTLS
814 struct sysctl_oid *oid = &dev->oid[1];
815 oid->oid_arg1 = dev->desc ? dev->desc : "";
816 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
822 device_set_desc(device_t dev, const char* desc)
824 device_set_desc_internal(dev, desc, FALSE);
828 device_set_desc_copy(device_t dev, const char* desc)
830 device_set_desc_internal(dev, desc, TRUE);
834 device_set_flags(device_t dev, u_int32_t flags)
836 dev->devflags = flags;
840 device_get_softc(device_t dev)
846 device_set_softc(device_t dev, void *softc)
848 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
849 free(dev->softc, M_BUS);
852 dev->flags |= DF_EXTERNALSOFTC;
854 dev->flags &= ~DF_EXTERNALSOFTC;
858 device_get_ivars(device_t dev)
864 device_set_ivars(device_t dev, void * ivars)
875 device_get_state(device_t dev)
881 device_enable(device_t dev)
883 dev->flags |= DF_ENABLED;
887 device_disable(device_t dev)
889 dev->flags &= ~DF_ENABLED;
896 device_busy(device_t dev)
898 if (dev->state < DS_ATTACHED)
899 panic("device_busy: called for unattached device");
900 if (dev->busy == 0 && dev->parent)
901 device_busy(dev->parent);
903 dev->state = DS_BUSY;
910 device_unbusy(device_t dev)
912 if (dev->state != DS_BUSY)
913 panic("device_unbusy: called for non-busy device");
915 if (dev->busy == 0) {
917 device_unbusy(dev->parent);
918 dev->state = DS_ATTACHED;
923 device_quiet(device_t dev)
925 dev->flags |= DF_QUIET;
929 device_verbose(device_t dev)
931 dev->flags &= ~DF_QUIET;
935 device_is_quiet(device_t dev)
937 return (dev->flags & DF_QUIET) != 0;
941 device_is_enabled(device_t dev)
943 return (dev->flags & DF_ENABLED) != 0;
947 device_is_alive(device_t dev)
949 return dev->state >= DS_ALIVE;
953 device_is_attached(device_t dev)
955 return (dev->state >= DS_ATTACHED);
959 device_set_devclass(device_t dev, const char *classname)
965 devclass_delete_device(dev->devclass, dev);
970 printf("device_set_devclass: device class already set\n");
974 dc = devclass_find_internal(classname, TRUE);
978 return devclass_add_device(dc, dev);
982 device_set_driver(device_t dev, driver_t *driver)
984 if (dev->state >= DS_ATTACHED)
987 if (dev->driver == driver)
990 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
991 free(dev->softc, M_BUS);
994 kobj_delete((kobj_t) dev, 0);
995 dev->driver = driver;
997 kobj_init((kobj_t) dev, (kobj_class_t) driver);
998 if (!(dev->flags & DF_EXTERNALSOFTC)) {
999 dev->softc = malloc(driver->size, M_BUS, M_NOWAIT | M_ZERO);
1001 kobj_delete((kobj_t)dev, 0);
1002 kobj_init((kobj_t) dev, &null_class);
1008 kobj_init((kobj_t) dev, &null_class);
1013 device_probe_and_attach(device_t dev)
1015 device_t bus = dev->parent;
1017 int hasclass = (dev->devclass != 0);
1019 if (dev->state >= DS_ALIVE)
1022 if (dev->flags & DF_ENABLED) {
1023 error = device_probe_child(bus, dev);
1025 if (!device_is_quiet(dev))
1026 device_print_child(bus, dev);
1027 error = DEVICE_ATTACH(dev);
1029 dev->state = DS_ATTACHED;
1031 printf("device_probe_and_attach: %s%d attach returned %d\n",
1032 dev->driver->name, dev->unit, error);
1033 /* Unset the class that was set in device_probe_child */
1035 device_set_devclass(dev, 0);
1036 device_set_driver(dev, NULL);
1037 dev->state = DS_NOTPRESENT;
1040 if (!(dev->flags & DF_DONENOMATCH)) {
1041 BUS_PROBE_NOMATCH(bus, dev);
1042 dev->flags |= DF_DONENOMATCH;
1047 device_print_prettyname(dev);
1048 printf("not probed (disabled)\n");
1056 device_detach(device_t dev)
1060 PDEBUG(("%s", DEVICENAME(dev)));
1061 if (dev->state == DS_BUSY)
1063 if (dev->state != DS_ATTACHED)
1066 if ((error = DEVICE_DETACH(dev)) != 0)
1068 device_printf(dev, "detached\n");
1070 BUS_CHILD_DETACHED(dev->parent, dev);
1072 if (!(dev->flags & DF_FIXEDCLASS))
1073 devclass_delete_device(dev->devclass, dev);
1075 dev->state = DS_NOTPRESENT;
1076 device_set_driver(dev, NULL);
1082 device_shutdown(device_t dev)
1084 if (dev->state < DS_ATTACHED)
1086 return DEVICE_SHUTDOWN(dev);
1090 device_set_unit(device_t dev, int unit)
1095 dc = device_get_devclass(dev);
1096 if (unit < dc->maxunit && dc->devices[unit])
1098 err = devclass_delete_device(dc, dev);
1102 err = devclass_add_device(dc, dev);
1108 #ifdef DEVICE_SYSCTLS
1111 * Sysctl nodes for devices.
1114 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1117 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1119 device_t dev = arg1;
1121 int first = 1, error = 0;
1123 TAILQ_FOREACH(child, &dev->children, link) {
1124 if (child->nameunit) {
1126 error = SYSCTL_OUT(req, ",", 1);
1127 if (error) return error;
1131 error = SYSCTL_OUT(req, child->nameunit, strlen(child->nameunit));
1132 if (error) return error;
1136 error = SYSCTL_OUT(req, "", 1);
1142 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1144 device_t dev = arg1;
1146 switch (dev->state) {
1148 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1150 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1152 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1154 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1161 device_register_oids(device_t dev)
1163 struct sysctl_oid* oid;
1166 bzero(oid, sizeof(*oid));
1167 oid->oid_parent = &sysctl__hw_devices_children;
1168 oid->oid_number = OID_AUTO;
1169 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1170 oid->oid_arg1 = &dev->oidlist[0];
1172 oid->oid_name = dev->nameunit;
1173 oid->oid_handler = 0;
1175 SLIST_INIT(&dev->oidlist[0]);
1176 sysctl_register_oid(oid);
1179 bzero(oid, sizeof(*oid));
1180 oid->oid_parent = &dev->oidlist[0];
1181 oid->oid_number = OID_AUTO;
1182 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1183 oid->oid_arg1 = dev->desc ? dev->desc : "";
1184 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1185 oid->oid_name = "desc";
1186 oid->oid_handler = sysctl_handle_string;
1188 sysctl_register_oid(oid);
1191 bzero(oid, sizeof(*oid));
1192 oid->oid_parent = &dev->oidlist[0];
1193 oid->oid_number = OID_AUTO;
1194 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1195 oid->oid_arg1 = dev;
1197 oid->oid_name = "children";
1198 oid->oid_handler = sysctl_handle_children;
1200 sysctl_register_oid(oid);
1203 bzero(oid, sizeof(*oid));
1204 oid->oid_parent = &dev->oidlist[0];
1205 oid->oid_number = OID_AUTO;
1206 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1207 oid->oid_arg1 = dev;
1209 oid->oid_name = "state";
1210 oid->oid_handler = sysctl_handle_state;
1212 sysctl_register_oid(oid);
1216 device_unregister_oids(device_t dev)
1218 sysctl_unregister_oid(&dev->oid[0]);
1219 sysctl_unregister_oid(&dev->oid[1]);
1220 sysctl_unregister_oid(&dev->oid[2]);
1225 /*======================================*/
1227 * Access functions for device resources.
1230 /* Supplied by config(8) in ioconf.c */
1231 extern struct config_device config_devtab[];
1232 extern int devtab_count;
1234 /* Runtime version */
1235 struct config_device *devtab = config_devtab;
1238 resource_new_name(const char *name, int unit)
1240 struct config_device *new;
1242 new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1246 if (devtab && devtab_count > 0)
1247 bcopy(devtab, new, devtab_count * sizeof(*new));
1248 new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_NOWAIT);
1249 if (new[devtab_count].name == NULL) {
1253 strcpy(new[devtab_count].name, name);
1254 new[devtab_count].unit = unit;
1255 new[devtab_count].resource_count = 0;
1256 new[devtab_count].resources = NULL;
1258 return devtab_count++;
1262 resource_new_resname(int j, const char *resname, resource_type type)
1264 struct config_resource *new;
1267 i = devtab[j].resource_count;
1268 new = malloc((i + 1) * sizeof(*new), M_TEMP, M_NOWAIT | M_ZERO);
1271 if (devtab[j].resources && i > 0)
1272 bcopy(devtab[j].resources, new, i * sizeof(*new));
1273 new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_NOWAIT);
1274 if (new[i].name == NULL) {
1278 strcpy(new[i].name, resname);
1280 if (devtab[j].resources)
1281 free(devtab[j].resources, M_TEMP);
1282 devtab[j].resources = new;
1283 devtab[j].resource_count = i + 1;
1288 resource_match_string(int i, const char *resname, const char *value)
1291 struct config_resource *res;
1293 for (j = 0, res = devtab[i].resources;
1294 j < devtab[i].resource_count; j++, res++)
1295 if (!strcmp(res->name, resname)
1296 && res->type == RES_STRING
1297 && !strcmp(res->u.stringval, value))
1303 resource_find(const char *name, int unit, const char *resname,
1304 struct config_resource **result)
1307 struct config_resource *res;
1310 * First check specific instances, then generic.
1312 for (i = 0; i < devtab_count; i++) {
1313 if (devtab[i].unit < 0)
1315 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1316 res = devtab[i].resources;
1317 for (j = 0; j < devtab[i].resource_count; j++, res++)
1318 if (!strcmp(res->name, resname)) {
1324 for (i = 0; i < devtab_count; i++) {
1325 if (devtab[i].unit >= 0)
1327 /* XXX should this `&& devtab[i].unit == unit' be here? */
1328 /* XXX if so, then the generic match does nothing */
1329 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1330 res = devtab[i].resources;
1331 for (j = 0; j < devtab[i].resource_count; j++, res++)
1332 if (!strcmp(res->name, resname)) {
1342 resource_int_value(const char *name, int unit, const char *resname, int *result)
1345 struct config_resource *res;
1347 if ((error = resource_find(name, unit, resname, &res)) != 0)
1349 if (res->type != RES_INT)
1351 *result = res->u.intval;
1356 resource_long_value(const char *name, int unit, const char *resname,
1360 struct config_resource *res;
1362 if ((error = resource_find(name, unit, resname, &res)) != 0)
1364 if (res->type != RES_LONG)
1366 *result = res->u.longval;
1371 resource_string_value(const char *name, int unit, const char *resname,
1375 struct config_resource *res;
1377 if ((error = resource_find(name, unit, resname, &res)) != 0)
1379 if (res->type != RES_STRING)
1381 *result = res->u.stringval;
1386 resource_query_string(int i, const char *resname, const char *value)
1392 for (; i < devtab_count; i++)
1393 if (resource_match_string(i, resname, value) >= 0)
1399 resource_locate(int i, const char *resname)
1405 for (; i < devtab_count; i++)
1406 if (!strcmp(devtab[i].name, resname))
1412 resource_count(void)
1414 return devtab_count;
1418 resource_query_name(int i)
1420 return devtab[i].name;
1424 resource_query_unit(int i)
1426 return devtab[i].unit;
1430 resource_create(const char *name, int unit, const char *resname,
1431 resource_type type, struct config_resource **result)
1434 struct config_resource *res = NULL;
1436 for (i = 0; i < devtab_count; i++) {
1437 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1438 res = devtab[i].resources;
1443 i = resource_new_name(name, unit);
1446 res = devtab[i].resources;
1448 for (j = 0; j < devtab[i].resource_count; j++, res++) {
1449 if (!strcmp(res->name, resname)) {
1454 j = resource_new_resname(i, resname, type);
1457 res = &devtab[i].resources[j];
1463 resource_set_int(const char *name, int unit, const char *resname, int value)
1466 struct config_resource *res;
1468 error = resource_create(name, unit, resname, RES_INT, &res);
1471 if (res->type != RES_INT)
1473 res->u.intval = value;
1478 resource_set_long(const char *name, int unit, const char *resname, long value)
1481 struct config_resource *res;
1483 error = resource_create(name, unit, resname, RES_LONG, &res);
1486 if (res->type != RES_LONG)
1488 res->u.longval = value;
1493 resource_set_string(const char *name, int unit, const char *resname,
1497 struct config_resource *res;
1499 error = resource_create(name, unit, resname, RES_STRING, &res);
1502 if (res->type != RES_STRING)
1504 if (res->u.stringval)
1505 free(res->u.stringval, M_TEMP);
1506 res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_NOWAIT);
1507 if (res->u.stringval == NULL)
1509 strcpy(res->u.stringval, value);
1515 resource_cfgload(void *dummy __unused)
1517 struct config_resource *res, *cfgres;
1520 char *name, *resname;
1524 int config_devtab_count;
1526 config_devtab_count = devtab_count;
1530 for (i = 0; i < config_devtab_count; i++) {
1531 name = config_devtab[i].name;
1532 unit = config_devtab[i].unit;
1534 for (j = 0; j < config_devtab[i].resource_count; j++) {
1535 cfgres = config_devtab[i].resources;
1536 resname = cfgres[j].name;
1537 type = cfgres[j].type;
1538 error = resource_create(name, unit, resname, type,
1541 printf("create resource %s%d: error %d\n",
1545 if (res->type != type) {
1546 printf("type mismatch %s%d: %d != %d\n",
1547 name, unit, res->type, type);
1552 res->u.intval = cfgres[j].u.intval;
1555 res->u.longval = cfgres[j].u.longval;
1558 if (res->u.stringval)
1559 free(res->u.stringval, M_TEMP);
1560 stringval = cfgres[j].u.stringval;
1561 res->u.stringval = malloc(strlen(stringval) + 1,
1563 if (res->u.stringval == NULL)
1565 strcpy(res->u.stringval, stringval);
1568 panic("unknown resource type %d\n", type);
1573 SYSINIT(cfgload, SI_SUB_KMEM, SI_ORDER_ANY + 50, resource_cfgload, 0)
1576 /*======================================*/
1578 * Some useful method implementations to make life easier for bus drivers.
1582 resource_list_init(struct resource_list *rl)
1588 resource_list_free(struct resource_list *rl)
1590 struct resource_list_entry *rle;
1592 while ((rle = SLIST_FIRST(rl)) != NULL) {
1594 panic("resource_list_free: resource entry is busy");
1595 SLIST_REMOVE_HEAD(rl, link);
1601 resource_list_add(struct resource_list *rl,
1603 u_long start, u_long end, u_long count)
1605 struct resource_list_entry *rle;
1607 rle = resource_list_find(rl, type, rid);
1609 rle = malloc(sizeof(struct resource_list_entry), M_BUS, M_NOWAIT);
1611 panic("resource_list_add: can't record entry");
1612 SLIST_INSERT_HEAD(rl, rle, link);
1619 panic("resource_list_add: resource entry is busy");
1626 struct resource_list_entry*
1627 resource_list_find(struct resource_list *rl,
1630 struct resource_list_entry *rle;
1632 SLIST_FOREACH(rle, rl, link)
1633 if (rle->type == type && rle->rid == rid)
1639 resource_list_delete(struct resource_list *rl,
1642 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1645 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1651 resource_list_alloc(struct resource_list *rl,
1652 device_t bus, device_t child,
1654 u_long start, u_long end,
1655 u_long count, u_int flags)
1657 struct resource_list_entry *rle = 0;
1658 int passthrough = (device_get_parent(child) != bus);
1659 int isdefault = (start == 0UL && end == ~0UL);
1662 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1664 start, end, count, flags);
1667 rle = resource_list_find(rl, type, *rid);
1670 return 0; /* no resource of that type/rid */
1672 panic("resource_list_alloc: resource entry is busy");
1676 count = max(count, rle->count);
1677 end = max(rle->end, start + count - 1);
1680 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1681 type, rid, start, end, count, flags);
1684 * Record the new range.
1687 rle->start = rman_get_start(rle->res);
1688 rle->end = rman_get_end(rle->res);
1696 resource_list_release(struct resource_list *rl,
1697 device_t bus, device_t child,
1698 int type, int rid, struct resource *res)
1700 struct resource_list_entry *rle = 0;
1701 int passthrough = (device_get_parent(child) != bus);
1705 return BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1709 rle = resource_list_find(rl, type, rid);
1712 panic("resource_list_release: can't find resource");
1714 panic("resource_list_release: resource entry is not busy");
1716 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1726 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1729 struct resource_list_entry *rle;
1730 int printed, retval;
1734 /* Yes, this is kinda cheating */
1735 SLIST_FOREACH(rle, rl, link) {
1736 if (rle->type == type) {
1738 retval += printf(" %s ", name);
1740 retval += printf(",");
1742 retval += printf(format, rle->start);
1743 if (rle->count > 1) {
1744 retval += printf("-");
1745 retval += printf(format, rle->start +
1754 * Call DEVICE_IDENTIFY for each driver.
1757 bus_generic_probe(device_t dev)
1759 devclass_t dc = dev->devclass;
1762 TAILQ_FOREACH(dl, &dc->drivers, link) {
1763 DEVICE_IDENTIFY(dl->driver, dev);
1770 bus_generic_attach(device_t dev)
1774 TAILQ_FOREACH(child, &dev->children, link) {
1775 device_probe_and_attach(child);
1782 bus_generic_detach(device_t dev)
1787 if (dev->state != DS_ATTACHED)
1790 TAILQ_FOREACH(child, &dev->children, link) {
1791 if ((error = device_detach(child)) != 0)
1799 bus_generic_shutdown(device_t dev)
1803 TAILQ_FOREACH(child, &dev->children, link) {
1804 device_shutdown(child);
1811 bus_generic_suspend(device_t dev)
1814 device_t child, child2;
1816 TAILQ_FOREACH(child, &dev->children, link) {
1817 error = DEVICE_SUSPEND(child);
1819 for (child2 = TAILQ_FIRST(&dev->children);
1820 child2 && child2 != child;
1821 child2 = TAILQ_NEXT(child2, link))
1822 DEVICE_RESUME(child2);
1830 bus_generic_resume(device_t dev)
1834 TAILQ_FOREACH(child, &dev->children, link) {
1835 DEVICE_RESUME(child);
1836 /* if resume fails, there's nothing we can usefully do... */
1842 bus_print_child_header (device_t dev, device_t child)
1846 if (device_get_desc(child)) {
1847 retval += device_printf(child, "<%s>",
1848 device_get_desc(child));
1850 retval += printf("%s", device_get_nameunit(child));
1857 bus_print_child_footer (device_t dev, device_t child)
1859 return(printf(" on %s\n", device_get_nameunit(dev)));
1863 bus_generic_print_child(device_t dev, device_t child)
1867 retval += bus_print_child_header(dev, child);
1868 retval += bus_print_child_footer(dev, child);
1874 bus_generic_read_ivar(device_t dev, device_t child, int index,
1881 bus_generic_write_ivar(device_t dev, device_t child, int index,
1888 bus_generic_driver_added(device_t dev, driver_t *driver)
1892 DEVICE_IDENTIFY(driver, dev);
1893 TAILQ_FOREACH(child, &dev->children, link) {
1894 if (child->state == DS_NOTPRESENT)
1895 device_probe_and_attach(child);
1900 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
1901 int flags, driver_intr_t *intr, void *arg,
1904 /* Propagate up the bus hierarchy until someone handles it. */
1906 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
1907 intr, arg, cookiep));
1913 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
1916 /* Propagate up the bus hierarchy until someone handles it. */
1918 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
1924 bus_generic_child_present(device_t bus, device_t child)
1926 return (BUS_CHILD_PRESENT(device_get_parent(bus), bus));
1930 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
1931 u_long start, u_long end, u_long count, u_int flags)
1933 /* Propagate up the bus hierarchy until someone handles it. */
1935 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
1936 start, end, count, flags));
1942 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
1945 /* Propagate up the bus hierarchy until someone handles it. */
1947 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
1954 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
1957 /* Propagate up the bus hierarchy until someone handles it. */
1959 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
1966 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
1967 int rid, struct resource *r)
1969 /* Propagate up the bus hierarchy until someone handles it. */
1971 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
1978 * Some convenience functions to make it easier for drivers to use the
1979 * resource-management functions. All these really do is hide the
1980 * indirection through the parent's method table, making for slightly
1981 * less-wordy code. In the future, it might make sense for this code
1982 * to maintain some sort of a list of resources allocated by each device.
1985 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
1986 u_long count, u_int flags)
1988 if (dev->parent == 0)
1990 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
1995 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
1997 if (dev->parent == 0)
1999 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2003 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2005 if (dev->parent == 0)
2007 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2011 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2013 if (dev->parent == 0)
2015 return (BUS_RELEASE_RESOURCE(dev->parent, dev,
2020 bus_setup_intr(device_t dev, struct resource *r, int flags,
2021 driver_intr_t handler, void *arg, void **cookiep)
2023 if (dev->parent == 0)
2025 return (BUS_SETUP_INTR(dev->parent, dev, r, flags,
2026 handler, arg, cookiep));
2030 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2032 if (dev->parent == 0)
2034 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2038 bus_set_resource(device_t dev, int type, int rid,
2039 u_long start, u_long count)
2041 return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2046 bus_get_resource(device_t dev, int type, int rid,
2047 u_long *startp, u_long *countp)
2049 return BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2054 bus_get_resource_start(device_t dev, int type, int rid)
2056 u_long start, count;
2059 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2067 bus_get_resource_count(device_t dev, int type, int rid)
2069 u_long start, count;
2072 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2080 bus_delete_resource(device_t dev, int type, int rid)
2082 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2086 root_print_child(device_t dev, device_t child)
2092 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2096 * If an interrupt mapping gets to here something bad has happened.
2098 panic("root_setup_intr");
2101 static kobj_method_t root_methods[] = {
2102 /* Device interface */
2103 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2104 KOBJMETHOD(device_suspend, bus_generic_suspend),
2105 KOBJMETHOD(device_resume, bus_generic_resume),
2108 KOBJMETHOD(bus_print_child, root_print_child),
2109 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2110 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2111 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2116 static driver_t root_driver = {
2123 devclass_t root_devclass;
2126 root_bus_module_handler(module_t mod, int what, void* arg)
2130 kobj_class_compile((kobj_class_t) &root_driver);
2131 root_bus = make_device(NULL, "root", 0);
2132 root_bus->desc = "System root bus";
2133 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2134 root_bus->driver = &root_driver;
2135 root_bus->state = DS_ATTACHED;
2136 root_devclass = devclass_find_internal("root", FALSE);
2140 device_shutdown(root_bus);
2147 static moduledata_t root_bus_mod = {
2149 root_bus_module_handler,
2152 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2155 root_bus_configure(void)
2161 TAILQ_FOREACH(dev, &root_bus->children, link) {
2162 device_probe_and_attach(dev);
2167 driver_module_handler(module_t mod, int what, void *arg)
2170 struct driver_module_data *dmd;
2171 devclass_t bus_devclass;
2173 dmd = (struct driver_module_data *)arg;
2174 bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE);
2179 if (dmd->dmd_chainevh)
2180 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2182 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2183 PDEBUG(("Loading module: driver %s on bus %s",
2184 DRIVERNAME(dmd->dmd_drivers[i]),
2186 error = devclass_add_driver(bus_devclass,
2187 dmd->dmd_drivers[i]);
2193 * The drivers loaded in this way are assumed to all
2194 * implement the same devclass.
2196 *dmd->dmd_devclass =
2197 devclass_find_internal(dmd->dmd_drivers[0]->name,
2202 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2203 PDEBUG(("Unloading module: driver %s from bus %s",
2204 DRIVERNAME(dmd->dmd_drivers[i]),
2206 error = devclass_delete_driver(bus_devclass,
2207 dmd->dmd_drivers[i]);
2210 if (!error && dmd->dmd_chainevh)
2211 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2220 /* the _short versions avoid iteration by not calling anything that prints
2221 * more than oneliners. I love oneliners.
2225 print_device_short(device_t dev, int indent)
2230 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2231 dev->unit, dev->desc,
2232 (dev->parent? "":"no "),
2233 (TAILQ_EMPTY(&dev->children)? "no ":""),
2234 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2235 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2236 (dev->flags&DF_WILDCARD? "wildcard,":""),
2237 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2238 (dev->ivars? "":"no "),
2239 (dev->softc? "":"no "),
2244 print_device(device_t dev, int indent)
2249 print_device_short(dev, indent);
2251 indentprintf(("Parent:\n"));
2252 print_device_short(dev->parent, indent+1);
2253 indentprintf(("Driver:\n"));
2254 print_driver_short(dev->driver, indent+1);
2255 indentprintf(("Devclass:\n"));
2256 print_devclass_short(dev->devclass, indent+1);
2260 print_device_tree_short(device_t dev, int indent)
2261 /* print the device and all its children (indented) */
2268 print_device_short(dev, indent);
2270 TAILQ_FOREACH(child, &dev->children, link) {
2271 print_device_tree_short(child, indent+1);
2276 print_device_tree(device_t dev, int indent)
2277 /* print the device and all its children (indented) */
2284 print_device(dev, indent);
2286 TAILQ_FOREACH(child, &dev->children, link) {
2287 print_device_tree(child, indent+1);
2292 print_driver_short(driver_t *driver, int indent)
2297 indentprintf(("driver %s: softc size = %d\n",
2298 driver->name, driver->size));
2302 print_driver(driver_t *driver, int indent)
2307 print_driver_short(driver, indent);
2312 print_driver_list(driver_list_t drivers, int indent)
2314 driverlink_t driver;
2316 TAILQ_FOREACH(driver, &drivers, link) {
2317 print_driver(driver->driver, indent);
2322 print_devclass_short(devclass_t dc, int indent)
2327 indentprintf(("devclass %s: max units = %d\n",
2328 dc->name, dc->maxunit));
2332 print_devclass(devclass_t dc, int indent)
2339 print_devclass_short(dc, indent);
2340 indentprintf(("Drivers:\n"));
2341 print_driver_list(dc->drivers, indent+1);
2343 indentprintf(("Devices:\n"));
2344 for (i = 0; i < dc->maxunit; i++)
2346 print_device(dc->devices[i], indent+1);
2350 print_devclass_list_short(void)
2354 printf("Short listing of devclasses, drivers & devices:\n");
2355 TAILQ_FOREACH(dc, &devclasses, link) {
2356 print_devclass_short(dc, 0);
2361 print_devclass_list(void)
2365 printf("Full listing of devclasses, drivers & devices:\n");
2366 TAILQ_FOREACH(dc, &devclasses, link) {
2367 print_devclass(dc, 0);