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 /* Deprecated way to adjust queue length */
359 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
360 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
361 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
362 0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
364 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
365 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
366 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
367 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
368 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
369 0, sysctl_devctl_queue, "I", "devctl queue length");
371 static d_open_t devopen;
372 static d_close_t devclose;
373 static d_read_t devread;
374 static d_ioctl_t devioctl;
375 static d_poll_t devpoll;
377 static struct cdevsw dev_cdevsw = {
378 .d_version = D_VERSION,
379 .d_flags = D_NEEDGIANT,
388 struct dev_event_info
391 TAILQ_ENTRY(dev_event_info) dei_link;
394 TAILQ_HEAD(devq, dev_event_info);
396 static struct dev_softc
405 struct proc *async_proc;
408 static struct cdev *devctl_dev;
413 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
414 UID_ROOT, GID_WHEEL, 0600, "devctl");
415 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
416 cv_init(&devsoftc.cv, "dev cv");
417 TAILQ_INIT(&devsoftc.devq);
421 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
427 devsoftc.nonblock = 0;
428 devsoftc.async_proc = NULL;
433 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
436 mtx_lock(&devsoftc.mtx);
437 cv_broadcast(&devsoftc.cv);
438 mtx_unlock(&devsoftc.mtx);
439 devsoftc.async_proc = NULL;
444 * The read channel for this device is used to report changes to
445 * userland in realtime. We are required to free the data as well as
446 * the n1 object because we allocate them separately. Also note that
447 * we return one record at a time. If you try to read this device a
448 * character at a time, you will lose the rest of the data. Listening
449 * programs are expected to cope.
452 devread(struct cdev *dev, struct uio *uio, int ioflag)
454 struct dev_event_info *n1;
457 mtx_lock(&devsoftc.mtx);
458 while (TAILQ_EMPTY(&devsoftc.devq)) {
459 if (devsoftc.nonblock) {
460 mtx_unlock(&devsoftc.mtx);
463 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
466 * Need to translate ERESTART to EINTR here? -- jake
468 mtx_unlock(&devsoftc.mtx);
472 n1 = TAILQ_FIRST(&devsoftc.devq);
473 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
475 mtx_unlock(&devsoftc.mtx);
476 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
477 free(n1->dei_data, M_BUS);
483 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
489 devsoftc.nonblock = 1;
491 devsoftc.nonblock = 0;
495 devsoftc.async_proc = td->td_proc;
497 devsoftc.async_proc = NULL;
500 /* (un)Support for other fcntl() calls. */
513 devpoll(struct cdev *dev, int events, struct thread *td)
517 mtx_lock(&devsoftc.mtx);
518 if (events & (POLLIN | POLLRDNORM)) {
519 if (!TAILQ_EMPTY(&devsoftc.devq))
520 revents = events & (POLLIN | POLLRDNORM);
522 selrecord(td, &devsoftc.sel);
524 mtx_unlock(&devsoftc.mtx);
530 * @brief Return whether the userland process is running
533 devctl_process_running(void)
535 return (devsoftc.inuse == 1);
539 * @brief Queue data to be read from the devctl device
541 * Generic interface to queue data to the devctl device. It is
542 * assumed that @p data is properly formatted. It is further assumed
543 * that @p data is allocated using the M_BUS malloc type.
546 devctl_queue_data_f(char *data, int flags)
548 struct dev_event_info *n1 = NULL, *n2 = NULL;
551 if (strlen(data) == 0)
553 if (devctl_queue_length == 0)
555 n1 = malloc(sizeof(*n1), M_BUS, flags);
559 mtx_lock(&devsoftc.mtx);
560 if (devctl_queue_length == 0) {
561 mtx_unlock(&devsoftc.mtx);
562 free(n1->dei_data, M_BUS);
566 /* Leave at least one spot in the queue... */
567 while (devsoftc.queued > devctl_queue_length - 1) {
568 n2 = TAILQ_FIRST(&devsoftc.devq);
569 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
570 free(n2->dei_data, M_BUS);
574 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
576 cv_broadcast(&devsoftc.cv);
577 mtx_unlock(&devsoftc.mtx);
578 selwakeup(&devsoftc.sel);
579 p = devsoftc.async_proc;
582 kern_psignal(p, SIGIO);
588 * We have to free data on all error paths since the caller
589 * assumes it will be free'd when this item is dequeued.
596 devctl_queue_data(char *data)
599 devctl_queue_data_f(data, M_NOWAIT);
603 * @brief Send a 'notification' to userland, using standard ways
606 devctl_notify_f(const char *system, const char *subsystem, const char *type,
607 const char *data, int flags)
613 return; /* BOGUS! Must specify system. */
614 if (subsystem == NULL)
615 return; /* BOGUS! Must specify subsystem. */
617 return; /* BOGUS! Must specify type. */
618 len += strlen(" system=") + strlen(system);
619 len += strlen(" subsystem=") + strlen(subsystem);
620 len += strlen(" type=") + strlen(type);
621 /* add in the data message plus newline. */
624 len += 3; /* '!', '\n', and NUL */
625 msg = malloc(len, M_BUS, flags);
627 return; /* Drop it on the floor */
629 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
630 system, subsystem, type, data);
632 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
633 system, subsystem, type);
634 devctl_queue_data_f(msg, flags);
638 devctl_notify(const char *system, const char *subsystem, const char *type,
642 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
646 * Common routine that tries to make sending messages as easy as possible.
647 * We allocate memory for the data, copy strings into that, but do not
648 * free it unless there's an error. The dequeue part of the driver should
649 * free the data. We don't send data when the device is disabled. We do
650 * send data, even when we have no listeners, because we wish to avoid
651 * races relating to startup and restart of listening applications.
653 * devaddq is designed to string together the type of event, with the
654 * object of that event, plus the plug and play info and location info
655 * for that event. This is likely most useful for devices, but less
656 * useful for other consumers of this interface. Those should use
657 * the devctl_queue_data() interface instead.
660 devaddq(const char *type, const char *what, device_t dev)
667 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
669 data = malloc(1024, M_BUS, M_NOWAIT);
673 /* get the bus specific location of this device */
674 loc = malloc(1024, M_BUS, M_NOWAIT);
678 bus_child_location_str(dev, loc, 1024);
680 /* Get the bus specific pnp info of this device */
681 pnp = malloc(1024, M_BUS, M_NOWAIT);
685 bus_child_pnpinfo_str(dev, pnp, 1024);
687 /* Get the parent of this device, or / if high enough in the tree. */
688 if (device_get_parent(dev) == NULL)
689 parstr = "."; /* Or '/' ? */
691 parstr = device_get_nameunit(device_get_parent(dev));
692 /* String it all together. */
693 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
697 devctl_queue_data(data);
707 * A device was added to the tree. We are called just after it successfully
708 * attaches (that is, probe and attach success for this device). No call
709 * is made if a device is merely parented into the tree. See devnomatch
710 * if probe fails. If attach fails, no notification is sent (but maybe
711 * we should have a different message for this).
714 devadded(device_t dev)
716 devaddq("+", device_get_nameunit(dev), dev);
720 * A device was removed from the tree. We are called just before this
724 devremoved(device_t dev)
726 devaddq("-", device_get_nameunit(dev), dev);
730 * Called when there's no match for this device. This is only called
731 * the first time that no match happens, so we don't keep getting this
732 * message. Should that prove to be undesirable, we can change it.
733 * This is called when all drivers that can attach to a given bus
734 * decline to accept this device. Other errors may not be detected.
737 devnomatch(device_t dev)
739 devaddq("?", "", dev);
743 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
745 struct dev_event_info *n1;
748 dis = devctl_queue_length == 0;
749 error = sysctl_handle_int(oidp, &dis, 0, req);
750 if (error || !req->newptr)
752 mtx_lock(&devsoftc.mtx);
754 while (!TAILQ_EMPTY(&devsoftc.devq)) {
755 n1 = TAILQ_FIRST(&devsoftc.devq);
756 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
757 free(n1->dei_data, M_BUS);
761 devctl_queue_length = 0;
763 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
765 mtx_unlock(&devsoftc.mtx);
770 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
772 struct dev_event_info *n1;
775 q = devctl_queue_length;
776 error = sysctl_handle_int(oidp, &q, 0, req);
777 if (error || !req->newptr)
781 mtx_lock(&devsoftc.mtx);
782 devctl_queue_length = q;
783 while (devsoftc.queued > devctl_queue_length) {
784 n1 = TAILQ_FIRST(&devsoftc.devq);
785 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
786 free(n1->dei_data, M_BUS);
790 mtx_unlock(&devsoftc.mtx);
794 /* End of /dev/devctl code */
796 static TAILQ_HEAD(,device) bus_data_devices;
797 static int bus_data_generation = 1;
799 static kobj_method_t null_methods[] = {
803 DEFINE_CLASS(null, null_methods, 0);
806 * Bus pass implementation
809 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
810 int bus_current_pass = BUS_PASS_ROOT;
814 * @brief Register the pass level of a new driver attachment
816 * Register a new driver attachment's pass level. If no driver
817 * attachment with the same pass level has been added, then @p new
818 * will be added to the global passes list.
820 * @param new the new driver attachment
823 driver_register_pass(struct driverlink *new)
825 struct driverlink *dl;
827 /* We only consider pass numbers during boot. */
828 if (bus_current_pass == BUS_PASS_DEFAULT)
832 * Walk the passes list. If we already know about this pass
833 * then there is nothing to do. If we don't, then insert this
834 * driver link into the list.
836 TAILQ_FOREACH(dl, &passes, passlink) {
837 if (dl->pass < new->pass)
839 if (dl->pass == new->pass)
841 TAILQ_INSERT_BEFORE(dl, new, passlink);
844 TAILQ_INSERT_TAIL(&passes, new, passlink);
848 * @brief Raise the current bus pass
850 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
851 * method on the root bus to kick off a new device tree scan for each
852 * new pass level that has at least one driver.
855 bus_set_pass(int pass)
857 struct driverlink *dl;
859 if (bus_current_pass > pass)
860 panic("Attempt to lower bus pass level");
862 TAILQ_FOREACH(dl, &passes, passlink) {
863 /* Skip pass values below the current pass level. */
864 if (dl->pass <= bus_current_pass)
868 * Bail once we hit a driver with a pass level that is
875 * Raise the pass level to the next level and rescan
878 bus_current_pass = dl->pass;
879 BUS_NEW_PASS(root_bus);
883 * If there isn't a driver registered for the requested pass,
884 * then bus_current_pass might still be less than 'pass'. Set
885 * it to 'pass' in that case.
887 if (bus_current_pass < pass)
888 bus_current_pass = pass;
889 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
893 * Devclass implementation
896 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
900 * @brief Find or create a device class
902 * If a device class with the name @p classname exists, return it,
903 * otherwise if @p create is non-zero create and return a new device
906 * If @p parentname is non-NULL, the parent of the devclass is set to
907 * the devclass of that name.
909 * @param classname the devclass name to find or create
910 * @param parentname the parent devclass name or @c NULL
911 * @param create non-zero to create a devclass
914 devclass_find_internal(const char *classname, const char *parentname,
919 PDEBUG(("looking for %s", classname));
923 TAILQ_FOREACH(dc, &devclasses, link) {
924 if (!strcmp(dc->name, classname))
929 PDEBUG(("creating %s", classname));
930 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
931 M_BUS, M_NOWAIT | M_ZERO);
935 dc->name = (char*) (dc + 1);
936 strcpy(dc->name, classname);
937 TAILQ_INIT(&dc->drivers);
938 TAILQ_INSERT_TAIL(&devclasses, dc, link);
940 bus_data_generation_update();
944 * If a parent class is specified, then set that as our parent so
945 * that this devclass will support drivers for the parent class as
946 * well. If the parent class has the same name don't do this though
947 * as it creates a cycle that can trigger an infinite loop in
948 * device_probe_child() if a device exists for which there is no
951 if (parentname && dc && !dc->parent &&
952 strcmp(classname, parentname) != 0) {
953 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
954 dc->parent->flags |= DC_HAS_CHILDREN;
961 * @brief Create a device class
963 * If a device class with the name @p classname exists, return it,
964 * otherwise create and return a new device class.
966 * @param classname the devclass name to find or create
969 devclass_create(const char *classname)
971 return (devclass_find_internal(classname, NULL, TRUE));
975 * @brief Find a device class
977 * If a device class with the name @p classname exists, return it,
978 * otherwise return @c NULL.
980 * @param classname the devclass name to find
983 devclass_find(const char *classname)
985 return (devclass_find_internal(classname, NULL, FALSE));
989 * @brief Register that a device driver has been added to a devclass
991 * Register that a device driver has been added to a devclass. This
992 * is called by devclass_add_driver to accomplish the recursive
993 * notification of all the children classes of dc, as well as dc.
994 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
997 * We do a full search here of the devclass list at each iteration
998 * level to save storing children-lists in the devclass structure. If
999 * we ever move beyond a few dozen devices doing this, we may need to
1002 * @param dc the devclass to edit
1003 * @param driver the driver that was just added
1006 devclass_driver_added(devclass_t dc, driver_t *driver)
1012 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1014 for (i = 0; i < dc->maxunit; i++)
1015 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1016 BUS_DRIVER_ADDED(dc->devices[i], driver);
1019 * Walk through the children classes. Since we only keep a
1020 * single parent pointer around, we walk the entire list of
1021 * devclasses looking for children. We set the
1022 * DC_HAS_CHILDREN flag when a child devclass is created on
1023 * the parent, so we only walk the list for those devclasses
1024 * that have children.
1026 if (!(dc->flags & DC_HAS_CHILDREN))
1029 TAILQ_FOREACH(dc, &devclasses, link) {
1030 if (dc->parent == parent)
1031 devclass_driver_added(dc, driver);
1036 * @brief Add a device driver to a device class
1038 * Add a device driver to a devclass. This is normally called
1039 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1040 * all devices in the devclass will be called to allow them to attempt
1041 * to re-probe any unmatched children.
1043 * @param dc the devclass to edit
1044 * @param driver the driver to register
1047 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1050 const char *parentname;
1052 PDEBUG(("%s", DRIVERNAME(driver)));
1054 /* Don't allow invalid pass values. */
1055 if (pass <= BUS_PASS_ROOT)
1058 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1063 * Compile the driver's methods. Also increase the reference count
1064 * so that the class doesn't get freed when the last instance
1065 * goes. This means we can safely use static methods and avoids a
1066 * double-free in devclass_delete_driver.
1068 kobj_class_compile((kobj_class_t) driver);
1071 * If the driver has any base classes, make the
1072 * devclass inherit from the devclass of the driver's
1073 * first base class. This will allow the system to
1074 * search for drivers in both devclasses for children
1075 * of a device using this driver.
1077 if (driver->baseclasses)
1078 parentname = driver->baseclasses[0]->name;
1081 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1083 dl->driver = driver;
1084 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1085 driver->refs++; /* XXX: kobj_mtx */
1087 driver_register_pass(dl);
1089 devclass_driver_added(dc, driver);
1090 bus_data_generation_update();
1095 * @brief Register that a device driver has been deleted from a devclass
1097 * Register that a device driver has been removed from a devclass.
1098 * This is called by devclass_delete_driver to accomplish the
1099 * recursive notification of all the children classes of busclass, as
1100 * well as busclass. Each layer will attempt to detach the driver
1101 * from any devices that are children of the bus's devclass. The function
1102 * will return an error if a device fails to detach.
1104 * We do a full search here of the devclass list at each iteration
1105 * level to save storing children-lists in the devclass structure. If
1106 * we ever move beyond a few dozen devices doing this, we may need to
1109 * @param busclass the devclass of the parent bus
1110 * @param dc the devclass of the driver being deleted
1111 * @param driver the driver being deleted
1114 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1121 * Disassociate from any devices. We iterate through all the
1122 * devices in the devclass of the driver and detach any which are
1123 * using the driver and which have a parent in the devclass which
1124 * we are deleting from.
1126 * Note that since a driver can be in multiple devclasses, we
1127 * should not detach devices which are not children of devices in
1128 * the affected devclass.
1130 for (i = 0; i < dc->maxunit; i++) {
1131 if (dc->devices[i]) {
1132 dev = dc->devices[i];
1133 if (dev->driver == driver && dev->parent &&
1134 dev->parent->devclass == busclass) {
1135 if ((error = device_detach(dev)) != 0)
1137 BUS_PROBE_NOMATCH(dev->parent, dev);
1139 dev->flags |= DF_DONENOMATCH;
1145 * Walk through the children classes. Since we only keep a
1146 * single parent pointer around, we walk the entire list of
1147 * devclasses looking for children. We set the
1148 * DC_HAS_CHILDREN flag when a child devclass is created on
1149 * the parent, so we only walk the list for those devclasses
1150 * that have children.
1152 if (!(busclass->flags & DC_HAS_CHILDREN))
1155 TAILQ_FOREACH(busclass, &devclasses, link) {
1156 if (busclass->parent == parent) {
1157 error = devclass_driver_deleted(busclass, dc, driver);
1166 * @brief Delete a device driver from a device class
1168 * Delete a device driver from a devclass. This is normally called
1169 * automatically by DRIVER_MODULE().
1171 * If the driver is currently attached to any devices,
1172 * devclass_delete_driver() will first attempt to detach from each
1173 * device. If one of the detach calls fails, the driver will not be
1176 * @param dc the devclass to edit
1177 * @param driver the driver to unregister
1180 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1182 devclass_t dc = devclass_find(driver->name);
1186 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1192 * Find the link structure in the bus' list of drivers.
1194 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1195 if (dl->driver == driver)
1200 PDEBUG(("%s not found in %s list", driver->name,
1205 error = devclass_driver_deleted(busclass, dc, driver);
1209 TAILQ_REMOVE(&busclass->drivers, dl, link);
1214 if (driver->refs == 0)
1215 kobj_class_free((kobj_class_t) driver);
1217 bus_data_generation_update();
1222 * @brief Quiesces a set of device drivers from a device class
1224 * Quiesce a device driver from a devclass. This is normally called
1225 * automatically by DRIVER_MODULE().
1227 * If the driver is currently attached to any devices,
1228 * devclass_quiesece_driver() will first attempt to quiesce each
1231 * @param dc the devclass to edit
1232 * @param driver the driver to unregister
1235 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1237 devclass_t dc = devclass_find(driver->name);
1243 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1249 * Find the link structure in the bus' list of drivers.
1251 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1252 if (dl->driver == driver)
1257 PDEBUG(("%s not found in %s list", driver->name,
1263 * Quiesce all devices. We iterate through all the devices in
1264 * the devclass of the driver and quiesce any which are using
1265 * the driver and which have a parent in the devclass which we
1268 * Note that since a driver can be in multiple devclasses, we
1269 * should not quiesce devices which are not children of
1270 * devices in the affected devclass.
1272 for (i = 0; i < dc->maxunit; i++) {
1273 if (dc->devices[i]) {
1274 dev = dc->devices[i];
1275 if (dev->driver == driver && dev->parent &&
1276 dev->parent->devclass == busclass) {
1277 if ((error = device_quiesce(dev)) != 0)
1290 devclass_find_driver_internal(devclass_t dc, const char *classname)
1294 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1296 TAILQ_FOREACH(dl, &dc->drivers, link) {
1297 if (!strcmp(dl->driver->name, classname))
1301 PDEBUG(("not found"));
1306 * @brief Return the name of the devclass
1309 devclass_get_name(devclass_t dc)
1315 * @brief Find a device given a unit number
1317 * @param dc the devclass to search
1318 * @param unit the unit number to search for
1320 * @returns the device with the given unit number or @c
1321 * NULL if there is no such device
1324 devclass_get_device(devclass_t dc, int unit)
1326 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1328 return (dc->devices[unit]);
1332 * @brief Find the softc field of a device given a unit number
1334 * @param dc the devclass to search
1335 * @param unit the unit number to search for
1337 * @returns the softc field of the device with the given
1338 * unit number or @c NULL if there is no such
1342 devclass_get_softc(devclass_t dc, int unit)
1346 dev = devclass_get_device(dc, unit);
1350 return (device_get_softc(dev));
1354 * @brief Get a list of devices in the devclass
1356 * An array containing a list of all the devices in the given devclass
1357 * is allocated and returned in @p *devlistp. The number of devices
1358 * in the array is returned in @p *devcountp. The caller should free
1359 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1361 * @param dc the devclass to examine
1362 * @param devlistp points at location for array pointer return
1364 * @param devcountp points at location for array size return value
1367 * @retval ENOMEM the array allocation failed
1370 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1375 count = devclass_get_count(dc);
1376 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1381 for (i = 0; i < dc->maxunit; i++) {
1382 if (dc->devices[i]) {
1383 list[count] = dc->devices[i];
1395 * @brief Get a list of drivers in the devclass
1397 * An array containing a list of pointers to all the drivers in the
1398 * given devclass is allocated and returned in @p *listp. The number
1399 * of drivers in the array is returned in @p *countp. The caller should
1400 * free the array using @c free(p, M_TEMP).
1402 * @param dc the devclass to examine
1403 * @param listp gives location for array pointer return value
1404 * @param countp gives location for number of array elements
1408 * @retval ENOMEM the array allocation failed
1411 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1418 TAILQ_FOREACH(dl, &dc->drivers, link)
1420 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1425 TAILQ_FOREACH(dl, &dc->drivers, link) {
1426 list[count] = dl->driver;
1436 * @brief Get the number of devices in a devclass
1438 * @param dc the devclass to examine
1441 devclass_get_count(devclass_t dc)
1446 for (i = 0; i < dc->maxunit; i++)
1453 * @brief Get the maximum unit number used in a devclass
1455 * Note that this is one greater than the highest currently-allocated
1456 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1457 * that not even the devclass has been allocated yet.
1459 * @param dc the devclass to examine
1462 devclass_get_maxunit(devclass_t dc)
1466 return (dc->maxunit);
1470 * @brief Find a free unit number in a devclass
1472 * This function searches for the first unused unit number greater
1473 * that or equal to @p unit.
1475 * @param dc the devclass to examine
1476 * @param unit the first unit number to check
1479 devclass_find_free_unit(devclass_t dc, int unit)
1483 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1489 * @brief Set the parent of a devclass
1491 * The parent class is normally initialised automatically by
1494 * @param dc the devclass to edit
1495 * @param pdc the new parent devclass
1498 devclass_set_parent(devclass_t dc, devclass_t pdc)
1504 * @brief Get the parent of a devclass
1506 * @param dc the devclass to examine
1509 devclass_get_parent(devclass_t dc)
1511 return (dc->parent);
1514 struct sysctl_ctx_list *
1515 devclass_get_sysctl_ctx(devclass_t dc)
1517 return (&dc->sysctl_ctx);
1521 devclass_get_sysctl_tree(devclass_t dc)
1523 return (dc->sysctl_tree);
1528 * @brief Allocate a unit number
1530 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1531 * will do). The allocated unit number is returned in @p *unitp.
1533 * @param dc the devclass to allocate from
1534 * @param unitp points at the location for the allocated unit
1538 * @retval EEXIST the requested unit number is already allocated
1539 * @retval ENOMEM memory allocation failure
1542 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1547 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1549 /* Ask the parent bus if it wants to wire this device. */
1551 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1554 /* If we were given a wired unit number, check for existing device */
1557 if (unit >= 0 && unit < dc->maxunit &&
1558 dc->devices[unit] != NULL) {
1560 printf("%s: %s%d already exists; skipping it\n",
1561 dc->name, dc->name, *unitp);
1565 /* Unwired device, find the next available slot for it */
1567 for (unit = 0;; unit++) {
1568 /* If there is an "at" hint for a unit then skip it. */
1569 if (resource_string_value(dc->name, unit, "at", &s) ==
1573 /* If this device slot is already in use, skip it. */
1574 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1582 * We've selected a unit beyond the length of the table, so let's
1583 * extend the table to make room for all units up to and including
1586 if (unit >= dc->maxunit) {
1587 device_t *newlist, *oldlist;
1590 oldlist = dc->devices;
1591 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1592 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1595 if (oldlist != NULL)
1596 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1597 bzero(newlist + dc->maxunit,
1598 sizeof(device_t) * (newsize - dc->maxunit));
1599 dc->devices = newlist;
1600 dc->maxunit = newsize;
1601 if (oldlist != NULL)
1602 free(oldlist, M_BUS);
1604 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1612 * @brief Add a device to a devclass
1614 * A unit number is allocated for the device (using the device's
1615 * preferred unit number if any) and the device is registered in the
1616 * devclass. This allows the device to be looked up by its unit
1617 * number, e.g. by decoding a dev_t minor number.
1619 * @param dc the devclass to add to
1620 * @param dev the device to add
1623 * @retval EEXIST the requested unit number is already allocated
1624 * @retval ENOMEM memory allocation failure
1627 devclass_add_device(devclass_t dc, device_t dev)
1631 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1633 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1636 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1640 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1641 free(dev->nameunit, M_BUS);
1642 dev->nameunit = NULL;
1645 dc->devices[dev->unit] = dev;
1647 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1654 * @brief Delete a device from a devclass
1656 * The device is removed from the devclass's device list and its unit
1659 * @param dc the devclass to delete from
1660 * @param dev the device to delete
1665 devclass_delete_device(devclass_t dc, device_t dev)
1670 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1672 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1673 panic("devclass_delete_device: inconsistent device class");
1674 dc->devices[dev->unit] = NULL;
1675 if (dev->flags & DF_WILDCARD)
1677 dev->devclass = NULL;
1678 free(dev->nameunit, M_BUS);
1679 dev->nameunit = NULL;
1686 * @brief Make a new device and add it as a child of @p parent
1688 * @param parent the parent of the new device
1689 * @param name the devclass name of the new device or @c NULL
1690 * to leave the devclass unspecified
1691 * @parem unit the unit number of the new device of @c -1 to
1692 * leave the unit number unspecified
1694 * @returns the new device
1697 make_device(device_t parent, const char *name, int unit)
1702 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1705 dc = devclass_find_internal(name, NULL, TRUE);
1707 printf("make_device: can't find device class %s\n",
1715 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1719 dev->parent = parent;
1720 TAILQ_INIT(&dev->children);
1721 kobj_init((kobj_t) dev, &null_class);
1723 dev->devclass = NULL;
1725 dev->nameunit = NULL;
1729 dev->flags = DF_ENABLED;
1732 dev->flags |= DF_WILDCARD;
1734 dev->flags |= DF_FIXEDCLASS;
1735 if (devclass_add_device(dc, dev)) {
1736 kobj_delete((kobj_t) dev, M_BUS);
1743 dev->state = DS_NOTPRESENT;
1745 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1746 bus_data_generation_update();
1753 * @brief Print a description of a device.
1756 device_print_child(device_t dev, device_t child)
1760 if (device_is_alive(child))
1761 retval += BUS_PRINT_CHILD(dev, child);
1763 retval += device_printf(child, " not found\n");
1769 * @brief Create a new device
1771 * This creates a new device and adds it as a child of an existing
1772 * parent device. The new device will be added after the last existing
1773 * child with order zero.
1775 * @param dev the device which will be the parent of the
1777 * @param name devclass name for new device or @c NULL if not
1779 * @param unit unit number for new device or @c -1 if not
1782 * @returns the new device
1785 device_add_child(device_t dev, const char *name, int unit)
1787 return (device_add_child_ordered(dev, 0, name, unit));
1791 * @brief Create a new device
1793 * This creates a new device and adds it as a child of an existing
1794 * parent device. The new device will be added after the last existing
1795 * child with the same order.
1797 * @param dev the device which will be the parent of the
1799 * @param order a value which is used to partially sort the
1800 * children of @p dev - devices created using
1801 * lower values of @p order appear first in @p
1802 * dev's list of children
1803 * @param name devclass name for new device or @c NULL if not
1805 * @param unit unit number for new device or @c -1 if not
1808 * @returns the new device
1811 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1816 PDEBUG(("%s at %s with order %u as unit %d",
1817 name, DEVICENAME(dev), order, unit));
1818 KASSERT(name != NULL || unit == -1,
1819 ("child device with wildcard name and specific unit number"));
1821 child = make_device(dev, name, unit);
1824 child->order = order;
1826 TAILQ_FOREACH(place, &dev->children, link) {
1827 if (place->order > order)
1833 * The device 'place' is the first device whose order is
1834 * greater than the new child.
1836 TAILQ_INSERT_BEFORE(place, child, link);
1839 * The new child's order is greater or equal to the order of
1840 * any existing device. Add the child to the tail of the list.
1842 TAILQ_INSERT_TAIL(&dev->children, child, link);
1845 bus_data_generation_update();
1850 * @brief Delete a device
1852 * This function deletes a device along with all of its children. If
1853 * the device currently has a driver attached to it, the device is
1854 * detached first using device_detach().
1856 * @param dev the parent device
1857 * @param child the device to delete
1860 * @retval non-zero a unit error code describing the error
1863 device_delete_child(device_t dev, device_t child)
1866 device_t grandchild;
1868 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1870 /* remove children first */
1871 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1872 error = device_delete_child(child, grandchild);
1877 if ((error = device_detach(child)) != 0)
1879 if (child->devclass)
1880 devclass_delete_device(child->devclass, child);
1882 BUS_CHILD_DELETED(dev, child);
1883 TAILQ_REMOVE(&dev->children, child, link);
1884 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1885 kobj_delete((kobj_t) child, M_BUS);
1887 bus_data_generation_update();
1892 * @brief Delete all children devices of the given device, if any.
1894 * This function deletes all children devices of the given device, if
1895 * any, using the device_delete_child() function for each device it
1896 * finds. If a child device cannot be deleted, this function will
1897 * return an error code.
1899 * @param dev the parent device
1902 * @retval non-zero a device would not detach
1905 device_delete_children(device_t dev)
1910 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1914 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1915 error = device_delete_child(dev, child);
1917 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1925 * @brief Find a device given a unit number
1927 * This is similar to devclass_get_devices() but only searches for
1928 * devices which have @p dev as a parent.
1930 * @param dev the parent device to search
1931 * @param unit the unit number to search for. If the unit is -1,
1932 * return the first child of @p dev which has name
1933 * @p classname (that is, the one with the lowest unit.)
1935 * @returns the device with the given unit number or @c
1936 * NULL if there is no such device
1939 device_find_child(device_t dev, const char *classname, int unit)
1944 dc = devclass_find(classname);
1949 child = devclass_get_device(dc, unit);
1950 if (child && child->parent == dev)
1953 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1954 child = devclass_get_device(dc, unit);
1955 if (child && child->parent == dev)
1966 first_matching_driver(devclass_t dc, device_t dev)
1969 return (devclass_find_driver_internal(dc, dev->devclass->name));
1970 return (TAILQ_FIRST(&dc->drivers));
1977 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1979 if (dev->devclass) {
1981 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1982 if (!strcmp(dev->devclass->name, dl->driver->name))
1986 return (TAILQ_NEXT(last, link));
1993 device_probe_child(device_t dev, device_t child)
1996 driverlink_t best = NULL;
1998 int result, pri = 0;
1999 int hasclass = (child->devclass != NULL);
2005 panic("device_probe_child: parent device has no devclass");
2008 * If the state is already probed, then return. However, don't
2009 * return if we can rebid this object.
2011 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2014 for (; dc; dc = dc->parent) {
2015 for (dl = first_matching_driver(dc, child);
2017 dl = next_matching_driver(dc, child, dl)) {
2018 /* If this driver's pass is too high, then ignore it. */
2019 if (dl->pass > bus_current_pass)
2022 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2023 result = device_set_driver(child, dl->driver);
2024 if (result == ENOMEM)
2026 else if (result != 0)
2029 if (device_set_devclass(child,
2030 dl->driver->name) != 0) {
2031 char const * devname =
2032 device_get_name(child);
2033 if (devname == NULL)
2034 devname = "(unknown)";
2035 printf("driver bug: Unable to set "
2036 "devclass (class: %s "
2040 (void)device_set_driver(child, NULL);
2045 /* Fetch any flags for the device before probing. */
2046 resource_int_value(dl->driver->name, child->unit,
2047 "flags", &child->devflags);
2049 result = DEVICE_PROBE(child);
2051 /* Reset flags and devclass before the next probe. */
2052 child->devflags = 0;
2054 (void)device_set_devclass(child, NULL);
2057 * If the driver returns SUCCESS, there can be
2058 * no higher match for this device.
2067 * The driver returned an error so it
2068 * certainly doesn't match.
2071 (void)device_set_driver(child, NULL);
2076 * A priority lower than SUCCESS, remember the
2077 * best matching driver. Initialise the value
2078 * of pri for the first match.
2080 if (best == NULL || result > pri) {
2082 * Probes that return BUS_PROBE_NOWILDCARD
2083 * or lower only match on devices whose
2084 * driver was explicitly specified.
2086 if (result <= BUS_PROBE_NOWILDCARD &&
2087 !(child->flags & DF_FIXEDCLASS))
2095 * If we have an unambiguous match in this devclass,
2096 * don't look in the parent.
2098 if (best && pri == 0)
2103 * If we found a driver, change state and initialise the devclass.
2105 /* XXX What happens if we rebid and got no best? */
2108 * If this device was attached, and we were asked to
2109 * rescan, and it is a different driver, then we have
2110 * to detach the old driver and reattach this new one.
2111 * Note, we don't have to check for DF_REBID here
2112 * because if the state is > DS_ALIVE, we know it must
2115 * This assumes that all DF_REBID drivers can have
2116 * their probe routine called at any time and that
2117 * they are idempotent as well as completely benign in
2118 * normal operations.
2120 * We also have to make sure that the detach
2121 * succeeded, otherwise we fail the operation (or
2122 * maybe it should just fail silently? I'm torn).
2124 if (child->state > DS_ALIVE && best->driver != child->driver)
2125 if ((result = device_detach(dev)) != 0)
2128 /* Set the winning driver, devclass, and flags. */
2129 if (!child->devclass) {
2130 result = device_set_devclass(child, best->driver->name);
2134 result = device_set_driver(child, best->driver);
2137 resource_int_value(best->driver->name, child->unit,
2138 "flags", &child->devflags);
2142 * A bit bogus. Call the probe method again to make
2143 * sure that we have the right description.
2145 DEVICE_PROBE(child);
2147 child->flags |= DF_REBID;
2150 child->flags &= ~DF_REBID;
2151 child->state = DS_ALIVE;
2153 bus_data_generation_update();
2161 * @brief Return the parent of a device
2164 device_get_parent(device_t dev)
2166 return (dev->parent);
2170 * @brief Get a list of children of a device
2172 * An array containing a list of all the children of the given device
2173 * is allocated and returned in @p *devlistp. The number of devices
2174 * in the array is returned in @p *devcountp. The caller should free
2175 * the array using @c free(p, M_TEMP).
2177 * @param dev the device to examine
2178 * @param devlistp points at location for array pointer return
2180 * @param devcountp points at location for array size return value
2183 * @retval ENOMEM the array allocation failed
2186 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2193 TAILQ_FOREACH(child, &dev->children, link) {
2202 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2207 TAILQ_FOREACH(child, &dev->children, link) {
2208 list[count] = child;
2219 * @brief Return the current driver for the device or @c NULL if there
2220 * is no driver currently attached
2223 device_get_driver(device_t dev)
2225 return (dev->driver);
2229 * @brief Return the current devclass for the device or @c NULL if
2233 device_get_devclass(device_t dev)
2235 return (dev->devclass);
2239 * @brief Return the name of the device's devclass or @c NULL if there
2243 device_get_name(device_t dev)
2245 if (dev != NULL && dev->devclass)
2246 return (devclass_get_name(dev->devclass));
2251 * @brief Return a string containing the device's devclass name
2252 * followed by an ascii representation of the device's unit number
2256 device_get_nameunit(device_t dev)
2258 return (dev->nameunit);
2262 * @brief Return the device's unit number.
2265 device_get_unit(device_t dev)
2271 * @brief Return the device's description string
2274 device_get_desc(device_t dev)
2280 * @brief Return the device's flags
2283 device_get_flags(device_t dev)
2285 return (dev->devflags);
2288 struct sysctl_ctx_list *
2289 device_get_sysctl_ctx(device_t dev)
2291 return (&dev->sysctl_ctx);
2295 device_get_sysctl_tree(device_t dev)
2297 return (dev->sysctl_tree);
2301 * @brief Print the name of the device followed by a colon and a space
2303 * @returns the number of characters printed
2306 device_print_prettyname(device_t dev)
2308 const char *name = device_get_name(dev);
2311 return (printf("unknown: "));
2312 return (printf("%s%d: ", name, device_get_unit(dev)));
2316 * @brief Print the name of the device followed by a colon, a space
2317 * and the result of calling vprintf() with the value of @p fmt and
2318 * the following arguments.
2320 * @returns the number of characters printed
2323 device_printf(device_t dev, const char * fmt, ...)
2328 retval = device_print_prettyname(dev);
2330 retval += vprintf(fmt, ap);
2339 device_set_desc_internal(device_t dev, const char* desc, int copy)
2341 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2342 free(dev->desc, M_BUS);
2343 dev->flags &= ~DF_DESCMALLOCED;
2348 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2350 strcpy(dev->desc, desc);
2351 dev->flags |= DF_DESCMALLOCED;
2354 /* Avoid a -Wcast-qual warning */
2355 dev->desc = (char *)(uintptr_t) desc;
2358 bus_data_generation_update();
2362 * @brief Set the device's description
2364 * The value of @c desc should be a string constant that will not
2365 * change (at least until the description is changed in a subsequent
2366 * call to device_set_desc() or device_set_desc_copy()).
2369 device_set_desc(device_t dev, const char* desc)
2371 device_set_desc_internal(dev, desc, FALSE);
2375 * @brief Set the device's description
2377 * The string pointed to by @c desc is copied. Use this function if
2378 * the device description is generated, (e.g. with sprintf()).
2381 device_set_desc_copy(device_t dev, const char* desc)
2383 device_set_desc_internal(dev, desc, TRUE);
2387 * @brief Set the device's flags
2390 device_set_flags(device_t dev, uint32_t flags)
2392 dev->devflags = flags;
2396 * @brief Return the device's softc field
2398 * The softc is allocated and zeroed when a driver is attached, based
2399 * on the size field of the driver.
2402 device_get_softc(device_t dev)
2404 return (dev->softc);
2408 * @brief Set the device's softc field
2410 * Most drivers do not need to use this since the softc is allocated
2411 * automatically when the driver is attached.
2414 device_set_softc(device_t dev, void *softc)
2416 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2417 free(dev->softc, M_BUS_SC);
2420 dev->flags |= DF_EXTERNALSOFTC;
2422 dev->flags &= ~DF_EXTERNALSOFTC;
2426 * @brief Free claimed softc
2428 * Most drivers do not need to use this since the softc is freed
2429 * automatically when the driver is detached.
2432 device_free_softc(void *softc)
2434 free(softc, M_BUS_SC);
2438 * @brief Claim softc
2440 * This function can be used to let the driver free the automatically
2441 * allocated softc using "device_free_softc()". This function is
2442 * useful when the driver is refcounting the softc and the softc
2443 * cannot be freed when the "device_detach" method is called.
2446 device_claim_softc(device_t dev)
2449 dev->flags |= DF_EXTERNALSOFTC;
2451 dev->flags &= ~DF_EXTERNALSOFTC;
2455 * @brief Get the device's ivars field
2457 * The ivars field is used by the parent device to store per-device
2458 * state (e.g. the physical location of the device or a list of
2462 device_get_ivars(device_t dev)
2465 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2466 return (dev->ivars);
2470 * @brief Set the device's ivars field
2473 device_set_ivars(device_t dev, void * ivars)
2476 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2481 * @brief Return the device's state
2484 device_get_state(device_t dev)
2486 return (dev->state);
2490 * @brief Set the DF_ENABLED flag for the device
2493 device_enable(device_t dev)
2495 dev->flags |= DF_ENABLED;
2499 * @brief Clear the DF_ENABLED flag for the device
2502 device_disable(device_t dev)
2504 dev->flags &= ~DF_ENABLED;
2508 * @brief Increment the busy counter for the device
2511 device_busy(device_t dev)
2513 if (dev->state < DS_ATTACHING)
2514 panic("device_busy: called for unattached device");
2515 if (dev->busy == 0 && dev->parent)
2516 device_busy(dev->parent);
2518 if (dev->state == DS_ATTACHED)
2519 dev->state = DS_BUSY;
2523 * @brief Decrement the busy counter for the device
2526 device_unbusy(device_t dev)
2528 if (dev->busy != 0 && dev->state != DS_BUSY &&
2529 dev->state != DS_ATTACHING)
2530 panic("device_unbusy: called for non-busy device %s",
2531 device_get_nameunit(dev));
2533 if (dev->busy == 0) {
2535 device_unbusy(dev->parent);
2536 if (dev->state == DS_BUSY)
2537 dev->state = DS_ATTACHED;
2542 * @brief Set the DF_QUIET flag for the device
2545 device_quiet(device_t dev)
2547 dev->flags |= DF_QUIET;
2551 * @brief Clear the DF_QUIET flag for the device
2554 device_verbose(device_t dev)
2556 dev->flags &= ~DF_QUIET;
2560 * @brief Return non-zero if the DF_QUIET flag is set on the device
2563 device_is_quiet(device_t dev)
2565 return ((dev->flags & DF_QUIET) != 0);
2569 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2572 device_is_enabled(device_t dev)
2574 return ((dev->flags & DF_ENABLED) != 0);
2578 * @brief Return non-zero if the device was successfully probed
2581 device_is_alive(device_t dev)
2583 return (dev->state >= DS_ALIVE);
2587 * @brief Return non-zero if the device currently has a driver
2591 device_is_attached(device_t dev)
2593 return (dev->state >= DS_ATTACHED);
2597 * @brief Set the devclass of a device
2598 * @see devclass_add_device().
2601 device_set_devclass(device_t dev, const char *classname)
2608 devclass_delete_device(dev->devclass, dev);
2612 if (dev->devclass) {
2613 printf("device_set_devclass: device class already set\n");
2617 dc = devclass_find_internal(classname, NULL, TRUE);
2621 error = devclass_add_device(dc, dev);
2623 bus_data_generation_update();
2628 * @brief Set the driver of a device
2631 * @retval EBUSY the device already has a driver attached
2632 * @retval ENOMEM a memory allocation failure occurred
2635 device_set_driver(device_t dev, driver_t *driver)
2637 if (dev->state >= DS_ATTACHED)
2640 if (dev->driver == driver)
2643 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2644 free(dev->softc, M_BUS_SC);
2647 device_set_desc(dev, NULL);
2648 kobj_delete((kobj_t) dev, NULL);
2649 dev->driver = driver;
2651 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2652 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2653 dev->softc = malloc(driver->size, M_BUS_SC,
2656 kobj_delete((kobj_t) dev, NULL);
2657 kobj_init((kobj_t) dev, &null_class);
2663 kobj_init((kobj_t) dev, &null_class);
2666 bus_data_generation_update();
2671 * @brief Probe a device, and return this status.
2673 * This function is the core of the device autoconfiguration
2674 * system. Its purpose is to select a suitable driver for a device and
2675 * then call that driver to initialise the hardware appropriately. The
2676 * driver is selected by calling the DEVICE_PROBE() method of a set of
2677 * candidate drivers and then choosing the driver which returned the
2678 * best value. This driver is then attached to the device using
2681 * The set of suitable drivers is taken from the list of drivers in
2682 * the parent device's devclass. If the device was originally created
2683 * with a specific class name (see device_add_child()), only drivers
2684 * with that name are probed, otherwise all drivers in the devclass
2685 * are probed. If no drivers return successful probe values in the
2686 * parent devclass, the search continues in the parent of that
2687 * devclass (see devclass_get_parent()) if any.
2689 * @param dev the device to initialise
2692 * @retval ENXIO no driver was found
2693 * @retval ENOMEM memory allocation failure
2694 * @retval non-zero some other unix error code
2695 * @retval -1 Device already attached
2698 device_probe(device_t dev)
2704 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2707 if (!(dev->flags & DF_ENABLED)) {
2708 if (bootverbose && device_get_name(dev) != NULL) {
2709 device_print_prettyname(dev);
2710 printf("not probed (disabled)\n");
2714 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2715 if (bus_current_pass == BUS_PASS_DEFAULT &&
2716 !(dev->flags & DF_DONENOMATCH)) {
2717 BUS_PROBE_NOMATCH(dev->parent, dev);
2719 dev->flags |= DF_DONENOMATCH;
2727 * @brief Probe a device and attach a driver if possible
2729 * calls device_probe() and attaches if that was successful.
2732 device_probe_and_attach(device_t dev)
2738 error = device_probe(dev);
2741 else if (error != 0)
2744 CURVNET_SET_QUIET(vnet0);
2745 error = device_attach(dev);
2751 * @brief Attach a device driver to a device
2753 * This function is a wrapper around the DEVICE_ATTACH() driver
2754 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2755 * device's sysctl tree, optionally prints a description of the device
2756 * and queues a notification event for user-based device management
2759 * Normally this function is only called internally from
2760 * device_probe_and_attach().
2762 * @param dev the device to initialise
2765 * @retval ENXIO no driver was found
2766 * @retval ENOMEM memory allocation failure
2767 * @retval non-zero some other unix error code
2770 device_attach(device_t dev)
2772 uint64_t attachtime;
2775 if (resource_disabled(dev->driver->name, dev->unit)) {
2776 device_disable(dev);
2778 device_printf(dev, "disabled via hints entry\n");
2782 device_sysctl_init(dev);
2783 if (!device_is_quiet(dev))
2784 device_print_child(dev->parent, dev);
2785 attachtime = get_cyclecount();
2786 dev->state = DS_ATTACHING;
2787 if ((error = DEVICE_ATTACH(dev)) != 0) {
2788 printf("device_attach: %s%d attach returned %d\n",
2789 dev->driver->name, dev->unit, error);
2790 if (!(dev->flags & DF_FIXEDCLASS))
2791 devclass_delete_device(dev->devclass, dev);
2792 (void)device_set_driver(dev, NULL);
2793 device_sysctl_fini(dev);
2794 KASSERT(dev->busy == 0, ("attach failed but busy"));
2795 dev->state = DS_NOTPRESENT;
2798 attachtime = get_cyclecount() - attachtime;
2800 * 4 bits per device is a reasonable value for desktop and server
2801 * hardware with good get_cyclecount() implementations, but may
2802 * need to be adjusted on other platforms.
2805 printf("%s(): feeding %d bit(s) of entropy from %s%d\n",
2806 __func__, 4, dev->driver->name, dev->unit);
2808 random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH);
2809 device_sysctl_update(dev);
2811 dev->state = DS_BUSY;
2813 dev->state = DS_ATTACHED;
2814 dev->flags &= ~DF_DONENOMATCH;
2820 * @brief Detach a driver from a device
2822 * This function is a wrapper around the DEVICE_DETACH() driver
2823 * method. If the call to DEVICE_DETACH() succeeds, it calls
2824 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2825 * notification event for user-based device management services and
2826 * cleans up the device's sysctl tree.
2828 * @param dev the device to un-initialise
2831 * @retval ENXIO no driver was found
2832 * @retval ENOMEM memory allocation failure
2833 * @retval non-zero some other unix error code
2836 device_detach(device_t dev)
2842 PDEBUG(("%s", DEVICENAME(dev)));
2843 if (dev->state == DS_BUSY)
2845 if (dev->state != DS_ATTACHED)
2848 if ((error = DEVICE_DETACH(dev)) != 0)
2851 if (!device_is_quiet(dev))
2852 device_printf(dev, "detached\n");
2854 BUS_CHILD_DETACHED(dev->parent, dev);
2856 if (!(dev->flags & DF_FIXEDCLASS))
2857 devclass_delete_device(dev->devclass, dev);
2859 dev->state = DS_NOTPRESENT;
2860 (void)device_set_driver(dev, NULL);
2861 device_sysctl_fini(dev);
2867 * @brief Tells a driver to quiesce itself.
2869 * This function is a wrapper around the DEVICE_QUIESCE() driver
2870 * method. If the call to DEVICE_QUIESCE() succeeds.
2872 * @param dev the device to quiesce
2875 * @retval ENXIO no driver was found
2876 * @retval ENOMEM memory allocation failure
2877 * @retval non-zero some other unix error code
2880 device_quiesce(device_t dev)
2883 PDEBUG(("%s", DEVICENAME(dev)));
2884 if (dev->state == DS_BUSY)
2886 if (dev->state != DS_ATTACHED)
2889 return (DEVICE_QUIESCE(dev));
2893 * @brief Notify a device of system shutdown
2895 * This function calls the DEVICE_SHUTDOWN() driver method if the
2896 * device currently has an attached driver.
2898 * @returns the value returned by DEVICE_SHUTDOWN()
2901 device_shutdown(device_t dev)
2903 if (dev->state < DS_ATTACHED)
2905 return (DEVICE_SHUTDOWN(dev));
2909 * @brief Set the unit number of a device
2911 * This function can be used to override the unit number used for a
2912 * device (e.g. to wire a device to a pre-configured unit number).
2915 device_set_unit(device_t dev, int unit)
2920 dc = device_get_devclass(dev);
2921 if (unit < dc->maxunit && dc->devices[unit])
2923 err = devclass_delete_device(dc, dev);
2927 err = devclass_add_device(dc, dev);
2931 bus_data_generation_update();
2935 /*======================================*/
2937 * Some useful method implementations to make life easier for bus drivers.
2941 * @brief Initialise a resource list.
2943 * @param rl the resource list to initialise
2946 resource_list_init(struct resource_list *rl)
2952 * @brief Reclaim memory used by a resource list.
2954 * This function frees the memory for all resource entries on the list
2957 * @param rl the resource list to free
2960 resource_list_free(struct resource_list *rl)
2962 struct resource_list_entry *rle;
2964 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2966 panic("resource_list_free: resource entry is busy");
2967 STAILQ_REMOVE_HEAD(rl, link);
2973 * @brief Add a resource entry.
2975 * This function adds a resource entry using the given @p type, @p
2976 * start, @p end and @p count values. A rid value is chosen by
2977 * searching sequentially for the first unused rid starting at zero.
2979 * @param rl the resource list to edit
2980 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2981 * @param start the start address of the resource
2982 * @param end the end address of the resource
2983 * @param count XXX end-start+1
2986 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2987 u_long end, u_long count)
2992 while (resource_list_find(rl, type, rid) != NULL)
2994 resource_list_add(rl, type, rid, start, end, count);
2999 * @brief Add or modify a resource entry.
3001 * If an existing entry exists with the same type and rid, it will be
3002 * modified using the given values of @p start, @p end and @p
3003 * count. If no entry exists, a new one will be created using the
3004 * given values. The resource list entry that matches is then returned.
3006 * @param rl the resource list to edit
3007 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3008 * @param rid the resource identifier
3009 * @param start the start address of the resource
3010 * @param end the end address of the resource
3011 * @param count XXX end-start+1
3013 struct resource_list_entry *
3014 resource_list_add(struct resource_list *rl, int type, int rid,
3015 u_long start, u_long end, u_long count)
3017 struct resource_list_entry *rle;
3019 rle = resource_list_find(rl, type, rid);
3021 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3024 panic("resource_list_add: can't record entry");
3025 STAILQ_INSERT_TAIL(rl, rle, link);
3033 panic("resource_list_add: resource entry is busy");
3042 * @brief Determine if a resource entry is busy.
3044 * Returns true if a resource entry is busy meaning that it has an
3045 * associated resource that is not an unallocated "reserved" resource.
3047 * @param rl the resource list to search
3048 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3049 * @param rid the resource identifier
3051 * @returns Non-zero if the entry is busy, zero otherwise.
3054 resource_list_busy(struct resource_list *rl, int type, int rid)
3056 struct resource_list_entry *rle;
3058 rle = resource_list_find(rl, type, rid);
3059 if (rle == NULL || rle->res == NULL)
3061 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3062 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3063 ("reserved resource is active"));
3070 * @brief Determine if a resource entry is reserved.
3072 * Returns true if a resource entry is reserved meaning that it has an
3073 * associated "reserved" resource. The resource can either be
3074 * allocated or unallocated.
3076 * @param rl the resource list to search
3077 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3078 * @param rid the resource identifier
3080 * @returns Non-zero if the entry is reserved, zero otherwise.
3083 resource_list_reserved(struct resource_list *rl, int type, int rid)
3085 struct resource_list_entry *rle;
3087 rle = resource_list_find(rl, type, rid);
3088 if (rle != NULL && rle->flags & RLE_RESERVED)
3094 * @brief Find a resource entry by type and rid.
3096 * @param rl the resource list to search
3097 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3098 * @param rid the resource identifier
3100 * @returns the resource entry pointer or NULL if there is no such
3103 struct resource_list_entry *
3104 resource_list_find(struct resource_list *rl, int type, int rid)
3106 struct resource_list_entry *rle;
3108 STAILQ_FOREACH(rle, rl, link) {
3109 if (rle->type == type && rle->rid == rid)
3116 * @brief Delete a resource entry.
3118 * @param rl the resource list to edit
3119 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3120 * @param rid the resource identifier
3123 resource_list_delete(struct resource_list *rl, int type, int rid)
3125 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3128 if (rle->res != NULL)
3129 panic("resource_list_delete: resource has not been released");
3130 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3136 * @brief Allocate a reserved resource
3138 * This can be used by busses to force the allocation of resources
3139 * that are always active in the system even if they are not allocated
3140 * by a driver (e.g. PCI BARs). This function is usually called when
3141 * adding a new child to the bus. The resource is allocated from the
3142 * parent bus when it is reserved. The resource list entry is marked
3143 * with RLE_RESERVED to note that it is a reserved resource.
3145 * Subsequent attempts to allocate the resource with
3146 * resource_list_alloc() will succeed the first time and will set
3147 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3148 * resource that has been allocated is released with
3149 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3150 * the actual resource remains allocated. The resource can be released to
3151 * the parent bus by calling resource_list_unreserve().
3153 * @param rl the resource list to allocate from
3154 * @param bus the parent device of @p child
3155 * @param child the device for which the resource is being reserved
3156 * @param type the type of resource to allocate
3157 * @param rid a pointer to the resource identifier
3158 * @param start hint at the start of the resource range - pass
3159 * @c 0UL for any start address
3160 * @param end hint at the end of the resource range - pass
3161 * @c ~0UL for any end address
3162 * @param count hint at the size of range required - pass @c 1
3164 * @param flags any extra flags to control the resource
3165 * allocation - see @c RF_XXX flags in
3166 * <sys/rman.h> for details
3168 * @returns the resource which was allocated or @c NULL if no
3169 * resource could be allocated
3172 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3173 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3175 struct resource_list_entry *rle = NULL;
3176 int passthrough = (device_get_parent(child) != bus);
3181 "resource_list_reserve() should only be called for direct children");
3182 if (flags & RF_ACTIVE)
3184 "resource_list_reserve() should only reserve inactive resources");
3186 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3189 rle = resource_list_find(rl, type, *rid);
3190 rle->flags |= RLE_RESERVED;
3196 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3198 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3199 * and passing the allocation up to the parent of @p bus. This assumes
3200 * that the first entry of @c device_get_ivars(child) is a struct
3201 * resource_list. This also handles 'passthrough' allocations where a
3202 * child is a remote descendant of bus by passing the allocation up to
3203 * the parent of bus.
3205 * Typically, a bus driver would store a list of child resources
3206 * somewhere in the child device's ivars (see device_get_ivars()) and
3207 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3208 * then call resource_list_alloc() to perform the allocation.
3210 * @param rl the resource list to allocate from
3211 * @param bus the parent device of @p child
3212 * @param child the device which is requesting an allocation
3213 * @param type the type of resource to allocate
3214 * @param rid a pointer to the resource identifier
3215 * @param start hint at the start of the resource range - pass
3216 * @c 0UL for any start address
3217 * @param end hint at the end of the resource range - pass
3218 * @c ~0UL for any end address
3219 * @param count hint at the size of range required - pass @c 1
3221 * @param flags any extra flags to control the resource
3222 * allocation - see @c RF_XXX flags in
3223 * <sys/rman.h> for details
3225 * @returns the resource which was allocated or @c NULL if no
3226 * resource could be allocated
3229 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3230 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3232 struct resource_list_entry *rle = NULL;
3233 int passthrough = (device_get_parent(child) != bus);
3234 int isdefault = (start == 0UL && end == ~0UL);
3237 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3238 type, rid, start, end, count, flags));
3241 rle = resource_list_find(rl, type, *rid);
3244 return (NULL); /* no resource of that type/rid */
3247 if (rle->flags & RLE_RESERVED) {
3248 if (rle->flags & RLE_ALLOCATED)
3250 if ((flags & RF_ACTIVE) &&
3251 bus_activate_resource(child, type, *rid,
3254 rle->flags |= RLE_ALLOCATED;
3257 panic("resource_list_alloc: resource entry is busy");
3262 count = ulmax(count, rle->count);
3263 end = ulmax(rle->end, start + count - 1);
3266 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3267 type, rid, start, end, count, flags);
3270 * Record the new range.
3273 rle->start = rman_get_start(rle->res);
3274 rle->end = rman_get_end(rle->res);
3282 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3284 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3285 * used with resource_list_alloc().
3287 * @param rl the resource list which was allocated from
3288 * @param bus the parent device of @p child
3289 * @param child the device which is requesting a release
3290 * @param type the type of resource to release
3291 * @param rid the resource identifier
3292 * @param res the resource to release
3295 * @retval non-zero a standard unix error code indicating what
3296 * error condition prevented the operation
3299 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3300 int type, int rid, struct resource *res)
3302 struct resource_list_entry *rle = NULL;
3303 int passthrough = (device_get_parent(child) != bus);
3307 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3311 rle = resource_list_find(rl, type, rid);
3314 panic("resource_list_release: can't find resource");
3316 panic("resource_list_release: resource entry is not busy");
3317 if (rle->flags & RLE_RESERVED) {
3318 if (rle->flags & RLE_ALLOCATED) {
3319 if (rman_get_flags(res) & RF_ACTIVE) {
3320 error = bus_deactivate_resource(child, type,
3325 rle->flags &= ~RLE_ALLOCATED;
3331 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3341 * @brief Release all active resources of a given type
3343 * Release all active resources of a specified type. This is intended
3344 * to be used to cleanup resources leaked by a driver after detach or
3347 * @param rl the resource list which was allocated from
3348 * @param bus the parent device of @p child
3349 * @param child the device whose active resources are being released
3350 * @param type the type of resources to release
3353 * @retval EBUSY at least one resource was active
3356 resource_list_release_active(struct resource_list *rl, device_t bus,
3357 device_t child, int type)
3359 struct resource_list_entry *rle;
3363 STAILQ_FOREACH(rle, rl, link) {
3364 if (rle->type != type)
3366 if (rle->res == NULL)
3368 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3372 error = resource_list_release(rl, bus, child, type,
3373 rman_get_rid(rle->res), rle->res);
3376 "Failed to release active resource: %d\n", error);
3383 * @brief Fully release a reserved resource
3385 * Fully releases a resource reserved via resource_list_reserve().
3387 * @param rl the resource list which was allocated from
3388 * @param bus the parent device of @p child
3389 * @param child the device whose reserved resource is being released
3390 * @param type the type of resource to release
3391 * @param rid the resource identifier
3392 * @param res the resource to release
3395 * @retval non-zero a standard unix error code indicating what
3396 * error condition prevented the operation
3399 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3402 struct resource_list_entry *rle = NULL;
3403 int passthrough = (device_get_parent(child) != bus);
3407 "resource_list_unreserve() should only be called for direct children");
3409 rle = resource_list_find(rl, type, rid);
3412 panic("resource_list_unreserve: can't find resource");
3413 if (!(rle->flags & RLE_RESERVED))
3415 if (rle->flags & RLE_ALLOCATED)
3417 rle->flags &= ~RLE_RESERVED;
3418 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3422 * @brief Print a description of resources in a resource list
3424 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3425 * The name is printed if at least one resource of the given type is available.
3426 * The format is used to print resource start and end.
3428 * @param rl the resource list to print
3429 * @param name the name of @p type, e.g. @c "memory"
3430 * @param type type type of resource entry to print
3431 * @param format printf(9) format string to print resource
3432 * start and end values
3434 * @returns the number of characters printed
3437 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3440 struct resource_list_entry *rle;
3441 int printed, retval;
3445 /* Yes, this is kinda cheating */
3446 STAILQ_FOREACH(rle, rl, link) {
3447 if (rle->type == type) {
3449 retval += printf(" %s ", name);
3451 retval += printf(",");
3453 retval += printf(format, rle->start);
3454 if (rle->count > 1) {
3455 retval += printf("-");
3456 retval += printf(format, rle->start +
3465 * @brief Releases all the resources in a list.
3467 * @param rl The resource list to purge.
3472 resource_list_purge(struct resource_list *rl)
3474 struct resource_list_entry *rle;
3476 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3478 bus_release_resource(rman_get_device(rle->res),
3479 rle->type, rle->rid, rle->res);
3480 STAILQ_REMOVE_HEAD(rl, link);
3486 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3489 return (device_add_child_ordered(dev, order, name, unit));
3493 * @brief Helper function for implementing DEVICE_PROBE()
3495 * This function can be used to help implement the DEVICE_PROBE() for
3496 * a bus (i.e. a device which has other devices attached to it). It
3497 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3501 bus_generic_probe(device_t dev)
3503 devclass_t dc = dev->devclass;
3506 TAILQ_FOREACH(dl, &dc->drivers, link) {
3508 * If this driver's pass is too high, then ignore it.
3509 * For most drivers in the default pass, this will
3510 * never be true. For early-pass drivers they will
3511 * only call the identify routines of eligible drivers
3512 * when this routine is called. Drivers for later
3513 * passes should have their identify routines called
3514 * on early-pass busses during BUS_NEW_PASS().
3516 if (dl->pass > bus_current_pass)
3518 DEVICE_IDENTIFY(dl->driver, dev);
3525 * @brief Helper function for implementing DEVICE_ATTACH()
3527 * This function can be used to help implement the DEVICE_ATTACH() for
3528 * a bus. It calls device_probe_and_attach() for each of the device's
3532 bus_generic_attach(device_t dev)
3536 TAILQ_FOREACH(child, &dev->children, link) {
3537 device_probe_and_attach(child);
3544 * @brief Helper function for implementing DEVICE_DETACH()
3546 * This function can be used to help implement the DEVICE_DETACH() for
3547 * a bus. It calls device_detach() for each of the device's
3551 bus_generic_detach(device_t dev)
3556 if (dev->state != DS_ATTACHED)
3559 TAILQ_FOREACH(child, &dev->children, link) {
3560 if ((error = device_detach(child)) != 0)
3568 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3570 * This function can be used to help implement the DEVICE_SHUTDOWN()
3571 * for a bus. It calls device_shutdown() for each of the device's
3575 bus_generic_shutdown(device_t dev)
3579 TAILQ_FOREACH(child, &dev->children, link) {
3580 device_shutdown(child);
3587 * @brief Helper function for implementing DEVICE_SUSPEND()
3589 * This function can be used to help implement the DEVICE_SUSPEND()
3590 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3591 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3592 * operation is aborted and any devices which were suspended are
3593 * resumed immediately by calling their DEVICE_RESUME() methods.
3596 bus_generic_suspend(device_t dev)
3599 device_t child, child2;
3601 TAILQ_FOREACH(child, &dev->children, link) {
3602 error = DEVICE_SUSPEND(child);
3604 for (child2 = TAILQ_FIRST(&dev->children);
3605 child2 && child2 != child;
3606 child2 = TAILQ_NEXT(child2, link))
3607 DEVICE_RESUME(child2);
3615 * @brief Helper function for implementing DEVICE_RESUME()
3617 * This function can be used to help implement the DEVICE_RESUME() for
3618 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3621 bus_generic_resume(device_t dev)
3625 TAILQ_FOREACH(child, &dev->children, link) {
3626 DEVICE_RESUME(child);
3627 /* if resume fails, there's nothing we can usefully do... */
3633 * @brief Helper function for implementing BUS_PRINT_CHILD().
3635 * This function prints the first part of the ascii representation of
3636 * @p child, including its name, unit and description (if any - see
3637 * device_set_desc()).
3639 * @returns the number of characters printed
3642 bus_print_child_header(device_t dev, device_t child)
3646 if (device_get_desc(child)) {
3647 retval += device_printf(child, "<%s>", device_get_desc(child));
3649 retval += printf("%s", device_get_nameunit(child));
3656 * @brief Helper function for implementing BUS_PRINT_CHILD().
3658 * This function prints the last part of the ascii representation of
3659 * @p child, which consists of the string @c " on " followed by the
3660 * name and unit of the @p dev.
3662 * @returns the number of characters printed
3665 bus_print_child_footer(device_t dev, device_t child)
3667 return (printf(" on %s\n", device_get_nameunit(dev)));
3671 * @brief Helper function for implementing BUS_PRINT_CHILD().
3673 * This function simply calls bus_print_child_header() followed by
3674 * bus_print_child_footer().
3676 * @returns the number of characters printed
3679 bus_generic_print_child(device_t dev, device_t child)
3683 retval += bus_print_child_header(dev, child);
3684 retval += bus_print_child_footer(dev, child);
3690 * @brief Stub function for implementing BUS_READ_IVAR().
3695 bus_generic_read_ivar(device_t dev, device_t child, int index,
3702 * @brief Stub function for implementing BUS_WRITE_IVAR().
3707 bus_generic_write_ivar(device_t dev, device_t child, int index,
3714 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3718 struct resource_list *
3719 bus_generic_get_resource_list(device_t dev, device_t child)
3725 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3727 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3728 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3729 * and then calls device_probe_and_attach() for each unattached child.
3732 bus_generic_driver_added(device_t dev, driver_t *driver)
3736 DEVICE_IDENTIFY(driver, dev);
3737 TAILQ_FOREACH(child, &dev->children, link) {
3738 if (child->state == DS_NOTPRESENT ||
3739 (child->flags & DF_REBID))
3740 device_probe_and_attach(child);
3745 * @brief Helper function for implementing BUS_NEW_PASS().
3747 * This implementing of BUS_NEW_PASS() first calls the identify
3748 * routines for any drivers that probe at the current pass. Then it
3749 * walks the list of devices for this bus. If a device is already
3750 * attached, then it calls BUS_NEW_PASS() on that device. If the
3751 * device is not already attached, it attempts to attach a driver to
3755 bus_generic_new_pass(device_t dev)
3762 TAILQ_FOREACH(dl, &dc->drivers, link) {
3763 if (dl->pass == bus_current_pass)
3764 DEVICE_IDENTIFY(dl->driver, dev);
3766 TAILQ_FOREACH(child, &dev->children, link) {
3767 if (child->state >= DS_ATTACHED)
3768 BUS_NEW_PASS(child);
3769 else if (child->state == DS_NOTPRESENT)
3770 device_probe_and_attach(child);
3775 * @brief Helper function for implementing BUS_SETUP_INTR().
3777 * This simple implementation of BUS_SETUP_INTR() simply calls the
3778 * BUS_SETUP_INTR() method of the parent of @p dev.
3781 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3782 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3785 /* Propagate up the bus hierarchy until someone handles it. */
3787 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3788 filter, intr, arg, cookiep));
3793 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3795 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3796 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3799 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3802 /* Propagate up the bus hierarchy until someone handles it. */
3804 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3809 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3811 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3812 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3815 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3816 struct resource *r, u_long start, u_long end)
3818 /* Propagate up the bus hierarchy until someone handles it. */
3820 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3826 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3828 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3829 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3832 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3833 u_long start, u_long end, u_long count, u_int flags)
3835 /* Propagate up the bus hierarchy until someone handles it. */
3837 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3838 start, end, count, flags));
3843 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3845 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3846 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3849 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3852 /* Propagate up the bus hierarchy until someone handles it. */
3854 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3860 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3862 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3863 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3866 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3869 /* Propagate up the bus hierarchy until someone handles it. */
3871 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3877 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3879 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3880 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3883 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3884 int rid, struct resource *r)
3886 /* Propagate up the bus hierarchy until someone handles it. */
3888 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3894 * @brief Helper function for implementing BUS_BIND_INTR().
3896 * This simple implementation of BUS_BIND_INTR() simply calls the
3897 * BUS_BIND_INTR() method of the parent of @p dev.
3900 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3904 /* Propagate up the bus hierarchy until someone handles it. */
3906 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3911 * @brief Helper function for implementing BUS_CONFIG_INTR().
3913 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3914 * BUS_CONFIG_INTR() method of the parent of @p dev.
3917 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3918 enum intr_polarity pol)
3921 /* Propagate up the bus hierarchy until someone handles it. */
3923 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3928 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3930 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3931 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3934 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3935 void *cookie, const char *descr)
3938 /* Propagate up the bus hierarchy until someone handles it. */
3940 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3946 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3948 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3949 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3952 bus_generic_get_dma_tag(device_t dev, device_t child)
3955 /* Propagate up the bus hierarchy until someone handles it. */
3956 if (dev->parent != NULL)
3957 return (BUS_GET_DMA_TAG(dev->parent, child));
3962 * @brief Helper function for implementing BUS_GET_RESOURCE().
3964 * This implementation of BUS_GET_RESOURCE() uses the
3965 * resource_list_find() function to do most of the work. It calls
3966 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3970 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3971 u_long *startp, u_long *countp)
3973 struct resource_list * rl = NULL;
3974 struct resource_list_entry * rle = NULL;
3976 rl = BUS_GET_RESOURCE_LIST(dev, child);
3980 rle = resource_list_find(rl, type, rid);
3985 *startp = rle->start;
3987 *countp = rle->count;
3993 * @brief Helper function for implementing BUS_SET_RESOURCE().
3995 * This implementation of BUS_SET_RESOURCE() uses the
3996 * resource_list_add() function to do most of the work. It calls
3997 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4001 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4002 u_long start, u_long count)
4004 struct resource_list * rl = NULL;
4006 rl = BUS_GET_RESOURCE_LIST(dev, child);
4010 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4016 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4018 * This implementation of BUS_DELETE_RESOURCE() uses the
4019 * resource_list_delete() function to do most of the work. It calls
4020 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4024 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4026 struct resource_list * rl = NULL;
4028 rl = BUS_GET_RESOURCE_LIST(dev, child);
4032 resource_list_delete(rl, type, rid);
4038 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4040 * This implementation of BUS_RELEASE_RESOURCE() uses the
4041 * resource_list_release() function to do most of the work. It calls
4042 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4045 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4046 int rid, struct resource *r)
4048 struct resource_list * rl = NULL;
4050 if (device_get_parent(child) != dev)
4051 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4054 rl = BUS_GET_RESOURCE_LIST(dev, child);
4058 return (resource_list_release(rl, dev, child, type, rid, r));
4062 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4064 * This implementation of BUS_ALLOC_RESOURCE() uses the
4065 * resource_list_alloc() function to do most of the work. It calls
4066 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4069 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4070 int *rid, u_long start, u_long end, u_long count, u_int flags)
4072 struct resource_list * rl = NULL;
4074 if (device_get_parent(child) != dev)
4075 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4076 type, rid, start, end, count, flags));
4078 rl = BUS_GET_RESOURCE_LIST(dev, child);
4082 return (resource_list_alloc(rl, dev, child, type, rid,
4083 start, end, count, flags));
4087 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4089 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4090 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4093 bus_generic_child_present(device_t dev, device_t child)
4095 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4099 * Some convenience functions to make it easier for drivers to use the
4100 * resource-management functions. All these really do is hide the
4101 * indirection through the parent's method table, making for slightly
4102 * less-wordy code. In the future, it might make sense for this code
4103 * to maintain some sort of a list of resources allocated by each device.
4107 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4108 struct resource **res)
4112 for (i = 0; rs[i].type != -1; i++)
4114 for (i = 0; rs[i].type != -1; i++) {
4115 res[i] = bus_alloc_resource_any(dev,
4116 rs[i].type, &rs[i].rid, rs[i].flags);
4117 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4118 bus_release_resources(dev, rs, res);
4126 bus_release_resources(device_t dev, const struct resource_spec *rs,
4127 struct resource **res)
4131 for (i = 0; rs[i].type != -1; i++)
4132 if (res[i] != NULL) {
4133 bus_release_resource(
4134 dev, rs[i].type, rs[i].rid, res[i]);
4140 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4142 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4146 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4147 u_long count, u_int flags)
4149 if (dev->parent == NULL)
4151 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4156 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4158 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4162 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4165 if (dev->parent == NULL)
4167 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4171 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4173 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4177 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4179 if (dev->parent == NULL)
4181 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4185 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4187 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4191 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4193 if (dev->parent == NULL)
4195 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4199 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4201 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4205 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4207 if (dev->parent == NULL)
4209 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4213 * @brief Wrapper function for BUS_SETUP_INTR().
4215 * This function simply calls the BUS_SETUP_INTR() method of the
4219 bus_setup_intr(device_t dev, struct resource *r, int flags,
4220 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4224 if (dev->parent == NULL)
4226 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4230 if (handler != NULL && !(flags & INTR_MPSAFE))
4231 device_printf(dev, "[GIANT-LOCKED]\n");
4236 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4238 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4242 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4244 if (dev->parent == NULL)
4246 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4250 * @brief Wrapper function for BUS_BIND_INTR().
4252 * This function simply calls the BUS_BIND_INTR() method of the
4256 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4258 if (dev->parent == NULL)
4260 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4264 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4266 * This function first formats the requested description into a
4267 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4268 * the parent of @p dev.
4271 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4272 const char *fmt, ...)
4275 char descr[MAXCOMLEN + 1];
4277 if (dev->parent == NULL)
4280 vsnprintf(descr, sizeof(descr), fmt, ap);
4282 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4286 * @brief Wrapper function for BUS_SET_RESOURCE().
4288 * This function simply calls the BUS_SET_RESOURCE() method of the
4292 bus_set_resource(device_t dev, int type, int rid,
4293 u_long start, u_long count)
4295 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4300 * @brief Wrapper function for BUS_GET_RESOURCE().
4302 * This function simply calls the BUS_GET_RESOURCE() method of the
4306 bus_get_resource(device_t dev, int type, int rid,
4307 u_long *startp, u_long *countp)
4309 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4314 * @brief Wrapper function for BUS_GET_RESOURCE().
4316 * This function simply calls the BUS_GET_RESOURCE() method of the
4317 * parent of @p dev and returns the start value.
4320 bus_get_resource_start(device_t dev, int type, int rid)
4322 u_long start, count;
4325 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4333 * @brief Wrapper function for BUS_GET_RESOURCE().
4335 * This function simply calls the BUS_GET_RESOURCE() method of the
4336 * parent of @p dev and returns the count value.
4339 bus_get_resource_count(device_t dev, int type, int rid)
4341 u_long start, count;
4344 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4352 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4354 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4358 bus_delete_resource(device_t dev, int type, int rid)
4360 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4364 * @brief Wrapper function for BUS_CHILD_PRESENT().
4366 * This function simply calls the BUS_CHILD_PRESENT() method of the
4370 bus_child_present(device_t child)
4372 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4376 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4378 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4382 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4386 parent = device_get_parent(child);
4387 if (parent == NULL) {
4391 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4395 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4397 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4401 bus_child_location_str(device_t child, char *buf, size_t buflen)
4405 parent = device_get_parent(child);
4406 if (parent == NULL) {
4410 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4414 * @brief Wrapper function for BUS_GET_DMA_TAG().
4416 * This function simply calls the BUS_GET_DMA_TAG() method of the
4420 bus_get_dma_tag(device_t dev)
4424 parent = device_get_parent(dev);
4427 return (BUS_GET_DMA_TAG(parent, dev));
4430 /* Resume all devices and then notify userland that we're up again. */
4432 root_resume(device_t dev)
4436 error = bus_generic_resume(dev);
4438 devctl_notify("kern", "power", "resume", NULL);
4443 root_print_child(device_t dev, device_t child)
4447 retval += bus_print_child_header(dev, child);
4448 retval += printf("\n");
4454 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4455 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4458 * If an interrupt mapping gets to here something bad has happened.
4460 panic("root_setup_intr");
4464 * If we get here, assume that the device is permanant and really is
4465 * present in the system. Removable bus drivers are expected to intercept
4466 * this call long before it gets here. We return -1 so that drivers that
4467 * really care can check vs -1 or some ERRNO returned higher in the food
4471 root_child_present(device_t dev, device_t child)
4476 static kobj_method_t root_methods[] = {
4477 /* Device interface */
4478 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4479 KOBJMETHOD(device_suspend, bus_generic_suspend),
4480 KOBJMETHOD(device_resume, root_resume),
4483 KOBJMETHOD(bus_print_child, root_print_child),
4484 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4485 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4486 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4487 KOBJMETHOD(bus_child_present, root_child_present),
4492 static driver_t root_driver = {
4499 devclass_t root_devclass;
4502 root_bus_module_handler(module_t mod, int what, void* arg)
4506 TAILQ_INIT(&bus_data_devices);
4507 kobj_class_compile((kobj_class_t) &root_driver);
4508 root_bus = make_device(NULL, "root", 0);
4509 root_bus->desc = "System root bus";
4510 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4511 root_bus->driver = &root_driver;
4512 root_bus->state = DS_ATTACHED;
4513 root_devclass = devclass_find_internal("root", NULL, FALSE);
4518 device_shutdown(root_bus);
4521 return (EOPNOTSUPP);
4527 static moduledata_t root_bus_mod = {
4529 root_bus_module_handler,
4532 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4535 * @brief Automatically configure devices
4537 * This function begins the autoconfiguration process by calling
4538 * device_probe_and_attach() for each child of the @c root0 device.
4541 root_bus_configure(void)
4546 /* Eventually this will be split up, but this is sufficient for now. */
4547 bus_set_pass(BUS_PASS_DEFAULT);
4551 * @brief Module handler for registering device drivers
4553 * This module handler is used to automatically register device
4554 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4555 * devclass_add_driver() for the driver described by the
4556 * driver_module_data structure pointed to by @p arg
4559 driver_module_handler(module_t mod, int what, void *arg)
4561 struct driver_module_data *dmd;
4562 devclass_t bus_devclass;
4563 kobj_class_t driver;
4566 dmd = (struct driver_module_data *)arg;
4567 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4572 if (dmd->dmd_chainevh)
4573 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4575 pass = dmd->dmd_pass;
4576 driver = dmd->dmd_driver;
4577 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4578 DRIVERNAME(driver), dmd->dmd_busname, pass));
4579 error = devclass_add_driver(bus_devclass, driver, pass,
4584 PDEBUG(("Unloading module: driver %s from bus %s",
4585 DRIVERNAME(dmd->dmd_driver),
4587 error = devclass_delete_driver(bus_devclass,
4590 if (!error && dmd->dmd_chainevh)
4591 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4594 PDEBUG(("Quiesce module: driver %s from bus %s",
4595 DRIVERNAME(dmd->dmd_driver),
4597 error = devclass_quiesce_driver(bus_devclass,
4600 if (!error && dmd->dmd_chainevh)
4601 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4612 * @brief Enumerate all hinted devices for this bus.
4614 * Walks through the hints for this bus and calls the bus_hinted_child
4615 * routine for each one it fines. It searches first for the specific
4616 * bus that's being probed for hinted children (eg isa0), and then for
4617 * generic children (eg isa).
4619 * @param dev bus device to enumerate
4622 bus_enumerate_hinted_children(device_t bus)
4625 const char *dname, *busname;
4629 * enumerate all devices on the specific bus
4631 busname = device_get_nameunit(bus);
4633 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4634 BUS_HINTED_CHILD(bus, dname, dunit);
4637 * and all the generic ones.
4639 busname = device_get_name(bus);
4641 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4642 BUS_HINTED_CHILD(bus, dname, dunit);
4647 /* the _short versions avoid iteration by not calling anything that prints
4648 * more than oneliners. I love oneliners.
4652 print_device_short(device_t dev, int indent)
4657 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4658 dev->unit, dev->desc,
4659 (dev->parent? "":"no "),
4660 (TAILQ_EMPTY(&dev->children)? "no ":""),
4661 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4662 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4663 (dev->flags&DF_WILDCARD? "wildcard,":""),
4664 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4665 (dev->flags&DF_REBID? "rebiddable,":""),
4666 (dev->ivars? "":"no "),
4667 (dev->softc? "":"no "),
4672 print_device(device_t dev, int indent)
4677 print_device_short(dev, indent);
4679 indentprintf(("Parent:\n"));
4680 print_device_short(dev->parent, indent+1);
4681 indentprintf(("Driver:\n"));
4682 print_driver_short(dev->driver, indent+1);
4683 indentprintf(("Devclass:\n"));
4684 print_devclass_short(dev->devclass, indent+1);
4688 print_device_tree_short(device_t dev, int indent)
4689 /* print the device and all its children (indented) */
4696 print_device_short(dev, indent);
4698 TAILQ_FOREACH(child, &dev->children, link) {
4699 print_device_tree_short(child, indent+1);
4704 print_device_tree(device_t dev, int indent)
4705 /* print the device and all its children (indented) */
4712 print_device(dev, indent);
4714 TAILQ_FOREACH(child, &dev->children, link) {
4715 print_device_tree(child, indent+1);
4720 print_driver_short(driver_t *driver, int indent)
4725 indentprintf(("driver %s: softc size = %zd\n",
4726 driver->name, driver->size));
4730 print_driver(driver_t *driver, int indent)
4735 print_driver_short(driver, indent);
4739 print_driver_list(driver_list_t drivers, int indent)
4741 driverlink_t driver;
4743 TAILQ_FOREACH(driver, &drivers, link) {
4744 print_driver(driver->driver, indent);
4749 print_devclass_short(devclass_t dc, int indent)
4754 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4758 print_devclass(devclass_t dc, int indent)
4765 print_devclass_short(dc, indent);
4766 indentprintf(("Drivers:\n"));
4767 print_driver_list(dc->drivers, indent+1);
4769 indentprintf(("Devices:\n"));
4770 for (i = 0; i < dc->maxunit; i++)
4772 print_device(dc->devices[i], indent+1);
4776 print_devclass_list_short(void)
4780 printf("Short listing of devclasses, drivers & devices:\n");
4781 TAILQ_FOREACH(dc, &devclasses, link) {
4782 print_devclass_short(dc, 0);
4787 print_devclass_list(void)
4791 printf("Full listing of devclasses, drivers & devices:\n");
4792 TAILQ_FOREACH(dc, &devclasses, link) {
4793 print_devclass(dc, 0);
4800 * User-space access to the device tree.
4802 * We implement a small set of nodes:
4804 * hw.bus Single integer read method to obtain the
4805 * current generation count.
4806 * hw.bus.devices Reads the entire device tree in flat space.
4807 * hw.bus.rman Resource manager interface
4809 * We might like to add the ability to scan devclasses and/or drivers to
4810 * determine what else is currently loaded/available.
4814 sysctl_bus(SYSCTL_HANDLER_ARGS)
4816 struct u_businfo ubus;
4818 ubus.ub_version = BUS_USER_VERSION;
4819 ubus.ub_generation = bus_data_generation;
4821 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4823 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4824 "bus-related data");
4827 sysctl_devices(SYSCTL_HANDLER_ARGS)
4829 int *name = (int *)arg1;
4830 u_int namelen = arg2;
4833 struct u_device udev; /* XXX this is a bit big */
4839 if (bus_data_generation_check(name[0]))
4845 * Scan the list of devices, looking for the requested index.
4847 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4855 * Populate the return array.
4857 bzero(&udev, sizeof(udev));
4858 udev.dv_handle = (uintptr_t)dev;
4859 udev.dv_parent = (uintptr_t)dev->parent;
4860 if (dev->nameunit != NULL)
4861 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4862 if (dev->desc != NULL)
4863 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4864 if (dev->driver != NULL && dev->driver->name != NULL)
4865 strlcpy(udev.dv_drivername, dev->driver->name,
4866 sizeof(udev.dv_drivername));
4867 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4868 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4869 udev.dv_devflags = dev->devflags;
4870 udev.dv_flags = dev->flags;
4871 udev.dv_state = dev->state;
4872 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4876 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4877 "system device tree");
4880 bus_data_generation_check(int generation)
4882 if (generation != bus_data_generation)
4885 /* XXX generate optimised lists here? */
4890 bus_data_generation_update(void)
4892 bus_data_generation++;
4896 bus_free_resource(device_t dev, int type, struct resource *r)
4900 return (bus_release_resource(dev, type, rman_get_rid(r), r));