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 */
72 * Forward declarations
74 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
75 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
76 typedef TAILQ_HEAD(device_list, device) device_list_t;
79 TAILQ_ENTRY(devclass) link;
80 devclass_t parent; /* parent in devclass hierarchy */
81 driver_list_t drivers; /* bus devclasses store drivers for bus */
83 device_t *devices; /* array of devices indexed by unit */
84 int maxunit; /* size of devices array */
86 struct sysctl_ctx_list sysctl_ctx;
87 struct sysctl_oid *sysctl_tree;
91 * @brief Implementation of device.
95 * A device is a kernel object. The first field must be the
96 * current ops table for the object.
103 TAILQ_ENTRY(device) link; /**< list of devices in parent */
104 TAILQ_ENTRY(device) devlink; /**< global device list membership */
105 device_t parent; /**< parent of this device */
106 device_list_t children; /**< list of child devices */
109 * Details of this device.
111 driver_t *driver; /**< current driver */
112 devclass_t devclass; /**< current device class */
113 int unit; /**< current unit number */
114 char* nameunit; /**< name+unit e.g. foodev0 */
115 char* desc; /**< driver specific description */
116 int busy; /**< count of calls to device_busy() */
117 device_state_t state; /**< current device state */
118 u_int32_t devflags; /**< api level flags for device_get_flags() */
119 u_short flags; /**< internal device flags */
120 #define DF_ENABLED 1 /* device should be probed/attached */
121 #define DF_FIXEDCLASS 2 /* devclass specified at create time */
122 #define DF_WILDCARD 4 /* unit was originally wildcard */
123 #define DF_DESCMALLOCED 8 /* description was malloced */
124 #define DF_QUIET 16 /* don't print verbose attach message */
125 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */
126 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */
127 #define DF_REBID 128 /* Can rebid after attach */
128 u_char order; /**< order from device_add_child_ordered() */
130 void *ivars; /**< instance variables */
131 void *softc; /**< current driver's variables */
133 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
134 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
137 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
138 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
142 static int bus_debug = 1;
143 TUNABLE_INT("bus.debug", &bus_debug);
144 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
147 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
148 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
149 #define DRIVERNAME(d) ((d)? d->name : "no driver")
150 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
153 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
154 * prevent syslog from deleting initial spaces
156 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
158 static void print_device_short(device_t dev, int indent);
159 static void print_device(device_t dev, int indent);
160 void print_device_tree_short(device_t dev, int indent);
161 void print_device_tree(device_t dev, int indent);
162 static void print_driver_short(driver_t *driver, int indent);
163 static void print_driver(driver_t *driver, int indent);
164 static void print_driver_list(driver_list_t drivers, int indent);
165 static void print_devclass_short(devclass_t dc, int indent);
166 static void print_devclass(devclass_t dc, int indent);
167 void print_devclass_list_short(void);
168 void print_devclass_list(void);
171 /* Make the compiler ignore the function calls */
172 #define PDEBUG(a) /* nop */
173 #define DEVICENAME(d) /* nop */
174 #define DRIVERNAME(d) /* nop */
175 #define DEVCLANAME(d) /* nop */
177 #define print_device_short(d,i) /* nop */
178 #define print_device(d,i) /* nop */
179 #define print_device_tree_short(d,i) /* nop */
180 #define print_device_tree(d,i) /* nop */
181 #define print_driver_short(d,i) /* nop */
182 #define print_driver(d,i) /* nop */
183 #define print_driver_list(d,i) /* nop */
184 #define print_devclass_short(d,i) /* nop */
185 #define print_devclass(d,i) /* nop */
186 #define print_devclass_list_short() /* nop */
187 #define print_devclass_list() /* nop */
195 DEVCLASS_SYSCTL_PARENT,
199 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
201 devclass_t dc = (devclass_t)arg1;
205 case DEVCLASS_SYSCTL_PARENT:
206 value = dc->parent ? dc->parent->name : "";
211 return (SYSCTL_OUT(req, value, strlen(value)));
215 devclass_sysctl_init(devclass_t dc)
218 if (dc->sysctl_tree != NULL)
220 sysctl_ctx_init(&dc->sysctl_ctx);
221 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
222 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
224 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
225 OID_AUTO, "%parent", CTLFLAG_RD,
226 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
232 DEVICE_SYSCTL_DRIVER,
233 DEVICE_SYSCTL_LOCATION,
234 DEVICE_SYSCTL_PNPINFO,
235 DEVICE_SYSCTL_PARENT,
239 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
241 device_t dev = (device_t)arg1;
248 case DEVICE_SYSCTL_DESC:
249 value = dev->desc ? dev->desc : "";
251 case DEVICE_SYSCTL_DRIVER:
252 value = dev->driver ? dev->driver->name : "";
254 case DEVICE_SYSCTL_LOCATION:
255 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
256 bus_child_location_str(dev, buf, 1024);
258 case DEVICE_SYSCTL_PNPINFO:
259 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
260 bus_child_pnpinfo_str(dev, buf, 1024);
262 case DEVICE_SYSCTL_PARENT:
263 value = dev->parent ? dev->parent->nameunit : "";
268 error = SYSCTL_OUT(req, value, strlen(value));
275 device_sysctl_init(device_t dev)
277 devclass_t dc = dev->devclass;
279 if (dev->sysctl_tree != NULL)
281 devclass_sysctl_init(dc);
282 sysctl_ctx_init(&dev->sysctl_ctx);
283 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
284 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
285 dev->nameunit + strlen(dc->name),
287 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
288 OID_AUTO, "%desc", CTLFLAG_RD,
289 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
290 "device description");
291 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
292 OID_AUTO, "%driver", CTLFLAG_RD,
293 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
294 "device driver name");
295 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
296 OID_AUTO, "%location", CTLFLAG_RD,
297 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
298 "device location relative to parent");
299 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
300 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
301 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
302 "device identification");
303 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
304 OID_AUTO, "%parent", CTLFLAG_RD,
305 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
310 device_sysctl_fini(device_t dev)
312 if (dev->sysctl_tree == NULL)
314 sysctl_ctx_free(&dev->sysctl_ctx);
315 dev->sysctl_tree = NULL;
319 * /dev/devctl implementation
323 * This design allows only one reader for /dev/devctl. This is not desirable
324 * in the long run, but will get a lot of hair out of this implementation.
325 * Maybe we should make this device a clonable device.
327 * Also note: we specifically do not attach a device to the device_t tree
328 * to avoid potential chicken and egg problems. One could argue that all
329 * of this belongs to the root node. One could also further argue that the
330 * sysctl interface that we have not might more properly be an ioctl
331 * interface, but at this stage of the game, I'm not inclined to rock that
334 * I'm also not sure that the SIGIO support is done correctly or not, as
335 * I copied it from a driver that had SIGIO support that likely hasn't been
336 * tested since 3.4 or 2.2.8!
339 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
340 static int devctl_disable = 0;
341 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
342 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
343 sysctl_devctl_disable, "I", "devctl disable");
345 static d_open_t devopen;
346 static d_close_t devclose;
347 static d_read_t devread;
348 static d_ioctl_t devioctl;
349 static d_poll_t devpoll;
351 static struct cdevsw dev_cdevsw = {
352 .d_version = D_VERSION,
353 .d_flags = D_NEEDGIANT,
362 struct dev_event_info
365 TAILQ_ENTRY(dev_event_info) dei_link;
368 TAILQ_HEAD(devq, dev_event_info);
370 static struct dev_softc
378 struct proc *async_proc;
381 static struct cdev *devctl_dev;
386 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
388 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
389 cv_init(&devsoftc.cv, "dev cv");
390 TAILQ_INIT(&devsoftc.devq);
394 devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
400 devsoftc.nonblock = 0;
401 devsoftc.async_proc = NULL;
406 devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
409 mtx_lock(&devsoftc.mtx);
410 cv_broadcast(&devsoftc.cv);
411 mtx_unlock(&devsoftc.mtx);
417 * The read channel for this device is used to report changes to
418 * userland in realtime. We are required to free the data as well as
419 * the n1 object because we allocate them separately. Also note that
420 * we return one record at a time. If you try to read this device a
421 * character at a time, you will lose the rest of the data. Listening
422 * programs are expected to cope.
425 devread(struct cdev *dev, struct uio *uio, int ioflag)
427 struct dev_event_info *n1;
430 mtx_lock(&devsoftc.mtx);
431 while (TAILQ_EMPTY(&devsoftc.devq)) {
432 if (devsoftc.nonblock) {
433 mtx_unlock(&devsoftc.mtx);
436 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
439 * Need to translate ERESTART to EINTR here? -- jake
441 mtx_unlock(&devsoftc.mtx);
445 n1 = TAILQ_FIRST(&devsoftc.devq);
446 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
447 mtx_unlock(&devsoftc.mtx);
448 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
449 free(n1->dei_data, M_BUS);
455 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
461 devsoftc.nonblock = 1;
463 devsoftc.nonblock = 0;
467 devsoftc.async_proc = td->td_proc;
469 devsoftc.async_proc = NULL;
472 /* (un)Support for other fcntl() calls. */
485 devpoll(struct cdev *dev, int events, d_thread_t *td)
489 mtx_lock(&devsoftc.mtx);
490 if (events & (POLLIN | POLLRDNORM)) {
491 if (!TAILQ_EMPTY(&devsoftc.devq))
492 revents = events & (POLLIN | POLLRDNORM);
494 selrecord(td, &devsoftc.sel);
496 mtx_unlock(&devsoftc.mtx);
502 * @brief Queue data to be read from the devctl device
504 * Generic interface to queue data to the devctl device. It is
505 * assumed that @p data is properly formatted. It is further assumed
506 * that @p data is allocated using the M_BUS malloc type.
509 devctl_queue_data(char *data)
511 struct dev_event_info *n1 = NULL;
514 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
518 mtx_lock(&devsoftc.mtx);
519 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
520 cv_broadcast(&devsoftc.cv);
521 mtx_unlock(&devsoftc.mtx);
522 selwakeup(&devsoftc.sel);
523 p = devsoftc.async_proc;
532 * @brief Send a 'notification' to userland, using standard ways
535 devctl_notify(const char *system, const char *subsystem, const char *type,
542 return; /* BOGUS! Must specify system. */
543 if (subsystem == NULL)
544 return; /* BOGUS! Must specify subsystem. */
546 return; /* BOGUS! Must specify type. */
547 len += strlen(" system=") + strlen(system);
548 len += strlen(" subsystem=") + strlen(subsystem);
549 len += strlen(" type=") + strlen(type);
550 /* add in the data message plus newline. */
553 len += 3; /* '!', '\n', and NUL */
554 msg = malloc(len, M_BUS, M_NOWAIT);
556 return; /* Drop it on the floor */
558 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
559 system, subsystem, type, data);
561 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
562 system, subsystem, type);
563 devctl_queue_data(msg);
567 * Common routine that tries to make sending messages as easy as possible.
568 * We allocate memory for the data, copy strings into that, but do not
569 * free it unless there's an error. The dequeue part of the driver should
570 * free the data. We don't send data when the device is disabled. We do
571 * send data, even when we have no listeners, because we wish to avoid
572 * races relating to startup and restart of listening applications.
574 * devaddq is designed to string together the type of event, with the
575 * object of that event, plus the plug and play info and location info
576 * for that event. This is likely most useful for devices, but less
577 * useful for other consumers of this interface. Those should use
578 * the devctl_queue_data() interface instead.
581 devaddq(const char *type, const char *what, device_t dev)
590 data = malloc(1024, M_BUS, M_NOWAIT);
594 /* get the bus specific location of this device */
595 loc = malloc(1024, M_BUS, M_NOWAIT);
599 bus_child_location_str(dev, loc, 1024);
601 /* Get the bus specific pnp info of this device */
602 pnp = malloc(1024, M_BUS, M_NOWAIT);
606 bus_child_pnpinfo_str(dev, pnp, 1024);
608 /* Get the parent of this device, or / if high enough in the tree. */
609 if (device_get_parent(dev) == NULL)
610 parstr = "."; /* Or '/' ? */
612 parstr = device_get_nameunit(device_get_parent(dev));
613 /* String it all together. */
614 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
618 devctl_queue_data(data);
628 * A device was added to the tree. We are called just after it successfully
629 * attaches (that is, probe and attach success for this device). No call
630 * is made if a device is merely parented into the tree. See devnomatch
631 * if probe fails. If attach fails, no notification is sent (but maybe
632 * we should have a different message for this).
635 devadded(device_t dev)
640 pnp = malloc(1024, M_BUS, M_NOWAIT);
643 tmp = malloc(1024, M_BUS, M_NOWAIT);
647 bus_child_pnpinfo_str(dev, pnp, 1024);
648 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
649 devaddq("+", tmp, dev);
659 * A device was removed from the tree. We are called just before this
663 devremoved(device_t dev)
668 pnp = malloc(1024, M_BUS, M_NOWAIT);
671 tmp = malloc(1024, M_BUS, M_NOWAIT);
675 bus_child_pnpinfo_str(dev, pnp, 1024);
676 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
677 devaddq("-", tmp, dev);
687 * Called when there's no match for this device. This is only called
688 * the first time that no match happens, so we don't keep getitng this
689 * message. Should that prove to be undesirable, we can change it.
690 * This is called when all drivers that can attach to a given bus
691 * decline to accept this device. Other errrors may not be detected.
694 devnomatch(device_t dev)
696 devaddq("?", "", dev);
700 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
702 struct dev_event_info *n1;
705 dis = devctl_disable;
706 error = sysctl_handle_int(oidp, &dis, 0, req);
707 if (error || !req->newptr)
709 mtx_lock(&devsoftc.mtx);
710 devctl_disable = dis;
712 while (!TAILQ_EMPTY(&devsoftc.devq)) {
713 n1 = TAILQ_FIRST(&devsoftc.devq);
714 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
715 free(n1->dei_data, M_BUS);
719 mtx_unlock(&devsoftc.mtx);
723 /* End of /dev/devctl code */
725 TAILQ_HEAD(,device) bus_data_devices;
726 static int bus_data_generation = 1;
728 kobj_method_t null_methods[] = {
732 DEFINE_CLASS(null, null_methods, 0);
735 * Devclass implementation
738 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
743 * @brief Find or create a device class
745 * If a device class with the name @p classname exists, return it,
746 * otherwise if @p create is non-zero create and return a new device
749 * If @p parentname is non-NULL, the parent of the devclass is set to
750 * the devclass of that name.
752 * @param classname the devclass name to find or create
753 * @param parentname the parent devclass name or @c NULL
754 * @param create non-zero to create a devclass
757 devclass_find_internal(const char *classname, const char *parentname,
762 PDEBUG(("looking for %s", classname));
766 TAILQ_FOREACH(dc, &devclasses, link) {
767 if (!strcmp(dc->name, classname))
772 PDEBUG(("creating %s", classname));
773 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
774 M_BUS, M_NOWAIT|M_ZERO);
778 dc->name = (char*) (dc + 1);
779 strcpy(dc->name, classname);
780 TAILQ_INIT(&dc->drivers);
781 TAILQ_INSERT_TAIL(&devclasses, dc, link);
783 bus_data_generation_update();
787 * If a parent class is specified, then set that as our parent so
788 * that this devclass will support drivers for the parent class as
789 * well. If the parent class has the same name don't do this though
790 * as it creates a cycle that can trigger an infinite loop in
791 * device_probe_child() if a device exists for which there is no
794 if (parentname && dc && !dc->parent &&
795 strcmp(classname, parentname) != 0) {
796 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
803 * @brief Create a device class
805 * If a device class with the name @p classname exists, return it,
806 * otherwise create and return a new device class.
808 * @param classname the devclass name to find or create
811 devclass_create(const char *classname)
813 return (devclass_find_internal(classname, NULL, TRUE));
817 * @brief Find a device class
819 * If a device class with the name @p classname exists, return it,
820 * otherwise return @c NULL.
822 * @param classname the devclass name to find
825 devclass_find(const char *classname)
827 return (devclass_find_internal(classname, NULL, FALSE));
831 * @brief Add a device driver to a device class
833 * Add a device driver to a devclass. This is normally called
834 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
835 * all devices in the devclass will be called to allow them to attempt
836 * to re-probe any unmatched children.
838 * @param dc the devclass to edit
839 * @param driver the driver to register
842 devclass_add_driver(devclass_t dc, driver_t *driver)
847 PDEBUG(("%s", DRIVERNAME(driver)));
849 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
854 * Compile the driver's methods. Also increase the reference count
855 * so that the class doesn't get freed when the last instance
856 * goes. This means we can safely use static methods and avoids a
857 * double-free in devclass_delete_driver.
859 kobj_class_compile((kobj_class_t) driver);
862 * Make sure the devclass which the driver is implementing exists.
864 devclass_find_internal(driver->name, NULL, TRUE);
867 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
868 driver->refs++; /* XXX: kobj_mtx */
871 * Call BUS_DRIVER_ADDED for any existing busses in this class.
873 for (i = 0; i < dc->maxunit; i++)
875 BUS_DRIVER_ADDED(dc->devices[i], driver);
877 bus_data_generation_update();
882 * @brief Delete a device driver from a device class
884 * Delete a device driver from a devclass. This is normally called
885 * automatically by DRIVER_MODULE().
887 * If the driver is currently attached to any devices,
888 * devclass_delete_driver() will first attempt to detach from each
889 * device. If one of the detach calls fails, the driver will not be
892 * @param dc the devclass to edit
893 * @param driver the driver to unregister
896 devclass_delete_driver(devclass_t busclass, driver_t *driver)
898 devclass_t dc = devclass_find(driver->name);
904 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
910 * Find the link structure in the bus' list of drivers.
912 TAILQ_FOREACH(dl, &busclass->drivers, link) {
913 if (dl->driver == driver)
918 PDEBUG(("%s not found in %s list", driver->name,
924 * Disassociate from any devices. We iterate through all the
925 * devices in the devclass of the driver and detach any which are
926 * using the driver and which have a parent in the devclass which
927 * we are deleting from.
929 * Note that since a driver can be in multiple devclasses, we
930 * should not detach devices which are not children of devices in
931 * the affected devclass.
933 for (i = 0; i < dc->maxunit; i++) {
934 if (dc->devices[i]) {
935 dev = dc->devices[i];
936 if (dev->driver == driver && dev->parent &&
937 dev->parent->devclass == busclass) {
938 if ((error = device_detach(dev)) != 0)
940 device_set_driver(dev, NULL);
945 TAILQ_REMOVE(&busclass->drivers, dl, link);
950 if (driver->refs == 0)
951 kobj_class_free((kobj_class_t) driver);
953 bus_data_generation_update();
958 * @brief Quiesces a set of device drivers from a device class
960 * Quiesce a device driver from a devclass. This is normally called
961 * automatically by DRIVER_MODULE().
963 * If the driver is currently attached to any devices,
964 * devclass_quiesece_driver() will first attempt to quiesce each
967 * @param dc the devclass to edit
968 * @param driver the driver to unregister
971 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
973 devclass_t dc = devclass_find(driver->name);
979 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
985 * Find the link structure in the bus' list of drivers.
987 TAILQ_FOREACH(dl, &busclass->drivers, link) {
988 if (dl->driver == driver)
993 PDEBUG(("%s not found in %s list", driver->name,
999 * Quiesce all devices. We iterate through all the devices in
1000 * the devclass of the driver and quiesce any which are using
1001 * the driver and which have a parent in the devclass which we
1004 * Note that since a driver can be in multiple devclasses, we
1005 * should not quiesce devices which are not children of
1006 * devices in the affected devclass.
1008 for (i = 0; i < dc->maxunit; i++) {
1009 if (dc->devices[i]) {
1010 dev = dc->devices[i];
1011 if (dev->driver == driver && dev->parent &&
1012 dev->parent->devclass == busclass) {
1013 if ((error = device_quiesce(dev)) != 0)
1026 devclass_find_driver_internal(devclass_t dc, const char *classname)
1030 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1032 TAILQ_FOREACH(dl, &dc->drivers, link) {
1033 if (!strcmp(dl->driver->name, classname))
1037 PDEBUG(("not found"));
1042 * @brief Search a devclass for a driver
1044 * This function searches the devclass's list of drivers and returns
1045 * the first driver whose name is @p classname or @c NULL if there is
1046 * no driver of that name.
1048 * @param dc the devclass to search
1049 * @param classname the driver name to search for
1052 devclass_find_driver(devclass_t dc, const char *classname)
1056 dl = devclass_find_driver_internal(dc, classname);
1058 return (dl->driver);
1063 * @brief Return the name of the devclass
1066 devclass_get_name(devclass_t dc)
1072 * @brief Find a device given a unit number
1074 * @param dc the devclass to search
1075 * @param unit the unit number to search for
1077 * @returns the device with the given unit number or @c
1078 * NULL if there is no such device
1081 devclass_get_device(devclass_t dc, int unit)
1083 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1085 return (dc->devices[unit]);
1089 * @brief Find the softc field of a device given a unit number
1091 * @param dc the devclass to search
1092 * @param unit the unit number to search for
1094 * @returns the softc field of the device with the given
1095 * unit number or @c NULL if there is no such
1099 devclass_get_softc(devclass_t dc, int unit)
1103 dev = devclass_get_device(dc, unit);
1107 return (device_get_softc(dev));
1111 * @brief Get a list of devices in the devclass
1113 * An array containing a list of all the devices in the given devclass
1114 * is allocated and returned in @p *devlistp. The number of devices
1115 * in the array is returned in @p *devcountp. The caller should free
1116 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1118 * @param dc the devclass to examine
1119 * @param devlistp points at location for array pointer return
1121 * @param devcountp points at location for array size return value
1124 * @retval ENOMEM the array allocation failed
1127 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1132 count = devclass_get_count(dc);
1133 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1138 for (i = 0; i < dc->maxunit; i++) {
1139 if (dc->devices[i]) {
1140 list[count] = dc->devices[i];
1152 * @brief Get a list of drivers in the devclass
1154 * An array containing a list of pointers to all the drivers in the
1155 * given devclass is allocated and returned in @p *listp. The number
1156 * of drivers in the array is returned in @p *countp. The caller should
1157 * free the array using @c free(p, M_TEMP).
1159 * @param dc the devclass to examine
1160 * @param listp gives location for array pointer return value
1161 * @param countp gives location for number of array elements
1165 * @retval ENOMEM the array allocation failed
1168 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1175 TAILQ_FOREACH(dl, &dc->drivers, link)
1177 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1182 TAILQ_FOREACH(dl, &dc->drivers, link) {
1183 list[count] = dl->driver;
1193 * @brief Get the number of devices in a devclass
1195 * @param dc the devclass to examine
1198 devclass_get_count(devclass_t dc)
1203 for (i = 0; i < dc->maxunit; i++)
1210 * @brief Get the maximum unit number used in a devclass
1212 * Note that this is one greater than the highest currently-allocated
1215 * @param dc the devclass to examine
1218 devclass_get_maxunit(devclass_t dc)
1220 return (dc->maxunit);
1224 * @brief Find a free unit number in a devclass
1226 * This function searches for the first unused unit number greater
1227 * that or equal to @p unit.
1229 * @param dc the devclass to examine
1230 * @param unit the first unit number to check
1233 devclass_find_free_unit(devclass_t dc, int unit)
1237 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1243 * @brief Set the parent of a devclass
1245 * The parent class is normally initialised automatically by
1248 * @param dc the devclass to edit
1249 * @param pdc the new parent devclass
1252 devclass_set_parent(devclass_t dc, devclass_t pdc)
1258 * @brief Get the parent of a devclass
1260 * @param dc the devclass to examine
1263 devclass_get_parent(devclass_t dc)
1265 return (dc->parent);
1268 struct sysctl_ctx_list *
1269 devclass_get_sysctl_ctx(devclass_t dc)
1271 return (&dc->sysctl_ctx);
1275 devclass_get_sysctl_tree(devclass_t dc)
1277 return (dc->sysctl_tree);
1282 * @brief Allocate a unit number
1284 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1285 * will do). The allocated unit number is returned in @p *unitp.
1287 * @param dc the devclass to allocate from
1288 * @param unitp points at the location for the allocated unit
1292 * @retval EEXIST the requested unit number is already allocated
1293 * @retval ENOMEM memory allocation failure
1296 devclass_alloc_unit(devclass_t dc, int *unitp)
1300 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1302 /* If we were given a wired unit number, check for existing device */
1305 if (unit >= 0 && unit < dc->maxunit &&
1306 dc->devices[unit] != NULL) {
1308 printf("%s: %s%d already exists; skipping it\n",
1309 dc->name, dc->name, *unitp);
1313 /* Unwired device, find the next available slot for it */
1315 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1320 * We've selected a unit beyond the length of the table, so let's
1321 * extend the table to make room for all units up to and including
1324 if (unit >= dc->maxunit) {
1328 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1329 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1332 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1333 bzero(newlist + dc->maxunit,
1334 sizeof(device_t) * (newsize - dc->maxunit));
1336 free(dc->devices, M_BUS);
1337 dc->devices = newlist;
1338 dc->maxunit = newsize;
1340 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1348 * @brief Add a device to a devclass
1350 * A unit number is allocated for the device (using the device's
1351 * preferred unit number if any) and the device is registered in the
1352 * devclass. This allows the device to be looked up by its unit
1353 * number, e.g. by decoding a dev_t minor number.
1355 * @param dc the devclass to add to
1356 * @param dev the device to add
1359 * @retval EEXIST the requested unit number is already allocated
1360 * @retval ENOMEM memory allocation failure
1363 devclass_add_device(devclass_t dc, device_t dev)
1367 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1369 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1372 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1376 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1377 free(dev->nameunit, M_BUS);
1378 dev->nameunit = NULL;
1381 dc->devices[dev->unit] = dev;
1383 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1390 * @brief Delete a device from a devclass
1392 * The device is removed from the devclass's device list and its unit
1395 * @param dc the devclass to delete from
1396 * @param dev the device to delete
1401 devclass_delete_device(devclass_t dc, device_t dev)
1406 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1408 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1409 panic("devclass_delete_device: inconsistent device class");
1410 dc->devices[dev->unit] = NULL;
1411 if (dev->flags & DF_WILDCARD)
1413 dev->devclass = NULL;
1414 free(dev->nameunit, M_BUS);
1415 dev->nameunit = NULL;
1422 * @brief Make a new device and add it as a child of @p parent
1424 * @param parent the parent of the new device
1425 * @param name the devclass name of the new device or @c NULL
1426 * to leave the devclass unspecified
1427 * @parem unit the unit number of the new device of @c -1 to
1428 * leave the unit number unspecified
1430 * @returns the new device
1433 make_device(device_t parent, const char *name, int unit)
1438 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1441 dc = devclass_find_internal(name, NULL, TRUE);
1443 printf("make_device: can't find device class %s\n",
1451 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1455 dev->parent = parent;
1456 TAILQ_INIT(&dev->children);
1457 kobj_init((kobj_t) dev, &null_class);
1459 dev->devclass = NULL;
1461 dev->nameunit = NULL;
1465 dev->flags = DF_ENABLED;
1468 dev->flags |= DF_WILDCARD;
1470 dev->flags |= DF_FIXEDCLASS;
1471 if (devclass_add_device(dc, dev)) {
1472 kobj_delete((kobj_t) dev, M_BUS);
1479 dev->state = DS_NOTPRESENT;
1481 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1482 bus_data_generation_update();
1489 * @brief Print a description of a device.
1492 device_print_child(device_t dev, device_t child)
1496 if (device_is_alive(child))
1497 retval += BUS_PRINT_CHILD(dev, child);
1499 retval += device_printf(child, " not found\n");
1505 * @brief Create a new device
1507 * This creates a new device and adds it as a child of an existing
1508 * parent device. The new device will be added after the last existing
1509 * child with order zero.
1511 * @param dev the device which will be the parent of the
1513 * @param name devclass name for new device or @c NULL if not
1515 * @param unit unit number for new device or @c -1 if not
1518 * @returns the new device
1521 device_add_child(device_t dev, const char *name, int unit)
1523 return (device_add_child_ordered(dev, 0, name, unit));
1527 * @brief Create a new device
1529 * This creates a new device and adds it as a child of an existing
1530 * parent device. The new device will be added after the last existing
1531 * child with the same order.
1533 * @param dev the device which will be the parent of the
1535 * @param order a value which is used to partially sort the
1536 * children of @p dev - devices created using
1537 * lower values of @p order appear first in @p
1538 * dev's list of children
1539 * @param name devclass name for new device or @c NULL if not
1541 * @param unit unit number for new device or @c -1 if not
1544 * @returns the new device
1547 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1552 PDEBUG(("%s at %s with order %d as unit %d",
1553 name, DEVICENAME(dev), order, unit));
1555 child = make_device(dev, name, unit);
1558 child->order = order;
1560 TAILQ_FOREACH(place, &dev->children, link) {
1561 if (place->order > order)
1567 * The device 'place' is the first device whose order is
1568 * greater than the new child.
1570 TAILQ_INSERT_BEFORE(place, child, link);
1573 * The new child's order is greater or equal to the order of
1574 * any existing device. Add the child to the tail of the list.
1576 TAILQ_INSERT_TAIL(&dev->children, child, link);
1579 bus_data_generation_update();
1584 * @brief Delete a device
1586 * This function deletes a device along with all of its children. If
1587 * the device currently has a driver attached to it, the device is
1588 * detached first using device_detach().
1590 * @param dev the parent device
1591 * @param child the device to delete
1594 * @retval non-zero a unit error code describing the error
1597 device_delete_child(device_t dev, device_t child)
1600 device_t grandchild;
1602 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1604 /* remove children first */
1605 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1606 error = device_delete_child(child, grandchild);
1611 if ((error = device_detach(child)) != 0)
1613 if (child->devclass)
1614 devclass_delete_device(child->devclass, child);
1615 TAILQ_REMOVE(&dev->children, child, link);
1616 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1617 kobj_delete((kobj_t) child, M_BUS);
1619 bus_data_generation_update();
1624 * @brief Find a device given a unit number
1626 * This is similar to devclass_get_devices() but only searches for
1627 * devices which have @p dev as a parent.
1629 * @param dev the parent device to search
1630 * @param unit the unit number to search for. If the unit is -1,
1631 * return the first child of @p dev which has name
1632 * @p classname (that is, the one with the lowest unit.)
1634 * @returns the device with the given unit number or @c
1635 * NULL if there is no such device
1638 device_find_child(device_t dev, const char *classname, int unit)
1643 dc = devclass_find(classname);
1648 child = devclass_get_device(dc, unit);
1649 if (child && child->parent == dev)
1652 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1653 child = devclass_get_device(dc, unit);
1654 if (child && child->parent == dev)
1665 first_matching_driver(devclass_t dc, device_t dev)
1668 return (devclass_find_driver_internal(dc, dev->devclass->name));
1669 return (TAILQ_FIRST(&dc->drivers));
1676 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1678 if (dev->devclass) {
1680 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1681 if (!strcmp(dev->devclass->name, dl->driver->name))
1685 return (TAILQ_NEXT(last, link));
1692 device_probe_child(device_t dev, device_t child)
1695 driverlink_t best = NULL;
1697 int result, pri = 0;
1698 int hasclass = (child->devclass != 0);
1704 panic("device_probe_child: parent device has no devclass");
1707 * If the state is already probed, then return. However, don't
1708 * return if we can rebid this object.
1710 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1713 for (; dc; dc = dc->parent) {
1714 for (dl = first_matching_driver(dc, child);
1716 dl = next_matching_driver(dc, child, dl)) {
1717 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1718 device_set_driver(child, dl->driver);
1720 device_set_devclass(child, dl->driver->name);
1722 /* Fetch any flags for the device before probing. */
1723 resource_int_value(dl->driver->name, child->unit,
1724 "flags", &child->devflags);
1726 result = DEVICE_PROBE(child);
1728 /* Reset flags and devclass before the next probe. */
1729 child->devflags = 0;
1731 device_set_devclass(child, NULL);
1734 * If the driver returns SUCCESS, there can be
1735 * no higher match for this device.
1744 * The driver returned an error so it
1745 * certainly doesn't match.
1748 device_set_driver(child, NULL);
1753 * A priority lower than SUCCESS, remember the
1754 * best matching driver. Initialise the value
1755 * of pri for the first match.
1757 if (best == NULL || result > pri) {
1764 * If we have an unambiguous match in this devclass,
1765 * don't look in the parent.
1767 if (best && pri == 0)
1772 * If we found a driver, change state and initialise the devclass.
1774 /* XXX What happens if we rebid and got no best? */
1777 * If this device was atached, and we were asked to
1778 * rescan, and it is a different driver, then we have
1779 * to detach the old driver and reattach this new one.
1780 * Note, we don't have to check for DF_REBID here
1781 * because if the state is > DS_ALIVE, we know it must
1784 * This assumes that all DF_REBID drivers can have
1785 * their probe routine called at any time and that
1786 * they are idempotent as well as completely benign in
1787 * normal operations.
1789 * We also have to make sure that the detach
1790 * succeeded, otherwise we fail the operation (or
1791 * maybe it should just fail silently? I'm torn).
1793 if (child->state > DS_ALIVE && best->driver != child->driver)
1794 if ((result = device_detach(dev)) != 0)
1797 /* Set the winning driver, devclass, and flags. */
1798 if (!child->devclass)
1799 device_set_devclass(child, best->driver->name);
1800 device_set_driver(child, best->driver);
1801 resource_int_value(best->driver->name, child->unit,
1802 "flags", &child->devflags);
1806 * A bit bogus. Call the probe method again to make
1807 * sure that we have the right description.
1809 DEVICE_PROBE(child);
1811 child->flags |= DF_REBID;
1814 child->flags &= ~DF_REBID;
1815 child->state = DS_ALIVE;
1817 bus_data_generation_update();
1825 * @brief Return the parent of a device
1828 device_get_parent(device_t dev)
1830 return (dev->parent);
1834 * @brief Get a list of children of a device
1836 * An array containing a list of all the children of the given device
1837 * is allocated and returned in @p *devlistp. The number of devices
1838 * in the array is returned in @p *devcountp. The caller should free
1839 * the array using @c free(p, M_TEMP).
1841 * @param dev the device to examine
1842 * @param devlistp points at location for array pointer return
1844 * @param devcountp points at location for array size return value
1847 * @retval ENOMEM the array allocation failed
1850 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1857 TAILQ_FOREACH(child, &dev->children, link) {
1861 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1866 TAILQ_FOREACH(child, &dev->children, link) {
1867 list[count] = child;
1878 * @brief Return the current driver for the device or @c NULL if there
1879 * is no driver currently attached
1882 device_get_driver(device_t dev)
1884 return (dev->driver);
1888 * @brief Return the current devclass for the device or @c NULL if
1892 device_get_devclass(device_t dev)
1894 return (dev->devclass);
1898 * @brief Return the name of the device's devclass or @c NULL if there
1902 device_get_name(device_t dev)
1904 if (dev != NULL && dev->devclass)
1905 return (devclass_get_name(dev->devclass));
1910 * @brief Return a string containing the device's devclass name
1911 * followed by an ascii representation of the device's unit number
1915 device_get_nameunit(device_t dev)
1917 return (dev->nameunit);
1921 * @brief Return the device's unit number.
1924 device_get_unit(device_t dev)
1930 * @brief Return the device's description string
1933 device_get_desc(device_t dev)
1939 * @brief Return the device's flags
1942 device_get_flags(device_t dev)
1944 return (dev->devflags);
1947 struct sysctl_ctx_list *
1948 device_get_sysctl_ctx(device_t dev)
1950 return (&dev->sysctl_ctx);
1954 device_get_sysctl_tree(device_t dev)
1956 return (dev->sysctl_tree);
1960 * @brief Print the name of the device followed by a colon and a space
1962 * @returns the number of characters printed
1965 device_print_prettyname(device_t dev)
1967 const char *name = device_get_name(dev);
1970 return (printf("unknown: "));
1971 return (printf("%s%d: ", name, device_get_unit(dev)));
1975 * @brief Print the name of the device followed by a colon, a space
1976 * and the result of calling vprintf() with the value of @p fmt and
1977 * the following arguments.
1979 * @returns the number of characters printed
1982 device_printf(device_t dev, const char * fmt, ...)
1987 retval = device_print_prettyname(dev);
1989 retval += vprintf(fmt, ap);
1998 device_set_desc_internal(device_t dev, const char* desc, int copy)
2000 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
2001 free(dev->desc, M_BUS);
2002 dev->flags &= ~DF_DESCMALLOCED;
2007 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
2009 strcpy(dev->desc, desc);
2010 dev->flags |= DF_DESCMALLOCED;
2013 /* Avoid a -Wcast-qual warning */
2014 dev->desc = (char *)(uintptr_t) desc;
2017 bus_data_generation_update();
2021 * @brief Set the device's description
2023 * The value of @c desc should be a string constant that will not
2024 * change (at least until the description is changed in a subsequent
2025 * call to device_set_desc() or device_set_desc_copy()).
2028 device_set_desc(device_t dev, const char* desc)
2030 device_set_desc_internal(dev, desc, FALSE);
2034 * @brief Set the device's description
2036 * The string pointed to by @c desc is copied. Use this function if
2037 * the device description is generated, (e.g. with sprintf()).
2040 device_set_desc_copy(device_t dev, const char* desc)
2042 device_set_desc_internal(dev, desc, TRUE);
2046 * @brief Set the device's flags
2049 device_set_flags(device_t dev, u_int32_t flags)
2051 dev->devflags = flags;
2055 * @brief Return the device's softc field
2057 * The softc is allocated and zeroed when a driver is attached, based
2058 * on the size field of the driver.
2061 device_get_softc(device_t dev)
2063 return (dev->softc);
2067 * @brief Set the device's softc field
2069 * Most drivers do not need to use this since the softc is allocated
2070 * automatically when the driver is attached.
2073 device_set_softc(device_t dev, void *softc)
2075 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2076 free(dev->softc, M_BUS_SC);
2079 dev->flags |= DF_EXTERNALSOFTC;
2081 dev->flags &= ~DF_EXTERNALSOFTC;
2085 * @brief Get the device's ivars field
2087 * The ivars field is used by the parent device to store per-device
2088 * state (e.g. the physical location of the device or a list of
2092 device_get_ivars(device_t dev)
2095 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2096 return (dev->ivars);
2100 * @brief Set the device's ivars field
2103 device_set_ivars(device_t dev, void * ivars)
2106 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2111 * @brief Return the device's state
2114 device_get_state(device_t dev)
2116 return (dev->state);
2120 * @brief Set the DF_ENABLED flag for the device
2123 device_enable(device_t dev)
2125 dev->flags |= DF_ENABLED;
2129 * @brief Clear the DF_ENABLED flag for the device
2132 device_disable(device_t dev)
2134 dev->flags &= ~DF_ENABLED;
2138 * @brief Increment the busy counter for the device
2141 device_busy(device_t dev)
2143 if (dev->state < DS_ATTACHED)
2144 panic("device_busy: called for unattached device");
2145 if (dev->busy == 0 && dev->parent)
2146 device_busy(dev->parent);
2148 dev->state = DS_BUSY;
2152 * @brief Decrement the busy counter for the device
2155 device_unbusy(device_t dev)
2157 if (dev->state != DS_BUSY)
2158 panic("device_unbusy: called for non-busy device %s",
2159 device_get_nameunit(dev));
2161 if (dev->busy == 0) {
2163 device_unbusy(dev->parent);
2164 dev->state = DS_ATTACHED;
2169 * @brief Set the DF_QUIET flag for the device
2172 device_quiet(device_t dev)
2174 dev->flags |= DF_QUIET;
2178 * @brief Clear the DF_QUIET flag for the device
2181 device_verbose(device_t dev)
2183 dev->flags &= ~DF_QUIET;
2187 * @brief Return non-zero if the DF_QUIET flag is set on the device
2190 device_is_quiet(device_t dev)
2192 return ((dev->flags & DF_QUIET) != 0);
2196 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2199 device_is_enabled(device_t dev)
2201 return ((dev->flags & DF_ENABLED) != 0);
2205 * @brief Return non-zero if the device was successfully probed
2208 device_is_alive(device_t dev)
2210 return (dev->state >= DS_ALIVE);
2214 * @brief Return non-zero if the device currently has a driver
2218 device_is_attached(device_t dev)
2220 return (dev->state >= DS_ATTACHED);
2224 * @brief Set the devclass of a device
2225 * @see devclass_add_device().
2228 device_set_devclass(device_t dev, const char *classname)
2235 devclass_delete_device(dev->devclass, dev);
2239 if (dev->devclass) {
2240 printf("device_set_devclass: device class already set\n");
2244 dc = devclass_find_internal(classname, NULL, TRUE);
2248 error = devclass_add_device(dc, dev);
2250 bus_data_generation_update();
2255 * @brief Set the driver of a device
2258 * @retval EBUSY the device already has a driver attached
2259 * @retval ENOMEM a memory allocation failure occurred
2262 device_set_driver(device_t dev, driver_t *driver)
2264 if (dev->state >= DS_ATTACHED)
2267 if (dev->driver == driver)
2270 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2271 free(dev->softc, M_BUS_SC);
2274 kobj_delete((kobj_t) dev, NULL);
2275 dev->driver = driver;
2277 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2278 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2279 dev->softc = malloc(driver->size, M_BUS_SC,
2282 kobj_delete((kobj_t) dev, NULL);
2283 kobj_init((kobj_t) dev, &null_class);
2289 kobj_init((kobj_t) dev, &null_class);
2292 bus_data_generation_update();
2297 * @brief Probe a device and attach a driver if possible
2299 * This function is the core of the device autoconfiguration
2300 * system. Its purpose is to select a suitable driver for a device and
2301 * then call that driver to initialise the hardware appropriately. The
2302 * driver is selected by calling the DEVICE_PROBE() method of a set of
2303 * candidate drivers and then choosing the driver which returned the
2304 * best value. This driver is then attached to the device using
2307 * The set of suitable drivers is taken from the list of drivers in
2308 * the parent device's devclass. If the device was originally created
2309 * with a specific class name (see device_add_child()), only drivers
2310 * with that name are probed, otherwise all drivers in the devclass
2311 * are probed. If no drivers return successful probe values in the
2312 * parent devclass, the search continues in the parent of that
2313 * devclass (see devclass_get_parent()) if any.
2315 * @param dev the device to initialise
2318 * @retval ENXIO no driver was found
2319 * @retval ENOMEM memory allocation failure
2320 * @retval non-zero some other unix error code
2323 device_probe_and_attach(device_t dev)
2329 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2332 if (!(dev->flags & DF_ENABLED)) {
2333 if (bootverbose && device_get_name(dev) != NULL) {
2334 device_print_prettyname(dev);
2335 printf("not probed (disabled)\n");
2339 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2340 if (!(dev->flags & DF_DONENOMATCH)) {
2341 BUS_PROBE_NOMATCH(dev->parent, dev);
2343 dev->flags |= DF_DONENOMATCH;
2347 error = device_attach(dev);
2353 * @brief Attach a device driver to a device
2355 * This function is a wrapper around the DEVICE_ATTACH() driver
2356 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2357 * device's sysctl tree, optionally prints a description of the device
2358 * and queues a notification event for user-based device management
2361 * Normally this function is only called internally from
2362 * device_probe_and_attach().
2364 * @param dev the device to initialise
2367 * @retval ENXIO no driver was found
2368 * @retval ENOMEM memory allocation failure
2369 * @retval non-zero some other unix error code
2372 device_attach(device_t dev)
2376 device_sysctl_init(dev);
2377 if (!device_is_quiet(dev))
2378 device_print_child(dev->parent, dev);
2379 if ((error = DEVICE_ATTACH(dev)) != 0) {
2380 printf("device_attach: %s%d attach returned %d\n",
2381 dev->driver->name, dev->unit, error);
2382 /* Unset the class; set in device_probe_child */
2383 if (dev->devclass == NULL)
2384 device_set_devclass(dev, NULL);
2385 device_set_driver(dev, NULL);
2386 device_sysctl_fini(dev);
2387 dev->state = DS_NOTPRESENT;
2390 dev->state = DS_ATTACHED;
2396 * @brief Detach a driver from a device
2398 * This function is a wrapper around the DEVICE_DETACH() driver
2399 * method. If the call to DEVICE_DETACH() succeeds, it calls
2400 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2401 * notification event for user-based device management services and
2402 * cleans up the device's sysctl tree.
2404 * @param dev the device to un-initialise
2407 * @retval ENXIO no driver was found
2408 * @retval ENOMEM memory allocation failure
2409 * @retval non-zero some other unix error code
2412 device_detach(device_t dev)
2418 PDEBUG(("%s", DEVICENAME(dev)));
2419 if (dev->state == DS_BUSY)
2421 if (dev->state != DS_ATTACHED)
2424 if ((error = DEVICE_DETACH(dev)) != 0)
2427 device_printf(dev, "detached\n");
2429 BUS_CHILD_DETACHED(dev->parent, dev);
2431 if (!(dev->flags & DF_FIXEDCLASS))
2432 devclass_delete_device(dev->devclass, dev);
2434 dev->state = DS_NOTPRESENT;
2435 device_set_driver(dev, NULL);
2436 device_set_desc(dev, NULL);
2437 device_sysctl_fini(dev);
2443 * @brief Tells a driver to quiesce itself.
2445 * This function is a wrapper around the DEVICE_QUIESCE() driver
2446 * method. If the call to DEVICE_QUIESCE() succeeds.
2448 * @param dev the device to quiesce
2451 * @retval ENXIO no driver was found
2452 * @retval ENOMEM memory allocation failure
2453 * @retval non-zero some other unix error code
2456 device_quiesce(device_t dev)
2459 PDEBUG(("%s", DEVICENAME(dev)));
2460 if (dev->state == DS_BUSY)
2462 if (dev->state != DS_ATTACHED)
2465 return (DEVICE_QUIESCE(dev));
2469 * @brief Notify a device of system shutdown
2471 * This function calls the DEVICE_SHUTDOWN() driver method if the
2472 * device currently has an attached driver.
2474 * @returns the value returned by DEVICE_SHUTDOWN()
2477 device_shutdown(device_t dev)
2479 if (dev->state < DS_ATTACHED)
2481 return (DEVICE_SHUTDOWN(dev));
2485 * @brief Set the unit number of a device
2487 * This function can be used to override the unit number used for a
2488 * device (e.g. to wire a device to a pre-configured unit number).
2491 device_set_unit(device_t dev, int unit)
2496 dc = device_get_devclass(dev);
2497 if (unit < dc->maxunit && dc->devices[unit])
2499 err = devclass_delete_device(dc, dev);
2503 err = devclass_add_device(dc, dev);
2507 bus_data_generation_update();
2511 /*======================================*/
2513 * Some useful method implementations to make life easier for bus drivers.
2517 * @brief Initialise a resource list.
2519 * @param rl the resource list to initialise
2522 resource_list_init(struct resource_list *rl)
2528 * @brief Reclaim memory used by a resource list.
2530 * This function frees the memory for all resource entries on the list
2533 * @param rl the resource list to free
2536 resource_list_free(struct resource_list *rl)
2538 struct resource_list_entry *rle;
2540 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2542 panic("resource_list_free: resource entry is busy");
2543 STAILQ_REMOVE_HEAD(rl, link);
2549 * @brief Add a resource entry.
2551 * This function adds a resource entry using the given @p type, @p
2552 * start, @p end and @p count values. A rid value is chosen by
2553 * searching sequentially for the first unused rid starting at zero.
2555 * @param rl the resource list to edit
2556 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2557 * @param start the start address of the resource
2558 * @param end the end address of the resource
2559 * @param count XXX end-start+1
2562 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2563 u_long end, u_long count)
2568 while (resource_list_find(rl, type, rid) != NULL)
2570 resource_list_add(rl, type, rid, start, end, count);
2575 * @brief Add or modify a resource entry.
2577 * If an existing entry exists with the same type and rid, it will be
2578 * modified using the given values of @p start, @p end and @p
2579 * count. If no entry exists, a new one will be created using the
2580 * given values. The resource list entry that matches is then returned.
2582 * @param rl the resource list to edit
2583 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2584 * @param rid the resource identifier
2585 * @param start the start address of the resource
2586 * @param end the end address of the resource
2587 * @param count XXX end-start+1
2589 struct resource_list_entry *
2590 resource_list_add(struct resource_list *rl, int type, int rid,
2591 u_long start, u_long end, u_long count)
2593 struct resource_list_entry *rle;
2595 rle = resource_list_find(rl, type, rid);
2597 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2600 panic("resource_list_add: can't record entry");
2601 STAILQ_INSERT_TAIL(rl, rle, link);
2608 panic("resource_list_add: resource entry is busy");
2617 * @brief Find a resource entry by type and rid.
2619 * @param rl the resource list to search
2620 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2621 * @param rid the resource identifier
2623 * @returns the resource entry pointer or NULL if there is no such
2626 struct resource_list_entry *
2627 resource_list_find(struct resource_list *rl, int type, int rid)
2629 struct resource_list_entry *rle;
2631 STAILQ_FOREACH(rle, rl, link) {
2632 if (rle->type == type && rle->rid == rid)
2639 * @brief Delete a resource entry.
2641 * @param rl the resource list to edit
2642 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2643 * @param rid the resource identifier
2646 resource_list_delete(struct resource_list *rl, int type, int rid)
2648 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2651 if (rle->res != NULL)
2652 panic("resource_list_delete: resource has not been released");
2653 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2659 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2661 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2662 * and passing the allocation up to the parent of @p bus. This assumes
2663 * that the first entry of @c device_get_ivars(child) is a struct
2664 * resource_list. This also handles 'passthrough' allocations where a
2665 * child is a remote descendant of bus by passing the allocation up to
2666 * the parent of bus.
2668 * Typically, a bus driver would store a list of child resources
2669 * somewhere in the child device's ivars (see device_get_ivars()) and
2670 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2671 * then call resource_list_alloc() to perform the allocation.
2673 * @param rl the resource list to allocate from
2674 * @param bus the parent device of @p child
2675 * @param child the device which is requesting an allocation
2676 * @param type the type of resource to allocate
2677 * @param rid a pointer to the resource identifier
2678 * @param start hint at the start of the resource range - pass
2679 * @c 0UL for any start address
2680 * @param end hint at the end of the resource range - pass
2681 * @c ~0UL for any end address
2682 * @param count hint at the size of range required - pass @c 1
2684 * @param flags any extra flags to control the resource
2685 * allocation - see @c RF_XXX flags in
2686 * <sys/rman.h> for details
2688 * @returns the resource which was allocated or @c NULL if no
2689 * resource could be allocated
2692 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2693 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2695 struct resource_list_entry *rle = NULL;
2696 int passthrough = (device_get_parent(child) != bus);
2697 int isdefault = (start == 0UL && end == ~0UL);
2700 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2701 type, rid, start, end, count, flags));
2704 rle = resource_list_find(rl, type, *rid);
2707 return (NULL); /* no resource of that type/rid */
2710 panic("resource_list_alloc: resource entry is busy");
2714 count = ulmax(count, rle->count);
2715 end = ulmax(rle->end, start + count - 1);
2718 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2719 type, rid, start, end, count, flags);
2722 * Record the new range.
2725 rle->start = rman_get_start(rle->res);
2726 rle->end = rman_get_end(rle->res);
2734 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
2736 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
2737 * used with resource_list_alloc().
2739 * @param rl the resource list which was allocated from
2740 * @param bus the parent device of @p child
2741 * @param child the device which is requesting a release
2742 * @param type the type of resource to allocate
2743 * @param rid the resource identifier
2744 * @param res the resource to release
2747 * @retval non-zero a standard unix error code indicating what
2748 * error condition prevented the operation
2751 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
2752 int type, int rid, struct resource *res)
2754 struct resource_list_entry *rle = NULL;
2755 int passthrough = (device_get_parent(child) != bus);
2759 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2763 rle = resource_list_find(rl, type, rid);
2766 panic("resource_list_release: can't find resource");
2768 panic("resource_list_release: resource entry is not busy");
2770 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2780 * @brief Print a description of resources in a resource list
2782 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
2783 * The name is printed if at least one resource of the given type is available.
2784 * The format is used to print resource start and end.
2786 * @param rl the resource list to print
2787 * @param name the name of @p type, e.g. @c "memory"
2788 * @param type type type of resource entry to print
2789 * @param format printf(9) format string to print resource
2790 * start and end values
2792 * @returns the number of characters printed
2795 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2798 struct resource_list_entry *rle;
2799 int printed, retval;
2803 /* Yes, this is kinda cheating */
2804 STAILQ_FOREACH(rle, rl, link) {
2805 if (rle->type == type) {
2807 retval += printf(" %s ", name);
2809 retval += printf(",");
2811 retval += printf(format, rle->start);
2812 if (rle->count > 1) {
2813 retval += printf("-");
2814 retval += printf(format, rle->start +
2823 * @brief Releases all the resources in a list.
2825 * @param rl The resource list to purge.
2830 resource_list_purge(struct resource_list *rl)
2832 struct resource_list_entry *rle;
2834 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2836 bus_release_resource(rman_get_device(rle->res),
2837 rle->type, rle->rid, rle->res);
2838 STAILQ_REMOVE_HEAD(rl, link);
2844 bus_generic_add_child(device_t dev, int order, const char *name, int unit)
2847 return (device_add_child_ordered(dev, order, name, unit));
2851 * @brief Helper function for implementing DEVICE_PROBE()
2853 * This function can be used to help implement the DEVICE_PROBE() for
2854 * a bus (i.e. a device which has other devices attached to it). It
2855 * calls the DEVICE_IDENTIFY() method of each driver in the device's
2859 bus_generic_probe(device_t dev)
2861 devclass_t dc = dev->devclass;
2864 TAILQ_FOREACH(dl, &dc->drivers, link) {
2865 DEVICE_IDENTIFY(dl->driver, dev);
2872 * @brief Helper function for implementing DEVICE_ATTACH()
2874 * This function can be used to help implement the DEVICE_ATTACH() for
2875 * a bus. It calls device_probe_and_attach() for each of the device's
2879 bus_generic_attach(device_t dev)
2883 TAILQ_FOREACH(child, &dev->children, link) {
2884 device_probe_and_attach(child);
2891 * @brief Helper function for implementing DEVICE_DETACH()
2893 * This function can be used to help implement the DEVICE_DETACH() for
2894 * a bus. It calls device_detach() for each of the device's
2898 bus_generic_detach(device_t dev)
2903 if (dev->state != DS_ATTACHED)
2906 TAILQ_FOREACH(child, &dev->children, link) {
2907 if ((error = device_detach(child)) != 0)
2915 * @brief Helper function for implementing DEVICE_SHUTDOWN()
2917 * This function can be used to help implement the DEVICE_SHUTDOWN()
2918 * for a bus. It calls device_shutdown() for each of the device's
2922 bus_generic_shutdown(device_t dev)
2926 TAILQ_FOREACH(child, &dev->children, link) {
2927 device_shutdown(child);
2934 * @brief Helper function for implementing DEVICE_SUSPEND()
2936 * This function can be used to help implement the DEVICE_SUSPEND()
2937 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
2938 * children. If any call to DEVICE_SUSPEND() fails, the suspend
2939 * operation is aborted and any devices which were suspended are
2940 * resumed immediately by calling their DEVICE_RESUME() methods.
2943 bus_generic_suspend(device_t dev)
2946 device_t child, child2;
2948 TAILQ_FOREACH(child, &dev->children, link) {
2949 error = DEVICE_SUSPEND(child);
2951 for (child2 = TAILQ_FIRST(&dev->children);
2952 child2 && child2 != child;
2953 child2 = TAILQ_NEXT(child2, link))
2954 DEVICE_RESUME(child2);
2962 * @brief Helper function for implementing DEVICE_RESUME()
2964 * This function can be used to help implement the DEVICE_RESUME() for
2965 * a bus. It calls DEVICE_RESUME() on each of the device's children.
2968 bus_generic_resume(device_t dev)
2972 TAILQ_FOREACH(child, &dev->children, link) {
2973 DEVICE_RESUME(child);
2974 /* if resume fails, there's nothing we can usefully do... */
2980 * @brief Helper function for implementing BUS_PRINT_CHILD().
2982 * This function prints the first part of the ascii representation of
2983 * @p child, including its name, unit and description (if any - see
2984 * device_set_desc()).
2986 * @returns the number of characters printed
2989 bus_print_child_header(device_t dev, device_t child)
2993 if (device_get_desc(child)) {
2994 retval += device_printf(child, "<%s>", device_get_desc(child));
2996 retval += printf("%s", device_get_nameunit(child));
3003 * @brief Helper function for implementing BUS_PRINT_CHILD().
3005 * This function prints the last part of the ascii representation of
3006 * @p child, which consists of the string @c " on " followed by the
3007 * name and unit of the @p dev.
3009 * @returns the number of characters printed
3012 bus_print_child_footer(device_t dev, device_t child)
3014 return (printf(" on %s\n", device_get_nameunit(dev)));
3018 * @brief Helper function for implementing BUS_PRINT_CHILD().
3020 * This function simply calls bus_print_child_header() followed by
3021 * bus_print_child_footer().
3023 * @returns the number of characters printed
3026 bus_generic_print_child(device_t dev, device_t child)
3030 retval += bus_print_child_header(dev, child);
3031 retval += bus_print_child_footer(dev, child);
3037 * @brief Stub function for implementing BUS_READ_IVAR().
3042 bus_generic_read_ivar(device_t dev, device_t child, int index,
3049 * @brief Stub function for implementing BUS_WRITE_IVAR().
3054 bus_generic_write_ivar(device_t dev, device_t child, int index,
3061 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3065 struct resource_list *
3066 bus_generic_get_resource_list(device_t dev, device_t child)
3072 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3074 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3075 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3076 * and then calls device_probe_and_attach() for each unattached child.
3079 bus_generic_driver_added(device_t dev, driver_t *driver)
3083 DEVICE_IDENTIFY(driver, dev);
3084 TAILQ_FOREACH(child, &dev->children, link) {
3085 if (child->state == DS_NOTPRESENT ||
3086 (child->flags & DF_REBID))
3087 device_probe_and_attach(child);
3092 * @brief Helper function for implementing BUS_SETUP_INTR().
3094 * This simple implementation of BUS_SETUP_INTR() simply calls the
3095 * BUS_SETUP_INTR() method of the parent of @p dev.
3098 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3099 int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg,
3102 /* Propagate up the bus hierarchy until someone handles it. */
3104 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3105 filter, intr, arg, cookiep));
3110 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3112 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3113 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3116 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3119 /* Propagate up the bus hierarchy until someone handles it. */
3121 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3126 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3128 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3129 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3132 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3133 u_long start, u_long end, u_long count, u_int flags)
3135 /* Propagate up the bus hierarchy until someone handles it. */
3137 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3138 start, end, count, flags));
3143 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3145 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3146 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3149 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3152 /* Propagate up the bus hierarchy until someone handles it. */
3154 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3160 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3162 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3163 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3166 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3169 /* Propagate up the bus hierarchy until someone handles it. */
3171 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3177 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3179 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3180 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3183 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3184 int rid, struct resource *r)
3186 /* Propagate up the bus hierarchy until someone handles it. */
3188 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3194 * @brief Helper function for implementing BUS_CONFIG_INTR().
3196 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3197 * BUS_CONFIG_INTR() method of the parent of @p dev.
3200 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3201 enum intr_polarity pol)
3204 /* Propagate up the bus hierarchy until someone handles it. */
3206 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3211 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3213 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3214 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3217 bus_generic_get_dma_tag(device_t dev, device_t child)
3220 /* Propagate up the bus hierarchy until someone handles it. */
3221 if (dev->parent != NULL)
3222 return (BUS_GET_DMA_TAG(dev->parent, child));
3227 * @brief Helper function for implementing BUS_GET_RESOURCE().
3229 * This implementation of BUS_GET_RESOURCE() uses the
3230 * resource_list_find() function to do most of the work. It calls
3231 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3235 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3236 u_long *startp, u_long *countp)
3238 struct resource_list * rl = NULL;
3239 struct resource_list_entry * rle = NULL;
3241 rl = BUS_GET_RESOURCE_LIST(dev, child);
3245 rle = resource_list_find(rl, type, rid);
3250 *startp = rle->start;
3252 *countp = rle->count;
3258 * @brief Helper function for implementing BUS_SET_RESOURCE().
3260 * This implementation of BUS_SET_RESOURCE() uses the
3261 * resource_list_add() function to do most of the work. It calls
3262 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3266 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3267 u_long start, u_long count)
3269 struct resource_list * rl = NULL;
3271 rl = BUS_GET_RESOURCE_LIST(dev, child);
3275 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3281 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3283 * This implementation of BUS_DELETE_RESOURCE() uses the
3284 * resource_list_delete() function to do most of the work. It calls
3285 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3289 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3291 struct resource_list * rl = NULL;
3293 rl = BUS_GET_RESOURCE_LIST(dev, child);
3297 resource_list_delete(rl, type, rid);
3303 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3305 * This implementation of BUS_RELEASE_RESOURCE() uses the
3306 * resource_list_release() function to do most of the work. It calls
3307 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3310 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3311 int rid, struct resource *r)
3313 struct resource_list * rl = NULL;
3315 rl = BUS_GET_RESOURCE_LIST(dev, child);
3319 return (resource_list_release(rl, dev, child, type, rid, r));
3323 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3325 * This implementation of BUS_ALLOC_RESOURCE() uses the
3326 * resource_list_alloc() function to do most of the work. It calls
3327 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3330 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3331 int *rid, u_long start, u_long end, u_long count, u_int flags)
3333 struct resource_list * rl = NULL;
3335 rl = BUS_GET_RESOURCE_LIST(dev, child);
3339 return (resource_list_alloc(rl, dev, child, type, rid,
3340 start, end, count, flags));
3344 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3346 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3347 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3350 bus_generic_child_present(device_t dev, device_t child)
3352 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3356 * Some convenience functions to make it easier for drivers to use the
3357 * resource-management functions. All these really do is hide the
3358 * indirection through the parent's method table, making for slightly
3359 * less-wordy code. In the future, it might make sense for this code
3360 * to maintain some sort of a list of resources allocated by each device.
3364 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3365 struct resource **res)
3369 for (i = 0; rs[i].type != -1; i++)
3371 for (i = 0; rs[i].type != -1; i++) {
3372 res[i] = bus_alloc_resource_any(dev,
3373 rs[i].type, &rs[i].rid, rs[i].flags);
3374 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
3375 bus_release_resources(dev, rs, res);
3383 bus_release_resources(device_t dev, const struct resource_spec *rs,
3384 struct resource **res)
3388 for (i = 0; rs[i].type != -1; i++)
3389 if (res[i] != NULL) {
3390 bus_release_resource(
3391 dev, rs[i].type, rs[i].rid, res[i]);
3397 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3399 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3403 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3404 u_long count, u_int flags)
3406 if (dev->parent == NULL)
3408 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3413 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3415 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3419 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3421 if (dev->parent == NULL)
3423 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3427 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3429 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3433 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3435 if (dev->parent == NULL)
3437 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3441 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3443 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3447 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3449 if (dev->parent == NULL)
3451 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3455 * @brief Wrapper function for BUS_SETUP_INTR().
3457 * This function simply calls the BUS_SETUP_INTR() method of the
3461 bus_setup_intr(device_t dev, struct resource *r, int flags,
3462 driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
3466 if (dev->parent != NULL) {
3467 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) &&
3469 flags &= ~INTR_MPSAFE;
3470 error = BUS_SETUP_INTR(dev->parent, dev, r, flags,
3471 filter, handler, arg, cookiep);
3473 if (handler != NULL && !(flags & INTR_MPSAFE))
3474 device_printf(dev, "[GIANT-LOCKED]\n");
3475 if (bootverbose && (flags & INTR_MPSAFE))
3476 device_printf(dev, "[MPSAFE]\n");
3477 if (filter != NULL) {
3478 if (handler == NULL)
3479 device_printf(dev, "[FILTER]\n");
3481 device_printf(dev, "[FILTER+ITHREAD]\n");
3483 device_printf(dev, "[ITHREAD]\n");
3491 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3493 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3497 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3499 if (dev->parent == NULL)
3501 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3505 * @brief Wrapper function for BUS_SET_RESOURCE().
3507 * This function simply calls the BUS_SET_RESOURCE() method of the
3511 bus_set_resource(device_t dev, int type, int rid,
3512 u_long start, u_long count)
3514 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3519 * @brief Wrapper function for BUS_GET_RESOURCE().
3521 * This function simply calls the BUS_GET_RESOURCE() method of the
3525 bus_get_resource(device_t dev, int type, int rid,
3526 u_long *startp, u_long *countp)
3528 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3533 * @brief Wrapper function for BUS_GET_RESOURCE().
3535 * This function simply calls the BUS_GET_RESOURCE() method of the
3536 * parent of @p dev and returns the start value.
3539 bus_get_resource_start(device_t dev, int type, int rid)
3541 u_long start, count;
3544 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3552 * @brief Wrapper function for BUS_GET_RESOURCE().
3554 * This function simply calls the BUS_GET_RESOURCE() method of the
3555 * parent of @p dev and returns the count value.
3558 bus_get_resource_count(device_t dev, int type, int rid)
3560 u_long start, count;
3563 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3571 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3573 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3577 bus_delete_resource(device_t dev, int type, int rid)
3579 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3583 * @brief Wrapper function for BUS_CHILD_PRESENT().
3585 * This function simply calls the BUS_CHILD_PRESENT() method of the
3589 bus_child_present(device_t child)
3591 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3595 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3597 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3601 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3605 parent = device_get_parent(child);
3606 if (parent == NULL) {
3610 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3614 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3616 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3620 bus_child_location_str(device_t child, char *buf, size_t buflen)
3624 parent = device_get_parent(child);
3625 if (parent == NULL) {
3629 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3633 * @brief Wrapper function for BUS_GET_DMA_TAG().
3635 * This function simply calls the BUS_GET_DMA_TAG() method of the
3639 bus_get_dma_tag(device_t dev)
3643 parent = device_get_parent(dev);
3646 return (BUS_GET_DMA_TAG(parent, dev));
3649 /* Resume all devices and then notify userland that we're up again. */
3651 root_resume(device_t dev)
3655 error = bus_generic_resume(dev);
3657 devctl_notify("kern", "power", "resume", NULL);
3662 root_print_child(device_t dev, device_t child)
3666 retval += bus_print_child_header(dev, child);
3667 retval += printf("\n");
3673 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3677 * If an interrupt mapping gets to here something bad has happened.
3679 panic("root_setup_intr");
3683 * If we get here, assume that the device is permanant and really is
3684 * present in the system. Removable bus drivers are expected to intercept
3685 * this call long before it gets here. We return -1 so that drivers that
3686 * really care can check vs -1 or some ERRNO returned higher in the food
3690 root_child_present(device_t dev, device_t child)
3695 static kobj_method_t root_methods[] = {
3696 /* Device interface */
3697 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3698 KOBJMETHOD(device_suspend, bus_generic_suspend),
3699 KOBJMETHOD(device_resume, root_resume),
3702 KOBJMETHOD(bus_print_child, root_print_child),
3703 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3704 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3705 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3706 KOBJMETHOD(bus_child_present, root_child_present),
3711 static driver_t root_driver = {
3718 devclass_t root_devclass;
3721 root_bus_module_handler(module_t mod, int what, void* arg)
3725 TAILQ_INIT(&bus_data_devices);
3726 kobj_class_compile((kobj_class_t) &root_driver);
3727 root_bus = make_device(NULL, "root", 0);
3728 root_bus->desc = "System root bus";
3729 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3730 root_bus->driver = &root_driver;
3731 root_bus->state = DS_ATTACHED;
3732 root_devclass = devclass_find_internal("root", NULL, FALSE);
3737 device_shutdown(root_bus);
3740 return (EOPNOTSUPP);
3746 static moduledata_t root_bus_mod = {
3748 root_bus_module_handler,
3751 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3754 * @brief Automatically configure devices
3756 * This function begins the autoconfiguration process by calling
3757 * device_probe_and_attach() for each child of the @c root0 device.
3760 root_bus_configure(void)
3766 TAILQ_FOREACH(dev, &root_bus->children, link) {
3767 device_probe_and_attach(dev);
3772 * @brief Module handler for registering device drivers
3774 * This module handler is used to automatically register device
3775 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
3776 * devclass_add_driver() for the driver described by the
3777 * driver_module_data structure pointed to by @p arg
3780 driver_module_handler(module_t mod, int what, void *arg)
3783 struct driver_module_data *dmd;
3784 devclass_t bus_devclass;
3785 kobj_class_t driver;
3787 dmd = (struct driver_module_data *)arg;
3788 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3793 if (dmd->dmd_chainevh)
3794 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3796 driver = dmd->dmd_driver;
3797 PDEBUG(("Loading module: driver %s on bus %s",
3798 DRIVERNAME(driver), dmd->dmd_busname));
3799 error = devclass_add_driver(bus_devclass, driver);
3804 * If the driver has any base classes, make the
3805 * devclass inherit from the devclass of the driver's
3806 * first base class. This will allow the system to
3807 * search for drivers in both devclasses for children
3808 * of a device using this driver.
3810 if (driver->baseclasses) {
3811 const char *parentname;
3812 parentname = driver->baseclasses[0]->name;
3813 *dmd->dmd_devclass =
3814 devclass_find_internal(driver->name,
3817 *dmd->dmd_devclass =
3818 devclass_find_internal(driver->name, NULL, TRUE);
3823 PDEBUG(("Unloading module: driver %s from bus %s",
3824 DRIVERNAME(dmd->dmd_driver),
3826 error = devclass_delete_driver(bus_devclass,
3829 if (!error && dmd->dmd_chainevh)
3830 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3833 PDEBUG(("Quiesce module: driver %s from bus %s",
3834 DRIVERNAME(dmd->dmd_driver),
3836 error = devclass_quiesce_driver(bus_devclass,
3839 if (!error && dmd->dmd_chainevh)
3840 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3851 * @brief Enumerate all hinted devices for this bus.
3853 * Walks through the hints for this bus and calls the bus_hinted_child
3854 * routine for each one it fines. It searches first for the specific
3855 * bus that's being probed for hinted children (eg isa0), and then for
3856 * generic children (eg isa).
3858 * @param dev bus device to enumerate
3861 bus_enumerate_hinted_children(device_t bus)
3864 const char *dname, *busname;
3868 * enumerate all devices on the specific bus
3870 busname = device_get_nameunit(bus);
3872 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3873 BUS_HINTED_CHILD(bus, dname, dunit);
3876 * and all the generic ones.
3878 busname = device_get_name(bus);
3880 while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
3881 BUS_HINTED_CHILD(bus, dname, dunit);
3886 /* the _short versions avoid iteration by not calling anything that prints
3887 * more than oneliners. I love oneliners.
3891 print_device_short(device_t dev, int indent)
3896 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3897 dev->unit, dev->desc,
3898 (dev->parent? "":"no "),
3899 (TAILQ_EMPTY(&dev->children)? "no ":""),
3900 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3901 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3902 (dev->flags&DF_WILDCARD? "wildcard,":""),
3903 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3904 (dev->flags&DF_REBID? "rebiddable,":""),
3905 (dev->ivars? "":"no "),
3906 (dev->softc? "":"no "),
3911 print_device(device_t dev, int indent)
3916 print_device_short(dev, indent);
3918 indentprintf(("Parent:\n"));
3919 print_device_short(dev->parent, indent+1);
3920 indentprintf(("Driver:\n"));
3921 print_driver_short(dev->driver, indent+1);
3922 indentprintf(("Devclass:\n"));
3923 print_devclass_short(dev->devclass, indent+1);
3927 print_device_tree_short(device_t dev, int indent)
3928 /* print the device and all its children (indented) */
3935 print_device_short(dev, indent);
3937 TAILQ_FOREACH(child, &dev->children, link) {
3938 print_device_tree_short(child, indent+1);
3943 print_device_tree(device_t dev, int indent)
3944 /* print the device and all its children (indented) */
3951 print_device(dev, indent);
3953 TAILQ_FOREACH(child, &dev->children, link) {
3954 print_device_tree(child, indent+1);
3959 print_driver_short(driver_t *driver, int indent)
3964 indentprintf(("driver %s: softc size = %zd\n",
3965 driver->name, driver->size));
3969 print_driver(driver_t *driver, int indent)
3974 print_driver_short(driver, indent);
3979 print_driver_list(driver_list_t drivers, int indent)
3981 driverlink_t driver;
3983 TAILQ_FOREACH(driver, &drivers, link) {
3984 print_driver(driver->driver, indent);
3989 print_devclass_short(devclass_t dc, int indent)
3994 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3998 print_devclass(devclass_t dc, int indent)
4005 print_devclass_short(dc, indent);
4006 indentprintf(("Drivers:\n"));
4007 print_driver_list(dc->drivers, indent+1);
4009 indentprintf(("Devices:\n"));
4010 for (i = 0; i < dc->maxunit; i++)
4012 print_device(dc->devices[i], indent+1);
4016 print_devclass_list_short(void)
4020 printf("Short listing of devclasses, drivers & devices:\n");
4021 TAILQ_FOREACH(dc, &devclasses, link) {
4022 print_devclass_short(dc, 0);
4027 print_devclass_list(void)
4031 printf("Full listing of devclasses, drivers & devices:\n");
4032 TAILQ_FOREACH(dc, &devclasses, link) {
4033 print_devclass(dc, 0);
4040 * User-space access to the device tree.
4042 * We implement a small set of nodes:
4044 * hw.bus Single integer read method to obtain the
4045 * current generation count.
4046 * hw.bus.devices Reads the entire device tree in flat space.
4047 * hw.bus.rman Resource manager interface
4049 * We might like to add the ability to scan devclasses and/or drivers to
4050 * determine what else is currently loaded/available.
4054 sysctl_bus(SYSCTL_HANDLER_ARGS)
4056 struct u_businfo ubus;
4058 ubus.ub_version = BUS_USER_VERSION;
4059 ubus.ub_generation = bus_data_generation;
4061 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
4063 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
4064 "bus-related data");
4067 sysctl_devices(SYSCTL_HANDLER_ARGS)
4069 int *name = (int *)arg1;
4070 u_int namelen = arg2;
4073 struct u_device udev; /* XXX this is a bit big */
4079 if (bus_data_generation_check(name[0]))
4085 * Scan the list of devices, looking for the requested index.
4087 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
4095 * Populate the return array.
4097 bzero(&udev, sizeof(udev));
4098 udev.dv_handle = (uintptr_t)dev;
4099 udev.dv_parent = (uintptr_t)dev->parent;
4100 if (dev->nameunit != NULL)
4101 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
4102 if (dev->desc != NULL)
4103 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
4104 if (dev->driver != NULL && dev->driver->name != NULL)
4105 strlcpy(udev.dv_drivername, dev->driver->name,
4106 sizeof(udev.dv_drivername));
4107 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
4108 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
4109 udev.dv_devflags = dev->devflags;
4110 udev.dv_flags = dev->flags;
4111 udev.dv_state = dev->state;
4112 error = SYSCTL_OUT(req, &udev, sizeof(udev));
4116 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
4117 "system device tree");
4120 bus_data_generation_check(int generation)
4122 if (generation != bus_data_generation)
4125 /* XXX generate optimised lists here? */
4130 bus_data_generation_update(void)
4132 bus_data_generation++;
4136 bus_free_resource(device_t dev, int type, struct resource *r)
4140 return (bus_release_resource(dev, type, rman_get_rid(r), r));