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.44 2008/09/29 06:59:45 hasso Exp $
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/sysctl.h>
41 #include <sys/bus_private.h>
42 #include <sys/systm.h>
46 #include <machine/stdarg.h> /* for device_printf() */
48 #include <sys/thread2.h>
50 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
53 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
54 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
55 #define DRIVERNAME(d) ((d)? d->name : "no driver")
56 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
58 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
59 * prevent syslog from deleting initial spaces
61 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
63 static void print_device_short(device_t dev, int indent);
64 static void print_device(device_t dev, int indent);
65 void print_device_tree_short(device_t dev, int indent);
66 void print_device_tree(device_t dev, int indent);
67 static void print_driver_short(driver_t *driver, int indent);
68 static void print_driver(driver_t *driver, int indent);
69 static void print_driver_list(driver_list_t drivers, int indent);
70 static void print_devclass_short(devclass_t dc, int indent);
71 static void print_devclass(devclass_t dc, int indent);
72 void print_devclass_list_short(void);
73 void print_devclass_list(void);
76 /* Make the compiler ignore the function calls */
77 #define PDEBUG(a) /* nop */
78 #define DEVICENAME(d) /* nop */
79 #define DRIVERNAME(d) /* nop */
80 #define DEVCLANAME(d) /* 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);
99 static void device_attach_async(device_t dev);
100 static void device_attach_thread(void *arg);
101 static int device_doattach(device_t dev);
103 static int do_async_attach = 0;
104 static int numasyncthreads;
105 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
107 kobj_method_t null_methods[] = {
111 DEFINE_CLASS(null, null_methods, 0);
114 * Devclass implementation
117 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
120 devclass_find_internal(const char *classname, const char *parentname,
125 PDEBUG(("looking for %s", classname));
126 if (classname == NULL)
129 TAILQ_FOREACH(dc, &devclasses, link)
130 if (!strcmp(dc->name, classname))
134 PDEBUG(("creating %s", classname));
135 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
136 M_BUS, M_INTWAIT | M_ZERO);
140 dc->name = (char*) (dc + 1);
141 strcpy(dc->name, classname);
144 TAILQ_INIT(&dc->drivers);
145 TAILQ_INSERT_TAIL(&devclasses, dc, link);
147 if (parentname && dc && !dc->parent)
148 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
154 devclass_create(const char *classname)
156 return(devclass_find_internal(classname, NULL, TRUE));
160 devclass_find(const char *classname)
162 return(devclass_find_internal(classname, NULL, FALSE));
166 devclass_find_unit(const char *classname, int unit)
170 if ((dc = devclass_find(classname)) != NULL)
171 return(devclass_get_device(dc, unit));
176 devclass_add_driver(devclass_t dc, driver_t *driver)
182 PDEBUG(("%s", DRIVERNAME(driver)));
184 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
189 * Compile the driver's methods. Also increase the reference count
190 * so that the class doesn't get freed when the last instance
191 * goes. This means we can safely use static methods and avoids a
192 * double-free in devclass_delete_driver.
194 kobj_class_instantiate(driver);
197 * Make sure the devclass which the driver is implementing exists.
199 devclass_find_internal(driver->name, NULL, TRUE);
202 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
205 * Call BUS_DRIVER_ADDED for any existing busses in this class,
206 * but only if the bus has already been attached (otherwise we
207 * might probe too early).
209 * This is what will cause a newly loaded module to be associated
210 * with hardware. bus_generic_driver_added() is typically what ends
213 for (i = 0; i < dc->maxunit; i++) {
214 if ((dev = dc->devices[i]) != NULL) {
215 if (dev->state >= DS_ATTACHED)
216 BUS_DRIVER_ADDED(dev, driver);
224 devclass_delete_driver(devclass_t busclass, driver_t *driver)
226 devclass_t dc = devclass_find(driver->name);
232 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
238 * Find the link structure in the bus' list of drivers.
240 TAILQ_FOREACH(dl, &busclass->drivers, link)
241 if (dl->driver == driver)
245 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
250 * Disassociate from any devices. We iterate through all the
251 * devices in the devclass of the driver and detach any which are
252 * using the driver and which have a parent in the devclass which
253 * we are deleting from.
255 * Note that since a driver can be in multiple devclasses, we
256 * should not detach devices which are not children of devices in
257 * the affected devclass.
259 for (i = 0; i < dc->maxunit; i++)
260 if (dc->devices[i]) {
261 dev = dc->devices[i];
262 if (dev->driver == driver && dev->parent &&
263 dev->parent->devclass == busclass) {
264 if ((error = device_detach(dev)) != 0)
266 device_set_driver(dev, NULL);
270 TAILQ_REMOVE(&busclass->drivers, dl, link);
273 kobj_class_uninstantiate(driver);
279 devclass_find_driver_internal(devclass_t dc, const char *classname)
283 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
285 TAILQ_FOREACH(dl, &dc->drivers, link)
286 if (!strcmp(dl->driver->name, classname))
289 PDEBUG(("not found"));
294 devclass_find_driver(devclass_t dc, const char *classname)
298 dl = devclass_find_driver_internal(dc, classname);
306 devclass_get_name(devclass_t dc)
312 devclass_get_device(devclass_t dc, int unit)
314 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
316 return(dc->devices[unit]);
320 devclass_get_softc(devclass_t dc, int unit)
324 dev = devclass_get_device(dc, unit);
328 return(device_get_softc(dev));
332 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
339 for (i = 0; i < dc->maxunit; i++)
343 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
348 for (i = 0; i < dc->maxunit; i++)
349 if (dc->devices[i]) {
350 list[count] = dc->devices[i];
361 * @brief Get a list of drivers in the devclass
363 * An array containing a list of pointers to all the drivers in the
364 * given devclass is allocated and returned in @p *listp. The number
365 * of drivers in the array is returned in @p *countp. The caller should
366 * free the array using @c free(p, M_TEMP).
368 * @param dc the devclass to examine
369 * @param listp gives location for array pointer return value
370 * @param countp gives location for number of array elements
374 * @retval ENOMEM the array allocation failed
377 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
384 TAILQ_FOREACH(dl, &dc->drivers, link)
386 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
391 TAILQ_FOREACH(dl, &dc->drivers, link) {
392 list[count] = dl->driver;
402 * @brief Get the number of devices in a devclass
404 * @param dc the devclass to examine
407 devclass_get_count(devclass_t dc)
412 for (i = 0; i < dc->maxunit; i++)
419 devclass_get_maxunit(devclass_t dc)
425 devclass_set_parent(devclass_t dc, devclass_t pdc)
431 devclass_get_parent(devclass_t dc)
437 devclass_alloc_unit(devclass_t dc, int *unitp)
441 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
443 /* If we have been given a wired unit number, check for existing device */
445 if (unit >= 0 && unit < dc->maxunit &&
446 dc->devices[unit] != NULL) {
448 kprintf("%s-: %s%d exists, using next available unit number\n",
449 dc->name, dc->name, unit);
450 /* find the next available slot */
451 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
455 /* Unwired device, find the next available slot for it */
457 while (unit < dc->maxunit && dc->devices[unit] != NULL)
462 * We've selected a unit beyond the length of the table, so let's
463 * extend the table to make room for all units up to and including
466 if (unit >= dc->maxunit) {
470 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
471 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
475 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
477 kfree(dc->devices, M_BUS);
478 dc->devices = newlist;
479 dc->maxunit = newsize;
481 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
488 devclass_add_device(devclass_t dc, device_t dev)
492 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
494 buflen = strlen(dc->name) + 5;
495 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
499 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
500 kfree(dev->nameunit, M_BUS);
501 dev->nameunit = NULL;
504 dc->devices[dev->unit] = dev;
506 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
508 #ifdef DEVICE_SYSCTLS
509 device_register_oids(dev);
516 devclass_delete_device(devclass_t dc, device_t dev)
521 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
523 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
524 panic("devclass_delete_device: inconsistent device class");
525 dc->devices[dev->unit] = NULL;
526 if (dev->flags & DF_WILDCARD)
528 dev->devclass = NULL;
529 kfree(dev->nameunit, M_BUS);
530 dev->nameunit = NULL;
532 #ifdef DEVICE_SYSCTLS
533 device_unregister_oids(dev);
540 make_device(device_t parent, const char *name, int unit)
545 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
548 dc = devclass_find_internal(name, NULL, TRUE);
550 kprintf("make_device: can't find device class %s\n", name);
556 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
560 dev->parent = parent;
561 TAILQ_INIT(&dev->children);
562 kobj_init((kobj_t) dev, &null_class);
564 dev->devclass = NULL;
566 dev->nameunit = NULL;
570 dev->flags = DF_ENABLED;
573 dev->flags |= DF_WILDCARD;
575 dev->flags |= DF_FIXEDCLASS;
576 if (devclass_add_device(dc, dev) != 0) {
577 kobj_delete((kobj_t)dev, M_BUS);
584 dev->state = DS_NOTPRESENT;
590 device_print_child(device_t dev, device_t child)
594 if (device_is_alive(child))
595 retval += BUS_PRINT_CHILD(dev, child);
597 retval += device_printf(child, " not found\n");
603 device_add_child(device_t dev, const char *name, int unit)
605 return device_add_child_ordered(dev, 0, name, unit);
609 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
614 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
617 child = make_device(dev, name, unit);
620 child->order = order;
622 TAILQ_FOREACH(place, &dev->children, link)
623 if (place->order > order)
628 * The device 'place' is the first device whose order is
629 * greater than the new child.
631 TAILQ_INSERT_BEFORE(place, child, link);
634 * The new child's order is greater or equal to the order of
635 * any existing device. Add the child to the tail of the list.
637 TAILQ_INSERT_TAIL(&dev->children, child, link);
644 device_delete_child(device_t dev, device_t child)
649 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
651 /* remove children first */
652 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
653 error = device_delete_child(child, grandchild);
658 if ((error = device_detach(child)) != 0)
661 devclass_delete_device(child->devclass, child);
662 TAILQ_REMOVE(&dev->children, child, link);
663 device_set_desc(child, NULL);
664 kobj_delete((kobj_t)child, M_BUS);
670 * @brief Find a device given a unit number
672 * This is similar to devclass_get_devices() but only searches for
673 * devices which have @p dev as a parent.
675 * @param dev the parent device to search
676 * @param unit the unit number to search for. If the unit is -1,
677 * return the first child of @p dev which has name
678 * @p classname (that is, the one with the lowest unit.)
680 * @returns the device with the given unit number or @c
681 * NULL if there is no such device
684 device_find_child(device_t dev, const char *classname, int unit)
689 dc = devclass_find(classname);
694 child = devclass_get_device(dc, unit);
695 if (child && child->parent == dev)
698 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
699 child = devclass_get_device(dc, unit);
700 if (child && child->parent == dev)
708 first_matching_driver(devclass_t dc, device_t dev)
711 return(devclass_find_driver_internal(dc, dev->devclass->name));
713 return(TAILQ_FIRST(&dc->drivers));
717 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
721 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
722 if (!strcmp(dev->devclass->name, dl->driver->name))
726 return(TAILQ_NEXT(last, link));
730 device_probe_child(device_t dev, device_t child)
733 driverlink_t best = 0;
736 int hasclass = (child->devclass != 0);
740 panic("device_probe_child: parent device has no devclass");
742 if (child->state == DS_ALIVE)
745 for (; dc; dc = dc->parent) {
746 for (dl = first_matching_driver(dc, child); dl;
747 dl = next_matching_driver(dc, child, dl)) {
748 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
749 device_set_driver(child, dl->driver);
751 device_set_devclass(child, dl->driver->name);
752 result = DEVICE_PROBE(child);
754 device_set_devclass(child, 0);
757 * If the driver returns SUCCESS, there can be
758 * no higher match for this device.
767 * The driver returned an error so it
768 * certainly doesn't match.
771 device_set_driver(child, 0);
776 * A priority lower than SUCCESS, remember the
777 * best matching driver. Initialise the value
778 * of pri for the first match.
780 if (best == 0 || result > pri) {
787 * If we have unambiguous match in this devclass,
788 * don't look in the parent.
790 if (best && pri == 0)
795 * If we found a driver, change state and initialise the devclass.
798 if (!child->devclass)
799 device_set_devclass(child, best->driver->name);
800 device_set_driver(child, best->driver);
803 * A bit bogus. Call the probe method again to make
804 * sure that we have the right description.
808 child->state = DS_ALIVE;
816 device_get_parent(device_t dev)
822 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
829 TAILQ_FOREACH(child, &dev->children, link)
832 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
837 TAILQ_FOREACH(child, &dev->children, link) {
849 device_get_driver(device_t dev)
855 device_get_devclass(device_t dev)
857 return(dev->devclass);
861 device_get_name(device_t dev)
864 return devclass_get_name(dev->devclass);
869 device_get_nameunit(device_t dev)
871 return(dev->nameunit);
875 device_get_unit(device_t dev)
881 device_get_desc(device_t dev)
887 device_get_flags(device_t dev)
889 return(dev->devflags);
893 device_print_prettyname(device_t dev)
895 const char *name = device_get_name(dev);
898 return kprintf("unknown: ");
900 return kprintf("%s%d: ", name, device_get_unit(dev));
904 device_printf(device_t dev, const char * fmt, ...)
909 retval = device_print_prettyname(dev);
911 retval += kvprintf(fmt, ap);
917 device_set_desc_internal(device_t dev, const char* desc, int copy)
919 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
920 kfree(dev->desc, M_BUS);
921 dev->flags &= ~DF_DESCMALLOCED;
926 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
928 strcpy(dev->desc, desc);
929 dev->flags |= DF_DESCMALLOCED;
932 /* Avoid a -Wcast-qual warning */
933 dev->desc = (char *)(uintptr_t) desc;
935 #ifdef DEVICE_SYSCTLS
937 struct sysctl_oid *oid = &dev->oid[1];
938 oid->oid_arg1 = dev->desc ? dev->desc : "";
939 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
945 device_set_desc(device_t dev, const char* desc)
947 device_set_desc_internal(dev, desc, FALSE);
951 device_set_desc_copy(device_t dev, const char* desc)
953 device_set_desc_internal(dev, desc, TRUE);
957 device_set_flags(device_t dev, uint32_t flags)
959 dev->devflags = flags;
963 device_get_softc(device_t dev)
969 device_set_softc(device_t dev, void *softc)
971 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
972 kfree(dev->softc, M_BUS);
975 dev->flags |= DF_EXTERNALSOFTC;
977 dev->flags &= ~DF_EXTERNALSOFTC;
981 device_set_async_attach(device_t dev, int enable)
984 dev->flags |= DF_ASYNCPROBE;
986 dev->flags &= ~DF_ASYNCPROBE;
990 device_get_ivars(device_t dev)
996 device_set_ivars(device_t dev, void * ivars)
1005 device_get_state(device_t dev)
1011 device_enable(device_t dev)
1013 dev->flags |= DF_ENABLED;
1017 device_disable(device_t dev)
1019 dev->flags &= ~DF_ENABLED;
1026 device_busy(device_t dev)
1028 if (dev->state < DS_ATTACHED)
1029 panic("device_busy: called for unattached device");
1030 if (dev->busy == 0 && dev->parent)
1031 device_busy(dev->parent);
1033 dev->state = DS_BUSY;
1040 device_unbusy(device_t dev)
1042 if (dev->state != DS_BUSY)
1043 panic("device_unbusy: called for non-busy device");
1045 if (dev->busy == 0) {
1047 device_unbusy(dev->parent);
1048 dev->state = DS_ATTACHED;
1053 device_quiet(device_t dev)
1055 dev->flags |= DF_QUIET;
1059 device_verbose(device_t dev)
1061 dev->flags &= ~DF_QUIET;
1065 device_is_quiet(device_t dev)
1067 return((dev->flags & DF_QUIET) != 0);
1071 device_is_enabled(device_t dev)
1073 return((dev->flags & DF_ENABLED) != 0);
1077 device_is_alive(device_t dev)
1079 return(dev->state >= DS_ALIVE);
1083 device_is_attached(device_t dev)
1085 return(dev->state >= DS_ATTACHED);
1089 device_set_devclass(device_t dev, const char *classname)
1095 devclass_delete_device(dev->devclass, dev);
1099 if (dev->devclass) {
1100 kprintf("device_set_devclass: device class already set\n");
1104 dc = devclass_find_internal(classname, NULL, TRUE);
1108 return(devclass_add_device(dc, dev));
1112 device_set_driver(device_t dev, driver_t *driver)
1114 if (dev->state >= DS_ATTACHED)
1117 if (dev->driver == driver)
1120 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1121 kfree(dev->softc, M_BUS);
1124 kobj_delete((kobj_t) dev, 0);
1125 dev->driver = driver;
1127 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1128 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1129 dev->softc = kmalloc(driver->size, M_BUS,
1130 M_INTWAIT | M_ZERO);
1132 kobj_delete((kobj_t)dev, 0);
1133 kobj_init((kobj_t) dev, &null_class);
1139 kobj_init((kobj_t) dev, &null_class);
1144 device_probe_and_attach(device_t dev)
1146 device_t bus = dev->parent;
1149 if (dev->state >= DS_ALIVE)
1152 if ((dev->flags & DF_ENABLED) == 0) {
1154 device_print_prettyname(dev);
1155 kprintf("not probed (disabled)\n");
1160 error = device_probe_child(bus, dev);
1162 if (!(dev->flags & DF_DONENOMATCH)) {
1163 BUS_PROBE_NOMATCH(bus, dev);
1164 dev->flags |= DF_DONENOMATCH;
1170 * Output the exact device chain prior to the attach in case the
1171 * system locks up during attach, and generate the full info after
1172 * the attach so correct irq and other information is displayed.
1174 if (bootverbose && !device_is_quiet(dev)) {
1177 kprintf("%s", device_get_nameunit(dev));
1178 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1179 kprintf(".%s", device_get_nameunit(tmp));
1182 if (!device_is_quiet(dev))
1183 device_print_child(bus, dev);
1184 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1185 kprintf("%s: probing asynchronously\n",
1186 device_get_nameunit(dev));
1187 dev->state = DS_INPROGRESS;
1188 device_attach_async(dev);
1191 error = device_doattach(dev);
1197 * Device is known to be alive, do the attach asynchronously.
1199 * The MP lock is held by all threads.
1202 device_attach_async(device_t dev)
1206 atomic_add_int(&numasyncthreads, 1);
1207 lwkt_create(device_attach_thread, dev, &td, NULL,
1208 0, 0, (dev->desc ? dev->desc : "devattach"));
1212 device_attach_thread(void *arg)
1216 (void)device_doattach(dev);
1217 atomic_subtract_int(&numasyncthreads, 1);
1218 wakeup(&numasyncthreads);
1222 * Device is known to be alive, do the attach (synchronous or asynchronous)
1225 device_doattach(device_t dev)
1227 device_t bus = dev->parent;
1228 int hasclass = (dev->devclass != 0);
1231 error = DEVICE_ATTACH(dev);
1233 dev->state = DS_ATTACHED;
1234 if (bootverbose && !device_is_quiet(dev))
1235 device_print_child(bus, dev);
1237 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1238 dev->driver->name, dev->unit, error);
1239 /* Unset the class that was set in device_probe_child */
1241 device_set_devclass(dev, 0);
1242 device_set_driver(dev, NULL);
1243 dev->state = DS_NOTPRESENT;
1249 device_detach(device_t dev)
1253 PDEBUG(("%s", DEVICENAME(dev)));
1254 if (dev->state == DS_BUSY)
1256 if (dev->state != DS_ATTACHED)
1259 if ((error = DEVICE_DETACH(dev)) != 0)
1261 device_printf(dev, "detached\n");
1263 BUS_CHILD_DETACHED(dev->parent, dev);
1265 if (!(dev->flags & DF_FIXEDCLASS))
1266 devclass_delete_device(dev->devclass, dev);
1268 dev->state = DS_NOTPRESENT;
1269 device_set_driver(dev, NULL);
1275 device_shutdown(device_t dev)
1277 if (dev->state < DS_ATTACHED)
1279 PDEBUG(("%s", DEVICENAME(dev)));
1280 return DEVICE_SHUTDOWN(dev);
1284 device_set_unit(device_t dev, int unit)
1289 dc = device_get_devclass(dev);
1290 if (unit < dc->maxunit && dc->devices[unit])
1292 err = devclass_delete_device(dc, dev);
1296 err = devclass_add_device(dc, dev);
1300 #ifdef DEVICE_SYSCTLS
1303 * Sysctl nodes for devices.
1306 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1309 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1311 device_t dev = arg1;
1313 int first = 1, error = 0;
1315 TAILQ_FOREACH(child, &dev->children, link)
1316 if (child->nameunit) {
1318 error = SYSCTL_OUT(req, ",", 1);
1323 error = SYSCTL_OUT(req, child->nameunit,
1324 strlen(child->nameunit));
1329 error = SYSCTL_OUT(req, "", 1);
1335 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1337 device_t dev = arg1;
1339 switch (dev->state) {
1341 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1343 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1345 return SYSCTL_OUT(req, "in-progress", sizeof("in-progress"));
1347 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1349 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1356 device_register_oids(device_t dev)
1358 struct sysctl_oid* oid;
1361 bzero(oid, sizeof(*oid));
1362 oid->oid_parent = &sysctl__hw_devices_children;
1363 oid->oid_number = OID_AUTO;
1364 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1365 oid->oid_arg1 = &dev->oidlist[0];
1367 oid->oid_name = dev->nameunit;
1368 oid->oid_handler = 0;
1370 SLIST_INIT(&dev->oidlist[0]);
1371 sysctl_register_oid(oid);
1374 bzero(oid, sizeof(*oid));
1375 oid->oid_parent = &dev->oidlist[0];
1376 oid->oid_number = OID_AUTO;
1377 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1378 oid->oid_arg1 = dev->desc ? dev->desc : "";
1379 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1380 oid->oid_name = "desc";
1381 oid->oid_handler = sysctl_handle_string;
1383 sysctl_register_oid(oid);
1386 bzero(oid, sizeof(*oid));
1387 oid->oid_parent = &dev->oidlist[0];
1388 oid->oid_number = OID_AUTO;
1389 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1390 oid->oid_arg1 = dev;
1392 oid->oid_name = "children";
1393 oid->oid_handler = sysctl_handle_children;
1395 sysctl_register_oid(oid);
1398 bzero(oid, sizeof(*oid));
1399 oid->oid_parent = &dev->oidlist[0];
1400 oid->oid_number = OID_AUTO;
1401 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1402 oid->oid_arg1 = dev;
1404 oid->oid_name = "state";
1405 oid->oid_handler = sysctl_handle_state;
1407 sysctl_register_oid(oid);
1411 device_unregister_oids(device_t dev)
1413 sysctl_unregister_oid(&dev->oid[0]);
1414 sysctl_unregister_oid(&dev->oid[1]);
1415 sysctl_unregister_oid(&dev->oid[2]);
1420 /*======================================*/
1422 * Access functions for device resources.
1425 /* Supplied by config(8) in ioconf.c */
1426 extern struct config_device config_devtab[];
1427 extern int devtab_count;
1429 /* Runtime version */
1430 struct config_device *devtab = config_devtab;
1433 resource_new_name(const char *name, int unit)
1435 struct config_device *new;
1437 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1438 M_INTWAIT | M_ZERO);
1441 if (devtab && devtab_count > 0)
1442 bcopy(devtab, new, devtab_count * sizeof(*new));
1443 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1444 if (new[devtab_count].name == NULL) {
1448 strcpy(new[devtab_count].name, name);
1449 new[devtab_count].unit = unit;
1450 new[devtab_count].resource_count = 0;
1451 new[devtab_count].resources = NULL;
1452 if (devtab && devtab != config_devtab)
1453 kfree(devtab, M_TEMP);
1455 return devtab_count++;
1459 resource_new_resname(int j, const char *resname, resource_type type)
1461 struct config_resource *new;
1464 i = devtab[j].resource_count;
1465 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1468 if (devtab[j].resources && i > 0)
1469 bcopy(devtab[j].resources, new, i * sizeof(*new));
1470 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1471 if (new[i].name == NULL) {
1475 strcpy(new[i].name, resname);
1477 if (devtab[j].resources)
1478 kfree(devtab[j].resources, M_TEMP);
1479 devtab[j].resources = new;
1480 devtab[j].resource_count = i + 1;
1485 resource_match_string(int i, const char *resname, const char *value)
1488 struct config_resource *res;
1490 for (j = 0, res = devtab[i].resources;
1491 j < devtab[i].resource_count; j++, res++)
1492 if (!strcmp(res->name, resname)
1493 && res->type == RES_STRING
1494 && !strcmp(res->u.stringval, value))
1500 resource_find(const char *name, int unit, const char *resname,
1501 struct config_resource **result)
1504 struct config_resource *res;
1507 * First check specific instances, then generic.
1509 for (i = 0; i < devtab_count; i++) {
1510 if (devtab[i].unit < 0)
1512 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1513 res = devtab[i].resources;
1514 for (j = 0; j < devtab[i].resource_count; j++, res++)
1515 if (!strcmp(res->name, resname)) {
1521 for (i = 0; i < devtab_count; i++) {
1522 if (devtab[i].unit >= 0)
1524 /* XXX should this `&& devtab[i].unit == unit' be here? */
1525 /* XXX if so, then the generic match does nothing */
1526 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1527 res = devtab[i].resources;
1528 for (j = 0; j < devtab[i].resource_count; j++, res++)
1529 if (!strcmp(res->name, resname)) {
1539 resource_int_value(const char *name, int unit, const char *resname, int *result)
1542 struct config_resource *res;
1544 if ((error = resource_find(name, unit, resname, &res)) != 0)
1546 if (res->type != RES_INT)
1548 *result = res->u.intval;
1553 resource_long_value(const char *name, int unit, const char *resname,
1557 struct config_resource *res;
1559 if ((error = resource_find(name, unit, resname, &res)) != 0)
1561 if (res->type != RES_LONG)
1563 *result = res->u.longval;
1568 resource_string_value(const char *name, int unit, const char *resname,
1572 struct config_resource *res;
1574 if ((error = resource_find(name, unit, resname, &res)) != 0)
1576 if (res->type != RES_STRING)
1578 *result = res->u.stringval;
1583 resource_query_string(int i, const char *resname, const char *value)
1589 for (; i < devtab_count; i++)
1590 if (resource_match_string(i, resname, value) >= 0)
1596 resource_locate(int i, const char *resname)
1602 for (; i < devtab_count; i++)
1603 if (!strcmp(devtab[i].name, resname))
1609 resource_count(void)
1611 return(devtab_count);
1615 resource_query_name(int i)
1617 return(devtab[i].name);
1621 resource_query_unit(int i)
1623 return(devtab[i].unit);
1627 resource_create(const char *name, int unit, const char *resname,
1628 resource_type type, struct config_resource **result)
1631 struct config_resource *res = NULL;
1633 for (i = 0; i < devtab_count; i++)
1634 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1635 res = devtab[i].resources;
1639 i = resource_new_name(name, unit);
1642 res = devtab[i].resources;
1644 for (j = 0; j < devtab[i].resource_count; j++, res++)
1645 if (!strcmp(res->name, resname)) {
1649 j = resource_new_resname(i, resname, type);
1652 res = &devtab[i].resources[j];
1658 resource_set_int(const char *name, int unit, const char *resname, int value)
1661 struct config_resource *res;
1663 error = resource_create(name, unit, resname, RES_INT, &res);
1666 if (res->type != RES_INT)
1668 res->u.intval = value;
1673 resource_set_long(const char *name, int unit, const char *resname, long value)
1676 struct config_resource *res;
1678 error = resource_create(name, unit, resname, RES_LONG, &res);
1681 if (res->type != RES_LONG)
1683 res->u.longval = value;
1688 resource_set_string(const char *name, int unit, const char *resname,
1692 struct config_resource *res;
1694 error = resource_create(name, unit, resname, RES_STRING, &res);
1697 if (res->type != RES_STRING)
1699 if (res->u.stringval)
1700 kfree(res->u.stringval, M_TEMP);
1701 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
1702 if (res->u.stringval == NULL)
1704 strcpy(res->u.stringval, value);
1709 resource_cfgload(void *dummy __unused)
1711 struct config_resource *res, *cfgres;
1714 char *name, *resname;
1718 int config_devtab_count;
1720 config_devtab_count = devtab_count;
1724 for (i = 0; i < config_devtab_count; i++) {
1725 name = config_devtab[i].name;
1726 unit = config_devtab[i].unit;
1728 for (j = 0; j < config_devtab[i].resource_count; j++) {
1729 cfgres = config_devtab[i].resources;
1730 resname = cfgres[j].name;
1731 type = cfgres[j].type;
1732 error = resource_create(name, unit, resname, type,
1735 kprintf("create resource %s%d: error %d\n",
1739 if (res->type != type) {
1740 kprintf("type mismatch %s%d: %d != %d\n",
1741 name, unit, res->type, type);
1746 res->u.intval = cfgres[j].u.intval;
1749 res->u.longval = cfgres[j].u.longval;
1752 if (res->u.stringval)
1753 kfree(res->u.stringval, M_TEMP);
1754 stringval = cfgres[j].u.stringval;
1755 res->u.stringval = kmalloc(strlen(stringval) + 1,
1757 if (res->u.stringval == NULL)
1759 strcpy(res->u.stringval, stringval);
1762 panic("unknown resource type %d", type);
1767 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
1770 /*======================================*/
1772 * Some useful method implementations to make life easier for bus drivers.
1776 resource_list_init(struct resource_list *rl)
1782 resource_list_free(struct resource_list *rl)
1784 struct resource_list_entry *rle;
1786 while ((rle = SLIST_FIRST(rl)) != NULL) {
1788 panic("resource_list_free: resource entry is busy");
1789 SLIST_REMOVE_HEAD(rl, link);
1795 resource_list_add(struct resource_list *rl,
1797 u_long start, u_long end, u_long count)
1799 struct resource_list_entry *rle;
1801 rle = resource_list_find(rl, type, rid);
1803 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
1806 panic("resource_list_add: can't record entry");
1807 SLIST_INSERT_HEAD(rl, rle, link);
1814 panic("resource_list_add: resource entry is busy");
1821 struct resource_list_entry*
1822 resource_list_find(struct resource_list *rl,
1825 struct resource_list_entry *rle;
1827 SLIST_FOREACH(rle, rl, link)
1828 if (rle->type == type && rle->rid == rid)
1834 resource_list_delete(struct resource_list *rl,
1837 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1840 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1846 resource_list_alloc(struct resource_list *rl,
1847 device_t bus, device_t child,
1849 u_long start, u_long end,
1850 u_long count, u_int flags)
1852 struct resource_list_entry *rle = 0;
1853 int passthrough = (device_get_parent(child) != bus);
1854 int isdefault = (start == 0UL && end == ~0UL);
1857 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1859 start, end, count, flags));
1862 rle = resource_list_find(rl, type, *rid);
1865 return(0); /* no resource of that type/rid */
1867 panic("resource_list_alloc: resource entry is busy");
1871 count = max(count, rle->count);
1872 end = max(rle->end, start + count - 1);
1875 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1876 type, rid, start, end, count, flags);
1879 * Record the new range.
1882 rle->start = rman_get_start(rle->res);
1883 rle->end = rman_get_end(rle->res);
1891 resource_list_release(struct resource_list *rl,
1892 device_t bus, device_t child,
1893 int type, int rid, struct resource *res)
1895 struct resource_list_entry *rle = 0;
1896 int passthrough = (device_get_parent(child) != bus);
1900 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1904 rle = resource_list_find(rl, type, rid);
1907 panic("resource_list_release: can't find resource");
1909 panic("resource_list_release: resource entry is not busy");
1911 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1921 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1924 struct resource_list_entry *rle;
1925 int printed, retval;
1929 /* Yes, this is kinda cheating */
1930 SLIST_FOREACH(rle, rl, link) {
1931 if (rle->type == type) {
1933 retval += kprintf(" %s ", name);
1935 retval += kprintf(",");
1937 retval += kprintf(format, rle->start);
1938 if (rle->count > 1) {
1939 retval += kprintf("-");
1940 retval += kprintf(format, rle->start +
1949 * Generic driver/device identify functions. These will install a device
1950 * rendezvous point under the parent using the same name as the driver
1951 * name, which will at a later time be probed and attached.
1953 * These functions are used when the parent does not 'scan' its bus for
1954 * matching devices, or for the particular devices using these functions,
1955 * or when the device is a pseudo or synthesized device (such as can be
1956 * found under firewire and ppbus).
1959 bus_generic_identify(driver_t *driver, device_t parent)
1961 if (parent->state == DS_ATTACHED)
1963 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
1968 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
1970 if (parent->state == DS_ATTACHED)
1972 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
1977 * Call DEVICE_IDENTIFY for each driver.
1980 bus_generic_probe(device_t dev)
1982 devclass_t dc = dev->devclass;
1985 TAILQ_FOREACH(dl, &dc->drivers, link) {
1986 DEVICE_IDENTIFY(dl->driver, dev);
1993 * This is an aweful hack due to the isa bus and autoconf code not
1994 * probing the ISA devices until after everything else has configured.
1995 * The ISA bus did a dummy attach long ago so we have to set it back
1996 * to an earlier state so the probe thinks its the initial probe and
1999 * XXX remove by properly defering the ISA bus scan.
2002 bus_generic_probe_hack(device_t dev)
2004 if (dev->state == DS_ATTACHED) {
2005 dev->state = DS_ALIVE;
2006 bus_generic_probe(dev);
2007 dev->state = DS_ATTACHED;
2013 bus_generic_attach(device_t dev)
2017 TAILQ_FOREACH(child, &dev->children, link) {
2018 device_probe_and_attach(child);
2025 bus_generic_detach(device_t dev)
2030 if (dev->state != DS_ATTACHED)
2033 TAILQ_FOREACH(child, &dev->children, link)
2034 if ((error = device_detach(child)) != 0)
2041 bus_generic_shutdown(device_t dev)
2045 TAILQ_FOREACH(child, &dev->children, link)
2046 device_shutdown(child);
2052 bus_generic_suspend(device_t dev)
2055 device_t child, child2;
2057 TAILQ_FOREACH(child, &dev->children, link) {
2058 error = DEVICE_SUSPEND(child);
2060 for (child2 = TAILQ_FIRST(&dev->children);
2061 child2 && child2 != child;
2062 child2 = TAILQ_NEXT(child2, link))
2063 DEVICE_RESUME(child2);
2071 bus_generic_resume(device_t dev)
2075 TAILQ_FOREACH(child, &dev->children, link)
2076 DEVICE_RESUME(child);
2077 /* if resume fails, there's nothing we can usefully do... */
2083 bus_print_child_header(device_t dev, device_t child)
2087 if (device_get_desc(child))
2088 retval += device_printf(child, "<%s>", device_get_desc(child));
2090 retval += kprintf("%s", device_get_nameunit(child));
2092 if (child->state != DS_ATTACHED)
2093 kprintf(" [tentative]");
2095 kprintf(" [attached!]");
2101 bus_print_child_footer(device_t dev, device_t child)
2103 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2107 bus_generic_add_child(device_t dev, device_t child, int order,
2108 const char *name, int unit)
2111 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2113 dev = device_add_child_ordered(child, order, name, unit);
2119 bus_generic_print_child(device_t dev, device_t child)
2123 retval += bus_print_child_header(dev, child);
2124 retval += bus_print_child_footer(dev, child);
2130 bus_generic_read_ivar(device_t dev, device_t child, int index,
2136 error = BUS_READ_IVAR(dev->parent, child, index, result);
2143 bus_generic_write_ivar(device_t dev, device_t child, int index,
2149 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2156 * Resource list are used for iterations, do not recurse.
2158 struct resource_list *
2159 bus_generic_get_resource_list(device_t dev, device_t child)
2165 bus_generic_driver_added(device_t dev, driver_t *driver)
2169 DEVICE_IDENTIFY(driver, dev);
2170 TAILQ_FOREACH(child, &dev->children, link) {
2171 if (child->state == DS_NOTPRESENT)
2172 device_probe_and_attach(child);
2177 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2178 int flags, driver_intr_t *intr, void *arg,
2179 void **cookiep, lwkt_serialize_t serializer)
2181 /* Propagate up the bus hierarchy until someone handles it. */
2183 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2184 intr, arg, cookiep, serializer));
2190 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2193 /* Propagate up the bus hierarchy until someone handles it. */
2195 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2201 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2204 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2210 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2213 BUS_ENABLE_INTR(dev->parent, child, cookie);
2217 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2218 enum intr_polarity pol)
2220 /* Propagate up the bus hierarchy until someone handles it. */
2222 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2228 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2229 u_long start, u_long end, u_long count, u_int flags)
2231 /* Propagate up the bus hierarchy until someone handles it. */
2233 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2234 start, end, count, flags));
2240 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2243 /* Propagate up the bus hierarchy until someone handles it. */
2245 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2251 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2254 /* Propagate up the bus hierarchy until someone handles it. */
2256 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2262 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2263 int rid, struct resource *r)
2265 /* Propagate up the bus hierarchy until someone handles it. */
2267 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2274 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2275 u_long *startp, u_long *countp)
2281 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2288 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2289 u_long start, u_long count)
2295 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2302 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2305 BUS_DELETE_RESOURCE(dev, child, type, rid);
2309 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2310 u_long *startp, u_long *countp)
2312 struct resource_list *rl = NULL;
2313 struct resource_list_entry *rle = NULL;
2315 rl = BUS_GET_RESOURCE_LIST(dev, child);
2319 rle = resource_list_find(rl, type, rid);
2324 *startp = rle->start;
2326 *countp = rle->count;
2332 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2333 u_long start, u_long count)
2335 struct resource_list *rl = NULL;
2337 rl = BUS_GET_RESOURCE_LIST(dev, child);
2341 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2347 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2349 struct resource_list *rl = NULL;
2351 rl = BUS_GET_RESOURCE_LIST(dev, child);
2355 resource_list_delete(rl, type, rid);
2359 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2360 int rid, struct resource *r)
2362 struct resource_list *rl = NULL;
2364 rl = BUS_GET_RESOURCE_LIST(dev, child);
2368 return(resource_list_release(rl, dev, child, type, rid, r));
2372 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2373 int *rid, u_long start, u_long end, u_long count, u_int flags)
2375 struct resource_list *rl = NULL;
2377 rl = BUS_GET_RESOURCE_LIST(dev, child);
2381 return(resource_list_alloc(rl, dev, child, type, rid,
2382 start, end, count, flags));
2386 bus_generic_child_present(device_t bus, device_t child)
2388 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2393 * Some convenience functions to make it easier for drivers to use the
2394 * resource-management functions. All these really do is hide the
2395 * indirection through the parent's method table, making for slightly
2396 * less-wordy code. In the future, it might make sense for this code
2397 * to maintain some sort of a list of resources allocated by each device.
2400 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2401 struct resource **res)
2405 for (i = 0; rs[i].type != -1; i++)
2407 for (i = 0; rs[i].type != -1; i++) {
2408 res[i] = bus_alloc_resource_any(dev,
2409 rs[i].type, &rs[i].rid, rs[i].flags);
2410 if (res[i] == NULL) {
2411 bus_release_resources(dev, rs, res);
2419 bus_release_resources(device_t dev, const struct resource_spec *rs,
2420 struct resource **res)
2424 for (i = 0; rs[i].type != -1; i++)
2425 if (res[i] != NULL) {
2426 bus_release_resource(
2427 dev, rs[i].type, rs[i].rid, res[i]);
2433 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2434 u_long count, u_int flags)
2436 if (dev->parent == 0)
2438 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2443 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2445 if (dev->parent == 0)
2447 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2451 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2453 if (dev->parent == 0)
2455 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2459 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2461 if (dev->parent == 0)
2463 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2467 bus_setup_intr(device_t dev, struct resource *r, int flags,
2468 driver_intr_t handler, void *arg,
2469 void **cookiep, lwkt_serialize_t serializer)
2471 if (dev->parent == 0)
2473 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2474 cookiep, serializer));
2478 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2480 if (dev->parent == 0)
2482 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2486 bus_enable_intr(device_t dev, void *cookie)
2489 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2493 bus_disable_intr(device_t dev, void *cookie)
2496 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2502 bus_set_resource(device_t dev, int type, int rid,
2503 u_long start, u_long count)
2505 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2510 bus_get_resource(device_t dev, int type, int rid,
2511 u_long *startp, u_long *countp)
2513 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2518 bus_get_resource_start(device_t dev, int type, int rid)
2520 u_long start, count;
2523 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2531 bus_get_resource_count(device_t dev, int type, int rid)
2533 u_long start, count;
2536 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2544 bus_delete_resource(device_t dev, int type, int rid)
2546 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2550 bus_child_present(device_t child)
2552 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2556 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2560 parent = device_get_parent(child);
2561 if (parent == NULL) {
2565 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2569 bus_child_location_str(device_t child, char *buf, size_t buflen)
2573 parent = device_get_parent(child);
2574 if (parent == NULL) {
2578 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2582 root_print_child(device_t dev, device_t child)
2588 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2589 void **cookiep, lwkt_serialize_t serializer)
2592 * If an interrupt mapping gets to here something bad has happened.
2594 panic("root_setup_intr");
2598 * If we get here, assume that the device is permanant and really is
2599 * present in the system. Removable bus drivers are expected to intercept
2600 * this call long before it gets here. We return -1 so that drivers that
2601 * really care can check vs -1 or some ERRNO returned higher in the food
2605 root_child_present(device_t dev, device_t child)
2611 * XXX NOTE! other defaults may be set in bus_if.m
2613 static kobj_method_t root_methods[] = {
2614 /* Device interface */
2615 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2616 KOBJMETHOD(device_suspend, bus_generic_suspend),
2617 KOBJMETHOD(device_resume, bus_generic_resume),
2620 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2621 KOBJMETHOD(bus_print_child, root_print_child),
2622 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2623 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2624 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2625 KOBJMETHOD(bus_child_present, root_child_present),
2630 static driver_t root_driver = {
2637 devclass_t root_devclass;
2640 root_bus_module_handler(module_t mod, int what, void* arg)
2644 root_bus = make_device(NULL, "root", 0);
2645 root_bus->desc = "System root bus";
2646 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2647 root_bus->driver = &root_driver;
2648 root_bus->state = DS_ALIVE;
2649 root_devclass = devclass_find_internal("root", NULL, FALSE);
2653 device_shutdown(root_bus);
2660 static moduledata_t root_bus_mod = {
2662 root_bus_module_handler,
2665 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2668 root_bus_configure(void)
2676 * handle device_identify based device attachments to the root_bus
2677 * (typically nexus).
2679 bus_generic_probe(root_bus);
2682 * Probe and attach the devices under root_bus.
2684 TAILQ_FOREACH(dev, &root_bus->children, link) {
2685 device_probe_and_attach(dev);
2689 * Wait for all asynchronous attaches to complete. If we don't
2690 * our legacy ISA bus scan could steal device unit numbers or
2694 if (numasyncthreads)
2695 kprintf("Waiting for async drivers to attach\n");
2696 while (numasyncthreads > 0) {
2697 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
2699 if (warncount == 0) {
2700 kprintf("Warning: Still waiting for %d "
2701 "drivers to attach\n", numasyncthreads);
2702 } else if (warncount == -30) {
2703 kprintf("Giving up on %d drivers\n", numasyncthreads);
2707 root_bus->state = DS_ATTACHED;
2711 driver_module_handler(module_t mod, int what, void *arg)
2714 struct driver_module_data *dmd;
2715 devclass_t bus_devclass;
2716 kobj_class_t driver;
2717 const char *parentname;
2719 dmd = (struct driver_module_data *)arg;
2720 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
2725 if (dmd->dmd_chainevh)
2726 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2728 driver = dmd->dmd_driver;
2729 PDEBUG(("Loading module: driver %s on bus %s",
2730 DRIVERNAME(driver), dmd->dmd_busname));
2733 * If the driver has any base classes, make the
2734 * devclass inherit from the devclass of the driver's
2735 * first base class. This will allow the system to
2736 * search for drivers in both devclasses for children
2737 * of a device using this driver.
2739 if (driver->baseclasses)
2740 parentname = driver->baseclasses[0]->name;
2743 *dmd->dmd_devclass = devclass_find_internal(driver->name,
2746 error = devclass_add_driver(bus_devclass, driver);
2752 PDEBUG(("Unloading module: driver %s from bus %s",
2753 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
2754 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
2756 if (!error && dmd->dmd_chainevh)
2757 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2767 * The _short versions avoid iteration by not calling anything that prints
2768 * more than oneliners. I love oneliners.
2772 print_device_short(device_t dev, int indent)
2777 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2778 dev->unit, dev->desc,
2779 (dev->parent? "":"no "),
2780 (TAILQ_EMPTY(&dev->children)? "no ":""),
2781 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2782 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2783 (dev->flags&DF_WILDCARD? "wildcard,":""),
2784 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2785 (dev->ivars? "":"no "),
2786 (dev->softc? "":"no "),
2791 print_device(device_t dev, int indent)
2796 print_device_short(dev, indent);
2798 indentprintf(("Parent:\n"));
2799 print_device_short(dev->parent, indent+1);
2800 indentprintf(("Driver:\n"));
2801 print_driver_short(dev->driver, indent+1);
2802 indentprintf(("Devclass:\n"));
2803 print_devclass_short(dev->devclass, indent+1);
2807 * Print the device and all its children (indented).
2810 print_device_tree_short(device_t dev, int indent)
2817 print_device_short(dev, indent);
2819 TAILQ_FOREACH(child, &dev->children, link)
2820 print_device_tree_short(child, indent+1);
2824 * Print the device and all its children (indented).
2827 print_device_tree(device_t dev, int indent)
2834 print_device(dev, indent);
2836 TAILQ_FOREACH(child, &dev->children, link)
2837 print_device_tree(child, indent+1);
2841 print_driver_short(driver_t *driver, int indent)
2846 indentprintf(("driver %s: softc size = %d\n",
2847 driver->name, driver->size));
2851 print_driver(driver_t *driver, int indent)
2856 print_driver_short(driver, indent);
2861 print_driver_list(driver_list_t drivers, int indent)
2863 driverlink_t driver;
2865 TAILQ_FOREACH(driver, &drivers, link)
2866 print_driver(driver->driver, indent);
2870 print_devclass_short(devclass_t dc, int indent)
2875 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2879 print_devclass(devclass_t dc, int indent)
2886 print_devclass_short(dc, indent);
2887 indentprintf(("Drivers:\n"));
2888 print_driver_list(dc->drivers, indent+1);
2890 indentprintf(("Devices:\n"));
2891 for (i = 0; i < dc->maxunit; i++)
2893 print_device(dc->devices[i], indent+1);
2897 print_devclass_list_short(void)
2901 kprintf("Short listing of devclasses, drivers & devices:\n");
2902 TAILQ_FOREACH(dc, &devclasses, link) {
2903 print_devclass_short(dc, 0);
2908 print_devclass_list(void)
2912 kprintf("Full listing of devclasses, drivers & devices:\n");
2913 TAILQ_FOREACH(dc, &devclasses, link) {
2914 print_devclass(dc, 0);
2921 * Check to see if a device is disabled via a disabled hint.
2924 resource_disabled(const char *name, int unit)
2928 error = resource_int_value(name, unit, "disabled", &value);