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>
56 #include <machine/stdarg.h>
60 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
61 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
64 * Used to attach drivers to devclasses.
66 typedef struct driverlink *driverlink_t;
69 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
71 TAILQ_ENTRY(driverlink) passlink;
75 * Forward declarations
77 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
78 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
79 typedef TAILQ_HEAD(device_list, device) device_list_t;
82 TAILQ_ENTRY(devclass) link;
83 devclass_t parent; /* parent in devclass hierarchy */
84 driver_list_t drivers; /* bus devclasses store drivers for bus */
86 device_t *devices; /* array of devices indexed by unit */
87 int maxunit; /* size of devices array */
89 #define DC_HAS_CHILDREN 1
91 struct sysctl_ctx_list sysctl_ctx;
92 struct sysctl_oid *sysctl_tree;
96 * @brief Implementation of device.
100 * A device is a kernel object. The first field must be the
101 * current ops table for the object.
108 TAILQ_ENTRY(device) link; /**< list of devices in parent */
109 TAILQ_ENTRY(device) devlink; /**< global device list membership */
110 device_t parent; /**< parent of this device */
111 device_list_t children; /**< list of child devices */
114 * Details of this device.
116 driver_t *driver; /**< current driver */
117 devclass_t devclass; /**< current device class */
118 int unit; /**< current unit number */
119 char* nameunit; /**< name+unit e.g. foodev0 */
120 char* desc; /**< driver specific description */
121 int busy; /**< count of calls to device_busy() */
122 device_state_t state; /**< current device state */
123 u_int32_t devflags; /**< api level flags for device_get_flags() */
124 u_short flags; /**< internal device flags */
125 #define DF_ENABLED 1 /* device should be probed/attached */
126 #define DF_FIXEDCLASS 2 /* devclass specified at create time */
127 #define DF_WILDCARD 4 /* unit was originally wildcard */
128 #define DF_DESCMALLOCED 8 /* description was malloced */
129 #define DF_QUIET 16 /* don't print verbose attach message */
130 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */
131 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */
132 #define DF_REBID 128 /* Can rebid after attach */
133 u_char order; /**< order from device_add_child_ordered() */
135 void *ivars; /**< instance variables */
136 void *softc; /**< current driver's variables */
138 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
139 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
142 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
143 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
147 static int bus_debug = 1;
148 TUNABLE_INT("bus.debug", &bus_debug);
149 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
152 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
153 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
154 #define DRIVERNAME(d) ((d)? d->name : "no driver")
155 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
158 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
159 * prevent syslog from deleting initial spaces
161 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
163 static void print_device_short(device_t dev, int indent);
164 static void print_device(device_t dev, int indent);
165 void print_device_tree_short(device_t dev, int indent);
166 void print_device_tree(device_t dev, int indent);
167 static void print_driver_short(driver_t *driver, int indent);
168 static void print_driver(driver_t *driver, int indent);
169 static void print_driver_list(driver_list_t drivers, int indent);
170 static void print_devclass_short(devclass_t dc, int indent);
171 static void print_devclass(devclass_t dc, int indent);
172 void print_devclass_list_short(void);
173 void print_devclass_list(void);
176 /* Make the compiler ignore the function calls */
177 #define PDEBUG(a) /* nop */
178 #define DEVICENAME(d) /* nop */
179 #define DRIVERNAME(d) /* nop */
180 #define DEVCLANAME(d) /* nop */
182 #define print_device_short(d,i) /* nop */
183 #define print_device(d,i) /* nop */
184 #define print_device_tree_short(d,i) /* nop */
185 #define print_device_tree(d,i) /* nop */
186 #define print_driver_short(d,i) /* nop */
187 #define print_driver(d,i) /* nop */
188 #define print_driver_list(d,i) /* nop */
189 #define print_devclass_short(d,i) /* nop */
190 #define print_devclass(d,i) /* nop */
191 #define print_devclass_list_short() /* nop */
192 #define print_devclass_list() /* nop */
200 DEVCLASS_SYSCTL_PARENT,
204 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
206 devclass_t dc = (devclass_t)arg1;
210 case DEVCLASS_SYSCTL_PARENT:
211 value = dc->parent ? dc->parent->name : "";
216 return (SYSCTL_OUT(req, value, strlen(value)));
220 devclass_sysctl_init(devclass_t dc)
223 if (dc->sysctl_tree != NULL)
225 sysctl_ctx_init(&dc->sysctl_ctx);
226 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
227 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
228 CTLFLAG_RD, NULL, "");
229 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
230 OID_AUTO, "%parent", CTLFLAG_RD,
231 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
237 DEVICE_SYSCTL_DRIVER,
238 DEVICE_SYSCTL_LOCATION,
239 DEVICE_SYSCTL_PNPINFO,
240 DEVICE_SYSCTL_PARENT,
244 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
246 device_t dev = (device_t)arg1;
253 case DEVICE_SYSCTL_DESC:
254 value = dev->desc ? dev->desc : "";
256 case DEVICE_SYSCTL_DRIVER:
257 value = dev->driver ? dev->driver->name : "";
259 case DEVICE_SYSCTL_LOCATION:
260 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
261 bus_child_location_str(dev, buf, 1024);
263 case DEVICE_SYSCTL_PNPINFO:
264 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
265 bus_child_pnpinfo_str(dev, buf, 1024);
267 case DEVICE_SYSCTL_PARENT:
268 value = dev->parent ? dev->parent->nameunit : "";
273 error = SYSCTL_OUT(req, value, strlen(value));
280 device_sysctl_init(device_t dev)
282 devclass_t dc = dev->devclass;
284 if (dev->sysctl_tree != NULL)
286 devclass_sysctl_init(dc);
287 sysctl_ctx_init(&dev->sysctl_ctx);
288 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
289 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
290 dev->nameunit + strlen(dc->name),
291 CTLFLAG_RD, NULL, "");
292 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
293 OID_AUTO, "%desc", CTLFLAG_RD,
294 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
295 "device description");
296 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
297 OID_AUTO, "%driver", CTLFLAG_RD,
298 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
299 "device driver name");
300 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
301 OID_AUTO, "%location", CTLFLAG_RD,
302 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
303 "device location relative to parent");
304 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
305 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
306 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
307 "device identification");
308 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
309 OID_AUTO, "%parent", CTLFLAG_RD,
310 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
315 device_sysctl_update(device_t dev)
317 devclass_t dc = dev->devclass;
319 if (dev->sysctl_tree == NULL)
321 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
325 device_sysctl_fini(device_t dev)
327 if (dev->sysctl_tree == NULL)
329 sysctl_ctx_free(&dev->sysctl_ctx);
330 dev->sysctl_tree = NULL;
334 * /dev/devctl implementation
338 * This design allows only one reader for /dev/devctl. This is not desirable
339 * in the long run, but will get a lot of hair out of this implementation.
340 * Maybe we should make this device a clonable device.
342 * Also note: we specifically do not attach a device to the device_t tree
343 * to avoid potential chicken and egg problems. One could argue that all
344 * of this belongs to the root node. One could also further argue that the
345 * sysctl interface that we have not might more properly be an ioctl
346 * interface, but at this stage of the game, I'm not inclined to rock that
349 * I'm also not sure that the SIGIO support is done correctly or not, as
350 * I copied it from a driver that had SIGIO support that likely hasn't been
351 * tested since 3.4 or 2.2.8!
354 /* Deprecated way to adjust queue length */
355 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
356 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
357 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
358 0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
360 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
361 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
362 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
363 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
364 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
365 0, sysctl_devctl_queue, "I", "devctl queue length");
367 static d_open_t devopen;
368 static d_close_t devclose;
369 static d_read_t devread;
370 static d_ioctl_t devioctl;
371 static d_poll_t devpoll;
373 static struct cdevsw dev_cdevsw = {
374 .d_version = D_VERSION,
375 .d_flags = D_NEEDGIANT,
384 struct dev_event_info
387 TAILQ_ENTRY(dev_event_info) dei_link;
390 TAILQ_HEAD(devq, dev_event_info);
392 static struct dev_softc
401 struct proc *async_proc;
404 static struct cdev *devctl_dev;
409 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
411 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
412 cv_init(&devsoftc.cv, "dev cv");
413 TAILQ_INIT(&devsoftc.devq);
417 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
423 devsoftc.nonblock = 0;
424 devsoftc.async_proc = NULL;
429 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
432 mtx_lock(&devsoftc.mtx);
433 cv_broadcast(&devsoftc.cv);
434 mtx_unlock(&devsoftc.mtx);
435 devsoftc.async_proc = NULL;
440 * The read channel for this device is used to report changes to
441 * userland in realtime. We are required to free the data as well as
442 * the n1 object because we allocate them separately. Also note that
443 * we return one record at a time. If you try to read this device a
444 * character at a time, you will lose the rest of the data. Listening
445 * programs are expected to cope.
448 devread(struct cdev *dev, struct uio *uio, int ioflag)
450 struct dev_event_info *n1;
453 mtx_lock(&devsoftc.mtx);
454 while (TAILQ_EMPTY(&devsoftc.devq)) {
455 if (devsoftc.nonblock) {
456 mtx_unlock(&devsoftc.mtx);
459 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
462 * Need to translate ERESTART to EINTR here? -- jake
464 mtx_unlock(&devsoftc.mtx);
468 n1 = TAILQ_FIRST(&devsoftc.devq);
469 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
471 mtx_unlock(&devsoftc.mtx);
472 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
473 free(n1->dei_data, M_BUS);
479 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
485 devsoftc.nonblock = 1;
487 devsoftc.nonblock = 0;
491 devsoftc.async_proc = td->td_proc;
493 devsoftc.async_proc = NULL;
496 /* (un)Support for other fcntl() calls. */
509 devpoll(struct cdev *dev, int events, struct thread *td)
513 mtx_lock(&devsoftc.mtx);
514 if (events & (POLLIN | POLLRDNORM)) {
515 if (!TAILQ_EMPTY(&devsoftc.devq))
516 revents = events & (POLLIN | POLLRDNORM);
518 selrecord(td, &devsoftc.sel);
520 mtx_unlock(&devsoftc.mtx);
526 * @brief Return whether the userland process is running
529 devctl_process_running(void)
531 return (devsoftc.inuse == 1);
535 * @brief Queue data to be read from the devctl device
537 * Generic interface to queue data to the devctl device. It is
538 * assumed that @p data is properly formatted. It is further assumed
539 * that @p data is allocated using the M_BUS malloc type.
542 devctl_queue_data(char *data)
544 struct dev_event_info *n1 = NULL, *n2 = NULL;
547 if (strlen(data) == 0)
549 if (devctl_queue_length == 0)
551 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
555 mtx_lock(&devsoftc.mtx);
556 if (devctl_queue_length == 0) {
557 free(n1->dei_data, M_BUS);
561 /* Leave at least one spot in the queue... */
562 while (devsoftc.queued > devctl_queue_length - 1) {
563 n2 = TAILQ_FIRST(&devsoftc.devq);
564 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
565 free(n2->dei_data, M_BUS);
569 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
571 cv_broadcast(&devsoftc.cv);
572 mtx_unlock(&devsoftc.mtx);
573 selwakeup(&devsoftc.sel);
574 p = devsoftc.async_proc;
583 * @brief Send a 'notification' to userland, using standard ways
586 devctl_notify(const char *system, const char *subsystem, const char *type,
593 return; /* BOGUS! Must specify system. */
594 if (subsystem == NULL)
595 return; /* BOGUS! Must specify subsystem. */
597 return; /* BOGUS! Must specify type. */
598 len += strlen(" system=") + strlen(system);
599 len += strlen(" subsystem=") + strlen(subsystem);
600 len += strlen(" type=") + strlen(type);
601 /* add in the data message plus newline. */
604 len += 3; /* '!', '\n', and NUL */
605 msg = malloc(len, M_BUS, M_NOWAIT);
607 return; /* Drop it on the floor */
609 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
610 system, subsystem, type, data);
612 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
613 system, subsystem, type);
614 devctl_queue_data(msg);
618 * Common routine that tries to make sending messages as easy as possible.
619 * We allocate memory for the data, copy strings into that, but do not
620 * free it unless there's an error. The dequeue part of the driver should
621 * free the data. We don't send data when the device is disabled. We do
622 * send data, even when we have no listeners, because we wish to avoid
623 * races relating to startup and restart of listening applications.
625 * devaddq is designed to string together the type of event, with the
626 * object of that event, plus the plug and play info and location info
627 * for that event. This is likely most useful for devices, but less
628 * useful for other consumers of this interface. Those should use
629 * the devctl_queue_data() interface instead.
632 devaddq(const char *type, const char *what, device_t dev)
639 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
641 data = malloc(1024, M_BUS, M_NOWAIT);
645 /* get the bus specific location of this device */
646 loc = malloc(1024, M_BUS, M_NOWAIT);
650 bus_child_location_str(dev, loc, 1024);
652 /* Get the bus specific pnp info of this device */
653 pnp = malloc(1024, M_BUS, M_NOWAIT);
657 bus_child_pnpinfo_str(dev, pnp, 1024);
659 /* Get the parent of this device, or / if high enough in the tree. */
660 if (device_get_parent(dev) == NULL)
661 parstr = "."; /* Or '/' ? */
663 parstr = device_get_nameunit(device_get_parent(dev));
664 /* String it all together. */
665 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
669 devctl_queue_data(data);
679 * A device was added to the tree. We are called just after it successfully
680 * attaches (that is, probe and attach success for this device). No call
681 * is made if a device is merely parented into the tree. See devnomatch
682 * if probe fails. If attach fails, no notification is sent (but maybe
683 * we should have a different message for this).
686 devadded(device_t dev)
691 pnp = malloc(1024, M_BUS, M_NOWAIT);
694 tmp = malloc(1024, M_BUS, M_NOWAIT);
698 bus_child_pnpinfo_str(dev, pnp, 1024);
699 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
700 devaddq("+", tmp, dev);
710 * A device was removed from the tree. We are called just before this
714 devremoved(device_t dev)
719 pnp = malloc(1024, M_BUS, M_NOWAIT);
722 tmp = malloc(1024, M_BUS, M_NOWAIT);
726 bus_child_pnpinfo_str(dev, pnp, 1024);
727 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
728 devaddq("-", tmp, dev);
738 * Called when there's no match for this device. This is only called
739 * the first time that no match happens, so we don't keep getting this
740 * message. Should that prove to be undesirable, we can change it.
741 * This is called when all drivers that can attach to a given bus
742 * decline to accept this device. Other errrors may not be detected.
745 devnomatch(device_t dev)
747 devaddq("?", "", dev);
751 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
753 struct dev_event_info *n1;
756 dis = devctl_queue_length == 0;
757 error = sysctl_handle_int(oidp, &dis, 0, req);
758 if (error || !req->newptr)
760 mtx_lock(&devsoftc.mtx);
762 while (!TAILQ_EMPTY(&devsoftc.devq)) {
763 n1 = TAILQ_FIRST(&devsoftc.devq);
764 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
765 free(n1->dei_data, M_BUS);
769 devctl_queue_length = 0;
771 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
773 mtx_unlock(&devsoftc.mtx);
778 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
780 struct dev_event_info *n1;
783 q = devctl_queue_length;
784 error = sysctl_handle_int(oidp, &q, 0, req);
785 if (error || !req->newptr)
789 mtx_lock(&devsoftc.mtx);
790 devctl_queue_length = q;
791 while (devsoftc.queued > devctl_queue_length) {
792 n1 = TAILQ_FIRST(&devsoftc.devq);
793 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
794 free(n1->dei_data, M_BUS);
798 mtx_unlock(&devsoftc.mtx);
802 /* End of /dev/devctl code */
804 static TAILQ_HEAD(,device) bus_data_devices;
805 static int bus_data_generation = 1;
807 static kobj_method_t null_methods[] = {
811 DEFINE_CLASS(null, null_methods, 0);
814 * Bus pass implementation
817 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
818 int bus_current_pass = BUS_PASS_ROOT;
822 * @brief Register the pass level of a new driver attachment
824 * Register a new driver attachment's pass level. If no driver
825 * attachment with the same pass level has been added, then @p new
826 * will be added to the global passes list.
828 * @param new the new driver attachment
831 driver_register_pass(struct driverlink *new)
833 struct driverlink *dl;
835 /* We only consider pass numbers during boot. */
836 if (bus_current_pass == BUS_PASS_DEFAULT)
840 * Walk the passes list. If we already know about this pass
841 * then there is nothing to do. If we don't, then insert this
842 * driver link into the list.
844 TAILQ_FOREACH(dl, &passes, passlink) {
845 if (dl->pass < new->pass)
847 if (dl->pass == new->pass)
849 TAILQ_INSERT_BEFORE(dl, new, passlink);
852 TAILQ_INSERT_TAIL(&passes, new, passlink);
856 * @brief Raise the current bus pass
858 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
859 * method on the root bus to kick off a new device tree scan for each
860 * new pass level that has at least one driver.
863 bus_set_pass(int pass)
865 struct driverlink *dl;
867 if (bus_current_pass > pass)
868 panic("Attempt to lower bus pass level");
870 TAILQ_FOREACH(dl, &passes, passlink) {
871 /* Skip pass values below the current pass level. */
872 if (dl->pass <= bus_current_pass)
876 * Bail once we hit a driver with a pass level that is
883 * Raise the pass level to the next level and rescan
886 bus_current_pass = dl->pass;
887 BUS_NEW_PASS(root_bus);
891 * If there isn't a driver registered for the requested pass,
892 * then bus_current_pass might still be less than 'pass'. Set
893 * it to 'pass' in that case.
895 if (bus_current_pass < pass)
896 bus_current_pass = pass;
897 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
901 * Devclass implementation
904 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
908 * @brief Find or create a device class
910 * If a device class with the name @p classname exists, return it,
911 * otherwise if @p create is non-zero create and return a new device
914 * If @p parentname is non-NULL, the parent of the devclass is set to
915 * the devclass of that name.
917 * @param classname the devclass name to find or create
918 * @param parentname the parent devclass name or @c NULL
919 * @param create non-zero to create a devclass
922 devclass_find_internal(const char *classname, const char *parentname,
927 PDEBUG(("looking for %s", classname));
931 TAILQ_FOREACH(dc, &devclasses, link) {
932 if (!strcmp(dc->name, classname))
937 PDEBUG(("creating %s", classname));
938 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
939 M_BUS, M_NOWAIT | M_ZERO);
943 dc->name = (char*) (dc + 1);
944 strcpy(dc->name, classname);
945 TAILQ_INIT(&dc->drivers);
946 TAILQ_INSERT_TAIL(&devclasses, dc, link);
948 bus_data_generation_update();
952 * If a parent class is specified, then set that as our parent so
953 * that this devclass will support drivers for the parent class as
954 * well. If the parent class has the same name don't do this though
955 * as it creates a cycle that can trigger an infinite loop in
956 * device_probe_child() if a device exists for which there is no
959 if (parentname && dc && !dc->parent &&
960 strcmp(classname, parentname) != 0) {
961 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
962 dc->parent->flags |= DC_HAS_CHILDREN;
969 * @brief Create a device class
971 * If a device class with the name @p classname exists, return it,
972 * otherwise create and return a new device class.
974 * @param classname the devclass name to find or create
977 devclass_create(const char *classname)
979 return (devclass_find_internal(classname, NULL, TRUE));
983 * @brief Find a device class
985 * If a device class with the name @p classname exists, return it,
986 * otherwise return @c NULL.
988 * @param classname the devclass name to find
991 devclass_find(const char *classname)
993 return (devclass_find_internal(classname, NULL, FALSE));
997 * @brief Register that a device driver has been added to a devclass
999 * Register that a device driver has been added to a devclass. This
1000 * is called by devclass_add_driver to accomplish the recursive
1001 * notification of all the children classes of dc, as well as dc.
1002 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1003 * the devclass. We do a full search here of the devclass list at
1004 * each iteration level to save storing children-lists in the devclass
1005 * structure. If we ever move beyond a few dozen devices doing this,
1006 * we may need to reevaluate...
1008 * @param dc the devclass to edit
1009 * @param driver the driver that was just added
1012 devclass_driver_added(devclass_t dc, driver_t *driver)
1018 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1020 for (i = 0; i < dc->maxunit; i++)
1021 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1022 BUS_DRIVER_ADDED(dc->devices[i], driver);
1025 * Walk through the children classes. Since we only keep a
1026 * single parent pointer around, we walk the entire list of
1027 * devclasses looking for children. We set the
1028 * DC_HAS_CHILDREN flag when a child devclass is created on
1029 * the parent, so we only walk the list for those devclasses
1030 * that have children.
1032 if (!(dc->flags & DC_HAS_CHILDREN))
1035 TAILQ_FOREACH(dc, &devclasses, link) {
1036 if (dc->parent == parent)
1037 devclass_driver_added(dc, driver);
1042 * @brief Add a device driver to a device class
1044 * Add a device driver to a devclass. This is normally called
1045 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1046 * all devices in the devclass will be called to allow them to attempt
1047 * to re-probe any unmatched children.
1049 * @param dc the devclass to edit
1050 * @param driver the driver to register
1053 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1056 const char *parentname;
1058 PDEBUG(("%s", DRIVERNAME(driver)));
1060 /* Don't allow invalid pass values. */
1061 if (pass <= BUS_PASS_ROOT)
1064 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1069 * Compile the driver's methods. Also increase the reference count
1070 * so that the class doesn't get freed when the last instance
1071 * goes. This means we can safely use static methods and avoids a
1072 * double-free in devclass_delete_driver.
1074 kobj_class_compile((kobj_class_t) driver);
1077 * If the driver has any base classes, make the
1078 * devclass inherit from the devclass of the driver's
1079 * first base class. This will allow the system to
1080 * search for drivers in both devclasses for children
1081 * of a device using this driver.
1083 if (driver->baseclasses)
1084 parentname = driver->baseclasses[0]->name;
1087 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1089 dl->driver = driver;
1090 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1091 driver->refs++; /* XXX: kobj_mtx */
1093 driver_register_pass(dl);
1095 devclass_driver_added(dc, driver);
1096 bus_data_generation_update();
1101 * @brief Delete a device driver from a device class
1103 * Delete a device driver from a devclass. This is normally called
1104 * automatically by DRIVER_MODULE().
1106 * If the driver is currently attached to any devices,
1107 * devclass_delete_driver() will first attempt to detach from each
1108 * device. If one of the detach calls fails, the driver will not be
1111 * @param dc the devclass to edit
1112 * @param driver the driver to unregister
1115 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1117 devclass_t dc = devclass_find(driver->name);
1123 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1129 * Find the link structure in the bus' list of drivers.
1131 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1132 if (dl->driver == driver)
1137 PDEBUG(("%s not found in %s list", driver->name,
1143 * Disassociate from any devices. We iterate through all the
1144 * devices in the devclass of the driver and detach any which are
1145 * using the driver and which have a parent in the devclass which
1146 * we are deleting from.
1148 * Note that since a driver can be in multiple devclasses, we
1149 * should not detach devices which are not children of devices in
1150 * the affected devclass.
1152 for (i = 0; i < dc->maxunit; i++) {
1153 if (dc->devices[i]) {
1154 dev = dc->devices[i];
1155 if (dev->driver == driver && dev->parent &&
1156 dev->parent->devclass == busclass) {
1157 if ((error = device_detach(dev)) != 0)
1159 device_set_driver(dev, NULL);
1164 TAILQ_REMOVE(&busclass->drivers, dl, link);
1169 if (driver->refs == 0)
1170 kobj_class_free((kobj_class_t) driver);
1172 bus_data_generation_update();
1177 * @brief Quiesces a set of device drivers from a device class
1179 * Quiesce a device driver from a devclass. This is normally called
1180 * automatically by DRIVER_MODULE().
1182 * If the driver is currently attached to any devices,
1183 * devclass_quiesece_driver() will first attempt to quiesce each
1186 * @param dc the devclass to edit
1187 * @param driver the driver to unregister
1190 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1192 devclass_t dc = devclass_find(driver->name);
1198 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1204 * Find the link structure in the bus' list of drivers.
1206 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1207 if (dl->driver == driver)
1212 PDEBUG(("%s not found in %s list", driver->name,
1218 * Quiesce all devices. We iterate through all the devices in
1219 * the devclass of the driver and quiesce any which are using
1220 * the driver and which have a parent in the devclass which we
1223 * Note that since a driver can be in multiple devclasses, we
1224 * should not quiesce devices which are not children of
1225 * devices in the affected devclass.
1227 for (i = 0; i < dc->maxunit; i++) {
1228 if (dc->devices[i]) {
1229 dev = dc->devices[i];
1230 if (dev->driver == driver && dev->parent &&
1231 dev->parent->devclass == busclass) {
1232 if ((error = device_quiesce(dev)) != 0)
1245 devclass_find_driver_internal(devclass_t dc, const char *classname)
1249 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1251 TAILQ_FOREACH(dl, &dc->drivers, link) {
1252 if (!strcmp(dl->driver->name, classname))
1256 PDEBUG(("not found"));
1261 * @brief Return the name of the devclass
1264 devclass_get_name(devclass_t dc)
1270 * @brief Find a device given a unit number
1272 * @param dc the devclass to search
1273 * @param unit the unit number to search for
1275 * @returns the device with the given unit number or @c
1276 * NULL if there is no such device
1279 devclass_get_device(devclass_t dc, int unit)
1281 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1283 return (dc->devices[unit]);
1287 * @brief Find the softc field of a device given a unit number
1289 * @param dc the devclass to search
1290 * @param unit the unit number to search for
1292 * @returns the softc field of the device with the given
1293 * unit number or @c NULL if there is no such
1297 devclass_get_softc(devclass_t dc, int unit)
1301 dev = devclass_get_device(dc, unit);
1305 return (device_get_softc(dev));
1309 * @brief Get a list of devices in the devclass
1311 * An array containing a list of all the devices in the given devclass
1312 * is allocated and returned in @p *devlistp. The number of devices
1313 * in the array is returned in @p *devcountp. The caller should free
1314 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1316 * @param dc the devclass to examine
1317 * @param devlistp points at location for array pointer return
1319 * @param devcountp points at location for array size return value
1322 * @retval ENOMEM the array allocation failed
1325 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1330 count = devclass_get_count(dc);
1331 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1336 for (i = 0; i < dc->maxunit; i++) {
1337 if (dc->devices[i]) {
1338 list[count] = dc->devices[i];
1350 * @brief Get a list of drivers in the devclass
1352 * An array containing a list of pointers to all the drivers in the
1353 * given devclass is allocated and returned in @p *listp. The number
1354 * of drivers in the array is returned in @p *countp. The caller should
1355 * free the array using @c free(p, M_TEMP).
1357 * @param dc the devclass to examine
1358 * @param listp gives location for array pointer return value
1359 * @param countp gives location for number of array elements
1363 * @retval ENOMEM the array allocation failed
1366 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1373 TAILQ_FOREACH(dl, &dc->drivers, link)
1375 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1380 TAILQ_FOREACH(dl, &dc->drivers, link) {
1381 list[count] = dl->driver;
1391 * @brief Get the number of devices in a devclass
1393 * @param dc the devclass to examine
1396 devclass_get_count(devclass_t dc)
1401 for (i = 0; i < dc->maxunit; i++)
1408 * @brief Get the maximum unit number used in a devclass
1410 * Note that this is one greater than the highest currently-allocated
1411 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1412 * that not even the devclass has been allocated yet.
1414 * @param dc the devclass to examine
1417 devclass_get_maxunit(devclass_t dc)
1421 return (dc->maxunit);
1425 * @brief Find a free unit number in a devclass
1427 * This function searches for the first unused unit number greater
1428 * that or equal to @p unit.
1430 * @param dc the devclass to examine
1431 * @param unit the first unit number to check
1434 devclass_find_free_unit(devclass_t dc, int unit)
1438 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1444 * @brief Set the parent of a devclass
1446 * The parent class is normally initialised automatically by
1449 * @param dc the devclass to edit
1450 * @param pdc the new parent devclass
1453 devclass_set_parent(devclass_t dc, devclass_t pdc)
1459 * @brief Get the parent of a devclass
1461 * @param dc the devclass to examine
1464 devclass_get_parent(devclass_t dc)
1466 return (dc->parent);
1469 struct sysctl_ctx_list *
1470 devclass_get_sysctl_ctx(devclass_t dc)
1472 return (&dc->sysctl_ctx);
1476 devclass_get_sysctl_tree(devclass_t dc)
1478 return (dc->sysctl_tree);
1483 * @brief Allocate a unit number
1485 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1486 * will do). The allocated unit number is returned in @p *unitp.
1488 * @param dc the devclass to allocate from
1489 * @param unitp points at the location for the allocated unit
1493 * @retval EEXIST the requested unit number is already allocated
1494 * @retval ENOMEM memory allocation failure
1497 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1502 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1504 /* Ask the parent bus if it wants to wire this device. */
1506 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1509 /* If we were given a wired unit number, check for existing device */
1512 if (unit >= 0 && unit < dc->maxunit &&
1513 dc->devices[unit] != NULL) {
1515 printf("%s: %s%d already exists; skipping it\n",
1516 dc->name, dc->name, *unitp);
1520 /* Unwired device, find the next available slot for it */
1522 for (unit = 0;; unit++) {
1523 /* If there is an "at" hint for a unit then skip it. */
1524 if (resource_string_value(dc->name, unit, "at", &s) ==
1528 /* If this device slot is already in use, skip it. */
1529 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1537 * We've selected a unit beyond the length of the table, so let's
1538 * extend the table to make room for all units up to and including
1541 if (unit >= dc->maxunit) {
1542 device_t *newlist, *oldlist;
1545 oldlist = dc->devices;
1546 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1547 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1550 if (oldlist != NULL)
1551 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1552 bzero(newlist + dc->maxunit,
1553 sizeof(device_t) * (newsize - dc->maxunit));
1554 dc->devices = newlist;
1555 dc->maxunit = newsize;
1556 if (oldlist != NULL)
1557 free(oldlist, M_BUS);
1559 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1567 * @brief Add a device to a devclass
1569 * A unit number is allocated for the device (using the device's
1570 * preferred unit number if any) and the device is registered in the
1571 * devclass. This allows the device to be looked up by its unit
1572 * number, e.g. by decoding a dev_t minor number.
1574 * @param dc the devclass to add to
1575 * @param dev the device to add
1578 * @retval EEXIST the requested unit number is already allocated
1579 * @retval ENOMEM memory allocation failure
1582 devclass_add_device(devclass_t dc, device_t dev)
1586 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1588 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1591 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1595 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1596 free(dev->nameunit, M_BUS);
1597 dev->nameunit = NULL;
1600 dc->devices[dev->unit] = dev;
1602 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1609 * @brief Delete a device from a devclass
1611 * The device is removed from the devclass's device list and its unit
1614 * @param dc the devclass to delete from
1615 * @param dev the device to delete
1620 devclass_delete_device(devclass_t dc, device_t dev)
1625 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1627 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1628 panic("devclass_delete_device: inconsistent device class");
1629 dc->devices[dev->unit] = NULL;
1630 if (dev->flags & DF_WILDCARD)
1632 dev->devclass = NULL;
1633 free(dev->nameunit, M_BUS);
1634 dev->nameunit = NULL;
1641 * @brief Make a new device and add it as a child of @p parent
1643 * @param parent the parent of the new device
1644 * @param name the devclass name of the new device or @c NULL
1645 * to leave the devclass unspecified
1646 * @parem unit the unit number of the new device of @c -1 to
1647 * leave the unit number unspecified
1649 * @returns the new device
1652 make_device(device_t parent, const char *name, int unit)
1657 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1660 dc = devclass_find_internal(name, NULL, TRUE);
1662 printf("make_device: can't find device class %s\n",
1670 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1674 dev->parent = parent;
1675 TAILQ_INIT(&dev->children);
1676 kobj_init((kobj_t) dev, &null_class);
1678 dev->devclass = NULL;
1680 dev->nameunit = NULL;
1684 dev->flags = DF_ENABLED;
1687 dev->flags |= DF_WILDCARD;
1689 dev->flags |= DF_FIXEDCLASS;
1690 if (devclass_add_device(dc, dev)) {
1691 kobj_delete((kobj_t) dev, M_BUS);
1698 dev->state = DS_NOTPRESENT;
1700 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1701 bus_data_generation_update();
1708 * @brief Print a description of a device.
1711 device_print_child(device_t dev, device_t child)
1715 if (device_is_alive(child))
1716 retval += BUS_PRINT_CHILD(dev, child);
1718 retval += device_printf(child, " not found\n");
1724 * @brief Create a new device
1726 * This creates a new device and adds it as a child of an existing
1727 * parent device. The new device will be added after the last existing
1728 * child with order zero.
1730 * @param dev the device which will be the parent of the
1732 * @param name devclass name for new device or @c NULL if not
1734 * @param unit unit number for new device or @c -1 if not
1737 * @returns the new device
1740 device_add_child(device_t dev, const char *name, int unit)
1742 return (device_add_child_ordered(dev, 0, name, unit));
1746 * @brief Create a new device
1748 * This creates a new device and adds it as a child of an existing
1749 * parent device. The new device will be added after the last existing
1750 * child with the same order.
1752 * @param dev the device which will be the parent of the
1754 * @param order a value which is used to partially sort the
1755 * children of @p dev - devices created using
1756 * lower values of @p order appear first in @p
1757 * dev's list of children
1758 * @param name devclass name for new device or @c NULL if not
1760 * @param unit unit number for new device or @c -1 if not
1763 * @returns the new device
1766 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1771 PDEBUG(("%s at %s with order %d as unit %d",
1772 name, DEVICENAME(dev), order, unit));
1774 child = make_device(dev, name, unit);
1777 child->order = order;
1779 TAILQ_FOREACH(place, &dev->children, link) {
1780 if (place->order > order)
1786 * The device 'place' is the first device whose order is
1787 * greater than the new child.
1789 TAILQ_INSERT_BEFORE(place, child, link);
1792 * The new child's order is greater or equal to the order of
1793 * any existing device. Add the child to the tail of the list.
1795 TAILQ_INSERT_TAIL(&dev->children, child, link);
1798 bus_data_generation_update();
1803 * @brief Delete a device
1805 * This function deletes a device along with all of its children. If
1806 * the device currently has a driver attached to it, the device is
1807 * detached first using device_detach().
1809 * @param dev the parent device
1810 * @param child the device to delete
1813 * @retval non-zero a unit error code describing the error
1816 device_delete_child(device_t dev, device_t child)
1819 device_t grandchild;
1821 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1823 /* remove children first */
1824 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1825 error = device_delete_child(child, grandchild);
1830 if ((error = device_detach(child)) != 0)
1832 if (child->devclass)
1833 devclass_delete_device(child->devclass, child);
1834 TAILQ_REMOVE(&dev->children, child, link);
1835 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1836 kobj_delete((kobj_t) child, M_BUS);
1838 bus_data_generation_update();
1843 * @brief Find a device given a unit number
1845 * This is similar to devclass_get_devices() but only searches for
1846 * devices which have @p dev as a parent.
1848 * @param dev the parent device to search
1849 * @param unit the unit number to search for. If the unit is -1,
1850 * return the first child of @p dev which has name
1851 * @p classname (that is, the one with the lowest unit.)
1853 * @returns the device with the given unit number or @c
1854 * NULL if there is no such device
1857 device_find_child(device_t dev, const char *classname, int unit)
1862 dc = devclass_find(classname);
1867 child = devclass_get_device(dc, unit);
1868 if (child && child->parent == dev)
1871 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1872 child = devclass_get_device(dc, unit);
1873 if (child && child->parent == dev)
1884 first_matching_driver(devclass_t dc, device_t dev)
1887 return (devclass_find_driver_internal(dc, dev->devclass->name));
1888 return (TAILQ_FIRST(&dc->drivers));
1895 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1897 if (dev->devclass) {
1899 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1900 if (!strcmp(dev->devclass->name, dl->driver->name))
1904 return (TAILQ_NEXT(last, link));
1911 device_probe_child(device_t dev, device_t child)
1914 driverlink_t best = NULL;
1916 int result, pri = 0;
1917 int hasclass = (child->devclass != NULL);
1923 panic("device_probe_child: parent device has no devclass");
1926 * If the state is already probed, then return. However, don't
1927 * return if we can rebid this object.
1929 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1932 for (; dc; dc = dc->parent) {
1933 for (dl = first_matching_driver(dc, child);
1935 dl = next_matching_driver(dc, child, dl)) {
1937 /* If this driver's pass is too high, then ignore it. */
1938 if (dl->pass > bus_current_pass)
1941 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1942 device_set_driver(child, dl->driver);
1944 if (device_set_devclass(child, dl->driver->name)) {
1945 printf("driver bug: Unable to set devclass (devname: %s)\n",
1946 (child ? device_get_name(child) :
1948 device_set_driver(child, NULL);
1953 /* Fetch any flags for the device before probing. */
1954 resource_int_value(dl->driver->name, child->unit,
1955 "flags", &child->devflags);
1957 result = DEVICE_PROBE(child);
1959 /* Reset flags and devclass before the next probe. */
1960 child->devflags = 0;
1962 device_set_devclass(child, NULL);
1965 * If the driver returns SUCCESS, there can be
1966 * no higher match for this device.
1975 * The driver returned an error so it
1976 * certainly doesn't match.
1979 device_set_driver(child, NULL);
1984 * A priority lower than SUCCESS, remember the
1985 * best matching driver. Initialise the value
1986 * of pri for the first match.
1988 if (best == NULL || result > pri) {
1990 * Probes that return BUS_PROBE_NOWILDCARD
1991 * or lower only match when they are set
1992 * in stone by the parent bus.
1994 if (result <= BUS_PROBE_NOWILDCARD &&
1995 child->flags & DF_WILDCARD)
2003 * If we have an unambiguous match in this devclass,
2004 * don't look in the parent.
2006 if (best && pri == 0)
2011 * If we found a driver, change state and initialise the devclass.
2013 /* XXX What happens if we rebid and got no best? */
2016 * If this device was atached, and we were asked to
2017 * rescan, and it is a different driver, then we have
2018 * to detach the old driver and reattach this new one.
2019 * Note, we don't have to check for DF_REBID here
2020 * because if the state is > DS_ALIVE, we know it must
2023 * This assumes that all DF_REBID drivers can have
2024 * their probe routine called at any time and that
2025 * they are idempotent as well as completely benign in
2026 * normal operations.
2028 * We also have to make sure that the detach
2029 * succeeded, otherwise we fail the operation (or
2030 * maybe it should just fail silently? I'm torn).
2032 if (child->state > DS_ALIVE && best->driver != child->driver)
2033 if ((result = device_detach(dev)) != 0)
2036 /* Set the winning driver, devclass, and flags. */
2037 if (!child->devclass) {
2038 result = device_set_devclass(child, best->driver->name);
2042 device_set_driver(child, best->driver);
2043 resource_int_value(best->driver->name, child->unit,
2044 "flags", &child->devflags);
2048 * A bit bogus. Call the probe method again to make
2049 * sure that we have the right description.
2051 DEVICE_PROBE(child);
2053 child->flags |= DF_REBID;
2056 child->flags &= ~DF_REBID;
2057 child->state = DS_ALIVE;
2059 bus_data_generation_update();
2067 * @brief Return the parent of a device
2070 device_get_parent(device_t dev)
2072 return (dev->parent);
2076 * @brief Get a list of children of a device
2078 * An array containing a list of all the children of the given device
2079 * is allocated and returned in @p *devlistp. The number of devices
2080 * in the array is returned in @p *devcountp. The caller should free
2081 * the array using @c free(p, M_TEMP).
2083 * @param dev the device to examine
2084 * @param devlistp points at location for array pointer return
2086 * @param devcountp points at location for array size return value
2089 * @retval ENOMEM the array allocation failed
2092 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2099 TAILQ_FOREACH(child, &dev->children, link) {
2103 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2108 TAILQ_FOREACH(child, &dev->children, link) {
2109 list[count] = child;
2120 * @brief Return the current driver for the device or @c NULL if there
2121 * is no driver currently attached
2124 device_get_driver(device_t dev)
2126 return (dev->driver);
2130 * @brief Return the current devclass for the device or @c NULL if
2134 device_get_devclass(device_t dev)
2136 return (dev->devclass);
2140 * @brief Return the name of the device's devclass or @c NULL if there
2144 device_get_name(device_t dev)
2146 if (dev != NULL && dev->devclass)
2147 return (devclass_get_name(dev->devclass));
2152 * @brief Return a string containing the device's devclass name
2153 * followed by an ascii representation of the device's unit number
2157 device_get_nameunit(device_t dev)
2159 return (dev->nameunit);
2163 * @brief Return the device's unit number.
2166 device_get_unit(device_t dev)
2172 * @brief Return the device's description string
2175 device_get_desc(device_t dev)
2181 * @brief Return the device's flags
2184 device_get_flags(device_t dev)
2186 return (dev->devflags);
2189 struct sysctl_ctx_list *
2190 device_get_sysctl_ctx(device_t dev)
2192 return (&dev->sysctl_ctx);
2196 device_get_sysctl_tree(device_t dev)
2198 return (dev->sysctl_tree);
2202 * @brief Print the name of the device followed by a colon and a space
2204 * @returns the number of characters printed
2207 device_print_prettyname(device_t dev)
2209 const char *name = device_get_name(dev);
2212 return (printf("unknown: "));
2213 return (printf("%s%d: ", name, device_get_unit(dev)));
2217 * @brief Print the name of the device followed by a colon, a space
2218 * and the result of calling vprintf() with the value of @p fmt and
2219 * the following arguments.
2221 * @returns the number of characters printed
2224 device_printf(device_t dev, const char * fmt, ...)
2229 retval = device_print_prettyname(dev);
2231 retval += vprintf(fmt, ap);
2240 device_set_desc_internal(device_t dev, const char* desc, int copy)
2242 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2243 free(dev->desc, M_BUS);
2244 dev->flags &= ~DF_DESCMALLOCED;
2249 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2251 strcpy(dev->desc, desc);
2252 dev->flags |= DF_DESCMALLOCED;
2255 /* Avoid a -Wcast-qual warning */
2256 dev->desc = (char *)(uintptr_t) desc;
2259 bus_data_generation_update();
2263 * @brief Set the device's description
2265 * The value of @c desc should be a string constant that will not
2266 * change (at least until the description is changed in a subsequent
2267 * call to device_set_desc() or device_set_desc_copy()).
2270 device_set_desc(device_t dev, const char* desc)
2272 device_set_desc_internal(dev, desc, FALSE);
2276 * @brief Set the device's description
2278 * The string pointed to by @c desc is copied. Use this function if
2279 * the device description is generated, (e.g. with sprintf()).
2282 device_set_desc_copy(device_t dev, const char* desc)
2284 device_set_desc_internal(dev, desc, TRUE);
2288 * @brief Set the device's flags
2291 device_set_flags(device_t dev, u_int32_t flags)
2293 dev->devflags = flags;
2297 * @brief Return the device's softc field
2299 * The softc is allocated and zeroed when a driver is attached, based
2300 * on the size field of the driver.
2303 device_get_softc(device_t dev)
2305 return (dev->softc);
2309 * @brief Set the device's softc field
2311 * Most drivers do not need to use this since the softc is allocated
2312 * automatically when the driver is attached.
2315 device_set_softc(device_t dev, void *softc)
2317 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2318 free(dev->softc, M_BUS_SC);
2321 dev->flags |= DF_EXTERNALSOFTC;
2323 dev->flags &= ~DF_EXTERNALSOFTC;
2327 * @brief Get the device's ivars field
2329 * The ivars field is used by the parent device to store per-device
2330 * state (e.g. the physical location of the device or a list of
2334 device_get_ivars(device_t dev)
2337 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2338 return (dev->ivars);
2342 * @brief Set the device's ivars field
2345 device_set_ivars(device_t dev, void * ivars)
2348 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2353 * @brief Return the device's state
2356 device_get_state(device_t dev)
2358 return (dev->state);
2362 * @brief Set the DF_ENABLED flag for the device
2365 device_enable(device_t dev)
2367 dev->flags |= DF_ENABLED;
2371 * @brief Clear the DF_ENABLED flag for the device
2374 device_disable(device_t dev)
2376 dev->flags &= ~DF_ENABLED;
2380 * @brief Increment the busy counter for the device
2383 device_busy(device_t dev)
2385 if (dev->state < DS_ATTACHED)
2386 panic("device_busy: called for unattached device");
2387 if (dev->busy == 0 && dev->parent)
2388 device_busy(dev->parent);
2390 dev->state = DS_BUSY;
2394 * @brief Decrement the busy counter for the device
2397 device_unbusy(device_t dev)
2399 if (dev->state != DS_BUSY)
2400 panic("device_unbusy: called for non-busy device %s",
2401 device_get_nameunit(dev));
2403 if (dev->busy == 0) {
2405 device_unbusy(dev->parent);
2406 dev->state = DS_ATTACHED;
2411 * @brief Set the DF_QUIET flag for the device
2414 device_quiet(device_t dev)
2416 dev->flags |= DF_QUIET;
2420 * @brief Clear the DF_QUIET flag for the device
2423 device_verbose(device_t dev)
2425 dev->flags &= ~DF_QUIET;
2429 * @brief Return non-zero if the DF_QUIET flag is set on the device
2432 device_is_quiet(device_t dev)
2434 return ((dev->flags & DF_QUIET) != 0);
2438 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2441 device_is_enabled(device_t dev)
2443 return ((dev->flags & DF_ENABLED) != 0);
2447 * @brief Return non-zero if the device was successfully probed
2450 device_is_alive(device_t dev)
2452 return (dev->state >= DS_ALIVE);
2456 * @brief Return non-zero if the device currently has a driver
2460 device_is_attached(device_t dev)
2462 return (dev->state >= DS_ATTACHED);
2466 * @brief Set the devclass of a device
2467 * @see devclass_add_device().
2470 device_set_devclass(device_t dev, const char *classname)
2477 devclass_delete_device(dev->devclass, dev);
2481 if (dev->devclass) {
2482 printf("device_set_devclass: device class already set\n");
2486 dc = devclass_find_internal(classname, NULL, TRUE);
2490 error = devclass_add_device(dc, dev);
2492 bus_data_generation_update();
2497 * @brief Set the driver of a device
2500 * @retval EBUSY the device already has a driver attached
2501 * @retval ENOMEM a memory allocation failure occurred
2504 device_set_driver(device_t dev, driver_t *driver)
2506 if (dev->state >= DS_ATTACHED)
2509 if (dev->driver == driver)
2512 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2513 free(dev->softc, M_BUS_SC);
2516 kobj_delete((kobj_t) dev, NULL);
2517 dev->driver = driver;
2519 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2520 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2521 dev->softc = malloc(driver->size, M_BUS_SC,
2524 kobj_delete((kobj_t) dev, NULL);
2525 kobj_init((kobj_t) dev, &null_class);
2531 kobj_init((kobj_t) dev, &null_class);
2534 bus_data_generation_update();
2539 * @brief Probe a device, and return this status.
2541 * This function is the core of the device autoconfiguration
2542 * system. Its purpose is to select a suitable driver for a device and
2543 * then call that driver to initialise the hardware appropriately. The
2544 * driver is selected by calling the DEVICE_PROBE() method of a set of
2545 * candidate drivers and then choosing the driver which returned the
2546 * best value. This driver is then attached to the device using
2549 * The set of suitable drivers is taken from the list of drivers in
2550 * the parent device's devclass. If the device was originally created
2551 * with a specific class name (see device_add_child()), only drivers
2552 * with that name are probed, otherwise all drivers in the devclass
2553 * are probed. If no drivers return successful probe values in the
2554 * parent devclass, the search continues in the parent of that
2555 * devclass (see devclass_get_parent()) if any.
2557 * @param dev the device to initialise
2560 * @retval ENXIO no driver was found
2561 * @retval ENOMEM memory allocation failure
2562 * @retval non-zero some other unix error code
2563 * @retval -1 Device already attached
2566 device_probe(device_t dev)
2572 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2575 if (!(dev->flags & DF_ENABLED)) {
2576 if (bootverbose && device_get_name(dev) != NULL) {
2577 device_print_prettyname(dev);
2578 printf("not probed (disabled)\n");
2582 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2583 if (bus_current_pass == BUS_PASS_DEFAULT &&
2584 !(dev->flags & DF_DONENOMATCH)) {
2585 BUS_PROBE_NOMATCH(dev->parent, dev);
2587 dev->flags |= DF_DONENOMATCH;
2595 * @brief Probe a device and attach a driver if possible
2597 * calls device_probe() and attaches if that was successful.
2600 device_probe_and_attach(device_t dev)
2606 error = device_probe(dev);
2609 else if (error != 0)
2611 return (device_attach(dev));
2615 * @brief Attach a device driver to a device
2617 * This function is a wrapper around the DEVICE_ATTACH() driver
2618 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2619 * device's sysctl tree, optionally prints a description of the device
2620 * and queues a notification event for user-based device management
2623 * Normally this function is only called internally from
2624 * device_probe_and_attach().
2626 * @param dev the device to initialise
2629 * @retval ENXIO no driver was found
2630 * @retval ENOMEM memory allocation failure
2631 * @retval non-zero some other unix error code
2634 device_attach(device_t dev)
2638 device_sysctl_init(dev);
2639 if (!device_is_quiet(dev))
2640 device_print_child(dev->parent, dev);
2641 if ((error = DEVICE_ATTACH(dev)) != 0) {
2642 printf("device_attach: %s%d attach returned %d\n",
2643 dev->driver->name, dev->unit, error);
2644 /* Unset the class; set in device_probe_child */
2645 if (dev->devclass == NULL)
2646 device_set_devclass(dev, NULL);
2647 device_set_driver(dev, NULL);
2648 device_sysctl_fini(dev);
2649 dev->state = DS_NOTPRESENT;
2652 device_sysctl_update(dev);
2653 dev->state = DS_ATTACHED;
2659 * @brief Detach a driver from a device
2661 * This function is a wrapper around the DEVICE_DETACH() driver
2662 * method. If the call to DEVICE_DETACH() succeeds, it calls
2663 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2664 * notification event for user-based device management services and
2665 * cleans up the device's sysctl tree.
2667 * @param dev the device to un-initialise
2670 * @retval ENXIO no driver was found
2671 * @retval ENOMEM memory allocation failure
2672 * @retval non-zero some other unix error code
2675 device_detach(device_t dev)
2681 PDEBUG(("%s", DEVICENAME(dev)));
2682 if (dev->state == DS_BUSY)
2684 if (dev->state != DS_ATTACHED)
2687 if ((error = DEVICE_DETACH(dev)) != 0)
2690 if (!device_is_quiet(dev))
2691 device_printf(dev, "detached\n");
2693 BUS_CHILD_DETACHED(dev->parent, dev);
2695 if (!(dev->flags & DF_FIXEDCLASS))
2696 devclass_delete_device(dev->devclass, dev);
2698 dev->state = DS_NOTPRESENT;
2699 device_set_driver(dev, NULL);
2700 device_set_desc(dev, NULL);
2701 device_sysctl_fini(dev);
2707 * @brief Tells a driver to quiesce itself.
2709 * This function is a wrapper around the DEVICE_QUIESCE() driver
2710 * method. If the call to DEVICE_QUIESCE() succeeds.
2712 * @param dev the device to quiesce
2715 * @retval ENXIO no driver was found
2716 * @retval ENOMEM memory allocation failure
2717 * @retval non-zero some other unix error code
2720 device_quiesce(device_t dev)
2723 PDEBUG(("%s", DEVICENAME(dev)));
2724 if (dev->state == DS_BUSY)
2726 if (dev->state != DS_ATTACHED)
2729 return (DEVICE_QUIESCE(dev));
2733 * @brief Notify a device of system shutdown
2735 * This function calls the DEVICE_SHUTDOWN() driver method if the
2736 * device currently has an attached driver.
2738 * @returns the value returned by DEVICE_SHUTDOWN()
2741 device_shutdown(device_t dev)
2743 if (dev->state < DS_ATTACHED)
2745 return (DEVICE_SHUTDOWN(dev));
2749 * @brief Set the unit number of a device
2751 * This function can be used to override the unit number used for a
2752 * device (e.g. to wire a device to a pre-configured unit number).
2755 device_set_unit(device_t dev, int unit)
2760 dc = device_get_devclass(dev);
2761 if (unit < dc->maxunit && dc->devices[unit])
2763 err = devclass_delete_device(dc, dev);
2767 err = devclass_add_device(dc, dev);
2771 bus_data_generation_update();
2775 /*======================================*/
2777 * Some useful method implementations to make life easier for bus drivers.
2781 * @brief Initialise a resource list.
2783 * @param rl the resource list to initialise
2786 resource_list_init(struct resource_list *rl)
2792 * @brief Reclaim memory used by a resource list.
2794 * This function frees the memory for all resource entries on the list
2797 * @param rl the resource list to free
2800 resource_list_free(struct resource_list *rl)
2802 struct resource_list_entry *rle;
2804 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2806 panic("resource_list_free: resource entry is busy");
2807 STAILQ_REMOVE_HEAD(rl, link);
2813 * @brief Add a resource entry.
2815 * This function adds a resource entry using the given @p type, @p
2816 * start, @p end and @p count values. A rid value is chosen by
2817 * searching sequentially for the first unused rid starting at zero.
2819 * @param rl the resource list to edit
2820 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2821 * @param start the start address of the resource
2822 * @param end the end address of the resource
2823 * @param count XXX end-start+1
2826 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2827 u_long end, u_long count)
2832 while (resource_list_find(rl, type, rid) != NULL)
2834 resource_list_add(rl, type, rid, start, end, count);
2839 * @brief Add or modify a resource entry.
2841 * If an existing entry exists with the same type and rid, it will be
2842 * modified using the given values of @p start, @p end and @p
2843 * count. If no entry exists, a new one will be created using the
2844 * given values. The resource list entry that matches is then returned.
2846 * @param rl the resource list to edit
2847 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2848 * @param rid the resource identifier
2849 * @param start the start address of the resource
2850 * @param end the end address of the resource
2851 * @param count XXX end-start+1
2853 struct resource_list_entry *
2854 resource_list_add(struct resource_list *rl, int type, int rid,
2855 u_long start, u_long end, u_long count)
2857 struct resource_list_entry *rle;
2859 rle = resource_list_find(rl, type, rid);
2861 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2864 panic("resource_list_add: can't record entry");
2865 STAILQ_INSERT_TAIL(rl, rle, link);
2872 panic("resource_list_add: resource entry is busy");
2881 * @brief Find a resource entry by type and rid.
2883 * @param rl the resource list to search
2884 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2885 * @param rid the resource identifier
2887 * @returns the resource entry pointer or NULL if there is no such
2890 struct resource_list_entry *
2891 resource_list_find(struct resource_list *rl, int type, int rid)
2893 struct resource_list_entry *rle;
2895 STAILQ_FOREACH(rle, rl, link) {
2896 if (rle->type == type && rle->rid == rid)
2903 * @brief Delete a resource entry.
2905 * @param rl the resource list to edit
2906 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2907 * @param rid the resource identifier
2910 resource_list_delete(struct resource_list *rl, int type, int rid)
2912 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2915 if (rle->res != NULL)
2916 panic("resource_list_delete: resource has not been released");
2917 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2923 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2925 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2926 * and passing the allocation up to the parent of @p bus. This assumes
2927 * that the first entry of @c device_get_ivars(child) is a struct
2928 * resource_list. This also handles 'passthrough' allocations where a
2929 * child is a remote descendant of bus by passing the allocation up to
2930 * the parent of bus.
2932 * Typically, a bus driver would store a list of child resources
2933 * somewhere in the child device's ivars (see device_get_ivars()) and
2934 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2935 * then call resource_list_alloc() to perform the allocation.
2937 * @param rl the resource list to allocate from
2938 * @param bus the parent device of @p child
2939 * @param child the device which is requesting an allocation
2940 * @param type the type of resource to allocate
2941 * @param rid a pointer to the resource identifier
2942 * @param start hint at the start of the resource range - pass
2943 * @c 0UL for any start address
2944 * @param end hint at the end of the resource range - pass
2945 * @c ~0UL for any end address
2946 * @param count hint at the size of range required - pass @c 1
2948 * @param flags any extra flags to control the resource
2949 * allocation - see @c RF_XXX flags in
2950 * <sys/rman.h> for details
2952 * @returns the resource which was allocated or @c NULL if no
2953 * resource could be allocated
2956 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2957 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2959 struct resource_list_entry *rle = NULL;
2960 int passthrough = (device_get_parent(child) != bus);
2961 int isdefault = (start == 0UL && end == ~0UL);
2964 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2965 type, rid, start, end, count, flags));
2968 rle = resource_list_find(rl, type, *rid);
2971 return (NULL); /* no resource of that type/rid */
2974 panic("resource_list_alloc: resource entry is busy");
2978 count = ulmax(count, rle->count);
2979 end = ulmax(rle->end, start + count - 1);
2982 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2983 type, rid, start, end, count, flags);
2986 * Record the new range.
2989 rle->start = rman_get_start(rle->res);
2990 rle->end = rman_get_end(rle->res);
2998 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3000 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3001 * used with resource_list_alloc().
3003 * @param rl the resource list which was allocated from
3004 * @param bus the parent device of @p child
3005 * @param child the device which is requesting a release
3006 * @param type the type of resource to allocate
3007 * @param rid the resource identifier
3008 * @param res the resource to release
3011 * @retval non-zero a standard unix error code indicating what
3012 * error condition prevented the operation
3015 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3016 int type, int rid, struct resource *res)
3018 struct resource_list_entry *rle = NULL;
3019 int passthrough = (device_get_parent(child) != bus);
3023 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3027 rle = resource_list_find(rl, type, rid);
3030 panic("resource_list_release: can't find resource");
3032 panic("resource_list_release: resource entry is not busy");
3034 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3044 * @brief Print a description of resources in a resource list
3046 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3047 * The name is printed if at least one resource of the given type is available.
3048 * The format is used to print resource start and end.
3050 * @param rl the resource list to print
3051 * @param name the name of @p type, e.g. @c "memory"
3052 * @param type type type of resource entry to print
3053 * @param format printf(9) format string to print resource
3054 * start and end values
3056 * @returns the number of characters printed
3059 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3062 struct resource_list_entry *rle;
3063 int printed, retval;
3067 /* Yes, this is kinda cheating */
3068 STAILQ_FOREACH(rle, rl, link) {
3069 if (rle->type == type) {
3071 retval += printf(" %s ", name);
3073 retval += printf(",");
3075 retval += printf(format, rle->start);
3076 if (rle->count > 1) {
3077 retval += printf("-");
3078 retval += printf(format, rle->start +
3087 * @brief Releases all the resources in a list.
3089 * @param rl The resource list to purge.
3094 resource_list_purge(struct resource_list *rl)
3096 struct resource_list_entry *rle;
3098 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3100 bus_release_resource(rman_get_device(rle->res),
3101 rle->type, rle->rid, rle->res);
3102 STAILQ_REMOVE_HEAD(rl, link);
3108 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
3111 return (device_add_child_ordered(dev, order, name, unit));
3115 * @brief Helper function for implementing DEVICE_PROBE()
3117 * This function can be used to help implement the DEVICE_PROBE() for
3118 * a bus (i.e. a device which has other devices attached to it). It
3119 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3123 bus_generic_probe(device_t dev)
3125 devclass_t dc = dev->devclass;
3128 TAILQ_FOREACH(dl, &dc->drivers, link) {
3130 * If this driver's pass is too high, then ignore it.
3131 * For most drivers in the default pass, this will
3132 * never be true. For early-pass drivers they will
3133 * only call the identify routines of eligible drivers
3134 * when this routine is called. Drivers for later
3135 * passes should have their identify routines called
3136 * on early-pass busses during BUS_NEW_PASS().
3138 if (dl->pass > bus_current_pass)
3140 DEVICE_IDENTIFY(dl->driver, dev);
3147 * @brief Helper function for implementing DEVICE_ATTACH()
3149 * This function can be used to help implement the DEVICE_ATTACH() for
3150 * a bus. It calls device_probe_and_attach() for each of the device's
3154 bus_generic_attach(device_t dev)
3158 TAILQ_FOREACH(child, &dev->children, link) {
3159 device_probe_and_attach(child);
3166 * @brief Helper function for implementing DEVICE_DETACH()
3168 * This function can be used to help implement the DEVICE_DETACH() for
3169 * a bus. It calls device_detach() for each of the device's
3173 bus_generic_detach(device_t dev)
3178 if (dev->state != DS_ATTACHED)
3181 TAILQ_FOREACH(child, &dev->children, link) {
3182 if ((error = device_detach(child)) != 0)
3190 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3192 * This function can be used to help implement the DEVICE_SHUTDOWN()
3193 * for a bus. It calls device_shutdown() for each of the device's
3197 bus_generic_shutdown(device_t dev)
3201 TAILQ_FOREACH(child, &dev->children, link) {
3202 device_shutdown(child);
3209 * @brief Helper function for implementing DEVICE_SUSPEND()
3211 * This function can be used to help implement the DEVICE_SUSPEND()
3212 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3213 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3214 * operation is aborted and any devices which were suspended are
3215 * resumed immediately by calling their DEVICE_RESUME() methods.
3218 bus_generic_suspend(device_t dev)
3221 device_t child, child2;
3223 TAILQ_FOREACH(child, &dev->children, link) {
3224 error = DEVICE_SUSPEND(child);
3226 for (child2 = TAILQ_FIRST(&dev->children);
3227 child2 && child2 != child;
3228 child2 = TAILQ_NEXT(child2, link))
3229 DEVICE_RESUME(child2);
3237 * @brief Helper function for implementing DEVICE_RESUME()
3239 * This function can be used to help implement the DEVICE_RESUME() for
3240 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3243 bus_generic_resume(device_t dev)
3247 TAILQ_FOREACH(child, &dev->children, link) {
3248 DEVICE_RESUME(child);
3249 /* if resume fails, there's nothing we can usefully do... */
3255 * @brief Helper function for implementing BUS_PRINT_CHILD().
3257 * This function prints the first part of the ascii representation of
3258 * @p child, including its name, unit and description (if any - see
3259 * device_set_desc()).
3261 * @returns the number of characters printed
3264 bus_print_child_header(device_t dev, device_t child)
3268 if (device_get_desc(child)) {
3269 retval += device_printf(child, "<%s>", device_get_desc(child));
3271 retval += printf("%s", device_get_nameunit(child));
3278 * @brief Helper function for implementing BUS_PRINT_CHILD().
3280 * This function prints the last part of the ascii representation of
3281 * @p child, which consists of the string @c " on " followed by the
3282 * name and unit of the @p dev.
3284 * @returns the number of characters printed
3287 bus_print_child_footer(device_t dev, device_t child)
3289 return (printf(" on %s\n", device_get_nameunit(dev)));
3293 * @brief Helper function for implementing BUS_PRINT_CHILD().
3295 * This function simply calls bus_print_child_header() followed by
3296 * bus_print_child_footer().
3298 * @returns the number of characters printed
3301 bus_generic_print_child(device_t dev, device_t child)
3305 retval += bus_print_child_header(dev, child);
3306 retval += bus_print_child_footer(dev, child);
3312 * @brief Stub function for implementing BUS_READ_IVAR().
3317 bus_generic_read_ivar(device_t dev, device_t child, int index,
3324 * @brief Stub function for implementing BUS_WRITE_IVAR().
3329 bus_generic_write_ivar(device_t dev, device_t child, int index,
3336 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3340 struct resource_list *
3341 bus_generic_get_resource_list(device_t dev, device_t child)
3347 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3349 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3350 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3351 * and then calls device_probe_and_attach() for each unattached child.
3354 bus_generic_driver_added(device_t dev, driver_t *driver)
3358 DEVICE_IDENTIFY(driver, dev);
3359 TAILQ_FOREACH(child, &dev->children, link) {
3360 if (child->state == DS_NOTPRESENT ||
3361 (child->flags & DF_REBID))
3362 device_probe_and_attach(child);
3367 * @brief Helper function for implementing BUS_NEW_PASS().
3369 * This implementing of BUS_NEW_PASS() first calls the identify
3370 * routines for any drivers that probe at the current pass. Then it
3371 * walks the list of devices for this bus. If a device is already
3372 * attached, then it calls BUS_NEW_PASS() on that device. If the
3373 * device is not already attached, it attempts to attach a driver to
3377 bus_generic_new_pass(device_t dev)
3384 TAILQ_FOREACH(dl, &dc->drivers, link) {
3385 if (dl->pass == bus_current_pass)
3386 DEVICE_IDENTIFY(dl->driver, dev);
3388 TAILQ_FOREACH(child, &dev->children, link) {
3389 if (child->state >= DS_ATTACHED)
3390 BUS_NEW_PASS(child);
3391 else if (child->state == DS_NOTPRESENT)
3392 device_probe_and_attach(child);
3397 * @brief Helper function for implementing BUS_SETUP_INTR().
3399 * This simple implementation of BUS_SETUP_INTR() simply calls the
3400 * BUS_SETUP_INTR() method of the parent of @p dev.
3403 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3404 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3407 /* Propagate up the bus hierarchy until someone handles it. */
3409 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3410 filter, intr, arg, cookiep));
3415 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3417 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3418 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3421 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3424 /* Propagate up the bus hierarchy until someone handles it. */
3426 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3431 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3433 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3434 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3437 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3438 u_long start, u_long end, u_long count, u_int flags)
3440 /* Propagate up the bus hierarchy until someone handles it. */
3442 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3443 start, end, count, flags));
3448 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3450 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3451 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3454 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3457 /* Propagate up the bus hierarchy until someone handles it. */
3459 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3465 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3467 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3468 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3471 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3474 /* Propagate up the bus hierarchy until someone handles it. */
3476 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3482 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3484 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3485 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3488 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3489 int rid, struct resource *r)
3491 /* Propagate up the bus hierarchy until someone handles it. */
3493 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3499 * @brief Helper function for implementing BUS_BIND_INTR().
3501 * This simple implementation of BUS_BIND_INTR() simply calls the
3502 * BUS_BIND_INTR() method of the parent of @p dev.
3505 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3509 /* Propagate up the bus hierarchy until someone handles it. */
3511 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3516 * @brief Helper function for implementing BUS_CONFIG_INTR().
3518 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3519 * BUS_CONFIG_INTR() method of the parent of @p dev.
3522 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3523 enum intr_polarity pol)
3526 /* Propagate up the bus hierarchy until someone handles it. */
3528 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3533 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3535 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3536 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3539 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3540 void *cookie, const char *descr)
3543 /* Propagate up the bus hierarchy until someone handles it. */
3545 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3551 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3553 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3554 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3557 bus_generic_get_dma_tag(device_t dev, device_t child)
3560 /* Propagate up the bus hierarchy until someone handles it. */
3561 if (dev->parent != NULL)
3562 return (BUS_GET_DMA_TAG(dev->parent, child));
3567 * @brief Helper function for implementing BUS_GET_RESOURCE().
3569 * This implementation of BUS_GET_RESOURCE() uses the
3570 * resource_list_find() function to do most of the work. It calls
3571 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3575 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3576 u_long *startp, u_long *countp)
3578 struct resource_list * rl = NULL;
3579 struct resource_list_entry * rle = NULL;
3581 rl = BUS_GET_RESOURCE_LIST(dev, child);
3585 rle = resource_list_find(rl, type, rid);
3590 *startp = rle->start;
3592 *countp = rle->count;
3598 * @brief Helper function for implementing BUS_SET_RESOURCE().
3600 * This implementation of BUS_SET_RESOURCE() uses the
3601 * resource_list_add() function to do most of the work. It calls
3602 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3606 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3607 u_long start, u_long count)
3609 struct resource_list * rl = NULL;
3611 rl = BUS_GET_RESOURCE_LIST(dev, child);
3615 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3621 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3623 * This implementation of BUS_DELETE_RESOURCE() uses the
3624 * resource_list_delete() function to do most of the work. It calls
3625 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3629 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3631 struct resource_list * rl = NULL;
3633 rl = BUS_GET_RESOURCE_LIST(dev, child);
3637 resource_list_delete(rl, type, rid);
3643 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3645 * This implementation of BUS_RELEASE_RESOURCE() uses the
3646 * resource_list_release() function to do most of the work. It calls
3647 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3650 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3651 int rid, struct resource *r)
3653 struct resource_list * rl = NULL;
3655 rl = BUS_GET_RESOURCE_LIST(dev, child);
3659 return (resource_list_release(rl, dev, child, type, rid, r));
3663 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3665 * This implementation of BUS_ALLOC_RESOURCE() uses the
3666 * resource_list_alloc() function to do most of the work. It calls
3667 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3670 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3671 int *rid, u_long start, u_long end, u_long count, u_int flags)
3673 struct resource_list * rl = NULL;
3675 rl = BUS_GET_RESOURCE_LIST(dev, child);
3679 return (resource_list_alloc(rl, dev, child, type, rid,
3680 start, end, count, flags));
3684 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3686 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3687 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3690 bus_generic_child_present(device_t dev, device_t child)
3692 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3696 * Some convenience functions to make it easier for drivers to use the
3697 * resource-management functions. All these really do is hide the
3698 * indirection through the parent's method table, making for slightly
3699 * less-wordy code. In the future, it might make sense for this code
3700 * to maintain some sort of a list of resources allocated by each device.
3704 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3705 struct resource **res)
3709 for (i = 0; rs[i].type != -1; i++)
3711 for (i = 0; rs[i].type != -1; i++) {
3712 res[i] = bus_alloc_resource_any(dev,
3713 rs[i].type, &rs[i].rid, rs[i].flags);
3714 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3715 bus_release_resources(dev, rs, res);
3723 bus_release_resources(device_t dev, const struct resource_spec *rs,
3724 struct resource **res)
3728 for (i = 0; rs[i].type != -1; i++)
3729 if (res[i] != NULL) {
3730 bus_release_resource(
3731 dev, rs[i].type, rs[i].rid, res[i]);
3737 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3739 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3743 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3744 u_long count, u_int flags)
3746 if (dev->parent == NULL)
3748 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3753 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3755 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3759 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3761 if (dev->parent == NULL)
3763 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3767 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3769 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3773 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3775 if (dev->parent == NULL)
3777 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3781 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3783 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3787 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3789 if (dev->parent == NULL)
3791 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3795 * @brief Wrapper function for BUS_SETUP_INTR().
3797 * This function simply calls the BUS_SETUP_INTR() method of the
3801 bus_setup_intr(device_t dev, struct resource *r, int flags,
3802 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3806 if (dev->parent == NULL)
3808 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
3812 if (handler != NULL && !(flags & INTR_MPSAFE))
3813 device_printf(dev, "[GIANT-LOCKED]\n");
3814 if (bootverbose && (flags & INTR_MPSAFE))
3815 device_printf(dev, "[MPSAFE]\n");
3816 if (filter != NULL) {
3817 if (handler == NULL)
3818 device_printf(dev, "[FILTER]\n");
3820 device_printf(dev, "[FILTER+ITHREAD]\n");
3822 device_printf(dev, "[ITHREAD]\n");
3827 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3829 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3833 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3835 if (dev->parent == NULL)
3837 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3841 * @brief Wrapper function for BUS_BIND_INTR().
3843 * This function simply calls the BUS_BIND_INTR() method of the
3847 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3849 if (dev->parent == NULL)
3851 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3855 * @brief Wrapper function for BUS_DESCRIBE_INTR().
3857 * This function first formats the requested description into a
3858 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
3859 * the parent of @p dev.
3862 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
3863 const char *fmt, ...)
3865 char descr[MAXCOMLEN];
3868 if (dev->parent == NULL)
3871 vsnprintf(descr, sizeof(descr), fmt, ap);
3873 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
3877 * @brief Wrapper function for BUS_SET_RESOURCE().
3879 * This function simply calls the BUS_SET_RESOURCE() method of the
3883 bus_set_resource(device_t dev, int type, int rid,
3884 u_long start, u_long count)
3886 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3891 * @brief Wrapper function for BUS_GET_RESOURCE().
3893 * This function simply calls the BUS_GET_RESOURCE() method of the
3897 bus_get_resource(device_t dev, int type, int rid,
3898 u_long *startp, u_long *countp)
3900 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3905 * @brief Wrapper function for BUS_GET_RESOURCE().
3907 * This function simply calls the BUS_GET_RESOURCE() method of the
3908 * parent of @p dev and returns the start value.
3911 bus_get_resource_start(device_t dev, int type, int rid)
3913 u_long start, count;
3916 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3924 * @brief Wrapper function for BUS_GET_RESOURCE().
3926 * This function simply calls the BUS_GET_RESOURCE() method of the
3927 * parent of @p dev and returns the count value.
3930 bus_get_resource_count(device_t dev, int type, int rid)
3932 u_long start, count;
3935 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3943 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3945 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3949 bus_delete_resource(device_t dev, int type, int rid)
3951 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3955 * @brief Wrapper function for BUS_CHILD_PRESENT().
3957 * This function simply calls the BUS_CHILD_PRESENT() method of the
3961 bus_child_present(device_t child)
3963 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3967 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3969 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3973 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3977 parent = device_get_parent(child);
3978 if (parent == NULL) {
3982 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3986 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3988 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3992 bus_child_location_str(device_t child, char *buf, size_t buflen)
3996 parent = device_get_parent(child);
3997 if (parent == NULL) {
4001 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4005 * @brief Wrapper function for BUS_GET_DMA_TAG().
4007 * This function simply calls the BUS_GET_DMA_TAG() method of the
4011 bus_get_dma_tag(device_t dev)
4015 parent = device_get_parent(dev);
4018 return (BUS_GET_DMA_TAG(parent, dev));
4021 /* Resume all devices and then notify userland that we're up again. */
4023 root_resume(device_t dev)
4027 error = bus_generic_resume(dev);
4029 devctl_notify("kern", "power", "resume", NULL);
4034 root_print_child(device_t dev, device_t child)
4038 retval += bus_print_child_header(dev, child);
4039 retval += printf("\n");
4045 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4046 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4049 * If an interrupt mapping gets to here something bad has happened.
4051 panic("root_setup_intr");
4055 * If we get here, assume that the device is permanant and really is
4056 * present in the system. Removable bus drivers are expected to intercept
4057 * this call long before it gets here. We return -1 so that drivers that
4058 * really care can check vs -1 or some ERRNO returned higher in the food
4062 root_child_present(device_t dev, device_t child)
4067 static kobj_method_t root_methods[] = {
4068 /* Device interface */
4069 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4070 KOBJMETHOD(device_suspend, bus_generic_suspend),
4071 KOBJMETHOD(device_resume, root_resume),
4074 KOBJMETHOD(bus_print_child, root_print_child),
4075 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4076 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4077 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4078 KOBJMETHOD(bus_child_present, root_child_present),
4083 static driver_t root_driver = {
4090 devclass_t root_devclass;
4093 root_bus_module_handler(module_t mod, int what, void* arg)
4097 TAILQ_INIT(&bus_data_devices);
4098 kobj_class_compile((kobj_class_t) &root_driver);
4099 root_bus = make_device(NULL, "root", 0);
4100 root_bus->desc = "System root bus";
4101 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4102 root_bus->driver = &root_driver;
4103 root_bus->state = DS_ATTACHED;
4104 root_devclass = devclass_find_internal("root", NULL, FALSE);
4109 device_shutdown(root_bus);
4112 return (EOPNOTSUPP);
4118 static moduledata_t root_bus_mod = {
4120 root_bus_module_handler,
4123 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4126 * @brief Automatically configure devices
4128 * This function begins the autoconfiguration process by calling
4129 * device_probe_and_attach() for each child of the @c root0 device.
4132 root_bus_configure(void)
4137 /* Eventually this will be split up, but this is sufficient for now. */
4138 bus_set_pass(BUS_PASS_DEFAULT);
4142 * @brief Module handler for registering device drivers
4144 * This module handler is used to automatically register device
4145 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4146 * devclass_add_driver() for the driver described by the
4147 * driver_module_data structure pointed to by @p arg
4150 driver_module_handler(module_t mod, int what, void *arg)
4152 struct driver_module_data *dmd;
4153 devclass_t bus_devclass;
4154 kobj_class_t driver;
4157 dmd = (struct driver_module_data *)arg;
4158 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4163 if (dmd->dmd_chainevh)
4164 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4166 pass = dmd->dmd_pass;
4167 driver = dmd->dmd_driver;
4168 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4169 DRIVERNAME(driver), dmd->dmd_busname, pass));
4170 error = devclass_add_driver(bus_devclass, driver, pass,
4175 PDEBUG(("Unloading module: driver %s from bus %s",
4176 DRIVERNAME(dmd->dmd_driver),
4178 error = devclass_delete_driver(bus_devclass,
4181 if (!error && dmd->dmd_chainevh)
4182 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4185 PDEBUG(("Quiesce module: driver %s from bus %s",
4186 DRIVERNAME(dmd->dmd_driver),
4188 error = devclass_quiesce_driver(bus_devclass,
4191 if (!error && dmd->dmd_chainevh)
4192 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4203 * @brief Enumerate all hinted devices for this bus.
4205 * Walks through the hints for this bus and calls the bus_hinted_child
4206 * routine for each one it fines. It searches first for the specific
4207 * bus that's being probed for hinted children (eg isa0), and then for
4208 * generic children (eg isa).
4210 * @param dev bus device to enumerate
4213 bus_enumerate_hinted_children(device_t bus)
4216 const char *dname, *busname;
4220 * enumerate all devices on the specific bus
4222 busname = device_get_nameunit(bus);
4224 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4225 BUS_HINTED_CHILD(bus, dname, dunit);
4228 * and all the generic ones.
4230 busname = device_get_name(bus);
4232 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4233 BUS_HINTED_CHILD(bus, dname, dunit);
4238 /* the _short versions avoid iteration by not calling anything that prints
4239 * more than oneliners. I love oneliners.
4243 print_device_short(device_t dev, int indent)
4248 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4249 dev->unit, dev->desc,
4250 (dev->parent? "":"no "),
4251 (TAILQ_EMPTY(&dev->children)? "no ":""),
4252 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4253 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4254 (dev->flags&DF_WILDCARD? "wildcard,":""),
4255 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4256 (dev->flags&DF_REBID? "rebiddable,":""),
4257 (dev->ivars? "":"no "),
4258 (dev->softc? "":"no "),
4263 print_device(device_t dev, int indent)
4268 print_device_short(dev, indent);
4270 indentprintf(("Parent:\n"));
4271 print_device_short(dev->parent, indent+1);
4272 indentprintf(("Driver:\n"));
4273 print_driver_short(dev->driver, indent+1);
4274 indentprintf(("Devclass:\n"));
4275 print_devclass_short(dev->devclass, indent+1);
4279 print_device_tree_short(device_t dev, int indent)
4280 /* print the device and all its children (indented) */
4287 print_device_short(dev, indent);
4289 TAILQ_FOREACH(child, &dev->children, link) {
4290 print_device_tree_short(child, indent+1);
4295 print_device_tree(device_t dev, int indent)
4296 /* print the device and all its children (indented) */
4303 print_device(dev, indent);
4305 TAILQ_FOREACH(child, &dev->children, link) {
4306 print_device_tree(child, indent+1);
4311 print_driver_short(driver_t *driver, int indent)
4316 indentprintf(("driver %s: softc size = %zd\n",
4317 driver->name, driver->size));
4321 print_driver(driver_t *driver, int indent)
4326 print_driver_short(driver, indent);
4331 print_driver_list(driver_list_t drivers, int indent)
4333 driverlink_t driver;
4335 TAILQ_FOREACH(driver, &drivers, link) {
4336 print_driver(driver->driver, indent);
4341 print_devclass_short(devclass_t dc, int indent)
4346 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4350 print_devclass(devclass_t dc, int indent)
4357 print_devclass_short(dc, indent);
4358 indentprintf(("Drivers:\n"));
4359 print_driver_list(dc->drivers, indent+1);
4361 indentprintf(("Devices:\n"));
4362 for (i = 0; i < dc->maxunit; i++)
4364 print_device(dc->devices[i], indent+1);
4368 print_devclass_list_short(void)
4372 printf("Short listing of devclasses, drivers & devices:\n");
4373 TAILQ_FOREACH(dc, &devclasses, link) {
4374 print_devclass_short(dc, 0);
4379 print_devclass_list(void)
4383 printf("Full listing of devclasses, drivers & devices:\n");
4384 TAILQ_FOREACH(dc, &devclasses, link) {
4385 print_devclass(dc, 0);
4392 * User-space access to the device tree.
4394 * We implement a small set of nodes:
4396 * hw.bus Single integer read method to obtain the
4397 * current generation count.
4398 * hw.bus.devices Reads the entire device tree in flat space.
4399 * hw.bus.rman Resource manager interface
4401 * We might like to add the ability to scan devclasses and/or drivers to
4402 * determine what else is currently loaded/available.
4406 sysctl_bus(SYSCTL_HANDLER_ARGS)
4408 struct u_businfo ubus;
4410 ubus.ub_version = BUS_USER_VERSION;
4411 ubus.ub_generation = bus_data_generation;
4413 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4415 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4416 "bus-related data");
4419 sysctl_devices(SYSCTL_HANDLER_ARGS)
4421 int *name = (int *)arg1;
4422 u_int namelen = arg2;
4425 struct u_device udev; /* XXX this is a bit big */
4431 if (bus_data_generation_check(name[0]))
4437 * Scan the list of devices, looking for the requested index.
4439 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4447 * Populate the return array.
4449 bzero(&udev, sizeof(udev));
4450 udev.dv_handle = (uintptr_t)dev;
4451 udev.dv_parent = (uintptr_t)dev->parent;
4452 if (dev->nameunit != NULL)
4453 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4454 if (dev->desc != NULL)
4455 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4456 if (dev->driver != NULL && dev->driver->name != NULL)
4457 strlcpy(udev.dv_drivername, dev->driver->name,
4458 sizeof(udev.dv_drivername));
4459 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4460 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4461 udev.dv_devflags = dev->devflags;
4462 udev.dv_flags = dev->flags;
4463 udev.dv_state = dev->state;
4464 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4468 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4469 "system device tree");
4472 bus_data_generation_check(int generation)
4474 if (generation != bus_data_generation)
4477 /* XXX generate optimised lists here? */
4482 bus_data_generation_update(void)
4484 bus_data_generation++;
4488 bus_free_resource(device_t dev, int type, struct resource *r)
4492 return (bus_release_resource(dev, type, rman_get_rid(r), r));