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 "%p/%p %d %p/%p\n", dev
->devclass
, dc
, dev
->unit
,
1154 dc
->devices
[dev
->unit
], dev
);
1156 dc
->devices
[dev
->unit
] = NULL
;
1157 if (dev
->flags
& DF_WILDCARD
)
1159 dev
->devclass
= NULL
;
1160 kfree(dev
->nameunit
, M_BUS
);
1161 dev
->nameunit
= NULL
;
1167 make_device(device_t parent
, const char *name
, int unit
)
1172 PDEBUG(("%s at %s as unit %d", name
, DEVICENAME(parent
), unit
));
1175 dc
= devclass_find_internal(name
, NULL
, TRUE
);
1177 kprintf("make_device: can't find device class %s\n", name
);
1183 dev
= kmalloc(sizeof(struct bsd_device
), M_BUS
, M_INTWAIT
| M_ZERO
);
1187 dev
->parent
= parent
;
1188 TAILQ_INIT(&dev
->children
);
1189 kobj_init((kobj_t
) dev
, &null_class
);
1191 dev
->devclass
= NULL
;
1193 dev
->nameunit
= NULL
;
1197 dev
->flags
= DF_ENABLED
;
1200 dev
->flags
|= DF_WILDCARD
;
1202 dev
->flags
|= DF_FIXEDCLASS
;
1203 if (devclass_add_device(dc
, dev
) != 0) {
1204 kobj_delete((kobj_t
)dev
, M_BUS
);
1211 dev
->state
= DS_NOTPRESENT
;
1213 TAILQ_INSERT_TAIL(&bus_data_devices
, dev
, devlink
);
1214 bus_data_generation_update();
1220 device_print_child(device_t dev
, device_t child
)
1224 if (device_is_alive(child
))
1225 retval
+= BUS_PRINT_CHILD(dev
, child
);
1227 retval
+= device_printf(child
, " not found\n");
1233 device_add_child(device_t dev
, const char *name
, int unit
)
1235 return device_add_child_ordered(dev
, 0, name
, unit
);
1239 device_add_child_ordered(device_t dev
, int order
, const char *name
, int unit
)
1244 PDEBUG(("%s at %s with order %d as unit %d", name
, DEVICENAME(dev
),
1247 child
= make_device(dev
, name
, unit
);
1250 child
->order
= order
;
1252 TAILQ_FOREACH(place
, &dev
->children
, link
) {
1253 if (place
->order
> order
)
1259 * The device 'place' is the first device whose order is
1260 * greater than the new child.
1262 TAILQ_INSERT_BEFORE(place
, child
, link
);
1265 * The new child's order is greater or equal to the order of
1266 * any existing device. Add the child to the tail of the list.
1268 TAILQ_INSERT_TAIL(&dev
->children
, child
, link
);
1271 bus_data_generation_update();
1276 device_delete_child(device_t dev
, device_t child
)
1279 device_t grandchild
;
1281 PDEBUG(("%s from %s", DEVICENAME(child
), DEVICENAME(dev
)));
1283 /* remove children first */
1284 while ( (grandchild
= TAILQ_FIRST(&child
->children
)) ) {
1285 error
= device_delete_child(child
, grandchild
);
1290 if ((error
= device_detach(child
)) != 0)
1292 if (child
->devclass
)
1293 devclass_delete_device(child
->devclass
, child
);
1294 TAILQ_REMOVE(&dev
->children
, child
, link
);
1295 TAILQ_REMOVE(&bus_data_devices
, child
, devlink
);
1296 kobj_delete((kobj_t
)child
, M_BUS
);
1298 bus_data_generation_update();
1303 * @brief Delete all children devices of the given device, if any.
1305 * This function deletes all children devices of the given device, if
1306 * any, using the device_delete_child() function for each device it
1307 * finds. If a child device cannot be deleted, this function will
1308 * return an error code.
1310 * @param dev the parent device
1313 * @retval non-zero a device would not detach
1316 device_delete_children(device_t dev
)
1321 PDEBUG(("Deleting all children of %s", DEVICENAME(dev
)));
1325 while ((child
= TAILQ_FIRST(&dev
->children
)) != NULL
) {
1326 error
= device_delete_child(dev
, child
);
1328 PDEBUG(("Failed deleting %s", DEVICENAME(child
)));
1336 * @brief Find a device given a unit number
1338 * This is similar to devclass_get_devices() but only searches for
1339 * devices which have @p dev as a parent.
1341 * @param dev the parent device to search
1342 * @param unit the unit number to search for. If the unit is -1,
1343 * return the first child of @p dev which has name
1344 * @p classname (that is, the one with the lowest unit.)
1346 * @returns the device with the given unit number or @c
1347 * NULL if there is no such device
1350 device_find_child(device_t dev
, const char *classname
, int unit
)
1355 dc
= devclass_find(classname
);
1360 child
= devclass_get_device(dc
, unit
);
1361 if (child
&& child
->parent
== dev
)
1364 for (unit
= 0; unit
< devclass_get_maxunit(dc
); unit
++) {
1365 child
= devclass_get_device(dc
, unit
);
1366 if (child
&& child
->parent
== dev
)
1374 first_matching_driver(devclass_t dc
, device_t dev
)
1377 return(devclass_find_driver_internal(dc
, dev
->devclass
->name
));
1379 return(TAILQ_FIRST(&dc
->drivers
));
1383 next_matching_driver(devclass_t dc
, device_t dev
, driverlink_t last
)
1385 if (dev
->devclass
) {
1387 for (dl
= TAILQ_NEXT(last
, link
); dl
; dl
= TAILQ_NEXT(dl
, link
))
1388 if (!strcmp(dev
->devclass
->name
, dl
->driver
->name
))
1392 return(TAILQ_NEXT(last
, link
));
1396 device_probe_child(device_t dev
, device_t child
)
1399 driverlink_t best
= NULL
;
1401 int result
, pri
= 0;
1402 int hasclass
= (child
->devclass
!= NULL
);
1406 panic("device_probe_child: parent device has no devclass");
1408 if (child
->state
== DS_ALIVE
)
1411 for (; dc
; dc
= dc
->parent
) {
1412 for (dl
= first_matching_driver(dc
, child
); dl
;
1413 dl
= next_matching_driver(dc
, child
, dl
)) {
1414 PDEBUG(("Trying %s", DRIVERNAME(dl
->driver
)));
1415 device_set_driver(child
, dl
->driver
);
1417 device_set_devclass(child
, dl
->driver
->name
);
1418 result
= DEVICE_PROBE(child
);
1420 device_set_devclass(child
, 0);
1423 * If the driver returns SUCCESS, there can be
1424 * no higher match for this device.
1433 * The driver returned an error so it
1434 * certainly doesn't match.
1437 device_set_driver(child
, NULL
);
1442 * A priority lower than SUCCESS, remember the
1443 * best matching driver. Initialise the value
1444 * of pri for the first match.
1446 if (best
== NULL
|| result
> pri
) {
1453 * If we have unambiguous match in this devclass,
1454 * don't look in the parent.
1456 if (best
&& pri
== 0)
1461 * If we found a driver, change state and initialise the devclass.
1464 if (!child
->devclass
)
1465 device_set_devclass(child
, best
->driver
->name
);
1466 device_set_driver(child
, best
->driver
);
1469 * A bit bogus. Call the probe method again to make
1470 * sure that we have the right description.
1472 DEVICE_PROBE(child
);
1475 bus_data_generation_update();
1476 child
->state
= DS_ALIVE
;
1484 device_probe_child_gpri(device_t dev
, device_t child
, u_int gpri
)
1487 driverlink_t best
= NULL
;
1489 int result
, pri
= 0;
1490 int hasclass
= (child
->devclass
!= NULL
);
1494 panic("device_probe_child: parent device has no devclass");
1496 if (child
->state
== DS_ALIVE
)
1499 for (; dc
; dc
= dc
->parent
) {
1500 for (dl
= first_matching_driver(dc
, child
); dl
;
1501 dl
= next_matching_driver(dc
, child
, dl
)) {
1503 * GPRI handling, only probe drivers with the
1506 if (dl
->driver
->gpri
!= gpri
)
1509 PDEBUG(("Trying %s", DRIVERNAME(dl
->driver
)));
1510 device_set_driver(child
, dl
->driver
);
1512 device_set_devclass(child
, dl
->driver
->name
);
1513 result
= DEVICE_PROBE(child
);
1515 device_set_devclass(child
, 0);
1518 * If the driver returns SUCCESS, there can be
1519 * no higher match for this device.
1528 * The driver returned an error so it
1529 * certainly doesn't match.
1532 device_set_driver(child
, NULL
);
1537 * A priority lower than SUCCESS, remember the
1538 * best matching driver. Initialise the value
1539 * of pri for the first match.
1541 if (best
== NULL
|| result
> pri
) {
1548 * If we have unambiguous match in this devclass,
1549 * don't look in the parent.
1551 if (best
&& pri
== 0)
1556 * If we found a driver, change state and initialise the devclass.
1559 if (!child
->devclass
)
1560 device_set_devclass(child
, best
->driver
->name
);
1561 device_set_driver(child
, best
->driver
);
1564 * A bit bogus. Call the probe method again to make
1565 * sure that we have the right description.
1567 DEVICE_PROBE(child
);
1570 bus_data_generation_update();
1571 child
->state
= DS_ALIVE
;
1579 device_get_parent(device_t dev
)
1585 device_get_children(device_t dev
, device_t
**devlistp
, int *devcountp
)
1592 TAILQ_FOREACH(child
, &dev
->children
, link
)
1595 list
= kmalloc(count
* sizeof(device_t
), M_TEMP
, M_INTWAIT
| M_ZERO
);
1598 TAILQ_FOREACH(child
, &dev
->children
, link
) {
1599 list
[count
] = child
;
1610 device_get_driver(device_t dev
)
1612 return(dev
->driver
);
1616 device_get_devclass(device_t dev
)
1618 return(dev
->devclass
);
1622 device_get_name(device_t dev
)
1625 return devclass_get_name(dev
->devclass
);
1630 device_get_nameunit(device_t dev
)
1632 return(dev
->nameunit
);
1636 device_get_unit(device_t dev
)
1642 device_get_desc(device_t dev
)
1648 device_get_flags(device_t dev
)
1650 return(dev
->devflags
);
1653 struct sysctl_ctx_list
*
1654 device_get_sysctl_ctx(device_t dev
)
1656 return (&dev
->sysctl_ctx
);
1660 device_get_sysctl_tree(device_t dev
)
1662 return (dev
->sysctl_tree
);
1666 device_print_prettyname(device_t dev
)
1668 const char *name
= device_get_name(dev
);
1671 return kprintf("unknown: ");
1673 return kprintf("%s%d: ", name
, device_get_unit(dev
));
1677 device_printf(device_t dev
, const char * fmt
, ...)
1682 retval
= device_print_prettyname(dev
);
1683 __va_start(ap
, fmt
);
1684 retval
+= kvprintf(fmt
, ap
);
1690 device_set_desc_internal(device_t dev
, const char* desc
, int copy
)
1692 if (dev
->desc
&& (dev
->flags
& DF_DESCMALLOCED
)) {
1693 kfree(dev
->desc
, M_BUS
);
1694 dev
->flags
&= ~DF_DESCMALLOCED
;
1699 dev
->desc
= kmalloc(strlen(desc
) + 1, M_BUS
, M_INTWAIT
);
1701 strcpy(dev
->desc
, desc
);
1702 dev
->flags
|= DF_DESCMALLOCED
;
1705 /* Avoid a -Wcast-qual warning */
1706 dev
->desc
= (char *)(uintptr_t) desc
;
1709 bus_data_generation_update();
1713 device_set_desc(device_t dev
, const char* desc
)
1715 device_set_desc_internal(dev
, desc
, FALSE
);
1719 device_set_desc_copy(device_t dev
, const char* desc
)
1721 device_set_desc_internal(dev
, desc
, TRUE
);
1725 device_set_flags(device_t dev
, uint32_t flags
)
1727 dev
->devflags
= flags
;
1731 device_get_softc(device_t dev
)
1737 device_set_softc(device_t dev
, void *softc
)
1739 if (dev
->softc
&& !(dev
->flags
& DF_EXTERNALSOFTC
))
1740 kfree(dev
->softc
, M_BUS
);
1743 dev
->flags
|= DF_EXTERNALSOFTC
;
1745 dev
->flags
&= ~DF_EXTERNALSOFTC
;
1749 device_set_async_attach(device_t dev
, int enable
)
1752 dev
->flags
|= DF_ASYNCPROBE
;
1754 dev
->flags
&= ~DF_ASYNCPROBE
;
1758 device_get_ivars(device_t dev
)
1764 device_set_ivars(device_t dev
, void * ivars
)
1773 device_get_state(device_t dev
)
1779 device_enable(device_t dev
)
1781 dev
->flags
|= DF_ENABLED
;
1785 device_disable(device_t dev
)
1787 dev
->flags
&= ~DF_ENABLED
;
1794 device_busy(device_t dev
)
1796 if (dev
->state
< DS_ATTACHED
)
1797 panic("device_busy: called for unattached device");
1798 if (dev
->busy
== 0 && dev
->parent
)
1799 device_busy(dev
->parent
);
1801 dev
->state
= DS_BUSY
;
1808 device_unbusy(device_t dev
)
1810 if (dev
->state
!= DS_BUSY
)
1811 panic("device_unbusy: called for non-busy device");
1813 if (dev
->busy
== 0) {
1815 device_unbusy(dev
->parent
);
1816 dev
->state
= DS_ATTACHED
;
1821 device_quiet(device_t dev
)
1823 dev
->flags
|= DF_QUIET
;
1827 device_verbose(device_t dev
)
1829 dev
->flags
&= ~DF_QUIET
;
1833 device_is_quiet(device_t dev
)
1835 return((dev
->flags
& DF_QUIET
) != 0);
1839 device_is_enabled(device_t dev
)
1841 return((dev
->flags
& DF_ENABLED
) != 0);
1845 device_is_alive(device_t dev
)
1847 return(dev
->state
>= DS_ALIVE
);
1851 device_is_attached(device_t dev
)
1853 return(dev
->state
>= DS_ATTACHED
);
1857 device_set_devclass(device_t dev
, const char *classname
)
1864 devclass_delete_device(dev
->devclass
, dev
);
1868 if (dev
->devclass
) {
1869 kprintf("device_set_devclass: device class already set\n");
1873 dc
= devclass_find_internal(classname
, NULL
, TRUE
);
1877 error
= devclass_add_device(dc
, dev
);
1879 bus_data_generation_update();
1884 device_set_driver(device_t dev
, driver_t
*driver
)
1886 if (dev
->state
>= DS_ATTACHED
)
1889 if (dev
->driver
== driver
)
1892 if (dev
->softc
&& !(dev
->flags
& DF_EXTERNALSOFTC
)) {
1893 kfree(dev
->softc
, M_BUS
);
1896 device_set_desc(dev
, NULL
);
1897 kobj_delete((kobj_t
) dev
, 0);
1898 dev
->driver
= driver
;
1900 kobj_init((kobj_t
) dev
, (kobj_class_t
) driver
);
1901 if (!(dev
->flags
& DF_EXTERNALSOFTC
))
1902 dev
->softc
= kmalloc(driver
->size
, M_BUS
,
1903 M_INTWAIT
| M_ZERO
);
1905 kobj_init((kobj_t
) dev
, &null_class
);
1908 bus_data_generation_update();
1913 device_probe_and_attach(device_t dev
)
1915 device_t bus
= dev
->parent
;
1918 if (dev
->state
>= DS_ALIVE
)
1921 if ((dev
->flags
& DF_ENABLED
) == 0) {
1923 device_print_prettyname(dev
);
1924 kprintf("not probed (disabled)\n");
1929 error
= device_probe_child(bus
, dev
);
1931 if (!(dev
->flags
& DF_DONENOMATCH
)) {
1932 BUS_PROBE_NOMATCH(bus
, dev
);
1934 dev
->flags
|= DF_DONENOMATCH
;
1940 * Output the exact device chain prior to the attach in case the
1941 * system locks up during attach, and generate the full info after
1942 * the attach so correct irq and other information is displayed.
1944 if (bootverbose
&& !device_is_quiet(dev
)) {
1947 kprintf("%s", device_get_nameunit(dev
));
1948 for (tmp
= dev
->parent
; tmp
; tmp
= tmp
->parent
)
1949 kprintf(".%s", device_get_nameunit(tmp
));
1952 if (!device_is_quiet(dev
))
1953 device_print_child(bus
, dev
);
1954 if ((dev
->flags
& DF_ASYNCPROBE
) && do_async_attach
) {
1955 kprintf("%s: probing asynchronously\n",
1956 device_get_nameunit(dev
));
1957 dev
->state
= DS_INPROGRESS
;
1958 device_attach_async(dev
);
1961 error
= device_doattach(dev
);
1967 device_probe_and_attach_gpri(device_t dev
, u_int gpri
)
1969 device_t bus
= dev
->parent
;
1972 if (dev
->state
>= DS_ALIVE
)
1975 if ((dev
->flags
& DF_ENABLED
) == 0) {
1977 device_print_prettyname(dev
);
1978 kprintf("not probed (disabled)\n");
1983 error
= device_probe_child_gpri(bus
, dev
, gpri
);
1986 if (!(dev
->flags
& DF_DONENOMATCH
)) {
1987 BUS_PROBE_NOMATCH(bus
, dev
);
1989 dev
->flags
|= DF_DONENOMATCH
;
1996 * Output the exact device chain prior to the attach in case the
1997 * system locks up during attach, and generate the full info after
1998 * the attach so correct irq and other information is displayed.
2000 if (bootverbose
&& !device_is_quiet(dev
)) {
2003 kprintf("%s", device_get_nameunit(dev
));
2004 for (tmp
= dev
->parent
; tmp
; tmp
= tmp
->parent
)
2005 kprintf(".%s", device_get_nameunit(tmp
));
2008 if (!device_is_quiet(dev
))
2009 device_print_child(bus
, dev
);
2010 if ((dev
->flags
& DF_ASYNCPROBE
) && do_async_attach
) {
2011 kprintf("%s: probing asynchronously\n",
2012 device_get_nameunit(dev
));
2013 dev
->state
= DS_INPROGRESS
;
2014 device_attach_async(dev
);
2017 error
= device_doattach(dev
);
2023 * Device is known to be alive, do the attach asynchronously.
2024 * However, serialize the attaches with the mp lock.
2027 device_attach_async(device_t dev
)
2031 atomic_add_int(&numasyncthreads
, 1);
2032 lwkt_create(device_attach_thread
, dev
, &td
, NULL
,
2033 0, 0, "%s", (dev
->desc
? dev
->desc
: "devattach"));
2037 device_attach_thread(void *arg
)
2041 (void)device_doattach(dev
);
2042 atomic_subtract_int(&numasyncthreads
, 1);
2043 wakeup(&numasyncthreads
);
2047 * Device is known to be alive, do the attach (synchronous or asynchronous)
2050 device_doattach(device_t dev
)
2052 device_t bus
= dev
->parent
;
2053 int hasclass
= (dev
->devclass
!= NULL
);
2056 device_sysctl_init(dev
);
2057 error
= DEVICE_ATTACH(dev
);
2059 dev
->state
= DS_ATTACHED
;
2060 if (bootverbose
&& !device_is_quiet(dev
))
2061 device_print_child(bus
, dev
);
2062 device_sysctl_update(dev
);
2065 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
2066 dev
->driver
->name
, dev
->unit
, error
);
2067 /* Unset the class that was set in device_probe_child */
2069 device_set_devclass(dev
, 0);
2070 device_set_driver(dev
, NULL
);
2071 dev
->state
= DS_NOTPRESENT
;
2072 device_sysctl_fini(dev
);
2078 device_detach(device_t dev
)
2082 PDEBUG(("%s", DEVICENAME(dev
)));
2083 if (dev
->state
== DS_BUSY
)
2085 if (dev
->state
!= DS_ATTACHED
)
2088 if ((error
= DEVICE_DETACH(dev
)) != 0)
2091 device_printf(dev
, "detached\n");
2093 BUS_CHILD_DETACHED(dev
->parent
, dev
);
2095 if (!(dev
->flags
& DF_FIXEDCLASS
))
2096 devclass_delete_device(dev
->devclass
, dev
);
2098 dev
->state
= DS_NOTPRESENT
;
2099 device_set_driver(dev
, NULL
);
2100 device_sysctl_fini(dev
);
2106 device_shutdown(device_t dev
)
2108 if (dev
->state
< DS_ATTACHED
)
2110 PDEBUG(("%s", DEVICENAME(dev
)));
2111 return DEVICE_SHUTDOWN(dev
);
2115 device_set_unit(device_t dev
, int unit
)
2120 dc
= device_get_devclass(dev
);
2121 if (unit
< dc
->maxunit
&& dc
->devices
[unit
])
2123 err
= devclass_delete_device(dc
, dev
);
2127 err
= devclass_add_device(dc
, dev
);
2131 bus_data_generation_update();
2135 /*======================================*/
2137 * Access functions for device resources.
2140 /* Supplied by config(8) in ioconf.c */
2141 extern struct config_device config_devtab
[];
2142 extern int devtab_count
;
2144 /* Runtime version */
2145 struct config_device
*devtab
= config_devtab
;
2148 resource_new_name(const char *name
, int unit
)
2150 struct config_device
*new;
2152 new = kmalloc((devtab_count
+ 1) * sizeof(*new), M_TEMP
,
2153 M_INTWAIT
| M_ZERO
);
2154 if (devtab
&& devtab_count
> 0)
2155 bcopy(devtab
, new, devtab_count
* sizeof(*new));
2156 new[devtab_count
].name
= kmalloc(strlen(name
) + 1, M_TEMP
, M_INTWAIT
);
2157 if (new[devtab_count
].name
== NULL
) {
2161 strcpy(new[devtab_count
].name
, name
);
2162 new[devtab_count
].unit
= unit
;
2163 new[devtab_count
].resource_count
= 0;
2164 new[devtab_count
].resources
= NULL
;
2165 if (devtab
&& devtab
!= config_devtab
)
2166 kfree(devtab
, M_TEMP
);
2168 return devtab_count
++;
2172 resource_new_resname(int j
, const char *resname
, resource_type type
)
2174 struct config_resource
*new;
2177 i
= devtab
[j
].resource_count
;
2178 new = kmalloc((i
+ 1) * sizeof(*new), M_TEMP
, M_INTWAIT
| M_ZERO
);
2179 if (devtab
[j
].resources
&& i
> 0)
2180 bcopy(devtab
[j
].resources
, new, i
* sizeof(*new));
2181 new[i
].name
= kmalloc(strlen(resname
) + 1, M_TEMP
, M_INTWAIT
);
2182 if (new[i
].name
== NULL
) {
2186 strcpy(new[i
].name
, resname
);
2188 if (devtab
[j
].resources
)
2189 kfree(devtab
[j
].resources
, M_TEMP
);
2190 devtab
[j
].resources
= new;
2191 devtab
[j
].resource_count
= i
+ 1;
2196 resource_match_string(int i
, const char *resname
, const char *value
)
2199 struct config_resource
*res
;
2201 for (j
= 0, res
= devtab
[i
].resources
;
2202 j
< devtab
[i
].resource_count
; j
++, res
++)
2203 if (!strcmp(res
->name
, resname
)
2204 && res
->type
== RES_STRING
2205 && !strcmp(res
->u
.stringval
, value
))
2211 resource_find(const char *name
, int unit
, const char *resname
,
2212 struct config_resource
**result
)
2215 struct config_resource
*res
;
2218 * First check specific instances, then generic.
2220 for (i
= 0; i
< devtab_count
; i
++) {
2221 if (devtab
[i
].unit
< 0)
2223 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
2224 res
= devtab
[i
].resources
;
2225 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
2226 if (!strcmp(res
->name
, resname
)) {
2232 for (i
= 0; i
< devtab_count
; i
++) {
2233 if (devtab
[i
].unit
>= 0)
2235 /* XXX should this `&& devtab[i].unit == unit' be here? */
2236 /* XXX if so, then the generic match does nothing */
2237 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
2238 res
= devtab
[i
].resources
;
2239 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
2240 if (!strcmp(res
->name
, resname
)) {
2250 resource_kenv(const char *name
, int unit
, const char *resname
, long *result
)
2256 * DragonFly style loader.conf hinting
2258 ksnprintf(buf
, sizeof(buf
), "%s%d.%s", name
, unit
, resname
);
2259 if ((env
= kgetenv(buf
)) != NULL
) {
2260 *result
= strtol(env
, NULL
, 0);
2265 * Also support FreeBSD style loader.conf hinting
2267 ksnprintf(buf
, sizeof(buf
), "hint.%s.%d.%s", name
, unit
, resname
);
2268 if ((env
= kgetenv(buf
)) != NULL
) {
2269 *result
= strtol(env
, NULL
, 0);
2277 resource_int_value(const char *name
, int unit
, const char *resname
, int *result
)
2279 struct config_resource
*res
;
2283 if (resource_kenv(name
, unit
, resname
, &kvalue
) == 0) {
2284 *result
= (int)kvalue
;
2287 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
2289 if (res
->type
!= RES_INT
)
2291 *result
= res
->u
.intval
;
2296 resource_long_value(const char *name
, int unit
, const char *resname
,
2299 struct config_resource
*res
;
2303 if (resource_kenv(name
, unit
, resname
, &kvalue
) == 0) {
2307 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
2309 if (res
->type
!= RES_LONG
)
2311 *result
= res
->u
.longval
;
2316 resource_string_value(const char *name
, int unit
, const char *resname
,
2317 const char **result
)
2320 struct config_resource
*res
;
2325 * DragonFly style loader.conf hinting
2327 ksnprintf(buf
, sizeof(buf
), "%s%d.%s", name
, unit
, resname
);
2328 if ((env
= kgetenv(buf
)) != NULL
) {
2334 * Also support FreeBSD style loader.conf hinting
2336 ksnprintf(buf
, sizeof(buf
), "hint.%s.%d.%s", name
, unit
, resname
);
2337 if ((env
= kgetenv(buf
)) != NULL
) {
2342 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
2344 if (res
->type
!= RES_STRING
)
2346 *result
= res
->u
.stringval
;
2351 resource_query_string(int i
, const char *resname
, const char *value
)
2357 for (; i
< devtab_count
; i
++)
2358 if (resource_match_string(i
, resname
, value
) >= 0)
2364 resource_locate(int i
, const char *resname
)
2370 for (; i
< devtab_count
; i
++)
2371 if (!strcmp(devtab
[i
].name
, resname
))
2377 resource_count(void)
2379 return(devtab_count
);
2383 resource_query_name(int i
)
2385 return(devtab
[i
].name
);
2389 resource_query_unit(int i
)
2391 return(devtab
[i
].unit
);
2395 resource_create(const char *name
, int unit
, const char *resname
,
2396 resource_type type
, struct config_resource
**result
)
2399 struct config_resource
*res
= NULL
;
2401 for (i
= 0; i
< devtab_count
; i
++)
2402 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
2403 res
= devtab
[i
].resources
;
2407 i
= resource_new_name(name
, unit
);
2410 res
= devtab
[i
].resources
;
2412 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
2413 if (!strcmp(res
->name
, resname
)) {
2417 j
= resource_new_resname(i
, resname
, type
);
2420 res
= &devtab
[i
].resources
[j
];
2426 resource_set_int(const char *name
, int unit
, const char *resname
, int value
)
2429 struct config_resource
*res
;
2431 error
= resource_create(name
, unit
, resname
, RES_INT
, &res
);
2434 if (res
->type
!= RES_INT
)
2436 res
->u
.intval
= value
;
2441 resource_set_long(const char *name
, int unit
, const char *resname
, long value
)
2444 struct config_resource
*res
;
2446 error
= resource_create(name
, unit
, resname
, RES_LONG
, &res
);
2449 if (res
->type
!= RES_LONG
)
2451 res
->u
.longval
= value
;
2456 resource_set_string(const char *name
, int unit
, const char *resname
,
2460 struct config_resource
*res
;
2462 error
= resource_create(name
, unit
, resname
, RES_STRING
, &res
);
2465 if (res
->type
!= RES_STRING
)
2467 if (res
->u
.stringval
)
2468 kfree(res
->u
.stringval
, M_TEMP
);
2469 res
->u
.stringval
= kmalloc(strlen(value
) + 1, M_TEMP
, M_INTWAIT
);
2470 if (res
->u
.stringval
== NULL
)
2472 strcpy(res
->u
.stringval
, value
);
2477 resource_cfgload(void *dummy __unused
)
2479 struct config_resource
*res
, *cfgres
;
2482 char *name
, *resname
;
2486 int config_devtab_count
;
2488 config_devtab_count
= devtab_count
;
2492 for (i
= 0; i
< config_devtab_count
; i
++) {
2493 name
= config_devtab
[i
].name
;
2494 unit
= config_devtab
[i
].unit
;
2496 for (j
= 0; j
< config_devtab
[i
].resource_count
; j
++) {
2497 cfgres
= config_devtab
[i
].resources
;
2498 resname
= cfgres
[j
].name
;
2499 type
= cfgres
[j
].type
;
2500 error
= resource_create(name
, unit
, resname
, type
,
2503 kprintf("create resource %s%d: error %d\n",
2507 if (res
->type
!= type
) {
2508 kprintf("type mismatch %s%d: %d != %d\n",
2509 name
, unit
, res
->type
, type
);
2514 res
->u
.intval
= cfgres
[j
].u
.intval
;
2517 res
->u
.longval
= cfgres
[j
].u
.longval
;
2520 if (res
->u
.stringval
)
2521 kfree(res
->u
.stringval
, M_TEMP
);
2522 stringval
= cfgres
[j
].u
.stringval
;
2523 res
->u
.stringval
= kmalloc(strlen(stringval
) + 1,
2525 if (res
->u
.stringval
== NULL
)
2527 strcpy(res
->u
.stringval
, stringval
);
2530 panic("unknown resource type %d", type
);
2535 SYSINIT(cfgload
, SI_BOOT1_POST
, SI_ORDER_ANY
+ 50, resource_cfgload
, 0);
2538 /*======================================*/
2540 * Some useful method implementations to make life easier for bus drivers.
2544 resource_list_init(struct resource_list
*rl
)
2550 resource_list_free(struct resource_list
*rl
)
2552 struct resource_list_entry
*rle
;
2554 while ((rle
= SLIST_FIRST(rl
)) != NULL
) {
2556 panic("resource_list_free: resource entry is busy");
2557 SLIST_REMOVE_HEAD(rl
, link
);
2563 resource_list_add(struct resource_list
*rl
, int type
, int rid
,
2564 u_long start
, u_long end
, u_long count
, int cpuid
)
2566 struct resource_list_entry
*rle
;
2568 rle
= resource_list_find(rl
, type
, rid
);
2570 rle
= kmalloc(sizeof(struct resource_list_entry
), M_BUS
,
2572 SLIST_INSERT_HEAD(rl
, rle
, link
);
2580 panic("resource_list_add: resource entry is busy");
2587 if (rle
->cpuid
!= -1 && rle
->cpuid
!= cpuid
) {
2588 panic("resource_list_add: moving from cpu%d -> cpu%d",
2595 struct resource_list_entry
*
2596 resource_list_find(struct resource_list
*rl
,
2599 struct resource_list_entry
*rle
;
2601 SLIST_FOREACH(rle
, rl
, link
)
2602 if (rle
->type
== type
&& rle
->rid
== rid
)
2608 resource_list_delete(struct resource_list
*rl
,
2611 struct resource_list_entry
*rle
= resource_list_find(rl
, type
, rid
);
2614 if (rle
->res
!= NULL
)
2615 panic("resource_list_delete: resource has not been released");
2616 SLIST_REMOVE(rl
, rle
, resource_list_entry
, link
);
2622 resource_list_alloc(struct resource_list
*rl
,
2623 device_t bus
, device_t child
,
2625 u_long start
, u_long end
,
2626 u_long count
, u_int flags
, int cpuid
)
2628 struct resource_list_entry
*rle
= NULL
;
2629 int passthrough
= (device_get_parent(child
) != bus
);
2630 int isdefault
= (start
== 0UL && end
== ~0UL);
2633 return(BUS_ALLOC_RESOURCE(device_get_parent(bus
), child
,
2635 start
, end
, count
, flags
, cpuid
));
2638 rle
= resource_list_find(rl
, type
, *rid
);
2641 return(0); /* no resource of that type/rid */
2644 panic("resource_list_alloc: resource entry is busy");
2648 count
= ulmax(count
, rle
->count
);
2649 end
= ulmax(rle
->end
, start
+ count
- 1);
2653 rle
->res
= BUS_ALLOC_RESOURCE(device_get_parent(bus
), child
,
2654 type
, rid
, start
, end
, count
,
2658 * Record the new range.
2661 rle
->start
= rman_get_start(rle
->res
);
2662 rle
->end
= rman_get_end(rle
->res
);
2670 resource_list_release(struct resource_list
*rl
,
2671 device_t bus
, device_t child
,
2672 int type
, int rid
, struct resource
*res
)
2674 struct resource_list_entry
*rle
= NULL
;
2675 int passthrough
= (device_get_parent(child
) != bus
);
2679 return(BUS_RELEASE_RESOURCE(device_get_parent(bus
), child
,
2683 rle
= resource_list_find(rl
, type
, rid
);
2686 panic("resource_list_release: can't find resource");
2688 panic("resource_list_release: resource entry is not busy");
2690 error
= BUS_RELEASE_RESOURCE(device_get_parent(bus
), child
,
2700 resource_list_print_type(struct resource_list
*rl
, const char *name
, int type
,
2703 struct resource_list_entry
*rle
;
2704 int printed
, retval
;
2708 /* Yes, this is kinda cheating */
2709 SLIST_FOREACH(rle
, rl
, link
) {
2710 if (rle
->type
== type
) {
2712 retval
+= kprintf(" %s ", name
);
2714 retval
+= kprintf(",");
2716 retval
+= kprintf(format
, rle
->start
);
2717 if (rle
->count
> 1) {
2718 retval
+= kprintf("-");
2719 retval
+= kprintf(format
, rle
->start
+
2728 * Generic driver/device identify functions. These will install a device
2729 * rendezvous point under the parent using the same name as the driver
2730 * name, which will at a later time be probed and attached.
2732 * These functions are used when the parent does not 'scan' its bus for
2733 * matching devices, or for the particular devices using these functions,
2734 * or when the device is a pseudo or synthesized device (such as can be
2735 * found under firewire and ppbus).
2738 bus_generic_identify(driver_t
*driver
, device_t parent
)
2740 if (parent
->state
== DS_ATTACHED
)
2742 BUS_ADD_CHILD(parent
, parent
, 0, driver
->name
, -1);
2747 bus_generic_identify_sameunit(driver_t
*driver
, device_t parent
)
2749 if (parent
->state
== DS_ATTACHED
)
2751 BUS_ADD_CHILD(parent
, parent
, 0, driver
->name
, device_get_unit(parent
));
2756 * Call DEVICE_IDENTIFY for each driver.
2759 bus_generic_probe(device_t dev
)
2761 devclass_t dc
= dev
->devclass
;
2764 TAILQ_FOREACH(dl
, &dc
->drivers
, link
) {
2765 DEVICE_IDENTIFY(dl
->driver
, dev
);
2772 * This is an aweful hack due to the isa bus and autoconf code not
2773 * probing the ISA devices until after everything else has configured.
2774 * The ISA bus did a dummy attach long ago so we have to set it back
2775 * to an earlier state so the probe thinks its the initial probe and
2778 * XXX remove by properly defering the ISA bus scan.
2781 bus_generic_probe_hack(device_t dev
)
2783 if (dev
->state
== DS_ATTACHED
) {
2784 dev
->state
= DS_ALIVE
;
2785 bus_generic_probe(dev
);
2786 dev
->state
= DS_ATTACHED
;
2792 bus_generic_attach(device_t dev
)
2796 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2797 device_probe_and_attach(child
);
2804 bus_generic_attach_gpri(device_t dev
, u_int gpri
)
2808 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2809 device_probe_and_attach_gpri(child
, gpri
);
2816 bus_generic_detach(device_t dev
)
2821 if (dev
->state
!= DS_ATTACHED
)
2824 TAILQ_FOREACH(child
, &dev
->children
, link
)
2825 if ((error
= device_detach(child
)) != 0)
2832 bus_generic_shutdown(device_t dev
)
2836 TAILQ_FOREACH(child
, &dev
->children
, link
)
2837 device_shutdown(child
);
2843 bus_generic_suspend(device_t dev
)
2846 device_t child
, child2
;
2848 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2849 error
= DEVICE_SUSPEND(child
);
2851 for (child2
= TAILQ_FIRST(&dev
->children
);
2852 child2
&& child2
!= child
;
2853 child2
= TAILQ_NEXT(child2
, link
))
2854 DEVICE_RESUME(child2
);
2862 bus_generic_resume(device_t dev
)
2866 TAILQ_FOREACH(child
, &dev
->children
, link
)
2867 DEVICE_RESUME(child
);
2868 /* if resume fails, there's nothing we can usefully do... */
2874 bus_print_child_header(device_t dev
, device_t child
)
2878 if (device_get_desc(child
))
2879 retval
+= device_printf(child
, "<%s>", device_get_desc(child
));
2881 retval
+= kprintf("%s", device_get_nameunit(child
));
2883 if (child
->state
!= DS_ATTACHED
)
2884 kprintf(" [tentative]");
2886 kprintf(" [attached!]");
2892 bus_print_child_footer(device_t dev
, device_t child
)
2894 return(kprintf(" on %s\n", device_get_nameunit(dev
)));
2898 bus_generic_add_child(device_t dev
, device_t child
, int order
,
2899 const char *name
, int unit
)
2902 dev
= BUS_ADD_CHILD(dev
->parent
, child
, order
, name
, unit
);
2904 dev
= device_add_child_ordered(child
, order
, name
, unit
);
2910 bus_generic_print_child(device_t dev
, device_t child
)
2914 retval
+= bus_print_child_header(dev
, child
);
2915 retval
+= bus_print_child_footer(dev
, child
);
2921 bus_generic_read_ivar(device_t dev
, device_t child
, int index
,
2927 error
= BUS_READ_IVAR(dev
->parent
, child
, index
, result
);
2934 bus_generic_write_ivar(device_t dev
, device_t child
, int index
,
2940 error
= BUS_WRITE_IVAR(dev
->parent
, child
, index
, value
);
2947 * Resource list are used for iterations, do not recurse.
2949 struct resource_list
*
2950 bus_generic_get_resource_list(device_t dev
, device_t child
)
2956 bus_generic_driver_added(device_t dev
, driver_t
*driver
)
2960 DEVICE_IDENTIFY(driver
, dev
);
2961 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2962 if (child
->state
== DS_NOTPRESENT
)
2963 device_probe_and_attach(child
);
2968 bus_generic_setup_intr(device_t dev
, device_t child
, struct resource
*irq
,
2969 int flags
, driver_intr_t
*intr
, void *arg
, void **cookiep
,
2970 lwkt_serialize_t serializer
, const char *desc
)
2972 /* Propagate up the bus hierarchy until someone handles it. */
2974 return BUS_SETUP_INTR(dev
->parent
, child
, irq
, flags
,
2975 intr
, arg
, cookiep
, serializer
, desc
);
2982 bus_generic_teardown_intr(device_t dev
, device_t child
, struct resource
*irq
,
2985 /* Propagate up the bus hierarchy until someone handles it. */
2987 return(BUS_TEARDOWN_INTR(dev
->parent
, child
, irq
, cookie
));
2993 bus_generic_disable_intr(device_t dev
, device_t child
, void *cookie
)
2996 return(BUS_DISABLE_INTR(dev
->parent
, child
, cookie
));
3002 bus_generic_enable_intr(device_t dev
, device_t child
, void *cookie
)
3005 BUS_ENABLE_INTR(dev
->parent
, child
, cookie
);
3009 bus_generic_config_intr(device_t dev
, device_t child
, int irq
, enum intr_trigger trig
,
3010 enum intr_polarity pol
)
3012 /* Propagate up the bus hierarchy until someone handles it. */
3014 return(BUS_CONFIG_INTR(dev
->parent
, child
, irq
, trig
, pol
));
3020 bus_generic_alloc_resource(device_t dev
, device_t child
, int type
, int *rid
,
3021 u_long start
, u_long end
, u_long count
, u_int flags
, int cpuid
)
3023 /* Propagate up the bus hierarchy until someone handles it. */
3025 return(BUS_ALLOC_RESOURCE(dev
->parent
, child
, type
, rid
,
3026 start
, end
, count
, flags
, cpuid
));
3032 bus_generic_release_resource(device_t dev
, device_t child
, int type
, int rid
,
3035 /* Propagate up the bus hierarchy until someone handles it. */
3037 return(BUS_RELEASE_RESOURCE(dev
->parent
, child
, type
, rid
, r
));
3043 bus_generic_activate_resource(device_t dev
, device_t child
, int type
, int rid
,
3046 /* Propagate up the bus hierarchy until someone handles it. */
3048 return(BUS_ACTIVATE_RESOURCE(dev
->parent
, child
, type
, rid
, r
));
3054 bus_generic_deactivate_resource(device_t dev
, device_t child
, int type
,
3055 int rid
, struct resource
*r
)
3057 /* Propagate up the bus hierarchy until someone handles it. */
3059 return(BUS_DEACTIVATE_RESOURCE(dev
->parent
, child
, type
, rid
,
3066 bus_generic_get_resource(device_t dev
, device_t child
, int type
, int rid
,
3067 u_long
*startp
, u_long
*countp
)
3073 error
= BUS_GET_RESOURCE(dev
->parent
, child
, type
, rid
,
3080 bus_generic_set_resource(device_t dev
, device_t child
, int type
, int rid
,
3081 u_long start
, u_long count
, int cpuid
)
3087 error
= BUS_SET_RESOURCE(dev
->parent
, child
, type
, rid
,
3088 start
, count
, cpuid
);
3094 bus_generic_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
3097 BUS_DELETE_RESOURCE(dev
, child
, type
, rid
);
3101 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3103 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3104 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3107 bus_generic_get_dma_tag(device_t dev
, device_t child
)
3110 /* Propagate up the bus hierarchy until someone handles it. */
3111 if (dev
->parent
!= NULL
)
3112 return (BUS_GET_DMA_TAG(dev
->parent
, child
));
3117 bus_generic_rl_get_resource(device_t dev
, device_t child
, int type
, int rid
,
3118 u_long
*startp
, u_long
*countp
)
3120 struct resource_list
*rl
= NULL
;
3121 struct resource_list_entry
*rle
= NULL
;
3123 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
3127 rle
= resource_list_find(rl
, type
, rid
);
3132 *startp
= rle
->start
;
3134 *countp
= rle
->count
;
3140 bus_generic_rl_set_resource(device_t dev
, device_t child
, int type
, int rid
,
3141 u_long start
, u_long count
, int cpuid
)
3143 struct resource_list
*rl
= NULL
;
3145 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
3149 resource_list_add(rl
, type
, rid
, start
, (start
+ count
- 1), count
,
3156 bus_generic_rl_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
3158 struct resource_list
*rl
= NULL
;
3160 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
3164 resource_list_delete(rl
, type
, rid
);
3168 bus_generic_rl_release_resource(device_t dev
, device_t child
, int type
,
3169 int rid
, struct resource
*r
)
3171 struct resource_list
*rl
= NULL
;
3173 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
3177 return(resource_list_release(rl
, dev
, child
, type
, rid
, r
));
3181 bus_generic_rl_alloc_resource(device_t dev
, device_t child
, int type
,
3182 int *rid
, u_long start
, u_long end
, u_long count
, u_int flags
, int cpuid
)
3184 struct resource_list
*rl
= NULL
;
3186 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
3190 return(resource_list_alloc(rl
, dev
, child
, type
, rid
,
3191 start
, end
, count
, flags
, cpuid
));
3195 bus_generic_child_present(device_t bus
, device_t child
)
3197 return(BUS_CHILD_PRESENT(device_get_parent(bus
), bus
));
3202 * Some convenience functions to make it easier for drivers to use the
3203 * resource-management functions. All these really do is hide the
3204 * indirection through the parent's method table, making for slightly
3205 * less-wordy code. In the future, it might make sense for this code
3206 * to maintain some sort of a list of resources allocated by each device.
3209 bus_alloc_resources(device_t dev
, struct resource_spec
*rs
,
3210 struct resource
**res
)
3214 for (i
= 0; rs
[i
].type
!= -1; i
++)
3216 for (i
= 0; rs
[i
].type
!= -1; i
++) {
3217 res
[i
] = bus_alloc_resource_any(dev
,
3218 rs
[i
].type
, &rs
[i
].rid
, rs
[i
].flags
);
3219 if (res
[i
] == NULL
) {
3220 bus_release_resources(dev
, rs
, res
);
3228 bus_release_resources(device_t dev
, const struct resource_spec
*rs
,
3229 struct resource
**res
)
3233 for (i
= 0; rs
[i
].type
!= -1; i
++)
3234 if (res
[i
] != NULL
) {
3235 bus_release_resource(
3236 dev
, rs
[i
].type
, rs
[i
].rid
, res
[i
]);
3242 bus_alloc_resource(device_t dev
, int type
, int *rid
, u_long start
, u_long end
,
3243 u_long count
, u_int flags
)
3245 if (dev
->parent
== NULL
)
3247 return(BUS_ALLOC_RESOURCE(dev
->parent
, dev
, type
, rid
, start
, end
,
3252 bus_alloc_legacy_irq_resource(device_t dev
, int *rid
, u_long irq
, u_int flags
)
3254 if (dev
->parent
== NULL
)
3256 return BUS_ALLOC_RESOURCE(dev
->parent
, dev
, SYS_RES_IRQ
, rid
,
3257 irq
, irq
, 1, flags
, machintr_legacy_intr_cpuid(irq
));
3261 bus_activate_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
3263 if (dev
->parent
== NULL
)
3265 return(BUS_ACTIVATE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
3269 bus_deactivate_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
3271 if (dev
->parent
== NULL
)
3273 return(BUS_DEACTIVATE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
3277 bus_release_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
3279 if (dev
->parent
== NULL
)
3281 return(BUS_RELEASE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
3285 bus_setup_intr_descr(device_t dev
, struct resource
*r
, int flags
,
3286 driver_intr_t handler
, void *arg
, void **cookiep
,
3287 lwkt_serialize_t serializer
, const char *desc
)
3289 if (dev
->parent
== NULL
)
3291 return BUS_SETUP_INTR(dev
->parent
, dev
, r
, flags
, handler
, arg
,
3292 cookiep
, serializer
, desc
);
3296 bus_setup_intr(device_t dev
, struct resource
*r
, int flags
,
3297 driver_intr_t handler
, void *arg
, void **cookiep
,
3298 lwkt_serialize_t serializer
)
3300 return bus_setup_intr_descr(dev
, r
, flags
, handler
, arg
, cookiep
,
3305 bus_teardown_intr(device_t dev
, struct resource
*r
, void *cookie
)
3307 if (dev
->parent
== NULL
)
3309 return(BUS_TEARDOWN_INTR(dev
->parent
, dev
, r
, cookie
));
3313 bus_enable_intr(device_t dev
, void *cookie
)
3316 BUS_ENABLE_INTR(dev
->parent
, dev
, cookie
);
3320 bus_disable_intr(device_t dev
, void *cookie
)
3323 return(BUS_DISABLE_INTR(dev
->parent
, dev
, cookie
));
3329 bus_set_resource(device_t dev
, int type
, int rid
,
3330 u_long start
, u_long count
, int cpuid
)
3332 return(BUS_SET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3333 start
, count
, cpuid
));
3337 bus_get_resource(device_t dev
, int type
, int rid
,
3338 u_long
*startp
, u_long
*countp
)
3340 return(BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3345 bus_get_resource_start(device_t dev
, int type
, int rid
)
3347 u_long start
, count
;
3350 error
= BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3358 bus_get_resource_count(device_t dev
, int type
, int rid
)
3360 u_long start
, count
;
3363 error
= BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3371 bus_delete_resource(device_t dev
, int type
, int rid
)
3373 BUS_DELETE_RESOURCE(device_get_parent(dev
), dev
, type
, rid
);
3377 bus_child_present(device_t child
)
3379 return (BUS_CHILD_PRESENT(device_get_parent(child
), child
));
3383 bus_child_pnpinfo_str(device_t child
, char *buf
, size_t buflen
)
3387 parent
= device_get_parent(child
);
3388 if (parent
== NULL
) {
3392 return (BUS_CHILD_PNPINFO_STR(parent
, child
, buf
, buflen
));
3396 bus_child_location_str(device_t child
, char *buf
, size_t buflen
)
3400 parent
= device_get_parent(child
);
3401 if (parent
== NULL
) {
3405 return (BUS_CHILD_LOCATION_STR(parent
, child
, buf
, buflen
));
3409 * @brief Wrapper function for BUS_GET_DMA_TAG().
3411 * This function simply calls the BUS_GET_DMA_TAG() method of the
3415 bus_get_dma_tag(device_t dev
)
3419 parent
= device_get_parent(dev
);
3422 return (BUS_GET_DMA_TAG(parent
, dev
));
3426 root_print_child(device_t dev
, device_t child
)
3432 root_setup_intr(device_t dev
, device_t child
, driver_intr_t
*intr
, void *arg
,
3433 void **cookiep
, lwkt_serialize_t serializer
, const char *desc
)
3436 * If an interrupt mapping gets to here something bad has happened.
3438 panic("root_setup_intr");
3442 * If we get here, assume that the device is permanant and really is
3443 * present in the system. Removable bus drivers are expected to intercept
3444 * this call long before it gets here. We return -1 so that drivers that
3445 * really care can check vs -1 or some ERRNO returned higher in the food
3449 root_child_present(device_t dev
, device_t child
)
3455 * XXX NOTE! other defaults may be set in bus_if.m
3457 static kobj_method_t root_methods
[] = {
3458 /* Device interface */
3459 KOBJMETHOD(device_shutdown
, bus_generic_shutdown
),
3460 KOBJMETHOD(device_suspend
, bus_generic_suspend
),
3461 KOBJMETHOD(device_resume
, bus_generic_resume
),
3464 KOBJMETHOD(bus_add_child
, bus_generic_add_child
),
3465 KOBJMETHOD(bus_print_child
, root_print_child
),
3466 KOBJMETHOD(bus_read_ivar
, bus_generic_read_ivar
),
3467 KOBJMETHOD(bus_write_ivar
, bus_generic_write_ivar
),
3468 KOBJMETHOD(bus_setup_intr
, root_setup_intr
),
3469 KOBJMETHOD(bus_child_present
, root_child_present
),
3474 static driver_t root_driver
= {
3481 devclass_t root_devclass
;
3484 root_bus_module_handler(module_t mod
, int what
, void* arg
)
3488 TAILQ_INIT(&bus_data_devices
);
3489 root_bus
= make_device(NULL
, "root", 0);
3490 root_bus
->desc
= "System root bus";
3491 kobj_init((kobj_t
) root_bus
, (kobj_class_t
) &root_driver
);
3492 root_bus
->driver
= &root_driver
;
3493 root_bus
->state
= DS_ALIVE
;
3494 root_devclass
= devclass_find_internal("root", NULL
, FALSE
);
3499 device_shutdown(root_bus
);
3506 static moduledata_t root_bus_mod
= {
3508 root_bus_module_handler
,
3511 DECLARE_MODULE(rootbus
, root_bus_mod
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
);
3514 root_bus_configure(void)
3522 * handle device_identify based device attachments to the root_bus
3523 * (typically nexus).
3525 bus_generic_probe(root_bus
);
3528 * Probe and attach the devices under root_bus.
3530 TAILQ_FOREACH(dev
, &root_bus
->children
, link
) {
3531 device_probe_and_attach(dev
);
3535 * Wait for all asynchronous attaches to complete. If we don't
3536 * our legacy ISA bus scan could steal device unit numbers or
3540 if (numasyncthreads
)
3541 kprintf("Waiting for async drivers to attach\n");
3542 while (numasyncthreads
> 0) {
3543 if (tsleep(&numasyncthreads
, 0, "rootbus", hz
) == EWOULDBLOCK
)
3545 if (warncount
== 0) {
3546 kprintf("Warning: Still waiting for %d "
3547 "drivers to attach\n", numasyncthreads
);
3548 } else if (warncount
== -30) {
3549 kprintf("Giving up on %d drivers\n", numasyncthreads
);
3553 root_bus
->state
= DS_ATTACHED
;
3557 driver_module_handler(module_t mod
, int what
, void *arg
)
3560 struct driver_module_data
*dmd
;
3561 devclass_t bus_devclass
;
3562 kobj_class_t driver
;
3563 const char *parentname
;
3565 dmd
= (struct driver_module_data
*)arg
;
3566 bus_devclass
= devclass_find_internal(dmd
->dmd_busname
, NULL
, TRUE
);
3571 if (dmd
->dmd_chainevh
)
3572 error
= dmd
->dmd_chainevh(mod
,what
,dmd
->dmd_chainarg
);
3574 driver
= dmd
->dmd_driver
;
3575 PDEBUG(("Loading module: driver %s on bus %s",
3576 DRIVERNAME(driver
), dmd
->dmd_busname
));
3579 * If the driver has any base classes, make the
3580 * devclass inherit from the devclass of the driver's
3581 * first base class. This will allow the system to
3582 * search for drivers in both devclasses for children
3583 * of a device using this driver.
3585 if (driver
->baseclasses
)
3586 parentname
= driver
->baseclasses
[0]->name
;
3589 *dmd
->dmd_devclass
= devclass_find_internal(driver
->name
,
3592 error
= devclass_add_driver(bus_devclass
, driver
);
3598 PDEBUG(("Unloading module: driver %s from bus %s",
3599 DRIVERNAME(dmd
->dmd_driver
), dmd
->dmd_busname
));
3600 error
= devclass_delete_driver(bus_devclass
, dmd
->dmd_driver
);
3602 if (!error
&& dmd
->dmd_chainevh
)
3603 error
= dmd
->dmd_chainevh(mod
,what
,dmd
->dmd_chainarg
);
3613 * The _short versions avoid iteration by not calling anything that prints
3614 * more than oneliners. I love oneliners.
3618 print_device_short(device_t dev
, int indent
)
3623 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3624 dev
->unit
, dev
->desc
,
3625 (dev
->parent
? "":"no "),
3626 (TAILQ_EMPTY(&dev
->children
)? "no ":""),
3627 (dev
->flags
&DF_ENABLED
? "enabled,":"disabled,"),
3628 (dev
->flags
&DF_FIXEDCLASS
? "fixed,":""),
3629 (dev
->flags
&DF_WILDCARD
? "wildcard,":""),
3630 (dev
->flags
&DF_DESCMALLOCED
? "descmalloced,":""),
3631 (dev
->ivars
? "":"no "),
3632 (dev
->softc
? "":"no "),
3637 print_device(device_t dev
, int indent
)
3642 print_device_short(dev
, indent
);
3644 indentprintf(("Parent:\n"));
3645 print_device_short(dev
->parent
, indent
+1);
3646 indentprintf(("Driver:\n"));
3647 print_driver_short(dev
->driver
, indent
+1);
3648 indentprintf(("Devclass:\n"));
3649 print_devclass_short(dev
->devclass
, indent
+1);
3653 * Print the device and all its children (indented).
3656 print_device_tree_short(device_t dev
, int indent
)
3663 print_device_short(dev
, indent
);
3665 TAILQ_FOREACH(child
, &dev
->children
, link
)
3666 print_device_tree_short(child
, indent
+1);
3670 * Print the device and all its children (indented).
3673 print_device_tree(device_t dev
, int indent
)
3680 print_device(dev
, indent
);
3682 TAILQ_FOREACH(child
, &dev
->children
, link
)
3683 print_device_tree(child
, indent
+1);
3687 print_driver_short(driver_t
*driver
, int indent
)
3692 indentprintf(("driver %s: softc size = %zu\n",
3693 driver
->name
, driver
->size
));
3697 print_driver(driver_t
*driver
, int indent
)
3702 print_driver_short(driver
, indent
);
3707 print_driver_list(driver_list_t drivers
, int indent
)
3709 driverlink_t driver
;
3711 TAILQ_FOREACH(driver
, &drivers
, link
)
3712 print_driver(driver
->driver
, indent
);
3716 print_devclass_short(devclass_t dc
, int indent
)
3721 indentprintf(("devclass %s: max units = %d\n", dc
->name
, dc
->maxunit
));
3725 print_devclass(devclass_t dc
, int indent
)
3732 print_devclass_short(dc
, indent
);
3733 indentprintf(("Drivers:\n"));
3734 print_driver_list(dc
->drivers
, indent
+1);
3736 indentprintf(("Devices:\n"));
3737 for (i
= 0; i
< dc
->maxunit
; i
++)
3739 print_device(dc
->devices
[i
], indent
+1);
3743 print_devclass_list_short(void)
3747 kprintf("Short listing of devclasses, drivers & devices:\n");
3748 TAILQ_FOREACH(dc
, &devclasses
, link
) {
3749 print_devclass_short(dc
, 0);
3754 print_devclass_list(void)
3758 kprintf("Full listing of devclasses, drivers & devices:\n");
3759 TAILQ_FOREACH(dc
, &devclasses
, link
) {
3760 print_devclass(dc
, 0);
3767 * Check to see if a device is disabled via a disabled hint.
3770 resource_disabled(const char *name
, int unit
)
3774 error
= resource_int_value(name
, unit
, "disabled", &value
);
3781 * User-space access to the device tree.
3783 * We implement a small set of nodes:
3785 * hw.bus Single integer read method to obtain the
3786 * current generation count.
3787 * hw.bus.devices Reads the entire device tree in flat space.
3788 * hw.bus.rman Resource manager interface
3790 * We might like to add the ability to scan devclasses and/or drivers to
3791 * determine what else is currently loaded/available.
3795 sysctl_bus(SYSCTL_HANDLER_ARGS
)
3797 struct u_businfo ubus
;
3799 ubus
.ub_version
= BUS_USER_VERSION
;
3800 ubus
.ub_generation
= bus_data_generation
;
3802 return (SYSCTL_OUT(req
, &ubus
, sizeof(ubus
)));
3804 SYSCTL_NODE(_hw_bus
, OID_AUTO
, info
, CTLFLAG_RW
, sysctl_bus
,
3805 "bus-related data");
3808 sysctl_devices(SYSCTL_HANDLER_ARGS
)
3810 int *name
= (int *)arg1
;
3811 u_int namelen
= arg2
;
3814 struct u_device udev
; /* XXX this is a bit big */
3820 if (bus_data_generation_check(name
[0]))
3826 * Scan the list of devices, looking for the requested index.
3828 TAILQ_FOREACH(dev
, &bus_data_devices
, devlink
) {
3836 * Populate the return array.
3838 bzero(&udev
, sizeof(udev
));
3839 udev
.dv_handle
= (uintptr_t)dev
;
3840 udev
.dv_parent
= (uintptr_t)dev
->parent
;
3841 if (dev
->nameunit
!= NULL
)
3842 strlcpy(udev
.dv_name
, dev
->nameunit
, sizeof(udev
.dv_name
));
3843 if (dev
->desc
!= NULL
)
3844 strlcpy(udev
.dv_desc
, dev
->desc
, sizeof(udev
.dv_desc
));
3845 if (dev
->driver
!= NULL
&& dev
->driver
->name
!= NULL
)
3846 strlcpy(udev
.dv_drivername
, dev
->driver
->name
,
3847 sizeof(udev
.dv_drivername
));
3848 bus_child_pnpinfo_str(dev
, udev
.dv_pnpinfo
, sizeof(udev
.dv_pnpinfo
));
3849 bus_child_location_str(dev
, udev
.dv_location
, sizeof(udev
.dv_location
));
3850 udev
.dv_devflags
= dev
->devflags
;
3851 udev
.dv_flags
= dev
->flags
;
3852 udev
.dv_state
= dev
->state
;
3853 error
= SYSCTL_OUT(req
, &udev
, sizeof(udev
));
3857 SYSCTL_NODE(_hw_bus
, OID_AUTO
, devices
, CTLFLAG_RD
, sysctl_devices
,
3858 "system device tree");
3861 bus_data_generation_check(int generation
)
3863 if (generation
!= bus_data_generation
)
3866 /* XXX generate optimised lists here? */
3871 bus_data_generation_update(void)
3873 bus_data_generation
++;
3877 intr_str_polarity(enum intr_polarity pola
)
3880 case INTR_POLARITY_LOW
:
3883 case INTR_POLARITY_HIGH
:
3886 case INTR_POLARITY_CONFORM
:
3893 intr_str_trigger(enum intr_trigger trig
)
3896 case INTR_TRIGGER_EDGE
:
3899 case INTR_TRIGGER_LEVEL
:
3902 case INTR_TRIGGER_CONFORM
:
3909 device_getenv_int(device_t dev
, const char *knob
, int def
)
3913 /* Deprecated; for compat */
3914 ksnprintf(env
, sizeof(env
), "hw.%s.%s", device_get_nameunit(dev
), knob
);
3915 kgetenv_int(env
, &def
);
3917 /* Prefer dev.driver.unit.knob */
3918 ksnprintf(env
, sizeof(env
), "dev.%s.%d.%s",
3919 device_get_name(dev
), device_get_unit(dev
), knob
);
3920 kgetenv_int(env
, &def
);
3926 device_getenv_string(device_t dev
, const char *knob
, char * __restrict data
,
3927 int dlen
, const char * __restrict def
)
3931 strlcpy(data
, def
, dlen
);
3933 /* Deprecated; for compat */
3934 ksnprintf(env
, sizeof(env
), "hw.%s.%s", device_get_nameunit(dev
), knob
);
3935 kgetenv_string(env
, data
, dlen
);
3937 /* Prefer dev.driver.unit.knob */
3938 ksnprintf(env
, sizeof(env
), "dev.%s.%d.%s",
3939 device_get_name(dev
), device_get_unit(dev
), knob
);
3940 kgetenv_string(env
, data
, dlen
);