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.43 2008/09/05 10:28:35 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 devclass_get_maxunit(devclass_t dc)
408 devclass_set_parent(devclass_t dc, devclass_t pdc)
414 devclass_get_parent(devclass_t dc)
420 devclass_alloc_unit(devclass_t dc, int *unitp)
424 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
426 /* If we have been given a wired unit number, check for existing device */
428 if (unit >= 0 && unit < dc->maxunit &&
429 dc->devices[unit] != NULL) {
431 kprintf("%s-: %s%d exists, using next available unit number\n",
432 dc->name, dc->name, unit);
433 /* find the next available slot */
434 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
438 /* Unwired device, find the next available slot for it */
440 while (unit < dc->maxunit && dc->devices[unit] != NULL)
445 * We've selected a unit beyond the length of the table, so let's
446 * extend the table to make room for all units up to and including
449 if (unit >= dc->maxunit) {
453 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
454 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
458 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
460 kfree(dc->devices, M_BUS);
461 dc->devices = newlist;
462 dc->maxunit = newsize;
464 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
471 devclass_add_device(devclass_t dc, device_t dev)
475 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
477 buflen = strlen(dc->name) + 5;
478 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
482 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
483 kfree(dev->nameunit, M_BUS);
484 dev->nameunit = NULL;
487 dc->devices[dev->unit] = dev;
489 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
491 #ifdef DEVICE_SYSCTLS
492 device_register_oids(dev);
499 devclass_delete_device(devclass_t dc, device_t dev)
504 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
506 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
507 panic("devclass_delete_device: inconsistent device class");
508 dc->devices[dev->unit] = NULL;
509 if (dev->flags & DF_WILDCARD)
511 dev->devclass = NULL;
512 kfree(dev->nameunit, M_BUS);
513 dev->nameunit = NULL;
515 #ifdef DEVICE_SYSCTLS
516 device_unregister_oids(dev);
523 make_device(device_t parent, const char *name, int unit)
528 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
531 dc = devclass_find_internal(name, NULL, TRUE);
533 kprintf("make_device: can't find device class %s\n", name);
539 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
543 dev->parent = parent;
544 TAILQ_INIT(&dev->children);
545 kobj_init((kobj_t) dev, &null_class);
547 dev->devclass = NULL;
549 dev->nameunit = NULL;
553 dev->flags = DF_ENABLED;
556 dev->flags |= DF_WILDCARD;
558 dev->flags |= DF_FIXEDCLASS;
559 if (devclass_add_device(dc, dev) != 0) {
560 kobj_delete((kobj_t)dev, M_BUS);
567 dev->state = DS_NOTPRESENT;
573 device_print_child(device_t dev, device_t child)
577 if (device_is_alive(child))
578 retval += BUS_PRINT_CHILD(dev, child);
580 retval += device_printf(child, " not found\n");
586 device_add_child(device_t dev, const char *name, int unit)
588 return device_add_child_ordered(dev, 0, name, unit);
592 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
597 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
600 child = make_device(dev, name, unit);
603 child->order = order;
605 TAILQ_FOREACH(place, &dev->children, link)
606 if (place->order > order)
611 * The device 'place' is the first device whose order is
612 * greater than the new child.
614 TAILQ_INSERT_BEFORE(place, child, link);
617 * The new child's order is greater or equal to the order of
618 * any existing device. Add the child to the tail of the list.
620 TAILQ_INSERT_TAIL(&dev->children, child, link);
627 device_delete_child(device_t dev, device_t child)
632 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
634 /* remove children first */
635 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
636 error = device_delete_child(child, grandchild);
641 if ((error = device_detach(child)) != 0)
644 devclass_delete_device(child->devclass, child);
645 TAILQ_REMOVE(&dev->children, child, link);
646 device_set_desc(child, NULL);
647 kobj_delete((kobj_t)child, M_BUS);
653 * @brief Find a device given a unit number
655 * This is similar to devclass_get_devices() but only searches for
656 * devices which have @p dev as a parent.
658 * @param dev the parent device to search
659 * @param unit the unit number to search for. If the unit is -1,
660 * return the first child of @p dev which has name
661 * @p classname (that is, the one with the lowest unit.)
663 * @returns the device with the given unit number or @c
664 * NULL if there is no such device
667 device_find_child(device_t dev, const char *classname, int unit)
672 dc = devclass_find(classname);
677 child = devclass_get_device(dc, unit);
678 if (child && child->parent == dev)
681 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
682 child = devclass_get_device(dc, unit);
683 if (child && child->parent == dev)
691 first_matching_driver(devclass_t dc, device_t dev)
694 return(devclass_find_driver_internal(dc, dev->devclass->name));
696 return(TAILQ_FIRST(&dc->drivers));
700 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
704 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
705 if (!strcmp(dev->devclass->name, dl->driver->name))
709 return(TAILQ_NEXT(last, link));
713 device_probe_child(device_t dev, device_t child)
716 driverlink_t best = 0;
719 int hasclass = (child->devclass != 0);
723 panic("device_probe_child: parent device has no devclass");
725 if (child->state == DS_ALIVE)
728 for (; dc; dc = dc->parent) {
729 for (dl = first_matching_driver(dc, child); dl;
730 dl = next_matching_driver(dc, child, dl)) {
731 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
732 device_set_driver(child, dl->driver);
734 device_set_devclass(child, dl->driver->name);
735 result = DEVICE_PROBE(child);
737 device_set_devclass(child, 0);
740 * If the driver returns SUCCESS, there can be
741 * no higher match for this device.
750 * The driver returned an error so it
751 * certainly doesn't match.
754 device_set_driver(child, 0);
759 * A priority lower than SUCCESS, remember the
760 * best matching driver. Initialise the value
761 * of pri for the first match.
763 if (best == 0 || result > pri) {
770 * If we have unambiguous match in this devclass,
771 * don't look in the parent.
773 if (best && pri == 0)
778 * If we found a driver, change state and initialise the devclass.
781 if (!child->devclass)
782 device_set_devclass(child, best->driver->name);
783 device_set_driver(child, best->driver);
786 * A bit bogus. Call the probe method again to make
787 * sure that we have the right description.
791 child->state = DS_ALIVE;
799 device_get_parent(device_t dev)
805 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
812 TAILQ_FOREACH(child, &dev->children, link)
815 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
820 TAILQ_FOREACH(child, &dev->children, link) {
832 device_get_driver(device_t dev)
838 device_get_devclass(device_t dev)
840 return(dev->devclass);
844 device_get_name(device_t dev)
847 return devclass_get_name(dev->devclass);
852 device_get_nameunit(device_t dev)
854 return(dev->nameunit);
858 device_get_unit(device_t dev)
864 device_get_desc(device_t dev)
870 device_get_flags(device_t dev)
872 return(dev->devflags);
876 device_print_prettyname(device_t dev)
878 const char *name = device_get_name(dev);
881 return kprintf("unknown: ");
883 return kprintf("%s%d: ", name, device_get_unit(dev));
887 device_printf(device_t dev, const char * fmt, ...)
892 retval = device_print_prettyname(dev);
894 retval += kvprintf(fmt, ap);
900 device_set_desc_internal(device_t dev, const char* desc, int copy)
902 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
903 kfree(dev->desc, M_BUS);
904 dev->flags &= ~DF_DESCMALLOCED;
909 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
911 strcpy(dev->desc, desc);
912 dev->flags |= DF_DESCMALLOCED;
915 /* Avoid a -Wcast-qual warning */
916 dev->desc = (char *)(uintptr_t) desc;
918 #ifdef DEVICE_SYSCTLS
920 struct sysctl_oid *oid = &dev->oid[1];
921 oid->oid_arg1 = dev->desc ? dev->desc : "";
922 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
928 device_set_desc(device_t dev, const char* desc)
930 device_set_desc_internal(dev, desc, FALSE);
934 device_set_desc_copy(device_t dev, const char* desc)
936 device_set_desc_internal(dev, desc, TRUE);
940 device_set_flags(device_t dev, uint32_t flags)
942 dev->devflags = flags;
946 device_get_softc(device_t dev)
952 device_set_softc(device_t dev, void *softc)
954 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
955 kfree(dev->softc, M_BUS);
958 dev->flags |= DF_EXTERNALSOFTC;
960 dev->flags &= ~DF_EXTERNALSOFTC;
964 device_set_async_attach(device_t dev, int enable)
967 dev->flags |= DF_ASYNCPROBE;
969 dev->flags &= ~DF_ASYNCPROBE;
973 device_get_ivars(device_t dev)
979 device_set_ivars(device_t dev, void * ivars)
988 device_get_state(device_t dev)
994 device_enable(device_t dev)
996 dev->flags |= DF_ENABLED;
1000 device_disable(device_t dev)
1002 dev->flags &= ~DF_ENABLED;
1009 device_busy(device_t dev)
1011 if (dev->state < DS_ATTACHED)
1012 panic("device_busy: called for unattached device");
1013 if (dev->busy == 0 && dev->parent)
1014 device_busy(dev->parent);
1016 dev->state = DS_BUSY;
1023 device_unbusy(device_t dev)
1025 if (dev->state != DS_BUSY)
1026 panic("device_unbusy: called for non-busy device");
1028 if (dev->busy == 0) {
1030 device_unbusy(dev->parent);
1031 dev->state = DS_ATTACHED;
1036 device_quiet(device_t dev)
1038 dev->flags |= DF_QUIET;
1042 device_verbose(device_t dev)
1044 dev->flags &= ~DF_QUIET;
1048 device_is_quiet(device_t dev)
1050 return((dev->flags & DF_QUIET) != 0);
1054 device_is_enabled(device_t dev)
1056 return((dev->flags & DF_ENABLED) != 0);
1060 device_is_alive(device_t dev)
1062 return(dev->state >= DS_ALIVE);
1066 device_is_attached(device_t dev)
1068 return(dev->state >= DS_ATTACHED);
1072 device_set_devclass(device_t dev, const char *classname)
1078 devclass_delete_device(dev->devclass, dev);
1082 if (dev->devclass) {
1083 kprintf("device_set_devclass: device class already set\n");
1087 dc = devclass_find_internal(classname, NULL, TRUE);
1091 return(devclass_add_device(dc, dev));
1095 device_set_driver(device_t dev, driver_t *driver)
1097 if (dev->state >= DS_ATTACHED)
1100 if (dev->driver == driver)
1103 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1104 kfree(dev->softc, M_BUS);
1107 kobj_delete((kobj_t) dev, 0);
1108 dev->driver = driver;
1110 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1111 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1112 dev->softc = kmalloc(driver->size, M_BUS,
1113 M_INTWAIT | M_ZERO);
1115 kobj_delete((kobj_t)dev, 0);
1116 kobj_init((kobj_t) dev, &null_class);
1122 kobj_init((kobj_t) dev, &null_class);
1127 device_probe_and_attach(device_t dev)
1129 device_t bus = dev->parent;
1132 if (dev->state >= DS_ALIVE)
1135 if ((dev->flags & DF_ENABLED) == 0) {
1137 device_print_prettyname(dev);
1138 kprintf("not probed (disabled)\n");
1143 error = device_probe_child(bus, dev);
1145 if (!(dev->flags & DF_DONENOMATCH)) {
1146 BUS_PROBE_NOMATCH(bus, dev);
1147 dev->flags |= DF_DONENOMATCH;
1153 * Output the exact device chain prior to the attach in case the
1154 * system locks up during attach, and generate the full info after
1155 * the attach so correct irq and other information is displayed.
1157 if (bootverbose && !device_is_quiet(dev)) {
1160 kprintf("%s", device_get_nameunit(dev));
1161 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1162 kprintf(".%s", device_get_nameunit(tmp));
1165 if (!device_is_quiet(dev))
1166 device_print_child(bus, dev);
1167 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1168 kprintf("%s: probing asynchronously\n",
1169 device_get_nameunit(dev));
1170 dev->state = DS_INPROGRESS;
1171 device_attach_async(dev);
1174 error = device_doattach(dev);
1180 * Device is known to be alive, do the attach asynchronously.
1182 * The MP lock is held by all threads.
1185 device_attach_async(device_t dev)
1189 atomic_add_int(&numasyncthreads, 1);
1190 lwkt_create(device_attach_thread, dev, &td, NULL,
1191 0, 0, (dev->desc ? dev->desc : "devattach"));
1195 device_attach_thread(void *arg)
1199 (void)device_doattach(dev);
1200 atomic_subtract_int(&numasyncthreads, 1);
1201 wakeup(&numasyncthreads);
1205 * Device is known to be alive, do the attach (synchronous or asynchronous)
1208 device_doattach(device_t dev)
1210 device_t bus = dev->parent;
1211 int hasclass = (dev->devclass != 0);
1214 error = DEVICE_ATTACH(dev);
1216 dev->state = DS_ATTACHED;
1217 if (bootverbose && !device_is_quiet(dev))
1218 device_print_child(bus, dev);
1220 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1221 dev->driver->name, dev->unit, error);
1222 /* Unset the class that was set in device_probe_child */
1224 device_set_devclass(dev, 0);
1225 device_set_driver(dev, NULL);
1226 dev->state = DS_NOTPRESENT;
1232 device_detach(device_t dev)
1236 PDEBUG(("%s", DEVICENAME(dev)));
1237 if (dev->state == DS_BUSY)
1239 if (dev->state != DS_ATTACHED)
1242 if ((error = DEVICE_DETACH(dev)) != 0)
1244 device_printf(dev, "detached\n");
1246 BUS_CHILD_DETACHED(dev->parent, dev);
1248 if (!(dev->flags & DF_FIXEDCLASS))
1249 devclass_delete_device(dev->devclass, dev);
1251 dev->state = DS_NOTPRESENT;
1252 device_set_driver(dev, NULL);
1258 device_shutdown(device_t dev)
1260 if (dev->state < DS_ATTACHED)
1262 PDEBUG(("%s", DEVICENAME(dev)));
1263 return DEVICE_SHUTDOWN(dev);
1267 device_set_unit(device_t dev, int unit)
1272 dc = device_get_devclass(dev);
1273 if (unit < dc->maxunit && dc->devices[unit])
1275 err = devclass_delete_device(dc, dev);
1279 err = devclass_add_device(dc, dev);
1283 #ifdef DEVICE_SYSCTLS
1286 * Sysctl nodes for devices.
1289 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1292 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1294 device_t dev = arg1;
1296 int first = 1, error = 0;
1298 TAILQ_FOREACH(child, &dev->children, link)
1299 if (child->nameunit) {
1301 error = SYSCTL_OUT(req, ",", 1);
1306 error = SYSCTL_OUT(req, child->nameunit,
1307 strlen(child->nameunit));
1312 error = SYSCTL_OUT(req, "", 1);
1318 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1320 device_t dev = arg1;
1322 switch (dev->state) {
1324 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1326 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1328 return SYSCTL_OUT(req, "in-progress", sizeof("in-progress"));
1330 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1332 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1339 device_register_oids(device_t dev)
1341 struct sysctl_oid* oid;
1344 bzero(oid, sizeof(*oid));
1345 oid->oid_parent = &sysctl__hw_devices_children;
1346 oid->oid_number = OID_AUTO;
1347 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1348 oid->oid_arg1 = &dev->oidlist[0];
1350 oid->oid_name = dev->nameunit;
1351 oid->oid_handler = 0;
1353 SLIST_INIT(&dev->oidlist[0]);
1354 sysctl_register_oid(oid);
1357 bzero(oid, sizeof(*oid));
1358 oid->oid_parent = &dev->oidlist[0];
1359 oid->oid_number = OID_AUTO;
1360 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1361 oid->oid_arg1 = dev->desc ? dev->desc : "";
1362 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1363 oid->oid_name = "desc";
1364 oid->oid_handler = sysctl_handle_string;
1366 sysctl_register_oid(oid);
1369 bzero(oid, sizeof(*oid));
1370 oid->oid_parent = &dev->oidlist[0];
1371 oid->oid_number = OID_AUTO;
1372 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1373 oid->oid_arg1 = dev;
1375 oid->oid_name = "children";
1376 oid->oid_handler = sysctl_handle_children;
1378 sysctl_register_oid(oid);
1381 bzero(oid, sizeof(*oid));
1382 oid->oid_parent = &dev->oidlist[0];
1383 oid->oid_number = OID_AUTO;
1384 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1385 oid->oid_arg1 = dev;
1387 oid->oid_name = "state";
1388 oid->oid_handler = sysctl_handle_state;
1390 sysctl_register_oid(oid);
1394 device_unregister_oids(device_t dev)
1396 sysctl_unregister_oid(&dev->oid[0]);
1397 sysctl_unregister_oid(&dev->oid[1]);
1398 sysctl_unregister_oid(&dev->oid[2]);
1403 /*======================================*/
1405 * Access functions for device resources.
1408 /* Supplied by config(8) in ioconf.c */
1409 extern struct config_device config_devtab[];
1410 extern int devtab_count;
1412 /* Runtime version */
1413 struct config_device *devtab = config_devtab;
1416 resource_new_name(const char *name, int unit)
1418 struct config_device *new;
1420 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1421 M_INTWAIT | M_ZERO);
1424 if (devtab && devtab_count > 0)
1425 bcopy(devtab, new, devtab_count * sizeof(*new));
1426 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1427 if (new[devtab_count].name == NULL) {
1431 strcpy(new[devtab_count].name, name);
1432 new[devtab_count].unit = unit;
1433 new[devtab_count].resource_count = 0;
1434 new[devtab_count].resources = NULL;
1435 if (devtab && devtab != config_devtab)
1436 kfree(devtab, M_TEMP);
1438 return devtab_count++;
1442 resource_new_resname(int j, const char *resname, resource_type type)
1444 struct config_resource *new;
1447 i = devtab[j].resource_count;
1448 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1451 if (devtab[j].resources && i > 0)
1452 bcopy(devtab[j].resources, new, i * sizeof(*new));
1453 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1454 if (new[i].name == NULL) {
1458 strcpy(new[i].name, resname);
1460 if (devtab[j].resources)
1461 kfree(devtab[j].resources, M_TEMP);
1462 devtab[j].resources = new;
1463 devtab[j].resource_count = i + 1;
1468 resource_match_string(int i, const char *resname, const char *value)
1471 struct config_resource *res;
1473 for (j = 0, res = devtab[i].resources;
1474 j < devtab[i].resource_count; j++, res++)
1475 if (!strcmp(res->name, resname)
1476 && res->type == RES_STRING
1477 && !strcmp(res->u.stringval, value))
1483 resource_find(const char *name, int unit, const char *resname,
1484 struct config_resource **result)
1487 struct config_resource *res;
1490 * First check specific instances, then generic.
1492 for (i = 0; i < devtab_count; i++) {
1493 if (devtab[i].unit < 0)
1495 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1496 res = devtab[i].resources;
1497 for (j = 0; j < devtab[i].resource_count; j++, res++)
1498 if (!strcmp(res->name, resname)) {
1504 for (i = 0; i < devtab_count; i++) {
1505 if (devtab[i].unit >= 0)
1507 /* XXX should this `&& devtab[i].unit == unit' be here? */
1508 /* XXX if so, then the generic match does nothing */
1509 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1510 res = devtab[i].resources;
1511 for (j = 0; j < devtab[i].resource_count; j++, res++)
1512 if (!strcmp(res->name, resname)) {
1522 resource_int_value(const char *name, int unit, const char *resname, int *result)
1525 struct config_resource *res;
1527 if ((error = resource_find(name, unit, resname, &res)) != 0)
1529 if (res->type != RES_INT)
1531 *result = res->u.intval;
1536 resource_long_value(const char *name, int unit, const char *resname,
1540 struct config_resource *res;
1542 if ((error = resource_find(name, unit, resname, &res)) != 0)
1544 if (res->type != RES_LONG)
1546 *result = res->u.longval;
1551 resource_string_value(const char *name, int unit, const char *resname,
1555 struct config_resource *res;
1557 if ((error = resource_find(name, unit, resname, &res)) != 0)
1559 if (res->type != RES_STRING)
1561 *result = res->u.stringval;
1566 resource_query_string(int i, const char *resname, const char *value)
1572 for (; i < devtab_count; i++)
1573 if (resource_match_string(i, resname, value) >= 0)
1579 resource_locate(int i, const char *resname)
1585 for (; i < devtab_count; i++)
1586 if (!strcmp(devtab[i].name, resname))
1592 resource_count(void)
1594 return(devtab_count);
1598 resource_query_name(int i)
1600 return(devtab[i].name);
1604 resource_query_unit(int i)
1606 return(devtab[i].unit);
1610 resource_create(const char *name, int unit, const char *resname,
1611 resource_type type, struct config_resource **result)
1614 struct config_resource *res = NULL;
1616 for (i = 0; i < devtab_count; i++)
1617 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1618 res = devtab[i].resources;
1622 i = resource_new_name(name, unit);
1625 res = devtab[i].resources;
1627 for (j = 0; j < devtab[i].resource_count; j++, res++)
1628 if (!strcmp(res->name, resname)) {
1632 j = resource_new_resname(i, resname, type);
1635 res = &devtab[i].resources[j];
1641 resource_set_int(const char *name, int unit, const char *resname, int value)
1644 struct config_resource *res;
1646 error = resource_create(name, unit, resname, RES_INT, &res);
1649 if (res->type != RES_INT)
1651 res->u.intval = value;
1656 resource_set_long(const char *name, int unit, const char *resname, long value)
1659 struct config_resource *res;
1661 error = resource_create(name, unit, resname, RES_LONG, &res);
1664 if (res->type != RES_LONG)
1666 res->u.longval = value;
1671 resource_set_string(const char *name, int unit, const char *resname,
1675 struct config_resource *res;
1677 error = resource_create(name, unit, resname, RES_STRING, &res);
1680 if (res->type != RES_STRING)
1682 if (res->u.stringval)
1683 kfree(res->u.stringval, M_TEMP);
1684 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
1685 if (res->u.stringval == NULL)
1687 strcpy(res->u.stringval, value);
1692 resource_cfgload(void *dummy __unused)
1694 struct config_resource *res, *cfgres;
1697 char *name, *resname;
1701 int config_devtab_count;
1703 config_devtab_count = devtab_count;
1707 for (i = 0; i < config_devtab_count; i++) {
1708 name = config_devtab[i].name;
1709 unit = config_devtab[i].unit;
1711 for (j = 0; j < config_devtab[i].resource_count; j++) {
1712 cfgres = config_devtab[i].resources;
1713 resname = cfgres[j].name;
1714 type = cfgres[j].type;
1715 error = resource_create(name, unit, resname, type,
1718 kprintf("create resource %s%d: error %d\n",
1722 if (res->type != type) {
1723 kprintf("type mismatch %s%d: %d != %d\n",
1724 name, unit, res->type, type);
1729 res->u.intval = cfgres[j].u.intval;
1732 res->u.longval = cfgres[j].u.longval;
1735 if (res->u.stringval)
1736 kfree(res->u.stringval, M_TEMP);
1737 stringval = cfgres[j].u.stringval;
1738 res->u.stringval = kmalloc(strlen(stringval) + 1,
1740 if (res->u.stringval == NULL)
1742 strcpy(res->u.stringval, stringval);
1745 panic("unknown resource type %d", type);
1750 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
1753 /*======================================*/
1755 * Some useful method implementations to make life easier for bus drivers.
1759 resource_list_init(struct resource_list *rl)
1765 resource_list_free(struct resource_list *rl)
1767 struct resource_list_entry *rle;
1769 while ((rle = SLIST_FIRST(rl)) != NULL) {
1771 panic("resource_list_free: resource entry is busy");
1772 SLIST_REMOVE_HEAD(rl, link);
1778 resource_list_add(struct resource_list *rl,
1780 u_long start, u_long end, u_long count)
1782 struct resource_list_entry *rle;
1784 rle = resource_list_find(rl, type, rid);
1786 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
1789 panic("resource_list_add: can't record entry");
1790 SLIST_INSERT_HEAD(rl, rle, link);
1797 panic("resource_list_add: resource entry is busy");
1804 struct resource_list_entry*
1805 resource_list_find(struct resource_list *rl,
1808 struct resource_list_entry *rle;
1810 SLIST_FOREACH(rle, rl, link)
1811 if (rle->type == type && rle->rid == rid)
1817 resource_list_delete(struct resource_list *rl,
1820 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1823 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1829 resource_list_alloc(struct resource_list *rl,
1830 device_t bus, device_t child,
1832 u_long start, u_long end,
1833 u_long count, u_int flags)
1835 struct resource_list_entry *rle = 0;
1836 int passthrough = (device_get_parent(child) != bus);
1837 int isdefault = (start == 0UL && end == ~0UL);
1840 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1842 start, end, count, flags));
1845 rle = resource_list_find(rl, type, *rid);
1848 return(0); /* no resource of that type/rid */
1850 panic("resource_list_alloc: resource entry is busy");
1854 count = max(count, rle->count);
1855 end = max(rle->end, start + count - 1);
1858 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1859 type, rid, start, end, count, flags);
1862 * Record the new range.
1865 rle->start = rman_get_start(rle->res);
1866 rle->end = rman_get_end(rle->res);
1874 resource_list_release(struct resource_list *rl,
1875 device_t bus, device_t child,
1876 int type, int rid, struct resource *res)
1878 struct resource_list_entry *rle = 0;
1879 int passthrough = (device_get_parent(child) != bus);
1883 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1887 rle = resource_list_find(rl, type, rid);
1890 panic("resource_list_release: can't find resource");
1892 panic("resource_list_release: resource entry is not busy");
1894 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1904 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1907 struct resource_list_entry *rle;
1908 int printed, retval;
1912 /* Yes, this is kinda cheating */
1913 SLIST_FOREACH(rle, rl, link) {
1914 if (rle->type == type) {
1916 retval += kprintf(" %s ", name);
1918 retval += kprintf(",");
1920 retval += kprintf(format, rle->start);
1921 if (rle->count > 1) {
1922 retval += kprintf("-");
1923 retval += kprintf(format, rle->start +
1932 * Generic driver/device identify functions. These will install a device
1933 * rendezvous point under the parent using the same name as the driver
1934 * name, which will at a later time be probed and attached.
1936 * These functions are used when the parent does not 'scan' its bus for
1937 * matching devices, or for the particular devices using these functions,
1938 * or when the device is a pseudo or synthesized device (such as can be
1939 * found under firewire and ppbus).
1942 bus_generic_identify(driver_t *driver, device_t parent)
1944 if (parent->state == DS_ATTACHED)
1946 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
1951 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
1953 if (parent->state == DS_ATTACHED)
1955 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
1960 * Call DEVICE_IDENTIFY for each driver.
1963 bus_generic_probe(device_t dev)
1965 devclass_t dc = dev->devclass;
1968 TAILQ_FOREACH(dl, &dc->drivers, link) {
1969 DEVICE_IDENTIFY(dl->driver, dev);
1976 * This is an aweful hack due to the isa bus and autoconf code not
1977 * probing the ISA devices until after everything else has configured.
1978 * The ISA bus did a dummy attach long ago so we have to set it back
1979 * to an earlier state so the probe thinks its the initial probe and
1982 * XXX remove by properly defering the ISA bus scan.
1985 bus_generic_probe_hack(device_t dev)
1987 if (dev->state == DS_ATTACHED) {
1988 dev->state = DS_ALIVE;
1989 bus_generic_probe(dev);
1990 dev->state = DS_ATTACHED;
1996 bus_generic_attach(device_t dev)
2000 TAILQ_FOREACH(child, &dev->children, link) {
2001 device_probe_and_attach(child);
2008 bus_generic_detach(device_t dev)
2013 if (dev->state != DS_ATTACHED)
2016 TAILQ_FOREACH(child, &dev->children, link)
2017 if ((error = device_detach(child)) != 0)
2024 bus_generic_shutdown(device_t dev)
2028 TAILQ_FOREACH(child, &dev->children, link)
2029 device_shutdown(child);
2035 bus_generic_suspend(device_t dev)
2038 device_t child, child2;
2040 TAILQ_FOREACH(child, &dev->children, link) {
2041 error = DEVICE_SUSPEND(child);
2043 for (child2 = TAILQ_FIRST(&dev->children);
2044 child2 && child2 != child;
2045 child2 = TAILQ_NEXT(child2, link))
2046 DEVICE_RESUME(child2);
2054 bus_generic_resume(device_t dev)
2058 TAILQ_FOREACH(child, &dev->children, link)
2059 DEVICE_RESUME(child);
2060 /* if resume fails, there's nothing we can usefully do... */
2066 bus_print_child_header(device_t dev, device_t child)
2070 if (device_get_desc(child))
2071 retval += device_printf(child, "<%s>", device_get_desc(child));
2073 retval += kprintf("%s", device_get_nameunit(child));
2075 if (child->state != DS_ATTACHED)
2076 kprintf(" [tentative]");
2078 kprintf(" [attached!]");
2084 bus_print_child_footer(device_t dev, device_t child)
2086 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2090 bus_generic_add_child(device_t dev, device_t child, int order,
2091 const char *name, int unit)
2094 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2096 dev = device_add_child_ordered(child, order, name, unit);
2102 bus_generic_print_child(device_t dev, device_t child)
2106 retval += bus_print_child_header(dev, child);
2107 retval += bus_print_child_footer(dev, child);
2113 bus_generic_read_ivar(device_t dev, device_t child, int index,
2119 error = BUS_READ_IVAR(dev->parent, child, index, result);
2126 bus_generic_write_ivar(device_t dev, device_t child, int index,
2132 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2139 * Resource list are used for iterations, do not recurse.
2141 struct resource_list *
2142 bus_generic_get_resource_list(device_t dev, device_t child)
2148 bus_generic_driver_added(device_t dev, driver_t *driver)
2152 DEVICE_IDENTIFY(driver, dev);
2153 TAILQ_FOREACH(child, &dev->children, link) {
2154 if (child->state == DS_NOTPRESENT)
2155 device_probe_and_attach(child);
2160 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2161 int flags, driver_intr_t *intr, void *arg,
2162 void **cookiep, lwkt_serialize_t serializer)
2164 /* Propagate up the bus hierarchy until someone handles it. */
2166 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2167 intr, arg, cookiep, serializer));
2173 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2176 /* Propagate up the bus hierarchy until someone handles it. */
2178 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2184 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2187 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2193 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2196 BUS_ENABLE_INTR(dev->parent, child, cookie);
2200 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2201 enum intr_polarity pol)
2203 /* Propagate up the bus hierarchy until someone handles it. */
2205 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2211 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2212 u_long start, u_long end, u_long count, u_int flags)
2214 /* Propagate up the bus hierarchy until someone handles it. */
2216 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2217 start, end, count, flags));
2223 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2226 /* Propagate up the bus hierarchy until someone handles it. */
2228 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2234 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2237 /* Propagate up the bus hierarchy until someone handles it. */
2239 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2245 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2246 int rid, struct resource *r)
2248 /* Propagate up the bus hierarchy until someone handles it. */
2250 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2257 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2258 u_long *startp, u_long *countp)
2264 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2271 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2272 u_long start, u_long count)
2278 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2285 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2288 BUS_DELETE_RESOURCE(dev, child, type, rid);
2292 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2293 u_long *startp, u_long *countp)
2295 struct resource_list *rl = NULL;
2296 struct resource_list_entry *rle = NULL;
2298 rl = BUS_GET_RESOURCE_LIST(dev, child);
2302 rle = resource_list_find(rl, type, rid);
2307 *startp = rle->start;
2309 *countp = rle->count;
2315 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2316 u_long start, u_long count)
2318 struct resource_list *rl = NULL;
2320 rl = BUS_GET_RESOURCE_LIST(dev, child);
2324 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2330 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2332 struct resource_list *rl = NULL;
2334 rl = BUS_GET_RESOURCE_LIST(dev, child);
2338 resource_list_delete(rl, type, rid);
2342 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2343 int rid, struct resource *r)
2345 struct resource_list *rl = NULL;
2347 rl = BUS_GET_RESOURCE_LIST(dev, child);
2351 return(resource_list_release(rl, dev, child, type, rid, r));
2355 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2356 int *rid, u_long start, u_long end, u_long count, u_int flags)
2358 struct resource_list *rl = NULL;
2360 rl = BUS_GET_RESOURCE_LIST(dev, child);
2364 return(resource_list_alloc(rl, dev, child, type, rid,
2365 start, end, count, flags));
2369 bus_generic_child_present(device_t bus, device_t child)
2371 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2376 * Some convenience functions to make it easier for drivers to use the
2377 * resource-management functions. All these really do is hide the
2378 * indirection through the parent's method table, making for slightly
2379 * less-wordy code. In the future, it might make sense for this code
2380 * to maintain some sort of a list of resources allocated by each device.
2383 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2384 struct resource **res)
2388 for (i = 0; rs[i].type != -1; i++)
2390 for (i = 0; rs[i].type != -1; i++) {
2391 res[i] = bus_alloc_resource_any(dev,
2392 rs[i].type, &rs[i].rid, rs[i].flags);
2393 if (res[i] == NULL) {
2394 bus_release_resources(dev, rs, res);
2402 bus_release_resources(device_t dev, const struct resource_spec *rs,
2403 struct resource **res)
2407 for (i = 0; rs[i].type != -1; i++)
2408 if (res[i] != NULL) {
2409 bus_release_resource(
2410 dev, rs[i].type, rs[i].rid, res[i]);
2416 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2417 u_long count, u_int flags)
2419 if (dev->parent == 0)
2421 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2426 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2428 if (dev->parent == 0)
2430 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2434 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2436 if (dev->parent == 0)
2438 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2442 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2444 if (dev->parent == 0)
2446 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2450 bus_setup_intr(device_t dev, struct resource *r, int flags,
2451 driver_intr_t handler, void *arg,
2452 void **cookiep, lwkt_serialize_t serializer)
2454 if (dev->parent == 0)
2456 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2457 cookiep, serializer));
2461 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2463 if (dev->parent == 0)
2465 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2469 bus_enable_intr(device_t dev, void *cookie)
2472 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2476 bus_disable_intr(device_t dev, void *cookie)
2479 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2485 bus_set_resource(device_t dev, int type, int rid,
2486 u_long start, u_long count)
2488 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2493 bus_get_resource(device_t dev, int type, int rid,
2494 u_long *startp, u_long *countp)
2496 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2501 bus_get_resource_start(device_t dev, int type, int rid)
2503 u_long start, count;
2506 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2514 bus_get_resource_count(device_t dev, int type, int rid)
2516 u_long start, count;
2519 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2527 bus_delete_resource(device_t dev, int type, int rid)
2529 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2533 bus_child_present(device_t child)
2535 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2539 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2543 parent = device_get_parent(child);
2544 if (parent == NULL) {
2548 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2552 bus_child_location_str(device_t child, char *buf, size_t buflen)
2556 parent = device_get_parent(child);
2557 if (parent == NULL) {
2561 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2565 root_print_child(device_t dev, device_t child)
2571 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2572 void **cookiep, lwkt_serialize_t serializer)
2575 * If an interrupt mapping gets to here something bad has happened.
2577 panic("root_setup_intr");
2581 * If we get here, assume that the device is permanant and really is
2582 * present in the system. Removable bus drivers are expected to intercept
2583 * this call long before it gets here. We return -1 so that drivers that
2584 * really care can check vs -1 or some ERRNO returned higher in the food
2588 root_child_present(device_t dev, device_t child)
2594 * XXX NOTE! other defaults may be set in bus_if.m
2596 static kobj_method_t root_methods[] = {
2597 /* Device interface */
2598 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2599 KOBJMETHOD(device_suspend, bus_generic_suspend),
2600 KOBJMETHOD(device_resume, bus_generic_resume),
2603 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2604 KOBJMETHOD(bus_print_child, root_print_child),
2605 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2606 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2607 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2608 KOBJMETHOD(bus_child_present, root_child_present),
2613 static driver_t root_driver = {
2620 devclass_t root_devclass;
2623 root_bus_module_handler(module_t mod, int what, void* arg)
2627 root_bus = make_device(NULL, "root", 0);
2628 root_bus->desc = "System root bus";
2629 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2630 root_bus->driver = &root_driver;
2631 root_bus->state = DS_ALIVE;
2632 root_devclass = devclass_find_internal("root", NULL, FALSE);
2636 device_shutdown(root_bus);
2643 static moduledata_t root_bus_mod = {
2645 root_bus_module_handler,
2648 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2651 root_bus_configure(void)
2659 * handle device_identify based device attachments to the root_bus
2660 * (typically nexus).
2662 bus_generic_probe(root_bus);
2665 * Probe and attach the devices under root_bus.
2667 TAILQ_FOREACH(dev, &root_bus->children, link) {
2668 device_probe_and_attach(dev);
2672 * Wait for all asynchronous attaches to complete. If we don't
2673 * our legacy ISA bus scan could steal device unit numbers or
2677 if (numasyncthreads)
2678 kprintf("Waiting for async drivers to attach\n");
2679 while (numasyncthreads > 0) {
2680 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
2682 if (warncount == 0) {
2683 kprintf("Warning: Still waiting for %d "
2684 "drivers to attach\n", numasyncthreads);
2685 } else if (warncount == -30) {
2686 kprintf("Giving up on %d drivers\n", numasyncthreads);
2690 root_bus->state = DS_ATTACHED;
2694 driver_module_handler(module_t mod, int what, void *arg)
2697 struct driver_module_data *dmd;
2698 devclass_t bus_devclass;
2699 kobj_class_t driver;
2700 const char *parentname;
2702 dmd = (struct driver_module_data *)arg;
2703 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
2708 if (dmd->dmd_chainevh)
2709 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2711 driver = dmd->dmd_driver;
2712 PDEBUG(("Loading module: driver %s on bus %s",
2713 DRIVERNAME(driver), dmd->dmd_busname));
2716 * If the driver has any base classes, make the
2717 * devclass inherit from the devclass of the driver's
2718 * first base class. This will allow the system to
2719 * search for drivers in both devclasses for children
2720 * of a device using this driver.
2722 if (driver->baseclasses)
2723 parentname = driver->baseclasses[0]->name;
2726 *dmd->dmd_devclass = devclass_find_internal(driver->name,
2729 error = devclass_add_driver(bus_devclass, driver);
2735 PDEBUG(("Unloading module: driver %s from bus %s",
2736 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
2737 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
2739 if (!error && dmd->dmd_chainevh)
2740 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2750 * The _short versions avoid iteration by not calling anything that prints
2751 * more than oneliners. I love oneliners.
2755 print_device_short(device_t dev, int indent)
2760 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2761 dev->unit, dev->desc,
2762 (dev->parent? "":"no "),
2763 (TAILQ_EMPTY(&dev->children)? "no ":""),
2764 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2765 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2766 (dev->flags&DF_WILDCARD? "wildcard,":""),
2767 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2768 (dev->ivars? "":"no "),
2769 (dev->softc? "":"no "),
2774 print_device(device_t dev, int indent)
2779 print_device_short(dev, indent);
2781 indentprintf(("Parent:\n"));
2782 print_device_short(dev->parent, indent+1);
2783 indentprintf(("Driver:\n"));
2784 print_driver_short(dev->driver, indent+1);
2785 indentprintf(("Devclass:\n"));
2786 print_devclass_short(dev->devclass, indent+1);
2790 * Print the device and all its children (indented).
2793 print_device_tree_short(device_t dev, int indent)
2800 print_device_short(dev, indent);
2802 TAILQ_FOREACH(child, &dev->children, link)
2803 print_device_tree_short(child, indent+1);
2807 * Print the device and all its children (indented).
2810 print_device_tree(device_t dev, int indent)
2817 print_device(dev, indent);
2819 TAILQ_FOREACH(child, &dev->children, link)
2820 print_device_tree(child, indent+1);
2824 print_driver_short(driver_t *driver, int indent)
2829 indentprintf(("driver %s: softc size = %d\n",
2830 driver->name, driver->size));
2834 print_driver(driver_t *driver, int indent)
2839 print_driver_short(driver, indent);
2844 print_driver_list(driver_list_t drivers, int indent)
2846 driverlink_t driver;
2848 TAILQ_FOREACH(driver, &drivers, link)
2849 print_driver(driver->driver, indent);
2853 print_devclass_short(devclass_t dc, int indent)
2858 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2862 print_devclass(devclass_t dc, int indent)
2869 print_devclass_short(dc, indent);
2870 indentprintf(("Drivers:\n"));
2871 print_driver_list(dc->drivers, indent+1);
2873 indentprintf(("Devices:\n"));
2874 for (i = 0; i < dc->maxunit; i++)
2876 print_device(dc->devices[i], indent+1);
2880 print_devclass_list_short(void)
2884 kprintf("Short listing of devclasses, drivers & devices:\n");
2885 TAILQ_FOREACH(dc, &devclasses, link) {
2886 print_devclass_short(dc, 0);
2891 print_devclass_list(void)
2895 kprintf("Full listing of devclasses, drivers & devices:\n");
2896 TAILQ_FOREACH(dc, &devclasses, link) {
2897 print_devclass(dc, 0);
2904 * Check to see if a device is disabled via a disabled hint.
2907 resource_disabled(const char *name, int unit)
2911 error = resource_int_value(name, unit, "disabled", &value);