2 * Copyright (c) 1997,1998 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
27 * $DragonFly: src/sys/kern/subr_bus.c,v 1.41 2008/01/05 13:30:33 corecode Exp $
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <sys/sysctl.h>
41 #include <sys/bus_private.h>
42 #include <sys/systm.h>
46 #include <machine/stdarg.h> /* for device_printf() */
48 #include <sys/thread2.h>
50 MALLOC_DEFINE(M_BUS
, "bus", "Bus data structures");
53 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
54 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
55 #define DRIVERNAME(d) ((d)? d->name : "no driver")
56 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
58 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
59 * prevent syslog from deleting initial spaces
61 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
63 static void print_device_short(device_t dev
, int indent
);
64 static void print_device(device_t dev
, int indent
);
65 void print_device_tree_short(device_t dev
, int indent
);
66 void print_device_tree(device_t dev
, int indent
);
67 static void print_driver_short(driver_t
*driver
, int indent
);
68 static void print_driver(driver_t
*driver
, int indent
);
69 static void print_driver_list(driver_list_t drivers
, int indent
);
70 static void print_devclass_short(devclass_t dc
, int indent
);
71 static void print_devclass(devclass_t dc
, int indent
);
72 void print_devclass_list_short(void);
73 void print_devclass_list(void);
76 /* Make the compiler ignore the function calls */
77 #define PDEBUG(a) /* nop */
78 #define DEVICENAME(d) /* nop */
79 #define DRIVERNAME(d) /* nop */
80 #define DEVCLANAME(d) /* nop */
82 #define print_device_short(d,i) /* nop */
83 #define print_device(d,i) /* nop */
84 #define print_device_tree_short(d,i) /* nop */
85 #define print_device_tree(d,i) /* nop */
86 #define print_driver_short(d,i) /* nop */
87 #define print_driver(d,i) /* nop */
88 #define print_driver_list(d,i) /* nop */
89 #define print_devclass_short(d,i) /* nop */
90 #define print_devclass(d,i) /* nop */
91 #define print_devclass_list_short() /* nop */
92 #define print_devclass_list() /* nop */
96 static void device_register_oids(device_t dev
);
97 static void device_unregister_oids(device_t dev
);
99 static void device_attach_async(device_t dev
);
100 static void device_attach_thread(void *arg
);
101 static int device_doattach(device_t dev
);
103 static int do_async_attach
= 0;
104 static int numasyncthreads
;
105 TUNABLE_INT("kern.do_async_attach", &do_async_attach
);
107 kobj_method_t null_methods
[] = {
111 DEFINE_CLASS(null
, null_methods
, 0);
114 * Devclass implementation
117 static devclass_list_t devclasses
= TAILQ_HEAD_INITIALIZER(devclasses
);
120 devclass_find_internal(const char *classname
, const char *parentname
,
125 PDEBUG(("looking for %s", classname
));
126 if (classname
== NULL
)
129 TAILQ_FOREACH(dc
, &devclasses
, link
)
130 if (!strcmp(dc
->name
, classname
))
134 PDEBUG(("creating %s", classname
));
135 dc
= kmalloc(sizeof(struct devclass
) + strlen(classname
) + 1,
136 M_BUS
, M_INTWAIT
| M_ZERO
);
140 dc
->name
= (char*) (dc
+ 1);
141 strcpy(dc
->name
, classname
);
144 TAILQ_INIT(&dc
->drivers
);
145 TAILQ_INSERT_TAIL(&devclasses
, dc
, link
);
147 if (parentname
&& dc
&& !dc
->parent
)
148 dc
->parent
= devclass_find_internal(parentname
, NULL
, FALSE
);
154 devclass_create(const char *classname
)
156 return(devclass_find_internal(classname
, NULL
, TRUE
));
160 devclass_find(const char *classname
)
162 return(devclass_find_internal(classname
, NULL
, FALSE
));
166 devclass_find_unit(const char *classname
, int unit
)
170 if ((dc
= devclass_find(classname
)) != NULL
)
171 return(devclass_get_device(dc
, unit
));
176 devclass_add_driver(devclass_t dc
, driver_t
*driver
)
182 PDEBUG(("%s", DRIVERNAME(driver
)));
184 dl
= kmalloc(sizeof *dl
, M_BUS
, M_INTWAIT
| M_ZERO
);
189 * Compile the driver's methods. Also increase the reference count
190 * so that the class doesn't get freed when the last instance
191 * goes. This means we can safely use static methods and avoids a
192 * double-free in devclass_delete_driver.
194 kobj_class_instantiate(driver
);
197 * Make sure the devclass which the driver is implementing exists.
199 devclass_find_internal(driver
->name
, NULL
, TRUE
);
202 TAILQ_INSERT_TAIL(&dc
->drivers
, dl
, link
);
205 * Call BUS_DRIVER_ADDED for any existing busses in this class,
206 * but only if the bus has already been attached (otherwise we
207 * might probe too early).
209 * This is what will cause a newly loaded module to be associated
210 * with hardware. bus_generic_driver_added() is typically what ends
213 for (i
= 0; i
< dc
->maxunit
; i
++) {
214 if ((dev
= dc
->devices
[i
]) != NULL
) {
215 if (dev
->state
>= DS_ATTACHED
)
216 BUS_DRIVER_ADDED(dev
, driver
);
224 devclass_delete_driver(devclass_t busclass
, driver_t
*driver
)
226 devclass_t dc
= devclass_find(driver
->name
);
232 PDEBUG(("%s from devclass %s", driver
->name
, DEVCLANAME(busclass
)));
238 * Find the link structure in the bus' list of drivers.
240 TAILQ_FOREACH(dl
, &busclass
->drivers
, link
)
241 if (dl
->driver
== driver
)
245 PDEBUG(("%s not found in %s list", driver
->name
, busclass
->name
));
250 * Disassociate from any devices. We iterate through all the
251 * devices in the devclass of the driver and detach any which are
252 * using the driver and which have a parent in the devclass which
253 * we are deleting from.
255 * Note that since a driver can be in multiple devclasses, we
256 * should not detach devices which are not children of devices in
257 * the affected devclass.
259 for (i
= 0; i
< dc
->maxunit
; i
++)
260 if (dc
->devices
[i
]) {
261 dev
= dc
->devices
[i
];
262 if (dev
->driver
== driver
&& dev
->parent
&&
263 dev
->parent
->devclass
== busclass
) {
264 if ((error
= device_detach(dev
)) != 0)
266 device_set_driver(dev
, NULL
);
270 TAILQ_REMOVE(&busclass
->drivers
, dl
, link
);
273 kobj_class_uninstantiate(driver
);
279 devclass_find_driver_internal(devclass_t dc
, const char *classname
)
283 PDEBUG(("%s in devclass %s", classname
, DEVCLANAME(dc
)));
285 TAILQ_FOREACH(dl
, &dc
->drivers
, link
)
286 if (!strcmp(dl
->driver
->name
, classname
))
289 PDEBUG(("not found"));
294 devclass_find_driver(devclass_t dc
, const char *classname
)
298 dl
= devclass_find_driver_internal(dc
, classname
);
306 devclass_get_name(devclass_t dc
)
312 devclass_get_device(devclass_t dc
, int unit
)
314 if (dc
== NULL
|| unit
< 0 || unit
>= dc
->maxunit
)
316 return(dc
->devices
[unit
]);
320 devclass_get_softc(devclass_t dc
, int unit
)
324 dev
= devclass_get_device(dc
, unit
);
328 return(device_get_softc(dev
));
332 devclass_get_devices(devclass_t dc
, device_t
**devlistp
, int *devcountp
)
339 for (i
= 0; i
< dc
->maxunit
; i
++)
343 list
= kmalloc(count
* sizeof(device_t
), M_TEMP
, M_INTWAIT
| M_ZERO
);
348 for (i
= 0; i
< dc
->maxunit
; i
++)
349 if (dc
->devices
[i
]) {
350 list
[count
] = dc
->devices
[i
];
361 devclass_get_maxunit(devclass_t dc
)
367 devclass_set_parent(devclass_t dc
, devclass_t pdc
)
373 devclass_get_parent(devclass_t dc
)
379 devclass_alloc_unit(devclass_t dc
, int *unitp
)
383 PDEBUG(("unit %d in devclass %s", unit
, DEVCLANAME(dc
)));
385 /* If we have been given a wired unit number, check for existing device */
387 if (unit
>= 0 && unit
< dc
->maxunit
&&
388 dc
->devices
[unit
] != NULL
) {
390 kprintf("%s-: %s%d exists, using next available unit number\n",
391 dc
->name
, dc
->name
, unit
);
392 /* find the next available slot */
393 while (++unit
< dc
->maxunit
&& dc
->devices
[unit
] != NULL
)
397 /* Unwired device, find the next available slot for it */
399 while (unit
< dc
->maxunit
&& dc
->devices
[unit
] != NULL
)
404 * We've selected a unit beyond the length of the table, so let's
405 * extend the table to make room for all units up to and including
408 if (unit
>= dc
->maxunit
) {
412 newsize
= roundup((unit
+ 1), MINALLOCSIZE
/ sizeof(device_t
));
413 newlist
= kmalloc(sizeof(device_t
) * newsize
, M_BUS
,
417 bcopy(dc
->devices
, newlist
, sizeof(device_t
) * dc
->maxunit
);
419 kfree(dc
->devices
, M_BUS
);
420 dc
->devices
= newlist
;
421 dc
->maxunit
= newsize
;
423 PDEBUG(("now: unit %d in devclass %s", unit
, DEVCLANAME(dc
)));
430 devclass_add_device(devclass_t dc
, device_t dev
)
434 PDEBUG(("%s in devclass %s", DEVICENAME(dev
), DEVCLANAME(dc
)));
436 buflen
= strlen(dc
->name
) + 5;
437 dev
->nameunit
= kmalloc(buflen
, M_BUS
, M_INTWAIT
| M_ZERO
);
441 if ((error
= devclass_alloc_unit(dc
, &dev
->unit
)) != 0) {
442 kfree(dev
->nameunit
, M_BUS
);
443 dev
->nameunit
= NULL
;
446 dc
->devices
[dev
->unit
] = dev
;
448 ksnprintf(dev
->nameunit
, buflen
, "%s%d", dc
->name
, dev
->unit
);
450 #ifdef DEVICE_SYSCTLS
451 device_register_oids(dev
);
458 devclass_delete_device(devclass_t dc
, device_t dev
)
463 PDEBUG(("%s in devclass %s", DEVICENAME(dev
), DEVCLANAME(dc
)));
465 if (dev
->devclass
!= dc
|| dc
->devices
[dev
->unit
] != dev
)
466 panic("devclass_delete_device: inconsistent device class");
467 dc
->devices
[dev
->unit
] = NULL
;
468 if (dev
->flags
& DF_WILDCARD
)
470 dev
->devclass
= NULL
;
471 kfree(dev
->nameunit
, M_BUS
);
472 dev
->nameunit
= NULL
;
474 #ifdef DEVICE_SYSCTLS
475 device_unregister_oids(dev
);
482 make_device(device_t parent
, const char *name
, int unit
)
487 PDEBUG(("%s at %s as unit %d", name
, DEVICENAME(parent
), unit
));
490 dc
= devclass_find_internal(name
, NULL
, TRUE
);
492 kprintf("make_device: can't find device class %s\n", name
);
498 dev
= kmalloc(sizeof(struct device
), M_BUS
, M_INTWAIT
| M_ZERO
);
502 dev
->parent
= parent
;
503 TAILQ_INIT(&dev
->children
);
504 kobj_init((kobj_t
) dev
, &null_class
);
506 dev
->devclass
= NULL
;
508 dev
->nameunit
= NULL
;
512 dev
->flags
= DF_ENABLED
;
515 dev
->flags
|= DF_WILDCARD
;
517 dev
->flags
|= DF_FIXEDCLASS
;
518 if (devclass_add_device(dc
, dev
) != 0) {
519 kobj_delete((kobj_t
)dev
, M_BUS
);
526 dev
->state
= DS_NOTPRESENT
;
532 device_print_child(device_t dev
, device_t child
)
536 if (device_is_alive(child
))
537 retval
+= BUS_PRINT_CHILD(dev
, child
);
539 retval
+= device_printf(child
, " not found\n");
545 device_add_child(device_t dev
, const char *name
, int unit
)
547 return device_add_child_ordered(dev
, 0, name
, unit
);
551 device_add_child_ordered(device_t dev
, int order
, const char *name
, int unit
)
556 PDEBUG(("%s at %s with order %d as unit %d", name
, DEVICENAME(dev
),
559 child
= make_device(dev
, name
, unit
);
562 child
->order
= order
;
564 TAILQ_FOREACH(place
, &dev
->children
, link
)
565 if (place
->order
> order
)
570 * The device 'place' is the first device whose order is
571 * greater than the new child.
573 TAILQ_INSERT_BEFORE(place
, child
, link
);
576 * The new child's order is greater or equal to the order of
577 * any existing device. Add the child to the tail of the list.
579 TAILQ_INSERT_TAIL(&dev
->children
, child
, link
);
586 device_delete_child(device_t dev
, device_t child
)
591 PDEBUG(("%s from %s", DEVICENAME(child
), DEVICENAME(dev
)));
593 /* remove children first */
594 while ( (grandchild
= TAILQ_FIRST(&child
->children
)) ) {
595 error
= device_delete_child(child
, grandchild
);
600 if ((error
= device_detach(child
)) != 0)
603 devclass_delete_device(child
->devclass
, child
);
604 TAILQ_REMOVE(&dev
->children
, child
, link
);
605 device_set_desc(child
, NULL
);
606 kobj_delete((kobj_t
)child
, M_BUS
);
612 * Find only devices attached to this bus.
615 device_find_child(device_t dev
, const char *classname
, int unit
)
620 dc
= devclass_find(classname
);
624 child
= devclass_get_device(dc
, unit
);
625 if (child
&& child
->parent
== dev
)
631 first_matching_driver(devclass_t dc
, device_t dev
)
634 return(devclass_find_driver_internal(dc
, dev
->devclass
->name
));
636 return(TAILQ_FIRST(&dc
->drivers
));
640 next_matching_driver(devclass_t dc
, device_t dev
, driverlink_t last
)
644 for (dl
= TAILQ_NEXT(last
, link
); dl
; dl
= TAILQ_NEXT(dl
, link
))
645 if (!strcmp(dev
->devclass
->name
, dl
->driver
->name
))
649 return(TAILQ_NEXT(last
, link
));
653 device_probe_child(device_t dev
, device_t child
)
656 driverlink_t best
= 0;
659 int hasclass
= (child
->devclass
!= 0);
663 panic("device_probe_child: parent device has no devclass");
665 if (child
->state
== DS_ALIVE
)
668 for (; dc
; dc
= dc
->parent
) {
669 for (dl
= first_matching_driver(dc
, child
); dl
;
670 dl
= next_matching_driver(dc
, child
, dl
)) {
671 PDEBUG(("Trying %s", DRIVERNAME(dl
->driver
)));
672 device_set_driver(child
, dl
->driver
);
674 device_set_devclass(child
, dl
->driver
->name
);
675 result
= DEVICE_PROBE(child
);
677 device_set_devclass(child
, 0);
680 * If the driver returns SUCCESS, there can be
681 * no higher match for this device.
690 * The driver returned an error so it
691 * certainly doesn't match.
694 device_set_driver(child
, 0);
699 * A priority lower than SUCCESS, remember the
700 * best matching driver. Initialise the value
701 * of pri for the first match.
703 if (best
== 0 || result
> pri
) {
710 * If we have unambiguous match in this devclass,
711 * don't look in the parent.
713 if (best
&& pri
== 0)
718 * If we found a driver, change state and initialise the devclass.
721 if (!child
->devclass
)
722 device_set_devclass(child
, best
->driver
->name
);
723 device_set_driver(child
, best
->driver
);
726 * A bit bogus. Call the probe method again to make
727 * sure that we have the right description.
731 child
->state
= DS_ALIVE
;
739 device_get_parent(device_t dev
)
745 device_get_children(device_t dev
, device_t
**devlistp
, int *devcountp
)
752 TAILQ_FOREACH(child
, &dev
->children
, link
)
755 list
= kmalloc(count
* sizeof(device_t
), M_TEMP
, M_INTWAIT
| M_ZERO
);
760 TAILQ_FOREACH(child
, &dev
->children
, link
) {
772 device_get_driver(device_t dev
)
778 device_get_devclass(device_t dev
)
780 return(dev
->devclass
);
784 device_get_name(device_t dev
)
787 return devclass_get_name(dev
->devclass
);
792 device_get_nameunit(device_t dev
)
794 return(dev
->nameunit
);
798 device_get_unit(device_t dev
)
804 device_get_desc(device_t dev
)
810 device_get_flags(device_t dev
)
812 return(dev
->devflags
);
816 device_print_prettyname(device_t dev
)
818 const char *name
= device_get_name(dev
);
821 return kprintf("unknown: ");
823 return kprintf("%s%d: ", name
, device_get_unit(dev
));
827 device_printf(device_t dev
, const char * fmt
, ...)
832 retval
= device_print_prettyname(dev
);
834 retval
+= kvprintf(fmt
, ap
);
840 device_set_desc_internal(device_t dev
, const char* desc
, int copy
)
842 if (dev
->desc
&& (dev
->flags
& DF_DESCMALLOCED
)) {
843 kfree(dev
->desc
, M_BUS
);
844 dev
->flags
&= ~DF_DESCMALLOCED
;
849 dev
->desc
= kmalloc(strlen(desc
) + 1, M_BUS
, M_INTWAIT
);
851 strcpy(dev
->desc
, desc
);
852 dev
->flags
|= DF_DESCMALLOCED
;
855 /* Avoid a -Wcast-qual warning */
856 dev
->desc
= (char *)(uintptr_t) desc
;
858 #ifdef DEVICE_SYSCTLS
860 struct sysctl_oid
*oid
= &dev
->oid
[1];
861 oid
->oid_arg1
= dev
->desc
? dev
->desc
: "";
862 oid
->oid_arg2
= dev
->desc
? strlen(dev
->desc
) : 0;
868 device_set_desc(device_t dev
, const char* desc
)
870 device_set_desc_internal(dev
, desc
, FALSE
);
874 device_set_desc_copy(device_t dev
, const char* desc
)
876 device_set_desc_internal(dev
, desc
, TRUE
);
880 device_set_flags(device_t dev
, uint32_t flags
)
882 dev
->devflags
= flags
;
886 device_get_softc(device_t dev
)
892 device_set_softc(device_t dev
, void *softc
)
894 if (dev
->softc
&& !(dev
->flags
& DF_EXTERNALSOFTC
))
895 kfree(dev
->softc
, M_BUS
);
898 dev
->flags
|= DF_EXTERNALSOFTC
;
900 dev
->flags
&= ~DF_EXTERNALSOFTC
;
904 device_set_async_attach(device_t dev
, int enable
)
907 dev
->flags
|= DF_ASYNCPROBE
;
909 dev
->flags
&= ~DF_ASYNCPROBE
;
913 device_get_ivars(device_t dev
)
919 device_set_ivars(device_t dev
, void * ivars
)
928 device_get_state(device_t dev
)
934 device_enable(device_t dev
)
936 dev
->flags
|= DF_ENABLED
;
940 device_disable(device_t dev
)
942 dev
->flags
&= ~DF_ENABLED
;
949 device_busy(device_t dev
)
951 if (dev
->state
< DS_ATTACHED
)
952 panic("device_busy: called for unattached device");
953 if (dev
->busy
== 0 && dev
->parent
)
954 device_busy(dev
->parent
);
956 dev
->state
= DS_BUSY
;
963 device_unbusy(device_t dev
)
965 if (dev
->state
!= DS_BUSY
)
966 panic("device_unbusy: called for non-busy device");
968 if (dev
->busy
== 0) {
970 device_unbusy(dev
->parent
);
971 dev
->state
= DS_ATTACHED
;
976 device_quiet(device_t dev
)
978 dev
->flags
|= DF_QUIET
;
982 device_verbose(device_t dev
)
984 dev
->flags
&= ~DF_QUIET
;
988 device_is_quiet(device_t dev
)
990 return((dev
->flags
& DF_QUIET
) != 0);
994 device_is_enabled(device_t dev
)
996 return((dev
->flags
& DF_ENABLED
) != 0);
1000 device_is_alive(device_t dev
)
1002 return(dev
->state
>= DS_ALIVE
);
1006 device_is_attached(device_t dev
)
1008 return(dev
->state
>= DS_ATTACHED
);
1012 device_set_devclass(device_t dev
, const char *classname
)
1018 devclass_delete_device(dev
->devclass
, dev
);
1022 if (dev
->devclass
) {
1023 kprintf("device_set_devclass: device class already set\n");
1027 dc
= devclass_find_internal(classname
, NULL
, TRUE
);
1031 return(devclass_add_device(dc
, dev
));
1035 device_set_driver(device_t dev
, driver_t
*driver
)
1037 if (dev
->state
>= DS_ATTACHED
)
1040 if (dev
->driver
== driver
)
1043 if (dev
->softc
&& !(dev
->flags
& DF_EXTERNALSOFTC
)) {
1044 kfree(dev
->softc
, M_BUS
);
1047 kobj_delete((kobj_t
) dev
, 0);
1048 dev
->driver
= driver
;
1050 kobj_init((kobj_t
) dev
, (kobj_class_t
) driver
);
1051 if (!(dev
->flags
& DF_EXTERNALSOFTC
)) {
1052 dev
->softc
= kmalloc(driver
->size
, M_BUS
,
1053 M_INTWAIT
| M_ZERO
);
1055 kobj_delete((kobj_t
)dev
, 0);
1056 kobj_init((kobj_t
) dev
, &null_class
);
1062 kobj_init((kobj_t
) dev
, &null_class
);
1067 device_probe_and_attach(device_t dev
)
1069 device_t bus
= dev
->parent
;
1072 if (dev
->state
>= DS_ALIVE
)
1075 if ((dev
->flags
& DF_ENABLED
) == 0) {
1077 device_print_prettyname(dev
);
1078 kprintf("not probed (disabled)\n");
1083 error
= device_probe_child(bus
, dev
);
1085 if (!(dev
->flags
& DF_DONENOMATCH
)) {
1086 BUS_PROBE_NOMATCH(bus
, dev
);
1087 dev
->flags
|= DF_DONENOMATCH
;
1093 * Output the exact device chain prior to the attach in case the
1094 * system locks up during attach, and generate the full info after
1095 * the attach so correct irq and other information is displayed.
1097 if (bootverbose
&& !device_is_quiet(dev
)) {
1100 kprintf("%s", device_get_nameunit(dev
));
1101 for (tmp
= dev
->parent
; tmp
; tmp
= tmp
->parent
)
1102 kprintf(".%s", device_get_nameunit(tmp
));
1105 if (!device_is_quiet(dev
))
1106 device_print_child(bus
, dev
);
1107 if ((dev
->flags
& DF_ASYNCPROBE
) && do_async_attach
) {
1108 kprintf("%s: probing asynchronously\n",
1109 device_get_nameunit(dev
));
1110 dev
->state
= DS_INPROGRESS
;
1111 device_attach_async(dev
);
1114 error
= device_doattach(dev
);
1120 * Device is known to be alive, do the attach asynchronously.
1122 * The MP lock is held by all threads.
1125 device_attach_async(device_t dev
)
1129 atomic_add_int(&numasyncthreads
, 1);
1130 lwkt_create(device_attach_thread
, dev
, &td
, NULL
,
1131 0, 0, (dev
->desc
? dev
->desc
: "devattach"));
1135 device_attach_thread(void *arg
)
1139 (void)device_doattach(dev
);
1140 atomic_subtract_int(&numasyncthreads
, 1);
1141 wakeup(&numasyncthreads
);
1145 * Device is known to be alive, do the attach (synchronous or asynchronous)
1148 device_doattach(device_t dev
)
1150 device_t bus
= dev
->parent
;
1151 int hasclass
= (dev
->devclass
!= 0);
1154 error
= DEVICE_ATTACH(dev
);
1156 dev
->state
= DS_ATTACHED
;
1157 if (bootverbose
&& !device_is_quiet(dev
))
1158 device_print_child(bus
, dev
);
1160 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1161 dev
->driver
->name
, dev
->unit
, error
);
1162 /* Unset the class that was set in device_probe_child */
1164 device_set_devclass(dev
, 0);
1165 device_set_driver(dev
, NULL
);
1166 dev
->state
= DS_NOTPRESENT
;
1172 device_detach(device_t dev
)
1176 PDEBUG(("%s", DEVICENAME(dev
)));
1177 if (dev
->state
== DS_BUSY
)
1179 if (dev
->state
!= DS_ATTACHED
)
1182 if ((error
= DEVICE_DETACH(dev
)) != 0)
1184 device_printf(dev
, "detached\n");
1186 BUS_CHILD_DETACHED(dev
->parent
, dev
);
1188 if (!(dev
->flags
& DF_FIXEDCLASS
))
1189 devclass_delete_device(dev
->devclass
, dev
);
1191 dev
->state
= DS_NOTPRESENT
;
1192 device_set_driver(dev
, NULL
);
1198 device_shutdown(device_t dev
)
1200 if (dev
->state
< DS_ATTACHED
)
1202 PDEBUG(("%s", DEVICENAME(dev
)));
1203 return DEVICE_SHUTDOWN(dev
);
1207 device_set_unit(device_t dev
, int unit
)
1212 dc
= device_get_devclass(dev
);
1213 if (unit
< dc
->maxunit
&& dc
->devices
[unit
])
1215 err
= devclass_delete_device(dc
, dev
);
1219 err
= devclass_add_device(dc
, dev
);
1223 #ifdef DEVICE_SYSCTLS
1226 * Sysctl nodes for devices.
1229 SYSCTL_NODE(_hw
, OID_AUTO
, devices
, CTLFLAG_RW
, 0, "A list of all devices");
1232 sysctl_handle_children(SYSCTL_HANDLER_ARGS
)
1234 device_t dev
= arg1
;
1236 int first
= 1, error
= 0;
1238 TAILQ_FOREACH(child
, &dev
->children
, link
)
1239 if (child
->nameunit
) {
1241 error
= SYSCTL_OUT(req
, ",", 1);
1246 error
= SYSCTL_OUT(req
, child
->nameunit
,
1247 strlen(child
->nameunit
));
1252 error
= SYSCTL_OUT(req
, "", 1);
1258 sysctl_handle_state(SYSCTL_HANDLER_ARGS
)
1260 device_t dev
= arg1
;
1262 switch (dev
->state
) {
1264 return SYSCTL_OUT(req
, "notpresent", sizeof("notpresent"));
1266 return SYSCTL_OUT(req
, "alive", sizeof("alive"));
1268 return SYSCTL_OUT(req
, "in-progress", sizeof("in-progress"));
1270 return SYSCTL_OUT(req
, "attached", sizeof("attached"));
1272 return SYSCTL_OUT(req
, "busy", sizeof("busy"));
1279 device_register_oids(device_t dev
)
1281 struct sysctl_oid
* oid
;
1284 bzero(oid
, sizeof(*oid
));
1285 oid
->oid_parent
= &sysctl__hw_devices_children
;
1286 oid
->oid_number
= OID_AUTO
;
1287 oid
->oid_kind
= CTLTYPE_NODE
| CTLFLAG_RW
;
1288 oid
->oid_arg1
= &dev
->oidlist
[0];
1290 oid
->oid_name
= dev
->nameunit
;
1291 oid
->oid_handler
= 0;
1293 SLIST_INIT(&dev
->oidlist
[0]);
1294 sysctl_register_oid(oid
);
1297 bzero(oid
, sizeof(*oid
));
1298 oid
->oid_parent
= &dev
->oidlist
[0];
1299 oid
->oid_number
= OID_AUTO
;
1300 oid
->oid_kind
= CTLTYPE_STRING
| CTLFLAG_RD
;
1301 oid
->oid_arg1
= dev
->desc
? dev
->desc
: "";
1302 oid
->oid_arg2
= dev
->desc
? strlen(dev
->desc
) : 0;
1303 oid
->oid_name
= "desc";
1304 oid
->oid_handler
= sysctl_handle_string
;
1306 sysctl_register_oid(oid
);
1309 bzero(oid
, sizeof(*oid
));
1310 oid
->oid_parent
= &dev
->oidlist
[0];
1311 oid
->oid_number
= OID_AUTO
;
1312 oid
->oid_kind
= CTLTYPE_INT
| CTLFLAG_RD
;
1313 oid
->oid_arg1
= dev
;
1315 oid
->oid_name
= "children";
1316 oid
->oid_handler
= sysctl_handle_children
;
1318 sysctl_register_oid(oid
);
1321 bzero(oid
, sizeof(*oid
));
1322 oid
->oid_parent
= &dev
->oidlist
[0];
1323 oid
->oid_number
= OID_AUTO
;
1324 oid
->oid_kind
= CTLTYPE_INT
| CTLFLAG_RD
;
1325 oid
->oid_arg1
= dev
;
1327 oid
->oid_name
= "state";
1328 oid
->oid_handler
= sysctl_handle_state
;
1330 sysctl_register_oid(oid
);
1334 device_unregister_oids(device_t dev
)
1336 sysctl_unregister_oid(&dev
->oid
[0]);
1337 sysctl_unregister_oid(&dev
->oid
[1]);
1338 sysctl_unregister_oid(&dev
->oid
[2]);
1343 /*======================================*/
1345 * Access functions for device resources.
1348 /* Supplied by config(8) in ioconf.c */
1349 extern struct config_device config_devtab
[];
1350 extern int devtab_count
;
1352 /* Runtime version */
1353 struct config_device
*devtab
= config_devtab
;
1356 resource_new_name(const char *name
, int unit
)
1358 struct config_device
*new;
1360 new = kmalloc((devtab_count
+ 1) * sizeof(*new), M_TEMP
,
1361 M_INTWAIT
| M_ZERO
);
1364 if (devtab
&& devtab_count
> 0)
1365 bcopy(devtab
, new, devtab_count
* sizeof(*new));
1366 new[devtab_count
].name
= kmalloc(strlen(name
) + 1, M_TEMP
, M_INTWAIT
);
1367 if (new[devtab_count
].name
== NULL
) {
1371 strcpy(new[devtab_count
].name
, name
);
1372 new[devtab_count
].unit
= unit
;
1373 new[devtab_count
].resource_count
= 0;
1374 new[devtab_count
].resources
= NULL
;
1375 if (devtab
&& devtab
!= config_devtab
)
1376 kfree(devtab
, M_TEMP
);
1378 return devtab_count
++;
1382 resource_new_resname(int j
, const char *resname
, resource_type type
)
1384 struct config_resource
*new;
1387 i
= devtab
[j
].resource_count
;
1388 new = kmalloc((i
+ 1) * sizeof(*new), M_TEMP
, M_INTWAIT
| M_ZERO
);
1391 if (devtab
[j
].resources
&& i
> 0)
1392 bcopy(devtab
[j
].resources
, new, i
* sizeof(*new));
1393 new[i
].name
= kmalloc(strlen(resname
) + 1, M_TEMP
, M_INTWAIT
);
1394 if (new[i
].name
== NULL
) {
1398 strcpy(new[i
].name
, resname
);
1400 if (devtab
[j
].resources
)
1401 kfree(devtab
[j
].resources
, M_TEMP
);
1402 devtab
[j
].resources
= new;
1403 devtab
[j
].resource_count
= i
+ 1;
1408 resource_match_string(int i
, const char *resname
, const char *value
)
1411 struct config_resource
*res
;
1413 for (j
= 0, res
= devtab
[i
].resources
;
1414 j
< devtab
[i
].resource_count
; j
++, res
++)
1415 if (!strcmp(res
->name
, resname
)
1416 && res
->type
== RES_STRING
1417 && !strcmp(res
->u
.stringval
, value
))
1423 resource_find(const char *name
, int unit
, const char *resname
,
1424 struct config_resource
**result
)
1427 struct config_resource
*res
;
1430 * First check specific instances, then generic.
1432 for (i
= 0; i
< devtab_count
; i
++) {
1433 if (devtab
[i
].unit
< 0)
1435 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
1436 res
= devtab
[i
].resources
;
1437 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
1438 if (!strcmp(res
->name
, resname
)) {
1444 for (i
= 0; i
< devtab_count
; i
++) {
1445 if (devtab
[i
].unit
>= 0)
1447 /* XXX should this `&& devtab[i].unit == unit' be here? */
1448 /* XXX if so, then the generic match does nothing */
1449 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
1450 res
= devtab
[i
].resources
;
1451 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
1452 if (!strcmp(res
->name
, resname
)) {
1462 resource_int_value(const char *name
, int unit
, const char *resname
, int *result
)
1465 struct config_resource
*res
;
1467 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
1469 if (res
->type
!= RES_INT
)
1471 *result
= res
->u
.intval
;
1476 resource_long_value(const char *name
, int unit
, const char *resname
,
1480 struct config_resource
*res
;
1482 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
1484 if (res
->type
!= RES_LONG
)
1486 *result
= res
->u
.longval
;
1491 resource_string_value(const char *name
, int unit
, const char *resname
,
1495 struct config_resource
*res
;
1497 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
1499 if (res
->type
!= RES_STRING
)
1501 *result
= res
->u
.stringval
;
1506 resource_query_string(int i
, const char *resname
, const char *value
)
1512 for (; i
< devtab_count
; i
++)
1513 if (resource_match_string(i
, resname
, value
) >= 0)
1519 resource_locate(int i
, const char *resname
)
1525 for (; i
< devtab_count
; i
++)
1526 if (!strcmp(devtab
[i
].name
, resname
))
1532 resource_count(void)
1534 return(devtab_count
);
1538 resource_query_name(int i
)
1540 return(devtab
[i
].name
);
1544 resource_query_unit(int i
)
1546 return(devtab
[i
].unit
);
1550 resource_create(const char *name
, int unit
, const char *resname
,
1551 resource_type type
, struct config_resource
**result
)
1554 struct config_resource
*res
= NULL
;
1556 for (i
= 0; i
< devtab_count
; i
++)
1557 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
1558 res
= devtab
[i
].resources
;
1562 i
= resource_new_name(name
, unit
);
1565 res
= devtab
[i
].resources
;
1567 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
1568 if (!strcmp(res
->name
, resname
)) {
1572 j
= resource_new_resname(i
, resname
, type
);
1575 res
= &devtab
[i
].resources
[j
];
1581 resource_set_int(const char *name
, int unit
, const char *resname
, int value
)
1584 struct config_resource
*res
;
1586 error
= resource_create(name
, unit
, resname
, RES_INT
, &res
);
1589 if (res
->type
!= RES_INT
)
1591 res
->u
.intval
= value
;
1596 resource_set_long(const char *name
, int unit
, const char *resname
, long value
)
1599 struct config_resource
*res
;
1601 error
= resource_create(name
, unit
, resname
, RES_LONG
, &res
);
1604 if (res
->type
!= RES_LONG
)
1606 res
->u
.longval
= value
;
1611 resource_set_string(const char *name
, int unit
, const char *resname
,
1615 struct config_resource
*res
;
1617 error
= resource_create(name
, unit
, resname
, RES_STRING
, &res
);
1620 if (res
->type
!= RES_STRING
)
1622 if (res
->u
.stringval
)
1623 kfree(res
->u
.stringval
, M_TEMP
);
1624 res
->u
.stringval
= kmalloc(strlen(value
) + 1, M_TEMP
, M_INTWAIT
);
1625 if (res
->u
.stringval
== NULL
)
1627 strcpy(res
->u
.stringval
, value
);
1632 resource_cfgload(void *dummy __unused
)
1634 struct config_resource
*res
, *cfgres
;
1637 char *name
, *resname
;
1641 int config_devtab_count
;
1643 config_devtab_count
= devtab_count
;
1647 for (i
= 0; i
< config_devtab_count
; i
++) {
1648 name
= config_devtab
[i
].name
;
1649 unit
= config_devtab
[i
].unit
;
1651 for (j
= 0; j
< config_devtab
[i
].resource_count
; j
++) {
1652 cfgres
= config_devtab
[i
].resources
;
1653 resname
= cfgres
[j
].name
;
1654 type
= cfgres
[j
].type
;
1655 error
= resource_create(name
, unit
, resname
, type
,
1658 kprintf("create resource %s%d: error %d\n",
1662 if (res
->type
!= type
) {
1663 kprintf("type mismatch %s%d: %d != %d\n",
1664 name
, unit
, res
->type
, type
);
1669 res
->u
.intval
= cfgres
[j
].u
.intval
;
1672 res
->u
.longval
= cfgres
[j
].u
.longval
;
1675 if (res
->u
.stringval
)
1676 kfree(res
->u
.stringval
, M_TEMP
);
1677 stringval
= cfgres
[j
].u
.stringval
;
1678 res
->u
.stringval
= kmalloc(strlen(stringval
) + 1,
1680 if (res
->u
.stringval
== NULL
)
1682 strcpy(res
->u
.stringval
, stringval
);
1685 panic("unknown resource type %d", type
);
1690 SYSINIT(cfgload
, SI_BOOT1_POST
, SI_ORDER_ANY
+ 50, resource_cfgload
, 0)
1693 /*======================================*/
1695 * Some useful method implementations to make life easier for bus drivers.
1699 resource_list_init(struct resource_list
*rl
)
1705 resource_list_free(struct resource_list
*rl
)
1707 struct resource_list_entry
*rle
;
1709 while ((rle
= SLIST_FIRST(rl
)) != NULL
) {
1711 panic("resource_list_free: resource entry is busy");
1712 SLIST_REMOVE_HEAD(rl
, link
);
1718 resource_list_add(struct resource_list
*rl
,
1720 u_long start
, u_long end
, u_long count
)
1722 struct resource_list_entry
*rle
;
1724 rle
= resource_list_find(rl
, type
, rid
);
1726 rle
= kmalloc(sizeof(struct resource_list_entry
), M_BUS
,
1729 panic("resource_list_add: can't record entry");
1730 SLIST_INSERT_HEAD(rl
, rle
, link
);
1737 panic("resource_list_add: resource entry is busy");
1744 struct resource_list_entry
*
1745 resource_list_find(struct resource_list
*rl
,
1748 struct resource_list_entry
*rle
;
1750 SLIST_FOREACH(rle
, rl
, link
)
1751 if (rle
->type
== type
&& rle
->rid
== rid
)
1757 resource_list_delete(struct resource_list
*rl
,
1760 struct resource_list_entry
*rle
= resource_list_find(rl
, type
, rid
);
1763 SLIST_REMOVE(rl
, rle
, resource_list_entry
, link
);
1769 resource_list_alloc(struct resource_list
*rl
,
1770 device_t bus
, device_t child
,
1772 u_long start
, u_long end
,
1773 u_long count
, u_int flags
)
1775 struct resource_list_entry
*rle
= 0;
1776 int passthrough
= (device_get_parent(child
) != bus
);
1777 int isdefault
= (start
== 0UL && end
== ~0UL);
1780 return(BUS_ALLOC_RESOURCE(device_get_parent(bus
), child
,
1782 start
, end
, count
, flags
));
1785 rle
= resource_list_find(rl
, type
, *rid
);
1788 return(0); /* no resource of that type/rid */
1790 panic("resource_list_alloc: resource entry is busy");
1794 count
= max(count
, rle
->count
);
1795 end
= max(rle
->end
, start
+ count
- 1);
1798 rle
->res
= BUS_ALLOC_RESOURCE(device_get_parent(bus
), child
,
1799 type
, rid
, start
, end
, count
, flags
);
1802 * Record the new range.
1805 rle
->start
= rman_get_start(rle
->res
);
1806 rle
->end
= rman_get_end(rle
->res
);
1814 resource_list_release(struct resource_list
*rl
,
1815 device_t bus
, device_t child
,
1816 int type
, int rid
, struct resource
*res
)
1818 struct resource_list_entry
*rle
= 0;
1819 int passthrough
= (device_get_parent(child
) != bus
);
1823 return(BUS_RELEASE_RESOURCE(device_get_parent(bus
), child
,
1827 rle
= resource_list_find(rl
, type
, rid
);
1830 panic("resource_list_release: can't find resource");
1832 panic("resource_list_release: resource entry is not busy");
1834 error
= BUS_RELEASE_RESOURCE(device_get_parent(bus
), child
,
1844 resource_list_print_type(struct resource_list
*rl
, const char *name
, int type
,
1847 struct resource_list_entry
*rle
;
1848 int printed
, retval
;
1852 /* Yes, this is kinda cheating */
1853 SLIST_FOREACH(rle
, rl
, link
) {
1854 if (rle
->type
== type
) {
1856 retval
+= kprintf(" %s ", name
);
1858 retval
+= kprintf(",");
1860 retval
+= kprintf(format
, rle
->start
);
1861 if (rle
->count
> 1) {
1862 retval
+= kprintf("-");
1863 retval
+= kprintf(format
, rle
->start
+
1872 * Generic driver/device identify functions. These will install a device
1873 * rendezvous point under the parent using the same name as the driver
1874 * name, which will at a later time be probed and attached.
1876 * These functions are used when the parent does not 'scan' its bus for
1877 * matching devices, or for the particular devices using these functions,
1878 * or when the device is a pseudo or synthesized device (such as can be
1879 * found under firewire and ppbus).
1882 bus_generic_identify(driver_t
*driver
, device_t parent
)
1884 if (parent
->state
== DS_ATTACHED
)
1886 BUS_ADD_CHILD(parent
, parent
, 0, driver
->name
, -1);
1891 bus_generic_identify_sameunit(driver_t
*driver
, device_t parent
)
1893 if (parent
->state
== DS_ATTACHED
)
1895 BUS_ADD_CHILD(parent
, parent
, 0, driver
->name
, device_get_unit(parent
));
1900 * Call DEVICE_IDENTIFY for each driver.
1903 bus_generic_probe(device_t dev
)
1905 devclass_t dc
= dev
->devclass
;
1908 TAILQ_FOREACH(dl
, &dc
->drivers
, link
) {
1909 DEVICE_IDENTIFY(dl
->driver
, dev
);
1916 * This is an aweful hack due to the isa bus and autoconf code not
1917 * probing the ISA devices until after everything else has configured.
1918 * The ISA bus did a dummy attach long ago so we have to set it back
1919 * to an earlier state so the probe thinks its the initial probe and
1922 * XXX remove by properly defering the ISA bus scan.
1925 bus_generic_probe_hack(device_t dev
)
1927 if (dev
->state
== DS_ATTACHED
) {
1928 dev
->state
= DS_ALIVE
;
1929 bus_generic_probe(dev
);
1930 dev
->state
= DS_ATTACHED
;
1936 bus_generic_attach(device_t dev
)
1940 TAILQ_FOREACH(child
, &dev
->children
, link
) {
1941 device_probe_and_attach(child
);
1948 bus_generic_detach(device_t dev
)
1953 if (dev
->state
!= DS_ATTACHED
)
1956 TAILQ_FOREACH(child
, &dev
->children
, link
)
1957 if ((error
= device_detach(child
)) != 0)
1964 bus_generic_shutdown(device_t dev
)
1968 TAILQ_FOREACH(child
, &dev
->children
, link
)
1969 device_shutdown(child
);
1975 bus_generic_suspend(device_t dev
)
1978 device_t child
, child2
;
1980 TAILQ_FOREACH(child
, &dev
->children
, link
) {
1981 error
= DEVICE_SUSPEND(child
);
1983 for (child2
= TAILQ_FIRST(&dev
->children
);
1984 child2
&& child2
!= child
;
1985 child2
= TAILQ_NEXT(child2
, link
))
1986 DEVICE_RESUME(child2
);
1994 bus_generic_resume(device_t dev
)
1998 TAILQ_FOREACH(child
, &dev
->children
, link
)
1999 DEVICE_RESUME(child
);
2000 /* if resume fails, there's nothing we can usefully do... */
2006 bus_print_child_header(device_t dev
, device_t child
)
2010 if (device_get_desc(child
))
2011 retval
+= device_printf(child
, "<%s>", device_get_desc(child
));
2013 retval
+= kprintf("%s", device_get_nameunit(child
));
2015 if (child
->state
!= DS_ATTACHED
)
2016 kprintf(" [tentative]");
2018 kprintf(" [attached!]");
2024 bus_print_child_footer(device_t dev
, device_t child
)
2026 return(kprintf(" on %s\n", device_get_nameunit(dev
)));
2030 bus_generic_add_child(device_t dev
, device_t child
, int order
,
2031 const char *name
, int unit
)
2034 dev
= BUS_ADD_CHILD(dev
->parent
, child
, order
, name
, unit
);
2036 dev
= device_add_child_ordered(child
, order
, name
, unit
);
2042 bus_generic_print_child(device_t dev
, device_t child
)
2046 retval
+= bus_print_child_header(dev
, child
);
2047 retval
+= bus_print_child_footer(dev
, child
);
2053 bus_generic_read_ivar(device_t dev
, device_t child
, int index
,
2059 error
= BUS_READ_IVAR(dev
->parent
, child
, index
, result
);
2066 bus_generic_write_ivar(device_t dev
, device_t child
, int index
,
2072 error
= BUS_WRITE_IVAR(dev
->parent
, child
, index
, value
);
2079 * Resource list are used for iterations, do not recurse.
2081 struct resource_list
*
2082 bus_generic_get_resource_list(device_t dev
, device_t child
)
2088 bus_generic_driver_added(device_t dev
, driver_t
*driver
)
2092 DEVICE_IDENTIFY(driver
, dev
);
2093 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2094 if (child
->state
== DS_NOTPRESENT
)
2095 device_probe_and_attach(child
);
2100 bus_generic_setup_intr(device_t dev
, device_t child
, struct resource
*irq
,
2101 int flags
, driver_intr_t
*intr
, void *arg
,
2102 void **cookiep
, lwkt_serialize_t serializer
)
2104 /* Propagate up the bus hierarchy until someone handles it. */
2106 return(BUS_SETUP_INTR(dev
->parent
, child
, irq
, flags
,
2107 intr
, arg
, cookiep
, serializer
));
2113 bus_generic_teardown_intr(device_t dev
, device_t child
, struct resource
*irq
,
2116 /* Propagate up the bus hierarchy until someone handles it. */
2118 return(BUS_TEARDOWN_INTR(dev
->parent
, child
, irq
, cookie
));
2124 bus_generic_disable_intr(device_t dev
, device_t child
, void *cookie
)
2127 return(BUS_DISABLE_INTR(dev
->parent
, child
, cookie
));
2133 bus_generic_enable_intr(device_t dev
, device_t child
, void *cookie
)
2136 BUS_ENABLE_INTR(dev
->parent
, child
, cookie
);
2140 bus_generic_config_intr(device_t dev
, int irq
, enum intr_trigger trig
,
2141 enum intr_polarity pol
)
2143 /* Propagate up the bus hierarchy until someone handles it. */
2145 return(BUS_CONFIG_INTR(dev
->parent
, irq
, trig
, pol
));
2151 bus_generic_alloc_resource(device_t dev
, device_t child
, int type
, int *rid
,
2152 u_long start
, u_long end
, u_long count
, u_int flags
)
2154 /* Propagate up the bus hierarchy until someone handles it. */
2156 return(BUS_ALLOC_RESOURCE(dev
->parent
, child
, type
, rid
,
2157 start
, end
, count
, flags
));
2163 bus_generic_release_resource(device_t dev
, device_t child
, int type
, int rid
,
2166 /* Propagate up the bus hierarchy until someone handles it. */
2168 return(BUS_RELEASE_RESOURCE(dev
->parent
, child
, type
, rid
, r
));
2174 bus_generic_activate_resource(device_t dev
, device_t child
, int type
, int rid
,
2177 /* Propagate up the bus hierarchy until someone handles it. */
2179 return(BUS_ACTIVATE_RESOURCE(dev
->parent
, child
, type
, rid
, r
));
2185 bus_generic_deactivate_resource(device_t dev
, device_t child
, int type
,
2186 int rid
, struct resource
*r
)
2188 /* Propagate up the bus hierarchy until someone handles it. */
2190 return(BUS_DEACTIVATE_RESOURCE(dev
->parent
, child
, type
, rid
,
2197 bus_generic_get_resource(device_t dev
, device_t child
, int type
, int rid
,
2198 u_long
*startp
, u_long
*countp
)
2204 error
= BUS_GET_RESOURCE(dev
->parent
, child
, type
, rid
,
2211 bus_generic_set_resource(device_t dev
, device_t child
, int type
, int rid
,
2212 u_long start
, u_long count
)
2218 error
= BUS_SET_RESOURCE(dev
->parent
, child
, type
, rid
,
2225 bus_generic_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
2228 BUS_DELETE_RESOURCE(dev
, child
, type
, rid
);
2232 bus_generic_rl_get_resource(device_t dev
, device_t child
, int type
, int rid
,
2233 u_long
*startp
, u_long
*countp
)
2235 struct resource_list
*rl
= NULL
;
2236 struct resource_list_entry
*rle
= NULL
;
2238 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2242 rle
= resource_list_find(rl
, type
, rid
);
2247 *startp
= rle
->start
;
2249 *countp
= rle
->count
;
2255 bus_generic_rl_set_resource(device_t dev
, device_t child
, int type
, int rid
,
2256 u_long start
, u_long count
)
2258 struct resource_list
*rl
= NULL
;
2260 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2264 resource_list_add(rl
, type
, rid
, start
, (start
+ count
- 1), count
);
2270 bus_generic_rl_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
2272 struct resource_list
*rl
= NULL
;
2274 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2278 resource_list_delete(rl
, type
, rid
);
2282 bus_generic_rl_release_resource(device_t dev
, device_t child
, int type
,
2283 int rid
, struct resource
*r
)
2285 struct resource_list
*rl
= NULL
;
2287 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2291 return(resource_list_release(rl
, dev
, child
, type
, rid
, r
));
2295 bus_generic_rl_alloc_resource(device_t dev
, device_t child
, int type
,
2296 int *rid
, u_long start
, u_long end
, u_long count
, u_int flags
)
2298 struct resource_list
*rl
= NULL
;
2300 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2304 return(resource_list_alloc(rl
, dev
, child
, type
, rid
,
2305 start
, end
, count
, flags
));
2309 bus_generic_child_present(device_t bus
, device_t child
)
2311 return(BUS_CHILD_PRESENT(device_get_parent(bus
), bus
));
2316 * Some convenience functions to make it easier for drivers to use the
2317 * resource-management functions. All these really do is hide the
2318 * indirection through the parent's method table, making for slightly
2319 * less-wordy code. In the future, it might make sense for this code
2320 * to maintain some sort of a list of resources allocated by each device.
2323 bus_alloc_resources(device_t dev
, struct resource_spec
*rs
,
2324 struct resource
**res
)
2328 for (i
= 0; rs
[i
].type
!= -1; i
++)
2330 for (i
= 0; rs
[i
].type
!= -1; i
++) {
2331 res
[i
] = bus_alloc_resource_any(dev
,
2332 rs
[i
].type
, &rs
[i
].rid
, rs
[i
].flags
);
2333 if (res
[i
] == NULL
) {
2334 bus_release_resources(dev
, rs
, res
);
2342 bus_release_resources(device_t dev
, const struct resource_spec
*rs
,
2343 struct resource
**res
)
2347 for (i
= 0; rs
[i
].type
!= -1; i
++)
2348 if (res
[i
] != NULL
) {
2349 bus_release_resource(
2350 dev
, rs
[i
].type
, rs
[i
].rid
, res
[i
]);
2356 bus_alloc_resource(device_t dev
, int type
, int *rid
, u_long start
, u_long end
,
2357 u_long count
, u_int flags
)
2359 if (dev
->parent
== 0)
2361 return(BUS_ALLOC_RESOURCE(dev
->parent
, dev
, type
, rid
, start
, end
,
2366 bus_activate_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
2368 if (dev
->parent
== 0)
2370 return(BUS_ACTIVATE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
2374 bus_deactivate_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
2376 if (dev
->parent
== 0)
2378 return(BUS_DEACTIVATE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
2382 bus_release_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
2384 if (dev
->parent
== 0)
2386 return(BUS_RELEASE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
2390 bus_setup_intr(device_t dev
, struct resource
*r
, int flags
,
2391 driver_intr_t handler
, void *arg
,
2392 void **cookiep
, lwkt_serialize_t serializer
)
2394 if (dev
->parent
== 0)
2396 return(BUS_SETUP_INTR(dev
->parent
, dev
, r
, flags
, handler
, arg
,
2397 cookiep
, serializer
));
2401 bus_teardown_intr(device_t dev
, struct resource
*r
, void *cookie
)
2403 if (dev
->parent
== 0)
2405 return(BUS_TEARDOWN_INTR(dev
->parent
, dev
, r
, cookie
));
2409 bus_enable_intr(device_t dev
, void *cookie
)
2412 BUS_ENABLE_INTR(dev
->parent
, dev
, cookie
);
2416 bus_disable_intr(device_t dev
, void *cookie
)
2419 return(BUS_DISABLE_INTR(dev
->parent
, dev
, cookie
));
2425 bus_set_resource(device_t dev
, int type
, int rid
,
2426 u_long start
, u_long count
)
2428 return(BUS_SET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
2433 bus_get_resource(device_t dev
, int type
, int rid
,
2434 u_long
*startp
, u_long
*countp
)
2436 return(BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
2441 bus_get_resource_start(device_t dev
, int type
, int rid
)
2443 u_long start
, count
;
2446 error
= BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
2454 bus_get_resource_count(device_t dev
, int type
, int rid
)
2456 u_long start
, count
;
2459 error
= BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
2467 bus_delete_resource(device_t dev
, int type
, int rid
)
2469 BUS_DELETE_RESOURCE(device_get_parent(dev
), dev
, type
, rid
);
2473 bus_child_present(device_t child
)
2475 return (BUS_CHILD_PRESENT(device_get_parent(child
), child
));
2479 bus_child_pnpinfo_str(device_t child
, char *buf
, size_t buflen
)
2483 parent
= device_get_parent(child
);
2484 if (parent
== NULL
) {
2488 return (BUS_CHILD_PNPINFO_STR(parent
, child
, buf
, buflen
));
2492 bus_child_location_str(device_t child
, char *buf
, size_t buflen
)
2496 parent
= device_get_parent(child
);
2497 if (parent
== NULL
) {
2501 return (BUS_CHILD_LOCATION_STR(parent
, child
, buf
, buflen
));
2505 root_print_child(device_t dev
, device_t child
)
2511 root_setup_intr(device_t dev
, device_t child
, driver_intr_t
*intr
, void *arg
,
2512 void **cookiep
, lwkt_serialize_t serializer
)
2515 * If an interrupt mapping gets to here something bad has happened.
2517 panic("root_setup_intr");
2521 * If we get here, assume that the device is permanant and really is
2522 * present in the system. Removable bus drivers are expected to intercept
2523 * this call long before it gets here. We return -1 so that drivers that
2524 * really care can check vs -1 or some ERRNO returned higher in the food
2528 root_child_present(device_t dev
, device_t child
)
2534 * XXX NOTE! other defaults may be set in bus_if.m
2536 static kobj_method_t root_methods
[] = {
2537 /* Device interface */
2538 KOBJMETHOD(device_shutdown
, bus_generic_shutdown
),
2539 KOBJMETHOD(device_suspend
, bus_generic_suspend
),
2540 KOBJMETHOD(device_resume
, bus_generic_resume
),
2543 KOBJMETHOD(bus_add_child
, bus_generic_add_child
),
2544 KOBJMETHOD(bus_print_child
, root_print_child
),
2545 KOBJMETHOD(bus_read_ivar
, bus_generic_read_ivar
),
2546 KOBJMETHOD(bus_write_ivar
, bus_generic_write_ivar
),
2547 KOBJMETHOD(bus_setup_intr
, root_setup_intr
),
2548 KOBJMETHOD(bus_child_present
, root_child_present
),
2553 static driver_t root_driver
= {
2560 devclass_t root_devclass
;
2563 root_bus_module_handler(module_t mod
, int what
, void* arg
)
2567 root_bus
= make_device(NULL
, "root", 0);
2568 root_bus
->desc
= "System root bus";
2569 kobj_init((kobj_t
) root_bus
, (kobj_class_t
) &root_driver
);
2570 root_bus
->driver
= &root_driver
;
2571 root_bus
->state
= DS_ALIVE
;
2572 root_devclass
= devclass_find_internal("root", NULL
, FALSE
);
2576 device_shutdown(root_bus
);
2583 static moduledata_t root_bus_mod
= {
2585 root_bus_module_handler
,
2588 DECLARE_MODULE(rootbus
, root_bus_mod
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
);
2591 root_bus_configure(void)
2599 * handle device_identify based device attachments to the root_bus
2600 * (typically nexus).
2602 bus_generic_probe(root_bus
);
2605 * Probe and attach the devices under root_bus.
2607 TAILQ_FOREACH(dev
, &root_bus
->children
, link
) {
2608 device_probe_and_attach(dev
);
2612 * Wait for all asynchronous attaches to complete. If we don't
2613 * our legacy ISA bus scan could steal device unit numbers or
2617 if (numasyncthreads
)
2618 kprintf("Waiting for async drivers to attach\n");
2619 while (numasyncthreads
> 0) {
2620 if (tsleep(&numasyncthreads
, 0, "rootbus", hz
) == EWOULDBLOCK
)
2622 if (warncount
== 0) {
2623 kprintf("Warning: Still waiting for %d "
2624 "drivers to attach\n", numasyncthreads
);
2625 } else if (warncount
== -30) {
2626 kprintf("Giving up on %d drivers\n", numasyncthreads
);
2630 root_bus
->state
= DS_ATTACHED
;
2634 driver_module_handler(module_t mod
, int what
, void *arg
)
2637 struct driver_module_data
*dmd
;
2638 devclass_t bus_devclass
;
2639 kobj_class_t driver
;
2640 const char *parentname
;
2642 dmd
= (struct driver_module_data
*)arg
;
2643 bus_devclass
= devclass_find_internal(dmd
->dmd_busname
, NULL
, TRUE
);
2648 if (dmd
->dmd_chainevh
)
2649 error
= dmd
->dmd_chainevh(mod
,what
,dmd
->dmd_chainarg
);
2651 driver
= dmd
->dmd_driver
;
2652 PDEBUG(("Loading module: driver %s on bus %s",
2653 DRIVERNAME(driver
), dmd
->dmd_busname
));
2656 * If the driver has any base classes, make the
2657 * devclass inherit from the devclass of the driver's
2658 * first base class. This will allow the system to
2659 * search for drivers in both devclasses for children
2660 * of a device using this driver.
2662 if (driver
->baseclasses
)
2663 parentname
= driver
->baseclasses
[0]->name
;
2666 *dmd
->dmd_devclass
= devclass_find_internal(driver
->name
,
2669 error
= devclass_add_driver(bus_devclass
, driver
);
2675 PDEBUG(("Unloading module: driver %s from bus %s",
2676 DRIVERNAME(dmd
->dmd_driver
), dmd
->dmd_busname
));
2677 error
= devclass_delete_driver(bus_devclass
, dmd
->dmd_driver
);
2679 if (!error
&& dmd
->dmd_chainevh
)
2680 error
= dmd
->dmd_chainevh(mod
,what
,dmd
->dmd_chainarg
);
2690 * The _short versions avoid iteration by not calling anything that prints
2691 * more than oneliners. I love oneliners.
2695 print_device_short(device_t dev
, int indent
)
2700 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2701 dev
->unit
, dev
->desc
,
2702 (dev
->parent
? "":"no "),
2703 (TAILQ_EMPTY(&dev
->children
)? "no ":""),
2704 (dev
->flags
&DF_ENABLED
? "enabled,":"disabled,"),
2705 (dev
->flags
&DF_FIXEDCLASS
? "fixed,":""),
2706 (dev
->flags
&DF_WILDCARD
? "wildcard,":""),
2707 (dev
->flags
&DF_DESCMALLOCED
? "descmalloced,":""),
2708 (dev
->ivars
? "":"no "),
2709 (dev
->softc
? "":"no "),
2714 print_device(device_t dev
, int indent
)
2719 print_device_short(dev
, indent
);
2721 indentprintf(("Parent:\n"));
2722 print_device_short(dev
->parent
, indent
+1);
2723 indentprintf(("Driver:\n"));
2724 print_driver_short(dev
->driver
, indent
+1);
2725 indentprintf(("Devclass:\n"));
2726 print_devclass_short(dev
->devclass
, indent
+1);
2730 * Print the device and all its children (indented).
2733 print_device_tree_short(device_t dev
, int indent
)
2740 print_device_short(dev
, indent
);
2742 TAILQ_FOREACH(child
, &dev
->children
, link
)
2743 print_device_tree_short(child
, indent
+1);
2747 * Print the device and all its children (indented).
2750 print_device_tree(device_t dev
, int indent
)
2757 print_device(dev
, indent
);
2759 TAILQ_FOREACH(child
, &dev
->children
, link
)
2760 print_device_tree(child
, indent
+1);
2764 print_driver_short(driver_t
*driver
, int indent
)
2769 indentprintf(("driver %s: softc size = %d\n",
2770 driver
->name
, driver
->size
));
2774 print_driver(driver_t
*driver
, int indent
)
2779 print_driver_short(driver
, indent
);
2784 print_driver_list(driver_list_t drivers
, int indent
)
2786 driverlink_t driver
;
2788 TAILQ_FOREACH(driver
, &drivers
, link
)
2789 print_driver(driver
->driver
, indent
);
2793 print_devclass_short(devclass_t dc
, int indent
)
2798 indentprintf(("devclass %s: max units = %d\n", dc
->name
, dc
->maxunit
));
2802 print_devclass(devclass_t dc
, int indent
)
2809 print_devclass_short(dc
, indent
);
2810 indentprintf(("Drivers:\n"));
2811 print_driver_list(dc
->drivers
, indent
+1);
2813 indentprintf(("Devices:\n"));
2814 for (i
= 0; i
< dc
->maxunit
; i
++)
2816 print_device(dc
->devices
[i
], indent
+1);
2820 print_devclass_list_short(void)
2824 kprintf("Short listing of devclasses, drivers & devices:\n");
2825 TAILQ_FOREACH(dc
, &devclasses
, link
) {
2826 print_devclass_short(dc
, 0);
2831 print_devclass_list(void)
2835 kprintf("Full listing of devclasses, drivers & devices:\n");
2836 TAILQ_FOREACH(dc
, &devclasses
, link
) {
2837 print_devclass(dc
, 0);
2844 * Check to see if a device is disabled via a disabled hint.
2847 resource_disabled(const char *name
, int unit
)
2851 error
= resource_int_value(name
, unit
, "disabled", &value
);