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);
57 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
59 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
62 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
63 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
64 #define DRIVERNAME(d) ((d)? d->name : "no driver")
65 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
67 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
68 * prevent syslog from deleting initial spaces
70 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
72 static void print_device_short(device_t dev, int indent);
73 static void print_device(device_t dev, int indent);
74 void print_device_tree_short(device_t dev, int indent);
75 void print_device_tree(device_t dev, int indent);
76 static void print_driver_short(driver_t *driver, int indent);
77 static void print_driver(driver_t *driver, int indent);
78 static void print_driver_list(driver_list_t drivers, int indent);
79 static void print_devclass_short(devclass_t dc, int indent);
80 static void print_devclass(devclass_t dc, int indent);
81 void print_devclass_list_short(void);
82 void print_devclass_list(void);
85 /* Make the compiler ignore the function calls */
86 #define PDEBUG(a) /* nop */
87 #define DEVICENAME(d) /* nop */
88 #define DRIVERNAME(d) /* nop */
89 #define DEVCLANAME(d) /* nop */
91 #define print_device_short(d,i) /* nop */
92 #define print_device(d,i) /* nop */
93 #define print_device_tree_short(d,i) /* nop */
94 #define print_device_tree(d,i) /* nop */
95 #define print_driver_short(d,i) /* nop */
96 #define print_driver(d,i) /* nop */
97 #define print_driver_list(d,i) /* nop */
98 #define print_devclass_short(d,i) /* nop */
99 #define print_devclass(d,i) /* nop */
100 #define print_devclass_list_short() /* nop */
101 #define print_devclass_list() /* nop */
109 DEVCLASS_SYSCTL_PARENT,
113 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
115 devclass_t dc = (devclass_t)arg1;
119 case DEVCLASS_SYSCTL_PARENT:
120 value = dc->parent ? dc->parent->name : "";
125 return (SYSCTL_OUT(req, value, strlen(value)));
129 devclass_sysctl_init(devclass_t dc)
132 if (dc->sysctl_tree != NULL)
134 sysctl_ctx_init(&dc->sysctl_ctx);
135 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
136 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
137 CTLFLAG_RD, NULL, "");
138 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
139 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
140 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
146 DEVICE_SYSCTL_DRIVER,
147 DEVICE_SYSCTL_LOCATION,
148 DEVICE_SYSCTL_PNPINFO,
149 DEVICE_SYSCTL_PARENT,
153 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
155 device_t dev = (device_t)arg1;
162 case DEVICE_SYSCTL_DESC:
163 value = dev->desc ? dev->desc : "";
165 case DEVICE_SYSCTL_DRIVER:
166 value = dev->driver ? dev->driver->name : "";
168 case DEVICE_SYSCTL_LOCATION:
169 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
170 bus_child_location_str(dev, buf, 1024);
172 case DEVICE_SYSCTL_PNPINFO:
173 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
174 bus_child_pnpinfo_str(dev, buf, 1024);
176 case DEVICE_SYSCTL_PARENT:
177 value = dev->parent ? dev->parent->nameunit : "";
182 error = SYSCTL_OUT(req, value, strlen(value));
189 device_sysctl_init(device_t dev)
191 devclass_t dc = dev->devclass;
193 if (dev->sysctl_tree != NULL)
195 devclass_sysctl_init(dc);
196 sysctl_ctx_init(&dev->sysctl_ctx);
197 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
198 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
199 dev->nameunit + strlen(dc->name),
200 CTLFLAG_RD, NULL, "");
201 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
202 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
203 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
204 "device description");
205 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
206 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
207 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
208 "device driver name");
209 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
210 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
211 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
212 "device location relative to parent");
213 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
214 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
215 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
216 "device identification");
217 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
218 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
219 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
224 device_sysctl_update(device_t dev)
226 devclass_t dc = dev->devclass;
228 if (dev->sysctl_tree == NULL)
230 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
234 device_sysctl_fini(device_t dev)
236 if (dev->sysctl_tree == NULL)
238 sysctl_ctx_free(&dev->sysctl_ctx);
239 dev->sysctl_tree = NULL;
242 static void device_attach_async(device_t dev);
243 static void device_attach_thread(void *arg);
244 static int device_doattach(device_t dev);
246 static int do_async_attach = 0;
247 static int numasyncthreads;
248 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
251 * /dev/devctl implementation
255 * This design allows only one reader for /dev/devctl. This is not desirable
256 * in the long run, but will get a lot of hair out of this implementation.
257 * Maybe we should make this device a clonable device.
259 * Also note: we specifically do not attach a device to the device_t tree
260 * to avoid potential chicken and egg problems. One could argue that all
261 * of this belongs to the root node. One could also further argue that the
262 * sysctl interface that we have not might more properly be an ioctl
263 * interface, but at this stage of the game, I'm not inclined to rock that
266 * I'm also not sure that the SIGIO support is done correctly or not, as
267 * I copied it from a driver that had SIGIO support that likely hasn't been
268 * tested since 3.4 or 2.2.8!
271 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
272 static int devctl_disable = 0;
273 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
274 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
275 sysctl_devctl_disable, "I", "devctl disable");
277 static d_open_t devopen;
278 static d_close_t devclose;
279 static d_read_t devread;
280 static d_ioctl_t devioctl;
281 static d_kqfilter_t devkqfilter;
283 static struct dev_ops devctl_ops = {
289 .d_kqfilter = devkqfilter
292 struct dev_event_info
295 TAILQ_ENTRY(dev_event_info) dei_link;
298 TAILQ_HEAD(devq, dev_event_info);
300 static struct dev_softc
307 struct proc *async_proc;
313 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
314 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
315 TAILQ_INIT(&devsoftc.devq);
319 devopen(struct dev_open_args *ap)
325 devsoftc.nonblock = 0;
326 devsoftc.async_proc = NULL;
331 devclose(struct dev_close_args *ap)
334 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
336 lockmgr(&devsoftc.lock, LK_RELEASE);
342 * The read channel for this device is used to report changes to
343 * userland in realtime. We are required to free the data as well as
344 * the n1 object because we allocate them separately. Also note that
345 * we return one record at a time. If you try to read this device a
346 * character at a time, you will lose the rest of the data. Listening
347 * programs are expected to cope.
350 devread(struct dev_read_args *ap)
352 struct uio *uio = ap->a_uio;
353 struct dev_event_info *n1;
356 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
357 while (TAILQ_EMPTY(&devsoftc.devq)) {
358 if (devsoftc.nonblock) {
359 lockmgr(&devsoftc.lock, LK_RELEASE);
362 tsleep_interlock(&devsoftc, PCATCH);
363 lockmgr(&devsoftc.lock, LK_RELEASE);
364 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
365 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
368 * Need to translate ERESTART to EINTR here? -- jake
370 lockmgr(&devsoftc.lock, LK_RELEASE);
374 n1 = TAILQ_FIRST(&devsoftc.devq);
375 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
376 lockmgr(&devsoftc.lock, LK_RELEASE);
377 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
378 kfree(n1->dei_data, M_BUS);
384 devioctl(struct dev_ioctl_args *ap)
389 if (*(int*)ap->a_data)
390 devsoftc.nonblock = 1;
392 devsoftc.nonblock = 0;
395 if (*(int*)ap->a_data)
396 devsoftc.async_proc = curproc;
398 devsoftc.async_proc = NULL;
401 /* (un)Support for other fcntl() calls. */
413 static void dev_filter_detach(struct knote *);
414 static int dev_filter_read(struct knote *, long);
416 static struct filterops dev_filtops =
417 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
420 devkqfilter(struct dev_kqfilter_args *ap)
422 struct knote *kn = ap->a_kn;
426 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
428 switch (kn->kn_filter) {
430 kn->kn_fop = &dev_filtops;
433 ap->a_result = EOPNOTSUPP;
434 lockmgr(&devsoftc.lock, LK_RELEASE);
438 klist = &devsoftc.kq.ki_note;
439 knote_insert(klist, kn);
441 lockmgr(&devsoftc.lock, LK_RELEASE);
447 dev_filter_detach(struct knote *kn)
451 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
452 klist = &devsoftc.kq.ki_note;
453 knote_remove(klist, kn);
454 lockmgr(&devsoftc.lock, LK_RELEASE);
458 dev_filter_read(struct knote *kn, long hint)
462 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
463 if (!TAILQ_EMPTY(&devsoftc.devq))
465 lockmgr(&devsoftc.lock, LK_RELEASE);
472 * @brief Return whether the userland process is running
475 devctl_process_running(void)
477 return (devsoftc.inuse == 1);
481 * @brief Queue data to be read from the devctl device
483 * Generic interface to queue data to the devctl device. It is
484 * assumed that @p data is properly formatted. It is further assumed
485 * that @p data is allocated using the M_BUS malloc type.
488 devctl_queue_data(char *data)
490 struct dev_event_info *n1 = NULL;
493 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
497 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
498 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
500 lockmgr(&devsoftc.lock, LK_RELEASE);
501 KNOTE(&devsoftc.kq.ki_note, 0);
502 p = devsoftc.async_proc;
508 * @brief Send a 'notification' to userland, using standard ways
511 devctl_notify(const char *system, const char *subsystem, const char *type,
518 return; /* BOGUS! Must specify system. */
519 if (subsystem == NULL)
520 return; /* BOGUS! Must specify subsystem. */
522 return; /* BOGUS! Must specify type. */
523 len += strlen(" system=") + strlen(system);
524 len += strlen(" subsystem=") + strlen(subsystem);
525 len += strlen(" type=") + strlen(type);
526 /* add in the data message plus newline. */
529 len += 3; /* '!', '\n', and NUL */
530 msg = kmalloc(len, M_BUS, M_NOWAIT);
532 return; /* Drop it on the floor */
534 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
535 system, subsystem, type, data);
537 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
538 system, subsystem, type);
539 devctl_queue_data(msg);
543 * Common routine that tries to make sending messages as easy as possible.
544 * We allocate memory for the data, copy strings into that, but do not
545 * free it unless there's an error. The dequeue part of the driver should
546 * free the data. We don't send data when the device is disabled. We do
547 * send data, even when we have no listeners, because we wish to avoid
548 * races relating to startup and restart of listening applications.
550 * devaddq is designed to string together the type of event, with the
551 * object of that event, plus the plug and play info and location info
552 * for that event. This is likely most useful for devices, but less
553 * useful for other consumers of this interface. Those should use
554 * the devctl_queue_data() interface instead.
557 devaddq(const char *type, const char *what, device_t dev)
566 data = kmalloc(1024, M_BUS, M_NOWAIT);
570 /* get the bus specific location of this device */
571 loc = kmalloc(1024, M_BUS, M_NOWAIT);
575 bus_child_location_str(dev, loc, 1024);
577 /* Get the bus specific pnp info of this device */
578 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
582 bus_child_pnpinfo_str(dev, pnp, 1024);
584 /* Get the parent of this device, or / if high enough in the tree. */
585 if (device_get_parent(dev) == NULL)
586 parstr = "."; /* Or '/' ? */
588 parstr = device_get_nameunit(device_get_parent(dev));
589 /* String it all together. */
590 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
594 devctl_queue_data(data);
604 * A device was added to the tree. We are called just after it successfully
605 * attaches (that is, probe and attach success for this device). No call
606 * is made if a device is merely parented into the tree. See devnomatch
607 * if probe fails. If attach fails, no notification is sent (but maybe
608 * we should have a different message for this).
611 devadded(device_t dev)
616 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
619 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
623 bus_child_pnpinfo_str(dev, pnp, 1024);
624 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
625 devaddq("+", tmp, dev);
635 * A device was removed from the tree. We are called just before this
639 devremoved(device_t dev)
644 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
647 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
651 bus_child_pnpinfo_str(dev, pnp, 1024);
652 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
653 devaddq("-", tmp, dev);
663 * Called when there's no match for this device. This is only called
664 * the first time that no match happens, so we don't keep getitng this
665 * message. Should that prove to be undesirable, we can change it.
666 * This is called when all drivers that can attach to a given bus
667 * decline to accept this device. Other errrors may not be detected.
670 devnomatch(device_t dev)
672 devaddq("?", "", dev);
676 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
678 struct dev_event_info *n1;
681 dis = devctl_disable;
682 error = sysctl_handle_int(oidp, &dis, 0, req);
683 if (error || !req->newptr)
685 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
686 devctl_disable = dis;
688 while (!TAILQ_EMPTY(&devsoftc.devq)) {
689 n1 = TAILQ_FIRST(&devsoftc.devq);
690 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
691 kfree(n1->dei_data, M_BUS);
695 lockmgr(&devsoftc.lock, LK_RELEASE);
699 /* End of /dev/devctl code */
701 TAILQ_HEAD(,device) bus_data_devices;
702 static int bus_data_generation = 1;
704 kobj_method_t null_methods[] = {
708 DEFINE_CLASS(null, null_methods, 0);
711 * Devclass implementation
714 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
717 devclass_find_internal(const char *classname, const char *parentname,
722 PDEBUG(("looking for %s", classname));
723 if (classname == NULL)
726 TAILQ_FOREACH(dc, &devclasses, link)
727 if (!strcmp(dc->name, classname))
731 PDEBUG(("creating %s", classname));
732 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
733 M_BUS, M_INTWAIT | M_ZERO);
735 dc->name = (char*) (dc + 1);
736 strcpy(dc->name, classname);
739 TAILQ_INIT(&dc->drivers);
740 TAILQ_INSERT_TAIL(&devclasses, dc, link);
742 bus_data_generation_update();
747 * If a parent class is specified, then set that as our parent so
748 * that this devclass will support drivers for the parent class as
749 * well. If the parent class has the same name don't do this though
750 * as it creates a cycle that can trigger an infinite loop in
751 * device_probe_child() if a device exists for which there is no
754 if (parentname && dc && !dc->parent &&
755 strcmp(classname, parentname) != 0)
756 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
762 devclass_create(const char *classname)
764 return(devclass_find_internal(classname, NULL, TRUE));
768 devclass_find(const char *classname)
770 return(devclass_find_internal(classname, NULL, FALSE));
774 devclass_find_unit(const char *classname, int unit)
778 if ((dc = devclass_find(classname)) != NULL)
779 return(devclass_get_device(dc, unit));
784 devclass_add_driver(devclass_t dc, driver_t *driver)
790 PDEBUG(("%s", DRIVERNAME(driver)));
792 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
795 * Compile the driver's methods. Also increase the reference count
796 * so that the class doesn't get freed when the last instance
797 * goes. This means we can safely use static methods and avoids a
798 * double-free in devclass_delete_driver.
800 kobj_class_instantiate(driver);
803 * Make sure the devclass which the driver is implementing exists.
805 devclass_find_internal(driver->name, NULL, TRUE);
808 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
811 * Call BUS_DRIVER_ADDED for any existing busses in this class,
812 * but only if the bus has already been attached (otherwise we
813 * might probe too early).
815 * This is what will cause a newly loaded module to be associated
816 * with hardware. bus_generic_driver_added() is typically what ends
819 for (i = 0; i < dc->maxunit; i++) {
820 if ((dev = dc->devices[i]) != NULL) {
821 if (dev->state >= DS_ATTACHED)
822 BUS_DRIVER_ADDED(dev, driver);
826 bus_data_generation_update();
831 devclass_delete_driver(devclass_t busclass, driver_t *driver)
833 devclass_t dc = devclass_find(driver->name);
839 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
845 * Find the link structure in the bus' list of drivers.
847 TAILQ_FOREACH(dl, &busclass->drivers, link)
848 if (dl->driver == driver)
852 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
857 * Disassociate from any devices. We iterate through all the
858 * devices in the devclass of the driver and detach any which are
859 * using the driver and which have a parent in the devclass which
860 * we are deleting from.
862 * Note that since a driver can be in multiple devclasses, we
863 * should not detach devices which are not children of devices in
864 * the affected devclass.
866 for (i = 0; i < dc->maxunit; i++)
867 if (dc->devices[i]) {
868 dev = dc->devices[i];
869 if (dev->driver == driver && dev->parent &&
870 dev->parent->devclass == busclass) {
871 if ((error = device_detach(dev)) != 0)
873 device_set_driver(dev, NULL);
877 TAILQ_REMOVE(&busclass->drivers, dl, link);
880 kobj_class_uninstantiate(driver);
882 bus_data_generation_update();
887 devclass_find_driver_internal(devclass_t dc, const char *classname)
891 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
893 TAILQ_FOREACH(dl, &dc->drivers, link)
894 if (!strcmp(dl->driver->name, classname))
897 PDEBUG(("not found"));
902 devclass_find_driver(devclass_t dc, const char *classname)
906 dl = devclass_find_driver_internal(dc, classname);
914 devclass_get_name(devclass_t dc)
920 devclass_get_device(devclass_t dc, int unit)
922 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
924 return(dc->devices[unit]);
928 devclass_get_softc(devclass_t dc, int unit)
932 dev = devclass_get_device(dc, unit);
936 return(device_get_softc(dev));
940 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
947 for (i = 0; i < dc->maxunit; i++)
951 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
954 for (i = 0; i < dc->maxunit; i++)
955 if (dc->devices[i]) {
956 list[count] = dc->devices[i];
967 * @brief Get a list of drivers in the devclass
969 * An array containing a list of pointers to all the drivers in the
970 * given devclass is allocated and returned in @p *listp. The number
971 * of drivers in the array is returned in @p *countp. The caller should
972 * free the array using @c free(p, M_TEMP).
974 * @param dc the devclass to examine
975 * @param listp gives location for array pointer return value
976 * @param countp gives location for number of array elements
980 * @retval ENOMEM the array allocation failed
983 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
990 TAILQ_FOREACH(dl, &dc->drivers, link)
992 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
997 TAILQ_FOREACH(dl, &dc->drivers, link) {
998 list[count] = dl->driver;
1008 * @brief Get the number of devices in a devclass
1010 * @param dc the devclass to examine
1013 devclass_get_count(devclass_t dc)
1018 for (i = 0; i < dc->maxunit; i++)
1025 devclass_get_maxunit(devclass_t dc)
1027 return(dc->maxunit);
1031 devclass_set_parent(devclass_t dc, devclass_t pdc)
1037 devclass_get_parent(devclass_t dc)
1043 devclass_alloc_unit(devclass_t dc, int *unitp)
1047 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1049 /* If we have been given a wired unit number, check for existing device */
1051 if (unit >= 0 && unit < dc->maxunit &&
1052 dc->devices[unit] != NULL) {
1054 kprintf("%s-: %s%d exists, using next available unit number\n",
1055 dc->name, dc->name, unit);
1056 /* find the next available slot */
1057 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
1061 /* Unwired device, find the next available slot for it */
1063 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1068 * We've selected a unit beyond the length of the table, so let's
1069 * extend the table to make room for all units up to and including
1072 if (unit >= dc->maxunit) {
1076 newsize = (unit + 1);
1077 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
1078 M_INTWAIT | M_ZERO);
1079 if (newlist == NULL)
1081 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1083 kfree(dc->devices, M_BUS);
1084 dc->devices = newlist;
1085 dc->maxunit = newsize;
1087 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1094 devclass_add_device(devclass_t dc, device_t dev)
1098 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1100 buflen = strlen(dc->name) + 5;
1101 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1102 if (dev->nameunit == NULL)
1105 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1106 kfree(dev->nameunit, M_BUS);
1107 dev->nameunit = NULL;
1110 dc->devices[dev->unit] = dev;
1112 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1118 devclass_delete_device(devclass_t dc, device_t dev)
1123 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1125 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1126 panic("devclass_delete_device: inconsistent device class");
1127 dc->devices[dev->unit] = NULL;
1128 if (dev->flags & DF_WILDCARD)
1130 dev->devclass = NULL;
1131 kfree(dev->nameunit, M_BUS);
1132 dev->nameunit = NULL;
1138 make_device(device_t parent, const char *name, int unit)
1143 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1146 dc = devclass_find_internal(name, NULL, TRUE);
1148 kprintf("make_device: can't find device class %s\n", name);
1154 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1158 dev->parent = parent;
1159 TAILQ_INIT(&dev->children);
1160 kobj_init((kobj_t) dev, &null_class);
1162 dev->devclass = NULL;
1164 dev->nameunit = NULL;
1168 dev->flags = DF_ENABLED;
1171 dev->flags |= DF_WILDCARD;
1173 dev->flags |= DF_FIXEDCLASS;
1174 if (devclass_add_device(dc, dev) != 0) {
1175 kobj_delete((kobj_t)dev, M_BUS);
1182 dev->state = DS_NOTPRESENT;
1184 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1185 bus_data_generation_update();
1191 device_print_child(device_t dev, device_t child)
1195 if (device_is_alive(child))
1196 retval += BUS_PRINT_CHILD(dev, child);
1198 retval += device_printf(child, " not found\n");
1204 device_add_child(device_t dev, const char *name, int unit)
1206 return device_add_child_ordered(dev, 0, name, unit);
1210 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1215 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1218 child = make_device(dev, name, unit);
1221 child->order = order;
1223 TAILQ_FOREACH(place, &dev->children, link)
1224 if (place->order > order)
1229 * The device 'place' is the first device whose order is
1230 * greater than the new child.
1232 TAILQ_INSERT_BEFORE(place, child, link);
1235 * The new child's order is greater or equal to the order of
1236 * any existing device. Add the child to the tail of the list.
1238 TAILQ_INSERT_TAIL(&dev->children, child, link);
1241 bus_data_generation_update();
1246 device_delete_child(device_t dev, device_t child)
1249 device_t grandchild;
1251 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1253 /* remove children first */
1254 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1255 error = device_delete_child(child, grandchild);
1260 if ((error = device_detach(child)) != 0)
1262 if (child->devclass)
1263 devclass_delete_device(child->devclass, child);
1264 TAILQ_REMOVE(&dev->children, child, link);
1265 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1266 device_set_desc(child, NULL);
1267 kobj_delete((kobj_t)child, M_BUS);
1269 bus_data_generation_update();
1274 * @brief Delete all children devices of the given device, if any.
1276 * This function deletes all children devices of the given device, if
1277 * any, using the device_delete_child() function for each device it
1278 * finds. If a child device cannot be deleted, this function will
1279 * return an error code.
1281 * @param dev the parent device
1284 * @retval non-zero a device would not detach
1287 device_delete_children(device_t dev)
1292 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1296 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1297 error = device_delete_child(dev, child);
1299 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1307 * @brief Find a device given a unit number
1309 * This is similar to devclass_get_devices() but only searches for
1310 * devices which have @p dev as a parent.
1312 * @param dev the parent device to search
1313 * @param unit the unit number to search for. If the unit is -1,
1314 * return the first child of @p dev which has name
1315 * @p classname (that is, the one with the lowest unit.)
1317 * @returns the device with the given unit number or @c
1318 * NULL if there is no such device
1321 device_find_child(device_t dev, const char *classname, int unit)
1326 dc = devclass_find(classname);
1331 child = devclass_get_device(dc, unit);
1332 if (child && child->parent == dev)
1335 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1336 child = devclass_get_device(dc, unit);
1337 if (child && child->parent == dev)
1345 first_matching_driver(devclass_t dc, device_t dev)
1348 return(devclass_find_driver_internal(dc, dev->devclass->name));
1350 return(TAILQ_FIRST(&dc->drivers));
1354 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1356 if (dev->devclass) {
1358 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1359 if (!strcmp(dev->devclass->name, dl->driver->name))
1363 return(TAILQ_NEXT(last, link));
1367 device_probe_child(device_t dev, device_t child)
1370 driverlink_t best = NULL;
1372 int result, pri = 0;
1373 int hasclass = (child->devclass != NULL);
1377 panic("device_probe_child: parent device has no devclass");
1379 if (child->state == DS_ALIVE)
1382 for (; dc; dc = dc->parent) {
1383 for (dl = first_matching_driver(dc, child); dl;
1384 dl = next_matching_driver(dc, child, dl)) {
1385 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1386 device_set_driver(child, dl->driver);
1388 device_set_devclass(child, dl->driver->name);
1389 result = DEVICE_PROBE(child);
1391 device_set_devclass(child, 0);
1394 * If the driver returns SUCCESS, there can be
1395 * no higher match for this device.
1404 * The driver returned an error so it
1405 * certainly doesn't match.
1408 device_set_driver(child, 0);
1413 * A priority lower than SUCCESS, remember the
1414 * best matching driver. Initialise the value
1415 * of pri for the first match.
1417 if (best == NULL || result > pri) {
1424 * If we have unambiguous match in this devclass,
1425 * don't look in the parent.
1427 if (best && pri == 0)
1432 * If we found a driver, change state and initialise the devclass.
1435 if (!child->devclass)
1436 device_set_devclass(child, best->driver->name);
1437 device_set_driver(child, best->driver);
1440 * A bit bogus. Call the probe method again to make
1441 * sure that we have the right description.
1443 DEVICE_PROBE(child);
1446 bus_data_generation_update();
1447 child->state = DS_ALIVE;
1455 device_get_parent(device_t dev)
1461 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1468 TAILQ_FOREACH(child, &dev->children, link)
1471 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1474 TAILQ_FOREACH(child, &dev->children, link) {
1475 list[count] = child;
1486 device_get_driver(device_t dev)
1488 return(dev->driver);
1492 device_get_devclass(device_t dev)
1494 return(dev->devclass);
1498 device_get_name(device_t dev)
1501 return devclass_get_name(dev->devclass);
1506 device_get_nameunit(device_t dev)
1508 return(dev->nameunit);
1512 device_get_unit(device_t dev)
1518 device_get_desc(device_t dev)
1524 device_get_flags(device_t dev)
1526 return(dev->devflags);
1529 struct sysctl_ctx_list *
1530 device_get_sysctl_ctx(device_t dev)
1532 return (&dev->sysctl_ctx);
1536 device_get_sysctl_tree(device_t dev)
1538 return (dev->sysctl_tree);
1542 device_print_prettyname(device_t dev)
1544 const char *name = device_get_name(dev);
1547 return kprintf("unknown: ");
1549 return kprintf("%s%d: ", name, device_get_unit(dev));
1553 device_printf(device_t dev, const char * fmt, ...)
1558 retval = device_print_prettyname(dev);
1559 __va_start(ap, fmt);
1560 retval += kvprintf(fmt, ap);
1566 device_set_desc_internal(device_t dev, const char* desc, int copy)
1568 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1569 kfree(dev->desc, M_BUS);
1570 dev->flags &= ~DF_DESCMALLOCED;
1575 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1577 strcpy(dev->desc, desc);
1578 dev->flags |= DF_DESCMALLOCED;
1581 /* Avoid a -Wcast-qual warning */
1582 dev->desc = (char *)(uintptr_t) desc;
1585 bus_data_generation_update();
1589 device_set_desc(device_t dev, const char* desc)
1591 device_set_desc_internal(dev, desc, FALSE);
1595 device_set_desc_copy(device_t dev, const char* desc)
1597 device_set_desc_internal(dev, desc, TRUE);
1601 device_set_flags(device_t dev, uint32_t flags)
1603 dev->devflags = flags;
1607 device_get_softc(device_t dev)
1613 device_set_softc(device_t dev, void *softc)
1615 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1616 kfree(dev->softc, M_BUS);
1619 dev->flags |= DF_EXTERNALSOFTC;
1621 dev->flags &= ~DF_EXTERNALSOFTC;
1625 device_set_async_attach(device_t dev, int enable)
1628 dev->flags |= DF_ASYNCPROBE;
1630 dev->flags &= ~DF_ASYNCPROBE;
1634 device_get_ivars(device_t dev)
1640 device_set_ivars(device_t dev, void * ivars)
1649 device_get_state(device_t dev)
1655 device_enable(device_t dev)
1657 dev->flags |= DF_ENABLED;
1661 device_disable(device_t dev)
1663 dev->flags &= ~DF_ENABLED;
1670 device_busy(device_t dev)
1672 if (dev->state < DS_ATTACHED)
1673 panic("device_busy: called for unattached device");
1674 if (dev->busy == 0 && dev->parent)
1675 device_busy(dev->parent);
1677 dev->state = DS_BUSY;
1684 device_unbusy(device_t dev)
1686 if (dev->state != DS_BUSY)
1687 panic("device_unbusy: called for non-busy device");
1689 if (dev->busy == 0) {
1691 device_unbusy(dev->parent);
1692 dev->state = DS_ATTACHED;
1697 device_quiet(device_t dev)
1699 dev->flags |= DF_QUIET;
1703 device_verbose(device_t dev)
1705 dev->flags &= ~DF_QUIET;
1709 device_is_quiet(device_t dev)
1711 return((dev->flags & DF_QUIET) != 0);
1715 device_is_enabled(device_t dev)
1717 return((dev->flags & DF_ENABLED) != 0);
1721 device_is_alive(device_t dev)
1723 return(dev->state >= DS_ALIVE);
1727 device_is_attached(device_t dev)
1729 return(dev->state >= DS_ATTACHED);
1733 device_set_devclass(device_t dev, const char *classname)
1740 devclass_delete_device(dev->devclass, dev);
1744 if (dev->devclass) {
1745 kprintf("device_set_devclass: device class already set\n");
1749 dc = devclass_find_internal(classname, NULL, TRUE);
1753 error = devclass_add_device(dc, dev);
1755 bus_data_generation_update();
1760 device_set_driver(device_t dev, driver_t *driver)
1762 if (dev->state >= DS_ATTACHED)
1765 if (dev->driver == driver)
1768 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1769 kfree(dev->softc, M_BUS);
1772 kobj_delete((kobj_t) dev, 0);
1773 dev->driver = driver;
1775 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1776 if (!(dev->flags & DF_EXTERNALSOFTC))
1777 dev->softc = kmalloc(driver->size, M_BUS,
1778 M_INTWAIT | M_ZERO);
1780 kobj_init((kobj_t) dev, &null_class);
1783 bus_data_generation_update();
1788 device_probe_and_attach(device_t dev)
1790 device_t bus = dev->parent;
1793 if (dev->state >= DS_ALIVE)
1796 if ((dev->flags & DF_ENABLED) == 0) {
1798 device_print_prettyname(dev);
1799 kprintf("not probed (disabled)\n");
1804 error = device_probe_child(bus, dev);
1806 if (!(dev->flags & DF_DONENOMATCH)) {
1807 BUS_PROBE_NOMATCH(bus, dev);
1809 dev->flags |= DF_DONENOMATCH;
1815 * Output the exact device chain prior to the attach in case the
1816 * system locks up during attach, and generate the full info after
1817 * the attach so correct irq and other information is displayed.
1819 if (bootverbose && !device_is_quiet(dev)) {
1822 kprintf("%s", device_get_nameunit(dev));
1823 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1824 kprintf(".%s", device_get_nameunit(tmp));
1827 if (!device_is_quiet(dev))
1828 device_print_child(bus, dev);
1829 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1830 kprintf("%s: probing asynchronously\n",
1831 device_get_nameunit(dev));
1832 dev->state = DS_INPROGRESS;
1833 device_attach_async(dev);
1836 error = device_doattach(dev);
1842 * Device is known to be alive, do the attach asynchronously.
1843 * However, serialize the attaches with the mp lock.
1846 device_attach_async(device_t dev)
1850 atomic_add_int(&numasyncthreads, 1);
1851 lwkt_create(device_attach_thread, dev, &td, NULL,
1852 0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
1856 device_attach_thread(void *arg)
1860 (void)device_doattach(dev);
1861 atomic_subtract_int(&numasyncthreads, 1);
1862 wakeup(&numasyncthreads);
1866 * Device is known to be alive, do the attach (synchronous or asynchronous)
1869 device_doattach(device_t dev)
1871 device_t bus = dev->parent;
1872 int hasclass = (dev->devclass != NULL);
1875 device_sysctl_init(dev);
1876 error = DEVICE_ATTACH(dev);
1878 dev->state = DS_ATTACHED;
1879 if (bootverbose && !device_is_quiet(dev))
1880 device_print_child(bus, dev);
1881 device_sysctl_update(dev);
1884 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1885 dev->driver->name, dev->unit, error);
1886 /* Unset the class that was set in device_probe_child */
1888 device_set_devclass(dev, 0);
1889 device_set_driver(dev, NULL);
1890 dev->state = DS_NOTPRESENT;
1891 device_sysctl_fini(dev);
1897 device_detach(device_t dev)
1901 PDEBUG(("%s", DEVICENAME(dev)));
1902 if (dev->state == DS_BUSY)
1904 if (dev->state != DS_ATTACHED)
1907 if ((error = DEVICE_DETACH(dev)) != 0)
1910 device_printf(dev, "detached\n");
1912 BUS_CHILD_DETACHED(dev->parent, dev);
1914 if (!(dev->flags & DF_FIXEDCLASS))
1915 devclass_delete_device(dev->devclass, dev);
1917 dev->state = DS_NOTPRESENT;
1918 device_set_driver(dev, NULL);
1919 device_sysctl_fini(dev);
1925 device_shutdown(device_t dev)
1927 if (dev->state < DS_ATTACHED)
1929 PDEBUG(("%s", DEVICENAME(dev)));
1930 return DEVICE_SHUTDOWN(dev);
1934 device_set_unit(device_t dev, int unit)
1939 dc = device_get_devclass(dev);
1940 if (unit < dc->maxunit && dc->devices[unit])
1942 err = devclass_delete_device(dc, dev);
1946 err = devclass_add_device(dc, dev);
1950 bus_data_generation_update();
1954 /*======================================*/
1956 * Access functions for device resources.
1959 /* Supplied by config(8) in ioconf.c */
1960 extern struct config_device config_devtab[];
1961 extern int devtab_count;
1963 /* Runtime version */
1964 struct config_device *devtab = config_devtab;
1967 resource_new_name(const char *name, int unit)
1969 struct config_device *new;
1971 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1972 M_INTWAIT | M_ZERO);
1973 if (devtab && devtab_count > 0)
1974 bcopy(devtab, new, devtab_count * sizeof(*new));
1975 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1976 if (new[devtab_count].name == NULL) {
1980 strcpy(new[devtab_count].name, name);
1981 new[devtab_count].unit = unit;
1982 new[devtab_count].resource_count = 0;
1983 new[devtab_count].resources = NULL;
1984 if (devtab && devtab != config_devtab)
1985 kfree(devtab, M_TEMP);
1987 return devtab_count++;
1991 resource_new_resname(int j, const char *resname, resource_type type)
1993 struct config_resource *new;
1996 i = devtab[j].resource_count;
1997 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1998 if (devtab[j].resources && i > 0)
1999 bcopy(devtab[j].resources, new, i * sizeof(*new));
2000 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
2001 if (new[i].name == NULL) {
2005 strcpy(new[i].name, resname);
2007 if (devtab[j].resources)
2008 kfree(devtab[j].resources, M_TEMP);
2009 devtab[j].resources = new;
2010 devtab[j].resource_count = i + 1;
2015 resource_match_string(int i, const char *resname, const char *value)
2018 struct config_resource *res;
2020 for (j = 0, res = devtab[i].resources;
2021 j < devtab[i].resource_count; j++, res++)
2022 if (!strcmp(res->name, resname)
2023 && res->type == RES_STRING
2024 && !strcmp(res->u.stringval, value))
2030 resource_find(const char *name, int unit, const char *resname,
2031 struct config_resource **result)
2034 struct config_resource *res;
2037 * First check specific instances, then generic.
2039 for (i = 0; i < devtab_count; i++) {
2040 if (devtab[i].unit < 0)
2042 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2043 res = devtab[i].resources;
2044 for (j = 0; j < devtab[i].resource_count; j++, res++)
2045 if (!strcmp(res->name, resname)) {
2051 for (i = 0; i < devtab_count; i++) {
2052 if (devtab[i].unit >= 0)
2054 /* XXX should this `&& devtab[i].unit == unit' be here? */
2055 /* XXX if so, then the generic match does nothing */
2056 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2057 res = devtab[i].resources;
2058 for (j = 0; j < devtab[i].resource_count; j++, res++)
2059 if (!strcmp(res->name, resname)) {
2069 resource_kenv(const char *name, int unit, const char *resname, long *result)
2074 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2075 if ((env = kgetenv(buf)) != NULL) {
2076 *result = strtol(env, NULL, 0);
2083 resource_int_value(const char *name, int unit, const char *resname, int *result)
2085 struct config_resource *res;
2089 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2090 *result = (int)kvalue;
2093 if ((error = resource_find(name, unit, resname, &res)) != 0)
2095 if (res->type != RES_INT)
2097 *result = res->u.intval;
2102 resource_long_value(const char *name, int unit, const char *resname,
2105 struct config_resource *res;
2109 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2113 if ((error = resource_find(name, unit, resname, &res)) != 0)
2115 if (res->type != RES_LONG)
2117 *result = res->u.longval;
2122 resource_string_value(const char *name, int unit, const char *resname,
2123 const char **result)
2126 struct config_resource *res;
2128 if ((error = resource_find(name, unit, resname, &res)) != 0)
2130 if (res->type != RES_STRING)
2132 *result = res->u.stringval;
2137 resource_query_string(int i, const char *resname, const char *value)
2143 for (; i < devtab_count; i++)
2144 if (resource_match_string(i, resname, value) >= 0)
2150 resource_locate(int i, const char *resname)
2156 for (; i < devtab_count; i++)
2157 if (!strcmp(devtab[i].name, resname))
2163 resource_count(void)
2165 return(devtab_count);
2169 resource_query_name(int i)
2171 return(devtab[i].name);
2175 resource_query_unit(int i)
2177 return(devtab[i].unit);
2181 resource_create(const char *name, int unit, const char *resname,
2182 resource_type type, struct config_resource **result)
2185 struct config_resource *res = NULL;
2187 for (i = 0; i < devtab_count; i++)
2188 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2189 res = devtab[i].resources;
2193 i = resource_new_name(name, unit);
2196 res = devtab[i].resources;
2198 for (j = 0; j < devtab[i].resource_count; j++, res++)
2199 if (!strcmp(res->name, resname)) {
2203 j = resource_new_resname(i, resname, type);
2206 res = &devtab[i].resources[j];
2212 resource_set_int(const char *name, int unit, const char *resname, int value)
2215 struct config_resource *res;
2217 error = resource_create(name, unit, resname, RES_INT, &res);
2220 if (res->type != RES_INT)
2222 res->u.intval = value;
2227 resource_set_long(const char *name, int unit, const char *resname, long value)
2230 struct config_resource *res;
2232 error = resource_create(name, unit, resname, RES_LONG, &res);
2235 if (res->type != RES_LONG)
2237 res->u.longval = value;
2242 resource_set_string(const char *name, int unit, const char *resname,
2246 struct config_resource *res;
2248 error = resource_create(name, unit, resname, RES_STRING, &res);
2251 if (res->type != RES_STRING)
2253 if (res->u.stringval)
2254 kfree(res->u.stringval, M_TEMP);
2255 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2256 if (res->u.stringval == NULL)
2258 strcpy(res->u.stringval, value);
2263 resource_cfgload(void *dummy __unused)
2265 struct config_resource *res, *cfgres;
2268 char *name, *resname;
2272 int config_devtab_count;
2274 config_devtab_count = devtab_count;
2278 for (i = 0; i < config_devtab_count; i++) {
2279 name = config_devtab[i].name;
2280 unit = config_devtab[i].unit;
2282 for (j = 0; j < config_devtab[i].resource_count; j++) {
2283 cfgres = config_devtab[i].resources;
2284 resname = cfgres[j].name;
2285 type = cfgres[j].type;
2286 error = resource_create(name, unit, resname, type,
2289 kprintf("create resource %s%d: error %d\n",
2293 if (res->type != type) {
2294 kprintf("type mismatch %s%d: %d != %d\n",
2295 name, unit, res->type, type);
2300 res->u.intval = cfgres[j].u.intval;
2303 res->u.longval = cfgres[j].u.longval;
2306 if (res->u.stringval)
2307 kfree(res->u.stringval, M_TEMP);
2308 stringval = cfgres[j].u.stringval;
2309 res->u.stringval = kmalloc(strlen(stringval) + 1,
2311 if (res->u.stringval == NULL)
2313 strcpy(res->u.stringval, stringval);
2316 panic("unknown resource type %d", type);
2321 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2324 /*======================================*/
2326 * Some useful method implementations to make life easier for bus drivers.
2330 resource_list_init(struct resource_list *rl)
2336 resource_list_free(struct resource_list *rl)
2338 struct resource_list_entry *rle;
2340 while ((rle = SLIST_FIRST(rl)) != NULL) {
2342 panic("resource_list_free: resource entry is busy");
2343 SLIST_REMOVE_HEAD(rl, link);
2349 resource_list_add(struct resource_list *rl, int type, int rid,
2350 u_long start, u_long end, u_long count, int cpuid)
2352 struct resource_list_entry *rle;
2354 rle = resource_list_find(rl, type, rid);
2356 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2358 SLIST_INSERT_HEAD(rl, rle, link);
2366 panic("resource_list_add: resource entry is busy");
2373 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2374 panic("resource_list_add: moving from cpu%d -> cpu%d",
2381 struct resource_list_entry*
2382 resource_list_find(struct resource_list *rl,
2385 struct resource_list_entry *rle;
2387 SLIST_FOREACH(rle, rl, link)
2388 if (rle->type == type && rle->rid == rid)
2394 resource_list_delete(struct resource_list *rl,
2397 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2400 if (rle->res != NULL)
2401 panic("resource_list_delete: resource has not been released");
2402 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2408 resource_list_alloc(struct resource_list *rl,
2409 device_t bus, device_t child,
2411 u_long start, u_long end,
2412 u_long count, u_int flags, int cpuid)
2414 struct resource_list_entry *rle = NULL;
2415 int passthrough = (device_get_parent(child) != bus);
2416 int isdefault = (start == 0UL && end == ~0UL);
2419 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2421 start, end, count, flags, cpuid));
2424 rle = resource_list_find(rl, type, *rid);
2427 return(0); /* no resource of that type/rid */
2430 panic("resource_list_alloc: resource entry is busy");
2434 count = max(count, rle->count);
2435 end = max(rle->end, start + count - 1);
2439 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2440 type, rid, start, end, count,
2444 * Record the new range.
2447 rle->start = rman_get_start(rle->res);
2448 rle->end = rman_get_end(rle->res);
2456 resource_list_release(struct resource_list *rl,
2457 device_t bus, device_t child,
2458 int type, int rid, struct resource *res)
2460 struct resource_list_entry *rle = NULL;
2461 int passthrough = (device_get_parent(child) != bus);
2465 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2469 rle = resource_list_find(rl, type, rid);
2472 panic("resource_list_release: can't find resource");
2474 panic("resource_list_release: resource entry is not busy");
2476 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2486 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2489 struct resource_list_entry *rle;
2490 int printed, retval;
2494 /* Yes, this is kinda cheating */
2495 SLIST_FOREACH(rle, rl, link) {
2496 if (rle->type == type) {
2498 retval += kprintf(" %s ", name);
2500 retval += kprintf(",");
2502 retval += kprintf(format, rle->start);
2503 if (rle->count > 1) {
2504 retval += kprintf("-");
2505 retval += kprintf(format, rle->start +
2514 * Generic driver/device identify functions. These will install a device
2515 * rendezvous point under the parent using the same name as the driver
2516 * name, which will at a later time be probed and attached.
2518 * These functions are used when the parent does not 'scan' its bus for
2519 * matching devices, or for the particular devices using these functions,
2520 * or when the device is a pseudo or synthesized device (such as can be
2521 * found under firewire and ppbus).
2524 bus_generic_identify(driver_t *driver, device_t parent)
2526 if (parent->state == DS_ATTACHED)
2528 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2533 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2535 if (parent->state == DS_ATTACHED)
2537 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2542 * Call DEVICE_IDENTIFY for each driver.
2545 bus_generic_probe(device_t dev)
2547 devclass_t dc = dev->devclass;
2550 TAILQ_FOREACH(dl, &dc->drivers, link) {
2551 DEVICE_IDENTIFY(dl->driver, dev);
2558 * This is an aweful hack due to the isa bus and autoconf code not
2559 * probing the ISA devices until after everything else has configured.
2560 * The ISA bus did a dummy attach long ago so we have to set it back
2561 * to an earlier state so the probe thinks its the initial probe and
2564 * XXX remove by properly defering the ISA bus scan.
2567 bus_generic_probe_hack(device_t dev)
2569 if (dev->state == DS_ATTACHED) {
2570 dev->state = DS_ALIVE;
2571 bus_generic_probe(dev);
2572 dev->state = DS_ATTACHED;
2578 bus_generic_attach(device_t dev)
2582 TAILQ_FOREACH(child, &dev->children, link) {
2583 device_probe_and_attach(child);
2590 bus_generic_detach(device_t dev)
2595 if (dev->state != DS_ATTACHED)
2598 TAILQ_FOREACH(child, &dev->children, link)
2599 if ((error = device_detach(child)) != 0)
2606 bus_generic_shutdown(device_t dev)
2610 TAILQ_FOREACH(child, &dev->children, link)
2611 device_shutdown(child);
2617 bus_generic_suspend(device_t dev)
2620 device_t child, child2;
2622 TAILQ_FOREACH(child, &dev->children, link) {
2623 error = DEVICE_SUSPEND(child);
2625 for (child2 = TAILQ_FIRST(&dev->children);
2626 child2 && child2 != child;
2627 child2 = TAILQ_NEXT(child2, link))
2628 DEVICE_RESUME(child2);
2636 bus_generic_resume(device_t dev)
2640 TAILQ_FOREACH(child, &dev->children, link)
2641 DEVICE_RESUME(child);
2642 /* if resume fails, there's nothing we can usefully do... */
2648 bus_print_child_header(device_t dev, device_t child)
2652 if (device_get_desc(child))
2653 retval += device_printf(child, "<%s>", device_get_desc(child));
2655 retval += kprintf("%s", device_get_nameunit(child));
2657 if (child->state != DS_ATTACHED)
2658 kprintf(" [tentative]");
2660 kprintf(" [attached!]");
2666 bus_print_child_footer(device_t dev, device_t child)
2668 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2672 bus_generic_add_child(device_t dev, device_t child, int order,
2673 const char *name, int unit)
2676 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2678 dev = device_add_child_ordered(child, order, name, unit);
2684 bus_generic_print_child(device_t dev, device_t child)
2688 retval += bus_print_child_header(dev, child);
2689 retval += bus_print_child_footer(dev, child);
2695 bus_generic_read_ivar(device_t dev, device_t child, int index,
2701 error = BUS_READ_IVAR(dev->parent, child, index, result);
2708 bus_generic_write_ivar(device_t dev, device_t child, int index,
2714 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2721 * Resource list are used for iterations, do not recurse.
2723 struct resource_list *
2724 bus_generic_get_resource_list(device_t dev, device_t child)
2730 bus_generic_driver_added(device_t dev, driver_t *driver)
2734 DEVICE_IDENTIFY(driver, dev);
2735 TAILQ_FOREACH(child, &dev->children, link) {
2736 if (child->state == DS_NOTPRESENT)
2737 device_probe_and_attach(child);
2742 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2743 int flags, driver_intr_t *intr, void *arg, void **cookiep,
2744 lwkt_serialize_t serializer, const char *desc)
2746 /* Propagate up the bus hierarchy until someone handles it. */
2748 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2749 intr, arg, cookiep, serializer, desc);
2756 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2759 /* Propagate up the bus hierarchy until someone handles it. */
2761 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2767 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2770 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2776 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2779 BUS_ENABLE_INTR(dev->parent, child, cookie);
2783 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2784 enum intr_polarity pol)
2786 /* Propagate up the bus hierarchy until someone handles it. */
2788 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2794 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2795 u_long start, u_long end, u_long count, u_int flags, int cpuid)
2797 /* Propagate up the bus hierarchy until someone handles it. */
2799 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2800 start, end, count, flags, cpuid));
2806 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2809 /* Propagate up the bus hierarchy until someone handles it. */
2811 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2817 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2820 /* Propagate up the bus hierarchy until someone handles it. */
2822 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2828 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2829 int rid, struct resource *r)
2831 /* Propagate up the bus hierarchy until someone handles it. */
2833 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2840 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2841 u_long *startp, u_long *countp)
2847 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2854 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2855 u_long start, u_long count, int cpuid)
2861 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2862 start, count, cpuid);
2868 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2871 BUS_DELETE_RESOURCE(dev, child, type, rid);
2875 * @brief Helper function for implementing BUS_GET_DMA_TAG().
2877 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
2878 * BUS_GET_DMA_TAG() method of the parent of @p dev.
2881 bus_generic_get_dma_tag(device_t dev, device_t child)
2884 /* Propagate up the bus hierarchy until someone handles it. */
2885 if (dev->parent != NULL)
2886 return (BUS_GET_DMA_TAG(dev->parent, child));
2891 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2892 u_long *startp, u_long *countp)
2894 struct resource_list *rl = NULL;
2895 struct resource_list_entry *rle = NULL;
2897 rl = BUS_GET_RESOURCE_LIST(dev, child);
2901 rle = resource_list_find(rl, type, rid);
2906 *startp = rle->start;
2908 *countp = rle->count;
2914 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2915 u_long start, u_long count, int cpuid)
2917 struct resource_list *rl = NULL;
2919 rl = BUS_GET_RESOURCE_LIST(dev, child);
2923 resource_list_add(rl, type, rid, start, (start + count - 1), count,
2930 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2932 struct resource_list *rl = NULL;
2934 rl = BUS_GET_RESOURCE_LIST(dev, child);
2938 resource_list_delete(rl, type, rid);
2942 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2943 int rid, struct resource *r)
2945 struct resource_list *rl = NULL;
2947 rl = BUS_GET_RESOURCE_LIST(dev, child);
2951 return(resource_list_release(rl, dev, child, type, rid, r));
2955 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2956 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
2958 struct resource_list *rl = NULL;
2960 rl = BUS_GET_RESOURCE_LIST(dev, child);
2964 return(resource_list_alloc(rl, dev, child, type, rid,
2965 start, end, count, flags, cpuid));
2969 bus_generic_child_present(device_t bus, device_t child)
2971 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2976 * Some convenience functions to make it easier for drivers to use the
2977 * resource-management functions. All these really do is hide the
2978 * indirection through the parent's method table, making for slightly
2979 * less-wordy code. In the future, it might make sense for this code
2980 * to maintain some sort of a list of resources allocated by each device.
2983 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2984 struct resource **res)
2988 for (i = 0; rs[i].type != -1; i++)
2990 for (i = 0; rs[i].type != -1; i++) {
2991 res[i] = bus_alloc_resource_any(dev,
2992 rs[i].type, &rs[i].rid, rs[i].flags);
2993 if (res[i] == NULL) {
2994 bus_release_resources(dev, rs, res);
3002 bus_release_resources(device_t dev, const struct resource_spec *rs,
3003 struct resource **res)
3007 for (i = 0; rs[i].type != -1; i++)
3008 if (res[i] != NULL) {
3009 bus_release_resource(
3010 dev, rs[i].type, rs[i].rid, res[i]);
3016 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3017 u_long count, u_int flags)
3019 if (dev->parent == NULL)
3021 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3026 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3028 if (dev->parent == NULL)
3030 return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3031 irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3035 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3037 if (dev->parent == NULL)
3039 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3043 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3045 if (dev->parent == NULL)
3047 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3051 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3053 if (dev->parent == NULL)
3055 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3059 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3060 driver_intr_t handler, void *arg, void **cookiep,
3061 lwkt_serialize_t serializer, const char *desc)
3063 if (dev->parent == NULL)
3065 return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3066 cookiep, serializer, desc);
3070 bus_setup_intr(device_t dev, struct resource *r, int flags,
3071 driver_intr_t handler, void *arg, void **cookiep,
3072 lwkt_serialize_t serializer)
3074 return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3079 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3081 if (dev->parent == NULL)
3083 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3087 bus_enable_intr(device_t dev, void *cookie)
3090 BUS_ENABLE_INTR(dev->parent, dev, cookie);
3094 bus_disable_intr(device_t dev, void *cookie)
3097 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3103 bus_set_resource(device_t dev, int type, int rid,
3104 u_long start, u_long count, int cpuid)
3106 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3107 start, count, cpuid));
3111 bus_get_resource(device_t dev, int type, int rid,
3112 u_long *startp, u_long *countp)
3114 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3119 bus_get_resource_start(device_t dev, int type, int rid)
3121 u_long start, count;
3124 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3132 bus_get_resource_count(device_t dev, int type, int rid)
3134 u_long start, count;
3137 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3145 bus_delete_resource(device_t dev, int type, int rid)
3147 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3151 bus_child_present(device_t child)
3153 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3157 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3161 parent = device_get_parent(child);
3162 if (parent == NULL) {
3166 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3170 bus_child_location_str(device_t child, char *buf, size_t buflen)
3174 parent = device_get_parent(child);
3175 if (parent == NULL) {
3179 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3183 * @brief Wrapper function for BUS_GET_DMA_TAG().
3185 * This function simply calls the BUS_GET_DMA_TAG() method of the
3189 bus_get_dma_tag(device_t dev)
3193 parent = device_get_parent(dev);
3196 return (BUS_GET_DMA_TAG(parent, dev));
3200 root_print_child(device_t dev, device_t child)
3206 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3207 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3210 * If an interrupt mapping gets to here something bad has happened.
3212 panic("root_setup_intr");
3216 * If we get here, assume that the device is permanant and really is
3217 * present in the system. Removable bus drivers are expected to intercept
3218 * this call long before it gets here. We return -1 so that drivers that
3219 * really care can check vs -1 or some ERRNO returned higher in the food
3223 root_child_present(device_t dev, device_t child)
3229 * XXX NOTE! other defaults may be set in bus_if.m
3231 static kobj_method_t root_methods[] = {
3232 /* Device interface */
3233 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3234 KOBJMETHOD(device_suspend, bus_generic_suspend),
3235 KOBJMETHOD(device_resume, bus_generic_resume),
3238 KOBJMETHOD(bus_add_child, bus_generic_add_child),
3239 KOBJMETHOD(bus_print_child, root_print_child),
3240 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3241 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3242 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3243 KOBJMETHOD(bus_child_present, root_child_present),
3248 static driver_t root_driver = {
3255 devclass_t root_devclass;
3258 root_bus_module_handler(module_t mod, int what, void* arg)
3262 TAILQ_INIT(&bus_data_devices);
3263 root_bus = make_device(NULL, "root", 0);
3264 root_bus->desc = "System root bus";
3265 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3266 root_bus->driver = &root_driver;
3267 root_bus->state = DS_ALIVE;
3268 root_devclass = devclass_find_internal("root", NULL, FALSE);
3273 device_shutdown(root_bus);
3280 static moduledata_t root_bus_mod = {
3282 root_bus_module_handler,
3285 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3288 root_bus_configure(void)
3296 * handle device_identify based device attachments to the root_bus
3297 * (typically nexus).
3299 bus_generic_probe(root_bus);
3302 * Probe and attach the devices under root_bus.
3304 TAILQ_FOREACH(dev, &root_bus->children, link) {
3305 device_probe_and_attach(dev);
3309 * Wait for all asynchronous attaches to complete. If we don't
3310 * our legacy ISA bus scan could steal device unit numbers or
3314 if (numasyncthreads)
3315 kprintf("Waiting for async drivers to attach\n");
3316 while (numasyncthreads > 0) {
3317 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3319 if (warncount == 0) {
3320 kprintf("Warning: Still waiting for %d "
3321 "drivers to attach\n", numasyncthreads);
3322 } else if (warncount == -30) {
3323 kprintf("Giving up on %d drivers\n", numasyncthreads);
3327 root_bus->state = DS_ATTACHED;
3331 driver_module_handler(module_t mod, int what, void *arg)
3334 struct driver_module_data *dmd;
3335 devclass_t bus_devclass;
3336 kobj_class_t driver;
3337 const char *parentname;
3339 dmd = (struct driver_module_data *)arg;
3340 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3345 if (dmd->dmd_chainevh)
3346 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3348 driver = dmd->dmd_driver;
3349 PDEBUG(("Loading module: driver %s on bus %s",
3350 DRIVERNAME(driver), dmd->dmd_busname));
3353 * If the driver has any base classes, make the
3354 * devclass inherit from the devclass of the driver's
3355 * first base class. This will allow the system to
3356 * search for drivers in both devclasses for children
3357 * of a device using this driver.
3359 if (driver->baseclasses)
3360 parentname = driver->baseclasses[0]->name;
3363 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3366 error = devclass_add_driver(bus_devclass, driver);
3372 PDEBUG(("Unloading module: driver %s from bus %s",
3373 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3374 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3376 if (!error && dmd->dmd_chainevh)
3377 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3387 * The _short versions avoid iteration by not calling anything that prints
3388 * more than oneliners. I love oneliners.
3392 print_device_short(device_t dev, int indent)
3397 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3398 dev->unit, dev->desc,
3399 (dev->parent? "":"no "),
3400 (TAILQ_EMPTY(&dev->children)? "no ":""),
3401 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3402 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3403 (dev->flags&DF_WILDCARD? "wildcard,":""),
3404 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3405 (dev->ivars? "":"no "),
3406 (dev->softc? "":"no "),
3411 print_device(device_t dev, int indent)
3416 print_device_short(dev, indent);
3418 indentprintf(("Parent:\n"));
3419 print_device_short(dev->parent, indent+1);
3420 indentprintf(("Driver:\n"));
3421 print_driver_short(dev->driver, indent+1);
3422 indentprintf(("Devclass:\n"));
3423 print_devclass_short(dev->devclass, indent+1);
3427 * Print the device and all its children (indented).
3430 print_device_tree_short(device_t dev, int indent)
3437 print_device_short(dev, indent);
3439 TAILQ_FOREACH(child, &dev->children, link)
3440 print_device_tree_short(child, indent+1);
3444 * Print the device and all its children (indented).
3447 print_device_tree(device_t dev, int indent)
3454 print_device(dev, indent);
3456 TAILQ_FOREACH(child, &dev->children, link)
3457 print_device_tree(child, indent+1);
3461 print_driver_short(driver_t *driver, int indent)
3466 indentprintf(("driver %s: softc size = %zu\n",
3467 driver->name, driver->size));
3471 print_driver(driver_t *driver, int indent)
3476 print_driver_short(driver, indent);
3481 print_driver_list(driver_list_t drivers, int indent)
3483 driverlink_t driver;
3485 TAILQ_FOREACH(driver, &drivers, link)
3486 print_driver(driver->driver, indent);
3490 print_devclass_short(devclass_t dc, int indent)
3495 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3499 print_devclass(devclass_t dc, int indent)
3506 print_devclass_short(dc, indent);
3507 indentprintf(("Drivers:\n"));
3508 print_driver_list(dc->drivers, indent+1);
3510 indentprintf(("Devices:\n"));
3511 for (i = 0; i < dc->maxunit; i++)
3513 print_device(dc->devices[i], indent+1);
3517 print_devclass_list_short(void)
3521 kprintf("Short listing of devclasses, drivers & devices:\n");
3522 TAILQ_FOREACH(dc, &devclasses, link) {
3523 print_devclass_short(dc, 0);
3528 print_devclass_list(void)
3532 kprintf("Full listing of devclasses, drivers & devices:\n");
3533 TAILQ_FOREACH(dc, &devclasses, link) {
3534 print_devclass(dc, 0);
3541 * Check to see if a device is disabled via a disabled hint.
3544 resource_disabled(const char *name, int unit)
3548 error = resource_int_value(name, unit, "disabled", &value);
3555 * User-space access to the device tree.
3557 * We implement a small set of nodes:
3559 * hw.bus Single integer read method to obtain the
3560 * current generation count.
3561 * hw.bus.devices Reads the entire device tree in flat space.
3562 * hw.bus.rman Resource manager interface
3564 * We might like to add the ability to scan devclasses and/or drivers to
3565 * determine what else is currently loaded/available.
3569 sysctl_bus(SYSCTL_HANDLER_ARGS)
3571 struct u_businfo ubus;
3573 ubus.ub_version = BUS_USER_VERSION;
3574 ubus.ub_generation = bus_data_generation;
3576 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3578 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3579 "bus-related data");
3582 sysctl_devices(SYSCTL_HANDLER_ARGS)
3584 int *name = (int *)arg1;
3585 u_int namelen = arg2;
3588 struct u_device udev; /* XXX this is a bit big */
3594 if (bus_data_generation_check(name[0]))
3600 * Scan the list of devices, looking for the requested index.
3602 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3610 * Populate the return array.
3612 bzero(&udev, sizeof(udev));
3613 udev.dv_handle = (uintptr_t)dev;
3614 udev.dv_parent = (uintptr_t)dev->parent;
3615 if (dev->nameunit != NULL)
3616 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3617 if (dev->desc != NULL)
3618 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3619 if (dev->driver != NULL && dev->driver->name != NULL)
3620 strlcpy(udev.dv_drivername, dev->driver->name,
3621 sizeof(udev.dv_drivername));
3622 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3623 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3624 udev.dv_devflags = dev->devflags;
3625 udev.dv_flags = dev->flags;
3626 udev.dv_state = dev->state;
3627 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3631 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3632 "system device tree");
3635 bus_data_generation_check(int generation)
3637 if (generation != bus_data_generation)
3640 /* XXX generate optimised lists here? */
3645 bus_data_generation_update(void)
3647 bus_data_generation++;
3651 intr_str_polarity(enum intr_polarity pola)
3654 case INTR_POLARITY_LOW:
3657 case INTR_POLARITY_HIGH:
3660 case INTR_POLARITY_CONFORM:
3667 intr_str_trigger(enum intr_trigger trig)
3670 case INTR_TRIGGER_EDGE:
3673 case INTR_TRIGGER_LEVEL:
3676 case INTR_TRIGGER_CONFORM:
3683 device_getenv_int(device_t dev, const char *knob, int def)
3687 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3688 kgetenv_int(env, &def);