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$");
31 #include "opt_random.h"
33 #include <sys/param.h>
35 #include <sys/filio.h>
37 #include <sys/kernel.h>
39 #include <sys/limits.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/queue.h>
47 #include <machine/bus.h>
48 #include <sys/random.h>
50 #include <sys/selinfo.h>
51 #include <sys/signalvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
56 #include <sys/interrupt.h>
60 #include <machine/cpu.h>
61 #include <machine/stdarg.h>
65 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
66 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
69 * Used to attach drivers to devclasses.
71 typedef struct driverlink *driverlink_t;
74 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
76 TAILQ_ENTRY(driverlink) passlink;
80 * Forward declarations
82 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
83 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
84 typedef TAILQ_HEAD(device_list, device) device_list_t;
87 TAILQ_ENTRY(devclass) link;
88 devclass_t parent; /* parent in devclass hierarchy */
89 driver_list_t drivers; /* bus devclasses store drivers for bus */
91 device_t *devices; /* array of devices indexed by unit */
92 int maxunit; /* size of devices array */
94 #define DC_HAS_CHILDREN 1
96 struct sysctl_ctx_list sysctl_ctx;
97 struct sysctl_oid *sysctl_tree;
101 * @brief Implementation of device.
105 * A device is a kernel object. The first field must be the
106 * current ops table for the object.
113 TAILQ_ENTRY(device) link; /**< list of devices in parent */
114 TAILQ_ENTRY(device) devlink; /**< global device list membership */
115 device_t parent; /**< parent of this device */
116 device_list_t children; /**< list of child devices */
119 * Details of this device.
121 driver_t *driver; /**< current driver */
122 devclass_t devclass; /**< current device class */
123 int unit; /**< current unit number */
124 char* nameunit; /**< name+unit e.g. foodev0 */
125 char* desc; /**< driver specific description */
126 int busy; /**< count of calls to device_busy() */
127 device_state_t state; /**< current device state */
128 uint32_t devflags; /**< api level flags for device_get_flags() */
129 u_int flags; /**< internal device flags */
130 #define DF_ENABLED 0x01 /* device should be probed/attached */
131 #define DF_FIXEDCLASS 0x02 /* devclass specified at create time */
132 #define DF_WILDCARD 0x04 /* unit was originally wildcard */
133 #define DF_DESCMALLOCED 0x08 /* description was malloced */
134 #define DF_QUIET 0x10 /* don't print verbose attach message */
135 #define DF_DONENOMATCH 0x20 /* don't execute DEVICE_NOMATCH again */
136 #define DF_EXTERNALSOFTC 0x40 /* softc not allocated by us */
137 #define DF_REBID 0x80 /* Can rebid after attach */
138 u_int order; /**< order from device_add_child_ordered() */
139 void *ivars; /**< instance variables */
140 void *softc; /**< current driver's variables */
142 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
143 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
146 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
147 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
151 static int bus_debug = 1;
152 TUNABLE_INT("bus.debug", &bus_debug);
153 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
156 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
157 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
158 #define DRIVERNAME(d) ((d)? d->name : "no driver")
159 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
162 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
163 * prevent syslog from deleting initial spaces
165 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
167 static void print_device_short(device_t dev, int indent);
168 static void print_device(device_t dev, int indent);
169 void print_device_tree_short(device_t dev, int indent);
170 void print_device_tree(device_t dev, int indent);
171 static void print_driver_short(driver_t *driver, int indent);
172 static void print_driver(driver_t *driver, int indent);
173 static void print_driver_list(driver_list_t drivers, int indent);
174 static void print_devclass_short(devclass_t dc, int indent);
175 static void print_devclass(devclass_t dc, int indent);
176 void print_devclass_list_short(void);
177 void print_devclass_list(void);
180 /* Make the compiler ignore the function calls */
181 #define PDEBUG(a) /* nop */
182 #define DEVICENAME(d) /* nop */
183 #define DRIVERNAME(d) /* nop */
184 #define DEVCLANAME(d) /* nop */
186 #define print_device_short(d,i) /* nop */
187 #define print_device(d,i) /* nop */
188 #define print_device_tree_short(d,i) /* nop */
189 #define print_device_tree(d,i) /* nop */
190 #define print_driver_short(d,i) /* nop */
191 #define print_driver(d,i) /* nop */
192 #define print_driver_list(d,i) /* nop */
193 #define print_devclass_short(d,i) /* nop */
194 #define print_devclass(d,i) /* nop */
195 #define print_devclass_list_short() /* nop */
196 #define print_devclass_list() /* nop */
204 DEVCLASS_SYSCTL_PARENT,
208 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
210 devclass_t dc = (devclass_t)arg1;
214 case DEVCLASS_SYSCTL_PARENT:
215 value = dc->parent ? dc->parent->name : "";
220 return (SYSCTL_OUT(req, value, strlen(value)));
224 devclass_sysctl_init(devclass_t dc)
227 if (dc->sysctl_tree != NULL)
229 sysctl_ctx_init(&dc->sysctl_ctx);
230 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
231 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
232 CTLFLAG_RD, NULL, "");
233 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
234 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
235 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
241 DEVICE_SYSCTL_DRIVER,
242 DEVICE_SYSCTL_LOCATION,
243 DEVICE_SYSCTL_PNPINFO,
244 DEVICE_SYSCTL_PARENT,
248 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
250 device_t dev = (device_t)arg1;
257 case DEVICE_SYSCTL_DESC:
258 value = dev->desc ? dev->desc : "";
260 case DEVICE_SYSCTL_DRIVER:
261 value = dev->driver ? dev->driver->name : "";
263 case DEVICE_SYSCTL_LOCATION:
264 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
265 bus_child_location_str(dev, buf, 1024);
267 case DEVICE_SYSCTL_PNPINFO:
268 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
269 bus_child_pnpinfo_str(dev, buf, 1024);
271 case DEVICE_SYSCTL_PARENT:
272 value = dev->parent ? dev->parent->nameunit : "";
277 error = SYSCTL_OUT(req, value, strlen(value));
284 device_sysctl_init(device_t dev)
286 devclass_t dc = dev->devclass;
288 if (dev->sysctl_tree != NULL)
290 devclass_sysctl_init(dc);
291 sysctl_ctx_init(&dev->sysctl_ctx);
292 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
293 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
294 dev->nameunit + strlen(dc->name),
295 CTLFLAG_RD, NULL, "");
296 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
297 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
298 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
299 "device description");
300 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
301 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
302 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
303 "device driver name");
304 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
305 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
306 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
307 "device location relative to parent");
308 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
309 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
310 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
311 "device identification");
312 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
313 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
314 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
319 device_sysctl_update(device_t dev)
321 devclass_t dc = dev->devclass;
323 if (dev->sysctl_tree == NULL)
325 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
329 device_sysctl_fini(device_t dev)
331 if (dev->sysctl_tree == NULL)
333 sysctl_ctx_free(&dev->sysctl_ctx);
334 dev->sysctl_tree = NULL;
338 * /dev/devctl implementation
342 * This design allows only one reader for /dev/devctl. This is not desirable
343 * in the long run, but will get a lot of hair out of this implementation.
344 * Maybe we should make this device a clonable device.
346 * Also note: we specifically do not attach a device to the device_t tree
347 * to avoid potential chicken and egg problems. One could argue that all
348 * of this belongs to the root node. One could also further argue that the
349 * sysctl interface that we have not might more properly be an ioctl
350 * interface, but at this stage of the game, I'm not inclined to rock that
353 * I'm also not sure that the SIGIO support is done correctly or not, as
354 * I copied it from a driver that had SIGIO support that likely hasn't been
355 * tested since 3.4 or 2.2.8!
358 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
359 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
360 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
361 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
362 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW |
363 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
365 static d_open_t devopen;
366 static d_close_t devclose;
367 static d_read_t devread;
368 static d_ioctl_t devioctl;
369 static d_poll_t devpoll;
370 static d_kqfilter_t devkqfilter;
372 static struct cdevsw dev_cdevsw = {
373 .d_version = D_VERSION,
379 .d_kqfilter = devkqfilter,
383 struct dev_event_info
386 TAILQ_ENTRY(dev_event_info) dei_link;
389 TAILQ_HEAD(devq, dev_event_info);
391 static struct dev_softc
404 static void filt_devctl_detach(struct knote *kn);
405 static int filt_devctl_read(struct knote *kn, long hint);
407 struct filterops devctl_rfiltops = {
409 .f_detach = filt_devctl_detach,
410 .f_event = filt_devctl_read,
413 static struct cdev *devctl_dev;
418 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
419 UID_ROOT, GID_WHEEL, 0600, "devctl");
420 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
421 cv_init(&devsoftc.cv, "dev cv");
422 TAILQ_INIT(&devsoftc.devq);
423 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
427 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
430 mtx_lock(&devsoftc.mtx);
431 if (devsoftc.inuse) {
432 mtx_unlock(&devsoftc.mtx);
437 devsoftc.nonblock = 0;
439 mtx_unlock(&devsoftc.mtx);
444 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
447 mtx_lock(&devsoftc.mtx);
449 cv_broadcast(&devsoftc.cv);
450 funsetown(&devsoftc.sigio);
451 mtx_unlock(&devsoftc.mtx);
456 * The read channel for this device is used to report changes to
457 * userland in realtime. We are required to free the data as well as
458 * the n1 object because we allocate them separately. Also note that
459 * we return one record at a time. If you try to read this device a
460 * character at a time, you will lose the rest of the data. Listening
461 * programs are expected to cope.
464 devread(struct cdev *dev, struct uio *uio, int ioflag)
466 struct dev_event_info *n1;
469 mtx_lock(&devsoftc.mtx);
470 while (TAILQ_EMPTY(&devsoftc.devq)) {
471 if (devsoftc.nonblock) {
472 mtx_unlock(&devsoftc.mtx);
475 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
478 * Need to translate ERESTART to EINTR here? -- jake
480 mtx_unlock(&devsoftc.mtx);
484 n1 = TAILQ_FIRST(&devsoftc.devq);
485 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
487 mtx_unlock(&devsoftc.mtx);
488 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
489 free(n1->dei_data, M_BUS);
495 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
501 devsoftc.nonblock = 1;
503 devsoftc.nonblock = 0;
512 return fsetown(*(int *)data, &devsoftc.sigio);
514 *(int *)data = fgetown(&devsoftc.sigio);
517 /* (un)Support for other fcntl() calls. */
528 devpoll(struct cdev *dev, int events, struct thread *td)
532 mtx_lock(&devsoftc.mtx);
533 if (events & (POLLIN | POLLRDNORM)) {
534 if (!TAILQ_EMPTY(&devsoftc.devq))
535 revents = events & (POLLIN | POLLRDNORM);
537 selrecord(td, &devsoftc.sel);
539 mtx_unlock(&devsoftc.mtx);
545 devkqfilter(struct cdev *dev, struct knote *kn)
549 if (kn->kn_filter == EVFILT_READ) {
550 kn->kn_fop = &devctl_rfiltops;
551 knlist_add(&devsoftc.sel.si_note, kn, 0);
559 filt_devctl_detach(struct knote *kn)
562 knlist_remove(&devsoftc.sel.si_note, kn, 0);
566 filt_devctl_read(struct knote *kn, long hint)
568 kn->kn_data = devsoftc.queued;
569 return (kn->kn_data != 0);
573 * @brief Return whether the userland process is running
576 devctl_process_running(void)
578 return (devsoftc.inuse == 1);
582 * @brief Queue data to be read from the devctl device
584 * Generic interface to queue data to the devctl device. It is
585 * assumed that @p data is properly formatted. It is further assumed
586 * that @p data is allocated using the M_BUS malloc type.
589 devctl_queue_data_f(char *data, int flags)
591 struct dev_event_info *n1 = NULL, *n2 = NULL;
593 if (strlen(data) == 0)
595 if (devctl_queue_length == 0)
597 n1 = malloc(sizeof(*n1), M_BUS, flags);
601 mtx_lock(&devsoftc.mtx);
602 if (devctl_queue_length == 0) {
603 mtx_unlock(&devsoftc.mtx);
604 free(n1->dei_data, M_BUS);
608 /* Leave at least one spot in the queue... */
609 while (devsoftc.queued > devctl_queue_length - 1) {
610 n2 = TAILQ_FIRST(&devsoftc.devq);
611 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
612 free(n2->dei_data, M_BUS);
616 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
618 cv_broadcast(&devsoftc.cv);
619 KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
620 mtx_unlock(&devsoftc.mtx);
621 selwakeup(&devsoftc.sel);
622 if (devsoftc.async && devsoftc.sigio != NULL)
623 pgsigio(&devsoftc.sigio, SIGIO, 0);
627 * We have to free data on all error paths since the caller
628 * assumes it will be free'd when this item is dequeued.
635 devctl_queue_data(char *data)
638 devctl_queue_data_f(data, M_NOWAIT);
642 * @brief Send a 'notification' to userland, using standard ways
645 devctl_notify_f(const char *system, const char *subsystem, const char *type,
646 const char *data, int flags)
652 return; /* BOGUS! Must specify system. */
653 if (subsystem == NULL)
654 return; /* BOGUS! Must specify subsystem. */
656 return; /* BOGUS! Must specify type. */
657 len += strlen(" system=") + strlen(system);
658 len += strlen(" subsystem=") + strlen(subsystem);
659 len += strlen(" type=") + strlen(type);
660 /* add in the data message plus newline. */
663 len += 3; /* '!', '\n', and NUL */
664 msg = malloc(len, M_BUS, flags);
666 return; /* Drop it on the floor */
668 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
669 system, subsystem, type, data);
671 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
672 system, subsystem, type);
673 devctl_queue_data_f(msg, flags);
677 devctl_notify(const char *system, const char *subsystem, const char *type,
681 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
685 * Common routine that tries to make sending messages as easy as possible.
686 * We allocate memory for the data, copy strings into that, but do not
687 * free it unless there's an error. The dequeue part of the driver should
688 * free the data. We don't send data when queue length is 0. We do send
689 * data, even when we have no listeners, because we wish to avoid races
690 * relating to startup and restart of listening applications.
692 * devaddq is designed to string together the type of event, with the
693 * object of that event, plus the plug and play info and location info
694 * for that event. This is likely most useful for devices, but less
695 * useful for other consumers of this interface. Those should use
696 * the devctl_queue_data() interface instead.
699 devaddq(const char *type, const char *what, device_t dev)
706 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
708 data = malloc(1024, M_BUS, M_NOWAIT);
712 /* get the bus specific location of this device */
713 loc = malloc(1024, M_BUS, M_NOWAIT);
717 bus_child_location_str(dev, loc, 1024);
719 /* Get the bus specific pnp info of this device */
720 pnp = malloc(1024, M_BUS, M_NOWAIT);
724 bus_child_pnpinfo_str(dev, pnp, 1024);
726 /* Get the parent of this device, or / if high enough in the tree. */
727 if (device_get_parent(dev) == NULL)
728 parstr = "."; /* Or '/' ? */
730 parstr = device_get_nameunit(device_get_parent(dev));
731 /* String it all together. */
732 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
736 devctl_queue_data(data);
746 * A device was added to the tree. We are called just after it successfully
747 * attaches (that is, probe and attach success for this device). No call
748 * is made if a device is merely parented into the tree. See devnomatch
749 * if probe fails. If attach fails, no notification is sent (but maybe
750 * we should have a different message for this).
753 devadded(device_t dev)
755 devaddq("+", device_get_nameunit(dev), dev);
759 * A device was removed from the tree. We are called just before this
763 devremoved(device_t dev)
765 devaddq("-", device_get_nameunit(dev), dev);
769 * Called when there's no match for this device. This is only called
770 * the first time that no match happens, so we don't keep getting this
771 * message. Should that prove to be undesirable, we can change it.
772 * This is called when all drivers that can attach to a given bus
773 * decline to accept this device. Other errors may not be detected.
776 devnomatch(device_t dev)
778 devaddq("?", "", dev);
782 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
784 struct dev_event_info *n1;
787 q = devctl_queue_length;
788 error = sysctl_handle_int(oidp, &q, 0, req);
789 if (error || !req->newptr)
793 mtx_lock(&devsoftc.mtx);
794 devctl_queue_length = q;
795 while (devsoftc.queued > devctl_queue_length) {
796 n1 = TAILQ_FIRST(&devsoftc.devq);
797 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
798 free(n1->dei_data, M_BUS);
802 mtx_unlock(&devsoftc.mtx);
806 /* End of /dev/devctl code */
808 static TAILQ_HEAD(,device) bus_data_devices;
809 static int bus_data_generation = 1;
811 static kobj_method_t null_methods[] = {
815 DEFINE_CLASS(null, null_methods, 0);
818 * Bus pass implementation
821 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
822 int bus_current_pass = BUS_PASS_ROOT;
826 * @brief Register the pass level of a new driver attachment
828 * Register a new driver attachment's pass level. If no driver
829 * attachment with the same pass level has been added, then @p new
830 * will be added to the global passes list.
832 * @param new the new driver attachment
835 driver_register_pass(struct driverlink *new)
837 struct driverlink *dl;
839 /* We only consider pass numbers during boot. */
840 if (bus_current_pass == BUS_PASS_DEFAULT)
844 * Walk the passes list. If we already know about this pass
845 * then there is nothing to do. If we don't, then insert this
846 * driver link into the list.
848 TAILQ_FOREACH(dl, &passes, passlink) {
849 if (dl->pass < new->pass)
851 if (dl->pass == new->pass)
853 TAILQ_INSERT_BEFORE(dl, new, passlink);
856 TAILQ_INSERT_TAIL(&passes, new, passlink);
860 * @brief Raise the current bus pass
862 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
863 * method on the root bus to kick off a new device tree scan for each
864 * new pass level that has at least one driver.
867 bus_set_pass(int pass)
869 struct driverlink *dl;
871 if (bus_current_pass > pass)
872 panic("Attempt to lower bus pass level");
874 TAILQ_FOREACH(dl, &passes, passlink) {
875 /* Skip pass values below the current pass level. */
876 if (dl->pass <= bus_current_pass)
880 * Bail once we hit a driver with a pass level that is
887 * Raise the pass level to the next level and rescan
890 bus_current_pass = dl->pass;
891 BUS_NEW_PASS(root_bus);
895 * If there isn't a driver registered for the requested pass,
896 * then bus_current_pass might still be less than 'pass'. Set
897 * it to 'pass' in that case.
899 if (bus_current_pass < pass)
900 bus_current_pass = pass;
901 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
905 * Devclass implementation
908 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
912 * @brief Find or create a device class
914 * If a device class with the name @p classname exists, return it,
915 * otherwise if @p create is non-zero create and return a new device
918 * If @p parentname is non-NULL, the parent of the devclass is set to
919 * the devclass of that name.
921 * @param classname the devclass name to find or create
922 * @param parentname the parent devclass name or @c NULL
923 * @param create non-zero to create a devclass
926 devclass_find_internal(const char *classname, const char *parentname,
931 PDEBUG(("looking for %s", classname));
935 TAILQ_FOREACH(dc, &devclasses, link) {
936 if (!strcmp(dc->name, classname))
941 PDEBUG(("creating %s", classname));
942 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
943 M_BUS, M_NOWAIT | M_ZERO);
947 dc->name = (char*) (dc + 1);
948 strcpy(dc->name, classname);
949 TAILQ_INIT(&dc->drivers);
950 TAILQ_INSERT_TAIL(&devclasses, dc, link);
952 bus_data_generation_update();
956 * If a parent class is specified, then set that as our parent so
957 * that this devclass will support drivers for the parent class as
958 * well. If the parent class has the same name don't do this though
959 * as it creates a cycle that can trigger an infinite loop in
960 * device_probe_child() if a device exists for which there is no
963 if (parentname && dc && !dc->parent &&
964 strcmp(classname, parentname) != 0) {
965 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
966 dc->parent->flags |= DC_HAS_CHILDREN;
973 * @brief Create a device class
975 * If a device class with the name @p classname exists, return it,
976 * otherwise create and return a new device class.
978 * @param classname the devclass name to find or create
981 devclass_create(const char *classname)
983 return (devclass_find_internal(classname, NULL, TRUE));
987 * @brief Find a device class
989 * If a device class with the name @p classname exists, return it,
990 * otherwise return @c NULL.
992 * @param classname the devclass name to find
995 devclass_find(const char *classname)
997 return (devclass_find_internal(classname, NULL, FALSE));
1001 * @brief Register that a device driver has been added to a devclass
1003 * Register that a device driver has been added to a devclass. This
1004 * is called by devclass_add_driver to accomplish the recursive
1005 * notification of all the children classes of dc, as well as dc.
1006 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1009 * We do a full search here of the devclass list at each iteration
1010 * level to save storing children-lists in the devclass structure. If
1011 * we ever move beyond a few dozen devices doing this, we may need to
1014 * @param dc the devclass to edit
1015 * @param driver the driver that was just added
1018 devclass_driver_added(devclass_t dc, driver_t *driver)
1024 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1026 for (i = 0; i < dc->maxunit; i++)
1027 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1028 BUS_DRIVER_ADDED(dc->devices[i], driver);
1031 * Walk through the children classes. Since we only keep a
1032 * single parent pointer around, we walk the entire list of
1033 * devclasses looking for children. We set the
1034 * DC_HAS_CHILDREN flag when a child devclass is created on
1035 * the parent, so we only walk the list for those devclasses
1036 * that have children.
1038 if (!(dc->flags & DC_HAS_CHILDREN))
1041 TAILQ_FOREACH(dc, &devclasses, link) {
1042 if (dc->parent == parent)
1043 devclass_driver_added(dc, driver);
1048 * @brief Add a device driver to a device class
1050 * Add a device driver to a devclass. This is normally called
1051 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1052 * all devices in the devclass will be called to allow them to attempt
1053 * to re-probe any unmatched children.
1055 * @param dc the devclass to edit
1056 * @param driver the driver to register
1059 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1062 const char *parentname;
1064 PDEBUG(("%s", DRIVERNAME(driver)));
1066 /* Don't allow invalid pass values. */
1067 if (pass <= BUS_PASS_ROOT)
1070 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1075 * Compile the driver's methods. Also increase the reference count
1076 * so that the class doesn't get freed when the last instance
1077 * goes. This means we can safely use static methods and avoids a
1078 * double-free in devclass_delete_driver.
1080 kobj_class_compile((kobj_class_t) driver);
1083 * If the driver has any base classes, make the
1084 * devclass inherit from the devclass of the driver's
1085 * first base class. This will allow the system to
1086 * search for drivers in both devclasses for children
1087 * of a device using this driver.
1089 if (driver->baseclasses)
1090 parentname = driver->baseclasses[0]->name;
1093 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1095 dl->driver = driver;
1096 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1097 driver->refs++; /* XXX: kobj_mtx */
1099 driver_register_pass(dl);
1101 devclass_driver_added(dc, driver);
1102 bus_data_generation_update();
1107 * @brief Register that a device driver has been deleted from a devclass
1109 * Register that a device driver has been removed from a devclass.
1110 * This is called by devclass_delete_driver to accomplish the
1111 * recursive notification of all the children classes of busclass, as
1112 * well as busclass. Each layer will attempt to detach the driver
1113 * from any devices that are children of the bus's devclass. The function
1114 * will return an error if a device fails to detach.
1116 * We do a full search here of the devclass list at each iteration
1117 * level to save storing children-lists in the devclass structure. If
1118 * we ever move beyond a few dozen devices doing this, we may need to
1121 * @param busclass the devclass of the parent bus
1122 * @param dc the devclass of the driver being deleted
1123 * @param driver the driver being deleted
1126 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1133 * Disassociate from any devices. We iterate through all the
1134 * devices in the devclass of the driver and detach any which are
1135 * using the driver and which have a parent in the devclass which
1136 * we are deleting from.
1138 * Note that since a driver can be in multiple devclasses, we
1139 * should not detach devices which are not children of devices in
1140 * the affected devclass.
1142 for (i = 0; i < dc->maxunit; i++) {
1143 if (dc->devices[i]) {
1144 dev = dc->devices[i];
1145 if (dev->driver == driver && dev->parent &&
1146 dev->parent->devclass == busclass) {
1147 if ((error = device_detach(dev)) != 0)
1149 BUS_PROBE_NOMATCH(dev->parent, dev);
1151 dev->flags |= DF_DONENOMATCH;
1157 * Walk through the children classes. Since we only keep a
1158 * single parent pointer around, we walk the entire list of
1159 * devclasses looking for children. We set the
1160 * DC_HAS_CHILDREN flag when a child devclass is created on
1161 * the parent, so we only walk the list for those devclasses
1162 * that have children.
1164 if (!(busclass->flags & DC_HAS_CHILDREN))
1167 TAILQ_FOREACH(busclass, &devclasses, link) {
1168 if (busclass->parent == parent) {
1169 error = devclass_driver_deleted(busclass, dc, driver);
1178 * @brief Delete a device driver from a device class
1180 * Delete a device driver from a devclass. This is normally called
1181 * automatically by DRIVER_MODULE().
1183 * If the driver is currently attached to any devices,
1184 * devclass_delete_driver() will first attempt to detach from each
1185 * device. If one of the detach calls fails, the driver will not be
1188 * @param dc the devclass to edit
1189 * @param driver the driver to unregister
1192 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1194 devclass_t dc = devclass_find(driver->name);
1198 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1204 * Find the link structure in the bus' list of drivers.
1206 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1207 if (dl->driver == driver)
1212 PDEBUG(("%s not found in %s list", driver->name,
1217 error = devclass_driver_deleted(busclass, dc, driver);
1221 TAILQ_REMOVE(&busclass->drivers, dl, link);
1226 if (driver->refs == 0)
1227 kobj_class_free((kobj_class_t) driver);
1229 bus_data_generation_update();
1234 * @brief Quiesces a set of device drivers from a device class
1236 * Quiesce a device driver from a devclass. This is normally called
1237 * automatically by DRIVER_MODULE().
1239 * If the driver is currently attached to any devices,
1240 * devclass_quiesece_driver() will first attempt to quiesce each
1243 * @param dc the devclass to edit
1244 * @param driver the driver to unregister
1247 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1249 devclass_t dc = devclass_find(driver->name);
1255 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1261 * Find the link structure in the bus' list of drivers.
1263 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1264 if (dl->driver == driver)
1269 PDEBUG(("%s not found in %s list", driver->name,
1275 * Quiesce all devices. We iterate through all the devices in
1276 * the devclass of the driver and quiesce any which are using
1277 * the driver and which have a parent in the devclass which we
1280 * Note that since a driver can be in multiple devclasses, we
1281 * should not quiesce devices which are not children of
1282 * devices in the affected devclass.
1284 for (i = 0; i < dc->maxunit; i++) {
1285 if (dc->devices[i]) {
1286 dev = dc->devices[i];
1287 if (dev->driver == driver && dev->parent &&
1288 dev->parent->devclass == busclass) {
1289 if ((error = device_quiesce(dev)) != 0)
1302 devclass_find_driver_internal(devclass_t dc, const char *classname)
1306 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1308 TAILQ_FOREACH(dl, &dc->drivers, link) {
1309 if (!strcmp(dl->driver->name, classname))
1313 PDEBUG(("not found"));
1318 * @brief Return the name of the devclass
1321 devclass_get_name(devclass_t dc)
1327 * @brief Find a device given a unit number
1329 * @param dc the devclass to search
1330 * @param unit the unit number to search for
1332 * @returns the device with the given unit number or @c
1333 * NULL if there is no such device
1336 devclass_get_device(devclass_t dc, int unit)
1338 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1340 return (dc->devices[unit]);
1344 * @brief Find the softc field of a device given a unit number
1346 * @param dc the devclass to search
1347 * @param unit the unit number to search for
1349 * @returns the softc field of the device with the given
1350 * unit number or @c NULL if there is no such
1354 devclass_get_softc(devclass_t dc, int unit)
1358 dev = devclass_get_device(dc, unit);
1362 return (device_get_softc(dev));
1366 * @brief Get a list of devices in the devclass
1368 * An array containing a list of all the devices in the given devclass
1369 * is allocated and returned in @p *devlistp. The number of devices
1370 * in the array is returned in @p *devcountp. The caller should free
1371 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1373 * @param dc the devclass to examine
1374 * @param devlistp points at location for array pointer return
1376 * @param devcountp points at location for array size return value
1379 * @retval ENOMEM the array allocation failed
1382 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1387 count = devclass_get_count(dc);
1388 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1393 for (i = 0; i < dc->maxunit; i++) {
1394 if (dc->devices[i]) {
1395 list[count] = dc->devices[i];
1407 * @brief Get a list of drivers in the devclass
1409 * An array containing a list of pointers to all the drivers in the
1410 * given devclass is allocated and returned in @p *listp. The number
1411 * of drivers in the array is returned in @p *countp. The caller should
1412 * free the array using @c free(p, M_TEMP).
1414 * @param dc the devclass to examine
1415 * @param listp gives location for array pointer return value
1416 * @param countp gives location for number of array elements
1420 * @retval ENOMEM the array allocation failed
1423 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1430 TAILQ_FOREACH(dl, &dc->drivers, link)
1432 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1437 TAILQ_FOREACH(dl, &dc->drivers, link) {
1438 list[count] = dl->driver;
1448 * @brief Get the number of devices in a devclass
1450 * @param dc the devclass to examine
1453 devclass_get_count(devclass_t dc)
1458 for (i = 0; i < dc->maxunit; i++)
1465 * @brief Get the maximum unit number used in a devclass
1467 * Note that this is one greater than the highest currently-allocated
1468 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1469 * that not even the devclass has been allocated yet.
1471 * @param dc the devclass to examine
1474 devclass_get_maxunit(devclass_t dc)
1478 return (dc->maxunit);
1482 * @brief Find a free unit number in a devclass
1484 * This function searches for the first unused unit number greater
1485 * that or equal to @p unit.
1487 * @param dc the devclass to examine
1488 * @param unit the first unit number to check
1491 devclass_find_free_unit(devclass_t dc, int unit)
1495 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1501 * @brief Set the parent of a devclass
1503 * The parent class is normally initialised automatically by
1506 * @param dc the devclass to edit
1507 * @param pdc the new parent devclass
1510 devclass_set_parent(devclass_t dc, devclass_t pdc)
1516 * @brief Get the parent of a devclass
1518 * @param dc the devclass to examine
1521 devclass_get_parent(devclass_t dc)
1523 return (dc->parent);
1526 struct sysctl_ctx_list *
1527 devclass_get_sysctl_ctx(devclass_t dc)
1529 return (&dc->sysctl_ctx);
1533 devclass_get_sysctl_tree(devclass_t dc)
1535 return (dc->sysctl_tree);
1540 * @brief Allocate a unit number
1542 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1543 * will do). The allocated unit number is returned in @p *unitp.
1545 * @param dc the devclass to allocate from
1546 * @param unitp points at the location for the allocated unit
1550 * @retval EEXIST the requested unit number is already allocated
1551 * @retval ENOMEM memory allocation failure
1554 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1559 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1561 /* Ask the parent bus if it wants to wire this device. */
1563 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1566 /* If we were given a wired unit number, check for existing device */
1569 if (unit >= 0 && unit < dc->maxunit &&
1570 dc->devices[unit] != NULL) {
1572 printf("%s: %s%d already exists; skipping it\n",
1573 dc->name, dc->name, *unitp);
1577 /* Unwired device, find the next available slot for it */
1579 for (unit = 0;; unit++) {
1580 /* If there is an "at" hint for a unit then skip it. */
1581 if (resource_string_value(dc->name, unit, "at", &s) ==
1585 /* If this device slot is already in use, skip it. */
1586 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1594 * We've selected a unit beyond the length of the table, so let's
1595 * extend the table to make room for all units up to and including
1598 if (unit >= dc->maxunit) {
1599 device_t *newlist, *oldlist;
1602 oldlist = dc->devices;
1603 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1604 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1607 if (oldlist != NULL)
1608 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1609 bzero(newlist + dc->maxunit,
1610 sizeof(device_t) * (newsize - dc->maxunit));
1611 dc->devices = newlist;
1612 dc->maxunit = newsize;
1613 if (oldlist != NULL)
1614 free(oldlist, M_BUS);
1616 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1624 * @brief Add a device to a devclass
1626 * A unit number is allocated for the device (using the device's
1627 * preferred unit number if any) and the device is registered in the
1628 * devclass. This allows the device to be looked up by its unit
1629 * number, e.g. by decoding a dev_t minor number.
1631 * @param dc the devclass to add to
1632 * @param dev the device to add
1635 * @retval EEXIST the requested unit number is already allocated
1636 * @retval ENOMEM memory allocation failure
1639 devclass_add_device(devclass_t dc, device_t dev)
1643 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1645 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1648 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1652 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1653 free(dev->nameunit, M_BUS);
1654 dev->nameunit = NULL;
1657 dc->devices[dev->unit] = dev;
1659 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1666 * @brief Delete a device from a devclass
1668 * The device is removed from the devclass's device list and its unit
1671 * @param dc the devclass to delete from
1672 * @param dev the device to delete
1677 devclass_delete_device(devclass_t dc, device_t dev)
1682 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1684 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1685 panic("devclass_delete_device: inconsistent device class");
1686 dc->devices[dev->unit] = NULL;
1687 if (dev->flags & DF_WILDCARD)
1689 dev->devclass = NULL;
1690 free(dev->nameunit, M_BUS);
1691 dev->nameunit = NULL;
1698 * @brief Make a new device and add it as a child of @p parent
1700 * @param parent the parent of the new device
1701 * @param name the devclass name of the new device or @c NULL
1702 * to leave the devclass unspecified
1703 * @parem unit the unit number of the new device of @c -1 to
1704 * leave the unit number unspecified
1706 * @returns the new device
1709 make_device(device_t parent, const char *name, int unit)
1714 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1717 dc = devclass_find_internal(name, NULL, TRUE);
1719 printf("make_device: can't find device class %s\n",
1727 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1731 dev->parent = parent;
1732 TAILQ_INIT(&dev->children);
1733 kobj_init((kobj_t) dev, &null_class);
1735 dev->devclass = NULL;
1737 dev->nameunit = NULL;
1741 dev->flags = DF_ENABLED;
1744 dev->flags |= DF_WILDCARD;
1746 dev->flags |= DF_FIXEDCLASS;
1747 if (devclass_add_device(dc, dev)) {
1748 kobj_delete((kobj_t) dev, M_BUS);
1755 dev->state = DS_NOTPRESENT;
1757 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1758 bus_data_generation_update();
1765 * @brief Print a description of a device.
1768 device_print_child(device_t dev, device_t child)
1772 if (device_is_alive(child))
1773 retval += BUS_PRINT_CHILD(dev, child);
1775 retval += device_printf(child, " not found\n");
1781 * @brief Create a new device
1783 * This creates a new device and adds it as a child of an existing
1784 * parent device. The new device will be added after the last existing
1785 * child with order zero.
1787 * @param dev the device which will be the parent of the
1789 * @param name devclass name for new device or @c NULL if not
1791 * @param unit unit number for new device or @c -1 if not
1794 * @returns the new device
1797 device_add_child(device_t dev, const char *name, int unit)
1799 return (device_add_child_ordered(dev, 0, name, unit));
1803 * @brief Create a new device
1805 * This creates a new device and adds it as a child of an existing
1806 * parent device. The new device will be added after the last existing
1807 * child with the same order.
1809 * @param dev the device which will be the parent of the
1811 * @param order a value which is used to partially sort the
1812 * children of @p dev - devices created using
1813 * lower values of @p order appear first in @p
1814 * dev's list of children
1815 * @param name devclass name for new device or @c NULL if not
1817 * @param unit unit number for new device or @c -1 if not
1820 * @returns the new device
1823 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1828 PDEBUG(("%s at %s with order %u as unit %d",
1829 name, DEVICENAME(dev), order, unit));
1830 KASSERT(name != NULL || unit == -1,
1831 ("child device with wildcard name and specific unit number"));
1833 child = make_device(dev, name, unit);
1836 child->order = order;
1838 TAILQ_FOREACH(place, &dev->children, link) {
1839 if (place->order > order)
1845 * The device 'place' is the first device whose order is
1846 * greater than the new child.
1848 TAILQ_INSERT_BEFORE(place, child, link);
1851 * The new child's order is greater or equal to the order of
1852 * any existing device. Add the child to the tail of the list.
1854 TAILQ_INSERT_TAIL(&dev->children, child, link);
1857 bus_data_generation_update();
1862 * @brief Delete a device
1864 * This function deletes a device along with all of its children. If
1865 * the device currently has a driver attached to it, the device is
1866 * detached first using device_detach().
1868 * @param dev the parent device
1869 * @param child the device to delete
1872 * @retval non-zero a unit error code describing the error
1875 device_delete_child(device_t dev, device_t child)
1878 device_t grandchild;
1880 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1882 /* remove children first */
1883 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1884 error = device_delete_child(child, grandchild);
1889 if ((error = device_detach(child)) != 0)
1891 if (child->devclass)
1892 devclass_delete_device(child->devclass, child);
1894 BUS_CHILD_DELETED(dev, child);
1895 TAILQ_REMOVE(&dev->children, child, link);
1896 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1897 kobj_delete((kobj_t) child, M_BUS);
1899 bus_data_generation_update();
1904 * @brief Delete all children devices of the given device, if any.
1906 * This function deletes all children devices of the given device, if
1907 * any, using the device_delete_child() function for each device it
1908 * finds. If a child device cannot be deleted, this function will
1909 * return an error code.
1911 * @param dev the parent device
1914 * @retval non-zero a device would not detach
1917 device_delete_children(device_t dev)
1922 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1926 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1927 error = device_delete_child(dev, child);
1929 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1937 * @brief Find a device given a unit number
1939 * This is similar to devclass_get_devices() but only searches for
1940 * devices which have @p dev as a parent.
1942 * @param dev the parent device to search
1943 * @param unit the unit number to search for. If the unit is -1,
1944 * return the first child of @p dev which has name
1945 * @p classname (that is, the one with the lowest unit.)
1947 * @returns the device with the given unit number or @c
1948 * NULL if there is no such device
1951 device_find_child(device_t dev, const char *classname, int unit)
1956 dc = devclass_find(classname);
1961 child = devclass_get_device(dc, unit);
1962 if (child && child->parent == dev)
1965 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1966 child = devclass_get_device(dc, unit);
1967 if (child && child->parent == dev)
1978 first_matching_driver(devclass_t dc, device_t dev)
1981 return (devclass_find_driver_internal(dc, dev->devclass->name));
1982 return (TAILQ_FIRST(&dc->drivers));
1989 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1991 if (dev->devclass) {
1993 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1994 if (!strcmp(dev->devclass->name, dl->driver->name))
1998 return (TAILQ_NEXT(last, link));
2005 device_probe_child(device_t dev, device_t child)
2008 driverlink_t best = NULL;
2010 int result, pri = 0;
2011 int hasclass = (child->devclass != NULL);
2017 panic("device_probe_child: parent device has no devclass");
2020 * If the state is already probed, then return. However, don't
2021 * return if we can rebid this object.
2023 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2026 for (; dc; dc = dc->parent) {
2027 for (dl = first_matching_driver(dc, child);
2029 dl = next_matching_driver(dc, child, dl)) {
2030 /* If this driver's pass is too high, then ignore it. */
2031 if (dl->pass > bus_current_pass)
2034 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2035 result = device_set_driver(child, dl->driver);
2036 if (result == ENOMEM)
2038 else if (result != 0)
2041 if (device_set_devclass(child,
2042 dl->driver->name) != 0) {
2043 char const * devname =
2044 device_get_name(child);
2045 if (devname == NULL)
2046 devname = "(unknown)";
2047 printf("driver bug: Unable to set "
2048 "devclass (class: %s "
2052 (void)device_set_driver(child, NULL);
2057 /* Fetch any flags for the device before probing. */
2058 resource_int_value(dl->driver->name, child->unit,
2059 "flags", &child->devflags);
2061 result = DEVICE_PROBE(child);
2063 /* Reset flags and devclass before the next probe. */
2064 child->devflags = 0;
2066 (void)device_set_devclass(child, NULL);
2069 * If the driver returns SUCCESS, there can be
2070 * no higher match for this device.
2079 * The driver returned an error so it
2080 * certainly doesn't match.
2083 (void)device_set_driver(child, NULL);
2088 * A priority lower than SUCCESS, remember the
2089 * best matching driver. Initialise the value
2090 * of pri for the first match.
2092 if (best == NULL || result > pri) {
2094 * Probes that return BUS_PROBE_NOWILDCARD
2095 * or lower only match on devices whose
2096 * driver was explicitly specified.
2098 if (result <= BUS_PROBE_NOWILDCARD &&
2099 !(child->flags & DF_FIXEDCLASS))
2107 * If we have an unambiguous match in this devclass,
2108 * don't look in the parent.
2110 if (best && pri == 0)
2115 * If we found a driver, change state and initialise the devclass.
2117 /* XXX What happens if we rebid and got no best? */
2120 * If this device was attached, and we were asked to
2121 * rescan, and it is a different driver, then we have
2122 * to detach the old driver and reattach this new one.
2123 * Note, we don't have to check for DF_REBID here
2124 * because if the state is > DS_ALIVE, we know it must
2127 * This assumes that all DF_REBID drivers can have
2128 * their probe routine called at any time and that
2129 * they are idempotent as well as completely benign in
2130 * normal operations.
2132 * We also have to make sure that the detach
2133 * succeeded, otherwise we fail the operation (or
2134 * maybe it should just fail silently? I'm torn).
2136 if (child->state > DS_ALIVE && best->driver != child->driver)
2137 if ((result = device_detach(dev)) != 0)
2140 /* Set the winning driver, devclass, and flags. */
2141 if (!child->devclass) {
2142 result = device_set_devclass(child, best->driver->name);
2146 result = device_set_driver(child, best->driver);
2149 resource_int_value(best->driver->name, child->unit,
2150 "flags", &child->devflags);
2154 * A bit bogus. Call the probe method again to make
2155 * sure that we have the right description.
2157 DEVICE_PROBE(child);
2159 child->flags |= DF_REBID;
2162 child->flags &= ~DF_REBID;
2163 child->state = DS_ALIVE;
2165 bus_data_generation_update();
2173 * @brief Return the parent of a device
2176 device_get_parent(device_t dev)
2178 return (dev->parent);
2182 * @brief Get a list of children of a device
2184 * An array containing a list of all the children of the given device
2185 * is allocated and returned in @p *devlistp. The number of devices
2186 * in the array is returned in @p *devcountp. The caller should free
2187 * the array using @c free(p, M_TEMP).
2189 * @param dev the device to examine
2190 * @param devlistp points at location for array pointer return
2192 * @param devcountp points at location for array size return value
2195 * @retval ENOMEM the array allocation failed
2198 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2205 TAILQ_FOREACH(child, &dev->children, link) {
2214 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2219 TAILQ_FOREACH(child, &dev->children, link) {
2220 list[count] = child;
2231 * @brief Return the current driver for the device or @c NULL if there
2232 * is no driver currently attached
2235 device_get_driver(device_t dev)
2237 return (dev->driver);
2241 * @brief Return the current devclass for the device or @c NULL if
2245 device_get_devclass(device_t dev)
2247 return (dev->devclass);
2251 * @brief Return the name of the device's devclass or @c NULL if there
2255 device_get_name(device_t dev)
2257 if (dev != NULL && dev->devclass)
2258 return (devclass_get_name(dev->devclass));
2263 * @brief Return a string containing the device's devclass name
2264 * followed by an ascii representation of the device's unit number
2268 device_get_nameunit(device_t dev)
2270 return (dev->nameunit);
2274 * @brief Return the device's unit number.
2277 device_get_unit(device_t dev)
2283 * @brief Return the device's description string
2286 device_get_desc(device_t dev)
2292 * @brief Return the device's flags
2295 device_get_flags(device_t dev)
2297 return (dev->devflags);
2300 struct sysctl_ctx_list *
2301 device_get_sysctl_ctx(device_t dev)
2303 return (&dev->sysctl_ctx);
2307 device_get_sysctl_tree(device_t dev)
2309 return (dev->sysctl_tree);
2313 * @brief Print the name of the device followed by a colon and a space
2315 * @returns the number of characters printed
2318 device_print_prettyname(device_t dev)
2320 const char *name = device_get_name(dev);
2323 return (printf("unknown: "));
2324 return (printf("%s%d: ", name, device_get_unit(dev)));
2328 * @brief Print the name of the device followed by a colon, a space
2329 * and the result of calling vprintf() with the value of @p fmt and
2330 * the following arguments.
2332 * @returns the number of characters printed
2335 device_printf(device_t dev, const char * fmt, ...)
2340 retval = device_print_prettyname(dev);
2342 retval += vprintf(fmt, ap);
2351 device_set_desc_internal(device_t dev, const char* desc, int copy)
2353 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2354 free(dev->desc, M_BUS);
2355 dev->flags &= ~DF_DESCMALLOCED;
2360 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2362 strcpy(dev->desc, desc);
2363 dev->flags |= DF_DESCMALLOCED;
2366 /* Avoid a -Wcast-qual warning */
2367 dev->desc = (char *)(uintptr_t) desc;
2370 bus_data_generation_update();
2374 * @brief Set the device's description
2376 * The value of @c desc should be a string constant that will not
2377 * change (at least until the description is changed in a subsequent
2378 * call to device_set_desc() or device_set_desc_copy()).
2381 device_set_desc(device_t dev, const char* desc)
2383 device_set_desc_internal(dev, desc, FALSE);
2387 * @brief Set the device's description
2389 * The string pointed to by @c desc is copied. Use this function if
2390 * the device description is generated, (e.g. with sprintf()).
2393 device_set_desc_copy(device_t dev, const char* desc)
2395 device_set_desc_internal(dev, desc, TRUE);
2399 * @brief Set the device's flags
2402 device_set_flags(device_t dev, uint32_t flags)
2404 dev->devflags = flags;
2408 * @brief Return the device's softc field
2410 * The softc is allocated and zeroed when a driver is attached, based
2411 * on the size field of the driver.
2414 device_get_softc(device_t dev)
2416 return (dev->softc);
2420 * @brief Set the device's softc field
2422 * Most drivers do not need to use this since the softc is allocated
2423 * automatically when the driver is attached.
2426 device_set_softc(device_t dev, void *softc)
2428 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2429 free(dev->softc, M_BUS_SC);
2432 dev->flags |= DF_EXTERNALSOFTC;
2434 dev->flags &= ~DF_EXTERNALSOFTC;
2438 * @brief Free claimed softc
2440 * Most drivers do not need to use this since the softc is freed
2441 * automatically when the driver is detached.
2444 device_free_softc(void *softc)
2446 free(softc, M_BUS_SC);
2450 * @brief Claim softc
2452 * This function can be used to let the driver free the automatically
2453 * allocated softc using "device_free_softc()". This function is
2454 * useful when the driver is refcounting the softc and the softc
2455 * cannot be freed when the "device_detach" method is called.
2458 device_claim_softc(device_t dev)
2461 dev->flags |= DF_EXTERNALSOFTC;
2463 dev->flags &= ~DF_EXTERNALSOFTC;
2467 * @brief Get the device's ivars field
2469 * The ivars field is used by the parent device to store per-device
2470 * state (e.g. the physical location of the device or a list of
2474 device_get_ivars(device_t dev)
2477 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2478 return (dev->ivars);
2482 * @brief Set the device's ivars field
2485 device_set_ivars(device_t dev, void * ivars)
2488 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2493 * @brief Return the device's state
2496 device_get_state(device_t dev)
2498 return (dev->state);
2502 * @brief Set the DF_ENABLED flag for the device
2505 device_enable(device_t dev)
2507 dev->flags |= DF_ENABLED;
2511 * @brief Clear the DF_ENABLED flag for the device
2514 device_disable(device_t dev)
2516 dev->flags &= ~DF_ENABLED;
2520 * @brief Increment the busy counter for the device
2523 device_busy(device_t dev)
2525 if (dev->state < DS_ATTACHING)
2526 panic("device_busy: called for unattached device");
2527 if (dev->busy == 0 && dev->parent)
2528 device_busy(dev->parent);
2530 if (dev->state == DS_ATTACHED)
2531 dev->state = DS_BUSY;
2535 * @brief Decrement the busy counter for the device
2538 device_unbusy(device_t dev)
2540 if (dev->busy != 0 && dev->state != DS_BUSY &&
2541 dev->state != DS_ATTACHING)
2542 panic("device_unbusy: called for non-busy device %s",
2543 device_get_nameunit(dev));
2545 if (dev->busy == 0) {
2547 device_unbusy(dev->parent);
2548 if (dev->state == DS_BUSY)
2549 dev->state = DS_ATTACHED;
2554 * @brief Set the DF_QUIET flag for the device
2557 device_quiet(device_t dev)
2559 dev->flags |= DF_QUIET;
2563 * @brief Clear the DF_QUIET flag for the device
2566 device_verbose(device_t dev)
2568 dev->flags &= ~DF_QUIET;
2572 * @brief Return non-zero if the DF_QUIET flag is set on the device
2575 device_is_quiet(device_t dev)
2577 return ((dev->flags & DF_QUIET) != 0);
2581 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2584 device_is_enabled(device_t dev)
2586 return ((dev->flags & DF_ENABLED) != 0);
2590 * @brief Return non-zero if the device was successfully probed
2593 device_is_alive(device_t dev)
2595 return (dev->state >= DS_ALIVE);
2599 * @brief Return non-zero if the device currently has a driver
2603 device_is_attached(device_t dev)
2605 return (dev->state >= DS_ATTACHED);
2609 * @brief Set the devclass of a device
2610 * @see devclass_add_device().
2613 device_set_devclass(device_t dev, const char *classname)
2620 devclass_delete_device(dev->devclass, dev);
2624 if (dev->devclass) {
2625 printf("device_set_devclass: device class already set\n");
2629 dc = devclass_find_internal(classname, NULL, TRUE);
2633 error = devclass_add_device(dc, dev);
2635 bus_data_generation_update();
2640 * @brief Set the driver of a device
2643 * @retval EBUSY the device already has a driver attached
2644 * @retval ENOMEM a memory allocation failure occurred
2647 device_set_driver(device_t dev, driver_t *driver)
2649 if (dev->state >= DS_ATTACHED)
2652 if (dev->driver == driver)
2655 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2656 free(dev->softc, M_BUS_SC);
2659 device_set_desc(dev, NULL);
2660 kobj_delete((kobj_t) dev, NULL);
2661 dev->driver = driver;
2663 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2664 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2665 dev->softc = malloc(driver->size, M_BUS_SC,
2668 kobj_delete((kobj_t) dev, NULL);
2669 kobj_init((kobj_t) dev, &null_class);
2675 kobj_init((kobj_t) dev, &null_class);
2678 bus_data_generation_update();
2683 * @brief Probe a device, and return this status.
2685 * This function is the core of the device autoconfiguration
2686 * system. Its purpose is to select a suitable driver for a device and
2687 * then call that driver to initialise the hardware appropriately. The
2688 * driver is selected by calling the DEVICE_PROBE() method of a set of
2689 * candidate drivers and then choosing the driver which returned the
2690 * best value. This driver is then attached to the device using
2693 * The set of suitable drivers is taken from the list of drivers in
2694 * the parent device's devclass. If the device was originally created
2695 * with a specific class name (see device_add_child()), only drivers
2696 * with that name are probed, otherwise all drivers in the devclass
2697 * are probed. If no drivers return successful probe values in the
2698 * parent devclass, the search continues in the parent of that
2699 * devclass (see devclass_get_parent()) if any.
2701 * @param dev the device to initialise
2704 * @retval ENXIO no driver was found
2705 * @retval ENOMEM memory allocation failure
2706 * @retval non-zero some other unix error code
2707 * @retval -1 Device already attached
2710 device_probe(device_t dev)
2716 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2719 if (!(dev->flags & DF_ENABLED)) {
2720 if (bootverbose && device_get_name(dev) != NULL) {
2721 device_print_prettyname(dev);
2722 printf("not probed (disabled)\n");
2726 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2727 if (bus_current_pass == BUS_PASS_DEFAULT &&
2728 !(dev->flags & DF_DONENOMATCH)) {
2729 BUS_PROBE_NOMATCH(dev->parent, dev);
2731 dev->flags |= DF_DONENOMATCH;
2739 * @brief Probe a device and attach a driver if possible
2741 * calls device_probe() and attaches if that was successful.
2744 device_probe_and_attach(device_t dev)
2750 error = device_probe(dev);
2753 else if (error != 0)
2756 CURVNET_SET_QUIET(vnet0);
2757 error = device_attach(dev);
2763 * @brief Attach a device driver to a device
2765 * This function is a wrapper around the DEVICE_ATTACH() driver
2766 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2767 * device's sysctl tree, optionally prints a description of the device
2768 * and queues a notification event for user-based device management
2771 * Normally this function is only called internally from
2772 * device_probe_and_attach().
2774 * @param dev the device to initialise
2777 * @retval ENXIO no driver was found
2778 * @retval ENOMEM memory allocation failure
2779 * @retval non-zero some other unix error code
2782 device_attach(device_t dev)
2784 uint64_t attachtime;
2787 if (resource_disabled(dev->driver->name, dev->unit)) {
2788 device_disable(dev);
2790 device_printf(dev, "disabled via hints entry\n");
2794 device_sysctl_init(dev);
2795 if (!device_is_quiet(dev))
2796 device_print_child(dev->parent, dev);
2797 attachtime = get_cyclecount();
2798 dev->state = DS_ATTACHING;
2799 if ((error = DEVICE_ATTACH(dev)) != 0) {
2800 printf("device_attach: %s%d attach returned %d\n",
2801 dev->driver->name, dev->unit, error);
2802 if (!(dev->flags & DF_FIXEDCLASS))
2803 devclass_delete_device(dev->devclass, dev);
2804 (void)device_set_driver(dev, NULL);
2805 device_sysctl_fini(dev);
2806 KASSERT(dev->busy == 0, ("attach failed but busy"));
2807 dev->state = DS_NOTPRESENT;
2810 attachtime = get_cyclecount() - attachtime;
2812 * 4 bits per device is a reasonable value for desktop and server
2813 * hardware with good get_cyclecount() implementations, but may
2814 * need to be adjusted on other platforms.
2817 printf("%s(): feeding %d bit(s) of entropy from %s%d\n",
2818 __func__, 4, dev->driver->name, dev->unit);
2820 random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH);
2821 device_sysctl_update(dev);
2823 dev->state = DS_BUSY;
2825 dev->state = DS_ATTACHED;
2826 dev->flags &= ~DF_DONENOMATCH;
2832 * @brief Detach a driver from a device
2834 * This function is a wrapper around the DEVICE_DETACH() driver
2835 * method. If the call to DEVICE_DETACH() succeeds, it calls
2836 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2837 * notification event for user-based device management services and
2838 * cleans up the device's sysctl tree.
2840 * @param dev the device to un-initialise
2843 * @retval ENXIO no driver was found
2844 * @retval ENOMEM memory allocation failure
2845 * @retval non-zero some other unix error code
2848 device_detach(device_t dev)
2854 PDEBUG(("%s", DEVICENAME(dev)));
2855 if (dev->state == DS_BUSY)
2857 if (dev->state != DS_ATTACHED)
2860 if ((error = DEVICE_DETACH(dev)) != 0)
2863 if (!device_is_quiet(dev))
2864 device_printf(dev, "detached\n");
2866 BUS_CHILD_DETACHED(dev->parent, dev);
2868 if (!(dev->flags & DF_FIXEDCLASS))
2869 devclass_delete_device(dev->devclass, dev);
2871 dev->state = DS_NOTPRESENT;
2872 (void)device_set_driver(dev, NULL);
2873 device_sysctl_fini(dev);
2879 * @brief Tells a driver to quiesce itself.
2881 * This function is a wrapper around the DEVICE_QUIESCE() driver
2882 * method. If the call to DEVICE_QUIESCE() succeeds.
2884 * @param dev the device to quiesce
2887 * @retval ENXIO no driver was found
2888 * @retval ENOMEM memory allocation failure
2889 * @retval non-zero some other unix error code
2892 device_quiesce(device_t dev)
2895 PDEBUG(("%s", DEVICENAME(dev)));
2896 if (dev->state == DS_BUSY)
2898 if (dev->state != DS_ATTACHED)
2901 return (DEVICE_QUIESCE(dev));
2905 * @brief Notify a device of system shutdown
2907 * This function calls the DEVICE_SHUTDOWN() driver method if the
2908 * device currently has an attached driver.
2910 * @returns the value returned by DEVICE_SHUTDOWN()
2913 device_shutdown(device_t dev)
2915 if (dev->state < DS_ATTACHED)
2917 return (DEVICE_SHUTDOWN(dev));
2921 * @brief Set the unit number of a device
2923 * This function can be used to override the unit number used for a
2924 * device (e.g. to wire a device to a pre-configured unit number).
2927 device_set_unit(device_t dev, int unit)
2932 dc = device_get_devclass(dev);
2933 if (unit < dc->maxunit && dc->devices[unit])
2935 err = devclass_delete_device(dc, dev);
2939 err = devclass_add_device(dc, dev);
2943 bus_data_generation_update();
2947 /*======================================*/
2949 * Some useful method implementations to make life easier for bus drivers.
2953 * @brief Initialise a resource list.
2955 * @param rl the resource list to initialise
2958 resource_list_init(struct resource_list *rl)
2964 * @brief Reclaim memory used by a resource list.
2966 * This function frees the memory for all resource entries on the list
2969 * @param rl the resource list to free
2972 resource_list_free(struct resource_list *rl)
2974 struct resource_list_entry *rle;
2976 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2978 panic("resource_list_free: resource entry is busy");
2979 STAILQ_REMOVE_HEAD(rl, link);
2985 * @brief Add a resource entry.
2987 * This function adds a resource entry using the given @p type, @p
2988 * start, @p end and @p count values. A rid value is chosen by
2989 * searching sequentially for the first unused rid starting at zero.
2991 * @param rl the resource list to edit
2992 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2993 * @param start the start address of the resource
2994 * @param end the end address of the resource
2995 * @param count XXX end-start+1
2998 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2999 u_long end, u_long count)
3004 while (resource_list_find(rl, type, rid) != NULL)
3006 resource_list_add(rl, type, rid, start, end, count);
3011 * @brief Add or modify a resource entry.
3013 * If an existing entry exists with the same type and rid, it will be
3014 * modified using the given values of @p start, @p end and @p
3015 * count. If no entry exists, a new one will be created using the
3016 * given values. The resource list entry that matches is then returned.
3018 * @param rl the resource list to edit
3019 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3020 * @param rid the resource identifier
3021 * @param start the start address of the resource
3022 * @param end the end address of the resource
3023 * @param count XXX end-start+1
3025 struct resource_list_entry *
3026 resource_list_add(struct resource_list *rl, int type, int rid,
3027 u_long start, u_long end, u_long count)
3029 struct resource_list_entry *rle;
3031 rle = resource_list_find(rl, type, rid);
3033 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3036 panic("resource_list_add: can't record entry");
3037 STAILQ_INSERT_TAIL(rl, rle, link);
3045 panic("resource_list_add: resource entry is busy");
3054 * @brief Determine if a resource entry is busy.
3056 * Returns true if a resource entry is busy meaning that it has an
3057 * associated resource that is not an unallocated "reserved" resource.
3059 * @param rl the resource list to search
3060 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3061 * @param rid the resource identifier
3063 * @returns Non-zero if the entry is busy, zero otherwise.
3066 resource_list_busy(struct resource_list *rl, int type, int rid)
3068 struct resource_list_entry *rle;
3070 rle = resource_list_find(rl, type, rid);
3071 if (rle == NULL || rle->res == NULL)
3073 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3074 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3075 ("reserved resource is active"));
3082 * @brief Determine if a resource entry is reserved.
3084 * Returns true if a resource entry is reserved meaning that it has an
3085 * associated "reserved" resource. The resource can either be
3086 * allocated or unallocated.
3088 * @param rl the resource list to search
3089 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3090 * @param rid the resource identifier
3092 * @returns Non-zero if the entry is reserved, zero otherwise.
3095 resource_list_reserved(struct resource_list *rl, int type, int rid)
3097 struct resource_list_entry *rle;
3099 rle = resource_list_find(rl, type, rid);
3100 if (rle != NULL && rle->flags & RLE_RESERVED)
3106 * @brief Find a resource entry by type and rid.
3108 * @param rl the resource list to search
3109 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3110 * @param rid the resource identifier
3112 * @returns the resource entry pointer or NULL if there is no such
3115 struct resource_list_entry *
3116 resource_list_find(struct resource_list *rl, int type, int rid)
3118 struct resource_list_entry *rle;
3120 STAILQ_FOREACH(rle, rl, link) {
3121 if (rle->type == type && rle->rid == rid)
3128 * @brief Delete a resource entry.
3130 * @param rl the resource list to edit
3131 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3132 * @param rid the resource identifier
3135 resource_list_delete(struct resource_list *rl, int type, int rid)
3137 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3140 if (rle->res != NULL)
3141 panic("resource_list_delete: resource has not been released");
3142 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3148 * @brief Allocate a reserved resource
3150 * This can be used by busses to force the allocation of resources
3151 * that are always active in the system even if they are not allocated
3152 * by a driver (e.g. PCI BARs). This function is usually called when
3153 * adding a new child to the bus. The resource is allocated from the
3154 * parent bus when it is reserved. The resource list entry is marked
3155 * with RLE_RESERVED to note that it is a reserved resource.
3157 * Subsequent attempts to allocate the resource with
3158 * resource_list_alloc() will succeed the first time and will set
3159 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3160 * resource that has been allocated is released with
3161 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3162 * the actual resource remains allocated. The resource can be released to
3163 * the parent bus by calling resource_list_unreserve().
3165 * @param rl the resource list to allocate from
3166 * @param bus the parent device of @p child
3167 * @param child the device for which the resource is being reserved
3168 * @param type the type of resource to allocate
3169 * @param rid a pointer to the resource identifier
3170 * @param start hint at the start of the resource range - pass
3171 * @c 0UL for any start address
3172 * @param end hint at the end of the resource range - pass
3173 * @c ~0UL for any end address
3174 * @param count hint at the size of range required - pass @c 1
3176 * @param flags any extra flags to control the resource
3177 * allocation - see @c RF_XXX flags in
3178 * <sys/rman.h> for details
3180 * @returns the resource which was allocated or @c NULL if no
3181 * resource could be allocated
3184 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3185 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3187 struct resource_list_entry *rle = NULL;
3188 int passthrough = (device_get_parent(child) != bus);
3193 "resource_list_reserve() should only be called for direct children");
3194 if (flags & RF_ACTIVE)
3196 "resource_list_reserve() should only reserve inactive resources");
3198 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3201 rle = resource_list_find(rl, type, *rid);
3202 rle->flags |= RLE_RESERVED;
3208 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3210 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3211 * and passing the allocation up to the parent of @p bus. This assumes
3212 * that the first entry of @c device_get_ivars(child) is a struct
3213 * resource_list. This also handles 'passthrough' allocations where a
3214 * child is a remote descendant of bus by passing the allocation up to
3215 * the parent of bus.
3217 * Typically, a bus driver would store a list of child resources
3218 * somewhere in the child device's ivars (see device_get_ivars()) and
3219 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3220 * then call resource_list_alloc() to perform the allocation.
3222 * @param rl the resource list to allocate from
3223 * @param bus the parent device of @p child
3224 * @param child the device which is requesting an allocation
3225 * @param type the type of resource to allocate
3226 * @param rid a pointer to the resource identifier
3227 * @param start hint at the start of the resource range - pass
3228 * @c 0UL for any start address
3229 * @param end hint at the end of the resource range - pass
3230 * @c ~0UL for any end address
3231 * @param count hint at the size of range required - pass @c 1
3233 * @param flags any extra flags to control the resource
3234 * allocation - see @c RF_XXX flags in
3235 * <sys/rman.h> for details
3237 * @returns the resource which was allocated or @c NULL if no
3238 * resource could be allocated
3241 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3242 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3244 struct resource_list_entry *rle = NULL;
3245 int passthrough = (device_get_parent(child) != bus);
3246 int isdefault = (start == 0UL && end == ~0UL);
3249 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3250 type, rid, start, end, count, flags));
3253 rle = resource_list_find(rl, type, *rid);
3256 return (NULL); /* no resource of that type/rid */
3259 if (rle->flags & RLE_RESERVED) {
3260 if (rle->flags & RLE_ALLOCATED)
3262 if ((flags & RF_ACTIVE) &&
3263 bus_activate_resource(child, type, *rid,
3266 rle->flags |= RLE_ALLOCATED;
3269 panic("resource_list_alloc: resource entry is busy");
3274 count = ulmax(count, rle->count);
3275 end = ulmax(rle->end, start + count - 1);
3278 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3279 type, rid, start, end, count, flags);
3282 * Record the new range.
3285 rle->start = rman_get_start(rle->res);
3286 rle->end = rman_get_end(rle->res);
3294 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3296 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3297 * used with resource_list_alloc().
3299 * @param rl the resource list which was allocated from
3300 * @param bus the parent device of @p child
3301 * @param child the device which is requesting a release
3302 * @param type the type of resource to release
3303 * @param rid the resource identifier
3304 * @param res the resource to release
3307 * @retval non-zero a standard unix error code indicating what
3308 * error condition prevented the operation
3311 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3312 int type, int rid, struct resource *res)
3314 struct resource_list_entry *rle = NULL;
3315 int passthrough = (device_get_parent(child) != bus);
3319 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3323 rle = resource_list_find(rl, type, rid);
3326 panic("resource_list_release: can't find resource");
3328 panic("resource_list_release: resource entry is not busy");
3329 if (rle->flags & RLE_RESERVED) {
3330 if (rle->flags & RLE_ALLOCATED) {
3331 if (rman_get_flags(res) & RF_ACTIVE) {
3332 error = bus_deactivate_resource(child, type,
3337 rle->flags &= ~RLE_ALLOCATED;
3343 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3353 * @brief Release all active resources of a given type
3355 * Release all active resources of a specified type. This is intended
3356 * to be used to cleanup resources leaked by a driver after detach or
3359 * @param rl the resource list which was allocated from
3360 * @param bus the parent device of @p child
3361 * @param child the device whose active resources are being released
3362 * @param type the type of resources to release
3365 * @retval EBUSY at least one resource was active
3368 resource_list_release_active(struct resource_list *rl, device_t bus,
3369 device_t child, int type)
3371 struct resource_list_entry *rle;
3375 STAILQ_FOREACH(rle, rl, link) {
3376 if (rle->type != type)
3378 if (rle->res == NULL)
3380 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3384 error = resource_list_release(rl, bus, child, type,
3385 rman_get_rid(rle->res), rle->res);
3388 "Failed to release active resource: %d\n", error);
3395 * @brief Fully release a reserved resource
3397 * Fully releases a resource reserved via resource_list_reserve().
3399 * @param rl the resource list which was allocated from
3400 * @param bus the parent device of @p child
3401 * @param child the device whose reserved resource is being released
3402 * @param type the type of resource to release
3403 * @param rid the resource identifier
3404 * @param res the resource to release
3407 * @retval non-zero a standard unix error code indicating what
3408 * error condition prevented the operation
3411 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3414 struct resource_list_entry *rle = NULL;
3415 int passthrough = (device_get_parent(child) != bus);
3419 "resource_list_unreserve() should only be called for direct children");
3421 rle = resource_list_find(rl, type, rid);
3424 panic("resource_list_unreserve: can't find resource");
3425 if (!(rle->flags & RLE_RESERVED))
3427 if (rle->flags & RLE_ALLOCATED)
3429 rle->flags &= ~RLE_RESERVED;
3430 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3434 * @brief Print a description of resources in a resource list
3436 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3437 * The name is printed if at least one resource of the given type is available.
3438 * The format is used to print resource start and end.
3440 * @param rl the resource list to print
3441 * @param name the name of @p type, e.g. @c "memory"
3442 * @param type type type of resource entry to print
3443 * @param format printf(9) format string to print resource
3444 * start and end values
3446 * @returns the number of characters printed
3449 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3452 struct resource_list_entry *rle;
3453 int printed, retval;
3457 /* Yes, this is kinda cheating */
3458 STAILQ_FOREACH(rle, rl, link) {
3459 if (rle->type == type) {
3461 retval += printf(" %s ", name);
3463 retval += printf(",");
3465 retval += printf(format, rle->start);
3466 if (rle->count > 1) {
3467 retval += printf("-");
3468 retval += printf(format, rle->start +
3477 * @brief Releases all the resources in a list.
3479 * @param rl The resource list to purge.
3484 resource_list_purge(struct resource_list *rl)
3486 struct resource_list_entry *rle;
3488 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3490 bus_release_resource(rman_get_device(rle->res),
3491 rle->type, rle->rid, rle->res);
3492 STAILQ_REMOVE_HEAD(rl, link);
3498 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3501 return (device_add_child_ordered(dev, order, name, unit));
3505 * @brief Helper function for implementing DEVICE_PROBE()
3507 * This function can be used to help implement the DEVICE_PROBE() for
3508 * a bus (i.e. a device which has other devices attached to it). It
3509 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3513 bus_generic_probe(device_t dev)
3515 devclass_t dc = dev->devclass;
3518 TAILQ_FOREACH(dl, &dc->drivers, link) {
3520 * If this driver's pass is too high, then ignore it.
3521 * For most drivers in the default pass, this will
3522 * never be true. For early-pass drivers they will
3523 * only call the identify routines of eligible drivers
3524 * when this routine is called. Drivers for later
3525 * passes should have their identify routines called
3526 * on early-pass busses during BUS_NEW_PASS().
3528 if (dl->pass > bus_current_pass)
3530 DEVICE_IDENTIFY(dl->driver, dev);
3537 * @brief Helper function for implementing DEVICE_ATTACH()
3539 * This function can be used to help implement the DEVICE_ATTACH() for
3540 * a bus. It calls device_probe_and_attach() for each of the device's
3544 bus_generic_attach(device_t dev)
3548 TAILQ_FOREACH(child, &dev->children, link) {
3549 device_probe_and_attach(child);
3556 * @brief Helper function for implementing DEVICE_DETACH()
3558 * This function can be used to help implement the DEVICE_DETACH() for
3559 * a bus. It calls device_detach() for each of the device's
3563 bus_generic_detach(device_t dev)
3568 if (dev->state != DS_ATTACHED)
3571 TAILQ_FOREACH(child, &dev->children, link) {
3572 if ((error = device_detach(child)) != 0)
3580 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3582 * This function can be used to help implement the DEVICE_SHUTDOWN()
3583 * for a bus. It calls device_shutdown() for each of the device's
3587 bus_generic_shutdown(device_t dev)
3591 TAILQ_FOREACH(child, &dev->children, link) {
3592 device_shutdown(child);
3599 * @brief Helper function for implementing DEVICE_SUSPEND()
3601 * This function can be used to help implement the DEVICE_SUSPEND()
3602 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3603 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3604 * operation is aborted and any devices which were suspended are
3605 * resumed immediately by calling their DEVICE_RESUME() methods.
3608 bus_generic_suspend(device_t dev)
3611 device_t child, child2;
3613 TAILQ_FOREACH(child, &dev->children, link) {
3614 error = DEVICE_SUSPEND(child);
3616 for (child2 = TAILQ_FIRST(&dev->children);
3617 child2 && child2 != child;
3618 child2 = TAILQ_NEXT(child2, link))
3619 DEVICE_RESUME(child2);
3627 * @brief Helper function for implementing DEVICE_RESUME()
3629 * This function can be used to help implement the DEVICE_RESUME() for
3630 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3633 bus_generic_resume(device_t dev)
3637 TAILQ_FOREACH(child, &dev->children, link) {
3638 DEVICE_RESUME(child);
3639 /* if resume fails, there's nothing we can usefully do... */
3645 * @brief Helper function for implementing BUS_PRINT_CHILD().
3647 * This function prints the first part of the ascii representation of
3648 * @p child, including its name, unit and description (if any - see
3649 * device_set_desc()).
3651 * @returns the number of characters printed
3654 bus_print_child_header(device_t dev, device_t child)
3658 if (device_get_desc(child)) {
3659 retval += device_printf(child, "<%s>", device_get_desc(child));
3661 retval += printf("%s", device_get_nameunit(child));
3668 * @brief Helper function for implementing BUS_PRINT_CHILD().
3670 * This function prints the last part of the ascii representation of
3671 * @p child, which consists of the string @c " on " followed by the
3672 * name and unit of the @p dev.
3674 * @returns the number of characters printed
3677 bus_print_child_footer(device_t dev, device_t child)
3679 return (printf(" on %s\n", device_get_nameunit(dev)));
3683 * @brief Helper function for implementing BUS_PRINT_CHILD().
3685 * This function simply calls bus_print_child_header() followed by
3686 * bus_print_child_footer().
3688 * @returns the number of characters printed
3691 bus_generic_print_child(device_t dev, device_t child)
3695 retval += bus_print_child_header(dev, child);
3696 retval += bus_print_child_footer(dev, child);
3702 * @brief Stub function for implementing BUS_READ_IVAR().
3707 bus_generic_read_ivar(device_t dev, device_t child, int index,
3714 * @brief Stub function for implementing BUS_WRITE_IVAR().
3719 bus_generic_write_ivar(device_t dev, device_t child, int index,
3726 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3730 struct resource_list *
3731 bus_generic_get_resource_list(device_t dev, device_t child)
3737 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3739 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3740 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3741 * and then calls device_probe_and_attach() for each unattached child.
3744 bus_generic_driver_added(device_t dev, driver_t *driver)
3748 DEVICE_IDENTIFY(driver, dev);
3749 TAILQ_FOREACH(child, &dev->children, link) {
3750 if (child->state == DS_NOTPRESENT ||
3751 (child->flags & DF_REBID))
3752 device_probe_and_attach(child);
3757 * @brief Helper function for implementing BUS_NEW_PASS().
3759 * This implementing of BUS_NEW_PASS() first calls the identify
3760 * routines for any drivers that probe at the current pass. Then it
3761 * walks the list of devices for this bus. If a device is already
3762 * attached, then it calls BUS_NEW_PASS() on that device. If the
3763 * device is not already attached, it attempts to attach a driver to
3767 bus_generic_new_pass(device_t dev)
3774 TAILQ_FOREACH(dl, &dc->drivers, link) {
3775 if (dl->pass == bus_current_pass)
3776 DEVICE_IDENTIFY(dl->driver, dev);
3778 TAILQ_FOREACH(child, &dev->children, link) {
3779 if (child->state >= DS_ATTACHED)
3780 BUS_NEW_PASS(child);
3781 else if (child->state == DS_NOTPRESENT)
3782 device_probe_and_attach(child);
3787 * @brief Helper function for implementing BUS_SETUP_INTR().
3789 * This simple implementation of BUS_SETUP_INTR() simply calls the
3790 * BUS_SETUP_INTR() method of the parent of @p dev.
3793 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3794 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3797 /* Propagate up the bus hierarchy until someone handles it. */
3799 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3800 filter, intr, arg, cookiep));
3805 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3807 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3808 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3811 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3814 /* Propagate up the bus hierarchy until someone handles it. */
3816 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3821 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3823 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3824 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3827 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3828 struct resource *r, u_long start, u_long end)
3830 /* Propagate up the bus hierarchy until someone handles it. */
3832 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3838 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3840 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3841 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3844 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3845 u_long start, u_long end, u_long count, u_int flags)
3847 /* Propagate up the bus hierarchy until someone handles it. */
3849 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3850 start, end, count, flags));
3855 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3857 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3858 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3861 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3864 /* Propagate up the bus hierarchy until someone handles it. */
3866 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3872 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3874 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3875 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3878 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3881 /* Propagate up the bus hierarchy until someone handles it. */
3883 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3889 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3891 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3892 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3895 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3896 int rid, struct resource *r)
3898 /* Propagate up the bus hierarchy until someone handles it. */
3900 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3906 * @brief Helper function for implementing BUS_BIND_INTR().
3908 * This simple implementation of BUS_BIND_INTR() simply calls the
3909 * BUS_BIND_INTR() method of the parent of @p dev.
3912 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3916 /* Propagate up the bus hierarchy until someone handles it. */
3918 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3923 * @brief Helper function for implementing BUS_CONFIG_INTR().
3925 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3926 * BUS_CONFIG_INTR() method of the parent of @p dev.
3929 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3930 enum intr_polarity pol)
3933 /* Propagate up the bus hierarchy until someone handles it. */
3935 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3940 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3942 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3943 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3946 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3947 void *cookie, const char *descr)
3950 /* Propagate up the bus hierarchy until someone handles it. */
3952 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3958 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3960 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3961 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3964 bus_generic_get_dma_tag(device_t dev, device_t child)
3967 /* Propagate up the bus hierarchy until someone handles it. */
3968 if (dev->parent != NULL)
3969 return (BUS_GET_DMA_TAG(dev->parent, child));
3974 * @brief Helper function for implementing BUS_GET_RESOURCE().
3976 * This implementation of BUS_GET_RESOURCE() uses the
3977 * resource_list_find() function to do most of the work. It calls
3978 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3982 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3983 u_long *startp, u_long *countp)
3985 struct resource_list * rl = NULL;
3986 struct resource_list_entry * rle = NULL;
3988 rl = BUS_GET_RESOURCE_LIST(dev, child);
3992 rle = resource_list_find(rl, type, rid);
3997 *startp = rle->start;
3999 *countp = rle->count;
4005 * @brief Helper function for implementing BUS_SET_RESOURCE().
4007 * This implementation of BUS_SET_RESOURCE() uses the
4008 * resource_list_add() function to do most of the work. It calls
4009 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4013 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4014 u_long start, u_long count)
4016 struct resource_list * rl = NULL;
4018 rl = BUS_GET_RESOURCE_LIST(dev, child);
4022 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4028 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4030 * This implementation of BUS_DELETE_RESOURCE() uses the
4031 * resource_list_delete() function to do most of the work. It calls
4032 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4036 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4038 struct resource_list * rl = NULL;
4040 rl = BUS_GET_RESOURCE_LIST(dev, child);
4044 resource_list_delete(rl, type, rid);
4050 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4052 * This implementation of BUS_RELEASE_RESOURCE() uses the
4053 * resource_list_release() function to do most of the work. It calls
4054 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4057 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4058 int rid, struct resource *r)
4060 struct resource_list * rl = NULL;
4062 if (device_get_parent(child) != dev)
4063 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4066 rl = BUS_GET_RESOURCE_LIST(dev, child);
4070 return (resource_list_release(rl, dev, child, type, rid, r));
4074 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4076 * This implementation of BUS_ALLOC_RESOURCE() uses the
4077 * resource_list_alloc() function to do most of the work. It calls
4078 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4081 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4082 int *rid, u_long start, u_long end, u_long count, u_int flags)
4084 struct resource_list * rl = NULL;
4086 if (device_get_parent(child) != dev)
4087 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4088 type, rid, start, end, count, flags));
4090 rl = BUS_GET_RESOURCE_LIST(dev, child);
4094 return (resource_list_alloc(rl, dev, child, type, rid,
4095 start, end, count, flags));
4099 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4101 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4102 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4105 bus_generic_child_present(device_t dev, device_t child)
4107 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4111 * Some convenience functions to make it easier for drivers to use the
4112 * resource-management functions. All these really do is hide the
4113 * indirection through the parent's method table, making for slightly
4114 * less-wordy code. In the future, it might make sense for this code
4115 * to maintain some sort of a list of resources allocated by each device.
4119 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4120 struct resource **res)
4124 for (i = 0; rs[i].type != -1; i++)
4126 for (i = 0; rs[i].type != -1; i++) {
4127 res[i] = bus_alloc_resource_any(dev,
4128 rs[i].type, &rs[i].rid, rs[i].flags);
4129 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4130 bus_release_resources(dev, rs, res);
4138 bus_release_resources(device_t dev, const struct resource_spec *rs,
4139 struct resource **res)
4143 for (i = 0; rs[i].type != -1; i++)
4144 if (res[i] != NULL) {
4145 bus_release_resource(
4146 dev, rs[i].type, rs[i].rid, res[i]);
4152 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4154 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4158 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4159 u_long count, u_int flags)
4161 if (dev->parent == NULL)
4163 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4168 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4170 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4174 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4177 if (dev->parent == NULL)
4179 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4183 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4185 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4189 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4191 if (dev->parent == NULL)
4193 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4197 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4199 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4203 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4205 if (dev->parent == NULL)
4207 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4211 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4213 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4217 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4219 if (dev->parent == NULL)
4221 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4225 * @brief Wrapper function for BUS_SETUP_INTR().
4227 * This function simply calls the BUS_SETUP_INTR() method of the
4231 bus_setup_intr(device_t dev, struct resource *r, int flags,
4232 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4236 if (dev->parent == NULL)
4238 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4242 if (handler != NULL && !(flags & INTR_MPSAFE))
4243 device_printf(dev, "[GIANT-LOCKED]\n");
4248 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4250 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4254 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4256 if (dev->parent == NULL)
4258 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4262 * @brief Wrapper function for BUS_BIND_INTR().
4264 * This function simply calls the BUS_BIND_INTR() method of the
4268 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4270 if (dev->parent == NULL)
4272 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4276 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4278 * This function first formats the requested description into a
4279 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4280 * the parent of @p dev.
4283 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4284 const char *fmt, ...)
4287 char descr[MAXCOMLEN + 1];
4289 if (dev->parent == NULL)
4292 vsnprintf(descr, sizeof(descr), fmt, ap);
4294 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4298 * @brief Wrapper function for BUS_SET_RESOURCE().
4300 * This function simply calls the BUS_SET_RESOURCE() method of the
4304 bus_set_resource(device_t dev, int type, int rid,
4305 u_long start, u_long count)
4307 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4312 * @brief Wrapper function for BUS_GET_RESOURCE().
4314 * This function simply calls the BUS_GET_RESOURCE() method of the
4318 bus_get_resource(device_t dev, int type, int rid,
4319 u_long *startp, u_long *countp)
4321 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4326 * @brief Wrapper function for BUS_GET_RESOURCE().
4328 * This function simply calls the BUS_GET_RESOURCE() method of the
4329 * parent of @p dev and returns the start value.
4332 bus_get_resource_start(device_t dev, int type, int rid)
4334 u_long start, count;
4337 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4345 * @brief Wrapper function for BUS_GET_RESOURCE().
4347 * This function simply calls the BUS_GET_RESOURCE() method of the
4348 * parent of @p dev and returns the count value.
4351 bus_get_resource_count(device_t dev, int type, int rid)
4353 u_long start, count;
4356 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4364 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4366 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4370 bus_delete_resource(device_t dev, int type, int rid)
4372 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4376 * @brief Wrapper function for BUS_CHILD_PRESENT().
4378 * This function simply calls the BUS_CHILD_PRESENT() method of the
4382 bus_child_present(device_t child)
4384 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4388 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4390 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4394 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4398 parent = device_get_parent(child);
4399 if (parent == NULL) {
4403 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4407 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4409 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4413 bus_child_location_str(device_t child, char *buf, size_t buflen)
4417 parent = device_get_parent(child);
4418 if (parent == NULL) {
4422 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4426 * @brief Wrapper function for BUS_GET_DMA_TAG().
4428 * This function simply calls the BUS_GET_DMA_TAG() method of the
4432 bus_get_dma_tag(device_t dev)
4436 parent = device_get_parent(dev);
4439 return (BUS_GET_DMA_TAG(parent, dev));
4442 /* Resume all devices and then notify userland that we're up again. */
4444 root_resume(device_t dev)
4448 error = bus_generic_resume(dev);
4450 devctl_notify("kern", "power", "resume", NULL);
4455 root_print_child(device_t dev, device_t child)
4459 retval += bus_print_child_header(dev, child);
4460 retval += printf("\n");
4466 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4467 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4470 * If an interrupt mapping gets to here something bad has happened.
4472 panic("root_setup_intr");
4476 * If we get here, assume that the device is permanant and really is
4477 * present in the system. Removable bus drivers are expected to intercept
4478 * this call long before it gets here. We return -1 so that drivers that
4479 * really care can check vs -1 or some ERRNO returned higher in the food
4483 root_child_present(device_t dev, device_t child)
4488 static kobj_method_t root_methods[] = {
4489 /* Device interface */
4490 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4491 KOBJMETHOD(device_suspend, bus_generic_suspend),
4492 KOBJMETHOD(device_resume, root_resume),
4495 KOBJMETHOD(bus_print_child, root_print_child),
4496 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4497 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4498 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4499 KOBJMETHOD(bus_child_present, root_child_present),
4504 static driver_t root_driver = {
4511 devclass_t root_devclass;
4514 root_bus_module_handler(module_t mod, int what, void* arg)
4518 TAILQ_INIT(&bus_data_devices);
4519 kobj_class_compile((kobj_class_t) &root_driver);
4520 root_bus = make_device(NULL, "root", 0);
4521 root_bus->desc = "System root bus";
4522 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4523 root_bus->driver = &root_driver;
4524 root_bus->state = DS_ATTACHED;
4525 root_devclass = devclass_find_internal("root", NULL, FALSE);
4530 device_shutdown(root_bus);
4533 return (EOPNOTSUPP);
4539 static moduledata_t root_bus_mod = {
4541 root_bus_module_handler,
4544 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4547 * @brief Automatically configure devices
4549 * This function begins the autoconfiguration process by calling
4550 * device_probe_and_attach() for each child of the @c root0 device.
4553 root_bus_configure(void)
4558 /* Eventually this will be split up, but this is sufficient for now. */
4559 bus_set_pass(BUS_PASS_DEFAULT);
4563 * @brief Module handler for registering device drivers
4565 * This module handler is used to automatically register device
4566 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4567 * devclass_add_driver() for the driver described by the
4568 * driver_module_data structure pointed to by @p arg
4571 driver_module_handler(module_t mod, int what, void *arg)
4573 struct driver_module_data *dmd;
4574 devclass_t bus_devclass;
4575 kobj_class_t driver;
4578 dmd = (struct driver_module_data *)arg;
4579 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4584 if (dmd->dmd_chainevh)
4585 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4587 pass = dmd->dmd_pass;
4588 driver = dmd->dmd_driver;
4589 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4590 DRIVERNAME(driver), dmd->dmd_busname, pass));
4591 error = devclass_add_driver(bus_devclass, driver, pass,
4596 PDEBUG(("Unloading module: driver %s from bus %s",
4597 DRIVERNAME(dmd->dmd_driver),
4599 error = devclass_delete_driver(bus_devclass,
4602 if (!error && dmd->dmd_chainevh)
4603 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4606 PDEBUG(("Quiesce module: driver %s from bus %s",
4607 DRIVERNAME(dmd->dmd_driver),
4609 error = devclass_quiesce_driver(bus_devclass,
4612 if (!error && dmd->dmd_chainevh)
4613 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4624 * @brief Enumerate all hinted devices for this bus.
4626 * Walks through the hints for this bus and calls the bus_hinted_child
4627 * routine for each one it fines. It searches first for the specific
4628 * bus that's being probed for hinted children (eg isa0), and then for
4629 * generic children (eg isa).
4631 * @param dev bus device to enumerate
4634 bus_enumerate_hinted_children(device_t bus)
4637 const char *dname, *busname;
4641 * enumerate all devices on the specific bus
4643 busname = device_get_nameunit(bus);
4645 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4646 BUS_HINTED_CHILD(bus, dname, dunit);
4649 * and all the generic ones.
4651 busname = device_get_name(bus);
4653 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4654 BUS_HINTED_CHILD(bus, dname, dunit);
4659 /* the _short versions avoid iteration by not calling anything that prints
4660 * more than oneliners. I love oneliners.
4664 print_device_short(device_t dev, int indent)
4669 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4670 dev->unit, dev->desc,
4671 (dev->parent? "":"no "),
4672 (TAILQ_EMPTY(&dev->children)? "no ":""),
4673 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4674 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4675 (dev->flags&DF_WILDCARD? "wildcard,":""),
4676 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4677 (dev->flags&DF_REBID? "rebiddable,":""),
4678 (dev->ivars? "":"no "),
4679 (dev->softc? "":"no "),
4684 print_device(device_t dev, int indent)
4689 print_device_short(dev, indent);
4691 indentprintf(("Parent:\n"));
4692 print_device_short(dev->parent, indent+1);
4693 indentprintf(("Driver:\n"));
4694 print_driver_short(dev->driver, indent+1);
4695 indentprintf(("Devclass:\n"));
4696 print_devclass_short(dev->devclass, indent+1);
4700 print_device_tree_short(device_t dev, int indent)
4701 /* print the device and all its children (indented) */
4708 print_device_short(dev, indent);
4710 TAILQ_FOREACH(child, &dev->children, link) {
4711 print_device_tree_short(child, indent+1);
4716 print_device_tree(device_t dev, int indent)
4717 /* print the device and all its children (indented) */
4724 print_device(dev, indent);
4726 TAILQ_FOREACH(child, &dev->children, link) {
4727 print_device_tree(child, indent+1);
4732 print_driver_short(driver_t *driver, int indent)
4737 indentprintf(("driver %s: softc size = %zd\n",
4738 driver->name, driver->size));
4742 print_driver(driver_t *driver, int indent)
4747 print_driver_short(driver, indent);
4751 print_driver_list(driver_list_t drivers, int indent)
4753 driverlink_t driver;
4755 TAILQ_FOREACH(driver, &drivers, link) {
4756 print_driver(driver->driver, indent);
4761 print_devclass_short(devclass_t dc, int indent)
4766 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4770 print_devclass(devclass_t dc, int indent)
4777 print_devclass_short(dc, indent);
4778 indentprintf(("Drivers:\n"));
4779 print_driver_list(dc->drivers, indent+1);
4781 indentprintf(("Devices:\n"));
4782 for (i = 0; i < dc->maxunit; i++)
4784 print_device(dc->devices[i], indent+1);
4788 print_devclass_list_short(void)
4792 printf("Short listing of devclasses, drivers & devices:\n");
4793 TAILQ_FOREACH(dc, &devclasses, link) {
4794 print_devclass_short(dc, 0);
4799 print_devclass_list(void)
4803 printf("Full listing of devclasses, drivers & devices:\n");
4804 TAILQ_FOREACH(dc, &devclasses, link) {
4805 print_devclass(dc, 0);
4812 * User-space access to the device tree.
4814 * We implement a small set of nodes:
4816 * hw.bus Single integer read method to obtain the
4817 * current generation count.
4818 * hw.bus.devices Reads the entire device tree in flat space.
4819 * hw.bus.rman Resource manager interface
4821 * We might like to add the ability to scan devclasses and/or drivers to
4822 * determine what else is currently loaded/available.
4826 sysctl_bus(SYSCTL_HANDLER_ARGS)
4828 struct u_businfo ubus;
4830 ubus.ub_version = BUS_USER_VERSION;
4831 ubus.ub_generation = bus_data_generation;
4833 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4835 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4836 "bus-related data");
4839 sysctl_devices(SYSCTL_HANDLER_ARGS)
4841 int *name = (int *)arg1;
4842 u_int namelen = arg2;
4845 struct u_device udev; /* XXX this is a bit big */
4851 if (bus_data_generation_check(name[0]))
4857 * Scan the list of devices, looking for the requested index.
4859 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4867 * Populate the return array.
4869 bzero(&udev, sizeof(udev));
4870 udev.dv_handle = (uintptr_t)dev;
4871 udev.dv_parent = (uintptr_t)dev->parent;
4872 if (dev->nameunit != NULL)
4873 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4874 if (dev->desc != NULL)
4875 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4876 if (dev->driver != NULL && dev->driver->name != NULL)
4877 strlcpy(udev.dv_drivername, dev->driver->name,
4878 sizeof(udev.dv_drivername));
4879 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4880 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4881 udev.dv_devflags = dev->devflags;
4882 udev.dv_flags = dev->flags;
4883 udev.dv_state = dev->state;
4884 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4888 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4889 "system device tree");
4892 bus_data_generation_check(int generation)
4894 if (generation != bus_data_generation)
4897 /* XXX generate optimised lists here? */
4902 bus_data_generation_update(void)
4904 bus_data_generation++;
4908 bus_free_resource(device_t dev, int type, struct resource *r)
4912 return (bus_release_resource(dev, type, rman_get_rid(r), r));