2 * Copyright (c) 1997,1998,2003 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/filio.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/queue.h>
45 #include <machine/bus.h>
47 #include <sys/selinfo.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
53 #include <sys/interrupt.h>
55 #include <machine/stdarg.h>
59 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
60 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
63 * Used to attach drivers to devclasses.
65 typedef struct driverlink *driverlink_t;
68 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
72 * Forward declarations
74 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
75 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
76 typedef TAILQ_HEAD(device_list, device) device_list_t;
79 TAILQ_ENTRY(devclass) link;
80 devclass_t parent; /* parent in devclass hierarchy */
81 driver_list_t drivers; /* bus devclasses store drivers for bus */
83 device_t *devices; /* array of devices indexed by unit */
84 int maxunit; /* size of devices array */
86 struct sysctl_ctx_list sysctl_ctx;
87 struct sysctl_oid *sysctl_tree;
91 * @brief Implementation of device.
95 * A device is a kernel object. The first field must be the
96 * current ops table for the object.
103 TAILQ_ENTRY(device) link; /**< list of devices in parent */
104 TAILQ_ENTRY(device) devlink; /**< global device list membership */
105 device_t parent; /**< parent of this device */
106 device_list_t children; /**< list of child devices */
109 * Details of this device.
111 driver_t *driver; /**< current driver */
112 devclass_t devclass; /**< current device class */
113 int unit; /**< current unit number */
114 char* nameunit; /**< name+unit e.g. foodev0 */
115 char* desc; /**< driver specific description */
116 int busy; /**< count of calls to device_busy() */
117 device_state_t state; /**< current device state */
118 u_int32_t devflags; /**< api level flags for device_get_flags() */
119 u_short flags; /**< internal device flags */
120 #define DF_ENABLED 1 /* device should be probed/attached */
121 #define DF_FIXEDCLASS 2 /* devclass specified at create time */
122 #define DF_WILDCARD 4 /* unit was originally wildcard */
123 #define DF_DESCMALLOCED 8 /* description was malloced */
124 #define DF_QUIET 16 /* don't print verbose attach message */
125 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */
126 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */
127 #define DF_REBID 128 /* Can rebid after attach */
128 u_char order; /**< order from device_add_child_ordered() */
130 void *ivars; /**< instance variables */
131 void *softc; /**< current driver's variables */
133 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
134 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
137 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
138 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
142 static int bus_debug = 1;
143 TUNABLE_INT("bus.debug", &bus_debug);
144 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
147 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
148 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
149 #define DRIVERNAME(d) ((d)? d->name : "no driver")
150 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
153 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
154 * prevent syslog from deleting initial spaces
156 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
158 static void print_device_short(device_t dev, int indent);
159 static void print_device(device_t dev, int indent);
160 void print_device_tree_short(device_t dev, int indent);
161 void print_device_tree(device_t dev, int indent);
162 static void print_driver_short(driver_t *driver, int indent);
163 static void print_driver(driver_t *driver, int indent);
164 static void print_driver_list(driver_list_t drivers, int indent);
165 static void print_devclass_short(devclass_t dc, int indent);
166 static void print_devclass(devclass_t dc, int indent);
167 void print_devclass_list_short(void);
168 void print_devclass_list(void);
171 /* Make the compiler ignore the function calls */
172 #define PDEBUG(a) /* nop */
173 #define DEVICENAME(d) /* nop */
174 #define DRIVERNAME(d) /* nop */
175 #define DEVCLANAME(d) /* nop */
177 #define print_device_short(d,i) /* nop */
178 #define print_device(d,i) /* nop */
179 #define print_device_tree_short(d,i) /* nop */
180 #define print_device_tree(d,i) /* nop */
181 #define print_driver_short(d,i) /* nop */
182 #define print_driver(d,i) /* nop */
183 #define print_driver_list(d,i) /* nop */
184 #define print_devclass_short(d,i) /* nop */
185 #define print_devclass(d,i) /* nop */
186 #define print_devclass_list_short() /* nop */
187 #define print_devclass_list() /* nop */
195 DEVCLASS_SYSCTL_PARENT,
199 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
201 devclass_t dc = (devclass_t)arg1;
205 case DEVCLASS_SYSCTL_PARENT:
206 value = dc->parent ? dc->parent->name : "";
211 return (SYSCTL_OUT(req, value, strlen(value)));
215 devclass_sysctl_init(devclass_t dc)
218 if (dc->sysctl_tree != NULL)
220 sysctl_ctx_init(&dc->sysctl_ctx);
221 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
222 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
224 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
225 OID_AUTO, "%parent", CTLFLAG_RD,
226 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
232 DEVICE_SYSCTL_DRIVER,
233 DEVICE_SYSCTL_LOCATION,
234 DEVICE_SYSCTL_PNPINFO,
235 DEVICE_SYSCTL_PARENT,
239 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
241 device_t dev = (device_t)arg1;
248 case DEVICE_SYSCTL_DESC:
249 value = dev->desc ? dev->desc : "";
251 case DEVICE_SYSCTL_DRIVER:
252 value = dev->driver ? dev->driver->name : "";
254 case DEVICE_SYSCTL_LOCATION:
255 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
256 bus_child_location_str(dev, buf, 1024);
258 case DEVICE_SYSCTL_PNPINFO:
259 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
260 bus_child_pnpinfo_str(dev, buf, 1024);
262 case DEVICE_SYSCTL_PARENT:
263 value = dev->parent ? dev->parent->nameunit : "";
268 error = SYSCTL_OUT(req, value, strlen(value));
275 device_sysctl_init(device_t dev)
277 devclass_t dc = dev->devclass;
279 if (dev->sysctl_tree != NULL)
281 devclass_sysctl_init(dc);
282 sysctl_ctx_init(&dev->sysctl_ctx);
283 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
284 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
285 dev->nameunit + strlen(dc->name),
287 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
288 OID_AUTO, "%desc", CTLFLAG_RD,
289 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
290 "device description");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%driver", CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
294 "device driver name");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%location", CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
298 "device location relative to parent");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
302 "device identification");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%parent", CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
310 device_sysctl_update(device_t dev)
312 devclass_t dc = dev->devclass;
314 if (dev->sysctl_tree == NULL)
316 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
320 device_sysctl_fini(device_t dev)
322 if (dev->sysctl_tree == NULL)
324 sysctl_ctx_free(&dev->sysctl_ctx);
325 dev->sysctl_tree = NULL;
329 * /dev/devctl implementation
333 * This design allows only one reader for /dev/devctl. This is not desirable
334 * in the long run, but will get a lot of hair out of this implementation.
335 * Maybe we should make this device a clonable device.
337 * Also note: we specifically do not attach a device to the device_t tree
338 * to avoid potential chicken and egg problems. One could argue that all
339 * of this belongs to the root node. One could also further argue that the
340 * sysctl interface that we have not might more properly be an ioctl
341 * interface, but at this stage of the game, I'm not inclined to rock that
344 * I'm also not sure that the SIGIO support is done correctly or not, as
345 * I copied it from a driver that had SIGIO support that likely hasn't been
346 * tested since 3.4 or 2.2.8!
349 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
350 static int devctl_disable = 0;
351 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
352 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
353 sysctl_devctl_disable, "I", "devctl disable");
355 static d_open_t devopen;
356 static d_close_t devclose;
357 static d_read_t devread;
358 static d_ioctl_t devioctl;
359 static d_poll_t devpoll;
361 static struct cdevsw dev_cdevsw = {
362 .d_version = D_VERSION,
363 .d_flags = D_NEEDGIANT,
372 struct dev_event_info
375 TAILQ_ENTRY(dev_event_info) dei_link;
378 TAILQ_HEAD(devq, dev_event_info);
380 static struct dev_softc
388 struct proc *async_proc;
391 static struct cdev *devctl_dev;
396 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
398 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
399 cv_init(&devsoftc.cv, "dev cv");
400 TAILQ_INIT(&devsoftc.devq);
404 devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
410 devsoftc.nonblock = 0;
411 devsoftc.async_proc = NULL;
416 devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
419 mtx_lock(&devsoftc.mtx);
420 cv_broadcast(&devsoftc.cv);
421 mtx_unlock(&devsoftc.mtx);
427 * The read channel for this device is used to report changes to
428 * userland in realtime. We are required to free the data as well as
429 * the n1 object because we allocate them separately. Also note that
430 * we return one record at a time. If you try to read this device a
431 * character at a time, you will lose the rest of the data. Listening
432 * programs are expected to cope.
435 devread(struct cdev *dev, struct uio *uio, int ioflag)
437 struct dev_event_info *n1;
440 mtx_lock(&devsoftc.mtx);
441 while (TAILQ_EMPTY(&devsoftc.devq)) {
442 if (devsoftc.nonblock) {
443 mtx_unlock(&devsoftc.mtx);
446 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
449 * Need to translate ERESTART to EINTR here? -- jake
451 mtx_unlock(&devsoftc.mtx);
455 n1 = TAILQ_FIRST(&devsoftc.devq);
456 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
457 mtx_unlock(&devsoftc.mtx);
458 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
459 free(n1->dei_data, M_BUS);
465 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
471 devsoftc.nonblock = 1;
473 devsoftc.nonblock = 0;
477 devsoftc.async_proc = td->td_proc;
479 devsoftc.async_proc = NULL;
482 /* (un)Support for other fcntl() calls. */
495 devpoll(struct cdev *dev, int events, d_thread_t *td)
499 mtx_lock(&devsoftc.mtx);
500 if (events & (POLLIN | POLLRDNORM)) {
501 if (!TAILQ_EMPTY(&devsoftc.devq))
502 revents = events & (POLLIN | POLLRDNORM);
504 selrecord(td, &devsoftc.sel);
506 mtx_unlock(&devsoftc.mtx);
512 * @brief Return whether the userland process is running
515 devctl_process_running(void)
517 return (devsoftc.inuse == 1);
521 * @brief Queue data to be read from the devctl device
523 * Generic interface to queue data to the devctl device. It is
524 * assumed that @p data is properly formatted. It is further assumed
525 * that @p data is allocated using the M_BUS malloc type.
528 devctl_queue_data(char *data)
530 struct dev_event_info *n1 = NULL;
533 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
537 mtx_lock(&devsoftc.mtx);
538 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
539 cv_broadcast(&devsoftc.cv);
540 mtx_unlock(&devsoftc.mtx);
541 selwakeup(&devsoftc.sel);
542 p = devsoftc.async_proc;
551 * @brief Send a 'notification' to userland, using standard ways
554 devctl_notify(const char *system, const char *subsystem, const char *type,
561 return; /* BOGUS! Must specify system. */
562 if (subsystem == NULL)
563 return; /* BOGUS! Must specify subsystem. */
565 return; /* BOGUS! Must specify type. */
566 len += strlen(" system=") + strlen(system);
567 len += strlen(" subsystem=") + strlen(subsystem);
568 len += strlen(" type=") + strlen(type);
569 /* add in the data message plus newline. */
572 len += 3; /* '!', '\n', and NUL */
573 msg = malloc(len, M_BUS, M_NOWAIT);
575 return; /* Drop it on the floor */
577 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
578 system, subsystem, type, data);
580 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
581 system, subsystem, type);
582 devctl_queue_data(msg);
586 * Common routine that tries to make sending messages as easy as possible.
587 * We allocate memory for the data, copy strings into that, but do not
588 * free it unless there's an error. The dequeue part of the driver should
589 * free the data. We don't send data when the device is disabled. We do
590 * send data, even when we have no listeners, because we wish to avoid
591 * races relating to startup and restart of listening applications.
593 * devaddq is designed to string together the type of event, with the
594 * object of that event, plus the plug and play info and location info
595 * for that event. This is likely most useful for devices, but less
596 * useful for other consumers of this interface. Those should use
597 * the devctl_queue_data() interface instead.
600 devaddq(const char *type, const char *what, device_t dev)
609 data = malloc(1024, M_BUS, M_NOWAIT);
613 /* get the bus specific location of this device */
614 loc = malloc(1024, M_BUS, M_NOWAIT);
618 bus_child_location_str(dev, loc, 1024);
620 /* Get the bus specific pnp info of this device */
621 pnp = malloc(1024, M_BUS, M_NOWAIT);
625 bus_child_pnpinfo_str(dev, pnp, 1024);
627 /* Get the parent of this device, or / if high enough in the tree. */
628 if (device_get_parent(dev) == NULL)
629 parstr = "."; /* Or '/' ? */
631 parstr = device_get_nameunit(device_get_parent(dev));
632 /* String it all together. */
633 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
637 devctl_queue_data(data);
647 * A device was added to the tree. We are called just after it successfully
648 * attaches (that is, probe and attach success for this device). No call
649 * is made if a device is merely parented into the tree. See devnomatch
650 * if probe fails. If attach fails, no notification is sent (but maybe
651 * we should have a different message for this).
654 devadded(device_t dev)
659 pnp = malloc(1024, M_BUS, M_NOWAIT);
662 tmp = malloc(1024, M_BUS, M_NOWAIT);
666 bus_child_pnpinfo_str(dev, pnp, 1024);
667 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
668 devaddq("+", tmp, dev);
678 * A device was removed from the tree. We are called just before this
682 devremoved(device_t dev)
687 pnp = malloc(1024, M_BUS, M_NOWAIT);
690 tmp = malloc(1024, M_BUS, M_NOWAIT);
694 bus_child_pnpinfo_str(dev, pnp, 1024);
695 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
696 devaddq("-", tmp, dev);
706 * Called when there's no match for this device. This is only called
707 * the first time that no match happens, so we don't keep getitng this
708 * message. Should that prove to be undesirable, we can change it.
709 * This is called when all drivers that can attach to a given bus
710 * decline to accept this device. Other errrors may not be detected.
713 devnomatch(device_t dev)
715 devaddq("?", "", dev);
719 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
721 struct dev_event_info *n1;
724 dis = devctl_disable;
725 error = sysctl_handle_int(oidp, &dis, 0, req);
726 if (error || !req->newptr)
728 mtx_lock(&devsoftc.mtx);
729 devctl_disable = dis;
731 while (!TAILQ_EMPTY(&devsoftc.devq)) {
732 n1 = TAILQ_FIRST(&devsoftc.devq);
733 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
734 free(n1->dei_data, M_BUS);
738 mtx_unlock(&devsoftc.mtx);
742 /* End of /dev/devctl code */
744 TAILQ_HEAD(,device) bus_data_devices;
745 static int bus_data_generation = 1;
747 kobj_method_t null_methods[] = {
751 DEFINE_CLASS(null, null_methods, 0);
754 * Devclass implementation
757 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
762 * @brief Find or create a device class
764 * If a device class with the name @p classname exists, return it,
765 * otherwise if @p create is non-zero create and return a new device
768 * If @p parentname is non-NULL, the parent of the devclass is set to
769 * the devclass of that name.
771 * @param classname the devclass name to find or create
772 * @param parentname the parent devclass name or @c NULL
773 * @param create non-zero to create a devclass
776 devclass_find_internal(const char *classname, const char *parentname,
781 PDEBUG(("looking for %s", classname));
785 TAILQ_FOREACH(dc, &devclasses, link) {
786 if (!strcmp(dc->name, classname))
791 PDEBUG(("creating %s", classname));
792 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
793 M_BUS, M_NOWAIT|M_ZERO);
797 dc->name = (char*) (dc + 1);
798 strcpy(dc->name, classname);
799 TAILQ_INIT(&dc->drivers);
800 TAILQ_INSERT_TAIL(&devclasses, dc, link);
802 bus_data_generation_update();
806 * If a parent class is specified, then set that as our parent so
807 * that this devclass will support drivers for the parent class as
808 * well. If the parent class has the same name don't do this though
809 * as it creates a cycle that can trigger an infinite loop in
810 * device_probe_child() if a device exists for which there is no
813 if (parentname && dc && !dc->parent &&
814 strcmp(classname, parentname) != 0) {
815 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
822 * @brief Create a device class
824 * If a device class with the name @p classname exists, return it,
825 * otherwise create and return a new device class.
827 * @param classname the devclass name to find or create
830 devclass_create(const char *classname)
832 return (devclass_find_internal(classname, NULL, TRUE));
836 * @brief Find a device class
838 * If a device class with the name @p classname exists, return it,
839 * otherwise return @c NULL.
841 * @param classname the devclass name to find
844 devclass_find(const char *classname)
846 return (devclass_find_internal(classname, NULL, FALSE));
850 * @brief Add a device driver to a device class
852 * Add a device driver to a devclass. This is normally called
853 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
854 * all devices in the devclass will be called to allow them to attempt
855 * to re-probe any unmatched children.
857 * @param dc the devclass to edit
858 * @param driver the driver to register
861 devclass_add_driver(devclass_t dc, driver_t *driver)
866 PDEBUG(("%s", DRIVERNAME(driver)));
868 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
873 * Compile the driver's methods. Also increase the reference count
874 * so that the class doesn't get freed when the last instance
875 * goes. This means we can safely use static methods and avoids a
876 * double-free in devclass_delete_driver.
878 kobj_class_compile((kobj_class_t) driver);
881 * Make sure the devclass which the driver is implementing exists.
883 devclass_find_internal(driver->name, NULL, TRUE);
886 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
887 driver->refs++; /* XXX: kobj_mtx */
890 * Call BUS_DRIVER_ADDED for any existing busses in this class.
892 for (i = 0; i < dc->maxunit; i++)
894 BUS_DRIVER_ADDED(dc->devices[i], driver);
896 bus_data_generation_update();
901 * @brief Delete a device driver from a device class
903 * Delete a device driver from a devclass. This is normally called
904 * automatically by DRIVER_MODULE().
906 * If the driver is currently attached to any devices,
907 * devclass_delete_driver() will first attempt to detach from each
908 * device. If one of the detach calls fails, the driver will not be
911 * @param dc the devclass to edit
912 * @param driver the driver to unregister
915 devclass_delete_driver(devclass_t busclass, driver_t *driver)
917 devclass_t dc = devclass_find(driver->name);
923 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
929 * Find the link structure in the bus' list of drivers.
931 TAILQ_FOREACH(dl, &busclass->drivers, link) {
932 if (dl->driver == driver)
937 PDEBUG(("%s not found in %s list", driver->name,
943 * Disassociate from any devices. We iterate through all the
944 * devices in the devclass of the driver and detach any which are
945 * using the driver and which have a parent in the devclass which
946 * we are deleting from.
948 * Note that since a driver can be in multiple devclasses, we
949 * should not detach devices which are not children of devices in
950 * the affected devclass.
952 for (i = 0; i < dc->maxunit; i++) {
953 if (dc->devices[i]) {
954 dev = dc->devices[i];
955 if (dev->driver == driver && dev->parent &&
956 dev->parent->devclass == busclass) {
957 if ((error = device_detach(dev)) != 0)
959 device_set_driver(dev, NULL);
964 TAILQ_REMOVE(&busclass->drivers, dl, link);
969 if (driver->refs == 0)
970 kobj_class_free((kobj_class_t) driver);
972 bus_data_generation_update();
977 * @brief Quiesces a set of device drivers from a device class
979 * Quiesce a device driver from a devclass. This is normally called
980 * automatically by DRIVER_MODULE().
982 * If the driver is currently attached to any devices,
983 * devclass_quiesece_driver() will first attempt to quiesce each
986 * @param dc the devclass to edit
987 * @param driver the driver to unregister
990 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
992 devclass_t dc = devclass_find(driver->name);
998 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1004 * Find the link structure in the bus' list of drivers.
1006 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1007 if (dl->driver == driver)
1012 PDEBUG(("%s not found in %s list", driver->name,
1018 * Quiesce all devices. We iterate through all the devices in
1019 * the devclass of the driver and quiesce any which are using
1020 * the driver and which have a parent in the devclass which we
1023 * Note that since a driver can be in multiple devclasses, we
1024 * should not quiesce devices which are not children of
1025 * devices in the affected devclass.
1027 for (i = 0; i < dc->maxunit; i++) {
1028 if (dc->devices[i]) {
1029 dev = dc->devices[i];
1030 if (dev->driver == driver && dev->parent &&
1031 dev->parent->devclass == busclass) {
1032 if ((error = device_quiesce(dev)) != 0)
1045 devclass_find_driver_internal(devclass_t dc, const char *classname)
1049 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1051 TAILQ_FOREACH(dl, &dc->drivers, link) {
1052 if (!strcmp(dl->driver->name, classname))
1056 PDEBUG(("not found"));
1061 * @brief Search a devclass for a driver
1063 * This function searches the devclass's list of drivers and returns
1064 * the first driver whose name is @p classname or @c NULL if there is
1065 * no driver of that name.
1067 * @param dc the devclass to search
1068 * @param classname the driver name to search for
1071 devclass_find_driver(devclass_t dc, const char *classname)
1075 dl = devclass_find_driver_internal(dc, classname);
1077 return (dl->driver);
1082 * @brief Return the name of the devclass
1085 devclass_get_name(devclass_t dc)
1091 * @brief Find a device given a unit number
1093 * @param dc the devclass to search
1094 * @param unit the unit number to search for
1096 * @returns the device with the given unit number or @c
1097 * NULL if there is no such device
1100 devclass_get_device(devclass_t dc, int unit)
1102 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1104 return (dc->devices[unit]);
1108 * @brief Find the softc field of a device given a unit number
1110 * @param dc the devclass to search
1111 * @param unit the unit number to search for
1113 * @returns the softc field of the device with the given
1114 * unit number or @c NULL if there is no such
1118 devclass_get_softc(devclass_t dc, int unit)
1122 dev = devclass_get_device(dc, unit);
1126 return (device_get_softc(dev));
1130 * @brief Get a list of devices in the devclass
1132 * An array containing a list of all the devices in the given devclass
1133 * is allocated and returned in @p *devlistp. The number of devices
1134 * in the array is returned in @p *devcountp. The caller should free
1135 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1137 * @param dc the devclass to examine
1138 * @param devlistp points at location for array pointer return
1140 * @param devcountp points at location for array size return value
1143 * @retval ENOMEM the array allocation failed
1146 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1151 count = devclass_get_count(dc);
1152 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1157 for (i = 0; i < dc->maxunit; i++) {
1158 if (dc->devices[i]) {
1159 list[count] = dc->devices[i];
1171 * @brief Get a list of drivers in the devclass
1173 * An array containing a list of pointers to all the drivers in the
1174 * given devclass is allocated and returned in @p *listp. The number
1175 * of drivers in the array is returned in @p *countp. The caller should
1176 * free the array using @c free(p, M_TEMP).
1178 * @param dc the devclass to examine
1179 * @param listp gives location for array pointer return value
1180 * @param countp gives location for number of array elements
1184 * @retval ENOMEM the array allocation failed
1187 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1194 TAILQ_FOREACH(dl, &dc->drivers, link)
1196 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1201 TAILQ_FOREACH(dl, &dc->drivers, link) {
1202 list[count] = dl->driver;
1212 * @brief Get the number of devices in a devclass
1214 * @param dc the devclass to examine
1217 devclass_get_count(devclass_t dc)
1222 for (i = 0; i < dc->maxunit; i++)
1229 * @brief Get the maximum unit number used in a devclass
1231 * Note that this is one greater than the highest currently-allocated
1234 * @param dc the devclass to examine
1237 devclass_get_maxunit(devclass_t dc)
1239 return (dc->maxunit);
1243 * @brief Find a free unit number in a devclass
1245 * This function searches for the first unused unit number greater
1246 * that or equal to @p unit.
1248 * @param dc the devclass to examine
1249 * @param unit the first unit number to check
1252 devclass_find_free_unit(devclass_t dc, int unit)
1256 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1262 * @brief Set the parent of a devclass
1264 * The parent class is normally initialised automatically by
1267 * @param dc the devclass to edit
1268 * @param pdc the new parent devclass
1271 devclass_set_parent(devclass_t dc, devclass_t pdc)
1277 * @brief Get the parent of a devclass
1279 * @param dc the devclass to examine
1282 devclass_get_parent(devclass_t dc)
1284 return (dc->parent);
1287 struct sysctl_ctx_list *
1288 devclass_get_sysctl_ctx(devclass_t dc)
1290 return (&dc->sysctl_ctx);
1294 devclass_get_sysctl_tree(devclass_t dc)
1296 return (dc->sysctl_tree);
1301 * @brief Allocate a unit number
1303 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1304 * will do). The allocated unit number is returned in @p *unitp.
1306 * @param dc the devclass to allocate from
1307 * @param unitp points at the location for the allocated unit
1311 * @retval EEXIST the requested unit number is already allocated
1312 * @retval ENOMEM memory allocation failure
1315 devclass_alloc_unit(devclass_t dc, int *unitp)
1319 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1321 /* If we were given a wired unit number, check for existing device */
1324 if (unit >= 0 && unit < dc->maxunit &&
1325 dc->devices[unit] != NULL) {
1327 printf("%s: %s%d already exists; skipping it\n",
1328 dc->name, dc->name, *unitp);
1332 /* Unwired device, find the next available slot for it */
1334 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1339 * We've selected a unit beyond the length of the table, so let's
1340 * extend the table to make room for all units up to and including
1343 if (unit >= dc->maxunit) {
1347 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1348 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1351 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1352 bzero(newlist + dc->maxunit,
1353 sizeof(device_t) * (newsize - dc->maxunit));
1355 free(dc->devices, M_BUS);
1356 dc->devices = newlist;
1357 dc->maxunit = newsize;
1359 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1367 * @brief Add a device to a devclass
1369 * A unit number is allocated for the device (using the device's
1370 * preferred unit number if any) and the device is registered in the
1371 * devclass. This allows the device to be looked up by its unit
1372 * number, e.g. by decoding a dev_t minor number.
1374 * @param dc the devclass to add to
1375 * @param dev the device to add
1378 * @retval EEXIST the requested unit number is already allocated
1379 * @retval ENOMEM memory allocation failure
1382 devclass_add_device(devclass_t dc, device_t dev)
1386 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1388 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1391 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1395 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1396 free(dev->nameunit, M_BUS);
1397 dev->nameunit = NULL;
1400 dc->devices[dev->unit] = dev;
1402 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1409 * @brief Delete a device from a devclass
1411 * The device is removed from the devclass's device list and its unit
1414 * @param dc the devclass to delete from
1415 * @param dev the device to delete
1420 devclass_delete_device(devclass_t dc, device_t dev)
1425 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1427 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1428 panic("devclass_delete_device: inconsistent device class");
1429 dc->devices[dev->unit] = NULL;
1430 if (dev->flags & DF_WILDCARD)
1432 dev->devclass = NULL;
1433 free(dev->nameunit, M_BUS);
1434 dev->nameunit = NULL;
1441 * @brief Make a new device and add it as a child of @p parent
1443 * @param parent the parent of the new device
1444 * @param name the devclass name of the new device or @c NULL
1445 * to leave the devclass unspecified
1446 * @parem unit the unit number of the new device of @c -1 to
1447 * leave the unit number unspecified
1449 * @returns the new device
1452 make_device(device_t parent, const char *name, int unit)
1457 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1460 dc = devclass_find_internal(name, NULL, TRUE);
1462 printf("make_device: can't find device class %s\n",
1470 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1474 dev->parent = parent;
1475 TAILQ_INIT(&dev->children);
1476 kobj_init((kobj_t) dev, &null_class);
1478 dev->devclass = NULL;
1480 dev->nameunit = NULL;
1484 dev->flags = DF_ENABLED;
1487 dev->flags |= DF_WILDCARD;
1489 dev->flags |= DF_FIXEDCLASS;
1490 if (devclass_add_device(dc, dev)) {
1491 kobj_delete((kobj_t) dev, M_BUS);
1498 dev->state = DS_NOTPRESENT;
1500 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1501 bus_data_generation_update();
1508 * @brief Print a description of a device.
1511 device_print_child(device_t dev, device_t child)
1515 if (device_is_alive(child))
1516 retval += BUS_PRINT_CHILD(dev, child);
1518 retval += device_printf(child, " not found\n");
1524 * @brief Create a new device
1526 * This creates a new device and adds it as a child of an existing
1527 * parent device. The new device will be added after the last existing
1528 * child with order zero.
1530 * @param dev the device which will be the parent of the
1532 * @param name devclass name for new device or @c NULL if not
1534 * @param unit unit number for new device or @c -1 if not
1537 * @returns the new device
1540 device_add_child(device_t dev, const char *name, int unit)
1542 return (device_add_child_ordered(dev, 0, name, unit));
1546 * @brief Create a new device
1548 * This creates a new device and adds it as a child of an existing
1549 * parent device. The new device will be added after the last existing
1550 * child with the same order.
1552 * @param dev the device which will be the parent of the
1554 * @param order a value which is used to partially sort the
1555 * children of @p dev - devices created using
1556 * lower values of @p order appear first in @p
1557 * dev's list of children
1558 * @param name devclass name for new device or @c NULL if not
1560 * @param unit unit number for new device or @c -1 if not
1563 * @returns the new device
1566 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1571 PDEBUG(("%s at %s with order %d as unit %d",
1572 name, DEVICENAME(dev), order, unit));
1574 child = make_device(dev, name, unit);
1577 child->order = order;
1579 TAILQ_FOREACH(place, &dev->children, link) {
1580 if (place->order > order)
1586 * The device 'place' is the first device whose order is
1587 * greater than the new child.
1589 TAILQ_INSERT_BEFORE(place, child, link);
1592 * The new child's order is greater or equal to the order of
1593 * any existing device. Add the child to the tail of the list.
1595 TAILQ_INSERT_TAIL(&dev->children, child, link);
1598 bus_data_generation_update();
1603 * @brief Delete a device
1605 * This function deletes a device along with all of its children. If
1606 * the device currently has a driver attached to it, the device is
1607 * detached first using device_detach().
1609 * @param dev the parent device
1610 * @param child the device to delete
1613 * @retval non-zero a unit error code describing the error
1616 device_delete_child(device_t dev, device_t child)
1619 device_t grandchild;
1621 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1623 /* remove children first */
1624 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1625 error = device_delete_child(child, grandchild);
1630 if ((error = device_detach(child)) != 0)
1632 if (child->devclass)
1633 devclass_delete_device(child->devclass, child);
1634 TAILQ_REMOVE(&dev->children, child, link);
1635 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1636 kobj_delete((kobj_t) child, M_BUS);
1638 bus_data_generation_update();
1643 * @brief Find a device given a unit number
1645 * This is similar to devclass_get_devices() but only searches for
1646 * devices which have @p dev as a parent.
1648 * @param dev the parent device to search
1649 * @param unit the unit number to search for. If the unit is -1,
1650 * return the first child of @p dev which has name
1651 * @p classname (that is, the one with the lowest unit.)
1653 * @returns the device with the given unit number or @c
1654 * NULL if there is no such device
1657 device_find_child(device_t dev, const char *classname, int unit)
1662 dc = devclass_find(classname);
1667 child = devclass_get_device(dc, unit);
1668 if (child && child->parent == dev)
1671 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1672 child = devclass_get_device(dc, unit);
1673 if (child && child->parent == dev)
1684 first_matching_driver(devclass_t dc, device_t dev)
1687 return (devclass_find_driver_internal(dc, dev->devclass->name));
1688 return (TAILQ_FIRST(&dc->drivers));
1695 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1697 if (dev->devclass) {
1699 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1700 if (!strcmp(dev->devclass->name, dl->driver->name))
1704 return (TAILQ_NEXT(last, link));
1711 device_probe_child(device_t dev, device_t child)
1714 driverlink_t best = NULL;
1716 int result, pri = 0;
1717 int hasclass = (child->devclass != 0);
1723 panic("device_probe_child: parent device has no devclass");
1726 * If the state is already probed, then return. However, don't
1727 * return if we can rebid this object.
1729 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1732 for (; dc; dc = dc->parent) {
1733 for (dl = first_matching_driver(dc, child);
1735 dl = next_matching_driver(dc, child, dl)) {
1736 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1737 device_set_driver(child, dl->driver);
1739 if (device_set_devclass(child, dl->driver->name)) {
1740 PDEBUG(("Unable to set device class"));
1741 device_set_driver(child, NULL);
1746 /* Fetch any flags for the device before probing. */
1747 resource_int_value(dl->driver->name, child->unit,
1748 "flags", &child->devflags);
1750 result = DEVICE_PROBE(child);
1752 /* Reset flags and devclass before the next probe. */
1753 child->devflags = 0;
1755 device_set_devclass(child, NULL);
1758 * If the driver returns SUCCESS, there can be
1759 * no higher match for this device.
1768 * The driver returned an error so it
1769 * certainly doesn't match.
1772 device_set_driver(child, NULL);
1777 * A priority lower than SUCCESS, remember the
1778 * best matching driver. Initialise the value
1779 * of pri for the first match.
1781 if (best == NULL || result > pri) {
1788 * If we have an unambiguous match in this devclass,
1789 * don't look in the parent.
1791 if (best && pri == 0)
1796 * If we found a driver, change state and initialise the devclass.
1798 /* XXX What happens if we rebid and got no best? */
1801 * If this device was atached, and we were asked to
1802 * rescan, and it is a different driver, then we have
1803 * to detach the old driver and reattach this new one.
1804 * Note, we don't have to check for DF_REBID here
1805 * because if the state is > DS_ALIVE, we know it must
1808 * This assumes that all DF_REBID drivers can have
1809 * their probe routine called at any time and that
1810 * they are idempotent as well as completely benign in
1811 * normal operations.
1813 * We also have to make sure that the detach
1814 * succeeded, otherwise we fail the operation (or
1815 * maybe it should just fail silently? I'm torn).
1817 if (child->state > DS_ALIVE && best->driver != child->driver)
1818 if ((result = device_detach(dev)) != 0)
1821 /* Set the winning driver, devclass, and flags. */
1822 if (!child->devclass) {
1823 result = device_set_devclass(child, best->driver->name);
1827 device_set_driver(child, best->driver);
1828 resource_int_value(best->driver->name, child->unit,
1829 "flags", &child->devflags);
1833 * A bit bogus. Call the probe method again to make
1834 * sure that we have the right description.
1836 DEVICE_PROBE(child);
1838 child->flags |= DF_REBID;
1841 child->flags &= ~DF_REBID;
1842 child->state = DS_ALIVE;
1844 bus_data_generation_update();
1852 * @brief Return the parent of a device
1855 device_get_parent(device_t dev)
1857 return (dev->parent);
1861 * @brief Get a list of children of a device
1863 * An array containing a list of all the children of the given device
1864 * is allocated and returned in @p *devlistp. The number of devices
1865 * in the array is returned in @p *devcountp. The caller should free
1866 * the array using @c free(p, M_TEMP).
1868 * @param dev the device to examine
1869 * @param devlistp points at location for array pointer return
1871 * @param devcountp points at location for array size return value
1874 * @retval ENOMEM the array allocation failed
1877 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1884 TAILQ_FOREACH(child, &dev->children, link) {
1888 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1893 TAILQ_FOREACH(child, &dev->children, link) {
1894 list[count] = child;
1905 * @brief Return the current driver for the device or @c NULL if there
1906 * is no driver currently attached
1909 device_get_driver(device_t dev)
1911 return (dev->driver);
1915 * @brief Return the current devclass for the device or @c NULL if
1919 device_get_devclass(device_t dev)
1921 return (dev->devclass);
1925 * @brief Return the name of the device's devclass or @c NULL if there
1929 device_get_name(device_t dev)
1931 if (dev != NULL && dev->devclass)
1932 return (devclass_get_name(dev->devclass));
1937 * @brief Return a string containing the device's devclass name
1938 * followed by an ascii representation of the device's unit number
1942 device_get_nameunit(device_t dev)
1944 return (dev->nameunit);
1948 * @brief Return the device's unit number.
1951 device_get_unit(device_t dev)
1957 * @brief Return the device's description string
1960 device_get_desc(device_t dev)
1966 * @brief Return the device's flags
1969 device_get_flags(device_t dev)
1971 return (dev->devflags);
1974 struct sysctl_ctx_list *
1975 device_get_sysctl_ctx(device_t dev)
1977 return (&dev->sysctl_ctx);
1981 device_get_sysctl_tree(device_t dev)
1983 return (dev->sysctl_tree);
1987 * @brief Print the name of the device followed by a colon and a space
1989 * @returns the number of characters printed
1992 device_print_prettyname(device_t dev)
1994 const char *name = device_get_name(dev);
1997 return (printf("unknown: "));
1998 return (printf("%s%d: ", name, device_get_unit(dev)));
2002 * @brief Print the name of the device followed by a colon, a space
2003 * and the result of calling vprintf() with the value of @p fmt and
2004 * the following arguments.
2006 * @returns the number of characters printed
2009 device_printf(device_t dev, const char * fmt, ...)
2014 retval = device_print_prettyname(dev);
2016 retval += vprintf(fmt, ap);
2025 device_set_desc_internal(device_t dev, const char* desc, int copy)
2027 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2028 free(dev->desc, M_BUS);
2029 dev->flags &= ~DF_DESCMALLOCED;
2034 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2036 strcpy(dev->desc, desc);
2037 dev->flags |= DF_DESCMALLOCED;
2040 /* Avoid a -Wcast-qual warning */
2041 dev->desc = (char *)(uintptr_t) desc;
2044 bus_data_generation_update();
2048 * @brief Set the device's description
2050 * The value of @c desc should be a string constant that will not
2051 * change (at least until the description is changed in a subsequent
2052 * call to device_set_desc() or device_set_desc_copy()).
2055 device_set_desc(device_t dev, const char* desc)
2057 device_set_desc_internal(dev, desc, FALSE);
2061 * @brief Set the device's description
2063 * The string pointed to by @c desc is copied. Use this function if
2064 * the device description is generated, (e.g. with sprintf()).
2067 device_set_desc_copy(device_t dev, const char* desc)
2069 device_set_desc_internal(dev, desc, TRUE);
2073 * @brief Set the device's flags
2076 device_set_flags(device_t dev, u_int32_t flags)
2078 dev->devflags = flags;
2082 * @brief Return the device's softc field
2084 * The softc is allocated and zeroed when a driver is attached, based
2085 * on the size field of the driver.
2088 device_get_softc(device_t dev)
2090 return (dev->softc);
2094 * @brief Set the device's softc field
2096 * Most drivers do not need to use this since the softc is allocated
2097 * automatically when the driver is attached.
2100 device_set_softc(device_t dev, void *softc)
2102 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2103 free(dev->softc, M_BUS_SC);
2106 dev->flags |= DF_EXTERNALSOFTC;
2108 dev->flags &= ~DF_EXTERNALSOFTC;
2112 * @brief Get the device's ivars field
2114 * The ivars field is used by the parent device to store per-device
2115 * state (e.g. the physical location of the device or a list of
2119 device_get_ivars(device_t dev)
2122 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2123 return (dev->ivars);
2127 * @brief Set the device's ivars field
2130 device_set_ivars(device_t dev, void * ivars)
2133 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2138 * @brief Return the device's state
2141 device_get_state(device_t dev)
2143 return (dev->state);
2147 * @brief Set the DF_ENABLED flag for the device
2150 device_enable(device_t dev)
2152 dev->flags |= DF_ENABLED;
2156 * @brief Clear the DF_ENABLED flag for the device
2159 device_disable(device_t dev)
2161 dev->flags &= ~DF_ENABLED;
2165 * @brief Increment the busy counter for the device
2168 device_busy(device_t dev)
2170 if (dev->state < DS_ATTACHED)
2171 panic("device_busy: called for unattached device");
2172 if (dev->busy == 0 && dev->parent)
2173 device_busy(dev->parent);
2175 dev->state = DS_BUSY;
2179 * @brief Decrement the busy counter for the device
2182 device_unbusy(device_t dev)
2184 if (dev->state != DS_BUSY)
2185 panic("device_unbusy: called for non-busy device %s",
2186 device_get_nameunit(dev));
2188 if (dev->busy == 0) {
2190 device_unbusy(dev->parent);
2191 dev->state = DS_ATTACHED;
2196 * @brief Set the DF_QUIET flag for the device
2199 device_quiet(device_t dev)
2201 dev->flags |= DF_QUIET;
2205 * @brief Clear the DF_QUIET flag for the device
2208 device_verbose(device_t dev)
2210 dev->flags &= ~DF_QUIET;
2214 * @brief Return non-zero if the DF_QUIET flag is set on the device
2217 device_is_quiet(device_t dev)
2219 return ((dev->flags & DF_QUIET) != 0);
2223 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2226 device_is_enabled(device_t dev)
2228 return ((dev->flags & DF_ENABLED) != 0);
2232 * @brief Return non-zero if the device was successfully probed
2235 device_is_alive(device_t dev)
2237 return (dev->state >= DS_ALIVE);
2241 * @brief Return non-zero if the device currently has a driver
2245 device_is_attached(device_t dev)
2247 return (dev->state >= DS_ATTACHED);
2251 * @brief Set the devclass of a device
2252 * @see devclass_add_device().
2255 device_set_devclass(device_t dev, const char *classname)
2262 devclass_delete_device(dev->devclass, dev);
2266 if (dev->devclass) {
2267 printf("device_set_devclass: device class already set\n");
2271 dc = devclass_find_internal(classname, NULL, TRUE);
2275 error = devclass_add_device(dc, dev);
2277 bus_data_generation_update();
2282 * @brief Set the driver of a device
2285 * @retval EBUSY the device already has a driver attached
2286 * @retval ENOMEM a memory allocation failure occurred
2289 device_set_driver(device_t dev, driver_t *driver)
2291 if (dev->state >= DS_ATTACHED)
2294 if (dev->driver == driver)
2297 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2298 free(dev->softc, M_BUS_SC);
2301 kobj_delete((kobj_t) dev, NULL);
2302 dev->driver = driver;
2304 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2305 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2306 dev->softc = malloc(driver->size, M_BUS_SC,
2309 kobj_delete((kobj_t) dev, NULL);
2310 kobj_init((kobj_t) dev, &null_class);
2316 kobj_init((kobj_t) dev, &null_class);
2319 bus_data_generation_update();
2324 * @brief Probe a device and attach a driver if possible
2326 * This function is the core of the device autoconfiguration
2327 * system. Its purpose is to select a suitable driver for a device and
2328 * then call that driver to initialise the hardware appropriately. The
2329 * driver is selected by calling the DEVICE_PROBE() method of a set of
2330 * candidate drivers and then choosing the driver which returned the
2331 * best value. This driver is then attached to the device using
2334 * The set of suitable drivers is taken from the list of drivers in
2335 * the parent device's devclass. If the device was originally created
2336 * with a specific class name (see device_add_child()), only drivers
2337 * with that name are probed, otherwise all drivers in the devclass
2338 * are probed. If no drivers return successful probe values in the
2339 * parent devclass, the search continues in the parent of that
2340 * devclass (see devclass_get_parent()) if any.
2342 * @param dev the device to initialise
2345 * @retval ENXIO no driver was found
2346 * @retval ENOMEM memory allocation failure
2347 * @retval non-zero some other unix error code
2350 device_probe_and_attach(device_t dev)
2356 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2359 if (!(dev->flags & DF_ENABLED)) {
2360 if (bootverbose && device_get_name(dev) != NULL) {
2361 device_print_prettyname(dev);
2362 printf("not probed (disabled)\n");
2366 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2367 if (!(dev->flags & DF_DONENOMATCH)) {
2368 BUS_PROBE_NOMATCH(dev->parent, dev);
2370 dev->flags |= DF_DONENOMATCH;
2374 error = device_attach(dev);
2380 * @brief Attach a device driver to a device
2382 * This function is a wrapper around the DEVICE_ATTACH() driver
2383 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2384 * device's sysctl tree, optionally prints a description of the device
2385 * and queues a notification event for user-based device management
2388 * Normally this function is only called internally from
2389 * device_probe_and_attach().
2391 * @param dev the device to initialise
2394 * @retval ENXIO no driver was found
2395 * @retval ENOMEM memory allocation failure
2396 * @retval non-zero some other unix error code
2399 device_attach(device_t dev)
2403 device_sysctl_init(dev);
2404 if (!device_is_quiet(dev))
2405 device_print_child(dev->parent, dev);
2406 if ((error = DEVICE_ATTACH(dev)) != 0) {
2407 printf("device_attach: %s%d attach returned %d\n",
2408 dev->driver->name, dev->unit, error);
2409 /* Unset the class; set in device_probe_child */
2410 if (dev->devclass == NULL)
2411 device_set_devclass(dev, NULL);
2412 device_set_driver(dev, NULL);
2413 device_sysctl_fini(dev);
2414 dev->state = DS_NOTPRESENT;
2417 device_sysctl_update(dev);
2418 dev->state = DS_ATTACHED;
2424 * @brief Detach a driver from a device
2426 * This function is a wrapper around the DEVICE_DETACH() driver
2427 * method. If the call to DEVICE_DETACH() succeeds, it calls
2428 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2429 * notification event for user-based device management services and
2430 * cleans up the device's sysctl tree.
2432 * @param dev the device to un-initialise
2435 * @retval ENXIO no driver was found
2436 * @retval ENOMEM memory allocation failure
2437 * @retval non-zero some other unix error code
2440 device_detach(device_t dev)
2446 PDEBUG(("%s", DEVICENAME(dev)));
2447 if (dev->state == DS_BUSY)
2449 if (dev->state != DS_ATTACHED)
2452 if ((error = DEVICE_DETACH(dev)) != 0)
2455 if (!device_is_quiet(dev))
2456 device_printf(dev, "detached\n");
2458 BUS_CHILD_DETACHED(dev->parent, dev);
2460 if (!(dev->flags & DF_FIXEDCLASS))
2461 devclass_delete_device(dev->devclass, dev);
2463 dev->state = DS_NOTPRESENT;
2464 device_set_driver(dev, NULL);
2465 device_set_desc(dev, NULL);
2466 device_sysctl_fini(dev);
2472 * @brief Tells a driver to quiesce itself.
2474 * This function is a wrapper around the DEVICE_QUIESCE() driver
2475 * method. If the call to DEVICE_QUIESCE() succeeds.
2477 * @param dev the device to quiesce
2480 * @retval ENXIO no driver was found
2481 * @retval ENOMEM memory allocation failure
2482 * @retval non-zero some other unix error code
2485 device_quiesce(device_t dev)
2488 PDEBUG(("%s", DEVICENAME(dev)));
2489 if (dev->state == DS_BUSY)
2491 if (dev->state != DS_ATTACHED)
2494 return (DEVICE_QUIESCE(dev));
2498 * @brief Notify a device of system shutdown
2500 * This function calls the DEVICE_SHUTDOWN() driver method if the
2501 * device currently has an attached driver.
2503 * @returns the value returned by DEVICE_SHUTDOWN()
2506 device_shutdown(device_t dev)
2508 if (dev->state < DS_ATTACHED)
2510 return (DEVICE_SHUTDOWN(dev));
2514 * @brief Set the unit number of a device
2516 * This function can be used to override the unit number used for a
2517 * device (e.g. to wire a device to a pre-configured unit number).
2520 device_set_unit(device_t dev, int unit)
2525 dc = device_get_devclass(dev);
2526 if (unit < dc->maxunit && dc->devices[unit])
2528 err = devclass_delete_device(dc, dev);
2532 err = devclass_add_device(dc, dev);
2536 bus_data_generation_update();
2540 /*======================================*/
2542 * Some useful method implementations to make life easier for bus drivers.
2546 * @brief Initialise a resource list.
2548 * @param rl the resource list to initialise
2551 resource_list_init(struct resource_list *rl)
2557 * @brief Reclaim memory used by a resource list.
2559 * This function frees the memory for all resource entries on the list
2562 * @param rl the resource list to free
2565 resource_list_free(struct resource_list *rl)
2567 struct resource_list_entry *rle;
2569 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2571 panic("resource_list_free: resource entry is busy");
2572 STAILQ_REMOVE_HEAD(rl, link);
2578 * @brief Add a resource entry.
2580 * This function adds a resource entry using the given @p type, @p
2581 * start, @p end and @p count values. A rid value is chosen by
2582 * searching sequentially for the first unused rid starting at zero.
2584 * @param rl the resource list to edit
2585 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2586 * @param start the start address of the resource
2587 * @param end the end address of the resource
2588 * @param count XXX end-start+1
2591 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2592 u_long end, u_long count)
2597 while (resource_list_find(rl, type, rid) != NULL)
2599 resource_list_add(rl, type, rid, start, end, count);
2604 * @brief Add or modify a resource entry.
2606 * If an existing entry exists with the same type and rid, it will be
2607 * modified using the given values of @p start, @p end and @p
2608 * count. If no entry exists, a new one will be created using the
2609 * given values. The resource list entry that matches is then returned.
2611 * @param rl the resource list to edit
2612 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2613 * @param rid the resource identifier
2614 * @param start the start address of the resource
2615 * @param end the end address of the resource
2616 * @param count XXX end-start+1
2618 struct resource_list_entry *
2619 resource_list_add(struct resource_list *rl, int type, int rid,
2620 u_long start, u_long end, u_long count)
2622 struct resource_list_entry *rle;
2624 rle = resource_list_find(rl, type, rid);
2626 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2629 panic("resource_list_add: can't record entry");
2630 STAILQ_INSERT_TAIL(rl, rle, link);
2637 panic("resource_list_add: resource entry is busy");
2646 * @brief Find a resource entry by type and rid.
2648 * @param rl the resource list to search
2649 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2650 * @param rid the resource identifier
2652 * @returns the resource entry pointer or NULL if there is no such
2655 struct resource_list_entry *
2656 resource_list_find(struct resource_list *rl, int type, int rid)
2658 struct resource_list_entry *rle;
2660 STAILQ_FOREACH(rle, rl, link) {
2661 if (rle->type == type && rle->rid == rid)
2668 * @brief Delete a resource entry.
2670 * @param rl the resource list to edit
2671 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2672 * @param rid the resource identifier
2675 resource_list_delete(struct resource_list *rl, int type, int rid)
2677 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2680 if (rle->res != NULL)
2681 panic("resource_list_delete: resource has not been released");
2682 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2688 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2690 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2691 * and passing the allocation up to the parent of @p bus. This assumes
2692 * that the first entry of @c device_get_ivars(child) is a struct
2693 * resource_list. This also handles 'passthrough' allocations where a
2694 * child is a remote descendant of bus by passing the allocation up to
2695 * the parent of bus.
2697 * Typically, a bus driver would store a list of child resources
2698 * somewhere in the child device's ivars (see device_get_ivars()) and
2699 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2700 * then call resource_list_alloc() to perform the allocation.
2702 * @param rl the resource list to allocate from
2703 * @param bus the parent device of @p child
2704 * @param child the device which is requesting an allocation
2705 * @param type the type of resource to allocate
2706 * @param rid a pointer to the resource identifier
2707 * @param start hint at the start of the resource range - pass
2708 * @c 0UL for any start address
2709 * @param end hint at the end of the resource range - pass
2710 * @c ~0UL for any end address
2711 * @param count hint at the size of range required - pass @c 1
2713 * @param flags any extra flags to control the resource
2714 * allocation - see @c RF_XXX flags in
2715 * <sys/rman.h> for details
2717 * @returns the resource which was allocated or @c NULL if no
2718 * resource could be allocated
2721 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2722 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2724 struct resource_list_entry *rle = NULL;
2725 int passthrough = (device_get_parent(child) != bus);
2726 int isdefault = (start == 0UL && end == ~0UL);
2729 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2730 type, rid, start, end, count, flags));
2733 rle = resource_list_find(rl, type, *rid);
2736 return (NULL); /* no resource of that type/rid */
2739 panic("resource_list_alloc: resource entry is busy");
2743 count = ulmax(count, rle->count);
2744 end = ulmax(rle->end, start + count - 1);
2747 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2748 type, rid, start, end, count, flags);
2751 * Record the new range.
2754 rle->start = rman_get_start(rle->res);
2755 rle->end = rman_get_end(rle->res);
2763 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
2765 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
2766 * used with resource_list_alloc().
2768 * @param rl the resource list which was allocated from
2769 * @param bus the parent device of @p child
2770 * @param child the device which is requesting a release
2771 * @param type the type of resource to allocate
2772 * @param rid the resource identifier
2773 * @param res the resource to release
2776 * @retval non-zero a standard unix error code indicating what
2777 * error condition prevented the operation
2780 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
2781 int type, int rid, struct resource *res)
2783 struct resource_list_entry *rle = NULL;
2784 int passthrough = (device_get_parent(child) != bus);
2788 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2792 rle = resource_list_find(rl, type, rid);
2795 panic("resource_list_release: can't find resource");
2797 panic("resource_list_release: resource entry is not busy");
2799 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2809 * @brief Print a description of resources in a resource list
2811 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
2812 * The name is printed if at least one resource of the given type is available.
2813 * The format is used to print resource start and end.
2815 * @param rl the resource list to print
2816 * @param name the name of @p type, e.g. @c "memory"
2817 * @param type type type of resource entry to print
2818 * @param format printf(9) format string to print resource
2819 * start and end values
2821 * @returns the number of characters printed
2824 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2827 struct resource_list_entry *rle;
2828 int printed, retval;
2832 /* Yes, this is kinda cheating */
2833 STAILQ_FOREACH(rle, rl, link) {
2834 if (rle->type == type) {
2836 retval += printf(" %s ", name);
2838 retval += printf(",");
2840 retval += printf(format, rle->start);
2841 if (rle->count > 1) {
2842 retval += printf("-");
2843 retval += printf(format, rle->start +
2852 * @brief Releases all the resources in a list.
2854 * @param rl The resource list to purge.
2859 resource_list_purge(struct resource_list *rl)
2861 struct resource_list_entry *rle;
2863 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2865 bus_release_resource(rman_get_device(rle->res),
2866 rle->type, rle->rid, rle->res);
2867 STAILQ_REMOVE_HEAD(rl, link);
2873 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
2876 return (device_add_child_ordered(dev, order, name, unit));
2880 * @brief Helper function for implementing DEVICE_PROBE()
2882 * This function can be used to help implement the DEVICE_PROBE() for
2883 * a bus (i.e. a device which has other devices attached to it). It
2884 * calls the DEVICE_IDENTIFY() method of each driver in the device's
2888 bus_generic_probe(device_t dev)
2890 devclass_t dc = dev->devclass;
2893 TAILQ_FOREACH(dl, &dc->drivers, link) {
2894 DEVICE_IDENTIFY(dl->driver, dev);
2901 * @brief Helper function for implementing DEVICE_ATTACH()
2903 * This function can be used to help implement the DEVICE_ATTACH() for
2904 * a bus. It calls device_probe_and_attach() for each of the device's
2908 bus_generic_attach(device_t dev)
2912 TAILQ_FOREACH(child, &dev->children, link) {
2913 device_probe_and_attach(child);
2920 * @brief Helper function for implementing DEVICE_DETACH()
2922 * This function can be used to help implement the DEVICE_DETACH() for
2923 * a bus. It calls device_detach() for each of the device's
2927 bus_generic_detach(device_t dev)
2932 if (dev->state != DS_ATTACHED)
2935 TAILQ_FOREACH(child, &dev->children, link) {
2936 if ((error = device_detach(child)) != 0)
2944 * @brief Helper function for implementing DEVICE_SHUTDOWN()
2946 * This function can be used to help implement the DEVICE_SHUTDOWN()
2947 * for a bus. It calls device_shutdown() for each of the device's
2951 bus_generic_shutdown(device_t dev)
2955 TAILQ_FOREACH(child, &dev->children, link) {
2956 device_shutdown(child);
2963 * @brief Helper function for implementing DEVICE_SUSPEND()
2965 * This function can be used to help implement the DEVICE_SUSPEND()
2966 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
2967 * children. If any call to DEVICE_SUSPEND() fails, the suspend
2968 * operation is aborted and any devices which were suspended are
2969 * resumed immediately by calling their DEVICE_RESUME() methods.
2972 bus_generic_suspend(device_t dev)
2975 device_t child, child2;
2977 TAILQ_FOREACH(child, &dev->children, link) {
2978 error = DEVICE_SUSPEND(child);
2980 for (child2 = TAILQ_FIRST(&dev->children);
2981 child2 && child2 != child;
2982 child2 = TAILQ_NEXT(child2, link))
2983 DEVICE_RESUME(child2);
2991 * @brief Helper function for implementing DEVICE_RESUME()
2993 * This function can be used to help implement the DEVICE_RESUME() for
2994 * a bus. It calls DEVICE_RESUME() on each of the device's children.
2997 bus_generic_resume(device_t dev)
3001 TAILQ_FOREACH(child, &dev->children, link) {
3002 DEVICE_RESUME(child);
3003 /* if resume fails, there's nothing we can usefully do... */
3009 * @brief Helper function for implementing BUS_PRINT_CHILD().
3011 * This function prints the first part of the ascii representation of
3012 * @p child, including its name, unit and description (if any - see
3013 * device_set_desc()).
3015 * @returns the number of characters printed
3018 bus_print_child_header(device_t dev, device_t child)
3022 if (device_get_desc(child)) {
3023 retval += device_printf(child, "<%s>", device_get_desc(child));
3025 retval += printf("%s", device_get_nameunit(child));
3032 * @brief Helper function for implementing BUS_PRINT_CHILD().
3034 * This function prints the last part of the ascii representation of
3035 * @p child, which consists of the string @c " on " followed by the
3036 * name and unit of the @p dev.
3038 * @returns the number of characters printed
3041 bus_print_child_footer(device_t dev, device_t child)
3043 return (printf(" on %s\n", device_get_nameunit(dev)));
3047 * @brief Helper function for implementing BUS_PRINT_CHILD().
3049 * This function simply calls bus_print_child_header() followed by
3050 * bus_print_child_footer().
3052 * @returns the number of characters printed
3055 bus_generic_print_child(device_t dev, device_t child)
3059 retval += bus_print_child_header(dev, child);
3060 retval += bus_print_child_footer(dev, child);
3066 * @brief Stub function for implementing BUS_READ_IVAR().
3071 bus_generic_read_ivar(device_t dev, device_t child, int index,
3078 * @brief Stub function for implementing BUS_WRITE_IVAR().
3083 bus_generic_write_ivar(device_t dev, device_t child, int index,
3090 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3094 struct resource_list *
3095 bus_generic_get_resource_list(device_t dev, device_t child)
3101 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3103 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3104 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3105 * and then calls device_probe_and_attach() for each unattached child.
3108 bus_generic_driver_added(device_t dev, driver_t *driver)
3112 DEVICE_IDENTIFY(driver, dev);
3113 TAILQ_FOREACH(child, &dev->children, link) {
3114 if (child->state == DS_NOTPRESENT ||
3115 (child->flags & DF_REBID))
3116 device_probe_and_attach(child);
3121 * @brief Helper function for implementing BUS_SETUP_INTR().
3123 * This simple implementation of BUS_SETUP_INTR() simply calls the
3124 * BUS_SETUP_INTR() method of the parent of @p dev.
3127 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3128 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3131 /* Propagate up the bus hierarchy until someone handles it. */
3133 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3134 filter, intr, arg, cookiep));
3139 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3141 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3142 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3145 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3148 /* Propagate up the bus hierarchy until someone handles it. */
3150 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3155 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3157 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3158 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3161 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3162 u_long start, u_long end, u_long count, u_int flags)
3164 /* Propagate up the bus hierarchy until someone handles it. */
3166 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3167 start, end, count, flags));
3172 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3174 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3175 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3178 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3181 /* Propagate up the bus hierarchy until someone handles it. */
3183 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3189 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3191 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3192 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3195 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3198 /* Propagate up the bus hierarchy until someone handles it. */
3200 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3206 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3208 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3209 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3212 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3213 int rid, struct resource *r)
3215 /* Propagate up the bus hierarchy until someone handles it. */
3217 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3223 * @brief Helper function for implementing BUS_BIND_INTR().
3225 * This simple implementation of BUS_BIND_INTR() simply calls the
3226 * BUS_BIND_INTR() method of the parent of @p dev.
3229 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3233 /* Propagate up the bus hierarchy until someone handles it. */
3235 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3240 * @brief Helper function for implementing BUS_CONFIG_INTR().
3242 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3243 * BUS_CONFIG_INTR() method of the parent of @p dev.
3246 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3247 enum intr_polarity pol)
3250 /* Propagate up the bus hierarchy until someone handles it. */
3252 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3257 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3259 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3260 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3263 bus_generic_get_dma_tag(device_t dev, device_t child)
3266 /* Propagate up the bus hierarchy until someone handles it. */
3267 if (dev->parent != NULL)
3268 return (BUS_GET_DMA_TAG(dev->parent, child));
3273 * @brief Helper function for implementing BUS_GET_RESOURCE().
3275 * This implementation of BUS_GET_RESOURCE() uses the
3276 * resource_list_find() function to do most of the work. It calls
3277 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3281 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3282 u_long *startp, u_long *countp)
3284 struct resource_list * rl = NULL;
3285 struct resource_list_entry * rle = NULL;
3287 rl = BUS_GET_RESOURCE_LIST(dev, child);
3291 rle = resource_list_find(rl, type, rid);
3296 *startp = rle->start;
3298 *countp = rle->count;
3304 * @brief Helper function for implementing BUS_SET_RESOURCE().
3306 * This implementation of BUS_SET_RESOURCE() uses the
3307 * resource_list_add() function to do most of the work. It calls
3308 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3312 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3313 u_long start, u_long count)
3315 struct resource_list * rl = NULL;
3317 rl = BUS_GET_RESOURCE_LIST(dev, child);
3321 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3327 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3329 * This implementation of BUS_DELETE_RESOURCE() uses the
3330 * resource_list_delete() function to do most of the work. It calls
3331 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3335 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3337 struct resource_list * rl = NULL;
3339 rl = BUS_GET_RESOURCE_LIST(dev, child);
3343 resource_list_delete(rl, type, rid);
3349 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3351 * This implementation of BUS_RELEASE_RESOURCE() uses the
3352 * resource_list_release() function to do most of the work. It calls
3353 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3356 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3357 int rid, struct resource *r)
3359 struct resource_list * rl = NULL;
3361 rl = BUS_GET_RESOURCE_LIST(dev, child);
3365 return (resource_list_release(rl, dev, child, type, rid, r));
3369 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3371 * This implementation of BUS_ALLOC_RESOURCE() uses the
3372 * resource_list_alloc() function to do most of the work. It calls
3373 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3376 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3377 int *rid, u_long start, u_long end, u_long count, u_int flags)
3379 struct resource_list * rl = NULL;
3381 rl = BUS_GET_RESOURCE_LIST(dev, child);
3385 return (resource_list_alloc(rl, dev, child, type, rid,
3386 start, end, count, flags));
3390 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3392 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3393 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3396 bus_generic_child_present(device_t dev, device_t child)
3398 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3402 * Some convenience functions to make it easier for drivers to use the
3403 * resource-management functions. All these really do is hide the
3404 * indirection through the parent's method table, making for slightly
3405 * less-wordy code. In the future, it might make sense for this code
3406 * to maintain some sort of a list of resources allocated by each device.
3410 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3411 struct resource **res)
3415 for (i = 0; rs[i].type != -1; i++)
3417 for (i = 0; rs[i].type != -1; i++) {
3418 res[i] = bus_alloc_resource_any(dev,
3419 rs[i].type, &rs[i].rid, rs[i].flags);
3420 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3421 bus_release_resources(dev, rs, res);
3429 bus_release_resources(device_t dev, const struct resource_spec *rs,
3430 struct resource **res)
3434 for (i = 0; rs[i].type != -1; i++)
3435 if (res[i] != NULL) {
3436 bus_release_resource(
3437 dev, rs[i].type, rs[i].rid, res[i]);
3443 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3445 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3449 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3450 u_long count, u_int flags)
3452 if (dev->parent == NULL)
3454 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3459 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3461 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3465 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3467 if (dev->parent == NULL)
3469 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3473 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3475 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3479 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3481 if (dev->parent == NULL)
3483 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3487 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3489 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3493 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3495 if (dev->parent == NULL)
3497 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3501 * @brief Wrapper function for BUS_SETUP_INTR().
3503 * This function simply calls the BUS_SETUP_INTR() method of the
3507 bus_setup_intr(device_t dev, struct resource *r, int flags,
3508 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3512 if (dev->parent != NULL) {
3513 error = BUS_SETUP_INTR(dev->parent, dev, r, flags,
3514 filter, handler, arg, cookiep);
3516 if (handler != NULL && !(flags & INTR_MPSAFE))
3517 device_printf(dev, "[GIANT-LOCKED]\n");
3518 if (bootverbose && (flags & INTR_MPSAFE))
3519 device_printf(dev, "[MPSAFE]\n");
3520 if (filter != NULL) {
3521 if (handler == NULL)
3522 device_printf(dev, "[FILTER]\n");
3524 device_printf(dev, "[FILTER+ITHREAD]\n");
3526 device_printf(dev, "[ITHREAD]\n");
3534 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3536 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3540 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3542 if (dev->parent == NULL)
3544 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3548 * @brief Wrapper function for BUS_BIND_INTR().
3550 * This function simply calls the BUS_BIND_INTR() method of the
3554 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3556 if (dev->parent == NULL)
3558 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3562 * @brief Wrapper function for BUS_SET_RESOURCE().
3564 * This function simply calls the BUS_SET_RESOURCE() method of the
3568 bus_set_resource(device_t dev, int type, int rid,
3569 u_long start, u_long count)
3571 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3576 * @brief Wrapper function for BUS_GET_RESOURCE().
3578 * This function simply calls the BUS_GET_RESOURCE() method of the
3582 bus_get_resource(device_t dev, int type, int rid,
3583 u_long *startp, u_long *countp)
3585 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3590 * @brief Wrapper function for BUS_GET_RESOURCE().
3592 * This function simply calls the BUS_GET_RESOURCE() method of the
3593 * parent of @p dev and returns the start value.
3596 bus_get_resource_start(device_t dev, int type, int rid)
3598 u_long start, count;
3601 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3609 * @brief Wrapper function for BUS_GET_RESOURCE().
3611 * This function simply calls the BUS_GET_RESOURCE() method of the
3612 * parent of @p dev and returns the count value.
3615 bus_get_resource_count(device_t dev, int type, int rid)
3617 u_long start, count;
3620 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3628 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3630 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3634 bus_delete_resource(device_t dev, int type, int rid)
3636 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3640 * @brief Wrapper function for BUS_CHILD_PRESENT().
3642 * This function simply calls the BUS_CHILD_PRESENT() method of the
3646 bus_child_present(device_t child)
3648 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3652 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3654 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3658 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3662 parent = device_get_parent(child);
3663 if (parent == NULL) {
3667 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3671 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3673 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3677 bus_child_location_str(device_t child, char *buf, size_t buflen)
3681 parent = device_get_parent(child);
3682 if (parent == NULL) {
3686 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3690 * @brief Wrapper function for BUS_GET_DMA_TAG().
3692 * This function simply calls the BUS_GET_DMA_TAG() method of the
3696 bus_get_dma_tag(device_t dev)
3700 parent = device_get_parent(dev);
3703 return (BUS_GET_DMA_TAG(parent, dev));
3706 /* Resume all devices and then notify userland that we're up again. */
3708 root_resume(device_t dev)
3712 error = bus_generic_resume(dev);
3714 devctl_notify("kern", "power", "resume", NULL);
3719 root_print_child(device_t dev, device_t child)
3723 retval += bus_print_child_header(dev, child);
3724 retval += printf("\n");
3730 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3734 * If an interrupt mapping gets to here something bad has happened.
3736 panic("root_setup_intr");
3740 * If we get here, assume that the device is permanant and really is
3741 * present in the system. Removable bus drivers are expected to intercept
3742 * this call long before it gets here. We return -1 so that drivers that
3743 * really care can check vs -1 or some ERRNO returned higher in the food
3747 root_child_present(device_t dev, device_t child)
3752 static kobj_method_t root_methods[] = {
3753 /* Device interface */
3754 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3755 KOBJMETHOD(device_suspend, bus_generic_suspend),
3756 KOBJMETHOD(device_resume, root_resume),
3759 KOBJMETHOD(bus_print_child, root_print_child),
3760 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3761 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3762 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3763 KOBJMETHOD(bus_child_present, root_child_present),
3768 static driver_t root_driver = {
3775 devclass_t root_devclass;
3778 root_bus_module_handler(module_t mod, int what, void* arg)
3782 TAILQ_INIT(&bus_data_devices);
3783 kobj_class_compile((kobj_class_t) &root_driver);
3784 root_bus = make_device(NULL, "root", 0);
3785 root_bus->desc = "System root bus";
3786 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3787 root_bus->driver = &root_driver;
3788 root_bus->state = DS_ATTACHED;
3789 root_devclass = devclass_find_internal("root", NULL, FALSE);
3794 device_shutdown(root_bus);
3797 return (EOPNOTSUPP);
3803 static moduledata_t root_bus_mod = {
3805 root_bus_module_handler,
3808 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3811 * @brief Automatically configure devices
3813 * This function begins the autoconfiguration process by calling
3814 * device_probe_and_attach() for each child of the @c root0 device.
3817 root_bus_configure(void)
3823 TAILQ_FOREACH(dev, &root_bus->children, link) {
3824 device_probe_and_attach(dev);
3829 * @brief Module handler for registering device drivers
3831 * This module handler is used to automatically register device
3832 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
3833 * devclass_add_driver() for the driver described by the
3834 * driver_module_data structure pointed to by @p arg
3837 driver_module_handler(module_t mod, int what, void *arg)
3840 struct driver_module_data *dmd;
3841 devclass_t bus_devclass;
3842 kobj_class_t driver;
3844 dmd = (struct driver_module_data *)arg;
3845 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3850 if (dmd->dmd_chainevh)
3851 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3853 driver = dmd->dmd_driver;
3854 PDEBUG(("Loading module: driver %s on bus %s",
3855 DRIVERNAME(driver), dmd->dmd_busname));
3856 error = devclass_add_driver(bus_devclass, driver);
3861 * If the driver has any base classes, make the
3862 * devclass inherit from the devclass of the driver's
3863 * first base class. This will allow the system to
3864 * search for drivers in both devclasses for children
3865 * of a device using this driver.
3867 if (driver->baseclasses) {
3868 const char *parentname;
3869 parentname = driver->baseclasses[0]->name;
3870 *dmd->dmd_devclass =
3871 devclass_find_internal(driver->name,
3874 *dmd->dmd_devclass =
3875 devclass_find_internal(driver->name, NULL, TRUE);
3880 PDEBUG(("Unloading module: driver %s from bus %s",
3881 DRIVERNAME(dmd->dmd_driver),
3883 error = devclass_delete_driver(bus_devclass,
3886 if (!error && dmd->dmd_chainevh)
3887 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3890 PDEBUG(("Quiesce module: driver %s from bus %s",
3891 DRIVERNAME(dmd->dmd_driver),
3893 error = devclass_quiesce_driver(bus_devclass,
3896 if (!error && dmd->dmd_chainevh)
3897 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3908 * @brief Enumerate all hinted devices for this bus.
3910 * Walks through the hints for this bus and calls the bus_hinted_child
3911 * routine for each one it fines. It searches first for the specific
3912 * bus that's being probed for hinted children (eg isa0), and then for
3913 * generic children (eg isa).
3915 * @param dev bus device to enumerate
3918 bus_enumerate_hinted_children(device_t bus)
3921 const char *dname, *busname;
3925 * enumerate all devices on the specific bus
3927 busname = device_get_nameunit(bus);
3929 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3930 BUS_HINTED_CHILD(bus, dname, dunit);
3933 * and all the generic ones.
3935 busname = device_get_name(bus);
3937 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3938 BUS_HINTED_CHILD(bus, dname, dunit);
3943 /* the _short versions avoid iteration by not calling anything that prints
3944 * more than oneliners. I love oneliners.
3948 print_device_short(device_t dev, int indent)
3953 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3954 dev->unit, dev->desc,
3955 (dev->parent? "":"no "),
3956 (TAILQ_EMPTY(&dev->children)? "no ":""),
3957 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3958 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3959 (dev->flags&DF_WILDCARD? "wildcard,":""),
3960 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3961 (dev->flags&DF_REBID? "rebiddable,":""),
3962 (dev->ivars? "":"no "),
3963 (dev->softc? "":"no "),
3968 print_device(device_t dev, int indent)
3973 print_device_short(dev, indent);
3975 indentprintf(("Parent:\n"));
3976 print_device_short(dev->parent, indent+1);
3977 indentprintf(("Driver:\n"));
3978 print_driver_short(dev->driver, indent+1);
3979 indentprintf(("Devclass:\n"));
3980 print_devclass_short(dev->devclass, indent+1);
3984 print_device_tree_short(device_t dev, int indent)
3985 /* print the device and all its children (indented) */
3992 print_device_short(dev, indent);
3994 TAILQ_FOREACH(child, &dev->children, link) {
3995 print_device_tree_short(child, indent+1);
4000 print_device_tree(device_t dev, int indent)
4001 /* print the device and all its children (indented) */
4008 print_device(dev, indent);
4010 TAILQ_FOREACH(child, &dev->children, link) {
4011 print_device_tree(child, indent+1);
4016 print_driver_short(driver_t *driver, int indent)
4021 indentprintf(("driver %s: softc size = %zd\n",
4022 driver->name, driver->size));
4026 print_driver(driver_t *driver, int indent)
4031 print_driver_short(driver, indent);
4036 print_driver_list(driver_list_t drivers, int indent)
4038 driverlink_t driver;
4040 TAILQ_FOREACH(driver, &drivers, link) {
4041 print_driver(driver->driver, indent);
4046 print_devclass_short(devclass_t dc, int indent)
4051 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4055 print_devclass(devclass_t dc, int indent)
4062 print_devclass_short(dc, indent);
4063 indentprintf(("Drivers:\n"));
4064 print_driver_list(dc->drivers, indent+1);
4066 indentprintf(("Devices:\n"));
4067 for (i = 0; i < dc->maxunit; i++)
4069 print_device(dc->devices[i], indent+1);
4073 print_devclass_list_short(void)
4077 printf("Short listing of devclasses, drivers & devices:\n");
4078 TAILQ_FOREACH(dc, &devclasses, link) {
4079 print_devclass_short(dc, 0);
4084 print_devclass_list(void)
4088 printf("Full listing of devclasses, drivers & devices:\n");
4089 TAILQ_FOREACH(dc, &devclasses, link) {
4090 print_devclass(dc, 0);
4097 * User-space access to the device tree.
4099 * We implement a small set of nodes:
4101 * hw.bus Single integer read method to obtain the
4102 * current generation count.
4103 * hw.bus.devices Reads the entire device tree in flat space.
4104 * hw.bus.rman Resource manager interface
4106 * We might like to add the ability to scan devclasses and/or drivers to
4107 * determine what else is currently loaded/available.
4111 sysctl_bus(SYSCTL_HANDLER_ARGS)
4113 struct u_businfo ubus;
4115 ubus.ub_version = BUS_USER_VERSION;
4116 ubus.ub_generation = bus_data_generation;
4118 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4120 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4121 "bus-related data");
4124 sysctl_devices(SYSCTL_HANDLER_ARGS)
4126 int *name = (int *)arg1;
4127 u_int namelen = arg2;
4130 struct u_device udev; /* XXX this is a bit big */
4136 if (bus_data_generation_check(name[0]))
4142 * Scan the list of devices, looking for the requested index.
4144 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4152 * Populate the return array.
4154 bzero(&udev, sizeof(udev));
4155 udev.dv_handle = (uintptr_t)dev;
4156 udev.dv_parent = (uintptr_t)dev->parent;
4157 if (dev->nameunit != NULL)
4158 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4159 if (dev->desc != NULL)
4160 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4161 if (dev->driver != NULL && dev->driver->name != NULL)
4162 strlcpy(udev.dv_drivername, dev->driver->name,
4163 sizeof(udev.dv_drivername));
4164 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4165 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4166 udev.dv_devflags = dev->devflags;
4167 udev.dv_flags = dev->flags;
4168 udev.dv_state = dev->state;
4169 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4173 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4174 "system device tree");
4177 bus_data_generation_check(int generation)
4179 if (generation != bus_data_generation)
4182 /* XXX generate optimised lists here? */
4187 bus_data_generation_update(void)
4189 bus_data_generation++;
4193 bus_free_resource(device_t dev, int type, struct resource *r)
4197 return (bus_release_resource(dev, type, rman_get_rid(r), r));