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_int flags; /**< internal device flags */
125 #define DF_ENABLED 0x01 /* device should be probed/attached */
126 #define DF_FIXEDCLASS 0x02 /* devclass specified at create time */
127 #define DF_WILDCARD 0x04 /* unit was originally wildcard */
128 #define DF_DESCMALLOCED 0x08 /* description was malloced */
129 #define DF_QUIET 0x10 /* don't print verbose attach message */
130 #define DF_DONENOMATCH 0x20 /* don't execute DEVICE_NOMATCH again */
131 #define DF_EXTERNALSOFTC 0x40 /* softc not allocated by us */
132 #define DF_REBID 0x80 /* Can rebid after attach */
133 u_int order; /**< order from device_add_child_ordered() */
134 void *ivars; /**< instance variables */
135 void *softc; /**< current driver's variables */
137 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
138 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
141 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
142 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
146 static int bus_debug = 1;
147 TUNABLE_INT("bus.debug", &bus_debug);
148 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
151 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
152 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
153 #define DRIVERNAME(d) ((d)? d->name : "no driver")
154 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
157 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
158 * prevent syslog from deleting initial spaces
160 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
162 static void print_device_short(device_t dev, int indent);
163 static void print_device(device_t dev, int indent);
164 void print_device_tree_short(device_t dev, int indent);
165 void print_device_tree(device_t dev, int indent);
166 static void print_driver_short(driver_t *driver, int indent);
167 static void print_driver(driver_t *driver, int indent);
168 static void print_driver_list(driver_list_t drivers, int indent);
169 static void print_devclass_short(devclass_t dc, int indent);
170 static void print_devclass(devclass_t dc, int indent);
171 void print_devclass_list_short(void);
172 void print_devclass_list(void);
175 /* Make the compiler ignore the function calls */
176 #define PDEBUG(a) /* nop */
177 #define DEVICENAME(d) /* nop */
178 #define DRIVERNAME(d) /* nop */
179 #define DEVCLANAME(d) /* nop */
181 #define print_device_short(d,i) /* nop */
182 #define print_device(d,i) /* nop */
183 #define print_device_tree_short(d,i) /* nop */
184 #define print_device_tree(d,i) /* nop */
185 #define print_driver_short(d,i) /* nop */
186 #define print_driver(d,i) /* nop */
187 #define print_driver_list(d,i) /* nop */
188 #define print_devclass_short(d,i) /* nop */
189 #define print_devclass(d,i) /* nop */
190 #define print_devclass_list_short() /* nop */
191 #define print_devclass_list() /* nop */
199 DEVCLASS_SYSCTL_PARENT,
203 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
205 devclass_t dc = (devclass_t)arg1;
209 case DEVCLASS_SYSCTL_PARENT:
210 value = dc->parent ? dc->parent->name : "";
215 return (SYSCTL_OUT(req, value, strlen(value)));
219 devclass_sysctl_init(devclass_t dc)
222 if (dc->sysctl_tree != NULL)
224 sysctl_ctx_init(&dc->sysctl_ctx);
225 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
226 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
227 CTLFLAG_RD, NULL, "");
228 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
229 OID_AUTO, "%parent", CTLFLAG_RD,
230 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
236 DEVICE_SYSCTL_DRIVER,
237 DEVICE_SYSCTL_LOCATION,
238 DEVICE_SYSCTL_PNPINFO,
239 DEVICE_SYSCTL_PARENT,
243 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
245 device_t dev = (device_t)arg1;
252 case DEVICE_SYSCTL_DESC:
253 value = dev->desc ? dev->desc : "";
255 case DEVICE_SYSCTL_DRIVER:
256 value = dev->driver ? dev->driver->name : "";
258 case DEVICE_SYSCTL_LOCATION:
259 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
260 bus_child_location_str(dev, buf, 1024);
262 case DEVICE_SYSCTL_PNPINFO:
263 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
264 bus_child_pnpinfo_str(dev, buf, 1024);
266 case DEVICE_SYSCTL_PARENT:
267 value = dev->parent ? dev->parent->nameunit : "";
272 error = SYSCTL_OUT(req, value, strlen(value));
279 device_sysctl_init(device_t dev)
281 devclass_t dc = dev->devclass;
283 if (dev->sysctl_tree != NULL)
285 devclass_sysctl_init(dc);
286 sysctl_ctx_init(&dev->sysctl_ctx);
287 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
288 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
289 dev->nameunit + strlen(dc->name),
290 CTLFLAG_RD, NULL, "");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%desc", CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
294 "device description");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%driver", CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
298 "device driver name");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%location", CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
302 "device location relative to parent");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
306 "device identification");
307 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 OID_AUTO, "%parent", CTLFLAG_RD,
309 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
314 device_sysctl_update(device_t dev)
316 devclass_t dc = dev->devclass;
318 if (dev->sysctl_tree == NULL)
320 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
324 device_sysctl_fini(device_t dev)
326 if (dev->sysctl_tree == NULL)
328 sysctl_ctx_free(&dev->sysctl_ctx);
329 dev->sysctl_tree = NULL;
333 * /dev/devctl implementation
337 * This design allows only one reader for /dev/devctl. This is not desirable
338 * in the long run, but will get a lot of hair out of this implementation.
339 * Maybe we should make this device a clonable device.
341 * Also note: we specifically do not attach a device to the device_t tree
342 * to avoid potential chicken and egg problems. One could argue that all
343 * of this belongs to the root node. One could also further argue that the
344 * sysctl interface that we have not might more properly be an ioctl
345 * interface, but at this stage of the game, I'm not inclined to rock that
348 * I'm also not sure that the SIGIO support is done correctly or not, as
349 * I copied it from a driver that had SIGIO support that likely hasn't been
350 * tested since 3.4 or 2.2.8!
353 /* Deprecated way to adjust queue length */
354 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
355 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
356 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
357 0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
359 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
360 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
361 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
362 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
363 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
364 0, sysctl_devctl_queue, "I", "devctl queue length");
366 static d_open_t devopen;
367 static d_close_t devclose;
368 static d_read_t devread;
369 static d_ioctl_t devioctl;
370 static d_poll_t devpoll;
372 static struct cdevsw dev_cdevsw = {
373 .d_version = D_VERSION,
374 .d_flags = D_NEEDGIANT,
383 struct dev_event_info
386 TAILQ_ENTRY(dev_event_info) dei_link;
389 TAILQ_HEAD(devq, dev_event_info);
391 static struct dev_softc
400 struct proc *async_proc;
403 static struct cdev *devctl_dev;
408 devctl_dev = make_dev_credf(MAKEDEV_ETERNAL, &dev_cdevsw, 0, NULL,
409 UID_ROOT, GID_WHEEL, 0600, "devctl");
410 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
411 cv_init(&devsoftc.cv, "dev cv");
412 TAILQ_INIT(&devsoftc.devq);
416 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
422 devsoftc.nonblock = 0;
423 devsoftc.async_proc = NULL;
428 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
431 mtx_lock(&devsoftc.mtx);
432 cv_broadcast(&devsoftc.cv);
433 mtx_unlock(&devsoftc.mtx);
434 devsoftc.async_proc = NULL;
439 * The read channel for this device is used to report changes to
440 * userland in realtime. We are required to free the data as well as
441 * the n1 object because we allocate them separately. Also note that
442 * we return one record at a time. If you try to read this device a
443 * character at a time, you will lose the rest of the data. Listening
444 * programs are expected to cope.
447 devread(struct cdev *dev, struct uio *uio, int ioflag)
449 struct dev_event_info *n1;
452 mtx_lock(&devsoftc.mtx);
453 while (TAILQ_EMPTY(&devsoftc.devq)) {
454 if (devsoftc.nonblock) {
455 mtx_unlock(&devsoftc.mtx);
458 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
461 * Need to translate ERESTART to EINTR here? -- jake
463 mtx_unlock(&devsoftc.mtx);
467 n1 = TAILQ_FIRST(&devsoftc.devq);
468 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
470 mtx_unlock(&devsoftc.mtx);
471 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
472 free(n1->dei_data, M_BUS);
478 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
484 devsoftc.nonblock = 1;
486 devsoftc.nonblock = 0;
490 devsoftc.async_proc = td->td_proc;
492 devsoftc.async_proc = NULL;
495 /* (un)Support for other fcntl() calls. */
508 devpoll(struct cdev *dev, int events, struct thread *td)
512 mtx_lock(&devsoftc.mtx);
513 if (events & (POLLIN | POLLRDNORM)) {
514 if (!TAILQ_EMPTY(&devsoftc.devq))
515 revents = events & (POLLIN | POLLRDNORM);
517 selrecord(td, &devsoftc.sel);
519 mtx_unlock(&devsoftc.mtx);
525 * @brief Return whether the userland process is running
528 devctl_process_running(void)
530 return (devsoftc.inuse == 1);
534 * @brief Queue data to be read from the devctl device
536 * Generic interface to queue data to the devctl device. It is
537 * assumed that @p data is properly formatted. It is further assumed
538 * that @p data is allocated using the M_BUS malloc type.
541 devctl_queue_data_f(char *data, int flags)
543 struct dev_event_info *n1 = NULL, *n2 = NULL;
546 if (strlen(data) == 0)
548 if (devctl_queue_length == 0)
550 n1 = malloc(sizeof(*n1), M_BUS, flags);
554 mtx_lock(&devsoftc.mtx);
555 if (devctl_queue_length == 0) {
556 mtx_unlock(&devsoftc.mtx);
557 free(n1->dei_data, M_BUS);
561 /* Leave at least one spot in the queue... */
562 while (devsoftc.queued > devctl_queue_length - 1) {
563 n2 = TAILQ_FIRST(&devsoftc.devq);
564 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
565 free(n2->dei_data, M_BUS);
569 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
571 cv_broadcast(&devsoftc.cv);
572 mtx_unlock(&devsoftc.mtx);
573 selwakeup(&devsoftc.sel);
574 p = devsoftc.async_proc;
583 * We have to free data on all error paths since the caller
584 * assumes it will be free'd when this item is dequeued.
591 devctl_queue_data(char *data)
594 devctl_queue_data_f(data, M_NOWAIT);
598 * @brief Send a 'notification' to userland, using standard ways
601 devctl_notify_f(const char *system, const char *subsystem, const char *type,
602 const char *data, int flags)
608 return; /* BOGUS! Must specify system. */
609 if (subsystem == NULL)
610 return; /* BOGUS! Must specify subsystem. */
612 return; /* BOGUS! Must specify type. */
613 len += strlen(" system=") + strlen(system);
614 len += strlen(" subsystem=") + strlen(subsystem);
615 len += strlen(" type=") + strlen(type);
616 /* add in the data message plus newline. */
619 len += 3; /* '!', '\n', and NUL */
620 msg = malloc(len, M_BUS, flags);
622 return; /* Drop it on the floor */
624 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
625 system, subsystem, type, data);
627 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
628 system, subsystem, type);
629 devctl_queue_data_f(msg, flags);
633 devctl_notify(const char *system, const char *subsystem, const char *type,
637 devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
641 * Common routine that tries to make sending messages as easy as possible.
642 * We allocate memory for the data, copy strings into that, but do not
643 * free it unless there's an error. The dequeue part of the driver should
644 * free the data. We don't send data when the device is disabled. We do
645 * send data, even when we have no listeners, because we wish to avoid
646 * races relating to startup and restart of listening applications.
648 * devaddq is designed to string together the type of event, with the
649 * object of that event, plus the plug and play info and location info
650 * for that event. This is likely most useful for devices, but less
651 * useful for other consumers of this interface. Those should use
652 * the devctl_queue_data() interface instead.
655 devaddq(const char *type, const char *what, device_t dev)
662 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
664 data = malloc(1024, M_BUS, M_NOWAIT);
668 /* get the bus specific location of this device */
669 loc = malloc(1024, M_BUS, M_NOWAIT);
673 bus_child_location_str(dev, loc, 1024);
675 /* Get the bus specific pnp info of this device */
676 pnp = malloc(1024, M_BUS, M_NOWAIT);
680 bus_child_pnpinfo_str(dev, pnp, 1024);
682 /* Get the parent of this device, or / if high enough in the tree. */
683 if (device_get_parent(dev) == NULL)
684 parstr = "."; /* Or '/' ? */
686 parstr = device_get_nameunit(device_get_parent(dev));
687 /* String it all together. */
688 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
692 devctl_queue_data(data);
702 * A device was added to the tree. We are called just after it successfully
703 * attaches (that is, probe and attach success for this device). No call
704 * is made if a device is merely parented into the tree. See devnomatch
705 * if probe fails. If attach fails, no notification is sent (but maybe
706 * we should have a different message for this).
709 devadded(device_t dev)
714 pnp = malloc(1024, M_BUS, M_NOWAIT);
717 tmp = malloc(1024, M_BUS, M_NOWAIT);
721 bus_child_pnpinfo_str(dev, pnp, 1024);
722 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
723 devaddq("+", tmp, dev);
733 * A device was removed from the tree. We are called just before this
737 devremoved(device_t dev)
742 pnp = malloc(1024, M_BUS, M_NOWAIT);
745 tmp = malloc(1024, M_BUS, M_NOWAIT);
749 bus_child_pnpinfo_str(dev, pnp, 1024);
750 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
751 devaddq("-", tmp, dev);
761 * Called when there's no match for this device. This is only called
762 * the first time that no match happens, so we don't keep getting this
763 * message. Should that prove to be undesirable, we can change it.
764 * This is called when all drivers that can attach to a given bus
765 * decline to accept this device. Other errrors may not be detected.
768 devnomatch(device_t dev)
770 devaddq("?", "", dev);
774 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
776 struct dev_event_info *n1;
779 dis = devctl_queue_length == 0;
780 error = sysctl_handle_int(oidp, &dis, 0, req);
781 if (error || !req->newptr)
783 mtx_lock(&devsoftc.mtx);
785 while (!TAILQ_EMPTY(&devsoftc.devq)) {
786 n1 = TAILQ_FIRST(&devsoftc.devq);
787 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
788 free(n1->dei_data, M_BUS);
792 devctl_queue_length = 0;
794 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
796 mtx_unlock(&devsoftc.mtx);
801 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
803 struct dev_event_info *n1;
806 q = devctl_queue_length;
807 error = sysctl_handle_int(oidp, &q, 0, req);
808 if (error || !req->newptr)
812 mtx_lock(&devsoftc.mtx);
813 devctl_queue_length = q;
814 while (devsoftc.queued > devctl_queue_length) {
815 n1 = TAILQ_FIRST(&devsoftc.devq);
816 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
817 free(n1->dei_data, M_BUS);
821 mtx_unlock(&devsoftc.mtx);
825 /* End of /dev/devctl code */
827 static TAILQ_HEAD(,device) bus_data_devices;
828 static int bus_data_generation = 1;
830 static kobj_method_t null_methods[] = {
834 DEFINE_CLASS(null, null_methods, 0);
837 * Bus pass implementation
840 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
841 int bus_current_pass = BUS_PASS_ROOT;
845 * @brief Register the pass level of a new driver attachment
847 * Register a new driver attachment's pass level. If no driver
848 * attachment with the same pass level has been added, then @p new
849 * will be added to the global passes list.
851 * @param new the new driver attachment
854 driver_register_pass(struct driverlink *new)
856 struct driverlink *dl;
858 /* We only consider pass numbers during boot. */
859 if (bus_current_pass == BUS_PASS_DEFAULT)
863 * Walk the passes list. If we already know about this pass
864 * then there is nothing to do. If we don't, then insert this
865 * driver link into the list.
867 TAILQ_FOREACH(dl, &passes, passlink) {
868 if (dl->pass < new->pass)
870 if (dl->pass == new->pass)
872 TAILQ_INSERT_BEFORE(dl, new, passlink);
875 TAILQ_INSERT_TAIL(&passes, new, passlink);
879 * @brief Raise the current bus pass
881 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
882 * method on the root bus to kick off a new device tree scan for each
883 * new pass level that has at least one driver.
886 bus_set_pass(int pass)
888 struct driverlink *dl;
890 if (bus_current_pass > pass)
891 panic("Attempt to lower bus pass level");
893 TAILQ_FOREACH(dl, &passes, passlink) {
894 /* Skip pass values below the current pass level. */
895 if (dl->pass <= bus_current_pass)
899 * Bail once we hit a driver with a pass level that is
906 * Raise the pass level to the next level and rescan
909 bus_current_pass = dl->pass;
910 BUS_NEW_PASS(root_bus);
914 * If there isn't a driver registered for the requested pass,
915 * then bus_current_pass might still be less than 'pass'. Set
916 * it to 'pass' in that case.
918 if (bus_current_pass < pass)
919 bus_current_pass = pass;
920 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
924 * Devclass implementation
927 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
931 * @brief Find or create a device class
933 * If a device class with the name @p classname exists, return it,
934 * otherwise if @p create is non-zero create and return a new device
937 * If @p parentname is non-NULL, the parent of the devclass is set to
938 * the devclass of that name.
940 * @param classname the devclass name to find or create
941 * @param parentname the parent devclass name or @c NULL
942 * @param create non-zero to create a devclass
945 devclass_find_internal(const char *classname, const char *parentname,
950 PDEBUG(("looking for %s", classname));
954 TAILQ_FOREACH(dc, &devclasses, link) {
955 if (!strcmp(dc->name, classname))
960 PDEBUG(("creating %s", classname));
961 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
962 M_BUS, M_NOWAIT | M_ZERO);
966 dc->name = (char*) (dc + 1);
967 strcpy(dc->name, classname);
968 TAILQ_INIT(&dc->drivers);
969 TAILQ_INSERT_TAIL(&devclasses, dc, link);
971 bus_data_generation_update();
975 * If a parent class is specified, then set that as our parent so
976 * that this devclass will support drivers for the parent class as
977 * well. If the parent class has the same name don't do this though
978 * as it creates a cycle that can trigger an infinite loop in
979 * device_probe_child() if a device exists for which there is no
982 if (parentname && dc && !dc->parent &&
983 strcmp(classname, parentname) != 0) {
984 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
985 dc->parent->flags |= DC_HAS_CHILDREN;
992 * @brief Create a device class
994 * If a device class with the name @p classname exists, return it,
995 * otherwise create and return a new device class.
997 * @param classname the devclass name to find or create
1000 devclass_create(const char *classname)
1002 return (devclass_find_internal(classname, NULL, TRUE));
1006 * @brief Find a device class
1008 * If a device class with the name @p classname exists, return it,
1009 * otherwise return @c NULL.
1011 * @param classname the devclass name to find
1014 devclass_find(const char *classname)
1016 return (devclass_find_internal(classname, NULL, FALSE));
1020 * @brief Register that a device driver has been added to a devclass
1022 * Register that a device driver has been added to a devclass. This
1023 * is called by devclass_add_driver to accomplish the recursive
1024 * notification of all the children classes of dc, as well as dc.
1025 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1028 * We do a full search here of the devclass list at each iteration
1029 * level to save storing children-lists in the devclass structure. If
1030 * we ever move beyond a few dozen devices doing this, we may need to
1033 * @param dc the devclass to edit
1034 * @param driver the driver that was just added
1037 devclass_driver_added(devclass_t dc, driver_t *driver)
1043 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1045 for (i = 0; i < dc->maxunit; i++)
1046 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1047 BUS_DRIVER_ADDED(dc->devices[i], driver);
1050 * Walk through the children classes. Since we only keep a
1051 * single parent pointer around, we walk the entire list of
1052 * devclasses looking for children. We set the
1053 * DC_HAS_CHILDREN flag when a child devclass is created on
1054 * the parent, so we only walk the list for those devclasses
1055 * that have children.
1057 if (!(dc->flags & DC_HAS_CHILDREN))
1060 TAILQ_FOREACH(dc, &devclasses, link) {
1061 if (dc->parent == parent)
1062 devclass_driver_added(dc, driver);
1067 * @brief Add a device driver to a device class
1069 * Add a device driver to a devclass. This is normally called
1070 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1071 * all devices in the devclass will be called to allow them to attempt
1072 * to re-probe any unmatched children.
1074 * @param dc the devclass to edit
1075 * @param driver the driver to register
1078 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1081 const char *parentname;
1083 PDEBUG(("%s", DRIVERNAME(driver)));
1085 /* Don't allow invalid pass values. */
1086 if (pass <= BUS_PASS_ROOT)
1089 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1094 * Compile the driver's methods. Also increase the reference count
1095 * so that the class doesn't get freed when the last instance
1096 * goes. This means we can safely use static methods and avoids a
1097 * double-free in devclass_delete_driver.
1099 kobj_class_compile((kobj_class_t) driver);
1102 * If the driver has any base classes, make the
1103 * devclass inherit from the devclass of the driver's
1104 * first base class. This will allow the system to
1105 * search for drivers in both devclasses for children
1106 * of a device using this driver.
1108 if (driver->baseclasses)
1109 parentname = driver->baseclasses[0]->name;
1112 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1114 dl->driver = driver;
1115 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1116 driver->refs++; /* XXX: kobj_mtx */
1118 driver_register_pass(dl);
1120 devclass_driver_added(dc, driver);
1121 bus_data_generation_update();
1126 * @brief Register that a device driver has been deleted from a devclass
1128 * Register that a device driver has been removed from a devclass.
1129 * This is called by devclass_delete_driver to accomplish the
1130 * recursive notification of all the children classes of busclass, as
1131 * well as busclass. Each layer will attempt to detach the driver
1132 * from any devices that are children of the bus's devclass. The function
1133 * will return an error if a device fails to detach.
1135 * We do a full search here of the devclass list at each iteration
1136 * level to save storing children-lists in the devclass structure. If
1137 * we ever move beyond a few dozen devices doing this, we may need to
1140 * @param busclass the devclass of the parent bus
1141 * @param dc the devclass of the driver being deleted
1142 * @param driver the driver being deleted
1145 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
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);
1169 BUS_PROBE_NOMATCH(dev->parent, dev);
1171 dev->flags |= DF_DONENOMATCH;
1177 * Walk through the children classes. Since we only keep a
1178 * single parent pointer around, we walk the entire list of
1179 * devclasses looking for children. We set the
1180 * DC_HAS_CHILDREN flag when a child devclass is created on
1181 * the parent, so we only walk the list for those devclasses
1182 * that have children.
1184 if (!(busclass->flags & DC_HAS_CHILDREN))
1187 TAILQ_FOREACH(busclass, &devclasses, link) {
1188 if (busclass->parent == parent) {
1189 error = devclass_driver_deleted(busclass, dc, driver);
1198 * @brief Delete a device driver from a device class
1200 * Delete a device driver from a devclass. This is normally called
1201 * automatically by DRIVER_MODULE().
1203 * If the driver is currently attached to any devices,
1204 * devclass_delete_driver() will first attempt to detach from each
1205 * device. If one of the detach calls fails, the driver will not be
1208 * @param dc the devclass to edit
1209 * @param driver the driver to unregister
1212 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1214 devclass_t dc = devclass_find(driver->name);
1218 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1224 * Find the link structure in the bus' list of drivers.
1226 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1227 if (dl->driver == driver)
1232 PDEBUG(("%s not found in %s list", driver->name,
1237 error = devclass_driver_deleted(busclass, dc, driver);
1241 TAILQ_REMOVE(&busclass->drivers, dl, link);
1246 if (driver->refs == 0)
1247 kobj_class_free((kobj_class_t) driver);
1249 bus_data_generation_update();
1254 * @brief Quiesces a set of device drivers from a device class
1256 * Quiesce a device driver from a devclass. This is normally called
1257 * automatically by DRIVER_MODULE().
1259 * If the driver is currently attached to any devices,
1260 * devclass_quiesece_driver() will first attempt to quiesce each
1263 * @param dc the devclass to edit
1264 * @param driver the driver to unregister
1267 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1269 devclass_t dc = devclass_find(driver->name);
1275 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1281 * Find the link structure in the bus' list of drivers.
1283 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1284 if (dl->driver == driver)
1289 PDEBUG(("%s not found in %s list", driver->name,
1295 * Quiesce all devices. We iterate through all the devices in
1296 * the devclass of the driver and quiesce any which are using
1297 * the driver and which have a parent in the devclass which we
1300 * Note that since a driver can be in multiple devclasses, we
1301 * should not quiesce devices which are not children of
1302 * devices in the affected devclass.
1304 for (i = 0; i < dc->maxunit; i++) {
1305 if (dc->devices[i]) {
1306 dev = dc->devices[i];
1307 if (dev->driver == driver && dev->parent &&
1308 dev->parent->devclass == busclass) {
1309 if ((error = device_quiesce(dev)) != 0)
1322 devclass_find_driver_internal(devclass_t dc, const char *classname)
1326 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1328 TAILQ_FOREACH(dl, &dc->drivers, link) {
1329 if (!strcmp(dl->driver->name, classname))
1333 PDEBUG(("not found"));
1338 * @brief Return the name of the devclass
1341 devclass_get_name(devclass_t dc)
1347 * @brief Find a device given a unit number
1349 * @param dc the devclass to search
1350 * @param unit the unit number to search for
1352 * @returns the device with the given unit number or @c
1353 * NULL if there is no such device
1356 devclass_get_device(devclass_t dc, int unit)
1358 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1360 return (dc->devices[unit]);
1364 * @brief Find the softc field of a device given a unit number
1366 * @param dc the devclass to search
1367 * @param unit the unit number to search for
1369 * @returns the softc field of the device with the given
1370 * unit number or @c NULL if there is no such
1374 devclass_get_softc(devclass_t dc, int unit)
1378 dev = devclass_get_device(dc, unit);
1382 return (device_get_softc(dev));
1386 * @brief Get a list of devices in the devclass
1388 * An array containing a list of all the devices in the given devclass
1389 * is allocated and returned in @p *devlistp. The number of devices
1390 * in the array is returned in @p *devcountp. The caller should free
1391 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1393 * @param dc the devclass to examine
1394 * @param devlistp points at location for array pointer return
1396 * @param devcountp points at location for array size return value
1399 * @retval ENOMEM the array allocation failed
1402 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1407 count = devclass_get_count(dc);
1408 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1413 for (i = 0; i < dc->maxunit; i++) {
1414 if (dc->devices[i]) {
1415 list[count] = dc->devices[i];
1427 * @brief Get a list of drivers in the devclass
1429 * An array containing a list of pointers to all the drivers in the
1430 * given devclass is allocated and returned in @p *listp. The number
1431 * of drivers in the array is returned in @p *countp. The caller should
1432 * free the array using @c free(p, M_TEMP).
1434 * @param dc the devclass to examine
1435 * @param listp gives location for array pointer return value
1436 * @param countp gives location for number of array elements
1440 * @retval ENOMEM the array allocation failed
1443 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1450 TAILQ_FOREACH(dl, &dc->drivers, link)
1452 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1457 TAILQ_FOREACH(dl, &dc->drivers, link) {
1458 list[count] = dl->driver;
1468 * @brief Get the number of devices in a devclass
1470 * @param dc the devclass to examine
1473 devclass_get_count(devclass_t dc)
1478 for (i = 0; i < dc->maxunit; i++)
1485 * @brief Get the maximum unit number used in a devclass
1487 * Note that this is one greater than the highest currently-allocated
1488 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1489 * that not even the devclass has been allocated yet.
1491 * @param dc the devclass to examine
1494 devclass_get_maxunit(devclass_t dc)
1498 return (dc->maxunit);
1502 * @brief Find a free unit number in a devclass
1504 * This function searches for the first unused unit number greater
1505 * that or equal to @p unit.
1507 * @param dc the devclass to examine
1508 * @param unit the first unit number to check
1511 devclass_find_free_unit(devclass_t dc, int unit)
1515 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1521 * @brief Set the parent of a devclass
1523 * The parent class is normally initialised automatically by
1526 * @param dc the devclass to edit
1527 * @param pdc the new parent devclass
1530 devclass_set_parent(devclass_t dc, devclass_t pdc)
1536 * @brief Get the parent of a devclass
1538 * @param dc the devclass to examine
1541 devclass_get_parent(devclass_t dc)
1543 return (dc->parent);
1546 struct sysctl_ctx_list *
1547 devclass_get_sysctl_ctx(devclass_t dc)
1549 return (&dc->sysctl_ctx);
1553 devclass_get_sysctl_tree(devclass_t dc)
1555 return (dc->sysctl_tree);
1560 * @brief Allocate a unit number
1562 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1563 * will do). The allocated unit number is returned in @p *unitp.
1565 * @param dc the devclass to allocate from
1566 * @param unitp points at the location for the allocated unit
1570 * @retval EEXIST the requested unit number is already allocated
1571 * @retval ENOMEM memory allocation failure
1574 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1579 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1581 /* Ask the parent bus if it wants to wire this device. */
1583 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1586 /* If we were given a wired unit number, check for existing device */
1589 if (unit >= 0 && unit < dc->maxunit &&
1590 dc->devices[unit] != NULL) {
1592 printf("%s: %s%d already exists; skipping it\n",
1593 dc->name, dc->name, *unitp);
1597 /* Unwired device, find the next available slot for it */
1599 for (unit = 0;; unit++) {
1600 /* If there is an "at" hint for a unit then skip it. */
1601 if (resource_string_value(dc->name, unit, "at", &s) ==
1605 /* If this device slot is already in use, skip it. */
1606 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1614 * We've selected a unit beyond the length of the table, so let's
1615 * extend the table to make room for all units up to and including
1618 if (unit >= dc->maxunit) {
1619 device_t *newlist, *oldlist;
1622 oldlist = dc->devices;
1623 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1624 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1627 if (oldlist != NULL)
1628 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1629 bzero(newlist + dc->maxunit,
1630 sizeof(device_t) * (newsize - dc->maxunit));
1631 dc->devices = newlist;
1632 dc->maxunit = newsize;
1633 if (oldlist != NULL)
1634 free(oldlist, M_BUS);
1636 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1644 * @brief Add a device to a devclass
1646 * A unit number is allocated for the device (using the device's
1647 * preferred unit number if any) and the device is registered in the
1648 * devclass. This allows the device to be looked up by its unit
1649 * number, e.g. by decoding a dev_t minor number.
1651 * @param dc the devclass to add to
1652 * @param dev the device to add
1655 * @retval EEXIST the requested unit number is already allocated
1656 * @retval ENOMEM memory allocation failure
1659 devclass_add_device(devclass_t dc, device_t dev)
1663 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1665 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1668 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1672 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1673 free(dev->nameunit, M_BUS);
1674 dev->nameunit = NULL;
1677 dc->devices[dev->unit] = dev;
1679 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1686 * @brief Delete a device from a devclass
1688 * The device is removed from the devclass's device list and its unit
1691 * @param dc the devclass to delete from
1692 * @param dev the device to delete
1697 devclass_delete_device(devclass_t dc, device_t dev)
1702 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1704 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1705 panic("devclass_delete_device: inconsistent device class");
1706 dc->devices[dev->unit] = NULL;
1707 if (dev->flags & DF_WILDCARD)
1709 dev->devclass = NULL;
1710 free(dev->nameunit, M_BUS);
1711 dev->nameunit = NULL;
1718 * @brief Make a new device and add it as a child of @p parent
1720 * @param parent the parent of the new device
1721 * @param name the devclass name of the new device or @c NULL
1722 * to leave the devclass unspecified
1723 * @parem unit the unit number of the new device of @c -1 to
1724 * leave the unit number unspecified
1726 * @returns the new device
1729 make_device(device_t parent, const char *name, int unit)
1734 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1737 dc = devclass_find_internal(name, NULL, TRUE);
1739 printf("make_device: can't find device class %s\n",
1747 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1751 dev->parent = parent;
1752 TAILQ_INIT(&dev->children);
1753 kobj_init((kobj_t) dev, &null_class);
1755 dev->devclass = NULL;
1757 dev->nameunit = NULL;
1761 dev->flags = DF_ENABLED;
1764 dev->flags |= DF_WILDCARD;
1766 dev->flags |= DF_FIXEDCLASS;
1767 if (devclass_add_device(dc, dev)) {
1768 kobj_delete((kobj_t) dev, M_BUS);
1775 dev->state = DS_NOTPRESENT;
1777 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1778 bus_data_generation_update();
1785 * @brief Print a description of a device.
1788 device_print_child(device_t dev, device_t child)
1792 if (device_is_alive(child))
1793 retval += BUS_PRINT_CHILD(dev, child);
1795 retval += device_printf(child, " not found\n");
1801 * @brief Create a new device
1803 * This creates a new device and adds it as a child of an existing
1804 * parent device. The new device will be added after the last existing
1805 * child with order zero.
1807 * @param dev the device which will be the parent of the
1809 * @param name devclass name for new device or @c NULL if not
1811 * @param unit unit number for new device or @c -1 if not
1814 * @returns the new device
1817 device_add_child(device_t dev, const char *name, int unit)
1819 return (device_add_child_ordered(dev, 0, name, unit));
1823 * @brief Create a new device
1825 * This creates a new device and adds it as a child of an existing
1826 * parent device. The new device will be added after the last existing
1827 * child with the same order.
1829 * @param dev the device which will be the parent of the
1831 * @param order a value which is used to partially sort the
1832 * children of @p dev - devices created using
1833 * lower values of @p order appear first in @p
1834 * dev's list of children
1835 * @param name devclass name for new device or @c NULL if not
1837 * @param unit unit number for new device or @c -1 if not
1840 * @returns the new device
1843 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1848 PDEBUG(("%s at %s with order %u as unit %d",
1849 name, DEVICENAME(dev), order, unit));
1851 child = make_device(dev, name, unit);
1854 child->order = order;
1856 TAILQ_FOREACH(place, &dev->children, link) {
1857 if (place->order > order)
1863 * The device 'place' is the first device whose order is
1864 * greater than the new child.
1866 TAILQ_INSERT_BEFORE(place, child, link);
1869 * The new child's order is greater or equal to the order of
1870 * any existing device. Add the child to the tail of the list.
1872 TAILQ_INSERT_TAIL(&dev->children, child, link);
1875 bus_data_generation_update();
1880 * @brief Delete a device
1882 * This function deletes a device along with all of its children. If
1883 * the device currently has a driver attached to it, the device is
1884 * detached first using device_detach().
1886 * @param dev the parent device
1887 * @param child the device to delete
1890 * @retval non-zero a unit error code describing the error
1893 device_delete_child(device_t dev, device_t child)
1896 device_t grandchild;
1898 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1900 /* remove children first */
1901 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1902 error = device_delete_child(child, grandchild);
1907 if ((error = device_detach(child)) != 0)
1909 if (child->devclass)
1910 devclass_delete_device(child->devclass, child);
1911 TAILQ_REMOVE(&dev->children, child, link);
1912 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1913 kobj_delete((kobj_t) child, M_BUS);
1915 bus_data_generation_update();
1920 * @brief Find a device given a unit number
1922 * This is similar to devclass_get_devices() but only searches for
1923 * devices which have @p dev as a parent.
1925 * @param dev the parent device to search
1926 * @param unit the unit number to search for. If the unit is -1,
1927 * return the first child of @p dev which has name
1928 * @p classname (that is, the one with the lowest unit.)
1930 * @returns the device with the given unit number or @c
1931 * NULL if there is no such device
1934 device_find_child(device_t dev, const char *classname, int unit)
1939 dc = devclass_find(classname);
1944 child = devclass_get_device(dc, unit);
1945 if (child && child->parent == dev)
1948 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1949 child = devclass_get_device(dc, unit);
1950 if (child && child->parent == dev)
1961 first_matching_driver(devclass_t dc, device_t dev)
1964 return (devclass_find_driver_internal(dc, dev->devclass->name));
1965 return (TAILQ_FIRST(&dc->drivers));
1972 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1974 if (dev->devclass) {
1976 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1977 if (!strcmp(dev->devclass->name, dl->driver->name))
1981 return (TAILQ_NEXT(last, link));
1988 device_probe_child(device_t dev, device_t child)
1991 driverlink_t best = NULL;
1993 int result, pri = 0;
1994 int hasclass = (child->devclass != NULL);
2000 panic("device_probe_child: parent device has no devclass");
2003 * If the state is already probed, then return. However, don't
2004 * return if we can rebid this object.
2006 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
2009 for (; dc; dc = dc->parent) {
2010 for (dl = first_matching_driver(dc, child);
2012 dl = next_matching_driver(dc, child, dl)) {
2014 /* If this driver's pass is too high, then ignore it. */
2015 if (dl->pass > bus_current_pass)
2018 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
2019 device_set_driver(child, dl->driver);
2021 if (device_set_devclass(child, dl->driver->name)) {
2022 printf("driver bug: Unable to set devclass (devname: %s)\n",
2023 (child ? device_get_name(child) :
2025 device_set_driver(child, NULL);
2030 /* Fetch any flags for the device before probing. */
2031 resource_int_value(dl->driver->name, child->unit,
2032 "flags", &child->devflags);
2034 result = DEVICE_PROBE(child);
2036 /* Reset flags and devclass before the next probe. */
2037 child->devflags = 0;
2039 device_set_devclass(child, NULL);
2042 * If the driver returns SUCCESS, there can be
2043 * no higher match for this device.
2052 * The driver returned an error so it
2053 * certainly doesn't match.
2056 device_set_driver(child, NULL);
2061 * A priority lower than SUCCESS, remember the
2062 * best matching driver. Initialise the value
2063 * of pri for the first match.
2065 if (best == NULL || result > pri) {
2067 * Probes that return BUS_PROBE_NOWILDCARD
2068 * or lower only match when they are set
2069 * in stone by the parent bus.
2071 if (result <= BUS_PROBE_NOWILDCARD &&
2072 child->flags & DF_WILDCARD)
2080 * If we have an unambiguous match in this devclass,
2081 * don't look in the parent.
2083 if (best && pri == 0)
2088 * If we found a driver, change state and initialise the devclass.
2090 /* XXX What happens if we rebid and got no best? */
2093 * If this device was atached, and we were asked to
2094 * rescan, and it is a different driver, then we have
2095 * to detach the old driver and reattach this new one.
2096 * Note, we don't have to check for DF_REBID here
2097 * because if the state is > DS_ALIVE, we know it must
2100 * This assumes that all DF_REBID drivers can have
2101 * their probe routine called at any time and that
2102 * they are idempotent as well as completely benign in
2103 * normal operations.
2105 * We also have to make sure that the detach
2106 * succeeded, otherwise we fail the operation (or
2107 * maybe it should just fail silently? I'm torn).
2109 if (child->state > DS_ALIVE && best->driver != child->driver)
2110 if ((result = device_detach(dev)) != 0)
2113 /* Set the winning driver, devclass, and flags. */
2114 if (!child->devclass) {
2115 result = device_set_devclass(child, best->driver->name);
2119 device_set_driver(child, best->driver);
2120 resource_int_value(best->driver->name, child->unit,
2121 "flags", &child->devflags);
2125 * A bit bogus. Call the probe method again to make
2126 * sure that we have the right description.
2128 DEVICE_PROBE(child);
2130 child->flags |= DF_REBID;
2133 child->flags &= ~DF_REBID;
2134 child->state = DS_ALIVE;
2136 bus_data_generation_update();
2144 * @brief Return the parent of a device
2147 device_get_parent(device_t dev)
2149 return (dev->parent);
2153 * @brief Get a list of children of a device
2155 * An array containing a list of all the children of the given device
2156 * is allocated and returned in @p *devlistp. The number of devices
2157 * in the array is returned in @p *devcountp. The caller should free
2158 * the array using @c free(p, M_TEMP).
2160 * @param dev the device to examine
2161 * @param devlistp points at location for array pointer return
2163 * @param devcountp points at location for array size return value
2166 * @retval ENOMEM the array allocation failed
2169 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2176 TAILQ_FOREACH(child, &dev->children, link) {
2180 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2185 TAILQ_FOREACH(child, &dev->children, link) {
2186 list[count] = child;
2197 * @brief Return the current driver for the device or @c NULL if there
2198 * is no driver currently attached
2201 device_get_driver(device_t dev)
2203 return (dev->driver);
2207 * @brief Return the current devclass for the device or @c NULL if
2211 device_get_devclass(device_t dev)
2213 return (dev->devclass);
2217 * @brief Return the name of the device's devclass or @c NULL if there
2221 device_get_name(device_t dev)
2223 if (dev != NULL && dev->devclass)
2224 return (devclass_get_name(dev->devclass));
2229 * @brief Return a string containing the device's devclass name
2230 * followed by an ascii representation of the device's unit number
2234 device_get_nameunit(device_t dev)
2236 return (dev->nameunit);
2240 * @brief Return the device's unit number.
2243 device_get_unit(device_t dev)
2249 * @brief Return the device's description string
2252 device_get_desc(device_t dev)
2258 * @brief Return the device's flags
2261 device_get_flags(device_t dev)
2263 return (dev->devflags);
2266 struct sysctl_ctx_list *
2267 device_get_sysctl_ctx(device_t dev)
2269 return (&dev->sysctl_ctx);
2273 device_get_sysctl_tree(device_t dev)
2275 return (dev->sysctl_tree);
2279 * @brief Print the name of the device followed by a colon and a space
2281 * @returns the number of characters printed
2284 device_print_prettyname(device_t dev)
2286 const char *name = device_get_name(dev);
2289 return (printf("unknown: "));
2290 return (printf("%s%d: ", name, device_get_unit(dev)));
2294 * @brief Print the name of the device followed by a colon, a space
2295 * and the result of calling vprintf() with the value of @p fmt and
2296 * the following arguments.
2298 * @returns the number of characters printed
2301 device_printf(device_t dev, const char * fmt, ...)
2306 retval = device_print_prettyname(dev);
2308 retval += vprintf(fmt, ap);
2317 device_set_desc_internal(device_t dev, const char* desc, int copy)
2319 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2320 free(dev->desc, M_BUS);
2321 dev->flags &= ~DF_DESCMALLOCED;
2326 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2328 strcpy(dev->desc, desc);
2329 dev->flags |= DF_DESCMALLOCED;
2332 /* Avoid a -Wcast-qual warning */
2333 dev->desc = (char *)(uintptr_t) desc;
2336 bus_data_generation_update();
2340 * @brief Set the device's description
2342 * The value of @c desc should be a string constant that will not
2343 * change (at least until the description is changed in a subsequent
2344 * call to device_set_desc() or device_set_desc_copy()).
2347 device_set_desc(device_t dev, const char* desc)
2349 device_set_desc_internal(dev, desc, FALSE);
2353 * @brief Set the device's description
2355 * The string pointed to by @c desc is copied. Use this function if
2356 * the device description is generated, (e.g. with sprintf()).
2359 device_set_desc_copy(device_t dev, const char* desc)
2361 device_set_desc_internal(dev, desc, TRUE);
2365 * @brief Set the device's flags
2368 device_set_flags(device_t dev, u_int32_t flags)
2370 dev->devflags = flags;
2374 * @brief Return the device's softc field
2376 * The softc is allocated and zeroed when a driver is attached, based
2377 * on the size field of the driver.
2380 device_get_softc(device_t dev)
2382 return (dev->softc);
2386 * @brief Set the device's softc field
2388 * Most drivers do not need to use this since the softc is allocated
2389 * automatically when the driver is attached.
2392 device_set_softc(device_t dev, void *softc)
2394 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2395 free(dev->softc, M_BUS_SC);
2398 dev->flags |= DF_EXTERNALSOFTC;
2400 dev->flags &= ~DF_EXTERNALSOFTC;
2404 * @brief Get the device's ivars field
2406 * The ivars field is used by the parent device to store per-device
2407 * state (e.g. the physical location of the device or a list of
2411 device_get_ivars(device_t dev)
2414 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2415 return (dev->ivars);
2419 * @brief Set the device's ivars field
2422 device_set_ivars(device_t dev, void * ivars)
2425 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2430 * @brief Return the device's state
2433 device_get_state(device_t dev)
2435 return (dev->state);
2439 * @brief Set the DF_ENABLED flag for the device
2442 device_enable(device_t dev)
2444 dev->flags |= DF_ENABLED;
2448 * @brief Clear the DF_ENABLED flag for the device
2451 device_disable(device_t dev)
2453 dev->flags &= ~DF_ENABLED;
2457 * @brief Increment the busy counter for the device
2460 device_busy(device_t dev)
2462 if (dev->state < DS_ATTACHED)
2463 panic("device_busy: called for unattached device");
2464 if (dev->busy == 0 && dev->parent)
2465 device_busy(dev->parent);
2467 dev->state = DS_BUSY;
2471 * @brief Decrement the busy counter for the device
2474 device_unbusy(device_t dev)
2476 if (dev->state != DS_BUSY)
2477 panic("device_unbusy: called for non-busy device %s",
2478 device_get_nameunit(dev));
2480 if (dev->busy == 0) {
2482 device_unbusy(dev->parent);
2483 dev->state = DS_ATTACHED;
2488 * @brief Set the DF_QUIET flag for the device
2491 device_quiet(device_t dev)
2493 dev->flags |= DF_QUIET;
2497 * @brief Clear the DF_QUIET flag for the device
2500 device_verbose(device_t dev)
2502 dev->flags &= ~DF_QUIET;
2506 * @brief Return non-zero if the DF_QUIET flag is set on the device
2509 device_is_quiet(device_t dev)
2511 return ((dev->flags & DF_QUIET) != 0);
2515 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2518 device_is_enabled(device_t dev)
2520 return ((dev->flags & DF_ENABLED) != 0);
2524 * @brief Return non-zero if the device was successfully probed
2527 device_is_alive(device_t dev)
2529 return (dev->state >= DS_ALIVE);
2533 * @brief Return non-zero if the device currently has a driver
2537 device_is_attached(device_t dev)
2539 return (dev->state >= DS_ATTACHED);
2543 * @brief Set the devclass of a device
2544 * @see devclass_add_device().
2547 device_set_devclass(device_t dev, const char *classname)
2554 devclass_delete_device(dev->devclass, dev);
2558 if (dev->devclass) {
2559 printf("device_set_devclass: device class already set\n");
2563 dc = devclass_find_internal(classname, NULL, TRUE);
2567 error = devclass_add_device(dc, dev);
2569 bus_data_generation_update();
2574 * @brief Set the driver of a device
2577 * @retval EBUSY the device already has a driver attached
2578 * @retval ENOMEM a memory allocation failure occurred
2581 device_set_driver(device_t dev, driver_t *driver)
2583 if (dev->state >= DS_ATTACHED)
2586 if (dev->driver == driver)
2589 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2590 free(dev->softc, M_BUS_SC);
2593 kobj_delete((kobj_t) dev, NULL);
2594 dev->driver = driver;
2596 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2597 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2598 dev->softc = malloc(driver->size, M_BUS_SC,
2601 kobj_delete((kobj_t) dev, NULL);
2602 kobj_init((kobj_t) dev, &null_class);
2608 kobj_init((kobj_t) dev, &null_class);
2611 bus_data_generation_update();
2616 * @brief Probe a device, and return this status.
2618 * This function is the core of the device autoconfiguration
2619 * system. Its purpose is to select a suitable driver for a device and
2620 * then call that driver to initialise the hardware appropriately. The
2621 * driver is selected by calling the DEVICE_PROBE() method of a set of
2622 * candidate drivers and then choosing the driver which returned the
2623 * best value. This driver is then attached to the device using
2626 * The set of suitable drivers is taken from the list of drivers in
2627 * the parent device's devclass. If the device was originally created
2628 * with a specific class name (see device_add_child()), only drivers
2629 * with that name are probed, otherwise all drivers in the devclass
2630 * are probed. If no drivers return successful probe values in the
2631 * parent devclass, the search continues in the parent of that
2632 * devclass (see devclass_get_parent()) if any.
2634 * @param dev the device to initialise
2637 * @retval ENXIO no driver was found
2638 * @retval ENOMEM memory allocation failure
2639 * @retval non-zero some other unix error code
2640 * @retval -1 Device already attached
2643 device_probe(device_t dev)
2649 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2652 if (!(dev->flags & DF_ENABLED)) {
2653 if (bootverbose && device_get_name(dev) != NULL) {
2654 device_print_prettyname(dev);
2655 printf("not probed (disabled)\n");
2659 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2660 if (bus_current_pass == BUS_PASS_DEFAULT &&
2661 !(dev->flags & DF_DONENOMATCH)) {
2662 BUS_PROBE_NOMATCH(dev->parent, dev);
2664 dev->flags |= DF_DONENOMATCH;
2672 * @brief Probe a device and attach a driver if possible
2674 * calls device_probe() and attaches if that was successful.
2677 device_probe_and_attach(device_t dev)
2683 error = device_probe(dev);
2686 else if (error != 0)
2688 return (device_attach(dev));
2692 * @brief Attach a device driver to a device
2694 * This function is a wrapper around the DEVICE_ATTACH() driver
2695 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2696 * device's sysctl tree, optionally prints a description of the device
2697 * and queues a notification event for user-based device management
2700 * Normally this function is only called internally from
2701 * device_probe_and_attach().
2703 * @param dev the device to initialise
2706 * @retval ENXIO no driver was found
2707 * @retval ENOMEM memory allocation failure
2708 * @retval non-zero some other unix error code
2711 device_attach(device_t dev)
2715 device_sysctl_init(dev);
2716 if (!device_is_quiet(dev))
2717 device_print_child(dev->parent, dev);
2718 if ((error = DEVICE_ATTACH(dev)) != 0) {
2719 printf("device_attach: %s%d attach returned %d\n",
2720 dev->driver->name, dev->unit, error);
2721 /* Unset the class; set in device_probe_child */
2722 if (dev->devclass == NULL)
2723 device_set_devclass(dev, NULL);
2724 device_set_driver(dev, NULL);
2725 device_sysctl_fini(dev);
2726 dev->state = DS_NOTPRESENT;
2729 device_sysctl_update(dev);
2730 dev->state = DS_ATTACHED;
2731 dev->flags &= ~DF_DONENOMATCH;
2737 * @brief Detach a driver from a device
2739 * This function is a wrapper around the DEVICE_DETACH() driver
2740 * method. If the call to DEVICE_DETACH() succeeds, it calls
2741 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2742 * notification event for user-based device management services and
2743 * cleans up the device's sysctl tree.
2745 * @param dev the device to un-initialise
2748 * @retval ENXIO no driver was found
2749 * @retval ENOMEM memory allocation failure
2750 * @retval non-zero some other unix error code
2753 device_detach(device_t dev)
2759 PDEBUG(("%s", DEVICENAME(dev)));
2760 if (dev->state == DS_BUSY)
2762 if (dev->state != DS_ATTACHED)
2765 if ((error = DEVICE_DETACH(dev)) != 0)
2768 if (!device_is_quiet(dev))
2769 device_printf(dev, "detached\n");
2771 BUS_CHILD_DETACHED(dev->parent, dev);
2773 if (!(dev->flags & DF_FIXEDCLASS))
2774 devclass_delete_device(dev->devclass, dev);
2776 dev->state = DS_NOTPRESENT;
2777 device_set_driver(dev, NULL);
2778 device_set_desc(dev, NULL);
2779 device_sysctl_fini(dev);
2785 * @brief Tells a driver to quiesce itself.
2787 * This function is a wrapper around the DEVICE_QUIESCE() driver
2788 * method. If the call to DEVICE_QUIESCE() succeeds.
2790 * @param dev the device to quiesce
2793 * @retval ENXIO no driver was found
2794 * @retval ENOMEM memory allocation failure
2795 * @retval non-zero some other unix error code
2798 device_quiesce(device_t dev)
2801 PDEBUG(("%s", DEVICENAME(dev)));
2802 if (dev->state == DS_BUSY)
2804 if (dev->state != DS_ATTACHED)
2807 return (DEVICE_QUIESCE(dev));
2811 * @brief Notify a device of system shutdown
2813 * This function calls the DEVICE_SHUTDOWN() driver method if the
2814 * device currently has an attached driver.
2816 * @returns the value returned by DEVICE_SHUTDOWN()
2819 device_shutdown(device_t dev)
2821 if (dev->state < DS_ATTACHED)
2823 return (DEVICE_SHUTDOWN(dev));
2827 * @brief Set the unit number of a device
2829 * This function can be used to override the unit number used for a
2830 * device (e.g. to wire a device to a pre-configured unit number).
2833 device_set_unit(device_t dev, int unit)
2838 dc = device_get_devclass(dev);
2839 if (unit < dc->maxunit && dc->devices[unit])
2841 err = devclass_delete_device(dc, dev);
2845 err = devclass_add_device(dc, dev);
2849 bus_data_generation_update();
2853 /*======================================*/
2855 * Some useful method implementations to make life easier for bus drivers.
2859 * @brief Initialise a resource list.
2861 * @param rl the resource list to initialise
2864 resource_list_init(struct resource_list *rl)
2870 * @brief Reclaim memory used by a resource list.
2872 * This function frees the memory for all resource entries on the list
2875 * @param rl the resource list to free
2878 resource_list_free(struct resource_list *rl)
2880 struct resource_list_entry *rle;
2882 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2884 panic("resource_list_free: resource entry is busy");
2885 STAILQ_REMOVE_HEAD(rl, link);
2891 * @brief Add a resource entry.
2893 * This function adds a resource entry using the given @p type, @p
2894 * start, @p end and @p count values. A rid value is chosen by
2895 * searching sequentially for the first unused rid starting at zero.
2897 * @param rl the resource list to edit
2898 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2899 * @param start the start address of the resource
2900 * @param end the end address of the resource
2901 * @param count XXX end-start+1
2904 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2905 u_long end, u_long count)
2910 while (resource_list_find(rl, type, rid) != NULL)
2912 resource_list_add(rl, type, rid, start, end, count);
2917 * @brief Add or modify a resource entry.
2919 * If an existing entry exists with the same type and rid, it will be
2920 * modified using the given values of @p start, @p end and @p
2921 * count. If no entry exists, a new one will be created using the
2922 * given values. The resource list entry that matches is then returned.
2924 * @param rl the resource list to edit
2925 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2926 * @param rid the resource identifier
2927 * @param start the start address of the resource
2928 * @param end the end address of the resource
2929 * @param count XXX end-start+1
2931 struct resource_list_entry *
2932 resource_list_add(struct resource_list *rl, int type, int rid,
2933 u_long start, u_long end, u_long count)
2935 struct resource_list_entry *rle;
2937 rle = resource_list_find(rl, type, rid);
2939 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2942 panic("resource_list_add: can't record entry");
2943 STAILQ_INSERT_TAIL(rl, rle, link);
2950 panic("resource_list_add: resource entry is busy");
2959 * @brief Find a resource entry by type and rid.
2961 * @param rl the resource list to search
2962 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2963 * @param rid the resource identifier
2965 * @returns the resource entry pointer or NULL if there is no such
2968 struct resource_list_entry *
2969 resource_list_find(struct resource_list *rl, int type, int rid)
2971 struct resource_list_entry *rle;
2973 STAILQ_FOREACH(rle, rl, link) {
2974 if (rle->type == type && rle->rid == rid)
2981 * @brief Delete a resource entry.
2983 * @param rl the resource list to edit
2984 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2985 * @param rid the resource identifier
2988 resource_list_delete(struct resource_list *rl, int type, int rid)
2990 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2993 if (rle->res != NULL)
2994 panic("resource_list_delete: resource has not been released");
2995 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3001 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3003 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3004 * and passing the allocation up to the parent of @p bus. This assumes
3005 * that the first entry of @c device_get_ivars(child) is a struct
3006 * resource_list. This also handles 'passthrough' allocations where a
3007 * child is a remote descendant of bus by passing the allocation up to
3008 * the parent of bus.
3010 * Typically, a bus driver would store a list of child resources
3011 * somewhere in the child device's ivars (see device_get_ivars()) and
3012 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3013 * then call resource_list_alloc() to perform the allocation.
3015 * @param rl the resource list to allocate from
3016 * @param bus the parent device of @p child
3017 * @param child the device which is requesting an allocation
3018 * @param type the type of resource to allocate
3019 * @param rid a pointer to the resource identifier
3020 * @param start hint at the start of the resource range - pass
3021 * @c 0UL for any start address
3022 * @param end hint at the end of the resource range - pass
3023 * @c ~0UL for any end address
3024 * @param count hint at the size of range required - pass @c 1
3026 * @param flags any extra flags to control the resource
3027 * allocation - see @c RF_XXX flags in
3028 * <sys/rman.h> for details
3030 * @returns the resource which was allocated or @c NULL if no
3031 * resource could be allocated
3034 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3035 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3037 struct resource_list_entry *rle = NULL;
3038 int passthrough = (device_get_parent(child) != bus);
3039 int isdefault = (start == 0UL && end == ~0UL);
3042 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3043 type, rid, start, end, count, flags));
3046 rle = resource_list_find(rl, type, *rid);
3049 return (NULL); /* no resource of that type/rid */
3052 panic("resource_list_alloc: resource entry is busy");
3056 count = ulmax(count, rle->count);
3057 end = ulmax(rle->end, start + count - 1);
3060 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3061 type, rid, start, end, count, flags);
3064 * Record the new range.
3067 rle->start = rman_get_start(rle->res);
3068 rle->end = rman_get_end(rle->res);
3076 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3078 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3079 * used with resource_list_alloc().
3081 * @param rl the resource list which was allocated from
3082 * @param bus the parent device of @p child
3083 * @param child the device which is requesting a release
3084 * @param type the type of resource to allocate
3085 * @param rid the resource identifier
3086 * @param res the resource to release
3089 * @retval non-zero a standard unix error code indicating what
3090 * error condition prevented the operation
3093 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3094 int type, int rid, struct resource *res)
3096 struct resource_list_entry *rle = NULL;
3097 int passthrough = (device_get_parent(child) != bus);
3101 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3105 rle = resource_list_find(rl, type, rid);
3108 panic("resource_list_release: can't find resource");
3110 panic("resource_list_release: resource entry is not busy");
3112 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3122 * @brief Print a description of resources in a resource list
3124 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3125 * The name is printed if at least one resource of the given type is available.
3126 * The format is used to print resource start and end.
3128 * @param rl the resource list to print
3129 * @param name the name of @p type, e.g. @c "memory"
3130 * @param type type type of resource entry to print
3131 * @param format printf(9) format string to print resource
3132 * start and end values
3134 * @returns the number of characters printed
3137 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3140 struct resource_list_entry *rle;
3141 int printed, retval;
3145 /* Yes, this is kinda cheating */
3146 STAILQ_FOREACH(rle, rl, link) {
3147 if (rle->type == type) {
3149 retval += printf(" %s ", name);
3151 retval += printf(",");
3153 retval += printf(format, rle->start);
3154 if (rle->count > 1) {
3155 retval += printf("-");
3156 retval += printf(format, rle->start +
3165 * @brief Releases all the resources in a list.
3167 * @param rl The resource list to purge.
3172 resource_list_purge(struct resource_list *rl)
3174 struct resource_list_entry *rle;
3176 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3178 bus_release_resource(rman_get_device(rle->res),
3179 rle->type, rle->rid, rle->res);
3180 STAILQ_REMOVE_HEAD(rl, link);
3186 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3189 return (device_add_child_ordered(dev, order, name, unit));
3193 * @brief Helper function for implementing DEVICE_PROBE()
3195 * This function can be used to help implement the DEVICE_PROBE() for
3196 * a bus (i.e. a device which has other devices attached to it). It
3197 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3201 bus_generic_probe(device_t dev)
3203 devclass_t dc = dev->devclass;
3206 TAILQ_FOREACH(dl, &dc->drivers, link) {
3208 * If this driver's pass is too high, then ignore it.
3209 * For most drivers in the default pass, this will
3210 * never be true. For early-pass drivers they will
3211 * only call the identify routines of eligible drivers
3212 * when this routine is called. Drivers for later
3213 * passes should have their identify routines called
3214 * on early-pass busses during BUS_NEW_PASS().
3216 if (dl->pass > bus_current_pass)
3218 DEVICE_IDENTIFY(dl->driver, dev);
3225 * @brief Helper function for implementing DEVICE_ATTACH()
3227 * This function can be used to help implement the DEVICE_ATTACH() for
3228 * a bus. It calls device_probe_and_attach() for each of the device's
3232 bus_generic_attach(device_t dev)
3236 TAILQ_FOREACH(child, &dev->children, link) {
3237 device_probe_and_attach(child);
3244 * @brief Helper function for implementing DEVICE_DETACH()
3246 * This function can be used to help implement the DEVICE_DETACH() for
3247 * a bus. It calls device_detach() for each of the device's
3251 bus_generic_detach(device_t dev)
3256 if (dev->state != DS_ATTACHED)
3259 TAILQ_FOREACH(child, &dev->children, link) {
3260 if ((error = device_detach(child)) != 0)
3268 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3270 * This function can be used to help implement the DEVICE_SHUTDOWN()
3271 * for a bus. It calls device_shutdown() for each of the device's
3275 bus_generic_shutdown(device_t dev)
3279 TAILQ_FOREACH(child, &dev->children, link) {
3280 device_shutdown(child);
3287 * @brief Helper function for implementing DEVICE_SUSPEND()
3289 * This function can be used to help implement the DEVICE_SUSPEND()
3290 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3291 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3292 * operation is aborted and any devices which were suspended are
3293 * resumed immediately by calling their DEVICE_RESUME() methods.
3296 bus_generic_suspend(device_t dev)
3299 device_t child, child2;
3301 TAILQ_FOREACH(child, &dev->children, link) {
3302 error = DEVICE_SUSPEND(child);
3304 for (child2 = TAILQ_FIRST(&dev->children);
3305 child2 && child2 != child;
3306 child2 = TAILQ_NEXT(child2, link))
3307 DEVICE_RESUME(child2);
3315 * @brief Helper function for implementing DEVICE_RESUME()
3317 * This function can be used to help implement the DEVICE_RESUME() for
3318 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3321 bus_generic_resume(device_t dev)
3325 TAILQ_FOREACH(child, &dev->children, link) {
3326 DEVICE_RESUME(child);
3327 /* if resume fails, there's nothing we can usefully do... */
3333 * @brief Helper function for implementing BUS_PRINT_CHILD().
3335 * This function prints the first part of the ascii representation of
3336 * @p child, including its name, unit and description (if any - see
3337 * device_set_desc()).
3339 * @returns the number of characters printed
3342 bus_print_child_header(device_t dev, device_t child)
3346 if (device_get_desc(child)) {
3347 retval += device_printf(child, "<%s>", device_get_desc(child));
3349 retval += printf("%s", device_get_nameunit(child));
3356 * @brief Helper function for implementing BUS_PRINT_CHILD().
3358 * This function prints the last part of the ascii representation of
3359 * @p child, which consists of the string @c " on " followed by the
3360 * name and unit of the @p dev.
3362 * @returns the number of characters printed
3365 bus_print_child_footer(device_t dev, device_t child)
3367 return (printf(" on %s\n", device_get_nameunit(dev)));
3371 * @brief Helper function for implementing BUS_PRINT_CHILD().
3373 * This function simply calls bus_print_child_header() followed by
3374 * bus_print_child_footer().
3376 * @returns the number of characters printed
3379 bus_generic_print_child(device_t dev, device_t child)
3383 retval += bus_print_child_header(dev, child);
3384 retval += bus_print_child_footer(dev, child);
3390 * @brief Stub function for implementing BUS_READ_IVAR().
3395 bus_generic_read_ivar(device_t dev, device_t child, int index,
3402 * @brief Stub function for implementing BUS_WRITE_IVAR().
3407 bus_generic_write_ivar(device_t dev, device_t child, int index,
3414 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3418 struct resource_list *
3419 bus_generic_get_resource_list(device_t dev, device_t child)
3425 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3427 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3428 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3429 * and then calls device_probe_and_attach() for each unattached child.
3432 bus_generic_driver_added(device_t dev, driver_t *driver)
3436 DEVICE_IDENTIFY(driver, dev);
3437 TAILQ_FOREACH(child, &dev->children, link) {
3438 if (child->state == DS_NOTPRESENT ||
3439 (child->flags & DF_REBID))
3440 device_probe_and_attach(child);
3445 * @brief Helper function for implementing BUS_NEW_PASS().
3447 * This implementing of BUS_NEW_PASS() first calls the identify
3448 * routines for any drivers that probe at the current pass. Then it
3449 * walks the list of devices for this bus. If a device is already
3450 * attached, then it calls BUS_NEW_PASS() on that device. If the
3451 * device is not already attached, it attempts to attach a driver to
3455 bus_generic_new_pass(device_t dev)
3462 TAILQ_FOREACH(dl, &dc->drivers, link) {
3463 if (dl->pass == bus_current_pass)
3464 DEVICE_IDENTIFY(dl->driver, dev);
3466 TAILQ_FOREACH(child, &dev->children, link) {
3467 if (child->state >= DS_ATTACHED)
3468 BUS_NEW_PASS(child);
3469 else if (child->state == DS_NOTPRESENT)
3470 device_probe_and_attach(child);
3475 * @brief Helper function for implementing BUS_SETUP_INTR().
3477 * This simple implementation of BUS_SETUP_INTR() simply calls the
3478 * BUS_SETUP_INTR() method of the parent of @p dev.
3481 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3482 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3485 /* Propagate up the bus hierarchy until someone handles it. */
3487 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3488 filter, intr, arg, cookiep));
3493 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3495 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3496 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3499 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3502 /* Propagate up the bus hierarchy until someone handles it. */
3504 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3509 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3511 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3512 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3515 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3516 struct resource *r, u_long start, u_long end)
3518 /* Propagate up the bus hierarchy until someone handles it. */
3520 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3526 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3528 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3529 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3532 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3533 u_long start, u_long end, u_long count, u_int flags)
3535 /* Propagate up the bus hierarchy until someone handles it. */
3537 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3538 start, end, count, flags));
3543 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3545 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3546 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3549 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3552 /* Propagate up the bus hierarchy until someone handles it. */
3554 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3560 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3562 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3563 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3566 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3569 /* Propagate up the bus hierarchy until someone handles it. */
3571 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3577 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3579 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3580 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3583 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3584 int rid, struct resource *r)
3586 /* Propagate up the bus hierarchy until someone handles it. */
3588 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3594 * @brief Helper function for implementing BUS_BIND_INTR().
3596 * This simple implementation of BUS_BIND_INTR() simply calls the
3597 * BUS_BIND_INTR() method of the parent of @p dev.
3600 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3604 /* Propagate up the bus hierarchy until someone handles it. */
3606 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3611 * @brief Helper function for implementing BUS_CONFIG_INTR().
3613 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3614 * BUS_CONFIG_INTR() method of the parent of @p dev.
3617 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3618 enum intr_polarity pol)
3621 /* Propagate up the bus hierarchy until someone handles it. */
3623 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3628 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3630 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3631 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3634 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3635 void *cookie, const char *descr)
3638 /* Propagate up the bus hierarchy until someone handles it. */
3640 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3646 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3648 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3649 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3652 bus_generic_get_dma_tag(device_t dev, device_t child)
3655 /* Propagate up the bus hierarchy until someone handles it. */
3656 if (dev->parent != NULL)
3657 return (BUS_GET_DMA_TAG(dev->parent, child));
3662 * @brief Helper function for implementing BUS_GET_RESOURCE().
3664 * This implementation of BUS_GET_RESOURCE() uses the
3665 * resource_list_find() function to do most of the work. It calls
3666 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3670 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3671 u_long *startp, u_long *countp)
3673 struct resource_list * rl = NULL;
3674 struct resource_list_entry * rle = NULL;
3676 rl = BUS_GET_RESOURCE_LIST(dev, child);
3680 rle = resource_list_find(rl, type, rid);
3685 *startp = rle->start;
3687 *countp = rle->count;
3693 * @brief Helper function for implementing BUS_SET_RESOURCE().
3695 * This implementation of BUS_SET_RESOURCE() uses the
3696 * resource_list_add() function to do most of the work. It calls
3697 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3701 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3702 u_long start, u_long count)
3704 struct resource_list * rl = NULL;
3706 rl = BUS_GET_RESOURCE_LIST(dev, child);
3710 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3716 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3718 * This implementation of BUS_DELETE_RESOURCE() uses the
3719 * resource_list_delete() function to do most of the work. It calls
3720 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3724 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3726 struct resource_list * rl = NULL;
3728 rl = BUS_GET_RESOURCE_LIST(dev, child);
3732 resource_list_delete(rl, type, rid);
3738 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3740 * This implementation of BUS_RELEASE_RESOURCE() uses the
3741 * resource_list_release() function to do most of the work. It calls
3742 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3745 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3746 int rid, struct resource *r)
3748 struct resource_list * rl = NULL;
3750 rl = BUS_GET_RESOURCE_LIST(dev, child);
3754 return (resource_list_release(rl, dev, child, type, rid, r));
3758 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3760 * This implementation of BUS_ALLOC_RESOURCE() uses the
3761 * resource_list_alloc() function to do most of the work. It calls
3762 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3765 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3766 int *rid, u_long start, u_long end, u_long count, u_int flags)
3768 struct resource_list * rl = NULL;
3770 rl = BUS_GET_RESOURCE_LIST(dev, child);
3774 return (resource_list_alloc(rl, dev, child, type, rid,
3775 start, end, count, flags));
3779 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3781 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3782 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3785 bus_generic_child_present(device_t dev, device_t child)
3787 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3791 * Some convenience functions to make it easier for drivers to use the
3792 * resource-management functions. All these really do is hide the
3793 * indirection through the parent's method table, making for slightly
3794 * less-wordy code. In the future, it might make sense for this code
3795 * to maintain some sort of a list of resources allocated by each device.
3799 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3800 struct resource **res)
3804 for (i = 0; rs[i].type != -1; i++)
3806 for (i = 0; rs[i].type != -1; i++) {
3807 res[i] = bus_alloc_resource_any(dev,
3808 rs[i].type, &rs[i].rid, rs[i].flags);
3809 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3810 bus_release_resources(dev, rs, res);
3818 bus_release_resources(device_t dev, const struct resource_spec *rs,
3819 struct resource **res)
3823 for (i = 0; rs[i].type != -1; i++)
3824 if (res[i] != NULL) {
3825 bus_release_resource(
3826 dev, rs[i].type, rs[i].rid, res[i]);
3832 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3834 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3838 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3839 u_long count, u_int flags)
3841 if (dev->parent == NULL)
3843 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3848 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
3850 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
3854 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
3857 if (dev->parent == NULL)
3859 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
3863 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3865 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3869 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3871 if (dev->parent == NULL)
3873 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3877 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3879 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3883 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3885 if (dev->parent == NULL)
3887 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3891 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3893 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3897 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3899 if (dev->parent == NULL)
3901 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3905 * @brief Wrapper function for BUS_SETUP_INTR().
3907 * This function simply calls the BUS_SETUP_INTR() method of the
3911 bus_setup_intr(device_t dev, struct resource *r, int flags,
3912 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3916 if (dev->parent == NULL)
3918 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
3922 if (handler != NULL && !(flags & INTR_MPSAFE))
3923 device_printf(dev, "[GIANT-LOCKED]\n");
3924 if (bootverbose && (flags & INTR_MPSAFE))
3925 device_printf(dev, "[MPSAFE]\n");
3926 if (filter != NULL) {
3927 if (handler == NULL)
3928 device_printf(dev, "[FILTER]\n");
3930 device_printf(dev, "[FILTER+ITHREAD]\n");
3932 device_printf(dev, "[ITHREAD]\n");
3937 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3939 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3943 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3945 if (dev->parent == NULL)
3947 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3951 * @brief Wrapper function for BUS_BIND_INTR().
3953 * This function simply calls the BUS_BIND_INTR() method of the
3957 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3959 if (dev->parent == NULL)
3961 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3965 * @brief Wrapper function for BUS_DESCRIBE_INTR().
3967 * This function first formats the requested description into a
3968 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
3969 * the parent of @p dev.
3972 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
3973 const char *fmt, ...)
3976 char descr[MAXCOMLEN + 1];
3978 if (dev->parent == NULL)
3981 vsnprintf(descr, sizeof(descr), fmt, ap);
3983 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
3987 * @brief Wrapper function for BUS_SET_RESOURCE().
3989 * This function simply calls the BUS_SET_RESOURCE() method of the
3993 bus_set_resource(device_t dev, int type, int rid,
3994 u_long start, u_long count)
3996 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4001 * @brief Wrapper function for BUS_GET_RESOURCE().
4003 * This function simply calls the BUS_GET_RESOURCE() method of the
4007 bus_get_resource(device_t dev, int type, int rid,
4008 u_long *startp, u_long *countp)
4010 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4015 * @brief Wrapper function for BUS_GET_RESOURCE().
4017 * This function simply calls the BUS_GET_RESOURCE() method of the
4018 * parent of @p dev and returns the start value.
4021 bus_get_resource_start(device_t dev, int type, int rid)
4023 u_long start, count;
4026 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4034 * @brief Wrapper function for BUS_GET_RESOURCE().
4036 * This function simply calls the BUS_GET_RESOURCE() method of the
4037 * parent of @p dev and returns the count value.
4040 bus_get_resource_count(device_t dev, int type, int rid)
4042 u_long start, count;
4045 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4053 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4055 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4059 bus_delete_resource(device_t dev, int type, int rid)
4061 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4065 * @brief Wrapper function for BUS_CHILD_PRESENT().
4067 * This function simply calls the BUS_CHILD_PRESENT() method of the
4071 bus_child_present(device_t child)
4073 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4077 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4079 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4083 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4087 parent = device_get_parent(child);
4088 if (parent == NULL) {
4092 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4096 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4098 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4102 bus_child_location_str(device_t child, char *buf, size_t buflen)
4106 parent = device_get_parent(child);
4107 if (parent == NULL) {
4111 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4115 * @brief Wrapper function for BUS_GET_DMA_TAG().
4117 * This function simply calls the BUS_GET_DMA_TAG() method of the
4121 bus_get_dma_tag(device_t dev)
4125 parent = device_get_parent(dev);
4128 return (BUS_GET_DMA_TAG(parent, dev));
4131 /* Resume all devices and then notify userland that we're up again. */
4133 root_resume(device_t dev)
4137 error = bus_generic_resume(dev);
4139 devctl_notify("kern", "power", "resume", NULL);
4144 root_print_child(device_t dev, device_t child)
4148 retval += bus_print_child_header(dev, child);
4149 retval += printf("\n");
4155 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4156 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4159 * If an interrupt mapping gets to here something bad has happened.
4161 panic("root_setup_intr");
4165 * If we get here, assume that the device is permanant and really is
4166 * present in the system. Removable bus drivers are expected to intercept
4167 * this call long before it gets here. We return -1 so that drivers that
4168 * really care can check vs -1 or some ERRNO returned higher in the food
4172 root_child_present(device_t dev, device_t child)
4177 static kobj_method_t root_methods[] = {
4178 /* Device interface */
4179 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4180 KOBJMETHOD(device_suspend, bus_generic_suspend),
4181 KOBJMETHOD(device_resume, root_resume),
4184 KOBJMETHOD(bus_print_child, root_print_child),
4185 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4186 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4187 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4188 KOBJMETHOD(bus_child_present, root_child_present),
4193 static driver_t root_driver = {
4200 devclass_t root_devclass;
4203 root_bus_module_handler(module_t mod, int what, void* arg)
4207 TAILQ_INIT(&bus_data_devices);
4208 kobj_class_compile((kobj_class_t) &root_driver);
4209 root_bus = make_device(NULL, "root", 0);
4210 root_bus->desc = "System root bus";
4211 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4212 root_bus->driver = &root_driver;
4213 root_bus->state = DS_ATTACHED;
4214 root_devclass = devclass_find_internal("root", NULL, FALSE);
4219 device_shutdown(root_bus);
4222 return (EOPNOTSUPP);
4228 static moduledata_t root_bus_mod = {
4230 root_bus_module_handler,
4233 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4236 * @brief Automatically configure devices
4238 * This function begins the autoconfiguration process by calling
4239 * device_probe_and_attach() for each child of the @c root0 device.
4242 root_bus_configure(void)
4247 /* Eventually this will be split up, but this is sufficient for now. */
4248 bus_set_pass(BUS_PASS_DEFAULT);
4252 * @brief Module handler for registering device drivers
4254 * This module handler is used to automatically register device
4255 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4256 * devclass_add_driver() for the driver described by the
4257 * driver_module_data structure pointed to by @p arg
4260 driver_module_handler(module_t mod, int what, void *arg)
4262 struct driver_module_data *dmd;
4263 devclass_t bus_devclass;
4264 kobj_class_t driver;
4267 dmd = (struct driver_module_data *)arg;
4268 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4273 if (dmd->dmd_chainevh)
4274 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4276 pass = dmd->dmd_pass;
4277 driver = dmd->dmd_driver;
4278 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4279 DRIVERNAME(driver), dmd->dmd_busname, pass));
4280 error = devclass_add_driver(bus_devclass, driver, pass,
4285 PDEBUG(("Unloading module: driver %s from bus %s",
4286 DRIVERNAME(dmd->dmd_driver),
4288 error = devclass_delete_driver(bus_devclass,
4291 if (!error && dmd->dmd_chainevh)
4292 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4295 PDEBUG(("Quiesce module: driver %s from bus %s",
4296 DRIVERNAME(dmd->dmd_driver),
4298 error = devclass_quiesce_driver(bus_devclass,
4301 if (!error && dmd->dmd_chainevh)
4302 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4313 * @brief Enumerate all hinted devices for this bus.
4315 * Walks through the hints for this bus and calls the bus_hinted_child
4316 * routine for each one it fines. It searches first for the specific
4317 * bus that's being probed for hinted children (eg isa0), and then for
4318 * generic children (eg isa).
4320 * @param dev bus device to enumerate
4323 bus_enumerate_hinted_children(device_t bus)
4326 const char *dname, *busname;
4330 * enumerate all devices on the specific bus
4332 busname = device_get_nameunit(bus);
4334 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4335 BUS_HINTED_CHILD(bus, dname, dunit);
4338 * and all the generic ones.
4340 busname = device_get_name(bus);
4342 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4343 BUS_HINTED_CHILD(bus, dname, dunit);
4348 /* the _short versions avoid iteration by not calling anything that prints
4349 * more than oneliners. I love oneliners.
4353 print_device_short(device_t dev, int indent)
4358 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4359 dev->unit, dev->desc,
4360 (dev->parent? "":"no "),
4361 (TAILQ_EMPTY(&dev->children)? "no ":""),
4362 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4363 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4364 (dev->flags&DF_WILDCARD? "wildcard,":""),
4365 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4366 (dev->flags&DF_REBID? "rebiddable,":""),
4367 (dev->ivars? "":"no "),
4368 (dev->softc? "":"no "),
4373 print_device(device_t dev, int indent)
4378 print_device_short(dev, indent);
4380 indentprintf(("Parent:\n"));
4381 print_device_short(dev->parent, indent+1);
4382 indentprintf(("Driver:\n"));
4383 print_driver_short(dev->driver, indent+1);
4384 indentprintf(("Devclass:\n"));
4385 print_devclass_short(dev->devclass, indent+1);
4389 print_device_tree_short(device_t dev, int indent)
4390 /* print the device and all its children (indented) */
4397 print_device_short(dev, indent);
4399 TAILQ_FOREACH(child, &dev->children, link) {
4400 print_device_tree_short(child, indent+1);
4405 print_device_tree(device_t dev, int indent)
4406 /* print the device and all its children (indented) */
4413 print_device(dev, indent);
4415 TAILQ_FOREACH(child, &dev->children, link) {
4416 print_device_tree(child, indent+1);
4421 print_driver_short(driver_t *driver, int indent)
4426 indentprintf(("driver %s: softc size = %zd\n",
4427 driver->name, driver->size));
4431 print_driver(driver_t *driver, int indent)
4436 print_driver_short(driver, indent);
4441 print_driver_list(driver_list_t drivers, int indent)
4443 driverlink_t driver;
4445 TAILQ_FOREACH(driver, &drivers, link) {
4446 print_driver(driver->driver, indent);
4451 print_devclass_short(devclass_t dc, int indent)
4456 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4460 print_devclass(devclass_t dc, int indent)
4467 print_devclass_short(dc, indent);
4468 indentprintf(("Drivers:\n"));
4469 print_driver_list(dc->drivers, indent+1);
4471 indentprintf(("Devices:\n"));
4472 for (i = 0; i < dc->maxunit; i++)
4474 print_device(dc->devices[i], indent+1);
4478 print_devclass_list_short(void)
4482 printf("Short listing of devclasses, drivers & devices:\n");
4483 TAILQ_FOREACH(dc, &devclasses, link) {
4484 print_devclass_short(dc, 0);
4489 print_devclass_list(void)
4493 printf("Full listing of devclasses, drivers & devices:\n");
4494 TAILQ_FOREACH(dc, &devclasses, link) {
4495 print_devclass(dc, 0);
4502 * User-space access to the device tree.
4504 * We implement a small set of nodes:
4506 * hw.bus Single integer read method to obtain the
4507 * current generation count.
4508 * hw.bus.devices Reads the entire device tree in flat space.
4509 * hw.bus.rman Resource manager interface
4511 * We might like to add the ability to scan devclasses and/or drivers to
4512 * determine what else is currently loaded/available.
4516 sysctl_bus(SYSCTL_HANDLER_ARGS)
4518 struct u_businfo ubus;
4520 ubus.ub_version = BUS_USER_VERSION;
4521 ubus.ub_generation = bus_data_generation;
4523 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4525 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4526 "bus-related data");
4529 sysctl_devices(SYSCTL_HANDLER_ARGS)
4531 int *name = (int *)arg1;
4532 u_int namelen = arg2;
4535 struct u_device udev; /* XXX this is a bit big */
4541 if (bus_data_generation_check(name[0]))
4547 * Scan the list of devices, looking for the requested index.
4549 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4557 * Populate the return array.
4559 bzero(&udev, sizeof(udev));
4560 udev.dv_handle = (uintptr_t)dev;
4561 udev.dv_parent = (uintptr_t)dev->parent;
4562 if (dev->nameunit != NULL)
4563 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4564 if (dev->desc != NULL)
4565 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4566 if (dev->driver != NULL && dev->driver->name != NULL)
4567 strlcpy(udev.dv_drivername, dev->driver->name,
4568 sizeof(udev.dv_drivername));
4569 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4570 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4571 udev.dv_devflags = dev->devflags;
4572 udev.dv_flags = dev->flags;
4573 udev.dv_state = dev->state;
4574 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4578 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4579 "system device tree");
4582 bus_data_generation_check(int generation)
4584 if (generation != bus_data_generation)
4587 /* XXX generate optimised lists here? */
4592 bus_data_generation_update(void)
4594 bus_data_generation++;
4598 bus_free_resource(device_t dev, int type, struct resource *r)
4602 return (bus_release_resource(dev, type, rman_get_rid(r), r));