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>
44 #include <sys/condvar.h>
45 #include <sys/queue.h>
46 #include <machine/bus.h>
48 #include <sys/selinfo.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
54 #include <sys/interrupt.h>
58 #include <machine/stdarg.h>
62 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
63 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
66 * Used to attach drivers to devclasses.
68 typedef struct driverlink *driverlink_t;
71 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
73 TAILQ_ENTRY(driverlink) passlink;
77 * Forward declarations
79 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
80 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
81 typedef TAILQ_HEAD(device_list, device) device_list_t;
84 TAILQ_ENTRY(devclass) link;
85 devclass_t parent; /* parent in devclass hierarchy */
86 driver_list_t drivers; /* bus devclasses store drivers for bus */
88 device_t *devices; /* array of devices indexed by unit */
89 int maxunit; /* size of devices array */
91 #define DC_HAS_CHILDREN 1
93 struct sysctl_ctx_list sysctl_ctx;
94 struct sysctl_oid *sysctl_tree;
98 * @brief Implementation of device.
102 * A device is a kernel object. The first field must be the
103 * current ops table for the object.
110 TAILQ_ENTRY(device) link; /**< list of devices in parent */
111 TAILQ_ENTRY(device) devlink; /**< global device list membership */
112 device_t parent; /**< parent of this device */
113 device_list_t children; /**< list of child devices */
116 * Details of this device.
118 driver_t *driver; /**< current driver */
119 devclass_t devclass; /**< current device class */
120 int unit; /**< current unit number */
121 char* nameunit; /**< name+unit e.g. foodev0 */
122 char* desc; /**< driver specific description */
123 int busy; /**< count of calls to device_busy() */
124 device_state_t state; /**< current device state */
125 uint32_t devflags; /**< api level flags for device_get_flags() */
126 u_int flags; /**< internal device flags */
127 #define DF_ENABLED 0x01 /* device should be probed/attached */
128 #define DF_FIXEDCLASS 0x02 /* devclass specified at create time */
129 #define DF_WILDCARD 0x04 /* unit was originally wildcard */
130 #define DF_DESCMALLOCED 0x08 /* description was malloced */
131 #define DF_QUIET 0x10 /* don't print verbose attach message */
132 #define DF_DONENOMATCH 0x20 /* don't execute DEVICE_NOMATCH again */
133 #define DF_EXTERNALSOFTC 0x40 /* softc not allocated by us */
134 #define DF_REBID 0x80 /* Can rebid after attach */
135 u_int order; /**< order from device_add_child_ordered() */
136 void *ivars; /**< instance variables */
137 void *softc; /**< current driver's variables */
139 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
140 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
143 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
144 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
148 static int bus_debug = 1;
149 TUNABLE_INT("bus.debug", &bus_debug);
150 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
153 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
154 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
155 #define DRIVERNAME(d) ((d)? d->name : "no driver")
156 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
159 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
160 * prevent syslog from deleting initial spaces
162 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
164 static void print_device_short(device_t dev, int indent);
165 static void print_device(device_t dev, int indent);
166 void print_device_tree_short(device_t dev, int indent);
167 void print_device_tree(device_t dev, int indent);
168 static void print_driver_short(driver_t *driver, int indent);
169 static void print_driver(driver_t *driver, int indent);
170 static void print_driver_list(driver_list_t drivers, int indent);
171 static void print_devclass_short(devclass_t dc, int indent);
172 static void print_devclass(devclass_t dc, int indent);
173 void print_devclass_list_short(void);
174 void print_devclass_list(void);
177 /* Make the compiler ignore the function calls */
178 #define PDEBUG(a) /* nop */
179 #define DEVICENAME(d) /* nop */
180 #define DRIVERNAME(d) /* nop */
181 #define DEVCLANAME(d) /* nop */
183 #define print_device_short(d,i) /* nop */
184 #define print_device(d,i) /* nop */
185 #define print_device_tree_short(d,i) /* nop */
186 #define print_device_tree(d,i) /* nop */
187 #define print_driver_short(d,i) /* nop */
188 #define print_driver(d,i) /* nop */
189 #define print_driver_list(d,i) /* nop */
190 #define print_devclass_short(d,i) /* nop */
191 #define print_devclass(d,i) /* nop */
192 #define print_devclass_list_short() /* nop */
193 #define print_devclass_list() /* nop */
201 DEVCLASS_SYSCTL_PARENT,
205 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
207 devclass_t dc = (devclass_t)arg1;
211 case DEVCLASS_SYSCTL_PARENT:
212 value = dc->parent ? dc->parent->name : "";
217 return (SYSCTL_OUT(req, value, strlen(value)));
221 devclass_sysctl_init(devclass_t dc)
224 if (dc->sysctl_tree != NULL)
226 sysctl_ctx_init(&dc->sysctl_ctx);
227 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
228 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
229 CTLFLAG_RD, NULL, "");
230 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
231 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
232 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
238 DEVICE_SYSCTL_DRIVER,
239 DEVICE_SYSCTL_LOCATION,
240 DEVICE_SYSCTL_PNPINFO,
241 DEVICE_SYSCTL_PARENT,
245 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
247 device_t dev = (device_t)arg1;
254 case DEVICE_SYSCTL_DESC:
255 value = dev->desc ? dev->desc : "";
257 case DEVICE_SYSCTL_DRIVER:
258 value = dev->driver ? dev->driver->name : "";
260 case DEVICE_SYSCTL_LOCATION:
261 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
262 bus_child_location_str(dev, buf, 1024);
264 case DEVICE_SYSCTL_PNPINFO:
265 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
266 bus_child_pnpinfo_str(dev, buf, 1024);
268 case DEVICE_SYSCTL_PARENT:
269 value = dev->parent ? dev->parent->nameunit : "";
274 error = SYSCTL_OUT(req, value, strlen(value));
281 device_sysctl_init(device_t dev)
283 devclass_t dc = dev->devclass;
285 if (dev->sysctl_tree != NULL)
287 devclass_sysctl_init(dc);
288 sysctl_ctx_init(&dev->sysctl_ctx);
289 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
290 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
291 dev->nameunit + strlen(dc->name),
292 CTLFLAG_RD, NULL, "");
293 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
294 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
295 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
296 "device description");
297 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
298 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
299 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
300 "device driver name");
301 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
302 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
303 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
304 "device location relative to parent");
305 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
306 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
307 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
308 "device identification");
309 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
310 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
311 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
316 device_sysctl_update(device_t dev)
318 devclass_t dc = dev->devclass;
320 if (dev->sysctl_tree == NULL)
322 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
326 device_sysctl_fini(device_t dev)
328 if (dev->sysctl_tree == NULL)
330 sysctl_ctx_free(&dev->sysctl_ctx);
331 dev->sysctl_tree = NULL;
335 * /dev/devctl implementation
339 * This design allows only one reader for /dev/devctl. This is not desirable
340 * in the long run, but will get a lot of hair out of this implementation.
341 * Maybe we should make this device a clonable device.
343 * Also note: we specifically do not attach a device to the device_t tree
344 * to avoid potential chicken and egg problems. One could argue that all
345 * of this belongs to the root node. One could also further argue that the
346 * sysctl interface that we have not might more properly be an ioctl
347 * interface, but at this stage of the game, I'm not inclined to rock that
350 * I'm also not sure that the SIGIO support is done correctly or not, as
351 * I copied it from a driver that had SIGIO support that likely hasn't been
352 * tested since 3.4 or 2.2.8!
355 /* Deprecated way to adjust queue length */
356 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
357 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
358 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
359 0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
361 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
362 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
363 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
364 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
365 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
366 0, sysctl_devctl_queue, "I", "devctl queue length");
368 static d_open_t devopen;
369 static d_close_t devclose;
370 static d_read_t devread;
371 static d_ioctl_t devioctl;
372 static d_poll_t devpoll;
374 static struct cdevsw dev_cdevsw = {
375 .d_version = D_VERSION,
376 .d_flags = D_NEEDGIANT,
385 struct dev_event_info
388 TAILQ_ENTRY(dev_event_info) dei_link;
391 TAILQ_HEAD(devq, dev_event_info);
393 static struct dev_softc
402 struct proc *async_proc;
405 static struct cdev *devctl_dev;
410 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
411 UID_ROOT, GID_WHEEL, 0600, "devctl");
412 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
413 cv_init(&devsoftc.cv, "dev cv");
414 TAILQ_INIT(&devsoftc.devq);
418 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
424 devsoftc.nonblock = 0;
425 devsoftc.async_proc = NULL;
430 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
433 mtx_lock(&devsoftc.mtx);
434 cv_broadcast(&devsoftc.cv);
435 mtx_unlock(&devsoftc.mtx);
436 devsoftc.async_proc = NULL;
441 * The read channel for this device is used to report changes to
442 * userland in realtime. We are required to free the data as well as
443 * the n1 object because we allocate them separately. Also note that
444 * we return one record at a time. If you try to read this device a
445 * character at a time, you will lose the rest of the data. Listening
446 * programs are expected to cope.
449 devread(struct cdev *dev, struct uio *uio, int ioflag)
451 struct dev_event_info *n1;
454 mtx_lock(&devsoftc.mtx);
455 while (TAILQ_EMPTY(&devsoftc.devq)) {
456 if (devsoftc.nonblock) {
457 mtx_unlock(&devsoftc.mtx);
460 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
463 * Need to translate ERESTART to EINTR here? -- jake
465 mtx_unlock(&devsoftc.mtx);
469 n1 = TAILQ_FIRST(&devsoftc.devq);
470 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
472 mtx_unlock(&devsoftc.mtx);
473 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
474 free(n1->dei_data, M_BUS);
480 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
486 devsoftc.nonblock = 1;
488 devsoftc.nonblock = 0;
492 devsoftc.async_proc = td->td_proc;
494 devsoftc.async_proc = NULL;
497 /* (un)Support for other fcntl() calls. */
510 devpoll(struct cdev *dev, int events, struct thread *td)
514 mtx_lock(&devsoftc.mtx);
515 if (events & (POLLIN | POLLRDNORM)) {
516 if (!TAILQ_EMPTY(&devsoftc.devq))
517 revents = events & (POLLIN | POLLRDNORM);
519 selrecord(td, &devsoftc.sel);
521 mtx_unlock(&devsoftc.mtx);
527 * @brief Return whether the userland process is running
530 devctl_process_running(void)
532 return (devsoftc.inuse == 1);
536 * @brief Queue data to be read from the devctl device
538 * Generic interface to queue data to the devctl device. It is
539 * assumed that @p data is properly formatted. It is further assumed
540 * that @p data is allocated using the M_BUS malloc type.
543 devctl_queue_data_f(char *data, int flags)
545 struct dev_event_info *n1 = NULL, *n2 = NULL;
548 if (strlen(data) == 0)
550 if (devctl_queue_length == 0)
552 n1 = malloc(sizeof(*n1), M_BUS, flags);
556 mtx_lock(&devsoftc.mtx);
557 if (devctl_queue_length == 0) {
558 mtx_unlock(&devsoftc.mtx);
559 free(n1->dei_data, M_BUS);
563 /* Leave at least one spot in the queue... */
564 while (devsoftc.queued > devctl_queue_length - 1) {
565 n2 = TAILQ_FIRST(&devsoftc.devq);
566 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
567 free(n2->dei_data, M_BUS);
571 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
573 cv_broadcast(&devsoftc.cv);
574 mtx_unlock(&devsoftc.mtx);
575 selwakeup(&devsoftc.sel);
576 p = devsoftc.async_proc;
579 kern_psignal(p, SIGIO);
585 * We have to free data on all error paths since the caller
586 * assumes it will be free'd when this item is dequeued.
593 devctl_queue_data(char *data)
596 devctl_queue_data_f(data, M_NOWAIT);
600 * @brief Send a 'notification' to userland, using standard ways
603 devctl_notify_f(const char *system, const char *subsystem, const char *type,
604 const char *data, int flags)
610 return; /* BOGUS! Must specify system. */
611 if (subsystem == NULL)
612 return; /* BOGUS! Must specify subsystem. */
614 return; /* BOGUS! Must specify type. */
615 len += strlen(" system=") + strlen(system);
616 len += strlen(" subsystem=") + strlen(subsystem);
617 len += strlen(" type=") + strlen(type);
618 /* add in the data message plus newline. */
621 len += 3; /* '!', '\n', and NUL */
622 msg = malloc(len, M_BUS, flags);
624 return; /* Drop it on the floor */
626 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
627 system, subsystem, type, data);
629 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
630 system, subsystem, type);
631 devctl_queue_data_f(msg, flags);
635 devctl_notify(const char *system, const char *subsystem, const char *type,
639 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
643 * Common routine that tries to make sending messages as easy as possible.
644 * We allocate memory for the data, copy strings into that, but do not
645 * free it unless there's an error. The dequeue part of the driver should
646 * free the data. We don't send data when the device is disabled. We do
647 * send data, even when we have no listeners, because we wish to avoid
648 * races relating to startup and restart of listening applications.
650 * devaddq is designed to string together the type of event, with the
651 * object of that event, plus the plug and play info and location info
652 * for that event. This is likely most useful for devices, but less
653 * useful for other consumers of this interface. Those should use
654 * the devctl_queue_data() interface instead.
657 devaddq(const char *type, const char *what, device_t dev)
664 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
666 data = malloc(1024, M_BUS, M_NOWAIT);
670 /* get the bus specific location of this device */
671 loc = malloc(1024, M_BUS, M_NOWAIT);
675 bus_child_location_str(dev, loc, 1024);
677 /* Get the bus specific pnp info of this device */
678 pnp = malloc(1024, M_BUS, M_NOWAIT);
682 bus_child_pnpinfo_str(dev, pnp, 1024);
684 /* Get the parent of this device, or / if high enough in the tree. */
685 if (device_get_parent(dev) == NULL)
686 parstr = "."; /* Or '/' ? */
688 parstr = device_get_nameunit(device_get_parent(dev));
689 /* String it all together. */
690 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
694 devctl_queue_data(data);
704 * A device was added to the tree. We are called just after it successfully
705 * attaches (that is, probe and attach success for this device). No call
706 * is made if a device is merely parented into the tree. See devnomatch
707 * if probe fails. If attach fails, no notification is sent (but maybe
708 * we should have a different message for this).
711 devadded(device_t dev)
713 devaddq("+", device_get_nameunit(dev), dev);
717 * A device was removed from the tree. We are called just before this
721 devremoved(device_t dev)
723 devaddq("-", device_get_nameunit(dev), dev);
727 * Called when there's no match for this device. This is only called
728 * the first time that no match happens, so we don't keep getting this
729 * message. Should that prove to be undesirable, we can change it.
730 * This is called when all drivers that can attach to a given bus
731 * decline to accept this device. Other errors may not be detected.
734 devnomatch(device_t dev)
736 devaddq("?", "", dev);
740 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
742 struct dev_event_info *n1;
745 dis = devctl_queue_length == 0;
746 error = sysctl_handle_int(oidp, &dis, 0, req);
747 if (error || !req->newptr)
749 mtx_lock(&devsoftc.mtx);
751 while (!TAILQ_EMPTY(&devsoftc.devq)) {
752 n1 = TAILQ_FIRST(&devsoftc.devq);
753 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
754 free(n1->dei_data, M_BUS);
758 devctl_queue_length = 0;
760 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
762 mtx_unlock(&devsoftc.mtx);
767 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
769 struct dev_event_info *n1;
772 q = devctl_queue_length;
773 error = sysctl_handle_int(oidp, &q, 0, req);
774 if (error || !req->newptr)
778 mtx_lock(&devsoftc.mtx);
779 devctl_queue_length = q;
780 while (devsoftc.queued > devctl_queue_length) {
781 n1 = TAILQ_FIRST(&devsoftc.devq);
782 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
783 free(n1->dei_data, M_BUS);
787 mtx_unlock(&devsoftc.mtx);
791 /* End of /dev/devctl code */
793 static TAILQ_HEAD(,device) bus_data_devices;
794 static int bus_data_generation = 1;
796 static kobj_method_t null_methods[] = {
800 DEFINE_CLASS(null, null_methods, 0);
803 * Bus pass implementation
806 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
807 int bus_current_pass = BUS_PASS_ROOT;
811 * @brief Register the pass level of a new driver attachment
813 * Register a new driver attachment's pass level. If no driver
814 * attachment with the same pass level has been added, then @p new
815 * will be added to the global passes list.
817 * @param new the new driver attachment
820 driver_register_pass(struct driverlink *new)
822 struct driverlink *dl;
824 /* We only consider pass numbers during boot. */
825 if (bus_current_pass == BUS_PASS_DEFAULT)
829 * Walk the passes list. If we already know about this pass
830 * then there is nothing to do. If we don't, then insert this
831 * driver link into the list.
833 TAILQ_FOREACH(dl, &passes, passlink) {
834 if (dl->pass < new->pass)
836 if (dl->pass == new->pass)
838 TAILQ_INSERT_BEFORE(dl, new, passlink);
841 TAILQ_INSERT_TAIL(&passes, new, passlink);
845 * @brief Raise the current bus pass
847 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
848 * method on the root bus to kick off a new device tree scan for each
849 * new pass level that has at least one driver.
852 bus_set_pass(int pass)
854 struct driverlink *dl;
856 if (bus_current_pass > pass)
857 panic("Attempt to lower bus pass level");
859 TAILQ_FOREACH(dl, &passes, passlink) {
860 /* Skip pass values below the current pass level. */
861 if (dl->pass <= bus_current_pass)
865 * Bail once we hit a driver with a pass level that is
872 * Raise the pass level to the next level and rescan
875 bus_current_pass = dl->pass;
876 BUS_NEW_PASS(root_bus);
880 * If there isn't a driver registered for the requested pass,
881 * then bus_current_pass might still be less than 'pass'. Set
882 * it to 'pass' in that case.
884 if (bus_current_pass < pass)
885 bus_current_pass = pass;
886 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
890 * Devclass implementation
893 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
897 * @brief Find or create a device class
899 * If a device class with the name @p classname exists, return it,
900 * otherwise if @p create is non-zero create and return a new device
903 * If @p parentname is non-NULL, the parent of the devclass is set to
904 * the devclass of that name.
906 * @param classname the devclass name to find or create
907 * @param parentname the parent devclass name or @c NULL
908 * @param create non-zero to create a devclass
911 devclass_find_internal(const char *classname, const char *parentname,
916 PDEBUG(("looking for %s", classname));
920 TAILQ_FOREACH(dc, &devclasses, link) {
921 if (!strcmp(dc->name, classname))
926 PDEBUG(("creating %s", classname));
927 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
928 M_BUS, M_NOWAIT | M_ZERO);
932 dc->name = (char*) (dc + 1);
933 strcpy(dc->name, classname);
934 TAILQ_INIT(&dc->drivers);
935 TAILQ_INSERT_TAIL(&devclasses, dc, link);
937 bus_data_generation_update();
941 * If a parent class is specified, then set that as our parent so
942 * that this devclass will support drivers for the parent class as
943 * well. If the parent class has the same name don't do this though
944 * as it creates a cycle that can trigger an infinite loop in
945 * device_probe_child() if a device exists for which there is no
948 if (parentname && dc && !dc->parent &&
949 strcmp(classname, parentname) != 0) {
950 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
951 dc->parent->flags |= DC_HAS_CHILDREN;
958 * @brief Create a device class
960 * If a device class with the name @p classname exists, return it,
961 * otherwise create and return a new device class.
963 * @param classname the devclass name to find or create
966 devclass_create(const char *classname)
968 return (devclass_find_internal(classname, NULL, TRUE));
972 * @brief Find a device class
974 * If a device class with the name @p classname exists, return it,
975 * otherwise return @c NULL.
977 * @param classname the devclass name to find
980 devclass_find(const char *classname)
982 return (devclass_find_internal(classname, NULL, FALSE));
986 * @brief Register that a device driver has been added to a devclass
988 * Register that a device driver has been added to a devclass. This
989 * is called by devclass_add_driver to accomplish the recursive
990 * notification of all the children classes of dc, as well as dc.
991 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
994 * We do a full search here of the devclass list at each iteration
995 * level to save storing children-lists in the devclass structure. If
996 * we ever move beyond a few dozen devices doing this, we may need to
999 * @param dc the devclass to edit
1000 * @param driver the driver that was just added
1003 devclass_driver_added(devclass_t dc, driver_t *driver)
1009 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1011 for (i = 0; i < dc->maxunit; i++)
1012 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1013 BUS_DRIVER_ADDED(dc->devices[i], driver);
1016 * Walk through the children classes. Since we only keep a
1017 * single parent pointer around, we walk the entire list of
1018 * devclasses looking for children. We set the
1019 * DC_HAS_CHILDREN flag when a child devclass is created on
1020 * the parent, so we only walk the list for those devclasses
1021 * that have children.
1023 if (!(dc->flags & DC_HAS_CHILDREN))
1026 TAILQ_FOREACH(dc, &devclasses, link) {
1027 if (dc->parent == parent)
1028 devclass_driver_added(dc, driver);
1033 * @brief Add a device driver to a device class
1035 * Add a device driver to a devclass. This is normally called
1036 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1037 * all devices in the devclass will be called to allow them to attempt
1038 * to re-probe any unmatched children.
1040 * @param dc the devclass to edit
1041 * @param driver the driver to register
1044 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1047 const char *parentname;
1049 PDEBUG(("%s", DRIVERNAME(driver)));
1051 /* Don't allow invalid pass values. */
1052 if (pass <= BUS_PASS_ROOT)
1055 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1060 * Compile the driver's methods. Also increase the reference count
1061 * so that the class doesn't get freed when the last instance
1062 * goes. This means we can safely use static methods and avoids a
1063 * double-free in devclass_delete_driver.
1065 kobj_class_compile((kobj_class_t) driver);
1068 * If the driver has any base classes, make the
1069 * devclass inherit from the devclass of the driver's
1070 * first base class. This will allow the system to
1071 * search for drivers in both devclasses for children
1072 * of a device using this driver.
1074 if (driver->baseclasses)
1075 parentname = driver->baseclasses[0]->name;
1078 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1080 dl->driver = driver;
1081 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1082 driver->refs++; /* XXX: kobj_mtx */
1084 driver_register_pass(dl);
1086 devclass_driver_added(dc, driver);
1087 bus_data_generation_update();
1092 * @brief Register that a device driver has been deleted from a devclass
1094 * Register that a device driver has been removed from a devclass.
1095 * This is called by devclass_delete_driver to accomplish the
1096 * recursive notification of all the children classes of busclass, as
1097 * well as busclass. Each layer will attempt to detach the driver
1098 * from any devices that are children of the bus's devclass. The function
1099 * will return an error if a device fails to detach.
1101 * We do a full search here of the devclass list at each iteration
1102 * level to save storing children-lists in the devclass structure. If
1103 * we ever move beyond a few dozen devices doing this, we may need to
1106 * @param busclass the devclass of the parent bus
1107 * @param dc the devclass of the driver being deleted
1108 * @param driver the driver being deleted
1111 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1118 * Disassociate from any devices. We iterate through all the
1119 * devices in the devclass of the driver and detach any which are
1120 * using the driver and which have a parent in the devclass which
1121 * we are deleting from.
1123 * Note that since a driver can be in multiple devclasses, we
1124 * should not detach devices which are not children of devices in
1125 * the affected devclass.
1127 for (i = 0; i < dc->maxunit; i++) {
1128 if (dc->devices[i]) {
1129 dev = dc->devices[i];
1130 if (dev->driver == driver && dev->parent &&
1131 dev->parent->devclass == busclass) {
1132 if ((error = device_detach(dev)) != 0)
1134 BUS_PROBE_NOMATCH(dev->parent, dev);
1136 dev->flags |= DF_DONENOMATCH;
1142 * Walk through the children classes. Since we only keep a
1143 * single parent pointer around, we walk the entire list of
1144 * devclasses looking for children. We set the
1145 * DC_HAS_CHILDREN flag when a child devclass is created on
1146 * the parent, so we only walk the list for those devclasses
1147 * that have children.
1149 if (!(busclass->flags & DC_HAS_CHILDREN))
1152 TAILQ_FOREACH(busclass, &devclasses, link) {
1153 if (busclass->parent == parent) {
1154 error = devclass_driver_deleted(busclass, dc, driver);
1163 * @brief Delete a device driver from a device class
1165 * Delete a device driver from a devclass. This is normally called
1166 * automatically by DRIVER_MODULE().
1168 * If the driver is currently attached to any devices,
1169 * devclass_delete_driver() will first attempt to detach from each
1170 * device. If one of the detach calls fails, the driver will not be
1173 * @param dc the devclass to edit
1174 * @param driver the driver to unregister
1177 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1179 devclass_t dc = devclass_find(driver->name);
1183 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1189 * Find the link structure in the bus' list of drivers.
1191 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1192 if (dl->driver == driver)
1197 PDEBUG(("%s not found in %s list", driver->name,
1202 error = devclass_driver_deleted(busclass, dc, driver);
1206 TAILQ_REMOVE(&busclass->drivers, dl, link);
1211 if (driver->refs == 0)
1212 kobj_class_free((kobj_class_t) driver);
1214 bus_data_generation_update();
1219 * @brief Quiesces a set of device drivers from a device class
1221 * Quiesce a device driver from a devclass. This is normally called
1222 * automatically by DRIVER_MODULE().
1224 * If the driver is currently attached to any devices,
1225 * devclass_quiesece_driver() will first attempt to quiesce each
1228 * @param dc the devclass to edit
1229 * @param driver the driver to unregister
1232 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1234 devclass_t dc = devclass_find(driver->name);
1240 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1246 * Find the link structure in the bus' list of drivers.
1248 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1249 if (dl->driver == driver)
1254 PDEBUG(("%s not found in %s list", driver->name,
1260 * Quiesce all devices. We iterate through all the devices in
1261 * the devclass of the driver and quiesce any which are using
1262 * the driver and which have a parent in the devclass which we
1265 * Note that since a driver can be in multiple devclasses, we
1266 * should not quiesce devices which are not children of
1267 * devices in the affected devclass.
1269 for (i = 0; i < dc->maxunit; i++) {
1270 if (dc->devices[i]) {
1271 dev = dc->devices[i];
1272 if (dev->driver == driver && dev->parent &&
1273 dev->parent->devclass == busclass) {
1274 if ((error = device_quiesce(dev)) != 0)
1287 devclass_find_driver_internal(devclass_t dc, const char *classname)
1291 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1293 TAILQ_FOREACH(dl, &dc->drivers, link) {
1294 if (!strcmp(dl->driver->name, classname))
1298 PDEBUG(("not found"));
1303 * @brief Return the name of the devclass
1306 devclass_get_name(devclass_t dc)
1312 * @brief Find a device given a unit number
1314 * @param dc the devclass to search
1315 * @param unit the unit number to search for
1317 * @returns the device with the given unit number or @c
1318 * NULL if there is no such device
1321 devclass_get_device(devclass_t dc, int unit)
1323 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1325 return (dc->devices[unit]);
1329 * @brief Find the softc field of a device given a unit number
1331 * @param dc the devclass to search
1332 * @param unit the unit number to search for
1334 * @returns the softc field of the device with the given
1335 * unit number or @c NULL if there is no such
1339 devclass_get_softc(devclass_t dc, int unit)
1343 dev = devclass_get_device(dc, unit);
1347 return (device_get_softc(dev));
1351 * @brief Get a list of devices in the devclass
1353 * An array containing a list of all the devices in the given devclass
1354 * is allocated and returned in @p *devlistp. The number of devices
1355 * in the array is returned in @p *devcountp. The caller should free
1356 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1358 * @param dc the devclass to examine
1359 * @param devlistp points at location for array pointer return
1361 * @param devcountp points at location for array size return value
1364 * @retval ENOMEM the array allocation failed
1367 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1372 count = devclass_get_count(dc);
1373 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1378 for (i = 0; i < dc->maxunit; i++) {
1379 if (dc->devices[i]) {
1380 list[count] = dc->devices[i];
1392 * @brief Get a list of drivers in the devclass
1394 * An array containing a list of pointers to all the drivers in the
1395 * given devclass is allocated and returned in @p *listp. The number
1396 * of drivers in the array is returned in @p *countp. The caller should
1397 * free the array using @c free(p, M_TEMP).
1399 * @param dc the devclass to examine
1400 * @param listp gives location for array pointer return value
1401 * @param countp gives location for number of array elements
1405 * @retval ENOMEM the array allocation failed
1408 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1415 TAILQ_FOREACH(dl, &dc->drivers, link)
1417 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1422 TAILQ_FOREACH(dl, &dc->drivers, link) {
1423 list[count] = dl->driver;
1433 * @brief Get the number of devices in a devclass
1435 * @param dc the devclass to examine
1438 devclass_get_count(devclass_t dc)
1443 for (i = 0; i < dc->maxunit; i++)
1450 * @brief Get the maximum unit number used in a devclass
1452 * Note that this is one greater than the highest currently-allocated
1453 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1454 * that not even the devclass has been allocated yet.
1456 * @param dc the devclass to examine
1459 devclass_get_maxunit(devclass_t dc)
1463 return (dc->maxunit);
1467 * @brief Find a free unit number in a devclass
1469 * This function searches for the first unused unit number greater
1470 * that or equal to @p unit.
1472 * @param dc the devclass to examine
1473 * @param unit the first unit number to check
1476 devclass_find_free_unit(devclass_t dc, int unit)
1480 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1486 * @brief Set the parent of a devclass
1488 * The parent class is normally initialised automatically by
1491 * @param dc the devclass to edit
1492 * @param pdc the new parent devclass
1495 devclass_set_parent(devclass_t dc, devclass_t pdc)
1501 * @brief Get the parent of a devclass
1503 * @param dc the devclass to examine
1506 devclass_get_parent(devclass_t dc)
1508 return (dc->parent);
1511 struct sysctl_ctx_list *
1512 devclass_get_sysctl_ctx(devclass_t dc)
1514 return (&dc->sysctl_ctx);
1518 devclass_get_sysctl_tree(devclass_t dc)
1520 return (dc->sysctl_tree);
1525 * @brief Allocate a unit number
1527 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1528 * will do). The allocated unit number is returned in @p *unitp.
1530 * @param dc the devclass to allocate from
1531 * @param unitp points at the location for the allocated unit
1535 * @retval EEXIST the requested unit number is already allocated
1536 * @retval ENOMEM memory allocation failure
1539 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1544 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1546 /* Ask the parent bus if it wants to wire this device. */
1548 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1551 /* If we were given a wired unit number, check for existing device */
1554 if (unit >= 0 && unit < dc->maxunit &&
1555 dc->devices[unit] != NULL) {
1557 printf("%s: %s%d already exists; skipping it\n",
1558 dc->name, dc->name, *unitp);
1562 /* Unwired device, find the next available slot for it */
1564 for (unit = 0;; unit++) {
1565 /* If there is an "at" hint for a unit then skip it. */
1566 if (resource_string_value(dc->name, unit, "at", &s) ==
1570 /* If this device slot is already in use, skip it. */
1571 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1579 * We've selected a unit beyond the length of the table, so let's
1580 * extend the table to make room for all units up to and including
1583 if (unit >= dc->maxunit) {
1584 device_t *newlist, *oldlist;
1587 oldlist = dc->devices;
1588 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1589 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1592 if (oldlist != NULL)
1593 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1594 bzero(newlist + dc->maxunit,
1595 sizeof(device_t) * (newsize - dc->maxunit));
1596 dc->devices = newlist;
1597 dc->maxunit = newsize;
1598 if (oldlist != NULL)
1599 free(oldlist, M_BUS);
1601 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1609 * @brief Add a device to a devclass
1611 * A unit number is allocated for the device (using the device's
1612 * preferred unit number if any) and the device is registered in the
1613 * devclass. This allows the device to be looked up by its unit
1614 * number, e.g. by decoding a dev_t minor number.
1616 * @param dc the devclass to add to
1617 * @param dev the device to add
1620 * @retval EEXIST the requested unit number is already allocated
1621 * @retval ENOMEM memory allocation failure
1624 devclass_add_device(devclass_t dc, device_t dev)
1628 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1630 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1633 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1637 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1638 free(dev->nameunit, M_BUS);
1639 dev->nameunit = NULL;
1642 dc->devices[dev->unit] = dev;
1644 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1651 * @brief Delete a device from a devclass
1653 * The device is removed from the devclass's device list and its unit
1656 * @param dc the devclass to delete from
1657 * @param dev the device to delete
1662 devclass_delete_device(devclass_t dc, device_t dev)
1667 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1669 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1670 panic("devclass_delete_device: inconsistent device class");
1671 dc->devices[dev->unit] = NULL;
1672 if (dev->flags & DF_WILDCARD)
1674 dev->devclass = NULL;
1675 free(dev->nameunit, M_BUS);
1676 dev->nameunit = NULL;
1683 * @brief Make a new device and add it as a child of @p parent
1685 * @param parent the parent of the new device
1686 * @param name the devclass name of the new device or @c NULL
1687 * to leave the devclass unspecified
1688 * @parem unit the unit number of the new device of @c -1 to
1689 * leave the unit number unspecified
1691 * @returns the new device
1694 make_device(device_t parent, const char *name, int unit)
1699 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1702 dc = devclass_find_internal(name, NULL, TRUE);
1704 printf("make_device: can't find device class %s\n",
1712 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1716 dev->parent = parent;
1717 TAILQ_INIT(&dev->children);
1718 kobj_init((kobj_t) dev, &null_class);
1720 dev->devclass = NULL;
1722 dev->nameunit = NULL;
1726 dev->flags = DF_ENABLED;
1729 dev->flags |= DF_WILDCARD;
1731 dev->flags |= DF_FIXEDCLASS;
1732 if (devclass_add_device(dc, dev)) {
1733 kobj_delete((kobj_t) dev, M_BUS);
1740 dev->state = DS_NOTPRESENT;
1742 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1743 bus_data_generation_update();
1750 * @brief Print a description of a device.
1753 device_print_child(device_t dev, device_t child)
1757 if (device_is_alive(child))
1758 retval += BUS_PRINT_CHILD(dev, child);
1760 retval += device_printf(child, " not found\n");
1766 * @brief Create a new device
1768 * This creates a new device and adds it as a child of an existing
1769 * parent device. The new device will be added after the last existing
1770 * child with order zero.
1772 * @param dev the device which will be the parent of the
1774 * @param name devclass name for new device or @c NULL if not
1776 * @param unit unit number for new device or @c -1 if not
1779 * @returns the new device
1782 device_add_child(device_t dev, const char *name, int unit)
1784 return (device_add_child_ordered(dev, 0, name, unit));
1788 * @brief Create a new device
1790 * This creates a new device and adds it as a child of an existing
1791 * parent device. The new device will be added after the last existing
1792 * child with the same order.
1794 * @param dev the device which will be the parent of the
1796 * @param order a value which is used to partially sort the
1797 * children of @p dev - devices created using
1798 * lower values of @p order appear first in @p
1799 * dev's list of children
1800 * @param name devclass name for new device or @c NULL if not
1802 * @param unit unit number for new device or @c -1 if not
1805 * @returns the new device
1808 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1813 PDEBUG(("%s at %s with order %u as unit %d",
1814 name, DEVICENAME(dev), order, unit));
1815 KASSERT(name != NULL || unit == -1,
1816 ("child device with wildcard name and specific unit number"));
1818 child = make_device(dev, name, unit);
1821 child->order = order;
1823 TAILQ_FOREACH(place, &dev->children, link) {
1824 if (place->order > order)
1830 * The device 'place' is the first device whose order is
1831 * greater than the new child.
1833 TAILQ_INSERT_BEFORE(place, child, link);
1836 * The new child's order is greater or equal to the order of
1837 * any existing device. Add the child to the tail of the list.
1839 TAILQ_INSERT_TAIL(&dev->children, child, link);
1842 bus_data_generation_update();
1847 * @brief Delete a device
1849 * This function deletes a device along with all of its children. If
1850 * the device currently has a driver attached to it, the device is
1851 * detached first using device_detach().
1853 * @param dev the parent device
1854 * @param child the device to delete
1857 * @retval non-zero a unit error code describing the error
1860 device_delete_child(device_t dev, device_t child)
1863 device_t grandchild;
1865 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1867 /* detach parent before deleting children, if any */
1868 if ((error = device_detach(child)) != 0)
1871 /* remove children second */
1872 while ((grandchild = TAILQ_FIRST(&child->children)) != NULL) {
1873 error = device_delete_child(child, grandchild);
1878 if (child->devclass)
1879 devclass_delete_device(child->devclass, child);
1881 BUS_CHILD_DELETED(dev, child);
1882 TAILQ_REMOVE(&dev->children, child, link);
1883 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1884 kobj_delete((kobj_t) child, M_BUS);
1886 bus_data_generation_update();
1891 * @brief Delete all children devices of the given device, if any.
1893 * This function deletes all children devices of the given device, if
1894 * any, using the device_delete_child() function for each device it
1895 * finds. If a child device cannot be deleted, this function will
1896 * return an error code.
1898 * @param dev the parent device
1901 * @retval non-zero a device would not detach
1904 device_delete_children(device_t dev)
1909 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1913 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1914 error = device_delete_child(dev, child);
1916 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1924 * @brief Find a device given a unit number
1926 * This is similar to devclass_get_devices() but only searches for
1927 * devices which have @p dev as a parent.
1929 * @param dev the parent device to search
1930 * @param unit the unit number to search for. If the unit is -1,
1931 * return the first child of @p dev which has name
1932 * @p classname (that is, the one with the lowest unit.)
1934 * @returns the device with the given unit number or @c
1935 * NULL if there is no such device
1938 device_find_child(device_t dev, const char *classname, int unit)
1943 dc = devclass_find(classname);
1948 child = devclass_get_device(dc, unit);
1949 if (child && child->parent == dev)
1952 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1953 child = devclass_get_device(dc, unit);
1954 if (child && child->parent == dev)
1965 first_matching_driver(devclass_t dc, device_t dev)
1968 return (devclass_find_driver_internal(dc, dev->devclass->name));
1969 return (TAILQ_FIRST(&dc->drivers));
1976 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1978 if (dev->devclass) {
1980 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1981 if (!strcmp(dev->devclass->name, dl->driver->name))
1985 return (TAILQ_NEXT(last, link));
1992 device_probe_child(device_t dev, device_t child)
1995 driverlink_t best = NULL;
1997 int result, pri = 0;
1998 int hasclass = (child->devclass != NULL);
2004 panic("device_probe_child: parent device has no devclass");
2007 * If the state is already probed, then return. However, don't
2008 * return if we can rebid this object.
2010 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2013 for (; dc; dc = dc->parent) {
2014 for (dl = first_matching_driver(dc, child);
2016 dl = next_matching_driver(dc, child, dl)) {
2017 /* If this driver's pass is too high, then ignore it. */
2018 if (dl->pass > bus_current_pass)
2021 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2022 result = device_set_driver(child, dl->driver);
2023 if (result == ENOMEM)
2025 else if (result != 0)
2028 if (device_set_devclass(child,
2029 dl->driver->name) != 0) {
2030 printf("driver bug: Unable to set "
2031 "devclass (devname: %s)\n",
2032 device_get_name(child));
2033 (void)device_set_driver(child, NULL);
2038 /* Fetch any flags for the device before probing. */
2039 resource_int_value(dl->driver->name, child->unit,
2040 "flags", &child->devflags);
2042 result = DEVICE_PROBE(child);
2044 /* Reset flags and devclass before the next probe. */
2045 child->devflags = 0;
2047 (void)device_set_devclass(child, NULL);
2050 * If the driver returns SUCCESS, there can be
2051 * no higher match for this device.
2060 * The driver returned an error so it
2061 * certainly doesn't match.
2064 (void)device_set_driver(child, NULL);
2069 * A priority lower than SUCCESS, remember the
2070 * best matching driver. Initialise the value
2071 * of pri for the first match.
2073 if (best == NULL || result > pri) {
2075 * Probes that return BUS_PROBE_NOWILDCARD
2076 * or lower only match when they are set
2077 * in stone by the parent bus.
2079 if (result <= BUS_PROBE_NOWILDCARD &&
2080 child->flags & DF_WILDCARD)
2088 * If we have an unambiguous match in this devclass,
2089 * don't look in the parent.
2091 if (best && pri == 0)
2096 * If we found a driver, change state and initialise the devclass.
2098 /* XXX What happens if we rebid and got no best? */
2101 * If this device was attached, and we were asked to
2102 * rescan, and it is a different driver, then we have
2103 * to detach the old driver and reattach this new one.
2104 * Note, we don't have to check for DF_REBID here
2105 * because if the state is > DS_ALIVE, we know it must
2108 * This assumes that all DF_REBID drivers can have
2109 * their probe routine called at any time and that
2110 * they are idempotent as well as completely benign in
2111 * normal operations.
2113 * We also have to make sure that the detach
2114 * succeeded, otherwise we fail the operation (or
2115 * maybe it should just fail silently? I'm torn).
2117 if (child->state > DS_ALIVE && best->driver != child->driver)
2118 if ((result = device_detach(dev)) != 0)
2121 /* Set the winning driver, devclass, and flags. */
2122 if (!child->devclass) {
2123 result = device_set_devclass(child, best->driver->name);
2127 result = device_set_driver(child, best->driver);
2130 resource_int_value(best->driver->name, child->unit,
2131 "flags", &child->devflags);
2135 * A bit bogus. Call the probe method again to make
2136 * sure that we have the right description.
2138 DEVICE_PROBE(child);
2140 child->flags |= DF_REBID;
2143 child->flags &= ~DF_REBID;
2144 child->state = DS_ALIVE;
2146 bus_data_generation_update();
2154 * @brief Return the parent of a device
2157 device_get_parent(device_t dev)
2159 return (dev->parent);
2163 * @brief Get a list of children of a device
2165 * An array containing a list of all the children of the given device
2166 * is allocated and returned in @p *devlistp. The number of devices
2167 * in the array is returned in @p *devcountp. The caller should free
2168 * the array using @c free(p, M_TEMP).
2170 * @param dev the device to examine
2171 * @param devlistp points at location for array pointer return
2173 * @param devcountp points at location for array size return value
2176 * @retval ENOMEM the array allocation failed
2179 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2186 TAILQ_FOREACH(child, &dev->children, link) {
2195 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2200 TAILQ_FOREACH(child, &dev->children, link) {
2201 list[count] = child;
2212 * @brief Return the current driver for the device or @c NULL if there
2213 * is no driver currently attached
2216 device_get_driver(device_t dev)
2218 return (dev->driver);
2222 * @brief Return the current devclass for the device or @c NULL if
2226 device_get_devclass(device_t dev)
2228 return (dev->devclass);
2232 * @brief Return the name of the device's devclass or @c NULL if there
2236 device_get_name(device_t dev)
2238 if (dev != NULL && dev->devclass)
2239 return (devclass_get_name(dev->devclass));
2244 * @brief Return a string containing the device's devclass name
2245 * followed by an ascii representation of the device's unit number
2249 device_get_nameunit(device_t dev)
2251 return (dev->nameunit);
2255 * @brief Return the device's unit number.
2258 device_get_unit(device_t dev)
2264 * @brief Return the device's description string
2267 device_get_desc(device_t dev)
2273 * @brief Return the device's flags
2276 device_get_flags(device_t dev)
2278 return (dev->devflags);
2281 struct sysctl_ctx_list *
2282 device_get_sysctl_ctx(device_t dev)
2284 return (&dev->sysctl_ctx);
2288 device_get_sysctl_tree(device_t dev)
2290 return (dev->sysctl_tree);
2294 * @brief Print the name of the device followed by a colon and a space
2296 * @returns the number of characters printed
2299 device_print_prettyname(device_t dev)
2301 const char *name = device_get_name(dev);
2304 return (printf("unknown: "));
2305 return (printf("%s%d: ", name, device_get_unit(dev)));
2309 * @brief Print the name of the device followed by a colon, a space
2310 * and the result of calling vprintf() with the value of @p fmt and
2311 * the following arguments.
2313 * @returns the number of characters printed
2316 device_printf(device_t dev, const char * fmt, ...)
2321 retval = device_print_prettyname(dev);
2323 retval += vprintf(fmt, ap);
2332 device_set_desc_internal(device_t dev, const char* desc, int copy)
2334 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2335 free(dev->desc, M_BUS);
2336 dev->flags &= ~DF_DESCMALLOCED;
2341 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2343 strcpy(dev->desc, desc);
2344 dev->flags |= DF_DESCMALLOCED;
2347 /* Avoid a -Wcast-qual warning */
2348 dev->desc = (char *)(uintptr_t) desc;
2351 bus_data_generation_update();
2355 * @brief Set the device's description
2357 * The value of @c desc should be a string constant that will not
2358 * change (at least until the description is changed in a subsequent
2359 * call to device_set_desc() or device_set_desc_copy()).
2362 device_set_desc(device_t dev, const char* desc)
2364 device_set_desc_internal(dev, desc, FALSE);
2368 * @brief Set the device's description
2370 * The string pointed to by @c desc is copied. Use this function if
2371 * the device description is generated, (e.g. with sprintf()).
2374 device_set_desc_copy(device_t dev, const char* desc)
2376 device_set_desc_internal(dev, desc, TRUE);
2380 * @brief Set the device's flags
2383 device_set_flags(device_t dev, uint32_t flags)
2385 dev->devflags = flags;
2389 * @brief Return the device's softc field
2391 * The softc is allocated and zeroed when a driver is attached, based
2392 * on the size field of the driver.
2395 device_get_softc(device_t dev)
2397 return (dev->softc);
2401 * @brief Set the device's softc field
2403 * Most drivers do not need to use this since the softc is allocated
2404 * automatically when the driver is attached.
2407 device_set_softc(device_t dev, void *softc)
2409 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2410 free(dev->softc, M_BUS_SC);
2413 dev->flags |= DF_EXTERNALSOFTC;
2415 dev->flags &= ~DF_EXTERNALSOFTC;
2419 * @brief Free claimed softc
2421 * Most drivers do not need to use this since the softc is freed
2422 * automatically when the driver is detached.
2425 device_free_softc(void *softc)
2427 free(softc, M_BUS_SC);
2431 * @brief Claim softc
2433 * This function can be used to let the driver free the automatically
2434 * allocated softc using "device_free_softc()". This function is
2435 * useful when the driver is refcounting the softc and the softc
2436 * cannot be freed when the "device_detach" method is called.
2439 device_claim_softc(device_t dev)
2442 dev->flags |= DF_EXTERNALSOFTC;
2444 dev->flags &= ~DF_EXTERNALSOFTC;
2448 * @brief Get the device's ivars field
2450 * The ivars field is used by the parent device to store per-device
2451 * state (e.g. the physical location of the device or a list of
2455 device_get_ivars(device_t dev)
2458 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2459 return (dev->ivars);
2463 * @brief Set the device's ivars field
2466 device_set_ivars(device_t dev, void * ivars)
2469 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2474 * @brief Return the device's state
2477 device_get_state(device_t dev)
2479 return (dev->state);
2483 * @brief Set the DF_ENABLED flag for the device
2486 device_enable(device_t dev)
2488 dev->flags |= DF_ENABLED;
2492 * @brief Clear the DF_ENABLED flag for the device
2495 device_disable(device_t dev)
2497 dev->flags &= ~DF_ENABLED;
2501 * @brief Increment the busy counter for the device
2504 device_busy(device_t dev)
2506 if (dev->state < DS_ATTACHING)
2507 panic("device_busy: called for unattached device");
2508 if (dev->busy == 0 && dev->parent)
2509 device_busy(dev->parent);
2511 if (dev->state == DS_ATTACHED)
2512 dev->state = DS_BUSY;
2516 * @brief Decrement the busy counter for the device
2519 device_unbusy(device_t dev)
2521 if (dev->busy != 0 && dev->state != DS_BUSY &&
2522 dev->state != DS_ATTACHING)
2523 panic("device_unbusy: called for non-busy device %s",
2524 device_get_nameunit(dev));
2526 if (dev->busy == 0) {
2528 device_unbusy(dev->parent);
2529 if (dev->state == DS_BUSY)
2530 dev->state = DS_ATTACHED;
2535 * @brief Set the DF_QUIET flag for the device
2538 device_quiet(device_t dev)
2540 dev->flags |= DF_QUIET;
2544 * @brief Clear the DF_QUIET flag for the device
2547 device_verbose(device_t dev)
2549 dev->flags &= ~DF_QUIET;
2553 * @brief Return non-zero if the DF_QUIET flag is set on the device
2556 device_is_quiet(device_t dev)
2558 return ((dev->flags & DF_QUIET) != 0);
2562 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2565 device_is_enabled(device_t dev)
2567 return ((dev->flags & DF_ENABLED) != 0);
2571 * @brief Return non-zero if the device was successfully probed
2574 device_is_alive(device_t dev)
2576 return (dev->state >= DS_ALIVE);
2580 * @brief Return non-zero if the device currently has a driver
2584 device_is_attached(device_t dev)
2586 return (dev->state >= DS_ATTACHED);
2590 * @brief Set the devclass of a device
2591 * @see devclass_add_device().
2594 device_set_devclass(device_t dev, const char *classname)
2601 devclass_delete_device(dev->devclass, dev);
2605 if (dev->devclass) {
2606 printf("device_set_devclass: device class already set\n");
2610 dc = devclass_find_internal(classname, NULL, TRUE);
2614 error = devclass_add_device(dc, dev);
2616 bus_data_generation_update();
2621 * @brief Set the driver of a device
2624 * @retval EBUSY the device already has a driver attached
2625 * @retval ENOMEM a memory allocation failure occurred
2628 device_set_driver(device_t dev, driver_t *driver)
2630 if (dev->state >= DS_ATTACHED)
2633 if (dev->driver == driver)
2636 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2637 free(dev->softc, M_BUS_SC);
2640 device_set_desc(dev, NULL);
2641 kobj_delete((kobj_t) dev, NULL);
2642 dev->driver = driver;
2644 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2645 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2646 dev->softc = malloc(driver->size, M_BUS_SC,
2649 kobj_delete((kobj_t) dev, NULL);
2650 kobj_init((kobj_t) dev, &null_class);
2656 kobj_init((kobj_t) dev, &null_class);
2659 bus_data_generation_update();
2664 * @brief Probe a device, and return this status.
2666 * This function is the core of the device autoconfiguration
2667 * system. Its purpose is to select a suitable driver for a device and
2668 * then call that driver to initialise the hardware appropriately. The
2669 * driver is selected by calling the DEVICE_PROBE() method of a set of
2670 * candidate drivers and then choosing the driver which returned the
2671 * best value. This driver is then attached to the device using
2674 * The set of suitable drivers is taken from the list of drivers in
2675 * the parent device's devclass. If the device was originally created
2676 * with a specific class name (see device_add_child()), only drivers
2677 * with that name are probed, otherwise all drivers in the devclass
2678 * are probed. If no drivers return successful probe values in the
2679 * parent devclass, the search continues in the parent of that
2680 * devclass (see devclass_get_parent()) if any.
2682 * @param dev the device to initialise
2685 * @retval ENXIO no driver was found
2686 * @retval ENOMEM memory allocation failure
2687 * @retval non-zero some other unix error code
2688 * @retval -1 Device already attached
2691 device_probe(device_t dev)
2697 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2700 if (!(dev->flags & DF_ENABLED)) {
2701 if (bootverbose && device_get_name(dev) != NULL) {
2702 device_print_prettyname(dev);
2703 printf("not probed (disabled)\n");
2707 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2708 if (bus_current_pass == BUS_PASS_DEFAULT &&
2709 !(dev->flags & DF_DONENOMATCH)) {
2710 BUS_PROBE_NOMATCH(dev->parent, dev);
2712 dev->flags |= DF_DONENOMATCH;
2720 * @brief Probe a device and attach a driver if possible
2722 * calls device_probe() and attaches if that was successful.
2725 device_probe_and_attach(device_t dev)
2731 error = device_probe(dev);
2734 else if (error != 0)
2737 CURVNET_SET_QUIET(vnet0);
2738 error = device_attach(dev);
2744 * @brief Attach a device driver to a device
2746 * This function is a wrapper around the DEVICE_ATTACH() driver
2747 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2748 * device's sysctl tree, optionally prints a description of the device
2749 * and queues a notification event for user-based device management
2752 * Normally this function is only called internally from
2753 * device_probe_and_attach().
2755 * @param dev the device to initialise
2758 * @retval ENXIO no driver was found
2759 * @retval ENOMEM memory allocation failure
2760 * @retval non-zero some other unix error code
2763 device_attach(device_t dev)
2767 if (resource_disabled(dev->driver->name, dev->unit)) {
2768 device_disable(dev);
2770 device_printf(dev, "disabled via hints entry\n");
2774 device_sysctl_init(dev);
2775 if (!device_is_quiet(dev))
2776 device_print_child(dev->parent, dev);
2777 dev->state = DS_ATTACHING;
2778 if ((error = DEVICE_ATTACH(dev)) != 0) {
2779 printf("device_attach: %s%d attach returned %d\n",
2780 dev->driver->name, dev->unit, error);
2781 if (!(dev->flags & DF_FIXEDCLASS))
2782 devclass_delete_device(dev->devclass, dev);
2783 (void)device_set_driver(dev, NULL);
2784 device_sysctl_fini(dev);
2785 KASSERT(dev->busy == 0, ("attach failed but busy"));
2786 dev->state = DS_NOTPRESENT;
2789 device_sysctl_update(dev);
2791 dev->state = DS_BUSY;
2793 dev->state = DS_ATTACHED;
2794 dev->flags &= ~DF_DONENOMATCH;
2800 * @brief Detach a driver from a device
2802 * This function is a wrapper around the DEVICE_DETACH() driver
2803 * method. If the call to DEVICE_DETACH() succeeds, it calls
2804 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2805 * notification event for user-based device management services and
2806 * cleans up the device's sysctl tree.
2808 * @param dev the device to un-initialise
2811 * @retval ENXIO no driver was found
2812 * @retval ENOMEM memory allocation failure
2813 * @retval non-zero some other unix error code
2816 device_detach(device_t dev)
2822 PDEBUG(("%s", DEVICENAME(dev)));
2823 if (dev->state == DS_BUSY)
2825 if (dev->state != DS_ATTACHED)
2828 if ((error = DEVICE_DETACH(dev)) != 0)
2831 if (!device_is_quiet(dev))
2832 device_printf(dev, "detached\n");
2834 BUS_CHILD_DETACHED(dev->parent, dev);
2836 if (!(dev->flags & DF_FIXEDCLASS))
2837 devclass_delete_device(dev->devclass, dev);
2839 dev->state = DS_NOTPRESENT;
2840 (void)device_set_driver(dev, NULL);
2841 device_sysctl_fini(dev);
2847 * @brief Tells a driver to quiesce itself.
2849 * This function is a wrapper around the DEVICE_QUIESCE() driver
2850 * method. If the call to DEVICE_QUIESCE() succeeds.
2852 * @param dev the device to quiesce
2855 * @retval ENXIO no driver was found
2856 * @retval ENOMEM memory allocation failure
2857 * @retval non-zero some other unix error code
2860 device_quiesce(device_t dev)
2863 PDEBUG(("%s", DEVICENAME(dev)));
2864 if (dev->state == DS_BUSY)
2866 if (dev->state != DS_ATTACHED)
2869 return (DEVICE_QUIESCE(dev));
2873 * @brief Notify a device of system shutdown
2875 * This function calls the DEVICE_SHUTDOWN() driver method if the
2876 * device currently has an attached driver.
2878 * @returns the value returned by DEVICE_SHUTDOWN()
2881 device_shutdown(device_t dev)
2883 if (dev->state < DS_ATTACHED)
2885 return (DEVICE_SHUTDOWN(dev));
2889 * @brief Set the unit number of a device
2891 * This function can be used to override the unit number used for a
2892 * device (e.g. to wire a device to a pre-configured unit number).
2895 device_set_unit(device_t dev, int unit)
2900 dc = device_get_devclass(dev);
2901 if (unit < dc->maxunit && dc->devices[unit])
2903 err = devclass_delete_device(dc, dev);
2907 err = devclass_add_device(dc, dev);
2911 bus_data_generation_update();
2915 /*======================================*/
2917 * Some useful method implementations to make life easier for bus drivers.
2921 * @brief Initialise a resource list.
2923 * @param rl the resource list to initialise
2926 resource_list_init(struct resource_list *rl)
2932 * @brief Reclaim memory used by a resource list.
2934 * This function frees the memory for all resource entries on the list
2937 * @param rl the resource list to free
2940 resource_list_free(struct resource_list *rl)
2942 struct resource_list_entry *rle;
2944 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2946 panic("resource_list_free: resource entry is busy");
2947 STAILQ_REMOVE_HEAD(rl, link);
2953 * @brief Add a resource entry.
2955 * This function adds a resource entry using the given @p type, @p
2956 * start, @p end and @p count values. A rid value is chosen by
2957 * searching sequentially for the first unused rid starting at zero.
2959 * @param rl the resource list to edit
2960 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2961 * @param start the start address of the resource
2962 * @param end the end address of the resource
2963 * @param count XXX end-start+1
2966 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2967 u_long end, u_long count)
2972 while (resource_list_find(rl, type, rid) != NULL)
2974 resource_list_add(rl, type, rid, start, end, count);
2979 * @brief Add or modify a resource entry.
2981 * If an existing entry exists with the same type and rid, it will be
2982 * modified using the given values of @p start, @p end and @p
2983 * count. If no entry exists, a new one will be created using the
2984 * given values. The resource list entry that matches is then returned.
2986 * @param rl the resource list to edit
2987 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2988 * @param rid the resource identifier
2989 * @param start the start address of the resource
2990 * @param end the end address of the resource
2991 * @param count XXX end-start+1
2993 struct resource_list_entry *
2994 resource_list_add(struct resource_list *rl, int type, int rid,
2995 u_long start, u_long end, u_long count)
2997 struct resource_list_entry *rle;
2999 rle = resource_list_find(rl, type, rid);
3001 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
3004 panic("resource_list_add: can't record entry");
3005 STAILQ_INSERT_TAIL(rl, rle, link);
3013 panic("resource_list_add: resource entry is busy");
3022 * @brief Determine if a resource entry is busy.
3024 * Returns true if a resource entry is busy meaning that it has an
3025 * associated resource that is not an unallocated "reserved" resource.
3027 * @param rl the resource list to search
3028 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3029 * @param rid the resource identifier
3031 * @returns Non-zero if the entry is busy, zero otherwise.
3034 resource_list_busy(struct resource_list *rl, int type, int rid)
3036 struct resource_list_entry *rle;
3038 rle = resource_list_find(rl, type, rid);
3039 if (rle == NULL || rle->res == NULL)
3041 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
3042 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
3043 ("reserved resource is active"));
3050 * @brief Determine if a resource entry is reserved.
3052 * Returns true if a resource entry is reserved meaning that it has an
3053 * associated "reserved" resource. The resource can either be
3054 * allocated or unallocated.
3056 * @param rl the resource list to search
3057 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3058 * @param rid the resource identifier
3060 * @returns Non-zero if the entry is reserved, zero otherwise.
3063 resource_list_reserved(struct resource_list *rl, int type, int rid)
3065 struct resource_list_entry *rle;
3067 rle = resource_list_find(rl, type, rid);
3068 if (rle != NULL && rle->flags & RLE_RESERVED)
3074 * @brief Find a resource entry by type and rid.
3076 * @param rl the resource list to search
3077 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3078 * @param rid the resource identifier
3080 * @returns the resource entry pointer or NULL if there is no such
3083 struct resource_list_entry *
3084 resource_list_find(struct resource_list *rl, int type, int rid)
3086 struct resource_list_entry *rle;
3088 STAILQ_FOREACH(rle, rl, link) {
3089 if (rle->type == type && rle->rid == rid)
3096 * @brief Delete a resource entry.
3098 * @param rl the resource list to edit
3099 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3100 * @param rid the resource identifier
3103 resource_list_delete(struct resource_list *rl, int type, int rid)
3105 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3108 if (rle->res != NULL)
3109 panic("resource_list_delete: resource has not been released");
3110 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3116 * @brief Allocate a reserved resource
3118 * This can be used by busses to force the allocation of resources
3119 * that are always active in the system even if they are not allocated
3120 * by a driver (e.g. PCI BARs). This function is usually called when
3121 * adding a new child to the bus. The resource is allocated from the
3122 * parent bus when it is reserved. The resource list entry is marked
3123 * with RLE_RESERVED to note that it is a reserved resource.
3125 * Subsequent attempts to allocate the resource with
3126 * resource_list_alloc() will succeed the first time and will set
3127 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3128 * resource that has been allocated is released with
3129 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3130 * the actual resource remains allocated. The resource can be released to
3131 * the parent bus by calling resource_list_unreserve().
3133 * @param rl the resource list to allocate from
3134 * @param bus the parent device of @p child
3135 * @param child the device for which the resource is being reserved
3136 * @param type the type of resource to allocate
3137 * @param rid a pointer to the resource identifier
3138 * @param start hint at the start of the resource range - pass
3139 * @c 0UL for any start address
3140 * @param end hint at the end of the resource range - pass
3141 * @c ~0UL for any end address
3142 * @param count hint at the size of range required - pass @c 1
3144 * @param flags any extra flags to control the resource
3145 * allocation - see @c RF_XXX flags in
3146 * <sys/rman.h> for details
3148 * @returns the resource which was allocated or @c NULL if no
3149 * resource could be allocated
3152 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3153 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3155 struct resource_list_entry *rle = NULL;
3156 int passthrough = (device_get_parent(child) != bus);
3161 "resource_list_reserve() should only be called for direct children");
3162 if (flags & RF_ACTIVE)
3164 "resource_list_reserve() should only reserve inactive resources");
3166 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3169 rle = resource_list_find(rl, type, *rid);
3170 rle->flags |= RLE_RESERVED;
3176 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3178 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3179 * and passing the allocation up to the parent of @p bus. This assumes
3180 * that the first entry of @c device_get_ivars(child) is a struct
3181 * resource_list. This also handles 'passthrough' allocations where a
3182 * child is a remote descendant of bus by passing the allocation up to
3183 * the parent of bus.
3185 * Typically, a bus driver would store a list of child resources
3186 * somewhere in the child device's ivars (see device_get_ivars()) and
3187 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3188 * then call resource_list_alloc() to perform the allocation.
3190 * @param rl the resource list to allocate from
3191 * @param bus the parent device of @p child
3192 * @param child the device which is requesting an allocation
3193 * @param type the type of resource to allocate
3194 * @param rid a pointer to the resource identifier
3195 * @param start hint at the start of the resource range - pass
3196 * @c 0UL for any start address
3197 * @param end hint at the end of the resource range - pass
3198 * @c ~0UL for any end address
3199 * @param count hint at the size of range required - pass @c 1
3201 * @param flags any extra flags to control the resource
3202 * allocation - see @c RF_XXX flags in
3203 * <sys/rman.h> for details
3205 * @returns the resource which was allocated or @c NULL if no
3206 * resource could be allocated
3209 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3210 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3212 struct resource_list_entry *rle = NULL;
3213 int passthrough = (device_get_parent(child) != bus);
3214 int isdefault = (start == 0UL && end == ~0UL);
3217 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3218 type, rid, start, end, count, flags));
3221 rle = resource_list_find(rl, type, *rid);
3224 return (NULL); /* no resource of that type/rid */
3227 if (rle->flags & RLE_RESERVED) {
3228 if (rle->flags & RLE_ALLOCATED)
3230 if ((flags & RF_ACTIVE) &&
3231 bus_activate_resource(child, type, *rid,
3234 rle->flags |= RLE_ALLOCATED;
3238 "resource entry %#x type %d for child %s is busy\n", *rid,
3239 type, device_get_nameunit(child));
3245 count = ulmax(count, rle->count);
3246 end = ulmax(rle->end, start + count - 1);
3249 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3250 type, rid, start, end, count, flags);
3253 * Record the new range.
3256 rle->start = rman_get_start(rle->res);
3257 rle->end = rman_get_end(rle->res);
3265 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3267 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3268 * used with resource_list_alloc().
3270 * @param rl the resource list which was allocated from
3271 * @param bus the parent device of @p child
3272 * @param child the device which is requesting a release
3273 * @param type the type of resource to release
3274 * @param rid the resource identifier
3275 * @param res the resource to release
3278 * @retval non-zero a standard unix error code indicating what
3279 * error condition prevented the operation
3282 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3283 int type, int rid, struct resource *res)
3285 struct resource_list_entry *rle = NULL;
3286 int passthrough = (device_get_parent(child) != bus);
3290 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3294 rle = resource_list_find(rl, type, rid);
3297 panic("resource_list_release: can't find resource");
3299 panic("resource_list_release: resource entry is not busy");
3300 if (rle->flags & RLE_RESERVED) {
3301 if (rle->flags & RLE_ALLOCATED) {
3302 if (rman_get_flags(res) & RF_ACTIVE) {
3303 error = bus_deactivate_resource(child, type,
3308 rle->flags &= ~RLE_ALLOCATED;
3314 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3324 * @brief Fully release a reserved resource
3326 * Fully releases a resouce reserved via resource_list_reserve().
3328 * @param rl the resource list which was allocated from
3329 * @param bus the parent device of @p child
3330 * @param child the device whose reserved resource is being released
3331 * @param type the type of resource to release
3332 * @param rid the resource identifier
3333 * @param res the resource to release
3336 * @retval non-zero a standard unix error code indicating what
3337 * error condition prevented the operation
3340 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3343 struct resource_list_entry *rle = NULL;
3344 int passthrough = (device_get_parent(child) != bus);
3348 "resource_list_unreserve() should only be called for direct children");
3350 rle = resource_list_find(rl, type, rid);
3353 panic("resource_list_unreserve: can't find resource");
3354 if (!(rle->flags & RLE_RESERVED))
3356 if (rle->flags & RLE_ALLOCATED)
3358 rle->flags &= ~RLE_RESERVED;
3359 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3363 * @brief Print a description of resources in a resource list
3365 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3366 * The name is printed if at least one resource of the given type is available.
3367 * The format is used to print resource start and end.
3369 * @param rl the resource list to print
3370 * @param name the name of @p type, e.g. @c "memory"
3371 * @param type type type of resource entry to print
3372 * @param format printf(9) format string to print resource
3373 * start and end values
3375 * @returns the number of characters printed
3378 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3381 struct resource_list_entry *rle;
3382 int printed, retval;
3386 /* Yes, this is kinda cheating */
3387 STAILQ_FOREACH(rle, rl, link) {
3388 if (rle->type == type) {
3390 retval += printf(" %s ", name);
3392 retval += printf(",");
3394 retval += printf(format, rle->start);
3395 if (rle->count > 1) {
3396 retval += printf("-");
3397 retval += printf(format, rle->start +
3406 * @brief Releases all the resources in a list.
3408 * @param rl The resource list to purge.
3413 resource_list_purge(struct resource_list *rl)
3415 struct resource_list_entry *rle;
3417 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3419 bus_release_resource(rman_get_device(rle->res),
3420 rle->type, rle->rid, rle->res);
3421 STAILQ_REMOVE_HEAD(rl, link);
3427 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3430 return (device_add_child_ordered(dev, order, name, unit));
3434 * @brief Helper function for implementing DEVICE_PROBE()
3436 * This function can be used to help implement the DEVICE_PROBE() for
3437 * a bus (i.e. a device which has other devices attached to it). It
3438 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3442 bus_generic_probe(device_t dev)
3444 devclass_t dc = dev->devclass;
3447 TAILQ_FOREACH(dl, &dc->drivers, link) {
3449 * If this driver's pass is too high, then ignore it.
3450 * For most drivers in the default pass, this will
3451 * never be true. For early-pass drivers they will
3452 * only call the identify routines of eligible drivers
3453 * when this routine is called. Drivers for later
3454 * passes should have their identify routines called
3455 * on early-pass busses during BUS_NEW_PASS().
3457 if (dl->pass > bus_current_pass)
3459 DEVICE_IDENTIFY(dl->driver, dev);
3466 * @brief Helper function for implementing DEVICE_ATTACH()
3468 * This function can be used to help implement the DEVICE_ATTACH() for
3469 * a bus. It calls device_probe_and_attach() for each of the device's
3473 bus_generic_attach(device_t dev)
3477 TAILQ_FOREACH(child, &dev->children, link) {
3478 device_probe_and_attach(child);
3485 * @brief Helper function for implementing DEVICE_DETACH()
3487 * This function can be used to help implement the DEVICE_DETACH() for
3488 * a bus. It calls device_detach() for each of the device's
3492 bus_generic_detach(device_t dev)
3497 if (dev->state != DS_ATTACHED)
3500 TAILQ_FOREACH(child, &dev->children, link) {
3501 if ((error = device_detach(child)) != 0)
3509 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3511 * This function can be used to help implement the DEVICE_SHUTDOWN()
3512 * for a bus. It calls device_shutdown() for each of the device's
3516 bus_generic_shutdown(device_t dev)
3520 TAILQ_FOREACH(child, &dev->children, link) {
3521 device_shutdown(child);
3528 * @brief Helper function for implementing DEVICE_SUSPEND()
3530 * This function can be used to help implement the DEVICE_SUSPEND()
3531 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3532 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3533 * operation is aborted and any devices which were suspended are
3534 * resumed immediately by calling their DEVICE_RESUME() methods.
3537 bus_generic_suspend(device_t dev)
3540 device_t child, child2;
3542 TAILQ_FOREACH(child, &dev->children, link) {
3543 error = DEVICE_SUSPEND(child);
3545 for (child2 = TAILQ_FIRST(&dev->children);
3546 child2 && child2 != child;
3547 child2 = TAILQ_NEXT(child2, link))
3548 DEVICE_RESUME(child2);
3556 * @brief Helper function for implementing DEVICE_RESUME()
3558 * This function can be used to help implement the DEVICE_RESUME() for
3559 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3562 bus_generic_resume(device_t dev)
3566 TAILQ_FOREACH(child, &dev->children, link) {
3567 DEVICE_RESUME(child);
3568 /* if resume fails, there's nothing we can usefully do... */
3574 * @brief Helper function for implementing BUS_PRINT_CHILD().
3576 * This function prints the first part of the ascii representation of
3577 * @p child, including its name, unit and description (if any - see
3578 * device_set_desc()).
3580 * @returns the number of characters printed
3583 bus_print_child_header(device_t dev, device_t child)
3587 if (device_get_desc(child)) {
3588 retval += device_printf(child, "<%s>", device_get_desc(child));
3590 retval += printf("%s", device_get_nameunit(child));
3597 * @brief Helper function for implementing BUS_PRINT_CHILD().
3599 * This function prints the last part of the ascii representation of
3600 * @p child, which consists of the string @c " on " followed by the
3601 * name and unit of the @p dev.
3603 * @returns the number of characters printed
3606 bus_print_child_footer(device_t dev, device_t child)
3608 return (printf(" on %s\n", device_get_nameunit(dev)));
3612 * @brief Helper function for implementing BUS_PRINT_CHILD().
3614 * This function simply calls bus_print_child_header() followed by
3615 * bus_print_child_footer().
3617 * @returns the number of characters printed
3620 bus_generic_print_child(device_t dev, device_t child)
3624 retval += bus_print_child_header(dev, child);
3625 retval += bus_print_child_footer(dev, child);
3631 * @brief Stub function for implementing BUS_READ_IVAR().
3636 bus_generic_read_ivar(device_t dev, device_t child, int index,
3643 * @brief Stub function for implementing BUS_WRITE_IVAR().
3648 bus_generic_write_ivar(device_t dev, device_t child, int index,
3655 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3659 struct resource_list *
3660 bus_generic_get_resource_list(device_t dev, device_t child)
3666 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3668 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3669 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3670 * and then calls device_probe_and_attach() for each unattached child.
3673 bus_generic_driver_added(device_t dev, driver_t *driver)
3677 DEVICE_IDENTIFY(driver, dev);
3678 TAILQ_FOREACH(child, &dev->children, link) {
3679 if (child->state == DS_NOTPRESENT ||
3680 (child->flags & DF_REBID))
3681 device_probe_and_attach(child);
3686 * @brief Helper function for implementing BUS_NEW_PASS().
3688 * This implementing of BUS_NEW_PASS() first calls the identify
3689 * routines for any drivers that probe at the current pass. Then it
3690 * walks the list of devices for this bus. If a device is already
3691 * attached, then it calls BUS_NEW_PASS() on that device. If the
3692 * device is not already attached, it attempts to attach a driver to
3696 bus_generic_new_pass(device_t dev)
3703 TAILQ_FOREACH(dl, &dc->drivers, link) {
3704 if (dl->pass == bus_current_pass)
3705 DEVICE_IDENTIFY(dl->driver, dev);
3707 TAILQ_FOREACH(child, &dev->children, link) {
3708 if (child->state >= DS_ATTACHED)
3709 BUS_NEW_PASS(child);
3710 else if (child->state == DS_NOTPRESENT)
3711 device_probe_and_attach(child);
3716 * @brief Helper function for implementing BUS_SETUP_INTR().
3718 * This simple implementation of BUS_SETUP_INTR() simply calls the
3719 * BUS_SETUP_INTR() method of the parent of @p dev.
3722 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3723 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3726 /* Propagate up the bus hierarchy until someone handles it. */
3728 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3729 filter, intr, arg, cookiep));
3734 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3736 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3737 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3740 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3743 /* Propagate up the bus hierarchy until someone handles it. */
3745 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3750 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3752 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3753 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3756 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3757 struct resource *r, u_long start, u_long end)
3759 /* Propagate up the bus hierarchy until someone handles it. */
3761 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3767 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3769 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3770 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3773 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3774 u_long start, u_long end, u_long count, u_int flags)
3776 /* Propagate up the bus hierarchy until someone handles it. */
3778 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3779 start, end, count, flags));
3784 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3786 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3787 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3790 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3793 /* Propagate up the bus hierarchy until someone handles it. */
3795 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3801 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3803 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3804 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3807 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3810 /* Propagate up the bus hierarchy until someone handles it. */
3812 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3818 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3820 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3821 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3824 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3825 int rid, struct resource *r)
3827 /* Propagate up the bus hierarchy until someone handles it. */
3829 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3835 * @brief Helper function for implementing BUS_BIND_INTR().
3837 * This simple implementation of BUS_BIND_INTR() simply calls the
3838 * BUS_BIND_INTR() method of the parent of @p dev.
3841 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3845 /* Propagate up the bus hierarchy until someone handles it. */
3847 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3852 * @brief Helper function for implementing BUS_CONFIG_INTR().
3854 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3855 * BUS_CONFIG_INTR() method of the parent of @p dev.
3858 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3859 enum intr_polarity pol)
3862 /* Propagate up the bus hierarchy until someone handles it. */
3864 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3869 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3871 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3872 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3875 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3876 void *cookie, const char *descr)
3879 /* Propagate up the bus hierarchy until someone handles it. */
3881 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3887 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3889 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3890 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3893 bus_generic_get_dma_tag(device_t dev, device_t child)
3896 /* Propagate up the bus hierarchy until someone handles it. */
3897 if (dev->parent != NULL)
3898 return (BUS_GET_DMA_TAG(dev->parent, child));
3903 * @brief Helper function for implementing BUS_GET_RESOURCE().
3905 * This implementation of BUS_GET_RESOURCE() uses the
3906 * resource_list_find() function to do most of the work. It calls
3907 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3911 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3912 u_long *startp, u_long *countp)
3914 struct resource_list * rl = NULL;
3915 struct resource_list_entry * rle = NULL;
3917 rl = BUS_GET_RESOURCE_LIST(dev, child);
3921 rle = resource_list_find(rl, type, rid);
3926 *startp = rle->start;
3928 *countp = rle->count;
3934 * @brief Helper function for implementing BUS_SET_RESOURCE().
3936 * This implementation of BUS_SET_RESOURCE() uses the
3937 * resource_list_add() function to do most of the work. It calls
3938 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3942 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3943 u_long start, u_long count)
3945 struct resource_list * rl = NULL;
3947 rl = BUS_GET_RESOURCE_LIST(dev, child);
3951 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3957 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3959 * This implementation of BUS_DELETE_RESOURCE() uses the
3960 * resource_list_delete() function to do most of the work. It calls
3961 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3965 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3967 struct resource_list * rl = NULL;
3969 rl = BUS_GET_RESOURCE_LIST(dev, child);
3973 resource_list_delete(rl, type, rid);
3979 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3981 * This implementation of BUS_RELEASE_RESOURCE() uses the
3982 * resource_list_release() function to do most of the work. It calls
3983 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3986 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3987 int rid, struct resource *r)
3989 struct resource_list * rl = NULL;
3991 if (device_get_parent(child) != dev)
3992 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
3995 rl = BUS_GET_RESOURCE_LIST(dev, child);
3999 return (resource_list_release(rl, dev, child, type, rid, r));
4003 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
4005 * This implementation of BUS_ALLOC_RESOURCE() uses the
4006 * resource_list_alloc() function to do most of the work. It calls
4007 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
4010 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
4011 int *rid, u_long start, u_long end, u_long count, u_int flags)
4013 struct resource_list * rl = NULL;
4015 if (device_get_parent(child) != dev)
4016 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
4017 type, rid, start, end, count, flags));
4019 rl = BUS_GET_RESOURCE_LIST(dev, child);
4023 return (resource_list_alloc(rl, dev, child, type, rid,
4024 start, end, count, flags));
4028 * @brief Helper function for implementing BUS_CHILD_PRESENT().
4030 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
4031 * BUS_CHILD_PRESENT() method of the parent of @p dev.
4034 bus_generic_child_present(device_t dev, device_t child)
4036 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
4040 * Some convenience functions to make it easier for drivers to use the
4041 * resource-management functions. All these really do is hide the
4042 * indirection through the parent's method table, making for slightly
4043 * less-wordy code. In the future, it might make sense for this code
4044 * to maintain some sort of a list of resources allocated by each device.
4048 bus_alloc_resources(device_t dev, struct resource_spec *rs,
4049 struct resource **res)
4053 for (i = 0; rs[i].type != -1; i++)
4055 for (i = 0; rs[i].type != -1; i++) {
4056 res[i] = bus_alloc_resource_any(dev,
4057 rs[i].type, &rs[i].rid, rs[i].flags);
4058 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
4059 bus_release_resources(dev, rs, res);
4067 bus_release_resources(device_t dev, const struct resource_spec *rs,
4068 struct resource **res)
4072 for (i = 0; rs[i].type != -1; i++)
4073 if (res[i] != NULL) {
4074 bus_release_resource(
4075 dev, rs[i].type, rs[i].rid, res[i]);
4081 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
4083 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
4087 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
4088 u_long count, u_int flags)
4090 if (dev->parent == NULL)
4092 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
4097 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
4099 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4103 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4106 if (dev->parent == NULL)
4108 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4112 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4114 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4118 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4120 if (dev->parent == NULL)
4122 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4126 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4128 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4132 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4134 if (dev->parent == NULL)
4136 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4140 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4142 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4146 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4148 if (dev->parent == NULL)
4150 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4154 * @brief Wrapper function for BUS_SETUP_INTR().
4156 * This function simply calls the BUS_SETUP_INTR() method of the
4160 bus_setup_intr(device_t dev, struct resource *r, int flags,
4161 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4165 if (dev->parent == NULL)
4167 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4171 if (handler != NULL && !(flags & INTR_MPSAFE))
4172 device_printf(dev, "[GIANT-LOCKED]\n");
4177 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4179 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4183 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4185 if (dev->parent == NULL)
4187 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4191 * @brief Wrapper function for BUS_BIND_INTR().
4193 * This function simply calls the BUS_BIND_INTR() method of the
4197 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4199 if (dev->parent == NULL)
4201 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4205 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4207 * This function first formats the requested description into a
4208 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4209 * the parent of @p dev.
4212 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4213 const char *fmt, ...)
4216 char descr[MAXCOMLEN + 1];
4218 if (dev->parent == NULL)
4221 vsnprintf(descr, sizeof(descr), fmt, ap);
4223 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4227 * @brief Wrapper function for BUS_SET_RESOURCE().
4229 * This function simply calls the BUS_SET_RESOURCE() method of the
4233 bus_set_resource(device_t dev, int type, int rid,
4234 u_long start, u_long count)
4236 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4241 * @brief Wrapper function for BUS_GET_RESOURCE().
4243 * This function simply calls the BUS_GET_RESOURCE() method of the
4247 bus_get_resource(device_t dev, int type, int rid,
4248 u_long *startp, u_long *countp)
4250 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4255 * @brief Wrapper function for BUS_GET_RESOURCE().
4257 * This function simply calls the BUS_GET_RESOURCE() method of the
4258 * parent of @p dev and returns the start value.
4261 bus_get_resource_start(device_t dev, int type, int rid)
4263 u_long start, count;
4266 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4274 * @brief Wrapper function for BUS_GET_RESOURCE().
4276 * This function simply calls the BUS_GET_RESOURCE() method of the
4277 * parent of @p dev and returns the count value.
4280 bus_get_resource_count(device_t dev, int type, int rid)
4282 u_long start, count;
4285 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4293 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4295 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4299 bus_delete_resource(device_t dev, int type, int rid)
4301 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4305 * @brief Wrapper function for BUS_CHILD_PRESENT().
4307 * This function simply calls the BUS_CHILD_PRESENT() method of the
4311 bus_child_present(device_t child)
4313 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4317 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4319 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4323 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4327 parent = device_get_parent(child);
4328 if (parent == NULL) {
4332 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4336 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4338 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4342 bus_child_location_str(device_t child, char *buf, size_t buflen)
4346 parent = device_get_parent(child);
4347 if (parent == NULL) {
4351 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4355 * @brief Wrapper function for BUS_GET_DMA_TAG().
4357 * This function simply calls the BUS_GET_DMA_TAG() method of the
4361 bus_get_dma_tag(device_t dev)
4365 parent = device_get_parent(dev);
4368 return (BUS_GET_DMA_TAG(parent, dev));
4371 /* Resume all devices and then notify userland that we're up again. */
4373 root_resume(device_t dev)
4377 error = bus_generic_resume(dev);
4379 devctl_notify("kern", "power", "resume", NULL);
4384 root_print_child(device_t dev, device_t child)
4388 retval += bus_print_child_header(dev, child);
4389 retval += printf("\n");
4395 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4396 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4399 * If an interrupt mapping gets to here something bad has happened.
4401 panic("root_setup_intr");
4405 * If we get here, assume that the device is permanant and really is
4406 * present in the system. Removable bus drivers are expected to intercept
4407 * this call long before it gets here. We return -1 so that drivers that
4408 * really care can check vs -1 or some ERRNO returned higher in the food
4412 root_child_present(device_t dev, device_t child)
4417 static kobj_method_t root_methods[] = {
4418 /* Device interface */
4419 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4420 KOBJMETHOD(device_suspend, bus_generic_suspend),
4421 KOBJMETHOD(device_resume, root_resume),
4424 KOBJMETHOD(bus_print_child, root_print_child),
4425 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4426 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4427 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4428 KOBJMETHOD(bus_child_present, root_child_present),
4433 static driver_t root_driver = {
4440 devclass_t root_devclass;
4443 root_bus_module_handler(module_t mod, int what, void* arg)
4447 TAILQ_INIT(&bus_data_devices);
4448 kobj_class_compile((kobj_class_t) &root_driver);
4449 root_bus = make_device(NULL, "root", 0);
4450 root_bus->desc = "System root bus";
4451 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4452 root_bus->driver = &root_driver;
4453 root_bus->state = DS_ATTACHED;
4454 root_devclass = devclass_find_internal("root", NULL, FALSE);
4459 device_shutdown(root_bus);
4462 return (EOPNOTSUPP);
4468 static moduledata_t root_bus_mod = {
4470 root_bus_module_handler,
4473 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4476 * @brief Automatically configure devices
4478 * This function begins the autoconfiguration process by calling
4479 * device_probe_and_attach() for each child of the @c root0 device.
4482 root_bus_configure(void)
4487 /* Eventually this will be split up, but this is sufficient for now. */
4488 bus_set_pass(BUS_PASS_DEFAULT);
4492 * @brief Module handler for registering device drivers
4494 * This module handler is used to automatically register device
4495 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4496 * devclass_add_driver() for the driver described by the
4497 * driver_module_data structure pointed to by @p arg
4500 driver_module_handler(module_t mod, int what, void *arg)
4502 struct driver_module_data *dmd;
4503 devclass_t bus_devclass;
4504 kobj_class_t driver;
4507 dmd = (struct driver_module_data *)arg;
4508 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4513 if (dmd->dmd_chainevh)
4514 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4516 pass = dmd->dmd_pass;
4517 driver = dmd->dmd_driver;
4518 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4519 DRIVERNAME(driver), dmd->dmd_busname, pass));
4520 error = devclass_add_driver(bus_devclass, driver, pass,
4525 PDEBUG(("Unloading module: driver %s from bus %s",
4526 DRIVERNAME(dmd->dmd_driver),
4528 error = devclass_delete_driver(bus_devclass,
4531 if (!error && dmd->dmd_chainevh)
4532 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4535 PDEBUG(("Quiesce module: driver %s from bus %s",
4536 DRIVERNAME(dmd->dmd_driver),
4538 error = devclass_quiesce_driver(bus_devclass,
4541 if (!error && dmd->dmd_chainevh)
4542 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4553 * @brief Enumerate all hinted devices for this bus.
4555 * Walks through the hints for this bus and calls the bus_hinted_child
4556 * routine for each one it fines. It searches first for the specific
4557 * bus that's being probed for hinted children (eg isa0), and then for
4558 * generic children (eg isa).
4560 * @param dev bus device to enumerate
4563 bus_enumerate_hinted_children(device_t bus)
4566 const char *dname, *busname;
4570 * enumerate all devices on the specific bus
4572 busname = device_get_nameunit(bus);
4574 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4575 BUS_HINTED_CHILD(bus, dname, dunit);
4578 * and all the generic ones.
4580 busname = device_get_name(bus);
4582 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4583 BUS_HINTED_CHILD(bus, dname, dunit);
4588 /* the _short versions avoid iteration by not calling anything that prints
4589 * more than oneliners. I love oneliners.
4593 print_device_short(device_t dev, int indent)
4598 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4599 dev->unit, dev->desc,
4600 (dev->parent? "":"no "),
4601 (TAILQ_EMPTY(&dev->children)? "no ":""),
4602 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4603 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4604 (dev->flags&DF_WILDCARD? "wildcard,":""),
4605 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4606 (dev->flags&DF_REBID? "rebiddable,":""),
4607 (dev->ivars? "":"no "),
4608 (dev->softc? "":"no "),
4613 print_device(device_t dev, int indent)
4618 print_device_short(dev, indent);
4620 indentprintf(("Parent:\n"));
4621 print_device_short(dev->parent, indent+1);
4622 indentprintf(("Driver:\n"));
4623 print_driver_short(dev->driver, indent+1);
4624 indentprintf(("Devclass:\n"));
4625 print_devclass_short(dev->devclass, indent+1);
4629 print_device_tree_short(device_t dev, int indent)
4630 /* print the device and all its children (indented) */
4637 print_device_short(dev, indent);
4639 TAILQ_FOREACH(child, &dev->children, link) {
4640 print_device_tree_short(child, indent+1);
4645 print_device_tree(device_t dev, int indent)
4646 /* print the device and all its children (indented) */
4653 print_device(dev, indent);
4655 TAILQ_FOREACH(child, &dev->children, link) {
4656 print_device_tree(child, indent+1);
4661 print_driver_short(driver_t *driver, int indent)
4666 indentprintf(("driver %s: softc size = %zd\n",
4667 driver->name, driver->size));
4671 print_driver(driver_t *driver, int indent)
4676 print_driver_short(driver, indent);
4680 print_driver_list(driver_list_t drivers, int indent)
4682 driverlink_t driver;
4684 TAILQ_FOREACH(driver, &drivers, link) {
4685 print_driver(driver->driver, indent);
4690 print_devclass_short(devclass_t dc, int indent)
4695 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4699 print_devclass(devclass_t dc, int indent)
4706 print_devclass_short(dc, indent);
4707 indentprintf(("Drivers:\n"));
4708 print_driver_list(dc->drivers, indent+1);
4710 indentprintf(("Devices:\n"));
4711 for (i = 0; i < dc->maxunit; i++)
4713 print_device(dc->devices[i], indent+1);
4717 print_devclass_list_short(void)
4721 printf("Short listing of devclasses, drivers & devices:\n");
4722 TAILQ_FOREACH(dc, &devclasses, link) {
4723 print_devclass_short(dc, 0);
4728 print_devclass_list(void)
4732 printf("Full listing of devclasses, drivers & devices:\n");
4733 TAILQ_FOREACH(dc, &devclasses, link) {
4734 print_devclass(dc, 0);
4741 * User-space access to the device tree.
4743 * We implement a small set of nodes:
4745 * hw.bus Single integer read method to obtain the
4746 * current generation count.
4747 * hw.bus.devices Reads the entire device tree in flat space.
4748 * hw.bus.rman Resource manager interface
4750 * We might like to add the ability to scan devclasses and/or drivers to
4751 * determine what else is currently loaded/available.
4755 sysctl_bus(SYSCTL_HANDLER_ARGS)
4757 struct u_businfo ubus;
4759 ubus.ub_version = BUS_USER_VERSION;
4760 ubus.ub_generation = bus_data_generation;
4762 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4764 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4765 "bus-related data");
4768 sysctl_devices(SYSCTL_HANDLER_ARGS)
4770 int *name = (int *)arg1;
4771 u_int namelen = arg2;
4774 struct u_device udev; /* XXX this is a bit big */
4780 if (bus_data_generation_check(name[0]))
4786 * Scan the list of devices, looking for the requested index.
4788 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4796 * Populate the return array.
4798 bzero(&udev, sizeof(udev));
4799 udev.dv_handle = (uintptr_t)dev;
4800 udev.dv_parent = (uintptr_t)dev->parent;
4801 if (dev->nameunit != NULL)
4802 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4803 if (dev->desc != NULL)
4804 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4805 if (dev->driver != NULL && dev->driver->name != NULL)
4806 strlcpy(udev.dv_drivername, dev->driver->name,
4807 sizeof(udev.dv_drivername));
4808 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4809 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4810 udev.dv_devflags = dev->devflags;
4811 udev.dv_flags = dev->flags;
4812 udev.dv_state = dev->state;
4813 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4817 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4818 "system device tree");
4821 bus_data_generation_check(int generation)
4823 if (generation != bus_data_generation)
4826 /* XXX generate optimised lists here? */
4831 bus_data_generation_update(void)
4833 bus_data_generation++;
4837 bus_free_resource(device_t dev, int type, struct resource *r)
4841 return (bus_release_resource(dev, type, rman_get_rid(r), r));