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 $
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
42 #include <sys/device.h>
46 #include <sys/filio.h>
47 #include <sys/event.h>
48 #include <sys/signalvar.h>
49 #include <sys/machintr.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 static d_open_t devopen;
139 static d_close_t devclose;
140 static d_read_t devread;
141 static d_ioctl_t devioctl;
142 static d_kqfilter_t devkqfilter;
144 static struct dev_ops devctl_ops = {
150 .d_kqfilter = devkqfilter
153 struct dev_event_info
156 TAILQ_ENTRY(dev_event_info) dei_link;
159 TAILQ_HEAD(devq, dev_event_info);
161 static struct dev_softc
168 struct proc *async_proc;
174 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
175 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
176 TAILQ_INIT(&devsoftc.devq);
180 devopen(struct dev_open_args *ap)
186 devsoftc.nonblock = 0;
187 devsoftc.async_proc = NULL;
192 devclose(struct dev_close_args *ap)
195 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
197 lockmgr(&devsoftc.lock, LK_RELEASE);
203 * The read channel for this device is used to report changes to
204 * userland in realtime. We are required to free the data as well as
205 * the n1 object because we allocate them separately. Also note that
206 * we return one record at a time. If you try to read this device a
207 * character at a time, you will lose the rest of the data. Listening
208 * programs are expected to cope.
211 devread(struct dev_read_args *ap)
213 struct uio *uio = ap->a_uio;
214 struct dev_event_info *n1;
217 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
218 while (TAILQ_EMPTY(&devsoftc.devq)) {
219 if (devsoftc.nonblock) {
220 lockmgr(&devsoftc.lock, LK_RELEASE);
223 tsleep_interlock(&devsoftc, PCATCH);
224 lockmgr(&devsoftc.lock, LK_RELEASE);
225 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
226 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
229 * Need to translate ERESTART to EINTR here? -- jake
231 lockmgr(&devsoftc.lock, LK_RELEASE);
235 n1 = TAILQ_FIRST(&devsoftc.devq);
236 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
237 lockmgr(&devsoftc.lock, LK_RELEASE);
238 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
239 kfree(n1->dei_data, M_BUS);
245 devioctl(struct dev_ioctl_args *ap)
250 if (*(int*)ap->a_data)
251 devsoftc.nonblock = 1;
253 devsoftc.nonblock = 0;
256 if (*(int*)ap->a_data)
257 devsoftc.async_proc = curproc;
259 devsoftc.async_proc = NULL;
262 /* (un)Support for other fcntl() calls. */
274 static void dev_filter_detach(struct knote *);
275 static int dev_filter_read(struct knote *, long);
277 static struct filterops dev_filtops =
278 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
281 devkqfilter(struct dev_kqfilter_args *ap)
283 struct knote *kn = ap->a_kn;
287 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
289 switch (kn->kn_filter) {
291 kn->kn_fop = &dev_filtops;
294 ap->a_result = EOPNOTSUPP;
295 lockmgr(&devsoftc.lock, LK_RELEASE);
299 klist = &devsoftc.kq.ki_note;
300 knote_insert(klist, kn);
302 lockmgr(&devsoftc.lock, LK_RELEASE);
308 dev_filter_detach(struct knote *kn)
312 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
313 klist = &devsoftc.kq.ki_note;
314 knote_remove(klist, kn);
315 lockmgr(&devsoftc.lock, LK_RELEASE);
319 dev_filter_read(struct knote *kn, long hint)
323 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
324 if (!TAILQ_EMPTY(&devsoftc.devq))
326 lockmgr(&devsoftc.lock, LK_RELEASE);
333 * @brief Return whether the userland process is running
336 devctl_process_running(void)
338 return (devsoftc.inuse == 1);
342 * @brief Queue data to be read from the devctl device
344 * Generic interface to queue data to the devctl device. It is
345 * assumed that @p data is properly formatted. It is further assumed
346 * that @p data is allocated using the M_BUS malloc type.
349 devctl_queue_data(char *data)
351 struct dev_event_info *n1 = NULL;
354 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
358 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
359 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
361 lockmgr(&devsoftc.lock, LK_RELEASE);
362 get_mplock(); /* XXX */
363 KNOTE(&devsoftc.kq.ki_note, 0);
364 rel_mplock(); /* XXX */
365 p = devsoftc.async_proc;
371 * @brief Send a 'notification' to userland, using standard ways
374 devctl_notify(const char *system, const char *subsystem, const char *type,
381 return; /* BOGUS! Must specify system. */
382 if (subsystem == NULL)
383 return; /* BOGUS! Must specify subsystem. */
385 return; /* BOGUS! Must specify type. */
386 len += strlen(" system=") + strlen(system);
387 len += strlen(" subsystem=") + strlen(subsystem);
388 len += strlen(" type=") + strlen(type);
389 /* add in the data message plus newline. */
392 len += 3; /* '!', '\n', and NUL */
393 msg = kmalloc(len, M_BUS, M_NOWAIT);
395 return; /* Drop it on the floor */
397 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
398 system, subsystem, type, data);
400 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
401 system, subsystem, type);
402 devctl_queue_data(msg);
406 * Common routine that tries to make sending messages as easy as possible.
407 * We allocate memory for the data, copy strings into that, but do not
408 * free it unless there's an error. The dequeue part of the driver should
409 * free the data. We don't send data when the device is disabled. We do
410 * send data, even when we have no listeners, because we wish to avoid
411 * races relating to startup and restart of listening applications.
413 * devaddq is designed to string together the type of event, with the
414 * object of that event, plus the plug and play info and location info
415 * for that event. This is likely most useful for devices, but less
416 * useful for other consumers of this interface. Those should use
417 * the devctl_queue_data() interface instead.
420 devaddq(const char *type, const char *what, device_t dev)
429 data = kmalloc(1024, M_BUS, M_NOWAIT);
433 /* get the bus specific location of this device */
434 loc = kmalloc(1024, M_BUS, M_NOWAIT);
438 bus_child_location_str(dev, loc, 1024);
440 /* Get the bus specific pnp info of this device */
441 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
445 bus_child_pnpinfo_str(dev, pnp, 1024);
447 /* Get the parent of this device, or / if high enough in the tree. */
448 if (device_get_parent(dev) == NULL)
449 parstr = "."; /* Or '/' ? */
451 parstr = device_get_nameunit(device_get_parent(dev));
452 /* String it all together. */
453 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
457 devctl_queue_data(data);
467 * A device was added to the tree. We are called just after it successfully
468 * attaches (that is, probe and attach success for this device). No call
469 * is made if a device is merely parented into the tree. See devnomatch
470 * if probe fails. If attach fails, no notification is sent (but maybe
471 * we should have a different message for this).
474 devadded(device_t dev)
479 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
482 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
486 bus_child_pnpinfo_str(dev, pnp, 1024);
487 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
488 devaddq("+", tmp, dev);
498 * A device was removed from the tree. We are called just before this
502 devremoved(device_t dev)
507 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
510 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
514 bus_child_pnpinfo_str(dev, pnp, 1024);
515 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
516 devaddq("-", tmp, dev);
526 * Called when there's no match for this device. This is only called
527 * the first time that no match happens, so we don't keep getitng this
528 * message. Should that prove to be undesirable, we can change it.
529 * This is called when all drivers that can attach to a given bus
530 * decline to accept this device. Other errrors may not be detected.
533 devnomatch(device_t dev)
535 devaddq("?", "", dev);
539 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
541 struct dev_event_info *n1;
544 dis = devctl_disable;
545 error = sysctl_handle_int(oidp, &dis, 0, req);
546 if (error || !req->newptr)
548 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
549 devctl_disable = dis;
551 while (!TAILQ_EMPTY(&devsoftc.devq)) {
552 n1 = TAILQ_FIRST(&devsoftc.devq);
553 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
554 kfree(n1->dei_data, M_BUS);
558 lockmgr(&devsoftc.lock, LK_RELEASE);
562 /* End of /dev/devctl code */
564 TAILQ_HEAD(,device) bus_data_devices;
565 static int bus_data_generation = 1;
567 kobj_method_t null_methods[] = {
571 DEFINE_CLASS(null, null_methods, 0);
574 * Devclass implementation
577 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
580 devclass_find_internal(const char *classname, const char *parentname,
585 PDEBUG(("looking for %s", classname));
586 if (classname == NULL)
589 TAILQ_FOREACH(dc, &devclasses, link)
590 if (!strcmp(dc->name, classname))
594 PDEBUG(("creating %s", classname));
595 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
596 M_BUS, M_INTWAIT | M_ZERO);
598 dc->name = (char*) (dc + 1);
599 strcpy(dc->name, classname);
602 TAILQ_INIT(&dc->drivers);
603 TAILQ_INSERT_TAIL(&devclasses, dc, link);
605 bus_data_generation_update();
610 * If a parent class is specified, then set that as our parent so
611 * that this devclass will support drivers for the parent class as
612 * well. If the parent class has the same name don't do this though
613 * as it creates a cycle that can trigger an infinite loop in
614 * device_probe_child() if a device exists for which there is no
617 if (parentname && dc && !dc->parent &&
618 strcmp(classname, parentname) != 0)
619 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
625 devclass_create(const char *classname)
627 return(devclass_find_internal(classname, NULL, TRUE));
631 devclass_find(const char *classname)
633 return(devclass_find_internal(classname, NULL, FALSE));
637 devclass_find_unit(const char *classname, int unit)
641 if ((dc = devclass_find(classname)) != NULL)
642 return(devclass_get_device(dc, unit));
647 devclass_add_driver(devclass_t dc, driver_t *driver)
653 PDEBUG(("%s", DRIVERNAME(driver)));
655 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
658 * Compile the driver's methods. Also increase the reference count
659 * so that the class doesn't get freed when the last instance
660 * goes. This means we can safely use static methods and avoids a
661 * double-free in devclass_delete_driver.
663 kobj_class_instantiate(driver);
666 * Make sure the devclass which the driver is implementing exists.
668 devclass_find_internal(driver->name, NULL, TRUE);
671 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
674 * Call BUS_DRIVER_ADDED for any existing busses in this class,
675 * but only if the bus has already been attached (otherwise we
676 * might probe too early).
678 * This is what will cause a newly loaded module to be associated
679 * with hardware. bus_generic_driver_added() is typically what ends
682 for (i = 0; i < dc->maxunit; i++) {
683 if ((dev = dc->devices[i]) != NULL) {
684 if (dev->state >= DS_ATTACHED)
685 BUS_DRIVER_ADDED(dev, driver);
689 bus_data_generation_update();
694 devclass_delete_driver(devclass_t busclass, driver_t *driver)
696 devclass_t dc = devclass_find(driver->name);
702 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
708 * Find the link structure in the bus' list of drivers.
710 TAILQ_FOREACH(dl, &busclass->drivers, link)
711 if (dl->driver == driver)
715 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
720 * Disassociate from any devices. We iterate through all the
721 * devices in the devclass of the driver and detach any which are
722 * using the driver and which have a parent in the devclass which
723 * we are deleting from.
725 * Note that since a driver can be in multiple devclasses, we
726 * should not detach devices which are not children of devices in
727 * the affected devclass.
729 for (i = 0; i < dc->maxunit; i++)
730 if (dc->devices[i]) {
731 dev = dc->devices[i];
732 if (dev->driver == driver && dev->parent &&
733 dev->parent->devclass == busclass) {
734 if ((error = device_detach(dev)) != 0)
736 device_set_driver(dev, NULL);
740 TAILQ_REMOVE(&busclass->drivers, dl, link);
743 kobj_class_uninstantiate(driver);
745 bus_data_generation_update();
750 devclass_find_driver_internal(devclass_t dc, const char *classname)
754 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
756 TAILQ_FOREACH(dl, &dc->drivers, link)
757 if (!strcmp(dl->driver->name, classname))
760 PDEBUG(("not found"));
765 devclass_find_driver(devclass_t dc, const char *classname)
769 dl = devclass_find_driver_internal(dc, classname);
777 devclass_get_name(devclass_t dc)
783 devclass_get_device(devclass_t dc, int unit)
785 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
787 return(dc->devices[unit]);
791 devclass_get_softc(devclass_t dc, int unit)
795 dev = devclass_get_device(dc, unit);
799 return(device_get_softc(dev));
803 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
810 for (i = 0; i < dc->maxunit; i++)
814 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
817 for (i = 0; i < dc->maxunit; i++)
818 if (dc->devices[i]) {
819 list[count] = dc->devices[i];
830 * @brief Get a list of drivers in the devclass
832 * An array containing a list of pointers to all the drivers in the
833 * given devclass is allocated and returned in @p *listp. The number
834 * of drivers in the array is returned in @p *countp. The caller should
835 * free the array using @c free(p, M_TEMP).
837 * @param dc the devclass to examine
838 * @param listp gives location for array pointer return value
839 * @param countp gives location for number of array elements
843 * @retval ENOMEM the array allocation failed
846 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
853 TAILQ_FOREACH(dl, &dc->drivers, link)
855 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
860 TAILQ_FOREACH(dl, &dc->drivers, link) {
861 list[count] = dl->driver;
871 * @brief Get the number of devices in a devclass
873 * @param dc the devclass to examine
876 devclass_get_count(devclass_t dc)
881 for (i = 0; i < dc->maxunit; i++)
888 devclass_get_maxunit(devclass_t dc)
894 devclass_set_parent(devclass_t dc, devclass_t pdc)
900 devclass_get_parent(devclass_t dc)
906 devclass_alloc_unit(devclass_t dc, int *unitp)
910 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
912 /* If we have been given a wired unit number, check for existing device */
914 if (unit >= 0 && unit < dc->maxunit &&
915 dc->devices[unit] != NULL) {
917 kprintf("%s-: %s%d exists, using next available unit number\n",
918 dc->name, dc->name, unit);
919 /* find the next available slot */
920 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
924 /* Unwired device, find the next available slot for it */
926 while (unit < dc->maxunit && dc->devices[unit] != NULL)
931 * We've selected a unit beyond the length of the table, so let's
932 * extend the table to make room for all units up to and including
935 if (unit >= dc->maxunit) {
939 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
940 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
944 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
946 kfree(dc->devices, M_BUS);
947 dc->devices = newlist;
948 dc->maxunit = newsize;
950 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
957 devclass_add_device(devclass_t dc, device_t dev)
961 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
963 buflen = strlen(dc->name) + 5;
964 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
968 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
969 kfree(dev->nameunit, M_BUS);
970 dev->nameunit = NULL;
973 dc->devices[dev->unit] = dev;
975 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
981 devclass_delete_device(devclass_t dc, device_t dev)
986 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
988 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
989 panic("devclass_delete_device: inconsistent device class");
990 dc->devices[dev->unit] = NULL;
991 if (dev->flags & DF_WILDCARD)
993 dev->devclass = NULL;
994 kfree(dev->nameunit, M_BUS);
995 dev->nameunit = NULL;
1001 make_device(device_t parent, const char *name, int unit)
1006 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1009 dc = devclass_find_internal(name, NULL, TRUE);
1011 kprintf("make_device: can't find device class %s\n", name);
1017 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1021 dev->parent = parent;
1022 TAILQ_INIT(&dev->children);
1023 kobj_init((kobj_t) dev, &null_class);
1025 dev->devclass = NULL;
1027 dev->nameunit = NULL;
1031 dev->flags = DF_ENABLED;
1034 dev->flags |= DF_WILDCARD;
1036 dev->flags |= DF_FIXEDCLASS;
1037 if (devclass_add_device(dc, dev) != 0) {
1038 kobj_delete((kobj_t)dev, M_BUS);
1045 dev->state = DS_NOTPRESENT;
1047 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1048 bus_data_generation_update();
1054 device_print_child(device_t dev, device_t child)
1058 if (device_is_alive(child))
1059 retval += BUS_PRINT_CHILD(dev, child);
1061 retval += device_printf(child, " not found\n");
1067 device_add_child(device_t dev, const char *name, int unit)
1069 return device_add_child_ordered(dev, 0, name, unit);
1073 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1078 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1081 child = make_device(dev, name, unit);
1084 child->order = order;
1086 TAILQ_FOREACH(place, &dev->children, link)
1087 if (place->order > order)
1092 * The device 'place' is the first device whose order is
1093 * greater than the new child.
1095 TAILQ_INSERT_BEFORE(place, child, link);
1098 * The new child's order is greater or equal to the order of
1099 * any existing device. Add the child to the tail of the list.
1101 TAILQ_INSERT_TAIL(&dev->children, child, link);
1104 bus_data_generation_update();
1109 device_delete_child(device_t dev, device_t child)
1112 device_t grandchild;
1114 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1116 /* remove children first */
1117 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1118 error = device_delete_child(child, grandchild);
1123 if ((error = device_detach(child)) != 0)
1125 if (child->devclass)
1126 devclass_delete_device(child->devclass, child);
1127 TAILQ_REMOVE(&dev->children, child, link);
1128 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1129 device_set_desc(child, NULL);
1130 kobj_delete((kobj_t)child, M_BUS);
1132 bus_data_generation_update();
1137 * @brief Delete all children devices of the given device, if any.
1139 * This function deletes all children devices of the given device, if
1140 * any, using the device_delete_child() function for each device it
1141 * finds. If a child device cannot be deleted, this function will
1142 * return an error code.
1144 * @param dev the parent device
1147 * @retval non-zero a device would not detach
1150 device_delete_children(device_t dev)
1155 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1159 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1160 error = device_delete_child(dev, child);
1162 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1170 * @brief Find a device given a unit number
1172 * This is similar to devclass_get_devices() but only searches for
1173 * devices which have @p dev as a parent.
1175 * @param dev the parent device to search
1176 * @param unit the unit number to search for. If the unit is -1,
1177 * return the first child of @p dev which has name
1178 * @p classname (that is, the one with the lowest unit.)
1180 * @returns the device with the given unit number or @c
1181 * NULL if there is no such device
1184 device_find_child(device_t dev, const char *classname, int unit)
1189 dc = devclass_find(classname);
1194 child = devclass_get_device(dc, unit);
1195 if (child && child->parent == dev)
1198 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1199 child = devclass_get_device(dc, unit);
1200 if (child && child->parent == dev)
1208 first_matching_driver(devclass_t dc, device_t dev)
1211 return(devclass_find_driver_internal(dc, dev->devclass->name));
1213 return(TAILQ_FIRST(&dc->drivers));
1217 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1219 if (dev->devclass) {
1221 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1222 if (!strcmp(dev->devclass->name, dl->driver->name))
1226 return(TAILQ_NEXT(last, link));
1230 device_probe_child(device_t dev, device_t child)
1233 driverlink_t best = NULL;
1235 int result, pri = 0;
1236 int hasclass = (child->devclass != NULL);
1240 panic("device_probe_child: parent device has no devclass");
1242 if (child->state == DS_ALIVE)
1245 for (; dc; dc = dc->parent) {
1246 for (dl = first_matching_driver(dc, child); dl;
1247 dl = next_matching_driver(dc, child, dl)) {
1248 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1249 device_set_driver(child, dl->driver);
1251 device_set_devclass(child, dl->driver->name);
1252 result = DEVICE_PROBE(child);
1254 device_set_devclass(child, 0);
1257 * If the driver returns SUCCESS, there can be
1258 * no higher match for this device.
1267 * The driver returned an error so it
1268 * certainly doesn't match.
1271 device_set_driver(child, 0);
1276 * A priority lower than SUCCESS, remember the
1277 * best matching driver. Initialise the value
1278 * of pri for the first match.
1280 if (best == NULL || result > pri) {
1287 * If we have unambiguous match in this devclass,
1288 * don't look in the parent.
1290 if (best && pri == 0)
1295 * If we found a driver, change state and initialise the devclass.
1298 if (!child->devclass)
1299 device_set_devclass(child, best->driver->name);
1300 device_set_driver(child, best->driver);
1303 * A bit bogus. Call the probe method again to make
1304 * sure that we have the right description.
1306 DEVICE_PROBE(child);
1309 bus_data_generation_update();
1310 child->state = DS_ALIVE;
1318 device_get_parent(device_t dev)
1324 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1331 TAILQ_FOREACH(child, &dev->children, link)
1334 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1337 TAILQ_FOREACH(child, &dev->children, link) {
1338 list[count] = child;
1349 device_get_driver(device_t dev)
1351 return(dev->driver);
1355 device_get_devclass(device_t dev)
1357 return(dev->devclass);
1361 device_get_name(device_t dev)
1364 return devclass_get_name(dev->devclass);
1369 device_get_nameunit(device_t dev)
1371 return(dev->nameunit);
1375 device_get_unit(device_t dev)
1381 device_get_desc(device_t dev)
1387 device_get_flags(device_t dev)
1389 return(dev->devflags);
1393 device_print_prettyname(device_t dev)
1395 const char *name = device_get_name(dev);
1398 return kprintf("unknown: ");
1400 return kprintf("%s%d: ", name, device_get_unit(dev));
1404 device_printf(device_t dev, const char * fmt, ...)
1409 retval = device_print_prettyname(dev);
1410 __va_start(ap, fmt);
1411 retval += kvprintf(fmt, ap);
1417 device_set_desc_internal(device_t dev, const char* desc, int copy)
1419 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1420 kfree(dev->desc, M_BUS);
1421 dev->flags &= ~DF_DESCMALLOCED;
1426 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1428 strcpy(dev->desc, desc);
1429 dev->flags |= DF_DESCMALLOCED;
1432 /* Avoid a -Wcast-qual warning */
1433 dev->desc = (char *)(uintptr_t) desc;
1436 bus_data_generation_update();
1440 device_set_desc(device_t dev, const char* desc)
1442 device_set_desc_internal(dev, desc, FALSE);
1446 device_set_desc_copy(device_t dev, const char* desc)
1448 device_set_desc_internal(dev, desc, TRUE);
1452 device_set_flags(device_t dev, uint32_t flags)
1454 dev->devflags = flags;
1458 device_get_softc(device_t dev)
1464 device_set_softc(device_t dev, void *softc)
1466 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1467 kfree(dev->softc, M_BUS);
1470 dev->flags |= DF_EXTERNALSOFTC;
1472 dev->flags &= ~DF_EXTERNALSOFTC;
1476 device_set_async_attach(device_t dev, int enable)
1479 dev->flags |= DF_ASYNCPROBE;
1481 dev->flags &= ~DF_ASYNCPROBE;
1485 device_get_ivars(device_t dev)
1491 device_set_ivars(device_t dev, void * ivars)
1500 device_get_state(device_t dev)
1506 device_enable(device_t dev)
1508 dev->flags |= DF_ENABLED;
1512 device_disable(device_t dev)
1514 dev->flags &= ~DF_ENABLED;
1521 device_busy(device_t dev)
1523 if (dev->state < DS_ATTACHED)
1524 panic("device_busy: called for unattached device");
1525 if (dev->busy == 0 && dev->parent)
1526 device_busy(dev->parent);
1528 dev->state = DS_BUSY;
1535 device_unbusy(device_t dev)
1537 if (dev->state != DS_BUSY)
1538 panic("device_unbusy: called for non-busy device");
1540 if (dev->busy == 0) {
1542 device_unbusy(dev->parent);
1543 dev->state = DS_ATTACHED;
1548 device_quiet(device_t dev)
1550 dev->flags |= DF_QUIET;
1554 device_verbose(device_t dev)
1556 dev->flags &= ~DF_QUIET;
1560 device_is_quiet(device_t dev)
1562 return((dev->flags & DF_QUIET) != 0);
1566 device_is_enabled(device_t dev)
1568 return((dev->flags & DF_ENABLED) != 0);
1572 device_is_alive(device_t dev)
1574 return(dev->state >= DS_ALIVE);
1578 device_is_attached(device_t dev)
1580 return(dev->state >= DS_ATTACHED);
1584 device_set_devclass(device_t dev, const char *classname)
1591 devclass_delete_device(dev->devclass, dev);
1595 if (dev->devclass) {
1596 kprintf("device_set_devclass: device class already set\n");
1600 dc = devclass_find_internal(classname, NULL, TRUE);
1604 error = devclass_add_device(dc, dev);
1606 bus_data_generation_update();
1611 device_set_driver(device_t dev, driver_t *driver)
1613 if (dev->state >= DS_ATTACHED)
1616 if (dev->driver == driver)
1619 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1620 kfree(dev->softc, M_BUS);
1623 kobj_delete((kobj_t) dev, 0);
1624 dev->driver = driver;
1626 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1627 if (!(dev->flags & DF_EXTERNALSOFTC))
1628 dev->softc = kmalloc(driver->size, M_BUS,
1629 M_INTWAIT | M_ZERO);
1631 kobj_init((kobj_t) dev, &null_class);
1634 bus_data_generation_update();
1639 device_probe_and_attach(device_t dev)
1641 device_t bus = dev->parent;
1644 if (dev->state >= DS_ALIVE)
1647 if ((dev->flags & DF_ENABLED) == 0) {
1649 device_print_prettyname(dev);
1650 kprintf("not probed (disabled)\n");
1655 error = device_probe_child(bus, dev);
1657 if (!(dev->flags & DF_DONENOMATCH)) {
1658 BUS_PROBE_NOMATCH(bus, dev);
1660 dev->flags |= DF_DONENOMATCH;
1666 * Output the exact device chain prior to the attach in case the
1667 * system locks up during attach, and generate the full info after
1668 * the attach so correct irq and other information is displayed.
1670 if (bootverbose && !device_is_quiet(dev)) {
1673 kprintf("%s", device_get_nameunit(dev));
1674 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1675 kprintf(".%s", device_get_nameunit(tmp));
1678 if (!device_is_quiet(dev))
1679 device_print_child(bus, dev);
1680 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1681 kprintf("%s: probing asynchronously\n",
1682 device_get_nameunit(dev));
1683 dev->state = DS_INPROGRESS;
1684 device_attach_async(dev);
1687 error = device_doattach(dev);
1693 * Device is known to be alive, do the attach asynchronously.
1694 * However, serialize the attaches with the mp lock.
1697 device_attach_async(device_t dev)
1701 atomic_add_int(&numasyncthreads, 1);
1702 lwkt_create(device_attach_thread, dev, &td, NULL,
1703 0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
1707 device_attach_thread(void *arg)
1711 get_mplock(); /* XXX replace with devattach_token later */
1712 (void)device_doattach(dev);
1713 atomic_subtract_int(&numasyncthreads, 1);
1714 wakeup(&numasyncthreads);
1715 rel_mplock(); /* XXX replace with devattach_token later */
1719 * Device is known to be alive, do the attach (synchronous or asynchronous)
1722 device_doattach(device_t dev)
1724 device_t bus = dev->parent;
1725 int hasclass = (dev->devclass != NULL);
1728 error = DEVICE_ATTACH(dev);
1730 dev->state = DS_ATTACHED;
1731 if (bootverbose && !device_is_quiet(dev))
1732 device_print_child(bus, dev);
1735 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1736 dev->driver->name, dev->unit, error);
1737 /* Unset the class that was set in device_probe_child */
1739 device_set_devclass(dev, 0);
1740 device_set_driver(dev, NULL);
1741 dev->state = DS_NOTPRESENT;
1747 device_detach(device_t dev)
1751 PDEBUG(("%s", DEVICENAME(dev)));
1752 if (dev->state == DS_BUSY)
1754 if (dev->state != DS_ATTACHED)
1757 if ((error = DEVICE_DETACH(dev)) != 0)
1760 device_printf(dev, "detached\n");
1762 BUS_CHILD_DETACHED(dev->parent, dev);
1764 if (!(dev->flags & DF_FIXEDCLASS))
1765 devclass_delete_device(dev->devclass, dev);
1767 dev->state = DS_NOTPRESENT;
1768 device_set_driver(dev, NULL);
1774 device_shutdown(device_t dev)
1776 if (dev->state < DS_ATTACHED)
1778 PDEBUG(("%s", DEVICENAME(dev)));
1779 return DEVICE_SHUTDOWN(dev);
1783 device_set_unit(device_t dev, int unit)
1788 dc = device_get_devclass(dev);
1789 if (unit < dc->maxunit && dc->devices[unit])
1791 err = devclass_delete_device(dc, dev);
1795 err = devclass_add_device(dc, dev);
1799 bus_data_generation_update();
1803 /*======================================*/
1805 * Access functions for device resources.
1808 /* Supplied by config(8) in ioconf.c */
1809 extern struct config_device config_devtab[];
1810 extern int devtab_count;
1812 /* Runtime version */
1813 struct config_device *devtab = config_devtab;
1816 resource_new_name(const char *name, int unit)
1818 struct config_device *new;
1820 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1821 M_INTWAIT | M_ZERO);
1822 if (devtab && devtab_count > 0)
1823 bcopy(devtab, new, devtab_count * sizeof(*new));
1824 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1825 if (new[devtab_count].name == NULL) {
1829 strcpy(new[devtab_count].name, name);
1830 new[devtab_count].unit = unit;
1831 new[devtab_count].resource_count = 0;
1832 new[devtab_count].resources = NULL;
1833 if (devtab && devtab != config_devtab)
1834 kfree(devtab, M_TEMP);
1836 return devtab_count++;
1840 resource_new_resname(int j, const char *resname, resource_type type)
1842 struct config_resource *new;
1845 i = devtab[j].resource_count;
1846 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1847 if (devtab[j].resources && i > 0)
1848 bcopy(devtab[j].resources, new, i * sizeof(*new));
1849 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1850 if (new[i].name == NULL) {
1854 strcpy(new[i].name, resname);
1856 if (devtab[j].resources)
1857 kfree(devtab[j].resources, M_TEMP);
1858 devtab[j].resources = new;
1859 devtab[j].resource_count = i + 1;
1864 resource_match_string(int i, const char *resname, const char *value)
1867 struct config_resource *res;
1869 for (j = 0, res = devtab[i].resources;
1870 j < devtab[i].resource_count; j++, res++)
1871 if (!strcmp(res->name, resname)
1872 && res->type == RES_STRING
1873 && !strcmp(res->u.stringval, value))
1879 resource_find(const char *name, int unit, const char *resname,
1880 struct config_resource **result)
1883 struct config_resource *res;
1886 * First check specific instances, then generic.
1888 for (i = 0; i < devtab_count; i++) {
1889 if (devtab[i].unit < 0)
1891 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1892 res = devtab[i].resources;
1893 for (j = 0; j < devtab[i].resource_count; j++, res++)
1894 if (!strcmp(res->name, resname)) {
1900 for (i = 0; i < devtab_count; i++) {
1901 if (devtab[i].unit >= 0)
1903 /* XXX should this `&& devtab[i].unit == unit' be here? */
1904 /* XXX if so, then the generic match does nothing */
1905 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1906 res = devtab[i].resources;
1907 for (j = 0; j < devtab[i].resource_count; j++, res++)
1908 if (!strcmp(res->name, resname)) {
1918 resource_kenv(const char *name, int unit, const char *resname, long *result)
1923 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
1924 if ((env = kgetenv(buf)) != NULL) {
1925 *result = strtol(env, NULL, 0);
1932 resource_int_value(const char *name, int unit, const char *resname, int *result)
1934 struct config_resource *res;
1938 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
1939 *result = (int)kvalue;
1942 if ((error = resource_find(name, unit, resname, &res)) != 0)
1944 if (res->type != RES_INT)
1946 *result = res->u.intval;
1951 resource_long_value(const char *name, int unit, const char *resname,
1954 struct config_resource *res;
1958 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
1962 if ((error = resource_find(name, unit, resname, &res)) != 0)
1964 if (res->type != RES_LONG)
1966 *result = res->u.longval;
1971 resource_string_value(const char *name, int unit, const char *resname,
1975 struct config_resource *res;
1977 if ((error = resource_find(name, unit, resname, &res)) != 0)
1979 if (res->type != RES_STRING)
1981 *result = res->u.stringval;
1986 resource_query_string(int i, const char *resname, const char *value)
1992 for (; i < devtab_count; i++)
1993 if (resource_match_string(i, resname, value) >= 0)
1999 resource_locate(int i, const char *resname)
2005 for (; i < devtab_count; i++)
2006 if (!strcmp(devtab[i].name, resname))
2012 resource_count(void)
2014 return(devtab_count);
2018 resource_query_name(int i)
2020 return(devtab[i].name);
2024 resource_query_unit(int i)
2026 return(devtab[i].unit);
2030 resource_create(const char *name, int unit, const char *resname,
2031 resource_type type, struct config_resource **result)
2034 struct config_resource *res = NULL;
2036 for (i = 0; i < devtab_count; i++)
2037 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2038 res = devtab[i].resources;
2042 i = resource_new_name(name, unit);
2045 res = devtab[i].resources;
2047 for (j = 0; j < devtab[i].resource_count; j++, res++)
2048 if (!strcmp(res->name, resname)) {
2052 j = resource_new_resname(i, resname, type);
2055 res = &devtab[i].resources[j];
2061 resource_set_int(const char *name, int unit, const char *resname, int value)
2064 struct config_resource *res;
2066 error = resource_create(name, unit, resname, RES_INT, &res);
2069 if (res->type != RES_INT)
2071 res->u.intval = value;
2076 resource_set_long(const char *name, int unit, const char *resname, long value)
2079 struct config_resource *res;
2081 error = resource_create(name, unit, resname, RES_LONG, &res);
2084 if (res->type != RES_LONG)
2086 res->u.longval = value;
2091 resource_set_string(const char *name, int unit, const char *resname,
2095 struct config_resource *res;
2097 error = resource_create(name, unit, resname, RES_STRING, &res);
2100 if (res->type != RES_STRING)
2102 if (res->u.stringval)
2103 kfree(res->u.stringval, M_TEMP);
2104 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2105 if (res->u.stringval == NULL)
2107 strcpy(res->u.stringval, value);
2112 resource_cfgload(void *dummy __unused)
2114 struct config_resource *res, *cfgres;
2117 char *name, *resname;
2121 int config_devtab_count;
2123 config_devtab_count = devtab_count;
2127 for (i = 0; i < config_devtab_count; i++) {
2128 name = config_devtab[i].name;
2129 unit = config_devtab[i].unit;
2131 for (j = 0; j < config_devtab[i].resource_count; j++) {
2132 cfgres = config_devtab[i].resources;
2133 resname = cfgres[j].name;
2134 type = cfgres[j].type;
2135 error = resource_create(name, unit, resname, type,
2138 kprintf("create resource %s%d: error %d\n",
2142 if (res->type != type) {
2143 kprintf("type mismatch %s%d: %d != %d\n",
2144 name, unit, res->type, type);
2149 res->u.intval = cfgres[j].u.intval;
2152 res->u.longval = cfgres[j].u.longval;
2155 if (res->u.stringval)
2156 kfree(res->u.stringval, M_TEMP);
2157 stringval = cfgres[j].u.stringval;
2158 res->u.stringval = kmalloc(strlen(stringval) + 1,
2160 if (res->u.stringval == NULL)
2162 strcpy(res->u.stringval, stringval);
2165 panic("unknown resource type %d", type);
2170 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2173 /*======================================*/
2175 * Some useful method implementations to make life easier for bus drivers.
2179 resource_list_init(struct resource_list *rl)
2185 resource_list_free(struct resource_list *rl)
2187 struct resource_list_entry *rle;
2189 while ((rle = SLIST_FIRST(rl)) != NULL) {
2191 panic("resource_list_free: resource entry is busy");
2192 SLIST_REMOVE_HEAD(rl, link);
2198 resource_list_add(struct resource_list *rl, int type, int rid,
2199 u_long start, u_long end, u_long count, int cpuid)
2201 struct resource_list_entry *rle;
2203 rle = resource_list_find(rl, type, rid);
2205 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2207 SLIST_INSERT_HEAD(rl, rle, link);
2215 panic("resource_list_add: resource entry is busy");
2222 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2223 panic("resource_list_add: moving from cpu%d -> cpu%d",
2230 struct resource_list_entry*
2231 resource_list_find(struct resource_list *rl,
2234 struct resource_list_entry *rle;
2236 SLIST_FOREACH(rle, rl, link)
2237 if (rle->type == type && rle->rid == rid)
2243 resource_list_delete(struct resource_list *rl,
2246 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2249 if (rle->res != NULL)
2250 panic("resource_list_delete: resource has not been released");
2251 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2257 resource_list_alloc(struct resource_list *rl,
2258 device_t bus, device_t child,
2260 u_long start, u_long end,
2261 u_long count, u_int flags, int cpuid)
2263 struct resource_list_entry *rle = NULL;
2264 int passthrough = (device_get_parent(child) != bus);
2265 int isdefault = (start == 0UL && end == ~0UL);
2268 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2270 start, end, count, flags, cpuid));
2273 rle = resource_list_find(rl, type, *rid);
2276 return(0); /* no resource of that type/rid */
2279 panic("resource_list_alloc: resource entry is busy");
2283 count = max(count, rle->count);
2284 end = max(rle->end, start + count - 1);
2288 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2289 type, rid, start, end, count,
2293 * Record the new range.
2296 rle->start = rman_get_start(rle->res);
2297 rle->end = rman_get_end(rle->res);
2305 resource_list_release(struct resource_list *rl,
2306 device_t bus, device_t child,
2307 int type, int rid, struct resource *res)
2309 struct resource_list_entry *rle = NULL;
2310 int passthrough = (device_get_parent(child) != bus);
2314 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2318 rle = resource_list_find(rl, type, rid);
2321 panic("resource_list_release: can't find resource");
2323 panic("resource_list_release: resource entry is not busy");
2325 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2335 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2338 struct resource_list_entry *rle;
2339 int printed, retval;
2343 /* Yes, this is kinda cheating */
2344 SLIST_FOREACH(rle, rl, link) {
2345 if (rle->type == type) {
2347 retval += kprintf(" %s ", name);
2349 retval += kprintf(",");
2351 retval += kprintf(format, rle->start);
2352 if (rle->count > 1) {
2353 retval += kprintf("-");
2354 retval += kprintf(format, rle->start +
2363 * Generic driver/device identify functions. These will install a device
2364 * rendezvous point under the parent using the same name as the driver
2365 * name, which will at a later time be probed and attached.
2367 * These functions are used when the parent does not 'scan' its bus for
2368 * matching devices, or for the particular devices using these functions,
2369 * or when the device is a pseudo or synthesized device (such as can be
2370 * found under firewire and ppbus).
2373 bus_generic_identify(driver_t *driver, device_t parent)
2375 if (parent->state == DS_ATTACHED)
2377 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2382 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2384 if (parent->state == DS_ATTACHED)
2386 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2391 * Call DEVICE_IDENTIFY for each driver.
2394 bus_generic_probe(device_t dev)
2396 devclass_t dc = dev->devclass;
2399 TAILQ_FOREACH(dl, &dc->drivers, link) {
2400 DEVICE_IDENTIFY(dl->driver, dev);
2407 * This is an aweful hack due to the isa bus and autoconf code not
2408 * probing the ISA devices until after everything else has configured.
2409 * The ISA bus did a dummy attach long ago so we have to set it back
2410 * to an earlier state so the probe thinks its the initial probe and
2413 * XXX remove by properly defering the ISA bus scan.
2416 bus_generic_probe_hack(device_t dev)
2418 if (dev->state == DS_ATTACHED) {
2419 dev->state = DS_ALIVE;
2420 bus_generic_probe(dev);
2421 dev->state = DS_ATTACHED;
2427 bus_generic_attach(device_t dev)
2431 TAILQ_FOREACH(child, &dev->children, link) {
2432 device_probe_and_attach(child);
2439 bus_generic_detach(device_t dev)
2444 if (dev->state != DS_ATTACHED)
2447 TAILQ_FOREACH(child, &dev->children, link)
2448 if ((error = device_detach(child)) != 0)
2455 bus_generic_shutdown(device_t dev)
2459 TAILQ_FOREACH(child, &dev->children, link)
2460 device_shutdown(child);
2466 bus_generic_suspend(device_t dev)
2469 device_t child, child2;
2471 TAILQ_FOREACH(child, &dev->children, link) {
2472 error = DEVICE_SUSPEND(child);
2474 for (child2 = TAILQ_FIRST(&dev->children);
2475 child2 && child2 != child;
2476 child2 = TAILQ_NEXT(child2, link))
2477 DEVICE_RESUME(child2);
2485 bus_generic_resume(device_t dev)
2489 TAILQ_FOREACH(child, &dev->children, link)
2490 DEVICE_RESUME(child);
2491 /* if resume fails, there's nothing we can usefully do... */
2497 bus_print_child_header(device_t dev, device_t child)
2501 if (device_get_desc(child))
2502 retval += device_printf(child, "<%s>", device_get_desc(child));
2504 retval += kprintf("%s", device_get_nameunit(child));
2506 if (child->state != DS_ATTACHED)
2507 kprintf(" [tentative]");
2509 kprintf(" [attached!]");
2515 bus_print_child_footer(device_t dev, device_t child)
2517 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2521 bus_generic_add_child(device_t dev, device_t child, int order,
2522 const char *name, int unit)
2525 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2527 dev = device_add_child_ordered(child, order, name, unit);
2533 bus_generic_print_child(device_t dev, device_t child)
2537 retval += bus_print_child_header(dev, child);
2538 retval += bus_print_child_footer(dev, child);
2544 bus_generic_read_ivar(device_t dev, device_t child, int index,
2550 error = BUS_READ_IVAR(dev->parent, child, index, result);
2557 bus_generic_write_ivar(device_t dev, device_t child, int index,
2563 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2570 * Resource list are used for iterations, do not recurse.
2572 struct resource_list *
2573 bus_generic_get_resource_list(device_t dev, device_t child)
2579 bus_generic_driver_added(device_t dev, driver_t *driver)
2583 DEVICE_IDENTIFY(driver, dev);
2584 TAILQ_FOREACH(child, &dev->children, link) {
2585 if (child->state == DS_NOTPRESENT)
2586 device_probe_and_attach(child);
2591 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2592 int flags, driver_intr_t *intr, void *arg, void **cookiep,
2593 lwkt_serialize_t serializer, const char *desc)
2595 /* Propagate up the bus hierarchy until someone handles it. */
2597 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2598 intr, arg, cookiep, serializer, desc);
2605 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2608 /* Propagate up the bus hierarchy until someone handles it. */
2610 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2616 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2619 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2625 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2628 BUS_ENABLE_INTR(dev->parent, child, cookie);
2632 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2633 enum intr_polarity pol)
2635 /* Propagate up the bus hierarchy until someone handles it. */
2637 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2643 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2644 u_long start, u_long end, u_long count, u_int flags, int cpuid)
2646 /* Propagate up the bus hierarchy until someone handles it. */
2648 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2649 start, end, count, flags, cpuid));
2655 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2658 /* Propagate up the bus hierarchy until someone handles it. */
2660 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2666 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2669 /* Propagate up the bus hierarchy until someone handles it. */
2671 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2677 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2678 int rid, struct resource *r)
2680 /* Propagate up the bus hierarchy until someone handles it. */
2682 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2689 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2690 u_long *startp, u_long *countp)
2696 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2703 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2704 u_long start, u_long count, int cpuid)
2710 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2711 start, count, cpuid);
2717 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2720 BUS_DELETE_RESOURCE(dev, child, type, rid);
2724 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2725 u_long *startp, u_long *countp)
2727 struct resource_list *rl = NULL;
2728 struct resource_list_entry *rle = NULL;
2730 rl = BUS_GET_RESOURCE_LIST(dev, child);
2734 rle = resource_list_find(rl, type, rid);
2739 *startp = rle->start;
2741 *countp = rle->count;
2747 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2748 u_long start, u_long count, int cpuid)
2750 struct resource_list *rl = NULL;
2752 rl = BUS_GET_RESOURCE_LIST(dev, child);
2756 resource_list_add(rl, type, rid, start, (start + count - 1), count,
2763 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2765 struct resource_list *rl = NULL;
2767 rl = BUS_GET_RESOURCE_LIST(dev, child);
2771 resource_list_delete(rl, type, rid);
2775 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2776 int rid, struct resource *r)
2778 struct resource_list *rl = NULL;
2780 rl = BUS_GET_RESOURCE_LIST(dev, child);
2784 return(resource_list_release(rl, dev, child, type, rid, r));
2788 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2789 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
2791 struct resource_list *rl = NULL;
2793 rl = BUS_GET_RESOURCE_LIST(dev, child);
2797 return(resource_list_alloc(rl, dev, child, type, rid,
2798 start, end, count, flags, cpuid));
2802 bus_generic_child_present(device_t bus, device_t child)
2804 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2809 * Some convenience functions to make it easier for drivers to use the
2810 * resource-management functions. All these really do is hide the
2811 * indirection through the parent's method table, making for slightly
2812 * less-wordy code. In the future, it might make sense for this code
2813 * to maintain some sort of a list of resources allocated by each device.
2816 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2817 struct resource **res)
2821 for (i = 0; rs[i].type != -1; i++)
2823 for (i = 0; rs[i].type != -1; i++) {
2824 res[i] = bus_alloc_resource_any(dev,
2825 rs[i].type, &rs[i].rid, rs[i].flags);
2826 if (res[i] == NULL) {
2827 bus_release_resources(dev, rs, res);
2835 bus_release_resources(device_t dev, const struct resource_spec *rs,
2836 struct resource **res)
2840 for (i = 0; rs[i].type != -1; i++)
2841 if (res[i] != NULL) {
2842 bus_release_resource(
2843 dev, rs[i].type, rs[i].rid, res[i]);
2849 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2850 u_long count, u_int flags)
2852 if (dev->parent == NULL)
2854 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2859 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
2861 if (dev->parent == NULL)
2863 return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
2864 irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
2868 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2870 if (dev->parent == NULL)
2872 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2876 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2878 if (dev->parent == NULL)
2880 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2884 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2886 if (dev->parent == NULL)
2888 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2892 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
2893 driver_intr_t handler, void *arg, void **cookiep,
2894 lwkt_serialize_t serializer, const char *desc)
2896 if (dev->parent == NULL)
2898 return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2899 cookiep, serializer, desc);
2903 bus_setup_intr(device_t dev, struct resource *r, int flags,
2904 driver_intr_t handler, void *arg, void **cookiep,
2905 lwkt_serialize_t serializer)
2907 return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
2912 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2914 if (dev->parent == NULL)
2916 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2920 bus_enable_intr(device_t dev, void *cookie)
2923 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2927 bus_disable_intr(device_t dev, void *cookie)
2930 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2936 bus_set_resource(device_t dev, int type, int rid,
2937 u_long start, u_long count, int cpuid)
2939 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2940 start, count, cpuid));
2944 bus_get_resource(device_t dev, int type, int rid,
2945 u_long *startp, u_long *countp)
2947 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2952 bus_get_resource_start(device_t dev, int type, int rid)
2954 u_long start, count;
2957 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2965 bus_get_resource_count(device_t dev, int type, int rid)
2967 u_long start, count;
2970 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2978 bus_delete_resource(device_t dev, int type, int rid)
2980 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2984 bus_child_present(device_t child)
2986 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2990 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2994 parent = device_get_parent(child);
2995 if (parent == NULL) {
2999 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3003 bus_child_location_str(device_t child, char *buf, size_t buflen)
3007 parent = device_get_parent(child);
3008 if (parent == NULL) {
3012 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3016 root_print_child(device_t dev, device_t child)
3022 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3023 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3026 * If an interrupt mapping gets to here something bad has happened.
3028 panic("root_setup_intr");
3032 * If we get here, assume that the device is permanant and really is
3033 * present in the system. Removable bus drivers are expected to intercept
3034 * this call long before it gets here. We return -1 so that drivers that
3035 * really care can check vs -1 or some ERRNO returned higher in the food
3039 root_child_present(device_t dev, device_t child)
3045 * XXX NOTE! other defaults may be set in bus_if.m
3047 static kobj_method_t root_methods[] = {
3048 /* Device interface */
3049 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3050 KOBJMETHOD(device_suspend, bus_generic_suspend),
3051 KOBJMETHOD(device_resume, bus_generic_resume),
3054 KOBJMETHOD(bus_add_child, bus_generic_add_child),
3055 KOBJMETHOD(bus_print_child, root_print_child),
3056 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3057 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3058 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3059 KOBJMETHOD(bus_child_present, root_child_present),
3064 static driver_t root_driver = {
3071 devclass_t root_devclass;
3074 root_bus_module_handler(module_t mod, int what, void* arg)
3078 TAILQ_INIT(&bus_data_devices);
3079 root_bus = make_device(NULL, "root", 0);
3080 root_bus->desc = "System root bus";
3081 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3082 root_bus->driver = &root_driver;
3083 root_bus->state = DS_ALIVE;
3084 root_devclass = devclass_find_internal("root", NULL, FALSE);
3089 device_shutdown(root_bus);
3096 static moduledata_t root_bus_mod = {
3098 root_bus_module_handler,
3101 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3104 root_bus_configure(void)
3112 * handle device_identify based device attachments to the root_bus
3113 * (typically nexus).
3115 bus_generic_probe(root_bus);
3118 * Probe and attach the devices under root_bus.
3120 TAILQ_FOREACH(dev, &root_bus->children, link) {
3121 device_probe_and_attach(dev);
3125 * Wait for all asynchronous attaches to complete. If we don't
3126 * our legacy ISA bus scan could steal device unit numbers or
3130 if (numasyncthreads)
3131 kprintf("Waiting for async drivers to attach\n");
3132 while (numasyncthreads > 0) {
3133 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3135 if (warncount == 0) {
3136 kprintf("Warning: Still waiting for %d "
3137 "drivers to attach\n", numasyncthreads);
3138 } else if (warncount == -30) {
3139 kprintf("Giving up on %d drivers\n", numasyncthreads);
3143 root_bus->state = DS_ATTACHED;
3147 driver_module_handler(module_t mod, int what, void *arg)
3150 struct driver_module_data *dmd;
3151 devclass_t bus_devclass;
3152 kobj_class_t driver;
3153 const char *parentname;
3155 dmd = (struct driver_module_data *)arg;
3156 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3161 if (dmd->dmd_chainevh)
3162 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3164 driver = dmd->dmd_driver;
3165 PDEBUG(("Loading module: driver %s on bus %s",
3166 DRIVERNAME(driver), dmd->dmd_busname));
3169 * If the driver has any base classes, make the
3170 * devclass inherit from the devclass of the driver's
3171 * first base class. This will allow the system to
3172 * search for drivers in both devclasses for children
3173 * of a device using this driver.
3175 if (driver->baseclasses)
3176 parentname = driver->baseclasses[0]->name;
3179 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3182 error = devclass_add_driver(bus_devclass, driver);
3188 PDEBUG(("Unloading module: driver %s from bus %s",
3189 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3190 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3192 if (!error && dmd->dmd_chainevh)
3193 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3203 * The _short versions avoid iteration by not calling anything that prints
3204 * more than oneliners. I love oneliners.
3208 print_device_short(device_t dev, int indent)
3213 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3214 dev->unit, dev->desc,
3215 (dev->parent? "":"no "),
3216 (TAILQ_EMPTY(&dev->children)? "no ":""),
3217 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3218 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3219 (dev->flags&DF_WILDCARD? "wildcard,":""),
3220 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3221 (dev->ivars? "":"no "),
3222 (dev->softc? "":"no "),
3227 print_device(device_t dev, int indent)
3232 print_device_short(dev, indent);
3234 indentprintf(("Parent:\n"));
3235 print_device_short(dev->parent, indent+1);
3236 indentprintf(("Driver:\n"));
3237 print_driver_short(dev->driver, indent+1);
3238 indentprintf(("Devclass:\n"));
3239 print_devclass_short(dev->devclass, indent+1);
3243 * Print the device and all its children (indented).
3246 print_device_tree_short(device_t dev, int indent)
3253 print_device_short(dev, indent);
3255 TAILQ_FOREACH(child, &dev->children, link)
3256 print_device_tree_short(child, indent+1);
3260 * Print the device and all its children (indented).
3263 print_device_tree(device_t dev, int indent)
3270 print_device(dev, indent);
3272 TAILQ_FOREACH(child, &dev->children, link)
3273 print_device_tree(child, indent+1);
3277 print_driver_short(driver_t *driver, int indent)
3282 indentprintf(("driver %s: softc size = %zu\n",
3283 driver->name, driver->size));
3287 print_driver(driver_t *driver, int indent)
3292 print_driver_short(driver, indent);
3297 print_driver_list(driver_list_t drivers, int indent)
3299 driverlink_t driver;
3301 TAILQ_FOREACH(driver, &drivers, link)
3302 print_driver(driver->driver, indent);
3306 print_devclass_short(devclass_t dc, int indent)
3311 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3315 print_devclass(devclass_t dc, int indent)
3322 print_devclass_short(dc, indent);
3323 indentprintf(("Drivers:\n"));
3324 print_driver_list(dc->drivers, indent+1);
3326 indentprintf(("Devices:\n"));
3327 for (i = 0; i < dc->maxunit; i++)
3329 print_device(dc->devices[i], indent+1);
3333 print_devclass_list_short(void)
3337 kprintf("Short listing of devclasses, drivers & devices:\n");
3338 TAILQ_FOREACH(dc, &devclasses, link) {
3339 print_devclass_short(dc, 0);
3344 print_devclass_list(void)
3348 kprintf("Full listing of devclasses, drivers & devices:\n");
3349 TAILQ_FOREACH(dc, &devclasses, link) {
3350 print_devclass(dc, 0);
3357 * Check to see if a device is disabled via a disabled hint.
3360 resource_disabled(const char *name, int unit)
3364 error = resource_int_value(name, unit, "disabled", &value);
3371 * User-space access to the device tree.
3373 * We implement a small set of nodes:
3375 * hw.bus Single integer read method to obtain the
3376 * current generation count.
3377 * hw.bus.devices Reads the entire device tree in flat space.
3378 * hw.bus.rman Resource manager interface
3380 * We might like to add the ability to scan devclasses and/or drivers to
3381 * determine what else is currently loaded/available.
3385 sysctl_bus(SYSCTL_HANDLER_ARGS)
3387 struct u_businfo ubus;
3389 ubus.ub_version = BUS_USER_VERSION;
3390 ubus.ub_generation = bus_data_generation;
3392 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3394 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3395 "bus-related data");
3398 sysctl_devices(SYSCTL_HANDLER_ARGS)
3400 int *name = (int *)arg1;
3401 u_int namelen = arg2;
3404 struct u_device udev; /* XXX this is a bit big */
3410 if (bus_data_generation_check(name[0]))
3416 * Scan the list of devices, looking for the requested index.
3418 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3426 * Populate the return array.
3428 bzero(&udev, sizeof(udev));
3429 udev.dv_handle = (uintptr_t)dev;
3430 udev.dv_parent = (uintptr_t)dev->parent;
3431 if (dev->nameunit != NULL)
3432 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3433 if (dev->desc != NULL)
3434 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3435 if (dev->driver != NULL && dev->driver->name != NULL)
3436 strlcpy(udev.dv_drivername, dev->driver->name,
3437 sizeof(udev.dv_drivername));
3438 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3439 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3440 udev.dv_devflags = dev->devflags;
3441 udev.dv_flags = dev->flags;
3442 udev.dv_state = dev->state;
3443 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3447 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3448 "system device tree");
3451 bus_data_generation_check(int generation)
3453 if (generation != bus_data_generation)
3456 /* XXX generate optimised lists here? */
3461 bus_data_generation_update(void)
3463 bus_data_generation++;
3467 intr_str_polarity(enum intr_polarity pola)
3470 case INTR_POLARITY_LOW:
3473 case INTR_POLARITY_HIGH:
3476 case INTR_POLARITY_CONFORM:
3483 intr_str_trigger(enum intr_trigger trig)
3486 case INTR_TRIGGER_EDGE:
3489 case INTR_TRIGGER_LEVEL:
3492 case INTR_TRIGGER_CONFORM:
3499 device_getenv_int(device_t dev, const char *knob, int def)
3503 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3504 kgetenv_int(env, &def);