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 mtx_unlock(&devsoftc.mtx);
558 free(n1->dei_data, M_BUS);
562 /* Leave at least one spot in the queue... */
563 while (devsoftc.queued > devctl_queue_length - 1) {
564 n2 = TAILQ_FIRST(&devsoftc.devq);
565 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
566 free(n2->dei_data, M_BUS);
570 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
572 cv_broadcast(&devsoftc.cv);
573 mtx_unlock(&devsoftc.mtx);
574 selwakeup(&devsoftc.sel);
575 p = devsoftc.async_proc;
584 * We have to free data on all error paths since the caller
585 * assumes it will be free'd when this item is dequeued.
592 * @brief Send a 'notification' to userland, using standard ways
595 devctl_notify(const char *system, const char *subsystem, const char *type,
602 return; /* BOGUS! Must specify system. */
603 if (subsystem == NULL)
604 return; /* BOGUS! Must specify subsystem. */
606 return; /* BOGUS! Must specify type. */
607 len += strlen(" system=") + strlen(system);
608 len += strlen(" subsystem=") + strlen(subsystem);
609 len += strlen(" type=") + strlen(type);
610 /* add in the data message plus newline. */
613 len += 3; /* '!', '\n', and NUL */
614 msg = malloc(len, M_BUS, M_NOWAIT);
616 return; /* Drop it on the floor */
618 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
619 system, subsystem, type, data);
621 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
622 system, subsystem, type);
623 devctl_queue_data(msg);
627 * Common routine that tries to make sending messages as easy as possible.
628 * We allocate memory for the data, copy strings into that, but do not
629 * free it unless there's an error. The dequeue part of the driver should
630 * free the data. We don't send data when the device is disabled. We do
631 * send data, even when we have no listeners, because we wish to avoid
632 * races relating to startup and restart of listening applications.
634 * devaddq is designed to string together the type of event, with the
635 * object of that event, plus the plug and play info and location info
636 * for that event. This is likely most useful for devices, but less
637 * useful for other consumers of this interface. Those should use
638 * the devctl_queue_data() interface instead.
641 devaddq(const char *type, const char *what, device_t dev)
648 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
650 data = malloc(1024, M_BUS, M_NOWAIT);
654 /* get the bus specific location of this device */
655 loc = malloc(1024, M_BUS, M_NOWAIT);
659 bus_child_location_str(dev, loc, 1024);
661 /* Get the bus specific pnp info of this device */
662 pnp = malloc(1024, M_BUS, M_NOWAIT);
666 bus_child_pnpinfo_str(dev, pnp, 1024);
668 /* Get the parent of this device, or / if high enough in the tree. */
669 if (device_get_parent(dev) == NULL)
670 parstr = "."; /* Or '/' ? */
672 parstr = device_get_nameunit(device_get_parent(dev));
673 /* String it all together. */
674 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
678 devctl_queue_data(data);
688 * A device was added to the tree. We are called just after it successfully
689 * attaches (that is, probe and attach success for this device). No call
690 * is made if a device is merely parented into the tree. See devnomatch
691 * if probe fails. If attach fails, no notification is sent (but maybe
692 * we should have a different message for this).
695 devadded(device_t dev)
700 pnp = malloc(1024, M_BUS, M_NOWAIT);
703 tmp = malloc(1024, M_BUS, M_NOWAIT);
707 bus_child_pnpinfo_str(dev, pnp, 1024);
708 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
709 devaddq("+", tmp, dev);
719 * A device was removed from the tree. We are called just before this
723 devremoved(device_t dev)
728 pnp = malloc(1024, M_BUS, M_NOWAIT);
731 tmp = malloc(1024, M_BUS, M_NOWAIT);
735 bus_child_pnpinfo_str(dev, pnp, 1024);
736 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
737 devaddq("-", tmp, dev);
747 * Called when there's no match for this device. This is only called
748 * the first time that no match happens, so we don't keep getting this
749 * message. Should that prove to be undesirable, we can change it.
750 * This is called when all drivers that can attach to a given bus
751 * decline to accept this device. Other errrors may not be detected.
754 devnomatch(device_t dev)
756 devaddq("?", "", dev);
760 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
762 struct dev_event_info *n1;
765 dis = devctl_queue_length == 0;
766 error = sysctl_handle_int(oidp, &dis, 0, req);
767 if (error || !req->newptr)
769 mtx_lock(&devsoftc.mtx);
771 while (!TAILQ_EMPTY(&devsoftc.devq)) {
772 n1 = TAILQ_FIRST(&devsoftc.devq);
773 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
774 free(n1->dei_data, M_BUS);
778 devctl_queue_length = 0;
780 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
782 mtx_unlock(&devsoftc.mtx);
787 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
789 struct dev_event_info *n1;
792 q = devctl_queue_length;
793 error = sysctl_handle_int(oidp, &q, 0, req);
794 if (error || !req->newptr)
798 mtx_lock(&devsoftc.mtx);
799 devctl_queue_length = q;
800 while (devsoftc.queued > devctl_queue_length) {
801 n1 = TAILQ_FIRST(&devsoftc.devq);
802 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
803 free(n1->dei_data, M_BUS);
807 mtx_unlock(&devsoftc.mtx);
811 /* End of /dev/devctl code */
813 static TAILQ_HEAD(,device) bus_data_devices;
814 static int bus_data_generation = 1;
816 static kobj_method_t null_methods[] = {
820 DEFINE_CLASS(null, null_methods, 0);
823 * Bus pass implementation
826 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
827 int bus_current_pass = BUS_PASS_ROOT;
831 * @brief Register the pass level of a new driver attachment
833 * Register a new driver attachment's pass level. If no driver
834 * attachment with the same pass level has been added, then @p new
835 * will be added to the global passes list.
837 * @param new the new driver attachment
840 driver_register_pass(struct driverlink *new)
842 struct driverlink *dl;
844 /* We only consider pass numbers during boot. */
845 if (bus_current_pass == BUS_PASS_DEFAULT)
849 * Walk the passes list. If we already know about this pass
850 * then there is nothing to do. If we don't, then insert this
851 * driver link into the list.
853 TAILQ_FOREACH(dl, &passes, passlink) {
854 if (dl->pass < new->pass)
856 if (dl->pass == new->pass)
858 TAILQ_INSERT_BEFORE(dl, new, passlink);
861 TAILQ_INSERT_TAIL(&passes, new, passlink);
865 * @brief Raise the current bus pass
867 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
868 * method on the root bus to kick off a new device tree scan for each
869 * new pass level that has at least one driver.
872 bus_set_pass(int pass)
874 struct driverlink *dl;
876 if (bus_current_pass > pass)
877 panic("Attempt to lower bus pass level");
879 TAILQ_FOREACH(dl, &passes, passlink) {
880 /* Skip pass values below the current pass level. */
881 if (dl->pass <= bus_current_pass)
885 * Bail once we hit a driver with a pass level that is
892 * Raise the pass level to the next level and rescan
895 bus_current_pass = dl->pass;
896 BUS_NEW_PASS(root_bus);
900 * If there isn't a driver registered for the requested pass,
901 * then bus_current_pass might still be less than 'pass'. Set
902 * it to 'pass' in that case.
904 if (bus_current_pass < pass)
905 bus_current_pass = pass;
906 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
910 * Devclass implementation
913 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
917 * @brief Find or create a device class
919 * If a device class with the name @p classname exists, return it,
920 * otherwise if @p create is non-zero create and return a new device
923 * If @p parentname is non-NULL, the parent of the devclass is set to
924 * the devclass of that name.
926 * @param classname the devclass name to find or create
927 * @param parentname the parent devclass name or @c NULL
928 * @param create non-zero to create a devclass
931 devclass_find_internal(const char *classname, const char *parentname,
936 PDEBUG(("looking for %s", classname));
940 TAILQ_FOREACH(dc, &devclasses, link) {
941 if (!strcmp(dc->name, classname))
946 PDEBUG(("creating %s", classname));
947 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
948 M_BUS, M_NOWAIT | M_ZERO);
952 dc->name = (char*) (dc + 1);
953 strcpy(dc->name, classname);
954 TAILQ_INIT(&dc->drivers);
955 TAILQ_INSERT_TAIL(&devclasses, dc, link);
957 bus_data_generation_update();
961 * If a parent class is specified, then set that as our parent so
962 * that this devclass will support drivers for the parent class as
963 * well. If the parent class has the same name don't do this though
964 * as it creates a cycle that can trigger an infinite loop in
965 * device_probe_child() if a device exists for which there is no
968 if (parentname && dc && !dc->parent &&
969 strcmp(classname, parentname) != 0) {
970 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
971 dc->parent->flags |= DC_HAS_CHILDREN;
978 * @brief Create a device class
980 * If a device class with the name @p classname exists, return it,
981 * otherwise create and return a new device class.
983 * @param classname the devclass name to find or create
986 devclass_create(const char *classname)
988 return (devclass_find_internal(classname, NULL, TRUE));
992 * @brief Find a device class
994 * If a device class with the name @p classname exists, return it,
995 * otherwise return @c NULL.
997 * @param classname the devclass name to find
1000 devclass_find(const char *classname)
1002 return (devclass_find_internal(classname, NULL, FALSE));
1006 * @brief Register that a device driver has been added to a devclass
1008 * Register that a device driver has been added to a devclass. This
1009 * is called by devclass_add_driver to accomplish the recursive
1010 * notification of all the children classes of dc, as well as dc.
1011 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1012 * the devclass. We do a full search here of the devclass list at
1013 * each iteration level to save storing children-lists in the devclass
1014 * structure. If we ever move beyond a few dozen devices doing this,
1015 * we may need to reevaluate...
1017 * @param dc the devclass to edit
1018 * @param driver the driver that was just added
1021 devclass_driver_added(devclass_t dc, driver_t *driver)
1027 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1029 for (i = 0; i < dc->maxunit; i++)
1030 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1031 BUS_DRIVER_ADDED(dc->devices[i], driver);
1034 * Walk through the children classes. Since we only keep a
1035 * single parent pointer around, we walk the entire list of
1036 * devclasses looking for children. We set the
1037 * DC_HAS_CHILDREN flag when a child devclass is created on
1038 * the parent, so we only walk the list for those devclasses
1039 * that have children.
1041 if (!(dc->flags & DC_HAS_CHILDREN))
1044 TAILQ_FOREACH(dc, &devclasses, link) {
1045 if (dc->parent == parent)
1046 devclass_driver_added(dc, driver);
1051 * @brief Add a device driver to a device class
1053 * Add a device driver to a devclass. This is normally called
1054 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1055 * all devices in the devclass will be called to allow them to attempt
1056 * to re-probe any unmatched children.
1058 * @param dc the devclass to edit
1059 * @param driver the driver to register
1062 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1065 const char *parentname;
1067 PDEBUG(("%s", DRIVERNAME(driver)));
1069 /* Don't allow invalid pass values. */
1070 if (pass <= BUS_PASS_ROOT)
1073 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1078 * Compile the driver's methods. Also increase the reference count
1079 * so that the class doesn't get freed when the last instance
1080 * goes. This means we can safely use static methods and avoids a
1081 * double-free in devclass_delete_driver.
1083 kobj_class_compile((kobj_class_t) driver);
1086 * If the driver has any base classes, make the
1087 * devclass inherit from the devclass of the driver's
1088 * first base class. This will allow the system to
1089 * search for drivers in both devclasses for children
1090 * of a device using this driver.
1092 if (driver->baseclasses)
1093 parentname = driver->baseclasses[0]->name;
1096 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1098 dl->driver = driver;
1099 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1100 driver->refs++; /* XXX: kobj_mtx */
1102 driver_register_pass(dl);
1104 devclass_driver_added(dc, driver);
1105 bus_data_generation_update();
1110 * @brief Delete a device driver from a device class
1112 * Delete a device driver from a devclass. This is normally called
1113 * automatically by DRIVER_MODULE().
1115 * If the driver is currently attached to any devices,
1116 * devclass_delete_driver() will first attempt to detach from each
1117 * device. If one of the detach calls fails, the driver will not be
1120 * @param dc the devclass to edit
1121 * @param driver the driver to unregister
1124 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1126 devclass_t dc = devclass_find(driver->name);
1132 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1138 * Find the link structure in the bus' list of drivers.
1140 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1141 if (dl->driver == driver)
1146 PDEBUG(("%s not found in %s list", driver->name,
1152 * Disassociate from any devices. We iterate through all the
1153 * devices in the devclass of the driver and detach any which are
1154 * using the driver and which have a parent in the devclass which
1155 * we are deleting from.
1157 * Note that since a driver can be in multiple devclasses, we
1158 * should not detach devices which are not children of devices in
1159 * the affected devclass.
1161 for (i = 0; i < dc->maxunit; i++) {
1162 if (dc->devices[i]) {
1163 dev = dc->devices[i];
1164 if (dev->driver == driver && dev->parent &&
1165 dev->parent->devclass == busclass) {
1166 if ((error = device_detach(dev)) != 0)
1168 device_set_driver(dev, NULL);
1173 TAILQ_REMOVE(&busclass->drivers, dl, link);
1178 if (driver->refs == 0)
1179 kobj_class_free((kobj_class_t) driver);
1181 bus_data_generation_update();
1186 * @brief Quiesces a set of device drivers from a device class
1188 * Quiesce a device driver from a devclass. This is normally called
1189 * automatically by DRIVER_MODULE().
1191 * If the driver is currently attached to any devices,
1192 * devclass_quiesece_driver() will first attempt to quiesce each
1195 * @param dc the devclass to edit
1196 * @param driver the driver to unregister
1199 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1201 devclass_t dc = devclass_find(driver->name);
1207 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1213 * Find the link structure in the bus' list of drivers.
1215 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1216 if (dl->driver == driver)
1221 PDEBUG(("%s not found in %s list", driver->name,
1227 * Quiesce all devices. We iterate through all the devices in
1228 * the devclass of the driver and quiesce any which are using
1229 * the driver and which have a parent in the devclass which we
1232 * Note that since a driver can be in multiple devclasses, we
1233 * should not quiesce devices which are not children of
1234 * devices in the affected devclass.
1236 for (i = 0; i < dc->maxunit; i++) {
1237 if (dc->devices[i]) {
1238 dev = dc->devices[i];
1239 if (dev->driver == driver && dev->parent &&
1240 dev->parent->devclass == busclass) {
1241 if ((error = device_quiesce(dev)) != 0)
1254 devclass_find_driver_internal(devclass_t dc, const char *classname)
1258 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1260 TAILQ_FOREACH(dl, &dc->drivers, link) {
1261 if (!strcmp(dl->driver->name, classname))
1265 PDEBUG(("not found"));
1270 * @brief Return the name of the devclass
1273 devclass_get_name(devclass_t dc)
1279 * @brief Find a device given a unit number
1281 * @param dc the devclass to search
1282 * @param unit the unit number to search for
1284 * @returns the device with the given unit number or @c
1285 * NULL if there is no such device
1288 devclass_get_device(devclass_t dc, int unit)
1290 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1292 return (dc->devices[unit]);
1296 * @brief Find the softc field of a device given a unit number
1298 * @param dc the devclass to search
1299 * @param unit the unit number to search for
1301 * @returns the softc field of the device with the given
1302 * unit number or @c NULL if there is no such
1306 devclass_get_softc(devclass_t dc, int unit)
1310 dev = devclass_get_device(dc, unit);
1314 return (device_get_softc(dev));
1318 * @brief Get a list of devices in the devclass
1320 * An array containing a list of all the devices in the given devclass
1321 * is allocated and returned in @p *devlistp. The number of devices
1322 * in the array is returned in @p *devcountp. The caller should free
1323 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1325 * @param dc the devclass to examine
1326 * @param devlistp points at location for array pointer return
1328 * @param devcountp points at location for array size return value
1331 * @retval ENOMEM the array allocation failed
1334 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1339 count = devclass_get_count(dc);
1340 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1345 for (i = 0; i < dc->maxunit; i++) {
1346 if (dc->devices[i]) {
1347 list[count] = dc->devices[i];
1359 * @brief Get a list of drivers in the devclass
1361 * An array containing a list of pointers to all the drivers in the
1362 * given devclass is allocated and returned in @p *listp. The number
1363 * of drivers in the array is returned in @p *countp. The caller should
1364 * free the array using @c free(p, M_TEMP).
1366 * @param dc the devclass to examine
1367 * @param listp gives location for array pointer return value
1368 * @param countp gives location for number of array elements
1372 * @retval ENOMEM the array allocation failed
1375 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1382 TAILQ_FOREACH(dl, &dc->drivers, link)
1384 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1389 TAILQ_FOREACH(dl, &dc->drivers, link) {
1390 list[count] = dl->driver;
1400 * @brief Get the number of devices in a devclass
1402 * @param dc the devclass to examine
1405 devclass_get_count(devclass_t dc)
1410 for (i = 0; i < dc->maxunit; i++)
1417 * @brief Get the maximum unit number used in a devclass
1419 * Note that this is one greater than the highest currently-allocated
1420 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1421 * that not even the devclass has been allocated yet.
1423 * @param dc the devclass to examine
1426 devclass_get_maxunit(devclass_t dc)
1430 return (dc->maxunit);
1434 * @brief Find a free unit number in a devclass
1436 * This function searches for the first unused unit number greater
1437 * that or equal to @p unit.
1439 * @param dc the devclass to examine
1440 * @param unit the first unit number to check
1443 devclass_find_free_unit(devclass_t dc, int unit)
1447 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1453 * @brief Set the parent of a devclass
1455 * The parent class is normally initialised automatically by
1458 * @param dc the devclass to edit
1459 * @param pdc the new parent devclass
1462 devclass_set_parent(devclass_t dc, devclass_t pdc)
1468 * @brief Get the parent of a devclass
1470 * @param dc the devclass to examine
1473 devclass_get_parent(devclass_t dc)
1475 return (dc->parent);
1478 struct sysctl_ctx_list *
1479 devclass_get_sysctl_ctx(devclass_t dc)
1481 return (&dc->sysctl_ctx);
1485 devclass_get_sysctl_tree(devclass_t dc)
1487 return (dc->sysctl_tree);
1492 * @brief Allocate a unit number
1494 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1495 * will do). The allocated unit number is returned in @p *unitp.
1497 * @param dc the devclass to allocate from
1498 * @param unitp points at the location for the allocated unit
1502 * @retval EEXIST the requested unit number is already allocated
1503 * @retval ENOMEM memory allocation failure
1506 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1511 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1513 /* Ask the parent bus if it wants to wire this device. */
1515 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1518 /* If we were given a wired unit number, check for existing device */
1521 if (unit >= 0 && unit < dc->maxunit &&
1522 dc->devices[unit] != NULL) {
1524 printf("%s: %s%d already exists; skipping it\n",
1525 dc->name, dc->name, *unitp);
1529 /* Unwired device, find the next available slot for it */
1531 for (unit = 0;; unit++) {
1532 /* If there is an "at" hint for a unit then skip it. */
1533 if (resource_string_value(dc->name, unit, "at", &s) ==
1537 /* If this device slot is already in use, skip it. */
1538 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1546 * We've selected a unit beyond the length of the table, so let's
1547 * extend the table to make room for all units up to and including
1550 if (unit >= dc->maxunit) {
1551 device_t *newlist, *oldlist;
1554 oldlist = dc->devices;
1555 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1556 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1559 if (oldlist != NULL)
1560 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1561 bzero(newlist + dc->maxunit,
1562 sizeof(device_t) * (newsize - dc->maxunit));
1563 dc->devices = newlist;
1564 dc->maxunit = newsize;
1565 if (oldlist != NULL)
1566 free(oldlist, M_BUS);
1568 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1576 * @brief Add a device to a devclass
1578 * A unit number is allocated for the device (using the device's
1579 * preferred unit number if any) and the device is registered in the
1580 * devclass. This allows the device to be looked up by its unit
1581 * number, e.g. by decoding a dev_t minor number.
1583 * @param dc the devclass to add to
1584 * @param dev the device to add
1587 * @retval EEXIST the requested unit number is already allocated
1588 * @retval ENOMEM memory allocation failure
1591 devclass_add_device(devclass_t dc, device_t dev)
1595 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1597 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1600 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1604 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1605 free(dev->nameunit, M_BUS);
1606 dev->nameunit = NULL;
1609 dc->devices[dev->unit] = dev;
1611 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1618 * @brief Delete a device from a devclass
1620 * The device is removed from the devclass's device list and its unit
1623 * @param dc the devclass to delete from
1624 * @param dev the device to delete
1629 devclass_delete_device(devclass_t dc, device_t dev)
1634 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1636 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1637 panic("devclass_delete_device: inconsistent device class");
1638 dc->devices[dev->unit] = NULL;
1639 if (dev->flags & DF_WILDCARD)
1641 dev->devclass = NULL;
1642 free(dev->nameunit, M_BUS);
1643 dev->nameunit = NULL;
1650 * @brief Make a new device and add it as a child of @p parent
1652 * @param parent the parent of the new device
1653 * @param name the devclass name of the new device or @c NULL
1654 * to leave the devclass unspecified
1655 * @parem unit the unit number of the new device of @c -1 to
1656 * leave the unit number unspecified
1658 * @returns the new device
1661 make_device(device_t parent, const char *name, int unit)
1666 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1669 dc = devclass_find_internal(name, NULL, TRUE);
1671 printf("make_device: can't find device class %s\n",
1679 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1683 dev->parent = parent;
1684 TAILQ_INIT(&dev->children);
1685 kobj_init((kobj_t) dev, &null_class);
1687 dev->devclass = NULL;
1689 dev->nameunit = NULL;
1693 dev->flags = DF_ENABLED;
1696 dev->flags |= DF_WILDCARD;
1698 dev->flags |= DF_FIXEDCLASS;
1699 if (devclass_add_device(dc, dev)) {
1700 kobj_delete((kobj_t) dev, M_BUS);
1707 dev->state = DS_NOTPRESENT;
1709 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1710 bus_data_generation_update();
1717 * @brief Print a description of a device.
1720 device_print_child(device_t dev, device_t child)
1724 if (device_is_alive(child))
1725 retval += BUS_PRINT_CHILD(dev, child);
1727 retval += device_printf(child, " not found\n");
1733 * @brief Create a new device
1735 * This creates a new device and adds it as a child of an existing
1736 * parent device. The new device will be added after the last existing
1737 * child with order zero.
1739 * @param dev the device which will be the parent of the
1741 * @param name devclass name for new device or @c NULL if not
1743 * @param unit unit number for new device or @c -1 if not
1746 * @returns the new device
1749 device_add_child(device_t dev, const char *name, int unit)
1751 return (device_add_child_ordered(dev, 0, name, unit));
1755 * @brief Create a new device
1757 * This creates a new device and adds it as a child of an existing
1758 * parent device. The new device will be added after the last existing
1759 * child with the same order.
1761 * @param dev the device which will be the parent of the
1763 * @param order a value which is used to partially sort the
1764 * children of @p dev - devices created using
1765 * lower values of @p order appear first in @p
1766 * dev's list of children
1767 * @param name devclass name for new device or @c NULL if not
1769 * @param unit unit number for new device or @c -1 if not
1772 * @returns the new device
1775 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1780 PDEBUG(("%s at %s with order %d as unit %d",
1781 name, DEVICENAME(dev), order, unit));
1783 child = make_device(dev, name, unit);
1786 child->order = order;
1788 TAILQ_FOREACH(place, &dev->children, link) {
1789 if (place->order > order)
1795 * The device 'place' is the first device whose order is
1796 * greater than the new child.
1798 TAILQ_INSERT_BEFORE(place, child, link);
1801 * The new child's order is greater or equal to the order of
1802 * any existing device. Add the child to the tail of the list.
1804 TAILQ_INSERT_TAIL(&dev->children, child, link);
1807 bus_data_generation_update();
1812 * @brief Delete a device
1814 * This function deletes a device along with all of its children. If
1815 * the device currently has a driver attached to it, the device is
1816 * detached first using device_detach().
1818 * @param dev the parent device
1819 * @param child the device to delete
1822 * @retval non-zero a unit error code describing the error
1825 device_delete_child(device_t dev, device_t child)
1828 device_t grandchild;
1830 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1832 /* remove children first */
1833 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1834 error = device_delete_child(child, grandchild);
1839 if ((error = device_detach(child)) != 0)
1841 if (child->devclass)
1842 devclass_delete_device(child->devclass, child);
1843 TAILQ_REMOVE(&dev->children, child, link);
1844 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1845 kobj_delete((kobj_t) child, M_BUS);
1847 bus_data_generation_update();
1852 * @brief Find a device given a unit number
1854 * This is similar to devclass_get_devices() but only searches for
1855 * devices which have @p dev as a parent.
1857 * @param dev the parent device to search
1858 * @param unit the unit number to search for. If the unit is -1,
1859 * return the first child of @p dev which has name
1860 * @p classname (that is, the one with the lowest unit.)
1862 * @returns the device with the given unit number or @c
1863 * NULL if there is no such device
1866 device_find_child(device_t dev, const char *classname, int unit)
1871 dc = devclass_find(classname);
1876 child = devclass_get_device(dc, unit);
1877 if (child && child->parent == dev)
1880 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1881 child = devclass_get_device(dc, unit);
1882 if (child && child->parent == dev)
1893 first_matching_driver(devclass_t dc, device_t dev)
1896 return (devclass_find_driver_internal(dc, dev->devclass->name));
1897 return (TAILQ_FIRST(&dc->drivers));
1904 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1906 if (dev->devclass) {
1908 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1909 if (!strcmp(dev->devclass->name, dl->driver->name))
1913 return (TAILQ_NEXT(last, link));
1920 device_probe_child(device_t dev, device_t child)
1923 driverlink_t best = NULL;
1925 int result, pri = 0;
1926 int hasclass = (child->devclass != NULL);
1932 panic("device_probe_child: parent device has no devclass");
1935 * If the state is already probed, then return. However, don't
1936 * return if we can rebid this object.
1938 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1941 for (; dc; dc = dc->parent) {
1942 for (dl = first_matching_driver(dc, child);
1944 dl = next_matching_driver(dc, child, dl)) {
1946 /* If this driver's pass is too high, then ignore it. */
1947 if (dl->pass > bus_current_pass)
1950 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1951 device_set_driver(child, dl->driver);
1953 if (device_set_devclass(child, dl->driver->name)) {
1954 printf("driver bug: Unable to set devclass (devname: %s)\n",
1955 (child ? device_get_name(child) :
1957 device_set_driver(child, NULL);
1962 /* Fetch any flags for the device before probing. */
1963 resource_int_value(dl->driver->name, child->unit,
1964 "flags", &child->devflags);
1966 result = DEVICE_PROBE(child);
1968 /* Reset flags and devclass before the next probe. */
1969 child->devflags = 0;
1971 device_set_devclass(child, NULL);
1974 * If the driver returns SUCCESS, there can be
1975 * no higher match for this device.
1984 * The driver returned an error so it
1985 * certainly doesn't match.
1988 device_set_driver(child, NULL);
1993 * A priority lower than SUCCESS, remember the
1994 * best matching driver. Initialise the value
1995 * of pri for the first match.
1997 if (best == NULL || result > pri) {
1999 * Probes that return BUS_PROBE_NOWILDCARD
2000 * or lower only match when they are set
2001 * in stone by the parent bus.
2003 if (result <= BUS_PROBE_NOWILDCARD &&
2004 child->flags & DF_WILDCARD)
2012 * If we have an unambiguous match in this devclass,
2013 * don't look in the parent.
2015 if (best && pri == 0)
2020 * If we found a driver, change state and initialise the devclass.
2022 /* XXX What happens if we rebid and got no best? */
2025 * If this device was atached, and we were asked to
2026 * rescan, and it is a different driver, then we have
2027 * to detach the old driver and reattach this new one.
2028 * Note, we don't have to check for DF_REBID here
2029 * because if the state is > DS_ALIVE, we know it must
2032 * This assumes that all DF_REBID drivers can have
2033 * their probe routine called at any time and that
2034 * they are idempotent as well as completely benign in
2035 * normal operations.
2037 * We also have to make sure that the detach
2038 * succeeded, otherwise we fail the operation (or
2039 * maybe it should just fail silently? I'm torn).
2041 if (child->state > DS_ALIVE && best->driver != child->driver)
2042 if ((result = device_detach(dev)) != 0)
2045 /* Set the winning driver, devclass, and flags. */
2046 if (!child->devclass) {
2047 result = device_set_devclass(child, best->driver->name);
2051 device_set_driver(child, best->driver);
2052 resource_int_value(best->driver->name, child->unit,
2053 "flags", &child->devflags);
2057 * A bit bogus. Call the probe method again to make
2058 * sure that we have the right description.
2060 DEVICE_PROBE(child);
2062 child->flags |= DF_REBID;
2065 child->flags &= ~DF_REBID;
2066 child->state = DS_ALIVE;
2068 bus_data_generation_update();
2076 * @brief Return the parent of a device
2079 device_get_parent(device_t dev)
2081 return (dev->parent);
2085 * @brief Get a list of children of a device
2087 * An array containing a list of all the children of the given device
2088 * is allocated and returned in @p *devlistp. The number of devices
2089 * in the array is returned in @p *devcountp. The caller should free
2090 * the array using @c free(p, M_TEMP).
2092 * @param dev the device to examine
2093 * @param devlistp points at location for array pointer return
2095 * @param devcountp points at location for array size return value
2098 * @retval ENOMEM the array allocation failed
2101 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2108 TAILQ_FOREACH(child, &dev->children, link) {
2112 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2117 TAILQ_FOREACH(child, &dev->children, link) {
2118 list[count] = child;
2129 * @brief Return the current driver for the device or @c NULL if there
2130 * is no driver currently attached
2133 device_get_driver(device_t dev)
2135 return (dev->driver);
2139 * @brief Return the current devclass for the device or @c NULL if
2143 device_get_devclass(device_t dev)
2145 return (dev->devclass);
2149 * @brief Return the name of the device's devclass or @c NULL if there
2153 device_get_name(device_t dev)
2155 if (dev != NULL && dev->devclass)
2156 return (devclass_get_name(dev->devclass));
2161 * @brief Return a string containing the device's devclass name
2162 * followed by an ascii representation of the device's unit number
2166 device_get_nameunit(device_t dev)
2168 return (dev->nameunit);
2172 * @brief Return the device's unit number.
2175 device_get_unit(device_t dev)
2181 * @brief Return the device's description string
2184 device_get_desc(device_t dev)
2190 * @brief Return the device's flags
2193 device_get_flags(device_t dev)
2195 return (dev->devflags);
2198 struct sysctl_ctx_list *
2199 device_get_sysctl_ctx(device_t dev)
2201 return (&dev->sysctl_ctx);
2205 device_get_sysctl_tree(device_t dev)
2207 return (dev->sysctl_tree);
2211 * @brief Print the name of the device followed by a colon and a space
2213 * @returns the number of characters printed
2216 device_print_prettyname(device_t dev)
2218 const char *name = device_get_name(dev);
2221 return (printf("unknown: "));
2222 return (printf("%s%d: ", name, device_get_unit(dev)));
2226 * @brief Print the name of the device followed by a colon, a space
2227 * and the result of calling vprintf() with the value of @p fmt and
2228 * the following arguments.
2230 * @returns the number of characters printed
2233 device_printf(device_t dev, const char * fmt, ...)
2238 retval = device_print_prettyname(dev);
2240 retval += vprintf(fmt, ap);
2249 device_set_desc_internal(device_t dev, const char* desc, int copy)
2251 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2252 free(dev->desc, M_BUS);
2253 dev->flags &= ~DF_DESCMALLOCED;
2258 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2260 strcpy(dev->desc, desc);
2261 dev->flags |= DF_DESCMALLOCED;
2264 /* Avoid a -Wcast-qual warning */
2265 dev->desc = (char *)(uintptr_t) desc;
2268 bus_data_generation_update();
2272 * @brief Set the device's description
2274 * The value of @c desc should be a string constant that will not
2275 * change (at least until the description is changed in a subsequent
2276 * call to device_set_desc() or device_set_desc_copy()).
2279 device_set_desc(device_t dev, const char* desc)
2281 device_set_desc_internal(dev, desc, FALSE);
2285 * @brief Set the device's description
2287 * The string pointed to by @c desc is copied. Use this function if
2288 * the device description is generated, (e.g. with sprintf()).
2291 device_set_desc_copy(device_t dev, const char* desc)
2293 device_set_desc_internal(dev, desc, TRUE);
2297 * @brief Set the device's flags
2300 device_set_flags(device_t dev, u_int32_t flags)
2302 dev->devflags = flags;
2306 * @brief Return the device's softc field
2308 * The softc is allocated and zeroed when a driver is attached, based
2309 * on the size field of the driver.
2312 device_get_softc(device_t dev)
2314 return (dev->softc);
2318 * @brief Set the device's softc field
2320 * Most drivers do not need to use this since the softc is allocated
2321 * automatically when the driver is attached.
2324 device_set_softc(device_t dev, void *softc)
2326 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2327 free(dev->softc, M_BUS_SC);
2330 dev->flags |= DF_EXTERNALSOFTC;
2332 dev->flags &= ~DF_EXTERNALSOFTC;
2336 * @brief Get the device's ivars field
2338 * The ivars field is used by the parent device to store per-device
2339 * state (e.g. the physical location of the device or a list of
2343 device_get_ivars(device_t dev)
2346 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2347 return (dev->ivars);
2351 * @brief Set the device's ivars field
2354 device_set_ivars(device_t dev, void * ivars)
2357 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2362 * @brief Return the device's state
2365 device_get_state(device_t dev)
2367 return (dev->state);
2371 * @brief Set the DF_ENABLED flag for the device
2374 device_enable(device_t dev)
2376 dev->flags |= DF_ENABLED;
2380 * @brief Clear the DF_ENABLED flag for the device
2383 device_disable(device_t dev)
2385 dev->flags &= ~DF_ENABLED;
2389 * @brief Increment the busy counter for the device
2392 device_busy(device_t dev)
2394 if (dev->state < DS_ATTACHED)
2395 panic("device_busy: called for unattached device");
2396 if (dev->busy == 0 && dev->parent)
2397 device_busy(dev->parent);
2399 dev->state = DS_BUSY;
2403 * @brief Decrement the busy counter for the device
2406 device_unbusy(device_t dev)
2408 if (dev->state != DS_BUSY)
2409 panic("device_unbusy: called for non-busy device %s",
2410 device_get_nameunit(dev));
2412 if (dev->busy == 0) {
2414 device_unbusy(dev->parent);
2415 dev->state = DS_ATTACHED;
2420 * @brief Set the DF_QUIET flag for the device
2423 device_quiet(device_t dev)
2425 dev->flags |= DF_QUIET;
2429 * @brief Clear the DF_QUIET flag for the device
2432 device_verbose(device_t dev)
2434 dev->flags &= ~DF_QUIET;
2438 * @brief Return non-zero if the DF_QUIET flag is set on the device
2441 device_is_quiet(device_t dev)
2443 return ((dev->flags & DF_QUIET) != 0);
2447 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2450 device_is_enabled(device_t dev)
2452 return ((dev->flags & DF_ENABLED) != 0);
2456 * @brief Return non-zero if the device was successfully probed
2459 device_is_alive(device_t dev)
2461 return (dev->state >= DS_ALIVE);
2465 * @brief Return non-zero if the device currently has a driver
2469 device_is_attached(device_t dev)
2471 return (dev->state >= DS_ATTACHED);
2475 * @brief Set the devclass of a device
2476 * @see devclass_add_device().
2479 device_set_devclass(device_t dev, const char *classname)
2486 devclass_delete_device(dev->devclass, dev);
2490 if (dev->devclass) {
2491 printf("device_set_devclass: device class already set\n");
2495 dc = devclass_find_internal(classname, NULL, TRUE);
2499 error = devclass_add_device(dc, dev);
2501 bus_data_generation_update();
2506 * @brief Set the driver of a device
2509 * @retval EBUSY the device already has a driver attached
2510 * @retval ENOMEM a memory allocation failure occurred
2513 device_set_driver(device_t dev, driver_t *driver)
2515 if (dev->state >= DS_ATTACHED)
2518 if (dev->driver == driver)
2521 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2522 free(dev->softc, M_BUS_SC);
2525 kobj_delete((kobj_t) dev, NULL);
2526 dev->driver = driver;
2528 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2529 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2530 dev->softc = malloc(driver->size, M_BUS_SC,
2533 kobj_delete((kobj_t) dev, NULL);
2534 kobj_init((kobj_t) dev, &null_class);
2540 kobj_init((kobj_t) dev, &null_class);
2543 bus_data_generation_update();
2548 * @brief Probe a device, and return this status.
2550 * This function is the core of the device autoconfiguration
2551 * system. Its purpose is to select a suitable driver for a device and
2552 * then call that driver to initialise the hardware appropriately. The
2553 * driver is selected by calling the DEVICE_PROBE() method of a set of
2554 * candidate drivers and then choosing the driver which returned the
2555 * best value. This driver is then attached to the device using
2558 * The set of suitable drivers is taken from the list of drivers in
2559 * the parent device's devclass. If the device was originally created
2560 * with a specific class name (see device_add_child()), only drivers
2561 * with that name are probed, otherwise all drivers in the devclass
2562 * are probed. If no drivers return successful probe values in the
2563 * parent devclass, the search continues in the parent of that
2564 * devclass (see devclass_get_parent()) if any.
2566 * @param dev the device to initialise
2569 * @retval ENXIO no driver was found
2570 * @retval ENOMEM memory allocation failure
2571 * @retval non-zero some other unix error code
2572 * @retval -1 Device already attached
2575 device_probe(device_t dev)
2581 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2584 if (!(dev->flags & DF_ENABLED)) {
2585 if (bootverbose && device_get_name(dev) != NULL) {
2586 device_print_prettyname(dev);
2587 printf("not probed (disabled)\n");
2591 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2592 if (bus_current_pass == BUS_PASS_DEFAULT &&
2593 !(dev->flags & DF_DONENOMATCH)) {
2594 BUS_PROBE_NOMATCH(dev->parent, dev);
2596 dev->flags |= DF_DONENOMATCH;
2604 * @brief Probe a device and attach a driver if possible
2606 * calls device_probe() and attaches if that was successful.
2609 device_probe_and_attach(device_t dev)
2615 error = device_probe(dev);
2618 else if (error != 0)
2620 return (device_attach(dev));
2624 * @brief Attach a device driver to a device
2626 * This function is a wrapper around the DEVICE_ATTACH() driver
2627 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2628 * device's sysctl tree, optionally prints a description of the device
2629 * and queues a notification event for user-based device management
2632 * Normally this function is only called internally from
2633 * device_probe_and_attach().
2635 * @param dev the device to initialise
2638 * @retval ENXIO no driver was found
2639 * @retval ENOMEM memory allocation failure
2640 * @retval non-zero some other unix error code
2643 device_attach(device_t dev)
2647 device_sysctl_init(dev);
2648 if (!device_is_quiet(dev))
2649 device_print_child(dev->parent, dev);
2650 if ((error = DEVICE_ATTACH(dev)) != 0) {
2651 printf("device_attach: %s%d attach returned %d\n",
2652 dev->driver->name, dev->unit, error);
2653 /* Unset the class; set in device_probe_child */
2654 if (dev->devclass == NULL)
2655 device_set_devclass(dev, NULL);
2656 device_set_driver(dev, NULL);
2657 device_sysctl_fini(dev);
2658 dev->state = DS_NOTPRESENT;
2661 device_sysctl_update(dev);
2662 dev->state = DS_ATTACHED;
2668 * @brief Detach a driver from a device
2670 * This function is a wrapper around the DEVICE_DETACH() driver
2671 * method. If the call to DEVICE_DETACH() succeeds, it calls
2672 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2673 * notification event for user-based device management services and
2674 * cleans up the device's sysctl tree.
2676 * @param dev the device to un-initialise
2679 * @retval ENXIO no driver was found
2680 * @retval ENOMEM memory allocation failure
2681 * @retval non-zero some other unix error code
2684 device_detach(device_t dev)
2690 PDEBUG(("%s", DEVICENAME(dev)));
2691 if (dev->state == DS_BUSY)
2693 if (dev->state != DS_ATTACHED)
2696 if ((error = DEVICE_DETACH(dev)) != 0)
2699 if (!device_is_quiet(dev))
2700 device_printf(dev, "detached\n");
2702 BUS_CHILD_DETACHED(dev->parent, dev);
2704 if (!(dev->flags & DF_FIXEDCLASS))
2705 devclass_delete_device(dev->devclass, dev);
2707 dev->state = DS_NOTPRESENT;
2708 device_set_driver(dev, NULL);
2709 device_set_desc(dev, NULL);
2710 device_sysctl_fini(dev);
2716 * @brief Tells a driver to quiesce itself.
2718 * This function is a wrapper around the DEVICE_QUIESCE() driver
2719 * method. If the call to DEVICE_QUIESCE() succeeds.
2721 * @param dev the device to quiesce
2724 * @retval ENXIO no driver was found
2725 * @retval ENOMEM memory allocation failure
2726 * @retval non-zero some other unix error code
2729 device_quiesce(device_t dev)
2732 PDEBUG(("%s", DEVICENAME(dev)));
2733 if (dev->state == DS_BUSY)
2735 if (dev->state != DS_ATTACHED)
2738 return (DEVICE_QUIESCE(dev));
2742 * @brief Notify a device of system shutdown
2744 * This function calls the DEVICE_SHUTDOWN() driver method if the
2745 * device currently has an attached driver.
2747 * @returns the value returned by DEVICE_SHUTDOWN()
2750 device_shutdown(device_t dev)
2752 if (dev->state < DS_ATTACHED)
2754 return (DEVICE_SHUTDOWN(dev));
2758 * @brief Set the unit number of a device
2760 * This function can be used to override the unit number used for a
2761 * device (e.g. to wire a device to a pre-configured unit number).
2764 device_set_unit(device_t dev, int unit)
2769 dc = device_get_devclass(dev);
2770 if (unit < dc->maxunit && dc->devices[unit])
2772 err = devclass_delete_device(dc, dev);
2776 err = devclass_add_device(dc, dev);
2780 bus_data_generation_update();
2784 /*======================================*/
2786 * Some useful method implementations to make life easier for bus drivers.
2790 * @brief Initialise a resource list.
2792 * @param rl the resource list to initialise
2795 resource_list_init(struct resource_list *rl)
2801 * @brief Reclaim memory used by a resource list.
2803 * This function frees the memory for all resource entries on the list
2806 * @param rl the resource list to free
2809 resource_list_free(struct resource_list *rl)
2811 struct resource_list_entry *rle;
2813 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2815 panic("resource_list_free: resource entry is busy");
2816 STAILQ_REMOVE_HEAD(rl, link);
2822 * @brief Add a resource entry.
2824 * This function adds a resource entry using the given @p type, @p
2825 * start, @p end and @p count values. A rid value is chosen by
2826 * searching sequentially for the first unused rid starting at zero.
2828 * @param rl the resource list to edit
2829 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2830 * @param start the start address of the resource
2831 * @param end the end address of the resource
2832 * @param count XXX end-start+1
2835 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2836 u_long end, u_long count)
2841 while (resource_list_find(rl, type, rid) != NULL)
2843 resource_list_add(rl, type, rid, start, end, count);
2848 * @brief Add or modify a resource entry.
2850 * If an existing entry exists with the same type and rid, it will be
2851 * modified using the given values of @p start, @p end and @p
2852 * count. If no entry exists, a new one will be created using the
2853 * given values. The resource list entry that matches is then returned.
2855 * @param rl the resource list to edit
2856 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2857 * @param rid the resource identifier
2858 * @param start the start address of the resource
2859 * @param end the end address of the resource
2860 * @param count XXX end-start+1
2862 struct resource_list_entry *
2863 resource_list_add(struct resource_list *rl, int type, int rid,
2864 u_long start, u_long end, u_long count)
2866 struct resource_list_entry *rle;
2868 rle = resource_list_find(rl, type, rid);
2870 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2873 panic("resource_list_add: can't record entry");
2874 STAILQ_INSERT_TAIL(rl, rle, link);
2881 panic("resource_list_add: resource entry is busy");
2890 * @brief Find a resource entry by type and rid.
2892 * @param rl the resource list to search
2893 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2894 * @param rid the resource identifier
2896 * @returns the resource entry pointer or NULL if there is no such
2899 struct resource_list_entry *
2900 resource_list_find(struct resource_list *rl, int type, int rid)
2902 struct resource_list_entry *rle;
2904 STAILQ_FOREACH(rle, rl, link) {
2905 if (rle->type == type && rle->rid == rid)
2912 * @brief Delete a resource entry.
2914 * @param rl the resource list to edit
2915 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2916 * @param rid the resource identifier
2919 resource_list_delete(struct resource_list *rl, int type, int rid)
2921 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2924 if (rle->res != NULL)
2925 panic("resource_list_delete: resource has not been released");
2926 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2932 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2934 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2935 * and passing the allocation up to the parent of @p bus. This assumes
2936 * that the first entry of @c device_get_ivars(child) is a struct
2937 * resource_list. This also handles 'passthrough' allocations where a
2938 * child is a remote descendant of bus by passing the allocation up to
2939 * the parent of bus.
2941 * Typically, a bus driver would store a list of child resources
2942 * somewhere in the child device's ivars (see device_get_ivars()) and
2943 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2944 * then call resource_list_alloc() to perform the allocation.
2946 * @param rl the resource list to allocate from
2947 * @param bus the parent device of @p child
2948 * @param child the device which is requesting an allocation
2949 * @param type the type of resource to allocate
2950 * @param rid a pointer to the resource identifier
2951 * @param start hint at the start of the resource range - pass
2952 * @c 0UL for any start address
2953 * @param end hint at the end of the resource range - pass
2954 * @c ~0UL for any end address
2955 * @param count hint at the size of range required - pass @c 1
2957 * @param flags any extra flags to control the resource
2958 * allocation - see @c RF_XXX flags in
2959 * <sys/rman.h> for details
2961 * @returns the resource which was allocated or @c NULL if no
2962 * resource could be allocated
2965 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2966 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2968 struct resource_list_entry *rle = NULL;
2969 int passthrough = (device_get_parent(child) != bus);
2970 int isdefault = (start == 0UL && end == ~0UL);
2973 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2974 type, rid, start, end, count, flags));
2977 rle = resource_list_find(rl, type, *rid);
2980 return (NULL); /* no resource of that type/rid */
2983 panic("resource_list_alloc: resource entry is busy");
2987 count = ulmax(count, rle->count);
2988 end = ulmax(rle->end, start + count - 1);
2991 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2992 type, rid, start, end, count, flags);
2995 * Record the new range.
2998 rle->start = rman_get_start(rle->res);
2999 rle->end = rman_get_end(rle->res);
3007 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3009 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3010 * used with resource_list_alloc().
3012 * @param rl the resource list which was allocated from
3013 * @param bus the parent device of @p child
3014 * @param child the device which is requesting a release
3015 * @param type the type of resource to allocate
3016 * @param rid the resource identifier
3017 * @param res the resource to release
3020 * @retval non-zero a standard unix error code indicating what
3021 * error condition prevented the operation
3024 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3025 int type, int rid, struct resource *res)
3027 struct resource_list_entry *rle = NULL;
3028 int passthrough = (device_get_parent(child) != bus);
3032 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3036 rle = resource_list_find(rl, type, rid);
3039 panic("resource_list_release: can't find resource");
3041 panic("resource_list_release: resource entry is not busy");
3043 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3053 * @brief Print a description of resources in a resource list
3055 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3056 * The name is printed if at least one resource of the given type is available.
3057 * The format is used to print resource start and end.
3059 * @param rl the resource list to print
3060 * @param name the name of @p type, e.g. @c "memory"
3061 * @param type type type of resource entry to print
3062 * @param format printf(9) format string to print resource
3063 * start and end values
3065 * @returns the number of characters printed
3068 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3071 struct resource_list_entry *rle;
3072 int printed, retval;
3076 /* Yes, this is kinda cheating */
3077 STAILQ_FOREACH(rle, rl, link) {
3078 if (rle->type == type) {
3080 retval += printf(" %s ", name);
3082 retval += printf(",");
3084 retval += printf(format, rle->start);
3085 if (rle->count > 1) {
3086 retval += printf("-");
3087 retval += printf(format, rle->start +
3096 * @brief Releases all the resources in a list.
3098 * @param rl The resource list to purge.
3103 resource_list_purge(struct resource_list *rl)
3105 struct resource_list_entry *rle;
3107 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3109 bus_release_resource(rman_get_device(rle->res),
3110 rle->type, rle->rid, rle->res);
3111 STAILQ_REMOVE_HEAD(rl, link);
3117 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
3120 return (device_add_child_ordered(dev, order, name, unit));
3124 * @brief Helper function for implementing DEVICE_PROBE()
3126 * This function can be used to help implement the DEVICE_PROBE() for
3127 * a bus (i.e. a device which has other devices attached to it). It
3128 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3132 bus_generic_probe(device_t dev)
3134 devclass_t dc = dev->devclass;
3137 TAILQ_FOREACH(dl, &dc->drivers, link) {
3139 * If this driver's pass is too high, then ignore it.
3140 * For most drivers in the default pass, this will
3141 * never be true. For early-pass drivers they will
3142 * only call the identify routines of eligible drivers
3143 * when this routine is called. Drivers for later
3144 * passes should have their identify routines called
3145 * on early-pass busses during BUS_NEW_PASS().
3147 if (dl->pass > bus_current_pass)
3149 DEVICE_IDENTIFY(dl->driver, dev);
3156 * @brief Helper function for implementing DEVICE_ATTACH()
3158 * This function can be used to help implement the DEVICE_ATTACH() for
3159 * a bus. It calls device_probe_and_attach() for each of the device's
3163 bus_generic_attach(device_t dev)
3167 TAILQ_FOREACH(child, &dev->children, link) {
3168 device_probe_and_attach(child);
3175 * @brief Helper function for implementing DEVICE_DETACH()
3177 * This function can be used to help implement the DEVICE_DETACH() for
3178 * a bus. It calls device_detach() for each of the device's
3182 bus_generic_detach(device_t dev)
3187 if (dev->state != DS_ATTACHED)
3190 TAILQ_FOREACH(child, &dev->children, link) {
3191 if ((error = device_detach(child)) != 0)
3199 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3201 * This function can be used to help implement the DEVICE_SHUTDOWN()
3202 * for a bus. It calls device_shutdown() for each of the device's
3206 bus_generic_shutdown(device_t dev)
3210 TAILQ_FOREACH(child, &dev->children, link) {
3211 device_shutdown(child);
3218 * @brief Helper function for implementing DEVICE_SUSPEND()
3220 * This function can be used to help implement the DEVICE_SUSPEND()
3221 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3222 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3223 * operation is aborted and any devices which were suspended are
3224 * resumed immediately by calling their DEVICE_RESUME() methods.
3227 bus_generic_suspend(device_t dev)
3230 device_t child, child2;
3232 TAILQ_FOREACH(child, &dev->children, link) {
3233 error = DEVICE_SUSPEND(child);
3235 for (child2 = TAILQ_FIRST(&dev->children);
3236 child2 && child2 != child;
3237 child2 = TAILQ_NEXT(child2, link))
3238 DEVICE_RESUME(child2);
3246 * @brief Helper function for implementing DEVICE_RESUME()
3248 * This function can be used to help implement the DEVICE_RESUME() for
3249 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3252 bus_generic_resume(device_t dev)
3256 TAILQ_FOREACH(child, &dev->children, link) {
3257 DEVICE_RESUME(child);
3258 /* if resume fails, there's nothing we can usefully do... */
3264 * @brief Helper function for implementing BUS_PRINT_CHILD().
3266 * This function prints the first part of the ascii representation of
3267 * @p child, including its name, unit and description (if any - see
3268 * device_set_desc()).
3270 * @returns the number of characters printed
3273 bus_print_child_header(device_t dev, device_t child)
3277 if (device_get_desc(child)) {
3278 retval += device_printf(child, "<%s>", device_get_desc(child));
3280 retval += printf("%s", device_get_nameunit(child));
3287 * @brief Helper function for implementing BUS_PRINT_CHILD().
3289 * This function prints the last part of the ascii representation of
3290 * @p child, which consists of the string @c " on " followed by the
3291 * name and unit of the @p dev.
3293 * @returns the number of characters printed
3296 bus_print_child_footer(device_t dev, device_t child)
3298 return (printf(" on %s\n", device_get_nameunit(dev)));
3302 * @brief Helper function for implementing BUS_PRINT_CHILD().
3304 * This function simply calls bus_print_child_header() followed by
3305 * bus_print_child_footer().
3307 * @returns the number of characters printed
3310 bus_generic_print_child(device_t dev, device_t child)
3314 retval += bus_print_child_header(dev, child);
3315 retval += bus_print_child_footer(dev, child);
3321 * @brief Stub function for implementing BUS_READ_IVAR().
3326 bus_generic_read_ivar(device_t dev, device_t child, int index,
3333 * @brief Stub function for implementing BUS_WRITE_IVAR().
3338 bus_generic_write_ivar(device_t dev, device_t child, int index,
3345 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3349 struct resource_list *
3350 bus_generic_get_resource_list(device_t dev, device_t child)
3356 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3358 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3359 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3360 * and then calls device_probe_and_attach() for each unattached child.
3363 bus_generic_driver_added(device_t dev, driver_t *driver)
3367 DEVICE_IDENTIFY(driver, dev);
3368 TAILQ_FOREACH(child, &dev->children, link) {
3369 if (child->state == DS_NOTPRESENT ||
3370 (child->flags & DF_REBID))
3371 device_probe_and_attach(child);
3376 * @brief Helper function for implementing BUS_NEW_PASS().
3378 * This implementing of BUS_NEW_PASS() first calls the identify
3379 * routines for any drivers that probe at the current pass. Then it
3380 * walks the list of devices for this bus. If a device is already
3381 * attached, then it calls BUS_NEW_PASS() on that device. If the
3382 * device is not already attached, it attempts to attach a driver to
3386 bus_generic_new_pass(device_t dev)
3393 TAILQ_FOREACH(dl, &dc->drivers, link) {
3394 if (dl->pass == bus_current_pass)
3395 DEVICE_IDENTIFY(dl->driver, dev);
3397 TAILQ_FOREACH(child, &dev->children, link) {
3398 if (child->state >= DS_ATTACHED)
3399 BUS_NEW_PASS(child);
3400 else if (child->state == DS_NOTPRESENT)
3401 device_probe_and_attach(child);
3406 * @brief Helper function for implementing BUS_SETUP_INTR().
3408 * This simple implementation of BUS_SETUP_INTR() simply calls the
3409 * BUS_SETUP_INTR() method of the parent of @p dev.
3412 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3413 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3416 /* Propagate up the bus hierarchy until someone handles it. */
3418 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3419 filter, intr, arg, cookiep));
3424 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3426 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3427 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3430 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3433 /* Propagate up the bus hierarchy until someone handles it. */
3435 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3440 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3442 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3443 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3446 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3447 u_long start, u_long end, u_long count, u_int flags)
3449 /* Propagate up the bus hierarchy until someone handles it. */
3451 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3452 start, end, count, flags));
3457 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3459 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3460 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3463 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3466 /* Propagate up the bus hierarchy until someone handles it. */
3468 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3474 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3476 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3477 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3480 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3483 /* Propagate up the bus hierarchy until someone handles it. */
3485 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3491 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3493 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3494 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3497 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3498 int rid, struct resource *r)
3500 /* Propagate up the bus hierarchy until someone handles it. */
3502 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3508 * @brief Helper function for implementing BUS_BIND_INTR().
3510 * This simple implementation of BUS_BIND_INTR() simply calls the
3511 * BUS_BIND_INTR() method of the parent of @p dev.
3514 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3518 /* Propagate up the bus hierarchy until someone handles it. */
3520 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3525 * @brief Helper function for implementing BUS_CONFIG_INTR().
3527 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3528 * BUS_CONFIG_INTR() method of the parent of @p dev.
3531 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3532 enum intr_polarity pol)
3535 /* Propagate up the bus hierarchy until someone handles it. */
3537 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3542 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3544 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3545 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3548 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3549 void *cookie, const char *descr)
3552 /* Propagate up the bus hierarchy until someone handles it. */
3554 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3560 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3562 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3563 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3566 bus_generic_get_dma_tag(device_t dev, device_t child)
3569 /* Propagate up the bus hierarchy until someone handles it. */
3570 if (dev->parent != NULL)
3571 return (BUS_GET_DMA_TAG(dev->parent, child));
3576 * @brief Helper function for implementing BUS_GET_RESOURCE().
3578 * This implementation of BUS_GET_RESOURCE() uses the
3579 * resource_list_find() function to do most of the work. It calls
3580 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3584 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3585 u_long *startp, u_long *countp)
3587 struct resource_list * rl = NULL;
3588 struct resource_list_entry * rle = NULL;
3590 rl = BUS_GET_RESOURCE_LIST(dev, child);
3594 rle = resource_list_find(rl, type, rid);
3599 *startp = rle->start;
3601 *countp = rle->count;
3607 * @brief Helper function for implementing BUS_SET_RESOURCE().
3609 * This implementation of BUS_SET_RESOURCE() uses the
3610 * resource_list_add() function to do most of the work. It calls
3611 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3615 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3616 u_long start, u_long count)
3618 struct resource_list * rl = NULL;
3620 rl = BUS_GET_RESOURCE_LIST(dev, child);
3624 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3630 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3632 * This implementation of BUS_DELETE_RESOURCE() uses the
3633 * resource_list_delete() function to do most of the work. It calls
3634 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3638 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3640 struct resource_list * rl = NULL;
3642 rl = BUS_GET_RESOURCE_LIST(dev, child);
3646 resource_list_delete(rl, type, rid);
3652 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3654 * This implementation of BUS_RELEASE_RESOURCE() uses the
3655 * resource_list_release() function to do most of the work. It calls
3656 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3659 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3660 int rid, struct resource *r)
3662 struct resource_list * rl = NULL;
3664 rl = BUS_GET_RESOURCE_LIST(dev, child);
3668 return (resource_list_release(rl, dev, child, type, rid, r));
3672 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3674 * This implementation of BUS_ALLOC_RESOURCE() uses the
3675 * resource_list_alloc() function to do most of the work. It calls
3676 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3679 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3680 int *rid, u_long start, u_long end, u_long count, u_int flags)
3682 struct resource_list * rl = NULL;
3684 rl = BUS_GET_RESOURCE_LIST(dev, child);
3688 return (resource_list_alloc(rl, dev, child, type, rid,
3689 start, end, count, flags));
3693 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3695 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3696 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3699 bus_generic_child_present(device_t dev, device_t child)
3701 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3705 * Some convenience functions to make it easier for drivers to use the
3706 * resource-management functions. All these really do is hide the
3707 * indirection through the parent's method table, making for slightly
3708 * less-wordy code. In the future, it might make sense for this code
3709 * to maintain some sort of a list of resources allocated by each device.
3713 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3714 struct resource **res)
3718 for (i = 0; rs[i].type != -1; i++)
3720 for (i = 0; rs[i].type != -1; i++) {
3721 res[i] = bus_alloc_resource_any(dev,
3722 rs[i].type, &rs[i].rid, rs[i].flags);
3723 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3724 bus_release_resources(dev, rs, res);
3732 bus_release_resources(device_t dev, const struct resource_spec *rs,
3733 struct resource **res)
3737 for (i = 0; rs[i].type != -1; i++)
3738 if (res[i] != NULL) {
3739 bus_release_resource(
3740 dev, rs[i].type, rs[i].rid, res[i]);
3746 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3748 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3752 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3753 u_long count, u_int flags)
3755 if (dev->parent == NULL)
3757 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3762 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3764 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3768 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3770 if (dev->parent == NULL)
3772 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3776 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3778 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3782 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3784 if (dev->parent == NULL)
3786 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3790 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3792 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3796 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3798 if (dev->parent == NULL)
3800 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3804 * @brief Wrapper function for BUS_SETUP_INTR().
3806 * This function simply calls the BUS_SETUP_INTR() method of the
3810 bus_setup_intr(device_t dev, struct resource *r, int flags,
3811 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3815 if (dev->parent == NULL)
3817 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
3821 if (handler != NULL && !(flags & INTR_MPSAFE))
3822 device_printf(dev, "[GIANT-LOCKED]\n");
3823 if (bootverbose && (flags & INTR_MPSAFE))
3824 device_printf(dev, "[MPSAFE]\n");
3825 if (filter != NULL) {
3826 if (handler == NULL)
3827 device_printf(dev, "[FILTER]\n");
3829 device_printf(dev, "[FILTER+ITHREAD]\n");
3831 device_printf(dev, "[ITHREAD]\n");
3836 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3838 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3842 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3844 if (dev->parent == NULL)
3846 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3850 * @brief Wrapper function for BUS_BIND_INTR().
3852 * This function simply calls the BUS_BIND_INTR() method of the
3856 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3858 if (dev->parent == NULL)
3860 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3864 * @brief Wrapper function for BUS_DESCRIBE_INTR().
3866 * This function first formats the requested description into a
3867 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
3868 * the parent of @p dev.
3871 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
3872 const char *fmt, ...)
3875 char descr[MAXCOMLEN + 1];
3877 if (dev->parent == NULL)
3880 vsnprintf(descr, sizeof(descr), fmt, ap);
3882 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
3886 * @brief Wrapper function for BUS_SET_RESOURCE().
3888 * This function simply calls the BUS_SET_RESOURCE() method of the
3892 bus_set_resource(device_t dev, int type, int rid,
3893 u_long start, u_long count)
3895 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3900 * @brief Wrapper function for BUS_GET_RESOURCE().
3902 * This function simply calls the BUS_GET_RESOURCE() method of the
3906 bus_get_resource(device_t dev, int type, int rid,
3907 u_long *startp, u_long *countp)
3909 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3914 * @brief Wrapper function for BUS_GET_RESOURCE().
3916 * This function simply calls the BUS_GET_RESOURCE() method of the
3917 * parent of @p dev and returns the start value.
3920 bus_get_resource_start(device_t dev, int type, int rid)
3922 u_long start, count;
3925 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3933 * @brief Wrapper function for BUS_GET_RESOURCE().
3935 * This function simply calls the BUS_GET_RESOURCE() method of the
3936 * parent of @p dev and returns the count value.
3939 bus_get_resource_count(device_t dev, int type, int rid)
3941 u_long start, count;
3944 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3952 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3954 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3958 bus_delete_resource(device_t dev, int type, int rid)
3960 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3964 * @brief Wrapper function for BUS_CHILD_PRESENT().
3966 * This function simply calls the BUS_CHILD_PRESENT() method of the
3970 bus_child_present(device_t child)
3972 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3976 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3978 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3982 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3986 parent = device_get_parent(child);
3987 if (parent == NULL) {
3991 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3995 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3997 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4001 bus_child_location_str(device_t child, char *buf, size_t buflen)
4005 parent = device_get_parent(child);
4006 if (parent == NULL) {
4010 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4014 * @brief Wrapper function for BUS_GET_DMA_TAG().
4016 * This function simply calls the BUS_GET_DMA_TAG() method of the
4020 bus_get_dma_tag(device_t dev)
4024 parent = device_get_parent(dev);
4027 return (BUS_GET_DMA_TAG(parent, dev));
4030 /* Resume all devices and then notify userland that we're up again. */
4032 root_resume(device_t dev)
4036 error = bus_generic_resume(dev);
4038 devctl_notify("kern", "power", "resume", NULL);
4043 root_print_child(device_t dev, device_t child)
4047 retval += bus_print_child_header(dev, child);
4048 retval += printf("\n");
4054 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4055 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4058 * If an interrupt mapping gets to here something bad has happened.
4060 panic("root_setup_intr");
4064 * If we get here, assume that the device is permanant and really is
4065 * present in the system. Removable bus drivers are expected to intercept
4066 * this call long before it gets here. We return -1 so that drivers that
4067 * really care can check vs -1 or some ERRNO returned higher in the food
4071 root_child_present(device_t dev, device_t child)
4076 static kobj_method_t root_methods[] = {
4077 /* Device interface */
4078 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4079 KOBJMETHOD(device_suspend, bus_generic_suspend),
4080 KOBJMETHOD(device_resume, root_resume),
4083 KOBJMETHOD(bus_print_child, root_print_child),
4084 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4085 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4086 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4087 KOBJMETHOD(bus_child_present, root_child_present),
4092 static driver_t root_driver = {
4099 devclass_t root_devclass;
4102 root_bus_module_handler(module_t mod, int what, void* arg)
4106 TAILQ_INIT(&bus_data_devices);
4107 kobj_class_compile((kobj_class_t) &root_driver);
4108 root_bus = make_device(NULL, "root", 0);
4109 root_bus->desc = "System root bus";
4110 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4111 root_bus->driver = &root_driver;
4112 root_bus->state = DS_ATTACHED;
4113 root_devclass = devclass_find_internal("root", NULL, FALSE);
4118 device_shutdown(root_bus);
4121 return (EOPNOTSUPP);
4127 static moduledata_t root_bus_mod = {
4129 root_bus_module_handler,
4132 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4135 * @brief Automatically configure devices
4137 * This function begins the autoconfiguration process by calling
4138 * device_probe_and_attach() for each child of the @c root0 device.
4141 root_bus_configure(void)
4146 /* Eventually this will be split up, but this is sufficient for now. */
4147 bus_set_pass(BUS_PASS_DEFAULT);
4151 * @brief Module handler for registering device drivers
4153 * This module handler is used to automatically register device
4154 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4155 * devclass_add_driver() for the driver described by the
4156 * driver_module_data structure pointed to by @p arg
4159 driver_module_handler(module_t mod, int what, void *arg)
4161 struct driver_module_data *dmd;
4162 devclass_t bus_devclass;
4163 kobj_class_t driver;
4166 dmd = (struct driver_module_data *)arg;
4167 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4172 if (dmd->dmd_chainevh)
4173 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4175 pass = dmd->dmd_pass;
4176 driver = dmd->dmd_driver;
4177 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4178 DRIVERNAME(driver), dmd->dmd_busname, pass));
4179 error = devclass_add_driver(bus_devclass, driver, pass,
4184 PDEBUG(("Unloading module: driver %s from bus %s",
4185 DRIVERNAME(dmd->dmd_driver),
4187 error = devclass_delete_driver(bus_devclass,
4190 if (!error && dmd->dmd_chainevh)
4191 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4194 PDEBUG(("Quiesce module: driver %s from bus %s",
4195 DRIVERNAME(dmd->dmd_driver),
4197 error = devclass_quiesce_driver(bus_devclass,
4200 if (!error && dmd->dmd_chainevh)
4201 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4212 * @brief Enumerate all hinted devices for this bus.
4214 * Walks through the hints for this bus and calls the bus_hinted_child
4215 * routine for each one it fines. It searches first for the specific
4216 * bus that's being probed for hinted children (eg isa0), and then for
4217 * generic children (eg isa).
4219 * @param dev bus device to enumerate
4222 bus_enumerate_hinted_children(device_t bus)
4225 const char *dname, *busname;
4229 * enumerate all devices on the specific bus
4231 busname = device_get_nameunit(bus);
4233 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4234 BUS_HINTED_CHILD(bus, dname, dunit);
4237 * and all the generic ones.
4239 busname = device_get_name(bus);
4241 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4242 BUS_HINTED_CHILD(bus, dname, dunit);
4247 /* the _short versions avoid iteration by not calling anything that prints
4248 * more than oneliners. I love oneliners.
4252 print_device_short(device_t dev, int indent)
4257 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4258 dev->unit, dev->desc,
4259 (dev->parent? "":"no "),
4260 (TAILQ_EMPTY(&dev->children)? "no ":""),
4261 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4262 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4263 (dev->flags&DF_WILDCARD? "wildcard,":""),
4264 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4265 (dev->flags&DF_REBID? "rebiddable,":""),
4266 (dev->ivars? "":"no "),
4267 (dev->softc? "":"no "),
4272 print_device(device_t dev, int indent)
4277 print_device_short(dev, indent);
4279 indentprintf(("Parent:\n"));
4280 print_device_short(dev->parent, indent+1);
4281 indentprintf(("Driver:\n"));
4282 print_driver_short(dev->driver, indent+1);
4283 indentprintf(("Devclass:\n"));
4284 print_devclass_short(dev->devclass, indent+1);
4288 print_device_tree_short(device_t dev, int indent)
4289 /* print the device and all its children (indented) */
4296 print_device_short(dev, indent);
4298 TAILQ_FOREACH(child, &dev->children, link) {
4299 print_device_tree_short(child, indent+1);
4304 print_device_tree(device_t dev, int indent)
4305 /* print the device and all its children (indented) */
4312 print_device(dev, indent);
4314 TAILQ_FOREACH(child, &dev->children, link) {
4315 print_device_tree(child, indent+1);
4320 print_driver_short(driver_t *driver, int indent)
4325 indentprintf(("driver %s: softc size = %zd\n",
4326 driver->name, driver->size));
4330 print_driver(driver_t *driver, int indent)
4335 print_driver_short(driver, indent);
4340 print_driver_list(driver_list_t drivers, int indent)
4342 driverlink_t driver;
4344 TAILQ_FOREACH(driver, &drivers, link) {
4345 print_driver(driver->driver, indent);
4350 print_devclass_short(devclass_t dc, int indent)
4355 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4359 print_devclass(devclass_t dc, int indent)
4366 print_devclass_short(dc, indent);
4367 indentprintf(("Drivers:\n"));
4368 print_driver_list(dc->drivers, indent+1);
4370 indentprintf(("Devices:\n"));
4371 for (i = 0; i < dc->maxunit; i++)
4373 print_device(dc->devices[i], indent+1);
4377 print_devclass_list_short(void)
4381 printf("Short listing of devclasses, drivers & devices:\n");
4382 TAILQ_FOREACH(dc, &devclasses, link) {
4383 print_devclass_short(dc, 0);
4388 print_devclass_list(void)
4392 printf("Full listing of devclasses, drivers & devices:\n");
4393 TAILQ_FOREACH(dc, &devclasses, link) {
4394 print_devclass(dc, 0);
4401 * User-space access to the device tree.
4403 * We implement a small set of nodes:
4405 * hw.bus Single integer read method to obtain the
4406 * current generation count.
4407 * hw.bus.devices Reads the entire device tree in flat space.
4408 * hw.bus.rman Resource manager interface
4410 * We might like to add the ability to scan devclasses and/or drivers to
4411 * determine what else is currently loaded/available.
4415 sysctl_bus(SYSCTL_HANDLER_ARGS)
4417 struct u_businfo ubus;
4419 ubus.ub_version = BUS_USER_VERSION;
4420 ubus.ub_generation = bus_data_generation;
4422 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4424 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4425 "bus-related data");
4428 sysctl_devices(SYSCTL_HANDLER_ARGS)
4430 int *name = (int *)arg1;
4431 u_int namelen = arg2;
4434 struct u_device udev; /* XXX this is a bit big */
4440 if (bus_data_generation_check(name[0]))
4446 * Scan the list of devices, looking for the requested index.
4448 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4456 * Populate the return array.
4458 bzero(&udev, sizeof(udev));
4459 udev.dv_handle = (uintptr_t)dev;
4460 udev.dv_parent = (uintptr_t)dev->parent;
4461 if (dev->nameunit != NULL)
4462 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4463 if (dev->desc != NULL)
4464 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4465 if (dev->driver != NULL && dev->driver->name != NULL)
4466 strlcpy(udev.dv_drivername, dev->driver->name,
4467 sizeof(udev.dv_drivername));
4468 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4469 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4470 udev.dv_devflags = dev->devflags;
4471 udev.dv_flags = dev->flags;
4472 udev.dv_state = dev->state;
4473 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4477 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4478 "system device tree");
4481 bus_data_generation_check(int generation)
4483 if (generation != bus_data_generation)
4486 /* XXX generate optimised lists here? */
4491 bus_data_generation_update(void)
4493 bus_data_generation++;
4497 bus_free_resource(device_t dev, int type, struct resource *r)
4501 return (bus_release_resource(dev, type, rman_get_rid(r), r));