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 uint32_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", CTLTYPE_STRING | 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", CTLTYPE_STRING | CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
294 "device description");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
298 "device driver name");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
302 "device location relative to parent");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
306 "device identification");
307 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
309 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
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;
577 kern_psignal(p, SIGIO);
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)
711 devaddq("+", device_get_nameunit(dev), dev);
715 * A device was removed from the tree. We are called just before this
719 devremoved(device_t dev)
721 devaddq("-", device_get_nameunit(dev), dev);
725 * Called when there's no match for this device. This is only called
726 * the first time that no match happens, so we don't keep getting this
727 * message. Should that prove to be undesirable, we can change it.
728 * This is called when all drivers that can attach to a given bus
729 * decline to accept this device. Other errors may not be detected.
732 devnomatch(device_t dev)
734 devaddq("?", "", dev);
738 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
740 struct dev_event_info *n1;
743 dis = devctl_queue_length == 0;
744 error = sysctl_handle_int(oidp, &dis, 0, req);
745 if (error || !req->newptr)
747 mtx_lock(&devsoftc.mtx);
749 while (!TAILQ_EMPTY(&devsoftc.devq)) {
750 n1 = TAILQ_FIRST(&devsoftc.devq);
751 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
752 free(n1->dei_data, M_BUS);
756 devctl_queue_length = 0;
758 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
760 mtx_unlock(&devsoftc.mtx);
765 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
767 struct dev_event_info *n1;
770 q = devctl_queue_length;
771 error = sysctl_handle_int(oidp, &q, 0, req);
772 if (error || !req->newptr)
776 mtx_lock(&devsoftc.mtx);
777 devctl_queue_length = q;
778 while (devsoftc.queued > devctl_queue_length) {
779 n1 = TAILQ_FIRST(&devsoftc.devq);
780 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
781 free(n1->dei_data, M_BUS);
785 mtx_unlock(&devsoftc.mtx);
789 /* End of /dev/devctl code */
791 static TAILQ_HEAD(,device) bus_data_devices;
792 static int bus_data_generation = 1;
794 static kobj_method_t null_methods[] = {
798 DEFINE_CLASS(null, null_methods, 0);
801 * Bus pass implementation
804 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
805 int bus_current_pass = BUS_PASS_ROOT;
809 * @brief Register the pass level of a new driver attachment
811 * Register a new driver attachment's pass level. If no driver
812 * attachment with the same pass level has been added, then @p new
813 * will be added to the global passes list.
815 * @param new the new driver attachment
818 driver_register_pass(struct driverlink *new)
820 struct driverlink *dl;
822 /* We only consider pass numbers during boot. */
823 if (bus_current_pass == BUS_PASS_DEFAULT)
827 * Walk the passes list. If we already know about this pass
828 * then there is nothing to do. If we don't, then insert this
829 * driver link into the list.
831 TAILQ_FOREACH(dl, &passes, passlink) {
832 if (dl->pass < new->pass)
834 if (dl->pass == new->pass)
836 TAILQ_INSERT_BEFORE(dl, new, passlink);
839 TAILQ_INSERT_TAIL(&passes, new, passlink);
843 * @brief Raise the current bus pass
845 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
846 * method on the root bus to kick off a new device tree scan for each
847 * new pass level that has at least one driver.
850 bus_set_pass(int pass)
852 struct driverlink *dl;
854 if (bus_current_pass > pass)
855 panic("Attempt to lower bus pass level");
857 TAILQ_FOREACH(dl, &passes, passlink) {
858 /* Skip pass values below the current pass level. */
859 if (dl->pass <= bus_current_pass)
863 * Bail once we hit a driver with a pass level that is
870 * Raise the pass level to the next level and rescan
873 bus_current_pass = dl->pass;
874 BUS_NEW_PASS(root_bus);
878 * If there isn't a driver registered for the requested pass,
879 * then bus_current_pass might still be less than 'pass'. Set
880 * it to 'pass' in that case.
882 if (bus_current_pass < pass)
883 bus_current_pass = pass;
884 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
888 * Devclass implementation
891 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
895 * @brief Find or create a device class
897 * If a device class with the name @p classname exists, return it,
898 * otherwise if @p create is non-zero create and return a new device
901 * If @p parentname is non-NULL, the parent of the devclass is set to
902 * the devclass of that name.
904 * @param classname the devclass name to find or create
905 * @param parentname the parent devclass name or @c NULL
906 * @param create non-zero to create a devclass
909 devclass_find_internal(const char *classname, const char *parentname,
914 PDEBUG(("looking for %s", classname));
918 TAILQ_FOREACH(dc, &devclasses, link) {
919 if (!strcmp(dc->name, classname))
924 PDEBUG(("creating %s", classname));
925 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
926 M_BUS, M_NOWAIT | M_ZERO);
930 dc->name = (char*) (dc + 1);
931 strcpy(dc->name, classname);
932 TAILQ_INIT(&dc->drivers);
933 TAILQ_INSERT_TAIL(&devclasses, dc, link);
935 bus_data_generation_update();
939 * If a parent class is specified, then set that as our parent so
940 * that this devclass will support drivers for the parent class as
941 * well. If the parent class has the same name don't do this though
942 * as it creates a cycle that can trigger an infinite loop in
943 * device_probe_child() if a device exists for which there is no
946 if (parentname && dc && !dc->parent &&
947 strcmp(classname, parentname) != 0) {
948 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
949 dc->parent->flags |= DC_HAS_CHILDREN;
956 * @brief Create a device class
958 * If a device class with the name @p classname exists, return it,
959 * otherwise create and return a new device class.
961 * @param classname the devclass name to find or create
964 devclass_create(const char *classname)
966 return (devclass_find_internal(classname, NULL, TRUE));
970 * @brief Find a device class
972 * If a device class with the name @p classname exists, return it,
973 * otherwise return @c NULL.
975 * @param classname the devclass name to find
978 devclass_find(const char *classname)
980 return (devclass_find_internal(classname, NULL, FALSE));
984 * @brief Register that a device driver has been added to a devclass
986 * Register that a device driver has been added to a devclass. This
987 * is called by devclass_add_driver to accomplish the recursive
988 * notification of all the children classes of dc, as well as dc.
989 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
992 * We do a full search here of the devclass list at each iteration
993 * level to save storing children-lists in the devclass structure. If
994 * we ever move beyond a few dozen devices doing this, we may need to
997 * @param dc the devclass to edit
998 * @param driver the driver that was just added
1001 devclass_driver_added(devclass_t dc, driver_t *driver)
1007 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1009 for (i = 0; i < dc->maxunit; i++)
1010 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1011 BUS_DRIVER_ADDED(dc->devices[i], driver);
1014 * Walk through the children classes. Since we only keep a
1015 * single parent pointer around, we walk the entire list of
1016 * devclasses looking for children. We set the
1017 * DC_HAS_CHILDREN flag when a child devclass is created on
1018 * the parent, so we only walk the list for those devclasses
1019 * that have children.
1021 if (!(dc->flags & DC_HAS_CHILDREN))
1024 TAILQ_FOREACH(dc, &devclasses, link) {
1025 if (dc->parent == parent)
1026 devclass_driver_added(dc, driver);
1031 * @brief Add a device driver to a device class
1033 * Add a device driver to a devclass. This is normally called
1034 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1035 * all devices in the devclass will be called to allow them to attempt
1036 * to re-probe any unmatched children.
1038 * @param dc the devclass to edit
1039 * @param driver the driver to register
1042 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
1045 const char *parentname;
1047 PDEBUG(("%s", DRIVERNAME(driver)));
1049 /* Don't allow invalid pass values. */
1050 if (pass <= BUS_PASS_ROOT)
1053 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1058 * Compile the driver's methods. Also increase the reference count
1059 * so that the class doesn't get freed when the last instance
1060 * goes. This means we can safely use static methods and avoids a
1061 * double-free in devclass_delete_driver.
1063 kobj_class_compile((kobj_class_t) driver);
1066 * If the driver has any base classes, make the
1067 * devclass inherit from the devclass of the driver's
1068 * first base class. This will allow the system to
1069 * search for drivers in both devclasses for children
1070 * of a device using this driver.
1072 if (driver->baseclasses)
1073 parentname = driver->baseclasses[0]->name;
1076 *dcp = devclass_find_internal(driver->name, parentname, TRUE);
1078 dl->driver = driver;
1079 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1080 driver->refs++; /* XXX: kobj_mtx */
1082 driver_register_pass(dl);
1084 devclass_driver_added(dc, driver);
1085 bus_data_generation_update();
1090 * @brief Register that a device driver has been deleted from a devclass
1092 * Register that a device driver has been removed from a devclass.
1093 * This is called by devclass_delete_driver to accomplish the
1094 * recursive notification of all the children classes of busclass, as
1095 * well as busclass. Each layer will attempt to detach the driver
1096 * from any devices that are children of the bus's devclass. The function
1097 * will return an error if a device fails to detach.
1099 * We do a full search here of the devclass list at each iteration
1100 * level to save storing children-lists in the devclass structure. If
1101 * we ever move beyond a few dozen devices doing this, we may need to
1104 * @param busclass the devclass of the parent bus
1105 * @param dc the devclass of the driver being deleted
1106 * @param driver the driver being deleted
1109 devclass_driver_deleted(devclass_t busclass, devclass_t dc, driver_t *driver)
1116 * Disassociate from any devices. We iterate through all the
1117 * devices in the devclass of the driver and detach any which are
1118 * using the driver and which have a parent in the devclass which
1119 * we are deleting from.
1121 * Note that since a driver can be in multiple devclasses, we
1122 * should not detach devices which are not children of devices in
1123 * the affected devclass.
1125 for (i = 0; i < dc->maxunit; i++) {
1126 if (dc->devices[i]) {
1127 dev = dc->devices[i];
1128 if (dev->driver == driver && dev->parent &&
1129 dev->parent->devclass == busclass) {
1130 if ((error = device_detach(dev)) != 0)
1132 device_set_driver(dev, NULL);
1133 BUS_PROBE_NOMATCH(dev->parent, dev);
1135 dev->flags |= DF_DONENOMATCH;
1141 * Walk through the children classes. Since we only keep a
1142 * single parent pointer around, we walk the entire list of
1143 * devclasses looking for children. We set the
1144 * DC_HAS_CHILDREN flag when a child devclass is created on
1145 * the parent, so we only walk the list for those devclasses
1146 * that have children.
1148 if (!(busclass->flags & DC_HAS_CHILDREN))
1151 TAILQ_FOREACH(busclass, &devclasses, link) {
1152 if (busclass->parent == parent) {
1153 error = devclass_driver_deleted(busclass, dc, driver);
1162 * @brief Delete a device driver from a device class
1164 * Delete a device driver from a devclass. This is normally called
1165 * automatically by DRIVER_MODULE().
1167 * If the driver is currently attached to any devices,
1168 * devclass_delete_driver() will first attempt to detach from each
1169 * device. If one of the detach calls fails, the driver will not be
1172 * @param dc the devclass to edit
1173 * @param driver the driver to unregister
1176 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1178 devclass_t dc = devclass_find(driver->name);
1182 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1188 * Find the link structure in the bus' list of drivers.
1190 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1191 if (dl->driver == driver)
1196 PDEBUG(("%s not found in %s list", driver->name,
1201 error = devclass_driver_deleted(busclass, dc, driver);
1205 TAILQ_REMOVE(&busclass->drivers, dl, link);
1210 if (driver->refs == 0)
1211 kobj_class_free((kobj_class_t) driver);
1213 bus_data_generation_update();
1218 * @brief Quiesces a set of device drivers from a device class
1220 * Quiesce a device driver from a devclass. This is normally called
1221 * automatically by DRIVER_MODULE().
1223 * If the driver is currently attached to any devices,
1224 * devclass_quiesece_driver() will first attempt to quiesce each
1227 * @param dc the devclass to edit
1228 * @param driver the driver to unregister
1231 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1233 devclass_t dc = devclass_find(driver->name);
1239 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1245 * Find the link structure in the bus' list of drivers.
1247 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1248 if (dl->driver == driver)
1253 PDEBUG(("%s not found in %s list", driver->name,
1259 * Quiesce all devices. We iterate through all the devices in
1260 * the devclass of the driver and quiesce any which are using
1261 * the driver and which have a parent in the devclass which we
1264 * Note that since a driver can be in multiple devclasses, we
1265 * should not quiesce devices which are not children of
1266 * devices in the affected devclass.
1268 for (i = 0; i < dc->maxunit; i++) {
1269 if (dc->devices[i]) {
1270 dev = dc->devices[i];
1271 if (dev->driver == driver && dev->parent &&
1272 dev->parent->devclass == busclass) {
1273 if ((error = device_quiesce(dev)) != 0)
1286 devclass_find_driver_internal(devclass_t dc, const char *classname)
1290 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1292 TAILQ_FOREACH(dl, &dc->drivers, link) {
1293 if (!strcmp(dl->driver->name, classname))
1297 PDEBUG(("not found"));
1302 * @brief Return the name of the devclass
1305 devclass_get_name(devclass_t dc)
1311 * @brief Find a device given a unit number
1313 * @param dc the devclass to search
1314 * @param unit the unit number to search for
1316 * @returns the device with the given unit number or @c
1317 * NULL if there is no such device
1320 devclass_get_device(devclass_t dc, int unit)
1322 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1324 return (dc->devices[unit]);
1328 * @brief Find the softc field of a device given a unit number
1330 * @param dc the devclass to search
1331 * @param unit the unit number to search for
1333 * @returns the softc field of the device with the given
1334 * unit number or @c NULL if there is no such
1338 devclass_get_softc(devclass_t dc, int unit)
1342 dev = devclass_get_device(dc, unit);
1346 return (device_get_softc(dev));
1350 * @brief Get a list of devices in the devclass
1352 * An array containing a list of all the devices in the given devclass
1353 * is allocated and returned in @p *devlistp. The number of devices
1354 * in the array is returned in @p *devcountp. The caller should free
1355 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1357 * @param dc the devclass to examine
1358 * @param devlistp points at location for array pointer return
1360 * @param devcountp points at location for array size return value
1363 * @retval ENOMEM the array allocation failed
1366 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1371 count = devclass_get_count(dc);
1372 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1377 for (i = 0; i < dc->maxunit; i++) {
1378 if (dc->devices[i]) {
1379 list[count] = dc->devices[i];
1391 * @brief Get a list of drivers in the devclass
1393 * An array containing a list of pointers to all the drivers in the
1394 * given devclass is allocated and returned in @p *listp. The number
1395 * of drivers in the array is returned in @p *countp. The caller should
1396 * free the array using @c free(p, M_TEMP).
1398 * @param dc the devclass to examine
1399 * @param listp gives location for array pointer return value
1400 * @param countp gives location for number of array elements
1404 * @retval ENOMEM the array allocation failed
1407 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1414 TAILQ_FOREACH(dl, &dc->drivers, link)
1416 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1421 TAILQ_FOREACH(dl, &dc->drivers, link) {
1422 list[count] = dl->driver;
1432 * @brief Get the number of devices in a devclass
1434 * @param dc the devclass to examine
1437 devclass_get_count(devclass_t dc)
1442 for (i = 0; i < dc->maxunit; i++)
1449 * @brief Get the maximum unit number used in a devclass
1451 * Note that this is one greater than the highest currently-allocated
1452 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1453 * that not even the devclass has been allocated yet.
1455 * @param dc the devclass to examine
1458 devclass_get_maxunit(devclass_t dc)
1462 return (dc->maxunit);
1466 * @brief Find a free unit number in a devclass
1468 * This function searches for the first unused unit number greater
1469 * that or equal to @p unit.
1471 * @param dc the devclass to examine
1472 * @param unit the first unit number to check
1475 devclass_find_free_unit(devclass_t dc, int unit)
1479 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1485 * @brief Set the parent of a devclass
1487 * The parent class is normally initialised automatically by
1490 * @param dc the devclass to edit
1491 * @param pdc the new parent devclass
1494 devclass_set_parent(devclass_t dc, devclass_t pdc)
1500 * @brief Get the parent of a devclass
1502 * @param dc the devclass to examine
1505 devclass_get_parent(devclass_t dc)
1507 return (dc->parent);
1510 struct sysctl_ctx_list *
1511 devclass_get_sysctl_ctx(devclass_t dc)
1513 return (&dc->sysctl_ctx);
1517 devclass_get_sysctl_tree(devclass_t dc)
1519 return (dc->sysctl_tree);
1524 * @brief Allocate a unit number
1526 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1527 * will do). The allocated unit number is returned in @p *unitp.
1529 * @param dc the devclass to allocate from
1530 * @param unitp points at the location for the allocated unit
1534 * @retval EEXIST the requested unit number is already allocated
1535 * @retval ENOMEM memory allocation failure
1538 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1543 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1545 /* Ask the parent bus if it wants to wire this device. */
1547 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1550 /* If we were given a wired unit number, check for existing device */
1553 if (unit >= 0 && unit < dc->maxunit &&
1554 dc->devices[unit] != NULL) {
1556 printf("%s: %s%d already exists; skipping it\n",
1557 dc->name, dc->name, *unitp);
1561 /* Unwired device, find the next available slot for it */
1563 for (unit = 0;; unit++) {
1564 /* If there is an "at" hint for a unit then skip it. */
1565 if (resource_string_value(dc->name, unit, "at", &s) ==
1569 /* If this device slot is already in use, skip it. */
1570 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1578 * We've selected a unit beyond the length of the table, so let's
1579 * extend the table to make room for all units up to and including
1582 if (unit >= dc->maxunit) {
1583 device_t *newlist, *oldlist;
1586 oldlist = dc->devices;
1587 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1588 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1591 if (oldlist != NULL)
1592 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1593 bzero(newlist + dc->maxunit,
1594 sizeof(device_t) * (newsize - dc->maxunit));
1595 dc->devices = newlist;
1596 dc->maxunit = newsize;
1597 if (oldlist != NULL)
1598 free(oldlist, M_BUS);
1600 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1608 * @brief Add a device to a devclass
1610 * A unit number is allocated for the device (using the device's
1611 * preferred unit number if any) and the device is registered in the
1612 * devclass. This allows the device to be looked up by its unit
1613 * number, e.g. by decoding a dev_t minor number.
1615 * @param dc the devclass to add to
1616 * @param dev the device to add
1619 * @retval EEXIST the requested unit number is already allocated
1620 * @retval ENOMEM memory allocation failure
1623 devclass_add_device(devclass_t dc, device_t dev)
1627 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1629 buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
1632 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1636 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1637 free(dev->nameunit, M_BUS);
1638 dev->nameunit = NULL;
1641 dc->devices[dev->unit] = dev;
1643 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1650 * @brief Delete a device from a devclass
1652 * The device is removed from the devclass's device list and its unit
1655 * @param dc the devclass to delete from
1656 * @param dev the device to delete
1661 devclass_delete_device(devclass_t dc, device_t dev)
1666 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1668 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1669 panic("devclass_delete_device: inconsistent device class");
1670 dc->devices[dev->unit] = NULL;
1671 if (dev->flags & DF_WILDCARD)
1673 dev->devclass = NULL;
1674 free(dev->nameunit, M_BUS);
1675 dev->nameunit = NULL;
1682 * @brief Make a new device and add it as a child of @p parent
1684 * @param parent the parent of the new device
1685 * @param name the devclass name of the new device or @c NULL
1686 * to leave the devclass unspecified
1687 * @parem unit the unit number of the new device of @c -1 to
1688 * leave the unit number unspecified
1690 * @returns the new device
1693 make_device(device_t parent, const char *name, int unit)
1698 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1701 dc = devclass_find_internal(name, NULL, TRUE);
1703 printf("make_device: can't find device class %s\n",
1711 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1715 dev->parent = parent;
1716 TAILQ_INIT(&dev->children);
1717 kobj_init((kobj_t) dev, &null_class);
1719 dev->devclass = NULL;
1721 dev->nameunit = NULL;
1725 dev->flags = DF_ENABLED;
1728 dev->flags |= DF_WILDCARD;
1730 dev->flags |= DF_FIXEDCLASS;
1731 if (devclass_add_device(dc, dev)) {
1732 kobj_delete((kobj_t) dev, M_BUS);
1739 dev->state = DS_NOTPRESENT;
1741 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1742 bus_data_generation_update();
1749 * @brief Print a description of a device.
1752 device_print_child(device_t dev, device_t child)
1756 if (device_is_alive(child))
1757 retval += BUS_PRINT_CHILD(dev, child);
1759 retval += device_printf(child, " not found\n");
1765 * @brief Create a new device
1767 * This creates a new device and adds it as a child of an existing
1768 * parent device. The new device will be added after the last existing
1769 * child with order zero.
1771 * @param dev the device which will be the parent of the
1773 * @param name devclass name for new device or @c NULL if not
1775 * @param unit unit number for new device or @c -1 if not
1778 * @returns the new device
1781 device_add_child(device_t dev, const char *name, int unit)
1783 return (device_add_child_ordered(dev, 0, name, unit));
1787 * @brief Create a new device
1789 * This creates a new device and adds it as a child of an existing
1790 * parent device. The new device will be added after the last existing
1791 * child with the same order.
1793 * @param dev the device which will be the parent of the
1795 * @param order a value which is used to partially sort the
1796 * children of @p dev - devices created using
1797 * lower values of @p order appear first in @p
1798 * dev's list of children
1799 * @param name devclass name for new device or @c NULL if not
1801 * @param unit unit number for new device or @c -1 if not
1804 * @returns the new device
1807 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
1812 PDEBUG(("%s at %s with order %u as unit %d",
1813 name, DEVICENAME(dev), order, unit));
1815 child = make_device(dev, name, unit);
1818 child->order = order;
1820 TAILQ_FOREACH(place, &dev->children, link) {
1821 if (place->order > order)
1827 * The device 'place' is the first device whose order is
1828 * greater than the new child.
1830 TAILQ_INSERT_BEFORE(place, child, link);
1833 * The new child's order is greater or equal to the order of
1834 * any existing device. Add the child to the tail of the list.
1836 TAILQ_INSERT_TAIL(&dev->children, child, link);
1839 bus_data_generation_update();
1844 * @brief Delete a device
1846 * This function deletes a device along with all of its children. If
1847 * the device currently has a driver attached to it, the device is
1848 * detached first using device_detach().
1850 * @param dev the parent device
1851 * @param child the device to delete
1854 * @retval non-zero a unit error code describing the error
1857 device_delete_child(device_t dev, device_t child)
1860 device_t grandchild;
1862 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1864 /* remove children first */
1865 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1866 error = device_delete_child(child, grandchild);
1871 if ((error = device_detach(child)) != 0)
1873 if (child->devclass)
1874 devclass_delete_device(child->devclass, child);
1875 TAILQ_REMOVE(&dev->children, child, link);
1876 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1877 kobj_delete((kobj_t) child, M_BUS);
1879 bus_data_generation_update();
1884 * @brief Find a device given a unit number
1886 * This is similar to devclass_get_devices() but only searches for
1887 * devices which have @p dev as a parent.
1889 * @param dev the parent device to search
1890 * @param unit the unit number to search for. If the unit is -1,
1891 * return the first child of @p dev which has name
1892 * @p classname (that is, the one with the lowest unit.)
1894 * @returns the device with the given unit number or @c
1895 * NULL if there is no such device
1898 device_find_child(device_t dev, const char *classname, int unit)
1903 dc = devclass_find(classname);
1908 child = devclass_get_device(dc, unit);
1909 if (child && child->parent == dev)
1912 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1913 child = devclass_get_device(dc, unit);
1914 if (child && child->parent == dev)
1925 first_matching_driver(devclass_t dc, device_t dev)
1928 return (devclass_find_driver_internal(dc, dev->devclass->name));
1929 return (TAILQ_FIRST(&dc->drivers));
1936 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1938 if (dev->devclass) {
1940 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1941 if (!strcmp(dev->devclass->name, dl->driver->name))
1945 return (TAILQ_NEXT(last, link));
1952 device_probe_child(device_t dev, device_t child)
1955 driverlink_t best = NULL;
1957 int result, pri = 0;
1958 int hasclass = (child->devclass != NULL);
1964 panic("device_probe_child: parent device has no devclass");
1967 * If the state is already probed, then return. However, don't
1968 * return if we can rebid this object.
1970 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1973 for (; dc; dc = dc->parent) {
1974 for (dl = first_matching_driver(dc, child);
1976 dl = next_matching_driver(dc, child, dl)) {
1978 /* If this driver's pass is too high, then ignore it. */
1979 if (dl->pass > bus_current_pass)
1982 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1983 device_set_driver(child, dl->driver);
1985 if (device_set_devclass(child, dl->driver->name)) {
1986 printf("driver bug: Unable to set devclass (devname: %s)\n",
1987 (child ? device_get_name(child) :
1989 device_set_driver(child, NULL);
1994 /* Fetch any flags for the device before probing. */
1995 resource_int_value(dl->driver->name, child->unit,
1996 "flags", &child->devflags);
1998 result = DEVICE_PROBE(child);
2000 /* Reset flags and devclass before the next probe. */
2001 child->devflags = 0;
2003 device_set_devclass(child, NULL);
2006 * If the driver returns SUCCESS, there can be
2007 * no higher match for this device.
2016 * The driver returned an error so it
2017 * certainly doesn't match.
2020 device_set_driver(child, NULL);
2025 * A priority lower than SUCCESS, remember the
2026 * best matching driver. Initialise the value
2027 * of pri for the first match.
2029 if (best == NULL || result > pri) {
2031 * Probes that return BUS_PROBE_NOWILDCARD
2032 * or lower only match when they are set
2033 * in stone by the parent bus.
2035 if (result <= BUS_PROBE_NOWILDCARD &&
2036 child->flags & DF_WILDCARD)
2044 * If we have an unambiguous match in this devclass,
2045 * don't look in the parent.
2047 if (best && pri == 0)
2052 * If we found a driver, change state and initialise the devclass.
2054 /* XXX What happens if we rebid and got no best? */
2057 * If this device was atached, and we were asked to
2058 * rescan, and it is a different driver, then we have
2059 * to detach the old driver and reattach this new one.
2060 * Note, we don't have to check for DF_REBID here
2061 * because if the state is > DS_ALIVE, we know it must
2064 * This assumes that all DF_REBID drivers can have
2065 * their probe routine called at any time and that
2066 * they are idempotent as well as completely benign in
2067 * normal operations.
2069 * We also have to make sure that the detach
2070 * succeeded, otherwise we fail the operation (or
2071 * maybe it should just fail silently? I'm torn).
2073 if (child->state > DS_ALIVE && best->driver != child->driver)
2074 if ((result = device_detach(dev)) != 0)
2077 /* Set the winning driver, devclass, and flags. */
2078 if (!child->devclass) {
2079 result = device_set_devclass(child, best->driver->name);
2083 device_set_driver(child, best->driver);
2084 resource_int_value(best->driver->name, child->unit,
2085 "flags", &child->devflags);
2089 * A bit bogus. Call the probe method again to make
2090 * sure that we have the right description.
2092 DEVICE_PROBE(child);
2094 child->flags |= DF_REBID;
2097 child->flags &= ~DF_REBID;
2098 child->state = DS_ALIVE;
2100 bus_data_generation_update();
2108 * @brief Return the parent of a device
2111 device_get_parent(device_t dev)
2113 return (dev->parent);
2117 * @brief Get a list of children of a device
2119 * An array containing a list of all the children of the given device
2120 * is allocated and returned in @p *devlistp. The number of devices
2121 * in the array is returned in @p *devcountp. The caller should free
2122 * the array using @c free(p, M_TEMP).
2124 * @param dev the device to examine
2125 * @param devlistp points at location for array pointer return
2127 * @param devcountp points at location for array size return value
2130 * @retval ENOMEM the array allocation failed
2133 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2140 TAILQ_FOREACH(child, &dev->children, link) {
2144 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2149 TAILQ_FOREACH(child, &dev->children, link) {
2150 list[count] = child;
2161 * @brief Return the current driver for the device or @c NULL if there
2162 * is no driver currently attached
2165 device_get_driver(device_t dev)
2167 return (dev->driver);
2171 * @brief Return the current devclass for the device or @c NULL if
2175 device_get_devclass(device_t dev)
2177 return (dev->devclass);
2181 * @brief Return the name of the device's devclass or @c NULL if there
2185 device_get_name(device_t dev)
2187 if (dev != NULL && dev->devclass)
2188 return (devclass_get_name(dev->devclass));
2193 * @brief Return a string containing the device's devclass name
2194 * followed by an ascii representation of the device's unit number
2198 device_get_nameunit(device_t dev)
2200 return (dev->nameunit);
2204 * @brief Return the device's unit number.
2207 device_get_unit(device_t dev)
2213 * @brief Return the device's description string
2216 device_get_desc(device_t dev)
2222 * @brief Return the device's flags
2225 device_get_flags(device_t dev)
2227 return (dev->devflags);
2230 struct sysctl_ctx_list *
2231 device_get_sysctl_ctx(device_t dev)
2233 return (&dev->sysctl_ctx);
2237 device_get_sysctl_tree(device_t dev)
2239 return (dev->sysctl_tree);
2243 * @brief Print the name of the device followed by a colon and a space
2245 * @returns the number of characters printed
2248 device_print_prettyname(device_t dev)
2250 const char *name = device_get_name(dev);
2253 return (printf("unknown: "));
2254 return (printf("%s%d: ", name, device_get_unit(dev)));
2258 * @brief Print the name of the device followed by a colon, a space
2259 * and the result of calling vprintf() with the value of @p fmt and
2260 * the following arguments.
2262 * @returns the number of characters printed
2265 device_printf(device_t dev, const char * fmt, ...)
2270 retval = device_print_prettyname(dev);
2272 retval += vprintf(fmt, ap);
2281 device_set_desc_internal(device_t dev, const char* desc, int copy)
2283 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2284 free(dev->desc, M_BUS);
2285 dev->flags &= ~DF_DESCMALLOCED;
2290 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2292 strcpy(dev->desc, desc);
2293 dev->flags |= DF_DESCMALLOCED;
2296 /* Avoid a -Wcast-qual warning */
2297 dev->desc = (char *)(uintptr_t) desc;
2300 bus_data_generation_update();
2304 * @brief Set the device's description
2306 * The value of @c desc should be a string constant that will not
2307 * change (at least until the description is changed in a subsequent
2308 * call to device_set_desc() or device_set_desc_copy()).
2311 device_set_desc(device_t dev, const char* desc)
2313 device_set_desc_internal(dev, desc, FALSE);
2317 * @brief Set the device's description
2319 * The string pointed to by @c desc is copied. Use this function if
2320 * the device description is generated, (e.g. with sprintf()).
2323 device_set_desc_copy(device_t dev, const char* desc)
2325 device_set_desc_internal(dev, desc, TRUE);
2329 * @brief Set the device's flags
2332 device_set_flags(device_t dev, uint32_t flags)
2334 dev->devflags = flags;
2338 * @brief Return the device's softc field
2340 * The softc is allocated and zeroed when a driver is attached, based
2341 * on the size field of the driver.
2344 device_get_softc(device_t dev)
2346 return (dev->softc);
2350 * @brief Set the device's softc field
2352 * Most drivers do not need to use this since the softc is allocated
2353 * automatically when the driver is attached.
2356 device_set_softc(device_t dev, void *softc)
2358 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2359 free(dev->softc, M_BUS_SC);
2362 dev->flags |= DF_EXTERNALSOFTC;
2364 dev->flags &= ~DF_EXTERNALSOFTC;
2368 * @brief Get the device's ivars field
2370 * The ivars field is used by the parent device to store per-device
2371 * state (e.g. the physical location of the device or a list of
2375 device_get_ivars(device_t dev)
2378 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2379 return (dev->ivars);
2383 * @brief Set the device's ivars field
2386 device_set_ivars(device_t dev, void * ivars)
2389 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2394 * @brief Return the device's state
2397 device_get_state(device_t dev)
2399 return (dev->state);
2403 * @brief Set the DF_ENABLED flag for the device
2406 device_enable(device_t dev)
2408 dev->flags |= DF_ENABLED;
2412 * @brief Clear the DF_ENABLED flag for the device
2415 device_disable(device_t dev)
2417 dev->flags &= ~DF_ENABLED;
2421 * @brief Increment the busy counter for the device
2424 device_busy(device_t dev)
2426 if (dev->state < DS_ATTACHED)
2427 panic("device_busy: called for unattached device");
2428 if (dev->busy == 0 && dev->parent)
2429 device_busy(dev->parent);
2431 dev->state = DS_BUSY;
2435 * @brief Decrement the busy counter for the device
2438 device_unbusy(device_t dev)
2440 if (dev->state != DS_BUSY)
2441 panic("device_unbusy: called for non-busy device %s",
2442 device_get_nameunit(dev));
2444 if (dev->busy == 0) {
2446 device_unbusy(dev->parent);
2447 dev->state = DS_ATTACHED;
2452 * @brief Set the DF_QUIET flag for the device
2455 device_quiet(device_t dev)
2457 dev->flags |= DF_QUIET;
2461 * @brief Clear the DF_QUIET flag for the device
2464 device_verbose(device_t dev)
2466 dev->flags &= ~DF_QUIET;
2470 * @brief Return non-zero if the DF_QUIET flag is set on the device
2473 device_is_quiet(device_t dev)
2475 return ((dev->flags & DF_QUIET) != 0);
2479 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2482 device_is_enabled(device_t dev)
2484 return ((dev->flags & DF_ENABLED) != 0);
2488 * @brief Return non-zero if the device was successfully probed
2491 device_is_alive(device_t dev)
2493 return (dev->state >= DS_ALIVE);
2497 * @brief Return non-zero if the device currently has a driver
2501 device_is_attached(device_t dev)
2503 return (dev->state >= DS_ATTACHED);
2507 * @brief Set the devclass of a device
2508 * @see devclass_add_device().
2511 device_set_devclass(device_t dev, const char *classname)
2518 devclass_delete_device(dev->devclass, dev);
2522 if (dev->devclass) {
2523 printf("device_set_devclass: device class already set\n");
2527 dc = devclass_find_internal(classname, NULL, TRUE);
2531 error = devclass_add_device(dc, dev);
2533 bus_data_generation_update();
2538 * @brief Set the driver of a device
2541 * @retval EBUSY the device already has a driver attached
2542 * @retval ENOMEM a memory allocation failure occurred
2545 device_set_driver(device_t dev, driver_t *driver)
2547 if (dev->state >= DS_ATTACHED)
2550 if (dev->driver == driver)
2553 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2554 free(dev->softc, M_BUS_SC);
2557 kobj_delete((kobj_t) dev, NULL);
2558 dev->driver = driver;
2560 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2561 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2562 dev->softc = malloc(driver->size, M_BUS_SC,
2565 kobj_delete((kobj_t) dev, NULL);
2566 kobj_init((kobj_t) dev, &null_class);
2572 kobj_init((kobj_t) dev, &null_class);
2575 bus_data_generation_update();
2580 * @brief Probe a device, and return this status.
2582 * This function is the core of the device autoconfiguration
2583 * system. Its purpose is to select a suitable driver for a device and
2584 * then call that driver to initialise the hardware appropriately. The
2585 * driver is selected by calling the DEVICE_PROBE() method of a set of
2586 * candidate drivers and then choosing the driver which returned the
2587 * best value. This driver is then attached to the device using
2590 * The set of suitable drivers is taken from the list of drivers in
2591 * the parent device's devclass. If the device was originally created
2592 * with a specific class name (see device_add_child()), only drivers
2593 * with that name are probed, otherwise all drivers in the devclass
2594 * are probed. If no drivers return successful probe values in the
2595 * parent devclass, the search continues in the parent of that
2596 * devclass (see devclass_get_parent()) if any.
2598 * @param dev the device to initialise
2601 * @retval ENXIO no driver was found
2602 * @retval ENOMEM memory allocation failure
2603 * @retval non-zero some other unix error code
2604 * @retval -1 Device already attached
2607 device_probe(device_t dev)
2613 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2616 if (!(dev->flags & DF_ENABLED)) {
2617 if (bootverbose && device_get_name(dev) != NULL) {
2618 device_print_prettyname(dev);
2619 printf("not probed (disabled)\n");
2623 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2624 if (bus_current_pass == BUS_PASS_DEFAULT &&
2625 !(dev->flags & DF_DONENOMATCH)) {
2626 BUS_PROBE_NOMATCH(dev->parent, dev);
2628 dev->flags |= DF_DONENOMATCH;
2636 * @brief Probe a device and attach a driver if possible
2638 * calls device_probe() and attaches if that was successful.
2641 device_probe_and_attach(device_t dev)
2647 error = device_probe(dev);
2650 else if (error != 0)
2652 return (device_attach(dev));
2656 * @brief Attach a device driver to a device
2658 * This function is a wrapper around the DEVICE_ATTACH() driver
2659 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2660 * device's sysctl tree, optionally prints a description of the device
2661 * and queues a notification event for user-based device management
2664 * Normally this function is only called internally from
2665 * device_probe_and_attach().
2667 * @param dev the device to initialise
2670 * @retval ENXIO no driver was found
2671 * @retval ENOMEM memory allocation failure
2672 * @retval non-zero some other unix error code
2675 device_attach(device_t dev)
2679 device_sysctl_init(dev);
2680 if (!device_is_quiet(dev))
2681 device_print_child(dev->parent, dev);
2682 if ((error = DEVICE_ATTACH(dev)) != 0) {
2683 printf("device_attach: %s%d attach returned %d\n",
2684 dev->driver->name, dev->unit, error);
2685 /* Unset the class; set in device_probe_child */
2686 if (dev->devclass == NULL)
2687 device_set_devclass(dev, NULL);
2688 device_set_driver(dev, NULL);
2689 device_sysctl_fini(dev);
2690 dev->state = DS_NOTPRESENT;
2693 device_sysctl_update(dev);
2694 dev->state = DS_ATTACHED;
2695 dev->flags &= ~DF_DONENOMATCH;
2701 * @brief Detach a driver from a device
2703 * This function is a wrapper around the DEVICE_DETACH() driver
2704 * method. If the call to DEVICE_DETACH() succeeds, it calls
2705 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2706 * notification event for user-based device management services and
2707 * cleans up the device's sysctl tree.
2709 * @param dev the device to un-initialise
2712 * @retval ENXIO no driver was found
2713 * @retval ENOMEM memory allocation failure
2714 * @retval non-zero some other unix error code
2717 device_detach(device_t dev)
2723 PDEBUG(("%s", DEVICENAME(dev)));
2724 if (dev->state == DS_BUSY)
2726 if (dev->state != DS_ATTACHED)
2729 if ((error = DEVICE_DETACH(dev)) != 0)
2732 if (!device_is_quiet(dev))
2733 device_printf(dev, "detached\n");
2735 BUS_CHILD_DETACHED(dev->parent, dev);
2737 if (!(dev->flags & DF_FIXEDCLASS))
2738 devclass_delete_device(dev->devclass, dev);
2740 dev->state = DS_NOTPRESENT;
2741 device_set_driver(dev, NULL);
2742 device_set_desc(dev, NULL);
2743 device_sysctl_fini(dev);
2749 * @brief Tells a driver to quiesce itself.
2751 * This function is a wrapper around the DEVICE_QUIESCE() driver
2752 * method. If the call to DEVICE_QUIESCE() succeeds.
2754 * @param dev the device to quiesce
2757 * @retval ENXIO no driver was found
2758 * @retval ENOMEM memory allocation failure
2759 * @retval non-zero some other unix error code
2762 device_quiesce(device_t dev)
2765 PDEBUG(("%s", DEVICENAME(dev)));
2766 if (dev->state == DS_BUSY)
2768 if (dev->state != DS_ATTACHED)
2771 return (DEVICE_QUIESCE(dev));
2775 * @brief Notify a device of system shutdown
2777 * This function calls the DEVICE_SHUTDOWN() driver method if the
2778 * device currently has an attached driver.
2780 * @returns the value returned by DEVICE_SHUTDOWN()
2783 device_shutdown(device_t dev)
2785 if (dev->state < DS_ATTACHED)
2787 return (DEVICE_SHUTDOWN(dev));
2791 * @brief Set the unit number of a device
2793 * This function can be used to override the unit number used for a
2794 * device (e.g. to wire a device to a pre-configured unit number).
2797 device_set_unit(device_t dev, int unit)
2802 dc = device_get_devclass(dev);
2803 if (unit < dc->maxunit && dc->devices[unit])
2805 err = devclass_delete_device(dc, dev);
2809 err = devclass_add_device(dc, dev);
2813 bus_data_generation_update();
2817 /*======================================*/
2819 * Some useful method implementations to make life easier for bus drivers.
2823 * @brief Initialise a resource list.
2825 * @param rl the resource list to initialise
2828 resource_list_init(struct resource_list *rl)
2834 * @brief Reclaim memory used by a resource list.
2836 * This function frees the memory for all resource entries on the list
2839 * @param rl the resource list to free
2842 resource_list_free(struct resource_list *rl)
2844 struct resource_list_entry *rle;
2846 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2848 panic("resource_list_free: resource entry is busy");
2849 STAILQ_REMOVE_HEAD(rl, link);
2855 * @brief Add a resource entry.
2857 * This function adds a resource entry using the given @p type, @p
2858 * start, @p end and @p count values. A rid value is chosen by
2859 * searching sequentially for the first unused rid starting at zero.
2861 * @param rl the resource list to edit
2862 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2863 * @param start the start address of the resource
2864 * @param end the end address of the resource
2865 * @param count XXX end-start+1
2868 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2869 u_long end, u_long count)
2874 while (resource_list_find(rl, type, rid) != NULL)
2876 resource_list_add(rl, type, rid, start, end, count);
2881 * @brief Add or modify a resource entry.
2883 * If an existing entry exists with the same type and rid, it will be
2884 * modified using the given values of @p start, @p end and @p
2885 * count. If no entry exists, a new one will be created using the
2886 * given values. The resource list entry that matches is then returned.
2888 * @param rl the resource list to edit
2889 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2890 * @param rid the resource identifier
2891 * @param start the start address of the resource
2892 * @param end the end address of the resource
2893 * @param count XXX end-start+1
2895 struct resource_list_entry *
2896 resource_list_add(struct resource_list *rl, int type, int rid,
2897 u_long start, u_long end, u_long count)
2899 struct resource_list_entry *rle;
2901 rle = resource_list_find(rl, type, rid);
2903 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2906 panic("resource_list_add: can't record entry");
2907 STAILQ_INSERT_TAIL(rl, rle, link);
2915 panic("resource_list_add: resource entry is busy");
2924 * @brief Determine if a resource entry is busy.
2926 * Returns true if a resource entry is busy meaning that it has an
2927 * associated resource that is not an unallocated "reserved" resource.
2929 * @param rl the resource list to search
2930 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2931 * @param rid the resource identifier
2933 * @returns Non-zero if the entry is busy, zero otherwise.
2936 resource_list_busy(struct resource_list *rl, int type, int rid)
2938 struct resource_list_entry *rle;
2940 rle = resource_list_find(rl, type, rid);
2941 if (rle == NULL || rle->res == NULL)
2943 if ((rle->flags & (RLE_RESERVED | RLE_ALLOCATED)) == RLE_RESERVED) {
2944 KASSERT(!(rman_get_flags(rle->res) & RF_ACTIVE),
2945 ("reserved resource is active"));
2952 * @brief Determine if a resource entry is reserved.
2954 * Returns true if a resource entry is reserved meaning that it has an
2955 * associated "reserved" resource. The resource can either be
2956 * allocated or unallocated.
2958 * @param rl the resource list to search
2959 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2960 * @param rid the resource identifier
2962 * @returns Non-zero if the entry is reserved, zero otherwise.
2965 resource_list_reserved(struct resource_list *rl, int type, int rid)
2967 struct resource_list_entry *rle;
2969 rle = resource_list_find(rl, type, rid);
2970 if (rle != NULL && rle->flags & RLE_RESERVED)
2976 * @brief Find a resource entry by type and rid.
2978 * @param rl the resource list to search
2979 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2980 * @param rid the resource identifier
2982 * @returns the resource entry pointer or NULL if there is no such
2985 struct resource_list_entry *
2986 resource_list_find(struct resource_list *rl, int type, int rid)
2988 struct resource_list_entry *rle;
2990 STAILQ_FOREACH(rle, rl, link) {
2991 if (rle->type == type && rle->rid == rid)
2998 * @brief Delete a resource entry.
3000 * @param rl the resource list to edit
3001 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
3002 * @param rid the resource identifier
3005 resource_list_delete(struct resource_list *rl, int type, int rid)
3007 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
3010 if (rle->res != NULL)
3011 panic("resource_list_delete: resource has not been released");
3012 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
3018 * @brief Allocate a reserved resource
3020 * This can be used by busses to force the allocation of resources
3021 * that are always active in the system even if they are not allocated
3022 * by a driver (e.g. PCI BARs). This function is usually called when
3023 * adding a new child to the bus. The resource is allocated from the
3024 * parent bus when it is reserved. The resource list entry is marked
3025 * with RLE_RESERVED to note that it is a reserved resource.
3027 * Subsequent attempts to allocate the resource with
3028 * resource_list_alloc() will succeed the first time and will set
3029 * RLE_ALLOCATED to note that it has been allocated. When a reserved
3030 * resource that has been allocated is released with
3031 * resource_list_release() the resource RLE_ALLOCATED is cleared, but
3032 * the actual resource remains allocated. The resource can be released to
3033 * the parent bus by calling resource_list_unreserve().
3035 * @param rl the resource list to allocate from
3036 * @param bus the parent device of @p child
3037 * @param child the device for which the resource is being reserved
3038 * @param type the type of resource to allocate
3039 * @param rid a pointer to the resource identifier
3040 * @param start hint at the start of the resource range - pass
3041 * @c 0UL for any start address
3042 * @param end hint at the end of the resource range - pass
3043 * @c ~0UL for any end address
3044 * @param count hint at the size of range required - pass @c 1
3046 * @param flags any extra flags to control the resource
3047 * allocation - see @c RF_XXX flags in
3048 * <sys/rman.h> for details
3050 * @returns the resource which was allocated or @c NULL if no
3051 * resource could be allocated
3054 resource_list_reserve(struct resource_list *rl, device_t bus, device_t child,
3055 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3057 struct resource_list_entry *rle = NULL;
3058 int passthrough = (device_get_parent(child) != bus);
3063 "resource_list_reserve() should only be called for direct children");
3064 if (flags & RF_ACTIVE)
3066 "resource_list_reserve() should only reserve inactive resources");
3068 r = resource_list_alloc(rl, bus, child, type, rid, start, end, count,
3071 rle = resource_list_find(rl, type, *rid);
3072 rle->flags |= RLE_RESERVED;
3078 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
3080 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
3081 * and passing the allocation up to the parent of @p bus. This assumes
3082 * that the first entry of @c device_get_ivars(child) is a struct
3083 * resource_list. This also handles 'passthrough' allocations where a
3084 * child is a remote descendant of bus by passing the allocation up to
3085 * the parent of bus.
3087 * Typically, a bus driver would store a list of child resources
3088 * somewhere in the child device's ivars (see device_get_ivars()) and
3089 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
3090 * then call resource_list_alloc() to perform the allocation.
3092 * @param rl the resource list to allocate from
3093 * @param bus the parent device of @p child
3094 * @param child the device which is requesting an allocation
3095 * @param type the type of resource to allocate
3096 * @param rid a pointer to the resource identifier
3097 * @param start hint at the start of the resource range - pass
3098 * @c 0UL for any start address
3099 * @param end hint at the end of the resource range - pass
3100 * @c ~0UL for any end address
3101 * @param count hint at the size of range required - pass @c 1
3103 * @param flags any extra flags to control the resource
3104 * allocation - see @c RF_XXX flags in
3105 * <sys/rman.h> for details
3107 * @returns the resource which was allocated or @c NULL if no
3108 * resource could be allocated
3111 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
3112 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
3114 struct resource_list_entry *rle = NULL;
3115 int passthrough = (device_get_parent(child) != bus);
3116 int isdefault = (start == 0UL && end == ~0UL);
3119 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3120 type, rid, start, end, count, flags));
3123 rle = resource_list_find(rl, type, *rid);
3126 return (NULL); /* no resource of that type/rid */
3129 if (rle->flags & RLE_RESERVED) {
3130 if (rle->flags & RLE_ALLOCATED)
3132 if ((flags & RF_ACTIVE) &&
3133 bus_activate_resource(child, type, *rid,
3136 rle->flags |= RLE_ALLOCATED;
3139 panic("resource_list_alloc: resource entry is busy");
3144 count = ulmax(count, rle->count);
3145 end = ulmax(rle->end, start + count - 1);
3148 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
3149 type, rid, start, end, count, flags);
3152 * Record the new range.
3155 rle->start = rman_get_start(rle->res);
3156 rle->end = rman_get_end(rle->res);
3164 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
3166 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
3167 * used with resource_list_alloc().
3169 * @param rl the resource list which was allocated from
3170 * @param bus the parent device of @p child
3171 * @param child the device which is requesting a release
3172 * @param type the type of resource to release
3173 * @param rid the resource identifier
3174 * @param res the resource to release
3177 * @retval non-zero a standard unix error code indicating what
3178 * error condition prevented the operation
3181 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3182 int type, int rid, struct resource *res)
3184 struct resource_list_entry *rle = NULL;
3185 int passthrough = (device_get_parent(child) != bus);
3189 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3193 rle = resource_list_find(rl, type, rid);
3196 panic("resource_list_release: can't find resource");
3198 panic("resource_list_release: resource entry is not busy");
3199 if (rle->flags & RLE_RESERVED) {
3200 if (rle->flags & RLE_ALLOCATED) {
3201 if (rman_get_flags(res) & RF_ACTIVE) {
3202 error = bus_deactivate_resource(child, type,
3207 rle->flags &= ~RLE_ALLOCATED;
3213 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3223 * @brief Fully release a reserved resource
3225 * Fully releases a resouce reserved via resource_list_reserve().
3227 * @param rl the resource list which was allocated from
3228 * @param bus the parent device of @p child
3229 * @param child the device whose reserved resource is being released
3230 * @param type the type of resource to release
3231 * @param rid the resource identifier
3232 * @param res the resource to release
3235 * @retval non-zero a standard unix error code indicating what
3236 * error condition prevented the operation
3239 resource_list_unreserve(struct resource_list *rl, device_t bus, device_t child,
3242 struct resource_list_entry *rle = NULL;
3243 int passthrough = (device_get_parent(child) != bus);
3247 "resource_list_unreserve() should only be called for direct children");
3249 rle = resource_list_find(rl, type, rid);
3252 panic("resource_list_unreserve: can't find resource");
3253 if (!(rle->flags & RLE_RESERVED))
3255 if (rle->flags & RLE_ALLOCATED)
3257 rle->flags &= ~RLE_RESERVED;
3258 return (resource_list_release(rl, bus, child, type, rid, rle->res));
3262 * @brief Print a description of resources in a resource list
3264 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3265 * The name is printed if at least one resource of the given type is available.
3266 * The format is used to print resource start and end.
3268 * @param rl the resource list to print
3269 * @param name the name of @p type, e.g. @c "memory"
3270 * @param type type type of resource entry to print
3271 * @param format printf(9) format string to print resource
3272 * start and end values
3274 * @returns the number of characters printed
3277 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3280 struct resource_list_entry *rle;
3281 int printed, retval;
3285 /* Yes, this is kinda cheating */
3286 STAILQ_FOREACH(rle, rl, link) {
3287 if (rle->type == type) {
3289 retval += printf(" %s ", name);
3291 retval += printf(",");
3293 retval += printf(format, rle->start);
3294 if (rle->count > 1) {
3295 retval += printf("-");
3296 retval += printf(format, rle->start +
3305 * @brief Releases all the resources in a list.
3307 * @param rl The resource list to purge.
3312 resource_list_purge(struct resource_list *rl)
3314 struct resource_list_entry *rle;
3316 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3318 bus_release_resource(rman_get_device(rle->res),
3319 rle->type, rle->rid, rle->res);
3320 STAILQ_REMOVE_HEAD(rl, link);
3326 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
3329 return (device_add_child_ordered(dev, order, name, unit));
3333 * @brief Helper function for implementing DEVICE_PROBE()
3335 * This function can be used to help implement the DEVICE_PROBE() for
3336 * a bus (i.e. a device which has other devices attached to it). It
3337 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3341 bus_generic_probe(device_t dev)
3343 devclass_t dc = dev->devclass;
3346 TAILQ_FOREACH(dl, &dc->drivers, link) {
3348 * If this driver's pass is too high, then ignore it.
3349 * For most drivers in the default pass, this will
3350 * never be true. For early-pass drivers they will
3351 * only call the identify routines of eligible drivers
3352 * when this routine is called. Drivers for later
3353 * passes should have their identify routines called
3354 * on early-pass busses during BUS_NEW_PASS().
3356 if (dl->pass > bus_current_pass)
3358 DEVICE_IDENTIFY(dl->driver, dev);
3365 * @brief Helper function for implementing DEVICE_ATTACH()
3367 * This function can be used to help implement the DEVICE_ATTACH() for
3368 * a bus. It calls device_probe_and_attach() for each of the device's
3372 bus_generic_attach(device_t dev)
3376 TAILQ_FOREACH(child, &dev->children, link) {
3377 device_probe_and_attach(child);
3384 * @brief Helper function for implementing DEVICE_DETACH()
3386 * This function can be used to help implement the DEVICE_DETACH() for
3387 * a bus. It calls device_detach() for each of the device's
3391 bus_generic_detach(device_t dev)
3396 if (dev->state != DS_ATTACHED)
3399 TAILQ_FOREACH(child, &dev->children, link) {
3400 if ((error = device_detach(child)) != 0)
3408 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3410 * This function can be used to help implement the DEVICE_SHUTDOWN()
3411 * for a bus. It calls device_shutdown() for each of the device's
3415 bus_generic_shutdown(device_t dev)
3419 TAILQ_FOREACH(child, &dev->children, link) {
3420 device_shutdown(child);
3427 * @brief Helper function for implementing DEVICE_SUSPEND()
3429 * This function can be used to help implement the DEVICE_SUSPEND()
3430 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3431 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3432 * operation is aborted and any devices which were suspended are
3433 * resumed immediately by calling their DEVICE_RESUME() methods.
3436 bus_generic_suspend(device_t dev)
3439 device_t child, child2;
3441 TAILQ_FOREACH(child, &dev->children, link) {
3442 error = DEVICE_SUSPEND(child);
3444 for (child2 = TAILQ_FIRST(&dev->children);
3445 child2 && child2 != child;
3446 child2 = TAILQ_NEXT(child2, link))
3447 DEVICE_RESUME(child2);
3455 * @brief Helper function for implementing DEVICE_RESUME()
3457 * This function can be used to help implement the DEVICE_RESUME() for
3458 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3461 bus_generic_resume(device_t dev)
3465 TAILQ_FOREACH(child, &dev->children, link) {
3466 DEVICE_RESUME(child);
3467 /* if resume fails, there's nothing we can usefully do... */
3473 * @brief Helper function for implementing BUS_PRINT_CHILD().
3475 * This function prints the first part of the ascii representation of
3476 * @p child, including its name, unit and description (if any - see
3477 * device_set_desc()).
3479 * @returns the number of characters printed
3482 bus_print_child_header(device_t dev, device_t child)
3486 if (device_get_desc(child)) {
3487 retval += device_printf(child, "<%s>", device_get_desc(child));
3489 retval += printf("%s", device_get_nameunit(child));
3496 * @brief Helper function for implementing BUS_PRINT_CHILD().
3498 * This function prints the last part of the ascii representation of
3499 * @p child, which consists of the string @c " on " followed by the
3500 * name and unit of the @p dev.
3502 * @returns the number of characters printed
3505 bus_print_child_footer(device_t dev, device_t child)
3507 return (printf(" on %s\n", device_get_nameunit(dev)));
3511 * @brief Helper function for implementing BUS_PRINT_CHILD().
3513 * This function simply calls bus_print_child_header() followed by
3514 * bus_print_child_footer().
3516 * @returns the number of characters printed
3519 bus_generic_print_child(device_t dev, device_t child)
3523 retval += bus_print_child_header(dev, child);
3524 retval += bus_print_child_footer(dev, child);
3530 * @brief Stub function for implementing BUS_READ_IVAR().
3535 bus_generic_read_ivar(device_t dev, device_t child, int index,
3542 * @brief Stub function for implementing BUS_WRITE_IVAR().
3547 bus_generic_write_ivar(device_t dev, device_t child, int index,
3554 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3558 struct resource_list *
3559 bus_generic_get_resource_list(device_t dev, device_t child)
3565 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3567 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3568 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3569 * and then calls device_probe_and_attach() for each unattached child.
3572 bus_generic_driver_added(device_t dev, driver_t *driver)
3576 DEVICE_IDENTIFY(driver, dev);
3577 TAILQ_FOREACH(child, &dev->children, link) {
3578 if (child->state == DS_NOTPRESENT ||
3579 (child->flags & DF_REBID))
3580 device_probe_and_attach(child);
3585 * @brief Helper function for implementing BUS_NEW_PASS().
3587 * This implementing of BUS_NEW_PASS() first calls the identify
3588 * routines for any drivers that probe at the current pass. Then it
3589 * walks the list of devices for this bus. If a device is already
3590 * attached, then it calls BUS_NEW_PASS() on that device. If the
3591 * device is not already attached, it attempts to attach a driver to
3595 bus_generic_new_pass(device_t dev)
3602 TAILQ_FOREACH(dl, &dc->drivers, link) {
3603 if (dl->pass == bus_current_pass)
3604 DEVICE_IDENTIFY(dl->driver, dev);
3606 TAILQ_FOREACH(child, &dev->children, link) {
3607 if (child->state >= DS_ATTACHED)
3608 BUS_NEW_PASS(child);
3609 else if (child->state == DS_NOTPRESENT)
3610 device_probe_and_attach(child);
3615 * @brief Helper function for implementing BUS_SETUP_INTR().
3617 * This simple implementation of BUS_SETUP_INTR() simply calls the
3618 * BUS_SETUP_INTR() method of the parent of @p dev.
3621 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3622 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3625 /* Propagate up the bus hierarchy until someone handles it. */
3627 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3628 filter, intr, arg, cookiep));
3633 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3635 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3636 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3639 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3642 /* Propagate up the bus hierarchy until someone handles it. */
3644 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3649 * @brief Helper function for implementing BUS_ADJUST_RESOURCE().
3651 * This simple implementation of BUS_ADJUST_RESOURCE() simply calls the
3652 * BUS_ADJUST_RESOURCE() method of the parent of @p dev.
3655 bus_generic_adjust_resource(device_t dev, device_t child, int type,
3656 struct resource *r, u_long start, u_long end)
3658 /* Propagate up the bus hierarchy until someone handles it. */
3660 return (BUS_ADJUST_RESOURCE(dev->parent, child, type, r, start,
3666 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3668 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3669 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3672 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3673 u_long start, u_long end, u_long count, u_int flags)
3675 /* Propagate up the bus hierarchy until someone handles it. */
3677 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3678 start, end, count, flags));
3683 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3685 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3686 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3689 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3692 /* Propagate up the bus hierarchy until someone handles it. */
3694 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3700 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3702 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3703 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3706 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3709 /* Propagate up the bus hierarchy until someone handles it. */
3711 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3717 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3719 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3720 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3723 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3724 int rid, struct resource *r)
3726 /* Propagate up the bus hierarchy until someone handles it. */
3728 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3734 * @brief Helper function for implementing BUS_BIND_INTR().
3736 * This simple implementation of BUS_BIND_INTR() simply calls the
3737 * BUS_BIND_INTR() method of the parent of @p dev.
3740 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3744 /* Propagate up the bus hierarchy until someone handles it. */
3746 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3751 * @brief Helper function for implementing BUS_CONFIG_INTR().
3753 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3754 * BUS_CONFIG_INTR() method of the parent of @p dev.
3757 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3758 enum intr_polarity pol)
3761 /* Propagate up the bus hierarchy until someone handles it. */
3763 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3768 * @brief Helper function for implementing BUS_DESCRIBE_INTR().
3770 * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
3771 * BUS_DESCRIBE_INTR() method of the parent of @p dev.
3774 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
3775 void *cookie, const char *descr)
3778 /* Propagate up the bus hierarchy until someone handles it. */
3780 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
3786 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3788 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3789 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3792 bus_generic_get_dma_tag(device_t dev, device_t child)
3795 /* Propagate up the bus hierarchy until someone handles it. */
3796 if (dev->parent != NULL)
3797 return (BUS_GET_DMA_TAG(dev->parent, child));
3802 * @brief Helper function for implementing BUS_GET_RESOURCE().
3804 * This implementation of BUS_GET_RESOURCE() uses the
3805 * resource_list_find() function to do most of the work. It calls
3806 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3810 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3811 u_long *startp, u_long *countp)
3813 struct resource_list * rl = NULL;
3814 struct resource_list_entry * rle = NULL;
3816 rl = BUS_GET_RESOURCE_LIST(dev, child);
3820 rle = resource_list_find(rl, type, rid);
3825 *startp = rle->start;
3827 *countp = rle->count;
3833 * @brief Helper function for implementing BUS_SET_RESOURCE().
3835 * This implementation of BUS_SET_RESOURCE() uses the
3836 * resource_list_add() function to do most of the work. It calls
3837 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3841 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3842 u_long start, u_long count)
3844 struct resource_list * rl = NULL;
3846 rl = BUS_GET_RESOURCE_LIST(dev, child);
3850 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3856 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3858 * This implementation of BUS_DELETE_RESOURCE() uses the
3859 * resource_list_delete() function to do most of the work. It calls
3860 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3864 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3866 struct resource_list * rl = NULL;
3868 rl = BUS_GET_RESOURCE_LIST(dev, child);
3872 resource_list_delete(rl, type, rid);
3878 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3880 * This implementation of BUS_RELEASE_RESOURCE() uses the
3881 * resource_list_release() function to do most of the work. It calls
3882 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3885 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3886 int rid, struct resource *r)
3888 struct resource_list * rl = NULL;
3890 if (device_get_parent(child) != dev)
3891 return (BUS_RELEASE_RESOURCE(device_get_parent(dev), child,
3894 rl = BUS_GET_RESOURCE_LIST(dev, child);
3898 return (resource_list_release(rl, dev, child, type, rid, r));
3902 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3904 * This implementation of BUS_ALLOC_RESOURCE() uses the
3905 * resource_list_alloc() function to do most of the work. It calls
3906 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3909 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3910 int *rid, u_long start, u_long end, u_long count, u_int flags)
3912 struct resource_list * rl = NULL;
3914 if (device_get_parent(child) != dev)
3915 return (BUS_ALLOC_RESOURCE(device_get_parent(dev), child,
3916 type, rid, start, end, count, flags));
3918 rl = BUS_GET_RESOURCE_LIST(dev, child);
3922 return (resource_list_alloc(rl, dev, child, type, rid,
3923 start, end, count, flags));
3927 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3929 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3930 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3933 bus_generic_child_present(device_t dev, device_t child)
3935 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3939 * Some convenience functions to make it easier for drivers to use the
3940 * resource-management functions. All these really do is hide the
3941 * indirection through the parent's method table, making for slightly
3942 * less-wordy code. In the future, it might make sense for this code
3943 * to maintain some sort of a list of resources allocated by each device.
3947 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3948 struct resource **res)
3952 for (i = 0; rs[i].type != -1; i++)
3954 for (i = 0; rs[i].type != -1; i++) {
3955 res[i] = bus_alloc_resource_any(dev,
3956 rs[i].type, &rs[i].rid, rs[i].flags);
3957 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3958 bus_release_resources(dev, rs, res);
3966 bus_release_resources(device_t dev, const struct resource_spec *rs,
3967 struct resource **res)
3971 for (i = 0; rs[i].type != -1; i++)
3972 if (res[i] != NULL) {
3973 bus_release_resource(
3974 dev, rs[i].type, rs[i].rid, res[i]);
3980 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3982 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3986 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3987 u_long count, u_int flags)
3989 if (dev->parent == NULL)
3991 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3996 * @brief Wrapper function for BUS_ADJUST_RESOURCE().
3998 * This function simply calls the BUS_ADJUST_RESOURCE() method of the
4002 bus_adjust_resource(device_t dev, int type, struct resource *r, u_long start,
4005 if (dev->parent == NULL)
4007 return (BUS_ADJUST_RESOURCE(dev->parent, dev, type, r, start, end));
4011 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
4013 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
4017 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
4019 if (dev->parent == NULL)
4021 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4025 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
4027 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
4031 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
4033 if (dev->parent == NULL)
4035 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
4039 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
4041 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
4045 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
4047 if (dev->parent == NULL)
4049 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
4053 * @brief Wrapper function for BUS_SETUP_INTR().
4055 * This function simply calls the BUS_SETUP_INTR() method of the
4059 bus_setup_intr(device_t dev, struct resource *r, int flags,
4060 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
4064 if (dev->parent == NULL)
4066 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
4070 if (handler != NULL && !(flags & INTR_MPSAFE))
4071 device_printf(dev, "[GIANT-LOCKED]\n");
4076 * @brief Wrapper function for BUS_TEARDOWN_INTR().
4078 * This function simply calls the BUS_TEARDOWN_INTR() method of the
4082 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
4084 if (dev->parent == NULL)
4086 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
4090 * @brief Wrapper function for BUS_BIND_INTR().
4092 * This function simply calls the BUS_BIND_INTR() method of the
4096 bus_bind_intr(device_t dev, struct resource *r, int cpu)
4098 if (dev->parent == NULL)
4100 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
4104 * @brief Wrapper function for BUS_DESCRIBE_INTR().
4106 * This function first formats the requested description into a
4107 * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
4108 * the parent of @p dev.
4111 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
4112 const char *fmt, ...)
4115 char descr[MAXCOMLEN + 1];
4117 if (dev->parent == NULL)
4120 vsnprintf(descr, sizeof(descr), fmt, ap);
4122 return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
4126 * @brief Wrapper function for BUS_SET_RESOURCE().
4128 * This function simply calls the BUS_SET_RESOURCE() method of the
4132 bus_set_resource(device_t dev, int type, int rid,
4133 u_long start, u_long count)
4135 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
4140 * @brief Wrapper function for BUS_GET_RESOURCE().
4142 * This function simply calls the BUS_GET_RESOURCE() method of the
4146 bus_get_resource(device_t dev, int type, int rid,
4147 u_long *startp, u_long *countp)
4149 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4154 * @brief Wrapper function for BUS_GET_RESOURCE().
4156 * This function simply calls the BUS_GET_RESOURCE() method of the
4157 * parent of @p dev and returns the start value.
4160 bus_get_resource_start(device_t dev, int type, int rid)
4162 u_long start, count;
4165 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4173 * @brief Wrapper function for BUS_GET_RESOURCE().
4175 * This function simply calls the BUS_GET_RESOURCE() method of the
4176 * parent of @p dev and returns the count value.
4179 bus_get_resource_count(device_t dev, int type, int rid)
4181 u_long start, count;
4184 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
4192 * @brief Wrapper function for BUS_DELETE_RESOURCE().
4194 * This function simply calls the BUS_DELETE_RESOURCE() method of the
4198 bus_delete_resource(device_t dev, int type, int rid)
4200 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
4204 * @brief Wrapper function for BUS_CHILD_PRESENT().
4206 * This function simply calls the BUS_CHILD_PRESENT() method of the
4210 bus_child_present(device_t child)
4212 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
4216 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
4218 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
4222 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
4226 parent = device_get_parent(child);
4227 if (parent == NULL) {
4231 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
4235 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
4237 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
4241 bus_child_location_str(device_t child, char *buf, size_t buflen)
4245 parent = device_get_parent(child);
4246 if (parent == NULL) {
4250 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
4254 * @brief Wrapper function for BUS_GET_DMA_TAG().
4256 * This function simply calls the BUS_GET_DMA_TAG() method of the
4260 bus_get_dma_tag(device_t dev)
4264 parent = device_get_parent(dev);
4267 return (BUS_GET_DMA_TAG(parent, dev));
4270 /* Resume all devices and then notify userland that we're up again. */
4272 root_resume(device_t dev)
4276 error = bus_generic_resume(dev);
4278 devctl_notify("kern", "power", "resume", NULL);
4283 root_print_child(device_t dev, device_t child)
4287 retval += bus_print_child_header(dev, child);
4288 retval += printf("\n");
4294 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
4295 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
4298 * If an interrupt mapping gets to here something bad has happened.
4300 panic("root_setup_intr");
4304 * If we get here, assume that the device is permanant and really is
4305 * present in the system. Removable bus drivers are expected to intercept
4306 * this call long before it gets here. We return -1 so that drivers that
4307 * really care can check vs -1 or some ERRNO returned higher in the food
4311 root_child_present(device_t dev, device_t child)
4316 static kobj_method_t root_methods[] = {
4317 /* Device interface */
4318 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4319 KOBJMETHOD(device_suspend, bus_generic_suspend),
4320 KOBJMETHOD(device_resume, root_resume),
4323 KOBJMETHOD(bus_print_child, root_print_child),
4324 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4325 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4326 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4327 KOBJMETHOD(bus_child_present, root_child_present),
4332 static driver_t root_driver = {
4339 devclass_t root_devclass;
4342 root_bus_module_handler(module_t mod, int what, void* arg)
4346 TAILQ_INIT(&bus_data_devices);
4347 kobj_class_compile((kobj_class_t) &root_driver);
4348 root_bus = make_device(NULL, "root", 0);
4349 root_bus->desc = "System root bus";
4350 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4351 root_bus->driver = &root_driver;
4352 root_bus->state = DS_ATTACHED;
4353 root_devclass = devclass_find_internal("root", NULL, FALSE);
4358 device_shutdown(root_bus);
4361 return (EOPNOTSUPP);
4367 static moduledata_t root_bus_mod = {
4369 root_bus_module_handler,
4372 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4375 * @brief Automatically configure devices
4377 * This function begins the autoconfiguration process by calling
4378 * device_probe_and_attach() for each child of the @c root0 device.
4381 root_bus_configure(void)
4386 /* Eventually this will be split up, but this is sufficient for now. */
4387 bus_set_pass(BUS_PASS_DEFAULT);
4391 * @brief Module handler for registering device drivers
4393 * This module handler is used to automatically register device
4394 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4395 * devclass_add_driver() for the driver described by the
4396 * driver_module_data structure pointed to by @p arg
4399 driver_module_handler(module_t mod, int what, void *arg)
4401 struct driver_module_data *dmd;
4402 devclass_t bus_devclass;
4403 kobj_class_t driver;
4406 dmd = (struct driver_module_data *)arg;
4407 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4412 if (dmd->dmd_chainevh)
4413 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4415 pass = dmd->dmd_pass;
4416 driver = dmd->dmd_driver;
4417 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4418 DRIVERNAME(driver), dmd->dmd_busname, pass));
4419 error = devclass_add_driver(bus_devclass, driver, pass,
4424 PDEBUG(("Unloading module: driver %s from bus %s",
4425 DRIVERNAME(dmd->dmd_driver),
4427 error = devclass_delete_driver(bus_devclass,
4430 if (!error && dmd->dmd_chainevh)
4431 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4434 PDEBUG(("Quiesce module: driver %s from bus %s",
4435 DRIVERNAME(dmd->dmd_driver),
4437 error = devclass_quiesce_driver(bus_devclass,
4440 if (!error && dmd->dmd_chainevh)
4441 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4452 * @brief Enumerate all hinted devices for this bus.
4454 * Walks through the hints for this bus and calls the bus_hinted_child
4455 * routine for each one it fines. It searches first for the specific
4456 * bus that's being probed for hinted children (eg isa0), and then for
4457 * generic children (eg isa).
4459 * @param dev bus device to enumerate
4462 bus_enumerate_hinted_children(device_t bus)
4465 const char *dname, *busname;
4469 * enumerate all devices on the specific bus
4471 busname = device_get_nameunit(bus);
4473 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4474 BUS_HINTED_CHILD(bus, dname, dunit);
4477 * and all the generic ones.
4479 busname = device_get_name(bus);
4481 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4482 BUS_HINTED_CHILD(bus, dname, dunit);
4487 /* the _short versions avoid iteration by not calling anything that prints
4488 * more than oneliners. I love oneliners.
4492 print_device_short(device_t dev, int indent)
4497 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4498 dev->unit, dev->desc,
4499 (dev->parent? "":"no "),
4500 (TAILQ_EMPTY(&dev->children)? "no ":""),
4501 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4502 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4503 (dev->flags&DF_WILDCARD? "wildcard,":""),
4504 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4505 (dev->flags&DF_REBID? "rebiddable,":""),
4506 (dev->ivars? "":"no "),
4507 (dev->softc? "":"no "),
4512 print_device(device_t dev, int indent)
4517 print_device_short(dev, indent);
4519 indentprintf(("Parent:\n"));
4520 print_device_short(dev->parent, indent+1);
4521 indentprintf(("Driver:\n"));
4522 print_driver_short(dev->driver, indent+1);
4523 indentprintf(("Devclass:\n"));
4524 print_devclass_short(dev->devclass, indent+1);
4528 print_device_tree_short(device_t dev, int indent)
4529 /* print the device and all its children (indented) */
4536 print_device_short(dev, indent);
4538 TAILQ_FOREACH(child, &dev->children, link) {
4539 print_device_tree_short(child, indent+1);
4544 print_device_tree(device_t dev, int indent)
4545 /* print the device and all its children (indented) */
4552 print_device(dev, indent);
4554 TAILQ_FOREACH(child, &dev->children, link) {
4555 print_device_tree(child, indent+1);
4560 print_driver_short(driver_t *driver, int indent)
4565 indentprintf(("driver %s: softc size = %zd\n",
4566 driver->name, driver->size));
4570 print_driver(driver_t *driver, int indent)
4575 print_driver_short(driver, indent);
4580 print_driver_list(driver_list_t drivers, int indent)
4582 driverlink_t driver;
4584 TAILQ_FOREACH(driver, &drivers, link) {
4585 print_driver(driver->driver, indent);
4590 print_devclass_short(devclass_t dc, int indent)
4595 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4599 print_devclass(devclass_t dc, int indent)
4606 print_devclass_short(dc, indent);
4607 indentprintf(("Drivers:\n"));
4608 print_driver_list(dc->drivers, indent+1);
4610 indentprintf(("Devices:\n"));
4611 for (i = 0; i < dc->maxunit; i++)
4613 print_device(dc->devices[i], indent+1);
4617 print_devclass_list_short(void)
4621 printf("Short listing of devclasses, drivers & devices:\n");
4622 TAILQ_FOREACH(dc, &devclasses, link) {
4623 print_devclass_short(dc, 0);
4628 print_devclass_list(void)
4632 printf("Full listing of devclasses, drivers & devices:\n");
4633 TAILQ_FOREACH(dc, &devclasses, link) {
4634 print_devclass(dc, 0);
4641 * User-space access to the device tree.
4643 * We implement a small set of nodes:
4645 * hw.bus Single integer read method to obtain the
4646 * current generation count.
4647 * hw.bus.devices Reads the entire device tree in flat space.
4648 * hw.bus.rman Resource manager interface
4650 * We might like to add the ability to scan devclasses and/or drivers to
4651 * determine what else is currently loaded/available.
4655 sysctl_bus(SYSCTL_HANDLER_ARGS)
4657 struct u_businfo ubus;
4659 ubus.ub_version = BUS_USER_VERSION;
4660 ubus.ub_generation = bus_data_generation;
4662 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4664 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4665 "bus-related data");
4668 sysctl_devices(SYSCTL_HANDLER_ARGS)
4670 int *name = (int *)arg1;
4671 u_int namelen = arg2;
4674 struct u_device udev; /* XXX this is a bit big */
4680 if (bus_data_generation_check(name[0]))
4686 * Scan the list of devices, looking for the requested index.
4688 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4696 * Populate the return array.
4698 bzero(&udev, sizeof(udev));
4699 udev.dv_handle = (uintptr_t)dev;
4700 udev.dv_parent = (uintptr_t)dev->parent;
4701 if (dev->nameunit != NULL)
4702 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4703 if (dev->desc != NULL)
4704 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4705 if (dev->driver != NULL && dev->driver->name != NULL)
4706 strlcpy(udev.dv_drivername, dev->driver->name,
4707 sizeof(udev.dv_drivername));
4708 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4709 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4710 udev.dv_devflags = dev->devflags;
4711 udev.dv_flags = dev->flags;
4712 udev.dv_state = dev->state;
4713 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4717 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4718 "system device tree");
4721 bus_data_generation_check(int generation)
4723 if (generation != bus_data_generation)
4726 /* XXX generate optimised lists here? */
4731 bus_data_generation_update(void)
4733 bus_data_generation++;
4737 bus_free_resource(device_t dev, int type, struct resource *r)
4741 return (bus_release_resource(dev, type, rman_get_rid(r), r));