usr.sbin/makefs/ffs: Remove m_buf::b_is_hammer2
[dragonfly.git] / sys / kern / subr_bus.c
blob623f1d0a8ced1ff4deeedbbd0ae859d72ddd86dd
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 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
55 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
57 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
59 #ifdef BUS_DEBUG
60 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
61 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
62 #define DRIVERNAME(d) ((d)? d->name : "no driver")
63 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
65 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
66 * prevent syslog from deleting initial spaces
68 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
70 static void print_device_short(device_t dev, int indent);
71 static void print_device(device_t dev, int indent);
72 void print_device_tree_short(device_t dev, int indent);
73 void print_device_tree(device_t dev, int indent);
74 static void print_driver_short(driver_t *driver, int indent);
75 static void print_driver(driver_t *driver, int indent);
76 static void print_driver_list(driver_list_t drivers, int indent);
77 static void print_devclass_short(devclass_t dc, int indent);
78 static void print_devclass(devclass_t dc, int indent);
79 void print_devclass_list_short(void);
80 void print_devclass_list(void);
82 #else
83 /* Make the compiler ignore the function calls */
84 #define PDEBUG(a) /* nop */
85 #define DEVICENAME(d) /* nop */
86 #define DRIVERNAME(d) /* nop */
87 #define DEVCLANAME(d) /* nop */
89 #define print_device_short(d,i) /* nop */
90 #define print_device(d,i) /* nop */
91 #define print_device_tree_short(d,i) /* nop */
92 #define print_device_tree(d,i) /* nop */
93 #define print_driver_short(d,i) /* nop */
94 #define print_driver(d,i) /* nop */
95 #define print_driver_list(d,i) /* nop */
96 #define print_devclass_short(d,i) /* nop */
97 #define print_devclass(d,i) /* nop */
98 #define print_devclass_list_short() /* nop */
99 #define print_devclass_list() /* nop */
100 #endif
103 * dev sysctl tree
106 enum {
107 DEVCLASS_SYSCTL_PARENT,
110 static int
111 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
113 devclass_t dc = (devclass_t)arg1;
114 const char *value;
116 switch (arg2) {
117 case DEVCLASS_SYSCTL_PARENT:
118 value = dc->parent ? dc->parent->name : "";
119 break;
120 default:
121 return (EINVAL);
123 return (SYSCTL_OUT(req, value, strlen(value)));
126 static void
127 devclass_sysctl_init(devclass_t dc)
130 if (dc->sysctl_tree != NULL)
131 return;
132 sysctl_ctx_init(&dc->sysctl_ctx);
133 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
134 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
135 CTLFLAG_RD, NULL, "");
136 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
137 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
138 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
139 "parent class");
142 enum {
143 DEVICE_SYSCTL_DESC,
144 DEVICE_SYSCTL_DRIVER,
145 DEVICE_SYSCTL_LOCATION,
146 DEVICE_SYSCTL_PNPINFO,
147 DEVICE_SYSCTL_PARENT,
150 static int
151 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
153 device_t dev = (device_t)arg1;
154 const char *value;
155 char *buf;
156 int error;
158 buf = NULL;
159 switch (arg2) {
160 case DEVICE_SYSCTL_DESC:
161 value = dev->desc ? dev->desc : "";
162 break;
163 case DEVICE_SYSCTL_DRIVER:
164 value = dev->driver ? dev->driver->name : "";
165 break;
166 case DEVICE_SYSCTL_LOCATION:
167 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
168 bus_child_location_str(dev, buf, 1024);
169 break;
170 case DEVICE_SYSCTL_PNPINFO:
171 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
172 bus_child_pnpinfo_str(dev, buf, 1024);
173 break;
174 case DEVICE_SYSCTL_PARENT:
175 value = dev->parent ? dev->parent->nameunit : "";
176 break;
177 default:
178 return (EINVAL);
180 error = SYSCTL_OUT(req, value, strlen(value));
181 if (buf != NULL)
182 kfree(buf, M_BUS);
183 return (error);
186 static void
187 device_sysctl_init(device_t dev)
189 devclass_t dc = dev->devclass;
191 if (dev->sysctl_tree != NULL)
192 return;
193 devclass_sysctl_init(dc);
194 sysctl_ctx_init(&dev->sysctl_ctx);
195 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
196 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
197 dev->nameunit + strlen(dc->name),
198 CTLFLAG_RD, NULL, "");
199 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
200 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
201 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
202 "device description");
203 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
204 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
205 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
206 "device driver name");
207 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
208 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
209 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
210 "device location relative to parent");
211 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
212 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
213 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
214 "device identification");
215 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
216 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
217 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
218 "parent device");
221 static void
222 device_sysctl_update(device_t dev)
224 devclass_t dc = dev->devclass;
226 if (dev->sysctl_tree == NULL)
227 return;
228 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
231 static void
232 device_sysctl_fini(device_t dev)
234 if (dev->sysctl_tree == NULL)
235 return;
236 sysctl_ctx_free(&dev->sysctl_ctx);
237 dev->sysctl_tree = NULL;
240 static void device_attach_async(device_t dev);
241 static void device_attach_thread(void *arg);
242 static int device_doattach(device_t dev);
244 static int do_async_attach = 0;
245 static int numasyncthreads;
246 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
249 * /dev/devctl implementation
253 * This design allows only one reader for /dev/devctl. This is not desirable
254 * in the long run, but will get a lot of hair out of this implementation.
255 * Maybe we should make this device a clonable device.
257 * Also note: we specifically do not attach a device to the device_t tree
258 * to avoid potential chicken and egg problems. One could argue that all
259 * of this belongs to the root node. One could also further argue that the
260 * sysctl interface that we have not might more properly be an ioctl
261 * interface, but at this stage of the game, I'm not inclined to rock that
262 * boat.
264 * I'm also not sure that the SIGIO support is done correctly or not, as
265 * I copied it from a driver that had SIGIO support that likely hasn't been
266 * tested since 3.4 or 2.2.8!
269 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
270 static int devctl_disable = 0;
271 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
272 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
273 sysctl_devctl_disable, "I", "devctl disable");
275 static d_open_t devopen;
276 static d_close_t devclose;
277 static d_read_t devread;
278 static d_ioctl_t devioctl;
279 static d_kqfilter_t devkqfilter;
281 static struct dev_ops devctl_ops = {
282 { "devctl", 0, D_MPSAFE },
283 .d_open = devopen,
284 .d_close = devclose,
285 .d_read = devread,
286 .d_ioctl = devioctl,
287 .d_kqfilter = devkqfilter
290 struct dev_event_info
292 char *dei_data;
293 TAILQ_ENTRY(dev_event_info) dei_link;
296 TAILQ_HEAD(devq, dev_event_info);
298 static struct dev_softc
300 int inuse;
301 struct lock lock;
302 struct kqinfo kq;
303 struct devq devq;
304 struct proc *async_proc;
305 } devsoftc;
308 * Chicken-and-egg problem with devfs, get the queue operational early.
310 static void
311 predevinit(void)
313 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
314 TAILQ_INIT(&devsoftc.devq);
316 SYSINIT(predevinit, SI_SUB_CREATE_INIT, SI_ORDER_ANY, predevinit, 0);
318 static void
319 devinit(void)
322 * WARNING! make_dev() can call back into devctl_queue_data()
323 * immediately.
325 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
328 static int
329 devopen(struct dev_open_args *ap)
331 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
332 if (devsoftc.inuse) {
333 lockmgr(&devsoftc.lock, LK_RELEASE);
334 return (EBUSY);
336 /* move to init */
337 devsoftc.inuse = 1;
338 devsoftc.async_proc = NULL;
339 lockmgr(&devsoftc.lock, LK_RELEASE);
341 return (0);
344 static int
345 devclose(struct dev_close_args *ap)
347 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
348 devsoftc.inuse = 0;
349 wakeup(&devsoftc);
350 lockmgr(&devsoftc.lock, LK_RELEASE);
352 return (0);
356 * The read channel for this device is used to report changes to
357 * userland in realtime. We are required to free the data as well as
358 * the n1 object because we allocate them separately. Also note that
359 * we return one record at a time. If you try to read this device a
360 * character at a time, you will lose the rest of the data. Listening
361 * programs are expected to cope.
363 static int
364 devread(struct dev_read_args *ap)
366 struct uio *uio = ap->a_uio;
367 struct dev_event_info *n1;
368 int rv;
370 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
371 while (TAILQ_EMPTY(&devsoftc.devq)) {
372 if (ap->a_ioflag & IO_NDELAY) {
373 lockmgr(&devsoftc.lock, LK_RELEASE);
374 return (EAGAIN);
376 tsleep_interlock(&devsoftc, PCATCH);
377 lockmgr(&devsoftc.lock, LK_RELEASE);
378 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
379 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
380 if (rv) {
382 * Need to translate ERESTART to EINTR here? -- jake
384 lockmgr(&devsoftc.lock, LK_RELEASE);
385 return (rv);
388 n1 = TAILQ_FIRST(&devsoftc.devq);
389 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
390 lockmgr(&devsoftc.lock, LK_RELEASE);
391 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
392 kfree(n1->dei_data, M_BUS);
393 kfree(n1, M_BUS);
394 return (rv);
397 static int
398 devioctl(struct dev_ioctl_args *ap)
400 switch (ap->a_cmd) {
402 case FIONBIO:
403 return (0);
404 case FIOASYNC:
405 if (*(int*)ap->a_data)
406 devsoftc.async_proc = curproc;
407 else
408 devsoftc.async_proc = NULL;
409 return (0);
411 /* (un)Support for other fcntl() calls. */
412 case FIOCLEX:
413 case FIONCLEX:
414 case FIONREAD:
415 case FIOSETOWN:
416 case FIOGETOWN:
417 default:
418 break;
420 return (ENOTTY);
423 static void dev_filter_detach(struct knote *);
424 static int dev_filter_read(struct knote *, long);
426 static struct filterops dev_filtops =
427 { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
428 dev_filter_detach, dev_filter_read };
430 static int
431 devkqfilter(struct dev_kqfilter_args *ap)
433 struct knote *kn = ap->a_kn;
434 struct klist *klist;
436 ap->a_result = 0;
437 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
439 switch (kn->kn_filter) {
440 case EVFILT_READ:
441 kn->kn_fop = &dev_filtops;
442 break;
443 default:
444 ap->a_result = EOPNOTSUPP;
445 lockmgr(&devsoftc.lock, LK_RELEASE);
446 return (0);
449 klist = &devsoftc.kq.ki_note;
450 knote_insert(klist, kn);
452 lockmgr(&devsoftc.lock, LK_RELEASE);
454 return (0);
457 static void
458 dev_filter_detach(struct knote *kn)
460 struct klist *klist;
462 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
463 klist = &devsoftc.kq.ki_note;
464 knote_remove(klist, kn);
465 lockmgr(&devsoftc.lock, LK_RELEASE);
468 static int
469 dev_filter_read(struct knote *kn, long hint)
471 int ready = 0;
473 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
474 if (!TAILQ_EMPTY(&devsoftc.devq))
475 ready = 1;
476 lockmgr(&devsoftc.lock, LK_RELEASE);
478 return (ready);
483 * @brief Return whether the userland process is running
485 boolean_t
486 devctl_process_running(void)
488 return (devsoftc.inuse == 1);
492 * @brief Queue data to be read from the devctl device
494 * Generic interface to queue data to the devctl device. It is
495 * assumed that @p data is properly formatted. It is further assumed
496 * that @p data is allocated using the M_BUS malloc type.
498 void
499 devctl_queue_data(char *data)
501 struct dev_event_info *n1 = NULL;
502 struct proc *p;
504 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
505 if (n1 == NULL)
506 return;
507 n1->dei_data = data;
508 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
509 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
510 wakeup(&devsoftc);
511 lockmgr(&devsoftc.lock, LK_RELEASE);
512 KNOTE(&devsoftc.kq.ki_note, 0);
513 p = devsoftc.async_proc;
514 if (p != NULL)
515 ksignal(p, SIGIO);
519 * @brief Send a 'notification' to userland, using standard ways
521 void
522 devctl_notify(const char *system, const char *subsystem, const char *type,
523 const char *data)
525 int len = 0;
526 char *msg;
528 if (system == NULL)
529 return; /* BOGUS! Must specify system. */
530 if (subsystem == NULL)
531 return; /* BOGUS! Must specify subsystem. */
532 if (type == NULL)
533 return; /* BOGUS! Must specify type. */
534 len += strlen(" system=") + strlen(system);
535 len += strlen(" subsystem=") + strlen(subsystem);
536 len += strlen(" type=") + strlen(type);
537 /* add in the data message plus newline. */
538 if (data != NULL)
539 len += strlen(data);
540 len += 3; /* '!', '\n', and NUL */
541 msg = kmalloc(len, M_BUS, M_NOWAIT);
542 if (msg == NULL)
543 return; /* Drop it on the floor */
544 if (data != NULL)
545 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
546 system, subsystem, type, data);
547 else
548 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
549 system, subsystem, type);
550 devctl_queue_data(msg);
554 * Common routine that tries to make sending messages as easy as possible.
555 * We allocate memory for the data, copy strings into that, but do not
556 * free it unless there's an error. The dequeue part of the driver should
557 * free the data. We don't send data when the device is disabled. We do
558 * send data, even when we have no listeners, because we wish to avoid
559 * races relating to startup and restart of listening applications.
561 * devaddq is designed to string together the type of event, with the
562 * object of that event, plus the plug and play info and location info
563 * for that event. This is likely most useful for devices, but less
564 * useful for other consumers of this interface. Those should use
565 * the devctl_queue_data() interface instead.
567 static void
568 devaddq(const char *type, const char *what, device_t dev)
570 char *data = NULL;
571 char *loc = NULL;
572 char *pnp = NULL;
573 const char *parstr;
575 if (devctl_disable)
576 return;
577 data = kmalloc(1024, M_BUS, M_NOWAIT);
578 if (data == NULL)
579 goto bad;
581 /* get the bus specific location of this device */
582 loc = kmalloc(1024, M_BUS, M_NOWAIT);
583 if (loc == NULL)
584 goto bad;
585 *loc = '\0';
586 bus_child_location_str(dev, loc, 1024);
588 /* Get the bus specific pnp info of this device */
589 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
590 if (pnp == NULL)
591 goto bad;
592 *pnp = '\0';
593 bus_child_pnpinfo_str(dev, pnp, 1024);
595 /* Get the parent of this device, or / if high enough in the tree. */
596 if (device_get_parent(dev) == NULL)
597 parstr = "."; /* Or '/' ? */
598 else
599 parstr = device_get_nameunit(device_get_parent(dev));
600 /* String it all together. */
601 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
602 parstr);
603 kfree(loc, M_BUS);
604 kfree(pnp, M_BUS);
605 devctl_queue_data(data);
606 return;
607 bad:
608 if (pnp != NULL)
609 kfree(pnp, M_BUS);
610 if (loc != NULL)
611 kfree(loc, M_BUS);
612 if (loc != NULL)
613 kfree(data, M_BUS);
614 return;
618 * A device was added to the tree. We are called just after it successfully
619 * attaches (that is, probe and attach success for this device). No call
620 * is made if a device is merely parented into the tree. See devnomatch
621 * if probe fails. If attach fails, no notification is sent (but maybe
622 * we should have a different message for this).
624 static void
625 devadded(device_t dev)
627 char *pnp = NULL;
628 char *tmp = NULL;
630 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
631 if (pnp == NULL)
632 goto fail;
633 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
634 if (tmp == NULL)
635 goto fail;
636 *pnp = '\0';
637 bus_child_pnpinfo_str(dev, pnp, 1024);
638 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
639 devaddq("+", tmp, dev);
640 fail:
641 if (pnp != NULL)
642 kfree(pnp, M_BUS);
643 if (tmp != NULL)
644 kfree(tmp, M_BUS);
645 return;
649 * A device was removed from the tree. We are called just before this
650 * happens.
652 static void
653 devremoved(device_t dev)
655 char *pnp = NULL;
656 char *tmp = NULL;
658 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
659 if (pnp == NULL)
660 goto fail;
661 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
662 if (tmp == NULL)
663 goto fail;
664 *pnp = '\0';
665 bus_child_pnpinfo_str(dev, pnp, 1024);
666 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
667 devaddq("-", tmp, dev);
668 fail:
669 if (pnp != NULL)
670 kfree(pnp, M_BUS);
671 if (tmp != NULL)
672 kfree(tmp, M_BUS);
673 return;
677 * Called when there's no match for this device. This is only called
678 * the first time that no match happens, so we don't keep getitng this
679 * message. Should that prove to be undesirable, we can change it.
680 * This is called when all drivers that can attach to a given bus
681 * decline to accept this device. Other errrors may not be detected.
683 static void
684 devnomatch(device_t dev)
686 devaddq("?", "", dev);
689 static int
690 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
692 struct dev_event_info *n1;
693 int dis, error;
695 dis = devctl_disable;
696 error = sysctl_handle_int(oidp, &dis, 0, req);
697 if (error || !req->newptr)
698 return (error);
699 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
700 devctl_disable = dis;
701 if (dis) {
702 while (!TAILQ_EMPTY(&devsoftc.devq)) {
703 n1 = TAILQ_FIRST(&devsoftc.devq);
704 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
705 kfree(n1->dei_data, M_BUS);
706 kfree(n1, M_BUS);
709 lockmgr(&devsoftc.lock, LK_RELEASE);
710 return (0);
713 /* End of /dev/devctl code */
715 TAILQ_HEAD(,bsd_device) bus_data_devices;
716 static int bus_data_generation = 1;
718 kobj_method_t null_methods[] = {
719 { 0, 0 }
722 DEFINE_CLASS(null, null_methods, 0);
725 * Devclass implementation
728 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
730 static devclass_t
731 devclass_find_internal(const char *classname, const char *parentname,
732 int create)
734 devclass_t dc;
736 PDEBUG(("looking for %s", classname));
737 if (classname == NULL)
738 return(NULL);
740 TAILQ_FOREACH(dc, &devclasses, link)
741 if (!strcmp(dc->name, classname))
742 break;
744 if (create && !dc) {
745 PDEBUG(("creating %s", classname));
746 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
747 M_BUS, M_INTWAIT | M_ZERO);
748 dc->parent = NULL;
749 dc->name = (char*) (dc + 1);
750 strcpy(dc->name, classname);
751 dc->devices = NULL;
752 dc->maxunit = 0;
753 TAILQ_INIT(&dc->drivers);
754 TAILQ_INSERT_TAIL(&devclasses, dc, link);
756 bus_data_generation_update();
761 * If a parent class is specified, then set that as our parent so
762 * that this devclass will support drivers for the parent class as
763 * well. If the parent class has the same name don't do this though
764 * as it creates a cycle that can trigger an infinite loop in
765 * device_probe_child() if a device exists for which there is no
766 * suitable driver.
768 if (parentname && dc && !dc->parent &&
769 strcmp(classname, parentname) != 0)
770 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
772 return(dc);
775 devclass_t
776 devclass_create(const char *classname)
778 return(devclass_find_internal(classname, NULL, TRUE));
781 devclass_t
782 devclass_find(const char *classname)
784 return(devclass_find_internal(classname, NULL, FALSE));
787 device_t
788 devclass_find_unit(const char *classname, int unit)
790 devclass_t dc;
792 if ((dc = devclass_find(classname)) != NULL)
793 return(devclass_get_device(dc, unit));
794 return (NULL);
798 devclass_add_driver(devclass_t dc, driver_t *driver)
800 driverlink_t dl;
801 device_t dev;
802 int i;
804 PDEBUG(("%s", DRIVERNAME(driver)));
806 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
809 * Compile the driver's methods. Also increase the reference count
810 * so that the class doesn't get freed when the last instance
811 * goes. This means we can safely use static methods and avoids a
812 * double-free in devclass_delete_driver.
814 kobj_class_instantiate(driver);
817 * Make sure the devclass which the driver is implementing exists.
819 devclass_find_internal(driver->name, NULL, TRUE);
821 dl->driver = driver;
822 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
825 * Call BUS_DRIVER_ADDED for any existing busses in this class,
826 * but only if the bus has already been attached (otherwise we
827 * might probe too early).
829 * This is what will cause a newly loaded module to be associated
830 * with hardware. bus_generic_driver_added() is typically what ends
831 * up being called.
833 for (i = 0; i < dc->maxunit; i++) {
834 if ((dev = dc->devices[i]) != NULL) {
835 if (dev->state >= DS_ATTACHED)
836 BUS_DRIVER_ADDED(dev, driver);
840 bus_data_generation_update();
841 return(0);
845 devclass_delete_driver(devclass_t busclass, driver_t *driver)
847 devclass_t dc = devclass_find(driver->name);
848 driverlink_t dl;
849 device_t dev;
850 int i;
851 int error;
853 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
855 if (!dc)
856 return(0);
859 * Find the link structure in the bus' list of drivers.
861 TAILQ_FOREACH(dl, &busclass->drivers, link)
862 if (dl->driver == driver)
863 break;
865 if (!dl) {
866 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
867 return(ENOENT);
871 * Disassociate from any devices. We iterate through all the
872 * devices in the devclass of the driver and detach any which are
873 * using the driver and which have a parent in the devclass which
874 * we are deleting from.
876 * Note that since a driver can be in multiple devclasses, we
877 * should not detach devices which are not children of devices in
878 * the affected devclass.
880 for (i = 0; i < dc->maxunit; i++)
881 if (dc->devices[i]) {
882 dev = dc->devices[i];
883 if (dev->driver == driver && dev->parent &&
884 dev->parent->devclass == busclass) {
885 if ((error = device_detach(dev)) != 0)
886 return(error);
887 device_set_driver(dev, NULL);
891 TAILQ_REMOVE(&busclass->drivers, dl, link);
892 kfree(dl, M_BUS);
894 kobj_class_uninstantiate(driver);
896 bus_data_generation_update();
897 return(0);
900 static driverlink_t
901 devclass_find_driver_internal(devclass_t dc, const char *classname)
903 driverlink_t dl;
905 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
907 TAILQ_FOREACH(dl, &dc->drivers, link)
908 if (!strcmp(dl->driver->name, classname))
909 return(dl);
911 PDEBUG(("not found"));
912 return(NULL);
915 kobj_class_t
916 devclass_find_driver(devclass_t dc, const char *classname)
918 driverlink_t dl;
920 dl = devclass_find_driver_internal(dc, classname);
921 if (dl)
922 return(dl->driver);
923 else
924 return(NULL);
927 const char *
928 devclass_get_name(devclass_t dc)
930 return(dc->name);
933 device_t
934 devclass_get_device(devclass_t dc, int unit)
936 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
937 return(NULL);
938 return(dc->devices[unit]);
941 void *
942 devclass_get_softc(devclass_t dc, int unit)
944 device_t dev;
946 dev = devclass_get_device(dc, unit);
947 if (!dev)
948 return(NULL);
950 return(device_get_softc(dev));
954 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
956 int i;
957 int count;
958 device_t *list;
960 count = 0;
961 for (i = 0; i < dc->maxunit; i++)
962 if (dc->devices[i])
963 count++;
965 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
967 count = 0;
968 for (i = 0; i < dc->maxunit; i++)
969 if (dc->devices[i]) {
970 list[count] = dc->devices[i];
971 count++;
974 *devlistp = list;
975 *devcountp = count;
977 return(0);
981 * @brief Get a list of drivers in the devclass
983 * An array containing a list of pointers to all the drivers in the
984 * given devclass is allocated and returned in @p *listp. The number
985 * of drivers in the array is returned in @p *countp. The caller should
986 * free the array using @c free(p, M_TEMP).
988 * @param dc the devclass to examine
989 * @param listp gives location for array pointer return value
990 * @param countp gives location for number of array elements
991 * return value
993 * @retval 0 success
994 * @retval ENOMEM the array allocation failed
997 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
999 driverlink_t dl;
1000 driver_t **list;
1001 int count;
1003 count = 0;
1004 TAILQ_FOREACH(dl, &dc->drivers, link)
1005 count++;
1006 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
1007 if (list == NULL)
1008 return (ENOMEM);
1010 count = 0;
1011 TAILQ_FOREACH(dl, &dc->drivers, link) {
1012 list[count] = dl->driver;
1013 count++;
1015 *listp = list;
1016 *countp = count;
1018 return (0);
1022 * @brief Get the number of devices in a devclass
1024 * @param dc the devclass to examine
1027 devclass_get_count(devclass_t dc)
1029 int count, i;
1031 count = 0;
1032 for (i = 0; i < dc->maxunit; i++)
1033 if (dc->devices[i])
1034 count++;
1035 return (count);
1039 devclass_get_maxunit(devclass_t dc)
1041 return(dc->maxunit);
1044 void
1045 devclass_set_parent(devclass_t dc, devclass_t pdc)
1047 dc->parent = pdc;
1050 devclass_t
1051 devclass_get_parent(devclass_t dc)
1053 return(dc->parent);
1056 static int
1057 devclass_alloc_unit(devclass_t dc, int *unitp)
1059 int unit = *unitp;
1061 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1063 /* If we have been given a wired unit number, check for existing device */
1064 if (unit != -1) {
1065 if (unit >= 0 && unit < dc->maxunit &&
1066 dc->devices[unit] != NULL) {
1067 if (bootverbose)
1068 kprintf("%s-: %s%d exists, using next available unit number\n",
1069 dc->name, dc->name, unit);
1070 /* find the next available slot */
1071 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
1074 } else {
1075 /* Unwired device, find the next available slot for it */
1076 unit = 0;
1077 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1078 unit++;
1082 * We've selected a unit beyond the length of the table, so let's
1083 * extend the table to make room for all units up to and including
1084 * this one.
1086 if (unit >= dc->maxunit) {
1087 device_t *newlist;
1088 int newsize;
1090 newsize = (unit + 1);
1091 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
1092 M_INTWAIT | M_ZERO);
1093 if (newlist == NULL)
1094 return(ENOMEM);
1096 * WARNING: Due to gcc builtin optimization,
1097 * calling bcopy causes gcc to assume
1098 * that the source and destination args
1099 * cannot be NULL and optimize-away later
1100 * conditional tests to determine if dc->devices
1101 * is NULL. In this situation, in fact,
1102 * dc->devices CAN be NULL w/ maxunit == 0.
1104 if (dc->devices) {
1105 bcopy(dc->devices,
1106 newlist,
1107 sizeof(device_t) * dc->maxunit);
1108 kfree(dc->devices, M_BUS);
1110 dc->devices = newlist;
1111 dc->maxunit = newsize;
1113 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1115 *unitp = unit;
1116 return(0);
1119 static int
1120 devclass_add_device(devclass_t dc, device_t dev)
1122 int buflen, error;
1124 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1126 buflen = strlen(dc->name) + 5;
1127 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1128 if (dev->nameunit == NULL)
1129 return(ENOMEM);
1131 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1132 kfree(dev->nameunit, M_BUS);
1133 dev->nameunit = NULL;
1134 return(error);
1136 dc->devices[dev->unit] = dev;
1137 dev->devclass = dc;
1138 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1140 return(0);
1143 static int
1144 devclass_delete_device(devclass_t dc, device_t dev)
1146 if (!dc || !dev)
1147 return(0);
1149 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1151 if (dev->devclass != dc || dc->devices[dev->unit] != dev) {
1152 panic("devclass_delete_device: inconsistent device class: "
1153 "%p/%p %d %p/%p\n", dev->devclass, dc, dev->unit,
1154 dc->devices[dev->unit], dev);
1156 dc->devices[dev->unit] = NULL;
1157 if (dev->flags & DF_WILDCARD)
1158 dev->unit = -1;
1159 dev->devclass = NULL;
1160 kfree(dev->nameunit, M_BUS);
1161 dev->nameunit = NULL;
1163 return(0);
1166 static device_t
1167 make_device(device_t parent, const char *name, int unit)
1169 device_t dev;
1170 devclass_t dc;
1172 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1174 if (name != NULL) {
1175 dc = devclass_find_internal(name, NULL, TRUE);
1176 if (!dc) {
1177 kprintf("make_device: can't find device class %s\n", name);
1178 return(NULL);
1180 } else
1181 dc = NULL;
1183 dev = kmalloc(sizeof(struct bsd_device), M_BUS, M_INTWAIT | M_ZERO);
1184 if (!dev)
1185 return(0);
1187 dev->parent = parent;
1188 TAILQ_INIT(&dev->children);
1189 kobj_init((kobj_t) dev, &null_class);
1190 dev->driver = NULL;
1191 dev->devclass = NULL;
1192 dev->unit = unit;
1193 dev->nameunit = NULL;
1194 dev->desc = NULL;
1195 dev->busy = 0;
1196 dev->devflags = 0;
1197 dev->flags = DF_ENABLED;
1198 dev->order = 0;
1199 if (unit == -1)
1200 dev->flags |= DF_WILDCARD;
1201 if (name) {
1202 dev->flags |= DF_FIXEDCLASS;
1203 if (devclass_add_device(dc, dev) != 0) {
1204 kobj_delete((kobj_t)dev, M_BUS);
1205 return(NULL);
1208 dev->ivars = NULL;
1209 dev->softc = NULL;
1211 dev->state = DS_NOTPRESENT;
1213 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1214 bus_data_generation_update();
1216 return(dev);
1219 static int
1220 device_print_child(device_t dev, device_t child)
1222 int retval = 0;
1224 if (device_is_alive(child))
1225 retval += BUS_PRINT_CHILD(dev, child);
1226 else
1227 retval += device_printf(child, " not found\n");
1229 return(retval);
1232 device_t
1233 device_add_child(device_t dev, const char *name, int unit)
1235 return device_add_child_ordered(dev, 0, name, unit);
1238 device_t
1239 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1241 device_t child;
1242 device_t place;
1244 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1245 order, unit));
1247 child = make_device(dev, name, unit);
1248 if (child == NULL)
1249 return child;
1250 child->order = order;
1252 TAILQ_FOREACH(place, &dev->children, link) {
1253 if (place->order > order)
1254 break;
1257 if (place) {
1259 * The device 'place' is the first device whose order is
1260 * greater than the new child.
1262 TAILQ_INSERT_BEFORE(place, child, link);
1263 } else {
1265 * The new child's order is greater or equal to the order of
1266 * any existing device. Add the child to the tail of the list.
1268 TAILQ_INSERT_TAIL(&dev->children, child, link);
1271 bus_data_generation_update();
1272 return(child);
1276 device_delete_child(device_t dev, device_t child)
1278 int error;
1279 device_t grandchild;
1281 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1283 /* remove children first */
1284 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1285 error = device_delete_child(child, grandchild);
1286 if (error)
1287 return(error);
1290 if ((error = device_detach(child)) != 0)
1291 return(error);
1292 if (child->devclass)
1293 devclass_delete_device(child->devclass, child);
1294 TAILQ_REMOVE(&dev->children, child, link);
1295 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1296 kobj_delete((kobj_t)child, M_BUS);
1298 bus_data_generation_update();
1299 return(0);
1303 * @brief Delete all children devices of the given device, if any.
1305 * This function deletes all children devices of the given device, if
1306 * any, using the device_delete_child() function for each device it
1307 * finds. If a child device cannot be deleted, this function will
1308 * return an error code.
1310 * @param dev the parent device
1312 * @retval 0 success
1313 * @retval non-zero a device would not detach
1316 device_delete_children(device_t dev)
1318 device_t child;
1319 int error;
1321 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1323 error = 0;
1325 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1326 error = device_delete_child(dev, child);
1327 if (error) {
1328 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1329 break;
1332 return (error);
1336 * @brief Find a device given a unit number
1338 * This is similar to devclass_get_devices() but only searches for
1339 * devices which have @p dev as a parent.
1341 * @param dev the parent device to search
1342 * @param unit the unit number to search for. If the unit is -1,
1343 * return the first child of @p dev which has name
1344 * @p classname (that is, the one with the lowest unit.)
1346 * @returns the device with the given unit number or @c
1347 * NULL if there is no such device
1349 device_t
1350 device_find_child(device_t dev, const char *classname, int unit)
1352 devclass_t dc;
1353 device_t child;
1355 dc = devclass_find(classname);
1356 if (!dc)
1357 return(NULL);
1359 if (unit != -1) {
1360 child = devclass_get_device(dc, unit);
1361 if (child && child->parent == dev)
1362 return (child);
1363 } else {
1364 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1365 child = devclass_get_device(dc, unit);
1366 if (child && child->parent == dev)
1367 return (child);
1370 return(NULL);
1373 static driverlink_t
1374 first_matching_driver(devclass_t dc, device_t dev)
1376 if (dev->devclass)
1377 return(devclass_find_driver_internal(dc, dev->devclass->name));
1378 else
1379 return(TAILQ_FIRST(&dc->drivers));
1382 static driverlink_t
1383 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1385 if (dev->devclass) {
1386 driverlink_t dl;
1387 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1388 if (!strcmp(dev->devclass->name, dl->driver->name))
1389 return(dl);
1390 return(NULL);
1391 } else
1392 return(TAILQ_NEXT(last, link));
1396 device_probe_child(device_t dev, device_t child)
1398 devclass_t dc;
1399 driverlink_t best = NULL;
1400 driverlink_t dl;
1401 int result, pri = 0;
1402 int hasclass = (child->devclass != NULL);
1404 dc = dev->devclass;
1405 if (!dc)
1406 panic("device_probe_child: parent device has no devclass");
1408 if (child->state == DS_ALIVE)
1409 return(0);
1411 for (; dc; dc = dc->parent) {
1412 for (dl = first_matching_driver(dc, child); dl;
1413 dl = next_matching_driver(dc, child, dl)) {
1414 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1415 device_set_driver(child, dl->driver);
1416 if (!hasclass)
1417 device_set_devclass(child, dl->driver->name);
1418 result = DEVICE_PROBE(child);
1419 if (!hasclass)
1420 device_set_devclass(child, 0);
1423 * If the driver returns SUCCESS, there can be
1424 * no higher match for this device.
1426 if (result == 0) {
1427 best = dl;
1428 pri = 0;
1429 break;
1433 * The driver returned an error so it
1434 * certainly doesn't match.
1436 if (result > 0) {
1437 device_set_driver(child, NULL);
1438 continue;
1442 * A priority lower than SUCCESS, remember the
1443 * best matching driver. Initialise the value
1444 * of pri for the first match.
1446 if (best == NULL || result > pri) {
1447 best = dl;
1448 pri = result;
1449 continue;
1453 * If we have unambiguous match in this devclass,
1454 * don't look in the parent.
1456 if (best && pri == 0)
1457 break;
1461 * If we found a driver, change state and initialise the devclass.
1463 if (best) {
1464 if (!child->devclass)
1465 device_set_devclass(child, best->driver->name);
1466 device_set_driver(child, best->driver);
1467 if (pri < 0) {
1469 * A bit bogus. Call the probe method again to make
1470 * sure that we have the right description.
1472 DEVICE_PROBE(child);
1475 bus_data_generation_update();
1476 child->state = DS_ALIVE;
1477 return(0);
1480 return(ENXIO);
1484 device_probe_child_gpri(device_t dev, device_t child, u_int gpri)
1486 devclass_t dc;
1487 driverlink_t best = NULL;
1488 driverlink_t dl;
1489 int result, pri = 0;
1490 int hasclass = (child->devclass != NULL);
1492 dc = dev->devclass;
1493 if (!dc)
1494 panic("device_probe_child: parent device has no devclass");
1496 if (child->state == DS_ALIVE)
1497 return(0);
1499 for (; dc; dc = dc->parent) {
1500 for (dl = first_matching_driver(dc, child); dl;
1501 dl = next_matching_driver(dc, child, dl)) {
1503 * GPRI handling, only probe drivers with the
1504 * specific GPRI.
1506 if (dl->driver->gpri != gpri)
1507 continue;
1509 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1510 device_set_driver(child, dl->driver);
1511 if (!hasclass)
1512 device_set_devclass(child, dl->driver->name);
1513 result = DEVICE_PROBE(child);
1514 if (!hasclass)
1515 device_set_devclass(child, 0);
1518 * If the driver returns SUCCESS, there can be
1519 * no higher match for this device.
1521 if (result == 0) {
1522 best = dl;
1523 pri = 0;
1524 break;
1528 * The driver returned an error so it
1529 * certainly doesn't match.
1531 if (result > 0) {
1532 device_set_driver(child, NULL);
1533 continue;
1537 * A priority lower than SUCCESS, remember the
1538 * best matching driver. Initialise the value
1539 * of pri for the first match.
1541 if (best == NULL || result > pri) {
1542 best = dl;
1543 pri = result;
1544 continue;
1548 * If we have unambiguous match in this devclass,
1549 * don't look in the parent.
1551 if (best && pri == 0)
1552 break;
1556 * If we found a driver, change state and initialise the devclass.
1558 if (best) {
1559 if (!child->devclass)
1560 device_set_devclass(child, best->driver->name);
1561 device_set_driver(child, best->driver);
1562 if (pri < 0) {
1564 * A bit bogus. Call the probe method again to make
1565 * sure that we have the right description.
1567 DEVICE_PROBE(child);
1570 bus_data_generation_update();
1571 child->state = DS_ALIVE;
1572 return(0);
1575 return(ENXIO);
1578 device_t
1579 device_get_parent(device_t dev)
1581 return dev->parent;
1585 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1587 int count;
1588 device_t child;
1589 device_t *list;
1591 count = 0;
1592 TAILQ_FOREACH(child, &dev->children, link)
1593 count++;
1595 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1597 count = 0;
1598 TAILQ_FOREACH(child, &dev->children, link) {
1599 list[count] = child;
1600 count++;
1603 *devlistp = list;
1604 *devcountp = count;
1606 return(0);
1609 driver_t *
1610 device_get_driver(device_t dev)
1612 return(dev->driver);
1615 devclass_t
1616 device_get_devclass(device_t dev)
1618 return(dev->devclass);
1621 const char *
1622 device_get_name(device_t dev)
1624 if (dev->devclass)
1625 return devclass_get_name(dev->devclass);
1626 return(NULL);
1629 const char *
1630 device_get_nameunit(device_t dev)
1632 return(dev->nameunit);
1636 device_get_unit(device_t dev)
1638 return(dev->unit);
1641 const char *
1642 device_get_desc(device_t dev)
1644 return(dev->desc);
1647 uint32_t
1648 device_get_flags(device_t dev)
1650 return(dev->devflags);
1653 struct sysctl_ctx_list *
1654 device_get_sysctl_ctx(device_t dev)
1656 return (&dev->sysctl_ctx);
1659 struct sysctl_oid *
1660 device_get_sysctl_tree(device_t dev)
1662 return (dev->sysctl_tree);
1666 device_print_prettyname(device_t dev)
1668 const char *name = device_get_name(dev);
1670 if (name == NULL)
1671 return kprintf("unknown: ");
1672 else
1673 return kprintf("%s%d: ", name, device_get_unit(dev));
1677 device_printf(device_t dev, const char * fmt, ...)
1679 __va_list ap;
1680 int retval;
1682 retval = device_print_prettyname(dev);
1683 __va_start(ap, fmt);
1684 retval += kvprintf(fmt, ap);
1685 __va_end(ap);
1686 return retval;
1689 static void
1690 device_set_desc_internal(device_t dev, const char* desc, int copy)
1692 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1693 kfree(dev->desc, M_BUS);
1694 dev->flags &= ~DF_DESCMALLOCED;
1695 dev->desc = NULL;
1698 if (copy && desc) {
1699 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1700 if (dev->desc) {
1701 strcpy(dev->desc, desc);
1702 dev->flags |= DF_DESCMALLOCED;
1704 } else {
1705 /* Avoid a -Wcast-qual warning */
1706 dev->desc = (char *)(uintptr_t) desc;
1709 bus_data_generation_update();
1712 void
1713 device_set_desc(device_t dev, const char* desc)
1715 device_set_desc_internal(dev, desc, FALSE);
1718 void
1719 device_set_desc_copy(device_t dev, const char* desc)
1721 device_set_desc_internal(dev, desc, TRUE);
1724 void
1725 device_set_flags(device_t dev, uint32_t flags)
1727 dev->devflags = flags;
1730 void *
1731 device_get_softc(device_t dev)
1733 return dev->softc;
1736 void
1737 device_set_softc(device_t dev, void *softc)
1739 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1740 kfree(dev->softc, M_BUS);
1741 dev->softc = softc;
1742 if (dev->softc)
1743 dev->flags |= DF_EXTERNALSOFTC;
1744 else
1745 dev->flags &= ~DF_EXTERNALSOFTC;
1748 void
1749 device_set_async_attach(device_t dev, int enable)
1751 if (enable)
1752 dev->flags |= DF_ASYNCPROBE;
1753 else
1754 dev->flags &= ~DF_ASYNCPROBE;
1757 void *
1758 device_get_ivars(device_t dev)
1760 return dev->ivars;
1763 void
1764 device_set_ivars(device_t dev, void * ivars)
1766 if (!dev)
1767 return;
1769 dev->ivars = ivars;
1772 device_state_t
1773 device_get_state(device_t dev)
1775 return(dev->state);
1778 void
1779 device_enable(device_t dev)
1781 dev->flags |= DF_ENABLED;
1784 void
1785 device_disable(device_t dev)
1787 dev->flags &= ~DF_ENABLED;
1791 * YYY cannot block
1793 void
1794 device_busy(device_t dev)
1796 if (dev->state < DS_ATTACHED)
1797 panic("device_busy: called for unattached device");
1798 if (dev->busy == 0 && dev->parent)
1799 device_busy(dev->parent);
1800 dev->busy++;
1801 dev->state = DS_BUSY;
1805 * YYY cannot block
1807 void
1808 device_unbusy(device_t dev)
1810 if (dev->state != DS_BUSY)
1811 panic("device_unbusy: called for non-busy device");
1812 dev->busy--;
1813 if (dev->busy == 0) {
1814 if (dev->parent)
1815 device_unbusy(dev->parent);
1816 dev->state = DS_ATTACHED;
1820 void
1821 device_quiet(device_t dev)
1823 dev->flags |= DF_QUIET;
1826 void
1827 device_verbose(device_t dev)
1829 dev->flags &= ~DF_QUIET;
1833 device_is_quiet(device_t dev)
1835 return((dev->flags & DF_QUIET) != 0);
1839 device_is_enabled(device_t dev)
1841 return((dev->flags & DF_ENABLED) != 0);
1845 device_is_alive(device_t dev)
1847 return(dev->state >= DS_ALIVE);
1851 device_is_attached(device_t dev)
1853 return(dev->state >= DS_ATTACHED);
1857 device_set_devclass(device_t dev, const char *classname)
1859 devclass_t dc;
1860 int error;
1862 if (!classname) {
1863 if (dev->devclass)
1864 devclass_delete_device(dev->devclass, dev);
1865 return(0);
1868 if (dev->devclass) {
1869 kprintf("device_set_devclass: device class already set\n");
1870 return(EINVAL);
1873 dc = devclass_find_internal(classname, NULL, TRUE);
1874 if (!dc)
1875 return(ENOMEM);
1877 error = devclass_add_device(dc, dev);
1879 bus_data_generation_update();
1880 return(error);
1884 device_set_driver(device_t dev, driver_t *driver)
1886 if (dev->state >= DS_ATTACHED)
1887 return(EBUSY);
1889 if (dev->driver == driver)
1890 return(0);
1892 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1893 kfree(dev->softc, M_BUS);
1894 dev->softc = NULL;
1896 device_set_desc(dev, NULL);
1897 kobj_delete((kobj_t) dev, 0);
1898 dev->driver = driver;
1899 if (driver) {
1900 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1901 if (!(dev->flags & DF_EXTERNALSOFTC))
1902 dev->softc = kmalloc(driver->size, M_BUS,
1903 M_INTWAIT | M_ZERO);
1904 } else {
1905 kobj_init((kobj_t) dev, &null_class);
1908 bus_data_generation_update();
1909 return(0);
1913 device_probe_and_attach(device_t dev)
1915 device_t bus = dev->parent;
1916 int error = 0;
1918 if (dev->state >= DS_ALIVE)
1919 return(0);
1921 if ((dev->flags & DF_ENABLED) == 0) {
1922 if (bootverbose) {
1923 device_print_prettyname(dev);
1924 kprintf("not probed (disabled)\n");
1926 return(0);
1929 error = device_probe_child(bus, dev);
1930 if (error) {
1931 if (!(dev->flags & DF_DONENOMATCH)) {
1932 BUS_PROBE_NOMATCH(bus, dev);
1933 devnomatch(dev);
1934 dev->flags |= DF_DONENOMATCH;
1936 return(error);
1940 * Output the exact device chain prior to the attach in case the
1941 * system locks up during attach, and generate the full info after
1942 * the attach so correct irq and other information is displayed.
1944 if (bootverbose && !device_is_quiet(dev)) {
1945 device_t tmp;
1947 kprintf("%s", device_get_nameunit(dev));
1948 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1949 kprintf(".%s", device_get_nameunit(tmp));
1950 kprintf("\n");
1952 if (!device_is_quiet(dev))
1953 device_print_child(bus, dev);
1954 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1955 kprintf("%s: probing asynchronously\n",
1956 device_get_nameunit(dev));
1957 dev->state = DS_INPROGRESS;
1958 device_attach_async(dev);
1959 error = 0;
1960 } else {
1961 error = device_doattach(dev);
1963 return(error);
1967 device_probe_and_attach_gpri(device_t dev, u_int gpri)
1969 device_t bus = dev->parent;
1970 int error = 0;
1972 if (dev->state >= DS_ALIVE)
1973 return(0);
1975 if ((dev->flags & DF_ENABLED) == 0) {
1976 if (bootverbose) {
1977 device_print_prettyname(dev);
1978 kprintf("not probed (disabled)\n");
1980 return(0);
1983 error = device_probe_child_gpri(bus, dev, gpri);
1984 if (error) {
1985 #if 0
1986 if (!(dev->flags & DF_DONENOMATCH)) {
1987 BUS_PROBE_NOMATCH(bus, dev);
1988 devnomatch(dev);
1989 dev->flags |= DF_DONENOMATCH;
1991 #endif
1992 return(error);
1996 * Output the exact device chain prior to the attach in case the
1997 * system locks up during attach, and generate the full info after
1998 * the attach so correct irq and other information is displayed.
2000 if (bootverbose && !device_is_quiet(dev)) {
2001 device_t tmp;
2003 kprintf("%s", device_get_nameunit(dev));
2004 for (tmp = dev->parent; tmp; tmp = tmp->parent)
2005 kprintf(".%s", device_get_nameunit(tmp));
2006 kprintf("\n");
2008 if (!device_is_quiet(dev))
2009 device_print_child(bus, dev);
2010 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
2011 kprintf("%s: probing asynchronously\n",
2012 device_get_nameunit(dev));
2013 dev->state = DS_INPROGRESS;
2014 device_attach_async(dev);
2015 error = 0;
2016 } else {
2017 error = device_doattach(dev);
2019 return(error);
2023 * Device is known to be alive, do the attach asynchronously.
2024 * However, serialize the attaches with the mp lock.
2026 static void
2027 device_attach_async(device_t dev)
2029 thread_t td;
2031 atomic_add_int(&numasyncthreads, 1);
2032 lwkt_create(device_attach_thread, dev, &td, NULL,
2033 0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
2036 static void
2037 device_attach_thread(void *arg)
2039 device_t dev = arg;
2041 (void)device_doattach(dev);
2042 atomic_subtract_int(&numasyncthreads, 1);
2043 wakeup(&numasyncthreads);
2047 * Device is known to be alive, do the attach (synchronous or asynchronous)
2049 static int
2050 device_doattach(device_t dev)
2052 device_t bus = dev->parent;
2053 int hasclass = (dev->devclass != NULL);
2054 int error;
2056 device_sysctl_init(dev);
2057 error = DEVICE_ATTACH(dev);
2058 if (error == 0) {
2059 dev->state = DS_ATTACHED;
2060 if (bootverbose && !device_is_quiet(dev))
2061 device_print_child(bus, dev);
2062 device_sysctl_update(dev);
2063 devadded(dev);
2064 } else {
2065 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
2066 dev->driver->name, dev->unit, error);
2067 /* Unset the class that was set in device_probe_child */
2068 if (!hasclass)
2069 device_set_devclass(dev, 0);
2070 device_set_driver(dev, NULL);
2071 dev->state = DS_NOTPRESENT;
2072 device_sysctl_fini(dev);
2074 return(error);
2078 device_detach(device_t dev)
2080 int error;
2082 PDEBUG(("%s", DEVICENAME(dev)));
2083 if (dev->state == DS_BUSY)
2084 return(EBUSY);
2085 if (dev->state != DS_ATTACHED)
2086 return(0);
2088 if ((error = DEVICE_DETACH(dev)) != 0)
2089 return(error);
2090 devremoved(dev);
2091 device_printf(dev, "detached\n");
2092 if (dev->parent)
2093 BUS_CHILD_DETACHED(dev->parent, dev);
2095 if (!(dev->flags & DF_FIXEDCLASS))
2096 devclass_delete_device(dev->devclass, dev);
2098 dev->state = DS_NOTPRESENT;
2099 device_set_driver(dev, NULL);
2100 device_sysctl_fini(dev);
2102 return(0);
2106 device_shutdown(device_t dev)
2108 if (dev->state < DS_ATTACHED)
2109 return 0;
2110 PDEBUG(("%s", DEVICENAME(dev)));
2111 return DEVICE_SHUTDOWN(dev);
2115 device_set_unit(device_t dev, int unit)
2117 devclass_t dc;
2118 int err;
2120 dc = device_get_devclass(dev);
2121 if (unit < dc->maxunit && dc->devices[unit])
2122 return(EBUSY);
2123 err = devclass_delete_device(dc, dev);
2124 if (err)
2125 return(err);
2126 dev->unit = unit;
2127 err = devclass_add_device(dc, dev);
2128 if (err)
2129 return(err);
2131 bus_data_generation_update();
2132 return(0);
2135 /*======================================*/
2137 * Access functions for device resources.
2140 /* Supplied by config(8) in ioconf.c */
2141 extern struct config_device config_devtab[];
2142 extern int devtab_count;
2144 /* Runtime version */
2145 struct config_device *devtab = config_devtab;
2147 static int
2148 resource_new_name(const char *name, int unit)
2150 struct config_device *new;
2152 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
2153 M_INTWAIT | M_ZERO);
2154 if (devtab && devtab_count > 0)
2155 bcopy(devtab, new, devtab_count * sizeof(*new));
2156 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
2157 if (new[devtab_count].name == NULL) {
2158 kfree(new, M_TEMP);
2159 return(-1);
2161 strcpy(new[devtab_count].name, name);
2162 new[devtab_count].unit = unit;
2163 new[devtab_count].resource_count = 0;
2164 new[devtab_count].resources = NULL;
2165 if (devtab && devtab != config_devtab)
2166 kfree(devtab, M_TEMP);
2167 devtab = new;
2168 return devtab_count++;
2171 static int
2172 resource_new_resname(int j, const char *resname, resource_type type)
2174 struct config_resource *new;
2175 int i;
2177 i = devtab[j].resource_count;
2178 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
2179 if (devtab[j].resources && i > 0)
2180 bcopy(devtab[j].resources, new, i * sizeof(*new));
2181 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
2182 if (new[i].name == NULL) {
2183 kfree(new, M_TEMP);
2184 return(-1);
2186 strcpy(new[i].name, resname);
2187 new[i].type = type;
2188 if (devtab[j].resources)
2189 kfree(devtab[j].resources, M_TEMP);
2190 devtab[j].resources = new;
2191 devtab[j].resource_count = i + 1;
2192 return(i);
2195 static int
2196 resource_match_string(int i, const char *resname, const char *value)
2198 int j;
2199 struct config_resource *res;
2201 for (j = 0, res = devtab[i].resources;
2202 j < devtab[i].resource_count; j++, res++)
2203 if (!strcmp(res->name, resname)
2204 && res->type == RES_STRING
2205 && !strcmp(res->u.stringval, value))
2206 return(j);
2207 return(-1);
2210 static int
2211 resource_find(const char *name, int unit, const char *resname,
2212 struct config_resource **result)
2214 int i, j;
2215 struct config_resource *res;
2218 * First check specific instances, then generic.
2220 for (i = 0; i < devtab_count; i++) {
2221 if (devtab[i].unit < 0)
2222 continue;
2223 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2224 res = devtab[i].resources;
2225 for (j = 0; j < devtab[i].resource_count; j++, res++)
2226 if (!strcmp(res->name, resname)) {
2227 *result = res;
2228 return(0);
2232 for (i = 0; i < devtab_count; i++) {
2233 if (devtab[i].unit >= 0)
2234 continue;
2235 /* XXX should this `&& devtab[i].unit == unit' be here? */
2236 /* XXX if so, then the generic match does nothing */
2237 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2238 res = devtab[i].resources;
2239 for (j = 0; j < devtab[i].resource_count; j++, res++)
2240 if (!strcmp(res->name, resname)) {
2241 *result = res;
2242 return(0);
2246 return(ENOENT);
2249 static int
2250 resource_kenv(const char *name, int unit, const char *resname, long *result)
2252 const char *env;
2253 char buf[64];
2256 * DragonFly style loader.conf hinting
2258 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2259 if ((env = kgetenv(buf)) != NULL) {
2260 *result = strtol(env, NULL, 0);
2261 return(0);
2265 * Also support FreeBSD style loader.conf hinting
2267 ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2268 if ((env = kgetenv(buf)) != NULL) {
2269 *result = strtol(env, NULL, 0);
2270 return(0);
2273 return (ENOENT);
2277 resource_int_value(const char *name, int unit, const char *resname, int *result)
2279 struct config_resource *res;
2280 long kvalue = 0;
2281 int error;
2283 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2284 *result = (int)kvalue;
2285 return 0;
2287 if ((error = resource_find(name, unit, resname, &res)) != 0)
2288 return(error);
2289 if (res->type != RES_INT)
2290 return(EFTYPE);
2291 *result = res->u.intval;
2292 return(0);
2296 resource_long_value(const char *name, int unit, const char *resname,
2297 long *result)
2299 struct config_resource *res;
2300 long kvalue;
2301 int error;
2303 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2304 *result = kvalue;
2305 return 0;
2307 if ((error = resource_find(name, unit, resname, &res)) != 0)
2308 return(error);
2309 if (res->type != RES_LONG)
2310 return(EFTYPE);
2311 *result = res->u.longval;
2312 return(0);
2316 resource_string_value(const char *name, int unit, const char *resname,
2317 const char **result)
2319 int error;
2320 struct config_resource *res;
2321 char buf[64];
2322 const char *env;
2325 * DragonFly style loader.conf hinting
2327 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2328 if ((env = kgetenv(buf)) != NULL) {
2329 *result = env;
2330 return 0;
2334 * Also support FreeBSD style loader.conf hinting
2336 ksnprintf(buf, sizeof(buf), "hint.%s.%d.%s", name, unit, resname);
2337 if ((env = kgetenv(buf)) != NULL) {
2338 *result = env;
2339 return 0;
2342 if ((error = resource_find(name, unit, resname, &res)) != 0)
2343 return(error);
2344 if (res->type != RES_STRING)
2345 return(EFTYPE);
2346 *result = res->u.stringval;
2347 return(0);
2351 resource_query_string(int i, const char *resname, const char *value)
2353 if (i < 0)
2354 i = 0;
2355 else
2356 i = i + 1;
2357 for (; i < devtab_count; i++)
2358 if (resource_match_string(i, resname, value) >= 0)
2359 return(i);
2360 return(-1);
2364 resource_locate(int i, const char *resname)
2366 if (i < 0)
2367 i = 0;
2368 else
2369 i = i + 1;
2370 for (; i < devtab_count; i++)
2371 if (!strcmp(devtab[i].name, resname))
2372 return(i);
2373 return(-1);
2377 resource_count(void)
2379 return(devtab_count);
2382 char *
2383 resource_query_name(int i)
2385 return(devtab[i].name);
2389 resource_query_unit(int i)
2391 return(devtab[i].unit);
2394 static int
2395 resource_create(const char *name, int unit, const char *resname,
2396 resource_type type, struct config_resource **result)
2398 int i, j;
2399 struct config_resource *res = NULL;
2401 for (i = 0; i < devtab_count; i++)
2402 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2403 res = devtab[i].resources;
2404 break;
2406 if (res == NULL) {
2407 i = resource_new_name(name, unit);
2408 if (i < 0)
2409 return(ENOMEM);
2410 res = devtab[i].resources;
2412 for (j = 0; j < devtab[i].resource_count; j++, res++)
2413 if (!strcmp(res->name, resname)) {
2414 *result = res;
2415 return(0);
2417 j = resource_new_resname(i, resname, type);
2418 if (j < 0)
2419 return(ENOMEM);
2420 res = &devtab[i].resources[j];
2421 *result = res;
2422 return(0);
2426 resource_set_int(const char *name, int unit, const char *resname, int value)
2428 int error;
2429 struct config_resource *res;
2431 error = resource_create(name, unit, resname, RES_INT, &res);
2432 if (error)
2433 return(error);
2434 if (res->type != RES_INT)
2435 return(EFTYPE);
2436 res->u.intval = value;
2437 return(0);
2441 resource_set_long(const char *name, int unit, const char *resname, long value)
2443 int error;
2444 struct config_resource *res;
2446 error = resource_create(name, unit, resname, RES_LONG, &res);
2447 if (error)
2448 return(error);
2449 if (res->type != RES_LONG)
2450 return(EFTYPE);
2451 res->u.longval = value;
2452 return(0);
2456 resource_set_string(const char *name, int unit, const char *resname,
2457 const char *value)
2459 int error;
2460 struct config_resource *res;
2462 error = resource_create(name, unit, resname, RES_STRING, &res);
2463 if (error)
2464 return(error);
2465 if (res->type != RES_STRING)
2466 return(EFTYPE);
2467 if (res->u.stringval)
2468 kfree(res->u.stringval, M_TEMP);
2469 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2470 if (res->u.stringval == NULL)
2471 return(ENOMEM);
2472 strcpy(res->u.stringval, value);
2473 return(0);
2476 static void
2477 resource_cfgload(void *dummy __unused)
2479 struct config_resource *res, *cfgres;
2480 int i, j;
2481 int error;
2482 char *name, *resname;
2483 int unit;
2484 resource_type type;
2485 char *stringval;
2486 int config_devtab_count;
2488 config_devtab_count = devtab_count;
2489 devtab = NULL;
2490 devtab_count = 0;
2492 for (i = 0; i < config_devtab_count; i++) {
2493 name = config_devtab[i].name;
2494 unit = config_devtab[i].unit;
2496 for (j = 0; j < config_devtab[i].resource_count; j++) {
2497 cfgres = config_devtab[i].resources;
2498 resname = cfgres[j].name;
2499 type = cfgres[j].type;
2500 error = resource_create(name, unit, resname, type,
2501 &res);
2502 if (error) {
2503 kprintf("create resource %s%d: error %d\n",
2504 name, unit, error);
2505 continue;
2507 if (res->type != type) {
2508 kprintf("type mismatch %s%d: %d != %d\n",
2509 name, unit, res->type, type);
2510 continue;
2512 switch (type) {
2513 case RES_INT:
2514 res->u.intval = cfgres[j].u.intval;
2515 break;
2516 case RES_LONG:
2517 res->u.longval = cfgres[j].u.longval;
2518 break;
2519 case RES_STRING:
2520 if (res->u.stringval)
2521 kfree(res->u.stringval, M_TEMP);
2522 stringval = cfgres[j].u.stringval;
2523 res->u.stringval = kmalloc(strlen(stringval) + 1,
2524 M_TEMP, M_INTWAIT);
2525 if (res->u.stringval == NULL)
2526 break;
2527 strcpy(res->u.stringval, stringval);
2528 break;
2529 default:
2530 panic("unknown resource type %d", type);
2535 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0);
2538 /*======================================*/
2540 * Some useful method implementations to make life easier for bus drivers.
2543 void
2544 resource_list_init(struct resource_list *rl)
2546 SLIST_INIT(rl);
2549 void
2550 resource_list_free(struct resource_list *rl)
2552 struct resource_list_entry *rle;
2554 while ((rle = SLIST_FIRST(rl)) != NULL) {
2555 if (rle->res)
2556 panic("resource_list_free: resource entry is busy");
2557 SLIST_REMOVE_HEAD(rl, link);
2558 kfree(rle, M_BUS);
2562 void
2563 resource_list_add(struct resource_list *rl, int type, int rid,
2564 u_long start, u_long end, u_long count, int cpuid)
2566 struct resource_list_entry *rle;
2568 rle = resource_list_find(rl, type, rid);
2569 if (rle == NULL) {
2570 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2571 M_INTWAIT);
2572 SLIST_INSERT_HEAD(rl, rle, link);
2573 rle->type = type;
2574 rle->rid = rid;
2575 rle->res = NULL;
2576 rle->cpuid = -1;
2579 if (rle->res)
2580 panic("resource_list_add: resource entry is busy");
2582 rle->start = start;
2583 rle->end = end;
2584 rle->count = count;
2586 if (cpuid != -1) {
2587 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2588 panic("resource_list_add: moving from cpu%d -> cpu%d",
2589 rle->cpuid, cpuid);
2591 rle->cpuid = cpuid;
2595 struct resource_list_entry*
2596 resource_list_find(struct resource_list *rl,
2597 int type, int rid)
2599 struct resource_list_entry *rle;
2601 SLIST_FOREACH(rle, rl, link)
2602 if (rle->type == type && rle->rid == rid)
2603 return(rle);
2604 return(NULL);
2607 void
2608 resource_list_delete(struct resource_list *rl,
2609 int type, int rid)
2611 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2613 if (rle) {
2614 if (rle->res != NULL)
2615 panic("resource_list_delete: resource has not been released");
2616 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2617 kfree(rle, M_BUS);
2621 struct resource *
2622 resource_list_alloc(struct resource_list *rl,
2623 device_t bus, device_t child,
2624 int type, int *rid,
2625 u_long start, u_long end,
2626 u_long count, u_int flags, int cpuid)
2628 struct resource_list_entry *rle = NULL;
2629 int passthrough = (device_get_parent(child) != bus);
2630 int isdefault = (start == 0UL && end == ~0UL);
2632 if (passthrough) {
2633 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2634 type, rid,
2635 start, end, count, flags, cpuid));
2638 rle = resource_list_find(rl, type, *rid);
2640 if (!rle)
2641 return(0); /* no resource of that type/rid */
2643 if (rle->res)
2644 panic("resource_list_alloc: resource entry is busy");
2646 if (isdefault) {
2647 start = rle->start;
2648 count = ulmax(count, rle->count);
2649 end = ulmax(rle->end, start + count - 1);
2651 cpuid = rle->cpuid;
2653 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2654 type, rid, start, end, count,
2655 flags, cpuid);
2658 * Record the new range.
2660 if (rle->res) {
2661 rle->start = rman_get_start(rle->res);
2662 rle->end = rman_get_end(rle->res);
2663 rle->count = count;
2666 return(rle->res);
2670 resource_list_release(struct resource_list *rl,
2671 device_t bus, device_t child,
2672 int type, int rid, struct resource *res)
2674 struct resource_list_entry *rle = NULL;
2675 int passthrough = (device_get_parent(child) != bus);
2676 int error;
2678 if (passthrough) {
2679 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2680 type, rid, res));
2683 rle = resource_list_find(rl, type, rid);
2685 if (!rle)
2686 panic("resource_list_release: can't find resource");
2687 if (!rle->res)
2688 panic("resource_list_release: resource entry is not busy");
2690 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2691 type, rid, res);
2692 if (error)
2693 return(error);
2695 rle->res = NULL;
2696 return(0);
2700 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2701 const char *format)
2703 struct resource_list_entry *rle;
2704 int printed, retval;
2706 printed = 0;
2707 retval = 0;
2708 /* Yes, this is kinda cheating */
2709 SLIST_FOREACH(rle, rl, link) {
2710 if (rle->type == type) {
2711 if (printed == 0)
2712 retval += kprintf(" %s ", name);
2713 else
2714 retval += kprintf(",");
2715 printed++;
2716 retval += kprintf(format, rle->start);
2717 if (rle->count > 1) {
2718 retval += kprintf("-");
2719 retval += kprintf(format, rle->start +
2720 rle->count - 1);
2724 return(retval);
2728 * Generic driver/device identify functions. These will install a device
2729 * rendezvous point under the parent using the same name as the driver
2730 * name, which will at a later time be probed and attached.
2732 * These functions are used when the parent does not 'scan' its bus for
2733 * matching devices, or for the particular devices using these functions,
2734 * or when the device is a pseudo or synthesized device (such as can be
2735 * found under firewire and ppbus).
2738 bus_generic_identify(driver_t *driver, device_t parent)
2740 if (parent->state == DS_ATTACHED)
2741 return (0);
2742 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2743 return (0);
2747 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2749 if (parent->state == DS_ATTACHED)
2750 return (0);
2751 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2752 return (0);
2756 * Call DEVICE_IDENTIFY for each driver.
2759 bus_generic_probe(device_t dev)
2761 devclass_t dc = dev->devclass;
2762 driverlink_t dl;
2764 TAILQ_FOREACH(dl, &dc->drivers, link) {
2765 DEVICE_IDENTIFY(dl->driver, dev);
2768 return(0);
2772 * This is an aweful hack due to the isa bus and autoconf code not
2773 * probing the ISA devices until after everything else has configured.
2774 * The ISA bus did a dummy attach long ago so we have to set it back
2775 * to an earlier state so the probe thinks its the initial probe and
2776 * not a bus rescan.
2778 * XXX remove by properly defering the ISA bus scan.
2781 bus_generic_probe_hack(device_t dev)
2783 if (dev->state == DS_ATTACHED) {
2784 dev->state = DS_ALIVE;
2785 bus_generic_probe(dev);
2786 dev->state = DS_ATTACHED;
2788 return (0);
2792 bus_generic_attach(device_t dev)
2794 device_t child;
2796 TAILQ_FOREACH(child, &dev->children, link) {
2797 device_probe_and_attach(child);
2800 return(0);
2804 bus_generic_attach_gpri(device_t dev, u_int gpri)
2806 device_t child;
2808 TAILQ_FOREACH(child, &dev->children, link) {
2809 device_probe_and_attach_gpri(child, gpri);
2812 return(0);
2816 bus_generic_detach(device_t dev)
2818 device_t child;
2819 int error;
2821 if (dev->state != DS_ATTACHED)
2822 return(EBUSY);
2824 TAILQ_FOREACH(child, &dev->children, link)
2825 if ((error = device_detach(child)) != 0)
2826 return(error);
2828 return 0;
2832 bus_generic_shutdown(device_t dev)
2834 device_t child;
2836 TAILQ_FOREACH(child, &dev->children, link)
2837 device_shutdown(child);
2839 return(0);
2843 bus_generic_suspend(device_t dev)
2845 int error;
2846 device_t child, child2;
2848 TAILQ_FOREACH(child, &dev->children, link) {
2849 error = DEVICE_SUSPEND(child);
2850 if (error) {
2851 for (child2 = TAILQ_FIRST(&dev->children);
2852 child2 && child2 != child;
2853 child2 = TAILQ_NEXT(child2, link))
2854 DEVICE_RESUME(child2);
2855 return(error);
2858 return(0);
2862 bus_generic_resume(device_t dev)
2864 device_t child;
2866 TAILQ_FOREACH(child, &dev->children, link)
2867 DEVICE_RESUME(child);
2868 /* if resume fails, there's nothing we can usefully do... */
2870 return(0);
2874 bus_print_child_header(device_t dev, device_t child)
2876 int retval = 0;
2878 if (device_get_desc(child))
2879 retval += device_printf(child, "<%s>", device_get_desc(child));
2880 else
2881 retval += kprintf("%s", device_get_nameunit(child));
2882 if (bootverbose) {
2883 if (child->state != DS_ATTACHED)
2884 kprintf(" [tentative]");
2885 else
2886 kprintf(" [attached!]");
2888 return(retval);
2892 bus_print_child_footer(device_t dev, device_t child)
2894 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2897 device_t
2898 bus_generic_add_child(device_t dev, device_t child, int order,
2899 const char *name, int unit)
2901 if (dev->parent)
2902 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2903 else
2904 dev = device_add_child_ordered(child, order, name, unit);
2905 return(dev);
2910 bus_generic_print_child(device_t dev, device_t child)
2912 int retval = 0;
2914 retval += bus_print_child_header(dev, child);
2915 retval += bus_print_child_footer(dev, child);
2917 return(retval);
2921 bus_generic_read_ivar(device_t dev, device_t child, int index,
2922 uintptr_t * result)
2924 int error;
2926 if (dev->parent)
2927 error = BUS_READ_IVAR(dev->parent, child, index, result);
2928 else
2929 error = ENOENT;
2930 return (error);
2934 bus_generic_write_ivar(device_t dev, device_t child, int index,
2935 uintptr_t value)
2937 int error;
2939 if (dev->parent)
2940 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2941 else
2942 error = ENOENT;
2943 return (error);
2947 * Resource list are used for iterations, do not recurse.
2949 struct resource_list *
2950 bus_generic_get_resource_list(device_t dev, device_t child)
2952 return (NULL);
2955 void
2956 bus_generic_driver_added(device_t dev, driver_t *driver)
2958 device_t child;
2960 DEVICE_IDENTIFY(driver, dev);
2961 TAILQ_FOREACH(child, &dev->children, link) {
2962 if (child->state == DS_NOTPRESENT)
2963 device_probe_and_attach(child);
2968 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2969 int flags, driver_intr_t *intr, void *arg, void **cookiep,
2970 lwkt_serialize_t serializer, const char *desc)
2972 /* Propagate up the bus hierarchy until someone handles it. */
2973 if (dev->parent) {
2974 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2975 intr, arg, cookiep, serializer, desc);
2976 } else {
2977 return EINVAL;
2982 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2983 void *cookie)
2985 /* Propagate up the bus hierarchy until someone handles it. */
2986 if (dev->parent)
2987 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2988 else
2989 return(EINVAL);
2993 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2995 if (dev->parent)
2996 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2997 else
2998 return(0);
3001 void
3002 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
3004 if (dev->parent)
3005 BUS_ENABLE_INTR(dev->parent, child, cookie);
3009 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
3010 enum intr_polarity pol)
3012 /* Propagate up the bus hierarchy until someone handles it. */
3013 if (dev->parent)
3014 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
3015 else
3016 return(EINVAL);
3019 struct resource *
3020 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
3021 u_long start, u_long end, u_long count, u_int flags, int cpuid)
3023 /* Propagate up the bus hierarchy until someone handles it. */
3024 if (dev->parent)
3025 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
3026 start, end, count, flags, cpuid));
3027 else
3028 return(NULL);
3032 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
3033 struct resource *r)
3035 /* Propagate up the bus hierarchy until someone handles it. */
3036 if (dev->parent)
3037 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
3038 else
3039 return(EINVAL);
3043 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
3044 struct resource *r)
3046 /* Propagate up the bus hierarchy until someone handles it. */
3047 if (dev->parent)
3048 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
3049 else
3050 return(EINVAL);
3054 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
3055 int rid, struct resource *r)
3057 /* Propagate up the bus hierarchy until someone handles it. */
3058 if (dev->parent)
3059 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
3060 r));
3061 else
3062 return(EINVAL);
3066 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
3067 u_long *startp, u_long *countp)
3069 int error;
3071 error = ENOENT;
3072 if (dev->parent) {
3073 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
3074 startp, countp);
3076 return (error);
3080 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
3081 u_long start, u_long count, int cpuid)
3083 int error;
3085 error = EINVAL;
3086 if (dev->parent) {
3087 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
3088 start, count, cpuid);
3090 return (error);
3093 void
3094 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
3096 if (dev->parent)
3097 BUS_DELETE_RESOURCE(dev, child, type, rid);
3101 * @brief Helper function for implementing BUS_GET_DMA_TAG().
3103 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
3104 * BUS_GET_DMA_TAG() method of the parent of @p dev.
3106 bus_dma_tag_t
3107 bus_generic_get_dma_tag(device_t dev, device_t child)
3110 /* Propagate up the bus hierarchy until someone handles it. */
3111 if (dev->parent != NULL)
3112 return (BUS_GET_DMA_TAG(dev->parent, child));
3113 return (NULL);
3117 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
3118 u_long *startp, u_long *countp)
3120 struct resource_list *rl = NULL;
3121 struct resource_list_entry *rle = NULL;
3123 rl = BUS_GET_RESOURCE_LIST(dev, child);
3124 if (!rl)
3125 return(EINVAL);
3127 rle = resource_list_find(rl, type, rid);
3128 if (!rle)
3129 return(ENOENT);
3131 if (startp)
3132 *startp = rle->start;
3133 if (countp)
3134 *countp = rle->count;
3136 return(0);
3140 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
3141 u_long start, u_long count, int cpuid)
3143 struct resource_list *rl = NULL;
3145 rl = BUS_GET_RESOURCE_LIST(dev, child);
3146 if (!rl)
3147 return(EINVAL);
3149 resource_list_add(rl, type, rid, start, (start + count - 1), count,
3150 cpuid);
3152 return(0);
3155 void
3156 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
3158 struct resource_list *rl = NULL;
3160 rl = BUS_GET_RESOURCE_LIST(dev, child);
3161 if (!rl)
3162 return;
3164 resource_list_delete(rl, type, rid);
3168 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
3169 int rid, struct resource *r)
3171 struct resource_list *rl = NULL;
3173 rl = BUS_GET_RESOURCE_LIST(dev, child);
3174 if (!rl)
3175 return(EINVAL);
3177 return(resource_list_release(rl, dev, child, type, rid, r));
3180 struct resource *
3181 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
3182 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
3184 struct resource_list *rl = NULL;
3186 rl = BUS_GET_RESOURCE_LIST(dev, child);
3187 if (!rl)
3188 return(NULL);
3190 return(resource_list_alloc(rl, dev, child, type, rid,
3191 start, end, count, flags, cpuid));
3195 bus_generic_child_present(device_t bus, device_t child)
3197 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
3202 * Some convenience functions to make it easier for drivers to use the
3203 * resource-management functions. All these really do is hide the
3204 * indirection through the parent's method table, making for slightly
3205 * less-wordy code. In the future, it might make sense for this code
3206 * to maintain some sort of a list of resources allocated by each device.
3209 bus_alloc_resources(device_t dev, struct resource_spec *rs,
3210 struct resource **res)
3212 int i;
3214 for (i = 0; rs[i].type != -1; i++)
3215 res[i] = NULL;
3216 for (i = 0; rs[i].type != -1; i++) {
3217 res[i] = bus_alloc_resource_any(dev,
3218 rs[i].type, &rs[i].rid, rs[i].flags);
3219 if (res[i] == NULL) {
3220 bus_release_resources(dev, rs, res);
3221 return (ENXIO);
3224 return (0);
3227 void
3228 bus_release_resources(device_t dev, const struct resource_spec *rs,
3229 struct resource **res)
3231 int i;
3233 for (i = 0; rs[i].type != -1; i++)
3234 if (res[i] != NULL) {
3235 bus_release_resource(
3236 dev, rs[i].type, rs[i].rid, res[i]);
3237 res[i] = NULL;
3241 struct resource *
3242 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3243 u_long count, u_int flags)
3245 if (dev->parent == NULL)
3246 return(0);
3247 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3248 count, flags, -1));
3251 struct resource *
3252 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3254 if (dev->parent == NULL)
3255 return(0);
3256 return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3257 irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3261 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3263 if (dev->parent == NULL)
3264 return(EINVAL);
3265 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3269 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3271 if (dev->parent == NULL)
3272 return(EINVAL);
3273 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3277 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3279 if (dev->parent == NULL)
3280 return(EINVAL);
3281 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3285 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3286 driver_intr_t handler, void *arg, void **cookiep,
3287 lwkt_serialize_t serializer, const char *desc)
3289 if (dev->parent == NULL)
3290 return EINVAL;
3291 return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3292 cookiep, serializer, desc);
3296 bus_setup_intr(device_t dev, struct resource *r, int flags,
3297 driver_intr_t handler, void *arg, void **cookiep,
3298 lwkt_serialize_t serializer)
3300 return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3301 serializer, NULL);
3305 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3307 if (dev->parent == NULL)
3308 return(EINVAL);
3309 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3312 void
3313 bus_enable_intr(device_t dev, void *cookie)
3315 if (dev->parent)
3316 BUS_ENABLE_INTR(dev->parent, dev, cookie);
3320 bus_disable_intr(device_t dev, void *cookie)
3322 if (dev->parent)
3323 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3324 else
3325 return(0);
3329 bus_set_resource(device_t dev, int type, int rid,
3330 u_long start, u_long count, int cpuid)
3332 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3333 start, count, cpuid));
3337 bus_get_resource(device_t dev, int type, int rid,
3338 u_long *startp, u_long *countp)
3340 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3341 startp, countp));
3344 u_long
3345 bus_get_resource_start(device_t dev, int type, int rid)
3347 u_long start, count;
3348 int error;
3350 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3351 &start, &count);
3352 if (error)
3353 return(0);
3354 return(start);
3357 u_long
3358 bus_get_resource_count(device_t dev, int type, int rid)
3360 u_long start, count;
3361 int error;
3363 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3364 &start, &count);
3365 if (error)
3366 return(0);
3367 return(count);
3370 void
3371 bus_delete_resource(device_t dev, int type, int rid)
3373 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3377 bus_child_present(device_t child)
3379 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3383 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3385 device_t parent;
3387 parent = device_get_parent(child);
3388 if (parent == NULL) {
3389 *buf = '\0';
3390 return (0);
3392 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3396 bus_child_location_str(device_t child, char *buf, size_t buflen)
3398 device_t parent;
3400 parent = device_get_parent(child);
3401 if (parent == NULL) {
3402 *buf = '\0';
3403 return (0);
3405 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3409 * @brief Wrapper function for BUS_GET_DMA_TAG().
3411 * This function simply calls the BUS_GET_DMA_TAG() method of the
3412 * parent of @p dev.
3414 bus_dma_tag_t
3415 bus_get_dma_tag(device_t dev)
3417 device_t parent;
3419 parent = device_get_parent(dev);
3420 if (parent == NULL)
3421 return (NULL);
3422 return (BUS_GET_DMA_TAG(parent, dev));
3425 static int
3426 root_print_child(device_t dev, device_t child)
3428 return(0);
3431 static int
3432 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3433 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3436 * If an interrupt mapping gets to here something bad has happened.
3438 panic("root_setup_intr");
3442 * If we get here, assume that the device is permanant and really is
3443 * present in the system. Removable bus drivers are expected to intercept
3444 * this call long before it gets here. We return -1 so that drivers that
3445 * really care can check vs -1 or some ERRNO returned higher in the food
3446 * chain.
3448 static int
3449 root_child_present(device_t dev, device_t child)
3451 return(-1);
3455 * XXX NOTE! other defaults may be set in bus_if.m
3457 static kobj_method_t root_methods[] = {
3458 /* Device interface */
3459 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3460 KOBJMETHOD(device_suspend, bus_generic_suspend),
3461 KOBJMETHOD(device_resume, bus_generic_resume),
3463 /* Bus interface */
3464 KOBJMETHOD(bus_add_child, bus_generic_add_child),
3465 KOBJMETHOD(bus_print_child, root_print_child),
3466 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3467 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3468 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3469 KOBJMETHOD(bus_child_present, root_child_present),
3471 KOBJMETHOD_END
3474 static driver_t root_driver = {
3475 "root",
3476 root_methods,
3477 1, /* no softc */
3480 device_t root_bus;
3481 devclass_t root_devclass;
3483 static int
3484 root_bus_module_handler(module_t mod, int what, void* arg)
3486 switch (what) {
3487 case MOD_LOAD:
3488 TAILQ_INIT(&bus_data_devices);
3489 root_bus = make_device(NULL, "root", 0);
3490 root_bus->desc = "System root bus";
3491 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3492 root_bus->driver = &root_driver;
3493 root_bus->state = DS_ALIVE;
3494 root_devclass = devclass_find_internal("root", NULL, FALSE);
3495 devinit();
3496 return(0);
3498 case MOD_SHUTDOWN:
3499 device_shutdown(root_bus);
3500 return(0);
3501 default:
3502 return(0);
3506 static moduledata_t root_bus_mod = {
3507 "rootbus",
3508 root_bus_module_handler,
3511 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3513 void
3514 root_bus_configure(void)
3516 int warncount;
3517 device_t dev;
3519 PDEBUG(("."));
3522 * handle device_identify based device attachments to the root_bus
3523 * (typically nexus).
3525 bus_generic_probe(root_bus);
3528 * Probe and attach the devices under root_bus.
3530 TAILQ_FOREACH(dev, &root_bus->children, link) {
3531 device_probe_and_attach(dev);
3535 * Wait for all asynchronous attaches to complete. If we don't
3536 * our legacy ISA bus scan could steal device unit numbers or
3537 * even I/O ports.
3539 warncount = 10;
3540 if (numasyncthreads)
3541 kprintf("Waiting for async drivers to attach\n");
3542 while (numasyncthreads > 0) {
3543 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3544 --warncount;
3545 if (warncount == 0) {
3546 kprintf("Warning: Still waiting for %d "
3547 "drivers to attach\n", numasyncthreads);
3548 } else if (warncount == -30) {
3549 kprintf("Giving up on %d drivers\n", numasyncthreads);
3550 break;
3553 root_bus->state = DS_ATTACHED;
3557 driver_module_handler(module_t mod, int what, void *arg)
3559 int error;
3560 struct driver_module_data *dmd;
3561 devclass_t bus_devclass;
3562 kobj_class_t driver;
3563 const char *parentname;
3565 dmd = (struct driver_module_data *)arg;
3566 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3567 error = 0;
3569 switch (what) {
3570 case MOD_LOAD:
3571 if (dmd->dmd_chainevh)
3572 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3574 driver = dmd->dmd_driver;
3575 PDEBUG(("Loading module: driver %s on bus %s",
3576 DRIVERNAME(driver), dmd->dmd_busname));
3579 * If the driver has any base classes, make the
3580 * devclass inherit from the devclass of the driver's
3581 * first base class. This will allow the system to
3582 * search for drivers in both devclasses for children
3583 * of a device using this driver.
3585 if (driver->baseclasses)
3586 parentname = driver->baseclasses[0]->name;
3587 else
3588 parentname = NULL;
3589 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3590 parentname, TRUE);
3592 error = devclass_add_driver(bus_devclass, driver);
3593 if (error)
3594 break;
3595 break;
3597 case MOD_UNLOAD:
3598 PDEBUG(("Unloading module: driver %s from bus %s",
3599 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3600 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3602 if (!error && dmd->dmd_chainevh)
3603 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3604 break;
3607 return (error);
3610 #ifdef BUS_DEBUG
3613 * The _short versions avoid iteration by not calling anything that prints
3614 * more than oneliners. I love oneliners.
3617 static void
3618 print_device_short(device_t dev, int indent)
3620 if (!dev)
3621 return;
3623 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3624 dev->unit, dev->desc,
3625 (dev->parent? "":"no "),
3626 (TAILQ_EMPTY(&dev->children)? "no ":""),
3627 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3628 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3629 (dev->flags&DF_WILDCARD? "wildcard,":""),
3630 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3631 (dev->ivars? "":"no "),
3632 (dev->softc? "":"no "),
3633 dev->busy));
3636 static void
3637 print_device(device_t dev, int indent)
3639 if (!dev)
3640 return;
3642 print_device_short(dev, indent);
3644 indentprintf(("Parent:\n"));
3645 print_device_short(dev->parent, indent+1);
3646 indentprintf(("Driver:\n"));
3647 print_driver_short(dev->driver, indent+1);
3648 indentprintf(("Devclass:\n"));
3649 print_devclass_short(dev->devclass, indent+1);
3653 * Print the device and all its children (indented).
3655 void
3656 print_device_tree_short(device_t dev, int indent)
3658 device_t child;
3660 if (!dev)
3661 return;
3663 print_device_short(dev, indent);
3665 TAILQ_FOREACH(child, &dev->children, link)
3666 print_device_tree_short(child, indent+1);
3670 * Print the device and all its children (indented).
3672 void
3673 print_device_tree(device_t dev, int indent)
3675 device_t child;
3677 if (!dev)
3678 return;
3680 print_device(dev, indent);
3682 TAILQ_FOREACH(child, &dev->children, link)
3683 print_device_tree(child, indent+1);
3686 static void
3687 print_driver_short(driver_t *driver, int indent)
3689 if (!driver)
3690 return;
3692 indentprintf(("driver %s: softc size = %zu\n",
3693 driver->name, driver->size));
3696 static void
3697 print_driver(driver_t *driver, int indent)
3699 if (!driver)
3700 return;
3702 print_driver_short(driver, indent);
3706 static void
3707 print_driver_list(driver_list_t drivers, int indent)
3709 driverlink_t driver;
3711 TAILQ_FOREACH(driver, &drivers, link)
3712 print_driver(driver->driver, indent);
3715 static void
3716 print_devclass_short(devclass_t dc, int indent)
3718 if (!dc)
3719 return;
3721 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3724 static void
3725 print_devclass(devclass_t dc, int indent)
3727 int i;
3729 if (!dc)
3730 return;
3732 print_devclass_short(dc, indent);
3733 indentprintf(("Drivers:\n"));
3734 print_driver_list(dc->drivers, indent+1);
3736 indentprintf(("Devices:\n"));
3737 for (i = 0; i < dc->maxunit; i++)
3738 if (dc->devices[i])
3739 print_device(dc->devices[i], indent+1);
3742 void
3743 print_devclass_list_short(void)
3745 devclass_t dc;
3747 kprintf("Short listing of devclasses, drivers & devices:\n");
3748 TAILQ_FOREACH(dc, &devclasses, link) {
3749 print_devclass_short(dc, 0);
3753 void
3754 print_devclass_list(void)
3756 devclass_t dc;
3758 kprintf("Full listing of devclasses, drivers & devices:\n");
3759 TAILQ_FOREACH(dc, &devclasses, link) {
3760 print_devclass(dc, 0);
3764 #endif
3767 * Check to see if a device is disabled via a disabled hint.
3770 resource_disabled(const char *name, int unit)
3772 int error, value;
3774 error = resource_int_value(name, unit, "disabled", &value);
3775 if (error)
3776 return(0);
3777 return(value);
3781 * User-space access to the device tree.
3783 * We implement a small set of nodes:
3785 * hw.bus Single integer read method to obtain the
3786 * current generation count.
3787 * hw.bus.devices Reads the entire device tree in flat space.
3788 * hw.bus.rman Resource manager interface
3790 * We might like to add the ability to scan devclasses and/or drivers to
3791 * determine what else is currently loaded/available.
3794 static int
3795 sysctl_bus(SYSCTL_HANDLER_ARGS)
3797 struct u_businfo ubus;
3799 ubus.ub_version = BUS_USER_VERSION;
3800 ubus.ub_generation = bus_data_generation;
3802 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3804 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3805 "bus-related data");
3807 static int
3808 sysctl_devices(SYSCTL_HANDLER_ARGS)
3810 int *name = (int *)arg1;
3811 u_int namelen = arg2;
3812 int index;
3813 device_t dev;
3814 struct u_device udev; /* XXX this is a bit big */
3815 int error;
3817 if (namelen != 2)
3818 return (EINVAL);
3820 if (bus_data_generation_check(name[0]))
3821 return (EINVAL);
3823 index = name[1];
3826 * Scan the list of devices, looking for the requested index.
3828 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3829 if (index-- == 0)
3830 break;
3832 if (dev == NULL)
3833 return (ENOENT);
3836 * Populate the return array.
3838 bzero(&udev, sizeof(udev));
3839 udev.dv_handle = (uintptr_t)dev;
3840 udev.dv_parent = (uintptr_t)dev->parent;
3841 if (dev->nameunit != NULL)
3842 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3843 if (dev->desc != NULL)
3844 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3845 if (dev->driver != NULL && dev->driver->name != NULL)
3846 strlcpy(udev.dv_drivername, dev->driver->name,
3847 sizeof(udev.dv_drivername));
3848 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3849 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3850 udev.dv_devflags = dev->devflags;
3851 udev.dv_flags = dev->flags;
3852 udev.dv_state = dev->state;
3853 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3854 return (error);
3857 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3858 "system device tree");
3861 bus_data_generation_check(int generation)
3863 if (generation != bus_data_generation)
3864 return (1);
3866 /* XXX generate optimised lists here? */
3867 return (0);
3870 void
3871 bus_data_generation_update(void)
3873 bus_data_generation++;
3876 const char *
3877 intr_str_polarity(enum intr_polarity pola)
3879 switch (pola) {
3880 case INTR_POLARITY_LOW:
3881 return "low";
3883 case INTR_POLARITY_HIGH:
3884 return "high";
3886 case INTR_POLARITY_CONFORM:
3887 return "conform";
3889 return "unknown";
3892 const char *
3893 intr_str_trigger(enum intr_trigger trig)
3895 switch (trig) {
3896 case INTR_TRIGGER_EDGE:
3897 return "edge";
3899 case INTR_TRIGGER_LEVEL:
3900 return "level";
3902 case INTR_TRIGGER_CONFORM:
3903 return "conform";
3905 return "unknown";
3909 device_getenv_int(device_t dev, const char *knob, int def)
3911 char env[128];
3913 /* Deprecated; for compat */
3914 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3915 kgetenv_int(env, &def);
3917 /* Prefer dev.driver.unit.knob */
3918 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3919 device_get_name(dev), device_get_unit(dev), knob);
3920 kgetenv_int(env, &def);
3922 return def;
3925 void
3926 device_getenv_string(device_t dev, const char *knob, char * __restrict data,
3927 int dlen, const char * __restrict def)
3929 char env[128];
3931 strlcpy(data, def, dlen);
3933 /* Deprecated; for compat */
3934 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3935 kgetenv_string(env, data, dlen);
3937 /* Prefer dev.driver.unit.knob */
3938 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3939 device_get_name(dev), device_get_unit(dev), knob);
3940 kgetenv_string(env, data, dlen);