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>
50 #include <sys/vnode.h>
52 #include <machine/stdarg.h> /* for device_printf() */
54 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
55 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
57 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
60 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
61 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
62 #define DRIVERNAME(d) ((d)? d->name : "no driver")
63 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
65 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
66 * prevent syslog from deleting initial spaces
68 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
70 static void print_device_short(device_t dev, int indent);
71 static void print_device(device_t dev, int indent);
72 void print_device_tree_short(device_t dev, int indent);
73 void print_device_tree(device_t dev, int indent);
74 static void print_driver_short(driver_t *driver, int indent);
75 static void print_driver(driver_t *driver, int indent);
76 static void print_driver_list(driver_list_t drivers, int indent);
77 static void print_devclass_short(devclass_t dc, int indent);
78 static void print_devclass(devclass_t dc, int indent);
79 void print_devclass_list_short(void);
80 void print_devclass_list(void);
83 /* Make the compiler ignore the function calls */
84 #define PDEBUG(a) /* nop */
85 #define DEVICENAME(d) /* nop */
86 #define DRIVERNAME(d) /* nop */
87 #define DEVCLANAME(d) /* nop */
89 #define print_device_short(d,i) /* nop */
90 #define print_device(d,i) /* nop */
91 #define print_device_tree_short(d,i) /* nop */
92 #define print_device_tree(d,i) /* nop */
93 #define print_driver_short(d,i) /* nop */
94 #define print_driver(d,i) /* nop */
95 #define print_driver_list(d,i) /* nop */
96 #define print_devclass_short(d,i) /* nop */
97 #define print_devclass(d,i) /* nop */
98 #define print_devclass_list_short() /* nop */
99 #define print_devclass_list() /* nop */
107 DEVCLASS_SYSCTL_PARENT,
111 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
113 devclass_t dc = (devclass_t)arg1;
117 case DEVCLASS_SYSCTL_PARENT:
118 value = dc->parent ? dc->parent->name : "";
123 return (SYSCTL_OUT(req, value, strlen(value)));
127 devclass_sysctl_init(devclass_t dc)
130 if (dc->sysctl_tree != NULL)
132 sysctl_ctx_init(&dc->sysctl_ctx);
133 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
134 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
135 CTLFLAG_RD, NULL, "");
136 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
137 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
138 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
144 DEVICE_SYSCTL_DRIVER,
145 DEVICE_SYSCTL_LOCATION,
146 DEVICE_SYSCTL_PNPINFO,
147 DEVICE_SYSCTL_PARENT,
151 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
153 device_t dev = (device_t)arg1;
160 case DEVICE_SYSCTL_DESC:
161 value = dev->desc ? dev->desc : "";
163 case DEVICE_SYSCTL_DRIVER:
164 value = dev->driver ? dev->driver->name : "";
166 case DEVICE_SYSCTL_LOCATION:
167 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
168 bus_child_location_str(dev, buf, 1024);
170 case DEVICE_SYSCTL_PNPINFO:
171 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
172 bus_child_pnpinfo_str(dev, buf, 1024);
174 case DEVICE_SYSCTL_PARENT:
175 value = dev->parent ? dev->parent->nameunit : "";
180 error = SYSCTL_OUT(req, value, strlen(value));
187 device_sysctl_init(device_t dev)
189 devclass_t dc = dev->devclass;
191 if (dev->sysctl_tree != NULL)
193 devclass_sysctl_init(dc);
194 sysctl_ctx_init(&dev->sysctl_ctx);
195 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
196 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
197 dev->nameunit + strlen(dc->name),
198 CTLFLAG_RD, NULL, "");
199 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
200 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
201 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
202 "device description");
203 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
204 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
205 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
206 "device driver name");
207 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
208 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
209 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
210 "device location relative to parent");
211 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
212 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
213 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
214 "device identification");
215 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
216 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
217 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
222 device_sysctl_update(device_t dev)
224 devclass_t dc = dev->devclass;
226 if (dev->sysctl_tree == NULL)
228 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
232 device_sysctl_fini(device_t dev)
234 if (dev->sysctl_tree == NULL)
236 sysctl_ctx_free(&dev->sysctl_ctx);
237 dev->sysctl_tree = NULL;
240 static void device_attach_async(device_t dev);
241 static void device_attach_thread(void *arg);
242 static int device_doattach(device_t dev);
244 static int do_async_attach = 0;
245 static int numasyncthreads;
246 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
249 * /dev/devctl implementation
253 * This design allows only one reader for /dev/devctl. This is not desirable
254 * in the long run, but will get a lot of hair out of this implementation.
255 * Maybe we should make this device a clonable device.
257 * Also note: we specifically do not attach a device to the device_t tree
258 * to avoid potential chicken and egg problems. One could argue that all
259 * of this belongs to the root node. One could also further argue that the
260 * sysctl interface that we have not might more properly be an ioctl
261 * interface, but at this stage of the game, I'm not inclined to rock that
264 * I'm also not sure that the SIGIO support is done correctly or not, as
265 * I copied it from a driver that had SIGIO support that likely hasn't been
266 * tested since 3.4 or 2.2.8!
269 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
270 static int devctl_disable = 0;
271 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
272 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
273 sysctl_devctl_disable, "I", "devctl disable");
275 static d_open_t devopen;
276 static d_close_t devclose;
277 static d_read_t devread;
278 static d_ioctl_t devioctl;
279 static d_kqfilter_t devkqfilter;
281 static struct dev_ops devctl_ops = {
282 { "devctl", 0, D_MPSAFE },
287 .d_kqfilter = devkqfilter
290 struct dev_event_info
293 TAILQ_ENTRY(dev_event_info) dei_link;
296 TAILQ_HEAD(devq, dev_event_info);
298 static struct dev_softc
304 struct proc *async_proc;
308 * Chicken-and-egg problem with devfs, get the queue operational early.
313 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
314 TAILQ_INIT(&devsoftc.devq);
316 SYSINIT(predevinit, SI_SUB_CREATE_INIT, SI_ORDER_ANY, predevinit, 0);
322 * WARNING! make_dev() can call back into devctl_queue_data()
325 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
329 devopen(struct dev_open_args *ap)
331 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
332 if (devsoftc.inuse) {
333 lockmgr(&devsoftc.lock, LK_RELEASE);
338 devsoftc.async_proc = NULL;
339 lockmgr(&devsoftc.lock, LK_RELEASE);
345 devclose(struct dev_close_args *ap)
347 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
350 lockmgr(&devsoftc.lock, LK_RELEASE);
356 * The read channel for this device is used to report changes to
357 * userland in realtime. We are required to free the data as well as
358 * the n1 object because we allocate them separately. Also note that
359 * we return one record at a time. If you try to read this device a
360 * character at a time, you will lose the rest of the data. Listening
361 * programs are expected to cope.
364 devread(struct dev_read_args *ap)
366 struct uio *uio = ap->a_uio;
367 struct dev_event_info *n1;
370 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
371 while (TAILQ_EMPTY(&devsoftc.devq)) {
372 if (ap->a_ioflag & IO_NDELAY) {
373 lockmgr(&devsoftc.lock, LK_RELEASE);
376 tsleep_interlock(&devsoftc, PCATCH);
377 lockmgr(&devsoftc.lock, LK_RELEASE);
378 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
379 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
382 * Need to translate ERESTART to EINTR here? -- jake
384 lockmgr(&devsoftc.lock, LK_RELEASE);
388 n1 = TAILQ_FIRST(&devsoftc.devq);
389 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
390 lockmgr(&devsoftc.lock, LK_RELEASE);
391 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
392 kfree(n1->dei_data, M_BUS);
398 devioctl(struct dev_ioctl_args *ap)
405 if (*(int*)ap->a_data)
406 devsoftc.async_proc = curproc;
408 devsoftc.async_proc = NULL;
411 /* (un)Support for other fcntl() calls. */
423 static void dev_filter_detach(struct knote *);
424 static int dev_filter_read(struct knote *, long);
426 static struct filterops dev_filtops =
427 { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
428 dev_filter_detach, dev_filter_read };
431 devkqfilter(struct dev_kqfilter_args *ap)
433 struct knote *kn = ap->a_kn;
437 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
439 switch (kn->kn_filter) {
441 kn->kn_fop = &dev_filtops;
444 ap->a_result = EOPNOTSUPP;
445 lockmgr(&devsoftc.lock, LK_RELEASE);
449 klist = &devsoftc.kq.ki_note;
450 knote_insert(klist, kn);
452 lockmgr(&devsoftc.lock, LK_RELEASE);
458 dev_filter_detach(struct knote *kn)
462 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
463 klist = &devsoftc.kq.ki_note;
464 knote_remove(klist, kn);
465 lockmgr(&devsoftc.lock, LK_RELEASE);
469 dev_filter_read(struct knote *kn, long hint)
473 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
474 if (!TAILQ_EMPTY(&devsoftc.devq))
476 lockmgr(&devsoftc.lock, LK_RELEASE);
483 * @brief Return whether the userland process is running
486 devctl_process_running(void)
488 return (devsoftc.inuse == 1);
492 * @brief Queue data to be read from the devctl device
494 * Generic interface to queue data to the devctl device. It is
495 * assumed that @p data is properly formatted. It is further assumed
496 * that @p data is allocated using the M_BUS malloc type.
499 devctl_queue_data(char *data)
501 struct dev_event_info *n1 = NULL;
504 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
508 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
509 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
511 lockmgr(&devsoftc.lock, LK_RELEASE);
512 KNOTE(&devsoftc.kq.ki_note, 0);
513 p = devsoftc.async_proc;
519 * @brief Send a 'notification' to userland, using standard ways
522 devctl_notify(const char *system, const char *subsystem, const char *type,
529 return; /* BOGUS! Must specify system. */
530 if (subsystem == NULL)
531 return; /* BOGUS! Must specify subsystem. */
533 return; /* BOGUS! Must specify type. */
534 len += strlen(" system=") + strlen(system);
535 len += strlen(" subsystem=") + strlen(subsystem);
536 len += strlen(" type=") + strlen(type);
537 /* add in the data message plus newline. */
540 len += 3; /* '!', '\n', and NUL */
541 msg = kmalloc(len, M_BUS, M_NOWAIT);
543 return; /* Drop it on the floor */
545 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
546 system, subsystem, type, data);
548 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
549 system, subsystem, type);
550 devctl_queue_data(msg);
554 * Common routine that tries to make sending messages as easy as possible.
555 * We allocate memory for the data, copy strings into that, but do not
556 * free it unless there's an error. The dequeue part of the driver should
557 * free the data. We don't send data when the device is disabled. We do
558 * send data, even when we have no listeners, because we wish to avoid
559 * races relating to startup and restart of listening applications.
561 * devaddq is designed to string together the type of event, with the
562 * object of that event, plus the plug and play info and location info
563 * for that event. This is likely most useful for devices, but less
564 * useful for other consumers of this interface. Those should use
565 * the devctl_queue_data() interface instead.
568 devaddq(const char *type, const char *what, device_t dev)
577 data = kmalloc(1024, M_BUS, M_NOWAIT);
581 /* get the bus specific location of this device */
582 loc = kmalloc(1024, M_BUS, M_NOWAIT);
586 bus_child_location_str(dev, loc, 1024);
588 /* Get the bus specific pnp info of this device */
589 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
593 bus_child_pnpinfo_str(dev, pnp, 1024);
595 /* Get the parent of this device, or / if high enough in the tree. */
596 if (device_get_parent(dev) == NULL)
597 parstr = "."; /* Or '/' ? */
599 parstr = device_get_nameunit(device_get_parent(dev));
600 /* String it all together. */
601 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
605 devctl_queue_data(data);
618 * A device was added to the tree. We are called just after it successfully
619 * attaches (that is, probe and attach success for this device). No call
620 * is made if a device is merely parented into the tree. See devnomatch
621 * if probe fails. If attach fails, no notification is sent (but maybe
622 * we should have a different message for this).
625 devadded(device_t dev)
630 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
633 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
637 bus_child_pnpinfo_str(dev, pnp, 1024);
638 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
639 devaddq("+", tmp, dev);
649 * A device was removed from the tree. We are called just before this
653 devremoved(device_t dev)
658 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
661 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
665 bus_child_pnpinfo_str(dev, pnp, 1024);
666 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
667 devaddq("-", tmp, dev);
677 * Called when there's no match for this device. This is only called
678 * the first time that no match happens, so we don't keep getitng this
679 * message. Should that prove to be undesirable, we can change it.
680 * This is called when all drivers that can attach to a given bus
681 * decline to accept this device. Other errrors may not be detected.
684 devnomatch(device_t dev)
686 devaddq("?", "", dev);
690 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
692 struct dev_event_info *n1;
695 dis = devctl_disable;
696 error = sysctl_handle_int(oidp, &dis, 0, req);
697 if (error || !req->newptr)
699 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
700 devctl_disable = dis;
702 while (!TAILQ_EMPTY(&devsoftc.devq)) {
703 n1 = TAILQ_FIRST(&devsoftc.devq);
704 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
705 kfree(n1->dei_data, M_BUS);
709 lockmgr(&devsoftc.lock, LK_RELEASE);
713 /* End of /dev/devctl code */
715 TAILQ_HEAD(,bsd_device) bus_data_devices;
716 static int bus_data_generation = 1;
718 kobj_method_t null_methods[] = {
722 DEFINE_CLASS(null, null_methods, 0);
725 * Devclass implementation
728 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
731 devclass_find_internal(const char *classname, const char *parentname,
736 PDEBUG(("looking for %s", classname));
737 if (classname == NULL)
740 TAILQ_FOREACH(dc, &devclasses, link)
741 if (!strcmp(dc->name, classname))
745 PDEBUG(("creating %s", classname));
746 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
747 M_BUS, M_INTWAIT | M_ZERO);
749 dc->name = (char*) (dc + 1);
750 strcpy(dc->name, classname);
753 TAILQ_INIT(&dc->drivers);
754 TAILQ_INSERT_TAIL(&devclasses, dc, link);
756 bus_data_generation_update();
761 * If a parent class is specified, then set that as our parent so
762 * that this devclass will support drivers for the parent class as
763 * well. If the parent class has the same name don't do this though
764 * as it creates a cycle that can trigger an infinite loop in
765 * device_probe_child() if a device exists for which there is no
768 if (parentname && dc && !dc->parent &&
769 strcmp(classname, parentname) != 0)
770 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
776 devclass_create(const char *classname)
778 return(devclass_find_internal(classname, NULL, TRUE));
782 devclass_find(const char *classname)
784 return(devclass_find_internal(classname, NULL, FALSE));
788 devclass_find_unit(const char *classname, int unit)
792 if ((dc = devclass_find(classname)) != NULL)
793 return(devclass_get_device(dc, unit));
798 devclass_add_driver(devclass_t dc, driver_t *driver)
804 PDEBUG(("%s", DRIVERNAME(driver)));
806 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
809 * Compile the driver's methods. Also increase the reference count
810 * so that the class doesn't get freed when the last instance
811 * goes. This means we can safely use static methods and avoids a
812 * double-free in devclass_delete_driver.
814 kobj_class_instantiate(driver);
817 * Make sure the devclass which the driver is implementing exists.
819 devclass_find_internal(driver->name, NULL, TRUE);
822 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
825 * Call BUS_DRIVER_ADDED for any existing busses in this class,
826 * but only if the bus has already been attached (otherwise we
827 * might probe too early).
829 * This is what will cause a newly loaded module to be associated
830 * with hardware. bus_generic_driver_added() is typically what ends
833 for (i = 0; i < dc->maxunit; i++) {
834 if ((dev = dc->devices[i]) != NULL) {
835 if (dev->state >= DS_ATTACHED)
836 BUS_DRIVER_ADDED(dev, driver);
840 bus_data_generation_update();
845 devclass_delete_driver(devclass_t busclass, driver_t *driver)
847 devclass_t dc = devclass_find(driver->name);
853 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
859 * Find the link structure in the bus' list of drivers.
861 TAILQ_FOREACH(dl, &busclass->drivers, link)
862 if (dl->driver == driver)
866 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
871 * Disassociate from any devices. We iterate through all the
872 * devices in the devclass of the driver and detach any which are
873 * using the driver and which have a parent in the devclass which
874 * we are deleting from.
876 * Note that since a driver can be in multiple devclasses, we
877 * should not detach devices which are not children of devices in
878 * the affected devclass.
880 for (i = 0; i < dc->maxunit; i++)
881 if (dc->devices[i]) {
882 dev = dc->devices[i];
883 if (dev->driver == driver && dev->parent &&
884 dev->parent->devclass == busclass) {
885 if ((error = device_detach(dev)) != 0)
887 device_set_driver(dev, NULL);
891 TAILQ_REMOVE(&busclass->drivers, dl, link);
894 kobj_class_uninstantiate(driver);
896 bus_data_generation_update();
901 devclass_find_driver_internal(devclass_t dc, const char *classname)
905 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
907 TAILQ_FOREACH(dl, &dc->drivers, link)
908 if (!strcmp(dl->driver->name, classname))
911 PDEBUG(("not found"));
916 devclass_find_driver(devclass_t dc, const char *classname)
920 dl = devclass_find_driver_internal(dc, classname);
928 devclass_get_name(devclass_t dc)
934 devclass_get_device(devclass_t dc, int unit)
936 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
938 return(dc->devices[unit]);
942 devclass_get_softc(devclass_t dc, int unit)
946 dev = devclass_get_device(dc, unit);
950 return(device_get_softc(dev));
954 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
961 for (i = 0; i < dc->maxunit; i++)
965 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
968 for (i = 0; i < dc->maxunit; i++)
969 if (dc->devices[i]) {
970 list[count] = dc->devices[i];
981 * @brief Get a list of drivers in the devclass
983 * An array containing a list of pointers to all the drivers in the
984 * given devclass is allocated and returned in @p *listp. The number
985 * of drivers in the array is returned in @p *countp. The caller should
986 * free the array using @c free(p, M_TEMP).
988 * @param dc the devclass to examine
989 * @param listp gives location for array pointer return value
990 * @param countp gives location for number of array elements
994 * @retval ENOMEM the array allocation failed
997 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
1004 TAILQ_FOREACH(dl, &dc->drivers, link)
1006 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1011 TAILQ_FOREACH(dl, &dc->drivers, link) {
1012 list[count] = dl->driver;
1022 * @brief Get the number of devices in a devclass
1024 * @param dc the devclass to examine
1027 devclass_get_count(devclass_t dc)
1032 for (i = 0; i < dc->maxunit; i++)
1039 devclass_get_maxunit(devclass_t dc)
1041 return(dc->maxunit);
1045 devclass_set_parent(devclass_t dc, devclass_t pdc)
1051 devclass_get_parent(devclass_t dc)
1057 devclass_alloc_unit(devclass_t dc, int *unitp)
1061 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1063 /* If we have been given a wired unit number, check for existing device */
1065 if (unit >= 0 && unit < dc->maxunit &&
1066 dc->devices[unit] != NULL) {
1068 kprintf("%s-: %s%d exists, using next available unit number\n",
1069 dc->name, dc->name, unit);
1070 /* find the next available slot */
1071 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
1075 /* Unwired device, find the next available slot for it */
1077 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1082 * We've selected a unit beyond the length of the table, so let's
1083 * extend the table to make room for all units up to and including
1086 if (unit >= dc->maxunit) {
1090 newsize = (unit + 1);
1091 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
1092 M_INTWAIT | M_ZERO);
1093 if (newlist == NULL)
1096 * WARNING: Due to gcc builtin optimization,
1097 * calling bcopy causes gcc to assume
1098 * that the source and destination args
1099 * cannot be NULL and optimize-away later
1100 * conditional tests to determine if dc->devices
1101 * is NULL. In this situation, in fact,
1102 * dc->devices CAN be NULL w/ maxunit == 0.
1107 sizeof(device_t) * dc->maxunit);
1108 kfree(dc->devices, M_BUS);
1110 dc->devices = newlist;
1111 dc->maxunit = newsize;
1113 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1120 devclass_add_device(devclass_t dc, device_t dev)
1124 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1126 buflen = strlen(dc->name) + 5;
1127 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1128 if (dev->nameunit == NULL)
1131 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1132 kfree(dev->nameunit, M_BUS);
1133 dev->nameunit = NULL;
1136 dc->devices[dev->unit] = dev;
1138 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1144 devclass_delete_device(devclass_t dc, device_t dev)
1149 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1151 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1152 panic("devclass_delete_device: inconsistent device class");
1153 dc->devices[dev->unit] = NULL;
1154 if (dev->flags & DF_WILDCARD)
1156 dev->devclass = NULL;
1157 kfree(dev->nameunit, M_BUS);
1158 dev->nameunit = NULL;
1164 make_device(device_t parent, const char *name, int unit)
1169 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1172 dc = devclass_find_internal(name, NULL, TRUE);
1174 kprintf("make_device: can't find device class %s\n", name);
1180 dev = kmalloc(sizeof(struct bsd_device), M_BUS, M_INTWAIT | M_ZERO);
1184 dev->parent = parent;
1185 TAILQ_INIT(&dev->children);
1186 kobj_init((kobj_t) dev, &null_class);
1188 dev->devclass = NULL;
1190 dev->nameunit = NULL;
1194 dev->flags = DF_ENABLED;
1197 dev->flags |= DF_WILDCARD;
1199 dev->flags |= DF_FIXEDCLASS;
1200 if (devclass_add_device(dc, dev) != 0) {
1201 kobj_delete((kobj_t)dev, M_BUS);
1208 dev->state = DS_NOTPRESENT;
1210 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1211 bus_data_generation_update();
1217 device_print_child(device_t dev, device_t child)
1221 if (device_is_alive(child))
1222 retval += BUS_PRINT_CHILD(dev, child);
1224 retval += device_printf(child, " not found\n");
1230 device_add_child(device_t dev, const char *name, int unit)
1232 return device_add_child_ordered(dev, 0, name, unit);
1236 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1241 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1244 child = make_device(dev, name, unit);
1247 child->order = order;
1249 TAILQ_FOREACH(place, &dev->children, link) {
1250 if (place->order > order)
1256 * The device 'place' is the first device whose order is
1257 * greater than the new child.
1259 TAILQ_INSERT_BEFORE(place, child, link);
1262 * The new child's order is greater or equal to the order of
1263 * any existing device. Add the child to the tail of the list.
1265 TAILQ_INSERT_TAIL(&dev->children, child, link);
1268 bus_data_generation_update();
1273 device_delete_child(device_t dev, device_t child)
1276 device_t grandchild;
1278 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1280 /* remove children first */
1281 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1282 error = device_delete_child(child, grandchild);
1287 if ((error = device_detach(child)) != 0)
1289 if (child->devclass)
1290 devclass_delete_device(child->devclass, child);
1291 TAILQ_REMOVE(&dev->children, child, link);
1292 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1293 kobj_delete((kobj_t)child, M_BUS);
1295 bus_data_generation_update();
1300 * @brief Delete all children devices of the given device, if any.
1302 * This function deletes all children devices of the given device, if
1303 * any, using the device_delete_child() function for each device it
1304 * finds. If a child device cannot be deleted, this function will
1305 * return an error code.
1307 * @param dev the parent device
1310 * @retval non-zero a device would not detach
1313 device_delete_children(device_t dev)
1318 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1322 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1323 error = device_delete_child(dev, child);
1325 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1333 * @brief Find a device given a unit number
1335 * This is similar to devclass_get_devices() but only searches for
1336 * devices which have @p dev as a parent.
1338 * @param dev the parent device to search
1339 * @param unit the unit number to search for. If the unit is -1,
1340 * return the first child of @p dev which has name
1341 * @p classname (that is, the one with the lowest unit.)
1343 * @returns the device with the given unit number or @c
1344 * NULL if there is no such device
1347 device_find_child(device_t dev, const char *classname, int unit)
1352 dc = devclass_find(classname);
1357 child = devclass_get_device(dc, unit);
1358 if (child && child->parent == dev)
1361 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1362 child = devclass_get_device(dc, unit);
1363 if (child && child->parent == dev)
1371 first_matching_driver(devclass_t dc, device_t dev)
1374 return(devclass_find_driver_internal(dc, dev->devclass->name));
1376 return(TAILQ_FIRST(&dc->drivers));
1380 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1382 if (dev->devclass) {
1384 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1385 if (!strcmp(dev->devclass->name, dl->driver->name))
1389 return(TAILQ_NEXT(last, link));
1393 device_probe_child(device_t dev, device_t child)
1396 driverlink_t best = NULL;
1398 int result, pri = 0;
1399 int hasclass = (child->devclass != NULL);
1403 panic("device_probe_child: parent device has no devclass");
1405 if (child->state == DS_ALIVE)
1408 for (; dc; dc = dc->parent) {
1409 for (dl = first_matching_driver(dc, child); dl;
1410 dl = next_matching_driver(dc, child, dl)) {
1411 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1412 device_set_driver(child, dl->driver);
1414 device_set_devclass(child, dl->driver->name);
1415 result = DEVICE_PROBE(child);
1417 device_set_devclass(child, 0);
1420 * If the driver returns SUCCESS, there can be
1421 * no higher match for this device.
1430 * The driver returned an error so it
1431 * certainly doesn't match.
1434 device_set_driver(child, NULL);
1439 * A priority lower than SUCCESS, remember the
1440 * best matching driver. Initialise the value
1441 * of pri for the first match.
1443 if (best == NULL || result > pri) {
1450 * If we have unambiguous match in this devclass,
1451 * don't look in the parent.
1453 if (best && pri == 0)
1458 * If we found a driver, change state and initialise the devclass.
1461 if (!child->devclass)
1462 device_set_devclass(child, best->driver->name);
1463 device_set_driver(child, best->driver);
1466 * A bit bogus. Call the probe method again to make
1467 * sure that we have the right description.
1469 DEVICE_PROBE(child);
1472 bus_data_generation_update();
1473 child->state = DS_ALIVE;
1481 device_probe_child_gpri(device_t dev, device_t child, u_int gpri)
1484 driverlink_t best = NULL;
1486 int result, pri = 0;
1487 int hasclass = (child->devclass != NULL);
1491 panic("device_probe_child: parent device has no devclass");
1493 if (child->state == DS_ALIVE)
1496 for (; dc; dc = dc->parent) {
1497 for (dl = first_matching_driver(dc, child); dl;
1498 dl = next_matching_driver(dc, child, dl)) {
1500 * GPRI handling, only probe drivers with the
1503 if (dl->driver->gpri != gpri)
1506 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1507 device_set_driver(child, dl->driver);
1509 device_set_devclass(child, dl->driver->name);
1510 result = DEVICE_PROBE(child);
1512 device_set_devclass(child, 0);
1515 * If the driver returns SUCCESS, there can be
1516 * no higher match for this device.
1525 * The driver returned an error so it
1526 * certainly doesn't match.
1529 device_set_driver(child, NULL);
1534 * A priority lower than SUCCESS, remember the
1535 * best matching driver. Initialise the value
1536 * of pri for the first match.
1538 if (best == NULL || result > pri) {
1545 * If we have unambiguous match in this devclass,
1546 * don't look in the parent.
1548 if (best && pri == 0)
1553 * If we found a driver, change state and initialise the devclass.
1556 if (!child->devclass)
1557 device_set_devclass(child, best->driver->name);
1558 device_set_driver(child, best->driver);
1561 * A bit bogus. Call the probe method again to make
1562 * sure that we have the right description.
1564 DEVICE_PROBE(child);
1567 bus_data_generation_update();
1568 child->state = DS_ALIVE;
1576 device_get_parent(device_t dev)
1582 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1589 TAILQ_FOREACH(child, &dev->children, link)
1592 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1595 TAILQ_FOREACH(child, &dev->children, link) {
1596 list[count] = child;
1607 device_get_driver(device_t dev)
1609 return(dev->driver);
1613 device_get_devclass(device_t dev)
1615 return(dev->devclass);
1619 device_get_name(device_t dev)
1622 return devclass_get_name(dev->devclass);
1627 device_get_nameunit(device_t dev)
1629 return(dev->nameunit);
1633 device_get_unit(device_t dev)
1639 device_get_desc(device_t dev)
1645 device_get_flags(device_t dev)
1647 return(dev->devflags);
1650 struct sysctl_ctx_list *
1651 device_get_sysctl_ctx(device_t dev)
1653 return (&dev->sysctl_ctx);
1657 device_get_sysctl_tree(device_t dev)
1659 return (dev->sysctl_tree);
1663 device_print_prettyname(device_t dev)
1665 const char *name = device_get_name(dev);
1668 return kprintf("unknown: ");
1670 return kprintf("%s%d: ", name, device_get_unit(dev));
1674 device_printf(device_t dev, const char * fmt, ...)
1679 retval = device_print_prettyname(dev);
1680 __va_start(ap, fmt);
1681 retval += kvprintf(fmt, ap);
1687 device_set_desc_internal(device_t dev, const char* desc, int copy)
1689 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1690 kfree(dev->desc, M_BUS);
1691 dev->flags &= ~DF_DESCMALLOCED;
1696 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1698 strcpy(dev->desc, desc);
1699 dev->flags |= DF_DESCMALLOCED;
1702 /* Avoid a -Wcast-qual warning */
1703 dev->desc = (char *)(uintptr_t) desc;
1706 bus_data_generation_update();
1710 device_set_desc(device_t dev, const char* desc)
1712 device_set_desc_internal(dev, desc, FALSE);
1716 device_set_desc_copy(device_t dev, const char* desc)
1718 device_set_desc_internal(dev, desc, TRUE);
1722 device_set_flags(device_t dev, uint32_t flags)
1724 dev->devflags = flags;
1728 device_get_softc(device_t dev)
1734 device_set_softc(device_t dev, void *softc)
1736 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1737 kfree(dev->softc, M_BUS);
1740 dev->flags |= DF_EXTERNALSOFTC;
1742 dev->flags &= ~DF_EXTERNALSOFTC;
1746 device_set_async_attach(device_t dev, int enable)
1749 dev->flags |= DF_ASYNCPROBE;
1751 dev->flags &= ~DF_ASYNCPROBE;
1755 device_get_ivars(device_t dev)
1761 device_set_ivars(device_t dev, void * ivars)
1770 device_get_state(device_t dev)
1776 device_enable(device_t dev)
1778 dev->flags |= DF_ENABLED;
1782 device_disable(device_t dev)
1784 dev->flags &= ~DF_ENABLED;
1791 device_busy(device_t dev)
1793 if (dev->state < DS_ATTACHED)
1794 panic("device_busy: called for unattached device");
1795 if (dev->busy == 0 && dev->parent)
1796 device_busy(dev->parent);
1798 dev->state = DS_BUSY;
1805 device_unbusy(device_t dev)
1807 if (dev->state != DS_BUSY)
1808 panic("device_unbusy: called for non-busy device");
1810 if (dev->busy == 0) {
1812 device_unbusy(dev->parent);
1813 dev->state = DS_ATTACHED;
1818 device_quiet(device_t dev)
1820 dev->flags |= DF_QUIET;
1824 device_verbose(device_t dev)
1826 dev->flags &= ~DF_QUIET;
1830 device_is_quiet(device_t dev)
1832 return((dev->flags & DF_QUIET) != 0);
1836 device_is_enabled(device_t dev)
1838 return((dev->flags & DF_ENABLED) != 0);
1842 device_is_alive(device_t dev)
1844 return(dev->state >= DS_ALIVE);
1848 device_is_attached(device_t dev)
1850 return(dev->state >= DS_ATTACHED);
1854 device_set_devclass(device_t dev, const char *classname)
1861 devclass_delete_device(dev->devclass, dev);
1865 if (dev->devclass) {
1866 kprintf("device_set_devclass: device class already set\n");
1870 dc = devclass_find_internal(classname, NULL, TRUE);
1874 error = devclass_add_device(dc, dev);
1876 bus_data_generation_update();
1881 device_set_driver(device_t dev, driver_t *driver)
1883 if (dev->state >= DS_ATTACHED)
1886 if (dev->driver == driver)
1889 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1890 kfree(dev->softc, M_BUS);
1893 device_set_desc(dev, NULL);
1894 kobj_delete((kobj_t) dev, 0);
1895 dev->driver = driver;
1897 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1898 if (!(dev->flags & DF_EXTERNALSOFTC))
1899 dev->softc = kmalloc(driver->size, M_BUS,
1900 M_INTWAIT | M_ZERO);
1902 kobj_init((kobj_t) dev, &null_class);
1905 bus_data_generation_update();
1910 device_probe_and_attach(device_t dev)
1912 device_t bus = dev->parent;
1915 if (dev->state >= DS_ALIVE)
1918 if ((dev->flags & DF_ENABLED) == 0) {
1920 device_print_prettyname(dev);
1921 kprintf("not probed (disabled)\n");
1926 error = device_probe_child(bus, dev);
1928 if (!(dev->flags & DF_DONENOMATCH)) {
1929 BUS_PROBE_NOMATCH(bus, dev);
1931 dev->flags |= DF_DONENOMATCH;
1937 * Output the exact device chain prior to the attach in case the
1938 * system locks up during attach, and generate the full info after
1939 * the attach so correct irq and other information is displayed.
1941 if (bootverbose && !device_is_quiet(dev)) {
1944 kprintf("%s", device_get_nameunit(dev));
1945 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1946 kprintf(".%s", device_get_nameunit(tmp));
1949 if (!device_is_quiet(dev))
1950 device_print_child(bus, dev);
1951 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1952 kprintf("%s: probing asynchronously\n",
1953 device_get_nameunit(dev));
1954 dev->state = DS_INPROGRESS;
1955 device_attach_async(dev);
1958 error = device_doattach(dev);
1964 device_probe_and_attach_gpri(device_t dev, u_int gpri)
1966 device_t bus = dev->parent;
1969 if (dev->state >= DS_ALIVE)
1972 if ((dev->flags & DF_ENABLED) == 0) {
1974 device_print_prettyname(dev);
1975 kprintf("not probed (disabled)\n");
1980 error = device_probe_child_gpri(bus, dev, gpri);
1983 if (!(dev->flags & DF_DONENOMATCH)) {
1984 BUS_PROBE_NOMATCH(bus, dev);
1986 dev->flags |= DF_DONENOMATCH;
1993 * Output the exact device chain prior to the attach in case the
1994 * system locks up during attach, and generate the full info after
1995 * the attach so correct irq and other information is displayed.
1997 if (bootverbose && !device_is_quiet(dev)) {
2000 kprintf("%s", device_get_nameunit(dev));
2001 for (tmp = dev->parent; tmp; tmp = tmp->parent)
2002 kprintf(".%s", device_get_nameunit(tmp));
2005 if (!device_is_quiet(dev))
2006 device_print_child(bus, dev);
2007 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
2008 kprintf("%s: probing asynchronously\n",
2009 device_get_nameunit(dev));
2010 dev->state = DS_INPROGRESS;
2011 device_attach_async(dev);
2014 error = device_doattach(dev);
2020 * Device is known to be alive, do the attach asynchronously.
2021 * However, serialize the attaches with the mp lock.
2024 device_attach_async(device_t dev)
2028 atomic_add_int(&numasyncthreads, 1);
2029 lwkt_create(device_attach_thread, dev, &td, NULL,
2030 0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
2034 device_attach_thread(void *arg)
2038 (void)device_doattach(dev);
2039 atomic_subtract_int(&numasyncthreads, 1);
2040 wakeup(&numasyncthreads);
2044 * Device is known to be alive, do the attach (synchronous or asynchronous)
2047 device_doattach(device_t dev)
2049 device_t bus = dev->parent;
2050 int hasclass = (dev->devclass != NULL);
2053 device_sysctl_init(dev);
2054 error = DEVICE_ATTACH(dev);
2056 dev->state = DS_ATTACHED;
2057 if (bootverbose && !device_is_quiet(dev))
2058 device_print_child(bus, dev);
2059 device_sysctl_update(dev);
2062 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
2063 dev->driver->name, dev->unit, error);
2064 /* Unset the class that was set in device_probe_child */
2066 device_set_devclass(dev, 0);
2067 device_set_driver(dev, NULL);
2068 dev->state = DS_NOTPRESENT;
2069 device_sysctl_fini(dev);
2075 device_detach(device_t dev)
2079 PDEBUG(("%s", DEVICENAME(dev)));
2080 if (dev->state == DS_BUSY)
2082 if (dev->state != DS_ATTACHED)
2085 if ((error = DEVICE_DETACH(dev)) != 0)
2088 device_printf(dev, "detached\n");
2090 BUS_CHILD_DETACHED(dev->parent, dev);
2092 if (!(dev->flags & DF_FIXEDCLASS))
2093 devclass_delete_device(dev->devclass, dev);
2095 dev->state = DS_NOTPRESENT;
2096 device_set_driver(dev, NULL);
2097 device_sysctl_fini(dev);
2103 device_shutdown(device_t dev)
2105 if (dev->state < DS_ATTACHED)
2107 PDEBUG(("%s", DEVICENAME(dev)));
2108 return DEVICE_SHUTDOWN(dev);
2112 device_set_unit(device_t dev, int unit)
2117 dc = device_get_devclass(dev);
2118 if (unit < dc->maxunit && dc->devices[unit])
2120 err = devclass_delete_device(dc, dev);
2124 err = devclass_add_device(dc, dev);
2128 bus_data_generation_update();
2132 /*======================================*/
2134 * Access functions for device resources.
2137 /* Supplied by config(8) in ioconf.c */
2138 extern struct config_device config_devtab[];
2139 extern int devtab_count;
2141 /* Runtime version */
2142 struct config_device *devtab = config_devtab;
2145 resource_new_name(const char *name, int unit)
2147 struct config_device *new;
2149 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
2150 M_INTWAIT | M_ZERO);
2151 if (devtab && devtab_count > 0)
2152 bcopy(devtab, new, devtab_count * sizeof(*new));
2153 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
2154 if (new[devtab_count].name == NULL) {
2158 strcpy(new[devtab_count].name, name);
2159 new[devtab_count].unit = unit;
2160 new[devtab_count].resource_count = 0;
2161 new[devtab_count].resources = NULL;
2162 if (devtab && devtab != config_devtab)
2163 kfree(devtab, M_TEMP);
2165 return devtab_count++;
2169 resource_new_resname(int j, const char *resname, resource_type type)
2171 struct config_resource *new;
2174 i = devtab[j].resource_count;
2175 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
2176 if (devtab[j].resources && i > 0)
2177 bcopy(devtab[j].resources, new, i * sizeof(*new));
2178 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
2179 if (new[i].name == NULL) {
2183 strcpy(new[i].name, resname);
2185 if (devtab[j].resources)
2186 kfree(devtab[j].resources, M_TEMP);
2187 devtab[j].resources = new;
2188 devtab[j].resource_count = i + 1;
2193 resource_match_string(int i, const char *resname, const char *value)
2196 struct config_resource *res;
2198 for (j = 0, res = devtab[i].resources;
2199 j < devtab[i].resource_count; j++, res++)
2200 if (!strcmp(res->name, resname)
2201 && res->type == RES_STRING
2202 && !strcmp(res->u.stringval, value))
2208 resource_find(const char *name, int unit, const char *resname,
2209 struct config_resource **result)
2212 struct config_resource *res;
2215 * First check specific instances, then generic.
2217 for (i = 0; i < devtab_count; i++) {
2218 if (devtab[i].unit < 0)
2220 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2221 res = devtab[i].resources;
2222 for (j = 0; j < devtab[i].resource_count; j++, res++)
2223 if (!strcmp(res->name, resname)) {
2229 for (i = 0; i < devtab_count; i++) {
2230 if (devtab[i].unit >= 0)
2232 /* XXX should this `&& devtab[i].unit == unit' be here? */
2233 /* XXX if so, then the generic match does nothing */
2234 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2235 res = devtab[i].resources;
2236 for (j = 0; j < devtab[i].resource_count; j++, res++)
2237 if (!strcmp(res->name, resname)) {
2247 resource_kenv(const char *name, int unit, const char *resname, long *result)
2253 * DragonFly style loader.conf hinting
2255 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2256 if ((env = kgetenv(buf)) != NULL) {
2257 *result = strtol(env, NULL, 0);
2262 * Also support FreeBSD style loader.conf hinting
2264 ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2265 if ((env = kgetenv(buf)) != NULL) {
2266 *result = strtol(env, NULL, 0);
2274 resource_int_value(const char *name, int unit, const char *resname, int *result)
2276 struct config_resource *res;
2280 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2281 *result = (int)kvalue;
2284 if ((error = resource_find(name, unit, resname, &res)) != 0)
2286 if (res->type != RES_INT)
2288 *result = res->u.intval;
2293 resource_long_value(const char *name, int unit, const char *resname,
2296 struct config_resource *res;
2300 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2304 if ((error = resource_find(name, unit, resname, &res)) != 0)
2306 if (res->type != RES_LONG)
2308 *result = res->u.longval;
2313 resource_string_value(const char *name, int unit, const char *resname,
2314 const char **result)
2317 struct config_resource *res;
2322 * DragonFly style loader.conf hinting
2324 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2325 if ((env = kgetenv(buf)) != NULL) {
2331 * Also support FreeBSD style loader.conf hinting
2333 ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2334 if ((env = kgetenv(buf)) != NULL) {
2339 if ((error = resource_find(name, unit, resname, &res)) != 0)
2341 if (res->type != RES_STRING)
2343 *result = res->u.stringval;
2348 resource_query_string(int i, const char *resname, const char *value)
2354 for (; i < devtab_count; i++)
2355 if (resource_match_string(i, resname, value) >= 0)
2361 resource_locate(int i, const char *resname)
2367 for (; i < devtab_count; i++)
2368 if (!strcmp(devtab[i].name, resname))
2374 resource_count(void)
2376 return(devtab_count);
2380 resource_query_name(int i)
2382 return(devtab[i].name);
2386 resource_query_unit(int i)
2388 return(devtab[i].unit);
2392 resource_create(const char *name, int unit, const char *resname,
2393 resource_type type, struct config_resource **result)
2396 struct config_resource *res = NULL;
2398 for (i = 0; i < devtab_count; i++)
2399 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2400 res = devtab[i].resources;
2404 i = resource_new_name(name, unit);
2407 res = devtab[i].resources;
2409 for (j = 0; j < devtab[i].resource_count; j++, res++)
2410 if (!strcmp(res->name, resname)) {
2414 j = resource_new_resname(i, resname, type);
2417 res = &devtab[i].resources[j];
2423 resource_set_int(const char *name, int unit, const char *resname, int value)
2426 struct config_resource *res;
2428 error = resource_create(name, unit, resname, RES_INT, &res);
2431 if (res->type != RES_INT)
2433 res->u.intval = value;
2438 resource_set_long(const char *name, int unit, const char *resname, long value)
2441 struct config_resource *res;
2443 error = resource_create(name, unit, resname, RES_LONG, &res);
2446 if (res->type != RES_LONG)
2448 res->u.longval = value;
2453 resource_set_string(const char *name, int unit, const char *resname,
2457 struct config_resource *res;
2459 error = resource_create(name, unit, resname, RES_STRING, &res);
2462 if (res->type != RES_STRING)
2464 if (res->u.stringval)
2465 kfree(res->u.stringval, M_TEMP);
2466 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2467 if (res->u.stringval == NULL)
2469 strcpy(res->u.stringval, value);
2474 resource_cfgload(void *dummy __unused)
2476 struct config_resource *res, *cfgres;
2479 char *name, *resname;
2483 int config_devtab_count;
2485 config_devtab_count = devtab_count;
2489 for (i = 0; i < config_devtab_count; i++) {
2490 name = config_devtab[i].name;
2491 unit = config_devtab[i].unit;
2493 for (j = 0; j < config_devtab[i].resource_count; j++) {
2494 cfgres = config_devtab[i].resources;
2495 resname = cfgres[j].name;
2496 type = cfgres[j].type;
2497 error = resource_create(name, unit, resname, type,
2500 kprintf("create resource %s%d: error %d\n",
2504 if (res->type != type) {
2505 kprintf("type mismatch %s%d: %d != %d\n",
2506 name, unit, res->type, type);
2511 res->u.intval = cfgres[j].u.intval;
2514 res->u.longval = cfgres[j].u.longval;
2517 if (res->u.stringval)
2518 kfree(res->u.stringval, M_TEMP);
2519 stringval = cfgres[j].u.stringval;
2520 res->u.stringval = kmalloc(strlen(stringval) + 1,
2522 if (res->u.stringval == NULL)
2524 strcpy(res->u.stringval, stringval);
2527 panic("unknown resource type %d", type);
2532 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0);
2535 /*======================================*/
2537 * Some useful method implementations to make life easier for bus drivers.
2541 resource_list_init(struct resource_list *rl)
2547 resource_list_free(struct resource_list *rl)
2549 struct resource_list_entry *rle;
2551 while ((rle = SLIST_FIRST(rl)) != NULL) {
2553 panic("resource_list_free: resource entry is busy");
2554 SLIST_REMOVE_HEAD(rl, link);
2560 resource_list_add(struct resource_list *rl, int type, int rid,
2561 u_long start, u_long end, u_long count, int cpuid)
2563 struct resource_list_entry *rle;
2565 rle = resource_list_find(rl, type, rid);
2567 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2569 SLIST_INSERT_HEAD(rl, rle, link);
2577 panic("resource_list_add: resource entry is busy");
2584 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2585 panic("resource_list_add: moving from cpu%d -> cpu%d",
2592 struct resource_list_entry*
2593 resource_list_find(struct resource_list *rl,
2596 struct resource_list_entry *rle;
2598 SLIST_FOREACH(rle, rl, link)
2599 if (rle->type == type && rle->rid == rid)
2605 resource_list_delete(struct resource_list *rl,
2608 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2611 if (rle->res != NULL)
2612 panic("resource_list_delete: resource has not been released");
2613 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2619 resource_list_alloc(struct resource_list *rl,
2620 device_t bus, device_t child,
2622 u_long start, u_long end,
2623 u_long count, u_int flags, int cpuid)
2625 struct resource_list_entry *rle = NULL;
2626 int passthrough = (device_get_parent(child) != bus);
2627 int isdefault = (start == 0UL && end == ~0UL);
2630 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2632 start, end, count, flags, cpuid));
2635 rle = resource_list_find(rl, type, *rid);
2638 return(0); /* no resource of that type/rid */
2641 panic("resource_list_alloc: resource entry is busy");
2645 count = max(count, rle->count);
2646 end = max(rle->end, start + count - 1);
2650 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2651 type, rid, start, end, count,
2655 * Record the new range.
2658 rle->start = rman_get_start(rle->res);
2659 rle->end = rman_get_end(rle->res);
2667 resource_list_release(struct resource_list *rl,
2668 device_t bus, device_t child,
2669 int type, int rid, struct resource *res)
2671 struct resource_list_entry *rle = NULL;
2672 int passthrough = (device_get_parent(child) != bus);
2676 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2680 rle = resource_list_find(rl, type, rid);
2683 panic("resource_list_release: can't find resource");
2685 panic("resource_list_release: resource entry is not busy");
2687 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2697 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2700 struct resource_list_entry *rle;
2701 int printed, retval;
2705 /* Yes, this is kinda cheating */
2706 SLIST_FOREACH(rle, rl, link) {
2707 if (rle->type == type) {
2709 retval += kprintf(" %s ", name);
2711 retval += kprintf(",");
2713 retval += kprintf(format, rle->start);
2714 if (rle->count > 1) {
2715 retval += kprintf("-");
2716 retval += kprintf(format, rle->start +
2725 * Generic driver/device identify functions. These will install a device
2726 * rendezvous point under the parent using the same name as the driver
2727 * name, which will at a later time be probed and attached.
2729 * These functions are used when the parent does not 'scan' its bus for
2730 * matching devices, or for the particular devices using these functions,
2731 * or when the device is a pseudo or synthesized device (such as can be
2732 * found under firewire and ppbus).
2735 bus_generic_identify(driver_t *driver, device_t parent)
2737 if (parent->state == DS_ATTACHED)
2739 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2744 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2746 if (parent->state == DS_ATTACHED)
2748 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2753 * Call DEVICE_IDENTIFY for each driver.
2756 bus_generic_probe(device_t dev)
2758 devclass_t dc = dev->devclass;
2761 TAILQ_FOREACH(dl, &dc->drivers, link) {
2762 DEVICE_IDENTIFY(dl->driver, dev);
2769 * This is an aweful hack due to the isa bus and autoconf code not
2770 * probing the ISA devices until after everything else has configured.
2771 * The ISA bus did a dummy attach long ago so we have to set it back
2772 * to an earlier state so the probe thinks its the initial probe and
2775 * XXX remove by properly defering the ISA bus scan.
2778 bus_generic_probe_hack(device_t dev)
2780 if (dev->state == DS_ATTACHED) {
2781 dev->state = DS_ALIVE;
2782 bus_generic_probe(dev);
2783 dev->state = DS_ATTACHED;
2789 bus_generic_attach(device_t dev)
2793 TAILQ_FOREACH(child, &dev->children, link) {
2794 device_probe_and_attach(child);
2801 bus_generic_attach_gpri(device_t dev, u_int gpri)
2805 TAILQ_FOREACH(child, &dev->children, link) {
2806 device_probe_and_attach_gpri(child, gpri);
2813 bus_generic_detach(device_t dev)
2818 if (dev->state != DS_ATTACHED)
2821 TAILQ_FOREACH(child, &dev->children, link)
2822 if ((error = device_detach(child)) != 0)
2829 bus_generic_shutdown(device_t dev)
2833 TAILQ_FOREACH(child, &dev->children, link)
2834 device_shutdown(child);
2840 bus_generic_suspend(device_t dev)
2843 device_t child, child2;
2845 TAILQ_FOREACH(child, &dev->children, link) {
2846 error = DEVICE_SUSPEND(child);
2848 for (child2 = TAILQ_FIRST(&dev->children);
2849 child2 && child2 != child;
2850 child2 = TAILQ_NEXT(child2, link))
2851 DEVICE_RESUME(child2);
2859 bus_generic_resume(device_t dev)
2863 TAILQ_FOREACH(child, &dev->children, link)
2864 DEVICE_RESUME(child);
2865 /* if resume fails, there's nothing we can usefully do... */
2871 bus_print_child_header(device_t dev, device_t child)
2875 if (device_get_desc(child))
2876 retval += device_printf(child, "<%s>", device_get_desc(child));
2878 retval += kprintf("%s", device_get_nameunit(child));
2880 if (child->state != DS_ATTACHED)
2881 kprintf(" [tentative]");
2883 kprintf(" [attached!]");
2889 bus_print_child_footer(device_t dev, device_t child)
2891 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2895 bus_generic_add_child(device_t dev, device_t child, int order,
2896 const char *name, int unit)
2899 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2901 dev = device_add_child_ordered(child, order, name, unit);
2907 bus_generic_print_child(device_t dev, device_t child)
2911 retval += bus_print_child_header(dev, child);
2912 retval += bus_print_child_footer(dev, child);
2918 bus_generic_read_ivar(device_t dev, device_t child, int index,
2924 error = BUS_READ_IVAR(dev->parent, child, index, result);
2931 bus_generic_write_ivar(device_t dev, device_t child, int index,
2937 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2944 * Resource list are used for iterations, do not recurse.
2946 struct resource_list *
2947 bus_generic_get_resource_list(device_t dev, device_t child)
2953 bus_generic_driver_added(device_t dev, driver_t *driver)
2957 DEVICE_IDENTIFY(driver, dev);
2958 TAILQ_FOREACH(child, &dev->children, link) {
2959 if (child->state == DS_NOTPRESENT)
2960 device_probe_and_attach(child);
2965 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2966 int flags, driver_intr_t *intr, void *arg, void **cookiep,
2967 lwkt_serialize_t serializer, const char *desc)
2969 /* Propagate up the bus hierarchy until someone handles it. */
2971 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2972 intr, arg, cookiep, serializer, desc);
2979 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2982 /* Propagate up the bus hierarchy until someone handles it. */
2984 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2990 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2993 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2999 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
3002 BUS_ENABLE_INTR(dev->parent, child, cookie);
3006 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
3007 enum intr_polarity pol)
3009 /* Propagate up the bus hierarchy until someone handles it. */
3011 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
3017 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3018 u_long start, u_long end, u_long count, u_int flags, int cpuid)
3020 /* Propagate up the bus hierarchy until someone handles it. */
3022 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3023 start, end, count, flags, cpuid));
3029 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3032 /* Propagate up the bus hierarchy until someone handles it. */
3034 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
3040 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3043 /* Propagate up the bus hierarchy until someone handles it. */
3045 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
3051 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3052 int rid, struct resource *r)
3054 /* Propagate up the bus hierarchy until someone handles it. */
3056 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3063 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
3064 u_long *startp, u_long *countp)
3070 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
3077 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
3078 u_long start, u_long count, int cpuid)
3084 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
3085 start, count, cpuid);
3091 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
3094 BUS_DELETE_RESOURCE(dev, child, type, rid);
3098 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3100 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3101 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3104 bus_generic_get_dma_tag(device_t dev, device_t child)
3107 /* Propagate up the bus hierarchy until someone handles it. */
3108 if (dev->parent != NULL)
3109 return (BUS_GET_DMA_TAG(dev->parent, child));
3114 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3115 u_long *startp, u_long *countp)
3117 struct resource_list *rl = NULL;
3118 struct resource_list_entry *rle = NULL;
3120 rl = BUS_GET_RESOURCE_LIST(dev, child);
3124 rle = resource_list_find(rl, type, rid);
3129 *startp = rle->start;
3131 *countp = rle->count;
3137 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3138 u_long start, u_long count, int cpuid)
3140 struct resource_list *rl = NULL;
3142 rl = BUS_GET_RESOURCE_LIST(dev, child);
3146 resource_list_add(rl, type, rid, start, (start + count - 1), count,
3153 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3155 struct resource_list *rl = NULL;
3157 rl = BUS_GET_RESOURCE_LIST(dev, child);
3161 resource_list_delete(rl, type, rid);
3165 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3166 int rid, struct resource *r)
3168 struct resource_list *rl = NULL;
3170 rl = BUS_GET_RESOURCE_LIST(dev, child);
3174 return(resource_list_release(rl, dev, child, type, rid, r));
3178 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3179 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
3181 struct resource_list *rl = NULL;
3183 rl = BUS_GET_RESOURCE_LIST(dev, child);
3187 return(resource_list_alloc(rl, dev, child, type, rid,
3188 start, end, count, flags, cpuid));
3192 bus_generic_child_present(device_t bus, device_t child)
3194 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
3199 * Some convenience functions to make it easier for drivers to use the
3200 * resource-management functions. All these really do is hide the
3201 * indirection through the parent's method table, making for slightly
3202 * less-wordy code. In the future, it might make sense for this code
3203 * to maintain some sort of a list of resources allocated by each device.
3206 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3207 struct resource **res)
3211 for (i = 0; rs[i].type != -1; i++)
3213 for (i = 0; rs[i].type != -1; i++) {
3214 res[i] = bus_alloc_resource_any(dev,
3215 rs[i].type, &rs[i].rid, rs[i].flags);
3216 if (res[i] == NULL) {
3217 bus_release_resources(dev, rs, res);
3225 bus_release_resources(device_t dev, const struct resource_spec *rs,
3226 struct resource **res)
3230 for (i = 0; rs[i].type != -1; i++)
3231 if (res[i] != NULL) {
3232 bus_release_resource(
3233 dev, rs[i].type, rs[i].rid, res[i]);
3239 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3240 u_long count, u_int flags)
3242 if (dev->parent == NULL)
3244 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3249 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3251 if (dev->parent == NULL)
3253 return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3254 irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3258 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3260 if (dev->parent == NULL)
3262 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3266 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3268 if (dev->parent == NULL)
3270 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3274 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3276 if (dev->parent == NULL)
3278 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3282 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3283 driver_intr_t handler, void *arg, void **cookiep,
3284 lwkt_serialize_t serializer, const char *desc)
3286 if (dev->parent == NULL)
3288 return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3289 cookiep, serializer, desc);
3293 bus_setup_intr(device_t dev, struct resource *r, int flags,
3294 driver_intr_t handler, void *arg, void **cookiep,
3295 lwkt_serialize_t serializer)
3297 return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3302 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3304 if (dev->parent == NULL)
3306 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3310 bus_enable_intr(device_t dev, void *cookie)
3313 BUS_ENABLE_INTR(dev->parent, dev, cookie);
3317 bus_disable_intr(device_t dev, void *cookie)
3320 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3326 bus_set_resource(device_t dev, int type, int rid,
3327 u_long start, u_long count, int cpuid)
3329 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3330 start, count, cpuid));
3334 bus_get_resource(device_t dev, int type, int rid,
3335 u_long *startp, u_long *countp)
3337 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3342 bus_get_resource_start(device_t dev, int type, int rid)
3344 u_long start, count;
3347 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3355 bus_get_resource_count(device_t dev, int type, int rid)
3357 u_long start, count;
3360 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3368 bus_delete_resource(device_t dev, int type, int rid)
3370 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3374 bus_child_present(device_t child)
3376 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3380 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3384 parent = device_get_parent(child);
3385 if (parent == NULL) {
3389 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3393 bus_child_location_str(device_t child, char *buf, size_t buflen)
3397 parent = device_get_parent(child);
3398 if (parent == NULL) {
3402 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3406 * @brief Wrapper function for BUS_GET_DMA_TAG().
3408 * This function simply calls the BUS_GET_DMA_TAG() method of the
3412 bus_get_dma_tag(device_t dev)
3416 parent = device_get_parent(dev);
3419 return (BUS_GET_DMA_TAG(parent, dev));
3423 root_print_child(device_t dev, device_t child)
3429 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3430 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3433 * If an interrupt mapping gets to here something bad has happened.
3435 panic("root_setup_intr");
3439 * If we get here, assume that the device is permanant and really is
3440 * present in the system. Removable bus drivers are expected to intercept
3441 * this call long before it gets here. We return -1 so that drivers that
3442 * really care can check vs -1 or some ERRNO returned higher in the food
3446 root_child_present(device_t dev, device_t child)
3452 * XXX NOTE! other defaults may be set in bus_if.m
3454 static kobj_method_t root_methods[] = {
3455 /* Device interface */
3456 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3457 KOBJMETHOD(device_suspend, bus_generic_suspend),
3458 KOBJMETHOD(device_resume, bus_generic_resume),
3461 KOBJMETHOD(bus_add_child, bus_generic_add_child),
3462 KOBJMETHOD(bus_print_child, root_print_child),
3463 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3464 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3465 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3466 KOBJMETHOD(bus_child_present, root_child_present),
3471 static driver_t root_driver = {
3478 devclass_t root_devclass;
3481 root_bus_module_handler(module_t mod, int what, void* arg)
3485 TAILQ_INIT(&bus_data_devices);
3486 root_bus = make_device(NULL, "root", 0);
3487 root_bus->desc = "System root bus";
3488 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3489 root_bus->driver = &root_driver;
3490 root_bus->state = DS_ALIVE;
3491 root_devclass = devclass_find_internal("root", NULL, FALSE);
3496 device_shutdown(root_bus);
3503 static moduledata_t root_bus_mod = {
3505 root_bus_module_handler,
3508 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3511 root_bus_configure(void)
3519 * handle device_identify based device attachments to the root_bus
3520 * (typically nexus).
3522 bus_generic_probe(root_bus);
3525 * Probe and attach the devices under root_bus.
3527 TAILQ_FOREACH(dev, &root_bus->children, link) {
3528 device_probe_and_attach(dev);
3532 * Wait for all asynchronous attaches to complete. If we don't
3533 * our legacy ISA bus scan could steal device unit numbers or
3537 if (numasyncthreads)
3538 kprintf("Waiting for async drivers to attach\n");
3539 while (numasyncthreads > 0) {
3540 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3542 if (warncount == 0) {
3543 kprintf("Warning: Still waiting for %d "
3544 "drivers to attach\n", numasyncthreads);
3545 } else if (warncount == -30) {
3546 kprintf("Giving up on %d drivers\n", numasyncthreads);
3550 root_bus->state = DS_ATTACHED;
3554 driver_module_handler(module_t mod, int what, void *arg)
3557 struct driver_module_data *dmd;
3558 devclass_t bus_devclass;
3559 kobj_class_t driver;
3560 const char *parentname;
3562 dmd = (struct driver_module_data *)arg;
3563 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3568 if (dmd->dmd_chainevh)
3569 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3571 driver = dmd->dmd_driver;
3572 PDEBUG(("Loading module: driver %s on bus %s",
3573 DRIVERNAME(driver), dmd->dmd_busname));
3576 * If the driver has any base classes, make the
3577 * devclass inherit from the devclass of the driver's
3578 * first base class. This will allow the system to
3579 * search for drivers in both devclasses for children
3580 * of a device using this driver.
3582 if (driver->baseclasses)
3583 parentname = driver->baseclasses[0]->name;
3586 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3589 error = devclass_add_driver(bus_devclass, driver);
3595 PDEBUG(("Unloading module: driver %s from bus %s",
3596 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3597 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3599 if (!error && dmd->dmd_chainevh)
3600 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3610 * The _short versions avoid iteration by not calling anything that prints
3611 * more than oneliners. I love oneliners.
3615 print_device_short(device_t dev, int indent)
3620 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3621 dev->unit, dev->desc,
3622 (dev->parent? "":"no "),
3623 (TAILQ_EMPTY(&dev->children)? "no ":""),
3624 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3625 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3626 (dev->flags&DF_WILDCARD? "wildcard,":""),
3627 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3628 (dev->ivars? "":"no "),
3629 (dev->softc? "":"no "),
3634 print_device(device_t dev, int indent)
3639 print_device_short(dev, indent);
3641 indentprintf(("Parent:\n"));
3642 print_device_short(dev->parent, indent+1);
3643 indentprintf(("Driver:\n"));
3644 print_driver_short(dev->driver, indent+1);
3645 indentprintf(("Devclass:\n"));
3646 print_devclass_short(dev->devclass, indent+1);
3650 * Print the device and all its children (indented).
3653 print_device_tree_short(device_t dev, int indent)
3660 print_device_short(dev, indent);
3662 TAILQ_FOREACH(child, &dev->children, link)
3663 print_device_tree_short(child, indent+1);
3667 * Print the device and all its children (indented).
3670 print_device_tree(device_t dev, int indent)
3677 print_device(dev, indent);
3679 TAILQ_FOREACH(child, &dev->children, link)
3680 print_device_tree(child, indent+1);
3684 print_driver_short(driver_t *driver, int indent)
3689 indentprintf(("driver %s: softc size = %zu\n",
3690 driver->name, driver->size));
3694 print_driver(driver_t *driver, int indent)
3699 print_driver_short(driver, indent);
3704 print_driver_list(driver_list_t drivers, int indent)
3706 driverlink_t driver;
3708 TAILQ_FOREACH(driver, &drivers, link)
3709 print_driver(driver->driver, indent);
3713 print_devclass_short(devclass_t dc, int indent)
3718 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3722 print_devclass(devclass_t dc, int indent)
3729 print_devclass_short(dc, indent);
3730 indentprintf(("Drivers:\n"));
3731 print_driver_list(dc->drivers, indent+1);
3733 indentprintf(("Devices:\n"));
3734 for (i = 0; i < dc->maxunit; i++)
3736 print_device(dc->devices[i], indent+1);
3740 print_devclass_list_short(void)
3744 kprintf("Short listing of devclasses, drivers & devices:\n");
3745 TAILQ_FOREACH(dc, &devclasses, link) {
3746 print_devclass_short(dc, 0);
3751 print_devclass_list(void)
3755 kprintf("Full listing of devclasses, drivers & devices:\n");
3756 TAILQ_FOREACH(dc, &devclasses, link) {
3757 print_devclass(dc, 0);
3764 * Check to see if a device is disabled via a disabled hint.
3767 resource_disabled(const char *name, int unit)
3771 error = resource_int_value(name, unit, "disabled", &value);
3778 * User-space access to the device tree.
3780 * We implement a small set of nodes:
3782 * hw.bus Single integer read method to obtain the
3783 * current generation count.
3784 * hw.bus.devices Reads the entire device tree in flat space.
3785 * hw.bus.rman Resource manager interface
3787 * We might like to add the ability to scan devclasses and/or drivers to
3788 * determine what else is currently loaded/available.
3792 sysctl_bus(SYSCTL_HANDLER_ARGS)
3794 struct u_businfo ubus;
3796 ubus.ub_version = BUS_USER_VERSION;
3797 ubus.ub_generation = bus_data_generation;
3799 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3801 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3802 "bus-related data");
3805 sysctl_devices(SYSCTL_HANDLER_ARGS)
3807 int *name = (int *)arg1;
3808 u_int namelen = arg2;
3811 struct u_device udev; /* XXX this is a bit big */
3817 if (bus_data_generation_check(name[0]))
3823 * Scan the list of devices, looking for the requested index.
3825 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3833 * Populate the return array.
3835 bzero(&udev, sizeof(udev));
3836 udev.dv_handle = (uintptr_t)dev;
3837 udev.dv_parent = (uintptr_t)dev->parent;
3838 if (dev->nameunit != NULL)
3839 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3840 if (dev->desc != NULL)
3841 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3842 if (dev->driver != NULL && dev->driver->name != NULL)
3843 strlcpy(udev.dv_drivername, dev->driver->name,
3844 sizeof(udev.dv_drivername));
3845 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3846 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3847 udev.dv_devflags = dev->devflags;
3848 udev.dv_flags = dev->flags;
3849 udev.dv_state = dev->state;
3850 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3854 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3855 "system device tree");
3858 bus_data_generation_check(int generation)
3860 if (generation != bus_data_generation)
3863 /* XXX generate optimised lists here? */
3868 bus_data_generation_update(void)
3870 bus_data_generation++;
3874 intr_str_polarity(enum intr_polarity pola)
3877 case INTR_POLARITY_LOW:
3880 case INTR_POLARITY_HIGH:
3883 case INTR_POLARITY_CONFORM:
3890 intr_str_trigger(enum intr_trigger trig)
3893 case INTR_TRIGGER_EDGE:
3896 case INTR_TRIGGER_LEVEL:
3899 case INTR_TRIGGER_CONFORM:
3906 device_getenv_int(device_t dev, const char *knob, int def)
3910 /* Deprecated; for compat */
3911 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3912 kgetenv_int(env, &def);
3914 /* Prefer dev.driver.unit.knob */
3915 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3916 device_get_name(dev), device_get_unit(dev), knob);
3917 kgetenv_int(env, &def);
3923 device_getenv_string(device_t dev, const char *knob, char * __restrict data,
3924 int dlen, const char * __restrict def)
3928 strlcpy(data, def, dlen);
3930 /* Deprecated; for compat */
3931 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3932 kgetenv_string(env, data, dlen);
3934 /* Prefer dev.driver.unit.knob */
3935 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3936 device_get_name(dev), device_get_unit(dev), knob);
3937 kgetenv_string(env, data, dlen);