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 #define DF_SUSPENDED 0x100 /* Device is suspended. */
139 u_int order; /**< order from device_add_child_ordered() */
140 void *ivars; /**< instance variables */
141 void *softc; /**< current driver's variables */
143 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
144 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
147 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
148 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
152 static int bus_debug = 1;
153 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
156 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
157 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
158 #define DRIVERNAME(d) ((d)? d->name : "no driver")
159 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
162 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
163 * prevent syslog from deleting initial spaces
165 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
167 static void print_device_short(device_t dev, int indent);
168 static void print_device(device_t dev, int indent);
169 void print_device_tree_short(device_t dev, int indent);
170 void print_device_tree(device_t dev, int indent);
171 static void print_driver_short(driver_t *driver, int indent);
172 static void print_driver(driver_t *driver, int indent);
173 static void print_driver_list(driver_list_t drivers, int indent);
174 static void print_devclass_short(devclass_t dc, int indent);
175 static void print_devclass(devclass_t dc, int indent);
176 void print_devclass_list_short(void);
177 void print_devclass_list(void);
180 /* Make the compiler ignore the function calls */
181 #define PDEBUG(a) /* nop */
182 #define DEVICENAME(d) /* nop */
183 #define DRIVERNAME(d) /* nop */
184 #define DEVCLANAME(d) /* nop */
186 #define print_device_short(d,i) /* nop */
187 #define print_device(d,i) /* nop */
188 #define print_device_tree_short(d,i) /* nop */
189 #define print_device_tree(d,i) /* nop */
190 #define print_driver_short(d,i) /* nop */
191 #define print_driver(d,i) /* nop */
192 #define print_driver_list(d,i) /* nop */
193 #define print_devclass_short(d,i) /* nop */
194 #define print_devclass(d,i) /* nop */
195 #define print_devclass_list_short() /* nop */
196 #define print_devclass_list() /* nop */
204 DEVCLASS_SYSCTL_PARENT,
208 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
210 devclass_t dc = (devclass_t)arg1;
214 case DEVCLASS_SYSCTL_PARENT:
215 value = dc->parent ? dc->parent->name : "";
220 return (SYSCTL_OUT(req, value, strlen(value)));
224 devclass_sysctl_init(devclass_t dc)
227 if (dc->sysctl_tree != NULL)
229 sysctl_ctx_init(&dc->sysctl_ctx);
230 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
231 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
232 CTLFLAG_RD, NULL, "");
233 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
234 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
235 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
241 DEVICE_SYSCTL_DRIVER,
242 DEVICE_SYSCTL_LOCATION,
243 DEVICE_SYSCTL_PNPINFO,
244 DEVICE_SYSCTL_PARENT,
248 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
250 device_t dev = (device_t)arg1;
257 case DEVICE_SYSCTL_DESC:
258 value = dev->desc ? dev->desc : "";
260 case DEVICE_SYSCTL_DRIVER:
261 value = dev->driver ? dev->driver->name : "";
263 case DEVICE_SYSCTL_LOCATION:
264 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
265 bus_child_location_str(dev, buf, 1024);
267 case DEVICE_SYSCTL_PNPINFO:
268 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
269 bus_child_pnpinfo_str(dev, buf, 1024);
271 case DEVICE_SYSCTL_PARENT:
272 value = dev->parent ? dev->parent->nameunit : "";
277 error = SYSCTL_OUT(req, value, strlen(value));
284 device_sysctl_init(device_t dev)
286 devclass_t dc = dev->devclass;
288 if (dev->sysctl_tree != NULL)
290 devclass_sysctl_init(dc);
291 sysctl_ctx_init(&dev->sysctl_ctx);
292 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
293 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
294 dev->nameunit + strlen(dc->name),
295 CTLFLAG_RD, NULL, "");
296 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
297 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
298 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
299 "device description");
300 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
301 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
302 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
303 "device driver name");
304 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
305 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
306 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
307 "device location relative to parent");
308 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
309 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
310 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
311 "device identification");
312 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
313 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
314 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
319 device_sysctl_update(device_t dev)
321 devclass_t dc = dev->devclass;
323 if (dev->sysctl_tree == NULL)
325 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
329 device_sysctl_fini(device_t dev)
331 if (dev->sysctl_tree == NULL)
333 sysctl_ctx_free(&dev->sysctl_ctx);
334 dev->sysctl_tree = NULL;
338 * /dev/devctl implementation
342 * This design allows only one reader for /dev/devctl. This is not desirable
343 * in the long run, but will get a lot of hair out of this implementation.
344 * Maybe we should make this device a clonable device.
346 * Also note: we specifically do not attach a device to the device_t tree
347 * to avoid potential chicken and egg problems. One could argue that all
348 * of this belongs to the root node. One could also further argue that the
349 * sysctl interface that we have not might more properly be an ioctl
350 * interface, but at this stage of the game, I'm not inclined to rock that
353 * I'm also not sure that the SIGIO support is done correctly or not, as
354 * I copied it from a driver that had SIGIO support that likely hasn't been
355 * tested since 3.4 or 2.2.8!
358 /* Deprecated way to adjust queue length */
359 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
360 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
361 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_disable, "I",
362 "devctl disable -- deprecated");
364 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
365 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
366 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
367 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
368 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
370 static d_open_t devopen;
371 static d_close_t devclose;
372 static d_read_t devread;
373 static d_ioctl_t devioctl;
374 static d_poll_t devpoll;
375 static d_kqfilter_t devkqfilter;
377 static struct cdevsw dev_cdevsw = {
378 .d_version = D_VERSION,
384 .d_kqfilter = devkqfilter,
388 struct dev_event_info
391 TAILQ_ENTRY(dev_event_info) dei_link;
394 TAILQ_HEAD(devq, dev_event_info);
396 static struct dev_softc
409 static void filt_devctl_detach(struct knote *kn);
410 static int filt_devctl_read(struct knote *kn, long hint);
412 struct filterops devctl_rfiltops = {
414 .f_detach = filt_devctl_detach,
415 .f_event = filt_devctl_read,
418 static struct cdev *devctl_dev;
423 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
424 UID_ROOT, GID_WHEEL, 0600, "devctl");
425 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
426 cv_init(&devsoftc.cv, "dev cv");
427 TAILQ_INIT(&devsoftc.devq);
428 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
432 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
435 mtx_lock(&devsoftc.mtx);
436 if (devsoftc.inuse) {
437 mtx_unlock(&devsoftc.mtx);
442 mtx_unlock(&devsoftc.mtx);
447 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
450 mtx_lock(&devsoftc.mtx);
452 devsoftc.nonblock = 0;
454 cv_broadcast(&devsoftc.cv);
455 funsetown(&devsoftc.sigio);
456 mtx_unlock(&devsoftc.mtx);
461 * The read channel for this device is used to report changes to
462 * userland in realtime. We are required to free the data as well as
463 * the n1 object because we allocate them separately. Also note that
464 * we return one record at a time. If you try to read this device a
465 * character at a time, you will lose the rest of the data. Listening
466 * programs are expected to cope.
469 devread(struct cdev *dev, struct uio *uio, int ioflag)
471 struct dev_event_info *n1;
474 mtx_lock(&devsoftc.mtx);
475 while (TAILQ_EMPTY(&devsoftc.devq)) {
476 if (devsoftc.nonblock) {
477 mtx_unlock(&devsoftc.mtx);
480 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
483 * Need to translate ERESTART to EINTR here? -- jake
485 mtx_unlock(&devsoftc.mtx);
489 n1 = TAILQ_FIRST(&devsoftc.devq);
490 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
492 mtx_unlock(&devsoftc.mtx);
493 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
494 free(n1->dei_data, M_BUS);
500 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
506 devsoftc.nonblock = 1;
508 devsoftc.nonblock = 0;
517 return fsetown(*(int *)data, &devsoftc.sigio);
519 *(int *)data = fgetown(&devsoftc.sigio);
522 /* (un)Support for other fcntl() calls. */
533 devpoll(struct cdev *dev, int events, struct thread *td)
537 mtx_lock(&devsoftc.mtx);
538 if (events & (POLLIN | POLLRDNORM)) {
539 if (!TAILQ_EMPTY(&devsoftc.devq))
540 revents = events & (POLLIN | POLLRDNORM);
542 selrecord(td, &devsoftc.sel);
544 mtx_unlock(&devsoftc.mtx);
550 devkqfilter(struct cdev *dev, struct knote *kn)
554 if (kn->kn_filter == EVFILT_READ) {
555 kn->kn_fop = &devctl_rfiltops;
556 knlist_add(&devsoftc.sel.si_note, kn, 0);
564 filt_devctl_detach(struct knote *kn)
567 knlist_remove(&devsoftc.sel.si_note, kn, 0);
571 filt_devctl_read(struct knote *kn, long hint)
573 kn->kn_data = devsoftc.queued;
574 return (kn->kn_data != 0);
578 * @brief Return whether the userland process is running
581 devctl_process_running(void)
583 return (devsoftc.inuse == 1);
587 * @brief Queue data to be read from the devctl device
589 * Generic interface to queue data to the devctl device. It is
590 * assumed that @p data is properly formatted. It is further assumed
591 * that @p data is allocated using the M_BUS malloc type.
594 devctl_queue_data_f(char *data, int flags)
596 struct dev_event_info *n1 = NULL, *n2 = NULL;
598 if (strlen(data) == 0)
600 if (devctl_queue_length == 0)
602 n1 = malloc(sizeof(*n1), M_BUS, flags);
606 mtx_lock(&devsoftc.mtx);
607 if (devctl_queue_length == 0) {
608 mtx_unlock(&devsoftc.mtx);
609 free(n1->dei_data, M_BUS);
613 /* Leave at least one spot in the queue... */
614 while (devsoftc.queued > devctl_queue_length - 1) {
615 n2 = TAILQ_FIRST(&devsoftc.devq);
616 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
617 free(n2->dei_data, M_BUS);
621 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
623 cv_broadcast(&devsoftc.cv);
624 KNOTE_LOCKED(&devsoftc.sel.si_note, 0);
625 mtx_unlock(&devsoftc.mtx);
626 selwakeup(&devsoftc.sel);
627 if (devsoftc.async && devsoftc.sigio != NULL)
628 pgsigio(&devsoftc.sigio, SIGIO, 0);
632 * We have to free data on all error paths since the caller
633 * assumes it will be free'd when this item is dequeued.
640 devctl_queue_data(char *data)
643 devctl_queue_data_f(data, M_NOWAIT);
647 * @brief Send a 'notification' to userland, using standard ways
650 devctl_notify_f(const char *system, const char *subsystem, const char *type,
651 const char *data, int flags)
657 return; /* BOGUS! Must specify system. */
658 if (subsystem == NULL)
659 return; /* BOGUS! Must specify subsystem. */
661 return; /* BOGUS! Must specify type. */
662 len += strlen(" system=") + strlen(system);
663 len += strlen(" subsystem=") + strlen(subsystem);
664 len += strlen(" type=") + strlen(type);
665 /* add in the data message plus newline. */
668 len += 3; /* '!', '\n', and NUL */
669 msg = malloc(len, M_BUS, flags);
671 return; /* Drop it on the floor */
673 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
674 system, subsystem, type, data);
676 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
677 system, subsystem, type);
678 devctl_queue_data_f(msg, flags);
682 devctl_notify(const char *system, const char *subsystem, const char *type,
686 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
690 * Common routine that tries to make sending messages as easy as possible.
691 * We allocate memory for the data, copy strings into that, but do not
692 * free it unless there's an error. The dequeue part of the driver should
693 * free the data. We don't send data when the device is disabled. We do
694 * send data, even when we have no listeners, because we wish to avoid
695 * races relating to startup and restart of listening applications.
697 * devaddq is designed to string together the type of event, with the
698 * object of that event, plus the plug and play info and location info
699 * for that event. This is likely most useful for devices, but less
700 * useful for other consumers of this interface. Those should use
701 * the devctl_queue_data() interface instead.
704 devaddq(const char *type, const char *what, device_t dev)
711 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
713 data = malloc(1024, M_BUS, M_NOWAIT);
717 /* get the bus specific location of this device */
718 loc = malloc(1024, M_BUS, M_NOWAIT);
722 bus_child_location_str(dev, loc, 1024);
724 /* Get the bus specific pnp info of this device */
725 pnp = malloc(1024, M_BUS, M_NOWAIT);
729 bus_child_pnpinfo_str(dev, pnp, 1024);
731 /* Get the parent of this device, or / if high enough in the tree. */
732 if (device_get_parent(dev) == NULL)
733 parstr = "."; /* Or '/' ? */
735 parstr = device_get_nameunit(device_get_parent(dev));
736 /* String it all together. */
737 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
741 devctl_queue_data(data);
751 * A device was added to the tree. We are called just after it successfully
752 * attaches (that is, probe and attach success for this device). No call
753 * is made if a device is merely parented into the tree. See devnomatch
754 * if probe fails. If attach fails, no notification is sent (but maybe
755 * we should have a different message for this).
758 devadded(device_t dev)
760 devaddq("+", device_get_nameunit(dev), dev);
764 * A device was removed from the tree. We are called just before this
768 devremoved(device_t dev)
770 devaddq("-", device_get_nameunit(dev), dev);
774 * Called when there's no match for this device. This is only called
775 * the first time that no match happens, so we don't keep getting this
776 * message. Should that prove to be undesirable, we can change it.
777 * This is called when all drivers that can attach to a given bus
778 * decline to accept this device. Other errors may not be detected.
781 devnomatch(device_t dev)
783 devaddq("?", "", dev);
787 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
789 struct dev_event_info *n1;
792 dis = (devctl_queue_length == 0);
793 error = sysctl_handle_int(oidp, &dis, 0, req);
794 if (error || !req->newptr)
796 if (mtx_initialized(&devsoftc.mtx))
797 mtx_lock(&devsoftc.mtx);
799 while (!TAILQ_EMPTY(&devsoftc.devq)) {
800 n1 = TAILQ_FIRST(&devsoftc.devq);
801 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
802 free(n1->dei_data, M_BUS);
806 devctl_queue_length = 0;
808 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
810 if (mtx_initialized(&devsoftc.mtx))
811 mtx_unlock(&devsoftc.mtx);
816 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
818 struct dev_event_info *n1;
821 q = devctl_queue_length;
822 error = sysctl_handle_int(oidp, &q, 0, req);
823 if (error || !req->newptr)
827 if (mtx_initialized(&devsoftc.mtx))
828 mtx_lock(&devsoftc.mtx);
829 devctl_queue_length = q;
830 while (devsoftc.queued > devctl_queue_length) {
831 n1 = TAILQ_FIRST(&devsoftc.devq);
832 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
833 free(n1->dei_data, M_BUS);
837 if (mtx_initialized(&devsoftc.mtx))
838 mtx_unlock(&devsoftc.mtx);
842 /* End of /dev/devctl code */
844 static TAILQ_HEAD(,device) bus_data_devices;
845 static int bus_data_generation = 1;
847 static kobj_method_t null_methods[] = {
851 DEFINE_CLASS(null, null_methods, 0);
854 * Bus pass implementation
857 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
858 int bus_current_pass = BUS_PASS_ROOT;
862 * @brief Register the pass level of a new driver attachment
864 * Register a new driver attachment's pass level. If no driver
865 * attachment with the same pass level has been added, then @p new
866 * will be added to the global passes list.
868 * @param new the new driver attachment
871 driver_register_pass(struct driverlink *new)
873 struct driverlink *dl;
875 /* We only consider pass numbers during boot. */
876 if (bus_current_pass == BUS_PASS_DEFAULT)
880 * Walk the passes list. If we already know about this pass
881 * then there is nothing to do. If we don't, then insert this
882 * driver link into the list.
884 TAILQ_FOREACH(dl, &passes, passlink) {
885 if (dl->pass < new->pass)
887 if (dl->pass == new->pass)
889 TAILQ_INSERT_BEFORE(dl, new, passlink);
892 TAILQ_INSERT_TAIL(&passes, new, passlink);
896 * @brief Raise the current bus pass
898 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
899 * method on the root bus to kick off a new device tree scan for each
900 * new pass level that has at least one driver.
903 bus_set_pass(int pass)
905 struct driverlink *dl;
907 if (bus_current_pass > pass)
908 panic("Attempt to lower bus pass level");
910 TAILQ_FOREACH(dl, &passes, passlink) {
911 /* Skip pass values below the current pass level. */
912 if (dl->pass <= bus_current_pass)
916 * Bail once we hit a driver with a pass level that is
923 * Raise the pass level to the next level and rescan
926 bus_current_pass = dl->pass;
927 BUS_NEW_PASS(root_bus);
931 * If there isn't a driver registered for the requested pass,
932 * then bus_current_pass might still be less than 'pass'. Set
933 * it to 'pass' in that case.
935 if (bus_current_pass < pass)
936 bus_current_pass = pass;
937 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
941 * Devclass implementation
944 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
948 * @brief Find or create a device class
950 * If a device class with the name @p classname exists, return it,
951 * otherwise if @p create is non-zero create and return a new device
954 * If @p parentname is non-NULL, the parent of the devclass is set to
955 * the devclass of that name.
957 * @param classname the devclass name to find or create
958 * @param parentname the parent devclass name or @c NULL
959 * @param create non-zero to create a devclass
962 devclass_find_internal(const char *classname, const char *parentname,
967 PDEBUG(("looking for %s", classname));
971 TAILQ_FOREACH(dc, &devclasses, link) {
972 if (!strcmp(dc->name, classname))
977 PDEBUG(("creating %s", classname));
978 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
979 M_BUS, M_NOWAIT | M_ZERO);
983 dc->name = (char*) (dc + 1);
984 strcpy(dc->name, classname);
985 TAILQ_INIT(&dc->drivers);
986 TAILQ_INSERT_TAIL(&devclasses, dc, link);
988 bus_data_generation_update();
992 * If a parent class is specified, then set that as our parent so
993 * that this devclass will support drivers for the parent class as
994 * well. If the parent class has the same name don't do this though
995 * as it creates a cycle that can trigger an infinite loop in
996 * device_probe_child() if a device exists for which there is no
999 if (parentname && dc && !dc->parent &&
1000 strcmp(classname, parentname) != 0) {
1001 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
1002 dc->parent->flags |= DC_HAS_CHILDREN;
1009 * @brief Create a device class
1011 * If a device class with the name @p classname exists, return it,
1012 * otherwise create and return a new device class.
1014 * @param classname the devclass name to find or create
1017 devclass_create(const char *classname)
1019 return (devclass_find_internal(classname, NULL, TRUE));
1023 * @brief Find a device class
1025 * If a device class with the name @p classname exists, return it,
1026 * otherwise return @c NULL.
1028 * @param classname the devclass name to find
1031 devclass_find(const char *classname)
1033 return (devclass_find_internal(classname, NULL, FALSE));
1037 * @brief Register that a device driver has been added to a devclass
1039 * Register that a device driver has been added to a devclass. This
1040 * is called by devclass_add_driver to accomplish the recursive
1041 * notification of all the children classes of dc, as well as dc.
1042 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1045 * We do a full search here of the devclass list at each iteration
1046 * level to save storing children-lists in the devclass structure. If
1047 * we ever move beyond a few dozen devices doing this, we may need to
1050 * @param dc the devclass to edit
1051 * @param driver the driver that was just added
1054 devclass_driver_added(devclass_t dc, driver_t *driver)
1060 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1062 for (i = 0; i < dc->maxunit; i++)
1063 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1064 BUS_DRIVER_ADDED(dc->devices[i], driver);
1067 * Walk through the children classes. Since we only keep a
1068 * single parent pointer around, we walk the entire list of
1069 * devclasses looking for children. We set the
1070 * DC_HAS_CHILDREN flag when a child devclass is created on
1071 * the parent, so we only walk the list for those devclasses
1072 * that have children.
1074 if (!(dc->flags & DC_HAS_CHILDREN))
1077 TAILQ_FOREACH(dc, &devclasses, link) {
1078 if (dc->parent == parent)
1079 devclass_driver_added(dc, driver);
1084 * @brief Add a device driver to a device class
1086 * Add a device driver to a devclass. This is normally called
1087 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1088 * all devices in the devclass will be called to allow them to attempt
1089 * to re-probe any unmatched children.
1091 * @param dc the devclass to edit
1092 * @param driver the driver to register
1095 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1098 const char *parentname;
1100 PDEBUG(("%s", DRIVERNAME(driver)));
1102 /* Don't allow invalid pass values. */
1103 if (pass <= BUS_PASS_ROOT)
1106 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1111 * Compile the driver's methods. Also increase the reference count
1112 * so that the class doesn't get freed when the last instance
1113 * goes. This means we can safely use static methods and avoids a
1114 * double-free in devclass_delete_driver.
1116 kobj_class_compile((kobj_class_t) driver);
1119 * If the driver has any base classes, make the
1120 * devclass inherit from the devclass of the driver's
1121 * first base class. This will allow the system to
1122 * search for drivers in both devclasses for children
1123 * of a device using this driver.
1125 if (driver->baseclasses)
1126 parentname = driver->baseclasses[0]->name;
1129 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1131 dl->driver = driver;
1132 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1133 driver->refs++; /* XXX: kobj_mtx */
1135 driver_register_pass(dl);
1137 devclass_driver_added(dc, driver);
1138 bus_data_generation_update();
1143 * @brief Register that a device driver has been deleted from a devclass
1145 * Register that a device driver has been removed from a devclass.
1146 * This is called by devclass_delete_driver to accomplish the
1147 * recursive notification of all the children classes of busclass, as
1148 * well as busclass. Each layer will attempt to detach the driver
1149 * from any devices that are children of the bus's devclass. The function
1150 * will return an error if a device fails to detach.
1152 * We do a full search here of the devclass list at each iteration
1153 * level to save storing children-lists in the devclass structure. If
1154 * we ever move beyond a few dozen devices doing this, we may need to
1157 * @param busclass the devclass of the parent bus
1158 * @param dc the devclass of the driver being deleted
1159 * @param driver the driver being deleted
1162 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1169 * Disassociate from any devices. We iterate through all the
1170 * devices in the devclass of the driver and detach any which are
1171 * using the driver and which have a parent in the devclass which
1172 * we are deleting from.
1174 * Note that since a driver can be in multiple devclasses, we
1175 * should not detach devices which are not children of devices in
1176 * the affected devclass.
1178 for (i = 0; i < dc->maxunit; i++) {
1179 if (dc->devices[i]) {
1180 dev = dc->devices[i];
1181 if (dev->driver == driver && dev->parent &&
1182 dev->parent->devclass == busclass) {
1183 if ((error = device_detach(dev)) != 0)
1185 BUS_PROBE_NOMATCH(dev->parent, dev);
1187 dev->flags |= DF_DONENOMATCH;
1193 * Walk through the children classes. Since we only keep a
1194 * single parent pointer around, we walk the entire list of
1195 * devclasses looking for children. We set the
1196 * DC_HAS_CHILDREN flag when a child devclass is created on
1197 * the parent, so we only walk the list for those devclasses
1198 * that have children.
1200 if (!(busclass->flags & DC_HAS_CHILDREN))
1203 TAILQ_FOREACH(busclass, &devclasses, link) {
1204 if (busclass->parent == parent) {
1205 error = devclass_driver_deleted(busclass, dc, driver);
1214 * @brief Delete a device driver from a device class
1216 * Delete a device driver from a devclass. This is normally called
1217 * automatically by DRIVER_MODULE().
1219 * If the driver is currently attached to any devices,
1220 * devclass_delete_driver() will first attempt to detach from each
1221 * device. If one of the detach calls fails, the driver will not be
1224 * @param dc the devclass to edit
1225 * @param driver the driver to unregister
1228 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1230 devclass_t dc = devclass_find(driver->name);
1234 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1240 * Find the link structure in the bus' list of drivers.
1242 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1243 if (dl->driver == driver)
1248 PDEBUG(("%s not found in %s list", driver->name,
1253 error = devclass_driver_deleted(busclass, dc, driver);
1257 TAILQ_REMOVE(&busclass->drivers, dl, link);
1262 if (driver->refs == 0)
1263 kobj_class_free((kobj_class_t) driver);
1265 bus_data_generation_update();
1270 * @brief Quiesces a set of device drivers from a device class
1272 * Quiesce a device driver from a devclass. This is normally called
1273 * automatically by DRIVER_MODULE().
1275 * If the driver is currently attached to any devices,
1276 * devclass_quiesece_driver() will first attempt to quiesce each
1279 * @param dc the devclass to edit
1280 * @param driver the driver to unregister
1283 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1285 devclass_t dc = devclass_find(driver->name);
1291 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1297 * Find the link structure in the bus' list of drivers.
1299 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1300 if (dl->driver == driver)
1305 PDEBUG(("%s not found in %s list", driver->name,
1311 * Quiesce all devices. We iterate through all the devices in
1312 * the devclass of the driver and quiesce any which are using
1313 * the driver and which have a parent in the devclass which we
1316 * Note that since a driver can be in multiple devclasses, we
1317 * should not quiesce devices which are not children of
1318 * devices in the affected devclass.
1320 for (i = 0; i < dc->maxunit; i++) {
1321 if (dc->devices[i]) {
1322 dev = dc->devices[i];
1323 if (dev->driver == driver && dev->parent &&
1324 dev->parent->devclass == busclass) {
1325 if ((error = device_quiesce(dev)) != 0)
1338 devclass_find_driver_internal(devclass_t dc, const char *classname)
1342 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1344 TAILQ_FOREACH(dl, &dc->drivers, link) {
1345 if (!strcmp(dl->driver->name, classname))
1349 PDEBUG(("not found"));
1354 * @brief Return the name of the devclass
1357 devclass_get_name(devclass_t dc)
1363 * @brief Find a device given a unit number
1365 * @param dc the devclass to search
1366 * @param unit the unit number to search for
1368 * @returns the device with the given unit number or @c
1369 * NULL if there is no such device
1372 devclass_get_device(devclass_t dc, int unit)
1374 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1376 return (dc->devices[unit]);
1380 * @brief Find the softc field of a device given a unit number
1382 * @param dc the devclass to search
1383 * @param unit the unit number to search for
1385 * @returns the softc field of the device with the given
1386 * unit number or @c NULL if there is no such
1390 devclass_get_softc(devclass_t dc, int unit)
1394 dev = devclass_get_device(dc, unit);
1398 return (device_get_softc(dev));
1402 * @brief Get a list of devices in the devclass
1404 * An array containing a list of all the devices in the given devclass
1405 * is allocated and returned in @p *devlistp. The number of devices
1406 * in the array is returned in @p *devcountp. The caller should free
1407 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1409 * @param dc the devclass to examine
1410 * @param devlistp points at location for array pointer return
1412 * @param devcountp points at location for array size return value
1415 * @retval ENOMEM the array allocation failed
1418 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1423 count = devclass_get_count(dc);
1424 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1429 for (i = 0; i < dc->maxunit; i++) {
1430 if (dc->devices[i]) {
1431 list[count] = dc->devices[i];
1443 * @brief Get a list of drivers in the devclass
1445 * An array containing a list of pointers to all the drivers in the
1446 * given devclass is allocated and returned in @p *listp. The number
1447 * of drivers in the array is returned in @p *countp. The caller should
1448 * free the array using @c free(p, M_TEMP).
1450 * @param dc the devclass to examine
1451 * @param listp gives location for array pointer return value
1452 * @param countp gives location for number of array elements
1456 * @retval ENOMEM the array allocation failed
1459 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1466 TAILQ_FOREACH(dl, &dc->drivers, link)
1468 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1473 TAILQ_FOREACH(dl, &dc->drivers, link) {
1474 list[count] = dl->driver;
1484 * @brief Get the number of devices in a devclass
1486 * @param dc the devclass to examine
1489 devclass_get_count(devclass_t dc)
1494 for (i = 0; i < dc->maxunit; i++)
1501 * @brief Get the maximum unit number used in a devclass
1503 * Note that this is one greater than the highest currently-allocated
1504 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1505 * that not even the devclass has been allocated yet.
1507 * @param dc the devclass to examine
1510 devclass_get_maxunit(devclass_t dc)
1514 return (dc->maxunit);
1518 * @brief Find a free unit number in a devclass
1520 * This function searches for the first unused unit number greater
1521 * that or equal to @p unit.
1523 * @param dc the devclass to examine
1524 * @param unit the first unit number to check
1527 devclass_find_free_unit(devclass_t dc, int unit)
1531 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1537 * @brief Set the parent of a devclass
1539 * The parent class is normally initialised automatically by
1542 * @param dc the devclass to edit
1543 * @param pdc the new parent devclass
1546 devclass_set_parent(devclass_t dc, devclass_t pdc)
1552 * @brief Get the parent of a devclass
1554 * @param dc the devclass to examine
1557 devclass_get_parent(devclass_t dc)
1559 return (dc->parent);
1562 struct sysctl_ctx_list *
1563 devclass_get_sysctl_ctx(devclass_t dc)
1565 return (&dc->sysctl_ctx);
1569 devclass_get_sysctl_tree(devclass_t dc)
1571 return (dc->sysctl_tree);
1576 * @brief Allocate a unit number
1578 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1579 * will do). The allocated unit number is returned in @p *unitp.
1581 * @param dc the devclass to allocate from
1582 * @param unitp points at the location for the allocated unit
1586 * @retval EEXIST the requested unit number is already allocated
1587 * @retval ENOMEM memory allocation failure
1590 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1595 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1597 /* Ask the parent bus if it wants to wire this device. */
1599 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1602 /* If we were given a wired unit number, check for existing device */
1605 if (unit >= 0 && unit < dc->maxunit &&
1606 dc->devices[unit] != NULL) {
1608 printf("%s: %s%d already exists; skipping it\n",
1609 dc->name, dc->name, *unitp);
1613 /* Unwired device, find the next available slot for it */
1615 for (unit = 0;; unit++) {
1616 /* If there is an "at" hint for a unit then skip it. */
1617 if (resource_string_value(dc->name, unit, "at", &s) ==
1621 /* If this device slot is already in use, skip it. */
1622 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1630 * We've selected a unit beyond the length of the table, so let's
1631 * extend the table to make room for all units up to and including
1634 if (unit >= dc->maxunit) {
1635 device_t *newlist, *oldlist;
1638 oldlist = dc->devices;
1639 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1640 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1643 if (oldlist != NULL)
1644 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1645 bzero(newlist + dc->maxunit,
1646 sizeof(device_t) * (newsize - dc->maxunit));
1647 dc->devices = newlist;
1648 dc->maxunit = newsize;
1649 if (oldlist != NULL)
1650 free(oldlist, M_BUS);
1652 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1660 * @brief Add a device to a devclass
1662 * A unit number is allocated for the device (using the device's
1663 * preferred unit number if any) and the device is registered in the
1664 * devclass. This allows the device to be looked up by its unit
1665 * number, e.g. by decoding a dev_t minor number.
1667 * @param dc the devclass to add to
1668 * @param dev the device to add
1671 * @retval EEXIST the requested unit number is already allocated
1672 * @retval ENOMEM memory allocation failure
1675 devclass_add_device(devclass_t dc, device_t dev)
1679 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1681 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1684 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1688 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1689 free(dev->nameunit, M_BUS);
1690 dev->nameunit = NULL;
1693 dc->devices[dev->unit] = dev;
1695 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1702 * @brief Delete a device from a devclass
1704 * The device is removed from the devclass's device list and its unit
1707 * @param dc the devclass to delete from
1708 * @param dev the device to delete
1713 devclass_delete_device(devclass_t dc, device_t dev)
1718 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1720 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1721 panic("devclass_delete_device: inconsistent device class");
1722 dc->devices[dev->unit] = NULL;
1723 if (dev->flags & DF_WILDCARD)
1725 dev->devclass = NULL;
1726 free(dev->nameunit, M_BUS);
1727 dev->nameunit = NULL;
1734 * @brief Make a new device and add it as a child of @p parent
1736 * @param parent the parent of the new device
1737 * @param name the devclass name of the new device or @c NULL
1738 * to leave the devclass unspecified
1739 * @parem unit the unit number of the new device of @c -1 to
1740 * leave the unit number unspecified
1742 * @returns the new device
1745 make_device(device_t parent, const char *name, int unit)
1750 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1753 dc = devclass_find_internal(name, NULL, TRUE);
1755 printf("make_device: can't find device class %s\n",
1763 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1767 dev->parent = parent;
1768 TAILQ_INIT(&dev->children);
1769 kobj_init((kobj_t) dev, &null_class);
1771 dev->devclass = NULL;
1773 dev->nameunit = NULL;
1777 dev->flags = DF_ENABLED;
1780 dev->flags |= DF_WILDCARD;
1782 dev->flags |= DF_FIXEDCLASS;
1783 if (devclass_add_device(dc, dev)) {
1784 kobj_delete((kobj_t) dev, M_BUS);
1791 dev->state = DS_NOTPRESENT;
1793 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1794 bus_data_generation_update();
1801 * @brief Print a description of a device.
1804 device_print_child(device_t dev, device_t child)
1808 if (device_is_alive(child))
1809 retval += BUS_PRINT_CHILD(dev, child);
1811 retval += device_printf(child, " not found\n");
1817 * @brief Create a new device
1819 * This creates a new device and adds it as a child of an existing
1820 * parent device. The new device will be added after the last existing
1821 * child with order zero.
1823 * @param dev the device which will be the parent of the
1825 * @param name devclass name for new device or @c NULL if not
1827 * @param unit unit number for new device or @c -1 if not
1830 * @returns the new device
1833 device_add_child(device_t dev, const char *name, int unit)
1835 return (device_add_child_ordered(dev, 0, name, unit));
1839 * @brief Create a new device
1841 * This creates a new device and adds it as a child of an existing
1842 * parent device. The new device will be added after the last existing
1843 * child with the same order.
1845 * @param dev the device which will be the parent of the
1847 * @param order a value which is used to partially sort the
1848 * children of @p dev - devices created using
1849 * lower values of @p order appear first in @p
1850 * dev's list of children
1851 * @param name devclass name for new device or @c NULL if not
1853 * @param unit unit number for new device or @c -1 if not
1856 * @returns the new device
1859 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1864 PDEBUG(("%s at %s with order %u as unit %d",
1865 name, DEVICENAME(dev), order, unit));
1866 KASSERT(name != NULL || unit == -1,
1867 ("child device with wildcard name and specific unit number"));
1869 child = make_device(dev, name, unit);
1872 child->order = order;
1874 TAILQ_FOREACH(place, &dev->children, link) {
1875 if (place->order > order)
1881 * The device 'place' is the first device whose order is
1882 * greater than the new child.
1884 TAILQ_INSERT_BEFORE(place, child, link);
1887 * The new child's order is greater or equal to the order of
1888 * any existing device. Add the child to the tail of the list.
1890 TAILQ_INSERT_TAIL(&dev->children, child, link);
1893 bus_data_generation_update();
1898 * @brief Delete a device
1900 * This function deletes a device along with all of its children. If
1901 * the device currently has a driver attached to it, the device is
1902 * detached first using device_detach().
1904 * @param dev the parent device
1905 * @param child the device to delete
1908 * @retval non-zero a unit error code describing the error
1911 device_delete_child(device_t dev, device_t child)
1914 device_t grandchild;
1916 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1918 /* remove children first */
1919 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1920 error = device_delete_child(child, grandchild);
1925 if ((error = device_detach(child)) != 0)
1927 if (child->devclass)
1928 devclass_delete_device(child->devclass, child);
1930 BUS_CHILD_DELETED(dev, child);
1931 TAILQ_REMOVE(&dev->children, child, link);
1932 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1933 kobj_delete((kobj_t) child, M_BUS);
1935 bus_data_generation_update();
1940 * @brief Delete all children devices of the given device, if any.
1942 * This function deletes all children devices of the given device, if
1943 * any, using the device_delete_child() function for each device it
1944 * finds. If a child device cannot be deleted, this function will
1945 * return an error code.
1947 * @param dev the parent device
1950 * @retval non-zero a device would not detach
1953 device_delete_children(device_t dev)
1958 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1962 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1963 error = device_delete_child(dev, child);
1965 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1973 * @brief Find a device given a unit number
1975 * This is similar to devclass_get_devices() but only searches for
1976 * devices which have @p dev as a parent.
1978 * @param dev the parent device to search
1979 * @param unit the unit number to search for. If the unit is -1,
1980 * return the first child of @p dev which has name
1981 * @p classname (that is, the one with the lowest unit.)
1983 * @returns the device with the given unit number or @c
1984 * NULL if there is no such device
1987 device_find_child(device_t dev, const char *classname, int unit)
1992 dc = devclass_find(classname);
1997 child = devclass_get_device(dc, unit);
1998 if (child && child->parent == dev)
2001 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2002 child = devclass_get_device(dc, unit);
2003 if (child && child->parent == dev)
2014 first_matching_driver(devclass_t dc, device_t dev)
2017 return (devclass_find_driver_internal(dc, dev->devclass->name));
2018 return (TAILQ_FIRST(&dc->drivers));
2025 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2027 if (dev->devclass) {
2029 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2030 if (!strcmp(dev->devclass->name, dl->driver->name))
2034 return (TAILQ_NEXT(last, link));
2041 device_probe_child(device_t dev, device_t child)
2044 driverlink_t best = NULL;
2046 int result, pri = 0;
2047 int hasclass = (child->devclass != NULL);
2053 panic("device_probe_child: parent device has no devclass");
2056 * If the state is already probed, then return. However, don't
2057 * return if we can rebid this object.
2059 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2062 for (; dc; dc = dc->parent) {
2063 for (dl = first_matching_driver(dc, child);
2065 dl = next_matching_driver(dc, child, dl)) {
2066 /* If this driver's pass is too high, then ignore it. */
2067 if (dl->pass > bus_current_pass)
2070 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2071 result = device_set_driver(child, dl->driver);
2072 if (result == ENOMEM)
2074 else if (result != 0)
2077 if (device_set_devclass(child,
2078 dl->driver->name) != 0) {
2079 char const * devname =
2080 device_get_name(child);
2081 if (devname == NULL)
2082 devname = "(unknown)";
2083 printf("driver bug: Unable to set "
2084 "devclass (class: %s "
2088 (void)device_set_driver(child, NULL);
2093 /* Fetch any flags for the device before probing. */
2094 resource_int_value(dl->driver->name, child->unit,
2095 "flags", &child->devflags);
2097 result = DEVICE_PROBE(child);
2099 /* Reset flags and devclass before the next probe. */
2100 child->devflags = 0;
2102 (void)device_set_devclass(child, NULL);
2105 * If the driver returns SUCCESS, there can be
2106 * no higher match for this device.
2115 * The driver returned an error so it
2116 * certainly doesn't match.
2119 (void)device_set_driver(child, NULL);
2124 * A priority lower than SUCCESS, remember the
2125 * best matching driver. Initialise the value
2126 * of pri for the first match.
2128 if (best == NULL || result > pri) {
2130 * Probes that return BUS_PROBE_NOWILDCARD
2131 * or lower only match on devices whose
2132 * driver was explicitly specified.
2134 if (result <= BUS_PROBE_NOWILDCARD &&
2135 !(child->flags & DF_FIXEDCLASS))
2143 * If we have an unambiguous match in this devclass,
2144 * don't look in the parent.
2146 if (best && pri == 0)
2151 * If we found a driver, change state and initialise the devclass.
2153 /* XXX What happens if we rebid and got no best? */
2156 * If this device was attached, and we were asked to
2157 * rescan, and it is a different driver, then we have
2158 * to detach the old driver and reattach this new one.
2159 * Note, we don't have to check for DF_REBID here
2160 * because if the state is > DS_ALIVE, we know it must
2163 * This assumes that all DF_REBID drivers can have
2164 * their probe routine called at any time and that
2165 * they are idempotent as well as completely benign in
2166 * normal operations.
2168 * We also have to make sure that the detach
2169 * succeeded, otherwise we fail the operation (or
2170 * maybe it should just fail silently? I'm torn).
2172 if (child->state > DS_ALIVE && best->driver != child->driver)
2173 if ((result = device_detach(dev)) != 0)
2176 /* Set the winning driver, devclass, and flags. */
2177 if (!child->devclass) {
2178 result = device_set_devclass(child, best->driver->name);
2182 result = device_set_driver(child, best->driver);
2185 resource_int_value(best->driver->name, child->unit,
2186 "flags", &child->devflags);
2190 * A bit bogus. Call the probe method again to make
2191 * sure that we have the right description.
2193 DEVICE_PROBE(child);
2195 child->flags |= DF_REBID;
2198 child->flags &= ~DF_REBID;
2199 child->state = DS_ALIVE;
2201 bus_data_generation_update();
2209 * @brief Return the parent of a device
2212 device_get_parent(device_t dev)
2214 return (dev->parent);
2218 * @brief Get a list of children of a device
2220 * An array containing a list of all the children of the given device
2221 * is allocated and returned in @p *devlistp. The number of devices
2222 * in the array is returned in @p *devcountp. The caller should free
2223 * the array using @c free(p, M_TEMP).
2225 * @param dev the device to examine
2226 * @param devlistp points at location for array pointer return
2228 * @param devcountp points at location for array size return value
2231 * @retval ENOMEM the array allocation failed
2234 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2241 TAILQ_FOREACH(child, &dev->children, link) {
2250 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2255 TAILQ_FOREACH(child, &dev->children, link) {
2256 list[count] = child;
2267 * @brief Return the current driver for the device or @c NULL if there
2268 * is no driver currently attached
2271 device_get_driver(device_t dev)
2273 return (dev->driver);
2277 * @brief Return the current devclass for the device or @c NULL if
2281 device_get_devclass(device_t dev)
2283 return (dev->devclass);
2287 * @brief Return the name of the device's devclass or @c NULL if there
2291 device_get_name(device_t dev)
2293 if (dev != NULL && dev->devclass)
2294 return (devclass_get_name(dev->devclass));
2299 * @brief Return a string containing the device's devclass name
2300 * followed by an ascii representation of the device's unit number
2304 device_get_nameunit(device_t dev)
2306 return (dev->nameunit);
2310 * @brief Return the device's unit number.
2313 device_get_unit(device_t dev)
2319 * @brief Return the device's description string
2322 device_get_desc(device_t dev)
2328 * @brief Return the device's flags
2331 device_get_flags(device_t dev)
2333 return (dev->devflags);
2336 struct sysctl_ctx_list *
2337 device_get_sysctl_ctx(device_t dev)
2339 return (&dev->sysctl_ctx);
2343 device_get_sysctl_tree(device_t dev)
2345 return (dev->sysctl_tree);
2349 * @brief Print the name of the device followed by a colon and a space
2351 * @returns the number of characters printed
2354 device_print_prettyname(device_t dev)
2356 const char *name = device_get_name(dev);
2359 return (printf("unknown: "));
2360 return (printf("%s%d: ", name, device_get_unit(dev)));
2364 * @brief Print the name of the device followed by a colon, a space
2365 * and the result of calling vprintf() with the value of @p fmt and
2366 * the following arguments.
2368 * @returns the number of characters printed
2371 device_printf(device_t dev, const char * fmt, ...)
2376 retval = device_print_prettyname(dev);
2378 retval += vprintf(fmt, ap);
2387 device_set_desc_internal(device_t dev, const char* desc, int copy)
2389 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2390 free(dev->desc, M_BUS);
2391 dev->flags &= ~DF_DESCMALLOCED;
2396 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2398 strcpy(dev->desc, desc);
2399 dev->flags |= DF_DESCMALLOCED;
2402 /* Avoid a -Wcast-qual warning */
2403 dev->desc = (char *)(uintptr_t) desc;
2406 bus_data_generation_update();
2410 * @brief Set the device's description
2412 * The value of @c desc should be a string constant that will not
2413 * change (at least until the description is changed in a subsequent
2414 * call to device_set_desc() or device_set_desc_copy()).
2417 device_set_desc(device_t dev, const char* desc)
2419 device_set_desc_internal(dev, desc, FALSE);
2423 * @brief Set the device's description
2425 * The string pointed to by @c desc is copied. Use this function if
2426 * the device description is generated, (e.g. with sprintf()).
2429 device_set_desc_copy(device_t dev, const char* desc)
2431 device_set_desc_internal(dev, desc, TRUE);
2435 * @brief Set the device's flags
2438 device_set_flags(device_t dev, uint32_t flags)
2440 dev->devflags = flags;
2444 * @brief Return the device's softc field
2446 * The softc is allocated and zeroed when a driver is attached, based
2447 * on the size field of the driver.
2450 device_get_softc(device_t dev)
2452 return (dev->softc);
2456 * @brief Set the device's softc field
2458 * Most drivers do not need to use this since the softc is allocated
2459 * automatically when the driver is attached.
2462 device_set_softc(device_t dev, void *softc)
2464 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2465 free(dev->softc, M_BUS_SC);
2468 dev->flags |= DF_EXTERNALSOFTC;
2470 dev->flags &= ~DF_EXTERNALSOFTC;
2474 * @brief Free claimed softc
2476 * Most drivers do not need to use this since the softc is freed
2477 * automatically when the driver is detached.
2480 device_free_softc(void *softc)
2482 free(softc, M_BUS_SC);
2486 * @brief Claim softc
2488 * This function can be used to let the driver free the automatically
2489 * allocated softc using "device_free_softc()". This function is
2490 * useful when the driver is refcounting the softc and the softc
2491 * cannot be freed when the "device_detach" method is called.
2494 device_claim_softc(device_t dev)
2497 dev->flags |= DF_EXTERNALSOFTC;
2499 dev->flags &= ~DF_EXTERNALSOFTC;
2503 * @brief Get the device's ivars field
2505 * The ivars field is used by the parent device to store per-device
2506 * state (e.g. the physical location of the device or a list of
2510 device_get_ivars(device_t dev)
2513 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2514 return (dev->ivars);
2518 * @brief Set the device's ivars field
2521 device_set_ivars(device_t dev, void * ivars)
2524 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2529 * @brief Return the device's state
2532 device_get_state(device_t dev)
2534 return (dev->state);
2538 * @brief Set the DF_ENABLED flag for the device
2541 device_enable(device_t dev)
2543 dev->flags |= DF_ENABLED;
2547 * @brief Clear the DF_ENABLED flag for the device
2550 device_disable(device_t dev)
2552 dev->flags &= ~DF_ENABLED;
2556 * @brief Increment the busy counter for the device
2559 device_busy(device_t dev)
2561 if (dev->state < DS_ATTACHING)
2562 panic("device_busy: called for unattached device");
2563 if (dev->busy == 0 && dev->parent)
2564 device_busy(dev->parent);
2566 if (dev->state == DS_ATTACHED)
2567 dev->state = DS_BUSY;
2571 * @brief Decrement the busy counter for the device
2574 device_unbusy(device_t dev)
2576 if (dev->busy != 0 && dev->state != DS_BUSY &&
2577 dev->state != DS_ATTACHING)
2578 panic("device_unbusy: called for non-busy device %s",
2579 device_get_nameunit(dev));
2581 if (dev->busy == 0) {
2583 device_unbusy(dev->parent);
2584 if (dev->state == DS_BUSY)
2585 dev->state = DS_ATTACHED;
2590 * @brief Set the DF_QUIET flag for the device
2593 device_quiet(device_t dev)
2595 dev->flags |= DF_QUIET;
2599 * @brief Clear the DF_QUIET flag for the device
2602 device_verbose(device_t dev)
2604 dev->flags &= ~DF_QUIET;
2608 * @brief Return non-zero if the DF_QUIET flag is set on the device
2611 device_is_quiet(device_t dev)
2613 return ((dev->flags & DF_QUIET) != 0);
2617 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2620 device_is_enabled(device_t dev)
2622 return ((dev->flags & DF_ENABLED) != 0);
2626 * @brief Return non-zero if the device was successfully probed
2629 device_is_alive(device_t dev)
2631 return (dev->state >= DS_ALIVE);
2635 * @brief Return non-zero if the device currently has a driver
2639 device_is_attached(device_t dev)
2641 return (dev->state >= DS_ATTACHED);
2645 * @brief Set the devclass of a device
2646 * @see devclass_add_device().
2649 device_set_devclass(device_t dev, const char *classname)
2656 devclass_delete_device(dev->devclass, dev);
2660 if (dev->devclass) {
2661 printf("device_set_devclass: device class already set\n");
2665 dc = devclass_find_internal(classname, NULL, TRUE);
2669 error = devclass_add_device(dc, dev);
2671 bus_data_generation_update();
2676 * @brief Set the driver of a device
2679 * @retval EBUSY the device already has a driver attached
2680 * @retval ENOMEM a memory allocation failure occurred
2683 device_set_driver(device_t dev, driver_t *driver)
2685 if (dev->state >= DS_ATTACHED)
2688 if (dev->driver == driver)
2691 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2692 free(dev->softc, M_BUS_SC);
2695 device_set_desc(dev, NULL);
2696 kobj_delete((kobj_t) dev, NULL);
2697 dev->driver = driver;
2699 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2700 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2701 dev->softc = malloc(driver->size, M_BUS_SC,
2704 kobj_delete((kobj_t) dev, NULL);
2705 kobj_init((kobj_t) dev, &null_class);
2711 kobj_init((kobj_t) dev, &null_class);
2714 bus_data_generation_update();
2719 * @brief Probe a device, and return this status.
2721 * This function is the core of the device autoconfiguration
2722 * system. Its purpose is to select a suitable driver for a device and
2723 * then call that driver to initialise the hardware appropriately. The
2724 * driver is selected by calling the DEVICE_PROBE() method of a set of
2725 * candidate drivers and then choosing the driver which returned the
2726 * best value. This driver is then attached to the device using
2729 * The set of suitable drivers is taken from the list of drivers in
2730 * the parent device's devclass. If the device was originally created
2731 * with a specific class name (see device_add_child()), only drivers
2732 * with that name are probed, otherwise all drivers in the devclass
2733 * are probed. If no drivers return successful probe values in the
2734 * parent devclass, the search continues in the parent of that
2735 * devclass (see devclass_get_parent()) if any.
2737 * @param dev the device to initialise
2740 * @retval ENXIO no driver was found
2741 * @retval ENOMEM memory allocation failure
2742 * @retval non-zero some other unix error code
2743 * @retval -1 Device already attached
2746 device_probe(device_t dev)
2752 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2755 if (!(dev->flags & DF_ENABLED)) {
2756 if (bootverbose && device_get_name(dev) != NULL) {
2757 device_print_prettyname(dev);
2758 printf("not probed (disabled)\n");
2762 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2763 if (bus_current_pass == BUS_PASS_DEFAULT &&
2764 !(dev->flags & DF_DONENOMATCH)) {
2765 BUS_PROBE_NOMATCH(dev->parent, dev);
2767 dev->flags |= DF_DONENOMATCH;
2775 * @brief Probe a device and attach a driver if possible
2777 * calls device_probe() and attaches if that was successful.
2780 device_probe_and_attach(device_t dev)
2786 error = device_probe(dev);
2789 else if (error != 0)
2792 CURVNET_SET_QUIET(vnet0);
2793 error = device_attach(dev);
2799 * @brief Attach a device driver to a device
2801 * This function is a wrapper around the DEVICE_ATTACH() driver
2802 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2803 * device's sysctl tree, optionally prints a description of the device
2804 * and queues a notification event for user-based device management
2807 * Normally this function is only called internally from
2808 * device_probe_and_attach().
2810 * @param dev the device to initialise
2813 * @retval ENXIO no driver was found
2814 * @retval ENOMEM memory allocation failure
2815 * @retval non-zero some other unix error code
2818 device_attach(device_t dev)
2820 uint64_t attachtime;
2823 if (resource_disabled(dev->driver->name, dev->unit)) {
2824 device_disable(dev);
2826 device_printf(dev, "disabled via hints entry\n");
2830 device_sysctl_init(dev);
2831 if (!device_is_quiet(dev))
2832 device_print_child(dev->parent, dev);
2833 attachtime = get_cyclecount();
2834 dev->state = DS_ATTACHING;
2835 if ((error = DEVICE_ATTACH(dev)) != 0) {
2836 printf("device_attach: %s%d attach returned %d\n",
2837 dev->driver->name, dev->unit, error);
2838 if (!(dev->flags & DF_FIXEDCLASS))
2839 devclass_delete_device(dev->devclass, dev);
2840 (void)device_set_driver(dev, NULL);
2841 device_sysctl_fini(dev);
2842 KASSERT(dev->busy == 0, ("attach failed but busy"));
2843 dev->state = DS_NOTPRESENT;
2846 attachtime = get_cyclecount() - attachtime;
2848 * 4 bits per device is a reasonable value for desktop and server
2849 * hardware with good get_cyclecount() implementations, but may
2850 * need to be adjusted on other platforms.
2853 printf("%s(): feeding %d bit(s) of entropy from %s%d\n",
2854 __func__, 4, dev->driver->name, dev->unit);
2856 random_harvest(&attachtime, sizeof(attachtime), 4, RANDOM_ATTACH);
2857 device_sysctl_update(dev);
2859 dev->state = DS_BUSY;
2861 dev->state = DS_ATTACHED;
2862 dev->flags &= ~DF_DONENOMATCH;
2868 * @brief Detach a driver from a device
2870 * This function is a wrapper around the DEVICE_DETACH() driver
2871 * method. If the call to DEVICE_DETACH() succeeds, it calls
2872 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2873 * notification event for user-based device management services and
2874 * cleans up the device's sysctl tree.
2876 * @param dev the device to un-initialise
2879 * @retval ENXIO no driver was found
2880 * @retval ENOMEM memory allocation failure
2881 * @retval non-zero some other unix error code
2884 device_detach(device_t dev)
2890 PDEBUG(("%s", DEVICENAME(dev)));
2891 if (dev->state == DS_BUSY)
2893 if (dev->state != DS_ATTACHED)
2896 if ((error = DEVICE_DETACH(dev)) != 0)
2899 if (!device_is_quiet(dev))
2900 device_printf(dev, "detached\n");
2902 BUS_CHILD_DETACHED(dev->parent, dev);
2904 if (!(dev->flags & DF_FIXEDCLASS))
2905 devclass_delete_device(dev->devclass, dev);
2907 dev->state = DS_NOTPRESENT;
2908 (void)device_set_driver(dev, NULL);
2909 device_sysctl_fini(dev);
2915 * @brief Tells a driver to quiesce itself.
2917 * This function is a wrapper around the DEVICE_QUIESCE() driver
2918 * method. If the call to DEVICE_QUIESCE() succeeds.
2920 * @param dev the device to quiesce
2923 * @retval ENXIO no driver was found
2924 * @retval ENOMEM memory allocation failure
2925 * @retval non-zero some other unix error code
2928 device_quiesce(device_t dev)
2931 PDEBUG(("%s", DEVICENAME(dev)));
2932 if (dev->state == DS_BUSY)
2934 if (dev->state != DS_ATTACHED)
2937 return (DEVICE_QUIESCE(dev));
2941 * @brief Notify a device of system shutdown
2943 * This function calls the DEVICE_SHUTDOWN() driver method if the
2944 * device currently has an attached driver.
2946 * @returns the value returned by DEVICE_SHUTDOWN()
2949 device_shutdown(device_t dev)
2951 if (dev->state < DS_ATTACHED)
2953 return (DEVICE_SHUTDOWN(dev));
2957 * @brief Set the unit number of a device
2959 * This function can be used to override the unit number used for a
2960 * device (e.g. to wire a device to a pre-configured unit number).
2963 device_set_unit(device_t dev, int unit)
2968 dc = device_get_devclass(dev);
2969 if (unit < dc->maxunit && dc->devices[unit])
2971 err = devclass_delete_device(dc, dev);
2975 err = devclass_add_device(dc, dev);
2979 bus_data_generation_update();
2983 /*======================================*/
2985 * Some useful method implementations to make life easier for bus drivers.
2989 * @brief Initialise a resource list.
2991 * @param rl the resource list to initialise
2994 resource_list_init(struct resource_list *rl)
3000 * @brief Reclaim memory used by a resource list.
3002 * This function frees the memory for all resource entries on the list
3005 * @param rl the resource list to free
3008 resource_list_free(struct resource_list *rl)
3010 struct resource_list_entry *rle;
3012 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3014 panic("resource_list_free: resource entry is busy");
3015 STAILQ_REMOVE_HEAD(rl, link);
3021 * @brief Add a resource entry.
3023 * This function adds a resource entry using the given @p type, @p
3024 * start, @p end and @p count values. A rid value is chosen by
3025 * searching sequentially for the first unused rid starting at zero.
3027 * @param rl the resource list to edit
3028 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3029 * @param start the start address of the resource
3030 * @param end the end address of the resource
3031 * @param count XXX end-start+1
3034 resource_list_add_next(struct resource_list *rl, int type, u_long start,
3035 u_long end, u_long count)
3040 while (resource_list_find(rl, type, rid) != NULL)
3042 resource_list_add(rl, type, rid, start, end, count);
3047 * @brief Add or modify a resource entry.
3049 * If an existing entry exists with the same type and rid, it will be
3050 * modified using the given values of @p start, @p end and @p
3051 * count. If no entry exists, a new one will be created using the
3052 * given values. The resource list entry that matches is then returned.
3054 * @param rl the resource list to edit
3055 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3056 * @param rid the resource identifier
3057 * @param start the start address of the resource
3058 * @param end the end address of the resource
3059 * @param count XXX end-start+1
3061 struct resource_list_entry *
3062 resource_list_add(struct resource_list *rl, int type, int rid,
3063 u_long start, u_long end, u_long count)
3065 struct resource_list_entry *rle;
3067 rle = resource_list_find(rl, type, rid);
3069 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3072 panic("resource_list_add: can't record entry");
3073 STAILQ_INSERT_TAIL(rl, rle, link);
3081 panic("resource_list_add: resource entry is busy");
3090 * @brief Determine if a resource entry is busy.
3092 * Returns true if a resource entry is busy meaning that it has an
3093 * associated resource that is not an unallocated "reserved" resource.
3095 * @param rl the resource list to search
3096 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3097 * @param rid the resource identifier
3099 * @returns Non-zero if the entry is busy, zero otherwise.
3102 resource_list_busy(struct resource_list *rl, int type, int rid)
3104 struct resource_list_entry *rle;
3106 rle = resource_list_find(rl, type, rid);
3107 if (rle == NULL || rle->res == NULL)
3109 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3110 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3111 ("reserved resource is active"));
3118 * @brief Determine if a resource entry is reserved.
3120 * Returns true if a resource entry is reserved meaning that it has an
3121 * associated "reserved" resource. The resource can either be
3122 * allocated or unallocated.
3124 * @param rl the resource list to search
3125 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3126 * @param rid the resource identifier
3128 * @returns Non-zero if the entry is reserved, zero otherwise.
3131 resource_list_reserved(struct resource_list *rl, int type, int rid)
3133 struct resource_list_entry *rle;
3135 rle = resource_list_find(rl, type, rid);
3136 if (rle != NULL && rle->flags & RLE_RESERVED)
3142 * @brief Find a resource entry by type and rid.
3144 * @param rl the resource list to search
3145 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3146 * @param rid the resource identifier
3148 * @returns the resource entry pointer or NULL if there is no such
3151 struct resource_list_entry *
3152 resource_list_find(struct resource_list *rl, int type, int rid)
3154 struct resource_list_entry *rle;
3156 STAILQ_FOREACH(rle, rl, link) {
3157 if (rle->type == type && rle->rid == rid)
3164 * @brief Delete a resource entry.
3166 * @param rl the resource list to edit
3167 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3168 * @param rid the resource identifier
3171 resource_list_delete(struct resource_list *rl, int type, int rid)
3173 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3176 if (rle->res != NULL)
3177 panic("resource_list_delete: resource has not been released");
3178 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3184 * @brief Allocate a reserved resource
3186 * This can be used by busses to force the allocation of resources
3187 * that are always active in the system even if they are not allocated
3188 * by a driver (e.g. PCI BARs). This function is usually called when
3189 * adding a new child to the bus. The resource is allocated from the
3190 * parent bus when it is reserved. The resource list entry is marked
3191 * with RLE_RESERVED to note that it is a reserved resource.
3193 * Subsequent attempts to allocate the resource with
3194 * resource_list_alloc() will succeed the first time and will set
3195 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3196 * resource that has been allocated is released with
3197 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3198 * the actual resource remains allocated. The resource can be released to
3199 * the parent bus by calling resource_list_unreserve().
3201 * @param rl the resource list to allocate from
3202 * @param bus the parent device of @p child
3203 * @param child the device for which the resource is being reserved
3204 * @param type the type of resource to allocate
3205 * @param rid a pointer to the resource identifier
3206 * @param start hint at the start of the resource range - pass
3207 * @c 0UL for any start address
3208 * @param end hint at the end of the resource range - pass
3209 * @c ~0UL for any end address
3210 * @param count hint at the size of range required - pass @c 1
3212 * @param flags any extra flags to control the resource
3213 * allocation - see @c RF_XXX flags in
3214 * <sys/rman.h> for details
3216 * @returns the resource which was allocated or @c NULL if no
3217 * resource could be allocated
3220 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3221 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3223 struct resource_list_entry *rle = NULL;
3224 int passthrough = (device_get_parent(child) != bus);
3229 "resource_list_reserve() should only be called for direct children");
3230 if (flags & RF_ACTIVE)
3232 "resource_list_reserve() should only reserve inactive resources");
3234 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3237 rle = resource_list_find(rl, type, *rid);
3238 rle->flags |= RLE_RESERVED;
3244 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3246 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3247 * and passing the allocation up to the parent of @p bus. This assumes
3248 * that the first entry of @c device_get_ivars(child) is a struct
3249 * resource_list. This also handles 'passthrough' allocations where a
3250 * child is a remote descendant of bus by passing the allocation up to
3251 * the parent of bus.
3253 * Typically, a bus driver would store a list of child resources
3254 * somewhere in the child device's ivars (see device_get_ivars()) and
3255 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3256 * then call resource_list_alloc() to perform the allocation.
3258 * @param rl the resource list to allocate from
3259 * @param bus the parent device of @p child
3260 * @param child the device which is requesting an allocation
3261 * @param type the type of resource to allocate
3262 * @param rid a pointer to the resource identifier
3263 * @param start hint at the start of the resource range - pass
3264 * @c 0UL for any start address
3265 * @param end hint at the end of the resource range - pass
3266 * @c ~0UL for any end address
3267 * @param count hint at the size of range required - pass @c 1
3269 * @param flags any extra flags to control the resource
3270 * allocation - see @c RF_XXX flags in
3271 * <sys/rman.h> for details
3273 * @returns the resource which was allocated or @c NULL if no
3274 * resource could be allocated
3277 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3278 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3280 struct resource_list_entry *rle = NULL;
3281 int passthrough = (device_get_parent(child) != bus);
3282 int isdefault = (start == 0UL && end == ~0UL);
3285 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3286 type, rid, start, end, count, flags));
3289 rle = resource_list_find(rl, type, *rid);
3292 return (NULL); /* no resource of that type/rid */
3295 if (rle->flags & RLE_RESERVED) {
3296 if (rle->flags & RLE_ALLOCATED)
3298 if ((flags & RF_ACTIVE) &&
3299 bus_activate_resource(child, type, *rid,
3302 rle->flags |= RLE_ALLOCATED;
3306 "resource entry %#x type %d for child %s is busy\n", *rid,
3307 type, device_get_nameunit(child));
3313 count = ulmax(count, rle->count);
3314 end = ulmax(rle->end, start + count - 1);
3317 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3318 type, rid, start, end, count, flags);
3321 * Record the new range.
3324 rle->start = rman_get_start(rle->res);
3325 rle->end = rman_get_end(rle->res);
3333 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3335 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3336 * used with resource_list_alloc().
3338 * @param rl the resource list which was allocated from
3339 * @param bus the parent device of @p child
3340 * @param child the device which is requesting a release
3341 * @param type the type of resource to release
3342 * @param rid the resource identifier
3343 * @param res the resource to release
3346 * @retval non-zero a standard unix error code indicating what
3347 * error condition prevented the operation
3350 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3351 int type, int rid, struct resource *res)
3353 struct resource_list_entry *rle = NULL;
3354 int passthrough = (device_get_parent(child) != bus);
3358 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3362 rle = resource_list_find(rl, type, rid);
3365 panic("resource_list_release: can't find resource");
3367 panic("resource_list_release: resource entry is not busy");
3368 if (rle->flags & RLE_RESERVED) {
3369 if (rle->flags & RLE_ALLOCATED) {
3370 if (rman_get_flags(res) & RF_ACTIVE) {
3371 error = bus_deactivate_resource(child, type,
3376 rle->flags &= ~RLE_ALLOCATED;
3382 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3392 * @brief Release all active resources of a given type
3394 * Release all active resources of a specified type. This is intended
3395 * to be used to cleanup resources leaked by a driver after detach or
3398 * @param rl the resource list which was allocated from
3399 * @param bus the parent device of @p child
3400 * @param child the device whose active resources are being released
3401 * @param type the type of resources to release
3404 * @retval EBUSY at least one resource was active
3407 resource_list_release_active(struct resource_list *rl, device_t bus,
3408 device_t child, int type)
3410 struct resource_list_entry *rle;
3414 STAILQ_FOREACH(rle, rl, link) {
3415 if (rle->type != type)
3417 if (rle->res == NULL)
3419 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3423 error = resource_list_release(rl, bus, child, type,
3424 rman_get_rid(rle->res), rle->res);
3427 "Failed to release active resource: %d\n", error);
3434 * @brief Fully release a reserved resource
3436 * Fully releases a resource reserved via resource_list_reserve().
3438 * @param rl the resource list which was allocated from
3439 * @param bus the parent device of @p child
3440 * @param child the device whose reserved resource is being released
3441 * @param type the type of resource to release
3442 * @param rid the resource identifier
3443 * @param res the resource to release
3446 * @retval non-zero a standard unix error code indicating what
3447 * error condition prevented the operation
3450 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3453 struct resource_list_entry *rle = NULL;
3454 int passthrough = (device_get_parent(child) != bus);
3458 "resource_list_unreserve() should only be called for direct children");
3460 rle = resource_list_find(rl, type, rid);
3463 panic("resource_list_unreserve: can't find resource");
3464 if (!(rle->flags & RLE_RESERVED))
3466 if (rle->flags & RLE_ALLOCATED)
3468 rle->flags &= ~RLE_RESERVED;
3469 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3473 * @brief Print a description of resources in a resource list
3475 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3476 * The name is printed if at least one resource of the given type is available.
3477 * The format is used to print resource start and end.
3479 * @param rl the resource list to print
3480 * @param name the name of @p type, e.g. @c "memory"
3481 * @param type type type of resource entry to print
3482 * @param format printf(9) format string to print resource
3483 * start and end values
3485 * @returns the number of characters printed
3488 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3491 struct resource_list_entry *rle;
3492 int printed, retval;
3496 /* Yes, this is kinda cheating */
3497 STAILQ_FOREACH(rle, rl, link) {
3498 if (rle->type == type) {
3500 retval += printf(" %s ", name);
3502 retval += printf(",");
3504 retval += printf(format, rle->start);
3505 if (rle->count > 1) {
3506 retval += printf("-");
3507 retval += printf(format, rle->start +
3516 * @brief Releases all the resources in a list.
3518 * @param rl The resource list to purge.
3523 resource_list_purge(struct resource_list *rl)
3525 struct resource_list_entry *rle;
3527 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3529 bus_release_resource(rman_get_device(rle->res),
3530 rle->type, rle->rid, rle->res);
3531 STAILQ_REMOVE_HEAD(rl, link);
3537 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3540 return (device_add_child_ordered(dev, order, name, unit));
3544 * @brief Helper function for implementing DEVICE_PROBE()
3546 * This function can be used to help implement the DEVICE_PROBE() for
3547 * a bus (i.e. a device which has other devices attached to it). It
3548 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3552 bus_generic_probe(device_t dev)
3554 devclass_t dc = dev->devclass;
3557 TAILQ_FOREACH(dl, &dc->drivers, link) {
3559 * If this driver's pass is too high, then ignore it.
3560 * For most drivers in the default pass, this will
3561 * never be true. For early-pass drivers they will
3562 * only call the identify routines of eligible drivers
3563 * when this routine is called. Drivers for later
3564 * passes should have their identify routines called
3565 * on early-pass busses during BUS_NEW_PASS().
3567 if (dl->pass > bus_current_pass)
3569 DEVICE_IDENTIFY(dl->driver, dev);
3576 * @brief Helper function for implementing DEVICE_ATTACH()
3578 * This function can be used to help implement the DEVICE_ATTACH() for
3579 * a bus. It calls device_probe_and_attach() for each of the device's
3583 bus_generic_attach(device_t dev)
3587 TAILQ_FOREACH(child, &dev->children, link) {
3588 device_probe_and_attach(child);
3595 * @brief Helper function for implementing DEVICE_DETACH()
3597 * This function can be used to help implement the DEVICE_DETACH() for
3598 * a bus. It calls device_detach() for each of the device's
3602 bus_generic_detach(device_t dev)
3607 if (dev->state != DS_ATTACHED)
3610 TAILQ_FOREACH(child, &dev->children, link) {
3611 if ((error = device_detach(child)) != 0)
3619 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3621 * This function can be used to help implement the DEVICE_SHUTDOWN()
3622 * for a bus. It calls device_shutdown() for each of the device's
3626 bus_generic_shutdown(device_t dev)
3630 TAILQ_FOREACH(child, &dev->children, link) {
3631 device_shutdown(child);
3638 * @brief Default function for suspending a child device.
3640 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3643 bus_generic_suspend_child(device_t dev, device_t child)
3647 error = DEVICE_SUSPEND(child);
3650 dev->flags |= DF_SUSPENDED;
3656 * @brief Default function for resuming a child device.
3658 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3661 bus_generic_resume_child(device_t dev, device_t child)
3664 DEVICE_RESUME(child);
3665 dev->flags &= ~DF_SUSPENDED;
3671 * @brief Helper function for implementing DEVICE_SUSPEND()
3673 * This function can be used to help implement the DEVICE_SUSPEND()
3674 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3675 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3676 * operation is aborted and any devices which were suspended are
3677 * resumed immediately by calling their DEVICE_RESUME() methods.
3680 bus_generic_suspend(device_t dev)
3683 device_t child, child2;
3685 TAILQ_FOREACH(child, &dev->children, link) {
3686 error = BUS_SUSPEND_CHILD(dev, child);
3688 for (child2 = TAILQ_FIRST(&dev->children);
3689 child2 && child2 != child;
3690 child2 = TAILQ_NEXT(child2, link))
3691 BUS_RESUME_CHILD(dev, child2);
3699 * @brief Helper function for implementing DEVICE_RESUME()
3701 * This function can be used to help implement the DEVICE_RESUME() for
3702 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3705 bus_generic_resume(device_t dev)
3709 TAILQ_FOREACH(child, &dev->children, link) {
3710 BUS_RESUME_CHILD(dev, child);
3711 /* if resume fails, there's nothing we can usefully do... */
3717 * @brief Helper function for implementing BUS_PRINT_CHILD().
3719 * This function prints the first part of the ascii representation of
3720 * @p child, including its name, unit and description (if any - see
3721 * device_set_desc()).
3723 * @returns the number of characters printed
3726 bus_print_child_header(device_t dev, device_t child)
3730 if (device_get_desc(child)) {
3731 retval += device_printf(child, "<%s>", device_get_desc(child));
3733 retval += printf("%s", device_get_nameunit(child));
3740 * @brief Helper function for implementing BUS_PRINT_CHILD().
3742 * This function prints the last part of the ascii representation of
3743 * @p child, which consists of the string @c " on " followed by the
3744 * name and unit of the @p dev.
3746 * @returns the number of characters printed
3749 bus_print_child_footer(device_t dev, device_t child)
3751 return (printf(" on %s\n", device_get_nameunit(dev)));
3755 * @brief Helper function for implementing BUS_PRINT_CHILD().
3757 * This function simply calls bus_print_child_header() followed by
3758 * bus_print_child_footer().
3760 * @returns the number of characters printed
3763 bus_generic_print_child(device_t dev, device_t child)
3767 retval += bus_print_child_header(dev, child);
3768 retval += bus_print_child_footer(dev, child);
3774 * @brief Stub function for implementing BUS_READ_IVAR().
3779 bus_generic_read_ivar(device_t dev, device_t child, int index,
3786 * @brief Stub function for implementing BUS_WRITE_IVAR().
3791 bus_generic_write_ivar(device_t dev, device_t child, int index,
3798 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3802 struct resource_list *
3803 bus_generic_get_resource_list(device_t dev, device_t child)
3809 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3811 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3812 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3813 * and then calls device_probe_and_attach() for each unattached child.
3816 bus_generic_driver_added(device_t dev, driver_t *driver)
3820 DEVICE_IDENTIFY(driver, dev);
3821 TAILQ_FOREACH(child, &dev->children, link) {
3822 if (child->state == DS_NOTPRESENT ||
3823 (child->flags & DF_REBID))
3824 device_probe_and_attach(child);
3829 * @brief Helper function for implementing BUS_NEW_PASS().
3831 * This implementing of BUS_NEW_PASS() first calls the identify
3832 * routines for any drivers that probe at the current pass. Then it
3833 * walks the list of devices for this bus. If a device is already
3834 * attached, then it calls BUS_NEW_PASS() on that device. If the
3835 * device is not already attached, it attempts to attach a driver to
3839 bus_generic_new_pass(device_t dev)
3846 TAILQ_FOREACH(dl, &dc->drivers, link) {
3847 if (dl->pass == bus_current_pass)
3848 DEVICE_IDENTIFY(dl->driver, dev);
3850 TAILQ_FOREACH(child, &dev->children, link) {
3851 if (child->state >= DS_ATTACHED)
3852 BUS_NEW_PASS(child);
3853 else if (child->state == DS_NOTPRESENT)
3854 device_probe_and_attach(child);
3859 * @brief Helper function for implementing BUS_SETUP_INTR().
3861 * This simple implementation of BUS_SETUP_INTR() simply calls the
3862 * BUS_SETUP_INTR() method of the parent of @p dev.
3865 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3866 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3869 /* Propagate up the bus hierarchy until someone handles it. */
3871 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3872 filter, intr, arg, cookiep));
3877 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3879 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3880 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3883 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3886 /* Propagate up the bus hierarchy until someone handles it. */
3888 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3893 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3895 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3896 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3899 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3900 struct resource *r, u_long start, u_long end)
3902 /* Propagate up the bus hierarchy until someone handles it. */
3904 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3910 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3912 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3913 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3916 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3917 u_long start, u_long end, u_long count, u_int flags)
3919 /* Propagate up the bus hierarchy until someone handles it. */
3921 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3922 start, end, count, flags));
3927 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3929 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3930 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3933 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3936 /* Propagate up the bus hierarchy until someone handles it. */
3938 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3944 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3946 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3947 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3950 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3953 /* Propagate up the bus hierarchy until someone handles it. */
3955 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3961 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3963 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3964 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3967 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3968 int rid, struct resource *r)
3970 /* Propagate up the bus hierarchy until someone handles it. */
3972 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3978 * @brief Helper function for implementing BUS_BIND_INTR().
3980 * This simple implementation of BUS_BIND_INTR() simply calls the
3981 * BUS_BIND_INTR() method of the parent of @p dev.
3984 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3988 /* Propagate up the bus hierarchy until someone handles it. */
3990 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3995 * @brief Helper function for implementing BUS_CONFIG_INTR().
3997 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3998 * BUS_CONFIG_INTR() method of the parent of @p dev.
4001 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4002 enum intr_polarity pol)
4005 /* Propagate up the bus hierarchy until someone handles it. */
4007 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4012 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4014 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4015 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4018 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4019 void *cookie, const char *descr)
4022 /* Propagate up the bus hierarchy until someone handles it. */
4024 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4030 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4032 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4033 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4036 bus_generic_get_dma_tag(device_t dev, device_t child)
4039 /* Propagate up the bus hierarchy until someone handles it. */
4040 if (dev->parent != NULL)
4041 return (BUS_GET_DMA_TAG(dev->parent, child));
4046 * @brief Helper function for implementing BUS_GET_RESOURCE().
4048 * This implementation of BUS_GET_RESOURCE() uses the
4049 * resource_list_find() function to do most of the work. It calls
4050 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4054 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4055 u_long *startp, u_long *countp)
4057 struct resource_list * rl = NULL;
4058 struct resource_list_entry * rle = NULL;
4060 rl = BUS_GET_RESOURCE_LIST(dev, child);
4064 rle = resource_list_find(rl, type, rid);
4069 *startp = rle->start;
4071 *countp = rle->count;
4077 * @brief Helper function for implementing BUS_SET_RESOURCE().
4079 * This implementation of BUS_SET_RESOURCE() uses the
4080 * resource_list_add() function to do most of the work. It calls
4081 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4085 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4086 u_long start, u_long count)
4088 struct resource_list * rl = NULL;
4090 rl = BUS_GET_RESOURCE_LIST(dev, child);
4094 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4100 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4102 * This implementation of BUS_DELETE_RESOURCE() uses the
4103 * resource_list_delete() function to do most of the work. It calls
4104 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4108 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4110 struct resource_list * rl = NULL;
4112 rl = BUS_GET_RESOURCE_LIST(dev, child);
4116 resource_list_delete(rl, type, rid);
4122 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4124 * This implementation of BUS_RELEASE_RESOURCE() uses the
4125 * resource_list_release() function to do most of the work. It calls
4126 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4129 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4130 int rid, struct resource *r)
4132 struct resource_list * rl = NULL;
4134 if (device_get_parent(child) != dev)
4135 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4138 rl = BUS_GET_RESOURCE_LIST(dev, child);
4142 return (resource_list_release(rl, dev, child, type, rid, r));
4146 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4148 * This implementation of BUS_ALLOC_RESOURCE() uses the
4149 * resource_list_alloc() function to do most of the work. It calls
4150 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4153 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4154 int *rid, u_long start, u_long end, u_long count, u_int flags)
4156 struct resource_list * rl = NULL;
4158 if (device_get_parent(child) != dev)
4159 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4160 type, rid, start, end, count, flags));
4162 rl = BUS_GET_RESOURCE_LIST(dev, child);
4166 return (resource_list_alloc(rl, dev, child, type, rid,
4167 start, end, count, flags));
4171 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4173 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4174 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4177 bus_generic_child_present(device_t dev, device_t child)
4179 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4183 * Some convenience functions to make it easier for drivers to use the
4184 * resource-management functions. All these really do is hide the
4185 * indirection through the parent's method table, making for slightly
4186 * less-wordy code. In the future, it might make sense for this code
4187 * to maintain some sort of a list of resources allocated by each device.
4191 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4192 struct resource **res)
4196 for (i = 0; rs[i].type != -1; i++)
4198 for (i = 0; rs[i].type != -1; i++) {
4199 res[i] = bus_alloc_resource_any(dev,
4200 rs[i].type, &rs[i].rid, rs[i].flags);
4201 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4202 bus_release_resources(dev, rs, res);
4210 bus_release_resources(device_t dev, const struct resource_spec *rs,
4211 struct resource **res)
4215 for (i = 0; rs[i].type != -1; i++)
4216 if (res[i] != NULL) {
4217 bus_release_resource(
4218 dev, rs[i].type, rs[i].rid, res[i]);
4224 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4226 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4230 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4231 u_long count, u_int flags)
4233 if (dev->parent == NULL)
4235 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4240 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4242 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4246 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4249 if (dev->parent == NULL)
4251 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4255 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4257 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4261 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4263 if (dev->parent == NULL)
4265 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4269 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4271 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4275 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4277 if (dev->parent == NULL)
4279 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4283 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4285 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4289 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4291 if (dev->parent == NULL)
4293 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4297 * @brief Wrapper function for BUS_SETUP_INTR().
4299 * This function simply calls the BUS_SETUP_INTR() method of the
4303 bus_setup_intr(device_t dev, struct resource *r, int flags,
4304 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4308 if (dev->parent == NULL)
4310 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4314 if (handler != NULL && !(flags & INTR_MPSAFE))
4315 device_printf(dev, "[GIANT-LOCKED]\n");
4320 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4322 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4326 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4328 if (dev->parent == NULL)
4330 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4334 * @brief Wrapper function for BUS_BIND_INTR().
4336 * This function simply calls the BUS_BIND_INTR() method of the
4340 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4342 if (dev->parent == NULL)
4344 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4348 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4350 * This function first formats the requested description into a
4351 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4352 * the parent of @p dev.
4355 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4356 const char *fmt, ...)
4359 char descr[MAXCOMLEN + 1];
4361 if (dev->parent == NULL)
4364 vsnprintf(descr, sizeof(descr), fmt, ap);
4366 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4370 * @brief Wrapper function for BUS_SET_RESOURCE().
4372 * This function simply calls the BUS_SET_RESOURCE() method of the
4376 bus_set_resource(device_t dev, int type, int rid,
4377 u_long start, u_long count)
4379 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4384 * @brief Wrapper function for BUS_GET_RESOURCE().
4386 * This function simply calls the BUS_GET_RESOURCE() method of the
4390 bus_get_resource(device_t dev, int type, int rid,
4391 u_long *startp, u_long *countp)
4393 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4398 * @brief Wrapper function for BUS_GET_RESOURCE().
4400 * This function simply calls the BUS_GET_RESOURCE() method of the
4401 * parent of @p dev and returns the start value.
4404 bus_get_resource_start(device_t dev, int type, int rid)
4406 u_long start, count;
4409 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4417 * @brief Wrapper function for BUS_GET_RESOURCE().
4419 * This function simply calls the BUS_GET_RESOURCE() method of the
4420 * parent of @p dev and returns the count value.
4423 bus_get_resource_count(device_t dev, int type, int rid)
4425 u_long start, count;
4428 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4436 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4438 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4442 bus_delete_resource(device_t dev, int type, int rid)
4444 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4448 * @brief Wrapper function for BUS_CHILD_PRESENT().
4450 * This function simply calls the BUS_CHILD_PRESENT() method of the
4454 bus_child_present(device_t child)
4456 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4460 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4462 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4466 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4470 parent = device_get_parent(child);
4471 if (parent == NULL) {
4475 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4479 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4481 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4485 bus_child_location_str(device_t child, char *buf, size_t buflen)
4489 parent = device_get_parent(child);
4490 if (parent == NULL) {
4494 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4498 * @brief Wrapper function for BUS_GET_DMA_TAG().
4500 * This function simply calls the BUS_GET_DMA_TAG() method of the
4504 bus_get_dma_tag(device_t dev)
4508 parent = device_get_parent(dev);
4511 return (BUS_GET_DMA_TAG(parent, dev));
4514 /* Resume all devices and then notify userland that we're up again. */
4516 root_resume(device_t dev)
4520 error = bus_generic_resume(dev);
4522 devctl_notify("kern", "power", "resume", NULL);
4527 root_print_child(device_t dev, device_t child)
4531 retval += bus_print_child_header(dev, child);
4532 retval += printf("\n");
4538 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4539 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4542 * If an interrupt mapping gets to here something bad has happened.
4544 panic("root_setup_intr");
4548 * If we get here, assume that the device is permanant and really is
4549 * present in the system. Removable bus drivers are expected to intercept
4550 * this call long before it gets here. We return -1 so that drivers that
4551 * really care can check vs -1 or some ERRNO returned higher in the food
4555 root_child_present(device_t dev, device_t child)
4560 static kobj_method_t root_methods[] = {
4561 /* Device interface */
4562 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4563 KOBJMETHOD(device_suspend, bus_generic_suspend),
4564 KOBJMETHOD(device_resume, root_resume),
4567 KOBJMETHOD(bus_print_child, root_print_child),
4568 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4569 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4570 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4571 KOBJMETHOD(bus_child_present, root_child_present),
4576 static driver_t root_driver = {
4583 devclass_t root_devclass;
4586 root_bus_module_handler(module_t mod, int what, void* arg)
4590 TAILQ_INIT(&bus_data_devices);
4591 kobj_class_compile((kobj_class_t) &root_driver);
4592 root_bus = make_device(NULL, "root", 0);
4593 root_bus->desc = "System root bus";
4594 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4595 root_bus->driver = &root_driver;
4596 root_bus->state = DS_ATTACHED;
4597 root_devclass = devclass_find_internal("root", NULL, FALSE);
4602 device_shutdown(root_bus);
4605 return (EOPNOTSUPP);
4611 static moduledata_t root_bus_mod = {
4613 root_bus_module_handler,
4616 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4619 * @brief Automatically configure devices
4621 * This function begins the autoconfiguration process by calling
4622 * device_probe_and_attach() for each child of the @c root0 device.
4625 root_bus_configure(void)
4630 /* Eventually this will be split up, but this is sufficient for now. */
4631 bus_set_pass(BUS_PASS_DEFAULT);
4635 * @brief Module handler for registering device drivers
4637 * This module handler is used to automatically register device
4638 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4639 * devclass_add_driver() for the driver described by the
4640 * driver_module_data structure pointed to by @p arg
4643 driver_module_handler(module_t mod, int what, void *arg)
4645 struct driver_module_data *dmd;
4646 devclass_t bus_devclass;
4647 kobj_class_t driver;
4650 dmd = (struct driver_module_data *)arg;
4651 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4656 if (dmd->dmd_chainevh)
4657 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4659 pass = dmd->dmd_pass;
4660 driver = dmd->dmd_driver;
4661 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4662 DRIVERNAME(driver), dmd->dmd_busname, pass));
4663 error = devclass_add_driver(bus_devclass, driver, pass,
4668 PDEBUG(("Unloading module: driver %s from bus %s",
4669 DRIVERNAME(dmd->dmd_driver),
4671 error = devclass_delete_driver(bus_devclass,
4674 if (!error && dmd->dmd_chainevh)
4675 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4678 PDEBUG(("Quiesce module: driver %s from bus %s",
4679 DRIVERNAME(dmd->dmd_driver),
4681 error = devclass_quiesce_driver(bus_devclass,
4684 if (!error && dmd->dmd_chainevh)
4685 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4696 * @brief Enumerate all hinted devices for this bus.
4698 * Walks through the hints for this bus and calls the bus_hinted_child
4699 * routine for each one it fines. It searches first for the specific
4700 * bus that's being probed for hinted children (eg isa0), and then for
4701 * generic children (eg isa).
4703 * @param dev bus device to enumerate
4706 bus_enumerate_hinted_children(device_t bus)
4709 const char *dname, *busname;
4713 * enumerate all devices on the specific bus
4715 busname = device_get_nameunit(bus);
4717 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4718 BUS_HINTED_CHILD(bus, dname, dunit);
4721 * and all the generic ones.
4723 busname = device_get_name(bus);
4725 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4726 BUS_HINTED_CHILD(bus, dname, dunit);
4731 /* the _short versions avoid iteration by not calling anything that prints
4732 * more than oneliners. I love oneliners.
4736 print_device_short(device_t dev, int indent)
4741 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4742 dev->unit, dev->desc,
4743 (dev->parent? "":"no "),
4744 (TAILQ_EMPTY(&dev->children)? "no ":""),
4745 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4746 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4747 (dev->flags&DF_WILDCARD? "wildcard,":""),
4748 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4749 (dev->flags&DF_REBID? "rebiddable,":""),
4750 (dev->ivars? "":"no "),
4751 (dev->softc? "":"no "),
4756 print_device(device_t dev, int indent)
4761 print_device_short(dev, indent);
4763 indentprintf(("Parent:\n"));
4764 print_device_short(dev->parent, indent+1);
4765 indentprintf(("Driver:\n"));
4766 print_driver_short(dev->driver, indent+1);
4767 indentprintf(("Devclass:\n"));
4768 print_devclass_short(dev->devclass, indent+1);
4772 print_device_tree_short(device_t dev, int indent)
4773 /* print the device and all its children (indented) */
4780 print_device_short(dev, indent);
4782 TAILQ_FOREACH(child, &dev->children, link) {
4783 print_device_tree_short(child, indent+1);
4788 print_device_tree(device_t dev, int indent)
4789 /* print the device and all its children (indented) */
4796 print_device(dev, indent);
4798 TAILQ_FOREACH(child, &dev->children, link) {
4799 print_device_tree(child, indent+1);
4804 print_driver_short(driver_t *driver, int indent)
4809 indentprintf(("driver %s: softc size = %zd\n",
4810 driver->name, driver->size));
4814 print_driver(driver_t *driver, int indent)
4819 print_driver_short(driver, indent);
4823 print_driver_list(driver_list_t drivers, int indent)
4825 driverlink_t driver;
4827 TAILQ_FOREACH(driver, &drivers, link) {
4828 print_driver(driver->driver, indent);
4833 print_devclass_short(devclass_t dc, int indent)
4838 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4842 print_devclass(devclass_t dc, int indent)
4849 print_devclass_short(dc, indent);
4850 indentprintf(("Drivers:\n"));
4851 print_driver_list(dc->drivers, indent+1);
4853 indentprintf(("Devices:\n"));
4854 for (i = 0; i < dc->maxunit; i++)
4856 print_device(dc->devices[i], indent+1);
4860 print_devclass_list_short(void)
4864 printf("Short listing of devclasses, drivers & devices:\n");
4865 TAILQ_FOREACH(dc, &devclasses, link) {
4866 print_devclass_short(dc, 0);
4871 print_devclass_list(void)
4875 printf("Full listing of devclasses, drivers & devices:\n");
4876 TAILQ_FOREACH(dc, &devclasses, link) {
4877 print_devclass(dc, 0);
4884 * User-space access to the device tree.
4886 * We implement a small set of nodes:
4888 * hw.bus Single integer read method to obtain the
4889 * current generation count.
4890 * hw.bus.devices Reads the entire device tree in flat space.
4891 * hw.bus.rman Resource manager interface
4893 * We might like to add the ability to scan devclasses and/or drivers to
4894 * determine what else is currently loaded/available.
4898 sysctl_bus(SYSCTL_HANDLER_ARGS)
4900 struct u_businfo ubus;
4902 ubus.ub_version = BUS_USER_VERSION;
4903 ubus.ub_generation = bus_data_generation;
4905 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4907 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4908 "bus-related data");
4911 sysctl_devices(SYSCTL_HANDLER_ARGS)
4913 int *name = (int *)arg1;
4914 u_int namelen = arg2;
4917 struct u_device udev; /* XXX this is a bit big */
4923 if (bus_data_generation_check(name[0]))
4929 * Scan the list of devices, looking for the requested index.
4931 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4939 * Populate the return array.
4941 bzero(&udev, sizeof(udev));
4942 udev.dv_handle = (uintptr_t)dev;
4943 udev.dv_parent = (uintptr_t)dev->parent;
4944 if (dev->nameunit != NULL)
4945 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4946 if (dev->desc != NULL)
4947 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4948 if (dev->driver != NULL && dev->driver->name != NULL)
4949 strlcpy(udev.dv_drivername, dev->driver->name,
4950 sizeof(udev.dv_drivername));
4951 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4952 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4953 udev.dv_devflags = dev->devflags;
4954 udev.dv_flags = dev->flags;
4955 udev.dv_state = dev->state;
4956 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4960 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4961 "system device tree");
4964 bus_data_generation_check(int generation)
4966 if (generation != bus_data_generation)
4969 /* XXX generate optimised lists here? */
4974 bus_data_generation_update(void)
4976 bus_data_generation++;
4980 bus_free_resource(device_t dev, int type, struct resource *r)
4984 return (bus_release_resource(dev, type, rman_get_rid(r), r));