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/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/queue.h>
45 #include <machine/bus.h>
47 #include <sys/selinfo.h>
48 #include <sys/signalvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
53 #include <sys/interrupt.h>
55 #include <machine/stdarg.h>
59 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
60 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
63 * Used to attach drivers to devclasses.
65 typedef struct driverlink *driverlink_t;
68 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
70 TAILQ_ENTRY(driverlink) passlink;
74 * Forward declarations
76 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
77 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
78 typedef TAILQ_HEAD(device_list, device) device_list_t;
81 TAILQ_ENTRY(devclass) link;
82 devclass_t parent; /* parent in devclass hierarchy */
83 driver_list_t drivers; /* bus devclasses store drivers for bus */
85 device_t *devices; /* array of devices indexed by unit */
86 int maxunit; /* size of devices array */
88 #define DC_HAS_CHILDREN 1
90 struct sysctl_ctx_list sysctl_ctx;
91 struct sysctl_oid *sysctl_tree;
95 * @brief Implementation of device.
99 * A device is a kernel object. The first field must be the
100 * current ops table for the object.
107 TAILQ_ENTRY(device) link; /**< list of devices in parent */
108 TAILQ_ENTRY(device) devlink; /**< global device list membership */
109 device_t parent; /**< parent of this device */
110 device_list_t children; /**< list of child devices */
113 * Details of this device.
115 driver_t *driver; /**< current driver */
116 devclass_t devclass; /**< current device class */
117 int unit; /**< current unit number */
118 char* nameunit; /**< name+unit e.g. foodev0 */
119 char* desc; /**< driver specific description */
120 int busy; /**< count of calls to device_busy() */
121 device_state_t state; /**< current device state */
122 u_int32_t devflags; /**< api level flags for device_get_flags() */
123 u_short flags; /**< internal device flags */
124 #define DF_ENABLED 1 /* device should be probed/attached */
125 #define DF_FIXEDCLASS 2 /* devclass specified at create time */
126 #define DF_WILDCARD 4 /* unit was originally wildcard */
127 #define DF_DESCMALLOCED 8 /* description was malloced */
128 #define DF_QUIET 16 /* don't print verbose attach message */
129 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */
130 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */
131 #define DF_REBID 128 /* Can rebid after attach */
132 u_char order; /**< order from device_add_child_ordered() */
134 void *ivars; /**< instance variables */
135 void *softc; /**< current driver's variables */
137 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
138 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
141 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
142 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
146 static int bus_debug = 1;
147 TUNABLE_INT("bus.debug", &bus_debug);
148 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
151 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
152 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
153 #define DRIVERNAME(d) ((d)? d->name : "no driver")
154 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
157 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
158 * prevent syslog from deleting initial spaces
160 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
162 static void print_device_short(device_t dev, int indent);
163 static void print_device(device_t dev, int indent);
164 void print_device_tree_short(device_t dev, int indent);
165 void print_device_tree(device_t dev, int indent);
166 static void print_driver_short(driver_t *driver, int indent);
167 static void print_driver(driver_t *driver, int indent);
168 static void print_driver_list(driver_list_t drivers, int indent);
169 static void print_devclass_short(devclass_t dc, int indent);
170 static void print_devclass(devclass_t dc, int indent);
171 void print_devclass_list_short(void);
172 void print_devclass_list(void);
175 /* Make the compiler ignore the function calls */
176 #define PDEBUG(a) /* nop */
177 #define DEVICENAME(d) /* nop */
178 #define DRIVERNAME(d) /* nop */
179 #define DEVCLANAME(d) /* nop */
181 #define print_device_short(d,i) /* nop */
182 #define print_device(d,i) /* nop */
183 #define print_device_tree_short(d,i) /* nop */
184 #define print_device_tree(d,i) /* nop */
185 #define print_driver_short(d,i) /* nop */
186 #define print_driver(d,i) /* nop */
187 #define print_driver_list(d,i) /* nop */
188 #define print_devclass_short(d,i) /* nop */
189 #define print_devclass(d,i) /* nop */
190 #define print_devclass_list_short() /* nop */
191 #define print_devclass_list() /* nop */
199 DEVCLASS_SYSCTL_PARENT,
203 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
205 devclass_t dc = (devclass_t)arg1;
209 case DEVCLASS_SYSCTL_PARENT:
210 value = dc->parent ? dc->parent->name : "";
215 return (SYSCTL_OUT(req, value, strlen(value)));
219 devclass_sysctl_init(devclass_t dc)
222 if (dc->sysctl_tree != NULL)
224 sysctl_ctx_init(&dc->sysctl_ctx);
225 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
226 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
227 CTLFLAG_RD, NULL, "");
228 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
229 OID_AUTO, "%parent", CTLFLAG_RD,
230 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
236 DEVICE_SYSCTL_DRIVER,
237 DEVICE_SYSCTL_LOCATION,
238 DEVICE_SYSCTL_PNPINFO,
239 DEVICE_SYSCTL_PARENT,
243 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
245 device_t dev = (device_t)arg1;
252 case DEVICE_SYSCTL_DESC:
253 value = dev->desc ? dev->desc : "";
255 case DEVICE_SYSCTL_DRIVER:
256 value = dev->driver ? dev->driver->name : "";
258 case DEVICE_SYSCTL_LOCATION:
259 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
260 bus_child_location_str(dev, buf, 1024);
262 case DEVICE_SYSCTL_PNPINFO:
263 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
264 bus_child_pnpinfo_str(dev, buf, 1024);
266 case DEVICE_SYSCTL_PARENT:
267 value = dev->parent ? dev->parent->nameunit : "";
272 error = SYSCTL_OUT(req, value, strlen(value));
279 device_sysctl_init(device_t dev)
281 devclass_t dc = dev->devclass;
283 if (dev->sysctl_tree != NULL)
285 devclass_sysctl_init(dc);
286 sysctl_ctx_init(&dev->sysctl_ctx);
287 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
288 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
289 dev->nameunit + strlen(dc->name),
290 CTLFLAG_RD, NULL, "");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%desc", CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
294 "device description");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%driver", CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
298 "device driver name");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%location", CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
302 "device location relative to parent");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
306 "device identification");
307 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
308 OID_AUTO, "%parent", CTLFLAG_RD,
309 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
314 device_sysctl_update(device_t dev)
316 devclass_t dc = dev->devclass;
318 if (dev->sysctl_tree == NULL)
320 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
324 device_sysctl_fini(device_t dev)
326 if (dev->sysctl_tree == NULL)
328 sysctl_ctx_free(&dev->sysctl_ctx);
329 dev->sysctl_tree = NULL;
333 * /dev/devctl implementation
337 * This design allows only one reader for /dev/devctl. This is not desirable
338 * in the long run, but will get a lot of hair out of this implementation.
339 * Maybe we should make this device a clonable device.
341 * Also note: we specifically do not attach a device to the device_t tree
342 * to avoid potential chicken and egg problems. One could argue that all
343 * of this belongs to the root node. One could also further argue that the
344 * sysctl interface that we have not might more properly be an ioctl
345 * interface, but at this stage of the game, I'm not inclined to rock that
348 * I'm also not sure that the SIGIO support is done correctly or not, as
349 * I copied it from a driver that had SIGIO support that likely hasn't been
350 * tested since 3.4 or 2.2.8!
353 /* Deprecated way to adjust queue length */
354 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
355 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
356 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
357 0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
359 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
360 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
361 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
362 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
363 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
364 0, sysctl_devctl_queue, "I", "devctl queue length");
366 static d_open_t devopen;
367 static d_close_t devclose;
368 static d_read_t devread;
369 static d_ioctl_t devioctl;
370 static d_poll_t devpoll;
372 static struct cdevsw dev_cdevsw = {
373 .d_version = D_VERSION,
374 .d_flags = D_NEEDGIANT,
383 struct dev_event_info
386 TAILQ_ENTRY(dev_event_info) dei_link;
389 TAILQ_HEAD(devq, dev_event_info);
391 static struct dev_softc
400 struct proc *async_proc;
403 static struct cdev *devctl_dev;
408 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
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(char *data)
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, M_NOWAIT);
554 mtx_lock(&devsoftc.mtx);
555 if (devctl_queue_length == 0) {
556 free(n1->dei_data, M_BUS);
560 /* Leave at least one spot in the queue... */
561 while (devsoftc.queued > devctl_queue_length - 1) {
562 n2 = TAILQ_FIRST(&devsoftc.devq);
563 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
564 free(n2->dei_data, M_BUS);
568 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
570 cv_broadcast(&devsoftc.cv);
571 mtx_unlock(&devsoftc.mtx);
572 selwakeup(&devsoftc.sel);
573 p = devsoftc.async_proc;
582 * @brief Send a 'notification' to userland, using standard ways
585 devctl_notify(const char *system, const char *subsystem, const char *type,
592 return; /* BOGUS! Must specify system. */
593 if (subsystem == NULL)
594 return; /* BOGUS! Must specify subsystem. */
596 return; /* BOGUS! Must specify type. */
597 len += strlen(" system=") + strlen(system);
598 len += strlen(" subsystem=") + strlen(subsystem);
599 len += strlen(" type=") + strlen(type);
600 /* add in the data message plus newline. */
603 len += 3; /* '!', '\n', and NUL */
604 msg = malloc(len, M_BUS, M_NOWAIT);
606 return; /* Drop it on the floor */
608 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
609 system, subsystem, type, data);
611 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
612 system, subsystem, type);
613 devctl_queue_data(msg);
617 * Common routine that tries to make sending messages as easy as possible.
618 * We allocate memory for the data, copy strings into that, but do not
619 * free it unless there's an error. The dequeue part of the driver should
620 * free the data. We don't send data when the device is disabled. We do
621 * send data, even when we have no listeners, because we wish to avoid
622 * races relating to startup and restart of listening applications.
624 * devaddq is designed to string together the type of event, with the
625 * object of that event, plus the plug and play info and location info
626 * for that event. This is likely most useful for devices, but less
627 * useful for other consumers of this interface. Those should use
628 * the devctl_queue_data() interface instead.
631 devaddq(const char *type, const char *what, device_t dev)
638 if (!devctl_queue_length)/* Rare race, but lost races safely discard */
640 data = malloc(1024, M_BUS, M_NOWAIT);
644 /* get the bus specific location of this device */
645 loc = malloc(1024, M_BUS, M_NOWAIT);
649 bus_child_location_str(dev, loc, 1024);
651 /* Get the bus specific pnp info of this device */
652 pnp = malloc(1024, M_BUS, M_NOWAIT);
656 bus_child_pnpinfo_str(dev, pnp, 1024);
658 /* Get the parent of this device, or / if high enough in the tree. */
659 if (device_get_parent(dev) == NULL)
660 parstr = "."; /* Or '/' ? */
662 parstr = device_get_nameunit(device_get_parent(dev));
663 /* String it all together. */
664 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
668 devctl_queue_data(data);
678 * A device was added to the tree. We are called just after it successfully
679 * attaches (that is, probe and attach success for this device). No call
680 * is made if a device is merely parented into the tree. See devnomatch
681 * if probe fails. If attach fails, no notification is sent (but maybe
682 * we should have a different message for this).
685 devadded(device_t dev)
690 pnp = malloc(1024, M_BUS, M_NOWAIT);
693 tmp = malloc(1024, M_BUS, M_NOWAIT);
697 bus_child_pnpinfo_str(dev, pnp, 1024);
698 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
699 devaddq("+", tmp, dev);
709 * A device was removed from the tree. We are called just before this
713 devremoved(device_t dev)
718 pnp = malloc(1024, M_BUS, M_NOWAIT);
721 tmp = malloc(1024, M_BUS, M_NOWAIT);
725 bus_child_pnpinfo_str(dev, pnp, 1024);
726 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
727 devaddq("-", tmp, dev);
737 * Called when there's no match for this device. This is only called
738 * the first time that no match happens, so we don't keep getting this
739 * message. Should that prove to be undesirable, we can change it.
740 * This is called when all drivers that can attach to a given bus
741 * decline to accept this device. Other errrors may not be detected.
744 devnomatch(device_t dev)
746 devaddq("?", "", dev);
750 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
752 struct dev_event_info *n1;
755 dis = devctl_queue_length == 0;
756 error = sysctl_handle_int(oidp, &dis, 0, req);
757 if (error || !req->newptr)
759 mtx_lock(&devsoftc.mtx);
761 while (!TAILQ_EMPTY(&devsoftc.devq)) {
762 n1 = TAILQ_FIRST(&devsoftc.devq);
763 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
764 free(n1->dei_data, M_BUS);
768 devctl_queue_length = 0;
770 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
772 mtx_unlock(&devsoftc.mtx);
777 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
779 struct dev_event_info *n1;
782 q = devctl_queue_length;
783 error = sysctl_handle_int(oidp, &q, 0, req);
784 if (error || !req->newptr)
788 mtx_lock(&devsoftc.mtx);
789 devctl_queue_length = q;
790 while (devsoftc.queued > devctl_queue_length) {
791 n1 = TAILQ_FIRST(&devsoftc.devq);
792 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
793 free(n1->dei_data, M_BUS);
797 mtx_unlock(&devsoftc.mtx);
801 /* End of /dev/devctl code */
803 static TAILQ_HEAD(,device) bus_data_devices;
804 static int bus_data_generation = 1;
806 static kobj_method_t null_methods[] = {
810 DEFINE_CLASS(null, null_methods, 0);
813 * Bus pass implementation
816 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
817 int bus_current_pass = BUS_PASS_ROOT;
821 * @brief Register the pass level of a new driver attachment
823 * Register a new driver attachment's pass level. If no driver
824 * attachment with the same pass level has been added, then @p new
825 * will be added to the global passes list.
827 * @param new the new driver attachment
830 driver_register_pass(struct driverlink *new)
832 struct driverlink *dl;
834 /* We only consider pass numbers during boot. */
835 if (bus_current_pass == BUS_PASS_DEFAULT)
839 * Walk the passes list. If we already know about this pass
840 * then there is nothing to do. If we don't, then insert this
841 * driver link into the list.
843 TAILQ_FOREACH(dl, &passes, passlink) {
844 if (dl->pass < new->pass)
846 if (dl->pass == new->pass)
848 TAILQ_INSERT_BEFORE(dl, new, passlink);
851 TAILQ_INSERT_TAIL(&passes, new, passlink);
855 * @brief Raise the current bus pass
857 * Raise the current bus pass level to @p pass. Call the BUS_NEW_PASS()
858 * method on the root bus to kick off a new device tree scan for each
859 * new pass level that has at least one driver.
862 bus_set_pass(int pass)
864 struct driverlink *dl;
866 if (bus_current_pass > pass)
867 panic("Attempt to lower bus pass level");
869 TAILQ_FOREACH(dl, &passes, passlink) {
870 /* Skip pass values below the current pass level. */
871 if (dl->pass <= bus_current_pass)
875 * Bail once we hit a driver with a pass level that is
882 * Raise the pass level to the next level and rescan
885 bus_current_pass = dl->pass;
886 BUS_NEW_PASS(root_bus);
890 * If there isn't a driver registered for the requested pass,
891 * then bus_current_pass might still be less than 'pass'. Set
892 * it to 'pass' in that case.
894 if (bus_current_pass < pass)
895 bus_current_pass = pass;
896 KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
900 * Devclass implementation
903 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
907 * @brief Find or create a device class
909 * If a device class with the name @p classname exists, return it,
910 * otherwise if @p create is non-zero create and return a new device
913 * If @p parentname is non-NULL, the parent of the devclass is set to
914 * the devclass of that name.
916 * @param classname the devclass name to find or create
917 * @param parentname the parent devclass name or @c NULL
918 * @param create non-zero to create a devclass
921 devclass_find_internal(const char *classname, const char *parentname,
926 PDEBUG(("looking for %s", classname));
930 TAILQ_FOREACH(dc, &devclasses, link) {
931 if (!strcmp(dc->name, classname))
936 PDEBUG(("creating %s", classname));
937 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
938 M_BUS, M_NOWAIT | M_ZERO);
942 dc->name = (char*) (dc + 1);
943 strcpy(dc->name, classname);
944 TAILQ_INIT(&dc->drivers);
945 TAILQ_INSERT_TAIL(&devclasses, dc, link);
947 bus_data_generation_update();
951 * If a parent class is specified, then set that as our parent so
952 * that this devclass will support drivers for the parent class as
953 * well. If the parent class has the same name don't do this though
954 * as it creates a cycle that can trigger an infinite loop in
955 * device_probe_child() if a device exists for which there is no
958 if (parentname && dc && !dc->parent &&
959 strcmp(classname, parentname) != 0) {
960 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
961 dc->parent->flags |= DC_HAS_CHILDREN;
968 * @brief Create a device class
970 * If a device class with the name @p classname exists, return it,
971 * otherwise create and return a new device class.
973 * @param classname the devclass name to find or create
976 devclass_create(const char *classname)
978 return (devclass_find_internal(classname, NULL, TRUE));
982 * @brief Find a device class
984 * If a device class with the name @p classname exists, return it,
985 * otherwise return @c NULL.
987 * @param classname the devclass name to find
990 devclass_find(const char *classname)
992 return (devclass_find_internal(classname, NULL, FALSE));
996 * @brief Register that a device driver has been added to a devclass
998 * Register that a device driver has been added to a devclass. This
999 * is called by devclass_add_driver to accomplish the recursive
1000 * notification of all the children classes of dc, as well as dc.
1001 * Each layer will have BUS_DRIVER_ADDED() called for all instances of
1002 * the devclass. We do a full search here of the devclass list at
1003 * each iteration level to save storing children-lists in the devclass
1004 * structure. If we ever move beyond a few dozen devices doing this,
1005 * we may need to reevaluate...
1007 * @param dc the devclass to edit
1008 * @param driver the driver that was just added
1011 devclass_driver_added(devclass_t dc, driver_t *driver)
1017 * Call BUS_DRIVER_ADDED for any existing busses in this class.
1019 for (i = 0; i < dc->maxunit; i++)
1020 if (dc->devices[i] && device_is_attached(dc->devices[i]))
1021 BUS_DRIVER_ADDED(dc->devices[i], driver);
1024 * Walk through the children classes. Since we only keep a
1025 * single parent pointer around, we walk the entire list of
1026 * devclasses looking for children. We set the
1027 * DC_HAS_CHILDREN flag when a child devclass is created on
1028 * the parent, so we only walk the list for those devclasses
1029 * that have children.
1031 if (!(dc->flags & DC_HAS_CHILDREN))
1034 TAILQ_FOREACH(dc, &devclasses, link) {
1035 if (dc->parent == parent)
1036 devclass_driver_added(dc, driver);
1041 * @brief Add a device driver to a device class
1043 * Add a device driver to a devclass. This is normally called
1044 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
1045 * all devices in the devclass will be called to allow them to attempt
1046 * to re-probe any unmatched children.
1048 * @param dc the devclass to edit
1049 * @param driver the driver to register
1052 devclass_add_driver(devclass_t dc, driver_t *driver, int pass)
1056 PDEBUG(("%s", DRIVERNAME(driver)));
1058 /* Don't allow invalid pass values. */
1059 if (pass <= BUS_PASS_ROOT)
1062 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
1067 * Compile the driver's methods. Also increase the reference count
1068 * so that the class doesn't get freed when the last instance
1069 * goes. This means we can safely use static methods and avoids a
1070 * double-free in devclass_delete_driver.
1072 kobj_class_compile((kobj_class_t) driver);
1075 * Make sure the devclass which the driver is implementing exists.
1077 devclass_find_internal(driver->name, NULL, TRUE);
1079 dl->driver = driver;
1080 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
1081 driver->refs++; /* XXX: kobj_mtx */
1083 driver_register_pass(dl);
1085 devclass_driver_added(dc, driver);
1086 bus_data_generation_update();
1091 * @brief Delete a device driver from a device class
1093 * Delete a device driver from a devclass. This is normally called
1094 * automatically by DRIVER_MODULE().
1096 * If the driver is currently attached to any devices,
1097 * devclass_delete_driver() will first attempt to detach from each
1098 * device. If one of the detach calls fails, the driver will not be
1101 * @param dc the devclass to edit
1102 * @param driver the driver to unregister
1105 devclass_delete_driver(devclass_t busclass, driver_t *driver)
1107 devclass_t dc = devclass_find(driver->name);
1113 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1119 * Find the link structure in the bus' list of drivers.
1121 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1122 if (dl->driver == driver)
1127 PDEBUG(("%s not found in %s list", driver->name,
1133 * Disassociate from any devices. We iterate through all the
1134 * devices in the devclass of the driver and detach any which are
1135 * using the driver and which have a parent in the devclass which
1136 * we are deleting from.
1138 * Note that since a driver can be in multiple devclasses, we
1139 * should not detach devices which are not children of devices in
1140 * the affected devclass.
1142 for (i = 0; i < dc->maxunit; i++) {
1143 if (dc->devices[i]) {
1144 dev = dc->devices[i];
1145 if (dev->driver == driver && dev->parent &&
1146 dev->parent->devclass == busclass) {
1147 if ((error = device_detach(dev)) != 0)
1149 device_set_driver(dev, NULL);
1154 TAILQ_REMOVE(&busclass->drivers, dl, link);
1159 if (driver->refs == 0)
1160 kobj_class_free((kobj_class_t) driver);
1162 bus_data_generation_update();
1167 * @brief Quiesces a set of device drivers from a device class
1169 * Quiesce a device driver from a devclass. This is normally called
1170 * automatically by DRIVER_MODULE().
1172 * If the driver is currently attached to any devices,
1173 * devclass_quiesece_driver() will first attempt to quiesce each
1176 * @param dc the devclass to edit
1177 * @param driver the driver to unregister
1180 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
1182 devclass_t dc = devclass_find(driver->name);
1188 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
1194 * Find the link structure in the bus' list of drivers.
1196 TAILQ_FOREACH(dl, &busclass->drivers, link) {
1197 if (dl->driver == driver)
1202 PDEBUG(("%s not found in %s list", driver->name,
1208 * Quiesce all devices. We iterate through all the devices in
1209 * the devclass of the driver and quiesce any which are using
1210 * the driver and which have a parent in the devclass which we
1213 * Note that since a driver can be in multiple devclasses, we
1214 * should not quiesce devices which are not children of
1215 * devices in the affected devclass.
1217 for (i = 0; i < dc->maxunit; i++) {
1218 if (dc->devices[i]) {
1219 dev = dc->devices[i];
1220 if (dev->driver == driver && dev->parent &&
1221 dev->parent->devclass == busclass) {
1222 if ((error = device_quiesce(dev)) != 0)
1235 devclass_find_driver_internal(devclass_t dc, const char *classname)
1239 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1241 TAILQ_FOREACH(dl, &dc->drivers, link) {
1242 if (!strcmp(dl->driver->name, classname))
1246 PDEBUG(("not found"));
1251 * @brief Return the name of the devclass
1254 devclass_get_name(devclass_t dc)
1260 * @brief Find a device given a unit number
1262 * @param dc the devclass to search
1263 * @param unit the unit number to search for
1265 * @returns the device with the given unit number or @c
1266 * NULL if there is no such device
1269 devclass_get_device(devclass_t dc, int unit)
1271 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1273 return (dc->devices[unit]);
1277 * @brief Find the softc field of a device given a unit number
1279 * @param dc the devclass to search
1280 * @param unit the unit number to search for
1282 * @returns the softc field of the device with the given
1283 * unit number or @c NULL if there is no such
1287 devclass_get_softc(devclass_t dc, int unit)
1291 dev = devclass_get_device(dc, unit);
1295 return (device_get_softc(dev));
1299 * @brief Get a list of devices in the devclass
1301 * An array containing a list of all the devices in the given devclass
1302 * is allocated and returned in @p *devlistp. The number of devices
1303 * in the array is returned in @p *devcountp. The caller should free
1304 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1306 * @param dc the devclass to examine
1307 * @param devlistp points at location for array pointer return
1309 * @param devcountp points at location for array size return value
1312 * @retval ENOMEM the array allocation failed
1315 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1320 count = devclass_get_count(dc);
1321 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1326 for (i = 0; i < dc->maxunit; i++) {
1327 if (dc->devices[i]) {
1328 list[count] = dc->devices[i];
1340 * @brief Get a list of drivers in the devclass
1342 * An array containing a list of pointers to all the drivers in the
1343 * given devclass is allocated and returned in @p *listp. The number
1344 * of drivers in the array is returned in @p *countp. The caller should
1345 * free the array using @c free(p, M_TEMP).
1347 * @param dc the devclass to examine
1348 * @param listp gives location for array pointer return value
1349 * @param countp gives location for number of array elements
1353 * @retval ENOMEM the array allocation failed
1356 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1363 TAILQ_FOREACH(dl, &dc->drivers, link)
1365 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1370 TAILQ_FOREACH(dl, &dc->drivers, link) {
1371 list[count] = dl->driver;
1381 * @brief Get the number of devices in a devclass
1383 * @param dc the devclass to examine
1386 devclass_get_count(devclass_t dc)
1391 for (i = 0; i < dc->maxunit; i++)
1398 * @brief Get the maximum unit number used in a devclass
1400 * Note that this is one greater than the highest currently-allocated
1401 * unit. If a null devclass_t is passed in, -1 is returned to indicate
1402 * that not even the devclass has been allocated yet.
1404 * @param dc the devclass to examine
1407 devclass_get_maxunit(devclass_t dc)
1411 return (dc->maxunit);
1415 * @brief Find a free unit number in a devclass
1417 * This function searches for the first unused unit number greater
1418 * that or equal to @p unit.
1420 * @param dc the devclass to examine
1421 * @param unit the first unit number to check
1424 devclass_find_free_unit(devclass_t dc, int unit)
1428 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1434 * @brief Set the parent of a devclass
1436 * The parent class is normally initialised automatically by
1439 * @param dc the devclass to edit
1440 * @param pdc the new parent devclass
1443 devclass_set_parent(devclass_t dc, devclass_t pdc)
1449 * @brief Get the parent of a devclass
1451 * @param dc the devclass to examine
1454 devclass_get_parent(devclass_t dc)
1456 return (dc->parent);
1459 struct sysctl_ctx_list *
1460 devclass_get_sysctl_ctx(devclass_t dc)
1462 return (&dc->sysctl_ctx);
1466 devclass_get_sysctl_tree(devclass_t dc)
1468 return (dc->sysctl_tree);
1473 * @brief Allocate a unit number
1475 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1476 * will do). The allocated unit number is returned in @p *unitp.
1478 * @param dc the devclass to allocate from
1479 * @param unitp points at the location for the allocated unit
1483 * @retval EEXIST the requested unit number is already allocated
1484 * @retval ENOMEM memory allocation failure
1487 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
1492 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1494 /* Ask the parent bus if it wants to wire this device. */
1496 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
1499 /* If we were given a wired unit number, check for existing device */
1502 if (unit >= 0 && unit < dc->maxunit &&
1503 dc->devices[unit] != NULL) {
1505 printf("%s: %s%d already exists; skipping it\n",
1506 dc->name, dc->name, *unitp);
1510 /* Unwired device, find the next available slot for it */
1512 for (unit = 0;; unit++) {
1513 /* If there is an "at" hint for a unit then skip it. */
1514 if (resource_string_value(dc->name, unit, "at", &s) ==
1518 /* If this device slot is already in use, skip it. */
1519 if (unit < dc->maxunit && dc->devices[unit] != NULL)
1527 * We've selected a unit beyond the length of the table, so let's
1528 * extend the table to make room for all units up to and including
1531 if (unit >= dc->maxunit) {
1532 device_t *newlist, *oldlist;
1535 oldlist = dc->devices;
1536 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1537 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1540 if (oldlist != NULL)
1541 bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
1542 bzero(newlist + dc->maxunit,
1543 sizeof(device_t) * (newsize - dc->maxunit));
1544 dc->devices = newlist;
1545 dc->maxunit = newsize;
1546 if (oldlist != NULL)
1547 free(oldlist, M_BUS);
1549 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1557 * @brief Add a device to a devclass
1559 * A unit number is allocated for the device (using the device's
1560 * preferred unit number if any) and the device is registered in the
1561 * devclass. This allows the device to be looked up by its unit
1562 * number, e.g. by decoding a dev_t minor number.
1564 * @param dc the devclass to add to
1565 * @param dev the device to add
1568 * @retval EEXIST the requested unit number is already allocated
1569 * @retval ENOMEM memory allocation failure
1572 devclass_add_device(devclass_t dc, device_t dev)
1576 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1578 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1581 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1585 if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
1586 free(dev->nameunit, M_BUS);
1587 dev->nameunit = NULL;
1590 dc->devices[dev->unit] = dev;
1592 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1599 * @brief Delete a device from a devclass
1601 * The device is removed from the devclass's device list and its unit
1604 * @param dc the devclass to delete from
1605 * @param dev the device to delete
1610 devclass_delete_device(devclass_t dc, device_t dev)
1615 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1617 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1618 panic("devclass_delete_device: inconsistent device class");
1619 dc->devices[dev->unit] = NULL;
1620 if (dev->flags & DF_WILDCARD)
1622 dev->devclass = NULL;
1623 free(dev->nameunit, M_BUS);
1624 dev->nameunit = NULL;
1631 * @brief Make a new device and add it as a child of @p parent
1633 * @param parent the parent of the new device
1634 * @param name the devclass name of the new device or @c NULL
1635 * to leave the devclass unspecified
1636 * @parem unit the unit number of the new device of @c -1 to
1637 * leave the unit number unspecified
1639 * @returns the new device
1642 make_device(device_t parent, const char *name, int unit)
1647 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1650 dc = devclass_find_internal(name, NULL, TRUE);
1652 printf("make_device: can't find device class %s\n",
1660 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1664 dev->parent = parent;
1665 TAILQ_INIT(&dev->children);
1666 kobj_init((kobj_t) dev, &null_class);
1668 dev->devclass = NULL;
1670 dev->nameunit = NULL;
1674 dev->flags = DF_ENABLED;
1677 dev->flags |= DF_WILDCARD;
1679 dev->flags |= DF_FIXEDCLASS;
1680 if (devclass_add_device(dc, dev)) {
1681 kobj_delete((kobj_t) dev, M_BUS);
1688 dev->state = DS_NOTPRESENT;
1690 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1691 bus_data_generation_update();
1698 * @brief Print a description of a device.
1701 device_print_child(device_t dev, device_t child)
1705 if (device_is_alive(child))
1706 retval += BUS_PRINT_CHILD(dev, child);
1708 retval += device_printf(child, " not found\n");
1714 * @brief Create a new device
1716 * This creates a new device and adds it as a child of an existing
1717 * parent device. The new device will be added after the last existing
1718 * child with order zero.
1720 * @param dev the device which will be the parent of the
1722 * @param name devclass name for new device or @c NULL if not
1724 * @param unit unit number for new device or @c -1 if not
1727 * @returns the new device
1730 device_add_child(device_t dev, const char *name, int unit)
1732 return (device_add_child_ordered(dev, 0, name, unit));
1736 * @brief Create a new device
1738 * This creates a new device and adds it as a child of an existing
1739 * parent device. The new device will be added after the last existing
1740 * child with the same order.
1742 * @param dev the device which will be the parent of the
1744 * @param order a value which is used to partially sort the
1745 * children of @p dev - devices created using
1746 * lower values of @p order appear first in @p
1747 * dev's list of children
1748 * @param name devclass name for new device or @c NULL if not
1750 * @param unit unit number for new device or @c -1 if not
1753 * @returns the new device
1756 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1761 PDEBUG(("%s at %s with order %d as unit %d",
1762 name, DEVICENAME(dev), order, unit));
1764 child = make_device(dev, name, unit);
1767 child->order = order;
1769 TAILQ_FOREACH(place, &dev->children, link) {
1770 if (place->order > order)
1776 * The device 'place' is the first device whose order is
1777 * greater than the new child.
1779 TAILQ_INSERT_BEFORE(place, child, link);
1782 * The new child's order is greater or equal to the order of
1783 * any existing device. Add the child to the tail of the list.
1785 TAILQ_INSERT_TAIL(&dev->children, child, link);
1788 bus_data_generation_update();
1793 * @brief Delete a device
1795 * This function deletes a device along with all of its children. If
1796 * the device currently has a driver attached to it, the device is
1797 * detached first using device_detach().
1799 * @param dev the parent device
1800 * @param child the device to delete
1803 * @retval non-zero a unit error code describing the error
1806 device_delete_child(device_t dev, device_t child)
1809 device_t grandchild;
1811 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1813 /* remove children first */
1814 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1815 error = device_delete_child(child, grandchild);
1820 if ((error = device_detach(child)) != 0)
1822 if (child->devclass)
1823 devclass_delete_device(child->devclass, child);
1824 TAILQ_REMOVE(&dev->children, child, link);
1825 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1826 kobj_delete((kobj_t) child, M_BUS);
1828 bus_data_generation_update();
1833 * @brief Find a device given a unit number
1835 * This is similar to devclass_get_devices() but only searches for
1836 * devices which have @p dev as a parent.
1838 * @param dev the parent device to search
1839 * @param unit the unit number to search for. If the unit is -1,
1840 * return the first child of @p dev which has name
1841 * @p classname (that is, the one with the lowest unit.)
1843 * @returns the device with the given unit number or @c
1844 * NULL if there is no such device
1847 device_find_child(device_t dev, const char *classname, int unit)
1852 dc = devclass_find(classname);
1857 child = devclass_get_device(dc, unit);
1858 if (child && child->parent == dev)
1861 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1862 child = devclass_get_device(dc, unit);
1863 if (child && child->parent == dev)
1874 first_matching_driver(devclass_t dc, device_t dev)
1877 return (devclass_find_driver_internal(dc, dev->devclass->name));
1878 return (TAILQ_FIRST(&dc->drivers));
1885 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1887 if (dev->devclass) {
1889 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1890 if (!strcmp(dev->devclass->name, dl->driver->name))
1894 return (TAILQ_NEXT(last, link));
1901 device_probe_child(device_t dev, device_t child)
1904 driverlink_t best = NULL;
1906 int result, pri = 0;
1907 int hasclass = (child->devclass != NULL);
1913 panic("device_probe_child: parent device has no devclass");
1916 * If the state is already probed, then return. However, don't
1917 * return if we can rebid this object.
1919 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1922 for (; dc; dc = dc->parent) {
1923 for (dl = first_matching_driver(dc, child);
1925 dl = next_matching_driver(dc, child, dl)) {
1927 /* If this driver's pass is too high, then ignore it. */
1928 if (dl->pass > bus_current_pass)
1931 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1932 device_set_driver(child, dl->driver);
1934 if (device_set_devclass(child, dl->driver->name)) {
1935 printf("driver bug: Unable to set devclass (devname: %s)\n",
1936 (child ? device_get_name(child) :
1938 device_set_driver(child, NULL);
1943 /* Fetch any flags for the device before probing. */
1944 resource_int_value(dl->driver->name, child->unit,
1945 "flags", &child->devflags);
1947 result = DEVICE_PROBE(child);
1949 /* Reset flags and devclass before the next probe. */
1950 child->devflags = 0;
1952 device_set_devclass(child, NULL);
1955 * If the driver returns SUCCESS, there can be
1956 * no higher match for this device.
1965 * The driver returned an error so it
1966 * certainly doesn't match.
1969 device_set_driver(child, NULL);
1974 * A priority lower than SUCCESS, remember the
1975 * best matching driver. Initialise the value
1976 * of pri for the first match.
1978 if (best == NULL || result > pri) {
1980 * Probes that return BUS_PROBE_NOWILDCARD
1981 * or lower only match when they are set
1982 * in stone by the parent bus.
1984 if (result <= BUS_PROBE_NOWILDCARD &&
1985 child->flags & DF_WILDCARD)
1993 * If we have an unambiguous match in this devclass,
1994 * don't look in the parent.
1996 if (best && pri == 0)
2001 * If we found a driver, change state and initialise the devclass.
2003 /* XXX What happens if we rebid and got no best? */
2006 * If this device was atached, and we were asked to
2007 * rescan, and it is a different driver, then we have
2008 * to detach the old driver and reattach this new one.
2009 * Note, we don't have to check for DF_REBID here
2010 * because if the state is > DS_ALIVE, we know it must
2013 * This assumes that all DF_REBID drivers can have
2014 * their probe routine called at any time and that
2015 * they are idempotent as well as completely benign in
2016 * normal operations.
2018 * We also have to make sure that the detach
2019 * succeeded, otherwise we fail the operation (or
2020 * maybe it should just fail silently? I'm torn).
2022 if (child->state > DS_ALIVE && best->driver != child->driver)
2023 if ((result = device_detach(dev)) != 0)
2026 /* Set the winning driver, devclass, and flags. */
2027 if (!child->devclass) {
2028 result = device_set_devclass(child, best->driver->name);
2032 device_set_driver(child, best->driver);
2033 resource_int_value(best->driver->name, child->unit,
2034 "flags", &child->devflags);
2038 * A bit bogus. Call the probe method again to make
2039 * sure that we have the right description.
2041 DEVICE_PROBE(child);
2043 child->flags |= DF_REBID;
2046 child->flags &= ~DF_REBID;
2047 child->state = DS_ALIVE;
2049 bus_data_generation_update();
2057 * @brief Return the parent of a device
2060 device_get_parent(device_t dev)
2062 return (dev->parent);
2066 * @brief Get a list of children of a device
2068 * An array containing a list of all the children of the given device
2069 * is allocated and returned in @p *devlistp. The number of devices
2070 * in the array is returned in @p *devcountp. The caller should free
2071 * the array using @c free(p, M_TEMP).
2073 * @param dev the device to examine
2074 * @param devlistp points at location for array pointer return
2076 * @param devcountp points at location for array size return value
2079 * @retval ENOMEM the array allocation failed
2082 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
2089 TAILQ_FOREACH(child, &dev->children, link) {
2093 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
2098 TAILQ_FOREACH(child, &dev->children, link) {
2099 list[count] = child;
2110 * @brief Return the current driver for the device or @c NULL if there
2111 * is no driver currently attached
2114 device_get_driver(device_t dev)
2116 return (dev->driver);
2120 * @brief Return the current devclass for the device or @c NULL if
2124 device_get_devclass(device_t dev)
2126 return (dev->devclass);
2130 * @brief Return the name of the device's devclass or @c NULL if there
2134 device_get_name(device_t dev)
2136 if (dev != NULL && dev->devclass)
2137 return (devclass_get_name(dev->devclass));
2142 * @brief Return a string containing the device's devclass name
2143 * followed by an ascii representation of the device's unit number
2147 device_get_nameunit(device_t dev)
2149 return (dev->nameunit);
2153 * @brief Return the device's unit number.
2156 device_get_unit(device_t dev)
2162 * @brief Return the device's description string
2165 device_get_desc(device_t dev)
2171 * @brief Return the device's flags
2174 device_get_flags(device_t dev)
2176 return (dev->devflags);
2179 struct sysctl_ctx_list *
2180 device_get_sysctl_ctx(device_t dev)
2182 return (&dev->sysctl_ctx);
2186 device_get_sysctl_tree(device_t dev)
2188 return (dev->sysctl_tree);
2192 * @brief Print the name of the device followed by a colon and a space
2194 * @returns the number of characters printed
2197 device_print_prettyname(device_t dev)
2199 const char *name = device_get_name(dev);
2202 return (printf("unknown: "));
2203 return (printf("%s%d: ", name, device_get_unit(dev)));
2207 * @brief Print the name of the device followed by a colon, a space
2208 * and the result of calling vprintf() with the value of @p fmt and
2209 * the following arguments.
2211 * @returns the number of characters printed
2214 device_printf(device_t dev, const char * fmt, ...)
2219 retval = device_print_prettyname(dev);
2221 retval += vprintf(fmt, ap);
2230 device_set_desc_internal(device_t dev, const char* desc, int copy)
2232 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2233 free(dev->desc, M_BUS);
2234 dev->flags &= ~DF_DESCMALLOCED;
2239 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2241 strcpy(dev->desc, desc);
2242 dev->flags |= DF_DESCMALLOCED;
2245 /* Avoid a -Wcast-qual warning */
2246 dev->desc = (char *)(uintptr_t) desc;
2249 bus_data_generation_update();
2253 * @brief Set the device's description
2255 * The value of @c desc should be a string constant that will not
2256 * change (at least until the description is changed in a subsequent
2257 * call to device_set_desc() or device_set_desc_copy()).
2260 device_set_desc(device_t dev, const char* desc)
2262 device_set_desc_internal(dev, desc, FALSE);
2266 * @brief Set the device's description
2268 * The string pointed to by @c desc is copied. Use this function if
2269 * the device description is generated, (e.g. with sprintf()).
2272 device_set_desc_copy(device_t dev, const char* desc)
2274 device_set_desc_internal(dev, desc, TRUE);
2278 * @brief Set the device's flags
2281 device_set_flags(device_t dev, u_int32_t flags)
2283 dev->devflags = flags;
2287 * @brief Return the device's softc field
2289 * The softc is allocated and zeroed when a driver is attached, based
2290 * on the size field of the driver.
2293 device_get_softc(device_t dev)
2295 return (dev->softc);
2299 * @brief Set the device's softc field
2301 * Most drivers do not need to use this since the softc is allocated
2302 * automatically when the driver is attached.
2305 device_set_softc(device_t dev, void *softc)
2307 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2308 free(dev->softc, M_BUS_SC);
2311 dev->flags |= DF_EXTERNALSOFTC;
2313 dev->flags &= ~DF_EXTERNALSOFTC;
2317 * @brief Get the device's ivars field
2319 * The ivars field is used by the parent device to store per-device
2320 * state (e.g. the physical location of the device or a list of
2324 device_get_ivars(device_t dev)
2327 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2328 return (dev->ivars);
2332 * @brief Set the device's ivars field
2335 device_set_ivars(device_t dev, void * ivars)
2338 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2343 * @brief Return the device's state
2346 device_get_state(device_t dev)
2348 return (dev->state);
2352 * @brief Set the DF_ENABLED flag for the device
2355 device_enable(device_t dev)
2357 dev->flags |= DF_ENABLED;
2361 * @brief Clear the DF_ENABLED flag for the device
2364 device_disable(device_t dev)
2366 dev->flags &= ~DF_ENABLED;
2370 * @brief Increment the busy counter for the device
2373 device_busy(device_t dev)
2375 if (dev->state < DS_ATTACHED)
2376 panic("device_busy: called for unattached device");
2377 if (dev->busy == 0 && dev->parent)
2378 device_busy(dev->parent);
2380 dev->state = DS_BUSY;
2384 * @brief Decrement the busy counter for the device
2387 device_unbusy(device_t dev)
2389 if (dev->state != DS_BUSY)
2390 panic("device_unbusy: called for non-busy device %s",
2391 device_get_nameunit(dev));
2393 if (dev->busy == 0) {
2395 device_unbusy(dev->parent);
2396 dev->state = DS_ATTACHED;
2401 * @brief Set the DF_QUIET flag for the device
2404 device_quiet(device_t dev)
2406 dev->flags |= DF_QUIET;
2410 * @brief Clear the DF_QUIET flag for the device
2413 device_verbose(device_t dev)
2415 dev->flags &= ~DF_QUIET;
2419 * @brief Return non-zero if the DF_QUIET flag is set on the device
2422 device_is_quiet(device_t dev)
2424 return ((dev->flags & DF_QUIET) != 0);
2428 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2431 device_is_enabled(device_t dev)
2433 return ((dev->flags & DF_ENABLED) != 0);
2437 * @brief Return non-zero if the device was successfully probed
2440 device_is_alive(device_t dev)
2442 return (dev->state >= DS_ALIVE);
2446 * @brief Return non-zero if the device currently has a driver
2450 device_is_attached(device_t dev)
2452 return (dev->state >= DS_ATTACHED);
2456 * @brief Set the devclass of a device
2457 * @see devclass_add_device().
2460 device_set_devclass(device_t dev, const char *classname)
2467 devclass_delete_device(dev->devclass, dev);
2471 if (dev->devclass) {
2472 printf("device_set_devclass: device class already set\n");
2476 dc = devclass_find_internal(classname, NULL, TRUE);
2480 error = devclass_add_device(dc, dev);
2482 bus_data_generation_update();
2487 * @brief Set the driver of a device
2490 * @retval EBUSY the device already has a driver attached
2491 * @retval ENOMEM a memory allocation failure occurred
2494 device_set_driver(device_t dev, driver_t *driver)
2496 if (dev->state >= DS_ATTACHED)
2499 if (dev->driver == driver)
2502 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2503 free(dev->softc, M_BUS_SC);
2506 kobj_delete((kobj_t) dev, NULL);
2507 dev->driver = driver;
2509 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2510 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2511 dev->softc = malloc(driver->size, M_BUS_SC,
2514 kobj_delete((kobj_t) dev, NULL);
2515 kobj_init((kobj_t) dev, &null_class);
2521 kobj_init((kobj_t) dev, &null_class);
2524 bus_data_generation_update();
2529 * @brief Probe a device, and return this status.
2531 * This function is the core of the device autoconfiguration
2532 * system. Its purpose is to select a suitable driver for a device and
2533 * then call that driver to initialise the hardware appropriately. The
2534 * driver is selected by calling the DEVICE_PROBE() method of a set of
2535 * candidate drivers and then choosing the driver which returned the
2536 * best value. This driver is then attached to the device using
2539 * The set of suitable drivers is taken from the list of drivers in
2540 * the parent device's devclass. If the device was originally created
2541 * with a specific class name (see device_add_child()), only drivers
2542 * with that name are probed, otherwise all drivers in the devclass
2543 * are probed. If no drivers return successful probe values in the
2544 * parent devclass, the search continues in the parent of that
2545 * devclass (see devclass_get_parent()) if any.
2547 * @param dev the device to initialise
2550 * @retval ENXIO no driver was found
2551 * @retval ENOMEM memory allocation failure
2552 * @retval non-zero some other unix error code
2553 * @retval -1 Device already attached
2556 device_probe(device_t dev)
2562 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2565 if (!(dev->flags & DF_ENABLED)) {
2566 if (bootverbose && device_get_name(dev) != NULL) {
2567 device_print_prettyname(dev);
2568 printf("not probed (disabled)\n");
2572 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2573 if (bus_current_pass == BUS_PASS_DEFAULT &&
2574 !(dev->flags & DF_DONENOMATCH)) {
2575 BUS_PROBE_NOMATCH(dev->parent, dev);
2577 dev->flags |= DF_DONENOMATCH;
2585 * @brief Probe a device and attach a driver if possible
2587 * calls device_probe() and attaches if that was successful.
2590 device_probe_and_attach(device_t dev)
2596 error = device_probe(dev);
2599 else if (error != 0)
2601 return (device_attach(dev));
2605 * @brief Attach a device driver to a device
2607 * This function is a wrapper around the DEVICE_ATTACH() driver
2608 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2609 * device's sysctl tree, optionally prints a description of the device
2610 * and queues a notification event for user-based device management
2613 * Normally this function is only called internally from
2614 * device_probe_and_attach().
2616 * @param dev the device to initialise
2619 * @retval ENXIO no driver was found
2620 * @retval ENOMEM memory allocation failure
2621 * @retval non-zero some other unix error code
2624 device_attach(device_t dev)
2628 device_sysctl_init(dev);
2629 if (!device_is_quiet(dev))
2630 device_print_child(dev->parent, dev);
2631 if ((error = DEVICE_ATTACH(dev)) != 0) {
2632 printf("device_attach: %s%d attach returned %d\n",
2633 dev->driver->name, dev->unit, error);
2634 /* Unset the class; set in device_probe_child */
2635 if (dev->devclass == NULL)
2636 device_set_devclass(dev, NULL);
2637 device_set_driver(dev, NULL);
2638 device_sysctl_fini(dev);
2639 dev->state = DS_NOTPRESENT;
2642 device_sysctl_update(dev);
2643 dev->state = DS_ATTACHED;
2649 * @brief Detach a driver from a device
2651 * This function is a wrapper around the DEVICE_DETACH() driver
2652 * method. If the call to DEVICE_DETACH() succeeds, it calls
2653 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2654 * notification event for user-based device management services and
2655 * cleans up the device's sysctl tree.
2657 * @param dev the device to un-initialise
2660 * @retval ENXIO no driver was found
2661 * @retval ENOMEM memory allocation failure
2662 * @retval non-zero some other unix error code
2665 device_detach(device_t dev)
2671 PDEBUG(("%s", DEVICENAME(dev)));
2672 if (dev->state == DS_BUSY)
2674 if (dev->state != DS_ATTACHED)
2677 if ((error = DEVICE_DETACH(dev)) != 0)
2680 if (!device_is_quiet(dev))
2681 device_printf(dev, "detached\n");
2683 BUS_CHILD_DETACHED(dev->parent, dev);
2685 if (!(dev->flags & DF_FIXEDCLASS))
2686 devclass_delete_device(dev->devclass, dev);
2688 dev->state = DS_NOTPRESENT;
2689 device_set_driver(dev, NULL);
2690 device_set_desc(dev, NULL);
2691 device_sysctl_fini(dev);
2697 * @brief Tells a driver to quiesce itself.
2699 * This function is a wrapper around the DEVICE_QUIESCE() driver
2700 * method. If the call to DEVICE_QUIESCE() succeeds.
2702 * @param dev the device to quiesce
2705 * @retval ENXIO no driver was found
2706 * @retval ENOMEM memory allocation failure
2707 * @retval non-zero some other unix error code
2710 device_quiesce(device_t dev)
2713 PDEBUG(("%s", DEVICENAME(dev)));
2714 if (dev->state == DS_BUSY)
2716 if (dev->state != DS_ATTACHED)
2719 return (DEVICE_QUIESCE(dev));
2723 * @brief Notify a device of system shutdown
2725 * This function calls the DEVICE_SHUTDOWN() driver method if the
2726 * device currently has an attached driver.
2728 * @returns the value returned by DEVICE_SHUTDOWN()
2731 device_shutdown(device_t dev)
2733 if (dev->state < DS_ATTACHED)
2735 return (DEVICE_SHUTDOWN(dev));
2739 * @brief Set the unit number of a device
2741 * This function can be used to override the unit number used for a
2742 * device (e.g. to wire a device to a pre-configured unit number).
2745 device_set_unit(device_t dev, int unit)
2750 dc = device_get_devclass(dev);
2751 if (unit < dc->maxunit && dc->devices[unit])
2753 err = devclass_delete_device(dc, dev);
2757 err = devclass_add_device(dc, dev);
2761 bus_data_generation_update();
2765 /*======================================*/
2767 * Some useful method implementations to make life easier for bus drivers.
2771 * @brief Initialise a resource list.
2773 * @param rl the resource list to initialise
2776 resource_list_init(struct resource_list *rl)
2782 * @brief Reclaim memory used by a resource list.
2784 * This function frees the memory for all resource entries on the list
2787 * @param rl the resource list to free
2790 resource_list_free(struct resource_list *rl)
2792 struct resource_list_entry *rle;
2794 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2796 panic("resource_list_free: resource entry is busy");
2797 STAILQ_REMOVE_HEAD(rl, link);
2803 * @brief Add a resource entry.
2805 * This function adds a resource entry using the given @p type, @p
2806 * start, @p end and @p count values. A rid value is chosen by
2807 * searching sequentially for the first unused rid starting at zero.
2809 * @param rl the resource list to edit
2810 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2811 * @param start the start address of the resource
2812 * @param end the end address of the resource
2813 * @param count XXX end-start+1
2816 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2817 u_long end, u_long count)
2822 while (resource_list_find(rl, type, rid) != NULL)
2824 resource_list_add(rl, type, rid, start, end, count);
2829 * @brief Add or modify a resource entry.
2831 * If an existing entry exists with the same type and rid, it will be
2832 * modified using the given values of @p start, @p end and @p
2833 * count. If no entry exists, a new one will be created using the
2834 * given values. The resource list entry that matches is then returned.
2836 * @param rl the resource list to edit
2837 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2838 * @param rid the resource identifier
2839 * @param start the start address of the resource
2840 * @param end the end address of the resource
2841 * @param count XXX end-start+1
2843 struct resource_list_entry *
2844 resource_list_add(struct resource_list *rl, int type, int rid,
2845 u_long start, u_long end, u_long count)
2847 struct resource_list_entry *rle;
2849 rle = resource_list_find(rl, type, rid);
2851 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2854 panic("resource_list_add: can't record entry");
2855 STAILQ_INSERT_TAIL(rl, rle, link);
2862 panic("resource_list_add: resource entry is busy");
2871 * @brief Find a resource entry by type and rid.
2873 * @param rl the resource list to search
2874 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2875 * @param rid the resource identifier
2877 * @returns the resource entry pointer or NULL if there is no such
2880 struct resource_list_entry *
2881 resource_list_find(struct resource_list *rl, int type, int rid)
2883 struct resource_list_entry *rle;
2885 STAILQ_FOREACH(rle, rl, link) {
2886 if (rle->type == type && rle->rid == rid)
2893 * @brief Delete a resource entry.
2895 * @param rl the resource list to edit
2896 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2897 * @param rid the resource identifier
2900 resource_list_delete(struct resource_list *rl, int type, int rid)
2902 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2905 if (rle->res != NULL)
2906 panic("resource_list_delete: resource has not been released");
2907 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2913 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2915 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2916 * and passing the allocation up to the parent of @p bus. This assumes
2917 * that the first entry of @c device_get_ivars(child) is a struct
2918 * resource_list. This also handles 'passthrough' allocations where a
2919 * child is a remote descendant of bus by passing the allocation up to
2920 * the parent of bus.
2922 * Typically, a bus driver would store a list of child resources
2923 * somewhere in the child device's ivars (see device_get_ivars()) and
2924 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2925 * then call resource_list_alloc() to perform the allocation.
2927 * @param rl the resource list to allocate from
2928 * @param bus the parent device of @p child
2929 * @param child the device which is requesting an allocation
2930 * @param type the type of resource to allocate
2931 * @param rid a pointer to the resource identifier
2932 * @param start hint at the start of the resource range - pass
2933 * @c 0UL for any start address
2934 * @param end hint at the end of the resource range - pass
2935 * @c ~0UL for any end address
2936 * @param count hint at the size of range required - pass @c 1
2938 * @param flags any extra flags to control the resource
2939 * allocation - see @c RF_XXX flags in
2940 * <sys/rman.h> for details
2942 * @returns the resource which was allocated or @c NULL if no
2943 * resource could be allocated
2946 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2947 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2949 struct resource_list_entry *rle = NULL;
2950 int passthrough = (device_get_parent(child) != bus);
2951 int isdefault = (start == 0UL && end == ~0UL);
2954 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2955 type, rid, start, end, count, flags));
2958 rle = resource_list_find(rl, type, *rid);
2961 return (NULL); /* no resource of that type/rid */
2964 panic("resource_list_alloc: resource entry is busy");
2968 count = ulmax(count, rle->count);
2969 end = ulmax(rle->end, start + count - 1);
2972 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2973 type, rid, start, end, count, flags);
2976 * Record the new range.
2979 rle->start = rman_get_start(rle->res);
2980 rle->end = rman_get_end(rle->res);
2988 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
2990 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
2991 * used with resource_list_alloc().
2993 * @param rl the resource list which was allocated from
2994 * @param bus the parent device of @p child
2995 * @param child the device which is requesting a release
2996 * @param type the type of resource to allocate
2997 * @param rid the resource identifier
2998 * @param res the resource to release
3001 * @retval non-zero a standard unix error code indicating what
3002 * error condition prevented the operation
3005 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
3006 int type, int rid, struct resource *res)
3008 struct resource_list_entry *rle = NULL;
3009 int passthrough = (device_get_parent(child) != bus);
3013 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3017 rle = resource_list_find(rl, type, rid);
3020 panic("resource_list_release: can't find resource");
3022 panic("resource_list_release: resource entry is not busy");
3024 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
3034 * @brief Print a description of resources in a resource list
3036 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
3037 * The name is printed if at least one resource of the given type is available.
3038 * The format is used to print resource start and end.
3040 * @param rl the resource list to print
3041 * @param name the name of @p type, e.g. @c "memory"
3042 * @param type type type of resource entry to print
3043 * @param format printf(9) format string to print resource
3044 * start and end values
3046 * @returns the number of characters printed
3049 resource_list_print_type(struct resource_list *rl, const char *name, int type,
3052 struct resource_list_entry *rle;
3053 int printed, retval;
3057 /* Yes, this is kinda cheating */
3058 STAILQ_FOREACH(rle, rl, link) {
3059 if (rle->type == type) {
3061 retval += printf(" %s ", name);
3063 retval += printf(",");
3065 retval += printf(format, rle->start);
3066 if (rle->count > 1) {
3067 retval += printf("-");
3068 retval += printf(format, rle->start +
3077 * @brief Releases all the resources in a list.
3079 * @param rl The resource list to purge.
3084 resource_list_purge(struct resource_list *rl)
3086 struct resource_list_entry *rle;
3088 while ((rle = STAILQ_FIRST(rl)) != NULL) {
3090 bus_release_resource(rman_get_device(rle->res),
3091 rle->type, rle->rid, rle->res);
3092 STAILQ_REMOVE_HEAD(rl, link);
3098 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
3101 return (device_add_child_ordered(dev, order, name, unit));
3105 * @brief Helper function for implementing DEVICE_PROBE()
3107 * This function can be used to help implement the DEVICE_PROBE() for
3108 * a bus (i.e. a device which has other devices attached to it). It
3109 * calls the DEVICE_IDENTIFY() method of each driver in the device's
3113 bus_generic_probe(device_t dev)
3115 devclass_t dc = dev->devclass;
3118 TAILQ_FOREACH(dl, &dc->drivers, link) {
3120 * If this driver's pass is too high, then ignore it.
3121 * For most drivers in the default pass, this will
3122 * never be true. For early-pass drivers they will
3123 * only call the identify routines of eligible drivers
3124 * when this routine is called. Drivers for later
3125 * passes should have their identify routines called
3126 * on early-pass busses during BUS_NEW_PASS().
3128 if (dl->pass > bus_current_pass)
3130 DEVICE_IDENTIFY(dl->driver, dev);
3137 * @brief Helper function for implementing DEVICE_ATTACH()
3139 * This function can be used to help implement the DEVICE_ATTACH() for
3140 * a bus. It calls device_probe_and_attach() for each of the device's
3144 bus_generic_attach(device_t dev)
3148 TAILQ_FOREACH(child, &dev->children, link) {
3149 device_probe_and_attach(child);
3156 * @brief Helper function for implementing DEVICE_DETACH()
3158 * This function can be used to help implement the DEVICE_DETACH() for
3159 * a bus. It calls device_detach() for each of the device's
3163 bus_generic_detach(device_t dev)
3168 if (dev->state != DS_ATTACHED)
3171 TAILQ_FOREACH(child, &dev->children, link) {
3172 if ((error = device_detach(child)) != 0)
3180 * @brief Helper function for implementing DEVICE_SHUTDOWN()
3182 * This function can be used to help implement the DEVICE_SHUTDOWN()
3183 * for a bus. It calls device_shutdown() for each of the device's
3187 bus_generic_shutdown(device_t dev)
3191 TAILQ_FOREACH(child, &dev->children, link) {
3192 device_shutdown(child);
3199 * @brief Helper function for implementing DEVICE_SUSPEND()
3201 * This function can be used to help implement the DEVICE_SUSPEND()
3202 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
3203 * children. If any call to DEVICE_SUSPEND() fails, the suspend
3204 * operation is aborted and any devices which were suspended are
3205 * resumed immediately by calling their DEVICE_RESUME() methods.
3208 bus_generic_suspend(device_t dev)
3211 device_t child, child2;
3213 TAILQ_FOREACH(child, &dev->children, link) {
3214 error = DEVICE_SUSPEND(child);
3216 for (child2 = TAILQ_FIRST(&dev->children);
3217 child2 && child2 != child;
3218 child2 = TAILQ_NEXT(child2, link))
3219 DEVICE_RESUME(child2);
3227 * @brief Helper function for implementing DEVICE_RESUME()
3229 * This function can be used to help implement the DEVICE_RESUME() for
3230 * a bus. It calls DEVICE_RESUME() on each of the device's children.
3233 bus_generic_resume(device_t dev)
3237 TAILQ_FOREACH(child, &dev->children, link) {
3238 DEVICE_RESUME(child);
3239 /* if resume fails, there's nothing we can usefully do... */
3245 * @brief Helper function for implementing BUS_PRINT_CHILD().
3247 * This function prints the first part of the ascii representation of
3248 * @p child, including its name, unit and description (if any - see
3249 * device_set_desc()).
3251 * @returns the number of characters printed
3254 bus_print_child_header(device_t dev, device_t child)
3258 if (device_get_desc(child)) {
3259 retval += device_printf(child, "<%s>", device_get_desc(child));
3261 retval += printf("%s", device_get_nameunit(child));
3268 * @brief Helper function for implementing BUS_PRINT_CHILD().
3270 * This function prints the last part of the ascii representation of
3271 * @p child, which consists of the string @c " on " followed by the
3272 * name and unit of the @p dev.
3274 * @returns the number of characters printed
3277 bus_print_child_footer(device_t dev, device_t child)
3279 return (printf(" on %s\n", device_get_nameunit(dev)));
3283 * @brief Helper function for implementing BUS_PRINT_CHILD().
3285 * This function simply calls bus_print_child_header() followed by
3286 * bus_print_child_footer().
3288 * @returns the number of characters printed
3291 bus_generic_print_child(device_t dev, device_t child)
3295 retval += bus_print_child_header(dev, child);
3296 retval += bus_print_child_footer(dev, child);
3302 * @brief Stub function for implementing BUS_READ_IVAR().
3307 bus_generic_read_ivar(device_t dev, device_t child, int index,
3314 * @brief Stub function for implementing BUS_WRITE_IVAR().
3319 bus_generic_write_ivar(device_t dev, device_t child, int index,
3326 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3330 struct resource_list *
3331 bus_generic_get_resource_list(device_t dev, device_t child)
3337 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3339 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3340 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3341 * and then calls device_probe_and_attach() for each unattached child.
3344 bus_generic_driver_added(device_t dev, driver_t *driver)
3348 DEVICE_IDENTIFY(driver, dev);
3349 TAILQ_FOREACH(child, &dev->children, link) {
3350 if (child->state == DS_NOTPRESENT ||
3351 (child->flags & DF_REBID))
3352 device_probe_and_attach(child);
3357 * @brief Helper function for implementing BUS_NEW_PASS().
3359 * This implementing of BUS_NEW_PASS() first calls the identify
3360 * routines for any drivers that probe at the current pass. Then it
3361 * walks the list of devices for this bus. If a device is already
3362 * attached, then it calls BUS_NEW_PASS() on that device. If the
3363 * device is not already attached, it attempts to attach a driver to
3367 bus_generic_new_pass(device_t dev)
3374 TAILQ_FOREACH(dl, &dc->drivers, link) {
3375 if (dl->pass == bus_current_pass)
3376 DEVICE_IDENTIFY(dl->driver, dev);
3378 TAILQ_FOREACH(child, &dev->children, link) {
3379 if (child->state >= DS_ATTACHED)
3380 BUS_NEW_PASS(child);
3381 else if (child->state == DS_NOTPRESENT)
3382 device_probe_and_attach(child);
3387 * @brief Helper function for implementing BUS_SETUP_INTR().
3389 * This simple implementation of BUS_SETUP_INTR() simply calls the
3390 * BUS_SETUP_INTR() method of the parent of @p dev.
3393 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3394 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3397 /* Propagate up the bus hierarchy until someone handles it. */
3399 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3400 filter, intr, arg, cookiep));
3405 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3407 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3408 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3411 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3414 /* Propagate up the bus hierarchy until someone handles it. */
3416 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3421 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3423 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3424 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3427 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3428 u_long start, u_long end, u_long count, u_int flags)
3430 /* Propagate up the bus hierarchy until someone handles it. */
3432 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3433 start, end, count, flags));
3438 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3440 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3441 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3444 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3447 /* Propagate up the bus hierarchy until someone handles it. */
3449 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3455 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3457 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3458 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3461 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3464 /* Propagate up the bus hierarchy until someone handles it. */
3466 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3472 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3474 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3475 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3478 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3479 int rid, struct resource *r)
3481 /* Propagate up the bus hierarchy until someone handles it. */
3483 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3489 * @brief Helper function for implementing BUS_BIND_INTR().
3491 * This simple implementation of BUS_BIND_INTR() simply calls the
3492 * BUS_BIND_INTR() method of the parent of @p dev.
3495 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
3499 /* Propagate up the bus hierarchy until someone handles it. */
3501 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
3506 * @brief Helper function for implementing BUS_CONFIG_INTR().
3508 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3509 * BUS_CONFIG_INTR() method of the parent of @p dev.
3512 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3513 enum intr_polarity pol)
3516 /* Propagate up the bus hierarchy until someone handles it. */
3518 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3523 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3525 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3526 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3529 bus_generic_get_dma_tag(device_t dev, device_t child)
3532 /* Propagate up the bus hierarchy until someone handles it. */
3533 if (dev->parent != NULL)
3534 return (BUS_GET_DMA_TAG(dev->parent, child));
3539 * @brief Helper function for implementing BUS_GET_RESOURCE().
3541 * This implementation of BUS_GET_RESOURCE() uses the
3542 * resource_list_find() function to do most of the work. It calls
3543 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3547 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3548 u_long *startp, u_long *countp)
3550 struct resource_list * rl = NULL;
3551 struct resource_list_entry * rle = NULL;
3553 rl = BUS_GET_RESOURCE_LIST(dev, child);
3557 rle = resource_list_find(rl, type, rid);
3562 *startp = rle->start;
3564 *countp = rle->count;
3570 * @brief Helper function for implementing BUS_SET_RESOURCE().
3572 * This implementation of BUS_SET_RESOURCE() uses the
3573 * resource_list_add() function to do most of the work. It calls
3574 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3578 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3579 u_long start, u_long count)
3581 struct resource_list * rl = NULL;
3583 rl = BUS_GET_RESOURCE_LIST(dev, child);
3587 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3593 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3595 * This implementation of BUS_DELETE_RESOURCE() uses the
3596 * resource_list_delete() function to do most of the work. It calls
3597 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3601 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3603 struct resource_list * rl = NULL;
3605 rl = BUS_GET_RESOURCE_LIST(dev, child);
3609 resource_list_delete(rl, type, rid);
3615 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3617 * This implementation of BUS_RELEASE_RESOURCE() uses the
3618 * resource_list_release() function to do most of the work. It calls
3619 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3622 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3623 int rid, struct resource *r)
3625 struct resource_list * rl = NULL;
3627 rl = BUS_GET_RESOURCE_LIST(dev, child);
3631 return (resource_list_release(rl, dev, child, type, rid, r));
3635 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3637 * This implementation of BUS_ALLOC_RESOURCE() uses the
3638 * resource_list_alloc() function to do most of the work. It calls
3639 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3642 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3643 int *rid, u_long start, u_long end, u_long count, u_int flags)
3645 struct resource_list * rl = NULL;
3647 rl = BUS_GET_RESOURCE_LIST(dev, child);
3651 return (resource_list_alloc(rl, dev, child, type, rid,
3652 start, end, count, flags));
3656 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3658 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3659 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3662 bus_generic_child_present(device_t dev, device_t child)
3664 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3668 * Some convenience functions to make it easier for drivers to use the
3669 * resource-management functions. All these really do is hide the
3670 * indirection through the parent's method table, making for slightly
3671 * less-wordy code. In the future, it might make sense for this code
3672 * to maintain some sort of a list of resources allocated by each device.
3676 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3677 struct resource **res)
3681 for (i = 0; rs[i].type != -1; i++)
3683 for (i = 0; rs[i].type != -1; i++) {
3684 res[i] = bus_alloc_resource_any(dev,
3685 rs[i].type, &rs[i].rid, rs[i].flags);
3686 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3687 bus_release_resources(dev, rs, res);
3695 bus_release_resources(device_t dev, const struct resource_spec *rs,
3696 struct resource **res)
3700 for (i = 0; rs[i].type != -1; i++)
3701 if (res[i] != NULL) {
3702 bus_release_resource(
3703 dev, rs[i].type, rs[i].rid, res[i]);
3709 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3711 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3715 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3716 u_long count, u_int flags)
3718 if (dev->parent == NULL)
3720 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3725 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3727 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3731 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3733 if (dev->parent == NULL)
3735 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3739 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3741 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3745 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3747 if (dev->parent == NULL)
3749 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3753 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3755 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3759 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3761 if (dev->parent == NULL)
3763 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3767 * @brief Wrapper function for BUS_SETUP_INTR().
3769 * This function simply calls the BUS_SETUP_INTR() method of the
3773 bus_setup_intr(device_t dev, struct resource *r, int flags,
3774 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3778 if (dev->parent == NULL)
3780 error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
3784 if (handler != NULL && !(flags & INTR_MPSAFE))
3785 device_printf(dev, "[GIANT-LOCKED]\n");
3786 if (bootverbose && (flags & INTR_MPSAFE))
3787 device_printf(dev, "[MPSAFE]\n");
3788 if (filter != NULL) {
3789 if (handler == NULL)
3790 device_printf(dev, "[FILTER]\n");
3792 device_printf(dev, "[FILTER+ITHREAD]\n");
3794 device_printf(dev, "[ITHREAD]\n");
3799 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3801 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3805 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3807 if (dev->parent == NULL)
3809 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3813 * @brief Wrapper function for BUS_BIND_INTR().
3815 * This function simply calls the BUS_BIND_INTR() method of the
3819 bus_bind_intr(device_t dev, struct resource *r, int cpu)
3821 if (dev->parent == NULL)
3823 return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
3827 * @brief Wrapper function for BUS_SET_RESOURCE().
3829 * This function simply calls the BUS_SET_RESOURCE() method of the
3833 bus_set_resource(device_t dev, int type, int rid,
3834 u_long start, u_long count)
3836 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3841 * @brief Wrapper function for BUS_GET_RESOURCE().
3843 * This function simply calls the BUS_GET_RESOURCE() method of the
3847 bus_get_resource(device_t dev, int type, int rid,
3848 u_long *startp, u_long *countp)
3850 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3855 * @brief Wrapper function for BUS_GET_RESOURCE().
3857 * This function simply calls the BUS_GET_RESOURCE() method of the
3858 * parent of @p dev and returns the start value.
3861 bus_get_resource_start(device_t dev, int type, int rid)
3863 u_long start, count;
3866 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3874 * @brief Wrapper function for BUS_GET_RESOURCE().
3876 * This function simply calls the BUS_GET_RESOURCE() method of the
3877 * parent of @p dev and returns the count value.
3880 bus_get_resource_count(device_t dev, int type, int rid)
3882 u_long start, count;
3885 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3893 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3895 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3899 bus_delete_resource(device_t dev, int type, int rid)
3901 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3905 * @brief Wrapper function for BUS_CHILD_PRESENT().
3907 * This function simply calls the BUS_CHILD_PRESENT() method of the
3911 bus_child_present(device_t child)
3913 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3917 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3919 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3923 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3927 parent = device_get_parent(child);
3928 if (parent == NULL) {
3932 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3936 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3938 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3942 bus_child_location_str(device_t child, char *buf, size_t buflen)
3946 parent = device_get_parent(child);
3947 if (parent == NULL) {
3951 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3955 * @brief Wrapper function for BUS_GET_DMA_TAG().
3957 * This function simply calls the BUS_GET_DMA_TAG() method of the
3961 bus_get_dma_tag(device_t dev)
3965 parent = device_get_parent(dev);
3968 return (BUS_GET_DMA_TAG(parent, dev));
3971 /* Resume all devices and then notify userland that we're up again. */
3973 root_resume(device_t dev)
3977 error = bus_generic_resume(dev);
3979 devctl_notify("kern", "power", "resume", NULL);
3984 root_print_child(device_t dev, device_t child)
3988 retval += bus_print_child_header(dev, child);
3989 retval += printf("\n");
3995 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
3996 driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
3999 * If an interrupt mapping gets to here something bad has happened.
4001 panic("root_setup_intr");
4005 * If we get here, assume that the device is permanant and really is
4006 * present in the system. Removable bus drivers are expected to intercept
4007 * this call long before it gets here. We return -1 so that drivers that
4008 * really care can check vs -1 or some ERRNO returned higher in the food
4012 root_child_present(device_t dev, device_t child)
4017 static kobj_method_t root_methods[] = {
4018 /* Device interface */
4019 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
4020 KOBJMETHOD(device_suspend, bus_generic_suspend),
4021 KOBJMETHOD(device_resume, root_resume),
4024 KOBJMETHOD(bus_print_child, root_print_child),
4025 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
4026 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
4027 KOBJMETHOD(bus_setup_intr, root_setup_intr),
4028 KOBJMETHOD(bus_child_present, root_child_present),
4033 static driver_t root_driver = {
4040 devclass_t root_devclass;
4043 root_bus_module_handler(module_t mod, int what, void* arg)
4047 TAILQ_INIT(&bus_data_devices);
4048 kobj_class_compile((kobj_class_t) &root_driver);
4049 root_bus = make_device(NULL, "root", 0);
4050 root_bus->desc = "System root bus";
4051 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
4052 root_bus->driver = &root_driver;
4053 root_bus->state = DS_ATTACHED;
4054 root_devclass = devclass_find_internal("root", NULL, FALSE);
4059 device_shutdown(root_bus);
4062 return (EOPNOTSUPP);
4068 static moduledata_t root_bus_mod = {
4070 root_bus_module_handler,
4073 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
4076 * @brief Automatically configure devices
4078 * This function begins the autoconfiguration process by calling
4079 * device_probe_and_attach() for each child of the @c root0 device.
4082 root_bus_configure(void)
4087 /* Eventually this will be split up, but this is sufficient for now. */
4088 bus_set_pass(BUS_PASS_DEFAULT);
4092 * @brief Module handler for registering device drivers
4094 * This module handler is used to automatically register device
4095 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
4096 * devclass_add_driver() for the driver described by the
4097 * driver_module_data structure pointed to by @p arg
4100 driver_module_handler(module_t mod, int what, void *arg)
4102 struct driver_module_data *dmd;
4103 devclass_t bus_devclass;
4104 kobj_class_t driver;
4107 dmd = (struct driver_module_data *)arg;
4108 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
4113 if (dmd->dmd_chainevh)
4114 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4116 pass = dmd->dmd_pass;
4117 driver = dmd->dmd_driver;
4118 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
4119 DRIVERNAME(driver), dmd->dmd_busname, pass));
4120 error = devclass_add_driver(bus_devclass, driver, pass);
4125 * If the driver has any base classes, make the
4126 * devclass inherit from the devclass of the driver's
4127 * first base class. This will allow the system to
4128 * search for drivers in both devclasses for children
4129 * of a device using this driver.
4131 if (driver->baseclasses) {
4132 const char *parentname;
4133 parentname = driver->baseclasses[0]->name;
4134 *dmd->dmd_devclass =
4135 devclass_find_internal(driver->name,
4138 *dmd->dmd_devclass =
4139 devclass_find_internal(driver->name, NULL, TRUE);
4144 PDEBUG(("Unloading module: driver %s from bus %s",
4145 DRIVERNAME(dmd->dmd_driver),
4147 error = devclass_delete_driver(bus_devclass,
4150 if (!error && dmd->dmd_chainevh)
4151 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4154 PDEBUG(("Quiesce module: driver %s from bus %s",
4155 DRIVERNAME(dmd->dmd_driver),
4157 error = devclass_quiesce_driver(bus_devclass,
4160 if (!error && dmd->dmd_chainevh)
4161 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
4172 * @brief Enumerate all hinted devices for this bus.
4174 * Walks through the hints for this bus and calls the bus_hinted_child
4175 * routine for each one it fines. It searches first for the specific
4176 * bus that's being probed for hinted children (eg isa0), and then for
4177 * generic children (eg isa).
4179 * @param dev bus device to enumerate
4182 bus_enumerate_hinted_children(device_t bus)
4185 const char *dname, *busname;
4189 * enumerate all devices on the specific bus
4191 busname = device_get_nameunit(bus);
4193 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4194 BUS_HINTED_CHILD(bus, dname, dunit);
4197 * and all the generic ones.
4199 busname = device_get_name(bus);
4201 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
4202 BUS_HINTED_CHILD(bus, dname, dunit);
4207 /* the _short versions avoid iteration by not calling anything that prints
4208 * more than oneliners. I love oneliners.
4212 print_device_short(device_t dev, int indent)
4217 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
4218 dev->unit, dev->desc,
4219 (dev->parent? "":"no "),
4220 (TAILQ_EMPTY(&dev->children)? "no ":""),
4221 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
4222 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
4223 (dev->flags&DF_WILDCARD? "wildcard,":""),
4224 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
4225 (dev->flags&DF_REBID? "rebiddable,":""),
4226 (dev->ivars? "":"no "),
4227 (dev->softc? "":"no "),
4232 print_device(device_t dev, int indent)
4237 print_device_short(dev, indent);
4239 indentprintf(("Parent:\n"));
4240 print_device_short(dev->parent, indent+1);
4241 indentprintf(("Driver:\n"));
4242 print_driver_short(dev->driver, indent+1);
4243 indentprintf(("Devclass:\n"));
4244 print_devclass_short(dev->devclass, indent+1);
4248 print_device_tree_short(device_t dev, int indent)
4249 /* print the device and all its children (indented) */
4256 print_device_short(dev, indent);
4258 TAILQ_FOREACH(child, &dev->children, link) {
4259 print_device_tree_short(child, indent+1);
4264 print_device_tree(device_t dev, int indent)
4265 /* print the device and all its children (indented) */
4272 print_device(dev, indent);
4274 TAILQ_FOREACH(child, &dev->children, link) {
4275 print_device_tree(child, indent+1);
4280 print_driver_short(driver_t *driver, int indent)
4285 indentprintf(("driver %s: softc size = %zd\n",
4286 driver->name, driver->size));
4290 print_driver(driver_t *driver, int indent)
4295 print_driver_short(driver, indent);
4300 print_driver_list(driver_list_t drivers, int indent)
4302 driverlink_t driver;
4304 TAILQ_FOREACH(driver, &drivers, link) {
4305 print_driver(driver->driver, indent);
4310 print_devclass_short(devclass_t dc, int indent)
4315 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
4319 print_devclass(devclass_t dc, int indent)
4326 print_devclass_short(dc, indent);
4327 indentprintf(("Drivers:\n"));
4328 print_driver_list(dc->drivers, indent+1);
4330 indentprintf(("Devices:\n"));
4331 for (i = 0; i < dc->maxunit; i++)
4333 print_device(dc->devices[i], indent+1);
4337 print_devclass_list_short(void)
4341 printf("Short listing of devclasses, drivers & devices:\n");
4342 TAILQ_FOREACH(dc, &devclasses, link) {
4343 print_devclass_short(dc, 0);
4348 print_devclass_list(void)
4352 printf("Full listing of devclasses, drivers & devices:\n");
4353 TAILQ_FOREACH(dc, &devclasses, link) {
4354 print_devclass(dc, 0);
4361 * User-space access to the device tree.
4363 * We implement a small set of nodes:
4365 * hw.bus Single integer read method to obtain the
4366 * current generation count.
4367 * hw.bus.devices Reads the entire device tree in flat space.
4368 * hw.bus.rman Resource manager interface
4370 * We might like to add the ability to scan devclasses and/or drivers to
4371 * determine what else is currently loaded/available.
4375 sysctl_bus(SYSCTL_HANDLER_ARGS)
4377 struct u_businfo ubus;
4379 ubus.ub_version = BUS_USER_VERSION;
4380 ubus.ub_generation = bus_data_generation;
4382 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4384 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4385 "bus-related data");
4388 sysctl_devices(SYSCTL_HANDLER_ARGS)
4390 int *name = (int *)arg1;
4391 u_int namelen = arg2;
4394 struct u_device udev; /* XXX this is a bit big */
4400 if (bus_data_generation_check(name[0]))
4406 * Scan the list of devices, looking for the requested index.
4408 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4416 * Populate the return array.
4418 bzero(&udev, sizeof(udev));
4419 udev.dv_handle = (uintptr_t)dev;
4420 udev.dv_parent = (uintptr_t)dev->parent;
4421 if (dev->nameunit != NULL)
4422 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4423 if (dev->desc != NULL)
4424 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4425 if (dev->driver != NULL && dev->driver->name != NULL)
4426 strlcpy(udev.dv_drivername, dev->driver->name,
4427 sizeof(udev.dv_drivername));
4428 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4429 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4430 udev.dv_devflags = dev->devflags;
4431 udev.dv_flags = dev->flags;
4432 udev.dv_state = dev->state;
4433 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4437 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4438 "system device tree");
4441 bus_data_generation_check(int generation)
4443 if (generation != bus_data_generation)
4446 /* XXX generate optimised lists here? */
4451 bus_data_generation_update(void)
4453 bus_data_generation++;
4457 bus_free_resource(device_t dev, int type, struct resource *r)
4461 return (bus_release_resource(dev, type, rman_get_rid(r), r));