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 #include <sys/thread2.h>
55 #include <sys/mplock2.h>
57 SYSCTL_NODE(_hw
, OID_AUTO
, bus
, CTLFLAG_RW
, NULL
, NULL
);
58 SYSCTL_NODE(, OID_AUTO
, dev
, CTLFLAG_RW
, NULL
, NULL
);
60 MALLOC_DEFINE(M_BUS
, "bus", "Bus data structures");
63 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
64 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
65 #define DRIVERNAME(d) ((d)? d->name : "no driver")
66 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
68 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
69 * prevent syslog from deleting initial spaces
71 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
73 static void print_device_short(device_t dev
, int indent
);
74 static void print_device(device_t dev
, int indent
);
75 void print_device_tree_short(device_t dev
, int indent
);
76 void print_device_tree(device_t dev
, int indent
);
77 static void print_driver_short(driver_t
*driver
, int indent
);
78 static void print_driver(driver_t
*driver
, int indent
);
79 static void print_driver_list(driver_list_t drivers
, int indent
);
80 static void print_devclass_short(devclass_t dc
, int indent
);
81 static void print_devclass(devclass_t dc
, int indent
);
82 void print_devclass_list_short(void);
83 void print_devclass_list(void);
86 /* Make the compiler ignore the function calls */
87 #define PDEBUG(a) /* nop */
88 #define DEVICENAME(d) /* nop */
89 #define DRIVERNAME(d) /* nop */
90 #define DEVCLANAME(d) /* nop */
92 #define print_device_short(d,i) /* nop */
93 #define print_device(d,i) /* nop */
94 #define print_device_tree_short(d,i) /* nop */
95 #define print_device_tree(d,i) /* nop */
96 #define print_driver_short(d,i) /* nop */
97 #define print_driver(d,i) /* nop */
98 #define print_driver_list(d,i) /* nop */
99 #define print_devclass_short(d,i) /* nop */
100 #define print_devclass(d,i) /* nop */
101 #define print_devclass_list_short() /* nop */
102 #define print_devclass_list() /* nop */
110 DEVCLASS_SYSCTL_PARENT
,
114 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS
)
116 devclass_t dc
= (devclass_t
)arg1
;
120 case DEVCLASS_SYSCTL_PARENT
:
121 value
= dc
->parent
? dc
->parent
->name
: "";
126 return (SYSCTL_OUT(req
, value
, strlen(value
)));
130 devclass_sysctl_init(devclass_t dc
)
133 if (dc
->sysctl_tree
!= NULL
)
135 sysctl_ctx_init(&dc
->sysctl_ctx
);
136 dc
->sysctl_tree
= SYSCTL_ADD_NODE(&dc
->sysctl_ctx
,
137 SYSCTL_STATIC_CHILDREN(_dev
), OID_AUTO
, dc
->name
,
138 CTLFLAG_RD
, NULL
, "");
139 SYSCTL_ADD_PROC(&dc
->sysctl_ctx
, SYSCTL_CHILDREN(dc
->sysctl_tree
),
140 OID_AUTO
, "%parent", CTLTYPE_STRING
| CTLFLAG_RD
,
141 dc
, DEVCLASS_SYSCTL_PARENT
, devclass_sysctl_handler
, "A",
147 DEVICE_SYSCTL_DRIVER
,
148 DEVICE_SYSCTL_LOCATION
,
149 DEVICE_SYSCTL_PNPINFO
,
150 DEVICE_SYSCTL_PARENT
,
154 device_sysctl_handler(SYSCTL_HANDLER_ARGS
)
156 device_t dev
= (device_t
)arg1
;
163 case DEVICE_SYSCTL_DESC
:
164 value
= dev
->desc
? dev
->desc
: "";
166 case DEVICE_SYSCTL_DRIVER
:
167 value
= dev
->driver
? dev
->driver
->name
: "";
169 case DEVICE_SYSCTL_LOCATION
:
170 value
= buf
= kmalloc(1024, M_BUS
, M_WAITOK
| M_ZERO
);
171 bus_child_location_str(dev
, buf
, 1024);
173 case DEVICE_SYSCTL_PNPINFO
:
174 value
= buf
= kmalloc(1024, M_BUS
, M_WAITOK
| M_ZERO
);
175 bus_child_pnpinfo_str(dev
, buf
, 1024);
177 case DEVICE_SYSCTL_PARENT
:
178 value
= dev
->parent
? dev
->parent
->nameunit
: "";
183 error
= SYSCTL_OUT(req
, value
, strlen(value
));
190 device_sysctl_init(device_t dev
)
192 devclass_t dc
= dev
->devclass
;
194 if (dev
->sysctl_tree
!= NULL
)
196 devclass_sysctl_init(dc
);
197 sysctl_ctx_init(&dev
->sysctl_ctx
);
198 dev
->sysctl_tree
= SYSCTL_ADD_NODE(&dev
->sysctl_ctx
,
199 SYSCTL_CHILDREN(dc
->sysctl_tree
), OID_AUTO
,
200 dev
->nameunit
+ strlen(dc
->name
),
201 CTLFLAG_RD
, NULL
, "");
202 SYSCTL_ADD_PROC(&dev
->sysctl_ctx
, SYSCTL_CHILDREN(dev
->sysctl_tree
),
203 OID_AUTO
, "%desc", CTLTYPE_STRING
| CTLFLAG_RD
,
204 dev
, DEVICE_SYSCTL_DESC
, device_sysctl_handler
, "A",
205 "device description");
206 SYSCTL_ADD_PROC(&dev
->sysctl_ctx
, SYSCTL_CHILDREN(dev
->sysctl_tree
),
207 OID_AUTO
, "%driver", CTLTYPE_STRING
| CTLFLAG_RD
,
208 dev
, DEVICE_SYSCTL_DRIVER
, device_sysctl_handler
, "A",
209 "device driver name");
210 SYSCTL_ADD_PROC(&dev
->sysctl_ctx
, SYSCTL_CHILDREN(dev
->sysctl_tree
),
211 OID_AUTO
, "%location", CTLTYPE_STRING
| CTLFLAG_RD
,
212 dev
, DEVICE_SYSCTL_LOCATION
, device_sysctl_handler
, "A",
213 "device location relative to parent");
214 SYSCTL_ADD_PROC(&dev
->sysctl_ctx
, SYSCTL_CHILDREN(dev
->sysctl_tree
),
215 OID_AUTO
, "%pnpinfo", CTLTYPE_STRING
| CTLFLAG_RD
,
216 dev
, DEVICE_SYSCTL_PNPINFO
, device_sysctl_handler
, "A",
217 "device identification");
218 SYSCTL_ADD_PROC(&dev
->sysctl_ctx
, SYSCTL_CHILDREN(dev
->sysctl_tree
),
219 OID_AUTO
, "%parent", CTLTYPE_STRING
| CTLFLAG_RD
,
220 dev
, DEVICE_SYSCTL_PARENT
, device_sysctl_handler
, "A",
225 device_sysctl_update(device_t dev
)
227 devclass_t dc
= dev
->devclass
;
229 if (dev
->sysctl_tree
== NULL
)
231 sysctl_rename_oid(dev
->sysctl_tree
, dev
->nameunit
+ strlen(dc
->name
));
235 device_sysctl_fini(device_t dev
)
237 if (dev
->sysctl_tree
== NULL
)
239 sysctl_ctx_free(&dev
->sysctl_ctx
);
240 dev
->sysctl_tree
= NULL
;
243 static void device_attach_async(device_t dev
);
244 static void device_attach_thread(void *arg
);
245 static int device_doattach(device_t dev
);
247 static int do_async_attach
= 0;
248 static int numasyncthreads
;
249 TUNABLE_INT("kern.do_async_attach", &do_async_attach
);
252 * /dev/devctl implementation
256 * This design allows only one reader for /dev/devctl. This is not desirable
257 * in the long run, but will get a lot of hair out of this implementation.
258 * Maybe we should make this device a clonable device.
260 * Also note: we specifically do not attach a device to the device_t tree
261 * to avoid potential chicken and egg problems. One could argue that all
262 * of this belongs to the root node. One could also further argue that the
263 * sysctl interface that we have not might more properly be an ioctl
264 * interface, but at this stage of the game, I'm not inclined to rock that
267 * I'm also not sure that the SIGIO support is done correctly or not, as
268 * I copied it from a driver that had SIGIO support that likely hasn't been
269 * tested since 3.4 or 2.2.8!
272 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS
);
273 static int devctl_disable
= 0;
274 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable
);
275 SYSCTL_PROC(_hw_bus
, OID_AUTO
, devctl_disable
, CTLTYPE_INT
| CTLFLAG_RW
, 0, 0,
276 sysctl_devctl_disable
, "I", "devctl disable");
278 static d_open_t devopen
;
279 static d_close_t devclose
;
280 static d_read_t devread
;
281 static d_ioctl_t devioctl
;
282 static d_kqfilter_t devkqfilter
;
284 static struct dev_ops devctl_ops
= {
290 .d_kqfilter
= devkqfilter
293 struct dev_event_info
296 TAILQ_ENTRY(dev_event_info
) dei_link
;
299 TAILQ_HEAD(devq
, dev_event_info
);
301 static struct dev_softc
307 struct proc
*async_proc
;
313 make_dev(&devctl_ops
, 0, UID_ROOT
, GID_WHEEL
, 0600, "devctl");
314 lockinit(&devsoftc
.lock
, "dev mtx", 0, 0);
315 TAILQ_INIT(&devsoftc
.devq
);
319 devopen(struct dev_open_args
*ap
)
325 devsoftc
.async_proc
= NULL
;
330 devclose(struct dev_close_args
*ap
)
333 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
335 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
341 * The read channel for this device is used to report changes to
342 * userland in realtime. We are required to free the data as well as
343 * the n1 object because we allocate them separately. Also note that
344 * we return one record at a time. If you try to read this device a
345 * character at a time, you will lose the rest of the data. Listening
346 * programs are expected to cope.
349 devread(struct dev_read_args
*ap
)
351 struct uio
*uio
= ap
->a_uio
;
352 struct dev_event_info
*n1
;
355 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
356 while (TAILQ_EMPTY(&devsoftc
.devq
)) {
357 if (ap
->a_ioflag
& IO_NDELAY
) {
358 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
361 tsleep_interlock(&devsoftc
, PCATCH
);
362 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
363 rv
= tsleep(&devsoftc
, PCATCH
| PINTERLOCKED
, "devctl", 0);
364 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
367 * Need to translate ERESTART to EINTR here? -- jake
369 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
373 n1
= TAILQ_FIRST(&devsoftc
.devq
);
374 TAILQ_REMOVE(&devsoftc
.devq
, n1
, dei_link
);
375 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
376 rv
= uiomove(n1
->dei_data
, strlen(n1
->dei_data
), uio
);
377 kfree(n1
->dei_data
, M_BUS
);
383 devioctl(struct dev_ioctl_args
*ap
)
390 if (*(int*)ap
->a_data
)
391 devsoftc
.async_proc
= curproc
;
393 devsoftc
.async_proc
= NULL
;
396 /* (un)Support for other fcntl() calls. */
408 static void dev_filter_detach(struct knote
*);
409 static int dev_filter_read(struct knote
*, long);
411 static struct filterops dev_filtops
=
412 { FILTEROP_ISFD
, NULL
, dev_filter_detach
, dev_filter_read
};
415 devkqfilter(struct dev_kqfilter_args
*ap
)
417 struct knote
*kn
= ap
->a_kn
;
421 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
423 switch (kn
->kn_filter
) {
425 kn
->kn_fop
= &dev_filtops
;
428 ap
->a_result
= EOPNOTSUPP
;
429 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
433 klist
= &devsoftc
.kq
.ki_note
;
434 knote_insert(klist
, kn
);
436 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
442 dev_filter_detach(struct knote
*kn
)
446 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
447 klist
= &devsoftc
.kq
.ki_note
;
448 knote_remove(klist
, kn
);
449 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
453 dev_filter_read(struct knote
*kn
, long hint
)
457 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
458 if (!TAILQ_EMPTY(&devsoftc
.devq
))
460 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
467 * @brief Return whether the userland process is running
470 devctl_process_running(void)
472 return (devsoftc
.inuse
== 1);
476 * @brief Queue data to be read from the devctl device
478 * Generic interface to queue data to the devctl device. It is
479 * assumed that @p data is properly formatted. It is further assumed
480 * that @p data is allocated using the M_BUS malloc type.
483 devctl_queue_data(char *data
)
485 struct dev_event_info
*n1
= NULL
;
488 n1
= kmalloc(sizeof(*n1
), M_BUS
, M_NOWAIT
);
492 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
493 TAILQ_INSERT_TAIL(&devsoftc
.devq
, n1
, dei_link
);
495 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
496 KNOTE(&devsoftc
.kq
.ki_note
, 0);
497 p
= devsoftc
.async_proc
;
503 * @brief Send a 'notification' to userland, using standard ways
506 devctl_notify(const char *system
, const char *subsystem
, const char *type
,
513 return; /* BOGUS! Must specify system. */
514 if (subsystem
== NULL
)
515 return; /* BOGUS! Must specify subsystem. */
517 return; /* BOGUS! Must specify type. */
518 len
+= strlen(" system=") + strlen(system
);
519 len
+= strlen(" subsystem=") + strlen(subsystem
);
520 len
+= strlen(" type=") + strlen(type
);
521 /* add in the data message plus newline. */
524 len
+= 3; /* '!', '\n', and NUL */
525 msg
= kmalloc(len
, M_BUS
, M_NOWAIT
);
527 return; /* Drop it on the floor */
529 ksnprintf(msg
, len
, "!system=%s subsystem=%s type=%s %s\n",
530 system
, subsystem
, type
, data
);
532 ksnprintf(msg
, len
, "!system=%s subsystem=%s type=%s\n",
533 system
, subsystem
, type
);
534 devctl_queue_data(msg
);
538 * Common routine that tries to make sending messages as easy as possible.
539 * We allocate memory for the data, copy strings into that, but do not
540 * free it unless there's an error. The dequeue part of the driver should
541 * free the data. We don't send data when the device is disabled. We do
542 * send data, even when we have no listeners, because we wish to avoid
543 * races relating to startup and restart of listening applications.
545 * devaddq is designed to string together the type of event, with the
546 * object of that event, plus the plug and play info and location info
547 * for that event. This is likely most useful for devices, but less
548 * useful for other consumers of this interface. Those should use
549 * the devctl_queue_data() interface instead.
552 devaddq(const char *type
, const char *what
, device_t dev
)
561 data
= kmalloc(1024, M_BUS
, M_NOWAIT
);
565 /* get the bus specific location of this device */
566 loc
= kmalloc(1024, M_BUS
, M_NOWAIT
);
570 bus_child_location_str(dev
, loc
, 1024);
572 /* Get the bus specific pnp info of this device */
573 pnp
= kmalloc(1024, M_BUS
, M_NOWAIT
);
577 bus_child_pnpinfo_str(dev
, pnp
, 1024);
579 /* Get the parent of this device, or / if high enough in the tree. */
580 if (device_get_parent(dev
) == NULL
)
581 parstr
= "."; /* Or '/' ? */
583 parstr
= device_get_nameunit(device_get_parent(dev
));
584 /* String it all together. */
585 ksnprintf(data
, 1024, "%s%s at %s %s on %s\n", type
, what
, loc
, pnp
,
589 devctl_queue_data(data
);
599 * A device was added to the tree. We are called just after it successfully
600 * attaches (that is, probe and attach success for this device). No call
601 * is made if a device is merely parented into the tree. See devnomatch
602 * if probe fails. If attach fails, no notification is sent (but maybe
603 * we should have a different message for this).
606 devadded(device_t dev
)
611 pnp
= kmalloc(1024, M_BUS
, M_NOWAIT
);
614 tmp
= kmalloc(1024, M_BUS
, M_NOWAIT
);
618 bus_child_pnpinfo_str(dev
, pnp
, 1024);
619 ksnprintf(tmp
, 1024, "%s %s", device_get_nameunit(dev
), pnp
);
620 devaddq("+", tmp
, dev
);
630 * A device was removed from the tree. We are called just before this
634 devremoved(device_t dev
)
639 pnp
= kmalloc(1024, M_BUS
, M_NOWAIT
);
642 tmp
= kmalloc(1024, M_BUS
, M_NOWAIT
);
646 bus_child_pnpinfo_str(dev
, pnp
, 1024);
647 ksnprintf(tmp
, 1024, "%s %s", device_get_nameunit(dev
), pnp
);
648 devaddq("-", tmp
, dev
);
658 * Called when there's no match for this device. This is only called
659 * the first time that no match happens, so we don't keep getitng this
660 * message. Should that prove to be undesirable, we can change it.
661 * This is called when all drivers that can attach to a given bus
662 * decline to accept this device. Other errrors may not be detected.
665 devnomatch(device_t dev
)
667 devaddq("?", "", dev
);
671 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS
)
673 struct dev_event_info
*n1
;
676 dis
= devctl_disable
;
677 error
= sysctl_handle_int(oidp
, &dis
, 0, req
);
678 if (error
|| !req
->newptr
)
680 lockmgr(&devsoftc
.lock
, LK_EXCLUSIVE
);
681 devctl_disable
= dis
;
683 while (!TAILQ_EMPTY(&devsoftc
.devq
)) {
684 n1
= TAILQ_FIRST(&devsoftc
.devq
);
685 TAILQ_REMOVE(&devsoftc
.devq
, n1
, dei_link
);
686 kfree(n1
->dei_data
, M_BUS
);
690 lockmgr(&devsoftc
.lock
, LK_RELEASE
);
694 /* End of /dev/devctl code */
696 TAILQ_HEAD(,device
) bus_data_devices
;
697 static int bus_data_generation
= 1;
699 kobj_method_t null_methods
[] = {
703 DEFINE_CLASS(null
, null_methods
, 0);
706 * Devclass implementation
709 static devclass_list_t devclasses
= TAILQ_HEAD_INITIALIZER(devclasses
);
712 devclass_find_internal(const char *classname
, const char *parentname
,
717 PDEBUG(("looking for %s", classname
));
718 if (classname
== NULL
)
721 TAILQ_FOREACH(dc
, &devclasses
, link
)
722 if (!strcmp(dc
->name
, classname
))
726 PDEBUG(("creating %s", classname
));
727 dc
= kmalloc(sizeof(struct devclass
) + strlen(classname
) + 1,
728 M_BUS
, M_INTWAIT
| M_ZERO
);
730 dc
->name
= (char*) (dc
+ 1);
731 strcpy(dc
->name
, classname
);
734 TAILQ_INIT(&dc
->drivers
);
735 TAILQ_INSERT_TAIL(&devclasses
, dc
, link
);
737 bus_data_generation_update();
742 * If a parent class is specified, then set that as our parent so
743 * that this devclass will support drivers for the parent class as
744 * well. If the parent class has the same name don't do this though
745 * as it creates a cycle that can trigger an infinite loop in
746 * device_probe_child() if a device exists for which there is no
749 if (parentname
&& dc
&& !dc
->parent
&&
750 strcmp(classname
, parentname
) != 0)
751 dc
->parent
= devclass_find_internal(parentname
, NULL
, FALSE
);
757 devclass_create(const char *classname
)
759 return(devclass_find_internal(classname
, NULL
, TRUE
));
763 devclass_find(const char *classname
)
765 return(devclass_find_internal(classname
, NULL
, FALSE
));
769 devclass_find_unit(const char *classname
, int unit
)
773 if ((dc
= devclass_find(classname
)) != NULL
)
774 return(devclass_get_device(dc
, unit
));
779 devclass_add_driver(devclass_t dc
, driver_t
*driver
)
785 PDEBUG(("%s", DRIVERNAME(driver
)));
787 dl
= kmalloc(sizeof *dl
, M_BUS
, M_INTWAIT
| M_ZERO
);
790 * Compile the driver's methods. Also increase the reference count
791 * so that the class doesn't get freed when the last instance
792 * goes. This means we can safely use static methods and avoids a
793 * double-free in devclass_delete_driver.
795 kobj_class_instantiate(driver
);
798 * Make sure the devclass which the driver is implementing exists.
800 devclass_find_internal(driver
->name
, NULL
, TRUE
);
803 TAILQ_INSERT_TAIL(&dc
->drivers
, dl
, link
);
806 * Call BUS_DRIVER_ADDED for any existing busses in this class,
807 * but only if the bus has already been attached (otherwise we
808 * might probe too early).
810 * This is what will cause a newly loaded module to be associated
811 * with hardware. bus_generic_driver_added() is typically what ends
814 for (i
= 0; i
< dc
->maxunit
; i
++) {
815 if ((dev
= dc
->devices
[i
]) != NULL
) {
816 if (dev
->state
>= DS_ATTACHED
)
817 BUS_DRIVER_ADDED(dev
, driver
);
821 bus_data_generation_update();
826 devclass_delete_driver(devclass_t busclass
, driver_t
*driver
)
828 devclass_t dc
= devclass_find(driver
->name
);
834 PDEBUG(("%s from devclass %s", driver
->name
, DEVCLANAME(busclass
)));
840 * Find the link structure in the bus' list of drivers.
842 TAILQ_FOREACH(dl
, &busclass
->drivers
, link
)
843 if (dl
->driver
== driver
)
847 PDEBUG(("%s not found in %s list", driver
->name
, busclass
->name
));
852 * Disassociate from any devices. We iterate through all the
853 * devices in the devclass of the driver and detach any which are
854 * using the driver and which have a parent in the devclass which
855 * we are deleting from.
857 * Note that since a driver can be in multiple devclasses, we
858 * should not detach devices which are not children of devices in
859 * the affected devclass.
861 for (i
= 0; i
< dc
->maxunit
; i
++)
862 if (dc
->devices
[i
]) {
863 dev
= dc
->devices
[i
];
864 if (dev
->driver
== driver
&& dev
->parent
&&
865 dev
->parent
->devclass
== busclass
) {
866 if ((error
= device_detach(dev
)) != 0)
868 device_set_driver(dev
, NULL
);
872 TAILQ_REMOVE(&busclass
->drivers
, dl
, link
);
875 kobj_class_uninstantiate(driver
);
877 bus_data_generation_update();
882 devclass_find_driver_internal(devclass_t dc
, const char *classname
)
886 PDEBUG(("%s in devclass %s", classname
, DEVCLANAME(dc
)));
888 TAILQ_FOREACH(dl
, &dc
->drivers
, link
)
889 if (!strcmp(dl
->driver
->name
, classname
))
892 PDEBUG(("not found"));
897 devclass_find_driver(devclass_t dc
, const char *classname
)
901 dl
= devclass_find_driver_internal(dc
, classname
);
909 devclass_get_name(devclass_t dc
)
915 devclass_get_device(devclass_t dc
, int unit
)
917 if (dc
== NULL
|| unit
< 0 || unit
>= dc
->maxunit
)
919 return(dc
->devices
[unit
]);
923 devclass_get_softc(devclass_t dc
, int unit
)
927 dev
= devclass_get_device(dc
, unit
);
931 return(device_get_softc(dev
));
935 devclass_get_devices(devclass_t dc
, device_t
**devlistp
, int *devcountp
)
942 for (i
= 0; i
< dc
->maxunit
; i
++)
946 list
= kmalloc(count
* sizeof(device_t
), M_TEMP
, M_INTWAIT
| M_ZERO
);
949 for (i
= 0; i
< dc
->maxunit
; i
++)
950 if (dc
->devices
[i
]) {
951 list
[count
] = dc
->devices
[i
];
962 * @brief Get a list of drivers in the devclass
964 * An array containing a list of pointers to all the drivers in the
965 * given devclass is allocated and returned in @p *listp. The number
966 * of drivers in the array is returned in @p *countp. The caller should
967 * free the array using @c free(p, M_TEMP).
969 * @param dc the devclass to examine
970 * @param listp gives location for array pointer return value
971 * @param countp gives location for number of array elements
975 * @retval ENOMEM the array allocation failed
978 devclass_get_drivers(devclass_t dc
, driver_t
***listp
, int *countp
)
985 TAILQ_FOREACH(dl
, &dc
->drivers
, link
)
987 list
= kmalloc(count
* sizeof(driver_t
*), M_TEMP
, M_NOWAIT
);
992 TAILQ_FOREACH(dl
, &dc
->drivers
, link
) {
993 list
[count
] = dl
->driver
;
1003 * @brief Get the number of devices in a devclass
1005 * @param dc the devclass to examine
1008 devclass_get_count(devclass_t dc
)
1013 for (i
= 0; i
< dc
->maxunit
; i
++)
1020 devclass_get_maxunit(devclass_t dc
)
1022 return(dc
->maxunit
);
1026 devclass_set_parent(devclass_t dc
, devclass_t pdc
)
1032 devclass_get_parent(devclass_t dc
)
1038 devclass_alloc_unit(devclass_t dc
, int *unitp
)
1042 PDEBUG(("unit %d in devclass %s", unit
, DEVCLANAME(dc
)));
1044 /* If we have been given a wired unit number, check for existing device */
1046 if (unit
>= 0 && unit
< dc
->maxunit
&&
1047 dc
->devices
[unit
] != NULL
) {
1049 kprintf("%s-: %s%d exists, using next available unit number\n",
1050 dc
->name
, dc
->name
, unit
);
1051 /* find the next available slot */
1052 while (++unit
< dc
->maxunit
&& dc
->devices
[unit
] != NULL
)
1056 /* Unwired device, find the next available slot for it */
1058 while (unit
< dc
->maxunit
&& dc
->devices
[unit
] != NULL
)
1063 * We've selected a unit beyond the length of the table, so let's
1064 * extend the table to make room for all units up to and including
1067 if (unit
>= dc
->maxunit
) {
1071 newsize
= (unit
+ 1);
1072 newlist
= kmalloc(sizeof(device_t
) * newsize
, M_BUS
,
1073 M_INTWAIT
| M_ZERO
);
1074 if (newlist
== NULL
)
1076 bcopy(dc
->devices
, newlist
, sizeof(device_t
) * dc
->maxunit
);
1078 kfree(dc
->devices
, M_BUS
);
1079 dc
->devices
= newlist
;
1080 dc
->maxunit
= newsize
;
1082 PDEBUG(("now: unit %d in devclass %s", unit
, DEVCLANAME(dc
)));
1089 devclass_add_device(devclass_t dc
, device_t dev
)
1093 PDEBUG(("%s in devclass %s", DEVICENAME(dev
), DEVCLANAME(dc
)));
1095 buflen
= strlen(dc
->name
) + 5;
1096 dev
->nameunit
= kmalloc(buflen
, M_BUS
, M_INTWAIT
| M_ZERO
);
1097 if (dev
->nameunit
== NULL
)
1100 if ((error
= devclass_alloc_unit(dc
, &dev
->unit
)) != 0) {
1101 kfree(dev
->nameunit
, M_BUS
);
1102 dev
->nameunit
= NULL
;
1105 dc
->devices
[dev
->unit
] = dev
;
1107 ksnprintf(dev
->nameunit
, buflen
, "%s%d", dc
->name
, dev
->unit
);
1113 devclass_delete_device(devclass_t dc
, device_t dev
)
1118 PDEBUG(("%s in devclass %s", DEVICENAME(dev
), DEVCLANAME(dc
)));
1120 if (dev
->devclass
!= dc
|| dc
->devices
[dev
->unit
] != dev
)
1121 panic("devclass_delete_device: inconsistent device class");
1122 dc
->devices
[dev
->unit
] = NULL
;
1123 if (dev
->flags
& DF_WILDCARD
)
1125 dev
->devclass
= NULL
;
1126 kfree(dev
->nameunit
, M_BUS
);
1127 dev
->nameunit
= NULL
;
1133 make_device(device_t parent
, const char *name
, int unit
)
1138 PDEBUG(("%s at %s as unit %d", name
, DEVICENAME(parent
), unit
));
1141 dc
= devclass_find_internal(name
, NULL
, TRUE
);
1143 kprintf("make_device: can't find device class %s\n", name
);
1149 dev
= kmalloc(sizeof(struct device
), M_BUS
, M_INTWAIT
| M_ZERO
);
1153 dev
->parent
= parent
;
1154 TAILQ_INIT(&dev
->children
);
1155 kobj_init((kobj_t
) dev
, &null_class
);
1157 dev
->devclass
= NULL
;
1159 dev
->nameunit
= NULL
;
1163 dev
->flags
= DF_ENABLED
;
1166 dev
->flags
|= DF_WILDCARD
;
1168 dev
->flags
|= DF_FIXEDCLASS
;
1169 if (devclass_add_device(dc
, dev
) != 0) {
1170 kobj_delete((kobj_t
)dev
, M_BUS
);
1177 dev
->state
= DS_NOTPRESENT
;
1179 TAILQ_INSERT_TAIL(&bus_data_devices
, dev
, devlink
);
1180 bus_data_generation_update();
1186 device_print_child(device_t dev
, device_t child
)
1190 if (device_is_alive(child
))
1191 retval
+= BUS_PRINT_CHILD(dev
, child
);
1193 retval
+= device_printf(child
, " not found\n");
1199 device_add_child(device_t dev
, const char *name
, int unit
)
1201 return device_add_child_ordered(dev
, 0, name
, unit
);
1205 device_add_child_ordered(device_t dev
, int order
, const char *name
, int unit
)
1210 PDEBUG(("%s at %s with order %d as unit %d", name
, DEVICENAME(dev
),
1213 child
= make_device(dev
, name
, unit
);
1216 child
->order
= order
;
1218 TAILQ_FOREACH(place
, &dev
->children
, link
)
1219 if (place
->order
> order
)
1224 * The device 'place' is the first device whose order is
1225 * greater than the new child.
1227 TAILQ_INSERT_BEFORE(place
, child
, link
);
1230 * The new child's order is greater or equal to the order of
1231 * any existing device. Add the child to the tail of the list.
1233 TAILQ_INSERT_TAIL(&dev
->children
, child
, link
);
1236 bus_data_generation_update();
1241 device_delete_child(device_t dev
, device_t child
)
1244 device_t grandchild
;
1246 PDEBUG(("%s from %s", DEVICENAME(child
), DEVICENAME(dev
)));
1248 /* remove children first */
1249 while ( (grandchild
= TAILQ_FIRST(&child
->children
)) ) {
1250 error
= device_delete_child(child
, grandchild
);
1255 if ((error
= device_detach(child
)) != 0)
1257 if (child
->devclass
)
1258 devclass_delete_device(child
->devclass
, child
);
1259 TAILQ_REMOVE(&dev
->children
, child
, link
);
1260 TAILQ_REMOVE(&bus_data_devices
, child
, devlink
);
1261 kobj_delete((kobj_t
)child
, M_BUS
);
1263 bus_data_generation_update();
1268 * @brief Delete all children devices of the given device, if any.
1270 * This function deletes all children devices of the given device, if
1271 * any, using the device_delete_child() function for each device it
1272 * finds. If a child device cannot be deleted, this function will
1273 * return an error code.
1275 * @param dev the parent device
1278 * @retval non-zero a device would not detach
1281 device_delete_children(device_t dev
)
1286 PDEBUG(("Deleting all children of %s", DEVICENAME(dev
)));
1290 while ((child
= TAILQ_FIRST(&dev
->children
)) != NULL
) {
1291 error
= device_delete_child(dev
, child
);
1293 PDEBUG(("Failed deleting %s", DEVICENAME(child
)));
1301 * @brief Find a device given a unit number
1303 * This is similar to devclass_get_devices() but only searches for
1304 * devices which have @p dev as a parent.
1306 * @param dev the parent device to search
1307 * @param unit the unit number to search for. If the unit is -1,
1308 * return the first child of @p dev which has name
1309 * @p classname (that is, the one with the lowest unit.)
1311 * @returns the device with the given unit number or @c
1312 * NULL if there is no such device
1315 device_find_child(device_t dev
, const char *classname
, int unit
)
1320 dc
= devclass_find(classname
);
1325 child
= devclass_get_device(dc
, unit
);
1326 if (child
&& child
->parent
== dev
)
1329 for (unit
= 0; unit
< devclass_get_maxunit(dc
); unit
++) {
1330 child
= devclass_get_device(dc
, unit
);
1331 if (child
&& child
->parent
== dev
)
1339 first_matching_driver(devclass_t dc
, device_t dev
)
1342 return(devclass_find_driver_internal(dc
, dev
->devclass
->name
));
1344 return(TAILQ_FIRST(&dc
->drivers
));
1348 next_matching_driver(devclass_t dc
, device_t dev
, driverlink_t last
)
1350 if (dev
->devclass
) {
1352 for (dl
= TAILQ_NEXT(last
, link
); dl
; dl
= TAILQ_NEXT(dl
, link
))
1353 if (!strcmp(dev
->devclass
->name
, dl
->driver
->name
))
1357 return(TAILQ_NEXT(last
, link
));
1361 device_probe_child(device_t dev
, device_t child
)
1364 driverlink_t best
= NULL
;
1366 int result
, pri
= 0;
1367 int hasclass
= (child
->devclass
!= NULL
);
1371 panic("device_probe_child: parent device has no devclass");
1373 if (child
->state
== DS_ALIVE
)
1376 for (; dc
; dc
= dc
->parent
) {
1377 for (dl
= first_matching_driver(dc
, child
); dl
;
1378 dl
= next_matching_driver(dc
, child
, dl
)) {
1379 PDEBUG(("Trying %s", DRIVERNAME(dl
->driver
)));
1380 device_set_driver(child
, dl
->driver
);
1382 device_set_devclass(child
, dl
->driver
->name
);
1383 result
= DEVICE_PROBE(child
);
1385 device_set_devclass(child
, 0);
1388 * If the driver returns SUCCESS, there can be
1389 * no higher match for this device.
1398 * The driver returned an error so it
1399 * certainly doesn't match.
1402 device_set_driver(child
, NULL
);
1407 * A priority lower than SUCCESS, remember the
1408 * best matching driver. Initialise the value
1409 * of pri for the first match.
1411 if (best
== NULL
|| result
> pri
) {
1418 * If we have unambiguous match in this devclass,
1419 * don't look in the parent.
1421 if (best
&& pri
== 0)
1426 * If we found a driver, change state and initialise the devclass.
1429 if (!child
->devclass
)
1430 device_set_devclass(child
, best
->driver
->name
);
1431 device_set_driver(child
, best
->driver
);
1434 * A bit bogus. Call the probe method again to make
1435 * sure that we have the right description.
1437 DEVICE_PROBE(child
);
1440 bus_data_generation_update();
1441 child
->state
= DS_ALIVE
;
1449 device_get_parent(device_t dev
)
1455 device_get_children(device_t dev
, device_t
**devlistp
, int *devcountp
)
1462 TAILQ_FOREACH(child
, &dev
->children
, link
)
1465 list
= kmalloc(count
* sizeof(device_t
), M_TEMP
, M_INTWAIT
| M_ZERO
);
1468 TAILQ_FOREACH(child
, &dev
->children
, link
) {
1469 list
[count
] = child
;
1480 device_get_driver(device_t dev
)
1482 return(dev
->driver
);
1486 device_get_devclass(device_t dev
)
1488 return(dev
->devclass
);
1492 device_get_name(device_t dev
)
1495 return devclass_get_name(dev
->devclass
);
1500 device_get_nameunit(device_t dev
)
1502 return(dev
->nameunit
);
1506 device_get_unit(device_t dev
)
1512 device_get_desc(device_t dev
)
1518 device_get_flags(device_t dev
)
1520 return(dev
->devflags
);
1523 struct sysctl_ctx_list
*
1524 device_get_sysctl_ctx(device_t dev
)
1526 return (&dev
->sysctl_ctx
);
1530 device_get_sysctl_tree(device_t dev
)
1532 return (dev
->sysctl_tree
);
1536 device_print_prettyname(device_t dev
)
1538 const char *name
= device_get_name(dev
);
1541 return kprintf("unknown: ");
1543 return kprintf("%s%d: ", name
, device_get_unit(dev
));
1547 device_printf(device_t dev
, const char * fmt
, ...)
1552 retval
= device_print_prettyname(dev
);
1553 __va_start(ap
, fmt
);
1554 retval
+= kvprintf(fmt
, ap
);
1560 device_set_desc_internal(device_t dev
, const char* desc
, int copy
)
1562 if (dev
->desc
&& (dev
->flags
& DF_DESCMALLOCED
)) {
1563 kfree(dev
->desc
, M_BUS
);
1564 dev
->flags
&= ~DF_DESCMALLOCED
;
1569 dev
->desc
= kmalloc(strlen(desc
) + 1, M_BUS
, M_INTWAIT
);
1571 strcpy(dev
->desc
, desc
);
1572 dev
->flags
|= DF_DESCMALLOCED
;
1575 /* Avoid a -Wcast-qual warning */
1576 dev
->desc
= (char *)(uintptr_t) desc
;
1579 bus_data_generation_update();
1583 device_set_desc(device_t dev
, const char* desc
)
1585 device_set_desc_internal(dev
, desc
, FALSE
);
1589 device_set_desc_copy(device_t dev
, const char* desc
)
1591 device_set_desc_internal(dev
, desc
, TRUE
);
1595 device_set_flags(device_t dev
, uint32_t flags
)
1597 dev
->devflags
= flags
;
1601 device_get_softc(device_t dev
)
1607 device_set_softc(device_t dev
, void *softc
)
1609 if (dev
->softc
&& !(dev
->flags
& DF_EXTERNALSOFTC
))
1610 kfree(dev
->softc
, M_BUS
);
1613 dev
->flags
|= DF_EXTERNALSOFTC
;
1615 dev
->flags
&= ~DF_EXTERNALSOFTC
;
1619 device_set_async_attach(device_t dev
, int enable
)
1622 dev
->flags
|= DF_ASYNCPROBE
;
1624 dev
->flags
&= ~DF_ASYNCPROBE
;
1628 device_get_ivars(device_t dev
)
1634 device_set_ivars(device_t dev
, void * ivars
)
1643 device_get_state(device_t dev
)
1649 device_enable(device_t dev
)
1651 dev
->flags
|= DF_ENABLED
;
1655 device_disable(device_t dev
)
1657 dev
->flags
&= ~DF_ENABLED
;
1664 device_busy(device_t dev
)
1666 if (dev
->state
< DS_ATTACHED
)
1667 panic("device_busy: called for unattached device");
1668 if (dev
->busy
== 0 && dev
->parent
)
1669 device_busy(dev
->parent
);
1671 dev
->state
= DS_BUSY
;
1678 device_unbusy(device_t dev
)
1680 if (dev
->state
!= DS_BUSY
)
1681 panic("device_unbusy: called for non-busy device");
1683 if (dev
->busy
== 0) {
1685 device_unbusy(dev
->parent
);
1686 dev
->state
= DS_ATTACHED
;
1691 device_quiet(device_t dev
)
1693 dev
->flags
|= DF_QUIET
;
1697 device_verbose(device_t dev
)
1699 dev
->flags
&= ~DF_QUIET
;
1703 device_is_quiet(device_t dev
)
1705 return((dev
->flags
& DF_QUIET
) != 0);
1709 device_is_enabled(device_t dev
)
1711 return((dev
->flags
& DF_ENABLED
) != 0);
1715 device_is_alive(device_t dev
)
1717 return(dev
->state
>= DS_ALIVE
);
1721 device_is_attached(device_t dev
)
1723 return(dev
->state
>= DS_ATTACHED
);
1727 device_set_devclass(device_t dev
, const char *classname
)
1734 devclass_delete_device(dev
->devclass
, dev
);
1738 if (dev
->devclass
) {
1739 kprintf("device_set_devclass: device class already set\n");
1743 dc
= devclass_find_internal(classname
, NULL
, TRUE
);
1747 error
= devclass_add_device(dc
, dev
);
1749 bus_data_generation_update();
1754 device_set_driver(device_t dev
, driver_t
*driver
)
1756 if (dev
->state
>= DS_ATTACHED
)
1759 if (dev
->driver
== driver
)
1762 if (dev
->softc
&& !(dev
->flags
& DF_EXTERNALSOFTC
)) {
1763 kfree(dev
->softc
, M_BUS
);
1766 device_set_desc(dev
, NULL
);
1767 kobj_delete((kobj_t
) dev
, 0);
1768 dev
->driver
= driver
;
1770 kobj_init((kobj_t
) dev
, (kobj_class_t
) driver
);
1771 if (!(dev
->flags
& DF_EXTERNALSOFTC
))
1772 dev
->softc
= kmalloc(driver
->size
, M_BUS
,
1773 M_INTWAIT
| M_ZERO
);
1775 kobj_init((kobj_t
) dev
, &null_class
);
1778 bus_data_generation_update();
1783 device_probe_and_attach(device_t dev
)
1785 device_t bus
= dev
->parent
;
1788 if (dev
->state
>= DS_ALIVE
)
1791 if ((dev
->flags
& DF_ENABLED
) == 0) {
1793 device_print_prettyname(dev
);
1794 kprintf("not probed (disabled)\n");
1799 error
= device_probe_child(bus
, dev
);
1801 if (!(dev
->flags
& DF_DONENOMATCH
)) {
1802 BUS_PROBE_NOMATCH(bus
, dev
);
1804 dev
->flags
|= DF_DONENOMATCH
;
1810 * Output the exact device chain prior to the attach in case the
1811 * system locks up during attach, and generate the full info after
1812 * the attach so correct irq and other information is displayed.
1814 if (bootverbose
&& !device_is_quiet(dev
)) {
1817 kprintf("%s", device_get_nameunit(dev
));
1818 for (tmp
= dev
->parent
; tmp
; tmp
= tmp
->parent
)
1819 kprintf(".%s", device_get_nameunit(tmp
));
1822 if (!device_is_quiet(dev
))
1823 device_print_child(bus
, dev
);
1824 if ((dev
->flags
& DF_ASYNCPROBE
) && do_async_attach
) {
1825 kprintf("%s: probing asynchronously\n",
1826 device_get_nameunit(dev
));
1827 dev
->state
= DS_INPROGRESS
;
1828 device_attach_async(dev
);
1831 error
= device_doattach(dev
);
1837 * Device is known to be alive, do the attach asynchronously.
1838 * However, serialize the attaches with the mp lock.
1841 device_attach_async(device_t dev
)
1845 atomic_add_int(&numasyncthreads
, 1);
1846 lwkt_create(device_attach_thread
, dev
, &td
, NULL
,
1847 0, 0, "%s", (dev
->desc
? dev
->desc
: "devattach"));
1851 device_attach_thread(void *arg
)
1855 (void)device_doattach(dev
);
1856 atomic_subtract_int(&numasyncthreads
, 1);
1857 wakeup(&numasyncthreads
);
1861 * Device is known to be alive, do the attach (synchronous or asynchronous)
1864 device_doattach(device_t dev
)
1866 device_t bus
= dev
->parent
;
1867 int hasclass
= (dev
->devclass
!= NULL
);
1870 device_sysctl_init(dev
);
1871 error
= DEVICE_ATTACH(dev
);
1873 dev
->state
= DS_ATTACHED
;
1874 if (bootverbose
&& !device_is_quiet(dev
))
1875 device_print_child(bus
, dev
);
1876 device_sysctl_update(dev
);
1879 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1880 dev
->driver
->name
, dev
->unit
, error
);
1881 /* Unset the class that was set in device_probe_child */
1883 device_set_devclass(dev
, 0);
1884 device_set_driver(dev
, NULL
);
1885 dev
->state
= DS_NOTPRESENT
;
1886 device_sysctl_fini(dev
);
1892 device_detach(device_t dev
)
1896 PDEBUG(("%s", DEVICENAME(dev
)));
1897 if (dev
->state
== DS_BUSY
)
1899 if (dev
->state
!= DS_ATTACHED
)
1902 if ((error
= DEVICE_DETACH(dev
)) != 0)
1905 device_printf(dev
, "detached\n");
1907 BUS_CHILD_DETACHED(dev
->parent
, dev
);
1909 if (!(dev
->flags
& DF_FIXEDCLASS
))
1910 devclass_delete_device(dev
->devclass
, dev
);
1912 dev
->state
= DS_NOTPRESENT
;
1913 device_set_driver(dev
, NULL
);
1914 device_sysctl_fini(dev
);
1920 device_shutdown(device_t dev
)
1922 if (dev
->state
< DS_ATTACHED
)
1924 PDEBUG(("%s", DEVICENAME(dev
)));
1925 return DEVICE_SHUTDOWN(dev
);
1929 device_set_unit(device_t dev
, int unit
)
1934 dc
= device_get_devclass(dev
);
1935 if (unit
< dc
->maxunit
&& dc
->devices
[unit
])
1937 err
= devclass_delete_device(dc
, dev
);
1941 err
= devclass_add_device(dc
, dev
);
1945 bus_data_generation_update();
1949 /*======================================*/
1951 * Access functions for device resources.
1954 /* Supplied by config(8) in ioconf.c */
1955 extern struct config_device config_devtab
[];
1956 extern int devtab_count
;
1958 /* Runtime version */
1959 struct config_device
*devtab
= config_devtab
;
1962 resource_new_name(const char *name
, int unit
)
1964 struct config_device
*new;
1966 new = kmalloc((devtab_count
+ 1) * sizeof(*new), M_TEMP
,
1967 M_INTWAIT
| M_ZERO
);
1968 if (devtab
&& devtab_count
> 0)
1969 bcopy(devtab
, new, devtab_count
* sizeof(*new));
1970 new[devtab_count
].name
= kmalloc(strlen(name
) + 1, M_TEMP
, M_INTWAIT
);
1971 if (new[devtab_count
].name
== NULL
) {
1975 strcpy(new[devtab_count
].name
, name
);
1976 new[devtab_count
].unit
= unit
;
1977 new[devtab_count
].resource_count
= 0;
1978 new[devtab_count
].resources
= NULL
;
1979 if (devtab
&& devtab
!= config_devtab
)
1980 kfree(devtab
, M_TEMP
);
1982 return devtab_count
++;
1986 resource_new_resname(int j
, const char *resname
, resource_type type
)
1988 struct config_resource
*new;
1991 i
= devtab
[j
].resource_count
;
1992 new = kmalloc((i
+ 1) * sizeof(*new), M_TEMP
, M_INTWAIT
| M_ZERO
);
1993 if (devtab
[j
].resources
&& i
> 0)
1994 bcopy(devtab
[j
].resources
, new, i
* sizeof(*new));
1995 new[i
].name
= kmalloc(strlen(resname
) + 1, M_TEMP
, M_INTWAIT
);
1996 if (new[i
].name
== NULL
) {
2000 strcpy(new[i
].name
, resname
);
2002 if (devtab
[j
].resources
)
2003 kfree(devtab
[j
].resources
, M_TEMP
);
2004 devtab
[j
].resources
= new;
2005 devtab
[j
].resource_count
= i
+ 1;
2010 resource_match_string(int i
, const char *resname
, const char *value
)
2013 struct config_resource
*res
;
2015 for (j
= 0, res
= devtab
[i
].resources
;
2016 j
< devtab
[i
].resource_count
; j
++, res
++)
2017 if (!strcmp(res
->name
, resname
)
2018 && res
->type
== RES_STRING
2019 && !strcmp(res
->u
.stringval
, value
))
2025 resource_find(const char *name
, int unit
, const char *resname
,
2026 struct config_resource
**result
)
2029 struct config_resource
*res
;
2032 * First check specific instances, then generic.
2034 for (i
= 0; i
< devtab_count
; i
++) {
2035 if (devtab
[i
].unit
< 0)
2037 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
2038 res
= devtab
[i
].resources
;
2039 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
2040 if (!strcmp(res
->name
, resname
)) {
2046 for (i
= 0; i
< devtab_count
; i
++) {
2047 if (devtab
[i
].unit
>= 0)
2049 /* XXX should this `&& devtab[i].unit == unit' be here? */
2050 /* XXX if so, then the generic match does nothing */
2051 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
2052 res
= devtab
[i
].resources
;
2053 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
2054 if (!strcmp(res
->name
, resname
)) {
2064 resource_kenv(const char *name
, int unit
, const char *resname
, long *result
)
2069 ksnprintf(buf
, sizeof(buf
), "%s%d.%s", name
, unit
, resname
);
2070 if ((env
= kgetenv(buf
)) != NULL
) {
2071 *result
= strtol(env
, NULL
, 0);
2078 resource_int_value(const char *name
, int unit
, const char *resname
, int *result
)
2080 struct config_resource
*res
;
2084 if (resource_kenv(name
, unit
, resname
, &kvalue
) == 0) {
2085 *result
= (int)kvalue
;
2088 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
2090 if (res
->type
!= RES_INT
)
2092 *result
= res
->u
.intval
;
2097 resource_long_value(const char *name
, int unit
, const char *resname
,
2100 struct config_resource
*res
;
2104 if (resource_kenv(name
, unit
, resname
, &kvalue
) == 0) {
2108 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
2110 if (res
->type
!= RES_LONG
)
2112 *result
= res
->u
.longval
;
2117 resource_string_value(const char *name
, int unit
, const char *resname
,
2118 const char **result
)
2121 struct config_resource
*res
;
2125 ksnprintf(buf
, sizeof(buf
), "%s%d.%s", name
, unit
, resname
);
2126 if ((env
= kgetenv(buf
)) != NULL
) {
2131 if ((error
= resource_find(name
, unit
, resname
, &res
)) != 0)
2133 if (res
->type
!= RES_STRING
)
2135 *result
= res
->u
.stringval
;
2140 resource_query_string(int i
, const char *resname
, const char *value
)
2146 for (; i
< devtab_count
; i
++)
2147 if (resource_match_string(i
, resname
, value
) >= 0)
2153 resource_locate(int i
, const char *resname
)
2159 for (; i
< devtab_count
; i
++)
2160 if (!strcmp(devtab
[i
].name
, resname
))
2166 resource_count(void)
2168 return(devtab_count
);
2172 resource_query_name(int i
)
2174 return(devtab
[i
].name
);
2178 resource_query_unit(int i
)
2180 return(devtab
[i
].unit
);
2184 resource_create(const char *name
, int unit
, const char *resname
,
2185 resource_type type
, struct config_resource
**result
)
2188 struct config_resource
*res
= NULL
;
2190 for (i
= 0; i
< devtab_count
; i
++)
2191 if (!strcmp(devtab
[i
].name
, name
) && devtab
[i
].unit
== unit
) {
2192 res
= devtab
[i
].resources
;
2196 i
= resource_new_name(name
, unit
);
2199 res
= devtab
[i
].resources
;
2201 for (j
= 0; j
< devtab
[i
].resource_count
; j
++, res
++)
2202 if (!strcmp(res
->name
, resname
)) {
2206 j
= resource_new_resname(i
, resname
, type
);
2209 res
= &devtab
[i
].resources
[j
];
2215 resource_set_int(const char *name
, int unit
, const char *resname
, int value
)
2218 struct config_resource
*res
;
2220 error
= resource_create(name
, unit
, resname
, RES_INT
, &res
);
2223 if (res
->type
!= RES_INT
)
2225 res
->u
.intval
= value
;
2230 resource_set_long(const char *name
, int unit
, const char *resname
, long value
)
2233 struct config_resource
*res
;
2235 error
= resource_create(name
, unit
, resname
, RES_LONG
, &res
);
2238 if (res
->type
!= RES_LONG
)
2240 res
->u
.longval
= value
;
2245 resource_set_string(const char *name
, int unit
, const char *resname
,
2249 struct config_resource
*res
;
2251 error
= resource_create(name
, unit
, resname
, RES_STRING
, &res
);
2254 if (res
->type
!= RES_STRING
)
2256 if (res
->u
.stringval
)
2257 kfree(res
->u
.stringval
, M_TEMP
);
2258 res
->u
.stringval
= kmalloc(strlen(value
) + 1, M_TEMP
, M_INTWAIT
);
2259 if (res
->u
.stringval
== NULL
)
2261 strcpy(res
->u
.stringval
, value
);
2266 resource_cfgload(void *dummy __unused
)
2268 struct config_resource
*res
, *cfgres
;
2271 char *name
, *resname
;
2275 int config_devtab_count
;
2277 config_devtab_count
= devtab_count
;
2281 for (i
= 0; i
< config_devtab_count
; i
++) {
2282 name
= config_devtab
[i
].name
;
2283 unit
= config_devtab
[i
].unit
;
2285 for (j
= 0; j
< config_devtab
[i
].resource_count
; j
++) {
2286 cfgres
= config_devtab
[i
].resources
;
2287 resname
= cfgres
[j
].name
;
2288 type
= cfgres
[j
].type
;
2289 error
= resource_create(name
, unit
, resname
, type
,
2292 kprintf("create resource %s%d: error %d\n",
2296 if (res
->type
!= type
) {
2297 kprintf("type mismatch %s%d: %d != %d\n",
2298 name
, unit
, res
->type
, type
);
2303 res
->u
.intval
= cfgres
[j
].u
.intval
;
2306 res
->u
.longval
= cfgres
[j
].u
.longval
;
2309 if (res
->u
.stringval
)
2310 kfree(res
->u
.stringval
, M_TEMP
);
2311 stringval
= cfgres
[j
].u
.stringval
;
2312 res
->u
.stringval
= kmalloc(strlen(stringval
) + 1,
2314 if (res
->u
.stringval
== NULL
)
2316 strcpy(res
->u
.stringval
, stringval
);
2319 panic("unknown resource type %d", type
);
2324 SYSINIT(cfgload
, SI_BOOT1_POST
, SI_ORDER_ANY
+ 50, resource_cfgload
, 0);
2327 /*======================================*/
2329 * Some useful method implementations to make life easier for bus drivers.
2333 resource_list_init(struct resource_list
*rl
)
2339 resource_list_free(struct resource_list
*rl
)
2341 struct resource_list_entry
*rle
;
2343 while ((rle
= SLIST_FIRST(rl
)) != NULL
) {
2345 panic("resource_list_free: resource entry is busy");
2346 SLIST_REMOVE_HEAD(rl
, link
);
2352 resource_list_add(struct resource_list
*rl
, int type
, int rid
,
2353 u_long start
, u_long end
, u_long count
, int cpuid
)
2355 struct resource_list_entry
*rle
;
2357 rle
= resource_list_find(rl
, type
, rid
);
2359 rle
= kmalloc(sizeof(struct resource_list_entry
), M_BUS
,
2361 SLIST_INSERT_HEAD(rl
, rle
, link
);
2369 panic("resource_list_add: resource entry is busy");
2376 if (rle
->cpuid
!= -1 && rle
->cpuid
!= cpuid
) {
2377 panic("resource_list_add: moving from cpu%d -> cpu%d",
2384 struct resource_list_entry
*
2385 resource_list_find(struct resource_list
*rl
,
2388 struct resource_list_entry
*rle
;
2390 SLIST_FOREACH(rle
, rl
, link
)
2391 if (rle
->type
== type
&& rle
->rid
== rid
)
2397 resource_list_delete(struct resource_list
*rl
,
2400 struct resource_list_entry
*rle
= resource_list_find(rl
, type
, rid
);
2403 if (rle
->res
!= NULL
)
2404 panic("resource_list_delete: resource has not been released");
2405 SLIST_REMOVE(rl
, rle
, resource_list_entry
, link
);
2411 resource_list_alloc(struct resource_list
*rl
,
2412 device_t bus
, device_t child
,
2414 u_long start
, u_long end
,
2415 u_long count
, u_int flags
, int cpuid
)
2417 struct resource_list_entry
*rle
= NULL
;
2418 int passthrough
= (device_get_parent(child
) != bus
);
2419 int isdefault
= (start
== 0UL && end
== ~0UL);
2422 return(BUS_ALLOC_RESOURCE(device_get_parent(bus
), child
,
2424 start
, end
, count
, flags
, cpuid
));
2427 rle
= resource_list_find(rl
, type
, *rid
);
2430 return(0); /* no resource of that type/rid */
2433 panic("resource_list_alloc: resource entry is busy");
2437 count
= max(count
, rle
->count
);
2438 end
= max(rle
->end
, start
+ count
- 1);
2442 rle
->res
= BUS_ALLOC_RESOURCE(device_get_parent(bus
), child
,
2443 type
, rid
, start
, end
, count
,
2447 * Record the new range.
2450 rle
->start
= rman_get_start(rle
->res
);
2451 rle
->end
= rman_get_end(rle
->res
);
2459 resource_list_release(struct resource_list
*rl
,
2460 device_t bus
, device_t child
,
2461 int type
, int rid
, struct resource
*res
)
2463 struct resource_list_entry
*rle
= NULL
;
2464 int passthrough
= (device_get_parent(child
) != bus
);
2468 return(BUS_RELEASE_RESOURCE(device_get_parent(bus
), child
,
2472 rle
= resource_list_find(rl
, type
, rid
);
2475 panic("resource_list_release: can't find resource");
2477 panic("resource_list_release: resource entry is not busy");
2479 error
= BUS_RELEASE_RESOURCE(device_get_parent(bus
), child
,
2489 resource_list_print_type(struct resource_list
*rl
, const char *name
, int type
,
2492 struct resource_list_entry
*rle
;
2493 int printed
, retval
;
2497 /* Yes, this is kinda cheating */
2498 SLIST_FOREACH(rle
, rl
, link
) {
2499 if (rle
->type
== type
) {
2501 retval
+= kprintf(" %s ", name
);
2503 retval
+= kprintf(",");
2505 retval
+= kprintf(format
, rle
->start
);
2506 if (rle
->count
> 1) {
2507 retval
+= kprintf("-");
2508 retval
+= kprintf(format
, rle
->start
+
2517 * Generic driver/device identify functions. These will install a device
2518 * rendezvous point under the parent using the same name as the driver
2519 * name, which will at a later time be probed and attached.
2521 * These functions are used when the parent does not 'scan' its bus for
2522 * matching devices, or for the particular devices using these functions,
2523 * or when the device is a pseudo or synthesized device (such as can be
2524 * found under firewire and ppbus).
2527 bus_generic_identify(driver_t
*driver
, device_t parent
)
2529 if (parent
->state
== DS_ATTACHED
)
2531 BUS_ADD_CHILD(parent
, parent
, 0, driver
->name
, -1);
2536 bus_generic_identify_sameunit(driver_t
*driver
, device_t parent
)
2538 if (parent
->state
== DS_ATTACHED
)
2540 BUS_ADD_CHILD(parent
, parent
, 0, driver
->name
, device_get_unit(parent
));
2545 * Call DEVICE_IDENTIFY for each driver.
2548 bus_generic_probe(device_t dev
)
2550 devclass_t dc
= dev
->devclass
;
2553 TAILQ_FOREACH(dl
, &dc
->drivers
, link
) {
2554 DEVICE_IDENTIFY(dl
->driver
, dev
);
2561 * This is an aweful hack due to the isa bus and autoconf code not
2562 * probing the ISA devices until after everything else has configured.
2563 * The ISA bus did a dummy attach long ago so we have to set it back
2564 * to an earlier state so the probe thinks its the initial probe and
2567 * XXX remove by properly defering the ISA bus scan.
2570 bus_generic_probe_hack(device_t dev
)
2572 if (dev
->state
== DS_ATTACHED
) {
2573 dev
->state
= DS_ALIVE
;
2574 bus_generic_probe(dev
);
2575 dev
->state
= DS_ATTACHED
;
2581 bus_generic_attach(device_t dev
)
2585 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2586 device_probe_and_attach(child
);
2593 bus_generic_detach(device_t dev
)
2598 if (dev
->state
!= DS_ATTACHED
)
2601 TAILQ_FOREACH(child
, &dev
->children
, link
)
2602 if ((error
= device_detach(child
)) != 0)
2609 bus_generic_shutdown(device_t dev
)
2613 TAILQ_FOREACH(child
, &dev
->children
, link
)
2614 device_shutdown(child
);
2620 bus_generic_suspend(device_t dev
)
2623 device_t child
, child2
;
2625 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2626 error
= DEVICE_SUSPEND(child
);
2628 for (child2
= TAILQ_FIRST(&dev
->children
);
2629 child2
&& child2
!= child
;
2630 child2
= TAILQ_NEXT(child2
, link
))
2631 DEVICE_RESUME(child2
);
2639 bus_generic_resume(device_t dev
)
2643 TAILQ_FOREACH(child
, &dev
->children
, link
)
2644 DEVICE_RESUME(child
);
2645 /* if resume fails, there's nothing we can usefully do... */
2651 bus_print_child_header(device_t dev
, device_t child
)
2655 if (device_get_desc(child
))
2656 retval
+= device_printf(child
, "<%s>", device_get_desc(child
));
2658 retval
+= kprintf("%s", device_get_nameunit(child
));
2660 if (child
->state
!= DS_ATTACHED
)
2661 kprintf(" [tentative]");
2663 kprintf(" [attached!]");
2669 bus_print_child_footer(device_t dev
, device_t child
)
2671 return(kprintf(" on %s\n", device_get_nameunit(dev
)));
2675 bus_generic_add_child(device_t dev
, device_t child
, int order
,
2676 const char *name
, int unit
)
2679 dev
= BUS_ADD_CHILD(dev
->parent
, child
, order
, name
, unit
);
2681 dev
= device_add_child_ordered(child
, order
, name
, unit
);
2687 bus_generic_print_child(device_t dev
, device_t child
)
2691 retval
+= bus_print_child_header(dev
, child
);
2692 retval
+= bus_print_child_footer(dev
, child
);
2698 bus_generic_read_ivar(device_t dev
, device_t child
, int index
,
2704 error
= BUS_READ_IVAR(dev
->parent
, child
, index
, result
);
2711 bus_generic_write_ivar(device_t dev
, device_t child
, int index
,
2717 error
= BUS_WRITE_IVAR(dev
->parent
, child
, index
, value
);
2724 * Resource list are used for iterations, do not recurse.
2726 struct resource_list
*
2727 bus_generic_get_resource_list(device_t dev
, device_t child
)
2733 bus_generic_driver_added(device_t dev
, driver_t
*driver
)
2737 DEVICE_IDENTIFY(driver
, dev
);
2738 TAILQ_FOREACH(child
, &dev
->children
, link
) {
2739 if (child
->state
== DS_NOTPRESENT
)
2740 device_probe_and_attach(child
);
2745 bus_generic_setup_intr(device_t dev
, device_t child
, struct resource
*irq
,
2746 int flags
, driver_intr_t
*intr
, void *arg
, void **cookiep
,
2747 lwkt_serialize_t serializer
, const char *desc
)
2749 /* Propagate up the bus hierarchy until someone handles it. */
2751 return BUS_SETUP_INTR(dev
->parent
, child
, irq
, flags
,
2752 intr
, arg
, cookiep
, serializer
, desc
);
2759 bus_generic_teardown_intr(device_t dev
, device_t child
, struct resource
*irq
,
2762 /* Propagate up the bus hierarchy until someone handles it. */
2764 return(BUS_TEARDOWN_INTR(dev
->parent
, child
, irq
, cookie
));
2770 bus_generic_disable_intr(device_t dev
, device_t child
, void *cookie
)
2773 return(BUS_DISABLE_INTR(dev
->parent
, child
, cookie
));
2779 bus_generic_enable_intr(device_t dev
, device_t child
, void *cookie
)
2782 BUS_ENABLE_INTR(dev
->parent
, child
, cookie
);
2786 bus_generic_config_intr(device_t dev
, device_t child
, int irq
, enum intr_trigger trig
,
2787 enum intr_polarity pol
)
2789 /* Propagate up the bus hierarchy until someone handles it. */
2791 return(BUS_CONFIG_INTR(dev
->parent
, child
, irq
, trig
, pol
));
2797 bus_generic_alloc_resource(device_t dev
, device_t child
, int type
, int *rid
,
2798 u_long start
, u_long end
, u_long count
, u_int flags
, int cpuid
)
2800 /* Propagate up the bus hierarchy until someone handles it. */
2802 return(BUS_ALLOC_RESOURCE(dev
->parent
, child
, type
, rid
,
2803 start
, end
, count
, flags
, cpuid
));
2809 bus_generic_release_resource(device_t dev
, device_t child
, int type
, int rid
,
2812 /* Propagate up the bus hierarchy until someone handles it. */
2814 return(BUS_RELEASE_RESOURCE(dev
->parent
, child
, type
, rid
, r
));
2820 bus_generic_activate_resource(device_t dev
, device_t child
, int type
, int rid
,
2823 /* Propagate up the bus hierarchy until someone handles it. */
2825 return(BUS_ACTIVATE_RESOURCE(dev
->parent
, child
, type
, rid
, r
));
2831 bus_generic_deactivate_resource(device_t dev
, device_t child
, int type
,
2832 int rid
, struct resource
*r
)
2834 /* Propagate up the bus hierarchy until someone handles it. */
2836 return(BUS_DEACTIVATE_RESOURCE(dev
->parent
, child
, type
, rid
,
2843 bus_generic_get_resource(device_t dev
, device_t child
, int type
, int rid
,
2844 u_long
*startp
, u_long
*countp
)
2850 error
= BUS_GET_RESOURCE(dev
->parent
, child
, type
, rid
,
2857 bus_generic_set_resource(device_t dev
, device_t child
, int type
, int rid
,
2858 u_long start
, u_long count
, int cpuid
)
2864 error
= BUS_SET_RESOURCE(dev
->parent
, child
, type
, rid
,
2865 start
, count
, cpuid
);
2871 bus_generic_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
2874 BUS_DELETE_RESOURCE(dev
, child
, type
, rid
);
2878 * @brief Helper function for implementing BUS_GET_DMA_TAG().
2880 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
2881 * BUS_GET_DMA_TAG() method of the parent of @p dev.
2884 bus_generic_get_dma_tag(device_t dev
, device_t child
)
2887 /* Propagate up the bus hierarchy until someone handles it. */
2888 if (dev
->parent
!= NULL
)
2889 return (BUS_GET_DMA_TAG(dev
->parent
, child
));
2894 bus_generic_rl_get_resource(device_t dev
, device_t child
, int type
, int rid
,
2895 u_long
*startp
, u_long
*countp
)
2897 struct resource_list
*rl
= NULL
;
2898 struct resource_list_entry
*rle
= NULL
;
2900 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2904 rle
= resource_list_find(rl
, type
, rid
);
2909 *startp
= rle
->start
;
2911 *countp
= rle
->count
;
2917 bus_generic_rl_set_resource(device_t dev
, device_t child
, int type
, int rid
,
2918 u_long start
, u_long count
, int cpuid
)
2920 struct resource_list
*rl
= NULL
;
2922 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2926 resource_list_add(rl
, type
, rid
, start
, (start
+ count
- 1), count
,
2933 bus_generic_rl_delete_resource(device_t dev
, device_t child
, int type
, int rid
)
2935 struct resource_list
*rl
= NULL
;
2937 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2941 resource_list_delete(rl
, type
, rid
);
2945 bus_generic_rl_release_resource(device_t dev
, device_t child
, int type
,
2946 int rid
, struct resource
*r
)
2948 struct resource_list
*rl
= NULL
;
2950 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2954 return(resource_list_release(rl
, dev
, child
, type
, rid
, r
));
2958 bus_generic_rl_alloc_resource(device_t dev
, device_t child
, int type
,
2959 int *rid
, u_long start
, u_long end
, u_long count
, u_int flags
, int cpuid
)
2961 struct resource_list
*rl
= NULL
;
2963 rl
= BUS_GET_RESOURCE_LIST(dev
, child
);
2967 return(resource_list_alloc(rl
, dev
, child
, type
, rid
,
2968 start
, end
, count
, flags
, cpuid
));
2972 bus_generic_child_present(device_t bus
, device_t child
)
2974 return(BUS_CHILD_PRESENT(device_get_parent(bus
), bus
));
2979 * Some convenience functions to make it easier for drivers to use the
2980 * resource-management functions. All these really do is hide the
2981 * indirection through the parent's method table, making for slightly
2982 * less-wordy code. In the future, it might make sense for this code
2983 * to maintain some sort of a list of resources allocated by each device.
2986 bus_alloc_resources(device_t dev
, struct resource_spec
*rs
,
2987 struct resource
**res
)
2991 for (i
= 0; rs
[i
].type
!= -1; i
++)
2993 for (i
= 0; rs
[i
].type
!= -1; i
++) {
2994 res
[i
] = bus_alloc_resource_any(dev
,
2995 rs
[i
].type
, &rs
[i
].rid
, rs
[i
].flags
);
2996 if (res
[i
] == NULL
) {
2997 bus_release_resources(dev
, rs
, res
);
3005 bus_release_resources(device_t dev
, const struct resource_spec
*rs
,
3006 struct resource
**res
)
3010 for (i
= 0; rs
[i
].type
!= -1; i
++)
3011 if (res
[i
] != NULL
) {
3012 bus_release_resource(
3013 dev
, rs
[i
].type
, rs
[i
].rid
, res
[i
]);
3019 bus_alloc_resource(device_t dev
, int type
, int *rid
, u_long start
, u_long end
,
3020 u_long count
, u_int flags
)
3022 if (dev
->parent
== NULL
)
3024 return(BUS_ALLOC_RESOURCE(dev
->parent
, dev
, type
, rid
, start
, end
,
3029 bus_alloc_legacy_irq_resource(device_t dev
, int *rid
, u_long irq
, u_int flags
)
3031 if (dev
->parent
== NULL
)
3033 return BUS_ALLOC_RESOURCE(dev
->parent
, dev
, SYS_RES_IRQ
, rid
,
3034 irq
, irq
, 1, flags
, machintr_legacy_intr_cpuid(irq
));
3038 bus_activate_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
3040 if (dev
->parent
== NULL
)
3042 return(BUS_ACTIVATE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
3046 bus_deactivate_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
3048 if (dev
->parent
== NULL
)
3050 return(BUS_DEACTIVATE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
3054 bus_release_resource(device_t dev
, int type
, int rid
, struct resource
*r
)
3056 if (dev
->parent
== NULL
)
3058 return(BUS_RELEASE_RESOURCE(dev
->parent
, dev
, type
, rid
, r
));
3062 bus_setup_intr_descr(device_t dev
, struct resource
*r
, int flags
,
3063 driver_intr_t handler
, void *arg
, void **cookiep
,
3064 lwkt_serialize_t serializer
, const char *desc
)
3066 if (dev
->parent
== NULL
)
3068 return BUS_SETUP_INTR(dev
->parent
, dev
, r
, flags
, handler
, arg
,
3069 cookiep
, serializer
, desc
);
3073 bus_setup_intr(device_t dev
, struct resource
*r
, int flags
,
3074 driver_intr_t handler
, void *arg
, void **cookiep
,
3075 lwkt_serialize_t serializer
)
3077 return bus_setup_intr_descr(dev
, r
, flags
, handler
, arg
, cookiep
,
3082 bus_teardown_intr(device_t dev
, struct resource
*r
, void *cookie
)
3084 if (dev
->parent
== NULL
)
3086 return(BUS_TEARDOWN_INTR(dev
->parent
, dev
, r
, cookie
));
3090 bus_enable_intr(device_t dev
, void *cookie
)
3093 BUS_ENABLE_INTR(dev
->parent
, dev
, cookie
);
3097 bus_disable_intr(device_t dev
, void *cookie
)
3100 return(BUS_DISABLE_INTR(dev
->parent
, dev
, cookie
));
3106 bus_set_resource(device_t dev
, int type
, int rid
,
3107 u_long start
, u_long count
, int cpuid
)
3109 return(BUS_SET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3110 start
, count
, cpuid
));
3114 bus_get_resource(device_t dev
, int type
, int rid
,
3115 u_long
*startp
, u_long
*countp
)
3117 return(BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3122 bus_get_resource_start(device_t dev
, int type
, int rid
)
3124 u_long start
, count
;
3127 error
= BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3135 bus_get_resource_count(device_t dev
, int type
, int rid
)
3137 u_long start
, count
;
3140 error
= BUS_GET_RESOURCE(device_get_parent(dev
), dev
, type
, rid
,
3148 bus_delete_resource(device_t dev
, int type
, int rid
)
3150 BUS_DELETE_RESOURCE(device_get_parent(dev
), dev
, type
, rid
);
3154 bus_child_present(device_t child
)
3156 return (BUS_CHILD_PRESENT(device_get_parent(child
), child
));
3160 bus_child_pnpinfo_str(device_t child
, char *buf
, size_t buflen
)
3164 parent
= device_get_parent(child
);
3165 if (parent
== NULL
) {
3169 return (BUS_CHILD_PNPINFO_STR(parent
, child
, buf
, buflen
));
3173 bus_child_location_str(device_t child
, char *buf
, size_t buflen
)
3177 parent
= device_get_parent(child
);
3178 if (parent
== NULL
) {
3182 return (BUS_CHILD_LOCATION_STR(parent
, child
, buf
, buflen
));
3186 * @brief Wrapper function for BUS_GET_DMA_TAG().
3188 * This function simply calls the BUS_GET_DMA_TAG() method of the
3192 bus_get_dma_tag(device_t dev
)
3196 parent
= device_get_parent(dev
);
3199 return (BUS_GET_DMA_TAG(parent
, dev
));
3203 root_print_child(device_t dev
, device_t child
)
3209 root_setup_intr(device_t dev
, device_t child
, driver_intr_t
*intr
, void *arg
,
3210 void **cookiep
, lwkt_serialize_t serializer
, const char *desc
)
3213 * If an interrupt mapping gets to here something bad has happened.
3215 panic("root_setup_intr");
3219 * If we get here, assume that the device is permanant and really is
3220 * present in the system. Removable bus drivers are expected to intercept
3221 * this call long before it gets here. We return -1 so that drivers that
3222 * really care can check vs -1 or some ERRNO returned higher in the food
3226 root_child_present(device_t dev
, device_t child
)
3232 * XXX NOTE! other defaults may be set in bus_if.m
3234 static kobj_method_t root_methods
[] = {
3235 /* Device interface */
3236 KOBJMETHOD(device_shutdown
, bus_generic_shutdown
),
3237 KOBJMETHOD(device_suspend
, bus_generic_suspend
),
3238 KOBJMETHOD(device_resume
, bus_generic_resume
),
3241 KOBJMETHOD(bus_add_child
, bus_generic_add_child
),
3242 KOBJMETHOD(bus_print_child
, root_print_child
),
3243 KOBJMETHOD(bus_read_ivar
, bus_generic_read_ivar
),
3244 KOBJMETHOD(bus_write_ivar
, bus_generic_write_ivar
),
3245 KOBJMETHOD(bus_setup_intr
, root_setup_intr
),
3246 KOBJMETHOD(bus_child_present
, root_child_present
),
3251 static driver_t root_driver
= {
3258 devclass_t root_devclass
;
3261 root_bus_module_handler(module_t mod
, int what
, void* arg
)
3265 TAILQ_INIT(&bus_data_devices
);
3266 root_bus
= make_device(NULL
, "root", 0);
3267 root_bus
->desc
= "System root bus";
3268 kobj_init((kobj_t
) root_bus
, (kobj_class_t
) &root_driver
);
3269 root_bus
->driver
= &root_driver
;
3270 root_bus
->state
= DS_ALIVE
;
3271 root_devclass
= devclass_find_internal("root", NULL
, FALSE
);
3276 device_shutdown(root_bus
);
3283 static moduledata_t root_bus_mod
= {
3285 root_bus_module_handler
,
3288 DECLARE_MODULE(rootbus
, root_bus_mod
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
);
3291 root_bus_configure(void)
3299 * handle device_identify based device attachments to the root_bus
3300 * (typically nexus).
3302 bus_generic_probe(root_bus
);
3305 * Probe and attach the devices under root_bus.
3307 TAILQ_FOREACH(dev
, &root_bus
->children
, link
) {
3308 device_probe_and_attach(dev
);
3312 * Wait for all asynchronous attaches to complete. If we don't
3313 * our legacy ISA bus scan could steal device unit numbers or
3317 if (numasyncthreads
)
3318 kprintf("Waiting for async drivers to attach\n");
3319 while (numasyncthreads
> 0) {
3320 if (tsleep(&numasyncthreads
, 0, "rootbus", hz
) == EWOULDBLOCK
)
3322 if (warncount
== 0) {
3323 kprintf("Warning: Still waiting for %d "
3324 "drivers to attach\n", numasyncthreads
);
3325 } else if (warncount
== -30) {
3326 kprintf("Giving up on %d drivers\n", numasyncthreads
);
3330 root_bus
->state
= DS_ATTACHED
;
3334 driver_module_handler(module_t mod
, int what
, void *arg
)
3337 struct driver_module_data
*dmd
;
3338 devclass_t bus_devclass
;
3339 kobj_class_t driver
;
3340 const char *parentname
;
3342 dmd
= (struct driver_module_data
*)arg
;
3343 bus_devclass
= devclass_find_internal(dmd
->dmd_busname
, NULL
, TRUE
);
3348 if (dmd
->dmd_chainevh
)
3349 error
= dmd
->dmd_chainevh(mod
,what
,dmd
->dmd_chainarg
);
3351 driver
= dmd
->dmd_driver
;
3352 PDEBUG(("Loading module: driver %s on bus %s",
3353 DRIVERNAME(driver
), dmd
->dmd_busname
));
3356 * If the driver has any base classes, make the
3357 * devclass inherit from the devclass of the driver's
3358 * first base class. This will allow the system to
3359 * search for drivers in both devclasses for children
3360 * of a device using this driver.
3362 if (driver
->baseclasses
)
3363 parentname
= driver
->baseclasses
[0]->name
;
3366 *dmd
->dmd_devclass
= devclass_find_internal(driver
->name
,
3369 error
= devclass_add_driver(bus_devclass
, driver
);
3375 PDEBUG(("Unloading module: driver %s from bus %s",
3376 DRIVERNAME(dmd
->dmd_driver
), dmd
->dmd_busname
));
3377 error
= devclass_delete_driver(bus_devclass
, dmd
->dmd_driver
);
3379 if (!error
&& dmd
->dmd_chainevh
)
3380 error
= dmd
->dmd_chainevh(mod
,what
,dmd
->dmd_chainarg
);
3390 * The _short versions avoid iteration by not calling anything that prints
3391 * more than oneliners. I love oneliners.
3395 print_device_short(device_t dev
, int indent
)
3400 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3401 dev
->unit
, dev
->desc
,
3402 (dev
->parent
? "":"no "),
3403 (TAILQ_EMPTY(&dev
->children
)? "no ":""),
3404 (dev
->flags
&DF_ENABLED
? "enabled,":"disabled,"),
3405 (dev
->flags
&DF_FIXEDCLASS
? "fixed,":""),
3406 (dev
->flags
&DF_WILDCARD
? "wildcard,":""),
3407 (dev
->flags
&DF_DESCMALLOCED
? "descmalloced,":""),
3408 (dev
->ivars
? "":"no "),
3409 (dev
->softc
? "":"no "),
3414 print_device(device_t dev
, int indent
)
3419 print_device_short(dev
, indent
);
3421 indentprintf(("Parent:\n"));
3422 print_device_short(dev
->parent
, indent
+1);
3423 indentprintf(("Driver:\n"));
3424 print_driver_short(dev
->driver
, indent
+1);
3425 indentprintf(("Devclass:\n"));
3426 print_devclass_short(dev
->devclass
, indent
+1);
3430 * Print the device and all its children (indented).
3433 print_device_tree_short(device_t dev
, int indent
)
3440 print_device_short(dev
, indent
);
3442 TAILQ_FOREACH(child
, &dev
->children
, link
)
3443 print_device_tree_short(child
, indent
+1);
3447 * Print the device and all its children (indented).
3450 print_device_tree(device_t dev
, int indent
)
3457 print_device(dev
, indent
);
3459 TAILQ_FOREACH(child
, &dev
->children
, link
)
3460 print_device_tree(child
, indent
+1);
3464 print_driver_short(driver_t
*driver
, int indent
)
3469 indentprintf(("driver %s: softc size = %zu\n",
3470 driver
->name
, driver
->size
));
3474 print_driver(driver_t
*driver
, int indent
)
3479 print_driver_short(driver
, indent
);
3484 print_driver_list(driver_list_t drivers
, int indent
)
3486 driverlink_t driver
;
3488 TAILQ_FOREACH(driver
, &drivers
, link
)
3489 print_driver(driver
->driver
, indent
);
3493 print_devclass_short(devclass_t dc
, int indent
)
3498 indentprintf(("devclass %s: max units = %d\n", dc
->name
, dc
->maxunit
));
3502 print_devclass(devclass_t dc
, int indent
)
3509 print_devclass_short(dc
, indent
);
3510 indentprintf(("Drivers:\n"));
3511 print_driver_list(dc
->drivers
, indent
+1);
3513 indentprintf(("Devices:\n"));
3514 for (i
= 0; i
< dc
->maxunit
; i
++)
3516 print_device(dc
->devices
[i
], indent
+1);
3520 print_devclass_list_short(void)
3524 kprintf("Short listing of devclasses, drivers & devices:\n");
3525 TAILQ_FOREACH(dc
, &devclasses
, link
) {
3526 print_devclass_short(dc
, 0);
3531 print_devclass_list(void)
3535 kprintf("Full listing of devclasses, drivers & devices:\n");
3536 TAILQ_FOREACH(dc
, &devclasses
, link
) {
3537 print_devclass(dc
, 0);
3544 * Check to see if a device is disabled via a disabled hint.
3547 resource_disabled(const char *name
, int unit
)
3551 error
= resource_int_value(name
, unit
, "disabled", &value
);
3558 * User-space access to the device tree.
3560 * We implement a small set of nodes:
3562 * hw.bus Single integer read method to obtain the
3563 * current generation count.
3564 * hw.bus.devices Reads the entire device tree in flat space.
3565 * hw.bus.rman Resource manager interface
3567 * We might like to add the ability to scan devclasses and/or drivers to
3568 * determine what else is currently loaded/available.
3572 sysctl_bus(SYSCTL_HANDLER_ARGS
)
3574 struct u_businfo ubus
;
3576 ubus
.ub_version
= BUS_USER_VERSION
;
3577 ubus
.ub_generation
= bus_data_generation
;
3579 return (SYSCTL_OUT(req
, &ubus
, sizeof(ubus
)));
3581 SYSCTL_NODE(_hw_bus
, OID_AUTO
, info
, CTLFLAG_RW
, sysctl_bus
,
3582 "bus-related data");
3585 sysctl_devices(SYSCTL_HANDLER_ARGS
)
3587 int *name
= (int *)arg1
;
3588 u_int namelen
= arg2
;
3591 struct u_device udev
; /* XXX this is a bit big */
3597 if (bus_data_generation_check(name
[0]))
3603 * Scan the list of devices, looking for the requested index.
3605 TAILQ_FOREACH(dev
, &bus_data_devices
, devlink
) {
3613 * Populate the return array.
3615 bzero(&udev
, sizeof(udev
));
3616 udev
.dv_handle
= (uintptr_t)dev
;
3617 udev
.dv_parent
= (uintptr_t)dev
->parent
;
3618 if (dev
->nameunit
!= NULL
)
3619 strlcpy(udev
.dv_name
, dev
->nameunit
, sizeof(udev
.dv_name
));
3620 if (dev
->desc
!= NULL
)
3621 strlcpy(udev
.dv_desc
, dev
->desc
, sizeof(udev
.dv_desc
));
3622 if (dev
->driver
!= NULL
&& dev
->driver
->name
!= NULL
)
3623 strlcpy(udev
.dv_drivername
, dev
->driver
->name
,
3624 sizeof(udev
.dv_drivername
));
3625 bus_child_pnpinfo_str(dev
, udev
.dv_pnpinfo
, sizeof(udev
.dv_pnpinfo
));
3626 bus_child_location_str(dev
, udev
.dv_location
, sizeof(udev
.dv_location
));
3627 udev
.dv_devflags
= dev
->devflags
;
3628 udev
.dv_flags
= dev
->flags
;
3629 udev
.dv_state
= dev
->state
;
3630 error
= SYSCTL_OUT(req
, &udev
, sizeof(udev
));
3634 SYSCTL_NODE(_hw_bus
, OID_AUTO
, devices
, CTLFLAG_RD
, sysctl_devices
,
3635 "system device tree");
3638 bus_data_generation_check(int generation
)
3640 if (generation
!= bus_data_generation
)
3643 /* XXX generate optimised lists here? */
3648 bus_data_generation_update(void)
3650 bus_data_generation
++;
3654 intr_str_polarity(enum intr_polarity pola
)
3657 case INTR_POLARITY_LOW
:
3660 case INTR_POLARITY_HIGH
:
3663 case INTR_POLARITY_CONFORM
:
3670 intr_str_trigger(enum intr_trigger trig
)
3673 case INTR_TRIGGER_EDGE
:
3676 case INTR_TRIGGER_LEVEL
:
3679 case INTR_TRIGGER_CONFORM
:
3686 device_getenv_int(device_t dev
, const char *knob
, int def
)
3690 /* Deprecated; for compat */
3691 ksnprintf(env
, sizeof(env
), "hw.%s.%s", device_get_nameunit(dev
), knob
);
3692 kgetenv_int(env
, &def
);
3694 /* Prefer dev.driver.unit.knob */
3695 ksnprintf(env
, sizeof(env
), "dev.%s.%d.%s",
3696 device_get_name(dev
), device_get_unit(dev
), knob
);
3697 kgetenv_int(env
, &def
);
3703 device_getenv_string(device_t dev
, const char *knob
, char * __restrict data
,
3704 int dlen
, const char * __restrict def
)
3708 strlcpy(data
, def
, dlen
);
3710 /* Deprecated; for compat */
3711 ksnprintf(env
, sizeof(env
), "hw.%s.%s", device_get_nameunit(dev
), knob
);
3712 kgetenv_string(env
, data
, dlen
);
3714 /* Prefer dev.driver.unit.knob */
3715 ksnprintf(env
, sizeof(env
), "dev.%s.%d.%s",
3716 device_get_name(dev
), device_get_unit(dev
), knob
);
3717 kgetenv_string(env
, data
, dlen
);