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>
54 #include <machine/stdarg.h>
58 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
59 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
62 * Used to attach drivers to devclasses.
64 typedef struct driverlink *driverlink_t;
67 TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
71 * Forward declarations
73 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
74 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
75 typedef TAILQ_HEAD(device_list, device) device_list_t;
78 TAILQ_ENTRY(devclass) link;
79 devclass_t parent; /* parent in devclass hierarchy */
80 driver_list_t drivers; /* bus devclasses store drivers for bus */
82 device_t *devices; /* array of devices indexed by unit */
83 int maxunit; /* size of devices array */
85 struct sysctl_ctx_list sysctl_ctx;
86 struct sysctl_oid *sysctl_tree;
90 * @brief Implementation of device.
94 * A device is a kernel object. The first field must be the
95 * current ops table for the object.
102 TAILQ_ENTRY(device) link; /**< list of devices in parent */
103 TAILQ_ENTRY(device) devlink; /**< global device list membership */
104 device_t parent; /**< parent of this device */
105 device_list_t children; /**< list of child devices */
108 * Details of this device.
110 driver_t *driver; /**< current driver */
111 devclass_t devclass; /**< current device class */
112 int unit; /**< current unit number */
113 char* nameunit; /**< name+unit e.g. foodev0 */
114 char* desc; /**< driver specific description */
115 int busy; /**< count of calls to device_busy() */
116 device_state_t state; /**< current device state */
117 u_int32_t devflags; /**< api level flags for device_get_flags() */
118 u_short flags; /**< internal device flags */
119 #define DF_ENABLED 1 /* device should be probed/attached */
120 #define DF_FIXEDCLASS 2 /* devclass specified at create time */
121 #define DF_WILDCARD 4 /* unit was originally wildcard */
122 #define DF_DESCMALLOCED 8 /* description was malloced */
123 #define DF_QUIET 16 /* don't print verbose attach message */
124 #define DF_DONENOMATCH 32 /* don't execute DEVICE_NOMATCH again */
125 #define DF_EXTERNALSOFTC 64 /* softc not allocated by us */
126 #define DF_REBID 128 /* Can rebid after attach */
127 u_char order; /**< order from device_add_child_ordered() */
129 void *ivars; /**< instance variables */
130 void *softc; /**< current driver's variables */
132 struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables */
133 struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
136 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
137 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
141 static int bus_debug = 1;
142 TUNABLE_INT("bus.debug", &bus_debug);
143 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
146 #define PDEBUG(a) if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
147 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
148 #define DRIVERNAME(d) ((d)? d->name : "no driver")
149 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
152 * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
153 * prevent syslog from deleting initial spaces
155 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while (0)
157 static void print_device_short(device_t dev, int indent);
158 static void print_device(device_t dev, int indent);
159 void print_device_tree_short(device_t dev, int indent);
160 void print_device_tree(device_t dev, int indent);
161 static void print_driver_short(driver_t *driver, int indent);
162 static void print_driver(driver_t *driver, int indent);
163 static void print_driver_list(driver_list_t drivers, int indent);
164 static void print_devclass_short(devclass_t dc, int indent);
165 static void print_devclass(devclass_t dc, int indent);
166 void print_devclass_list_short(void);
167 void print_devclass_list(void);
170 /* Make the compiler ignore the function calls */
171 #define PDEBUG(a) /* nop */
172 #define DEVICENAME(d) /* nop */
173 #define DRIVERNAME(d) /* nop */
174 #define DEVCLANAME(d) /* nop */
176 #define print_device_short(d,i) /* nop */
177 #define print_device(d,i) /* nop */
178 #define print_device_tree_short(d,i) /* nop */
179 #define print_device_tree(d,i) /* nop */
180 #define print_driver_short(d,i) /* nop */
181 #define print_driver(d,i) /* nop */
182 #define print_driver_list(d,i) /* nop */
183 #define print_devclass_short(d,i) /* nop */
184 #define print_devclass(d,i) /* nop */
185 #define print_devclass_list_short() /* nop */
186 #define print_devclass_list() /* nop */
194 DEVCLASS_SYSCTL_PARENT,
198 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
200 devclass_t dc = (devclass_t)arg1;
204 case DEVCLASS_SYSCTL_PARENT:
205 value = dc->parent ? dc->parent->name : "";
210 return (SYSCTL_OUT(req, value, strlen(value)));
214 devclass_sysctl_init(devclass_t dc)
217 if (dc->sysctl_tree != NULL)
219 sysctl_ctx_init(&dc->sysctl_ctx);
220 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
221 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
223 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
224 OID_AUTO, "%parent", CTLFLAG_RD,
225 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
231 DEVICE_SYSCTL_DRIVER,
232 DEVICE_SYSCTL_LOCATION,
233 DEVICE_SYSCTL_PNPINFO,
234 DEVICE_SYSCTL_PARENT,
238 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
240 device_t dev = (device_t)arg1;
247 case DEVICE_SYSCTL_DESC:
248 value = dev->desc ? dev->desc : "";
250 case DEVICE_SYSCTL_DRIVER:
251 value = dev->driver ? dev->driver->name : "";
253 case DEVICE_SYSCTL_LOCATION:
254 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
255 bus_child_location_str(dev, buf, 1024);
257 case DEVICE_SYSCTL_PNPINFO:
258 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
259 bus_child_pnpinfo_str(dev, buf, 1024);
261 case DEVICE_SYSCTL_PARENT:
262 value = dev->parent ? dev->parent->nameunit : "";
267 error = SYSCTL_OUT(req, value, strlen(value));
274 device_sysctl_init(device_t dev)
276 devclass_t dc = dev->devclass;
278 if (dev->sysctl_tree != NULL)
280 devclass_sysctl_init(dc);
281 sysctl_ctx_init(&dev->sysctl_ctx);
282 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
283 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
284 dev->nameunit + strlen(dc->name),
286 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
287 OID_AUTO, "%desc", CTLFLAG_RD,
288 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
289 "device description");
290 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
291 OID_AUTO, "%driver", CTLFLAG_RD,
292 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
293 "device driver name");
294 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
295 OID_AUTO, "%location", CTLFLAG_RD,
296 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
297 "device location relative to parent");
298 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
299 OID_AUTO, "%pnpinfo", CTLFLAG_RD,
300 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
301 "device identification");
302 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
303 OID_AUTO, "%parent", CTLFLAG_RD,
304 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
309 device_sysctl_fini(device_t dev)
311 if (dev->sysctl_tree == NULL)
313 sysctl_ctx_free(&dev->sysctl_ctx);
314 dev->sysctl_tree = NULL;
318 * /dev/devctl implementation
322 * This design allows only one reader for /dev/devctl. This is not desirable
323 * in the long run, but will get a lot of hair out of this implementation.
324 * Maybe we should make this device a clonable device.
326 * Also note: we specifically do not attach a device to the device_t tree
327 * to avoid potential chicken and egg problems. One could argue that all
328 * of this belongs to the root node. One could also further argue that the
329 * sysctl interface that we have not might more properly be an ioctl
330 * interface, but at this stage of the game, I'm not inclined to rock that
333 * I'm also not sure that the SIGIO support is done correctly or not, as
334 * I copied it from a driver that had SIGIO support that likely hasn't been
335 * tested since 3.4 or 2.2.8!
338 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
339 static int devctl_disable = 0;
340 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
341 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
342 sysctl_devctl_disable, "I", "devctl disable");
344 static d_open_t devopen;
345 static d_close_t devclose;
346 static d_read_t devread;
347 static d_ioctl_t devioctl;
348 static d_poll_t devpoll;
350 static struct cdevsw dev_cdevsw = {
351 .d_version = D_VERSION,
352 .d_flags = D_NEEDGIANT,
361 struct dev_event_info
364 TAILQ_ENTRY(dev_event_info) dei_link;
367 TAILQ_HEAD(devq, dev_event_info);
369 static struct dev_softc
377 struct proc *async_proc;
380 static struct cdev *devctl_dev;
385 devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
387 mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
388 cv_init(&devsoftc.cv, "dev cv");
389 TAILQ_INIT(&devsoftc.devq);
393 devopen(struct cdev *dev, int oflags, int devtype, d_thread_t *td)
399 devsoftc.nonblock = 0;
400 devsoftc.async_proc = NULL;
405 devclose(struct cdev *dev, int fflag, int devtype, d_thread_t *td)
408 mtx_lock(&devsoftc.mtx);
409 cv_broadcast(&devsoftc.cv);
410 mtx_unlock(&devsoftc.mtx);
416 * The read channel for this device is used to report changes to
417 * userland in realtime. We are required to free the data as well as
418 * the n1 object because we allocate them separately. Also note that
419 * we return one record at a time. If you try to read this device a
420 * character at a time, you will loose the rest of the data. Listening
421 * programs are expected to cope.
424 devread(struct cdev *dev, struct uio *uio, int ioflag)
426 struct dev_event_info *n1;
429 mtx_lock(&devsoftc.mtx);
430 while (TAILQ_EMPTY(&devsoftc.devq)) {
431 if (devsoftc.nonblock) {
432 mtx_unlock(&devsoftc.mtx);
435 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
438 * Need to translate ERESTART to EINTR here? -- jake
440 mtx_unlock(&devsoftc.mtx);
444 n1 = TAILQ_FIRST(&devsoftc.devq);
445 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
446 mtx_unlock(&devsoftc.mtx);
447 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
448 free(n1->dei_data, M_BUS);
454 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, d_thread_t *td)
460 devsoftc.nonblock = 1;
462 devsoftc.nonblock = 0;
466 devsoftc.async_proc = td->td_proc;
468 devsoftc.async_proc = NULL;
471 /* (un)Support for other fcntl() calls. */
484 devpoll(struct cdev *dev, int events, d_thread_t *td)
488 mtx_lock(&devsoftc.mtx);
489 if (events & (POLLIN | POLLRDNORM)) {
490 if (!TAILQ_EMPTY(&devsoftc.devq))
491 revents = events & (POLLIN | POLLRDNORM);
493 selrecord(td, &devsoftc.sel);
495 mtx_unlock(&devsoftc.mtx);
501 * @brief Queue data to be read from the devctl device
503 * Generic interface to queue data to the devctl device. It is
504 * assumed that @p data is properly formatted. It is further assumed
505 * that @p data is allocated using the M_BUS malloc type.
508 devctl_queue_data(char *data)
510 struct dev_event_info *n1 = NULL;
513 n1 = malloc(sizeof(*n1), M_BUS, M_NOWAIT);
517 mtx_lock(&devsoftc.mtx);
518 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
519 cv_broadcast(&devsoftc.cv);
520 mtx_unlock(&devsoftc.mtx);
521 selwakeup(&devsoftc.sel);
522 p = devsoftc.async_proc;
531 * @brief Send a 'notification' to userland, using standard ways
534 devctl_notify(const char *system, const char *subsystem, const char *type,
541 return; /* BOGUS! Must specify system. */
542 if (subsystem == NULL)
543 return; /* BOGUS! Must specify subsystem. */
545 return; /* BOGUS! Must specify type. */
546 len += strlen(" system=") + strlen(system);
547 len += strlen(" subsystem=") + strlen(subsystem);
548 len += strlen(" type=") + strlen(type);
549 /* add in the data message plus newline. */
552 len += 3; /* '!', '\n', and NUL */
553 msg = malloc(len, M_BUS, M_NOWAIT);
555 return; /* Drop it on the floor */
556 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n", system,
557 subsystem, type, data);
558 devctl_queue_data(msg);
562 * Common routine that tries to make sending messages as easy as possible.
563 * We allocate memory for the data, copy strings into that, but do not
564 * free it unless there's an error. The dequeue part of the driver should
565 * free the data. We don't send data when the device is disabled. We do
566 * send data, even when we have no listeners, because we wish to avoid
567 * races relating to startup and restart of listening applications.
569 * devaddq is designed to string together the type of event, with the
570 * object of that event, plus the plug and play info and location info
571 * for that event. This is likely most useful for devices, but less
572 * useful for other consumers of this interface. Those should use
573 * the devctl_queue_data() interface instead.
576 devaddq(const char *type, const char *what, device_t dev)
585 data = malloc(1024, M_BUS, M_NOWAIT);
589 /* get the bus specific location of this device */
590 loc = malloc(1024, M_BUS, M_NOWAIT);
594 bus_child_location_str(dev, loc, 1024);
596 /* Get the bus specific pnp info of this device */
597 pnp = malloc(1024, M_BUS, M_NOWAIT);
601 bus_child_pnpinfo_str(dev, pnp, 1024);
603 /* Get the parent of this device, or / if high enough in the tree. */
604 if (device_get_parent(dev) == NULL)
605 parstr = "."; /* Or '/' ? */
607 parstr = device_get_nameunit(device_get_parent(dev));
608 /* String it all together. */
609 snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
613 devctl_queue_data(data);
623 * A device was added to the tree. We are called just after it successfully
624 * attaches (that is, probe and attach success for this device). No call
625 * is made if a device is merely parented into the tree. See devnomatch
626 * if probe fails. If attach fails, no notification is sent (but maybe
627 * we should have a different message for this).
630 devadded(device_t dev)
635 pnp = malloc(1024, M_BUS, M_NOWAIT);
638 tmp = malloc(1024, M_BUS, M_NOWAIT);
642 bus_child_pnpinfo_str(dev, pnp, 1024);
643 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
644 devaddq("+", tmp, dev);
654 * A device was removed from the tree. We are called just before this
658 devremoved(device_t dev)
663 pnp = malloc(1024, M_BUS, M_NOWAIT);
666 tmp = malloc(1024, M_BUS, M_NOWAIT);
670 bus_child_pnpinfo_str(dev, pnp, 1024);
671 snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
672 devaddq("-", tmp, dev);
682 * Called when there's no match for this device. This is only called
683 * the first time that no match happens, so we don't keep getitng this
684 * message. Should that prove to be undesirable, we can change it.
685 * This is called when all drivers that can attach to a given bus
686 * decline to accept this device. Other errrors may not be detected.
689 devnomatch(device_t dev)
691 devaddq("?", "", dev);
695 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
697 struct dev_event_info *n1;
700 dis = devctl_disable;
701 error = sysctl_handle_int(oidp, &dis, 0, req);
702 if (error || !req->newptr)
704 mtx_lock(&devsoftc.mtx);
705 devctl_disable = dis;
707 while (!TAILQ_EMPTY(&devsoftc.devq)) {
708 n1 = TAILQ_FIRST(&devsoftc.devq);
709 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
710 free(n1->dei_data, M_BUS);
714 mtx_unlock(&devsoftc.mtx);
718 /* End of /dev/devctl code */
720 TAILQ_HEAD(,device) bus_data_devices;
721 static int bus_data_generation = 1;
723 kobj_method_t null_methods[] = {
727 DEFINE_CLASS(null, null_methods, 0);
730 * Devclass implementation
733 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
738 * @brief Find or create a device class
740 * If a device class with the name @p classname exists, return it,
741 * otherwise if @p create is non-zero create and return a new device
744 * If @p parentname is non-NULL, the parent of the devclass is set to
745 * the devclass of that name.
747 * @param classname the devclass name to find or create
748 * @param parentname the parent devclass name or @c NULL
749 * @param create non-zero to create a devclass
752 devclass_find_internal(const char *classname, const char *parentname,
757 PDEBUG(("looking for %s", classname));
761 TAILQ_FOREACH(dc, &devclasses, link) {
762 if (!strcmp(dc->name, classname))
767 PDEBUG(("creating %s", classname));
768 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
769 M_BUS, M_NOWAIT|M_ZERO);
773 dc->name = (char*) (dc + 1);
774 strcpy(dc->name, classname);
775 TAILQ_INIT(&dc->drivers);
776 TAILQ_INSERT_TAIL(&devclasses, dc, link);
778 bus_data_generation_update();
780 if (parentname && dc && !dc->parent) {
781 dc->parent = devclass_find_internal(parentname, 0, FALSE);
788 * @brief Create a device class
790 * If a device class with the name @p classname exists, return it,
791 * otherwise create and return a new device class.
793 * @param classname the devclass name to find or create
796 devclass_create(const char *classname)
798 return (devclass_find_internal(classname, 0, TRUE));
802 * @brief Find a device class
804 * If a device class with the name @p classname exists, return it,
805 * otherwise return @c NULL.
807 * @param classname the devclass name to find
810 devclass_find(const char *classname)
812 return (devclass_find_internal(classname, 0, FALSE));
816 * @brief Add a device driver to a device class
818 * Add a device driver to a devclass. This is normally called
819 * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
820 * all devices in the devclass will be called to allow them to attempt
821 * to re-probe any unmatched children.
823 * @param dc the devclass to edit
824 * @param driver the driver to register
827 devclass_add_driver(devclass_t dc, driver_t *driver)
832 PDEBUG(("%s", DRIVERNAME(driver)));
834 dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
839 * Compile the driver's methods. Also increase the reference count
840 * so that the class doesn't get freed when the last instance
841 * goes. This means we can safely use static methods and avoids a
842 * double-free in devclass_delete_driver.
844 kobj_class_compile((kobj_class_t) driver);
847 * Make sure the devclass which the driver is implementing exists.
849 devclass_find_internal(driver->name, 0, TRUE);
852 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
853 driver->refs++; /* XXX: kobj_mtx */
856 * Call BUS_DRIVER_ADDED for any existing busses in this class.
858 for (i = 0; i < dc->maxunit; i++)
860 BUS_DRIVER_ADDED(dc->devices[i], driver);
862 bus_data_generation_update();
867 * @brief Delete a device driver from a device class
869 * Delete a device driver from a devclass. This is normally called
870 * automatically by DRIVER_MODULE().
872 * If the driver is currently attached to any devices,
873 * devclass_delete_driver() will first attempt to detach from each
874 * device. If one of the detach calls fails, the driver will not be
877 * @param dc the devclass to edit
878 * @param driver the driver to unregister
881 devclass_delete_driver(devclass_t busclass, driver_t *driver)
883 devclass_t dc = devclass_find(driver->name);
889 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
895 * Find the link structure in the bus' list of drivers.
897 TAILQ_FOREACH(dl, &busclass->drivers, link) {
898 if (dl->driver == driver)
903 PDEBUG(("%s not found in %s list", driver->name,
909 * Disassociate from any devices. We iterate through all the
910 * devices in the devclass of the driver and detach any which are
911 * using the driver and which have a parent in the devclass which
912 * we are deleting from.
914 * Note that since a driver can be in multiple devclasses, we
915 * should not detach devices which are not children of devices in
916 * the affected devclass.
918 for (i = 0; i < dc->maxunit; i++) {
919 if (dc->devices[i]) {
920 dev = dc->devices[i];
921 if (dev->driver == driver && dev->parent &&
922 dev->parent->devclass == busclass) {
923 if ((error = device_detach(dev)) != 0)
925 device_set_driver(dev, NULL);
930 TAILQ_REMOVE(&busclass->drivers, dl, link);
935 if (driver->refs == 0)
936 kobj_class_free((kobj_class_t) driver);
938 bus_data_generation_update();
943 * @brief Quiesces a set of device drivers from a device class
945 * Quiesce a device driver from a devclass. This is normally called
946 * automatically by DRIVER_MODULE().
948 * If the driver is currently attached to any devices,
949 * devclass_quiesece_driver() will first attempt to quiesce each
952 * @param dc the devclass to edit
953 * @param driver the driver to unregister
956 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
958 devclass_t dc = devclass_find(driver->name);
964 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
970 * Find the link structure in the bus' list of drivers.
972 TAILQ_FOREACH(dl, &busclass->drivers, link) {
973 if (dl->driver == driver)
978 PDEBUG(("%s not found in %s list", driver->name,
984 * Quiesce all devices. We iterate through all the devices in
985 * the devclass of the driver and quiesce any which are using
986 * the driver and which have a parent in the devclass which we
989 * Note that since a driver can be in multiple devclasses, we
990 * should not quiesce devices which are not children of
991 * devices in the affected devclass.
993 for (i = 0; i < dc->maxunit; i++) {
994 if (dc->devices[i]) {
995 dev = dc->devices[i];
996 if (dev->driver == driver && dev->parent &&
997 dev->parent->devclass == busclass) {
998 if ((error = device_quiesce(dev)) != 0)
1011 devclass_find_driver_internal(devclass_t dc, const char *classname)
1015 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
1017 TAILQ_FOREACH(dl, &dc->drivers, link) {
1018 if (!strcmp(dl->driver->name, classname))
1022 PDEBUG(("not found"));
1027 * @brief Search a devclass for a driver
1029 * This function searches the devclass's list of drivers and returns
1030 * the first driver whose name is @p classname or @c NULL if there is
1031 * no driver of that name.
1033 * @param dc the devclass to search
1034 * @param classname the driver name to search for
1037 devclass_find_driver(devclass_t dc, const char *classname)
1041 dl = devclass_find_driver_internal(dc, classname);
1043 return (dl->driver);
1048 * @brief Return the name of the devclass
1051 devclass_get_name(devclass_t dc)
1057 * @brief Find a device given a unit number
1059 * @param dc the devclass to search
1060 * @param unit the unit number to search for
1062 * @returns the device with the given unit number or @c
1063 * NULL if there is no such device
1066 devclass_get_device(devclass_t dc, int unit)
1068 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
1070 return (dc->devices[unit]);
1074 * @brief Find the softc field of a device given a unit number
1076 * @param dc the devclass to search
1077 * @param unit the unit number to search for
1079 * @returns the softc field of the device with the given
1080 * unit number or @c NULL if there is no such
1084 devclass_get_softc(devclass_t dc, int unit)
1088 dev = devclass_get_device(dc, unit);
1092 return (device_get_softc(dev));
1096 * @brief Get a list of devices in the devclass
1098 * An array containing a list of all the devices in the given devclass
1099 * is allocated and returned in @p *devlistp. The number of devices
1100 * in the array is returned in @p *devcountp. The caller should free
1101 * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
1103 * @param dc the devclass to examine
1104 * @param devlistp points at location for array pointer return
1106 * @param devcountp points at location for array size return value
1109 * @retval ENOMEM the array allocation failed
1112 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
1117 count = devclass_get_count(dc);
1118 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1123 for (i = 0; i < dc->maxunit; i++) {
1124 if (dc->devices[i]) {
1125 list[count] = dc->devices[i];
1137 * @brief Get a list of drivers in the devclass
1139 * An array containing a list of pointers to all the drivers in the
1140 * given devclass is allocated and returned in @p *listp. The number
1141 * of drivers in the array is returned in @p *countp. The caller should
1142 * free the array using @c free(p, M_TEMP).
1144 * @param dc the devclass to examine
1145 * @param listp gives location for array pointer return value
1146 * @param countp gives location for number of array elements
1150 * @retval ENOMEM the array allocation failed
1153 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1160 TAILQ_FOREACH(dl, &dc->drivers, link)
1162 list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1167 TAILQ_FOREACH(dl, &dc->drivers, link) {
1168 list[count] = dl->driver;
1178 * @brief Get the number of devices in a devclass
1180 * @param dc the devclass to examine
1183 devclass_get_count(devclass_t dc)
1188 for (i = 0; i < dc->maxunit; i++)
1195 * @brief Get the maximum unit number used in a devclass
1197 * Note that this is one greater than the highest currently-allocated
1200 * @param dc the devclass to examine
1203 devclass_get_maxunit(devclass_t dc)
1205 return (dc->maxunit);
1209 * @brief Find a free unit number in a devclass
1211 * This function searches for the first unused unit number greater
1212 * that or equal to @p unit.
1214 * @param dc the devclass to examine
1215 * @param unit the first unit number to check
1218 devclass_find_free_unit(devclass_t dc, int unit)
1222 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1228 * @brief Set the parent of a devclass
1230 * The parent class is normally initialised automatically by
1233 * @param dc the devclass to edit
1234 * @param pdc the new parent devclass
1237 devclass_set_parent(devclass_t dc, devclass_t pdc)
1243 * @brief Get the parent of a devclass
1245 * @param dc the devclass to examine
1248 devclass_get_parent(devclass_t dc)
1250 return (dc->parent);
1253 struct sysctl_ctx_list *
1254 devclass_get_sysctl_ctx(devclass_t dc)
1256 return (&dc->sysctl_ctx);
1260 devclass_get_sysctl_tree(devclass_t dc)
1262 return (dc->sysctl_tree);
1267 * @brief Allocate a unit number
1269 * On entry, @p *unitp is the desired unit number (or @c -1 if any
1270 * will do). The allocated unit number is returned in @p *unitp.
1272 * @param dc the devclass to allocate from
1273 * @param unitp points at the location for the allocated unit
1277 * @retval EEXIST the requested unit number is already allocated
1278 * @retval ENOMEM memory allocation failure
1281 devclass_alloc_unit(devclass_t dc, int *unitp)
1285 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1287 /* If we were given a wired unit number, check for existing device */
1290 if (unit >= 0 && unit < dc->maxunit &&
1291 dc->devices[unit] != NULL) {
1293 printf("%s: %s%d already exists; skipping it\n",
1294 dc->name, dc->name, *unitp);
1298 /* Unwired device, find the next available slot for it */
1300 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1305 * We've selected a unit beyond the length of the table, so let's
1306 * extend the table to make room for all units up to and including
1309 if (unit >= dc->maxunit) {
1313 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
1314 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
1317 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1318 bzero(newlist + dc->maxunit,
1319 sizeof(device_t) * (newsize - dc->maxunit));
1321 free(dc->devices, M_BUS);
1322 dc->devices = newlist;
1323 dc->maxunit = newsize;
1325 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1333 * @brief Add a device to a devclass
1335 * A unit number is allocated for the device (using the device's
1336 * preferred unit number if any) and the device is registered in the
1337 * devclass. This allows the device to be looked up by its unit
1338 * number, e.g. by decoding a dev_t minor number.
1340 * @param dc the devclass to add to
1341 * @param dev the device to add
1344 * @retval EEXIST the requested unit number is already allocated
1345 * @retval ENOMEM memory allocation failure
1348 devclass_add_device(devclass_t dc, device_t dev)
1352 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1354 buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
1357 dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
1361 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1362 free(dev->nameunit, M_BUS);
1363 dev->nameunit = NULL;
1366 dc->devices[dev->unit] = dev;
1368 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1375 * @brief Delete a device from a devclass
1377 * The device is removed from the devclass's device list and its unit
1380 * @param dc the devclass to delete from
1381 * @param dev the device to delete
1386 devclass_delete_device(devclass_t dc, device_t dev)
1391 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1393 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1394 panic("devclass_delete_device: inconsistent device class");
1395 dc->devices[dev->unit] = NULL;
1396 if (dev->flags & DF_WILDCARD)
1398 dev->devclass = NULL;
1399 free(dev->nameunit, M_BUS);
1400 dev->nameunit = NULL;
1407 * @brief Make a new device and add it as a child of @p parent
1409 * @param parent the parent of the new device
1410 * @param name the devclass name of the new device or @c NULL
1411 * to leave the devclass unspecified
1412 * @parem unit the unit number of the new device of @c -1 to
1413 * leave the unit number unspecified
1415 * @returns the new device
1418 make_device(device_t parent, const char *name, int unit)
1423 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1426 dc = devclass_find_internal(name, 0, TRUE);
1428 printf("make_device: can't find device class %s\n",
1436 dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
1440 dev->parent = parent;
1441 TAILQ_INIT(&dev->children);
1442 kobj_init((kobj_t) dev, &null_class);
1444 dev->devclass = NULL;
1446 dev->nameunit = NULL;
1450 dev->flags = DF_ENABLED;
1453 dev->flags |= DF_WILDCARD;
1455 dev->flags |= DF_FIXEDCLASS;
1456 if (devclass_add_device(dc, dev)) {
1457 kobj_delete((kobj_t) dev, M_BUS);
1464 dev->state = DS_NOTPRESENT;
1466 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1467 bus_data_generation_update();
1474 * @brief Print a description of a device.
1477 device_print_child(device_t dev, device_t child)
1481 if (device_is_alive(child))
1482 retval += BUS_PRINT_CHILD(dev, child);
1484 retval += device_printf(child, " not found\n");
1490 * @brief Create a new device
1492 * This creates a new device and adds it as a child of an existing
1493 * parent device. The new device will be added after the last existing
1494 * child with order zero.
1496 * @param dev the device which will be the parent of the
1498 * @param name devclass name for new device or @c NULL if not
1500 * @param unit unit number for new device or @c -1 if not
1503 * @returns the new device
1506 device_add_child(device_t dev, const char *name, int unit)
1508 return (device_add_child_ordered(dev, 0, name, unit));
1512 * @brief Create a new device
1514 * This creates a new device and adds it as a child of an existing
1515 * parent device. The new device will be added after the last existing
1516 * child with the same order.
1518 * @param dev the device which will be the parent of the
1520 * @param order a value which is used to partially sort the
1521 * children of @p dev - devices created using
1522 * lower values of @p order appear first in @p
1523 * dev's list of children
1524 * @param name devclass name for new device or @c NULL if not
1526 * @param unit unit number for new device or @c -1 if not
1529 * @returns the new device
1532 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1537 PDEBUG(("%s at %s with order %d as unit %d",
1538 name, DEVICENAME(dev), order, unit));
1540 child = make_device(dev, name, unit);
1543 child->order = order;
1545 TAILQ_FOREACH(place, &dev->children, link) {
1546 if (place->order > order)
1552 * The device 'place' is the first device whose order is
1553 * greater than the new child.
1555 TAILQ_INSERT_BEFORE(place, child, link);
1558 * The new child's order is greater or equal to the order of
1559 * any existing device. Add the child to the tail of the list.
1561 TAILQ_INSERT_TAIL(&dev->children, child, link);
1564 bus_data_generation_update();
1569 * @brief Delete a device
1571 * This function deletes a device along with all of its children. If
1572 * the device currently has a driver attached to it, the device is
1573 * detached first using device_detach().
1575 * @param dev the parent device
1576 * @param child the device to delete
1579 * @retval non-zero a unit error code describing the error
1582 device_delete_child(device_t dev, device_t child)
1585 device_t grandchild;
1587 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1589 /* remove children first */
1590 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1591 error = device_delete_child(child, grandchild);
1596 if ((error = device_detach(child)) != 0)
1598 if (child->devclass)
1599 devclass_delete_device(child->devclass, child);
1600 TAILQ_REMOVE(&dev->children, child, link);
1601 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1602 kobj_delete((kobj_t) child, M_BUS);
1604 bus_data_generation_update();
1609 * @brief Find a device given a unit number
1611 * This is similar to devclass_get_devices() but only searches for
1612 * devices which have @p dev as a parent.
1614 * @param dev the parent device to search
1615 * @param unit the unit number to search for. If the unit is -1,
1616 * return the first child of @p dev which has name
1617 * @p classname (that is, the one with the lowest unit.)
1619 * @returns the device with the given unit number or @c
1620 * NULL if there is no such device
1623 device_find_child(device_t dev, const char *classname, int unit)
1628 dc = devclass_find(classname);
1633 child = devclass_get_device(dc, unit);
1634 if (child && child->parent == dev)
1637 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1638 child = devclass_get_device(dc, unit);
1639 if (child && child->parent == dev)
1650 first_matching_driver(devclass_t dc, device_t dev)
1653 return (devclass_find_driver_internal(dc, dev->devclass->name));
1654 return (TAILQ_FIRST(&dc->drivers));
1661 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1663 if (dev->devclass) {
1665 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1666 if (!strcmp(dev->devclass->name, dl->driver->name))
1670 return (TAILQ_NEXT(last, link));
1677 device_probe_child(device_t dev, device_t child)
1680 driverlink_t best = 0;
1682 int result, pri = 0;
1683 int hasclass = (child->devclass != 0);
1689 panic("device_probe_child: parent device has no devclass");
1692 * If the state is already probed, then return. However, don't
1693 * return if we can rebid this object.
1695 if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
1698 for (; dc; dc = dc->parent) {
1699 for (dl = first_matching_driver(dc, child);
1701 dl = next_matching_driver(dc, child, dl)) {
1702 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1703 device_set_driver(child, dl->driver);
1705 device_set_devclass(child, dl->driver->name);
1707 /* Fetch any flags for the device before probing. */
1708 resource_int_value(dl->driver->name, child->unit,
1709 "flags", &child->devflags);
1711 result = DEVICE_PROBE(child);
1713 /* Reset flags and devclass before the next probe. */
1714 child->devflags = 0;
1716 device_set_devclass(child, 0);
1719 * If the driver returns SUCCESS, there can be
1720 * no higher match for this device.
1729 * The driver returned an error so it
1730 * certainly doesn't match.
1733 device_set_driver(child, 0);
1738 * A priority lower than SUCCESS, remember the
1739 * best matching driver. Initialise the value
1740 * of pri for the first match.
1742 if (best == 0 || result > pri) {
1749 * If we have an unambiguous match in this devclass,
1750 * don't look in the parent.
1752 if (best && pri == 0)
1757 * If we found a driver, change state and initialise the devclass.
1759 /* XXX What happens if we rebid and got no best? */
1762 * If this device was atached, and we were asked to
1763 * rescan, and it is a different driver, then we have
1764 * to detach the old driver and reattach this new one.
1765 * Note, we don't have to check for DF_REBID here
1766 * because if the state is > DS_ALIVE, we know it must
1769 * This assumes that all DF_REBID drivers can have
1770 * their probe routine called at any time and that
1771 * they are idempotent as well as completely benign in
1772 * normal operations.
1774 * We also have to make sure that the detach
1775 * succeeded, otherwise we fail the operation (or
1776 * maybe it should just fail silently? I'm torn).
1778 if (child->state > DS_ALIVE && best->driver != child->driver)
1779 if ((result = device_detach(dev)) != 0)
1782 /* Set the winning driver, devclass, and flags. */
1783 if (!child->devclass)
1784 device_set_devclass(child, best->driver->name);
1785 device_set_driver(child, best->driver);
1786 resource_int_value(best->driver->name, child->unit,
1787 "flags", &child->devflags);
1791 * A bit bogus. Call the probe method again to make
1792 * sure that we have the right description.
1794 DEVICE_PROBE(child);
1796 child->flags |= DF_REBID;
1799 child->flags &= ~DF_REBID;
1800 child->state = DS_ALIVE;
1802 bus_data_generation_update();
1810 * @brief Return the parent of a device
1813 device_get_parent(device_t dev)
1815 return (dev->parent);
1819 * @brief Get a list of children of a device
1821 * An array containing a list of all the children of the given device
1822 * is allocated and returned in @p *devlistp. The number of devices
1823 * in the array is returned in @p *devcountp. The caller should free
1824 * the array using @c free(p, M_TEMP).
1826 * @param dev the device to examine
1827 * @param devlistp points at location for array pointer return
1829 * @param devcountp points at location for array size return value
1832 * @retval ENOMEM the array allocation failed
1835 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1842 TAILQ_FOREACH(child, &dev->children, link) {
1846 list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
1851 TAILQ_FOREACH(child, &dev->children, link) {
1852 list[count] = child;
1863 * @brief Return the current driver for the device or @c NULL if there
1864 * is no driver currently attached
1867 device_get_driver(device_t dev)
1869 return (dev->driver);
1873 * @brief Return the current devclass for the device or @c NULL if
1877 device_get_devclass(device_t dev)
1879 return (dev->devclass);
1883 * @brief Return the name of the device's devclass or @c NULL if there
1887 device_get_name(device_t dev)
1889 if (dev != NULL && dev->devclass)
1890 return (devclass_get_name(dev->devclass));
1895 * @brief Return a string containing the device's devclass name
1896 * followed by an ascii representation of the device's unit number
1900 device_get_nameunit(device_t dev)
1902 return (dev->nameunit);
1906 * @brief Return the device's unit number.
1909 device_get_unit(device_t dev)
1915 * @brief Return the device's description string
1918 device_get_desc(device_t dev)
1924 * @brief Return the device's flags
1927 device_get_flags(device_t dev)
1929 return (dev->devflags);
1932 struct sysctl_ctx_list *
1933 device_get_sysctl_ctx(device_t dev)
1935 return (&dev->sysctl_ctx);
1939 device_get_sysctl_tree(device_t dev)
1941 return (dev->sysctl_tree);
1945 * @brief Print the name of the device followed by a colon and a space
1947 * @returns the number of characters printed
1950 device_print_prettyname(device_t dev)
1952 const char *name = device_get_name(dev);
1955 return (printf("unknown: "));
1956 return (printf("%s%d: ", name, device_get_unit(dev)));
1960 * @brief Print the name of the device followed by a colon, a space
1961 * and the result of calling vprintf() with the value of @p fmt and
1962 * the following arguments.
1964 * @returns the number of characters printed
1967 device_printf(device_t dev, const char * fmt, ...)
1972 retval = device_print_prettyname(dev);
1974 retval += vprintf(fmt, ap);
1983 device_set_desc_internal(device_t dev, const char* desc, int copy)
1985 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1986 free(dev->desc, M_BUS);
1987 dev->flags &= ~DF_DESCMALLOCED;
1992 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
1994 strcpy(dev->desc, desc);
1995 dev->flags |= DF_DESCMALLOCED;
1998 /* Avoid a -Wcast-qual warning */
1999 dev->desc = (char *)(uintptr_t) desc;
2002 bus_data_generation_update();
2006 * @brief Set the device's description
2008 * The value of @c desc should be a string constant that will not
2009 * change (at least until the description is changed in a subsequent
2010 * call to device_set_desc() or device_set_desc_copy()).
2013 device_set_desc(device_t dev, const char* desc)
2015 device_set_desc_internal(dev, desc, FALSE);
2019 * @brief Set the device's description
2021 * The string pointed to by @c desc is copied. Use this function if
2022 * the device description is generated, (e.g. with sprintf()).
2025 device_set_desc_copy(device_t dev, const char* desc)
2027 device_set_desc_internal(dev, desc, TRUE);
2031 * @brief Set the device's flags
2034 device_set_flags(device_t dev, u_int32_t flags)
2036 dev->devflags = flags;
2040 * @brief Return the device's softc field
2042 * The softc is allocated and zeroed when a driver is attached, based
2043 * on the size field of the driver.
2046 device_get_softc(device_t dev)
2048 return (dev->softc);
2052 * @brief Set the device's softc field
2054 * Most drivers do not need to use this since the softc is allocated
2055 * automatically when the driver is attached.
2058 device_set_softc(device_t dev, void *softc)
2060 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
2061 free(dev->softc, M_BUS_SC);
2064 dev->flags |= DF_EXTERNALSOFTC;
2066 dev->flags &= ~DF_EXTERNALSOFTC;
2070 * @brief Get the device's ivars field
2072 * The ivars field is used by the parent device to store per-device
2073 * state (e.g. the physical location of the device or a list of
2077 device_get_ivars(device_t dev)
2080 KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
2081 return (dev->ivars);
2085 * @brief Set the device's ivars field
2088 device_set_ivars(device_t dev, void * ivars)
2091 KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
2096 * @brief Return the device's state
2099 device_get_state(device_t dev)
2101 return (dev->state);
2105 * @brief Set the DF_ENABLED flag for the device
2108 device_enable(device_t dev)
2110 dev->flags |= DF_ENABLED;
2114 * @brief Clear the DF_ENABLED flag for the device
2117 device_disable(device_t dev)
2119 dev->flags &= ~DF_ENABLED;
2123 * @brief Increment the busy counter for the device
2126 device_busy(device_t dev)
2128 if (dev->state < DS_ATTACHED)
2129 panic("device_busy: called for unattached device");
2130 if (dev->busy == 0 && dev->parent)
2131 device_busy(dev->parent);
2133 dev->state = DS_BUSY;
2137 * @brief Decrement the busy counter for the device
2140 device_unbusy(device_t dev)
2142 if (dev->state != DS_BUSY)
2143 panic("device_unbusy: called for non-busy device %s",
2144 device_get_nameunit(dev));
2146 if (dev->busy == 0) {
2148 device_unbusy(dev->parent);
2149 dev->state = DS_ATTACHED;
2154 * @brief Set the DF_QUIET flag for the device
2157 device_quiet(device_t dev)
2159 dev->flags |= DF_QUIET;
2163 * @brief Clear the DF_QUIET flag for the device
2166 device_verbose(device_t dev)
2168 dev->flags &= ~DF_QUIET;
2172 * @brief Return non-zero if the DF_QUIET flag is set on the device
2175 device_is_quiet(device_t dev)
2177 return ((dev->flags & DF_QUIET) != 0);
2181 * @brief Return non-zero if the DF_ENABLED flag is set on the device
2184 device_is_enabled(device_t dev)
2186 return ((dev->flags & DF_ENABLED) != 0);
2190 * @brief Return non-zero if the device was successfully probed
2193 device_is_alive(device_t dev)
2195 return (dev->state >= DS_ALIVE);
2199 * @brief Return non-zero if the device currently has a driver
2203 device_is_attached(device_t dev)
2205 return (dev->state >= DS_ATTACHED);
2209 * @brief Set the devclass of a device
2210 * @see devclass_add_device().
2213 device_set_devclass(device_t dev, const char *classname)
2220 devclass_delete_device(dev->devclass, dev);
2224 if (dev->devclass) {
2225 printf("device_set_devclass: device class already set\n");
2229 dc = devclass_find_internal(classname, 0, TRUE);
2233 error = devclass_add_device(dc, dev);
2235 bus_data_generation_update();
2240 * @brief Set the driver of a device
2243 * @retval EBUSY the device already has a driver attached
2244 * @retval ENOMEM a memory allocation failure occurred
2247 device_set_driver(device_t dev, driver_t *driver)
2249 if (dev->state >= DS_ATTACHED)
2252 if (dev->driver == driver)
2255 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
2256 free(dev->softc, M_BUS_SC);
2259 kobj_delete((kobj_t) dev, 0);
2260 dev->driver = driver;
2262 kobj_init((kobj_t) dev, (kobj_class_t) driver);
2263 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
2264 dev->softc = malloc(driver->size, M_BUS_SC,
2267 kobj_delete((kobj_t) dev, 0);
2268 kobj_init((kobj_t) dev, &null_class);
2274 kobj_init((kobj_t) dev, &null_class);
2277 bus_data_generation_update();
2282 * @brief Probe a device and attach a driver if possible
2284 * This function is the core of the device autoconfiguration
2285 * system. Its purpose is to select a suitable driver for a device and
2286 * then call that driver to initialise the hardware appropriately. The
2287 * driver is selected by calling the DEVICE_PROBE() method of a set of
2288 * candidate drivers and then choosing the driver which returned the
2289 * best value. This driver is then attached to the device using
2292 * The set of suitable drivers is taken from the list of drivers in
2293 * the parent device's devclass. If the device was originally created
2294 * with a specific class name (see device_add_child()), only drivers
2295 * with that name are probed, otherwise all drivers in the devclass
2296 * are probed. If no drivers return successful probe values in the
2297 * parent devclass, the search continues in the parent of that
2298 * devclass (see devclass_get_parent()) if any.
2300 * @param dev the device to initialise
2303 * @retval ENXIO no driver was found
2304 * @retval ENOMEM memory allocation failure
2305 * @retval non-zero some other unix error code
2308 device_probe_and_attach(device_t dev)
2314 if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
2317 if (!(dev->flags & DF_ENABLED)) {
2318 if (bootverbose && device_get_name(dev) != NULL) {
2319 device_print_prettyname(dev);
2320 printf("not probed (disabled)\n");
2324 if ((error = device_probe_child(dev->parent, dev)) != 0) {
2325 if (!(dev->flags & DF_DONENOMATCH)) {
2326 BUS_PROBE_NOMATCH(dev->parent, dev);
2328 dev->flags |= DF_DONENOMATCH;
2332 error = device_attach(dev);
2338 * @brief Attach a device driver to a device
2340 * This function is a wrapper around the DEVICE_ATTACH() driver
2341 * method. In addition to calling DEVICE_ATTACH(), it initialises the
2342 * device's sysctl tree, optionally prints a description of the device
2343 * and queues a notification event for user-based device management
2346 * Normally this function is only called internally from
2347 * device_probe_and_attach().
2349 * @param dev the device to initialise
2352 * @retval ENXIO no driver was found
2353 * @retval ENOMEM memory allocation failure
2354 * @retval non-zero some other unix error code
2357 device_attach(device_t dev)
2361 device_sysctl_init(dev);
2362 if (!device_is_quiet(dev))
2363 device_print_child(dev->parent, dev);
2364 if ((error = DEVICE_ATTACH(dev)) != 0) {
2365 printf("device_attach: %s%d attach returned %d\n",
2366 dev->driver->name, dev->unit, error);
2367 /* Unset the class; set in device_probe_child */
2368 if (dev->devclass == 0)
2369 device_set_devclass(dev, 0);
2370 device_set_driver(dev, NULL);
2371 device_sysctl_fini(dev);
2372 dev->state = DS_NOTPRESENT;
2375 dev->state = DS_ATTACHED;
2381 * @brief Detach a driver from a device
2383 * This function is a wrapper around the DEVICE_DETACH() driver
2384 * method. If the call to DEVICE_DETACH() succeeds, it calls
2385 * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
2386 * notification event for user-based device management services and
2387 * cleans up the device's sysctl tree.
2389 * @param dev the device to un-initialise
2392 * @retval ENXIO no driver was found
2393 * @retval ENOMEM memory allocation failure
2394 * @retval non-zero some other unix error code
2397 device_detach(device_t dev)
2403 PDEBUG(("%s", DEVICENAME(dev)));
2404 if (dev->state == DS_BUSY)
2406 if (dev->state != DS_ATTACHED)
2409 if ((error = DEVICE_DETACH(dev)) != 0)
2412 device_printf(dev, "detached\n");
2414 BUS_CHILD_DETACHED(dev->parent, dev);
2416 if (!(dev->flags & DF_FIXEDCLASS))
2417 devclass_delete_device(dev->devclass, dev);
2419 dev->state = DS_NOTPRESENT;
2420 device_set_driver(dev, NULL);
2421 device_set_desc(dev, NULL);
2422 device_sysctl_fini(dev);
2428 * @brief Tells a driver to quiesce itself.
2430 * This function is a wrapper around the DEVICE_QUIESCE() driver
2431 * method. If the call to DEVICE_QUIESCE() succeeds.
2433 * @param dev the device to quiesce
2436 * @retval ENXIO no driver was found
2437 * @retval ENOMEM memory allocation failure
2438 * @retval non-zero some other unix error code
2441 device_quiesce(device_t dev)
2444 PDEBUG(("%s", DEVICENAME(dev)));
2445 if (dev->state == DS_BUSY)
2447 if (dev->state != DS_ATTACHED)
2450 return (DEVICE_QUIESCE(dev));
2454 * @brief Notify a device of system shutdown
2456 * This function calls the DEVICE_SHUTDOWN() driver method if the
2457 * device currently has an attached driver.
2459 * @returns the value returned by DEVICE_SHUTDOWN()
2462 device_shutdown(device_t dev)
2464 if (dev->state < DS_ATTACHED)
2466 return (DEVICE_SHUTDOWN(dev));
2470 * @brief Set the unit number of a device
2472 * This function can be used to override the unit number used for a
2473 * device (e.g. to wire a device to a pre-configured unit number).
2476 device_set_unit(device_t dev, int unit)
2481 dc = device_get_devclass(dev);
2482 if (unit < dc->maxunit && dc->devices[unit])
2484 err = devclass_delete_device(dc, dev);
2488 err = devclass_add_device(dc, dev);
2492 bus_data_generation_update();
2496 /*======================================*/
2498 * Some useful method implementations to make life easier for bus drivers.
2502 * @brief Initialise a resource list.
2504 * @param rl the resource list to initialise
2507 resource_list_init(struct resource_list *rl)
2513 * @brief Reclaim memory used by a resource list.
2515 * This function frees the memory for all resource entries on the list
2518 * @param rl the resource list to free
2521 resource_list_free(struct resource_list *rl)
2523 struct resource_list_entry *rle;
2525 while ((rle = STAILQ_FIRST(rl)) != NULL) {
2527 panic("resource_list_free: resource entry is busy");
2528 STAILQ_REMOVE_HEAD(rl, link);
2534 * @brief Add a resource entry.
2536 * This function adds a resource entry using the given @p type, @p
2537 * start, @p end and @p count values. A rid value is chosen by
2538 * searching sequentially for the first unused rid starting at zero.
2540 * @param rl the resource list to edit
2541 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2542 * @param start the start address of the resource
2543 * @param end the end address of the resource
2544 * @param count XXX end-start+1
2547 resource_list_add_next(struct resource_list *rl, int type, u_long start,
2548 u_long end, u_long count)
2553 while (resource_list_find(rl, type, rid) != NULL)
2555 resource_list_add(rl, type, rid, start, end, count);
2560 * @brief Add or modify a resource entry.
2562 * If an existing entry exists with the same type and rid, it will be
2563 * modified using the given values of @p start, @p end and @p
2564 * count. If no entry exists, a new one will be created using the
2565 * given values. The resource list entry that matches is then returned.
2567 * @param rl the resource list to edit
2568 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2569 * @param rid the resource identifier
2570 * @param start the start address of the resource
2571 * @param end the end address of the resource
2572 * @param count XXX end-start+1
2574 struct resource_list_entry *
2575 resource_list_add(struct resource_list *rl, int type, int rid,
2576 u_long start, u_long end, u_long count)
2578 struct resource_list_entry *rle;
2580 rle = resource_list_find(rl, type, rid);
2582 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
2585 panic("resource_list_add: can't record entry");
2586 STAILQ_INSERT_TAIL(rl, rle, link);
2593 panic("resource_list_add: resource entry is busy");
2602 * @brief Find a resource entry by type and rid.
2604 * @param rl the resource list to search
2605 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2606 * @param rid the resource identifier
2608 * @returns the resource entry pointer or NULL if there is no such
2611 struct resource_list_entry *
2612 resource_list_find(struct resource_list *rl, int type, int rid)
2614 struct resource_list_entry *rle;
2616 STAILQ_FOREACH(rle, rl, link) {
2617 if (rle->type == type && rle->rid == rid)
2624 * @brief Delete a resource entry.
2626 * @param rl the resource list to edit
2627 * @param type the resource entry type (e.g. SYS_RES_MEMORY)
2628 * @param rid the resource identifier
2631 resource_list_delete(struct resource_list *rl, int type, int rid)
2633 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2636 if (rle->res != NULL)
2637 panic("resource_list_delete: resource has not been released");
2638 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
2644 * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
2646 * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
2647 * and passing the allocation up to the parent of @p bus. This assumes
2648 * that the first entry of @c device_get_ivars(child) is a struct
2649 * resource_list. This also handles 'passthrough' allocations where a
2650 * child is a remote descendant of bus by passing the allocation up to
2651 * the parent of bus.
2653 * Typically, a bus driver would store a list of child resources
2654 * somewhere in the child device's ivars (see device_get_ivars()) and
2655 * its implementation of BUS_ALLOC_RESOURCE() would find that list and
2656 * then call resource_list_alloc() to perform the allocation.
2658 * @param rl the resource list to allocate from
2659 * @param bus the parent device of @p child
2660 * @param child the device which is requesting an allocation
2661 * @param type the type of resource to allocate
2662 * @param rid a pointer to the resource identifier
2663 * @param start hint at the start of the resource range - pass
2664 * @c 0UL for any start address
2665 * @param end hint at the end of the resource range - pass
2666 * @c ~0UL for any end address
2667 * @param count hint at the size of range required - pass @c 1
2669 * @param flags any extra flags to control the resource
2670 * allocation - see @c RF_XXX flags in
2671 * <sys/rman.h> for details
2673 * @returns the resource which was allocated or @c NULL if no
2674 * resource could be allocated
2677 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
2678 int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
2680 struct resource_list_entry *rle = 0;
2681 int passthrough = (device_get_parent(child) != bus);
2682 int isdefault = (start == 0UL && end == ~0UL);
2685 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2686 type, rid, start, end, count, flags));
2689 rle = resource_list_find(rl, type, *rid);
2692 return (NULL); /* no resource of that type/rid */
2695 panic("resource_list_alloc: resource entry is busy");
2699 count = ulmax(count, rle->count);
2700 end = ulmax(rle->end, start + count - 1);
2703 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2704 type, rid, start, end, count, flags);
2707 * Record the new range.
2710 rle->start = rman_get_start(rle->res);
2711 rle->end = rman_get_end(rle->res);
2719 * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
2721 * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
2722 * used with resource_list_alloc().
2724 * @param rl the resource list which was allocated from
2725 * @param bus the parent device of @p child
2726 * @param child the device which is requesting a release
2727 * @param type the type of resource to allocate
2728 * @param rid the resource identifier
2729 * @param res the resource to release
2732 * @retval non-zero a standard unix error code indicating what
2733 * error condition prevented the operation
2736 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
2737 int type, int rid, struct resource *res)
2739 struct resource_list_entry *rle = 0;
2740 int passthrough = (device_get_parent(child) != bus);
2744 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2748 rle = resource_list_find(rl, type, rid);
2751 panic("resource_list_release: can't find resource");
2753 panic("resource_list_release: resource entry is not busy");
2755 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2765 * @brief Print a description of resources in a resource list
2767 * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
2768 * The name is printed if at least one resource of the given type is available.
2769 * The format is used to print resource start and end.
2771 * @param rl the resource list to print
2772 * @param name the name of @p type, e.g. @c "memory"
2773 * @param type type type of resource entry to print
2774 * @param format printf(9) format string to print resource
2775 * start and end values
2777 * @returns the number of characters printed
2780 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2783 struct resource_list_entry *rle;
2784 int printed, retval;
2788 /* Yes, this is kinda cheating */
2789 STAILQ_FOREACH(rle, rl, link) {
2790 if (rle->type == type) {
2792 retval += printf(" %s ", name);
2794 retval += printf(",");
2796 retval += printf(format, rle->start);
2797 if (rle->count > 1) {
2798 retval += printf("-");
2799 retval += printf(format, rle->start +
2808 * @brief Releases all the resources in a list.
2810 * @param rl The resource list to purge.
2815 resource_list_purge(struct resource_list *rl)
2817 struct resource_list_entry *rle;
2819 STAILQ_FOREACH(rle, rl, link) {
2821 bus_release_resource(rman_get_device(rle->res),
2822 rle->type, rle->rid, rle->res);
2823 STAILQ_REMOVE_HEAD(rl, link);
2829 * @brief Helper function for implementing DEVICE_PROBE()
2831 * This function can be used to help implement the DEVICE_PROBE() for
2832 * a bus (i.e. a device which has other devices attached to it). It
2833 * calls the DEVICE_IDENTIFY() method of each driver in the device's
2837 bus_generic_probe(device_t dev)
2839 devclass_t dc = dev->devclass;
2842 TAILQ_FOREACH(dl, &dc->drivers, link) {
2843 DEVICE_IDENTIFY(dl->driver, dev);
2850 * @brief Helper function for implementing DEVICE_ATTACH()
2852 * This function can be used to help implement the DEVICE_ATTACH() for
2853 * a bus. It calls device_probe_and_attach() for each of the device's
2857 bus_generic_attach(device_t dev)
2861 TAILQ_FOREACH(child, &dev->children, link) {
2862 device_probe_and_attach(child);
2869 * @brief Helper function for implementing DEVICE_DETACH()
2871 * This function can be used to help implement the DEVICE_DETACH() for
2872 * a bus. It calls device_detach() for each of the device's
2876 bus_generic_detach(device_t dev)
2881 if (dev->state != DS_ATTACHED)
2884 TAILQ_FOREACH(child, &dev->children, link) {
2885 if ((error = device_detach(child)) != 0)
2893 * @brief Helper function for implementing DEVICE_SHUTDOWN()
2895 * This function can be used to help implement the DEVICE_SHUTDOWN()
2896 * for a bus. It calls device_shutdown() for each of the device's
2900 bus_generic_shutdown(device_t dev)
2904 TAILQ_FOREACH(child, &dev->children, link) {
2905 device_shutdown(child);
2912 * @brief Helper function for implementing DEVICE_SUSPEND()
2914 * This function can be used to help implement the DEVICE_SUSPEND()
2915 * for a bus. It calls DEVICE_SUSPEND() for each of the device's
2916 * children. If any call to DEVICE_SUSPEND() fails, the suspend
2917 * operation is aborted and any devices which were suspended are
2918 * resumed immediately by calling their DEVICE_RESUME() methods.
2921 bus_generic_suspend(device_t dev)
2924 device_t child, child2;
2926 TAILQ_FOREACH(child, &dev->children, link) {
2927 error = DEVICE_SUSPEND(child);
2929 for (child2 = TAILQ_FIRST(&dev->children);
2930 child2 && child2 != child;
2931 child2 = TAILQ_NEXT(child2, link))
2932 DEVICE_RESUME(child2);
2940 * @brief Helper function for implementing DEVICE_RESUME()
2942 * This function can be used to help implement the DEVICE_RESUME() for
2943 * a bus. It calls DEVICE_RESUME() on each of the device's children.
2946 bus_generic_resume(device_t dev)
2950 TAILQ_FOREACH(child, &dev->children, link) {
2951 DEVICE_RESUME(child);
2952 /* if resume fails, there's nothing we can usefully do... */
2958 * @brief Helper function for implementing BUS_PRINT_CHILD().
2960 * This function prints the first part of the ascii representation of
2961 * @p child, including its name, unit and description (if any - see
2962 * device_set_desc()).
2964 * @returns the number of characters printed
2967 bus_print_child_header(device_t dev, device_t child)
2971 if (device_get_desc(child)) {
2972 retval += device_printf(child, "<%s>", device_get_desc(child));
2974 retval += printf("%s", device_get_nameunit(child));
2981 * @brief Helper function for implementing BUS_PRINT_CHILD().
2983 * This function prints the last part of the ascii representation of
2984 * @p child, which consists of the string @c " on " followed by the
2985 * name and unit of the @p dev.
2987 * @returns the number of characters printed
2990 bus_print_child_footer(device_t dev, device_t child)
2992 return (printf(" on %s\n", device_get_nameunit(dev)));
2996 * @brief Helper function for implementing BUS_PRINT_CHILD().
2998 * This function simply calls bus_print_child_header() followed by
2999 * bus_print_child_footer().
3001 * @returns the number of characters printed
3004 bus_generic_print_child(device_t dev, device_t child)
3008 retval += bus_print_child_header(dev, child);
3009 retval += bus_print_child_footer(dev, child);
3015 * @brief Stub function for implementing BUS_READ_IVAR().
3020 bus_generic_read_ivar(device_t dev, device_t child, int index,
3027 * @brief Stub function for implementing BUS_WRITE_IVAR().
3032 bus_generic_write_ivar(device_t dev, device_t child, int index,
3039 * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
3043 struct resource_list *
3044 bus_generic_get_resource_list(device_t dev, device_t child)
3050 * @brief Helper function for implementing BUS_DRIVER_ADDED().
3052 * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
3053 * DEVICE_IDENTIFY() method to allow it to add new children to the bus
3054 * and then calls device_probe_and_attach() for each unattached child.
3057 bus_generic_driver_added(device_t dev, driver_t *driver)
3061 DEVICE_IDENTIFY(driver, dev);
3062 TAILQ_FOREACH(child, &dev->children, link) {
3063 if (child->state == DS_NOTPRESENT ||
3064 (child->flags & DF_REBID))
3065 device_probe_and_attach(child);
3070 * @brief Helper function for implementing BUS_SETUP_INTR().
3072 * This simple implementation of BUS_SETUP_INTR() simply calls the
3073 * BUS_SETUP_INTR() method of the parent of @p dev.
3076 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
3077 int flags, driver_intr_t *intr, void *arg, void **cookiep)
3079 /* Propagate up the bus hierarchy until someone handles it. */
3081 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
3082 intr, arg, cookiep));
3087 * @brief Helper function for implementing BUS_TEARDOWN_INTR().
3089 * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
3090 * BUS_TEARDOWN_INTR() method of the parent of @p dev.
3093 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
3096 /* Propagate up the bus hierarchy until someone handles it. */
3098 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
3103 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3105 * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
3106 * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
3109 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3110 u_long start, u_long end, u_long count, u_int flags)
3112 /* Propagate up the bus hierarchy until someone handles it. */
3114 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3115 start, end, count, flags));
3120 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3122 * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
3123 * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
3126 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3129 /* Propagate up the bus hierarchy until someone handles it. */
3131 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
3137 * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
3139 * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
3140 * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
3143 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3146 /* Propagate up the bus hierarchy until someone handles it. */
3148 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
3154 * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
3156 * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
3157 * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
3160 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3161 int rid, struct resource *r)
3163 /* Propagate up the bus hierarchy until someone handles it. */
3165 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3171 * @brief Helper function for implementing BUS_CONFIG_INTR().
3173 * This simple implementation of BUS_CONFIG_INTR() simply calls the
3174 * BUS_CONFIG_INTR() method of the parent of @p dev.
3177 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
3178 enum intr_polarity pol)
3181 /* Propagate up the bus hierarchy until someone handles it. */
3183 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
3188 * @brief Helper function for implementing BUS_GET_RESOURCE().
3190 * This implementation of BUS_GET_RESOURCE() uses the
3191 * resource_list_find() function to do most of the work. It calls
3192 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3196 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3197 u_long *startp, u_long *countp)
3199 struct resource_list * rl = NULL;
3200 struct resource_list_entry * rle = NULL;
3202 rl = BUS_GET_RESOURCE_LIST(dev, child);
3206 rle = resource_list_find(rl, type, rid);
3211 *startp = rle->start;
3213 *countp = rle->count;
3219 * @brief Helper function for implementing BUS_SET_RESOURCE().
3221 * This implementation of BUS_SET_RESOURCE() uses the
3222 * resource_list_add() function to do most of the work. It calls
3223 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3227 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3228 u_long start, u_long count)
3230 struct resource_list * rl = NULL;
3232 rl = BUS_GET_RESOURCE_LIST(dev, child);
3236 resource_list_add(rl, type, rid, start, (start + count - 1), count);
3242 * @brief Helper function for implementing BUS_DELETE_RESOURCE().
3244 * This implementation of BUS_DELETE_RESOURCE() uses the
3245 * resource_list_delete() function to do most of the work. It calls
3246 * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
3250 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3252 struct resource_list * rl = NULL;
3254 rl = BUS_GET_RESOURCE_LIST(dev, child);
3258 resource_list_delete(rl, type, rid);
3264 * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
3266 * This implementation of BUS_RELEASE_RESOURCE() uses the
3267 * resource_list_release() function to do most of the work. It calls
3268 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3271 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3272 int rid, struct resource *r)
3274 struct resource_list * rl = NULL;
3276 rl = BUS_GET_RESOURCE_LIST(dev, child);
3280 return (resource_list_release(rl, dev, child, type, rid, r));
3284 * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
3286 * This implementation of BUS_ALLOC_RESOURCE() uses the
3287 * resource_list_alloc() function to do most of the work. It calls
3288 * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
3291 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3292 int *rid, u_long start, u_long end, u_long count, u_int flags)
3294 struct resource_list * rl = NULL;
3296 rl = BUS_GET_RESOURCE_LIST(dev, child);
3300 return (resource_list_alloc(rl, dev, child, type, rid,
3301 start, end, count, flags));
3305 * @brief Helper function for implementing BUS_CHILD_PRESENT().
3307 * This simple implementation of BUS_CHILD_PRESENT() simply calls the
3308 * BUS_CHILD_PRESENT() method of the parent of @p dev.
3311 bus_generic_child_present(device_t dev, device_t child)
3313 return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
3317 * Some convenience functions to make it easier for drivers to use the
3318 * resource-management functions. All these really do is hide the
3319 * indirection through the parent's method table, making for slightly
3320 * less-wordy code. In the future, it might make sense for this code
3321 * to maintain some sort of a list of resources allocated by each device.
3325 * @brief Wrapper function for BUS_ALLOC_RESOURCE().
3327 * This function simply calls the BUS_ALLOC_RESOURCE() method of the
3331 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3332 u_long count, u_int flags)
3334 if (dev->parent == 0)
3336 return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3341 * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
3343 * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
3347 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3349 if (dev->parent == 0)
3351 return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3355 * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
3357 * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
3361 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3363 if (dev->parent == 0)
3365 return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3369 * @brief Wrapper function for BUS_RELEASE_RESOURCE().
3371 * This function simply calls the BUS_RELEASE_RESOURCE() method of the
3375 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3377 if (dev->parent == 0)
3379 return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3383 * @brief Wrapper function for BUS_SETUP_INTR().
3385 * This function simply calls the BUS_SETUP_INTR() method of the
3389 bus_setup_intr(device_t dev, struct resource *r, int flags,
3390 driver_intr_t handler, void *arg, void **cookiep)
3394 if (dev->parent != 0) {
3395 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) &&
3397 flags &= ~INTR_MPSAFE;
3398 error = BUS_SETUP_INTR(dev->parent, dev, r, flags,
3399 handler, arg, cookiep);
3401 if (!(flags & (INTR_MPSAFE | INTR_FAST)))
3402 device_printf(dev, "[GIANT-LOCKED]\n");
3403 if (bootverbose && (flags & INTR_MPSAFE))
3404 device_printf(dev, "[MPSAFE]\n");
3405 if (flags & INTR_FAST)
3406 device_printf(dev, "[FAST]\n");
3414 * @brief Wrapper function for BUS_TEARDOWN_INTR().
3416 * This function simply calls the BUS_TEARDOWN_INTR() method of the
3420 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3422 if (dev->parent == 0)
3424 return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3428 * @brief Wrapper function for BUS_SET_RESOURCE().
3430 * This function simply calls the BUS_SET_RESOURCE() method of the
3434 bus_set_resource(device_t dev, int type, int rid,
3435 u_long start, u_long count)
3437 return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3442 * @brief Wrapper function for BUS_GET_RESOURCE().
3444 * This function simply calls the BUS_GET_RESOURCE() method of the
3448 bus_get_resource(device_t dev, int type, int rid,
3449 u_long *startp, u_long *countp)
3451 return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3456 * @brief Wrapper function for BUS_GET_RESOURCE().
3458 * This function simply calls the BUS_GET_RESOURCE() method of the
3459 * parent of @p dev and returns the start value.
3462 bus_get_resource_start(device_t dev, int type, int rid)
3464 u_long start, count;
3467 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3475 * @brief Wrapper function for BUS_GET_RESOURCE().
3477 * This function simply calls the BUS_GET_RESOURCE() method of the
3478 * parent of @p dev and returns the count value.
3481 bus_get_resource_count(device_t dev, int type, int rid)
3483 u_long start, count;
3486 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3494 * @brief Wrapper function for BUS_DELETE_RESOURCE().
3496 * This function simply calls the BUS_DELETE_RESOURCE() method of the
3500 bus_delete_resource(device_t dev, int type, int rid)
3502 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3506 * @brief Wrapper function for BUS_CHILD_PRESENT().
3508 * This function simply calls the BUS_CHILD_PRESENT() method of the
3512 bus_child_present(device_t child)
3514 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3518 * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
3520 * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
3524 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3528 parent = device_get_parent(child);
3529 if (parent == NULL) {
3533 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3537 * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
3539 * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
3543 bus_child_location_str(device_t child, char *buf, size_t buflen)
3547 parent = device_get_parent(child);
3548 if (parent == NULL) {
3552 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3556 root_print_child(device_t dev, device_t child)
3560 retval += bus_print_child_header(dev, child);
3561 retval += printf("\n");
3567 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3571 * If an interrupt mapping gets to here something bad has happened.
3573 panic("root_setup_intr");
3577 * If we get here, assume that the device is permanant and really is
3578 * present in the system. Removable bus drivers are expected to intercept
3579 * this call long before it gets here. We return -1 so that drivers that
3580 * really care can check vs -1 or some ERRNO returned higher in the food
3584 root_child_present(device_t dev, device_t child)
3589 static kobj_method_t root_methods[] = {
3590 /* Device interface */
3591 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3592 KOBJMETHOD(device_suspend, bus_generic_suspend),
3593 KOBJMETHOD(device_resume, bus_generic_resume),
3596 KOBJMETHOD(bus_print_child, root_print_child),
3597 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3598 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3599 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3600 KOBJMETHOD(bus_child_present, root_child_present),
3605 static driver_t root_driver = {
3612 devclass_t root_devclass;
3615 root_bus_module_handler(module_t mod, int what, void* arg)
3619 TAILQ_INIT(&bus_data_devices);
3620 kobj_class_compile((kobj_class_t) &root_driver);
3621 root_bus = make_device(NULL, "root", 0);
3622 root_bus->desc = "System root bus";
3623 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3624 root_bus->driver = &root_driver;
3625 root_bus->state = DS_ATTACHED;
3626 root_devclass = devclass_find_internal("root", 0, FALSE);
3631 device_shutdown(root_bus);
3634 return (EOPNOTSUPP);
3640 static moduledata_t root_bus_mod = {
3642 root_bus_module_handler,
3645 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3648 * @brief Automatically configure devices
3650 * This function begins the autoconfiguration process by calling
3651 * device_probe_and_attach() for each child of the @c root0 device.
3654 root_bus_configure(void)
3660 TAILQ_FOREACH(dev, &root_bus->children, link) {
3661 device_probe_and_attach(dev);
3666 * @brief Module handler for registering device drivers
3668 * This module handler is used to automatically register device
3669 * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
3670 * devclass_add_driver() for the driver described by the
3671 * driver_module_data structure pointed to by @p arg
3674 driver_module_handler(module_t mod, int what, void *arg)
3677 struct driver_module_data *dmd;
3678 devclass_t bus_devclass;
3679 kobj_class_t driver;
3681 dmd = (struct driver_module_data *)arg;
3682 bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE);
3687 if (dmd->dmd_chainevh)
3688 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3690 driver = dmd->dmd_driver;
3691 PDEBUG(("Loading module: driver %s on bus %s",
3692 DRIVERNAME(driver), dmd->dmd_busname));
3693 error = devclass_add_driver(bus_devclass, driver);
3698 * If the driver has any base classes, make the
3699 * devclass inherit from the devclass of the driver's
3700 * first base class. This will allow the system to
3701 * search for drivers in both devclasses for children
3702 * of a device using this driver.
3704 if (driver->baseclasses) {
3705 const char *parentname;
3706 parentname = driver->baseclasses[0]->name;
3707 *dmd->dmd_devclass =
3708 devclass_find_internal(driver->name,
3711 *dmd->dmd_devclass =
3712 devclass_find_internal(driver->name, 0, TRUE);
3717 PDEBUG(("Unloading module: driver %s from bus %s",
3718 DRIVERNAME(dmd->dmd_driver),
3720 error = devclass_delete_driver(bus_devclass,
3723 if (!error && dmd->dmd_chainevh)
3724 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3727 PDEBUG(("Quiesce module: driver %s from bus %s",
3728 DRIVERNAME(dmd->dmd_driver),
3730 error = devclass_quiesce_driver(bus_devclass,
3733 if (!error && dmd->dmd_chainevh)
3734 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3746 /* the _short versions avoid iteration by not calling anything that prints
3747 * more than oneliners. I love oneliners.
3751 print_device_short(device_t dev, int indent)
3756 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3757 dev->unit, dev->desc,
3758 (dev->parent? "":"no "),
3759 (TAILQ_EMPTY(&dev->children)? "no ":""),
3760 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3761 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3762 (dev->flags&DF_WILDCARD? "wildcard,":""),
3763 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3764 (dev->flags&DF_REBID? "rebiddable,":""),
3765 (dev->ivars? "":"no "),
3766 (dev->softc? "":"no "),
3771 print_device(device_t dev, int indent)
3776 print_device_short(dev, indent);
3778 indentprintf(("Parent:\n"));
3779 print_device_short(dev->parent, indent+1);
3780 indentprintf(("Driver:\n"));
3781 print_driver_short(dev->driver, indent+1);
3782 indentprintf(("Devclass:\n"));
3783 print_devclass_short(dev->devclass, indent+1);
3787 print_device_tree_short(device_t dev, int indent)
3788 /* print the device and all its children (indented) */
3795 print_device_short(dev, indent);
3797 TAILQ_FOREACH(child, &dev->children, link) {
3798 print_device_tree_short(child, indent+1);
3803 print_device_tree(device_t dev, int indent)
3804 /* print the device and all its children (indented) */
3811 print_device(dev, indent);
3813 TAILQ_FOREACH(child, &dev->children, link) {
3814 print_device_tree(child, indent+1);
3819 print_driver_short(driver_t *driver, int indent)
3824 indentprintf(("driver %s: softc size = %zd\n",
3825 driver->name, driver->size));
3829 print_driver(driver_t *driver, int indent)
3834 print_driver_short(driver, indent);
3839 print_driver_list(driver_list_t drivers, int indent)
3841 driverlink_t driver;
3843 TAILQ_FOREACH(driver, &drivers, link) {
3844 print_driver(driver->driver, indent);
3849 print_devclass_short(devclass_t dc, int indent)
3854 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3858 print_devclass(devclass_t dc, int indent)
3865 print_devclass_short(dc, indent);
3866 indentprintf(("Drivers:\n"));
3867 print_driver_list(dc->drivers, indent+1);
3869 indentprintf(("Devices:\n"));
3870 for (i = 0; i < dc->maxunit; i++)
3872 print_device(dc->devices[i], indent+1);
3876 print_devclass_list_short(void)
3880 printf("Short listing of devclasses, drivers & devices:\n");
3881 TAILQ_FOREACH(dc, &devclasses, link) {
3882 print_devclass_short(dc, 0);
3887 print_devclass_list(void)
3891 printf("Full listing of devclasses, drivers & devices:\n");
3892 TAILQ_FOREACH(dc, &devclasses, link) {
3893 print_devclass(dc, 0);
3900 * User-space access to the device tree.
3902 * We implement a small set of nodes:
3904 * hw.bus Single integer read method to obtain the
3905 * current generation count.
3906 * hw.bus.devices Reads the entire device tree in flat space.
3907 * hw.bus.rman Resource manager interface
3909 * We might like to add the ability to scan devclasses and/or drivers to
3910 * determine what else is currently loaded/available.
3914 sysctl_bus(SYSCTL_HANDLER_ARGS)
3916 struct u_businfo ubus;
3918 ubus.ub_version = BUS_USER_VERSION;
3919 ubus.ub_generation = bus_data_generation;
3921 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3923 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3924 "bus-related data");
3927 sysctl_devices(SYSCTL_HANDLER_ARGS)
3929 int *name = (int *)arg1;
3930 u_int namelen = arg2;
3933 struct u_device udev; /* XXX this is a bit big */
3939 if (bus_data_generation_check(name[0]))
3945 * Scan the list of devices, looking for the requested index.
3947 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3955 * Populate the return array.
3957 bzero(&udev, sizeof(udev));
3958 udev.dv_handle = (uintptr_t)dev;
3959 udev.dv_parent = (uintptr_t)dev->parent;
3960 if (dev->nameunit != NULL)
3961 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3962 if (dev->desc != NULL)
3963 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3964 if (dev->driver != NULL && dev->driver->name != NULL)
3965 strlcpy(udev.dv_drivername, dev->driver->name,
3966 sizeof(udev.dv_drivername));
3967 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3968 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3969 udev.dv_devflags = dev->devflags;
3970 udev.dv_flags = dev->flags;
3971 udev.dv_state = dev->state;
3972 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3976 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3977 "system device tree");
3980 bus_data_generation_check(int generation)
3982 if (generation != bus_data_generation)
3985 /* XXX generate optimised lists here? */
3990 bus_data_generation_update(void)
3992 bus_data_generation++;
3996 bus_free_resource(device_t dev, int type, struct resource *r)
4000 return (bus_release_resource(dev, type, rman_get_rid(r), r));