2 * Copyright (c) 1997,1998 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
26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
27 * $DragonFly: src/sys/kern/subr_bus.c,v 1.46 2008/10/03 00:26:21 hasso Exp $
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/bus_private.h>
39 #include <sys/sysctl.h>
40 #include <sys/systm.h>
43 #include <sys/device.h>
46 #include <sys/selinfo.h>
48 #include <sys/filio.h>
50 #include <sys/signalvar.h>
52 #include <machine/stdarg.h> /* for device_printf() */
54 #include <sys/thread2.h>
56 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
58 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
61 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
62 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
63 #define DRIVERNAME(d) ((d)? d->name : "no driver")
64 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
66 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
67 * prevent syslog from deleting initial spaces
69 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
71 static void print_device_short(device_t dev, int indent);
72 static void print_device(device_t dev, int indent);
73 void print_device_tree_short(device_t dev, int indent);
74 void print_device_tree(device_t dev, int indent);
75 static void print_driver_short(driver_t *driver, int indent);
76 static void print_driver(driver_t *driver, int indent);
77 static void print_driver_list(driver_list_t drivers, int indent);
78 static void print_devclass_short(devclass_t dc, int indent);
79 static void print_devclass(devclass_t dc, int indent);
80 void print_devclass_list_short(void);
81 void print_devclass_list(void);
84 /* Make the compiler ignore the function calls */
85 #define PDEBUG(a) /* nop */
86 #define DEVICENAME(d) /* nop */
87 #define DRIVERNAME(d) /* nop */
88 #define DEVCLANAME(d) /* nop */
90 #define print_device_short(d,i) /* nop */
91 #define print_device(d,i) /* nop */
92 #define print_device_tree_short(d,i) /* nop */
93 #define print_device_tree(d,i) /* nop */
94 #define print_driver_short(d,i) /* nop */
95 #define print_driver(d,i) /* nop */
96 #define print_driver_list(d,i) /* nop */
97 #define print_devclass_short(d,i) /* nop */
98 #define print_devclass(d,i) /* nop */
99 #define print_devclass_list_short() /* nop */
100 #define print_devclass_list() /* nop */
103 static void device_attach_async(device_t dev);
104 static void device_attach_thread(void *arg);
105 static int device_doattach(device_t dev);
107 static int do_async_attach = 0;
108 static int numasyncthreads;
109 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
112 * /dev/devctl implementation
116 * This design allows only one reader for /dev/devctl. This is not desirable
117 * in the long run, but will get a lot of hair out of this implementation.
118 * Maybe we should make this device a clonable device.
120 * Also note: we specifically do not attach a device to the device_t tree
121 * to avoid potential chicken and egg problems. One could argue that all
122 * of this belongs to the root node. One could also further argue that the
123 * sysctl interface that we have not might more properly be an ioctl
124 * interface, but at this stage of the game, I'm not inclined to rock that
127 * I'm also not sure that the SIGIO support is done correctly or not, as
128 * I copied it from a driver that had SIGIO support that likely hasn't been
129 * tested since 3.4 or 2.2.8!
132 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
133 static int devctl_disable = 0;
134 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
135 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
136 sysctl_devctl_disable, "I", "devctl disable");
138 #define CDEV_MAJOR 188
140 static d_open_t devopen;
141 static d_close_t devclose;
142 static d_read_t devread;
143 static d_ioctl_t devioctl;
144 static d_poll_t devpoll;
146 static struct dev_ops devctl_ops = {
147 { "devctl", CDEV_MAJOR, 0 },
155 struct dev_event_info
158 TAILQ_ENTRY(dev_event_info) dei_link;
161 TAILQ_HEAD(devq, dev_event_info);
163 static struct dev_softc
170 struct proc *async_proc;
176 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
177 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
178 TAILQ_INIT(&devsoftc.devq);
182 devopen(struct dev_open_args *ap)
188 devsoftc.nonblock = 0;
189 devsoftc.async_proc = NULL;
194 devclose(struct dev_close_args *ap)
197 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
199 lockmgr(&devsoftc.lock, LK_RELEASE);
205 * The read channel for this device is used to report changes to
206 * userland in realtime. We are required to free the data as well as
207 * the n1 object because we allocate them separately. Also note that
208 * we return one record at a time. If you try to read this device a
209 * character at a time, you will lose the rest of the data. Listening
210 * programs are expected to cope.
213 devread(struct dev_read_args *ap)
215 struct uio *uio = ap->a_uio;
216 struct dev_event_info *n1;
219 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
220 while (TAILQ_EMPTY(&devsoftc.devq)) {
221 if (devsoftc.nonblock) {
222 lockmgr(&devsoftc.lock, LK_RELEASE);
225 tsleep_interlock(&devsoftc, PCATCH);
226 lockmgr(&devsoftc.lock, LK_RELEASE);
227 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
228 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
231 * Need to translate ERESTART to EINTR here? -- jake
233 lockmgr(&devsoftc.lock, LK_RELEASE);
237 n1 = TAILQ_FIRST(&devsoftc.devq);
238 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
239 lockmgr(&devsoftc.lock, LK_RELEASE);
240 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
241 kfree(n1->dei_data, M_BUS);
247 devioctl(struct dev_ioctl_args *ap)
252 if (*(int*)ap->a_data)
253 devsoftc.nonblock = 1;
255 devsoftc.nonblock = 0;
258 if (*(int*)ap->a_data)
259 devsoftc.async_proc = curproc;
261 devsoftc.async_proc = NULL;
264 /* (un)Support for other fcntl() calls. */
277 devpoll(struct dev_poll_args *ap)
281 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
282 if (ap->a_events & (POLLIN | POLLRDNORM)) {
283 if (!TAILQ_EMPTY(&devsoftc.devq))
284 revents = ap->a_events & (POLLIN | POLLRDNORM);
286 selrecord(curthread, &devsoftc.sel);
288 lockmgr(&devsoftc.lock, LK_RELEASE);
290 ap->a_events = revents;
295 * @brief Return whether the userland process is running
298 devctl_process_running(void)
300 return (devsoftc.inuse == 1);
304 * @brief Queue data to be read from the devctl device
306 * Generic interface to queue data to the devctl device. It is
307 * assumed that @p data is properly formatted. It is further assumed
308 * that @p data is allocated using the M_BUS malloc type.
311 devctl_queue_data(char *data)
313 struct dev_event_info *n1 = NULL;
316 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
320 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
321 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
323 lockmgr(&devsoftc.lock, LK_RELEASE);
324 get_mplock(); /* XXX */
325 selwakeup(&devsoftc.sel);
326 rel_mplock(); /* XXX */
327 p = devsoftc.async_proc;
333 * @brief Send a 'notification' to userland, using standard ways
336 devctl_notify(const char *system, const char *subsystem, const char *type,
343 return; /* BOGUS! Must specify system. */
344 if (subsystem == NULL)
345 return; /* BOGUS! Must specify subsystem. */
347 return; /* BOGUS! Must specify type. */
348 len += strlen(" system=") + strlen(system);
349 len += strlen(" subsystem=") + strlen(subsystem);
350 len += strlen(" type=") + strlen(type);
351 /* add in the data message plus newline. */
354 len += 3; /* '!', '\n', and NUL */
355 msg = kmalloc(len, M_BUS, M_NOWAIT);
357 return; /* Drop it on the floor */
359 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
360 system, subsystem, type, data);
362 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
363 system, subsystem, type);
364 devctl_queue_data(msg);
368 * Common routine that tries to make sending messages as easy as possible.
369 * We allocate memory for the data, copy strings into that, but do not
370 * free it unless there's an error. The dequeue part of the driver should
371 * free the data. We don't send data when the device is disabled. We do
372 * send data, even when we have no listeners, because we wish to avoid
373 * races relating to startup and restart of listening applications.
375 * devaddq is designed to string together the type of event, with the
376 * object of that event, plus the plug and play info and location info
377 * for that event. This is likely most useful for devices, but less
378 * useful for other consumers of this interface. Those should use
379 * the devctl_queue_data() interface instead.
382 devaddq(const char *type, const char *what, device_t dev)
391 data = kmalloc(1024, M_BUS, M_NOWAIT);
395 /* get the bus specific location of this device */
396 loc = kmalloc(1024, M_BUS, M_NOWAIT);
400 bus_child_location_str(dev, loc, 1024);
402 /* Get the bus specific pnp info of this device */
403 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
407 bus_child_pnpinfo_str(dev, pnp, 1024);
409 /* Get the parent of this device, or / if high enough in the tree. */
410 if (device_get_parent(dev) == NULL)
411 parstr = "."; /* Or '/' ? */
413 parstr = device_get_nameunit(device_get_parent(dev));
414 /* String it all together. */
415 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
419 devctl_queue_data(data);
429 * A device was added to the tree. We are called just after it successfully
430 * attaches (that is, probe and attach success for this device). No call
431 * is made if a device is merely parented into the tree. See devnomatch
432 * if probe fails. If attach fails, no notification is sent (but maybe
433 * we should have a different message for this).
436 devadded(device_t dev)
441 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
444 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
448 bus_child_pnpinfo_str(dev, pnp, 1024);
449 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
450 devaddq("+", tmp, dev);
460 * A device was removed from the tree. We are called just before this
464 devremoved(device_t dev)
469 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
472 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
476 bus_child_pnpinfo_str(dev, pnp, 1024);
477 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
478 devaddq("-", tmp, dev);
488 * Called when there's no match for this device. This is only called
489 * the first time that no match happens, so we don't keep getitng this
490 * message. Should that prove to be undesirable, we can change it.
491 * This is called when all drivers that can attach to a given bus
492 * decline to accept this device. Other errrors may not be detected.
495 devnomatch(device_t dev)
497 devaddq("?", "", dev);
501 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
503 struct dev_event_info *n1;
506 dis = devctl_disable;
507 error = sysctl_handle_int(oidp, &dis, 0, req);
508 if (error || !req->newptr)
510 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
511 devctl_disable = dis;
513 while (!TAILQ_EMPTY(&devsoftc.devq)) {
514 n1 = TAILQ_FIRST(&devsoftc.devq);
515 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
516 kfree(n1->dei_data, M_BUS);
520 lockmgr(&devsoftc.lock, LK_RELEASE);
524 /* End of /dev/devctl code */
526 TAILQ_HEAD(,device) bus_data_devices;
527 static int bus_data_generation = 1;
529 kobj_method_t null_methods[] = {
533 DEFINE_CLASS(null, null_methods, 0);
536 * Devclass implementation
539 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
542 devclass_find_internal(const char *classname, const char *parentname,
547 PDEBUG(("looking for %s", classname));
548 if (classname == NULL)
551 TAILQ_FOREACH(dc, &devclasses, link)
552 if (!strcmp(dc->name, classname))
556 PDEBUG(("creating %s", classname));
557 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
558 M_BUS, M_INTWAIT | M_ZERO);
562 dc->name = (char*) (dc + 1);
563 strcpy(dc->name, classname);
566 TAILQ_INIT(&dc->drivers);
567 TAILQ_INSERT_TAIL(&devclasses, dc, link);
569 bus_data_generation_update();
572 if (parentname && dc && !dc->parent)
573 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
579 devclass_create(const char *classname)
581 return(devclass_find_internal(classname, NULL, TRUE));
585 devclass_find(const char *classname)
587 return(devclass_find_internal(classname, NULL, FALSE));
591 devclass_find_unit(const char *classname, int unit)
595 if ((dc = devclass_find(classname)) != NULL)
596 return(devclass_get_device(dc, unit));
601 devclass_add_driver(devclass_t dc, driver_t *driver)
607 PDEBUG(("%s", DRIVERNAME(driver)));
609 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
614 * Compile the driver's methods. Also increase the reference count
615 * so that the class doesn't get freed when the last instance
616 * goes. This means we can safely use static methods and avoids a
617 * double-free in devclass_delete_driver.
619 kobj_class_instantiate(driver);
622 * Make sure the devclass which the driver is implementing exists.
624 devclass_find_internal(driver->name, NULL, TRUE);
627 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
630 * Call BUS_DRIVER_ADDED for any existing busses in this class,
631 * but only if the bus has already been attached (otherwise we
632 * might probe too early).
634 * This is what will cause a newly loaded module to be associated
635 * with hardware. bus_generic_driver_added() is typically what ends
638 for (i = 0; i < dc->maxunit; i++) {
639 if ((dev = dc->devices[i]) != NULL) {
640 if (dev->state >= DS_ATTACHED)
641 BUS_DRIVER_ADDED(dev, driver);
645 bus_data_generation_update();
650 devclass_delete_driver(devclass_t busclass, driver_t *driver)
652 devclass_t dc = devclass_find(driver->name);
658 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
664 * Find the link structure in the bus' list of drivers.
666 TAILQ_FOREACH(dl, &busclass->drivers, link)
667 if (dl->driver == driver)
671 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
676 * Disassociate from any devices. We iterate through all the
677 * devices in the devclass of the driver and detach any which are
678 * using the driver and which have a parent in the devclass which
679 * we are deleting from.
681 * Note that since a driver can be in multiple devclasses, we
682 * should not detach devices which are not children of devices in
683 * the affected devclass.
685 for (i = 0; i < dc->maxunit; i++)
686 if (dc->devices[i]) {
687 dev = dc->devices[i];
688 if (dev->driver == driver && dev->parent &&
689 dev->parent->devclass == busclass) {
690 if ((error = device_detach(dev)) != 0)
692 device_set_driver(dev, NULL);
696 TAILQ_REMOVE(&busclass->drivers, dl, link);
699 kobj_class_uninstantiate(driver);
701 bus_data_generation_update();
706 devclass_find_driver_internal(devclass_t dc, const char *classname)
710 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
712 TAILQ_FOREACH(dl, &dc->drivers, link)
713 if (!strcmp(dl->driver->name, classname))
716 PDEBUG(("not found"));
721 devclass_find_driver(devclass_t dc, const char *classname)
725 dl = devclass_find_driver_internal(dc, classname);
733 devclass_get_name(devclass_t dc)
739 devclass_get_device(devclass_t dc, int unit)
741 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
743 return(dc->devices[unit]);
747 devclass_get_softc(devclass_t dc, int unit)
751 dev = devclass_get_device(dc, unit);
755 return(device_get_softc(dev));
759 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
766 for (i = 0; i < dc->maxunit; i++)
770 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
775 for (i = 0; i < dc->maxunit; i++)
776 if (dc->devices[i]) {
777 list[count] = dc->devices[i];
788 * @brief Get a list of drivers in the devclass
790 * An array containing a list of pointers to all the drivers in the
791 * given devclass is allocated and returned in @p *listp. The number
792 * of drivers in the array is returned in @p *countp. The caller should
793 * free the array using @c free(p, M_TEMP).
795 * @param dc the devclass to examine
796 * @param listp gives location for array pointer return value
797 * @param countp gives location for number of array elements
801 * @retval ENOMEM the array allocation failed
804 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
811 TAILQ_FOREACH(dl, &dc->drivers, link)
813 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
818 TAILQ_FOREACH(dl, &dc->drivers, link) {
819 list[count] = dl->driver;
829 * @brief Get the number of devices in a devclass
831 * @param dc the devclass to examine
834 devclass_get_count(devclass_t dc)
839 for (i = 0; i < dc->maxunit; i++)
846 devclass_get_maxunit(devclass_t dc)
852 devclass_set_parent(devclass_t dc, devclass_t pdc)
858 devclass_get_parent(devclass_t dc)
864 devclass_alloc_unit(devclass_t dc, int *unitp)
868 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
870 /* If we have been given a wired unit number, check for existing device */
872 if (unit >= 0 && unit < dc->maxunit &&
873 dc->devices[unit] != NULL) {
875 kprintf("%s-: %s%d exists, using next available unit number\n",
876 dc->name, dc->name, unit);
877 /* find the next available slot */
878 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
882 /* Unwired device, find the next available slot for it */
884 while (unit < dc->maxunit && dc->devices[unit] != NULL)
889 * We've selected a unit beyond the length of the table, so let's
890 * extend the table to make room for all units up to and including
893 if (unit >= dc->maxunit) {
897 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
898 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
902 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
904 kfree(dc->devices, M_BUS);
905 dc->devices = newlist;
906 dc->maxunit = newsize;
908 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
915 devclass_add_device(devclass_t dc, device_t dev)
919 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
921 buflen = strlen(dc->name) + 5;
922 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
926 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
927 kfree(dev->nameunit, M_BUS);
928 dev->nameunit = NULL;
931 dc->devices[dev->unit] = dev;
933 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
939 devclass_delete_device(devclass_t dc, device_t dev)
944 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
946 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
947 panic("devclass_delete_device: inconsistent device class");
948 dc->devices[dev->unit] = NULL;
949 if (dev->flags & DF_WILDCARD)
951 dev->devclass = NULL;
952 kfree(dev->nameunit, M_BUS);
953 dev->nameunit = NULL;
959 make_device(device_t parent, const char *name, int unit)
964 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
967 dc = devclass_find_internal(name, NULL, TRUE);
969 kprintf("make_device: can't find device class %s\n", name);
975 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
979 dev->parent = parent;
980 TAILQ_INIT(&dev->children);
981 kobj_init((kobj_t) dev, &null_class);
983 dev->devclass = NULL;
985 dev->nameunit = NULL;
989 dev->flags = DF_ENABLED;
992 dev->flags |= DF_WILDCARD;
994 dev->flags |= DF_FIXEDCLASS;
995 if (devclass_add_device(dc, dev) != 0) {
996 kobj_delete((kobj_t)dev, M_BUS);
1003 dev->state = DS_NOTPRESENT;
1005 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1006 bus_data_generation_update();
1012 device_print_child(device_t dev, device_t child)
1016 if (device_is_alive(child))
1017 retval += BUS_PRINT_CHILD(dev, child);
1019 retval += device_printf(child, " not found\n");
1025 device_add_child(device_t dev, const char *name, int unit)
1027 return device_add_child_ordered(dev, 0, name, unit);
1031 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1036 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1039 child = make_device(dev, name, unit);
1042 child->order = order;
1044 TAILQ_FOREACH(place, &dev->children, link)
1045 if (place->order > order)
1050 * The device 'place' is the first device whose order is
1051 * greater than the new child.
1053 TAILQ_INSERT_BEFORE(place, child, link);
1056 * The new child's order is greater or equal to the order of
1057 * any existing device. Add the child to the tail of the list.
1059 TAILQ_INSERT_TAIL(&dev->children, child, link);
1062 bus_data_generation_update();
1067 device_delete_child(device_t dev, device_t child)
1070 device_t grandchild;
1072 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1074 /* remove children first */
1075 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1076 error = device_delete_child(child, grandchild);
1081 if ((error = device_detach(child)) != 0)
1083 if (child->devclass)
1084 devclass_delete_device(child->devclass, child);
1085 TAILQ_REMOVE(&dev->children, child, link);
1086 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1087 device_set_desc(child, NULL);
1088 kobj_delete((kobj_t)child, M_BUS);
1090 bus_data_generation_update();
1095 * @brief Find a device given a unit number
1097 * This is similar to devclass_get_devices() but only searches for
1098 * devices which have @p dev as a parent.
1100 * @param dev the parent device to search
1101 * @param unit the unit number to search for. If the unit is -1,
1102 * return the first child of @p dev which has name
1103 * @p classname (that is, the one with the lowest unit.)
1105 * @returns the device with the given unit number or @c
1106 * NULL if there is no such device
1109 device_find_child(device_t dev, const char *classname, int unit)
1114 dc = devclass_find(classname);
1119 child = devclass_get_device(dc, unit);
1120 if (child && child->parent == dev)
1123 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1124 child = devclass_get_device(dc, unit);
1125 if (child && child->parent == dev)
1133 first_matching_driver(devclass_t dc, device_t dev)
1136 return(devclass_find_driver_internal(dc, dev->devclass->name));
1138 return(TAILQ_FIRST(&dc->drivers));
1142 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1144 if (dev->devclass) {
1146 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1147 if (!strcmp(dev->devclass->name, dl->driver->name))
1151 return(TAILQ_NEXT(last, link));
1155 device_probe_child(device_t dev, device_t child)
1158 driverlink_t best = 0;
1160 int result, pri = 0;
1161 int hasclass = (child->devclass != 0);
1165 panic("device_probe_child: parent device has no devclass");
1167 if (child->state == DS_ALIVE)
1170 for (; dc; dc = dc->parent) {
1171 for (dl = first_matching_driver(dc, child); dl;
1172 dl = next_matching_driver(dc, child, dl)) {
1173 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1174 device_set_driver(child, dl->driver);
1176 device_set_devclass(child, dl->driver->name);
1177 result = DEVICE_PROBE(child);
1179 device_set_devclass(child, 0);
1182 * If the driver returns SUCCESS, there can be
1183 * no higher match for this device.
1192 * The driver returned an error so it
1193 * certainly doesn't match.
1196 device_set_driver(child, 0);
1201 * A priority lower than SUCCESS, remember the
1202 * best matching driver. Initialise the value
1203 * of pri for the first match.
1205 if (best == 0 || result > pri) {
1212 * If we have unambiguous match in this devclass,
1213 * don't look in the parent.
1215 if (best && pri == 0)
1220 * If we found a driver, change state and initialise the devclass.
1223 if (!child->devclass)
1224 device_set_devclass(child, best->driver->name);
1225 device_set_driver(child, best->driver);
1228 * A bit bogus. Call the probe method again to make
1229 * sure that we have the right description.
1231 DEVICE_PROBE(child);
1234 bus_data_generation_update();
1235 child->state = DS_ALIVE;
1243 device_get_parent(device_t dev)
1249 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1256 TAILQ_FOREACH(child, &dev->children, link)
1259 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1264 TAILQ_FOREACH(child, &dev->children, link) {
1265 list[count] = child;
1276 device_get_driver(device_t dev)
1278 return(dev->driver);
1282 device_get_devclass(device_t dev)
1284 return(dev->devclass);
1288 device_get_name(device_t dev)
1291 return devclass_get_name(dev->devclass);
1296 device_get_nameunit(device_t dev)
1298 return(dev->nameunit);
1302 device_get_unit(device_t dev)
1308 device_get_desc(device_t dev)
1314 device_get_flags(device_t dev)
1316 return(dev->devflags);
1320 device_print_prettyname(device_t dev)
1322 const char *name = device_get_name(dev);
1325 return kprintf("unknown: ");
1327 return kprintf("%s%d: ", name, device_get_unit(dev));
1331 device_printf(device_t dev, const char * fmt, ...)
1336 retval = device_print_prettyname(dev);
1337 __va_start(ap, fmt);
1338 retval += kvprintf(fmt, ap);
1344 device_set_desc_internal(device_t dev, const char* desc, int copy)
1346 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1347 kfree(dev->desc, M_BUS);
1348 dev->flags &= ~DF_DESCMALLOCED;
1353 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1355 strcpy(dev->desc, desc);
1356 dev->flags |= DF_DESCMALLOCED;
1359 /* Avoid a -Wcast-qual warning */
1360 dev->desc = (char *)(uintptr_t) desc;
1363 bus_data_generation_update();
1367 device_set_desc(device_t dev, const char* desc)
1369 device_set_desc_internal(dev, desc, FALSE);
1373 device_set_desc_copy(device_t dev, const char* desc)
1375 device_set_desc_internal(dev, desc, TRUE);
1379 device_set_flags(device_t dev, uint32_t flags)
1381 dev->devflags = flags;
1385 device_get_softc(device_t dev)
1391 device_set_softc(device_t dev, void *softc)
1393 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1394 kfree(dev->softc, M_BUS);
1397 dev->flags |= DF_EXTERNALSOFTC;
1399 dev->flags &= ~DF_EXTERNALSOFTC;
1403 device_set_async_attach(device_t dev, int enable)
1406 dev->flags |= DF_ASYNCPROBE;
1408 dev->flags &= ~DF_ASYNCPROBE;
1412 device_get_ivars(device_t dev)
1418 device_set_ivars(device_t dev, void * ivars)
1427 device_get_state(device_t dev)
1433 device_enable(device_t dev)
1435 dev->flags |= DF_ENABLED;
1439 device_disable(device_t dev)
1441 dev->flags &= ~DF_ENABLED;
1448 device_busy(device_t dev)
1450 if (dev->state < DS_ATTACHED)
1451 panic("device_busy: called for unattached device");
1452 if (dev->busy == 0 && dev->parent)
1453 device_busy(dev->parent);
1455 dev->state = DS_BUSY;
1462 device_unbusy(device_t dev)
1464 if (dev->state != DS_BUSY)
1465 panic("device_unbusy: called for non-busy device");
1467 if (dev->busy == 0) {
1469 device_unbusy(dev->parent);
1470 dev->state = DS_ATTACHED;
1475 device_quiet(device_t dev)
1477 dev->flags |= DF_QUIET;
1481 device_verbose(device_t dev)
1483 dev->flags &= ~DF_QUIET;
1487 device_is_quiet(device_t dev)
1489 return((dev->flags & DF_QUIET) != 0);
1493 device_is_enabled(device_t dev)
1495 return((dev->flags & DF_ENABLED) != 0);
1499 device_is_alive(device_t dev)
1501 return(dev->state >= DS_ALIVE);
1505 device_is_attached(device_t dev)
1507 return(dev->state >= DS_ATTACHED);
1511 device_set_devclass(device_t dev, const char *classname)
1518 devclass_delete_device(dev->devclass, dev);
1522 if (dev->devclass) {
1523 kprintf("device_set_devclass: device class already set\n");
1527 dc = devclass_find_internal(classname, NULL, TRUE);
1531 error = devclass_add_device(dc, dev);
1533 bus_data_generation_update();
1538 device_set_driver(device_t dev, driver_t *driver)
1540 if (dev->state >= DS_ATTACHED)
1543 if (dev->driver == driver)
1546 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1547 kfree(dev->softc, M_BUS);
1550 kobj_delete((kobj_t) dev, 0);
1551 dev->driver = driver;
1553 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1554 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1555 dev->softc = kmalloc(driver->size, M_BUS,
1556 M_INTWAIT | M_ZERO);
1558 kobj_delete((kobj_t)dev, 0);
1559 kobj_init((kobj_t) dev, &null_class);
1565 kobj_init((kobj_t) dev, &null_class);
1568 bus_data_generation_update();
1573 device_probe_and_attach(device_t dev)
1575 device_t bus = dev->parent;
1578 if (dev->state >= DS_ALIVE)
1581 if ((dev->flags & DF_ENABLED) == 0) {
1583 device_print_prettyname(dev);
1584 kprintf("not probed (disabled)\n");
1589 error = device_probe_child(bus, dev);
1591 if (!(dev->flags & DF_DONENOMATCH)) {
1592 BUS_PROBE_NOMATCH(bus, dev);
1594 dev->flags |= DF_DONENOMATCH;
1600 * Output the exact device chain prior to the attach in case the
1601 * system locks up during attach, and generate the full info after
1602 * the attach so correct irq and other information is displayed.
1604 if (bootverbose && !device_is_quiet(dev)) {
1607 kprintf("%s", device_get_nameunit(dev));
1608 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1609 kprintf(".%s", device_get_nameunit(tmp));
1612 if (!device_is_quiet(dev))
1613 device_print_child(bus, dev);
1614 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1615 kprintf("%s: probing asynchronously\n",
1616 device_get_nameunit(dev));
1617 dev->state = DS_INPROGRESS;
1618 device_attach_async(dev);
1621 error = device_doattach(dev);
1627 * Device is known to be alive, do the attach asynchronously.
1629 * The MP lock is held by all threads.
1632 device_attach_async(device_t dev)
1636 atomic_add_int(&numasyncthreads, 1);
1637 lwkt_create(device_attach_thread, dev, &td, NULL,
1638 0, 0, (dev->desc ? dev->desc : "devattach"));
1642 device_attach_thread(void *arg)
1646 (void)device_doattach(dev);
1647 atomic_subtract_int(&numasyncthreads, 1);
1648 wakeup(&numasyncthreads);
1652 * Device is known to be alive, do the attach (synchronous or asynchronous)
1655 device_doattach(device_t dev)
1657 device_t bus = dev->parent;
1658 int hasclass = (dev->devclass != 0);
1661 error = DEVICE_ATTACH(dev);
1663 dev->state = DS_ATTACHED;
1664 if (bootverbose && !device_is_quiet(dev))
1665 device_print_child(bus, dev);
1668 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1669 dev->driver->name, dev->unit, error);
1670 /* Unset the class that was set in device_probe_child */
1672 device_set_devclass(dev, 0);
1673 device_set_driver(dev, NULL);
1674 dev->state = DS_NOTPRESENT;
1680 device_detach(device_t dev)
1684 PDEBUG(("%s", DEVICENAME(dev)));
1685 if (dev->state == DS_BUSY)
1687 if (dev->state != DS_ATTACHED)
1690 if ((error = DEVICE_DETACH(dev)) != 0)
1693 device_printf(dev, "detached\n");
1695 BUS_CHILD_DETACHED(dev->parent, dev);
1697 if (!(dev->flags & DF_FIXEDCLASS))
1698 devclass_delete_device(dev->devclass, dev);
1700 dev->state = DS_NOTPRESENT;
1701 device_set_driver(dev, NULL);
1707 device_shutdown(device_t dev)
1709 if (dev->state < DS_ATTACHED)
1711 PDEBUG(("%s", DEVICENAME(dev)));
1712 return DEVICE_SHUTDOWN(dev);
1716 device_set_unit(device_t dev, int unit)
1721 dc = device_get_devclass(dev);
1722 if (unit < dc->maxunit && dc->devices[unit])
1724 err = devclass_delete_device(dc, dev);
1728 err = devclass_add_device(dc, dev);
1732 bus_data_generation_update();
1736 /*======================================*/
1738 * Access functions for device resources.
1741 /* Supplied by config(8) in ioconf.c */
1742 extern struct config_device config_devtab[];
1743 extern int devtab_count;
1745 /* Runtime version */
1746 struct config_device *devtab = config_devtab;
1749 resource_new_name(const char *name, int unit)
1751 struct config_device *new;
1753 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1754 M_INTWAIT | M_ZERO);
1757 if (devtab && devtab_count > 0)
1758 bcopy(devtab, new, devtab_count * sizeof(*new));
1759 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1760 if (new[devtab_count].name == NULL) {
1764 strcpy(new[devtab_count].name, name);
1765 new[devtab_count].unit = unit;
1766 new[devtab_count].resource_count = 0;
1767 new[devtab_count].resources = NULL;
1768 if (devtab && devtab != config_devtab)
1769 kfree(devtab, M_TEMP);
1771 return devtab_count++;
1775 resource_new_resname(int j, const char *resname, resource_type type)
1777 struct config_resource *new;
1780 i = devtab[j].resource_count;
1781 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1784 if (devtab[j].resources && i > 0)
1785 bcopy(devtab[j].resources, new, i * sizeof(*new));
1786 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1787 if (new[i].name == NULL) {
1791 strcpy(new[i].name, resname);
1793 if (devtab[j].resources)
1794 kfree(devtab[j].resources, M_TEMP);
1795 devtab[j].resources = new;
1796 devtab[j].resource_count = i + 1;
1801 resource_match_string(int i, const char *resname, const char *value)
1804 struct config_resource *res;
1806 for (j = 0, res = devtab[i].resources;
1807 j < devtab[i].resource_count; j++, res++)
1808 if (!strcmp(res->name, resname)
1809 && res->type == RES_STRING
1810 && !strcmp(res->u.stringval, value))
1816 resource_find(const char *name, int unit, const char *resname,
1817 struct config_resource **result)
1820 struct config_resource *res;
1823 * First check specific instances, then generic.
1825 for (i = 0; i < devtab_count; i++) {
1826 if (devtab[i].unit < 0)
1828 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1829 res = devtab[i].resources;
1830 for (j = 0; j < devtab[i].resource_count; j++, res++)
1831 if (!strcmp(res->name, resname)) {
1837 for (i = 0; i < devtab_count; i++) {
1838 if (devtab[i].unit >= 0)
1840 /* XXX should this `&& devtab[i].unit == unit' be here? */
1841 /* XXX if so, then the generic match does nothing */
1842 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1843 res = devtab[i].resources;
1844 for (j = 0; j < devtab[i].resource_count; j++, res++)
1845 if (!strcmp(res->name, resname)) {
1855 resource_int_value(const char *name, int unit, const char *resname, int *result)
1858 struct config_resource *res;
1860 if ((error = resource_find(name, unit, resname, &res)) != 0)
1862 if (res->type != RES_INT)
1864 *result = res->u.intval;
1869 resource_long_value(const char *name, int unit, const char *resname,
1873 struct config_resource *res;
1875 if ((error = resource_find(name, unit, resname, &res)) != 0)
1877 if (res->type != RES_LONG)
1879 *result = res->u.longval;
1884 resource_string_value(const char *name, int unit, const char *resname,
1888 struct config_resource *res;
1890 if ((error = resource_find(name, unit, resname, &res)) != 0)
1892 if (res->type != RES_STRING)
1894 *result = res->u.stringval;
1899 resource_query_string(int i, const char *resname, const char *value)
1905 for (; i < devtab_count; i++)
1906 if (resource_match_string(i, resname, value) >= 0)
1912 resource_locate(int i, const char *resname)
1918 for (; i < devtab_count; i++)
1919 if (!strcmp(devtab[i].name, resname))
1925 resource_count(void)
1927 return(devtab_count);
1931 resource_query_name(int i)
1933 return(devtab[i].name);
1937 resource_query_unit(int i)
1939 return(devtab[i].unit);
1943 resource_create(const char *name, int unit, const char *resname,
1944 resource_type type, struct config_resource **result)
1947 struct config_resource *res = NULL;
1949 for (i = 0; i < devtab_count; i++)
1950 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1951 res = devtab[i].resources;
1955 i = resource_new_name(name, unit);
1958 res = devtab[i].resources;
1960 for (j = 0; j < devtab[i].resource_count; j++, res++)
1961 if (!strcmp(res->name, resname)) {
1965 j = resource_new_resname(i, resname, type);
1968 res = &devtab[i].resources[j];
1974 resource_set_int(const char *name, int unit, const char *resname, int value)
1977 struct config_resource *res;
1979 error = resource_create(name, unit, resname, RES_INT, &res);
1982 if (res->type != RES_INT)
1984 res->u.intval = value;
1989 resource_set_long(const char *name, int unit, const char *resname, long value)
1992 struct config_resource *res;
1994 error = resource_create(name, unit, resname, RES_LONG, &res);
1997 if (res->type != RES_LONG)
1999 res->u.longval = value;
2004 resource_set_string(const char *name, int unit, const char *resname,
2008 struct config_resource *res;
2010 error = resource_create(name, unit, resname, RES_STRING, &res);
2013 if (res->type != RES_STRING)
2015 if (res->u.stringval)
2016 kfree(res->u.stringval, M_TEMP);
2017 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2018 if (res->u.stringval == NULL)
2020 strcpy(res->u.stringval, value);
2025 resource_cfgload(void *dummy __unused)
2027 struct config_resource *res, *cfgres;
2030 char *name, *resname;
2034 int config_devtab_count;
2036 config_devtab_count = devtab_count;
2040 for (i = 0; i < config_devtab_count; i++) {
2041 name = config_devtab[i].name;
2042 unit = config_devtab[i].unit;
2044 for (j = 0; j < config_devtab[i].resource_count; j++) {
2045 cfgres = config_devtab[i].resources;
2046 resname = cfgres[j].name;
2047 type = cfgres[j].type;
2048 error = resource_create(name, unit, resname, type,
2051 kprintf("create resource %s%d: error %d\n",
2055 if (res->type != type) {
2056 kprintf("type mismatch %s%d: %d != %d\n",
2057 name, unit, res->type, type);
2062 res->u.intval = cfgres[j].u.intval;
2065 res->u.longval = cfgres[j].u.longval;
2068 if (res->u.stringval)
2069 kfree(res->u.stringval, M_TEMP);
2070 stringval = cfgres[j].u.stringval;
2071 res->u.stringval = kmalloc(strlen(stringval) + 1,
2073 if (res->u.stringval == NULL)
2075 strcpy(res->u.stringval, stringval);
2078 panic("unknown resource type %d", type);
2083 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2086 /*======================================*/
2088 * Some useful method implementations to make life easier for bus drivers.
2092 resource_list_init(struct resource_list *rl)
2098 resource_list_free(struct resource_list *rl)
2100 struct resource_list_entry *rle;
2102 while ((rle = SLIST_FIRST(rl)) != NULL) {
2104 panic("resource_list_free: resource entry is busy");
2105 SLIST_REMOVE_HEAD(rl, link);
2111 resource_list_add(struct resource_list *rl,
2113 u_long start, u_long end, u_long count)
2115 struct resource_list_entry *rle;
2117 rle = resource_list_find(rl, type, rid);
2119 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2122 panic("resource_list_add: can't record entry");
2123 SLIST_INSERT_HEAD(rl, rle, link);
2130 panic("resource_list_add: resource entry is busy");
2137 struct resource_list_entry*
2138 resource_list_find(struct resource_list *rl,
2141 struct resource_list_entry *rle;
2143 SLIST_FOREACH(rle, rl, link)
2144 if (rle->type == type && rle->rid == rid)
2150 resource_list_delete(struct resource_list *rl,
2153 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2156 if (rle->res != NULL)
2157 panic("resource_list_delete: resource has not been released");
2158 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2164 resource_list_alloc(struct resource_list *rl,
2165 device_t bus, device_t child,
2167 u_long start, u_long end,
2168 u_long count, u_int flags)
2170 struct resource_list_entry *rle = 0;
2171 int passthrough = (device_get_parent(child) != bus);
2172 int isdefault = (start == 0UL && end == ~0UL);
2175 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2177 start, end, count, flags));
2180 rle = resource_list_find(rl, type, *rid);
2183 return(0); /* no resource of that type/rid */
2186 panic("resource_list_alloc: resource entry is busy");
2190 count = max(count, rle->count);
2191 end = max(rle->end, start + count - 1);
2194 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2195 type, rid, start, end, count, flags);
2198 * Record the new range.
2201 rle->start = rman_get_start(rle->res);
2202 rle->end = rman_get_end(rle->res);
2210 resource_list_release(struct resource_list *rl,
2211 device_t bus, device_t child,
2212 int type, int rid, struct resource *res)
2214 struct resource_list_entry *rle = 0;
2215 int passthrough = (device_get_parent(child) != bus);
2219 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2223 rle = resource_list_find(rl, type, rid);
2226 panic("resource_list_release: can't find resource");
2228 panic("resource_list_release: resource entry is not busy");
2230 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2240 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2243 struct resource_list_entry *rle;
2244 int printed, retval;
2248 /* Yes, this is kinda cheating */
2249 SLIST_FOREACH(rle, rl, link) {
2250 if (rle->type == type) {
2252 retval += kprintf(" %s ", name);
2254 retval += kprintf(",");
2256 retval += kprintf(format, rle->start);
2257 if (rle->count > 1) {
2258 retval += kprintf("-");
2259 retval += kprintf(format, rle->start +
2268 * Generic driver/device identify functions. These will install a device
2269 * rendezvous point under the parent using the same name as the driver
2270 * name, which will at a later time be probed and attached.
2272 * These functions are used when the parent does not 'scan' its bus for
2273 * matching devices, or for the particular devices using these functions,
2274 * or when the device is a pseudo or synthesized device (such as can be
2275 * found under firewire and ppbus).
2278 bus_generic_identify(driver_t *driver, device_t parent)
2280 if (parent->state == DS_ATTACHED)
2282 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2287 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2289 if (parent->state == DS_ATTACHED)
2291 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2296 * Call DEVICE_IDENTIFY for each driver.
2299 bus_generic_probe(device_t dev)
2301 devclass_t dc = dev->devclass;
2304 TAILQ_FOREACH(dl, &dc->drivers, link) {
2305 DEVICE_IDENTIFY(dl->driver, dev);
2312 * This is an aweful hack due to the isa bus and autoconf code not
2313 * probing the ISA devices until after everything else has configured.
2314 * The ISA bus did a dummy attach long ago so we have to set it back
2315 * to an earlier state so the probe thinks its the initial probe and
2318 * XXX remove by properly defering the ISA bus scan.
2321 bus_generic_probe_hack(device_t dev)
2323 if (dev->state == DS_ATTACHED) {
2324 dev->state = DS_ALIVE;
2325 bus_generic_probe(dev);
2326 dev->state = DS_ATTACHED;
2332 bus_generic_attach(device_t dev)
2336 TAILQ_FOREACH(child, &dev->children, link) {
2337 device_probe_and_attach(child);
2344 bus_generic_detach(device_t dev)
2349 if (dev->state != DS_ATTACHED)
2352 TAILQ_FOREACH(child, &dev->children, link)
2353 if ((error = device_detach(child)) != 0)
2360 bus_generic_shutdown(device_t dev)
2364 TAILQ_FOREACH(child, &dev->children, link)
2365 device_shutdown(child);
2371 bus_generic_suspend(device_t dev)
2374 device_t child, child2;
2376 TAILQ_FOREACH(child, &dev->children, link) {
2377 error = DEVICE_SUSPEND(child);
2379 for (child2 = TAILQ_FIRST(&dev->children);
2380 child2 && child2 != child;
2381 child2 = TAILQ_NEXT(child2, link))
2382 DEVICE_RESUME(child2);
2390 bus_generic_resume(device_t dev)
2394 TAILQ_FOREACH(child, &dev->children, link)
2395 DEVICE_RESUME(child);
2396 /* if resume fails, there's nothing we can usefully do... */
2402 bus_print_child_header(device_t dev, device_t child)
2406 if (device_get_desc(child))
2407 retval += device_printf(child, "<%s>", device_get_desc(child));
2409 retval += kprintf("%s", device_get_nameunit(child));
2411 if (child->state != DS_ATTACHED)
2412 kprintf(" [tentative]");
2414 kprintf(" [attached!]");
2420 bus_print_child_footer(device_t dev, device_t child)
2422 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2426 bus_generic_add_child(device_t dev, device_t child, int order,
2427 const char *name, int unit)
2430 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2432 dev = device_add_child_ordered(child, order, name, unit);
2438 bus_generic_print_child(device_t dev, device_t child)
2442 retval += bus_print_child_header(dev, child);
2443 retval += bus_print_child_footer(dev, child);
2449 bus_generic_read_ivar(device_t dev, device_t child, int index,
2455 error = BUS_READ_IVAR(dev->parent, child, index, result);
2462 bus_generic_write_ivar(device_t dev, device_t child, int index,
2468 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2475 * Resource list are used for iterations, do not recurse.
2477 struct resource_list *
2478 bus_generic_get_resource_list(device_t dev, device_t child)
2484 bus_generic_driver_added(device_t dev, driver_t *driver)
2488 DEVICE_IDENTIFY(driver, dev);
2489 TAILQ_FOREACH(child, &dev->children, link) {
2490 if (child->state == DS_NOTPRESENT)
2491 device_probe_and_attach(child);
2496 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2497 int flags, driver_intr_t *intr, void *arg,
2498 void **cookiep, lwkt_serialize_t serializer)
2500 /* Propagate up the bus hierarchy until someone handles it. */
2502 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2503 intr, arg, cookiep, serializer));
2509 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2512 /* Propagate up the bus hierarchy until someone handles it. */
2514 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2520 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2523 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2529 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2532 BUS_ENABLE_INTR(dev->parent, child, cookie);
2536 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2537 enum intr_polarity pol)
2539 /* Propagate up the bus hierarchy until someone handles it. */
2541 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2547 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2548 u_long start, u_long end, u_long count, u_int flags)
2550 /* Propagate up the bus hierarchy until someone handles it. */
2552 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2553 start, end, count, flags));
2559 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2562 /* Propagate up the bus hierarchy until someone handles it. */
2564 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2570 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2573 /* Propagate up the bus hierarchy until someone handles it. */
2575 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2581 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2582 int rid, struct resource *r)
2584 /* Propagate up the bus hierarchy until someone handles it. */
2586 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2593 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2594 u_long *startp, u_long *countp)
2600 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2607 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2608 u_long start, u_long count)
2614 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2621 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2624 BUS_DELETE_RESOURCE(dev, child, type, rid);
2628 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2629 u_long *startp, u_long *countp)
2631 struct resource_list *rl = NULL;
2632 struct resource_list_entry *rle = NULL;
2634 rl = BUS_GET_RESOURCE_LIST(dev, child);
2638 rle = resource_list_find(rl, type, rid);
2643 *startp = rle->start;
2645 *countp = rle->count;
2651 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2652 u_long start, u_long count)
2654 struct resource_list *rl = NULL;
2656 rl = BUS_GET_RESOURCE_LIST(dev, child);
2660 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2666 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2668 struct resource_list *rl = NULL;
2670 rl = BUS_GET_RESOURCE_LIST(dev, child);
2674 resource_list_delete(rl, type, rid);
2678 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2679 int rid, struct resource *r)
2681 struct resource_list *rl = NULL;
2683 rl = BUS_GET_RESOURCE_LIST(dev, child);
2687 return(resource_list_release(rl, dev, child, type, rid, r));
2691 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2692 int *rid, u_long start, u_long end, u_long count, u_int flags)
2694 struct resource_list *rl = NULL;
2696 rl = BUS_GET_RESOURCE_LIST(dev, child);
2700 return(resource_list_alloc(rl, dev, child, type, rid,
2701 start, end, count, flags));
2705 bus_generic_child_present(device_t bus, device_t child)
2707 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2712 * Some convenience functions to make it easier for drivers to use the
2713 * resource-management functions. All these really do is hide the
2714 * indirection through the parent's method table, making for slightly
2715 * less-wordy code. In the future, it might make sense for this code
2716 * to maintain some sort of a list of resources allocated by each device.
2719 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2720 struct resource **res)
2724 for (i = 0; rs[i].type != -1; i++)
2726 for (i = 0; rs[i].type != -1; i++) {
2727 res[i] = bus_alloc_resource_any(dev,
2728 rs[i].type, &rs[i].rid, rs[i].flags);
2729 if (res[i] == NULL) {
2730 bus_release_resources(dev, rs, res);
2738 bus_release_resources(device_t dev, const struct resource_spec *rs,
2739 struct resource **res)
2743 for (i = 0; rs[i].type != -1; i++)
2744 if (res[i] != NULL) {
2745 bus_release_resource(
2746 dev, rs[i].type, rs[i].rid, res[i]);
2752 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2753 u_long count, u_int flags)
2755 if (dev->parent == 0)
2757 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2762 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2764 if (dev->parent == 0)
2766 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2770 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2772 if (dev->parent == 0)
2774 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2778 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2780 if (dev->parent == 0)
2782 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2786 bus_setup_intr(device_t dev, struct resource *r, int flags,
2787 driver_intr_t handler, void *arg,
2788 void **cookiep, lwkt_serialize_t serializer)
2790 if (dev->parent == 0)
2792 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2793 cookiep, serializer));
2797 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2799 if (dev->parent == 0)
2801 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2805 bus_enable_intr(device_t dev, void *cookie)
2808 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2812 bus_disable_intr(device_t dev, void *cookie)
2815 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2821 bus_set_resource(device_t dev, int type, int rid,
2822 u_long start, u_long count)
2824 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2829 bus_get_resource(device_t dev, int type, int rid,
2830 u_long *startp, u_long *countp)
2832 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2837 bus_get_resource_start(device_t dev, int type, int rid)
2839 u_long start, count;
2842 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2850 bus_get_resource_count(device_t dev, int type, int rid)
2852 u_long start, count;
2855 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2863 bus_delete_resource(device_t dev, int type, int rid)
2865 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2869 bus_child_present(device_t child)
2871 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2875 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2879 parent = device_get_parent(child);
2880 if (parent == NULL) {
2884 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2888 bus_child_location_str(device_t child, char *buf, size_t buflen)
2892 parent = device_get_parent(child);
2893 if (parent == NULL) {
2897 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2901 root_print_child(device_t dev, device_t child)
2907 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2908 void **cookiep, lwkt_serialize_t serializer)
2911 * If an interrupt mapping gets to here something bad has happened.
2913 panic("root_setup_intr");
2917 * If we get here, assume that the device is permanant and really is
2918 * present in the system. Removable bus drivers are expected to intercept
2919 * this call long before it gets here. We return -1 so that drivers that
2920 * really care can check vs -1 or some ERRNO returned higher in the food
2924 root_child_present(device_t dev, device_t child)
2930 * XXX NOTE! other defaults may be set in bus_if.m
2932 static kobj_method_t root_methods[] = {
2933 /* Device interface */
2934 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2935 KOBJMETHOD(device_suspend, bus_generic_suspend),
2936 KOBJMETHOD(device_resume, bus_generic_resume),
2939 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2940 KOBJMETHOD(bus_print_child, root_print_child),
2941 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2942 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2943 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2944 KOBJMETHOD(bus_child_present, root_child_present),
2949 static driver_t root_driver = {
2956 devclass_t root_devclass;
2959 root_bus_module_handler(module_t mod, int what, void* arg)
2963 TAILQ_INIT(&bus_data_devices);
2964 root_bus = make_device(NULL, "root", 0);
2965 root_bus->desc = "System root bus";
2966 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2967 root_bus->driver = &root_driver;
2968 root_bus->state = DS_ALIVE;
2969 root_devclass = devclass_find_internal("root", NULL, FALSE);
2974 device_shutdown(root_bus);
2981 static moduledata_t root_bus_mod = {
2983 root_bus_module_handler,
2986 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2989 root_bus_configure(void)
2997 * handle device_identify based device attachments to the root_bus
2998 * (typically nexus).
3000 bus_generic_probe(root_bus);
3003 * Probe and attach the devices under root_bus.
3005 TAILQ_FOREACH(dev, &root_bus->children, link) {
3006 device_probe_and_attach(dev);
3010 * Wait for all asynchronous attaches to complete. If we don't
3011 * our legacy ISA bus scan could steal device unit numbers or
3015 if (numasyncthreads)
3016 kprintf("Waiting for async drivers to attach\n");
3017 while (numasyncthreads > 0) {
3018 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3020 if (warncount == 0) {
3021 kprintf("Warning: Still waiting for %d "
3022 "drivers to attach\n", numasyncthreads);
3023 } else if (warncount == -30) {
3024 kprintf("Giving up on %d drivers\n", numasyncthreads);
3028 root_bus->state = DS_ATTACHED;
3032 driver_module_handler(module_t mod, int what, void *arg)
3035 struct driver_module_data *dmd;
3036 devclass_t bus_devclass;
3037 kobj_class_t driver;
3038 const char *parentname;
3040 dmd = (struct driver_module_data *)arg;
3041 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3046 if (dmd->dmd_chainevh)
3047 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3049 driver = dmd->dmd_driver;
3050 PDEBUG(("Loading module: driver %s on bus %s",
3051 DRIVERNAME(driver), dmd->dmd_busname));
3054 * If the driver has any base classes, make the
3055 * devclass inherit from the devclass of the driver's
3056 * first base class. This will allow the system to
3057 * search for drivers in both devclasses for children
3058 * of a device using this driver.
3060 if (driver->baseclasses)
3061 parentname = driver->baseclasses[0]->name;
3064 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3067 error = devclass_add_driver(bus_devclass, driver);
3073 PDEBUG(("Unloading module: driver %s from bus %s",
3074 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3075 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3077 if (!error && dmd->dmd_chainevh)
3078 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3088 * The _short versions avoid iteration by not calling anything that prints
3089 * more than oneliners. I love oneliners.
3093 print_device_short(device_t dev, int indent)
3098 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3099 dev->unit, dev->desc,
3100 (dev->parent? "":"no "),
3101 (TAILQ_EMPTY(&dev->children)? "no ":""),
3102 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3103 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3104 (dev->flags&DF_WILDCARD? "wildcard,":""),
3105 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3106 (dev->ivars? "":"no "),
3107 (dev->softc? "":"no "),
3112 print_device(device_t dev, int indent)
3117 print_device_short(dev, indent);
3119 indentprintf(("Parent:\n"));
3120 print_device_short(dev->parent, indent+1);
3121 indentprintf(("Driver:\n"));
3122 print_driver_short(dev->driver, indent+1);
3123 indentprintf(("Devclass:\n"));
3124 print_devclass_short(dev->devclass, indent+1);
3128 * Print the device and all its children (indented).
3131 print_device_tree_short(device_t dev, int indent)
3138 print_device_short(dev, indent);
3140 TAILQ_FOREACH(child, &dev->children, link)
3141 print_device_tree_short(child, indent+1);
3145 * Print the device and all its children (indented).
3148 print_device_tree(device_t dev, int indent)
3155 print_device(dev, indent);
3157 TAILQ_FOREACH(child, &dev->children, link)
3158 print_device_tree(child, indent+1);
3162 print_driver_short(driver_t *driver, int indent)
3167 indentprintf(("driver %s: softc size = %d\n",
3168 driver->name, driver->size));
3172 print_driver(driver_t *driver, int indent)
3177 print_driver_short(driver, indent);
3182 print_driver_list(driver_list_t drivers, int indent)
3184 driverlink_t driver;
3186 TAILQ_FOREACH(driver, &drivers, link)
3187 print_driver(driver->driver, indent);
3191 print_devclass_short(devclass_t dc, int indent)
3196 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3200 print_devclass(devclass_t dc, int indent)
3207 print_devclass_short(dc, indent);
3208 indentprintf(("Drivers:\n"));
3209 print_driver_list(dc->drivers, indent+1);
3211 indentprintf(("Devices:\n"));
3212 for (i = 0; i < dc->maxunit; i++)
3214 print_device(dc->devices[i], indent+1);
3218 print_devclass_list_short(void)
3222 kprintf("Short listing of devclasses, drivers & devices:\n");
3223 TAILQ_FOREACH(dc, &devclasses, link) {
3224 print_devclass_short(dc, 0);
3229 print_devclass_list(void)
3233 kprintf("Full listing of devclasses, drivers & devices:\n");
3234 TAILQ_FOREACH(dc, &devclasses, link) {
3235 print_devclass(dc, 0);
3242 * Check to see if a device is disabled via a disabled hint.
3245 resource_disabled(const char *name, int unit)
3249 error = resource_int_value(name, unit, "disabled", &value);
3256 * User-space access to the device tree.
3258 * We implement a small set of nodes:
3260 * hw.bus Single integer read method to obtain the
3261 * current generation count.
3262 * hw.bus.devices Reads the entire device tree in flat space.
3263 * hw.bus.rman Resource manager interface
3265 * We might like to add the ability to scan devclasses and/or drivers to
3266 * determine what else is currently loaded/available.
3270 sysctl_bus(SYSCTL_HANDLER_ARGS)
3272 struct u_businfo ubus;
3274 ubus.ub_version = BUS_USER_VERSION;
3275 ubus.ub_generation = bus_data_generation;
3277 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3279 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3280 "bus-related data");
3283 sysctl_devices(SYSCTL_HANDLER_ARGS)
3285 int *name = (int *)arg1;
3286 u_int namelen = arg2;
3289 struct u_device udev; /* XXX this is a bit big */
3295 if (bus_data_generation_check(name[0]))
3301 * Scan the list of devices, looking for the requested index.
3303 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3311 * Populate the return array.
3313 bzero(&udev, sizeof(udev));
3314 udev.dv_handle = (uintptr_t)dev;
3315 udev.dv_parent = (uintptr_t)dev->parent;
3316 if (dev->nameunit != NULL)
3317 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3318 if (dev->desc != NULL)
3319 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3320 if (dev->driver != NULL && dev->driver->name != NULL)
3321 strlcpy(udev.dv_drivername, dev->driver->name,
3322 sizeof(udev.dv_drivername));
3323 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3324 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3325 udev.dv_devflags = dev->devflags;
3326 udev.dv_flags = dev->flags;
3327 udev.dv_state = dev->state;
3328 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3332 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3333 "system device tree");
3336 bus_data_generation_check(int generation)
3338 if (generation != bus_data_generation)
3341 /* XXX generate optimised lists here? */
3346 bus_data_generation_update(void)
3348 bus_data_generation++;