2 * Copyright (c) 1997,1998,2003 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/filio.h>
36 #include <sys/kernel.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #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>
57 #include <sys/cpuset.h>
61 #include <machine/cpu.h>
62 #include <machine/stdarg.h>
66 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
67 SYSCTL_ROOT_NODE(OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
70 * Used to attach drivers to devclasses.
72 typedef struct driverlink *driverlink_t;
75 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
77 TAILQ_ENTRY(driverlink) passlink;
81 * Forward declarations
83 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
84 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
85 typedef TAILQ_HEAD(device_list, device) device_list_t;
88 TAILQ_ENTRY(devclass) link;
89 devclass_t parent; /* parent in devclass hierarchy */
90 driver_list_t drivers; /* bus devclasses store drivers for bus */
92 device_t *devices; /* array of devices indexed by unit */
93 int maxunit; /* size of devices array */
95 #define DC_HAS_CHILDREN 1
97 struct sysctl_ctx_list sysctl_ctx;
98 struct sysctl_oid *sysctl_tree;
102 * @brief Implementation of device.
106 * A device is a kernel object. The first field must be the
107 * current ops table for the object.
114 TAILQ_ENTRY(device) link; /**< list of devices in parent */
115 TAILQ_ENTRY(device) devlink; /**< global device list membership */
116 device_t parent; /**< parent of this device */
117 device_list_t children; /**< list of child devices */
120 * Details of this device.
122 driver_t *driver; /**< current driver */
123 devclass_t devclass; /**< current device class */
124 int unit; /**< current unit number */
125 char* nameunit; /**< name+unit e.g. foodev0 */
126 char* desc; /**< driver specific description */
127 int busy; /**< count of calls to device_busy() */
128 device_state_t state; /**< current device state */
129 uint32_t devflags; /**< api level flags for device_get_flags() */
130 u_int flags; /**< internal device flags */
131 u_int order; /**< order from device_add_child_ordered() */
132 void *ivars; /**< instance variables */
133 void *softc; /**< current driver's variables */
135 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
136 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
139 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
140 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
142 static void devctl2_init(void);
146 static int bus_debug = 1;
147 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RWTUN, &bus_debug, 0,
150 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
151 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
152 #define DRIVERNAME(d) ((d)? d->name : "no driver")
153 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
156 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
157 * prevent syslog from deleting initial spaces
159 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
161 static void print_device_short(device_t dev, int indent);
162 static void print_device(device_t dev, int indent);
163 void print_device_tree_short(device_t dev, int indent);
164 void print_device_tree(device_t dev, int indent);
165 static void print_driver_short(driver_t *driver, int indent);
166 static void print_driver(driver_t *driver, int indent);
167 static void print_driver_list(driver_list_t drivers, int indent);
168 static void print_devclass_short(devclass_t dc, int indent);
169 static void print_devclass(devclass_t dc, int indent);
170 void print_devclass_list_short(void);
171 void print_devclass_list(void);
174 /* Make the compiler ignore the function calls */
175 #define PDEBUG(a) /* nop */
176 #define DEVICENAME(d) /* nop */
177 #define DRIVERNAME(d) /* nop */
178 #define DEVCLANAME(d) /* nop */
180 #define print_device_short(d,i) /* nop */
181 #define print_device(d,i) /* nop */
182 #define print_device_tree_short(d,i) /* nop */
183 #define print_device_tree(d,i) /* nop */
184 #define print_driver_short(d,i) /* nop */
185 #define print_driver(d,i) /* nop */
186 #define print_driver_list(d,i) /* nop */
187 #define print_devclass_short(d,i) /* nop */
188 #define print_devclass(d,i) /* nop */
189 #define print_devclass_list_short() /* nop */
190 #define print_devclass_list() /* nop */
198 DEVCLASS_SYSCTL_PARENT,
202 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
204 devclass_t dc = (devclass_t)arg1;
208 case DEVCLASS_SYSCTL_PARENT:
209 value = dc->parent ? dc->parent->name : "";
214 return (SYSCTL_OUT_STR(req, value));
218 devclass_sysctl_init(devclass_t dc)
221 if (dc->sysctl_tree != NULL)
223 sysctl_ctx_init(&dc->sysctl_ctx);
224 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
225 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
226 CTLFLAG_RD, NULL, "");
227 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
228 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
229 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
235 DEVICE_SYSCTL_DRIVER,
236 DEVICE_SYSCTL_LOCATION,
237 DEVICE_SYSCTL_PNPINFO,
238 DEVICE_SYSCTL_PARENT,
242 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
244 device_t dev = (device_t)arg1;
251 case DEVICE_SYSCTL_DESC:
252 value = dev->desc ? dev->desc : "";
254 case DEVICE_SYSCTL_DRIVER:
255 value = dev->driver ? dev->driver->name : "";
257 case DEVICE_SYSCTL_LOCATION:
258 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
259 bus_child_location_str(dev, buf, 1024);
261 case DEVICE_SYSCTL_PNPINFO:
262 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
263 bus_child_pnpinfo_str(dev, buf, 1024);
265 case DEVICE_SYSCTL_PARENT:
266 value = dev->parent ? dev->parent->nameunit : "";
271 error = SYSCTL_OUT_STR(req, value);
278 device_sysctl_init(device_t dev)
280 devclass_t dc = dev->devclass;
283 if (dev->sysctl_tree != NULL)
285 devclass_sysctl_init(dc);
286 sysctl_ctx_init(&dev->sysctl_ctx);
287 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
288 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
289 dev->nameunit + strlen(dc->name),
290 CTLFLAG_RD, NULL, "");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
294 "device description");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
298 "device driver name");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
302 "device location relative to parent");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
306 "device identification");
307 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
309 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
311 if (bus_get_domain(dev, &domain) == 0)
312 SYSCTL_ADD_INT(&dev->sysctl_ctx,
313 SYSCTL_CHILDREN(dev->sysctl_tree), OID_AUTO, "%domain",
314 CTLFLAG_RD, NULL, domain, "NUMA domain");
318 device_sysctl_update(device_t dev)
320 devclass_t dc = dev->devclass;
322 if (dev->sysctl_tree == NULL)
324 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
328 device_sysctl_fini(device_t dev)
330 if (dev->sysctl_tree == NULL)
332 sysctl_ctx_free(&dev->sysctl_ctx);
333 dev->sysctl_tree = NULL;
337 * /dev/devctl implementation
341 * This design allows only one reader for /dev/devctl. This is not desirable
342 * in the long run, but will get a lot of hair out of this implementation.
343 * Maybe we should make this device a clonable device.
345 * Also note: we specifically do not attach a device to the device_t tree
346 * to avoid potential chicken and egg problems. One could argue that all
347 * of this belongs to the root node. One could also further argue that the
348 * sysctl interface that we have not might more properly be an ioctl
349 * interface, but at this stage of the game, I'm not inclined to rock that
352 * I'm also not sure that the SIGIO support is done correctly or not, as
353 * I copied it from a driver that had SIGIO support that likely hasn't been
354 * tested since 3.4 or 2.2.8!
357 /* Deprecated way to adjust queue length */
358 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
359 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RWTUN |
360 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_disable, "I",
361 "devctl disable -- deprecated");
363 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
364 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
365 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
366 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RWTUN |
367 CTLFLAG_MPSAFE, NULL, 0, sysctl_devctl_queue, "I", "devctl queue length");
369 static d_open_t devopen;
370 static d_close_t devclose;
371 static d_read_t devread;
372 static d_ioctl_t devioctl;
373 static d_poll_t devpoll;
374 static d_kqfilter_t devkqfilter;
376 static struct cdevsw dev_cdevsw = {
377 .d_version = D_VERSION,
383 .d_kqfilter = devkqfilter,
387 struct dev_event_info
390 TAILQ_ENTRY(dev_event_info) dei_link;
393 TAILQ_HEAD(devq, dev_event_info);
395 static struct dev_softc
408 static void filt_devctl_detach(struct knote *kn);
409 static int filt_devctl_read(struct knote *kn, long hint);
411 struct filterops devctl_rfiltops = {
413 .f_detach = filt_devctl_detach,
414 .f_event = filt_devctl_read,
417 static struct cdev *devctl_dev;
422 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
423 UID_ROOT, GID_WHEEL, 0600, "devctl");
424 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
425 cv_init(&devsoftc.cv, "dev cv");
426 TAILQ_INIT(&devsoftc.devq);
427 knlist_init_mtx(&devsoftc.sel.si_note, &devsoftc.mtx);
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);
843 * @brief safely quotes strings that might have double quotes in them.
845 * The devctl protocol relies on quoted strings having matching quotes.
846 * This routine quotes any internal quotes so the resulting string
847 * is safe to pass to snprintf to construct, for example pnp info strings.
848 * Strings are always terminated with a NUL, but may be truncated if longer
849 * than @p len bytes after quotes.
851 * @param dst Buffer to hold the string. Must be at least @p len bytes long
852 * @param src Original buffer.
853 * @param len Length of buffer pointed to by @dst, including trailing NUL
856 devctl_safe_quote(char *dst, const char *src, size_t len)
858 char *walker = dst, *ep = dst + len - 1;
862 while (src != NULL && walker < ep)
874 /* End of /dev/devctl code */
876 static TAILQ_HEAD(,device) bus_data_devices;
877 static int bus_data_generation = 1;
879 static kobj_method_t null_methods[] = {
883 DEFINE_CLASS(null, null_methods, 0);
886 * Bus pass implementation
889 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
890 int bus_current_pass = BUS_PASS_ROOT;
894 * @brief Register the pass level of a new driver attachment
896 * Register a new driver attachment's pass level. If no driver
897 * attachment with the same pass level has been added, then @p new
898 * will be added to the global passes list.
900 * @param new the new driver attachment
903 driver_register_pass(struct driverlink *new)
905 struct driverlink *dl;
907 /* We only consider pass numbers during boot. */
908 if (bus_current_pass == BUS_PASS_DEFAULT)
912 * Walk the passes list. If we already know about this pass
913 * then there is nothing to do. If we don't, then insert this
914 * driver link into the list.
916 TAILQ_FOREACH(dl, &passes, passlink) {
917 if (dl->pass < new->pass)
919 if (dl->pass == new->pass)
921 TAILQ_INSERT_BEFORE(dl, new, passlink);
924 TAILQ_INSERT_TAIL(&passes, new, passlink);
928 * @brief Raise the current bus pass
930 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
931 * method on the root bus to kick off a new device tree scan for each
932 * new pass level that has at least one driver.
935 bus_set_pass(int pass)
937 struct driverlink *dl;
939 if (bus_current_pass > pass)
940 panic("Attempt to lower bus pass level");
942 TAILQ_FOREACH(dl, &passes, passlink) {
943 /* Skip pass values below the current pass level. */
944 if (dl->pass <= bus_current_pass)
948 * Bail once we hit a driver with a pass level that is
955 * Raise the pass level to the next level and rescan
958 bus_current_pass = dl->pass;
959 BUS_NEW_PASS(root_bus);
963 * If there isn't a driver registered for the requested pass,
964 * then bus_current_pass might still be less than 'pass'. Set
965 * it to 'pass' in that case.
967 if (bus_current_pass < pass)
968 bus_current_pass = pass;
969 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
973 * Devclass implementation
976 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
980 * @brief Find or create a device class
982 * If a device class with the name @p classname exists, return it,
983 * otherwise if @p create is non-zero create and return a new device
986 * If @p parentname is non-NULL, the parent of the devclass is set to
987 * the devclass of that name.
989 * @param classname the devclass name to find or create
990 * @param parentname the parent devclass name or @c NULL
991 * @param create non-zero to create a devclass
994 devclass_find_internal(const char *classname, const char *parentname,
999 PDEBUG(("looking for %s", classname));
1003 TAILQ_FOREACH(dc, &devclasses, link) {
1004 if (!strcmp(dc->name, classname))
1008 if (create && !dc) {
1009 PDEBUG(("creating %s", classname));
1010 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
1011 M_BUS, M_NOWAIT | M_ZERO);
1015 dc->name = (char*) (dc + 1);
1016 strcpy(dc->name, classname);
1017 TAILQ_INIT(&dc->drivers);
1018 TAILQ_INSERT_TAIL(&devclasses, dc, link);
1020 bus_data_generation_update();
1024 * If a parent class is specified, then set that as our parent so
1025 * that this devclass will support drivers for the parent class as
1026 * well. If the parent class has the same name don't do this though
1027 * as it creates a cycle that can trigger an infinite loop in
1028 * device_probe_child() if a device exists for which there is no
1031 if (parentname && dc && !dc->parent &&
1032 strcmp(classname, parentname) != 0) {
1033 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
1034 dc->parent->flags |= DC_HAS_CHILDREN;
1041 * @brief Create a device class
1043 * If a device class with the name @p classname exists, return it,
1044 * otherwise create and return a new device class.
1046 * @param classname the devclass name to find or create
1049 devclass_create(const char *classname)
1051 return (devclass_find_internal(classname, NULL, TRUE));
1055 * @brief Find a device class
1057 * If a device class with the name @p classname exists, return it,
1058 * otherwise return @c NULL.
1060 * @param classname the devclass name to find
1063 devclass_find(const char *classname)
1065 return (devclass_find_internal(classname, NULL, FALSE));
1069 * @brief Register that a device driver has been added to a devclass
1071 * Register that a device driver has been added to a devclass. This
1072 * is called by devclass_add_driver to accomplish the recursive
1073 * notification of all the children classes of dc, as well as dc.
1074 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1077 * We do a full search here of the devclass list at each iteration
1078 * level to save storing children-lists in the devclass structure. If
1079 * we ever move beyond a few dozen devices doing this, we may need to
1082 * @param dc the devclass to edit
1083 * @param driver the driver that was just added
1086 devclass_driver_added(devclass_t dc, driver_t *driver)
1092 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1094 for (i = 0; i < dc->maxunit; i++)
1095 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1096 BUS_DRIVER_ADDED(dc->devices[i], driver);
1099 * Walk through the children classes. Since we only keep a
1100 * single parent pointer around, we walk the entire list of
1101 * devclasses looking for children. We set the
1102 * DC_HAS_CHILDREN flag when a child devclass is created on
1103 * the parent, so we only walk the list for those devclasses
1104 * that have children.
1106 if (!(dc->flags & DC_HAS_CHILDREN))
1109 TAILQ_FOREACH(dc, &devclasses, link) {
1110 if (dc->parent == parent)
1111 devclass_driver_added(dc, driver);
1116 * @brief Add a device driver to a device class
1118 * Add a device driver to a devclass. This is normally called
1119 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1120 * all devices in the devclass will be called to allow them to attempt
1121 * to re-probe any unmatched children.
1123 * @param dc the devclass to edit
1124 * @param driver the driver to register
1127 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1130 const char *parentname;
1132 PDEBUG(("%s", DRIVERNAME(driver)));
1134 /* Don't allow invalid pass values. */
1135 if (pass <= BUS_PASS_ROOT)
1138 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1143 * Compile the driver's methods. Also increase the reference count
1144 * so that the class doesn't get freed when the last instance
1145 * goes. This means we can safely use static methods and avoids a
1146 * double-free in devclass_delete_driver.
1148 kobj_class_compile((kobj_class_t) driver);
1151 * If the driver has any base classes, make the
1152 * devclass inherit from the devclass of the driver's
1153 * first base class. This will allow the system to
1154 * search for drivers in both devclasses for children
1155 * of a device using this driver.
1157 if (driver->baseclasses)
1158 parentname = driver->baseclasses[0]->name;
1161 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1163 dl->driver = driver;
1164 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1165 driver->refs++; /* XXX: kobj_mtx */
1167 driver_register_pass(dl);
1169 devclass_driver_added(dc, driver);
1170 bus_data_generation_update();
1175 * @brief Register that a device driver has been deleted from a devclass
1177 * Register that a device driver has been removed from a devclass.
1178 * This is called by devclass_delete_driver to accomplish the
1179 * recursive notification of all the children classes of busclass, as
1180 * well as busclass. Each layer will attempt to detach the driver
1181 * from any devices that are children of the bus's devclass. The function
1182 * will return an error if a device fails to detach.
1184 * We do a full search here of the devclass list at each iteration
1185 * level to save storing children-lists in the devclass structure. If
1186 * we ever move beyond a few dozen devices doing this, we may need to
1189 * @param busclass the devclass of the parent bus
1190 * @param dc the devclass of the driver being deleted
1191 * @param driver the driver being deleted
1194 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1201 * Disassociate from any devices. We iterate through all the
1202 * devices in the devclass of the driver and detach any which are
1203 * using the driver and which have a parent in the devclass which
1204 * we are deleting from.
1206 * Note that since a driver can be in multiple devclasses, we
1207 * should not detach devices which are not children of devices in
1208 * the affected devclass.
1210 for (i = 0; i < dc->maxunit; i++) {
1211 if (dc->devices[i]) {
1212 dev = dc->devices[i];
1213 if (dev->driver == driver && dev->parent &&
1214 dev->parent->devclass == busclass) {
1215 if ((error = device_detach(dev)) != 0)
1217 BUS_PROBE_NOMATCH(dev->parent, dev);
1219 dev->flags |= DF_DONENOMATCH;
1225 * Walk through the children classes. Since we only keep a
1226 * single parent pointer around, we walk the entire list of
1227 * devclasses looking for children. We set the
1228 * DC_HAS_CHILDREN flag when a child devclass is created on
1229 * the parent, so we only walk the list for those devclasses
1230 * that have children.
1232 if (!(busclass->flags & DC_HAS_CHILDREN))
1235 TAILQ_FOREACH(busclass, &devclasses, link) {
1236 if (busclass->parent == parent) {
1237 error = devclass_driver_deleted(busclass, dc, driver);
1246 * @brief Delete a device driver from a device class
1248 * Delete a device driver from a devclass. This is normally called
1249 * automatically by DRIVER_MODULE().
1251 * If the driver is currently attached to any devices,
1252 * devclass_delete_driver() will first attempt to detach from each
1253 * device. If one of the detach calls fails, the driver will not be
1256 * @param dc the devclass to edit
1257 * @param driver the driver to unregister
1260 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1262 devclass_t dc = devclass_find(driver->name);
1266 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1272 * Find the link structure in the bus' list of drivers.
1274 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1275 if (dl->driver == driver)
1280 PDEBUG(("%s not found in %s list", driver->name,
1285 error = devclass_driver_deleted(busclass, dc, driver);
1289 TAILQ_REMOVE(&busclass->drivers, dl, link);
1294 if (driver->refs == 0)
1295 kobj_class_free((kobj_class_t) driver);
1297 bus_data_generation_update();
1302 * @brief Quiesces a set of device drivers from a device class
1304 * Quiesce a device driver from a devclass. This is normally called
1305 * automatically by DRIVER_MODULE().
1307 * If the driver is currently attached to any devices,
1308 * devclass_quiesece_driver() will first attempt to quiesce each
1311 * @param dc the devclass to edit
1312 * @param driver the driver to unregister
1315 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1317 devclass_t dc = devclass_find(driver->name);
1323 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1329 * Find the link structure in the bus' list of drivers.
1331 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1332 if (dl->driver == driver)
1337 PDEBUG(("%s not found in %s list", driver->name,
1343 * Quiesce all devices. We iterate through all the devices in
1344 * the devclass of the driver and quiesce any which are using
1345 * the driver and which have a parent in the devclass which we
1348 * Note that since a driver can be in multiple devclasses, we
1349 * should not quiesce devices which are not children of
1350 * devices in the affected devclass.
1352 for (i = 0; i < dc->maxunit; i++) {
1353 if (dc->devices[i]) {
1354 dev = dc->devices[i];
1355 if (dev->driver == driver && dev->parent &&
1356 dev->parent->devclass == busclass) {
1357 if ((error = device_quiesce(dev)) != 0)
1370 devclass_find_driver_internal(devclass_t dc, const char *classname)
1374 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1376 TAILQ_FOREACH(dl, &dc->drivers, link) {
1377 if (!strcmp(dl->driver->name, classname))
1381 PDEBUG(("not found"));
1386 * @brief Return the name of the devclass
1389 devclass_get_name(devclass_t dc)
1395 * @brief Find a device given a unit number
1397 * @param dc the devclass to search
1398 * @param unit the unit number to search for
1400 * @returns the device with the given unit number or @c
1401 * NULL if there is no such device
1404 devclass_get_device(devclass_t dc, int unit)
1406 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1408 return (dc->devices[unit]);
1412 * @brief Find the softc field of a device given a unit number
1414 * @param dc the devclass to search
1415 * @param unit the unit number to search for
1417 * @returns the softc field of the device with the given
1418 * unit number or @c NULL if there is no such
1422 devclass_get_softc(devclass_t dc, int unit)
1426 dev = devclass_get_device(dc, unit);
1430 return (device_get_softc(dev));
1434 * @brief Get a list of devices in the devclass
1436 * An array containing a list of all the devices in the given devclass
1437 * is allocated and returned in @p *devlistp. The number of devices
1438 * in the array is returned in @p *devcountp. The caller should free
1439 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1441 * @param dc the devclass to examine
1442 * @param devlistp points at location for array pointer return
1444 * @param devcountp points at location for array size return value
1447 * @retval ENOMEM the array allocation failed
1450 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1455 count = devclass_get_count(dc);
1456 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1461 for (i = 0; i < dc->maxunit; i++) {
1462 if (dc->devices[i]) {
1463 list[count] = dc->devices[i];
1475 * @brief Get a list of drivers in the devclass
1477 * An array containing a list of pointers to all the drivers in the
1478 * given devclass is allocated and returned in @p *listp. The number
1479 * of drivers in the array is returned in @p *countp. The caller should
1480 * free the array using @c free(p, M_TEMP).
1482 * @param dc the devclass to examine
1483 * @param listp gives location for array pointer return value
1484 * @param countp gives location for number of array elements
1488 * @retval ENOMEM the array allocation failed
1491 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1498 TAILQ_FOREACH(dl, &dc->drivers, link)
1500 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1505 TAILQ_FOREACH(dl, &dc->drivers, link) {
1506 list[count] = dl->driver;
1516 * @brief Get the number of devices in a devclass
1518 * @param dc the devclass to examine
1521 devclass_get_count(devclass_t dc)
1526 for (i = 0; i < dc->maxunit; i++)
1533 * @brief Get the maximum unit number used in a devclass
1535 * Note that this is one greater than the highest currently-allocated
1536 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1537 * that not even the devclass has been allocated yet.
1539 * @param dc the devclass to examine
1542 devclass_get_maxunit(devclass_t dc)
1546 return (dc->maxunit);
1550 * @brief Find a free unit number in a devclass
1552 * This function searches for the first unused unit number greater
1553 * that or equal to @p unit.
1555 * @param dc the devclass to examine
1556 * @param unit the first unit number to check
1559 devclass_find_free_unit(devclass_t dc, int unit)
1563 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1569 * @brief Set the parent of a devclass
1571 * The parent class is normally initialised automatically by
1574 * @param dc the devclass to edit
1575 * @param pdc the new parent devclass
1578 devclass_set_parent(devclass_t dc, devclass_t pdc)
1584 * @brief Get the parent of a devclass
1586 * @param dc the devclass to examine
1589 devclass_get_parent(devclass_t dc)
1591 return (dc->parent);
1594 struct sysctl_ctx_list *
1595 devclass_get_sysctl_ctx(devclass_t dc)
1597 return (&dc->sysctl_ctx);
1601 devclass_get_sysctl_tree(devclass_t dc)
1603 return (dc->sysctl_tree);
1608 * @brief Allocate a unit number
1610 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1611 * will do). The allocated unit number is returned in @p *unitp.
1613 * @param dc the devclass to allocate from
1614 * @param unitp points at the location for the allocated unit
1618 * @retval EEXIST the requested unit number is already allocated
1619 * @retval ENOMEM memory allocation failure
1622 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1627 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1629 /* Ask the parent bus if it wants to wire this device. */
1631 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1634 /* If we were given a wired unit number, check for existing device */
1637 if (unit >= 0 && unit < dc->maxunit &&
1638 dc->devices[unit] != NULL) {
1640 printf("%s: %s%d already exists; skipping it\n",
1641 dc->name, dc->name, *unitp);
1645 /* Unwired device, find the next available slot for it */
1647 for (unit = 0;; unit++) {
1648 /* If there is an "at" hint for a unit then skip it. */
1649 if (resource_string_value(dc->name, unit, "at", &s) ==
1653 /* If this device slot is already in use, skip it. */
1654 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1662 * We've selected a unit beyond the length of the table, so let's
1663 * extend the table to make room for all units up to and including
1666 if (unit >= dc->maxunit) {
1667 device_t *newlist, *oldlist;
1670 oldlist = dc->devices;
1671 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1672 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1675 if (oldlist != NULL)
1676 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1677 bzero(newlist + dc->maxunit,
1678 sizeof(device_t) * (newsize - dc->maxunit));
1679 dc->devices = newlist;
1680 dc->maxunit = newsize;
1681 if (oldlist != NULL)
1682 free(oldlist, M_BUS);
1684 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1692 * @brief Add a device to a devclass
1694 * A unit number is allocated for the device (using the device's
1695 * preferred unit number if any) and the device is registered in the
1696 * devclass. This allows the device to be looked up by its unit
1697 * number, e.g. by decoding a dev_t minor number.
1699 * @param dc the devclass to add to
1700 * @param dev the device to add
1703 * @retval EEXIST the requested unit number is already allocated
1704 * @retval ENOMEM memory allocation failure
1707 devclass_add_device(devclass_t dc, device_t dev)
1711 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1713 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1716 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1720 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1721 free(dev->nameunit, M_BUS);
1722 dev->nameunit = NULL;
1725 dc->devices[dev->unit] = dev;
1727 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1734 * @brief Delete a device from a devclass
1736 * The device is removed from the devclass's device list and its unit
1739 * @param dc the devclass to delete from
1740 * @param dev the device to delete
1745 devclass_delete_device(devclass_t dc, device_t dev)
1750 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1752 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1753 panic("devclass_delete_device: inconsistent device class");
1754 dc->devices[dev->unit] = NULL;
1755 if (dev->flags & DF_WILDCARD)
1757 dev->devclass = NULL;
1758 free(dev->nameunit, M_BUS);
1759 dev->nameunit = NULL;
1766 * @brief Make a new device and add it as a child of @p parent
1768 * @param parent the parent of the new device
1769 * @param name the devclass name of the new device or @c NULL
1770 * to leave the devclass unspecified
1771 * @parem unit the unit number of the new device of @c -1 to
1772 * leave the unit number unspecified
1774 * @returns the new device
1777 make_device(device_t parent, const char *name, int unit)
1782 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1785 dc = devclass_find_internal(name, NULL, TRUE);
1787 printf("make_device: can't find device class %s\n",
1795 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1799 dev->parent = parent;
1800 TAILQ_INIT(&dev->children);
1801 kobj_init((kobj_t) dev, &null_class);
1803 dev->devclass = NULL;
1805 dev->nameunit = NULL;
1809 dev->flags = DF_ENABLED;
1812 dev->flags |= DF_WILDCARD;
1814 dev->flags |= DF_FIXEDCLASS;
1815 if (devclass_add_device(dc, dev)) {
1816 kobj_delete((kobj_t) dev, M_BUS);
1823 dev->state = DS_NOTPRESENT;
1825 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1826 bus_data_generation_update();
1833 * @brief Print a description of a device.
1836 device_print_child(device_t dev, device_t child)
1840 if (device_is_alive(child))
1841 retval += BUS_PRINT_CHILD(dev, child);
1843 retval += device_printf(child, " not found\n");
1849 * @brief Create a new device
1851 * This creates a new device and adds it as a child of an existing
1852 * parent device. The new device will be added after the last existing
1853 * child with order zero.
1855 * @param dev the device which will be the parent of the
1857 * @param name devclass name for new device or @c NULL if not
1859 * @param unit unit number for new device or @c -1 if not
1862 * @returns the new device
1865 device_add_child(device_t dev, const char *name, int unit)
1867 return (device_add_child_ordered(dev, 0, name, unit));
1871 * @brief Create a new device
1873 * This creates a new device and adds it as a child of an existing
1874 * parent device. The new device will be added after the last existing
1875 * child with the same order.
1877 * @param dev the device which will be the parent of the
1879 * @param order a value which is used to partially sort the
1880 * children of @p dev - devices created using
1881 * lower values of @p order appear first in @p
1882 * dev's list of children
1883 * @param name devclass name for new device or @c NULL if not
1885 * @param unit unit number for new device or @c -1 if not
1888 * @returns the new device
1891 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1896 PDEBUG(("%s at %s with order %u as unit %d",
1897 name, DEVICENAME(dev), order, unit));
1898 KASSERT(name != NULL || unit == -1,
1899 ("child device with wildcard name and specific unit number"));
1901 child = make_device(dev, name, unit);
1904 child->order = order;
1906 TAILQ_FOREACH(place, &dev->children, link) {
1907 if (place->order > order)
1913 * The device 'place' is the first device whose order is
1914 * greater than the new child.
1916 TAILQ_INSERT_BEFORE(place, child, link);
1919 * The new child's order is greater or equal to the order of
1920 * any existing device. Add the child to the tail of the list.
1922 TAILQ_INSERT_TAIL(&dev->children, child, link);
1925 bus_data_generation_update();
1930 * @brief Delete a device
1932 * This function deletes a device along with all of its children. If
1933 * the device currently has a driver attached to it, the device is
1934 * detached first using device_detach().
1936 * @param dev the parent device
1937 * @param child the device to delete
1940 * @retval non-zero a unit error code describing the error
1943 device_delete_child(device_t dev, device_t child)
1946 device_t grandchild;
1948 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1950 /* remove children first */
1951 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1952 error = device_delete_child(child, grandchild);
1957 if ((error = device_detach(child)) != 0)
1959 if (child->devclass)
1960 devclass_delete_device(child->devclass, child);
1962 BUS_CHILD_DELETED(dev, child);
1963 TAILQ_REMOVE(&dev->children, child, link);
1964 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1965 kobj_delete((kobj_t) child, M_BUS);
1967 bus_data_generation_update();
1972 * @brief Delete all children devices of the given device, if any.
1974 * This function deletes all children devices of the given device, if
1975 * any, using the device_delete_child() function for each device it
1976 * finds. If a child device cannot be deleted, this function will
1977 * return an error code.
1979 * @param dev the parent device
1982 * @retval non-zero a device would not detach
1985 device_delete_children(device_t dev)
1990 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1994 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1995 error = device_delete_child(dev, child);
1997 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
2005 * @brief Find a device given a unit number
2007 * This is similar to devclass_get_devices() but only searches for
2008 * devices which have @p dev as a parent.
2010 * @param dev the parent device to search
2011 * @param unit the unit number to search for. If the unit is -1,
2012 * return the first child of @p dev which has name
2013 * @p classname (that is, the one with the lowest unit.)
2015 * @returns the device with the given unit number or @c
2016 * NULL if there is no such device
2019 device_find_child(device_t dev, const char *classname, int unit)
2024 dc = devclass_find(classname);
2029 child = devclass_get_device(dc, unit);
2030 if (child && child->parent == dev)
2033 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
2034 child = devclass_get_device(dc, unit);
2035 if (child && child->parent == dev)
2046 first_matching_driver(devclass_t dc, device_t dev)
2049 return (devclass_find_driver_internal(dc, dev->devclass->name));
2050 return (TAILQ_FIRST(&dc->drivers));
2057 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
2059 if (dev->devclass) {
2061 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
2062 if (!strcmp(dev->devclass->name, dl->driver->name))
2066 return (TAILQ_NEXT(last, link));
2073 device_probe_child(device_t dev, device_t child)
2076 driverlink_t best = NULL;
2078 int result, pri = 0;
2079 int hasclass = (child->devclass != NULL);
2085 panic("device_probe_child: parent device has no devclass");
2088 * If the state is already probed, then return. However, don't
2089 * return if we can rebid this object.
2091 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2094 for (; dc; dc = dc->parent) {
2095 for (dl = first_matching_driver(dc, child);
2097 dl = next_matching_driver(dc, child, dl)) {
2098 /* If this driver's pass is too high, then ignore it. */
2099 if (dl->pass > bus_current_pass)
2102 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2103 result = device_set_driver(child, dl->driver);
2104 if (result == ENOMEM)
2106 else if (result != 0)
2109 if (device_set_devclass(child,
2110 dl->driver->name) != 0) {
2111 char const * devname =
2112 device_get_name(child);
2113 if (devname == NULL)
2114 devname = "(unknown)";
2115 printf("driver bug: Unable to set "
2116 "devclass (class: %s "
2120 (void)device_set_driver(child, NULL);
2125 /* Fetch any flags for the device before probing. */
2126 resource_int_value(dl->driver->name, child->unit,
2127 "flags", &child->devflags);
2129 result = DEVICE_PROBE(child);
2131 /* Reset flags and devclass before the next probe. */
2132 child->devflags = 0;
2134 (void)device_set_devclass(child, NULL);
2137 * If the driver returns SUCCESS, there can be
2138 * no higher match for this device.
2147 * Probes that return BUS_PROBE_NOWILDCARD or lower
2148 * only match on devices whose driver was explicitly
2151 if (result <= BUS_PROBE_NOWILDCARD &&
2152 !(child->flags & DF_FIXEDCLASS)) {
2157 * The driver returned an error so it
2158 * certainly doesn't match.
2161 (void)device_set_driver(child, NULL);
2166 * A priority lower than SUCCESS, remember the
2167 * best matching driver. Initialise the value
2168 * of pri for the first match.
2170 if (best == NULL || result > pri) {
2177 * If we have an unambiguous match in this devclass,
2178 * don't look in the parent.
2180 if (best && pri == 0)
2185 * If we found a driver, change state and initialise the devclass.
2187 /* XXX What happens if we rebid and got no best? */
2190 * If this device was attached, and we were asked to
2191 * rescan, and it is a different driver, then we have
2192 * to detach the old driver and reattach this new one.
2193 * Note, we don't have to check for DF_REBID here
2194 * because if the state is > DS_ALIVE, we know it must
2197 * This assumes that all DF_REBID drivers can have
2198 * their probe routine called at any time and that
2199 * they are idempotent as well as completely benign in
2200 * normal operations.
2202 * We also have to make sure that the detach
2203 * succeeded, otherwise we fail the operation (or
2204 * maybe it should just fail silently? I'm torn).
2206 if (child->state > DS_ALIVE && best->driver != child->driver)
2207 if ((result = device_detach(dev)) != 0)
2210 /* Set the winning driver, devclass, and flags. */
2211 if (!child->devclass) {
2212 result = device_set_devclass(child, best->driver->name);
2216 result = device_set_driver(child, best->driver);
2219 resource_int_value(best->driver->name, child->unit,
2220 "flags", &child->devflags);
2224 * A bit bogus. Call the probe method again to make
2225 * sure that we have the right description.
2227 DEVICE_PROBE(child);
2229 child->flags |= DF_REBID;
2232 child->flags &= ~DF_REBID;
2233 child->state = DS_ALIVE;
2235 bus_data_generation_update();
2243 * @brief Return the parent of a device
2246 device_get_parent(device_t dev)
2248 return (dev->parent);
2252 * @brief Get a list of children of a device
2254 * An array containing a list of all the children of the given device
2255 * is allocated and returned in @p *devlistp. The number of devices
2256 * in the array is returned in @p *devcountp. The caller should free
2257 * the array using @c free(p, M_TEMP).
2259 * @param dev the device to examine
2260 * @param devlistp points at location for array pointer return
2262 * @param devcountp points at location for array size return value
2265 * @retval ENOMEM the array allocation failed
2268 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2275 TAILQ_FOREACH(child, &dev->children, link) {
2284 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2289 TAILQ_FOREACH(child, &dev->children, link) {
2290 list[count] = child;
2301 * @brief Return the current driver for the device or @c NULL if there
2302 * is no driver currently attached
2305 device_get_driver(device_t dev)
2307 return (dev->driver);
2311 * @brief Return the current devclass for the device or @c NULL if
2315 device_get_devclass(device_t dev)
2317 return (dev->devclass);
2321 * @brief Return the name of the device's devclass or @c NULL if there
2325 device_get_name(device_t dev)
2327 if (dev != NULL && dev->devclass)
2328 return (devclass_get_name(dev->devclass));
2333 * @brief Return a string containing the device's devclass name
2334 * followed by an ascii representation of the device's unit number
2338 device_get_nameunit(device_t dev)
2340 return (dev->nameunit);
2344 * @brief Return the device's unit number.
2347 device_get_unit(device_t dev)
2353 * @brief Return the device's description string
2356 device_get_desc(device_t dev)
2362 * @brief Return the device's flags
2365 device_get_flags(device_t dev)
2367 return (dev->devflags);
2370 struct sysctl_ctx_list *
2371 device_get_sysctl_ctx(device_t dev)
2373 return (&dev->sysctl_ctx);
2377 device_get_sysctl_tree(device_t dev)
2379 return (dev->sysctl_tree);
2383 * @brief Print the name of the device followed by a colon and a space
2385 * @returns the number of characters printed
2388 device_print_prettyname(device_t dev)
2390 const char *name = device_get_name(dev);
2393 return (printf("unknown: "));
2394 return (printf("%s%d: ", name, device_get_unit(dev)));
2398 * @brief Print the name of the device followed by a colon, a space
2399 * and the result of calling vprintf() with the value of @p fmt and
2400 * the following arguments.
2402 * @returns the number of characters printed
2405 device_printf(device_t dev, const char * fmt, ...)
2410 retval = device_print_prettyname(dev);
2412 retval += vprintf(fmt, ap);
2421 device_set_desc_internal(device_t dev, const char* desc, int copy)
2423 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2424 free(dev->desc, M_BUS);
2425 dev->flags &= ~DF_DESCMALLOCED;
2430 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2432 strcpy(dev->desc, desc);
2433 dev->flags |= DF_DESCMALLOCED;
2436 /* Avoid a -Wcast-qual warning */
2437 dev->desc = (char *)(uintptr_t) desc;
2440 bus_data_generation_update();
2444 * @brief Set the device's description
2446 * The value of @c desc should be a string constant that will not
2447 * change (at least until the description is changed in a subsequent
2448 * call to device_set_desc() or device_set_desc_copy()).
2451 device_set_desc(device_t dev, const char* desc)
2453 device_set_desc_internal(dev, desc, FALSE);
2457 * @brief Set the device's description
2459 * The string pointed to by @c desc is copied. Use this function if
2460 * the device description is generated, (e.g. with sprintf()).
2463 device_set_desc_copy(device_t dev, const char* desc)
2465 device_set_desc_internal(dev, desc, TRUE);
2469 * @brief Set the device's flags
2472 device_set_flags(device_t dev, uint32_t flags)
2474 dev->devflags = flags;
2478 * @brief Return the device's softc field
2480 * The softc is allocated and zeroed when a driver is attached, based
2481 * on the size field of the driver.
2484 device_get_softc(device_t dev)
2486 return (dev->softc);
2490 * @brief Set the device's softc field
2492 * Most drivers do not need to use this since the softc is allocated
2493 * automatically when the driver is attached.
2496 device_set_softc(device_t dev, void *softc)
2498 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2499 free(dev->softc, M_BUS_SC);
2502 dev->flags |= DF_EXTERNALSOFTC;
2504 dev->flags &= ~DF_EXTERNALSOFTC;
2508 * @brief Free claimed softc
2510 * Most drivers do not need to use this since the softc is freed
2511 * automatically when the driver is detached.
2514 device_free_softc(void *softc)
2516 free(softc, M_BUS_SC);
2520 * @brief Claim softc
2522 * This function can be used to let the driver free the automatically
2523 * allocated softc using "device_free_softc()". This function is
2524 * useful when the driver is refcounting the softc and the softc
2525 * cannot be freed when the "device_detach" method is called.
2528 device_claim_softc(device_t dev)
2531 dev->flags |= DF_EXTERNALSOFTC;
2533 dev->flags &= ~DF_EXTERNALSOFTC;
2537 * @brief Get the device's ivars field
2539 * The ivars field is used by the parent device to store per-device
2540 * state (e.g. the physical location of the device or a list of
2544 device_get_ivars(device_t dev)
2547 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2548 return (dev->ivars);
2552 * @brief Set the device's ivars field
2555 device_set_ivars(device_t dev, void * ivars)
2558 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2563 * @brief Return the device's state
2566 device_get_state(device_t dev)
2568 return (dev->state);
2572 * @brief Set the DF_ENABLED flag for the device
2575 device_enable(device_t dev)
2577 dev->flags |= DF_ENABLED;
2581 * @brief Clear the DF_ENABLED flag for the device
2584 device_disable(device_t dev)
2586 dev->flags &= ~DF_ENABLED;
2590 * @brief Increment the busy counter for the device
2593 device_busy(device_t dev)
2595 if (dev->state < DS_ATTACHING)
2596 panic("device_busy: called for unattached device");
2597 if (dev->busy == 0 && dev->parent)
2598 device_busy(dev->parent);
2600 if (dev->state == DS_ATTACHED)
2601 dev->state = DS_BUSY;
2605 * @brief Decrement the busy counter for the device
2608 device_unbusy(device_t dev)
2610 if (dev->busy != 0 && dev->state != DS_BUSY &&
2611 dev->state != DS_ATTACHING)
2612 panic("device_unbusy: called for non-busy device %s",
2613 device_get_nameunit(dev));
2615 if (dev->busy == 0) {
2617 device_unbusy(dev->parent);
2618 if (dev->state == DS_BUSY)
2619 dev->state = DS_ATTACHED;
2624 * @brief Set the DF_QUIET flag for the device
2627 device_quiet(device_t dev)
2629 dev->flags |= DF_QUIET;
2633 * @brief Clear the DF_QUIET flag for the device
2636 device_verbose(device_t dev)
2638 dev->flags &= ~DF_QUIET;
2642 * @brief Return non-zero if the DF_QUIET flag is set on the device
2645 device_is_quiet(device_t dev)
2647 return ((dev->flags & DF_QUIET) != 0);
2651 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2654 device_is_enabled(device_t dev)
2656 return ((dev->flags & DF_ENABLED) != 0);
2660 * @brief Return non-zero if the device was successfully probed
2663 device_is_alive(device_t dev)
2665 return (dev->state >= DS_ALIVE);
2669 * @brief Return non-zero if the device currently has a driver
2673 device_is_attached(device_t dev)
2675 return (dev->state >= DS_ATTACHED);
2679 * @brief Return non-zero if the device is currently suspended.
2682 device_is_suspended(device_t dev)
2684 return ((dev->flags & DF_SUSPENDED) != 0);
2688 * @brief Set the devclass of a device
2689 * @see devclass_add_device().
2692 device_set_devclass(device_t dev, const char *classname)
2699 devclass_delete_device(dev->devclass, dev);
2703 if (dev->devclass) {
2704 printf("device_set_devclass: device class already set\n");
2708 dc = devclass_find_internal(classname, NULL, TRUE);
2712 error = devclass_add_device(dc, dev);
2714 bus_data_generation_update();
2719 * @brief Set the devclass of a device and mark the devclass fixed.
2720 * @see device_set_devclass()
2723 device_set_devclass_fixed(device_t dev, const char *classname)
2727 if (classname == NULL)
2730 error = device_set_devclass(dev, classname);
2733 dev->flags |= DF_FIXEDCLASS;
2738 * @brief Set the driver of a device
2741 * @retval EBUSY the device already has a driver attached
2742 * @retval ENOMEM a memory allocation failure occurred
2745 device_set_driver(device_t dev, driver_t *driver)
2747 if (dev->state >= DS_ATTACHED)
2750 if (dev->driver == driver)
2753 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2754 free(dev->softc, M_BUS_SC);
2757 device_set_desc(dev, NULL);
2758 kobj_delete((kobj_t) dev, NULL);
2759 dev->driver = driver;
2761 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2762 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2763 dev->softc = malloc(driver->size, M_BUS_SC,
2766 kobj_delete((kobj_t) dev, NULL);
2767 kobj_init((kobj_t) dev, &null_class);
2773 kobj_init((kobj_t) dev, &null_class);
2776 bus_data_generation_update();
2781 * @brief Probe a device, and return this status.
2783 * This function is the core of the device autoconfiguration
2784 * system. Its purpose is to select a suitable driver for a device and
2785 * then call that driver to initialise the hardware appropriately. The
2786 * driver is selected by calling the DEVICE_PROBE() method of a set of
2787 * candidate drivers and then choosing the driver which returned the
2788 * best value. This driver is then attached to the device using
2791 * The set of suitable drivers is taken from the list of drivers in
2792 * the parent device's devclass. If the device was originally created
2793 * with a specific class name (see device_add_child()), only drivers
2794 * with that name are probed, otherwise all drivers in the devclass
2795 * are probed. If no drivers return successful probe values in the
2796 * parent devclass, the search continues in the parent of that
2797 * devclass (see devclass_get_parent()) if any.
2799 * @param dev the device to initialise
2802 * @retval ENXIO no driver was found
2803 * @retval ENOMEM memory allocation failure
2804 * @retval non-zero some other unix error code
2805 * @retval -1 Device already attached
2808 device_probe(device_t dev)
2814 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2817 if (!(dev->flags & DF_ENABLED)) {
2818 if (bootverbose && device_get_name(dev) != NULL) {
2819 device_print_prettyname(dev);
2820 printf("not probed (disabled)\n");
2824 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2825 if (bus_current_pass == BUS_PASS_DEFAULT &&
2826 !(dev->flags & DF_DONENOMATCH)) {
2827 BUS_PROBE_NOMATCH(dev->parent, dev);
2829 dev->flags |= DF_DONENOMATCH;
2837 * @brief Probe a device and attach a driver if possible
2839 * calls device_probe() and attaches if that was successful.
2842 device_probe_and_attach(device_t dev)
2848 error = device_probe(dev);
2851 else if (error != 0)
2854 CURVNET_SET_QUIET(vnet0);
2855 error = device_attach(dev);
2861 * @brief Attach a device driver to a device
2863 * This function is a wrapper around the DEVICE_ATTACH() driver
2864 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2865 * device's sysctl tree, optionally prints a description of the device
2866 * and queues a notification event for user-based device management
2869 * Normally this function is only called internally from
2870 * device_probe_and_attach().
2872 * @param dev the device to initialise
2875 * @retval ENXIO no driver was found
2876 * @retval ENOMEM memory allocation failure
2877 * @retval non-zero some other unix error code
2880 device_attach(device_t dev)
2882 uint64_t attachtime;
2885 if (resource_disabled(dev->driver->name, dev->unit)) {
2886 device_disable(dev);
2888 device_printf(dev, "disabled via hints entry\n");
2892 device_sysctl_init(dev);
2893 if (!device_is_quiet(dev))
2894 device_print_child(dev->parent, dev);
2895 attachtime = get_cyclecount();
2896 dev->state = DS_ATTACHING;
2897 if ((error = DEVICE_ATTACH(dev)) != 0) {
2898 printf("device_attach: %s%d attach returned %d\n",
2899 dev->driver->name, dev->unit, error);
2900 if (!(dev->flags & DF_FIXEDCLASS))
2901 devclass_delete_device(dev->devclass, dev);
2902 (void)device_set_driver(dev, NULL);
2903 device_sysctl_fini(dev);
2904 KASSERT(dev->busy == 0, ("attach failed but busy"));
2905 dev->state = DS_NOTPRESENT;
2908 attachtime = get_cyclecount() - attachtime;
2910 * 4 bits per device is a reasonable value for desktop and server
2911 * hardware with good get_cyclecount() implementations, but WILL
2912 * need to be adjusted on other platforms.
2914 #define RANDOM_PROBE_BIT_GUESS 4
2916 printf("random: harvesting attach, %zu bytes (%d bits) from %s%d\n",
2917 sizeof(attachtime), RANDOM_PROBE_BIT_GUESS,
2918 dev->driver->name, dev->unit);
2919 random_harvest_direct(&attachtime, sizeof(attachtime),
2920 RANDOM_PROBE_BIT_GUESS, RANDOM_ATTACH);
2921 device_sysctl_update(dev);
2923 dev->state = DS_BUSY;
2925 dev->state = DS_ATTACHED;
2926 dev->flags &= ~DF_DONENOMATCH;
2932 * @brief Detach a driver from a device
2934 * This function is a wrapper around the DEVICE_DETACH() driver
2935 * method. If the call to DEVICE_DETACH() succeeds, it calls
2936 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2937 * notification event for user-based device management services and
2938 * cleans up the device's sysctl tree.
2940 * @param dev the device to un-initialise
2943 * @retval ENXIO no driver was found
2944 * @retval ENOMEM memory allocation failure
2945 * @retval non-zero some other unix error code
2948 device_detach(device_t dev)
2954 PDEBUG(("%s", DEVICENAME(dev)));
2955 if (dev->state == DS_BUSY)
2957 if (dev->state != DS_ATTACHED)
2960 if ((error = DEVICE_DETACH(dev)) != 0)
2963 if (!device_is_quiet(dev))
2964 device_printf(dev, "detached\n");
2966 BUS_CHILD_DETACHED(dev->parent, dev);
2968 if (!(dev->flags & DF_FIXEDCLASS))
2969 devclass_delete_device(dev->devclass, dev);
2971 dev->state = DS_NOTPRESENT;
2972 (void)device_set_driver(dev, NULL);
2973 device_sysctl_fini(dev);
2979 * @brief Tells a driver to quiesce itself.
2981 * This function is a wrapper around the DEVICE_QUIESCE() driver
2982 * method. If the call to DEVICE_QUIESCE() succeeds.
2984 * @param dev the device to quiesce
2987 * @retval ENXIO no driver was found
2988 * @retval ENOMEM memory allocation failure
2989 * @retval non-zero some other unix error code
2992 device_quiesce(device_t dev)
2995 PDEBUG(("%s", DEVICENAME(dev)));
2996 if (dev->state == DS_BUSY)
2998 if (dev->state != DS_ATTACHED)
3001 return (DEVICE_QUIESCE(dev));
3005 * @brief Notify a device of system shutdown
3007 * This function calls the DEVICE_SHUTDOWN() driver method if the
3008 * device currently has an attached driver.
3010 * @returns the value returned by DEVICE_SHUTDOWN()
3013 device_shutdown(device_t dev)
3015 if (dev->state < DS_ATTACHED)
3017 return (DEVICE_SHUTDOWN(dev));
3021 * @brief Set the unit number of a device
3023 * This function can be used to override the unit number used for a
3024 * device (e.g. to wire a device to a pre-configured unit number).
3027 device_set_unit(device_t dev, int unit)
3032 dc = device_get_devclass(dev);
3033 if (unit < dc->maxunit && dc->devices[unit])
3035 err = devclass_delete_device(dc, dev);
3039 err = devclass_add_device(dc, dev);
3043 bus_data_generation_update();
3047 /*======================================*/
3049 * Some useful method implementations to make life easier for bus drivers.
3053 * @brief Initialise a resource list.
3055 * @param rl the resource list to initialise
3058 resource_list_init(struct resource_list *rl)
3064 * @brief Reclaim memory used by a resource list.
3066 * This function frees the memory for all resource entries on the list
3069 * @param rl the resource list to free
3072 resource_list_free(struct resource_list *rl)
3074 struct resource_list_entry *rle;
3076 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3078 panic("resource_list_free: resource entry is busy");
3079 STAILQ_REMOVE_HEAD(rl, link);
3085 * @brief Add a resource entry.
3087 * This function adds a resource entry using the given @p type, @p
3088 * start, @p end and @p count values. A rid value is chosen by
3089 * searching sequentially for the first unused rid starting at zero.
3091 * @param rl the resource list to edit
3092 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3093 * @param start the start address of the resource
3094 * @param end the end address of the resource
3095 * @param count XXX end-start+1
3098 resource_list_add_next(struct resource_list *rl, int type, rman_res_t start,
3099 rman_res_t end, rman_res_t count)
3104 while (resource_list_find(rl, type, rid) != NULL)
3106 resource_list_add(rl, type, rid, start, end, count);
3111 * @brief Add or modify a resource entry.
3113 * If an existing entry exists with the same type and rid, it will be
3114 * modified using the given values of @p start, @p end and @p
3115 * count. If no entry exists, a new one will be created using the
3116 * given values. The resource list entry that matches is then returned.
3118 * @param rl the resource list to edit
3119 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3120 * @param rid the resource identifier
3121 * @param start the start address of the resource
3122 * @param end the end address of the resource
3123 * @param count XXX end-start+1
3125 struct resource_list_entry *
3126 resource_list_add(struct resource_list *rl, int type, int rid,
3127 rman_res_t start, rman_res_t end, rman_res_t count)
3129 struct resource_list_entry *rle;
3131 rle = resource_list_find(rl, type, rid);
3133 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3136 panic("resource_list_add: can't record entry");
3137 STAILQ_INSERT_TAIL(rl, rle, link);
3145 panic("resource_list_add: resource entry is busy");
3154 * @brief Determine if a resource entry is busy.
3156 * Returns true if a resource entry is busy meaning that it has an
3157 * associated resource that is not an unallocated "reserved" resource.
3159 * @param rl the resource list to search
3160 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3161 * @param rid the resource identifier
3163 * @returns Non-zero if the entry is busy, zero otherwise.
3166 resource_list_busy(struct resource_list *rl, int type, int rid)
3168 struct resource_list_entry *rle;
3170 rle = resource_list_find(rl, type, rid);
3171 if (rle == NULL || rle->res == NULL)
3173 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3174 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3175 ("reserved resource is active"));
3182 * @brief Determine if a resource entry is reserved.
3184 * Returns true if a resource entry is reserved meaning that it has an
3185 * associated "reserved" resource. The resource can either be
3186 * allocated or unallocated.
3188 * @param rl the resource list to search
3189 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3190 * @param rid the resource identifier
3192 * @returns Non-zero if the entry is reserved, zero otherwise.
3195 resource_list_reserved(struct resource_list *rl, int type, int rid)
3197 struct resource_list_entry *rle;
3199 rle = resource_list_find(rl, type, rid);
3200 if (rle != NULL && rle->flags & RLE_RESERVED)
3206 * @brief Find a resource entry by type and rid.
3208 * @param rl the resource list to search
3209 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3210 * @param rid the resource identifier
3212 * @returns the resource entry pointer or NULL if there is no such
3215 struct resource_list_entry *
3216 resource_list_find(struct resource_list *rl, int type, int rid)
3218 struct resource_list_entry *rle;
3220 STAILQ_FOREACH(rle, rl, link) {
3221 if (rle->type == type && rle->rid == rid)
3228 * @brief Delete a resource entry.
3230 * @param rl the resource list to edit
3231 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3232 * @param rid the resource identifier
3235 resource_list_delete(struct resource_list *rl, int type, int rid)
3237 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3240 if (rle->res != NULL)
3241 panic("resource_list_delete: resource has not been released");
3242 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3248 * @brief Allocate a reserved resource
3250 * This can be used by busses to force the allocation of resources
3251 * that are always active in the system even if they are not allocated
3252 * by a driver (e.g. PCI BARs). This function is usually called when
3253 * adding a new child to the bus. The resource is allocated from the
3254 * parent bus when it is reserved. The resource list entry is marked
3255 * with RLE_RESERVED to note that it is a reserved resource.
3257 * Subsequent attempts to allocate the resource with
3258 * resource_list_alloc() will succeed the first time and will set
3259 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3260 * resource that has been allocated is released with
3261 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3262 * the actual resource remains allocated. The resource can be released to
3263 * the parent bus by calling resource_list_unreserve().
3265 * @param rl the resource list to allocate from
3266 * @param bus the parent device of @p child
3267 * @param child the device for which the resource is being reserved
3268 * @param type the type of resource to allocate
3269 * @param rid a pointer to the resource identifier
3270 * @param start hint at the start of the resource range - pass
3271 * @c 0 for any start address
3272 * @param end hint at the end of the resource range - pass
3273 * @c ~0 for any end address
3274 * @param count hint at the size of range required - pass @c 1
3276 * @param flags any extra flags to control the resource
3277 * allocation - see @c RF_XXX flags in
3278 * <sys/rman.h> for details
3280 * @returns the resource which was allocated or @c NULL if no
3281 * resource could be allocated
3284 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3285 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3287 struct resource_list_entry *rle = NULL;
3288 int passthrough = (device_get_parent(child) != bus);
3293 "resource_list_reserve() should only be called for direct children");
3294 if (flags & RF_ACTIVE)
3296 "resource_list_reserve() should only reserve inactive resources");
3298 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3301 rle = resource_list_find(rl, type, *rid);
3302 rle->flags |= RLE_RESERVED;
3308 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3310 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3311 * and passing the allocation up to the parent of @p bus. This assumes
3312 * that the first entry of @c device_get_ivars(child) is a struct
3313 * resource_list. This also handles 'passthrough' allocations where a
3314 * child is a remote descendant of bus by passing the allocation up to
3315 * the parent of bus.
3317 * Typically, a bus driver would store a list of child resources
3318 * somewhere in the child device's ivars (see device_get_ivars()) and
3319 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3320 * then call resource_list_alloc() to perform the allocation.
3322 * @param rl the resource list to allocate from
3323 * @param bus the parent device of @p child
3324 * @param child the device which is requesting an allocation
3325 * @param type the type of resource to allocate
3326 * @param rid a pointer to the resource identifier
3327 * @param start hint at the start of the resource range - pass
3328 * @c 0 for any start address
3329 * @param end hint at the end of the resource range - pass
3330 * @c ~0 for any end address
3331 * @param count hint at the size of range required - pass @c 1
3333 * @param flags any extra flags to control the resource
3334 * allocation - see @c RF_XXX flags in
3335 * <sys/rman.h> for details
3337 * @returns the resource which was allocated or @c NULL if no
3338 * resource could be allocated
3341 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3342 int type, int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
3344 struct resource_list_entry *rle = NULL;
3345 int passthrough = (device_get_parent(child) != bus);
3346 int isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
3349 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3350 type, rid, start, end, count, flags));
3353 rle = resource_list_find(rl, type, *rid);
3356 return (NULL); /* no resource of that type/rid */
3359 if (rle->flags & RLE_RESERVED) {
3360 if (rle->flags & RLE_ALLOCATED)
3362 if ((flags & RF_ACTIVE) &&
3363 bus_activate_resource(child, type, *rid,
3366 rle->flags |= RLE_ALLOCATED;
3370 "resource entry %#x type %d for child %s is busy\n", *rid,
3371 type, device_get_nameunit(child));
3377 count = ulmax(count, rle->count);
3378 end = ulmax(rle->end, start + count - 1);
3381 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3382 type, rid, start, end, count, flags);
3385 * Record the new range.
3388 rle->start = rman_get_start(rle->res);
3389 rle->end = rman_get_end(rle->res);
3397 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3399 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3400 * used with resource_list_alloc().
3402 * @param rl the resource list which was allocated from
3403 * @param bus the parent device of @p child
3404 * @param child the device which is requesting a release
3405 * @param type the type of resource to release
3406 * @param rid the resource identifier
3407 * @param res the resource to release
3410 * @retval non-zero a standard unix error code indicating what
3411 * error condition prevented the operation
3414 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3415 int type, int rid, struct resource *res)
3417 struct resource_list_entry *rle = NULL;
3418 int passthrough = (device_get_parent(child) != bus);
3422 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3426 rle = resource_list_find(rl, type, rid);
3429 panic("resource_list_release: can't find resource");
3431 panic("resource_list_release: resource entry is not busy");
3432 if (rle->flags & RLE_RESERVED) {
3433 if (rle->flags & RLE_ALLOCATED) {
3434 if (rman_get_flags(res) & RF_ACTIVE) {
3435 error = bus_deactivate_resource(child, type,
3440 rle->flags &= ~RLE_ALLOCATED;
3446 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3456 * @brief Release all active resources of a given type
3458 * Release all active resources of a specified type. This is intended
3459 * to be used to cleanup resources leaked by a driver after detach or
3462 * @param rl the resource list which was allocated from
3463 * @param bus the parent device of @p child
3464 * @param child the device whose active resources are being released
3465 * @param type the type of resources to release
3468 * @retval EBUSY at least one resource was active
3471 resource_list_release_active(struct resource_list *rl, device_t bus,
3472 device_t child, int type)
3474 struct resource_list_entry *rle;
3478 STAILQ_FOREACH(rle, rl, link) {
3479 if (rle->type != type)
3481 if (rle->res == NULL)
3483 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) ==
3487 error = resource_list_release(rl, bus, child, type,
3488 rman_get_rid(rle->res), rle->res);
3491 "Failed to release active resource: %d\n", error);
3498 * @brief Fully release a reserved resource
3500 * Fully releases a resource reserved via resource_list_reserve().
3502 * @param rl the resource list which was allocated from
3503 * @param bus the parent device of @p child
3504 * @param child the device whose reserved resource is being released
3505 * @param type the type of resource to release
3506 * @param rid the resource identifier
3507 * @param res the resource to release
3510 * @retval non-zero a standard unix error code indicating what
3511 * error condition prevented the operation
3514 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3517 struct resource_list_entry *rle = NULL;
3518 int passthrough = (device_get_parent(child) != bus);
3522 "resource_list_unreserve() should only be called for direct children");
3524 rle = resource_list_find(rl, type, rid);
3527 panic("resource_list_unreserve: can't find resource");
3528 if (!(rle->flags & RLE_RESERVED))
3530 if (rle->flags & RLE_ALLOCATED)
3532 rle->flags &= ~RLE_RESERVED;
3533 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3537 * @brief Print a description of resources in a resource list
3539 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3540 * The name is printed if at least one resource of the given type is available.
3541 * The format is used to print resource start and end.
3543 * @param rl the resource list to print
3544 * @param name the name of @p type, e.g. @c "memory"
3545 * @param type type type of resource entry to print
3546 * @param format printf(9) format string to print resource
3547 * start and end values
3549 * @returns the number of characters printed
3552 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3555 struct resource_list_entry *rle;
3556 int printed, retval;
3560 /* Yes, this is kinda cheating */
3561 STAILQ_FOREACH(rle, rl, link) {
3562 if (rle->type == type) {
3564 retval += printf(" %s ", name);
3566 retval += printf(",");
3568 retval += printf(format, rle->start);
3569 if (rle->count > 1) {
3570 retval += printf("-");
3571 retval += printf(format, rle->start +
3580 * @brief Releases all the resources in a list.
3582 * @param rl The resource list to purge.
3587 resource_list_purge(struct resource_list *rl)
3589 struct resource_list_entry *rle;
3591 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3593 bus_release_resource(rman_get_device(rle->res),
3594 rle->type, rle->rid, rle->res);
3595 STAILQ_REMOVE_HEAD(rl, link);
3601 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3604 return (device_add_child_ordered(dev, order, name, unit));
3608 * @brief Helper function for implementing DEVICE_PROBE()
3610 * This function can be used to help implement the DEVICE_PROBE() for
3611 * a bus (i.e. a device which has other devices attached to it). It
3612 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3616 bus_generic_probe(device_t dev)
3618 devclass_t dc = dev->devclass;
3621 TAILQ_FOREACH(dl, &dc->drivers, link) {
3623 * If this driver's pass is too high, then ignore it.
3624 * For most drivers in the default pass, this will
3625 * never be true. For early-pass drivers they will
3626 * only call the identify routines of eligible drivers
3627 * when this routine is called. Drivers for later
3628 * passes should have their identify routines called
3629 * on early-pass busses during BUS_NEW_PASS().
3631 if (dl->pass > bus_current_pass)
3633 DEVICE_IDENTIFY(dl->driver, dev);
3640 * @brief Helper function for implementing DEVICE_ATTACH()
3642 * This function can be used to help implement the DEVICE_ATTACH() for
3643 * a bus. It calls device_probe_and_attach() for each of the device's
3647 bus_generic_attach(device_t dev)
3651 TAILQ_FOREACH(child, &dev->children, link) {
3652 device_probe_and_attach(child);
3659 * @brief Helper function for implementing DEVICE_DETACH()
3661 * This function can be used to help implement the DEVICE_DETACH() for
3662 * a bus. It calls device_detach() for each of the device's
3666 bus_generic_detach(device_t dev)
3671 if (dev->state != DS_ATTACHED)
3674 TAILQ_FOREACH(child, &dev->children, link) {
3675 if ((error = device_detach(child)) != 0)
3683 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3685 * This function can be used to help implement the DEVICE_SHUTDOWN()
3686 * for a bus. It calls device_shutdown() for each of the device's
3690 bus_generic_shutdown(device_t dev)
3694 TAILQ_FOREACH(child, &dev->children, link) {
3695 device_shutdown(child);
3702 * @brief Default function for suspending a child device.
3704 * This function is to be used by a bus's DEVICE_SUSPEND_CHILD().
3707 bus_generic_suspend_child(device_t dev, device_t child)
3711 error = DEVICE_SUSPEND(child);
3714 child->flags |= DF_SUSPENDED;
3720 * @brief Default function for resuming a child device.
3722 * This function is to be used by a bus's DEVICE_RESUME_CHILD().
3725 bus_generic_resume_child(device_t dev, device_t child)
3728 DEVICE_RESUME(child);
3729 child->flags &= ~DF_SUSPENDED;
3735 * @brief Helper function for implementing DEVICE_SUSPEND()
3737 * This function can be used to help implement the DEVICE_SUSPEND()
3738 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3739 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3740 * operation is aborted and any devices which were suspended are
3741 * resumed immediately by calling their DEVICE_RESUME() methods.
3744 bus_generic_suspend(device_t dev)
3747 device_t child, child2;
3749 TAILQ_FOREACH(child, &dev->children, link) {
3750 error = BUS_SUSPEND_CHILD(dev, child);
3752 for (child2 = TAILQ_FIRST(&dev->children);
3753 child2 && child2 != child;
3754 child2 = TAILQ_NEXT(child2, link))
3755 BUS_RESUME_CHILD(dev, child2);
3763 * @brief Helper function for implementing DEVICE_RESUME()
3765 * This function can be used to help implement the DEVICE_RESUME() for
3766 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3769 bus_generic_resume(device_t dev)
3773 TAILQ_FOREACH(child, &dev->children, link) {
3774 BUS_RESUME_CHILD(dev, child);
3775 /* if resume fails, there's nothing we can usefully do... */
3781 * @brief Helper function for implementing BUS_PRINT_CHILD().
3783 * This function prints the first part of the ascii representation of
3784 * @p child, including its name, unit and description (if any - see
3785 * device_set_desc()).
3787 * @returns the number of characters printed
3790 bus_print_child_header(device_t dev, device_t child)
3794 if (device_get_desc(child)) {
3795 retval += device_printf(child, "<%s>", device_get_desc(child));
3797 retval += printf("%s", device_get_nameunit(child));
3804 * @brief Helper function for implementing BUS_PRINT_CHILD().
3806 * This function prints the last part of the ascii representation of
3807 * @p child, which consists of the string @c " on " followed by the
3808 * name and unit of the @p dev.
3810 * @returns the number of characters printed
3813 bus_print_child_footer(device_t dev, device_t child)
3815 return (printf(" on %s\n", device_get_nameunit(dev)));
3819 * @brief Helper function for implementing BUS_PRINT_CHILD().
3821 * This function prints out the VM domain for the given device.
3823 * @returns the number of characters printed
3826 bus_print_child_domain(device_t dev, device_t child)
3830 /* No domain? Don't print anything */
3831 if (BUS_GET_DOMAIN(dev, child, &domain) != 0)
3834 return (printf(" numa-domain %d", domain));
3838 * @brief Helper function for implementing BUS_PRINT_CHILD().
3840 * This function simply calls bus_print_child_header() followed by
3841 * bus_print_child_footer().
3843 * @returns the number of characters printed
3846 bus_generic_print_child(device_t dev, device_t child)
3850 retval += bus_print_child_header(dev, child);
3851 retval += bus_print_child_domain(dev, child);
3852 retval += bus_print_child_footer(dev, child);
3858 * @brief Stub function for implementing BUS_READ_IVAR().
3863 bus_generic_read_ivar(device_t dev, device_t child, int index,
3870 * @brief Stub function for implementing BUS_WRITE_IVAR().
3875 bus_generic_write_ivar(device_t dev, device_t child, int index,
3882 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3886 struct resource_list *
3887 bus_generic_get_resource_list(device_t dev, device_t child)
3893 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3895 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3896 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3897 * and then calls device_probe_and_attach() for each unattached child.
3900 bus_generic_driver_added(device_t dev, driver_t *driver)
3904 DEVICE_IDENTIFY(driver, dev);
3905 TAILQ_FOREACH(child, &dev->children, link) {
3906 if (child->state == DS_NOTPRESENT ||
3907 (child->flags & DF_REBID))
3908 device_probe_and_attach(child);
3913 * @brief Helper function for implementing BUS_NEW_PASS().
3915 * This implementing of BUS_NEW_PASS() first calls the identify
3916 * routines for any drivers that probe at the current pass. Then it
3917 * walks the list of devices for this bus. If a device is already
3918 * attached, then it calls BUS_NEW_PASS() on that device. If the
3919 * device is not already attached, it attempts to attach a driver to
3923 bus_generic_new_pass(device_t dev)
3930 TAILQ_FOREACH(dl, &dc->drivers, link) {
3931 if (dl->pass == bus_current_pass)
3932 DEVICE_IDENTIFY(dl->driver, dev);
3934 TAILQ_FOREACH(child, &dev->children, link) {
3935 if (child->state >= DS_ATTACHED)
3936 BUS_NEW_PASS(child);
3937 else if (child->state == DS_NOTPRESENT)
3938 device_probe_and_attach(child);
3943 * @brief Helper function for implementing BUS_SETUP_INTR().
3945 * This simple implementation of BUS_SETUP_INTR() simply calls the
3946 * BUS_SETUP_INTR() method of the parent of @p dev.
3949 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3950 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3953 /* Propagate up the bus hierarchy until someone handles it. */
3955 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3956 filter, intr, arg, cookiep));
3961 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3963 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3964 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3967 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3970 /* Propagate up the bus hierarchy until someone handles it. */
3972 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3977 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3979 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3980 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3983 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3984 struct resource *r, rman_res_t start, rman_res_t end)
3986 /* Propagate up the bus hierarchy until someone handles it. */
3988 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3994 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3996 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3997 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
4000 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
4001 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4003 /* Propagate up the bus hierarchy until someone handles it. */
4005 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
4006 start, end, count, flags));
4011 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4013 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
4014 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
4017 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
4020 /* Propagate up the bus hierarchy until someone handles it. */
4022 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
4028 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
4030 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
4031 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
4034 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
4037 /* Propagate up the bus hierarchy until someone handles it. */
4039 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
4045 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
4047 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
4048 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
4051 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
4052 int rid, struct resource *r)
4054 /* Propagate up the bus hierarchy until someone handles it. */
4056 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
4062 * @brief Helper function for implementing BUS_BIND_INTR().
4064 * This simple implementation of BUS_BIND_INTR() simply calls the
4065 * BUS_BIND_INTR() method of the parent of @p dev.
4068 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
4072 /* Propagate up the bus hierarchy until someone handles it. */
4074 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
4079 * @brief Helper function for implementing BUS_CONFIG_INTR().
4081 * This simple implementation of BUS_CONFIG_INTR() simply calls the
4082 * BUS_CONFIG_INTR() method of the parent of @p dev.
4085 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
4086 enum intr_polarity pol)
4089 /* Propagate up the bus hierarchy until someone handles it. */
4091 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
4096 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
4098 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
4099 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
4102 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
4103 void *cookie, const char *descr)
4106 /* Propagate up the bus hierarchy until someone handles it. */
4108 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
4114 * @brief Helper function for implementing BUS_GET_DMA_TAG().
4116 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
4117 * BUS_GET_DMA_TAG() method of the parent of @p dev.
4120 bus_generic_get_dma_tag(device_t dev, device_t child)
4123 /* Propagate up the bus hierarchy until someone handles it. */
4124 if (dev->parent != NULL)
4125 return (BUS_GET_DMA_TAG(dev->parent, child));
4130 * @brief Helper function for implementing BUS_GET_BUS_TAG().
4132 * This simple implementation of BUS_GET_BUS_TAG() simply calls the
4133 * BUS_GET_BUS_TAG() method of the parent of @p dev.
4136 bus_generic_get_bus_tag(device_t dev, device_t child)
4139 /* Propagate up the bus hierarchy until someone handles it. */
4140 if (dev->parent != NULL)
4141 return (BUS_GET_BUS_TAG(dev->parent, child));
4142 return ((bus_space_tag_t)0);
4146 * @brief Helper function for implementing BUS_GET_RESOURCE().
4148 * This implementation of BUS_GET_RESOURCE() uses the
4149 * resource_list_find() function to do most of the work. It calls
4150 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4154 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
4155 rman_res_t *startp, rman_res_t *countp)
4157 struct resource_list * rl = NULL;
4158 struct resource_list_entry * rle = NULL;
4160 rl = BUS_GET_RESOURCE_LIST(dev, child);
4164 rle = resource_list_find(rl, type, rid);
4169 *startp = rle->start;
4171 *countp = rle->count;
4177 * @brief Helper function for implementing BUS_SET_RESOURCE().
4179 * This implementation of BUS_SET_RESOURCE() uses the
4180 * resource_list_add() function to do most of the work. It calls
4181 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4185 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
4186 rman_res_t start, rman_res_t count)
4188 struct resource_list * rl = NULL;
4190 rl = BUS_GET_RESOURCE_LIST(dev, child);
4194 resource_list_add(rl, type, rid, start, (start + count - 1), count);
4200 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
4202 * This implementation of BUS_DELETE_RESOURCE() uses the
4203 * resource_list_delete() function to do most of the work. It calls
4204 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
4208 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
4210 struct resource_list * rl = NULL;
4212 rl = BUS_GET_RESOURCE_LIST(dev, child);
4216 resource_list_delete(rl, type, rid);
4222 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
4224 * This implementation of BUS_RELEASE_RESOURCE() uses the
4225 * resource_list_release() function to do most of the work. It calls
4226 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4229 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
4230 int rid, struct resource *r)
4232 struct resource_list * rl = NULL;
4234 if (device_get_parent(child) != dev)
4235 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
4238 rl = BUS_GET_RESOURCE_LIST(dev, child);
4242 return (resource_list_release(rl, dev, child, type, rid, r));
4246 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4248 * This implementation of BUS_ALLOC_RESOURCE() uses the
4249 * resource_list_alloc() function to do most of the work. It calls
4250 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4253 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4254 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
4256 struct resource_list * rl = NULL;
4258 if (device_get_parent(child) != dev)
4259 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4260 type, rid, start, end, count, flags));
4262 rl = BUS_GET_RESOURCE_LIST(dev, child);
4266 return (resource_list_alloc(rl, dev, child, type, rid,
4267 start, end, count, flags));
4271 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4273 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4274 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4277 bus_generic_child_present(device_t dev, device_t child)
4279 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4283 bus_generic_get_domain(device_t dev, device_t child, int *domain)
4287 return (BUS_GET_DOMAIN(dev->parent, dev, domain));
4293 * Some convenience functions to make it easier for drivers to use the
4294 * resource-management functions. All these really do is hide the
4295 * indirection through the parent's method table, making for slightly
4296 * less-wordy code. In the future, it might make sense for this code
4297 * to maintain some sort of a list of resources allocated by each device.
4301 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4302 struct resource **res)
4306 for (i = 0; rs[i].type != -1; i++)
4308 for (i = 0; rs[i].type != -1; i++) {
4309 res[i] = bus_alloc_resource_any(dev,
4310 rs[i].type, &rs[i].rid, rs[i].flags);
4311 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4312 bus_release_resources(dev, rs, res);
4320 bus_release_resources(device_t dev, const struct resource_spec *rs,
4321 struct resource **res)
4325 for (i = 0; rs[i].type != -1; i++)
4326 if (res[i] != NULL) {
4327 bus_release_resource(
4328 dev, rs[i].type, rs[i].rid, res[i]);
4334 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4336 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4340 bus_alloc_resource(device_t dev, int type, int *rid, rman_res_t start, rman_res_t end,
4341 rman_res_t count, u_int flags)
4343 if (dev->parent == NULL)
4345 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4350 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4352 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4356 bus_adjust_resource(device_t dev, int type, struct resource *r, rman_res_t start,
4359 if (dev->parent == NULL)
4361 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4365 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4367 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4371 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4373 if (dev->parent == NULL)
4375 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4379 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4381 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4385 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4387 if (dev->parent == NULL)
4389 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4393 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4395 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4399 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4401 if (dev->parent == NULL)
4403 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4407 * @brief Wrapper function for BUS_SETUP_INTR().
4409 * This function simply calls the BUS_SETUP_INTR() method of the
4413 bus_setup_intr(device_t dev, struct resource *r, int flags,
4414 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4418 if (dev->parent == NULL)
4420 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4424 if (handler != NULL && !(flags & INTR_MPSAFE))
4425 device_printf(dev, "[GIANT-LOCKED]\n");
4430 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4432 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4436 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4438 if (dev->parent == NULL)
4440 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4444 * @brief Wrapper function for BUS_BIND_INTR().
4446 * This function simply calls the BUS_BIND_INTR() method of the
4450 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4452 if (dev->parent == NULL)
4454 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4458 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4460 * This function first formats the requested description into a
4461 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4462 * the parent of @p dev.
4465 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4466 const char *fmt, ...)
4469 char descr[MAXCOMLEN + 1];
4471 if (dev->parent == NULL)
4474 vsnprintf(descr, sizeof(descr), fmt, ap);
4476 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4480 * @brief Wrapper function for BUS_SET_RESOURCE().
4482 * This function simply calls the BUS_SET_RESOURCE() method of the
4486 bus_set_resource(device_t dev, int type, int rid,
4487 rman_res_t start, rman_res_t count)
4489 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4494 * @brief Wrapper function for BUS_GET_RESOURCE().
4496 * This function simply calls the BUS_GET_RESOURCE() method of the
4500 bus_get_resource(device_t dev, int type, int rid,
4501 rman_res_t *startp, rman_res_t *countp)
4503 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4508 * @brief Wrapper function for BUS_GET_RESOURCE().
4510 * This function simply calls the BUS_GET_RESOURCE() method of the
4511 * parent of @p dev and returns the start value.
4514 bus_get_resource_start(device_t dev, int type, int rid)
4520 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4528 * @brief Wrapper function for BUS_GET_RESOURCE().
4530 * This function simply calls the BUS_GET_RESOURCE() method of the
4531 * parent of @p dev and returns the count value.
4534 bus_get_resource_count(device_t dev, int type, int rid)
4540 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4548 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4550 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4554 bus_delete_resource(device_t dev, int type, int rid)
4556 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4560 * @brief Wrapper function for BUS_CHILD_PRESENT().
4562 * This function simply calls the BUS_CHILD_PRESENT() method of the
4566 bus_child_present(device_t child)
4568 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4572 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4574 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4578 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4582 parent = device_get_parent(child);
4583 if (parent == NULL) {
4587 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4591 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4593 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4597 bus_child_location_str(device_t child, char *buf, size_t buflen)
4601 parent = device_get_parent(child);
4602 if (parent == NULL) {
4606 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4610 * @brief Wrapper function for BUS_GET_DMA_TAG().
4612 * This function simply calls the BUS_GET_DMA_TAG() method of the
4616 bus_get_dma_tag(device_t dev)
4620 parent = device_get_parent(dev);
4623 return (BUS_GET_DMA_TAG(parent, dev));
4627 * @brief Wrapper function for BUS_GET_BUS_TAG().
4629 * This function simply calls the BUS_GET_BUS_TAG() method of the
4633 bus_get_bus_tag(device_t dev)
4637 parent = device_get_parent(dev);
4639 return ((bus_space_tag_t)0);
4640 return (BUS_GET_BUS_TAG(parent, dev));
4644 * @brief Wrapper function for BUS_GET_DOMAIN().
4646 * This function simply calls the BUS_GET_DOMAIN() method of the
4650 bus_get_domain(device_t dev, int *domain)
4652 return (BUS_GET_DOMAIN(device_get_parent(dev), dev, domain));
4655 /* Resume all devices and then notify userland that we're up again. */
4657 root_resume(device_t dev)
4661 error = bus_generic_resume(dev);
4663 devctl_notify("kern", "power", "resume", NULL);
4668 root_print_child(device_t dev, device_t child)
4672 retval += bus_print_child_header(dev, child);
4673 retval += printf("\n");
4679 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4680 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4683 * If an interrupt mapping gets to here something bad has happened.
4685 panic("root_setup_intr");
4689 * If we get here, assume that the device is permanant and really is
4690 * present in the system. Removable bus drivers are expected to intercept
4691 * this call long before it gets here. We return -1 so that drivers that
4692 * really care can check vs -1 or some ERRNO returned higher in the food
4696 root_child_present(device_t dev, device_t child)
4701 static kobj_method_t root_methods[] = {
4702 /* Device interface */
4703 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4704 KOBJMETHOD(device_suspend, bus_generic_suspend),
4705 KOBJMETHOD(device_resume, root_resume),
4708 KOBJMETHOD(bus_print_child, root_print_child),
4709 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4710 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4711 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4712 KOBJMETHOD(bus_child_present, root_child_present),
4717 static driver_t root_driver = {
4724 devclass_t root_devclass;
4727 root_bus_module_handler(module_t mod, int what, void* arg)
4731 TAILQ_INIT(&bus_data_devices);
4732 kobj_class_compile((kobj_class_t) &root_driver);
4733 root_bus = make_device(NULL, "root", 0);
4734 root_bus->desc = "System root bus";
4735 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4736 root_bus->driver = &root_driver;
4737 root_bus->state = DS_ATTACHED;
4738 root_devclass = devclass_find_internal("root", NULL, FALSE);
4743 device_shutdown(root_bus);
4746 return (EOPNOTSUPP);
4752 static moduledata_t root_bus_mod = {
4754 root_bus_module_handler,
4757 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4760 * @brief Automatically configure devices
4762 * This function begins the autoconfiguration process by calling
4763 * device_probe_and_attach() for each child of the @c root0 device.
4766 root_bus_configure(void)
4771 /* Eventually this will be split up, but this is sufficient for now. */
4772 bus_set_pass(BUS_PASS_DEFAULT);
4776 * @brief Module handler for registering device drivers
4778 * This module handler is used to automatically register device
4779 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4780 * devclass_add_driver() for the driver described by the
4781 * driver_module_data structure pointed to by @p arg
4784 driver_module_handler(module_t mod, int what, void *arg)
4786 struct driver_module_data *dmd;
4787 devclass_t bus_devclass;
4788 kobj_class_t driver;
4791 dmd = (struct driver_module_data *)arg;
4792 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4797 if (dmd->dmd_chainevh)
4798 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4800 pass = dmd->dmd_pass;
4801 driver = dmd->dmd_driver;
4802 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4803 DRIVERNAME(driver), dmd->dmd_busname, pass));
4804 error = devclass_add_driver(bus_devclass, driver, pass,
4809 PDEBUG(("Unloading module: driver %s from bus %s",
4810 DRIVERNAME(dmd->dmd_driver),
4812 error = devclass_delete_driver(bus_devclass,
4815 if (!error && dmd->dmd_chainevh)
4816 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4819 PDEBUG(("Quiesce module: driver %s from bus %s",
4820 DRIVERNAME(dmd->dmd_driver),
4822 error = devclass_quiesce_driver(bus_devclass,
4825 if (!error && dmd->dmd_chainevh)
4826 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4837 * @brief Enumerate all hinted devices for this bus.
4839 * Walks through the hints for this bus and calls the bus_hinted_child
4840 * routine for each one it fines. It searches first for the specific
4841 * bus that's being probed for hinted children (eg isa0), and then for
4842 * generic children (eg isa).
4844 * @param dev bus device to enumerate
4847 bus_enumerate_hinted_children(device_t bus)
4850 const char *dname, *busname;
4854 * enumerate all devices on the specific bus
4856 busname = device_get_nameunit(bus);
4858 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4859 BUS_HINTED_CHILD(bus, dname, dunit);
4862 * and all the generic ones.
4864 busname = device_get_name(bus);
4866 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4867 BUS_HINTED_CHILD(bus, dname, dunit);
4872 /* the _short versions avoid iteration by not calling anything that prints
4873 * more than oneliners. I love oneliners.
4877 print_device_short(device_t dev, int indent)
4882 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4883 dev->unit, dev->desc,
4884 (dev->parent? "":"no "),
4885 (TAILQ_EMPTY(&dev->children)? "no ":""),
4886 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4887 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4888 (dev->flags&DF_WILDCARD? "wildcard,":""),
4889 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4890 (dev->flags&DF_REBID? "rebiddable,":""),
4891 (dev->ivars? "":"no "),
4892 (dev->softc? "":"no "),
4897 print_device(device_t dev, int indent)
4902 print_device_short(dev, indent);
4904 indentprintf(("Parent:\n"));
4905 print_device_short(dev->parent, indent+1);
4906 indentprintf(("Driver:\n"));
4907 print_driver_short(dev->driver, indent+1);
4908 indentprintf(("Devclass:\n"));
4909 print_devclass_short(dev->devclass, indent+1);
4913 print_device_tree_short(device_t dev, int indent)
4914 /* print the device and all its children (indented) */
4921 print_device_short(dev, indent);
4923 TAILQ_FOREACH(child, &dev->children, link) {
4924 print_device_tree_short(child, indent+1);
4929 print_device_tree(device_t dev, int indent)
4930 /* print the device and all its children (indented) */
4937 print_device(dev, indent);
4939 TAILQ_FOREACH(child, &dev->children, link) {
4940 print_device_tree(child, indent+1);
4945 print_driver_short(driver_t *driver, int indent)
4950 indentprintf(("driver %s: softc size = %zd\n",
4951 driver->name, driver->size));
4955 print_driver(driver_t *driver, int indent)
4960 print_driver_short(driver, indent);
4964 print_driver_list(driver_list_t drivers, int indent)
4966 driverlink_t driver;
4968 TAILQ_FOREACH(driver, &drivers, link) {
4969 print_driver(driver->driver, indent);
4974 print_devclass_short(devclass_t dc, int indent)
4979 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4983 print_devclass(devclass_t dc, int indent)
4990 print_devclass_short(dc, indent);
4991 indentprintf(("Drivers:\n"));
4992 print_driver_list(dc->drivers, indent+1);
4994 indentprintf(("Devices:\n"));
4995 for (i = 0; i < dc->maxunit; i++)
4997 print_device(dc->devices[i], indent+1);
5001 print_devclass_list_short(void)
5005 printf("Short listing of devclasses, drivers & devices:\n");
5006 TAILQ_FOREACH(dc, &devclasses, link) {
5007 print_devclass_short(dc, 0);
5012 print_devclass_list(void)
5016 printf("Full listing of devclasses, drivers & devices:\n");
5017 TAILQ_FOREACH(dc, &devclasses, link) {
5018 print_devclass(dc, 0);
5025 * User-space access to the device tree.
5027 * We implement a small set of nodes:
5029 * hw.bus Single integer read method to obtain the
5030 * current generation count.
5031 * hw.bus.devices Reads the entire device tree in flat space.
5032 * hw.bus.rman Resource manager interface
5034 * We might like to add the ability to scan devclasses and/or drivers to
5035 * determine what else is currently loaded/available.
5039 sysctl_bus(SYSCTL_HANDLER_ARGS)
5041 struct u_businfo ubus;
5043 ubus.ub_version = BUS_USER_VERSION;
5044 ubus.ub_generation = bus_data_generation;
5046 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
5048 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
5049 "bus-related data");
5052 sysctl_devices(SYSCTL_HANDLER_ARGS)
5054 int *name = (int *)arg1;
5055 u_int namelen = arg2;
5058 struct u_device udev; /* XXX this is a bit big */
5064 if (bus_data_generation_check(name[0]))
5070 * Scan the list of devices, looking for the requested index.
5072 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5080 * Populate the return array.
5082 bzero(&udev, sizeof(udev));
5083 udev.dv_handle = (uintptr_t)dev;
5084 udev.dv_parent = (uintptr_t)dev->parent;
5085 if (dev->nameunit != NULL)
5086 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
5087 if (dev->desc != NULL)
5088 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
5089 if (dev->driver != NULL && dev->driver->name != NULL)
5090 strlcpy(udev.dv_drivername, dev->driver->name,
5091 sizeof(udev.dv_drivername));
5092 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
5093 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
5094 udev.dv_devflags = dev->devflags;
5095 udev.dv_flags = dev->flags;
5096 udev.dv_state = dev->state;
5097 error = SYSCTL_OUT(req, &udev, sizeof(udev));
5101 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
5102 "system device tree");
5105 bus_data_generation_check(int generation)
5107 if (generation != bus_data_generation)
5110 /* XXX generate optimised lists here? */
5115 bus_data_generation_update(void)
5117 bus_data_generation++;
5121 bus_free_resource(device_t dev, int type, struct resource *r)
5125 return (bus_release_resource(dev, type, rman_get_rid(r), r));
5129 device_lookup_by_name(const char *name)
5133 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
5134 if (dev->nameunit != NULL && strcmp(dev->nameunit, name) == 0)
5141 * /dev/devctl2 implementation. The existing /dev/devctl device has
5142 * implicit semantics on open, so it could not be reused for this.
5143 * Another option would be to call this /dev/bus?
5146 find_device(struct devreq *req, device_t *devp)
5151 * First, ensure that the name is nul terminated.
5153 if (memchr(req->dr_name, '\0', sizeof(req->dr_name)) == NULL)
5157 * Second, try to find an attached device whose name matches
5160 dev = device_lookup_by_name(req->dr_name);
5166 /* Finally, give device enumerators a chance. */
5168 EVENTHANDLER_INVOKE(dev_lookup, req->dr_name, &dev);
5176 driver_exists(struct device *bus, const char *driver)
5180 for (dc = bus->devclass; dc != NULL; dc = dc->parent) {
5181 if (devclass_find_driver_internal(dc, driver) != NULL)
5188 devctl2_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag,
5195 /* Locate the device to control. */
5197 req = (struct devreq *)data;
5205 case DEV_SET_DRIVER:
5206 error = priv_check(td, PRIV_DRIVER);
5208 error = find_device(req, &dev);
5219 /* Perform the requested operation. */
5222 if (device_is_attached(dev) && (dev->flags & DF_REBID) == 0)
5224 else if (!device_is_enabled(dev))
5227 error = device_probe_and_attach(dev);
5230 if (!device_is_attached(dev)) {
5234 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5235 error = device_quiesce(dev);
5239 error = device_detach(dev);
5242 if (device_is_enabled(dev)) {
5248 * If the device has been probed but not attached (e.g.
5249 * when it has been disabled by a loader hint), just
5250 * attach the device rather than doing a full probe.
5253 if (device_is_alive(dev)) {
5255 * If the device was disabled via a hint, clear
5258 if (resource_disabled(dev->driver->name, dev->unit))
5259 resource_unset_value(dev->driver->name,
5260 dev->unit, "disabled");
5261 error = device_attach(dev);
5263 error = device_probe_and_attach(dev);
5266 if (!device_is_enabled(dev)) {
5271 if (!(req->dr_flags & DEVF_FORCE_DETACH)) {
5272 error = device_quiesce(dev);
5278 * Force DF_FIXEDCLASS on around detach to preserve
5279 * the existing name.
5282 dev->flags |= DF_FIXEDCLASS;
5283 error = device_detach(dev);
5284 if (!(old & DF_FIXEDCLASS))
5285 dev->flags &= ~DF_FIXEDCLASS;
5287 device_disable(dev);
5290 if (device_is_suspended(dev)) {
5294 if (device_get_parent(dev) == NULL) {
5298 error = BUS_SUSPEND_CHILD(device_get_parent(dev), dev);
5301 if (!device_is_suspended(dev)) {
5305 if (device_get_parent(dev) == NULL) {
5309 error = BUS_RESUME_CHILD(device_get_parent(dev), dev);
5311 case DEV_SET_DRIVER: {
5315 error = copyinstr(req->dr_data, driver, sizeof(driver), NULL);
5318 if (driver[0] == '\0') {
5322 if (dev->devclass != NULL &&
5323 strcmp(driver, dev->devclass->name) == 0)
5324 /* XXX: Could possibly force DF_FIXEDCLASS on? */
5328 * Scan drivers for this device's bus looking for at
5329 * least one matching driver.
5331 if (dev->parent == NULL) {
5335 if (!driver_exists(dev->parent, driver)) {
5339 dc = devclass_create(driver);
5345 /* Detach device if necessary. */
5346 if (device_is_attached(dev)) {
5347 if (req->dr_flags & DEVF_SET_DRIVER_DETACH)
5348 error = device_detach(dev);
5355 /* Clear any previously-fixed device class and unit. */
5356 if (dev->flags & DF_FIXEDCLASS)
5357 devclass_delete_device(dev->devclass, dev);
5358 dev->flags |= DF_WILDCARD;
5361 /* Force the new device class. */
5362 error = devclass_add_device(dc, dev);
5365 dev->flags |= DF_FIXEDCLASS;
5366 error = device_probe_and_attach(dev);
5374 static struct cdevsw devctl2_cdevsw = {
5375 .d_version = D_VERSION,
5376 .d_ioctl = devctl2_ioctl,
5377 .d_name = "devctl2",
5384 make_dev_credf(MAKEDEV_ETERNAL, &devctl2_cdevsw, 0, NULL,
5385 UID_ROOT, GID_WHEEL, 0600, "devctl2");