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>
47 #include <sys/filio.h>
48 #include <sys/event.h>
49 #include <sys/signalvar.h>
51 #include <machine/stdarg.h> /* for device_printf() */
53 #include <sys/thread2.h>
54 #include <sys/mplock2.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_kqfilter_t devkqfilter;
146 static struct dev_ops devctl_ops = {
147 { "devctl", CDEV_MAJOR, 0 },
152 .d_kqfilter = devkqfilter
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. */
276 static void dev_filter_detach(struct knote *);
277 static int dev_filter_read(struct knote *, long);
279 static struct filterops dev_filtops =
280 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
283 devkqfilter(struct dev_kqfilter_args *ap)
285 struct knote *kn = ap->a_kn;
289 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
291 switch (kn->kn_filter) {
293 kn->kn_fop = &dev_filtops;
296 ap->a_result = EOPNOTSUPP;
297 lockmgr(&devsoftc.lock, LK_RELEASE);
301 klist = &devsoftc.kq.ki_note;
302 knote_insert(klist, kn);
304 lockmgr(&devsoftc.lock, LK_RELEASE);
310 dev_filter_detach(struct knote *kn)
314 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
315 klist = &devsoftc.kq.ki_note;
316 knote_remove(klist, kn);
317 lockmgr(&devsoftc.lock, LK_RELEASE);
321 dev_filter_read(struct knote *kn, long hint)
325 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
326 if (!TAILQ_EMPTY(&devsoftc.devq))
328 lockmgr(&devsoftc.lock, LK_RELEASE);
335 * @brief Return whether the userland process is running
338 devctl_process_running(void)
340 return (devsoftc.inuse == 1);
344 * @brief Queue data to be read from the devctl device
346 * Generic interface to queue data to the devctl device. It is
347 * assumed that @p data is properly formatted. It is further assumed
348 * that @p data is allocated using the M_BUS malloc type.
351 devctl_queue_data(char *data)
353 struct dev_event_info *n1 = NULL;
356 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
360 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
361 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
363 lockmgr(&devsoftc.lock, LK_RELEASE);
364 get_mplock(); /* XXX */
365 KNOTE(&devsoftc.kq.ki_note, 0);
366 rel_mplock(); /* XXX */
367 p = devsoftc.async_proc;
373 * @brief Send a 'notification' to userland, using standard ways
376 devctl_notify(const char *system, const char *subsystem, const char *type,
383 return; /* BOGUS! Must specify system. */
384 if (subsystem == NULL)
385 return; /* BOGUS! Must specify subsystem. */
387 return; /* BOGUS! Must specify type. */
388 len += strlen(" system=") + strlen(system);
389 len += strlen(" subsystem=") + strlen(subsystem);
390 len += strlen(" type=") + strlen(type);
391 /* add in the data message plus newline. */
394 len += 3; /* '!', '\n', and NUL */
395 msg = kmalloc(len, M_BUS, M_NOWAIT);
397 return; /* Drop it on the floor */
399 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
400 system, subsystem, type, data);
402 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
403 system, subsystem, type);
404 devctl_queue_data(msg);
408 * Common routine that tries to make sending messages as easy as possible.
409 * We allocate memory for the data, copy strings into that, but do not
410 * free it unless there's an error. The dequeue part of the driver should
411 * free the data. We don't send data when the device is disabled. We do
412 * send data, even when we have no listeners, because we wish to avoid
413 * races relating to startup and restart of listening applications.
415 * devaddq is designed to string together the type of event, with the
416 * object of that event, plus the plug and play info and location info
417 * for that event. This is likely most useful for devices, but less
418 * useful for other consumers of this interface. Those should use
419 * the devctl_queue_data() interface instead.
422 devaddq(const char *type, const char *what, device_t dev)
431 data = kmalloc(1024, M_BUS, M_NOWAIT);
435 /* get the bus specific location of this device */
436 loc = kmalloc(1024, M_BUS, M_NOWAIT);
440 bus_child_location_str(dev, loc, 1024);
442 /* Get the bus specific pnp info of this device */
443 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
447 bus_child_pnpinfo_str(dev, pnp, 1024);
449 /* Get the parent of this device, or / if high enough in the tree. */
450 if (device_get_parent(dev) == NULL)
451 parstr = "."; /* Or '/' ? */
453 parstr = device_get_nameunit(device_get_parent(dev));
454 /* String it all together. */
455 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
459 devctl_queue_data(data);
469 * A device was added to the tree. We are called just after it successfully
470 * attaches (that is, probe and attach success for this device). No call
471 * is made if a device is merely parented into the tree. See devnomatch
472 * if probe fails. If attach fails, no notification is sent (but maybe
473 * we should have a different message for this).
476 devadded(device_t dev)
481 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
484 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
488 bus_child_pnpinfo_str(dev, pnp, 1024);
489 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
490 devaddq("+", tmp, dev);
500 * A device was removed from the tree. We are called just before this
504 devremoved(device_t dev)
509 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
512 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
516 bus_child_pnpinfo_str(dev, pnp, 1024);
517 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
518 devaddq("-", tmp, dev);
528 * Called when there's no match for this device. This is only called
529 * the first time that no match happens, so we don't keep getitng this
530 * message. Should that prove to be undesirable, we can change it.
531 * This is called when all drivers that can attach to a given bus
532 * decline to accept this device. Other errrors may not be detected.
535 devnomatch(device_t dev)
537 devaddq("?", "", dev);
541 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
543 struct dev_event_info *n1;
546 dis = devctl_disable;
547 error = sysctl_handle_int(oidp, &dis, 0, req);
548 if (error || !req->newptr)
550 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
551 devctl_disable = dis;
553 while (!TAILQ_EMPTY(&devsoftc.devq)) {
554 n1 = TAILQ_FIRST(&devsoftc.devq);
555 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
556 kfree(n1->dei_data, M_BUS);
560 lockmgr(&devsoftc.lock, LK_RELEASE);
564 /* End of /dev/devctl code */
566 TAILQ_HEAD(,device) bus_data_devices;
567 static int bus_data_generation = 1;
569 kobj_method_t null_methods[] = {
573 DEFINE_CLASS(null, null_methods, 0);
576 * Devclass implementation
579 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
582 devclass_find_internal(const char *classname, const char *parentname,
587 PDEBUG(("looking for %s", classname));
588 if (classname == NULL)
591 TAILQ_FOREACH(dc, &devclasses, link)
592 if (!strcmp(dc->name, classname))
596 PDEBUG(("creating %s", classname));
597 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
598 M_BUS, M_INTWAIT | M_ZERO);
602 dc->name = (char*) (dc + 1);
603 strcpy(dc->name, classname);
606 TAILQ_INIT(&dc->drivers);
607 TAILQ_INSERT_TAIL(&devclasses, dc, link);
609 bus_data_generation_update();
612 if (parentname && dc && !dc->parent)
613 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
619 devclass_create(const char *classname)
621 return(devclass_find_internal(classname, NULL, TRUE));
625 devclass_find(const char *classname)
627 return(devclass_find_internal(classname, NULL, FALSE));
631 devclass_find_unit(const char *classname, int unit)
635 if ((dc = devclass_find(classname)) != NULL)
636 return(devclass_get_device(dc, unit));
641 devclass_add_driver(devclass_t dc, driver_t *driver)
647 PDEBUG(("%s", DRIVERNAME(driver)));
649 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
654 * Compile the driver's methods. Also increase the reference count
655 * so that the class doesn't get freed when the last instance
656 * goes. This means we can safely use static methods and avoids a
657 * double-free in devclass_delete_driver.
659 kobj_class_instantiate(driver);
662 * Make sure the devclass which the driver is implementing exists.
664 devclass_find_internal(driver->name, NULL, TRUE);
667 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
670 * Call BUS_DRIVER_ADDED for any existing busses in this class,
671 * but only if the bus has already been attached (otherwise we
672 * might probe too early).
674 * This is what will cause a newly loaded module to be associated
675 * with hardware. bus_generic_driver_added() is typically what ends
678 for (i = 0; i < dc->maxunit; i++) {
679 if ((dev = dc->devices[i]) != NULL) {
680 if (dev->state >= DS_ATTACHED)
681 BUS_DRIVER_ADDED(dev, driver);
685 bus_data_generation_update();
690 devclass_delete_driver(devclass_t busclass, driver_t *driver)
692 devclass_t dc = devclass_find(driver->name);
698 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
704 * Find the link structure in the bus' list of drivers.
706 TAILQ_FOREACH(dl, &busclass->drivers, link)
707 if (dl->driver == driver)
711 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
716 * Disassociate from any devices. We iterate through all the
717 * devices in the devclass of the driver and detach any which are
718 * using the driver and which have a parent in the devclass which
719 * we are deleting from.
721 * Note that since a driver can be in multiple devclasses, we
722 * should not detach devices which are not children of devices in
723 * the affected devclass.
725 for (i = 0; i < dc->maxunit; i++)
726 if (dc->devices[i]) {
727 dev = dc->devices[i];
728 if (dev->driver == driver && dev->parent &&
729 dev->parent->devclass == busclass) {
730 if ((error = device_detach(dev)) != 0)
732 device_set_driver(dev, NULL);
736 TAILQ_REMOVE(&busclass->drivers, dl, link);
739 kobj_class_uninstantiate(driver);
741 bus_data_generation_update();
746 devclass_find_driver_internal(devclass_t dc, const char *classname)
750 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
752 TAILQ_FOREACH(dl, &dc->drivers, link)
753 if (!strcmp(dl->driver->name, classname))
756 PDEBUG(("not found"));
761 devclass_find_driver(devclass_t dc, const char *classname)
765 dl = devclass_find_driver_internal(dc, classname);
773 devclass_get_name(devclass_t dc)
779 devclass_get_device(devclass_t dc, int unit)
781 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
783 return(dc->devices[unit]);
787 devclass_get_softc(devclass_t dc, int unit)
791 dev = devclass_get_device(dc, unit);
795 return(device_get_softc(dev));
799 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
806 for (i = 0; i < dc->maxunit; i++)
810 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
815 for (i = 0; i < dc->maxunit; i++)
816 if (dc->devices[i]) {
817 list[count] = dc->devices[i];
828 * @brief Get a list of drivers in the devclass
830 * An array containing a list of pointers to all the drivers in the
831 * given devclass is allocated and returned in @p *listp. The number
832 * of drivers in the array is returned in @p *countp. The caller should
833 * free the array using @c free(p, M_TEMP).
835 * @param dc the devclass to examine
836 * @param listp gives location for array pointer return value
837 * @param countp gives location for number of array elements
841 * @retval ENOMEM the array allocation failed
844 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
851 TAILQ_FOREACH(dl, &dc->drivers, link)
853 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
858 TAILQ_FOREACH(dl, &dc->drivers, link) {
859 list[count] = dl->driver;
869 * @brief Get the number of devices in a devclass
871 * @param dc the devclass to examine
874 devclass_get_count(devclass_t dc)
879 for (i = 0; i < dc->maxunit; i++)
886 devclass_get_maxunit(devclass_t dc)
892 devclass_set_parent(devclass_t dc, devclass_t pdc)
898 devclass_get_parent(devclass_t dc)
904 devclass_alloc_unit(devclass_t dc, int *unitp)
908 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
910 /* If we have been given a wired unit number, check for existing device */
912 if (unit >= 0 && unit < dc->maxunit &&
913 dc->devices[unit] != NULL) {
915 kprintf("%s-: %s%d exists, using next available unit number\n",
916 dc->name, dc->name, unit);
917 /* find the next available slot */
918 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
922 /* Unwired device, find the next available slot for it */
924 while (unit < dc->maxunit && dc->devices[unit] != NULL)
929 * We've selected a unit beyond the length of the table, so let's
930 * extend the table to make room for all units up to and including
933 if (unit >= dc->maxunit) {
937 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
938 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
942 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
944 kfree(dc->devices, M_BUS);
945 dc->devices = newlist;
946 dc->maxunit = newsize;
948 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
955 devclass_add_device(devclass_t dc, device_t dev)
959 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
961 buflen = strlen(dc->name) + 5;
962 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
966 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
967 kfree(dev->nameunit, M_BUS);
968 dev->nameunit = NULL;
971 dc->devices[dev->unit] = dev;
973 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
979 devclass_delete_device(devclass_t dc, device_t dev)
984 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
986 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
987 panic("devclass_delete_device: inconsistent device class");
988 dc->devices[dev->unit] = NULL;
989 if (dev->flags & DF_WILDCARD)
991 dev->devclass = NULL;
992 kfree(dev->nameunit, M_BUS);
993 dev->nameunit = NULL;
999 make_device(device_t parent, const char *name, int unit)
1004 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1007 dc = devclass_find_internal(name, NULL, TRUE);
1009 kprintf("make_device: can't find device class %s\n", name);
1015 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1019 dev->parent = parent;
1020 TAILQ_INIT(&dev->children);
1021 kobj_init((kobj_t) dev, &null_class);
1023 dev->devclass = NULL;
1025 dev->nameunit = NULL;
1029 dev->flags = DF_ENABLED;
1032 dev->flags |= DF_WILDCARD;
1034 dev->flags |= DF_FIXEDCLASS;
1035 if (devclass_add_device(dc, dev) != 0) {
1036 kobj_delete((kobj_t)dev, M_BUS);
1043 dev->state = DS_NOTPRESENT;
1045 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1046 bus_data_generation_update();
1052 device_print_child(device_t dev, device_t child)
1056 if (device_is_alive(child))
1057 retval += BUS_PRINT_CHILD(dev, child);
1059 retval += device_printf(child, " not found\n");
1065 device_add_child(device_t dev, const char *name, int unit)
1067 return device_add_child_ordered(dev, 0, name, unit);
1071 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1076 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1079 child = make_device(dev, name, unit);
1082 child->order = order;
1084 TAILQ_FOREACH(place, &dev->children, link)
1085 if (place->order > order)
1090 * The device 'place' is the first device whose order is
1091 * greater than the new child.
1093 TAILQ_INSERT_BEFORE(place, child, link);
1096 * The new child's order is greater or equal to the order of
1097 * any existing device. Add the child to the tail of the list.
1099 TAILQ_INSERT_TAIL(&dev->children, child, link);
1102 bus_data_generation_update();
1107 device_delete_child(device_t dev, device_t child)
1110 device_t grandchild;
1112 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1114 /* remove children first */
1115 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1116 error = device_delete_child(child, grandchild);
1121 if ((error = device_detach(child)) != 0)
1123 if (child->devclass)
1124 devclass_delete_device(child->devclass, child);
1125 TAILQ_REMOVE(&dev->children, child, link);
1126 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1127 device_set_desc(child, NULL);
1128 kobj_delete((kobj_t)child, M_BUS);
1130 bus_data_generation_update();
1135 * @brief Find a device given a unit number
1137 * This is similar to devclass_get_devices() but only searches for
1138 * devices which have @p dev as a parent.
1140 * @param dev the parent device to search
1141 * @param unit the unit number to search for. If the unit is -1,
1142 * return the first child of @p dev which has name
1143 * @p classname (that is, the one with the lowest unit.)
1145 * @returns the device with the given unit number or @c
1146 * NULL if there is no such device
1149 device_find_child(device_t dev, const char *classname, int unit)
1154 dc = devclass_find(classname);
1159 child = devclass_get_device(dc, unit);
1160 if (child && child->parent == dev)
1163 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1164 child = devclass_get_device(dc, unit);
1165 if (child && child->parent == dev)
1173 first_matching_driver(devclass_t dc, device_t dev)
1176 return(devclass_find_driver_internal(dc, dev->devclass->name));
1178 return(TAILQ_FIRST(&dc->drivers));
1182 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1184 if (dev->devclass) {
1186 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1187 if (!strcmp(dev->devclass->name, dl->driver->name))
1191 return(TAILQ_NEXT(last, link));
1195 device_probe_child(device_t dev, device_t child)
1198 driverlink_t best = 0;
1200 int result, pri = 0;
1201 int hasclass = (child->devclass != 0);
1205 panic("device_probe_child: parent device has no devclass");
1207 if (child->state == DS_ALIVE)
1210 for (; dc; dc = dc->parent) {
1211 for (dl = first_matching_driver(dc, child); dl;
1212 dl = next_matching_driver(dc, child, dl)) {
1213 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1214 device_set_driver(child, dl->driver);
1216 device_set_devclass(child, dl->driver->name);
1217 result = DEVICE_PROBE(child);
1219 device_set_devclass(child, 0);
1222 * If the driver returns SUCCESS, there can be
1223 * no higher match for this device.
1232 * The driver returned an error so it
1233 * certainly doesn't match.
1236 device_set_driver(child, 0);
1241 * A priority lower than SUCCESS, remember the
1242 * best matching driver. Initialise the value
1243 * of pri for the first match.
1245 if (best == 0 || result > pri) {
1252 * If we have unambiguous match in this devclass,
1253 * don't look in the parent.
1255 if (best && pri == 0)
1260 * If we found a driver, change state and initialise the devclass.
1263 if (!child->devclass)
1264 device_set_devclass(child, best->driver->name);
1265 device_set_driver(child, best->driver);
1268 * A bit bogus. Call the probe method again to make
1269 * sure that we have the right description.
1271 DEVICE_PROBE(child);
1274 bus_data_generation_update();
1275 child->state = DS_ALIVE;
1283 device_get_parent(device_t dev)
1289 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1296 TAILQ_FOREACH(child, &dev->children, link)
1299 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1304 TAILQ_FOREACH(child, &dev->children, link) {
1305 list[count] = child;
1316 device_get_driver(device_t dev)
1318 return(dev->driver);
1322 device_get_devclass(device_t dev)
1324 return(dev->devclass);
1328 device_get_name(device_t dev)
1331 return devclass_get_name(dev->devclass);
1336 device_get_nameunit(device_t dev)
1338 return(dev->nameunit);
1342 device_get_unit(device_t dev)
1348 device_get_desc(device_t dev)
1354 device_get_flags(device_t dev)
1356 return(dev->devflags);
1360 device_print_prettyname(device_t dev)
1362 const char *name = device_get_name(dev);
1365 return kprintf("unknown: ");
1367 return kprintf("%s%d: ", name, device_get_unit(dev));
1371 device_printf(device_t dev, const char * fmt, ...)
1376 retval = device_print_prettyname(dev);
1377 __va_start(ap, fmt);
1378 retval += kvprintf(fmt, ap);
1384 device_set_desc_internal(device_t dev, const char* desc, int copy)
1386 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1387 kfree(dev->desc, M_BUS);
1388 dev->flags &= ~DF_DESCMALLOCED;
1393 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1395 strcpy(dev->desc, desc);
1396 dev->flags |= DF_DESCMALLOCED;
1399 /* Avoid a -Wcast-qual warning */
1400 dev->desc = (char *)(uintptr_t) desc;
1403 bus_data_generation_update();
1407 device_set_desc(device_t dev, const char* desc)
1409 device_set_desc_internal(dev, desc, FALSE);
1413 device_set_desc_copy(device_t dev, const char* desc)
1415 device_set_desc_internal(dev, desc, TRUE);
1419 device_set_flags(device_t dev, uint32_t flags)
1421 dev->devflags = flags;
1425 device_get_softc(device_t dev)
1431 device_set_softc(device_t dev, void *softc)
1433 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1434 kfree(dev->softc, M_BUS);
1437 dev->flags |= DF_EXTERNALSOFTC;
1439 dev->flags &= ~DF_EXTERNALSOFTC;
1443 device_set_async_attach(device_t dev, int enable)
1446 dev->flags |= DF_ASYNCPROBE;
1448 dev->flags &= ~DF_ASYNCPROBE;
1452 device_get_ivars(device_t dev)
1458 device_set_ivars(device_t dev, void * ivars)
1467 device_get_state(device_t dev)
1473 device_enable(device_t dev)
1475 dev->flags |= DF_ENABLED;
1479 device_disable(device_t dev)
1481 dev->flags &= ~DF_ENABLED;
1488 device_busy(device_t dev)
1490 if (dev->state < DS_ATTACHED)
1491 panic("device_busy: called for unattached device");
1492 if (dev->busy == 0 && dev->parent)
1493 device_busy(dev->parent);
1495 dev->state = DS_BUSY;
1502 device_unbusy(device_t dev)
1504 if (dev->state != DS_BUSY)
1505 panic("device_unbusy: called for non-busy device");
1507 if (dev->busy == 0) {
1509 device_unbusy(dev->parent);
1510 dev->state = DS_ATTACHED;
1515 device_quiet(device_t dev)
1517 dev->flags |= DF_QUIET;
1521 device_verbose(device_t dev)
1523 dev->flags &= ~DF_QUIET;
1527 device_is_quiet(device_t dev)
1529 return((dev->flags & DF_QUIET) != 0);
1533 device_is_enabled(device_t dev)
1535 return((dev->flags & DF_ENABLED) != 0);
1539 device_is_alive(device_t dev)
1541 return(dev->state >= DS_ALIVE);
1545 device_is_attached(device_t dev)
1547 return(dev->state >= DS_ATTACHED);
1551 device_set_devclass(device_t dev, const char *classname)
1558 devclass_delete_device(dev->devclass, dev);
1562 if (dev->devclass) {
1563 kprintf("device_set_devclass: device class already set\n");
1567 dc = devclass_find_internal(classname, NULL, TRUE);
1571 error = devclass_add_device(dc, dev);
1573 bus_data_generation_update();
1578 device_set_driver(device_t dev, driver_t *driver)
1580 if (dev->state >= DS_ATTACHED)
1583 if (dev->driver == driver)
1586 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1587 kfree(dev->softc, M_BUS);
1590 kobj_delete((kobj_t) dev, 0);
1591 dev->driver = driver;
1593 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1594 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1595 dev->softc = kmalloc(driver->size, M_BUS,
1596 M_INTWAIT | M_ZERO);
1598 kobj_delete((kobj_t)dev, 0);
1599 kobj_init((kobj_t) dev, &null_class);
1605 kobj_init((kobj_t) dev, &null_class);
1608 bus_data_generation_update();
1613 device_probe_and_attach(device_t dev)
1615 device_t bus = dev->parent;
1618 if (dev->state >= DS_ALIVE)
1621 if ((dev->flags & DF_ENABLED) == 0) {
1623 device_print_prettyname(dev);
1624 kprintf("not probed (disabled)\n");
1629 error = device_probe_child(bus, dev);
1631 if (!(dev->flags & DF_DONENOMATCH)) {
1632 BUS_PROBE_NOMATCH(bus, dev);
1634 dev->flags |= DF_DONENOMATCH;
1640 * Output the exact device chain prior to the attach in case the
1641 * system locks up during attach, and generate the full info after
1642 * the attach so correct irq and other information is displayed.
1644 if (bootverbose && !device_is_quiet(dev)) {
1647 kprintf("%s", device_get_nameunit(dev));
1648 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1649 kprintf(".%s", device_get_nameunit(tmp));
1652 if (!device_is_quiet(dev))
1653 device_print_child(bus, dev);
1654 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1655 kprintf("%s: probing asynchronously\n",
1656 device_get_nameunit(dev));
1657 dev->state = DS_INPROGRESS;
1658 device_attach_async(dev);
1661 error = device_doattach(dev);
1667 * Device is known to be alive, do the attach asynchronously.
1668 * However, serialize the attaches with the mp lock.
1671 device_attach_async(device_t dev)
1675 atomic_add_int(&numasyncthreads, 1);
1676 lwkt_create(device_attach_thread, dev, &td, NULL,
1678 (dev->desc ? dev->desc : "devattach"));
1682 device_attach_thread(void *arg)
1686 get_mplock(); /* XXX replace with devattach_token later */
1687 (void)device_doattach(dev);
1688 atomic_subtract_int(&numasyncthreads, 1);
1689 wakeup(&numasyncthreads);
1690 rel_mplock(); /* XXX replace with devattach_token later */
1694 * Device is known to be alive, do the attach (synchronous or asynchronous)
1697 device_doattach(device_t dev)
1699 device_t bus = dev->parent;
1700 int hasclass = (dev->devclass != 0);
1703 error = DEVICE_ATTACH(dev);
1705 dev->state = DS_ATTACHED;
1706 if (bootverbose && !device_is_quiet(dev))
1707 device_print_child(bus, dev);
1710 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1711 dev->driver->name, dev->unit, error);
1712 /* Unset the class that was set in device_probe_child */
1714 device_set_devclass(dev, 0);
1715 device_set_driver(dev, NULL);
1716 dev->state = DS_NOTPRESENT;
1722 device_detach(device_t dev)
1726 PDEBUG(("%s", DEVICENAME(dev)));
1727 if (dev->state == DS_BUSY)
1729 if (dev->state != DS_ATTACHED)
1732 if ((error = DEVICE_DETACH(dev)) != 0)
1735 device_printf(dev, "detached\n");
1737 BUS_CHILD_DETACHED(dev->parent, dev);
1739 if (!(dev->flags & DF_FIXEDCLASS))
1740 devclass_delete_device(dev->devclass, dev);
1742 dev->state = DS_NOTPRESENT;
1743 device_set_driver(dev, NULL);
1749 device_shutdown(device_t dev)
1751 if (dev->state < DS_ATTACHED)
1753 PDEBUG(("%s", DEVICENAME(dev)));
1754 return DEVICE_SHUTDOWN(dev);
1758 device_set_unit(device_t dev, int unit)
1763 dc = device_get_devclass(dev);
1764 if (unit < dc->maxunit && dc->devices[unit])
1766 err = devclass_delete_device(dc, dev);
1770 err = devclass_add_device(dc, dev);
1774 bus_data_generation_update();
1778 /*======================================*/
1780 * Access functions for device resources.
1783 /* Supplied by config(8) in ioconf.c */
1784 extern struct config_device config_devtab[];
1785 extern int devtab_count;
1787 /* Runtime version */
1788 struct config_device *devtab = config_devtab;
1791 resource_new_name(const char *name, int unit)
1793 struct config_device *new;
1795 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1796 M_INTWAIT | M_ZERO);
1799 if (devtab && devtab_count > 0)
1800 bcopy(devtab, new, devtab_count * sizeof(*new));
1801 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1802 if (new[devtab_count].name == NULL) {
1806 strcpy(new[devtab_count].name, name);
1807 new[devtab_count].unit = unit;
1808 new[devtab_count].resource_count = 0;
1809 new[devtab_count].resources = NULL;
1810 if (devtab && devtab != config_devtab)
1811 kfree(devtab, M_TEMP);
1813 return devtab_count++;
1817 resource_new_resname(int j, const char *resname, resource_type type)
1819 struct config_resource *new;
1822 i = devtab[j].resource_count;
1823 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1826 if (devtab[j].resources && i > 0)
1827 bcopy(devtab[j].resources, new, i * sizeof(*new));
1828 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1829 if (new[i].name == NULL) {
1833 strcpy(new[i].name, resname);
1835 if (devtab[j].resources)
1836 kfree(devtab[j].resources, M_TEMP);
1837 devtab[j].resources = new;
1838 devtab[j].resource_count = i + 1;
1843 resource_match_string(int i, const char *resname, const char *value)
1846 struct config_resource *res;
1848 for (j = 0, res = devtab[i].resources;
1849 j < devtab[i].resource_count; j++, res++)
1850 if (!strcmp(res->name, resname)
1851 && res->type == RES_STRING
1852 && !strcmp(res->u.stringval, value))
1858 resource_find(const char *name, int unit, const char *resname,
1859 struct config_resource **result)
1862 struct config_resource *res;
1865 * First check specific instances, then generic.
1867 for (i = 0; i < devtab_count; i++) {
1868 if (devtab[i].unit < 0)
1870 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1871 res = devtab[i].resources;
1872 for (j = 0; j < devtab[i].resource_count; j++, res++)
1873 if (!strcmp(res->name, resname)) {
1879 for (i = 0; i < devtab_count; i++) {
1880 if (devtab[i].unit >= 0)
1882 /* XXX should this `&& devtab[i].unit == unit' be here? */
1883 /* XXX if so, then the generic match does nothing */
1884 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1885 res = devtab[i].resources;
1886 for (j = 0; j < devtab[i].resource_count; j++, res++)
1887 if (!strcmp(res->name, resname)) {
1897 resource_int_value(const char *name, int unit, const char *resname, int *result)
1900 struct config_resource *res;
1902 if ((error = resource_find(name, unit, resname, &res)) != 0)
1904 if (res->type != RES_INT)
1906 *result = res->u.intval;
1911 resource_long_value(const char *name, int unit, const char *resname,
1915 struct config_resource *res;
1917 if ((error = resource_find(name, unit, resname, &res)) != 0)
1919 if (res->type != RES_LONG)
1921 *result = res->u.longval;
1926 resource_string_value(const char *name, int unit, const char *resname,
1930 struct config_resource *res;
1932 if ((error = resource_find(name, unit, resname, &res)) != 0)
1934 if (res->type != RES_STRING)
1936 *result = res->u.stringval;
1941 resource_query_string(int i, const char *resname, const char *value)
1947 for (; i < devtab_count; i++)
1948 if (resource_match_string(i, resname, value) >= 0)
1954 resource_locate(int i, const char *resname)
1960 for (; i < devtab_count; i++)
1961 if (!strcmp(devtab[i].name, resname))
1967 resource_count(void)
1969 return(devtab_count);
1973 resource_query_name(int i)
1975 return(devtab[i].name);
1979 resource_query_unit(int i)
1981 return(devtab[i].unit);
1985 resource_create(const char *name, int unit, const char *resname,
1986 resource_type type, struct config_resource **result)
1989 struct config_resource *res = NULL;
1991 for (i = 0; i < devtab_count; i++)
1992 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1993 res = devtab[i].resources;
1997 i = resource_new_name(name, unit);
2000 res = devtab[i].resources;
2002 for (j = 0; j < devtab[i].resource_count; j++, res++)
2003 if (!strcmp(res->name, resname)) {
2007 j = resource_new_resname(i, resname, type);
2010 res = &devtab[i].resources[j];
2016 resource_set_int(const char *name, int unit, const char *resname, int value)
2019 struct config_resource *res;
2021 error = resource_create(name, unit, resname, RES_INT, &res);
2024 if (res->type != RES_INT)
2026 res->u.intval = value;
2031 resource_set_long(const char *name, int unit, const char *resname, long value)
2034 struct config_resource *res;
2036 error = resource_create(name, unit, resname, RES_LONG, &res);
2039 if (res->type != RES_LONG)
2041 res->u.longval = value;
2046 resource_set_string(const char *name, int unit, const char *resname,
2050 struct config_resource *res;
2052 error = resource_create(name, unit, resname, RES_STRING, &res);
2055 if (res->type != RES_STRING)
2057 if (res->u.stringval)
2058 kfree(res->u.stringval, M_TEMP);
2059 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2060 if (res->u.stringval == NULL)
2062 strcpy(res->u.stringval, value);
2067 resource_cfgload(void *dummy __unused)
2069 struct config_resource *res, *cfgres;
2072 char *name, *resname;
2076 int config_devtab_count;
2078 config_devtab_count = devtab_count;
2082 for (i = 0; i < config_devtab_count; i++) {
2083 name = config_devtab[i].name;
2084 unit = config_devtab[i].unit;
2086 for (j = 0; j < config_devtab[i].resource_count; j++) {
2087 cfgres = config_devtab[i].resources;
2088 resname = cfgres[j].name;
2089 type = cfgres[j].type;
2090 error = resource_create(name, unit, resname, type,
2093 kprintf("create resource %s%d: error %d\n",
2097 if (res->type != type) {
2098 kprintf("type mismatch %s%d: %d != %d\n",
2099 name, unit, res->type, type);
2104 res->u.intval = cfgres[j].u.intval;
2107 res->u.longval = cfgres[j].u.longval;
2110 if (res->u.stringval)
2111 kfree(res->u.stringval, M_TEMP);
2112 stringval = cfgres[j].u.stringval;
2113 res->u.stringval = kmalloc(strlen(stringval) + 1,
2115 if (res->u.stringval == NULL)
2117 strcpy(res->u.stringval, stringval);
2120 panic("unknown resource type %d", type);
2125 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2128 /*======================================*/
2130 * Some useful method implementations to make life easier for bus drivers.
2134 resource_list_init(struct resource_list *rl)
2140 resource_list_free(struct resource_list *rl)
2142 struct resource_list_entry *rle;
2144 while ((rle = SLIST_FIRST(rl)) != NULL) {
2146 panic("resource_list_free: resource entry is busy");
2147 SLIST_REMOVE_HEAD(rl, link);
2153 resource_list_add(struct resource_list *rl,
2155 u_long start, u_long end, u_long count)
2157 struct resource_list_entry *rle;
2159 rle = resource_list_find(rl, type, rid);
2161 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2164 panic("resource_list_add: can't record entry");
2165 SLIST_INSERT_HEAD(rl, rle, link);
2172 panic("resource_list_add: resource entry is busy");
2179 struct resource_list_entry*
2180 resource_list_find(struct resource_list *rl,
2183 struct resource_list_entry *rle;
2185 SLIST_FOREACH(rle, rl, link)
2186 if (rle->type == type && rle->rid == rid)
2192 resource_list_delete(struct resource_list *rl,
2195 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2198 if (rle->res != NULL)
2199 panic("resource_list_delete: resource has not been released");
2200 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2206 resource_list_alloc(struct resource_list *rl,
2207 device_t bus, device_t child,
2209 u_long start, u_long end,
2210 u_long count, u_int flags)
2212 struct resource_list_entry *rle = 0;
2213 int passthrough = (device_get_parent(child) != bus);
2214 int isdefault = (start == 0UL && end == ~0UL);
2217 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2219 start, end, count, flags));
2222 rle = resource_list_find(rl, type, *rid);
2225 return(0); /* no resource of that type/rid */
2228 panic("resource_list_alloc: resource entry is busy");
2232 count = max(count, rle->count);
2233 end = max(rle->end, start + count - 1);
2236 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2237 type, rid, start, end, count, flags);
2240 * Record the new range.
2243 rle->start = rman_get_start(rle->res);
2244 rle->end = rman_get_end(rle->res);
2252 resource_list_release(struct resource_list *rl,
2253 device_t bus, device_t child,
2254 int type, int rid, struct resource *res)
2256 struct resource_list_entry *rle = 0;
2257 int passthrough = (device_get_parent(child) != bus);
2261 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2265 rle = resource_list_find(rl, type, rid);
2268 panic("resource_list_release: can't find resource");
2270 panic("resource_list_release: resource entry is not busy");
2272 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2282 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2285 struct resource_list_entry *rle;
2286 int printed, retval;
2290 /* Yes, this is kinda cheating */
2291 SLIST_FOREACH(rle, rl, link) {
2292 if (rle->type == type) {
2294 retval += kprintf(" %s ", name);
2296 retval += kprintf(",");
2298 retval += kprintf(format, rle->start);
2299 if (rle->count > 1) {
2300 retval += kprintf("-");
2301 retval += kprintf(format, rle->start +
2310 * Generic driver/device identify functions. These will install a device
2311 * rendezvous point under the parent using the same name as the driver
2312 * name, which will at a later time be probed and attached.
2314 * These functions are used when the parent does not 'scan' its bus for
2315 * matching devices, or for the particular devices using these functions,
2316 * or when the device is a pseudo or synthesized device (such as can be
2317 * found under firewire and ppbus).
2320 bus_generic_identify(driver_t *driver, device_t parent)
2322 if (parent->state == DS_ATTACHED)
2324 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2329 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2331 if (parent->state == DS_ATTACHED)
2333 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2338 * Call DEVICE_IDENTIFY for each driver.
2341 bus_generic_probe(device_t dev)
2343 devclass_t dc = dev->devclass;
2346 TAILQ_FOREACH(dl, &dc->drivers, link) {
2347 DEVICE_IDENTIFY(dl->driver, dev);
2354 * This is an aweful hack due to the isa bus and autoconf code not
2355 * probing the ISA devices until after everything else has configured.
2356 * The ISA bus did a dummy attach long ago so we have to set it back
2357 * to an earlier state so the probe thinks its the initial probe and
2360 * XXX remove by properly defering the ISA bus scan.
2363 bus_generic_probe_hack(device_t dev)
2365 if (dev->state == DS_ATTACHED) {
2366 dev->state = DS_ALIVE;
2367 bus_generic_probe(dev);
2368 dev->state = DS_ATTACHED;
2374 bus_generic_attach(device_t dev)
2378 TAILQ_FOREACH(child, &dev->children, link) {
2379 device_probe_and_attach(child);
2386 bus_generic_detach(device_t dev)
2391 if (dev->state != DS_ATTACHED)
2394 TAILQ_FOREACH(child, &dev->children, link)
2395 if ((error = device_detach(child)) != 0)
2402 bus_generic_shutdown(device_t dev)
2406 TAILQ_FOREACH(child, &dev->children, link)
2407 device_shutdown(child);
2413 bus_generic_suspend(device_t dev)
2416 device_t child, child2;
2418 TAILQ_FOREACH(child, &dev->children, link) {
2419 error = DEVICE_SUSPEND(child);
2421 for (child2 = TAILQ_FIRST(&dev->children);
2422 child2 && child2 != child;
2423 child2 = TAILQ_NEXT(child2, link))
2424 DEVICE_RESUME(child2);
2432 bus_generic_resume(device_t dev)
2436 TAILQ_FOREACH(child, &dev->children, link)
2437 DEVICE_RESUME(child);
2438 /* if resume fails, there's nothing we can usefully do... */
2444 bus_print_child_header(device_t dev, device_t child)
2448 if (device_get_desc(child))
2449 retval += device_printf(child, "<%s>", device_get_desc(child));
2451 retval += kprintf("%s", device_get_nameunit(child));
2453 if (child->state != DS_ATTACHED)
2454 kprintf(" [tentative]");
2456 kprintf(" [attached!]");
2462 bus_print_child_footer(device_t dev, device_t child)
2464 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2468 bus_generic_add_child(device_t dev, device_t child, int order,
2469 const char *name, int unit)
2472 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2474 dev = device_add_child_ordered(child, order, name, unit);
2480 bus_generic_print_child(device_t dev, device_t child)
2484 retval += bus_print_child_header(dev, child);
2485 retval += bus_print_child_footer(dev, child);
2491 bus_generic_read_ivar(device_t dev, device_t child, int index,
2497 error = BUS_READ_IVAR(dev->parent, child, index, result);
2504 bus_generic_write_ivar(device_t dev, device_t child, int index,
2510 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2517 * Resource list are used for iterations, do not recurse.
2519 struct resource_list *
2520 bus_generic_get_resource_list(device_t dev, device_t child)
2526 bus_generic_driver_added(device_t dev, driver_t *driver)
2530 DEVICE_IDENTIFY(driver, dev);
2531 TAILQ_FOREACH(child, &dev->children, link) {
2532 if (child->state == DS_NOTPRESENT)
2533 device_probe_and_attach(child);
2538 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2539 int flags, driver_intr_t *intr, void *arg,
2540 void **cookiep, lwkt_serialize_t serializer)
2542 /* Propagate up the bus hierarchy until someone handles it. */
2544 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
2545 intr, arg, cookiep, serializer));
2551 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2554 /* Propagate up the bus hierarchy until someone handles it. */
2556 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2562 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2565 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2571 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2574 BUS_ENABLE_INTR(dev->parent, child, cookie);
2578 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2579 enum intr_polarity pol)
2581 /* Propagate up the bus hierarchy until someone handles it. */
2583 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2589 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2590 u_long start, u_long end, u_long count, u_int flags)
2592 /* Propagate up the bus hierarchy until someone handles it. */
2594 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2595 start, end, count, flags));
2601 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2604 /* Propagate up the bus hierarchy until someone handles it. */
2606 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2612 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2615 /* Propagate up the bus hierarchy until someone handles it. */
2617 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2623 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2624 int rid, struct resource *r)
2626 /* Propagate up the bus hierarchy until someone handles it. */
2628 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2635 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2636 u_long *startp, u_long *countp)
2642 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2649 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2650 u_long start, u_long count)
2656 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2663 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2666 BUS_DELETE_RESOURCE(dev, child, type, rid);
2670 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2671 u_long *startp, u_long *countp)
2673 struct resource_list *rl = NULL;
2674 struct resource_list_entry *rle = NULL;
2676 rl = BUS_GET_RESOURCE_LIST(dev, child);
2680 rle = resource_list_find(rl, type, rid);
2685 *startp = rle->start;
2687 *countp = rle->count;
2693 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2694 u_long start, u_long count)
2696 struct resource_list *rl = NULL;
2698 rl = BUS_GET_RESOURCE_LIST(dev, child);
2702 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2708 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2710 struct resource_list *rl = NULL;
2712 rl = BUS_GET_RESOURCE_LIST(dev, child);
2716 resource_list_delete(rl, type, rid);
2720 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2721 int rid, struct resource *r)
2723 struct resource_list *rl = NULL;
2725 rl = BUS_GET_RESOURCE_LIST(dev, child);
2729 return(resource_list_release(rl, dev, child, type, rid, r));
2733 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2734 int *rid, u_long start, u_long end, u_long count, u_int flags)
2736 struct resource_list *rl = NULL;
2738 rl = BUS_GET_RESOURCE_LIST(dev, child);
2742 return(resource_list_alloc(rl, dev, child, type, rid,
2743 start, end, count, flags));
2747 bus_generic_child_present(device_t bus, device_t child)
2749 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2754 * Some convenience functions to make it easier for drivers to use the
2755 * resource-management functions. All these really do is hide the
2756 * indirection through the parent's method table, making for slightly
2757 * less-wordy code. In the future, it might make sense for this code
2758 * to maintain some sort of a list of resources allocated by each device.
2761 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2762 struct resource **res)
2766 for (i = 0; rs[i].type != -1; i++)
2768 for (i = 0; rs[i].type != -1; i++) {
2769 res[i] = bus_alloc_resource_any(dev,
2770 rs[i].type, &rs[i].rid, rs[i].flags);
2771 if (res[i] == NULL) {
2772 bus_release_resources(dev, rs, res);
2780 bus_release_resources(device_t dev, const struct resource_spec *rs,
2781 struct resource **res)
2785 for (i = 0; rs[i].type != -1; i++)
2786 if (res[i] != NULL) {
2787 bus_release_resource(
2788 dev, rs[i].type, rs[i].rid, res[i]);
2794 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2795 u_long count, u_int flags)
2797 if (dev->parent == 0)
2799 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2804 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2806 if (dev->parent == 0)
2808 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2812 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2814 if (dev->parent == 0)
2816 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2820 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2822 if (dev->parent == 0)
2824 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2828 bus_setup_intr(device_t dev, struct resource *r, int flags,
2829 driver_intr_t handler, void *arg,
2830 void **cookiep, lwkt_serialize_t serializer)
2832 if (dev->parent == 0)
2834 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2835 cookiep, serializer));
2839 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2841 if (dev->parent == 0)
2843 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2847 bus_enable_intr(device_t dev, void *cookie)
2850 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2854 bus_disable_intr(device_t dev, void *cookie)
2857 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2863 bus_set_resource(device_t dev, int type, int rid,
2864 u_long start, u_long count)
2866 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2871 bus_get_resource(device_t dev, int type, int rid,
2872 u_long *startp, u_long *countp)
2874 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2879 bus_get_resource_start(device_t dev, int type, int rid)
2881 u_long start, count;
2884 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2892 bus_get_resource_count(device_t dev, int type, int rid)
2894 u_long start, count;
2897 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2905 bus_delete_resource(device_t dev, int type, int rid)
2907 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2911 bus_child_present(device_t child)
2913 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2917 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2921 parent = device_get_parent(child);
2922 if (parent == NULL) {
2926 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2930 bus_child_location_str(device_t child, char *buf, size_t buflen)
2934 parent = device_get_parent(child);
2935 if (parent == NULL) {
2939 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2943 root_print_child(device_t dev, device_t child)
2949 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2950 void **cookiep, lwkt_serialize_t serializer)
2953 * If an interrupt mapping gets to here something bad has happened.
2955 panic("root_setup_intr");
2959 * If we get here, assume that the device is permanant and really is
2960 * present in the system. Removable bus drivers are expected to intercept
2961 * this call long before it gets here. We return -1 so that drivers that
2962 * really care can check vs -1 or some ERRNO returned higher in the food
2966 root_child_present(device_t dev, device_t child)
2972 * XXX NOTE! other defaults may be set in bus_if.m
2974 static kobj_method_t root_methods[] = {
2975 /* Device interface */
2976 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2977 KOBJMETHOD(device_suspend, bus_generic_suspend),
2978 KOBJMETHOD(device_resume, bus_generic_resume),
2981 KOBJMETHOD(bus_add_child, bus_generic_add_child),
2982 KOBJMETHOD(bus_print_child, root_print_child),
2983 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2984 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2985 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2986 KOBJMETHOD(bus_child_present, root_child_present),
2991 static driver_t root_driver = {
2998 devclass_t root_devclass;
3001 root_bus_module_handler(module_t mod, int what, void* arg)
3005 TAILQ_INIT(&bus_data_devices);
3006 root_bus = make_device(NULL, "root", 0);
3007 root_bus->desc = "System root bus";
3008 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3009 root_bus->driver = &root_driver;
3010 root_bus->state = DS_ALIVE;
3011 root_devclass = devclass_find_internal("root", NULL, FALSE);
3016 device_shutdown(root_bus);
3023 static moduledata_t root_bus_mod = {
3025 root_bus_module_handler,
3028 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3031 root_bus_configure(void)
3039 * handle device_identify based device attachments to the root_bus
3040 * (typically nexus).
3042 bus_generic_probe(root_bus);
3045 * Probe and attach the devices under root_bus.
3047 TAILQ_FOREACH(dev, &root_bus->children, link) {
3048 device_probe_and_attach(dev);
3052 * Wait for all asynchronous attaches to complete. If we don't
3053 * our legacy ISA bus scan could steal device unit numbers or
3057 if (numasyncthreads)
3058 kprintf("Waiting for async drivers to attach\n");
3059 while (numasyncthreads > 0) {
3060 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3062 if (warncount == 0) {
3063 kprintf("Warning: Still waiting for %d "
3064 "drivers to attach\n", numasyncthreads);
3065 } else if (warncount == -30) {
3066 kprintf("Giving up on %d drivers\n", numasyncthreads);
3070 root_bus->state = DS_ATTACHED;
3074 driver_module_handler(module_t mod, int what, void *arg)
3077 struct driver_module_data *dmd;
3078 devclass_t bus_devclass;
3079 kobj_class_t driver;
3080 const char *parentname;
3082 dmd = (struct driver_module_data *)arg;
3083 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3088 if (dmd->dmd_chainevh)
3089 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3091 driver = dmd->dmd_driver;
3092 PDEBUG(("Loading module: driver %s on bus %s",
3093 DRIVERNAME(driver), dmd->dmd_busname));
3096 * If the driver has any base classes, make the
3097 * devclass inherit from the devclass of the driver's
3098 * first base class. This will allow the system to
3099 * search for drivers in both devclasses for children
3100 * of a device using this driver.
3102 if (driver->baseclasses)
3103 parentname = driver->baseclasses[0]->name;
3106 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3109 error = devclass_add_driver(bus_devclass, driver);
3115 PDEBUG(("Unloading module: driver %s from bus %s",
3116 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3117 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3119 if (!error && dmd->dmd_chainevh)
3120 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3130 * The _short versions avoid iteration by not calling anything that prints
3131 * more than oneliners. I love oneliners.
3135 print_device_short(device_t dev, int indent)
3140 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3141 dev->unit, dev->desc,
3142 (dev->parent? "":"no "),
3143 (TAILQ_EMPTY(&dev->children)? "no ":""),
3144 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3145 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3146 (dev->flags&DF_WILDCARD? "wildcard,":""),
3147 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3148 (dev->ivars? "":"no "),
3149 (dev->softc? "":"no "),
3154 print_device(device_t dev, int indent)
3159 print_device_short(dev, indent);
3161 indentprintf(("Parent:\n"));
3162 print_device_short(dev->parent, indent+1);
3163 indentprintf(("Driver:\n"));
3164 print_driver_short(dev->driver, indent+1);
3165 indentprintf(("Devclass:\n"));
3166 print_devclass_short(dev->devclass, indent+1);
3170 * Print the device and all its children (indented).
3173 print_device_tree_short(device_t dev, int indent)
3180 print_device_short(dev, indent);
3182 TAILQ_FOREACH(child, &dev->children, link)
3183 print_device_tree_short(child, indent+1);
3187 * Print the device and all its children (indented).
3190 print_device_tree(device_t dev, int indent)
3197 print_device(dev, indent);
3199 TAILQ_FOREACH(child, &dev->children, link)
3200 print_device_tree(child, indent+1);
3204 print_driver_short(driver_t *driver, int indent)
3209 indentprintf(("driver %s: softc size = %zu\n",
3210 driver->name, driver->size));
3214 print_driver(driver_t *driver, int indent)
3219 print_driver_short(driver, indent);
3224 print_driver_list(driver_list_t drivers, int indent)
3226 driverlink_t driver;
3228 TAILQ_FOREACH(driver, &drivers, link)
3229 print_driver(driver->driver, indent);
3233 print_devclass_short(devclass_t dc, int indent)
3238 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3242 print_devclass(devclass_t dc, int indent)
3249 print_devclass_short(dc, indent);
3250 indentprintf(("Drivers:\n"));
3251 print_driver_list(dc->drivers, indent+1);
3253 indentprintf(("Devices:\n"));
3254 for (i = 0; i < dc->maxunit; i++)
3256 print_device(dc->devices[i], indent+1);
3260 print_devclass_list_short(void)
3264 kprintf("Short listing of devclasses, drivers & devices:\n");
3265 TAILQ_FOREACH(dc, &devclasses, link) {
3266 print_devclass_short(dc, 0);
3271 print_devclass_list(void)
3275 kprintf("Full listing of devclasses, drivers & devices:\n");
3276 TAILQ_FOREACH(dc, &devclasses, link) {
3277 print_devclass(dc, 0);
3284 * Check to see if a device is disabled via a disabled hint.
3287 resource_disabled(const char *name, int unit)
3291 error = resource_int_value(name, unit, "disabled", &value);
3298 * User-space access to the device tree.
3300 * We implement a small set of nodes:
3302 * hw.bus Single integer read method to obtain the
3303 * current generation count.
3304 * hw.bus.devices Reads the entire device tree in flat space.
3305 * hw.bus.rman Resource manager interface
3307 * We might like to add the ability to scan devclasses and/or drivers to
3308 * determine what else is currently loaded/available.
3312 sysctl_bus(SYSCTL_HANDLER_ARGS)
3314 struct u_businfo ubus;
3316 ubus.ub_version = BUS_USER_VERSION;
3317 ubus.ub_generation = bus_data_generation;
3319 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3321 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3322 "bus-related data");
3325 sysctl_devices(SYSCTL_HANDLER_ARGS)
3327 int *name = (int *)arg1;
3328 u_int namelen = arg2;
3331 struct u_device udev; /* XXX this is a bit big */
3337 if (bus_data_generation_check(name[0]))
3343 * Scan the list of devices, looking for the requested index.
3345 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3353 * Populate the return array.
3355 bzero(&udev, sizeof(udev));
3356 udev.dv_handle = (uintptr_t)dev;
3357 udev.dv_parent = (uintptr_t)dev->parent;
3358 if (dev->nameunit != NULL)
3359 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3360 if (dev->desc != NULL)
3361 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3362 if (dev->driver != NULL && dev->driver->name != NULL)
3363 strlcpy(udev.dv_drivername, dev->driver->name,
3364 sizeof(udev.dv_drivername));
3365 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3366 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3367 udev.dv_devflags = dev->devflags;
3368 udev.dv_flags = dev->flags;
3369 udev.dv_state = dev->state;
3370 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3374 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3375 "system device tree");
3378 bus_data_generation_check(int generation)
3380 if (generation != bus_data_generation)
3383 /* XXX generate optimised lists here? */
3388 bus_data_generation_update(void)
3390 bus_data_generation++;