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_ROOT_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 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
155 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
156 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
157 #define DRIVERNAME(d) ((d)? d->name : "no driver")
158 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
161 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
162 * prevent syslog from deleting initial spaces
164 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
166 static void print_device_short(device_t dev, int indent);
167 static void print_device(device_t dev, int indent);
168 void print_device_tree_short(device_t dev, int indent);
169 void print_device_tree(device_t dev, int indent);
170 static void print_driver_short(driver_t *driver, int indent);
171 static void print_driver(driver_t *driver, int indent);
172 static void print_driver_list(driver_list_t drivers, int indent);
173 static void print_devclass_short(devclass_t dc, int indent);
174 static void print_devclass(devclass_t dc, int indent);
175 void print_devclass_list_short(void);
176 void print_devclass_list(void);
179 /* Make the compiler ignore the function calls */
180 #define PDEBUG(a) /* nop */
181 #define DEVICENAME(d) /* nop */
182 #define DRIVERNAME(d) /* nop */
183 #define DEVCLANAME(d) /* nop */
185 #define print_device_short(d,i) /* nop */
186 #define print_device(d,i) /* nop */
187 #define print_device_tree_short(d,i) /* nop */
188 #define print_device_tree(d,i) /* nop */
189 #define print_driver_short(d,i) /* nop */
190 #define print_driver(d,i) /* nop */
191 #define print_driver_list(d,i) /* nop */
192 #define print_devclass_short(d,i) /* nop */
193 #define print_devclass(d,i) /* nop */
194 #define print_devclass_list_short() /* nop */
195 #define print_devclass_list() /* nop */
203 DEVCLASS_SYSCTL_PARENT,
207 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
209 devclass_t dc = (devclass_t)arg1;
213 case DEVCLASS_SYSCTL_PARENT:
214 value = dc->parent ? dc->parent->name : "";
219 return (SYSCTL_OUT(req, value, strlen(value)));
223 devclass_sysctl_init(devclass_t dc)
226 if (dc->sysctl_tree != NULL)
228 sysctl_ctx_init(&dc->sysctl_ctx);
229 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
230 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
231 CTLFLAG_RD, NULL, "");
232 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
233 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
234 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
240 DEVICE_SYSCTL_DRIVER,
241 DEVICE_SYSCTL_LOCATION,
242 DEVICE_SYSCTL_PNPINFO,
243 DEVICE_SYSCTL_PARENT,
247 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
249 device_t dev = (device_t)arg1;
256 case DEVICE_SYSCTL_DESC:
257 value = dev->desc ? dev->desc : "";
259 case DEVICE_SYSCTL_DRIVER:
260 value = dev->driver ? dev->driver->name : "";
262 case DEVICE_SYSCTL_LOCATION:
263 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
264 bus_child_location_str(dev, buf, 1024);
266 case DEVICE_SYSCTL_PNPINFO:
267 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
268 bus_child_pnpinfo_str(dev, buf, 1024);
270 case DEVICE_SYSCTL_PARENT:
271 value = dev->parent ? dev->parent->nameunit : "";
276 error = SYSCTL_OUT(req, value, strlen(value));
283 device_sysctl_init(device_t dev)
285 devclass_t dc = dev->devclass;
287 if (dev->sysctl_tree != NULL)
289 devclass_sysctl_init(dc);
290 sysctl_ctx_init(&dev->sysctl_ctx);
291 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
292 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
293 dev->nameunit + strlen(dc->name),
294 CTLFLAG_RD, NULL, "");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
298 "device description");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
302 "device driver name");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
306 "device location relative to parent");
307 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
309 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
310 "device identification");
311 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
312 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
313 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
318 device_sysctl_update(device_t dev)
320 devclass_t dc = dev->devclass;
322 if (dev->sysctl_tree == NULL)
324 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
328 device_sysctl_fini(device_t dev)
330 if (dev->sysctl_tree == NULL)
332 sysctl_ctx_free(&dev->sysctl_ctx);
333 dev->sysctl_tree = NULL;
337 * /dev/devctl implementation
341 * This design allows only one reader for /dev/devctl. This is not desirable
342 * in the long run, but will get a lot of hair out of this implementation.
343 * Maybe we should make this device a clonable device.
345 * Also note: we specifically do not attach a device to the device_t tree
346 * to avoid potential chicken and egg problems. One could argue that all
347 * of this belongs to the root node. One could also further argue that the
348 * sysctl interface that we have not might more properly be an ioctl
349 * interface, but at this stage of the game, I'm not inclined to rock that
352 * I'm also not sure that the SIGIO support is done correctly or not, as
353 * I copied it from a driver that had SIGIO support that likely hasn't been
354 * tested since 3.4 or 2.2.8!
357 /* Deprecated way to adjust queue length */
358 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
359 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
360 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_disable, "I",
361 "devctl disable -- deprecated");
363 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
364 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
365 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
366 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
367 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
369 static d_open_t devopen;
370 static d_close_t devclose;
371 static d_read_t devread;
372 static d_ioctl_t devioctl;
373 static d_poll_t devpoll;
374 static d_kqfilter_t devkqfilter;
376 static struct cdevsw dev_cdevsw = {
377 .d_version = D_VERSION,
383 .d_kqfilter = devkqfilter,
387 struct dev_event_info
390 TAILQ_ENTRY(dev_event_info) dei_link;
393 TAILQ_HEAD(devq, dev_event_info);
395 static struct dev_softc
408 static void filt_devctl_detach(struct knote *kn);
409 static int filt_devctl_read(struct knote *kn, long hint);
411 struct filterops devctl_rfiltops = {
413 .f_detach = filt_devctl_detach,
414 .f_event = filt_devctl_read,
417 static struct cdev *devctl_dev;
422 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
423 UID_ROOT, GID_WHEEL, 0600, "devctl");
424 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
425 cv_init(&devsoftc.cv, "dev cv");
426 TAILQ_INIT(&devsoftc.devq);
427 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
431 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
434 mtx_lock(&devsoftc.mtx);
435 if (devsoftc.inuse) {
436 mtx_unlock(&devsoftc.mtx);
441 devsoftc.nonblock = 0;
443 mtx_unlock(&devsoftc.mtx);
448 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
451 mtx_lock(&devsoftc.mtx);
453 cv_broadcast(&devsoftc.cv);
454 funsetown(&devsoftc.sigio);
455 mtx_unlock(&devsoftc.mtx);
460 * The read channel for this device is used to report changes to
461 * userland in realtime. We are required to free the data as well as
462 * the n1 object because we allocate them separately. Also note that
463 * we return one record at a time. If you try to read this device a
464 * character at a time, you will lose the rest of the data. Listening
465 * programs are expected to cope.
468 devread(struct cdev *dev, struct uio *uio, int ioflag)
470 struct dev_event_info *n1;
473 mtx_lock(&devsoftc.mtx);
474 while (TAILQ_EMPTY(&devsoftc.devq)) {
475 if (devsoftc.nonblock) {
476 mtx_unlock(&devsoftc.mtx);
479 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
482 * Need to translate ERESTART to EINTR here? -- jake
484 mtx_unlock(&devsoftc.mtx);
488 n1 = TAILQ_FIRST(&devsoftc.devq);
489 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
491 mtx_unlock(&devsoftc.mtx);
492 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
493 free(n1->dei_data, M_BUS);
499 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
505 devsoftc.nonblock = 1;
507 devsoftc.nonblock = 0;
516 return fsetown(*(int *)data, &devsoftc.sigio);
518 *(int *)data = fgetown(&devsoftc.sigio);
521 /* (un)Support for other fcntl() calls. */
532 devpoll(struct cdev *dev, int events, struct thread *td)
536 mtx_lock(&devsoftc.mtx);
537 if (events & (POLLIN | POLLRDNORM)) {
538 if (!TAILQ_EMPTY(&devsoftc.devq))
539 revents = events & (POLLIN | POLLRDNORM);
541 selrecord(td, &devsoftc.sel);
543 mtx_unlock(&devsoftc.mtx);
549 devkqfilter(struct cdev *dev, struct knote *kn)
553 if (kn->kn_filter == EVFILT_READ) {
554 kn->kn_fop = &devctl_rfiltops;
555 knlist_add(&devsoftc.sel.si_note, kn, 0);
563 filt_devctl_detach(struct knote *kn)
566 knlist_remove(&devsoftc.sel.si_note, kn, 0);
570 filt_devctl_read(struct knote *kn, long hint)
572 kn->kn_data = devsoftc.queued;
573 return (kn->kn_data != 0);
577 * @brief Return whether the userland process is running
580 devctl_process_running(void)
582 return (devsoftc.inuse == 1);
586 * @brief Queue data to be read from the devctl device
588 * Generic interface to queue data to the devctl device. It is
589 * assumed that @p data is properly formatted. It is further assumed
590 * that @p data is allocated using the M_BUS malloc type.
593 devctl_queue_data_f(char *data, int flags)
595 struct dev_event_info *n1 = NULL, *n2 = NULL;
597 if (strlen(data) == 0)
599 if (devctl_queue_length == 0)
601 n1 = malloc(sizeof(*n1), M_BUS, flags);
605 mtx_lock(&devsoftc.mtx);
606 if (devctl_queue_length == 0) {
607 mtx_unlock(&devsoftc.mtx);
608 free(n1->dei_data, M_BUS);
612 /* Leave at least one spot in the queue... */
613 while (devsoftc.queued > devctl_queue_length - 1) {
614 n2 = TAILQ_FIRST(&devsoftc.devq);
615 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
616 free(n2->dei_data, M_BUS);
620 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
622 cv_broadcast(&devsoftc.cv);
623 KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
624 mtx_unlock(&devsoftc.mtx);
625 selwakeup(&devsoftc.sel);
626 if (devsoftc.async && devsoftc.sigio != NULL)
627 pgsigio(&devsoftc.sigio, SIGIO, 0);
631 * We have to free data on all error paths since the caller
632 * assumes it will be free'd when this item is dequeued.
639 devctl_queue_data(char *data)
642 devctl_queue_data_f(data, M_NOWAIT);
646 * @brief Send a 'notification' to userland, using standard ways
649 devctl_notify_f(const char *system, const char *subsystem, const char *type,
650 const char *data, int flags)
656 return; /* BOGUS! Must specify system. */
657 if (subsystem == NULL)
658 return; /* BOGUS! Must specify subsystem. */
660 return; /* BOGUS! Must specify type. */
661 len += strlen(" system=") + strlen(system);
662 len += strlen(" subsystem=") + strlen(subsystem);
663 len += strlen(" type=") + strlen(type);
664 /* add in the data message plus newline. */
667 len += 3; /* '!', '\n', and NUL */
668 msg = malloc(len, M_BUS, flags);
670 return; /* Drop it on the floor */
672 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
673 system, subsystem, type, data);
675 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
676 system, subsystem, type);
677 devctl_queue_data_f(msg, flags);
681 devctl_notify(const char *system, const char *subsystem, const char *type,
685 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
689 * Common routine that tries to make sending messages as easy as possible.
690 * We allocate memory for the data, copy strings into that, but do not
691 * free it unless there's an error. The dequeue part of the driver should
692 * free the data. We don't send data when the device is disabled. We do
693 * send data, even when we have no listeners, because we wish to avoid
694 * races relating to startup and restart of listening applications.
696 * devaddq is designed to string together the type of event, with the
697 * object of that event, plus the plug and play info and location info
698 * for that event. This is likely most useful for devices, but less
699 * useful for other consumers of this interface. Those should use
700 * the devctl_queue_data() interface instead.
703 devaddq(const char *type, const char *what, device_t dev)
710 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
712 data = malloc(1024, M_BUS, M_NOWAIT);
716 /* get the bus specific location of this device */
717 loc = malloc(1024, M_BUS, M_NOWAIT);
721 bus_child_location_str(dev, loc, 1024);
723 /* Get the bus specific pnp info of this device */
724 pnp = malloc(1024, M_BUS, M_NOWAIT);
728 bus_child_pnpinfo_str(dev, pnp, 1024);
730 /* Get the parent of this device, or / if high enough in the tree. */
731 if (device_get_parent(dev) == NULL)
732 parstr = "."; /* Or '/' ? */
734 parstr = device_get_nameunit(device_get_parent(dev));
735 /* String it all together. */
736 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
740 devctl_queue_data(data);
750 * A device was added to the tree. We are called just after it successfully
751 * attaches (that is, probe and attach success for this device). No call
752 * is made if a device is merely parented into the tree. See devnomatch
753 * if probe fails. If attach fails, no notification is sent (but maybe
754 * we should have a different message for this).
757 devadded(device_t dev)
759 devaddq("+", device_get_nameunit(dev), dev);
763 * A device was removed from the tree. We are called just before this
767 devremoved(device_t dev)
769 devaddq("-", device_get_nameunit(dev), dev);
773 * Called when there's no match for this device. This is only called
774 * the first time that no match happens, so we don't keep getting this
775 * message. Should that prove to be undesirable, we can change it.
776 * This is called when all drivers that can attach to a given bus
777 * decline to accept this device. Other errors may not be detected.
780 devnomatch(device_t dev)
782 devaddq("?", "", dev);
786 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
788 struct dev_event_info *n1;
791 dis = (devctl_queue_length == 0);
792 error = sysctl_handle_int(oidp, &dis, 0, req);
793 if (error || !req->newptr)
795 if (mtx_initialized(&devsoftc.mtx))
796 mtx_lock(&devsoftc.mtx);
798 while (!TAILQ_EMPTY(&devsoftc.devq)) {
799 n1 = TAILQ_FIRST(&devsoftc.devq);
800 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
801 free(n1->dei_data, M_BUS);
805 devctl_queue_length = 0;
807 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
809 if (mtx_initialized(&devsoftc.mtx))
810 mtx_unlock(&devsoftc.mtx);
815 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
817 struct dev_event_info *n1;
820 q = devctl_queue_length;
821 error = sysctl_handle_int(oidp, &q, 0, req);
822 if (error || !req->newptr)
826 if (mtx_initialized(&devsoftc.mtx))
827 mtx_lock(&devsoftc.mtx);
828 devctl_queue_length = q;
829 while (devsoftc.queued > devctl_queue_length) {
830 n1 = TAILQ_FIRST(&devsoftc.devq);
831 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
832 free(n1->dei_data, M_BUS);
836 if (mtx_initialized(&devsoftc.mtx))
837 mtx_unlock(&devsoftc.mtx);
841 /* End of /dev/devctl code */
843 static TAILQ_HEAD(,device) bus_data_devices;
844 static int bus_data_generation = 1;
846 static kobj_method_t null_methods[] = {
850 DEFINE_CLASS(null, null_methods, 0);
853 * Bus pass implementation
856 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
857 int bus_current_pass = BUS_PASS_ROOT;
861 * @brief Register the pass level of a new driver attachment
863 * Register a new driver attachment's pass level. If no driver
864 * attachment with the same pass level has been added, then @p new
865 * will be added to the global passes list.
867 * @param new the new driver attachment
870 driver_register_pass(struct driverlink *new)
872 struct driverlink *dl;
874 /* We only consider pass numbers during boot. */
875 if (bus_current_pass == BUS_PASS_DEFAULT)
879 * Walk the passes list. If we already know about this pass
880 * then there is nothing to do. If we don't, then insert this
881 * driver link into the list.
883 TAILQ_FOREACH(dl, &passes, passlink) {
884 if (dl->pass < new->pass)
886 if (dl->pass == new->pass)
888 TAILQ_INSERT_BEFORE(dl, new, passlink);
891 TAILQ_INSERT_TAIL(&passes, new, passlink);
895 * @brief Raise the current bus pass
897 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
898 * method on the root bus to kick off a new device tree scan for each
899 * new pass level that has at least one driver.
902 bus_set_pass(int pass)
904 struct driverlink *dl;
906 if (bus_current_pass > pass)
907 panic("Attempt to lower bus pass level");
909 TAILQ_FOREACH(dl, &passes, passlink) {
910 /* Skip pass values below the current pass level. */
911 if (dl->pass <= bus_current_pass)
915 * Bail once we hit a driver with a pass level that is
922 * Raise the pass level to the next level and rescan
925 bus_current_pass = dl->pass;
926 BUS_NEW_PASS(root_bus);
930 * If there isn't a driver registered for the requested pass,
931 * then bus_current_pass might still be less than 'pass'. Set
932 * it to 'pass' in that case.
934 if (bus_current_pass < pass)
935 bus_current_pass = pass;
936 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
940 * Devclass implementation
943 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
947 * @brief Find or create a device class
949 * If a device class with the name @p classname exists, return it,
950 * otherwise if @p create is non-zero create and return a new device
953 * If @p parentname is non-NULL, the parent of the devclass is set to
954 * the devclass of that name.
956 * @param classname the devclass name to find or create
957 * @param parentname the parent devclass name or @c NULL
958 * @param create non-zero to create a devclass
961 devclass_find_internal(const char *classname, const char *parentname,
966 PDEBUG(("looking for %s", classname));
970 TAILQ_FOREACH(dc, &devclasses, link) {
971 if (!strcmp(dc->name, classname))
976 PDEBUG(("creating %s", classname));
977 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
978 M_BUS, M_NOWAIT | M_ZERO);
982 dc->name = (char*) (dc + 1);
983 strcpy(dc->name, classname);
984 TAILQ_INIT(&dc->drivers);
985 TAILQ_INSERT_TAIL(&devclasses, dc, link);
987 bus_data_generation_update();
991 * If a parent class is specified, then set that as our parent so
992 * that this devclass will support drivers for the parent class as
993 * well. If the parent class has the same name don't do this though
994 * as it creates a cycle that can trigger an infinite loop in
995 * device_probe_child() if a device exists for which there is no
998 if (parentname && dc && !dc->parent &&
999 strcmp(classname, parentname) != 0) {
1000 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
1001 dc->parent->flags |= DC_HAS_CHILDREN;
1008 * @brief Create a device class
1010 * If a device class with the name @p classname exists, return it,
1011 * otherwise create and return a new device class.
1013 * @param classname the devclass name to find or create
1016 devclass_create(const char *classname)
1018 return (devclass_find_internal(classname, NULL, TRUE));
1022 * @brief Find a device class
1024 * If a device class with the name @p classname exists, return it,
1025 * otherwise return @c NULL.
1027 * @param classname the devclass name to find
1030 devclass_find(const char *classname)
1032 return (devclass_find_internal(classname, NULL, FALSE));
1036 * @brief Register that a device driver has been added to a devclass
1038 * Register that a device driver has been added to a devclass. This
1039 * is called by devclass_add_driver to accomplish the recursive
1040 * notification of all the children classes of dc, as well as dc.
1041 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1044 * We do a full search here of the devclass list at each iteration
1045 * level to save storing children-lists in the devclass structure. If
1046 * we ever move beyond a few dozen devices doing this, we may need to
1049 * @param dc the devclass to edit
1050 * @param driver the driver that was just added
1053 devclass_driver_added(devclass_t dc, driver_t *driver)
1059 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1061 for (i = 0; i < dc->maxunit; i++)
1062 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1063 BUS_DRIVER_ADDED(dc->devices[i], driver);
1066 * Walk through the children classes. Since we only keep a
1067 * single parent pointer around, we walk the entire list of
1068 * devclasses looking for children. We set the
1069 * DC_HAS_CHILDREN flag when a child devclass is created on
1070 * the parent, so we only walk the list for those devclasses
1071 * that have children.
1073 if (!(dc->flags & DC_HAS_CHILDREN))
1076 TAILQ_FOREACH(dc, &devclasses, link) {
1077 if (dc->parent == parent)
1078 devclass_driver_added(dc, driver);
1083 * @brief Add a device driver to a device class
1085 * Add a device driver to a devclass. This is normally called
1086 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1087 * all devices in the devclass will be called to allow them to attempt
1088 * to re-probe any unmatched children.
1090 * @param dc the devclass to edit
1091 * @param driver the driver to register
1094 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1097 const char *parentname;
1099 PDEBUG(("%s", DRIVERNAME(driver)));
1101 /* Don't allow invalid pass values. */
1102 if (pass <= BUS_PASS_ROOT)
1105 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1110 * Compile the driver's methods. Also increase the reference count
1111 * so that the class doesn't get freed when the last instance
1112 * goes. This means we can safely use static methods and avoids a
1113 * double-free in devclass_delete_driver.
1115 kobj_class_compile((kobj_class_t) driver);
1118 * If the driver has any base classes, make the
1119 * devclass inherit from the devclass of the driver's
1120 * first base class. This will allow the system to
1121 * search for drivers in both devclasses for children
1122 * of a device using this driver.
1124 if (driver->baseclasses)
1125 parentname = driver->baseclasses[0]->name;
1128 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1130 dl->driver = driver;
1131 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1132 driver->refs++; /* XXX: kobj_mtx */
1134 driver_register_pass(dl);
1136 devclass_driver_added(dc, driver);
1137 bus_data_generation_update();
1142 * @brief Register that a device driver has been deleted from a devclass
1144 * Register that a device driver has been removed from a devclass.
1145 * This is called by devclass_delete_driver to accomplish the
1146 * recursive notification of all the children classes of busclass, as
1147 * well as busclass. Each layer will attempt to detach the driver
1148 * from any devices that are children of the bus's devclass. The function
1149 * will return an error if a device fails to detach.
1151 * We do a full search here of the devclass list at each iteration
1152 * level to save storing children-lists in the devclass structure. If
1153 * we ever move beyond a few dozen devices doing this, we may need to
1156 * @param busclass the devclass of the parent bus
1157 * @param dc the devclass of the driver being deleted
1158 * @param driver the driver being deleted
1161 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1168 * Disassociate from any devices. We iterate through all the
1169 * devices in the devclass of the driver and detach any which are
1170 * using the driver and which have a parent in the devclass which
1171 * we are deleting from.
1173 * Note that since a driver can be in multiple devclasses, we
1174 * should not detach devices which are not children of devices in
1175 * the affected devclass.
1177 for (i = 0; i < dc->maxunit; i++) {
1178 if (dc->devices[i]) {
1179 dev = dc->devices[i];
1180 if (dev->driver == driver && dev->parent &&
1181 dev->parent->devclass == busclass) {
1182 if ((error = device_detach(dev)) != 0)
1184 BUS_PROBE_NOMATCH(dev->parent, dev);
1186 dev->flags |= DF_DONENOMATCH;
1192 * Walk through the children classes. Since we only keep a
1193 * single parent pointer around, we walk the entire list of
1194 * devclasses looking for children. We set the
1195 * DC_HAS_CHILDREN flag when a child devclass is created on
1196 * the parent, so we only walk the list for those devclasses
1197 * that have children.
1199 if (!(busclass->flags & DC_HAS_CHILDREN))
1202 TAILQ_FOREACH(busclass, &devclasses, link) {
1203 if (busclass->parent == parent) {
1204 error = devclass_driver_deleted(busclass, dc, driver);
1213 * @brief Delete a device driver from a device class
1215 * Delete a device driver from a devclass. This is normally called
1216 * automatically by DRIVER_MODULE().
1218 * If the driver is currently attached to any devices,
1219 * devclass_delete_driver() will first attempt to detach from each
1220 * device. If one of the detach calls fails, the driver will not be
1223 * @param dc the devclass to edit
1224 * @param driver the driver to unregister
1227 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1229 devclass_t dc = devclass_find(driver->name);
1233 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1239 * Find the link structure in the bus' list of drivers.
1241 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1242 if (dl->driver == driver)
1247 PDEBUG(("%s not found in %s list", driver->name,
1252 error = devclass_driver_deleted(busclass, dc, driver);
1256 TAILQ_REMOVE(&busclass->drivers, dl, link);
1261 if (driver->refs == 0)
1262 kobj_class_free((kobj_class_t) driver);
1264 bus_data_generation_update();
1269 * @brief Quiesces a set of device drivers from a device class
1271 * Quiesce a device driver from a devclass. This is normally called
1272 * automatically by DRIVER_MODULE().
1274 * If the driver is currently attached to any devices,
1275 * devclass_quiesece_driver() will first attempt to quiesce each
1278 * @param dc the devclass to edit
1279 * @param driver the driver to unregister
1282 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1284 devclass_t dc = devclass_find(driver->name);
1290 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1296 * Find the link structure in the bus' list of drivers.
1298 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1299 if (dl->driver == driver)
1304 PDEBUG(("%s not found in %s list", driver->name,
1310 * Quiesce all devices. We iterate through all the devices in
1311 * the devclass of the driver and quiesce any which are using
1312 * the driver and which have a parent in the devclass which we
1315 * Note that since a driver can be in multiple devclasses, we
1316 * should not quiesce devices which are not children of
1317 * devices in the affected devclass.
1319 for (i = 0; i < dc->maxunit; i++) {
1320 if (dc->devices[i]) {
1321 dev = dc->devices[i];
1322 if (dev->driver == driver && dev->parent &&
1323 dev->parent->devclass == busclass) {
1324 if ((error = device_quiesce(dev)) != 0)
1337 devclass_find_driver_internal(devclass_t dc, const char *classname)
1341 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1343 TAILQ_FOREACH(dl, &dc->drivers, link) {
1344 if (!strcmp(dl->driver->name, classname))
1348 PDEBUG(("not found"));
1353 * @brief Return the name of the devclass
1356 devclass_get_name(devclass_t dc)
1362 * @brief Find a device given a unit number
1364 * @param dc the devclass to search
1365 * @param unit the unit number to search for
1367 * @returns the device with the given unit number or @c
1368 * NULL if there is no such device
1371 devclass_get_device(devclass_t dc, int unit)
1373 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1375 return (dc->devices[unit]);
1379 * @brief Find the softc field of a device given a unit number
1381 * @param dc the devclass to search
1382 * @param unit the unit number to search for
1384 * @returns the softc field of the device with the given
1385 * unit number or @c NULL if there is no such
1389 devclass_get_softc(devclass_t dc, int unit)
1393 dev = devclass_get_device(dc, unit);
1397 return (device_get_softc(dev));
1401 * @brief Get a list of devices in the devclass
1403 * An array containing a list of all the devices in the given devclass
1404 * is allocated and returned in @p *devlistp. The number of devices
1405 * in the array is returned in @p *devcountp. The caller should free
1406 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1408 * @param dc the devclass to examine
1409 * @param devlistp points at location for array pointer return
1411 * @param devcountp points at location for array size return value
1414 * @retval ENOMEM the array allocation failed
1417 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1422 count = devclass_get_count(dc);
1423 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1428 for (i = 0; i < dc->maxunit; i++) {
1429 if (dc->devices[i]) {
1430 list[count] = dc->devices[i];
1442 * @brief Get a list of drivers in the devclass
1444 * An array containing a list of pointers to all the drivers in the
1445 * given devclass is allocated and returned in @p *listp. The number
1446 * of drivers in the array is returned in @p *countp. The caller should
1447 * free the array using @c free(p, M_TEMP).
1449 * @param dc the devclass to examine
1450 * @param listp gives location for array pointer return value
1451 * @param countp gives location for number of array elements
1455 * @retval ENOMEM the array allocation failed
1458 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1465 TAILQ_FOREACH(dl, &dc->drivers, link)
1467 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1472 TAILQ_FOREACH(dl, &dc->drivers, link) {
1473 list[count] = dl->driver;
1483 * @brief Get the number of devices in a devclass
1485 * @param dc the devclass to examine
1488 devclass_get_count(devclass_t dc)
1493 for (i = 0; i < dc->maxunit; i++)
1500 * @brief Get the maximum unit number used in a devclass
1502 * Note that this is one greater than the highest currently-allocated
1503 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1504 * that not even the devclass has been allocated yet.
1506 * @param dc the devclass to examine
1509 devclass_get_maxunit(devclass_t dc)
1513 return (dc->maxunit);
1517 * @brief Find a free unit number in a devclass
1519 * This function searches for the first unused unit number greater
1520 * that or equal to @p unit.
1522 * @param dc the devclass to examine
1523 * @param unit the first unit number to check
1526 devclass_find_free_unit(devclass_t dc, int unit)
1530 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1536 * @brief Set the parent of a devclass
1538 * The parent class is normally initialised automatically by
1541 * @param dc the devclass to edit
1542 * @param pdc the new parent devclass
1545 devclass_set_parent(devclass_t dc, devclass_t pdc)
1551 * @brief Get the parent of a devclass
1553 * @param dc the devclass to examine
1556 devclass_get_parent(devclass_t dc)
1558 return (dc->parent);
1561 struct sysctl_ctx_list *
1562 devclass_get_sysctl_ctx(devclass_t dc)
1564 return (&dc->sysctl_ctx);
1568 devclass_get_sysctl_tree(devclass_t dc)
1570 return (dc->sysctl_tree);
1575 * @brief Allocate a unit number
1577 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1578 * will do). The allocated unit number is returned in @p *unitp.
1580 * @param dc the devclass to allocate from
1581 * @param unitp points at the location for the allocated unit
1585 * @retval EEXIST the requested unit number is already allocated
1586 * @retval ENOMEM memory allocation failure
1589 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1594 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1596 /* Ask the parent bus if it wants to wire this device. */
1598 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1601 /* If we were given a wired unit number, check for existing device */
1604 if (unit >= 0 && unit < dc->maxunit &&
1605 dc->devices[unit] != NULL) {
1607 printf("%s: %s%d already exists; skipping it\n",
1608 dc->name, dc->name, *unitp);
1612 /* Unwired device, find the next available slot for it */
1614 for (unit = 0;; unit++) {
1615 /* If there is an "at" hint for a unit then skip it. */
1616 if (resource_string_value(dc->name, unit, "at", &s) ==
1620 /* If this device slot is already in use, skip it. */
1621 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1629 * We've selected a unit beyond the length of the table, so let's
1630 * extend the table to make room for all units up to and including
1633 if (unit >= dc->maxunit) {
1634 device_t *newlist, *oldlist;
1637 oldlist = dc->devices;
1638 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1639 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1642 if (oldlist != NULL)
1643 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1644 bzero(newlist + dc->maxunit,
1645 sizeof(device_t) * (newsize - dc->maxunit));
1646 dc->devices = newlist;
1647 dc->maxunit = newsize;
1648 if (oldlist != NULL)
1649 free(oldlist, M_BUS);
1651 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1659 * @brief Add a device to a devclass
1661 * A unit number is allocated for the device (using the device's
1662 * preferred unit number if any) and the device is registered in the
1663 * devclass. This allows the device to be looked up by its unit
1664 * number, e.g. by decoding a dev_t minor number.
1666 * @param dc the devclass to add to
1667 * @param dev the device to add
1670 * @retval EEXIST the requested unit number is already allocated
1671 * @retval ENOMEM memory allocation failure
1674 devclass_add_device(devclass_t dc, device_t dev)
1678 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1680 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1683 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1687 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1688 free(dev->nameunit, M_BUS);
1689 dev->nameunit = NULL;
1692 dc->devices[dev->unit] = dev;
1694 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1701 * @brief Delete a device from a devclass
1703 * The device is removed from the devclass's device list and its unit
1706 * @param dc the devclass to delete from
1707 * @param dev the device to delete
1712 devclass_delete_device(devclass_t dc, device_t dev)
1717 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1719 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1720 panic("devclass_delete_device: inconsistent device class");
1721 dc->devices[dev->unit] = NULL;
1722 if (dev->flags & DF_WILDCARD)
1724 dev->devclass = NULL;
1725 free(dev->nameunit, M_BUS);
1726 dev->nameunit = NULL;
1733 * @brief Make a new device and add it as a child of @p parent
1735 * @param parent the parent of the new device
1736 * @param name the devclass name of the new device or @c NULL
1737 * to leave the devclass unspecified
1738 * @parem unit the unit number of the new device of @c -1 to
1739 * leave the unit number unspecified
1741 * @returns the new device
1744 make_device(device_t parent, const char *name, int unit)
1749 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1752 dc = devclass_find_internal(name, NULL, TRUE);
1754 printf("make_device: can't find device class %s\n",
1762 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1766 dev->parent = parent;
1767 TAILQ_INIT(&dev->children);
1768 kobj_init((kobj_t) dev, &null_class);
1770 dev->devclass = NULL;
1772 dev->nameunit = NULL;
1776 dev->flags = DF_ENABLED;
1779 dev->flags |= DF_WILDCARD;
1781 dev->flags |= DF_FIXEDCLASS;
1782 if (devclass_add_device(dc, dev)) {
1783 kobj_delete((kobj_t) dev, M_BUS);
1790 dev->state = DS_NOTPRESENT;
1792 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1793 bus_data_generation_update();
1800 * @brief Print a description of a device.
1803 device_print_child(device_t dev, device_t child)
1807 if (device_is_alive(child))
1808 retval += BUS_PRINT_CHILD(dev, child);
1810 retval += device_printf(child, " not found\n");
1816 * @brief Create a new device
1818 * This creates a new device and adds it as a child of an existing
1819 * parent device. The new device will be added after the last existing
1820 * child with order zero.
1822 * @param dev the device which will be the parent of the
1824 * @param name devclass name for new device or @c NULL if not
1826 * @param unit unit number for new device or @c -1 if not
1829 * @returns the new device
1832 device_add_child(device_t dev, const char *name, int unit)
1834 return (device_add_child_ordered(dev, 0, name, unit));
1838 * @brief Create a new device
1840 * This creates a new device and adds it as a child of an existing
1841 * parent device. The new device will be added after the last existing
1842 * child with the same order.
1844 * @param dev the device which will be the parent of the
1846 * @param order a value which is used to partially sort the
1847 * children of @p dev - devices created using
1848 * lower values of @p order appear first in @p
1849 * dev's list of children
1850 * @param name devclass name for new device or @c NULL if not
1852 * @param unit unit number for new device or @c -1 if not
1855 * @returns the new device
1858 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1863 PDEBUG(("%s at %s with order %u as unit %d",
1864 name, DEVICENAME(dev), order, unit));
1865 KASSERT(name != NULL || unit == -1,
1866 ("child device with wildcard name and specific unit number"));
1868 child = make_device(dev, name, unit);
1871 child->order = order;
1873 TAILQ_FOREACH(place, &dev->children, link) {
1874 if (place->order > order)
1880 * The device 'place' is the first device whose order is
1881 * greater than the new child.
1883 TAILQ_INSERT_BEFORE(place, child, link);
1886 * The new child's order is greater or equal to the order of
1887 * any existing device. Add the child to the tail of the list.
1889 TAILQ_INSERT_TAIL(&dev->children, child, link);
1892 bus_data_generation_update();
1897 * @brief Delete a device
1899 * This function deletes a device along with all of its children. If
1900 * the device currently has a driver attached to it, the device is
1901 * detached first using device_detach().
1903 * @param dev the parent device
1904 * @param child the device to delete
1907 * @retval non-zero a unit error code describing the error
1910 device_delete_child(device_t dev, device_t child)
1913 device_t grandchild;
1915 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1917 /* remove children first */
1918 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1919 error = device_delete_child(child, grandchild);
1924 if ((error = device_detach(child)) != 0)
1926 if (child->devclass)
1927 devclass_delete_device(child->devclass, child);
1929 BUS_CHILD_DELETED(dev, child);
1930 TAILQ_REMOVE(&dev->children, child, link);
1931 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1932 kobj_delete((kobj_t) child, M_BUS);
1934 bus_data_generation_update();
1939 * @brief Delete all children devices of the given device, if any.
1941 * This function deletes all children devices of the given device, if
1942 * any, using the device_delete_child() function for each device it
1943 * finds. If a child device cannot be deleted, this function will
1944 * return an error code.
1946 * @param dev the parent device
1949 * @retval non-zero a device would not detach
1952 device_delete_children(device_t dev)
1957 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1961 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1962 error = device_delete_child(dev, child);
1964 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1972 * @brief Find a device given a unit number
1974 * This is similar to devclass_get_devices() but only searches for
1975 * devices which have @p dev as a parent.
1977 * @param dev the parent device to search
1978 * @param unit the unit number to search for. If the unit is -1,
1979 * return the first child of @p dev which has name
1980 * @p classname (that is, the one with the lowest unit.)
1982 * @returns the device with the given unit number or @c
1983 * NULL if there is no such device
1986 device_find_child(device_t dev, const char *classname, int unit)
1991 dc = devclass_find(classname);
1996 child = devclass_get_device(dc, unit);
1997 if (child && child->parent == dev)
2000 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2001 child = devclass_get_device(dc, unit);
2002 if (child && child->parent == dev)
2013 first_matching_driver(devclass_t dc, device_t dev)
2016 return (devclass_find_driver_internal(dc, dev->devclass->name));
2017 return (TAILQ_FIRST(&dc->drivers));
2024 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2026 if (dev->devclass) {
2028 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2029 if (!strcmp(dev->devclass->name, dl->driver->name))
2033 return (TAILQ_NEXT(last, link));
2040 device_probe_child(device_t dev, device_t child)
2043 driverlink_t best = NULL;
2045 int result, pri = 0;
2046 int hasclass = (child->devclass != NULL);
2052 panic("device_probe_child: parent device has no devclass");
2055 * If the state is already probed, then return. However, don't
2056 * return if we can rebid this object.
2058 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2061 for (; dc; dc = dc->parent) {
2062 for (dl = first_matching_driver(dc, child);
2064 dl = next_matching_driver(dc, child, dl)) {
2065 /* If this driver's pass is too high, then ignore it. */
2066 if (dl->pass > bus_current_pass)
2069 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2070 result = device_set_driver(child, dl->driver);
2071 if (result == ENOMEM)
2073 else if (result != 0)
2076 if (device_set_devclass(child,
2077 dl->driver->name) != 0) {
2078 char const * devname =
2079 device_get_name(child);
2080 if (devname == NULL)
2081 devname = "(unknown)";
2082 printf("driver bug: Unable to set "
2083 "devclass (class: %s "
2087 (void)device_set_driver(child, NULL);
2092 /* Fetch any flags for the device before probing. */
2093 resource_int_value(dl->driver->name, child->unit,
2094 "flags", &child->devflags);
2096 result = DEVICE_PROBE(child);
2098 /* Reset flags and devclass before the next probe. */
2099 child->devflags = 0;
2101 (void)device_set_devclass(child, NULL);
2104 * If the driver returns SUCCESS, there can be
2105 * no higher match for this device.
2114 * The driver returned an error so it
2115 * certainly doesn't match.
2118 (void)device_set_driver(child, NULL);
2123 * A priority lower than SUCCESS, remember the
2124 * best matching driver. Initialise the value
2125 * of pri for the first match.
2127 if (best == NULL || result > pri) {
2129 * Probes that return BUS_PROBE_NOWILDCARD
2130 * or lower only match on devices whose
2131 * driver was explicitly specified.
2133 if (result <= BUS_PROBE_NOWILDCARD &&
2134 !(child->flags & DF_FIXEDCLASS))
2142 * If we have an unambiguous match in this devclass,
2143 * don't look in the parent.
2145 if (best && pri == 0)
2150 * If we found a driver, change state and initialise the devclass.
2152 /* XXX What happens if we rebid and got no best? */
2155 * If this device was attached, and we were asked to
2156 * rescan, and it is a different driver, then we have
2157 * to detach the old driver and reattach this new one.
2158 * Note, we don't have to check for DF_REBID here
2159 * because if the state is > DS_ALIVE, we know it must
2162 * This assumes that all DF_REBID drivers can have
2163 * their probe routine called at any time and that
2164 * they are idempotent as well as completely benign in
2165 * normal operations.
2167 * We also have to make sure that the detach
2168 * succeeded, otherwise we fail the operation (or
2169 * maybe it should just fail silently? I'm torn).
2171 if (child->state > DS_ALIVE && best->driver != child->driver)
2172 if ((result = device_detach(dev)) != 0)
2175 /* Set the winning driver, devclass, and flags. */
2176 if (!child->devclass) {
2177 result = device_set_devclass(child, best->driver->name);
2181 result = device_set_driver(child, best->driver);
2184 resource_int_value(best->driver->name, child->unit,
2185 "flags", &child->devflags);
2189 * A bit bogus. Call the probe method again to make
2190 * sure that we have the right description.
2192 DEVICE_PROBE(child);
2194 child->flags |= DF_REBID;
2197 child->flags &= ~DF_REBID;
2198 child->state = DS_ALIVE;
2200 bus_data_generation_update();
2208 * @brief Return the parent of a device
2211 device_get_parent(device_t dev)
2213 return (dev->parent);
2217 * @brief Get a list of children of a device
2219 * An array containing a list of all the children of the given device
2220 * is allocated and returned in @p *devlistp. The number of devices
2221 * in the array is returned in @p *devcountp. The caller should free
2222 * the array using @c free(p, M_TEMP).
2224 * @param dev the device to examine
2225 * @param devlistp points at location for array pointer return
2227 * @param devcountp points at location for array size return value
2230 * @retval ENOMEM the array allocation failed
2233 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2240 TAILQ_FOREACH(child, &dev->children, link) {
2249 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2254 TAILQ_FOREACH(child, &dev->children, link) {
2255 list[count] = child;
2266 * @brief Return the current driver for the device or @c NULL if there
2267 * is no driver currently attached
2270 device_get_driver(device_t dev)
2272 return (dev->driver);
2276 * @brief Return the current devclass for the device or @c NULL if
2280 device_get_devclass(device_t dev)
2282 return (dev->devclass);
2286 * @brief Return the name of the device's devclass or @c NULL if there
2290 device_get_name(device_t dev)
2292 if (dev != NULL && dev->devclass)
2293 return (devclass_get_name(dev->devclass));
2298 * @brief Return a string containing the device's devclass name
2299 * followed by an ascii representation of the device's unit number
2303 device_get_nameunit(device_t dev)
2305 return (dev->nameunit);
2309 * @brief Return the device's unit number.
2312 device_get_unit(device_t dev)
2318 * @brief Return the device's description string
2321 device_get_desc(device_t dev)
2327 * @brief Return the device's flags
2330 device_get_flags(device_t dev)
2332 return (dev->devflags);
2335 struct sysctl_ctx_list *
2336 device_get_sysctl_ctx(device_t dev)
2338 return (&dev->sysctl_ctx);
2342 device_get_sysctl_tree(device_t dev)
2344 return (dev->sysctl_tree);
2348 * @brief Print the name of the device followed by a colon and a space
2350 * @returns the number of characters printed
2353 device_print_prettyname(device_t dev)
2355 const char *name = device_get_name(dev);
2358 return (printf("unknown: "));
2359 return (printf("%s%d: ", name, device_get_unit(dev)));
2363 * @brief Print the name of the device followed by a colon, a space
2364 * and the result of calling vprintf() with the value of @p fmt and
2365 * the following arguments.
2367 * @returns the number of characters printed
2370 device_printf(device_t dev, const char * fmt, ...)
2375 retval = device_print_prettyname(dev);
2377 retval += vprintf(fmt, ap);
2386 device_set_desc_internal(device_t dev, const char* desc, int copy)
2388 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2389 free(dev->desc, M_BUS);
2390 dev->flags &= ~DF_DESCMALLOCED;
2395 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2397 strcpy(dev->desc, desc);
2398 dev->flags |= DF_DESCMALLOCED;
2401 /* Avoid a -Wcast-qual warning */
2402 dev->desc = (char *)(uintptr_t) desc;
2405 bus_data_generation_update();
2409 * @brief Set the device's description
2411 * The value of @c desc should be a string constant that will not
2412 * change (at least until the description is changed in a subsequent
2413 * call to device_set_desc() or device_set_desc_copy()).
2416 device_set_desc(device_t dev, const char* desc)
2418 device_set_desc_internal(dev, desc, FALSE);
2422 * @brief Set the device's description
2424 * The string pointed to by @c desc is copied. Use this function if
2425 * the device description is generated, (e.g. with sprintf()).
2428 device_set_desc_copy(device_t dev, const char* desc)
2430 device_set_desc_internal(dev, desc, TRUE);
2434 * @brief Set the device's flags
2437 device_set_flags(device_t dev, uint32_t flags)
2439 dev->devflags = flags;
2443 * @brief Return the device's softc field
2445 * The softc is allocated and zeroed when a driver is attached, based
2446 * on the size field of the driver.
2449 device_get_softc(device_t dev)
2451 return (dev->softc);
2455 * @brief Set the device's softc field
2457 * Most drivers do not need to use this since the softc is allocated
2458 * automatically when the driver is attached.
2461 device_set_softc(device_t dev, void *softc)
2463 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2464 free(dev->softc, M_BUS_SC);
2467 dev->flags |= DF_EXTERNALSOFTC;
2469 dev->flags &= ~DF_EXTERNALSOFTC;
2473 * @brief Free claimed softc
2475 * Most drivers do not need to use this since the softc is freed
2476 * automatically when the driver is detached.
2479 device_free_softc(void *softc)
2481 free(softc, M_BUS_SC);
2485 * @brief Claim softc
2487 * This function can be used to let the driver free the automatically
2488 * allocated softc using "device_free_softc()". This function is
2489 * useful when the driver is refcounting the softc and the softc
2490 * cannot be freed when the "device_detach" method is called.
2493 device_claim_softc(device_t dev)
2496 dev->flags |= DF_EXTERNALSOFTC;
2498 dev->flags &= ~DF_EXTERNALSOFTC;
2502 * @brief Get the device's ivars field
2504 * The ivars field is used by the parent device to store per-device
2505 * state (e.g. the physical location of the device or a list of
2509 device_get_ivars(device_t dev)
2512 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2513 return (dev->ivars);
2517 * @brief Set the device's ivars field
2520 device_set_ivars(device_t dev, void * ivars)
2523 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2528 * @brief Return the device's state
2531 device_get_state(device_t dev)
2533 return (dev->state);
2537 * @brief Set the DF_ENABLED flag for the device
2540 device_enable(device_t dev)
2542 dev->flags |= DF_ENABLED;
2546 * @brief Clear the DF_ENABLED flag for the device
2549 device_disable(device_t dev)
2551 dev->flags &= ~DF_ENABLED;
2555 * @brief Increment the busy counter for the device
2558 device_busy(device_t dev)
2560 if (dev->state < DS_ATTACHING)
2561 panic("device_busy: called for unattached device");
2562 if (dev->busy == 0 && dev->parent)
2563 device_busy(dev->parent);
2565 if (dev->state == DS_ATTACHED)
2566 dev->state = DS_BUSY;
2570 * @brief Decrement the busy counter for the device
2573 device_unbusy(device_t dev)
2575 if (dev->busy != 0 && dev->state != DS_BUSY &&
2576 dev->state != DS_ATTACHING)
2577 panic("device_unbusy: called for non-busy device %s",
2578 device_get_nameunit(dev));
2580 if (dev->busy == 0) {
2582 device_unbusy(dev->parent);
2583 if (dev->state == DS_BUSY)
2584 dev->state = DS_ATTACHED;
2589 * @brief Set the DF_QUIET flag for the device
2592 device_quiet(device_t dev)
2594 dev->flags |= DF_QUIET;
2598 * @brief Clear the DF_QUIET flag for the device
2601 device_verbose(device_t dev)
2603 dev->flags &= ~DF_QUIET;
2607 * @brief Return non-zero if the DF_QUIET flag is set on the device
2610 device_is_quiet(device_t dev)
2612 return ((dev->flags & DF_QUIET) != 0);
2616 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2619 device_is_enabled(device_t dev)
2621 return ((dev->flags & DF_ENABLED) != 0);
2625 * @brief Return non-zero if the device was successfully probed
2628 device_is_alive(device_t dev)
2630 return (dev->state >= DS_ALIVE);
2634 * @brief Return non-zero if the device currently has a driver
2638 device_is_attached(device_t dev)
2640 return (dev->state >= DS_ATTACHED);
2644 * @brief Set the devclass of a device
2645 * @see devclass_add_device().
2648 device_set_devclass(device_t dev, const char *classname)
2655 devclass_delete_device(dev->devclass, dev);
2659 if (dev->devclass) {
2660 printf("device_set_devclass: device class already set\n");
2664 dc = devclass_find_internal(classname, NULL, TRUE);
2668 error = devclass_add_device(dc, dev);
2670 bus_data_generation_update();
2675 * @brief Set the driver of a device
2678 * @retval EBUSY the device already has a driver attached
2679 * @retval ENOMEM a memory allocation failure occurred
2682 device_set_driver(device_t dev, driver_t *driver)
2684 if (dev->state >= DS_ATTACHED)
2687 if (dev->driver == driver)
2690 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2691 free(dev->softc, M_BUS_SC);
2694 device_set_desc(dev, NULL);
2695 kobj_delete((kobj_t) dev, NULL);
2696 dev->driver = driver;
2698 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2699 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2700 dev->softc = malloc(driver->size, M_BUS_SC,
2703 kobj_delete((kobj_t) dev, NULL);
2704 kobj_init((kobj_t) dev, &null_class);
2710 kobj_init((kobj_t) dev, &null_class);
2713 bus_data_generation_update();
2718 * @brief Probe a device, and return this status.
2720 * This function is the core of the device autoconfiguration
2721 * system. Its purpose is to select a suitable driver for a device and
2722 * then call that driver to initialise the hardware appropriately. The
2723 * driver is selected by calling the DEVICE_PROBE() method of a set of
2724 * candidate drivers and then choosing the driver which returned the
2725 * best value. This driver is then attached to the device using
2728 * The set of suitable drivers is taken from the list of drivers in
2729 * the parent device's devclass. If the device was originally created
2730 * with a specific class name (see device_add_child()), only drivers
2731 * with that name are probed, otherwise all drivers in the devclass
2732 * are probed. If no drivers return successful probe values in the
2733 * parent devclass, the search continues in the parent of that
2734 * devclass (see devclass_get_parent()) if any.
2736 * @param dev the device to initialise
2739 * @retval ENXIO no driver was found
2740 * @retval ENOMEM memory allocation failure
2741 * @retval non-zero some other unix error code
2742 * @retval -1 Device already attached
2745 device_probe(device_t dev)
2751 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2754 if (!(dev->flags & DF_ENABLED)) {
2755 if (bootverbose && device_get_name(dev) != NULL) {
2756 device_print_prettyname(dev);
2757 printf("not probed (disabled)\n");
2761 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2762 if (bus_current_pass == BUS_PASS_DEFAULT &&
2763 !(dev->flags & DF_DONENOMATCH)) {
2764 BUS_PROBE_NOMATCH(dev->parent, dev);
2766 dev->flags |= DF_DONENOMATCH;
2774 * @brief Probe a device and attach a driver if possible
2776 * calls device_probe() and attaches if that was successful.
2779 device_probe_and_attach(device_t dev)
2785 error = device_probe(dev);
2788 else if (error != 0)
2791 CURVNET_SET_QUIET(vnet0);
2792 error = device_attach(dev);
2798 * @brief Attach a device driver to a device
2800 * This function is a wrapper around the DEVICE_ATTACH() driver
2801 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2802 * device's sysctl tree, optionally prints a description of the device
2803 * and queues a notification event for user-based device management
2806 * Normally this function is only called internally from
2807 * device_probe_and_attach().
2809 * @param dev the device to initialise
2812 * @retval ENXIO no driver was found
2813 * @retval ENOMEM memory allocation failure
2814 * @retval non-zero some other unix error code
2817 device_attach(device_t dev)
2819 uint64_t attachtime;
2822 if (resource_disabled(dev->driver->name, dev->unit)) {
2823 device_disable(dev);
2825 device_printf(dev, "disabled via hints entry\n");
2829 device_sysctl_init(dev);
2830 if (!device_is_quiet(dev))
2831 device_print_child(dev->parent, dev);
2832 attachtime = get_cyclecount();
2833 dev->state = DS_ATTACHING;
2834 if ((error = DEVICE_ATTACH(dev)) != 0) {
2835 printf("device_attach: %s%d attach returned %d\n",
2836 dev->driver->name, dev->unit, error);
2837 if (!(dev->flags & DF_FIXEDCLASS))
2838 devclass_delete_device(dev->devclass, dev);
2839 (void)device_set_driver(dev, NULL);
2840 device_sysctl_fini(dev);
2841 KASSERT(dev->busy == 0, ("attach failed but busy"));
2842 dev->state = DS_NOTPRESENT;
2845 attachtime = get_cyclecount() - attachtime;
2847 * 4 bits per device is a reasonable value for desktop and server
2848 * hardware with good get_cyclecount() implementations, but may
2849 * need to be adjusted on other platforms.
2852 printf("%s(): feeding %d bit(s) of entropy from %s%d\n",
2853 __func__, 4, dev->driver->name, dev->unit);
2855 random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH);
2856 device_sysctl_update(dev);
2858 dev->state = DS_BUSY;
2860 dev->state = DS_ATTACHED;
2861 dev->flags &= ~DF_DONENOMATCH;
2867 * @brief Detach a driver from a device
2869 * This function is a wrapper around the DEVICE_DETACH() driver
2870 * method. If the call to DEVICE_DETACH() succeeds, it calls
2871 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2872 * notification event for user-based device management services and
2873 * cleans up the device's sysctl tree.
2875 * @param dev the device to un-initialise
2878 * @retval ENXIO no driver was found
2879 * @retval ENOMEM memory allocation failure
2880 * @retval non-zero some other unix error code
2883 device_detach(device_t dev)
2889 PDEBUG(("%s", DEVICENAME(dev)));
2890 if (dev->state == DS_BUSY)
2892 if (dev->state != DS_ATTACHED)
2895 if ((error = DEVICE_DETACH(dev)) != 0)
2898 if (!device_is_quiet(dev))
2899 device_printf(dev, "detached\n");
2901 BUS_CHILD_DETACHED(dev->parent, dev);
2903 if (!(dev->flags & DF_FIXEDCLASS))
2904 devclass_delete_device(dev->devclass, dev);
2906 dev->state = DS_NOTPRESENT;
2907 (void)device_set_driver(dev, NULL);
2908 device_sysctl_fini(dev);
2914 * @brief Tells a driver to quiesce itself.
2916 * This function is a wrapper around the DEVICE_QUIESCE() driver
2917 * method. If the call to DEVICE_QUIESCE() succeeds.
2919 * @param dev the device to quiesce
2922 * @retval ENXIO no driver was found
2923 * @retval ENOMEM memory allocation failure
2924 * @retval non-zero some other unix error code
2927 device_quiesce(device_t dev)
2930 PDEBUG(("%s", DEVICENAME(dev)));
2931 if (dev->state == DS_BUSY)
2933 if (dev->state != DS_ATTACHED)
2936 return (DEVICE_QUIESCE(dev));
2940 * @brief Notify a device of system shutdown
2942 * This function calls the DEVICE_SHUTDOWN() driver method if the
2943 * device currently has an attached driver.
2945 * @returns the value returned by DEVICE_SHUTDOWN()
2948 device_shutdown(device_t dev)
2950 if (dev->state < DS_ATTACHED)
2952 return (DEVICE_SHUTDOWN(dev));
2956 * @brief Set the unit number of a device
2958 * This function can be used to override the unit number used for a
2959 * device (e.g. to wire a device to a pre-configured unit number).
2962 device_set_unit(device_t dev, int unit)
2967 dc = device_get_devclass(dev);
2968 if (unit < dc->maxunit && dc->devices[unit])
2970 err = devclass_delete_device(dc, dev);
2974 err = devclass_add_device(dc, dev);
2978 bus_data_generation_update();
2982 /*======================================*/
2984 * Some useful method implementations to make life easier for bus drivers.
2988 * @brief Initialise a resource list.
2990 * @param rl the resource list to initialise
2993 resource_list_init(struct resource_list *rl)
2999 * @brief Reclaim memory used by a resource list.
3001 * This function frees the memory for all resource entries on the list
3004 * @param rl the resource list to free
3007 resource_list_free(struct resource_list *rl)
3009 struct resource_list_entry *rle;
3011 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3013 panic("resource_list_free: resource entry is busy");
3014 STAILQ_REMOVE_HEAD(rl, link);
3020 * @brief Add a resource entry.
3022 * This function adds a resource entry using the given @p type, @p
3023 * start, @p end and @p count values. A rid value is chosen by
3024 * searching sequentially for the first unused rid starting at zero.
3026 * @param rl the resource list to edit
3027 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3028 * @param start the start address of the resource
3029 * @param end the end address of the resource
3030 * @param count XXX end-start+1
3033 resource_list_add_next(struct resource_list *rl, int type, u_long start,
3034 u_long end, u_long count)
3039 while (resource_list_find(rl, type, rid) != NULL)
3041 resource_list_add(rl, type, rid, start, end, count);
3046 * @brief Add or modify a resource entry.
3048 * If an existing entry exists with the same type and rid, it will be
3049 * modified using the given values of @p start, @p end and @p
3050 * count. If no entry exists, a new one will be created using the
3051 * given values. The resource list entry that matches is then returned.
3053 * @param rl the resource list to edit
3054 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3055 * @param rid the resource identifier
3056 * @param start the start address of the resource
3057 * @param end the end address of the resource
3058 * @param count XXX end-start+1
3060 struct resource_list_entry *
3061 resource_list_add(struct resource_list *rl, int type, int rid,
3062 u_long start, u_long end, u_long count)
3064 struct resource_list_entry *rle;
3066 rle = resource_list_find(rl, type, rid);
3068 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3071 panic("resource_list_add: can't record entry");
3072 STAILQ_INSERT_TAIL(rl, rle, link);
3080 panic("resource_list_add: resource entry is busy");
3089 * @brief Determine if a resource entry is busy.
3091 * Returns true if a resource entry is busy meaning that it has an
3092 * associated resource that is not an unallocated "reserved" resource.
3094 * @param rl the resource list to search
3095 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3096 * @param rid the resource identifier
3098 * @returns Non-zero if the entry is busy, zero otherwise.
3101 resource_list_busy(struct resource_list *rl, int type, int rid)
3103 struct resource_list_entry *rle;
3105 rle = resource_list_find(rl, type, rid);
3106 if (rle == NULL || rle->res == NULL)
3108 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3109 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3110 ("reserved resource is active"));
3117 * @brief Determine if a resource entry is reserved.
3119 * Returns true if a resource entry is reserved meaning that it has an
3120 * associated "reserved" resource. The resource can either be
3121 * allocated or unallocated.
3123 * @param rl the resource list to search
3124 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3125 * @param rid the resource identifier
3127 * @returns Non-zero if the entry is reserved, zero otherwise.
3130 resource_list_reserved(struct resource_list *rl, int type, int rid)
3132 struct resource_list_entry *rle;
3134 rle = resource_list_find(rl, type, rid);
3135 if (rle != NULL && rle->flags & RLE_RESERVED)
3141 * @brief Find a resource entry by type and rid.
3143 * @param rl the resource list to search
3144 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3145 * @param rid the resource identifier
3147 * @returns the resource entry pointer or NULL if there is no such
3150 struct resource_list_entry *
3151 resource_list_find(struct resource_list *rl, int type, int rid)
3153 struct resource_list_entry *rle;
3155 STAILQ_FOREACH(rle, rl, link) {
3156 if (rle->type == type && rle->rid == rid)
3163 * @brief Delete a resource entry.
3165 * @param rl the resource list to edit
3166 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3167 * @param rid the resource identifier
3170 resource_list_delete(struct resource_list *rl, int type, int rid)
3172 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3175 if (rle->res != NULL)
3176 panic("resource_list_delete: resource has not been released");
3177 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3183 * @brief Allocate a reserved resource
3185 * This can be used by busses to force the allocation of resources
3186 * that are always active in the system even if they are not allocated
3187 * by a driver (e.g. PCI BARs). This function is usually called when
3188 * adding a new child to the bus. The resource is allocated from the
3189 * parent bus when it is reserved. The resource list entry is marked
3190 * with RLE_RESERVED to note that it is a reserved resource.
3192 * Subsequent attempts to allocate the resource with
3193 * resource_list_alloc() will succeed the first time and will set
3194 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3195 * resource that has been allocated is released with
3196 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3197 * the actual resource remains allocated. The resource can be released to
3198 * the parent bus by calling resource_list_unreserve().
3200 * @param rl the resource list to allocate from
3201 * @param bus the parent device of @p child
3202 * @param child the device for which the resource is being reserved
3203 * @param type the type of resource to allocate
3204 * @param rid a pointer to the resource identifier
3205 * @param start hint at the start of the resource range - pass
3206 * @c 0UL for any start address
3207 * @param end hint at the end of the resource range - pass
3208 * @c ~0UL for any end address
3209 * @param count hint at the size of range required - pass @c 1
3211 * @param flags any extra flags to control the resource
3212 * allocation - see @c RF_XXX flags in
3213 * <sys/rman.h> for details
3215 * @returns the resource which was allocated or @c NULL if no
3216 * resource could be allocated
3219 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3220 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3222 struct resource_list_entry *rle = NULL;
3223 int passthrough = (device_get_parent(child) != bus);
3228 "resource_list_reserve() should only be called for direct children");
3229 if (flags & RF_ACTIVE)
3231 "resource_list_reserve() should only reserve inactive resources");
3233 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3236 rle = resource_list_find(rl, type, *rid);
3237 rle->flags |= RLE_RESERVED;
3243 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3245 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3246 * and passing the allocation up to the parent of @p bus. This assumes
3247 * that the first entry of @c device_get_ivars(child) is a struct
3248 * resource_list. This also handles 'passthrough' allocations where a
3249 * child is a remote descendant of bus by passing the allocation up to
3250 * the parent of bus.
3252 * Typically, a bus driver would store a list of child resources
3253 * somewhere in the child device's ivars (see device_get_ivars()) and
3254 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3255 * then call resource_list_alloc() to perform the allocation.
3257 * @param rl the resource list to allocate from
3258 * @param bus the parent device of @p child
3259 * @param child the device which is requesting an allocation
3260 * @param type the type of resource to allocate
3261 * @param rid a pointer to the resource identifier
3262 * @param start hint at the start of the resource range - pass
3263 * @c 0UL for any start address
3264 * @param end hint at the end of the resource range - pass
3265 * @c ~0UL for any end address
3266 * @param count hint at the size of range required - pass @c 1
3268 * @param flags any extra flags to control the resource
3269 * allocation - see @c RF_XXX flags in
3270 * <sys/rman.h> for details
3272 * @returns the resource which was allocated or @c NULL if no
3273 * resource could be allocated
3276 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3277 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3279 struct resource_list_entry *rle = NULL;
3280 int passthrough = (device_get_parent(child) != bus);
3281 int isdefault = (start == 0UL && end == ~0UL);
3284 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3285 type, rid, start, end, count, flags));
3288 rle = resource_list_find(rl, type, *rid);
3291 return (NULL); /* no resource of that type/rid */
3294 if (rle->flags & RLE_RESERVED) {
3295 if (rle->flags & RLE_ALLOCATED)
3297 if ((flags & RF_ACTIVE) &&
3298 bus_activate_resource(child, type, *rid,
3301 rle->flags |= RLE_ALLOCATED;
3304 panic("resource_list_alloc: resource entry is busy");
3309 count = ulmax(count, rle->count);
3310 end = ulmax(rle->end, start + count - 1);
3313 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3314 type, rid, start, end, count, flags);
3317 * Record the new range.
3320 rle->start = rman_get_start(rle->res);
3321 rle->end = rman_get_end(rle->res);
3329 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3331 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3332 * used with resource_list_alloc().
3334 * @param rl the resource list which was allocated from
3335 * @param bus the parent device of @p child
3336 * @param child the device which is requesting a release
3337 * @param type the type of resource to release
3338 * @param rid the resource identifier
3339 * @param res the resource to release
3342 * @retval non-zero a standard unix error code indicating what
3343 * error condition prevented the operation
3346 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3347 int type, int rid, struct resource *res)
3349 struct resource_list_entry *rle = NULL;
3350 int passthrough = (device_get_parent(child) != bus);
3354 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3358 rle = resource_list_find(rl, type, rid);
3361 panic("resource_list_release: can't find resource");
3363 panic("resource_list_release: resource entry is not busy");
3364 if (rle->flags & RLE_RESERVED) {
3365 if (rle->flags & RLE_ALLOCATED) {
3366 if (rman_get_flags(res) & RF_ACTIVE) {
3367 error = bus_deactivate_resource(child, type,
3372 rle->flags &= ~RLE_ALLOCATED;
3378 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3388 * @brief Release all active resources of a given type
3390 * Release all active resources of a specified type. This is intended
3391 * to be used to cleanup resources leaked by a driver after detach or
3394 * @param rl the resource list which was allocated from
3395 * @param bus the parent device of @p child
3396 * @param child the device whose active resources are being released
3397 * @param type the type of resources to release
3400 * @retval EBUSY at least one resource was active
3403 resource_list_release_active(struct resource_list *rl, device_t bus,
3404 device_t child, int type)
3406 struct resource_list_entry *rle;
3410 STAILQ_FOREACH(rle, rl, link) {
3411 if (rle->type != type)
3413 if (rle->res == NULL)
3415 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3419 error = resource_list_release(rl, bus, child, type,
3420 rman_get_rid(rle->res), rle->res);
3423 "Failed to release active resource: %d\n", error);
3430 * @brief Fully release a reserved resource
3432 * Fully releases a resource reserved via resource_list_reserve().
3434 * @param rl the resource list which was allocated from
3435 * @param bus the parent device of @p child
3436 * @param child the device whose reserved resource is being released
3437 * @param type the type of resource to release
3438 * @param rid the resource identifier
3439 * @param res the resource to release
3442 * @retval non-zero a standard unix error code indicating what
3443 * error condition prevented the operation
3446 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3449 struct resource_list_entry *rle = NULL;
3450 int passthrough = (device_get_parent(child) != bus);
3454 "resource_list_unreserve() should only be called for direct children");
3456 rle = resource_list_find(rl, type, rid);
3459 panic("resource_list_unreserve: can't find resource");
3460 if (!(rle->flags & RLE_RESERVED))
3462 if (rle->flags & RLE_ALLOCATED)
3464 rle->flags &= ~RLE_RESERVED;
3465 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3469 * @brief Print a description of resources in a resource list
3471 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3472 * The name is printed if at least one resource of the given type is available.
3473 * The format is used to print resource start and end.
3475 * @param rl the resource list to print
3476 * @param name the name of @p type, e.g. @c "memory"
3477 * @param type type type of resource entry to print
3478 * @param format printf(9) format string to print resource
3479 * start and end values
3481 * @returns the number of characters printed
3484 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3487 struct resource_list_entry *rle;
3488 int printed, retval;
3492 /* Yes, this is kinda cheating */
3493 STAILQ_FOREACH(rle, rl, link) {
3494 if (rle->type == type) {
3496 retval += printf(" %s ", name);
3498 retval += printf(",");
3500 retval += printf(format, rle->start);
3501 if (rle->count > 1) {
3502 retval += printf("-");
3503 retval += printf(format, rle->start +
3512 * @brief Releases all the resources in a list.
3514 * @param rl The resource list to purge.
3519 resource_list_purge(struct resource_list *rl)
3521 struct resource_list_entry *rle;
3523 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3525 bus_release_resource(rman_get_device(rle->res),
3526 rle->type, rle->rid, rle->res);
3527 STAILQ_REMOVE_HEAD(rl, link);
3533 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3536 return (device_add_child_ordered(dev, order, name, unit));
3540 * @brief Helper function for implementing DEVICE_PROBE()
3542 * This function can be used to help implement the DEVICE_PROBE() for
3543 * a bus (i.e. a device which has other devices attached to it). It
3544 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3548 bus_generic_probe(device_t dev)
3550 devclass_t dc = dev->devclass;
3553 TAILQ_FOREACH(dl, &dc->drivers, link) {
3555 * If this driver's pass is too high, then ignore it.
3556 * For most drivers in the default pass, this will
3557 * never be true. For early-pass drivers they will
3558 * only call the identify routines of eligible drivers
3559 * when this routine is called. Drivers for later
3560 * passes should have their identify routines called
3561 * on early-pass busses during BUS_NEW_PASS().
3563 if (dl->pass > bus_current_pass)
3565 DEVICE_IDENTIFY(dl->driver, dev);
3572 * @brief Helper function for implementing DEVICE_ATTACH()
3574 * This function can be used to help implement the DEVICE_ATTACH() for
3575 * a bus. It calls device_probe_and_attach() for each of the device's
3579 bus_generic_attach(device_t dev)
3583 TAILQ_FOREACH(child, &dev->children, link) {
3584 device_probe_and_attach(child);
3591 * @brief Helper function for implementing DEVICE_DETACH()
3593 * This function can be used to help implement the DEVICE_DETACH() for
3594 * a bus. It calls device_detach() for each of the device's
3598 bus_generic_detach(device_t dev)
3603 if (dev->state != DS_ATTACHED)
3606 TAILQ_FOREACH(child, &dev->children, link) {
3607 if ((error = device_detach(child)) != 0)
3615 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3617 * This function can be used to help implement the DEVICE_SHUTDOWN()
3618 * for a bus. It calls device_shutdown() for each of the device's
3622 bus_generic_shutdown(device_t dev)
3626 TAILQ_FOREACH(child, &dev->children, link) {
3627 device_shutdown(child);
3634 * @brief Helper function for implementing DEVICE_SUSPEND()
3636 * This function can be used to help implement the DEVICE_SUSPEND()
3637 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3638 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3639 * operation is aborted and any devices which were suspended are
3640 * resumed immediately by calling their DEVICE_RESUME() methods.
3643 bus_generic_suspend(device_t dev)
3646 device_t child, child2;
3648 TAILQ_FOREACH(child, &dev->children, link) {
3649 error = DEVICE_SUSPEND(child);
3651 for (child2 = TAILQ_FIRST(&dev->children);
3652 child2 && child2 != child;
3653 child2 = TAILQ_NEXT(child2, link))
3654 DEVICE_RESUME(child2);
3662 * @brief Helper function for implementing DEVICE_RESUME()
3664 * This function can be used to help implement the DEVICE_RESUME() for
3665 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3668 bus_generic_resume(device_t dev)
3672 TAILQ_FOREACH(child, &dev->children, link) {
3673 DEVICE_RESUME(child);
3674 /* if resume fails, there's nothing we can usefully do... */
3680 * @brief Helper function for implementing BUS_PRINT_CHILD().
3682 * This function prints the first part of the ascii representation of
3683 * @p child, including its name, unit and description (if any - see
3684 * device_set_desc()).
3686 * @returns the number of characters printed
3689 bus_print_child_header(device_t dev, device_t child)
3693 if (device_get_desc(child)) {
3694 retval += device_printf(child, "<%s>", device_get_desc(child));
3696 retval += printf("%s", device_get_nameunit(child));
3703 * @brief Helper function for implementing BUS_PRINT_CHILD().
3705 * This function prints the last part of the ascii representation of
3706 * @p child, which consists of the string @c " on " followed by the
3707 * name and unit of the @p dev.
3709 * @returns the number of characters printed
3712 bus_print_child_footer(device_t dev, device_t child)
3714 return (printf(" on %s\n", device_get_nameunit(dev)));
3718 * @brief Helper function for implementing BUS_PRINT_CHILD().
3720 * This function simply calls bus_print_child_header() followed by
3721 * bus_print_child_footer().
3723 * @returns the number of characters printed
3726 bus_generic_print_child(device_t dev, device_t child)
3730 retval += bus_print_child_header(dev, child);
3731 retval += bus_print_child_footer(dev, child);
3737 * @brief Stub function for implementing BUS_READ_IVAR().
3742 bus_generic_read_ivar(device_t dev, device_t child, int index,
3749 * @brief Stub function for implementing BUS_WRITE_IVAR().
3754 bus_generic_write_ivar(device_t dev, device_t child, int index,
3761 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3765 struct resource_list *
3766 bus_generic_get_resource_list(device_t dev, device_t child)
3772 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3774 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3775 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3776 * and then calls device_probe_and_attach() for each unattached child.
3779 bus_generic_driver_added(device_t dev, driver_t *driver)
3783 DEVICE_IDENTIFY(driver, dev);
3784 TAILQ_FOREACH(child, &dev->children, link) {
3785 if (child->state == DS_NOTPRESENT ||
3786 (child->flags & DF_REBID))
3787 device_probe_and_attach(child);
3792 * @brief Helper function for implementing BUS_NEW_PASS().
3794 * This implementing of BUS_NEW_PASS() first calls the identify
3795 * routines for any drivers that probe at the current pass. Then it
3796 * walks the list of devices for this bus. If a device is already
3797 * attached, then it calls BUS_NEW_PASS() on that device. If the
3798 * device is not already attached, it attempts to attach a driver to
3802 bus_generic_new_pass(device_t dev)
3809 TAILQ_FOREACH(dl, &dc->drivers, link) {
3810 if (dl->pass == bus_current_pass)
3811 DEVICE_IDENTIFY(dl->driver, dev);
3813 TAILQ_FOREACH(child, &dev->children, link) {
3814 if (child->state >= DS_ATTACHED)
3815 BUS_NEW_PASS(child);
3816 else if (child->state == DS_NOTPRESENT)
3817 device_probe_and_attach(child);
3822 * @brief Helper function for implementing BUS_SETUP_INTR().
3824 * This simple implementation of BUS_SETUP_INTR() simply calls the
3825 * BUS_SETUP_INTR() method of the parent of @p dev.
3828 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3829 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3832 /* Propagate up the bus hierarchy until someone handles it. */
3834 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3835 filter, intr, arg, cookiep));
3840 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3842 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3843 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3846 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3849 /* Propagate up the bus hierarchy until someone handles it. */
3851 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3856 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3858 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3859 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3862 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3863 struct resource *r, u_long start, u_long end)
3865 /* Propagate up the bus hierarchy until someone handles it. */
3867 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3873 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3875 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3876 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3879 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3880 u_long start, u_long end, u_long count, u_int flags)
3882 /* Propagate up the bus hierarchy until someone handles it. */
3884 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3885 start, end, count, flags));
3890 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3892 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3893 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3896 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3899 /* Propagate up the bus hierarchy until someone handles it. */
3901 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3907 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3909 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3910 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3913 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3916 /* Propagate up the bus hierarchy until someone handles it. */
3918 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3924 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3926 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3927 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3930 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3931 int rid, struct resource *r)
3933 /* Propagate up the bus hierarchy until someone handles it. */
3935 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3941 * @brief Helper function for implementing BUS_BIND_INTR().
3943 * This simple implementation of BUS_BIND_INTR() simply calls the
3944 * BUS_BIND_INTR() method of the parent of @p dev.
3947 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3951 /* Propagate up the bus hierarchy until someone handles it. */
3953 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3958 * @brief Helper function for implementing BUS_CONFIG_INTR().
3960 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3961 * BUS_CONFIG_INTR() method of the parent of @p dev.
3964 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3965 enum intr_polarity pol)
3968 /* Propagate up the bus hierarchy until someone handles it. */
3970 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3975 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3977 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3978 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3981 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3982 void *cookie, const char *descr)
3985 /* Propagate up the bus hierarchy until someone handles it. */
3987 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3993 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3995 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3996 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3999 bus_generic_get_dma_tag(device_t dev, device_t child)
4002 /* Propagate up the bus hierarchy until someone handles it. */
4003 if (dev->parent != NULL)
4004 return (BUS_GET_DMA_TAG(dev->parent, child));
4009 * @brief Helper function for implementing BUS_GET_RESOURCE().
4011 * This implementation of BUS_GET_RESOURCE() uses the
4012 * resource_list_find() function to do most of the work. It calls
4013 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4017 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4018 u_long *startp, u_long *countp)
4020 struct resource_list * rl = NULL;
4021 struct resource_list_entry * rle = NULL;
4023 rl = BUS_GET_RESOURCE_LIST(dev, child);
4027 rle = resource_list_find(rl, type, rid);
4032 *startp = rle->start;
4034 *countp = rle->count;
4040 * @brief Helper function for implementing BUS_SET_RESOURCE().
4042 * This implementation of BUS_SET_RESOURCE() uses the
4043 * resource_list_add() function to do most of the work. It calls
4044 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4048 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4049 u_long start, u_long count)
4051 struct resource_list * rl = NULL;
4053 rl = BUS_GET_RESOURCE_LIST(dev, child);
4057 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4063 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4065 * This implementation of BUS_DELETE_RESOURCE() uses the
4066 * resource_list_delete() function to do most of the work. It calls
4067 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4071 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4073 struct resource_list * rl = NULL;
4075 rl = BUS_GET_RESOURCE_LIST(dev, child);
4079 resource_list_delete(rl, type, rid);
4085 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4087 * This implementation of BUS_RELEASE_RESOURCE() uses the
4088 * resource_list_release() function to do most of the work. It calls
4089 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4092 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4093 int rid, struct resource *r)
4095 struct resource_list * rl = NULL;
4097 if (device_get_parent(child) != dev)
4098 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4101 rl = BUS_GET_RESOURCE_LIST(dev, child);
4105 return (resource_list_release(rl, dev, child, type, rid, r));
4109 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4111 * This implementation of BUS_ALLOC_RESOURCE() uses the
4112 * resource_list_alloc() function to do most of the work. It calls
4113 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4116 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4117 int *rid, u_long start, u_long end, u_long count, u_int flags)
4119 struct resource_list * rl = NULL;
4121 if (device_get_parent(child) != dev)
4122 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4123 type, rid, start, end, count, flags));
4125 rl = BUS_GET_RESOURCE_LIST(dev, child);
4129 return (resource_list_alloc(rl, dev, child, type, rid,
4130 start, end, count, flags));
4134 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4136 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4137 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4140 bus_generic_child_present(device_t dev, device_t child)
4142 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4146 * Some convenience functions to make it easier for drivers to use the
4147 * resource-management functions. All these really do is hide the
4148 * indirection through the parent's method table, making for slightly
4149 * less-wordy code. In the future, it might make sense for this code
4150 * to maintain some sort of a list of resources allocated by each device.
4154 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4155 struct resource **res)
4159 for (i = 0; rs[i].type != -1; i++)
4161 for (i = 0; rs[i].type != -1; i++) {
4162 res[i] = bus_alloc_resource_any(dev,
4163 rs[i].type, &rs[i].rid, rs[i].flags);
4164 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4165 bus_release_resources(dev, rs, res);
4173 bus_release_resources(device_t dev, const struct resource_spec *rs,
4174 struct resource **res)
4178 for (i = 0; rs[i].type != -1; i++)
4179 if (res[i] != NULL) {
4180 bus_release_resource(
4181 dev, rs[i].type, rs[i].rid, res[i]);
4187 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4189 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4193 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4194 u_long count, u_int flags)
4196 if (dev->parent == NULL)
4198 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4203 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4205 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4209 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4212 if (dev->parent == NULL)
4214 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4218 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4220 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4224 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4226 if (dev->parent == NULL)
4228 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4232 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4234 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4238 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4240 if (dev->parent == NULL)
4242 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4246 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4248 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4252 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4254 if (dev->parent == NULL)
4256 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4260 * @brief Wrapper function for BUS_SETUP_INTR().
4262 * This function simply calls the BUS_SETUP_INTR() method of the
4266 bus_setup_intr(device_t dev, struct resource *r, int flags,
4267 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4271 if (dev->parent == NULL)
4273 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4277 if (handler != NULL && !(flags & INTR_MPSAFE))
4278 device_printf(dev, "[GIANT-LOCKED]\n");
4283 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4285 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4289 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4291 if (dev->parent == NULL)
4293 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4297 * @brief Wrapper function for BUS_BIND_INTR().
4299 * This function simply calls the BUS_BIND_INTR() method of the
4303 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4305 if (dev->parent == NULL)
4307 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4311 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4313 * This function first formats the requested description into a
4314 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4315 * the parent of @p dev.
4318 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4319 const char *fmt, ...)
4322 char descr[MAXCOMLEN + 1];
4324 if (dev->parent == NULL)
4327 vsnprintf(descr, sizeof(descr), fmt, ap);
4329 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4333 * @brief Wrapper function for BUS_SET_RESOURCE().
4335 * This function simply calls the BUS_SET_RESOURCE() method of the
4339 bus_set_resource(device_t dev, int type, int rid,
4340 u_long start, u_long count)
4342 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4347 * @brief Wrapper function for BUS_GET_RESOURCE().
4349 * This function simply calls the BUS_GET_RESOURCE() method of the
4353 bus_get_resource(device_t dev, int type, int rid,
4354 u_long *startp, u_long *countp)
4356 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4361 * @brief Wrapper function for BUS_GET_RESOURCE().
4363 * This function simply calls the BUS_GET_RESOURCE() method of the
4364 * parent of @p dev and returns the start value.
4367 bus_get_resource_start(device_t dev, int type, int rid)
4369 u_long start, count;
4372 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4380 * @brief Wrapper function for BUS_GET_RESOURCE().
4382 * This function simply calls the BUS_GET_RESOURCE() method of the
4383 * parent of @p dev and returns the count value.
4386 bus_get_resource_count(device_t dev, int type, int rid)
4388 u_long start, count;
4391 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4399 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4401 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4405 bus_delete_resource(device_t dev, int type, int rid)
4407 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4411 * @brief Wrapper function for BUS_CHILD_PRESENT().
4413 * This function simply calls the BUS_CHILD_PRESENT() method of the
4417 bus_child_present(device_t child)
4419 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4423 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4425 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4429 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4433 parent = device_get_parent(child);
4434 if (parent == NULL) {
4438 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4442 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4444 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4448 bus_child_location_str(device_t child, char *buf, size_t buflen)
4452 parent = device_get_parent(child);
4453 if (parent == NULL) {
4457 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4461 * @brief Wrapper function for BUS_GET_DMA_TAG().
4463 * This function simply calls the BUS_GET_DMA_TAG() method of the
4467 bus_get_dma_tag(device_t dev)
4471 parent = device_get_parent(dev);
4474 return (BUS_GET_DMA_TAG(parent, dev));
4477 /* Resume all devices and then notify userland that we're up again. */
4479 root_resume(device_t dev)
4483 error = bus_generic_resume(dev);
4485 devctl_notify("kern", "power", "resume", NULL);
4490 root_print_child(device_t dev, device_t child)
4494 retval += bus_print_child_header(dev, child);
4495 retval += printf("\n");
4501 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4502 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4505 * If an interrupt mapping gets to here something bad has happened.
4507 panic("root_setup_intr");
4511 * If we get here, assume that the device is permanant and really is
4512 * present in the system. Removable bus drivers are expected to intercept
4513 * this call long before it gets here. We return -1 so that drivers that
4514 * really care can check vs -1 or some ERRNO returned higher in the food
4518 root_child_present(device_t dev, device_t child)
4523 static kobj_method_t root_methods[] = {
4524 /* Device interface */
4525 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4526 KOBJMETHOD(device_suspend, bus_generic_suspend),
4527 KOBJMETHOD(device_resume, root_resume),
4530 KOBJMETHOD(bus_print_child, root_print_child),
4531 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4532 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4533 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4534 KOBJMETHOD(bus_child_present, root_child_present),
4539 static driver_t root_driver = {
4546 devclass_t root_devclass;
4549 root_bus_module_handler(module_t mod, int what, void* arg)
4553 TAILQ_INIT(&bus_data_devices);
4554 kobj_class_compile((kobj_class_t) &root_driver);
4555 root_bus = make_device(NULL, "root", 0);
4556 root_bus->desc = "System root bus";
4557 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4558 root_bus->driver = &root_driver;
4559 root_bus->state = DS_ATTACHED;
4560 root_devclass = devclass_find_internal("root", NULL, FALSE);
4565 device_shutdown(root_bus);
4568 return (EOPNOTSUPP);
4574 static moduledata_t root_bus_mod = {
4576 root_bus_module_handler,
4579 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4582 * @brief Automatically configure devices
4584 * This function begins the autoconfiguration process by calling
4585 * device_probe_and_attach() for each child of the @c root0 device.
4588 root_bus_configure(void)
4593 /* Eventually this will be split up, but this is sufficient for now. */
4594 bus_set_pass(BUS_PASS_DEFAULT);
4598 * @brief Module handler for registering device drivers
4600 * This module handler is used to automatically register device
4601 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4602 * devclass_add_driver() for the driver described by the
4603 * driver_module_data structure pointed to by @p arg
4606 driver_module_handler(module_t mod, int what, void *arg)
4608 struct driver_module_data *dmd;
4609 devclass_t bus_devclass;
4610 kobj_class_t driver;
4613 dmd = (struct driver_module_data *)arg;
4614 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4619 if (dmd->dmd_chainevh)
4620 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4622 pass = dmd->dmd_pass;
4623 driver = dmd->dmd_driver;
4624 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4625 DRIVERNAME(driver), dmd->dmd_busname, pass));
4626 error = devclass_add_driver(bus_devclass, driver, pass,
4631 PDEBUG(("Unloading module: driver %s from bus %s",
4632 DRIVERNAME(dmd->dmd_driver),
4634 error = devclass_delete_driver(bus_devclass,
4637 if (!error && dmd->dmd_chainevh)
4638 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4641 PDEBUG(("Quiesce module: driver %s from bus %s",
4642 DRIVERNAME(dmd->dmd_driver),
4644 error = devclass_quiesce_driver(bus_devclass,
4647 if (!error && dmd->dmd_chainevh)
4648 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4659 * @brief Enumerate all hinted devices for this bus.
4661 * Walks through the hints for this bus and calls the bus_hinted_child
4662 * routine for each one it fines. It searches first for the specific
4663 * bus that's being probed for hinted children (eg isa0), and then for
4664 * generic children (eg isa).
4666 * @param dev bus device to enumerate
4669 bus_enumerate_hinted_children(device_t bus)
4672 const char *dname, *busname;
4676 * enumerate all devices on the specific bus
4678 busname = device_get_nameunit(bus);
4680 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4681 BUS_HINTED_CHILD(bus, dname, dunit);
4684 * and all the generic ones.
4686 busname = device_get_name(bus);
4688 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4689 BUS_HINTED_CHILD(bus, dname, dunit);
4694 /* the _short versions avoid iteration by not calling anything that prints
4695 * more than oneliners. I love oneliners.
4699 print_device_short(device_t dev, int indent)
4704 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4705 dev->unit, dev->desc,
4706 (dev->parent? "":"no "),
4707 (TAILQ_EMPTY(&dev->children)? "no ":""),
4708 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4709 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4710 (dev->flags&DF_WILDCARD? "wildcard,":""),
4711 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4712 (dev->flags&DF_REBID? "rebiddable,":""),
4713 (dev->ivars? "":"no "),
4714 (dev->softc? "":"no "),
4719 print_device(device_t dev, int indent)
4724 print_device_short(dev, indent);
4726 indentprintf(("Parent:\n"));
4727 print_device_short(dev->parent, indent+1);
4728 indentprintf(("Driver:\n"));
4729 print_driver_short(dev->driver, indent+1);
4730 indentprintf(("Devclass:\n"));
4731 print_devclass_short(dev->devclass, indent+1);
4735 print_device_tree_short(device_t dev, int indent)
4736 /* print the device and all its children (indented) */
4743 print_device_short(dev, indent);
4745 TAILQ_FOREACH(child, &dev->children, link) {
4746 print_device_tree_short(child, indent+1);
4751 print_device_tree(device_t dev, int indent)
4752 /* print the device and all its children (indented) */
4759 print_device(dev, indent);
4761 TAILQ_FOREACH(child, &dev->children, link) {
4762 print_device_tree(child, indent+1);
4767 print_driver_short(driver_t *driver, int indent)
4772 indentprintf(("driver %s: softc size = %zd\n",
4773 driver->name, driver->size));
4777 print_driver(driver_t *driver, int indent)
4782 print_driver_short(driver, indent);
4786 print_driver_list(driver_list_t drivers, int indent)
4788 driverlink_t driver;
4790 TAILQ_FOREACH(driver, &drivers, link) {
4791 print_driver(driver->driver, indent);
4796 print_devclass_short(devclass_t dc, int indent)
4801 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4805 print_devclass(devclass_t dc, int indent)
4812 print_devclass_short(dc, indent);
4813 indentprintf(("Drivers:\n"));
4814 print_driver_list(dc->drivers, indent+1);
4816 indentprintf(("Devices:\n"));
4817 for (i = 0; i < dc->maxunit; i++)
4819 print_device(dc->devices[i], indent+1);
4823 print_devclass_list_short(void)
4827 printf("Short listing of devclasses, drivers & devices:\n");
4828 TAILQ_FOREACH(dc, &devclasses, link) {
4829 print_devclass_short(dc, 0);
4834 print_devclass_list(void)
4838 printf("Full listing of devclasses, drivers & devices:\n");
4839 TAILQ_FOREACH(dc, &devclasses, link) {
4840 print_devclass(dc, 0);
4847 * User-space access to the device tree.
4849 * We implement a small set of nodes:
4851 * hw.bus Single integer read method to obtain the
4852 * current generation count.
4853 * hw.bus.devices Reads the entire device tree in flat space.
4854 * hw.bus.rman Resource manager interface
4856 * We might like to add the ability to scan devclasses and/or drivers to
4857 * determine what else is currently loaded/available.
4861 sysctl_bus(SYSCTL_HANDLER_ARGS)
4863 struct u_businfo ubus;
4865 ubus.ub_version = BUS_USER_VERSION;
4866 ubus.ub_generation = bus_data_generation;
4868 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4870 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4871 "bus-related data");
4874 sysctl_devices(SYSCTL_HANDLER_ARGS)
4876 int *name = (int *)arg1;
4877 u_int namelen = arg2;
4880 struct u_device udev; /* XXX this is a bit big */
4886 if (bus_data_generation_check(name[0]))
4892 * Scan the list of devices, looking for the requested index.
4894 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4902 * Populate the return array.
4904 bzero(&udev, sizeof(udev));
4905 udev.dv_handle = (uintptr_t)dev;
4906 udev.dv_parent = (uintptr_t)dev->parent;
4907 if (dev->nameunit != NULL)
4908 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4909 if (dev->desc != NULL)
4910 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4911 if (dev->driver != NULL && dev->driver->name != NULL)
4912 strlcpy(udev.dv_drivername, dev->driver->name,
4913 sizeof(udev.dv_drivername));
4914 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4915 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4916 udev.dv_devflags = dev->devflags;
4917 udev.dv_flags = dev->flags;
4918 udev.dv_state = dev->state;
4919 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4923 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4924 "system device tree");
4927 bus_data_generation_check(int generation)
4929 if (generation != bus_data_generation)
4932 /* XXX generate optimised lists here? */
4937 bus_data_generation_update(void)
4939 bus_data_generation++;
4943 bus_free_resource(device_t dev, int type, struct resource *r)
4947 return (bus_release_resource(dev, type, rman_get_rid(r), r));