kernel - Incidental MPLOCK removal (non-performance)
[dragonfly.git] / sys / kern / subr_bus.c
blob46c17b9bc366e45f5d3db9a533bfc92b78e63f3f
1 /*
2 * Copyright (c) 1997,1998 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
29 #include "opt_bus.h"
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>
36 #include <sys/kobj.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/device.h>
43 #include <sys/lock.h>
44 #include <sys/conf.h>
45 #include <sys/uio.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>
56 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
57 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
59 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
61 #ifdef BUS_DEBUG
62 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
63 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
64 #define DRIVERNAME(d) ((d)? d->name : "no driver")
65 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
67 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
68 * prevent syslog from deleting initial spaces
70 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
72 static void print_device_short(device_t dev, int indent);
73 static void print_device(device_t dev, int indent);
74 void print_device_tree_short(device_t dev, int indent);
75 void print_device_tree(device_t dev, int indent);
76 static void print_driver_short(driver_t *driver, int indent);
77 static void print_driver(driver_t *driver, int indent);
78 static void print_driver_list(driver_list_t drivers, int indent);
79 static void print_devclass_short(devclass_t dc, int indent);
80 static void print_devclass(devclass_t dc, int indent);
81 void print_devclass_list_short(void);
82 void print_devclass_list(void);
84 #else
85 /* Make the compiler ignore the function calls */
86 #define PDEBUG(a) /* nop */
87 #define DEVICENAME(d) /* nop */
88 #define DRIVERNAME(d) /* nop */
89 #define DEVCLANAME(d) /* nop */
91 #define print_device_short(d,i) /* nop */
92 #define print_device(d,i) /* nop */
93 #define print_device_tree_short(d,i) /* nop */
94 #define print_device_tree(d,i) /* nop */
95 #define print_driver_short(d,i) /* nop */
96 #define print_driver(d,i) /* nop */
97 #define print_driver_list(d,i) /* nop */
98 #define print_devclass_short(d,i) /* nop */
99 #define print_devclass(d,i) /* nop */
100 #define print_devclass_list_short() /* nop */
101 #define print_devclass_list() /* nop */
102 #endif
105 * dev sysctl tree
108 enum {
109 DEVCLASS_SYSCTL_PARENT,
112 static int
113 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
115 devclass_t dc = (devclass_t)arg1;
116 const char *value;
118 switch (arg2) {
119 case DEVCLASS_SYSCTL_PARENT:
120 value = dc->parent ? dc->parent->name : "";
121 break;
122 default:
123 return (EINVAL);
125 return (SYSCTL_OUT(req, value, strlen(value)));
128 static void
129 devclass_sysctl_init(devclass_t dc)
132 if (dc->sysctl_tree != NULL)
133 return;
134 sysctl_ctx_init(&dc->sysctl_ctx);
135 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
136 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
137 CTLFLAG_RD, NULL, "");
138 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
139 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
140 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
141 "parent class");
144 enum {
145 DEVICE_SYSCTL_DESC,
146 DEVICE_SYSCTL_DRIVER,
147 DEVICE_SYSCTL_LOCATION,
148 DEVICE_SYSCTL_PNPINFO,
149 DEVICE_SYSCTL_PARENT,
152 static int
153 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
155 device_t dev = (device_t)arg1;
156 const char *value;
157 char *buf;
158 int error;
160 buf = NULL;
161 switch (arg2) {
162 case DEVICE_SYSCTL_DESC:
163 value = dev->desc ? dev->desc : "";
164 break;
165 case DEVICE_SYSCTL_DRIVER:
166 value = dev->driver ? dev->driver->name : "";
167 break;
168 case DEVICE_SYSCTL_LOCATION:
169 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
170 bus_child_location_str(dev, buf, 1024);
171 break;
172 case DEVICE_SYSCTL_PNPINFO:
173 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
174 bus_child_pnpinfo_str(dev, buf, 1024);
175 break;
176 case DEVICE_SYSCTL_PARENT:
177 value = dev->parent ? dev->parent->nameunit : "";
178 break;
179 default:
180 return (EINVAL);
182 error = SYSCTL_OUT(req, value, strlen(value));
183 if (buf != NULL)
184 kfree(buf, M_BUS);
185 return (error);
188 static void
189 device_sysctl_init(device_t dev)
191 devclass_t dc = dev->devclass;
193 if (dev->sysctl_tree != NULL)
194 return;
195 devclass_sysctl_init(dc);
196 sysctl_ctx_init(&dev->sysctl_ctx);
197 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
198 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
199 dev->nameunit + strlen(dc->name),
200 CTLFLAG_RD, NULL, "");
201 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
202 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
203 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
204 "device description");
205 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
206 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
207 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
208 "device driver name");
209 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
210 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
211 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
212 "device location relative to parent");
213 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
214 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
215 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
216 "device identification");
217 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
218 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
219 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
220 "parent device");
223 static void
224 device_sysctl_update(device_t dev)
226 devclass_t dc = dev->devclass;
228 if (dev->sysctl_tree == NULL)
229 return;
230 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
233 static void
234 device_sysctl_fini(device_t dev)
236 if (dev->sysctl_tree == NULL)
237 return;
238 sysctl_ctx_free(&dev->sysctl_ctx);
239 dev->sysctl_tree = NULL;
242 static void device_attach_async(device_t dev);
243 static void device_attach_thread(void *arg);
244 static int device_doattach(device_t dev);
246 static int do_async_attach = 0;
247 static int numasyncthreads;
248 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
251 * /dev/devctl implementation
255 * This design allows only one reader for /dev/devctl. This is not desirable
256 * in the long run, but will get a lot of hair out of this implementation.
257 * Maybe we should make this device a clonable device.
259 * Also note: we specifically do not attach a device to the device_t tree
260 * to avoid potential chicken and egg problems. One could argue that all
261 * of this belongs to the root node. One could also further argue that the
262 * sysctl interface that we have not might more properly be an ioctl
263 * interface, but at this stage of the game, I'm not inclined to rock that
264 * boat.
266 * I'm also not sure that the SIGIO support is done correctly or not, as
267 * I copied it from a driver that had SIGIO support that likely hasn't been
268 * tested since 3.4 or 2.2.8!
271 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
272 static int devctl_disable = 0;
273 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
274 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
275 sysctl_devctl_disable, "I", "devctl disable");
277 static d_open_t devopen;
278 static d_close_t devclose;
279 static d_read_t devread;
280 static d_ioctl_t devioctl;
281 static d_kqfilter_t devkqfilter;
283 static struct dev_ops devctl_ops = {
284 { "devctl", 0, D_MPSAFE },
285 .d_open = devopen,
286 .d_close = devclose,
287 .d_read = devread,
288 .d_ioctl = devioctl,
289 .d_kqfilter = devkqfilter
292 struct dev_event_info
294 char *dei_data;
295 TAILQ_ENTRY(dev_event_info) dei_link;
298 TAILQ_HEAD(devq, dev_event_info);
300 static struct dev_softc
302 int inuse;
303 struct lock lock;
304 struct kqinfo kq;
305 struct devq devq;
306 struct proc *async_proc;
307 } devsoftc;
310 * Chicken-and-egg problem with devfs, get the queue operational early.
312 static void
313 predevinit(void)
315 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
316 TAILQ_INIT(&devsoftc.devq);
318 SYSINIT(predevinit, SI_SUB_CREATE_INIT, SI_ORDER_ANY, predevinit, 0);
320 static void
321 devinit(void)
324 * WARNING! make_dev() can call back into devctl_queue_data()
325 * immediately.
327 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
330 static int
331 devopen(struct dev_open_args *ap)
333 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
334 if (devsoftc.inuse) {
335 lockmgr(&devsoftc.lock, LK_RELEASE);
336 return (EBUSY);
338 /* move to init */
339 devsoftc.inuse = 1;
340 devsoftc.async_proc = NULL;
341 lockmgr(&devsoftc.lock, LK_RELEASE);
343 return (0);
346 static int
347 devclose(struct dev_close_args *ap)
349 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
350 devsoftc.inuse = 0;
351 wakeup(&devsoftc);
352 lockmgr(&devsoftc.lock, LK_RELEASE);
354 return (0);
358 * The read channel for this device is used to report changes to
359 * userland in realtime. We are required to free the data as well as
360 * the n1 object because we allocate them separately. Also note that
361 * we return one record at a time. If you try to read this device a
362 * character at a time, you will lose the rest of the data. Listening
363 * programs are expected to cope.
365 static int
366 devread(struct dev_read_args *ap)
368 struct uio *uio = ap->a_uio;
369 struct dev_event_info *n1;
370 int rv;
372 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
373 while (TAILQ_EMPTY(&devsoftc.devq)) {
374 if (ap->a_ioflag & IO_NDELAY) {
375 lockmgr(&devsoftc.lock, LK_RELEASE);
376 return (EAGAIN);
378 tsleep_interlock(&devsoftc, PCATCH);
379 lockmgr(&devsoftc.lock, LK_RELEASE);
380 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
381 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
382 if (rv) {
384 * Need to translate ERESTART to EINTR here? -- jake
386 lockmgr(&devsoftc.lock, LK_RELEASE);
387 return (rv);
390 n1 = TAILQ_FIRST(&devsoftc.devq);
391 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
392 lockmgr(&devsoftc.lock, LK_RELEASE);
393 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
394 kfree(n1->dei_data, M_BUS);
395 kfree(n1, M_BUS);
396 return (rv);
399 static int
400 devioctl(struct dev_ioctl_args *ap)
402 switch (ap->a_cmd) {
404 case FIONBIO:
405 return (0);
406 case FIOASYNC:
407 if (*(int*)ap->a_data)
408 devsoftc.async_proc = curproc;
409 else
410 devsoftc.async_proc = NULL;
411 return (0);
413 /* (un)Support for other fcntl() calls. */
414 case FIOCLEX:
415 case FIONCLEX:
416 case FIONREAD:
417 case FIOSETOWN:
418 case FIOGETOWN:
419 default:
420 break;
422 return (ENOTTY);
425 static void dev_filter_detach(struct knote *);
426 static int dev_filter_read(struct knote *, long);
428 static struct filterops dev_filtops =
429 { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
430 dev_filter_detach, dev_filter_read };
432 static int
433 devkqfilter(struct dev_kqfilter_args *ap)
435 struct knote *kn = ap->a_kn;
436 struct klist *klist;
438 ap->a_result = 0;
439 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
441 switch (kn->kn_filter) {
442 case EVFILT_READ:
443 kn->kn_fop = &dev_filtops;
444 break;
445 default:
446 ap->a_result = EOPNOTSUPP;
447 lockmgr(&devsoftc.lock, LK_RELEASE);
448 return (0);
451 klist = &devsoftc.kq.ki_note;
452 knote_insert(klist, kn);
454 lockmgr(&devsoftc.lock, LK_RELEASE);
456 return (0);
459 static void
460 dev_filter_detach(struct knote *kn)
462 struct klist *klist;
464 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
465 klist = &devsoftc.kq.ki_note;
466 knote_remove(klist, kn);
467 lockmgr(&devsoftc.lock, LK_RELEASE);
470 static int
471 dev_filter_read(struct knote *kn, long hint)
473 int ready = 0;
475 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
476 if (!TAILQ_EMPTY(&devsoftc.devq))
477 ready = 1;
478 lockmgr(&devsoftc.lock, LK_RELEASE);
480 return (ready);
485 * @brief Return whether the userland process is running
487 boolean_t
488 devctl_process_running(void)
490 return (devsoftc.inuse == 1);
494 * @brief Queue data to be read from the devctl device
496 * Generic interface to queue data to the devctl device. It is
497 * assumed that @p data is properly formatted. It is further assumed
498 * that @p data is allocated using the M_BUS malloc type.
500 void
501 devctl_queue_data(char *data)
503 struct dev_event_info *n1 = NULL;
504 struct proc *p;
506 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
507 if (n1 == NULL)
508 return;
509 n1->dei_data = data;
510 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
511 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
512 wakeup(&devsoftc);
513 lockmgr(&devsoftc.lock, LK_RELEASE);
514 KNOTE(&devsoftc.kq.ki_note, 0);
515 p = devsoftc.async_proc;
516 if (p != NULL)
517 ksignal(p, SIGIO);
521 * @brief Send a 'notification' to userland, using standard ways
523 void
524 devctl_notify(const char *system, const char *subsystem, const char *type,
525 const char *data)
527 int len = 0;
528 char *msg;
530 if (system == NULL)
531 return; /* BOGUS! Must specify system. */
532 if (subsystem == NULL)
533 return; /* BOGUS! Must specify subsystem. */
534 if (type == NULL)
535 return; /* BOGUS! Must specify type. */
536 len += strlen(" system=") + strlen(system);
537 len += strlen(" subsystem=") + strlen(subsystem);
538 len += strlen(" type=") + strlen(type);
539 /* add in the data message plus newline. */
540 if (data != NULL)
541 len += strlen(data);
542 len += 3; /* '!', '\n', and NUL */
543 msg = kmalloc(len, M_BUS, M_NOWAIT);
544 if (msg == NULL)
545 return; /* Drop it on the floor */
546 if (data != NULL)
547 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
548 system, subsystem, type, data);
549 else
550 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
551 system, subsystem, type);
552 devctl_queue_data(msg);
556 * Common routine that tries to make sending messages as easy as possible.
557 * We allocate memory for the data, copy strings into that, but do not
558 * free it unless there's an error. The dequeue part of the driver should
559 * free the data. We don't send data when the device is disabled. We do
560 * send data, even when we have no listeners, because we wish to avoid
561 * races relating to startup and restart of listening applications.
563 * devaddq is designed to string together the type of event, with the
564 * object of that event, plus the plug and play info and location info
565 * for that event. This is likely most useful for devices, but less
566 * useful for other consumers of this interface. Those should use
567 * the devctl_queue_data() interface instead.
569 static void
570 devaddq(const char *type, const char *what, device_t dev)
572 char *data = NULL;
573 char *loc = NULL;
574 char *pnp = NULL;
575 const char *parstr;
577 if (devctl_disable)
578 return;
579 data = kmalloc(1024, M_BUS, M_NOWAIT);
580 if (data == NULL)
581 goto bad;
583 /* get the bus specific location of this device */
584 loc = kmalloc(1024, M_BUS, M_NOWAIT);
585 if (loc == NULL)
586 goto bad;
587 *loc = '\0';
588 bus_child_location_str(dev, loc, 1024);
590 /* Get the bus specific pnp info of this device */
591 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
592 if (pnp == NULL)
593 goto bad;
594 *pnp = '\0';
595 bus_child_pnpinfo_str(dev, pnp, 1024);
597 /* Get the parent of this device, or / if high enough in the tree. */
598 if (device_get_parent(dev) == NULL)
599 parstr = "."; /* Or '/' ? */
600 else
601 parstr = device_get_nameunit(device_get_parent(dev));
602 /* String it all together. */
603 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
604 parstr);
605 kfree(loc, M_BUS);
606 kfree(pnp, M_BUS);
607 devctl_queue_data(data);
608 return;
609 bad:
610 kfree(pnp, M_BUS);
611 kfree(loc, M_BUS);
612 kfree(data, M_BUS);
613 return;
617 * A device was added to the tree. We are called just after it successfully
618 * attaches (that is, probe and attach success for this device). No call
619 * is made if a device is merely parented into the tree. See devnomatch
620 * if probe fails. If attach fails, no notification is sent (but maybe
621 * we should have a different message for this).
623 static void
624 devadded(device_t dev)
626 char *pnp = NULL;
627 char *tmp = NULL;
629 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
630 if (pnp == NULL)
631 goto fail;
632 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
633 if (tmp == NULL)
634 goto fail;
635 *pnp = '\0';
636 bus_child_pnpinfo_str(dev, pnp, 1024);
637 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
638 devaddq("+", tmp, dev);
639 fail:
640 if (pnp != NULL)
641 kfree(pnp, M_BUS);
642 if (tmp != NULL)
643 kfree(tmp, M_BUS);
644 return;
648 * A device was removed from the tree. We are called just before this
649 * happens.
651 static void
652 devremoved(device_t dev)
654 char *pnp = NULL;
655 char *tmp = NULL;
657 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
658 if (pnp == NULL)
659 goto fail;
660 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
661 if (tmp == NULL)
662 goto fail;
663 *pnp = '\0';
664 bus_child_pnpinfo_str(dev, pnp, 1024);
665 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
666 devaddq("-", tmp, dev);
667 fail:
668 if (pnp != NULL)
669 kfree(pnp, M_BUS);
670 if (tmp != NULL)
671 kfree(tmp, M_BUS);
672 return;
676 * Called when there's no match for this device. This is only called
677 * the first time that no match happens, so we don't keep getitng this
678 * message. Should that prove to be undesirable, we can change it.
679 * This is called when all drivers that can attach to a given bus
680 * decline to accept this device. Other errrors may not be detected.
682 static void
683 devnomatch(device_t dev)
685 devaddq("?", "", dev);
688 static int
689 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
691 struct dev_event_info *n1;
692 int dis, error;
694 dis = devctl_disable;
695 error = sysctl_handle_int(oidp, &dis, 0, req);
696 if (error || !req->newptr)
697 return (error);
698 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
699 devctl_disable = dis;
700 if (dis) {
701 while (!TAILQ_EMPTY(&devsoftc.devq)) {
702 n1 = TAILQ_FIRST(&devsoftc.devq);
703 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
704 kfree(n1->dei_data, M_BUS);
705 kfree(n1, M_BUS);
708 lockmgr(&devsoftc.lock, LK_RELEASE);
709 return (0);
712 /* End of /dev/devctl code */
714 TAILQ_HEAD(,bsd_device) bus_data_devices;
715 static int bus_data_generation = 1;
717 kobj_method_t null_methods[] = {
718 { 0, 0 }
721 DEFINE_CLASS(null, null_methods, 0);
724 * Devclass implementation
727 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
729 static devclass_t
730 devclass_find_internal(const char *classname, const char *parentname,
731 int create)
733 devclass_t dc;
735 PDEBUG(("looking for %s", classname));
736 if (classname == NULL)
737 return(NULL);
739 TAILQ_FOREACH(dc, &devclasses, link)
740 if (!strcmp(dc->name, classname))
741 break;
743 if (create && !dc) {
744 PDEBUG(("creating %s", classname));
745 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
746 M_BUS, M_INTWAIT | M_ZERO);
747 dc->parent = NULL;
748 dc->name = (char*) (dc + 1);
749 strcpy(dc->name, classname);
750 dc->devices = NULL;
751 dc->maxunit = 0;
752 TAILQ_INIT(&dc->drivers);
753 TAILQ_INSERT_TAIL(&devclasses, dc, link);
755 bus_data_generation_update();
760 * If a parent class is specified, then set that as our parent so
761 * that this devclass will support drivers for the parent class as
762 * well. If the parent class has the same name don't do this though
763 * as it creates a cycle that can trigger an infinite loop in
764 * device_probe_child() if a device exists for which there is no
765 * suitable driver.
767 if (parentname && dc && !dc->parent &&
768 strcmp(classname, parentname) != 0)
769 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
771 return(dc);
774 devclass_t
775 devclass_create(const char *classname)
777 return(devclass_find_internal(classname, NULL, TRUE));
780 devclass_t
781 devclass_find(const char *classname)
783 return(devclass_find_internal(classname, NULL, FALSE));
786 device_t
787 devclass_find_unit(const char *classname, int unit)
789 devclass_t dc;
791 if ((dc = devclass_find(classname)) != NULL)
792 return(devclass_get_device(dc, unit));
793 return (NULL);
797 devclass_add_driver(devclass_t dc, driver_t *driver)
799 driverlink_t dl;
800 device_t dev;
801 int i;
803 PDEBUG(("%s", DRIVERNAME(driver)));
805 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
808 * Compile the driver's methods. Also increase the reference count
809 * so that the class doesn't get freed when the last instance
810 * goes. This means we can safely use static methods and avoids a
811 * double-free in devclass_delete_driver.
813 kobj_class_instantiate(driver);
816 * Make sure the devclass which the driver is implementing exists.
818 devclass_find_internal(driver->name, NULL, TRUE);
820 dl->driver = driver;
821 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
824 * Call BUS_DRIVER_ADDED for any existing busses in this class,
825 * but only if the bus has already been attached (otherwise we
826 * might probe too early).
828 * This is what will cause a newly loaded module to be associated
829 * with hardware. bus_generic_driver_added() is typically what ends
830 * up being called.
832 for (i = 0; i < dc->maxunit; i++) {
833 if ((dev = dc->devices[i]) != NULL) {
834 if (dev->state >= DS_ATTACHED)
835 BUS_DRIVER_ADDED(dev, driver);
839 bus_data_generation_update();
840 return(0);
844 devclass_delete_driver(devclass_t busclass, driver_t *driver)
846 devclass_t dc = devclass_find(driver->name);
847 driverlink_t dl;
848 device_t dev;
849 int i;
850 int error;
852 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
854 if (!dc)
855 return(0);
858 * Find the link structure in the bus' list of drivers.
860 TAILQ_FOREACH(dl, &busclass->drivers, link)
861 if (dl->driver == driver)
862 break;
864 if (!dl) {
865 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
866 return(ENOENT);
870 * Disassociate from any devices. We iterate through all the
871 * devices in the devclass of the driver and detach any which are
872 * using the driver and which have a parent in the devclass which
873 * we are deleting from.
875 * Note that since a driver can be in multiple devclasses, we
876 * should not detach devices which are not children of devices in
877 * the affected devclass.
879 for (i = 0; i < dc->maxunit; i++)
880 if (dc->devices[i]) {
881 dev = dc->devices[i];
882 if (dev->driver == driver && dev->parent &&
883 dev->parent->devclass == busclass) {
884 if ((error = device_detach(dev)) != 0)
885 return(error);
886 device_set_driver(dev, NULL);
890 TAILQ_REMOVE(&busclass->drivers, dl, link);
891 kfree(dl, M_BUS);
893 kobj_class_uninstantiate(driver);
895 bus_data_generation_update();
896 return(0);
899 static driverlink_t
900 devclass_find_driver_internal(devclass_t dc, const char *classname)
902 driverlink_t dl;
904 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
906 TAILQ_FOREACH(dl, &dc->drivers, link)
907 if (!strcmp(dl->driver->name, classname))
908 return(dl);
910 PDEBUG(("not found"));
911 return(NULL);
914 kobj_class_t
915 devclass_find_driver(devclass_t dc, const char *classname)
917 driverlink_t dl;
919 dl = devclass_find_driver_internal(dc, classname);
920 if (dl)
921 return(dl->driver);
922 else
923 return(NULL);
926 const char *
927 devclass_get_name(devclass_t dc)
929 return(dc->name);
932 device_t
933 devclass_get_device(devclass_t dc, int unit)
935 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
936 return(NULL);
937 return(dc->devices[unit]);
940 void *
941 devclass_get_softc(devclass_t dc, int unit)
943 device_t dev;
945 dev = devclass_get_device(dc, unit);
946 if (!dev)
947 return(NULL);
949 return(device_get_softc(dev));
953 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
955 int i;
956 int count;
957 device_t *list;
959 count = 0;
960 for (i = 0; i < dc->maxunit; i++)
961 if (dc->devices[i])
962 count++;
964 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
966 count = 0;
967 for (i = 0; i < dc->maxunit; i++)
968 if (dc->devices[i]) {
969 list[count] = dc->devices[i];
970 count++;
973 *devlistp = list;
974 *devcountp = count;
976 return(0);
980 * @brief Get a list of drivers in the devclass
982 * An array containing a list of pointers to all the drivers in the
983 * given devclass is allocated and returned in @p *listp. The number
984 * of drivers in the array is returned in @p *countp. The caller should
985 * free the array using @c free(p, M_TEMP).
987 * @param dc the devclass to examine
988 * @param listp gives location for array pointer return value
989 * @param countp gives location for number of array elements
990 * return value
992 * @retval 0 success
993 * @retval ENOMEM the array allocation failed
996 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
998 driverlink_t dl;
999 driver_t **list;
1000 int count;
1002 count = 0;
1003 TAILQ_FOREACH(dl, &dc->drivers, link)
1004 count++;
1005 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1006 if (list == NULL)
1007 return (ENOMEM);
1009 count = 0;
1010 TAILQ_FOREACH(dl, &dc->drivers, link) {
1011 list[count] = dl->driver;
1012 count++;
1014 *listp = list;
1015 *countp = count;
1017 return (0);
1021 * @brief Get the number of devices in a devclass
1023 * @param dc the devclass to examine
1026 devclass_get_count(devclass_t dc)
1028 int count, i;
1030 count = 0;
1031 for (i = 0; i < dc->maxunit; i++)
1032 if (dc->devices[i])
1033 count++;
1034 return (count);
1038 devclass_get_maxunit(devclass_t dc)
1040 return(dc->maxunit);
1043 void
1044 devclass_set_parent(devclass_t dc, devclass_t pdc)
1046 dc->parent = pdc;
1049 devclass_t
1050 devclass_get_parent(devclass_t dc)
1052 return(dc->parent);
1055 static int
1056 devclass_alloc_unit(devclass_t dc, int *unitp)
1058 int unit = *unitp;
1060 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1062 /* If we have been given a wired unit number, check for existing device */
1063 if (unit != -1) {
1064 if (unit >= 0 && unit < dc->maxunit &&
1065 dc->devices[unit] != NULL) {
1066 if (bootverbose)
1067 kprintf("%s-: %s%d exists, using next available unit number\n",
1068 dc->name, dc->name, unit);
1069 /* find the next available slot */
1070 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
1073 } else {
1074 /* Unwired device, find the next available slot for it */
1075 unit = 0;
1076 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1077 unit++;
1081 * We've selected a unit beyond the length of the table, so let's
1082 * extend the table to make room for all units up to and including
1083 * this one.
1085 if (unit >= dc->maxunit) {
1086 device_t *newlist;
1087 int newsize;
1089 newsize = (unit + 1);
1090 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
1091 M_INTWAIT | M_ZERO);
1092 if (newlist == NULL)
1093 return(ENOMEM);
1094 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1095 if (dc->devices)
1096 kfree(dc->devices, M_BUS);
1097 dc->devices = newlist;
1098 dc->maxunit = newsize;
1100 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1102 *unitp = unit;
1103 return(0);
1106 static int
1107 devclass_add_device(devclass_t dc, device_t dev)
1109 int buflen, error;
1111 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1113 buflen = strlen(dc->name) + 5;
1114 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1115 if (dev->nameunit == NULL)
1116 return(ENOMEM);
1118 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1119 kfree(dev->nameunit, M_BUS);
1120 dev->nameunit = NULL;
1121 return(error);
1123 dc->devices[dev->unit] = dev;
1124 dev->devclass = dc;
1125 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1127 return(0);
1130 static int
1131 devclass_delete_device(devclass_t dc, device_t dev)
1133 if (!dc || !dev)
1134 return(0);
1136 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1138 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1139 panic("devclass_delete_device: inconsistent device class");
1140 dc->devices[dev->unit] = NULL;
1141 if (dev->flags & DF_WILDCARD)
1142 dev->unit = -1;
1143 dev->devclass = NULL;
1144 kfree(dev->nameunit, M_BUS);
1145 dev->nameunit = NULL;
1147 return(0);
1150 static device_t
1151 make_device(device_t parent, const char *name, int unit)
1153 device_t dev;
1154 devclass_t dc;
1156 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1158 if (name != NULL) {
1159 dc = devclass_find_internal(name, NULL, TRUE);
1160 if (!dc) {
1161 kprintf("make_device: can't find device class %s\n", name);
1162 return(NULL);
1164 } else
1165 dc = NULL;
1167 dev = kmalloc(sizeof(struct bsd_device), M_BUS, M_INTWAIT | M_ZERO);
1168 if (!dev)
1169 return(0);
1171 dev->parent = parent;
1172 TAILQ_INIT(&dev->children);
1173 kobj_init((kobj_t) dev, &null_class);
1174 dev->driver = NULL;
1175 dev->devclass = NULL;
1176 dev->unit = unit;
1177 dev->nameunit = NULL;
1178 dev->desc = NULL;
1179 dev->busy = 0;
1180 dev->devflags = 0;
1181 dev->flags = DF_ENABLED;
1182 dev->order = 0;
1183 if (unit == -1)
1184 dev->flags |= DF_WILDCARD;
1185 if (name) {
1186 dev->flags |= DF_FIXEDCLASS;
1187 if (devclass_add_device(dc, dev) != 0) {
1188 kobj_delete((kobj_t)dev, M_BUS);
1189 return(NULL);
1192 dev->ivars = NULL;
1193 dev->softc = NULL;
1195 dev->state = DS_NOTPRESENT;
1197 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1198 bus_data_generation_update();
1200 return(dev);
1203 static int
1204 device_print_child(device_t dev, device_t child)
1206 int retval = 0;
1208 if (device_is_alive(child))
1209 retval += BUS_PRINT_CHILD(dev, child);
1210 else
1211 retval += device_printf(child, " not found\n");
1213 return(retval);
1216 device_t
1217 device_add_child(device_t dev, const char *name, int unit)
1219 return device_add_child_ordered(dev, 0, name, unit);
1222 device_t
1223 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1225 device_t child;
1226 device_t place;
1228 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1229 order, unit));
1231 child = make_device(dev, name, unit);
1232 if (child == NULL)
1233 return child;
1234 child->order = order;
1236 TAILQ_FOREACH(place, &dev->children, link)
1237 if (place->order > order)
1238 break;
1240 if (place) {
1242 * The device 'place' is the first device whose order is
1243 * greater than the new child.
1245 TAILQ_INSERT_BEFORE(place, child, link);
1246 } else {
1248 * The new child's order is greater or equal to the order of
1249 * any existing device. Add the child to the tail of the list.
1251 TAILQ_INSERT_TAIL(&dev->children, child, link);
1254 bus_data_generation_update();
1255 return(child);
1259 device_delete_child(device_t dev, device_t child)
1261 int error;
1262 device_t grandchild;
1264 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1266 /* remove children first */
1267 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1268 error = device_delete_child(child, grandchild);
1269 if (error)
1270 return(error);
1273 if ((error = device_detach(child)) != 0)
1274 return(error);
1275 if (child->devclass)
1276 devclass_delete_device(child->devclass, child);
1277 TAILQ_REMOVE(&dev->children, child, link);
1278 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1279 kobj_delete((kobj_t)child, M_BUS);
1281 bus_data_generation_update();
1282 return(0);
1286 * @brief Delete all children devices of the given device, if any.
1288 * This function deletes all children devices of the given device, if
1289 * any, using the device_delete_child() function for each device it
1290 * finds. If a child device cannot be deleted, this function will
1291 * return an error code.
1293 * @param dev the parent device
1295 * @retval 0 success
1296 * @retval non-zero a device would not detach
1299 device_delete_children(device_t dev)
1301 device_t child;
1302 int error;
1304 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1306 error = 0;
1308 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1309 error = device_delete_child(dev, child);
1310 if (error) {
1311 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1312 break;
1315 return (error);
1319 * @brief Find a device given a unit number
1321 * This is similar to devclass_get_devices() but only searches for
1322 * devices which have @p dev as a parent.
1324 * @param dev the parent device to search
1325 * @param unit the unit number to search for. If the unit is -1,
1326 * return the first child of @p dev which has name
1327 * @p classname (that is, the one with the lowest unit.)
1329 * @returns the device with the given unit number or @c
1330 * NULL if there is no such device
1332 device_t
1333 device_find_child(device_t dev, const char *classname, int unit)
1335 devclass_t dc;
1336 device_t child;
1338 dc = devclass_find(classname);
1339 if (!dc)
1340 return(NULL);
1342 if (unit != -1) {
1343 child = devclass_get_device(dc, unit);
1344 if (child && child->parent == dev)
1345 return (child);
1346 } else {
1347 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1348 child = devclass_get_device(dc, unit);
1349 if (child && child->parent == dev)
1350 return (child);
1353 return(NULL);
1356 static driverlink_t
1357 first_matching_driver(devclass_t dc, device_t dev)
1359 if (dev->devclass)
1360 return(devclass_find_driver_internal(dc, dev->devclass->name));
1361 else
1362 return(TAILQ_FIRST(&dc->drivers));
1365 static driverlink_t
1366 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1368 if (dev->devclass) {
1369 driverlink_t dl;
1370 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1371 if (!strcmp(dev->devclass->name, dl->driver->name))
1372 return(dl);
1373 return(NULL);
1374 } else
1375 return(TAILQ_NEXT(last, link));
1379 device_probe_child(device_t dev, device_t child)
1381 devclass_t dc;
1382 driverlink_t best = NULL;
1383 driverlink_t dl;
1384 int result, pri = 0;
1385 int hasclass = (child->devclass != NULL);
1387 dc = dev->devclass;
1388 if (!dc)
1389 panic("device_probe_child: parent device has no devclass");
1391 if (child->state == DS_ALIVE)
1392 return(0);
1394 for (; dc; dc = dc->parent) {
1395 for (dl = first_matching_driver(dc, child); dl;
1396 dl = next_matching_driver(dc, child, dl)) {
1397 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1398 device_set_driver(child, dl->driver);
1399 if (!hasclass)
1400 device_set_devclass(child, dl->driver->name);
1401 result = DEVICE_PROBE(child);
1402 if (!hasclass)
1403 device_set_devclass(child, 0);
1406 * If the driver returns SUCCESS, there can be
1407 * no higher match for this device.
1409 if (result == 0) {
1410 best = dl;
1411 pri = 0;
1412 break;
1416 * The driver returned an error so it
1417 * certainly doesn't match.
1419 if (result > 0) {
1420 device_set_driver(child, NULL);
1421 continue;
1425 * A priority lower than SUCCESS, remember the
1426 * best matching driver. Initialise the value
1427 * of pri for the first match.
1429 if (best == NULL || result > pri) {
1430 best = dl;
1431 pri = result;
1432 continue;
1436 * If we have unambiguous match in this devclass,
1437 * don't look in the parent.
1439 if (best && pri == 0)
1440 break;
1444 * If we found a driver, change state and initialise the devclass.
1446 if (best) {
1447 if (!child->devclass)
1448 device_set_devclass(child, best->driver->name);
1449 device_set_driver(child, best->driver);
1450 if (pri < 0) {
1452 * A bit bogus. Call the probe method again to make
1453 * sure that we have the right description.
1455 DEVICE_PROBE(child);
1458 bus_data_generation_update();
1459 child->state = DS_ALIVE;
1460 return(0);
1463 return(ENXIO);
1466 device_t
1467 device_get_parent(device_t dev)
1469 return dev->parent;
1473 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1475 int count;
1476 device_t child;
1477 device_t *list;
1479 count = 0;
1480 TAILQ_FOREACH(child, &dev->children, link)
1481 count++;
1483 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1485 count = 0;
1486 TAILQ_FOREACH(child, &dev->children, link) {
1487 list[count] = child;
1488 count++;
1491 *devlistp = list;
1492 *devcountp = count;
1494 return(0);
1497 driver_t *
1498 device_get_driver(device_t dev)
1500 return(dev->driver);
1503 devclass_t
1504 device_get_devclass(device_t dev)
1506 return(dev->devclass);
1509 const char *
1510 device_get_name(device_t dev)
1512 if (dev->devclass)
1513 return devclass_get_name(dev->devclass);
1514 return(NULL);
1517 const char *
1518 device_get_nameunit(device_t dev)
1520 return(dev->nameunit);
1524 device_get_unit(device_t dev)
1526 return(dev->unit);
1529 const char *
1530 device_get_desc(device_t dev)
1532 return(dev->desc);
1535 uint32_t
1536 device_get_flags(device_t dev)
1538 return(dev->devflags);
1541 struct sysctl_ctx_list *
1542 device_get_sysctl_ctx(device_t dev)
1544 return (&dev->sysctl_ctx);
1547 struct sysctl_oid *
1548 device_get_sysctl_tree(device_t dev)
1550 return (dev->sysctl_tree);
1554 device_print_prettyname(device_t dev)
1556 const char *name = device_get_name(dev);
1558 if (name == NULL)
1559 return kprintf("unknown: ");
1560 else
1561 return kprintf("%s%d: ", name, device_get_unit(dev));
1565 device_printf(device_t dev, const char * fmt, ...)
1567 __va_list ap;
1568 int retval;
1570 retval = device_print_prettyname(dev);
1571 __va_start(ap, fmt);
1572 retval += kvprintf(fmt, ap);
1573 __va_end(ap);
1574 return retval;
1577 static void
1578 device_set_desc_internal(device_t dev, const char* desc, int copy)
1580 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1581 kfree(dev->desc, M_BUS);
1582 dev->flags &= ~DF_DESCMALLOCED;
1583 dev->desc = NULL;
1586 if (copy && desc) {
1587 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1588 if (dev->desc) {
1589 strcpy(dev->desc, desc);
1590 dev->flags |= DF_DESCMALLOCED;
1592 } else {
1593 /* Avoid a -Wcast-qual warning */
1594 dev->desc = (char *)(uintptr_t) desc;
1597 bus_data_generation_update();
1600 void
1601 device_set_desc(device_t dev, const char* desc)
1603 device_set_desc_internal(dev, desc, FALSE);
1606 void
1607 device_set_desc_copy(device_t dev, const char* desc)
1609 device_set_desc_internal(dev, desc, TRUE);
1612 void
1613 device_set_flags(device_t dev, uint32_t flags)
1615 dev->devflags = flags;
1618 void *
1619 device_get_softc(device_t dev)
1621 return dev->softc;
1624 void
1625 device_set_softc(device_t dev, void *softc)
1627 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1628 kfree(dev->softc, M_BUS);
1629 dev->softc = softc;
1630 if (dev->softc)
1631 dev->flags |= DF_EXTERNALSOFTC;
1632 else
1633 dev->flags &= ~DF_EXTERNALSOFTC;
1636 void
1637 device_set_async_attach(device_t dev, int enable)
1639 if (enable)
1640 dev->flags |= DF_ASYNCPROBE;
1641 else
1642 dev->flags &= ~DF_ASYNCPROBE;
1645 void *
1646 device_get_ivars(device_t dev)
1648 return dev->ivars;
1651 void
1652 device_set_ivars(device_t dev, void * ivars)
1654 if (!dev)
1655 return;
1657 dev->ivars = ivars;
1660 device_state_t
1661 device_get_state(device_t dev)
1663 return(dev->state);
1666 void
1667 device_enable(device_t dev)
1669 dev->flags |= DF_ENABLED;
1672 void
1673 device_disable(device_t dev)
1675 dev->flags &= ~DF_ENABLED;
1679 * YYY cannot block
1681 void
1682 device_busy(device_t dev)
1684 if (dev->state < DS_ATTACHED)
1685 panic("device_busy: called for unattached device");
1686 if (dev->busy == 0 && dev->parent)
1687 device_busy(dev->parent);
1688 dev->busy++;
1689 dev->state = DS_BUSY;
1693 * YYY cannot block
1695 void
1696 device_unbusy(device_t dev)
1698 if (dev->state != DS_BUSY)
1699 panic("device_unbusy: called for non-busy device");
1700 dev->busy--;
1701 if (dev->busy == 0) {
1702 if (dev->parent)
1703 device_unbusy(dev->parent);
1704 dev->state = DS_ATTACHED;
1708 void
1709 device_quiet(device_t dev)
1711 dev->flags |= DF_QUIET;
1714 void
1715 device_verbose(device_t dev)
1717 dev->flags &= ~DF_QUIET;
1721 device_is_quiet(device_t dev)
1723 return((dev->flags & DF_QUIET) != 0);
1727 device_is_enabled(device_t dev)
1729 return((dev->flags & DF_ENABLED) != 0);
1733 device_is_alive(device_t dev)
1735 return(dev->state >= DS_ALIVE);
1739 device_is_attached(device_t dev)
1741 return(dev->state >= DS_ATTACHED);
1745 device_set_devclass(device_t dev, const char *classname)
1747 devclass_t dc;
1748 int error;
1750 if (!classname) {
1751 if (dev->devclass)
1752 devclass_delete_device(dev->devclass, dev);
1753 return(0);
1756 if (dev->devclass) {
1757 kprintf("device_set_devclass: device class already set\n");
1758 return(EINVAL);
1761 dc = devclass_find_internal(classname, NULL, TRUE);
1762 if (!dc)
1763 return(ENOMEM);
1765 error = devclass_add_device(dc, dev);
1767 bus_data_generation_update();
1768 return(error);
1772 device_set_driver(device_t dev, driver_t *driver)
1774 if (dev->state >= DS_ATTACHED)
1775 return(EBUSY);
1777 if (dev->driver == driver)
1778 return(0);
1780 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1781 kfree(dev->softc, M_BUS);
1782 dev->softc = NULL;
1784 device_set_desc(dev, NULL);
1785 kobj_delete((kobj_t) dev, 0);
1786 dev->driver = driver;
1787 if (driver) {
1788 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1789 if (!(dev->flags & DF_EXTERNALSOFTC))
1790 dev->softc = kmalloc(driver->size, M_BUS,
1791 M_INTWAIT | M_ZERO);
1792 } else {
1793 kobj_init((kobj_t) dev, &null_class);
1796 bus_data_generation_update();
1797 return(0);
1801 device_probe_and_attach(device_t dev)
1803 device_t bus = dev->parent;
1804 int error = 0;
1806 if (dev->state >= DS_ALIVE)
1807 return(0);
1809 if ((dev->flags & DF_ENABLED) == 0) {
1810 if (bootverbose) {
1811 device_print_prettyname(dev);
1812 kprintf("not probed (disabled)\n");
1814 return(0);
1817 error = device_probe_child(bus, dev);
1818 if (error) {
1819 if (!(dev->flags & DF_DONENOMATCH)) {
1820 BUS_PROBE_NOMATCH(bus, dev);
1821 devnomatch(dev);
1822 dev->flags |= DF_DONENOMATCH;
1824 return(error);
1828 * Output the exact device chain prior to the attach in case the
1829 * system locks up during attach, and generate the full info after
1830 * the attach so correct irq and other information is displayed.
1832 if (bootverbose && !device_is_quiet(dev)) {
1833 device_t tmp;
1835 kprintf("%s", device_get_nameunit(dev));
1836 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1837 kprintf(".%s", device_get_nameunit(tmp));
1838 kprintf("\n");
1840 if (!device_is_quiet(dev))
1841 device_print_child(bus, dev);
1842 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1843 kprintf("%s: probing asynchronously\n",
1844 device_get_nameunit(dev));
1845 dev->state = DS_INPROGRESS;
1846 device_attach_async(dev);
1847 error = 0;
1848 } else {
1849 error = device_doattach(dev);
1851 return(error);
1855 * Device is known to be alive, do the attach asynchronously.
1856 * However, serialize the attaches with the mp lock.
1858 static void
1859 device_attach_async(device_t dev)
1861 thread_t td;
1863 atomic_add_int(&numasyncthreads, 1);
1864 lwkt_create(device_attach_thread, dev, &td, NULL,
1865 0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
1868 static void
1869 device_attach_thread(void *arg)
1871 device_t dev = arg;
1873 (void)device_doattach(dev);
1874 atomic_subtract_int(&numasyncthreads, 1);
1875 wakeup(&numasyncthreads);
1879 * Device is known to be alive, do the attach (synchronous or asynchronous)
1881 static int
1882 device_doattach(device_t dev)
1884 device_t bus = dev->parent;
1885 int hasclass = (dev->devclass != NULL);
1886 int error;
1888 device_sysctl_init(dev);
1889 error = DEVICE_ATTACH(dev);
1890 if (error == 0) {
1891 dev->state = DS_ATTACHED;
1892 if (bootverbose && !device_is_quiet(dev))
1893 device_print_child(bus, dev);
1894 device_sysctl_update(dev);
1895 devadded(dev);
1896 } else {
1897 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1898 dev->driver->name, dev->unit, error);
1899 /* Unset the class that was set in device_probe_child */
1900 if (!hasclass)
1901 device_set_devclass(dev, 0);
1902 device_set_driver(dev, NULL);
1903 dev->state = DS_NOTPRESENT;
1904 device_sysctl_fini(dev);
1906 return(error);
1910 device_detach(device_t dev)
1912 int error;
1914 PDEBUG(("%s", DEVICENAME(dev)));
1915 if (dev->state == DS_BUSY)
1916 return(EBUSY);
1917 if (dev->state != DS_ATTACHED)
1918 return(0);
1920 if ((error = DEVICE_DETACH(dev)) != 0)
1921 return(error);
1922 devremoved(dev);
1923 device_printf(dev, "detached\n");
1924 if (dev->parent)
1925 BUS_CHILD_DETACHED(dev->parent, dev);
1927 if (!(dev->flags & DF_FIXEDCLASS))
1928 devclass_delete_device(dev->devclass, dev);
1930 dev->state = DS_NOTPRESENT;
1931 device_set_driver(dev, NULL);
1932 device_sysctl_fini(dev);
1934 return(0);
1938 device_shutdown(device_t dev)
1940 if (dev->state < DS_ATTACHED)
1941 return 0;
1942 PDEBUG(("%s", DEVICENAME(dev)));
1943 return DEVICE_SHUTDOWN(dev);
1947 device_set_unit(device_t dev, int unit)
1949 devclass_t dc;
1950 int err;
1952 dc = device_get_devclass(dev);
1953 if (unit < dc->maxunit && dc->devices[unit])
1954 return(EBUSY);
1955 err = devclass_delete_device(dc, dev);
1956 if (err)
1957 return(err);
1958 dev->unit = unit;
1959 err = devclass_add_device(dc, dev);
1960 if (err)
1961 return(err);
1963 bus_data_generation_update();
1964 return(0);
1967 /*======================================*/
1969 * Access functions for device resources.
1972 /* Supplied by config(8) in ioconf.c */
1973 extern struct config_device config_devtab[];
1974 extern int devtab_count;
1976 /* Runtime version */
1977 struct config_device *devtab = config_devtab;
1979 static int
1980 resource_new_name(const char *name, int unit)
1982 struct config_device *new;
1984 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1985 M_INTWAIT | M_ZERO);
1986 if (devtab && devtab_count > 0)
1987 bcopy(devtab, new, devtab_count * sizeof(*new));
1988 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1989 if (new[devtab_count].name == NULL) {
1990 kfree(new, M_TEMP);
1991 return(-1);
1993 strcpy(new[devtab_count].name, name);
1994 new[devtab_count].unit = unit;
1995 new[devtab_count].resource_count = 0;
1996 new[devtab_count].resources = NULL;
1997 if (devtab && devtab != config_devtab)
1998 kfree(devtab, M_TEMP);
1999 devtab = new;
2000 return devtab_count++;
2003 static int
2004 resource_new_resname(int j, const char *resname, resource_type type)
2006 struct config_resource *new;
2007 int i;
2009 i = devtab[j].resource_count;
2010 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
2011 if (devtab[j].resources && i > 0)
2012 bcopy(devtab[j].resources, new, i * sizeof(*new));
2013 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
2014 if (new[i].name == NULL) {
2015 kfree(new, M_TEMP);
2016 return(-1);
2018 strcpy(new[i].name, resname);
2019 new[i].type = type;
2020 if (devtab[j].resources)
2021 kfree(devtab[j].resources, M_TEMP);
2022 devtab[j].resources = new;
2023 devtab[j].resource_count = i + 1;
2024 return(i);
2027 static int
2028 resource_match_string(int i, const char *resname, const char *value)
2030 int j;
2031 struct config_resource *res;
2033 for (j = 0, res = devtab[i].resources;
2034 j < devtab[i].resource_count; j++, res++)
2035 if (!strcmp(res->name, resname)
2036 && res->type == RES_STRING
2037 && !strcmp(res->u.stringval, value))
2038 return(j);
2039 return(-1);
2042 static int
2043 resource_find(const char *name, int unit, const char *resname,
2044 struct config_resource **result)
2046 int i, j;
2047 struct config_resource *res;
2050 * First check specific instances, then generic.
2052 for (i = 0; i < devtab_count; i++) {
2053 if (devtab[i].unit < 0)
2054 continue;
2055 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2056 res = devtab[i].resources;
2057 for (j = 0; j < devtab[i].resource_count; j++, res++)
2058 if (!strcmp(res->name, resname)) {
2059 *result = res;
2060 return(0);
2064 for (i = 0; i < devtab_count; i++) {
2065 if (devtab[i].unit >= 0)
2066 continue;
2067 /* XXX should this `&& devtab[i].unit == unit' be here? */
2068 /* XXX if so, then the generic match does nothing */
2069 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2070 res = devtab[i].resources;
2071 for (j = 0; j < devtab[i].resource_count; j++, res++)
2072 if (!strcmp(res->name, resname)) {
2073 *result = res;
2074 return(0);
2078 return(ENOENT);
2081 static int
2082 resource_kenv(const char *name, int unit, const char *resname, long *result)
2084 const char *env;
2085 char buf[64];
2087 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2088 if ((env = kgetenv(buf)) != NULL) {
2089 *result = strtol(env, NULL, 0);
2090 return(0);
2092 return (ENOENT);
2096 resource_int_value(const char *name, int unit, const char *resname, int *result)
2098 struct config_resource *res;
2099 long kvalue = 0;
2100 int error;
2102 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2103 *result = (int)kvalue;
2104 return 0;
2106 if ((error = resource_find(name, unit, resname, &res)) != 0)
2107 return(error);
2108 if (res->type != RES_INT)
2109 return(EFTYPE);
2110 *result = res->u.intval;
2111 return(0);
2115 resource_long_value(const char *name, int unit, const char *resname,
2116 long *result)
2118 struct config_resource *res;
2119 long kvalue;
2120 int error;
2122 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2123 *result = kvalue;
2124 return 0;
2126 if ((error = resource_find(name, unit, resname, &res)) != 0)
2127 return(error);
2128 if (res->type != RES_LONG)
2129 return(EFTYPE);
2130 *result = res->u.longval;
2131 return(0);
2135 resource_string_value(const char *name, int unit, const char *resname,
2136 const char **result)
2138 int error;
2139 struct config_resource *res;
2140 char buf[64];
2141 const char *env;
2143 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2144 if ((env = kgetenv(buf)) != NULL) {
2145 *result = env;
2146 return 0;
2149 if ((error = resource_find(name, unit, resname, &res)) != 0)
2150 return(error);
2151 if (res->type != RES_STRING)
2152 return(EFTYPE);
2153 *result = res->u.stringval;
2154 return(0);
2158 resource_query_string(int i, const char *resname, const char *value)
2160 if (i < 0)
2161 i = 0;
2162 else
2163 i = i + 1;
2164 for (; i < devtab_count; i++)
2165 if (resource_match_string(i, resname, value) >= 0)
2166 return(i);
2167 return(-1);
2171 resource_locate(int i, const char *resname)
2173 if (i < 0)
2174 i = 0;
2175 else
2176 i = i + 1;
2177 for (; i < devtab_count; i++)
2178 if (!strcmp(devtab[i].name, resname))
2179 return(i);
2180 return(-1);
2184 resource_count(void)
2186 return(devtab_count);
2189 char *
2190 resource_query_name(int i)
2192 return(devtab[i].name);
2196 resource_query_unit(int i)
2198 return(devtab[i].unit);
2201 static int
2202 resource_create(const char *name, int unit, const char *resname,
2203 resource_type type, struct config_resource **result)
2205 int i, j;
2206 struct config_resource *res = NULL;
2208 for (i = 0; i < devtab_count; i++)
2209 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2210 res = devtab[i].resources;
2211 break;
2213 if (res == NULL) {
2214 i = resource_new_name(name, unit);
2215 if (i < 0)
2216 return(ENOMEM);
2217 res = devtab[i].resources;
2219 for (j = 0; j < devtab[i].resource_count; j++, res++)
2220 if (!strcmp(res->name, resname)) {
2221 *result = res;
2222 return(0);
2224 j = resource_new_resname(i, resname, type);
2225 if (j < 0)
2226 return(ENOMEM);
2227 res = &devtab[i].resources[j];
2228 *result = res;
2229 return(0);
2233 resource_set_int(const char *name, int unit, const char *resname, int value)
2235 int error;
2236 struct config_resource *res;
2238 error = resource_create(name, unit, resname, RES_INT, &res);
2239 if (error)
2240 return(error);
2241 if (res->type != RES_INT)
2242 return(EFTYPE);
2243 res->u.intval = value;
2244 return(0);
2248 resource_set_long(const char *name, int unit, const char *resname, long value)
2250 int error;
2251 struct config_resource *res;
2253 error = resource_create(name, unit, resname, RES_LONG, &res);
2254 if (error)
2255 return(error);
2256 if (res->type != RES_LONG)
2257 return(EFTYPE);
2258 res->u.longval = value;
2259 return(0);
2263 resource_set_string(const char *name, int unit, const char *resname,
2264 const char *value)
2266 int error;
2267 struct config_resource *res;
2269 error = resource_create(name, unit, resname, RES_STRING, &res);
2270 if (error)
2271 return(error);
2272 if (res->type != RES_STRING)
2273 return(EFTYPE);
2274 if (res->u.stringval)
2275 kfree(res->u.stringval, M_TEMP);
2276 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2277 if (res->u.stringval == NULL)
2278 return(ENOMEM);
2279 strcpy(res->u.stringval, value);
2280 return(0);
2283 static void
2284 resource_cfgload(void *dummy __unused)
2286 struct config_resource *res, *cfgres;
2287 int i, j;
2288 int error;
2289 char *name, *resname;
2290 int unit;
2291 resource_type type;
2292 char *stringval;
2293 int config_devtab_count;
2295 config_devtab_count = devtab_count;
2296 devtab = NULL;
2297 devtab_count = 0;
2299 for (i = 0; i < config_devtab_count; i++) {
2300 name = config_devtab[i].name;
2301 unit = config_devtab[i].unit;
2303 for (j = 0; j < config_devtab[i].resource_count; j++) {
2304 cfgres = config_devtab[i].resources;
2305 resname = cfgres[j].name;
2306 type = cfgres[j].type;
2307 error = resource_create(name, unit, resname, type,
2308 &res);
2309 if (error) {
2310 kprintf("create resource %s%d: error %d\n",
2311 name, unit, error);
2312 continue;
2314 if (res->type != type) {
2315 kprintf("type mismatch %s%d: %d != %d\n",
2316 name, unit, res->type, type);
2317 continue;
2319 switch (type) {
2320 case RES_INT:
2321 res->u.intval = cfgres[j].u.intval;
2322 break;
2323 case RES_LONG:
2324 res->u.longval = cfgres[j].u.longval;
2325 break;
2326 case RES_STRING:
2327 if (res->u.stringval)
2328 kfree(res->u.stringval, M_TEMP);
2329 stringval = cfgres[j].u.stringval;
2330 res->u.stringval = kmalloc(strlen(stringval) + 1,
2331 M_TEMP, M_INTWAIT);
2332 if (res->u.stringval == NULL)
2333 break;
2334 strcpy(res->u.stringval, stringval);
2335 break;
2336 default:
2337 panic("unknown resource type %d", type);
2342 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0);
2345 /*======================================*/
2347 * Some useful method implementations to make life easier for bus drivers.
2350 void
2351 resource_list_init(struct resource_list *rl)
2353 SLIST_INIT(rl);
2356 void
2357 resource_list_free(struct resource_list *rl)
2359 struct resource_list_entry *rle;
2361 while ((rle = SLIST_FIRST(rl)) != NULL) {
2362 if (rle->res)
2363 panic("resource_list_free: resource entry is busy");
2364 SLIST_REMOVE_HEAD(rl, link);
2365 kfree(rle, M_BUS);
2369 void
2370 resource_list_add(struct resource_list *rl, int type, int rid,
2371 u_long start, u_long end, u_long count, int cpuid)
2373 struct resource_list_entry *rle;
2375 rle = resource_list_find(rl, type, rid);
2376 if (rle == NULL) {
2377 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2378 M_INTWAIT);
2379 SLIST_INSERT_HEAD(rl, rle, link);
2380 rle->type = type;
2381 rle->rid = rid;
2382 rle->res = NULL;
2383 rle->cpuid = -1;
2386 if (rle->res)
2387 panic("resource_list_add: resource entry is busy");
2389 rle->start = start;
2390 rle->end = end;
2391 rle->count = count;
2393 if (cpuid != -1) {
2394 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2395 panic("resource_list_add: moving from cpu%d -> cpu%d",
2396 rle->cpuid, cpuid);
2398 rle->cpuid = cpuid;
2402 struct resource_list_entry*
2403 resource_list_find(struct resource_list *rl,
2404 int type, int rid)
2406 struct resource_list_entry *rle;
2408 SLIST_FOREACH(rle, rl, link)
2409 if (rle->type == type && rle->rid == rid)
2410 return(rle);
2411 return(NULL);
2414 void
2415 resource_list_delete(struct resource_list *rl,
2416 int type, int rid)
2418 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2420 if (rle) {
2421 if (rle->res != NULL)
2422 panic("resource_list_delete: resource has not been released");
2423 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2424 kfree(rle, M_BUS);
2428 struct resource *
2429 resource_list_alloc(struct resource_list *rl,
2430 device_t bus, device_t child,
2431 int type, int *rid,
2432 u_long start, u_long end,
2433 u_long count, u_int flags, int cpuid)
2435 struct resource_list_entry *rle = NULL;
2436 int passthrough = (device_get_parent(child) != bus);
2437 int isdefault = (start == 0UL && end == ~0UL);
2439 if (passthrough) {
2440 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2441 type, rid,
2442 start, end, count, flags, cpuid));
2445 rle = resource_list_find(rl, type, *rid);
2447 if (!rle)
2448 return(0); /* no resource of that type/rid */
2450 if (rle->res)
2451 panic("resource_list_alloc: resource entry is busy");
2453 if (isdefault) {
2454 start = rle->start;
2455 count = max(count, rle->count);
2456 end = max(rle->end, start + count - 1);
2458 cpuid = rle->cpuid;
2460 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2461 type, rid, start, end, count,
2462 flags, cpuid);
2465 * Record the new range.
2467 if (rle->res) {
2468 rle->start = rman_get_start(rle->res);
2469 rle->end = rman_get_end(rle->res);
2470 rle->count = count;
2473 return(rle->res);
2477 resource_list_release(struct resource_list *rl,
2478 device_t bus, device_t child,
2479 int type, int rid, struct resource *res)
2481 struct resource_list_entry *rle = NULL;
2482 int passthrough = (device_get_parent(child) != bus);
2483 int error;
2485 if (passthrough) {
2486 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2487 type, rid, res));
2490 rle = resource_list_find(rl, type, rid);
2492 if (!rle)
2493 panic("resource_list_release: can't find resource");
2494 if (!rle->res)
2495 panic("resource_list_release: resource entry is not busy");
2497 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2498 type, rid, res);
2499 if (error)
2500 return(error);
2502 rle->res = NULL;
2503 return(0);
2507 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2508 const char *format)
2510 struct resource_list_entry *rle;
2511 int printed, retval;
2513 printed = 0;
2514 retval = 0;
2515 /* Yes, this is kinda cheating */
2516 SLIST_FOREACH(rle, rl, link) {
2517 if (rle->type == type) {
2518 if (printed == 0)
2519 retval += kprintf(" %s ", name);
2520 else
2521 retval += kprintf(",");
2522 printed++;
2523 retval += kprintf(format, rle->start);
2524 if (rle->count > 1) {
2525 retval += kprintf("-");
2526 retval += kprintf(format, rle->start +
2527 rle->count - 1);
2531 return(retval);
2535 * Generic driver/device identify functions. These will install a device
2536 * rendezvous point under the parent using the same name as the driver
2537 * name, which will at a later time be probed and attached.
2539 * These functions are used when the parent does not 'scan' its bus for
2540 * matching devices, or for the particular devices using these functions,
2541 * or when the device is a pseudo or synthesized device (such as can be
2542 * found under firewire and ppbus).
2545 bus_generic_identify(driver_t *driver, device_t parent)
2547 if (parent->state == DS_ATTACHED)
2548 return (0);
2549 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2550 return (0);
2554 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2556 if (parent->state == DS_ATTACHED)
2557 return (0);
2558 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2559 return (0);
2563 * Call DEVICE_IDENTIFY for each driver.
2566 bus_generic_probe(device_t dev)
2568 devclass_t dc = dev->devclass;
2569 driverlink_t dl;
2571 TAILQ_FOREACH(dl, &dc->drivers, link) {
2572 DEVICE_IDENTIFY(dl->driver, dev);
2575 return(0);
2579 * This is an aweful hack due to the isa bus and autoconf code not
2580 * probing the ISA devices until after everything else has configured.
2581 * The ISA bus did a dummy attach long ago so we have to set it back
2582 * to an earlier state so the probe thinks its the initial probe and
2583 * not a bus rescan.
2585 * XXX remove by properly defering the ISA bus scan.
2588 bus_generic_probe_hack(device_t dev)
2590 if (dev->state == DS_ATTACHED) {
2591 dev->state = DS_ALIVE;
2592 bus_generic_probe(dev);
2593 dev->state = DS_ATTACHED;
2595 return (0);
2599 bus_generic_attach(device_t dev)
2601 device_t child;
2603 TAILQ_FOREACH(child, &dev->children, link) {
2604 device_probe_and_attach(child);
2607 return(0);
2611 bus_generic_detach(device_t dev)
2613 device_t child;
2614 int error;
2616 if (dev->state != DS_ATTACHED)
2617 return(EBUSY);
2619 TAILQ_FOREACH(child, &dev->children, link)
2620 if ((error = device_detach(child)) != 0)
2621 return(error);
2623 return 0;
2627 bus_generic_shutdown(device_t dev)
2629 device_t child;
2631 TAILQ_FOREACH(child, &dev->children, link)
2632 device_shutdown(child);
2634 return(0);
2638 bus_generic_suspend(device_t dev)
2640 int error;
2641 device_t child, child2;
2643 TAILQ_FOREACH(child, &dev->children, link) {
2644 error = DEVICE_SUSPEND(child);
2645 if (error) {
2646 for (child2 = TAILQ_FIRST(&dev->children);
2647 child2 && child2 != child;
2648 child2 = TAILQ_NEXT(child2, link))
2649 DEVICE_RESUME(child2);
2650 return(error);
2653 return(0);
2657 bus_generic_resume(device_t dev)
2659 device_t child;
2661 TAILQ_FOREACH(child, &dev->children, link)
2662 DEVICE_RESUME(child);
2663 /* if resume fails, there's nothing we can usefully do... */
2665 return(0);
2669 bus_print_child_header(device_t dev, device_t child)
2671 int retval = 0;
2673 if (device_get_desc(child))
2674 retval += device_printf(child, "<%s>", device_get_desc(child));
2675 else
2676 retval += kprintf("%s", device_get_nameunit(child));
2677 if (bootverbose) {
2678 if (child->state != DS_ATTACHED)
2679 kprintf(" [tentative]");
2680 else
2681 kprintf(" [attached!]");
2683 return(retval);
2687 bus_print_child_footer(device_t dev, device_t child)
2689 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2692 device_t
2693 bus_generic_add_child(device_t dev, device_t child, int order,
2694 const char *name, int unit)
2696 if (dev->parent)
2697 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2698 else
2699 dev = device_add_child_ordered(child, order, name, unit);
2700 return(dev);
2705 bus_generic_print_child(device_t dev, device_t child)
2707 int retval = 0;
2709 retval += bus_print_child_header(dev, child);
2710 retval += bus_print_child_footer(dev, child);
2712 return(retval);
2716 bus_generic_read_ivar(device_t dev, device_t child, int index,
2717 uintptr_t * result)
2719 int error;
2721 if (dev->parent)
2722 error = BUS_READ_IVAR(dev->parent, child, index, result);
2723 else
2724 error = ENOENT;
2725 return (error);
2729 bus_generic_write_ivar(device_t dev, device_t child, int index,
2730 uintptr_t value)
2732 int error;
2734 if (dev->parent)
2735 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2736 else
2737 error = ENOENT;
2738 return (error);
2742 * Resource list are used for iterations, do not recurse.
2744 struct resource_list *
2745 bus_generic_get_resource_list(device_t dev, device_t child)
2747 return (NULL);
2750 void
2751 bus_generic_driver_added(device_t dev, driver_t *driver)
2753 device_t child;
2755 DEVICE_IDENTIFY(driver, dev);
2756 TAILQ_FOREACH(child, &dev->children, link) {
2757 if (child->state == DS_NOTPRESENT)
2758 device_probe_and_attach(child);
2763 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2764 int flags, driver_intr_t *intr, void *arg, void **cookiep,
2765 lwkt_serialize_t serializer, const char *desc)
2767 /* Propagate up the bus hierarchy until someone handles it. */
2768 if (dev->parent) {
2769 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2770 intr, arg, cookiep, serializer, desc);
2771 } else {
2772 return EINVAL;
2777 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2778 void *cookie)
2780 /* Propagate up the bus hierarchy until someone handles it. */
2781 if (dev->parent)
2782 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2783 else
2784 return(EINVAL);
2788 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2790 if (dev->parent)
2791 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2792 else
2793 return(0);
2796 void
2797 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2799 if (dev->parent)
2800 BUS_ENABLE_INTR(dev->parent, child, cookie);
2804 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2805 enum intr_polarity pol)
2807 /* Propagate up the bus hierarchy until someone handles it. */
2808 if (dev->parent)
2809 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2810 else
2811 return(EINVAL);
2814 struct resource *
2815 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2816 u_long start, u_long end, u_long count, u_int flags, int cpuid)
2818 /* Propagate up the bus hierarchy until someone handles it. */
2819 if (dev->parent)
2820 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2821 start, end, count, flags, cpuid));
2822 else
2823 return(NULL);
2827 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2828 struct resource *r)
2830 /* Propagate up the bus hierarchy until someone handles it. */
2831 if (dev->parent)
2832 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2833 else
2834 return(EINVAL);
2838 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2839 struct resource *r)
2841 /* Propagate up the bus hierarchy until someone handles it. */
2842 if (dev->parent)
2843 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2844 else
2845 return(EINVAL);
2849 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2850 int rid, struct resource *r)
2852 /* Propagate up the bus hierarchy until someone handles it. */
2853 if (dev->parent)
2854 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2855 r));
2856 else
2857 return(EINVAL);
2861 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2862 u_long *startp, u_long *countp)
2864 int error;
2866 error = ENOENT;
2867 if (dev->parent) {
2868 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2869 startp, countp);
2871 return (error);
2875 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2876 u_long start, u_long count, int cpuid)
2878 int error;
2880 error = EINVAL;
2881 if (dev->parent) {
2882 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2883 start, count, cpuid);
2885 return (error);
2888 void
2889 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2891 if (dev->parent)
2892 BUS_DELETE_RESOURCE(dev, child, type, rid);
2896 * @brief Helper function for implementing BUS_GET_DMA_TAG().
2898 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
2899 * BUS_GET_DMA_TAG() method of the parent of @p dev.
2901 bus_dma_tag_t
2902 bus_generic_get_dma_tag(device_t dev, device_t child)
2905 /* Propagate up the bus hierarchy until someone handles it. */
2906 if (dev->parent != NULL)
2907 return (BUS_GET_DMA_TAG(dev->parent, child));
2908 return (NULL);
2912 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2913 u_long *startp, u_long *countp)
2915 struct resource_list *rl = NULL;
2916 struct resource_list_entry *rle = NULL;
2918 rl = BUS_GET_RESOURCE_LIST(dev, child);
2919 if (!rl)
2920 return(EINVAL);
2922 rle = resource_list_find(rl, type, rid);
2923 if (!rle)
2924 return(ENOENT);
2926 if (startp)
2927 *startp = rle->start;
2928 if (countp)
2929 *countp = rle->count;
2931 return(0);
2935 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2936 u_long start, u_long count, int cpuid)
2938 struct resource_list *rl = NULL;
2940 rl = BUS_GET_RESOURCE_LIST(dev, child);
2941 if (!rl)
2942 return(EINVAL);
2944 resource_list_add(rl, type, rid, start, (start + count - 1), count,
2945 cpuid);
2947 return(0);
2950 void
2951 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2953 struct resource_list *rl = NULL;
2955 rl = BUS_GET_RESOURCE_LIST(dev, child);
2956 if (!rl)
2957 return;
2959 resource_list_delete(rl, type, rid);
2963 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2964 int rid, struct resource *r)
2966 struct resource_list *rl = NULL;
2968 rl = BUS_GET_RESOURCE_LIST(dev, child);
2969 if (!rl)
2970 return(EINVAL);
2972 return(resource_list_release(rl, dev, child, type, rid, r));
2975 struct resource *
2976 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2977 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
2979 struct resource_list *rl = NULL;
2981 rl = BUS_GET_RESOURCE_LIST(dev, child);
2982 if (!rl)
2983 return(NULL);
2985 return(resource_list_alloc(rl, dev, child, type, rid,
2986 start, end, count, flags, cpuid));
2990 bus_generic_child_present(device_t bus, device_t child)
2992 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2997 * Some convenience functions to make it easier for drivers to use the
2998 * resource-management functions. All these really do is hide the
2999 * indirection through the parent's method table, making for slightly
3000 * less-wordy code. In the future, it might make sense for this code
3001 * to maintain some sort of a list of resources allocated by each device.
3004 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3005 struct resource **res)
3007 int i;
3009 for (i = 0; rs[i].type != -1; i++)
3010 res[i] = NULL;
3011 for (i = 0; rs[i].type != -1; i++) {
3012 res[i] = bus_alloc_resource_any(dev,
3013 rs[i].type, &rs[i].rid, rs[i].flags);
3014 if (res[i] == NULL) {
3015 bus_release_resources(dev, rs, res);
3016 return (ENXIO);
3019 return (0);
3022 void
3023 bus_release_resources(device_t dev, const struct resource_spec *rs,
3024 struct resource **res)
3026 int i;
3028 for (i = 0; rs[i].type != -1; i++)
3029 if (res[i] != NULL) {
3030 bus_release_resource(
3031 dev, rs[i].type, rs[i].rid, res[i]);
3032 res[i] = NULL;
3036 struct resource *
3037 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3038 u_long count, u_int flags)
3040 if (dev->parent == NULL)
3041 return(0);
3042 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3043 count, flags, -1));
3046 struct resource *
3047 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3049 if (dev->parent == NULL)
3050 return(0);
3051 return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3052 irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3056 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3058 if (dev->parent == NULL)
3059 return(EINVAL);
3060 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3064 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3066 if (dev->parent == NULL)
3067 return(EINVAL);
3068 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3072 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3074 if (dev->parent == NULL)
3075 return(EINVAL);
3076 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3080 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3081 driver_intr_t handler, void *arg, void **cookiep,
3082 lwkt_serialize_t serializer, const char *desc)
3084 if (dev->parent == NULL)
3085 return EINVAL;
3086 return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3087 cookiep, serializer, desc);
3091 bus_setup_intr(device_t dev, struct resource *r, int flags,
3092 driver_intr_t handler, void *arg, void **cookiep,
3093 lwkt_serialize_t serializer)
3095 return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3096 serializer, NULL);
3100 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3102 if (dev->parent == NULL)
3103 return(EINVAL);
3104 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3107 void
3108 bus_enable_intr(device_t dev, void *cookie)
3110 if (dev->parent)
3111 BUS_ENABLE_INTR(dev->parent, dev, cookie);
3115 bus_disable_intr(device_t dev, void *cookie)
3117 if (dev->parent)
3118 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3119 else
3120 return(0);
3124 bus_set_resource(device_t dev, int type, int rid,
3125 u_long start, u_long count, int cpuid)
3127 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3128 start, count, cpuid));
3132 bus_get_resource(device_t dev, int type, int rid,
3133 u_long *startp, u_long *countp)
3135 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3136 startp, countp));
3139 u_long
3140 bus_get_resource_start(device_t dev, int type, int rid)
3142 u_long start, count;
3143 int error;
3145 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3146 &start, &count);
3147 if (error)
3148 return(0);
3149 return(start);
3152 u_long
3153 bus_get_resource_count(device_t dev, int type, int rid)
3155 u_long start, count;
3156 int error;
3158 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3159 &start, &count);
3160 if (error)
3161 return(0);
3162 return(count);
3165 void
3166 bus_delete_resource(device_t dev, int type, int rid)
3168 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3172 bus_child_present(device_t child)
3174 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3178 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3180 device_t parent;
3182 parent = device_get_parent(child);
3183 if (parent == NULL) {
3184 *buf = '\0';
3185 return (0);
3187 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3191 bus_child_location_str(device_t child, char *buf, size_t buflen)
3193 device_t parent;
3195 parent = device_get_parent(child);
3196 if (parent == NULL) {
3197 *buf = '\0';
3198 return (0);
3200 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3204 * @brief Wrapper function for BUS_GET_DMA_TAG().
3206 * This function simply calls the BUS_GET_DMA_TAG() method of the
3207 * parent of @p dev.
3209 bus_dma_tag_t
3210 bus_get_dma_tag(device_t dev)
3212 device_t parent;
3214 parent = device_get_parent(dev);
3215 if (parent == NULL)
3216 return (NULL);
3217 return (BUS_GET_DMA_TAG(parent, dev));
3220 static int
3221 root_print_child(device_t dev, device_t child)
3223 return(0);
3226 static int
3227 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3228 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3231 * If an interrupt mapping gets to here something bad has happened.
3233 panic("root_setup_intr");
3237 * If we get here, assume that the device is permanant and really is
3238 * present in the system. Removable bus drivers are expected to intercept
3239 * this call long before it gets here. We return -1 so that drivers that
3240 * really care can check vs -1 or some ERRNO returned higher in the food
3241 * chain.
3243 static int
3244 root_child_present(device_t dev, device_t child)
3246 return(-1);
3250 * XXX NOTE! other defaults may be set in bus_if.m
3252 static kobj_method_t root_methods[] = {
3253 /* Device interface */
3254 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3255 KOBJMETHOD(device_suspend, bus_generic_suspend),
3256 KOBJMETHOD(device_resume, bus_generic_resume),
3258 /* Bus interface */
3259 KOBJMETHOD(bus_add_child, bus_generic_add_child),
3260 KOBJMETHOD(bus_print_child, root_print_child),
3261 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3262 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3263 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3264 KOBJMETHOD(bus_child_present, root_child_present),
3266 KOBJMETHOD_END
3269 static driver_t root_driver = {
3270 "root",
3271 root_methods,
3272 1, /* no softc */
3275 device_t root_bus;
3276 devclass_t root_devclass;
3278 static int
3279 root_bus_module_handler(module_t mod, int what, void* arg)
3281 switch (what) {
3282 case MOD_LOAD:
3283 TAILQ_INIT(&bus_data_devices);
3284 root_bus = make_device(NULL, "root", 0);
3285 root_bus->desc = "System root bus";
3286 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3287 root_bus->driver = &root_driver;
3288 root_bus->state = DS_ALIVE;
3289 root_devclass = devclass_find_internal("root", NULL, FALSE);
3290 devinit();
3291 return(0);
3293 case MOD_SHUTDOWN:
3294 device_shutdown(root_bus);
3295 return(0);
3296 default:
3297 return(0);
3301 static moduledata_t root_bus_mod = {
3302 "rootbus",
3303 root_bus_module_handler,
3306 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3308 void
3309 root_bus_configure(void)
3311 int warncount;
3312 device_t dev;
3314 PDEBUG(("."));
3317 * handle device_identify based device attachments to the root_bus
3318 * (typically nexus).
3320 bus_generic_probe(root_bus);
3323 * Probe and attach the devices under root_bus.
3325 TAILQ_FOREACH(dev, &root_bus->children, link) {
3326 device_probe_and_attach(dev);
3330 * Wait for all asynchronous attaches to complete. If we don't
3331 * our legacy ISA bus scan could steal device unit numbers or
3332 * even I/O ports.
3334 warncount = 10;
3335 if (numasyncthreads)
3336 kprintf("Waiting for async drivers to attach\n");
3337 while (numasyncthreads > 0) {
3338 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3339 --warncount;
3340 if (warncount == 0) {
3341 kprintf("Warning: Still waiting for %d "
3342 "drivers to attach\n", numasyncthreads);
3343 } else if (warncount == -30) {
3344 kprintf("Giving up on %d drivers\n", numasyncthreads);
3345 break;
3348 root_bus->state = DS_ATTACHED;
3352 driver_module_handler(module_t mod, int what, void *arg)
3354 int error;
3355 struct driver_module_data *dmd;
3356 devclass_t bus_devclass;
3357 kobj_class_t driver;
3358 const char *parentname;
3360 dmd = (struct driver_module_data *)arg;
3361 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3362 error = 0;
3364 switch (what) {
3365 case MOD_LOAD:
3366 if (dmd->dmd_chainevh)
3367 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3369 driver = dmd->dmd_driver;
3370 PDEBUG(("Loading module: driver %s on bus %s",
3371 DRIVERNAME(driver), dmd->dmd_busname));
3374 * If the driver has any base classes, make the
3375 * devclass inherit from the devclass of the driver's
3376 * first base class. This will allow the system to
3377 * search for drivers in both devclasses for children
3378 * of a device using this driver.
3380 if (driver->baseclasses)
3381 parentname = driver->baseclasses[0]->name;
3382 else
3383 parentname = NULL;
3384 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3385 parentname, TRUE);
3387 error = devclass_add_driver(bus_devclass, driver);
3388 if (error)
3389 break;
3390 break;
3392 case MOD_UNLOAD:
3393 PDEBUG(("Unloading module: driver %s from bus %s",
3394 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3395 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3397 if (!error && dmd->dmd_chainevh)
3398 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3399 break;
3402 return (error);
3405 #ifdef BUS_DEBUG
3408 * The _short versions avoid iteration by not calling anything that prints
3409 * more than oneliners. I love oneliners.
3412 static void
3413 print_device_short(device_t dev, int indent)
3415 if (!dev)
3416 return;
3418 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3419 dev->unit, dev->desc,
3420 (dev->parent? "":"no "),
3421 (TAILQ_EMPTY(&dev->children)? "no ":""),
3422 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3423 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3424 (dev->flags&DF_WILDCARD? "wildcard,":""),
3425 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3426 (dev->ivars? "":"no "),
3427 (dev->softc? "":"no "),
3428 dev->busy));
3431 static void
3432 print_device(device_t dev, int indent)
3434 if (!dev)
3435 return;
3437 print_device_short(dev, indent);
3439 indentprintf(("Parent:\n"));
3440 print_device_short(dev->parent, indent+1);
3441 indentprintf(("Driver:\n"));
3442 print_driver_short(dev->driver, indent+1);
3443 indentprintf(("Devclass:\n"));
3444 print_devclass_short(dev->devclass, indent+1);
3448 * Print the device and all its children (indented).
3450 void
3451 print_device_tree_short(device_t dev, int indent)
3453 device_t child;
3455 if (!dev)
3456 return;
3458 print_device_short(dev, indent);
3460 TAILQ_FOREACH(child, &dev->children, link)
3461 print_device_tree_short(child, indent+1);
3465 * Print the device and all its children (indented).
3467 void
3468 print_device_tree(device_t dev, int indent)
3470 device_t child;
3472 if (!dev)
3473 return;
3475 print_device(dev, indent);
3477 TAILQ_FOREACH(child, &dev->children, link)
3478 print_device_tree(child, indent+1);
3481 static void
3482 print_driver_short(driver_t *driver, int indent)
3484 if (!driver)
3485 return;
3487 indentprintf(("driver %s: softc size = %zu\n",
3488 driver->name, driver->size));
3491 static void
3492 print_driver(driver_t *driver, int indent)
3494 if (!driver)
3495 return;
3497 print_driver_short(driver, indent);
3501 static void
3502 print_driver_list(driver_list_t drivers, int indent)
3504 driverlink_t driver;
3506 TAILQ_FOREACH(driver, &drivers, link)
3507 print_driver(driver->driver, indent);
3510 static void
3511 print_devclass_short(devclass_t dc, int indent)
3513 if (!dc)
3514 return;
3516 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3519 static void
3520 print_devclass(devclass_t dc, int indent)
3522 int i;
3524 if (!dc)
3525 return;
3527 print_devclass_short(dc, indent);
3528 indentprintf(("Drivers:\n"));
3529 print_driver_list(dc->drivers, indent+1);
3531 indentprintf(("Devices:\n"));
3532 for (i = 0; i < dc->maxunit; i++)
3533 if (dc->devices[i])
3534 print_device(dc->devices[i], indent+1);
3537 void
3538 print_devclass_list_short(void)
3540 devclass_t dc;
3542 kprintf("Short listing of devclasses, drivers & devices:\n");
3543 TAILQ_FOREACH(dc, &devclasses, link) {
3544 print_devclass_short(dc, 0);
3548 void
3549 print_devclass_list(void)
3551 devclass_t dc;
3553 kprintf("Full listing of devclasses, drivers & devices:\n");
3554 TAILQ_FOREACH(dc, &devclasses, link) {
3555 print_devclass(dc, 0);
3559 #endif
3562 * Check to see if a device is disabled via a disabled hint.
3565 resource_disabled(const char *name, int unit)
3567 int error, value;
3569 error = resource_int_value(name, unit, "disabled", &value);
3570 if (error)
3571 return(0);
3572 return(value);
3576 * User-space access to the device tree.
3578 * We implement a small set of nodes:
3580 * hw.bus Single integer read method to obtain the
3581 * current generation count.
3582 * hw.bus.devices Reads the entire device tree in flat space.
3583 * hw.bus.rman Resource manager interface
3585 * We might like to add the ability to scan devclasses and/or drivers to
3586 * determine what else is currently loaded/available.
3589 static int
3590 sysctl_bus(SYSCTL_HANDLER_ARGS)
3592 struct u_businfo ubus;
3594 ubus.ub_version = BUS_USER_VERSION;
3595 ubus.ub_generation = bus_data_generation;
3597 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3599 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3600 "bus-related data");
3602 static int
3603 sysctl_devices(SYSCTL_HANDLER_ARGS)
3605 int *name = (int *)arg1;
3606 u_int namelen = arg2;
3607 int index;
3608 device_t dev;
3609 struct u_device udev; /* XXX this is a bit big */
3610 int error;
3612 if (namelen != 2)
3613 return (EINVAL);
3615 if (bus_data_generation_check(name[0]))
3616 return (EINVAL);
3618 index = name[1];
3621 * Scan the list of devices, looking for the requested index.
3623 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3624 if (index-- == 0)
3625 break;
3627 if (dev == NULL)
3628 return (ENOENT);
3631 * Populate the return array.
3633 bzero(&udev, sizeof(udev));
3634 udev.dv_handle = (uintptr_t)dev;
3635 udev.dv_parent = (uintptr_t)dev->parent;
3636 if (dev->nameunit != NULL)
3637 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3638 if (dev->desc != NULL)
3639 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3640 if (dev->driver != NULL && dev->driver->name != NULL)
3641 strlcpy(udev.dv_drivername, dev->driver->name,
3642 sizeof(udev.dv_drivername));
3643 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3644 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3645 udev.dv_devflags = dev->devflags;
3646 udev.dv_flags = dev->flags;
3647 udev.dv_state = dev->state;
3648 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3649 return (error);
3652 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3653 "system device tree");
3656 bus_data_generation_check(int generation)
3658 if (generation != bus_data_generation)
3659 return (1);
3661 /* XXX generate optimised lists here? */
3662 return (0);
3665 void
3666 bus_data_generation_update(void)
3668 bus_data_generation++;
3671 const char *
3672 intr_str_polarity(enum intr_polarity pola)
3674 switch (pola) {
3675 case INTR_POLARITY_LOW:
3676 return "low";
3678 case INTR_POLARITY_HIGH:
3679 return "high";
3681 case INTR_POLARITY_CONFORM:
3682 return "conform";
3684 return "unknown";
3687 const char *
3688 intr_str_trigger(enum intr_trigger trig)
3690 switch (trig) {
3691 case INTR_TRIGGER_EDGE:
3692 return "edge";
3694 case INTR_TRIGGER_LEVEL:
3695 return "level";
3697 case INTR_TRIGGER_CONFORM:
3698 return "conform";
3700 return "unknown";
3704 device_getenv_int(device_t dev, const char *knob, int def)
3706 char env[128];
3708 /* Deprecated; for compat */
3709 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3710 kgetenv_int(env, &def);
3712 /* Prefer dev.driver.unit.knob */
3713 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3714 device_get_name(dev), device_get_unit(dev), knob);
3715 kgetenv_int(env, &def);
3717 return def;
3720 void
3721 device_getenv_string(device_t dev, const char *knob, char * __restrict data,
3722 int dlen, const char * __restrict def)
3724 char env[128];
3726 strlcpy(data, def, dlen);
3728 /* Deprecated; for compat */
3729 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3730 kgetenv_string(env, data, dlen);
3732 /* Prefer dev.driver.unit.knob */
3733 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3734 device_get_name(dev), device_get_unit(dev), knob);
3735 kgetenv_string(env, data, dlen);