kernel - Improve physio performance
[dragonfly.git] / sys / kern / subr_bus.c
blob9da7eae54dac62f373657f966d4802efa622ef7a
1 /*
2 * Copyright (c) 1997,1998 Doug Rabson
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
29 #include "opt_bus.h"
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/kobj.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/device.h>
43 #include <sys/lock.h>
44 #include <sys/conf.h>
45 #include <sys/uio.h>
46 #include <sys/filio.h>
47 #include <sys/event.h>
48 #include <sys/signalvar.h>
49 #include <sys/machintr.h>
50 #include <sys/vnode.h>
52 #include <machine/stdarg.h> /* for device_printf() */
54 #include <sys/thread2.h>
55 #include <sys/mplock2.h>
57 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
58 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
60 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
62 #ifdef BUS_DEBUG
63 #define PDEBUG(a) (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
64 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
65 #define DRIVERNAME(d) ((d)? d->name : "no driver")
66 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
68 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
69 * prevent syslog from deleting initial spaces
71 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf(" "); kprintf p ; } while(0)
73 static void print_device_short(device_t dev, int indent);
74 static void print_device(device_t dev, int indent);
75 void print_device_tree_short(device_t dev, int indent);
76 void print_device_tree(device_t dev, int indent);
77 static void print_driver_short(driver_t *driver, int indent);
78 static void print_driver(driver_t *driver, int indent);
79 static void print_driver_list(driver_list_t drivers, int indent);
80 static void print_devclass_short(devclass_t dc, int indent);
81 static void print_devclass(devclass_t dc, int indent);
82 void print_devclass_list_short(void);
83 void print_devclass_list(void);
85 #else
86 /* Make the compiler ignore the function calls */
87 #define PDEBUG(a) /* nop */
88 #define DEVICENAME(d) /* nop */
89 #define DRIVERNAME(d) /* nop */
90 #define DEVCLANAME(d) /* nop */
92 #define print_device_short(d,i) /* nop */
93 #define print_device(d,i) /* nop */
94 #define print_device_tree_short(d,i) /* nop */
95 #define print_device_tree(d,i) /* nop */
96 #define print_driver_short(d,i) /* nop */
97 #define print_driver(d,i) /* nop */
98 #define print_driver_list(d,i) /* nop */
99 #define print_devclass_short(d,i) /* nop */
100 #define print_devclass(d,i) /* nop */
101 #define print_devclass_list_short() /* nop */
102 #define print_devclass_list() /* nop */
103 #endif
106 * dev sysctl tree
109 enum {
110 DEVCLASS_SYSCTL_PARENT,
113 static int
114 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
116 devclass_t dc = (devclass_t)arg1;
117 const char *value;
119 switch (arg2) {
120 case DEVCLASS_SYSCTL_PARENT:
121 value = dc->parent ? dc->parent->name : "";
122 break;
123 default:
124 return (EINVAL);
126 return (SYSCTL_OUT(req, value, strlen(value)));
129 static void
130 devclass_sysctl_init(devclass_t dc)
133 if (dc->sysctl_tree != NULL)
134 return;
135 sysctl_ctx_init(&dc->sysctl_ctx);
136 dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
137 SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
138 CTLFLAG_RD, NULL, "");
139 SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
140 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
141 dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
142 "parent class");
145 enum {
146 DEVICE_SYSCTL_DESC,
147 DEVICE_SYSCTL_DRIVER,
148 DEVICE_SYSCTL_LOCATION,
149 DEVICE_SYSCTL_PNPINFO,
150 DEVICE_SYSCTL_PARENT,
153 static int
154 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
156 device_t dev = (device_t)arg1;
157 const char *value;
158 char *buf;
159 int error;
161 buf = NULL;
162 switch (arg2) {
163 case DEVICE_SYSCTL_DESC:
164 value = dev->desc ? dev->desc : "";
165 break;
166 case DEVICE_SYSCTL_DRIVER:
167 value = dev->driver ? dev->driver->name : "";
168 break;
169 case DEVICE_SYSCTL_LOCATION:
170 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
171 bus_child_location_str(dev, buf, 1024);
172 break;
173 case DEVICE_SYSCTL_PNPINFO:
174 value = buf = kmalloc(1024, M_BUS, M_WAITOK | M_ZERO);
175 bus_child_pnpinfo_str(dev, buf, 1024);
176 break;
177 case DEVICE_SYSCTL_PARENT:
178 value = dev->parent ? dev->parent->nameunit : "";
179 break;
180 default:
181 return (EINVAL);
183 error = SYSCTL_OUT(req, value, strlen(value));
184 if (buf != NULL)
185 kfree(buf, M_BUS);
186 return (error);
189 static void
190 device_sysctl_init(device_t dev)
192 devclass_t dc = dev->devclass;
194 if (dev->sysctl_tree != NULL)
195 return;
196 devclass_sysctl_init(dc);
197 sysctl_ctx_init(&dev->sysctl_ctx);
198 dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
199 SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
200 dev->nameunit + strlen(dc->name),
201 CTLFLAG_RD, NULL, "");
202 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
203 OID_AUTO, "%desc", CTLTYPE_STRING | CTLFLAG_RD,
204 dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
205 "device description");
206 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
207 OID_AUTO, "%driver", CTLTYPE_STRING | CTLFLAG_RD,
208 dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
209 "device driver name");
210 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
211 OID_AUTO, "%location", CTLTYPE_STRING | CTLFLAG_RD,
212 dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
213 "device location relative to parent");
214 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
215 OID_AUTO, "%pnpinfo", CTLTYPE_STRING | CTLFLAG_RD,
216 dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
217 "device identification");
218 SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
219 OID_AUTO, "%parent", CTLTYPE_STRING | CTLFLAG_RD,
220 dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
221 "parent device");
224 static void
225 device_sysctl_update(device_t dev)
227 devclass_t dc = dev->devclass;
229 if (dev->sysctl_tree == NULL)
230 return;
231 sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
234 static void
235 device_sysctl_fini(device_t dev)
237 if (dev->sysctl_tree == NULL)
238 return;
239 sysctl_ctx_free(&dev->sysctl_ctx);
240 dev->sysctl_tree = NULL;
243 static void device_attach_async(device_t dev);
244 static void device_attach_thread(void *arg);
245 static int device_doattach(device_t dev);
247 static int do_async_attach = 0;
248 static int numasyncthreads;
249 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
252 * /dev/devctl implementation
256 * This design allows only one reader for /dev/devctl. This is not desirable
257 * in the long run, but will get a lot of hair out of this implementation.
258 * Maybe we should make this device a clonable device.
260 * Also note: we specifically do not attach a device to the device_t tree
261 * to avoid potential chicken and egg problems. One could argue that all
262 * of this belongs to the root node. One could also further argue that the
263 * sysctl interface that we have not might more properly be an ioctl
264 * interface, but at this stage of the game, I'm not inclined to rock that
265 * boat.
267 * I'm also not sure that the SIGIO support is done correctly or not, as
268 * I copied it from a driver that had SIGIO support that likely hasn't been
269 * tested since 3.4 or 2.2.8!
272 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
273 static int devctl_disable = 0;
274 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
275 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
276 sysctl_devctl_disable, "I", "devctl disable");
278 static d_open_t devopen;
279 static d_close_t devclose;
280 static d_read_t devread;
281 static d_ioctl_t devioctl;
282 static d_kqfilter_t devkqfilter;
284 static struct dev_ops devctl_ops = {
285 { "devctl", 0, 0 },
286 .d_open = devopen,
287 .d_close = devclose,
288 .d_read = devread,
289 .d_ioctl = devioctl,
290 .d_kqfilter = devkqfilter
293 struct dev_event_info
295 char *dei_data;
296 TAILQ_ENTRY(dev_event_info) dei_link;
299 TAILQ_HEAD(devq, dev_event_info);
301 static struct dev_softc
303 int inuse;
304 struct lock lock;
305 struct kqinfo kq;
306 struct devq devq;
307 struct proc *async_proc;
308 } devsoftc;
310 static void
311 devinit(void)
313 make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
314 lockinit(&devsoftc.lock, "dev mtx", 0, 0);
315 TAILQ_INIT(&devsoftc.devq);
318 static int
319 devopen(struct dev_open_args *ap)
321 if (devsoftc.inuse)
322 return (EBUSY);
323 /* move to init */
324 devsoftc.inuse = 1;
325 devsoftc.async_proc = NULL;
326 return (0);
329 static int
330 devclose(struct dev_close_args *ap)
332 devsoftc.inuse = 0;
333 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
334 wakeup(&devsoftc);
335 lockmgr(&devsoftc.lock, LK_RELEASE);
337 return (0);
341 * The read channel for this device is used to report changes to
342 * userland in realtime. We are required to free the data as well as
343 * the n1 object because we allocate them separately. Also note that
344 * we return one record at a time. If you try to read this device a
345 * character at a time, you will lose the rest of the data. Listening
346 * programs are expected to cope.
348 static int
349 devread(struct dev_read_args *ap)
351 struct uio *uio = ap->a_uio;
352 struct dev_event_info *n1;
353 int rv;
355 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
356 while (TAILQ_EMPTY(&devsoftc.devq)) {
357 if (ap->a_ioflag & IO_NDELAY) {
358 lockmgr(&devsoftc.lock, LK_RELEASE);
359 return (EAGAIN);
361 tsleep_interlock(&devsoftc, PCATCH);
362 lockmgr(&devsoftc.lock, LK_RELEASE);
363 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
364 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
365 if (rv) {
367 * Need to translate ERESTART to EINTR here? -- jake
369 lockmgr(&devsoftc.lock, LK_RELEASE);
370 return (rv);
373 n1 = TAILQ_FIRST(&devsoftc.devq);
374 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
375 lockmgr(&devsoftc.lock, LK_RELEASE);
376 rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
377 kfree(n1->dei_data, M_BUS);
378 kfree(n1, M_BUS);
379 return (rv);
382 static int
383 devioctl(struct dev_ioctl_args *ap)
385 switch (ap->a_cmd) {
387 case FIONBIO:
388 return (0);
389 case FIOASYNC:
390 if (*(int*)ap->a_data)
391 devsoftc.async_proc = curproc;
392 else
393 devsoftc.async_proc = NULL;
394 return (0);
396 /* (un)Support for other fcntl() calls. */
397 case FIOCLEX:
398 case FIONCLEX:
399 case FIONREAD:
400 case FIOSETOWN:
401 case FIOGETOWN:
402 default:
403 break;
405 return (ENOTTY);
408 static void dev_filter_detach(struct knote *);
409 static int dev_filter_read(struct knote *, long);
411 static struct filterops dev_filtops =
412 { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
414 static int
415 devkqfilter(struct dev_kqfilter_args *ap)
417 struct knote *kn = ap->a_kn;
418 struct klist *klist;
420 ap->a_result = 0;
421 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
423 switch (kn->kn_filter) {
424 case EVFILT_READ:
425 kn->kn_fop = &dev_filtops;
426 break;
427 default:
428 ap->a_result = EOPNOTSUPP;
429 lockmgr(&devsoftc.lock, LK_RELEASE);
430 return (0);
433 klist = &devsoftc.kq.ki_note;
434 knote_insert(klist, kn);
436 lockmgr(&devsoftc.lock, LK_RELEASE);
438 return (0);
441 static void
442 dev_filter_detach(struct knote *kn)
444 struct klist *klist;
446 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
447 klist = &devsoftc.kq.ki_note;
448 knote_remove(klist, kn);
449 lockmgr(&devsoftc.lock, LK_RELEASE);
452 static int
453 dev_filter_read(struct knote *kn, long hint)
455 int ready = 0;
457 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
458 if (!TAILQ_EMPTY(&devsoftc.devq))
459 ready = 1;
460 lockmgr(&devsoftc.lock, LK_RELEASE);
462 return (ready);
467 * @brief Return whether the userland process is running
469 boolean_t
470 devctl_process_running(void)
472 return (devsoftc.inuse == 1);
476 * @brief Queue data to be read from the devctl device
478 * Generic interface to queue data to the devctl device. It is
479 * assumed that @p data is properly formatted. It is further assumed
480 * that @p data is allocated using the M_BUS malloc type.
482 void
483 devctl_queue_data(char *data)
485 struct dev_event_info *n1 = NULL;
486 struct proc *p;
488 n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
489 if (n1 == NULL)
490 return;
491 n1->dei_data = data;
492 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
493 TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
494 wakeup(&devsoftc);
495 lockmgr(&devsoftc.lock, LK_RELEASE);
496 KNOTE(&devsoftc.kq.ki_note, 0);
497 p = devsoftc.async_proc;
498 if (p != NULL)
499 ksignal(p, SIGIO);
503 * @brief Send a 'notification' to userland, using standard ways
505 void
506 devctl_notify(const char *system, const char *subsystem, const char *type,
507 const char *data)
509 int len = 0;
510 char *msg;
512 if (system == NULL)
513 return; /* BOGUS! Must specify system. */
514 if (subsystem == NULL)
515 return; /* BOGUS! Must specify subsystem. */
516 if (type == NULL)
517 return; /* BOGUS! Must specify type. */
518 len += strlen(" system=") + strlen(system);
519 len += strlen(" subsystem=") + strlen(subsystem);
520 len += strlen(" type=") + strlen(type);
521 /* add in the data message plus newline. */
522 if (data != NULL)
523 len += strlen(data);
524 len += 3; /* '!', '\n', and NUL */
525 msg = kmalloc(len, M_BUS, M_NOWAIT);
526 if (msg == NULL)
527 return; /* Drop it on the floor */
528 if (data != NULL)
529 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
530 system, subsystem, type, data);
531 else
532 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
533 system, subsystem, type);
534 devctl_queue_data(msg);
538 * Common routine that tries to make sending messages as easy as possible.
539 * We allocate memory for the data, copy strings into that, but do not
540 * free it unless there's an error. The dequeue part of the driver should
541 * free the data. We don't send data when the device is disabled. We do
542 * send data, even when we have no listeners, because we wish to avoid
543 * races relating to startup and restart of listening applications.
545 * devaddq is designed to string together the type of event, with the
546 * object of that event, plus the plug and play info and location info
547 * for that event. This is likely most useful for devices, but less
548 * useful for other consumers of this interface. Those should use
549 * the devctl_queue_data() interface instead.
551 static void
552 devaddq(const char *type, const char *what, device_t dev)
554 char *data = NULL;
555 char *loc = NULL;
556 char *pnp = NULL;
557 const char *parstr;
559 if (devctl_disable)
560 return;
561 data = kmalloc(1024, M_BUS, M_NOWAIT);
562 if (data == NULL)
563 goto bad;
565 /* get the bus specific location of this device */
566 loc = kmalloc(1024, M_BUS, M_NOWAIT);
567 if (loc == NULL)
568 goto bad;
569 *loc = '\0';
570 bus_child_location_str(dev, loc, 1024);
572 /* Get the bus specific pnp info of this device */
573 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
574 if (pnp == NULL)
575 goto bad;
576 *pnp = '\0';
577 bus_child_pnpinfo_str(dev, pnp, 1024);
579 /* Get the parent of this device, or / if high enough in the tree. */
580 if (device_get_parent(dev) == NULL)
581 parstr = "."; /* Or '/' ? */
582 else
583 parstr = device_get_nameunit(device_get_parent(dev));
584 /* String it all together. */
585 ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
586 parstr);
587 kfree(loc, M_BUS);
588 kfree(pnp, M_BUS);
589 devctl_queue_data(data);
590 return;
591 bad:
592 kfree(pnp, M_BUS);
593 kfree(loc, M_BUS);
594 kfree(data, M_BUS);
595 return;
599 * A device was added to the tree. We are called just after it successfully
600 * attaches (that is, probe and attach success for this device). No call
601 * is made if a device is merely parented into the tree. See devnomatch
602 * if probe fails. If attach fails, no notification is sent (but maybe
603 * we should have a different message for this).
605 static void
606 devadded(device_t dev)
608 char *pnp = NULL;
609 char *tmp = NULL;
611 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
612 if (pnp == NULL)
613 goto fail;
614 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
615 if (tmp == NULL)
616 goto fail;
617 *pnp = '\0';
618 bus_child_pnpinfo_str(dev, pnp, 1024);
619 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
620 devaddq("+", tmp, dev);
621 fail:
622 if (pnp != NULL)
623 kfree(pnp, M_BUS);
624 if (tmp != NULL)
625 kfree(tmp, M_BUS);
626 return;
630 * A device was removed from the tree. We are called just before this
631 * happens.
633 static void
634 devremoved(device_t dev)
636 char *pnp = NULL;
637 char *tmp = NULL;
639 pnp = kmalloc(1024, M_BUS, M_NOWAIT);
640 if (pnp == NULL)
641 goto fail;
642 tmp = kmalloc(1024, M_BUS, M_NOWAIT);
643 if (tmp == NULL)
644 goto fail;
645 *pnp = '\0';
646 bus_child_pnpinfo_str(dev, pnp, 1024);
647 ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
648 devaddq("-", tmp, dev);
649 fail:
650 if (pnp != NULL)
651 kfree(pnp, M_BUS);
652 if (tmp != NULL)
653 kfree(tmp, M_BUS);
654 return;
658 * Called when there's no match for this device. This is only called
659 * the first time that no match happens, so we don't keep getitng this
660 * message. Should that prove to be undesirable, we can change it.
661 * This is called when all drivers that can attach to a given bus
662 * decline to accept this device. Other errrors may not be detected.
664 static void
665 devnomatch(device_t dev)
667 devaddq("?", "", dev);
670 static int
671 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
673 struct dev_event_info *n1;
674 int dis, error;
676 dis = devctl_disable;
677 error = sysctl_handle_int(oidp, &dis, 0, req);
678 if (error || !req->newptr)
679 return (error);
680 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
681 devctl_disable = dis;
682 if (dis) {
683 while (!TAILQ_EMPTY(&devsoftc.devq)) {
684 n1 = TAILQ_FIRST(&devsoftc.devq);
685 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
686 kfree(n1->dei_data, M_BUS);
687 kfree(n1, M_BUS);
690 lockmgr(&devsoftc.lock, LK_RELEASE);
691 return (0);
694 /* End of /dev/devctl code */
696 TAILQ_HEAD(,device) bus_data_devices;
697 static int bus_data_generation = 1;
699 kobj_method_t null_methods[] = {
700 { 0, 0 }
703 DEFINE_CLASS(null, null_methods, 0);
706 * Devclass implementation
709 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
711 static devclass_t
712 devclass_find_internal(const char *classname, const char *parentname,
713 int create)
715 devclass_t dc;
717 PDEBUG(("looking for %s", classname));
718 if (classname == NULL)
719 return(NULL);
721 TAILQ_FOREACH(dc, &devclasses, link)
722 if (!strcmp(dc->name, classname))
723 break;
725 if (create && !dc) {
726 PDEBUG(("creating %s", classname));
727 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
728 M_BUS, M_INTWAIT | M_ZERO);
729 dc->parent = NULL;
730 dc->name = (char*) (dc + 1);
731 strcpy(dc->name, classname);
732 dc->devices = NULL;
733 dc->maxunit = 0;
734 TAILQ_INIT(&dc->drivers);
735 TAILQ_INSERT_TAIL(&devclasses, dc, link);
737 bus_data_generation_update();
742 * If a parent class is specified, then set that as our parent so
743 * that this devclass will support drivers for the parent class as
744 * well. If the parent class has the same name don't do this though
745 * as it creates a cycle that can trigger an infinite loop in
746 * device_probe_child() if a device exists for which there is no
747 * suitable driver.
749 if (parentname && dc && !dc->parent &&
750 strcmp(classname, parentname) != 0)
751 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
753 return(dc);
756 devclass_t
757 devclass_create(const char *classname)
759 return(devclass_find_internal(classname, NULL, TRUE));
762 devclass_t
763 devclass_find(const char *classname)
765 return(devclass_find_internal(classname, NULL, FALSE));
768 device_t
769 devclass_find_unit(const char *classname, int unit)
771 devclass_t dc;
773 if ((dc = devclass_find(classname)) != NULL)
774 return(devclass_get_device(dc, unit));
775 return (NULL);
779 devclass_add_driver(devclass_t dc, driver_t *driver)
781 driverlink_t dl;
782 device_t dev;
783 int i;
785 PDEBUG(("%s", DRIVERNAME(driver)));
787 dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
790 * Compile the driver's methods. Also increase the reference count
791 * so that the class doesn't get freed when the last instance
792 * goes. This means we can safely use static methods and avoids a
793 * double-free in devclass_delete_driver.
795 kobj_class_instantiate(driver);
798 * Make sure the devclass which the driver is implementing exists.
800 devclass_find_internal(driver->name, NULL, TRUE);
802 dl->driver = driver;
803 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
806 * Call BUS_DRIVER_ADDED for any existing busses in this class,
807 * but only if the bus has already been attached (otherwise we
808 * might probe too early).
810 * This is what will cause a newly loaded module to be associated
811 * with hardware. bus_generic_driver_added() is typically what ends
812 * up being called.
814 for (i = 0; i < dc->maxunit; i++) {
815 if ((dev = dc->devices[i]) != NULL) {
816 if (dev->state >= DS_ATTACHED)
817 BUS_DRIVER_ADDED(dev, driver);
821 bus_data_generation_update();
822 return(0);
826 devclass_delete_driver(devclass_t busclass, driver_t *driver)
828 devclass_t dc = devclass_find(driver->name);
829 driverlink_t dl;
830 device_t dev;
831 int i;
832 int error;
834 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
836 if (!dc)
837 return(0);
840 * Find the link structure in the bus' list of drivers.
842 TAILQ_FOREACH(dl, &busclass->drivers, link)
843 if (dl->driver == driver)
844 break;
846 if (!dl) {
847 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
848 return(ENOENT);
852 * Disassociate from any devices. We iterate through all the
853 * devices in the devclass of the driver and detach any which are
854 * using the driver and which have a parent in the devclass which
855 * we are deleting from.
857 * Note that since a driver can be in multiple devclasses, we
858 * should not detach devices which are not children of devices in
859 * the affected devclass.
861 for (i = 0; i < dc->maxunit; i++)
862 if (dc->devices[i]) {
863 dev = dc->devices[i];
864 if (dev->driver == driver && dev->parent &&
865 dev->parent->devclass == busclass) {
866 if ((error = device_detach(dev)) != 0)
867 return(error);
868 device_set_driver(dev, NULL);
872 TAILQ_REMOVE(&busclass->drivers, dl, link);
873 kfree(dl, M_BUS);
875 kobj_class_uninstantiate(driver);
877 bus_data_generation_update();
878 return(0);
881 static driverlink_t
882 devclass_find_driver_internal(devclass_t dc, const char *classname)
884 driverlink_t dl;
886 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
888 TAILQ_FOREACH(dl, &dc->drivers, link)
889 if (!strcmp(dl->driver->name, classname))
890 return(dl);
892 PDEBUG(("not found"));
893 return(NULL);
896 kobj_class_t
897 devclass_find_driver(devclass_t dc, const char *classname)
899 driverlink_t dl;
901 dl = devclass_find_driver_internal(dc, classname);
902 if (dl)
903 return(dl->driver);
904 else
905 return(NULL);
908 const char *
909 devclass_get_name(devclass_t dc)
911 return(dc->name);
914 device_t
915 devclass_get_device(devclass_t dc, int unit)
917 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
918 return(NULL);
919 return(dc->devices[unit]);
922 void *
923 devclass_get_softc(devclass_t dc, int unit)
925 device_t dev;
927 dev = devclass_get_device(dc, unit);
928 if (!dev)
929 return(NULL);
931 return(device_get_softc(dev));
935 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
937 int i;
938 int count;
939 device_t *list;
941 count = 0;
942 for (i = 0; i < dc->maxunit; i++)
943 if (dc->devices[i])
944 count++;
946 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
948 count = 0;
949 for (i = 0; i < dc->maxunit; i++)
950 if (dc->devices[i]) {
951 list[count] = dc->devices[i];
952 count++;
955 *devlistp = list;
956 *devcountp = count;
958 return(0);
962 * @brief Get a list of drivers in the devclass
964 * An array containing a list of pointers to all the drivers in the
965 * given devclass is allocated and returned in @p *listp. The number
966 * of drivers in the array is returned in @p *countp. The caller should
967 * free the array using @c free(p, M_TEMP).
969 * @param dc the devclass to examine
970 * @param listp gives location for array pointer return value
971 * @param countp gives location for number of array elements
972 * return value
974 * @retval 0 success
975 * @retval ENOMEM the array allocation failed
978 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
980 driverlink_t dl;
981 driver_t **list;
982 int count;
984 count = 0;
985 TAILQ_FOREACH(dl, &dc->drivers, link)
986 count++;
987 list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
988 if (list == NULL)
989 return (ENOMEM);
991 count = 0;
992 TAILQ_FOREACH(dl, &dc->drivers, link) {
993 list[count] = dl->driver;
994 count++;
996 *listp = list;
997 *countp = count;
999 return (0);
1003 * @brief Get the number of devices in a devclass
1005 * @param dc the devclass to examine
1008 devclass_get_count(devclass_t dc)
1010 int count, i;
1012 count = 0;
1013 for (i = 0; i < dc->maxunit; i++)
1014 if (dc->devices[i])
1015 count++;
1016 return (count);
1020 devclass_get_maxunit(devclass_t dc)
1022 return(dc->maxunit);
1025 void
1026 devclass_set_parent(devclass_t dc, devclass_t pdc)
1028 dc->parent = pdc;
1031 devclass_t
1032 devclass_get_parent(devclass_t dc)
1034 return(dc->parent);
1037 static int
1038 devclass_alloc_unit(devclass_t dc, int *unitp)
1040 int unit = *unitp;
1042 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
1044 /* If we have been given a wired unit number, check for existing device */
1045 if (unit != -1) {
1046 if (unit >= 0 && unit < dc->maxunit &&
1047 dc->devices[unit] != NULL) {
1048 if (bootverbose)
1049 kprintf("%s-: %s%d exists, using next available unit number\n",
1050 dc->name, dc->name, unit);
1051 /* find the next available slot */
1052 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
1055 } else {
1056 /* Unwired device, find the next available slot for it */
1057 unit = 0;
1058 while (unit < dc->maxunit && dc->devices[unit] != NULL)
1059 unit++;
1063 * We've selected a unit beyond the length of the table, so let's
1064 * extend the table to make room for all units up to and including
1065 * this one.
1067 if (unit >= dc->maxunit) {
1068 device_t *newlist;
1069 int newsize;
1071 newsize = (unit + 1);
1072 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
1073 M_INTWAIT | M_ZERO);
1074 if (newlist == NULL)
1075 return(ENOMEM);
1076 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
1077 if (dc->devices)
1078 kfree(dc->devices, M_BUS);
1079 dc->devices = newlist;
1080 dc->maxunit = newsize;
1082 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
1084 *unitp = unit;
1085 return(0);
1088 static int
1089 devclass_add_device(devclass_t dc, device_t dev)
1091 int buflen, error;
1093 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1095 buflen = strlen(dc->name) + 5;
1096 dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
1097 if (dev->nameunit == NULL)
1098 return(ENOMEM);
1100 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
1101 kfree(dev->nameunit, M_BUS);
1102 dev->nameunit = NULL;
1103 return(error);
1105 dc->devices[dev->unit] = dev;
1106 dev->devclass = dc;
1107 ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
1109 return(0);
1112 static int
1113 devclass_delete_device(devclass_t dc, device_t dev)
1115 if (!dc || !dev)
1116 return(0);
1118 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
1120 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
1121 panic("devclass_delete_device: inconsistent device class");
1122 dc->devices[dev->unit] = NULL;
1123 if (dev->flags & DF_WILDCARD)
1124 dev->unit = -1;
1125 dev->devclass = NULL;
1126 kfree(dev->nameunit, M_BUS);
1127 dev->nameunit = NULL;
1129 return(0);
1132 static device_t
1133 make_device(device_t parent, const char *name, int unit)
1135 device_t dev;
1136 devclass_t dc;
1138 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1140 if (name != NULL) {
1141 dc = devclass_find_internal(name, NULL, TRUE);
1142 if (!dc) {
1143 kprintf("make_device: can't find device class %s\n", name);
1144 return(NULL);
1146 } else
1147 dc = NULL;
1149 dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1150 if (!dev)
1151 return(0);
1153 dev->parent = parent;
1154 TAILQ_INIT(&dev->children);
1155 kobj_init((kobj_t) dev, &null_class);
1156 dev->driver = NULL;
1157 dev->devclass = NULL;
1158 dev->unit = unit;
1159 dev->nameunit = NULL;
1160 dev->desc = NULL;
1161 dev->busy = 0;
1162 dev->devflags = 0;
1163 dev->flags = DF_ENABLED;
1164 dev->order = 0;
1165 if (unit == -1)
1166 dev->flags |= DF_WILDCARD;
1167 if (name) {
1168 dev->flags |= DF_FIXEDCLASS;
1169 if (devclass_add_device(dc, dev) != 0) {
1170 kobj_delete((kobj_t)dev, M_BUS);
1171 return(NULL);
1174 dev->ivars = NULL;
1175 dev->softc = NULL;
1177 dev->state = DS_NOTPRESENT;
1179 TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1180 bus_data_generation_update();
1182 return(dev);
1185 static int
1186 device_print_child(device_t dev, device_t child)
1188 int retval = 0;
1190 if (device_is_alive(child))
1191 retval += BUS_PRINT_CHILD(dev, child);
1192 else
1193 retval += device_printf(child, " not found\n");
1195 return(retval);
1198 device_t
1199 device_add_child(device_t dev, const char *name, int unit)
1201 return device_add_child_ordered(dev, 0, name, unit);
1204 device_t
1205 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1207 device_t child;
1208 device_t place;
1210 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1211 order, unit));
1213 child = make_device(dev, name, unit);
1214 if (child == NULL)
1215 return child;
1216 child->order = order;
1218 TAILQ_FOREACH(place, &dev->children, link)
1219 if (place->order > order)
1220 break;
1222 if (place) {
1224 * The device 'place' is the first device whose order is
1225 * greater than the new child.
1227 TAILQ_INSERT_BEFORE(place, child, link);
1228 } else {
1230 * The new child's order is greater or equal to the order of
1231 * any existing device. Add the child to the tail of the list.
1233 TAILQ_INSERT_TAIL(&dev->children, child, link);
1236 bus_data_generation_update();
1237 return(child);
1241 device_delete_child(device_t dev, device_t child)
1243 int error;
1244 device_t grandchild;
1246 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1248 /* remove children first */
1249 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1250 error = device_delete_child(child, grandchild);
1251 if (error)
1252 return(error);
1255 if ((error = device_detach(child)) != 0)
1256 return(error);
1257 if (child->devclass)
1258 devclass_delete_device(child->devclass, child);
1259 TAILQ_REMOVE(&dev->children, child, link);
1260 TAILQ_REMOVE(&bus_data_devices, child, devlink);
1261 kobj_delete((kobj_t)child, M_BUS);
1263 bus_data_generation_update();
1264 return(0);
1268 * @brief Delete all children devices of the given device, if any.
1270 * This function deletes all children devices of the given device, if
1271 * any, using the device_delete_child() function for each device it
1272 * finds. If a child device cannot be deleted, this function will
1273 * return an error code.
1275 * @param dev the parent device
1277 * @retval 0 success
1278 * @retval non-zero a device would not detach
1281 device_delete_children(device_t dev)
1283 device_t child;
1284 int error;
1286 PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1288 error = 0;
1290 while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1291 error = device_delete_child(dev, child);
1292 if (error) {
1293 PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1294 break;
1297 return (error);
1301 * @brief Find a device given a unit number
1303 * This is similar to devclass_get_devices() but only searches for
1304 * devices which have @p dev as a parent.
1306 * @param dev the parent device to search
1307 * @param unit the unit number to search for. If the unit is -1,
1308 * return the first child of @p dev which has name
1309 * @p classname (that is, the one with the lowest unit.)
1311 * @returns the device with the given unit number or @c
1312 * NULL if there is no such device
1314 device_t
1315 device_find_child(device_t dev, const char *classname, int unit)
1317 devclass_t dc;
1318 device_t child;
1320 dc = devclass_find(classname);
1321 if (!dc)
1322 return(NULL);
1324 if (unit != -1) {
1325 child = devclass_get_device(dc, unit);
1326 if (child && child->parent == dev)
1327 return (child);
1328 } else {
1329 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1330 child = devclass_get_device(dc, unit);
1331 if (child && child->parent == dev)
1332 return (child);
1335 return(NULL);
1338 static driverlink_t
1339 first_matching_driver(devclass_t dc, device_t dev)
1341 if (dev->devclass)
1342 return(devclass_find_driver_internal(dc, dev->devclass->name));
1343 else
1344 return(TAILQ_FIRST(&dc->drivers));
1347 static driverlink_t
1348 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1350 if (dev->devclass) {
1351 driverlink_t dl;
1352 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1353 if (!strcmp(dev->devclass->name, dl->driver->name))
1354 return(dl);
1355 return(NULL);
1356 } else
1357 return(TAILQ_NEXT(last, link));
1361 device_probe_child(device_t dev, device_t child)
1363 devclass_t dc;
1364 driverlink_t best = NULL;
1365 driverlink_t dl;
1366 int result, pri = 0;
1367 int hasclass = (child->devclass != NULL);
1369 dc = dev->devclass;
1370 if (!dc)
1371 panic("device_probe_child: parent device has no devclass");
1373 if (child->state == DS_ALIVE)
1374 return(0);
1376 for (; dc; dc = dc->parent) {
1377 for (dl = first_matching_driver(dc, child); dl;
1378 dl = next_matching_driver(dc, child, dl)) {
1379 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1380 device_set_driver(child, dl->driver);
1381 if (!hasclass)
1382 device_set_devclass(child, dl->driver->name);
1383 result = DEVICE_PROBE(child);
1384 if (!hasclass)
1385 device_set_devclass(child, 0);
1388 * If the driver returns SUCCESS, there can be
1389 * no higher match for this device.
1391 if (result == 0) {
1392 best = dl;
1393 pri = 0;
1394 break;
1398 * The driver returned an error so it
1399 * certainly doesn't match.
1401 if (result > 0) {
1402 device_set_driver(child, NULL);
1403 continue;
1407 * A priority lower than SUCCESS, remember the
1408 * best matching driver. Initialise the value
1409 * of pri for the first match.
1411 if (best == NULL || result > pri) {
1412 best = dl;
1413 pri = result;
1414 continue;
1418 * If we have unambiguous match in this devclass,
1419 * don't look in the parent.
1421 if (best && pri == 0)
1422 break;
1426 * If we found a driver, change state and initialise the devclass.
1428 if (best) {
1429 if (!child->devclass)
1430 device_set_devclass(child, best->driver->name);
1431 device_set_driver(child, best->driver);
1432 if (pri < 0) {
1434 * A bit bogus. Call the probe method again to make
1435 * sure that we have the right description.
1437 DEVICE_PROBE(child);
1440 bus_data_generation_update();
1441 child->state = DS_ALIVE;
1442 return(0);
1445 return(ENXIO);
1448 device_t
1449 device_get_parent(device_t dev)
1451 return dev->parent;
1455 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1457 int count;
1458 device_t child;
1459 device_t *list;
1461 count = 0;
1462 TAILQ_FOREACH(child, &dev->children, link)
1463 count++;
1465 list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1467 count = 0;
1468 TAILQ_FOREACH(child, &dev->children, link) {
1469 list[count] = child;
1470 count++;
1473 *devlistp = list;
1474 *devcountp = count;
1476 return(0);
1479 driver_t *
1480 device_get_driver(device_t dev)
1482 return(dev->driver);
1485 devclass_t
1486 device_get_devclass(device_t dev)
1488 return(dev->devclass);
1491 const char *
1492 device_get_name(device_t dev)
1494 if (dev->devclass)
1495 return devclass_get_name(dev->devclass);
1496 return(NULL);
1499 const char *
1500 device_get_nameunit(device_t dev)
1502 return(dev->nameunit);
1506 device_get_unit(device_t dev)
1508 return(dev->unit);
1511 const char *
1512 device_get_desc(device_t dev)
1514 return(dev->desc);
1517 uint32_t
1518 device_get_flags(device_t dev)
1520 return(dev->devflags);
1523 struct sysctl_ctx_list *
1524 device_get_sysctl_ctx(device_t dev)
1526 return (&dev->sysctl_ctx);
1529 struct sysctl_oid *
1530 device_get_sysctl_tree(device_t dev)
1532 return (dev->sysctl_tree);
1536 device_print_prettyname(device_t dev)
1538 const char *name = device_get_name(dev);
1540 if (name == NULL)
1541 return kprintf("unknown: ");
1542 else
1543 return kprintf("%s%d: ", name, device_get_unit(dev));
1547 device_printf(device_t dev, const char * fmt, ...)
1549 __va_list ap;
1550 int retval;
1552 retval = device_print_prettyname(dev);
1553 __va_start(ap, fmt);
1554 retval += kvprintf(fmt, ap);
1555 __va_end(ap);
1556 return retval;
1559 static void
1560 device_set_desc_internal(device_t dev, const char* desc, int copy)
1562 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1563 kfree(dev->desc, M_BUS);
1564 dev->flags &= ~DF_DESCMALLOCED;
1565 dev->desc = NULL;
1568 if (copy && desc) {
1569 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1570 if (dev->desc) {
1571 strcpy(dev->desc, desc);
1572 dev->flags |= DF_DESCMALLOCED;
1574 } else {
1575 /* Avoid a -Wcast-qual warning */
1576 dev->desc = (char *)(uintptr_t) desc;
1579 bus_data_generation_update();
1582 void
1583 device_set_desc(device_t dev, const char* desc)
1585 device_set_desc_internal(dev, desc, FALSE);
1588 void
1589 device_set_desc_copy(device_t dev, const char* desc)
1591 device_set_desc_internal(dev, desc, TRUE);
1594 void
1595 device_set_flags(device_t dev, uint32_t flags)
1597 dev->devflags = flags;
1600 void *
1601 device_get_softc(device_t dev)
1603 return dev->softc;
1606 void
1607 device_set_softc(device_t dev, void *softc)
1609 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1610 kfree(dev->softc, M_BUS);
1611 dev->softc = softc;
1612 if (dev->softc)
1613 dev->flags |= DF_EXTERNALSOFTC;
1614 else
1615 dev->flags &= ~DF_EXTERNALSOFTC;
1618 void
1619 device_set_async_attach(device_t dev, int enable)
1621 if (enable)
1622 dev->flags |= DF_ASYNCPROBE;
1623 else
1624 dev->flags &= ~DF_ASYNCPROBE;
1627 void *
1628 device_get_ivars(device_t dev)
1630 return dev->ivars;
1633 void
1634 device_set_ivars(device_t dev, void * ivars)
1636 if (!dev)
1637 return;
1639 dev->ivars = ivars;
1642 device_state_t
1643 device_get_state(device_t dev)
1645 return(dev->state);
1648 void
1649 device_enable(device_t dev)
1651 dev->flags |= DF_ENABLED;
1654 void
1655 device_disable(device_t dev)
1657 dev->flags &= ~DF_ENABLED;
1661 * YYY cannot block
1663 void
1664 device_busy(device_t dev)
1666 if (dev->state < DS_ATTACHED)
1667 panic("device_busy: called for unattached device");
1668 if (dev->busy == 0 && dev->parent)
1669 device_busy(dev->parent);
1670 dev->busy++;
1671 dev->state = DS_BUSY;
1675 * YYY cannot block
1677 void
1678 device_unbusy(device_t dev)
1680 if (dev->state != DS_BUSY)
1681 panic("device_unbusy: called for non-busy device");
1682 dev->busy--;
1683 if (dev->busy == 0) {
1684 if (dev->parent)
1685 device_unbusy(dev->parent);
1686 dev->state = DS_ATTACHED;
1690 void
1691 device_quiet(device_t dev)
1693 dev->flags |= DF_QUIET;
1696 void
1697 device_verbose(device_t dev)
1699 dev->flags &= ~DF_QUIET;
1703 device_is_quiet(device_t dev)
1705 return((dev->flags & DF_QUIET) != 0);
1709 device_is_enabled(device_t dev)
1711 return((dev->flags & DF_ENABLED) != 0);
1715 device_is_alive(device_t dev)
1717 return(dev->state >= DS_ALIVE);
1721 device_is_attached(device_t dev)
1723 return(dev->state >= DS_ATTACHED);
1727 device_set_devclass(device_t dev, const char *classname)
1729 devclass_t dc;
1730 int error;
1732 if (!classname) {
1733 if (dev->devclass)
1734 devclass_delete_device(dev->devclass, dev);
1735 return(0);
1738 if (dev->devclass) {
1739 kprintf("device_set_devclass: device class already set\n");
1740 return(EINVAL);
1743 dc = devclass_find_internal(classname, NULL, TRUE);
1744 if (!dc)
1745 return(ENOMEM);
1747 error = devclass_add_device(dc, dev);
1749 bus_data_generation_update();
1750 return(error);
1754 device_set_driver(device_t dev, driver_t *driver)
1756 if (dev->state >= DS_ATTACHED)
1757 return(EBUSY);
1759 if (dev->driver == driver)
1760 return(0);
1762 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1763 kfree(dev->softc, M_BUS);
1764 dev->softc = NULL;
1766 device_set_desc(dev, NULL);
1767 kobj_delete((kobj_t) dev, 0);
1768 dev->driver = driver;
1769 if (driver) {
1770 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1771 if (!(dev->flags & DF_EXTERNALSOFTC))
1772 dev->softc = kmalloc(driver->size, M_BUS,
1773 M_INTWAIT | M_ZERO);
1774 } else {
1775 kobj_init((kobj_t) dev, &null_class);
1778 bus_data_generation_update();
1779 return(0);
1783 device_probe_and_attach(device_t dev)
1785 device_t bus = dev->parent;
1786 int error = 0;
1788 if (dev->state >= DS_ALIVE)
1789 return(0);
1791 if ((dev->flags & DF_ENABLED) == 0) {
1792 if (bootverbose) {
1793 device_print_prettyname(dev);
1794 kprintf("not probed (disabled)\n");
1796 return(0);
1799 error = device_probe_child(bus, dev);
1800 if (error) {
1801 if (!(dev->flags & DF_DONENOMATCH)) {
1802 BUS_PROBE_NOMATCH(bus, dev);
1803 devnomatch(dev);
1804 dev->flags |= DF_DONENOMATCH;
1806 return(error);
1810 * Output the exact device chain prior to the attach in case the
1811 * system locks up during attach, and generate the full info after
1812 * the attach so correct irq and other information is displayed.
1814 if (bootverbose && !device_is_quiet(dev)) {
1815 device_t tmp;
1817 kprintf("%s", device_get_nameunit(dev));
1818 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1819 kprintf(".%s", device_get_nameunit(tmp));
1820 kprintf("\n");
1822 if (!device_is_quiet(dev))
1823 device_print_child(bus, dev);
1824 if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1825 kprintf("%s: probing asynchronously\n",
1826 device_get_nameunit(dev));
1827 dev->state = DS_INPROGRESS;
1828 device_attach_async(dev);
1829 error = 0;
1830 } else {
1831 error = device_doattach(dev);
1833 return(error);
1837 * Device is known to be alive, do the attach asynchronously.
1838 * However, serialize the attaches with the mp lock.
1840 static void
1841 device_attach_async(device_t dev)
1843 thread_t td;
1845 atomic_add_int(&numasyncthreads, 1);
1846 lwkt_create(device_attach_thread, dev, &td, NULL,
1847 0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
1850 static void
1851 device_attach_thread(void *arg)
1853 device_t dev = arg;
1855 (void)device_doattach(dev);
1856 atomic_subtract_int(&numasyncthreads, 1);
1857 wakeup(&numasyncthreads);
1861 * Device is known to be alive, do the attach (synchronous or asynchronous)
1863 static int
1864 device_doattach(device_t dev)
1866 device_t bus = dev->parent;
1867 int hasclass = (dev->devclass != NULL);
1868 int error;
1870 device_sysctl_init(dev);
1871 error = DEVICE_ATTACH(dev);
1872 if (error == 0) {
1873 dev->state = DS_ATTACHED;
1874 if (bootverbose && !device_is_quiet(dev))
1875 device_print_child(bus, dev);
1876 device_sysctl_update(dev);
1877 devadded(dev);
1878 } else {
1879 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1880 dev->driver->name, dev->unit, error);
1881 /* Unset the class that was set in device_probe_child */
1882 if (!hasclass)
1883 device_set_devclass(dev, 0);
1884 device_set_driver(dev, NULL);
1885 dev->state = DS_NOTPRESENT;
1886 device_sysctl_fini(dev);
1888 return(error);
1892 device_detach(device_t dev)
1894 int error;
1896 PDEBUG(("%s", DEVICENAME(dev)));
1897 if (dev->state == DS_BUSY)
1898 return(EBUSY);
1899 if (dev->state != DS_ATTACHED)
1900 return(0);
1902 if ((error = DEVICE_DETACH(dev)) != 0)
1903 return(error);
1904 devremoved(dev);
1905 device_printf(dev, "detached\n");
1906 if (dev->parent)
1907 BUS_CHILD_DETACHED(dev->parent, dev);
1909 if (!(dev->flags & DF_FIXEDCLASS))
1910 devclass_delete_device(dev->devclass, dev);
1912 dev->state = DS_NOTPRESENT;
1913 device_set_driver(dev, NULL);
1914 device_sysctl_fini(dev);
1916 return(0);
1920 device_shutdown(device_t dev)
1922 if (dev->state < DS_ATTACHED)
1923 return 0;
1924 PDEBUG(("%s", DEVICENAME(dev)));
1925 return DEVICE_SHUTDOWN(dev);
1929 device_set_unit(device_t dev, int unit)
1931 devclass_t dc;
1932 int err;
1934 dc = device_get_devclass(dev);
1935 if (unit < dc->maxunit && dc->devices[unit])
1936 return(EBUSY);
1937 err = devclass_delete_device(dc, dev);
1938 if (err)
1939 return(err);
1940 dev->unit = unit;
1941 err = devclass_add_device(dc, dev);
1942 if (err)
1943 return(err);
1945 bus_data_generation_update();
1946 return(0);
1949 /*======================================*/
1951 * Access functions for device resources.
1954 /* Supplied by config(8) in ioconf.c */
1955 extern struct config_device config_devtab[];
1956 extern int devtab_count;
1958 /* Runtime version */
1959 struct config_device *devtab = config_devtab;
1961 static int
1962 resource_new_name(const char *name, int unit)
1964 struct config_device *new;
1966 new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1967 M_INTWAIT | M_ZERO);
1968 if (devtab && devtab_count > 0)
1969 bcopy(devtab, new, devtab_count * sizeof(*new));
1970 new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1971 if (new[devtab_count].name == NULL) {
1972 kfree(new, M_TEMP);
1973 return(-1);
1975 strcpy(new[devtab_count].name, name);
1976 new[devtab_count].unit = unit;
1977 new[devtab_count].resource_count = 0;
1978 new[devtab_count].resources = NULL;
1979 if (devtab && devtab != config_devtab)
1980 kfree(devtab, M_TEMP);
1981 devtab = new;
1982 return devtab_count++;
1985 static int
1986 resource_new_resname(int j, const char *resname, resource_type type)
1988 struct config_resource *new;
1989 int i;
1991 i = devtab[j].resource_count;
1992 new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1993 if (devtab[j].resources && i > 0)
1994 bcopy(devtab[j].resources, new, i * sizeof(*new));
1995 new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1996 if (new[i].name == NULL) {
1997 kfree(new, M_TEMP);
1998 return(-1);
2000 strcpy(new[i].name, resname);
2001 new[i].type = type;
2002 if (devtab[j].resources)
2003 kfree(devtab[j].resources, M_TEMP);
2004 devtab[j].resources = new;
2005 devtab[j].resource_count = i + 1;
2006 return(i);
2009 static int
2010 resource_match_string(int i, const char *resname, const char *value)
2012 int j;
2013 struct config_resource *res;
2015 for (j = 0, res = devtab[i].resources;
2016 j < devtab[i].resource_count; j++, res++)
2017 if (!strcmp(res->name, resname)
2018 && res->type == RES_STRING
2019 && !strcmp(res->u.stringval, value))
2020 return(j);
2021 return(-1);
2024 static int
2025 resource_find(const char *name, int unit, const char *resname,
2026 struct config_resource **result)
2028 int i, j;
2029 struct config_resource *res;
2032 * First check specific instances, then generic.
2034 for (i = 0; i < devtab_count; i++) {
2035 if (devtab[i].unit < 0)
2036 continue;
2037 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2038 res = devtab[i].resources;
2039 for (j = 0; j < devtab[i].resource_count; j++, res++)
2040 if (!strcmp(res->name, resname)) {
2041 *result = res;
2042 return(0);
2046 for (i = 0; i < devtab_count; i++) {
2047 if (devtab[i].unit >= 0)
2048 continue;
2049 /* XXX should this `&& devtab[i].unit == unit' be here? */
2050 /* XXX if so, then the generic match does nothing */
2051 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2052 res = devtab[i].resources;
2053 for (j = 0; j < devtab[i].resource_count; j++, res++)
2054 if (!strcmp(res->name, resname)) {
2055 *result = res;
2056 return(0);
2060 return(ENOENT);
2063 static int
2064 resource_kenv(const char *name, int unit, const char *resname, long *result)
2066 const char *env;
2067 char buf[64];
2069 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2070 if ((env = kgetenv(buf)) != NULL) {
2071 *result = strtol(env, NULL, 0);
2072 return(0);
2074 return (ENOENT);
2078 resource_int_value(const char *name, int unit, const char *resname, int *result)
2080 struct config_resource *res;
2081 long kvalue = 0;
2082 int error;
2084 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2085 *result = (int)kvalue;
2086 return 0;
2088 if ((error = resource_find(name, unit, resname, &res)) != 0)
2089 return(error);
2090 if (res->type != RES_INT)
2091 return(EFTYPE);
2092 *result = res->u.intval;
2093 return(0);
2097 resource_long_value(const char *name, int unit, const char *resname,
2098 long *result)
2100 struct config_resource *res;
2101 long kvalue;
2102 int error;
2104 if (resource_kenv(name, unit, resname, &kvalue) == 0) {
2105 *result = kvalue;
2106 return 0;
2108 if ((error = resource_find(name, unit, resname, &res)) != 0)
2109 return(error);
2110 if (res->type != RES_LONG)
2111 return(EFTYPE);
2112 *result = res->u.longval;
2113 return(0);
2117 resource_string_value(const char *name, int unit, const char *resname,
2118 const char **result)
2120 int error;
2121 struct config_resource *res;
2122 char buf[64];
2123 const char *env;
2125 ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
2126 if ((env = kgetenv(buf)) != NULL) {
2127 *result = env;
2128 return 0;
2131 if ((error = resource_find(name, unit, resname, &res)) != 0)
2132 return(error);
2133 if (res->type != RES_STRING)
2134 return(EFTYPE);
2135 *result = res->u.stringval;
2136 return(0);
2140 resource_query_string(int i, const char *resname, const char *value)
2142 if (i < 0)
2143 i = 0;
2144 else
2145 i = i + 1;
2146 for (; i < devtab_count; i++)
2147 if (resource_match_string(i, resname, value) >= 0)
2148 return(i);
2149 return(-1);
2153 resource_locate(int i, const char *resname)
2155 if (i < 0)
2156 i = 0;
2157 else
2158 i = i + 1;
2159 for (; i < devtab_count; i++)
2160 if (!strcmp(devtab[i].name, resname))
2161 return(i);
2162 return(-1);
2166 resource_count(void)
2168 return(devtab_count);
2171 char *
2172 resource_query_name(int i)
2174 return(devtab[i].name);
2178 resource_query_unit(int i)
2180 return(devtab[i].unit);
2183 static int
2184 resource_create(const char *name, int unit, const char *resname,
2185 resource_type type, struct config_resource **result)
2187 int i, j;
2188 struct config_resource *res = NULL;
2190 for (i = 0; i < devtab_count; i++)
2191 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2192 res = devtab[i].resources;
2193 break;
2195 if (res == NULL) {
2196 i = resource_new_name(name, unit);
2197 if (i < 0)
2198 return(ENOMEM);
2199 res = devtab[i].resources;
2201 for (j = 0; j < devtab[i].resource_count; j++, res++)
2202 if (!strcmp(res->name, resname)) {
2203 *result = res;
2204 return(0);
2206 j = resource_new_resname(i, resname, type);
2207 if (j < 0)
2208 return(ENOMEM);
2209 res = &devtab[i].resources[j];
2210 *result = res;
2211 return(0);
2215 resource_set_int(const char *name, int unit, const char *resname, int value)
2217 int error;
2218 struct config_resource *res;
2220 error = resource_create(name, unit, resname, RES_INT, &res);
2221 if (error)
2222 return(error);
2223 if (res->type != RES_INT)
2224 return(EFTYPE);
2225 res->u.intval = value;
2226 return(0);
2230 resource_set_long(const char *name, int unit, const char *resname, long value)
2232 int error;
2233 struct config_resource *res;
2235 error = resource_create(name, unit, resname, RES_LONG, &res);
2236 if (error)
2237 return(error);
2238 if (res->type != RES_LONG)
2239 return(EFTYPE);
2240 res->u.longval = value;
2241 return(0);
2245 resource_set_string(const char *name, int unit, const char *resname,
2246 const char *value)
2248 int error;
2249 struct config_resource *res;
2251 error = resource_create(name, unit, resname, RES_STRING, &res);
2252 if (error)
2253 return(error);
2254 if (res->type != RES_STRING)
2255 return(EFTYPE);
2256 if (res->u.stringval)
2257 kfree(res->u.stringval, M_TEMP);
2258 res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2259 if (res->u.stringval == NULL)
2260 return(ENOMEM);
2261 strcpy(res->u.stringval, value);
2262 return(0);
2265 static void
2266 resource_cfgload(void *dummy __unused)
2268 struct config_resource *res, *cfgres;
2269 int i, j;
2270 int error;
2271 char *name, *resname;
2272 int unit;
2273 resource_type type;
2274 char *stringval;
2275 int config_devtab_count;
2277 config_devtab_count = devtab_count;
2278 devtab = NULL;
2279 devtab_count = 0;
2281 for (i = 0; i < config_devtab_count; i++) {
2282 name = config_devtab[i].name;
2283 unit = config_devtab[i].unit;
2285 for (j = 0; j < config_devtab[i].resource_count; j++) {
2286 cfgres = config_devtab[i].resources;
2287 resname = cfgres[j].name;
2288 type = cfgres[j].type;
2289 error = resource_create(name, unit, resname, type,
2290 &res);
2291 if (error) {
2292 kprintf("create resource %s%d: error %d\n",
2293 name, unit, error);
2294 continue;
2296 if (res->type != type) {
2297 kprintf("type mismatch %s%d: %d != %d\n",
2298 name, unit, res->type, type);
2299 continue;
2301 switch (type) {
2302 case RES_INT:
2303 res->u.intval = cfgres[j].u.intval;
2304 break;
2305 case RES_LONG:
2306 res->u.longval = cfgres[j].u.longval;
2307 break;
2308 case RES_STRING:
2309 if (res->u.stringval)
2310 kfree(res->u.stringval, M_TEMP);
2311 stringval = cfgres[j].u.stringval;
2312 res->u.stringval = kmalloc(strlen(stringval) + 1,
2313 M_TEMP, M_INTWAIT);
2314 if (res->u.stringval == NULL)
2315 break;
2316 strcpy(res->u.stringval, stringval);
2317 break;
2318 default:
2319 panic("unknown resource type %d", type);
2324 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0);
2327 /*======================================*/
2329 * Some useful method implementations to make life easier for bus drivers.
2332 void
2333 resource_list_init(struct resource_list *rl)
2335 SLIST_INIT(rl);
2338 void
2339 resource_list_free(struct resource_list *rl)
2341 struct resource_list_entry *rle;
2343 while ((rle = SLIST_FIRST(rl)) != NULL) {
2344 if (rle->res)
2345 panic("resource_list_free: resource entry is busy");
2346 SLIST_REMOVE_HEAD(rl, link);
2347 kfree(rle, M_BUS);
2351 void
2352 resource_list_add(struct resource_list *rl, int type, int rid,
2353 u_long start, u_long end, u_long count, int cpuid)
2355 struct resource_list_entry *rle;
2357 rle = resource_list_find(rl, type, rid);
2358 if (rle == NULL) {
2359 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2360 M_INTWAIT);
2361 SLIST_INSERT_HEAD(rl, rle, link);
2362 rle->type = type;
2363 rle->rid = rid;
2364 rle->res = NULL;
2365 rle->cpuid = -1;
2368 if (rle->res)
2369 panic("resource_list_add: resource entry is busy");
2371 rle->start = start;
2372 rle->end = end;
2373 rle->count = count;
2375 if (cpuid != -1) {
2376 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2377 panic("resource_list_add: moving from cpu%d -> cpu%d",
2378 rle->cpuid, cpuid);
2380 rle->cpuid = cpuid;
2384 struct resource_list_entry*
2385 resource_list_find(struct resource_list *rl,
2386 int type, int rid)
2388 struct resource_list_entry *rle;
2390 SLIST_FOREACH(rle, rl, link)
2391 if (rle->type == type && rle->rid == rid)
2392 return(rle);
2393 return(NULL);
2396 void
2397 resource_list_delete(struct resource_list *rl,
2398 int type, int rid)
2400 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2402 if (rle) {
2403 if (rle->res != NULL)
2404 panic("resource_list_delete: resource has not been released");
2405 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2406 kfree(rle, M_BUS);
2410 struct resource *
2411 resource_list_alloc(struct resource_list *rl,
2412 device_t bus, device_t child,
2413 int type, int *rid,
2414 u_long start, u_long end,
2415 u_long count, u_int flags, int cpuid)
2417 struct resource_list_entry *rle = NULL;
2418 int passthrough = (device_get_parent(child) != bus);
2419 int isdefault = (start == 0UL && end == ~0UL);
2421 if (passthrough) {
2422 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2423 type, rid,
2424 start, end, count, flags, cpuid));
2427 rle = resource_list_find(rl, type, *rid);
2429 if (!rle)
2430 return(0); /* no resource of that type/rid */
2432 if (rle->res)
2433 panic("resource_list_alloc: resource entry is busy");
2435 if (isdefault) {
2436 start = rle->start;
2437 count = max(count, rle->count);
2438 end = max(rle->end, start + count - 1);
2440 cpuid = rle->cpuid;
2442 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2443 type, rid, start, end, count,
2444 flags, cpuid);
2447 * Record the new range.
2449 if (rle->res) {
2450 rle->start = rman_get_start(rle->res);
2451 rle->end = rman_get_end(rle->res);
2452 rle->count = count;
2455 return(rle->res);
2459 resource_list_release(struct resource_list *rl,
2460 device_t bus, device_t child,
2461 int type, int rid, struct resource *res)
2463 struct resource_list_entry *rle = NULL;
2464 int passthrough = (device_get_parent(child) != bus);
2465 int error;
2467 if (passthrough) {
2468 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2469 type, rid, res));
2472 rle = resource_list_find(rl, type, rid);
2474 if (!rle)
2475 panic("resource_list_release: can't find resource");
2476 if (!rle->res)
2477 panic("resource_list_release: resource entry is not busy");
2479 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2480 type, rid, res);
2481 if (error)
2482 return(error);
2484 rle->res = NULL;
2485 return(0);
2489 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2490 const char *format)
2492 struct resource_list_entry *rle;
2493 int printed, retval;
2495 printed = 0;
2496 retval = 0;
2497 /* Yes, this is kinda cheating */
2498 SLIST_FOREACH(rle, rl, link) {
2499 if (rle->type == type) {
2500 if (printed == 0)
2501 retval += kprintf(" %s ", name);
2502 else
2503 retval += kprintf(",");
2504 printed++;
2505 retval += kprintf(format, rle->start);
2506 if (rle->count > 1) {
2507 retval += kprintf("-");
2508 retval += kprintf(format, rle->start +
2509 rle->count - 1);
2513 return(retval);
2517 * Generic driver/device identify functions. These will install a device
2518 * rendezvous point under the parent using the same name as the driver
2519 * name, which will at a later time be probed and attached.
2521 * These functions are used when the parent does not 'scan' its bus for
2522 * matching devices, or for the particular devices using these functions,
2523 * or when the device is a pseudo or synthesized device (such as can be
2524 * found under firewire and ppbus).
2527 bus_generic_identify(driver_t *driver, device_t parent)
2529 if (parent->state == DS_ATTACHED)
2530 return (0);
2531 BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2532 return (0);
2536 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2538 if (parent->state == DS_ATTACHED)
2539 return (0);
2540 BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2541 return (0);
2545 * Call DEVICE_IDENTIFY for each driver.
2548 bus_generic_probe(device_t dev)
2550 devclass_t dc = dev->devclass;
2551 driverlink_t dl;
2553 TAILQ_FOREACH(dl, &dc->drivers, link) {
2554 DEVICE_IDENTIFY(dl->driver, dev);
2557 return(0);
2561 * This is an aweful hack due to the isa bus and autoconf code not
2562 * probing the ISA devices until after everything else has configured.
2563 * The ISA bus did a dummy attach long ago so we have to set it back
2564 * to an earlier state so the probe thinks its the initial probe and
2565 * not a bus rescan.
2567 * XXX remove by properly defering the ISA bus scan.
2570 bus_generic_probe_hack(device_t dev)
2572 if (dev->state == DS_ATTACHED) {
2573 dev->state = DS_ALIVE;
2574 bus_generic_probe(dev);
2575 dev->state = DS_ATTACHED;
2577 return (0);
2581 bus_generic_attach(device_t dev)
2583 device_t child;
2585 TAILQ_FOREACH(child, &dev->children, link) {
2586 device_probe_and_attach(child);
2589 return(0);
2593 bus_generic_detach(device_t dev)
2595 device_t child;
2596 int error;
2598 if (dev->state != DS_ATTACHED)
2599 return(EBUSY);
2601 TAILQ_FOREACH(child, &dev->children, link)
2602 if ((error = device_detach(child)) != 0)
2603 return(error);
2605 return 0;
2609 bus_generic_shutdown(device_t dev)
2611 device_t child;
2613 TAILQ_FOREACH(child, &dev->children, link)
2614 device_shutdown(child);
2616 return(0);
2620 bus_generic_suspend(device_t dev)
2622 int error;
2623 device_t child, child2;
2625 TAILQ_FOREACH(child, &dev->children, link) {
2626 error = DEVICE_SUSPEND(child);
2627 if (error) {
2628 for (child2 = TAILQ_FIRST(&dev->children);
2629 child2 && child2 != child;
2630 child2 = TAILQ_NEXT(child2, link))
2631 DEVICE_RESUME(child2);
2632 return(error);
2635 return(0);
2639 bus_generic_resume(device_t dev)
2641 device_t child;
2643 TAILQ_FOREACH(child, &dev->children, link)
2644 DEVICE_RESUME(child);
2645 /* if resume fails, there's nothing we can usefully do... */
2647 return(0);
2651 bus_print_child_header(device_t dev, device_t child)
2653 int retval = 0;
2655 if (device_get_desc(child))
2656 retval += device_printf(child, "<%s>", device_get_desc(child));
2657 else
2658 retval += kprintf("%s", device_get_nameunit(child));
2659 if (bootverbose) {
2660 if (child->state != DS_ATTACHED)
2661 kprintf(" [tentative]");
2662 else
2663 kprintf(" [attached!]");
2665 return(retval);
2669 bus_print_child_footer(device_t dev, device_t child)
2671 return(kprintf(" on %s\n", device_get_nameunit(dev)));
2674 device_t
2675 bus_generic_add_child(device_t dev, device_t child, int order,
2676 const char *name, int unit)
2678 if (dev->parent)
2679 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2680 else
2681 dev = device_add_child_ordered(child, order, name, unit);
2682 return(dev);
2687 bus_generic_print_child(device_t dev, device_t child)
2689 int retval = 0;
2691 retval += bus_print_child_header(dev, child);
2692 retval += bus_print_child_footer(dev, child);
2694 return(retval);
2698 bus_generic_read_ivar(device_t dev, device_t child, int index,
2699 uintptr_t * result)
2701 int error;
2703 if (dev->parent)
2704 error = BUS_READ_IVAR(dev->parent, child, index, result);
2705 else
2706 error = ENOENT;
2707 return (error);
2711 bus_generic_write_ivar(device_t dev, device_t child, int index,
2712 uintptr_t value)
2714 int error;
2716 if (dev->parent)
2717 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2718 else
2719 error = ENOENT;
2720 return (error);
2724 * Resource list are used for iterations, do not recurse.
2726 struct resource_list *
2727 bus_generic_get_resource_list(device_t dev, device_t child)
2729 return (NULL);
2732 void
2733 bus_generic_driver_added(device_t dev, driver_t *driver)
2735 device_t child;
2737 DEVICE_IDENTIFY(driver, dev);
2738 TAILQ_FOREACH(child, &dev->children, link) {
2739 if (child->state == DS_NOTPRESENT)
2740 device_probe_and_attach(child);
2745 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2746 int flags, driver_intr_t *intr, void *arg, void **cookiep,
2747 lwkt_serialize_t serializer, const char *desc)
2749 /* Propagate up the bus hierarchy until someone handles it. */
2750 if (dev->parent) {
2751 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2752 intr, arg, cookiep, serializer, desc);
2753 } else {
2754 return EINVAL;
2759 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2760 void *cookie)
2762 /* Propagate up the bus hierarchy until someone handles it. */
2763 if (dev->parent)
2764 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2765 else
2766 return(EINVAL);
2770 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2772 if (dev->parent)
2773 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2774 else
2775 return(0);
2778 void
2779 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2781 if (dev->parent)
2782 BUS_ENABLE_INTR(dev->parent, child, cookie);
2786 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2787 enum intr_polarity pol)
2789 /* Propagate up the bus hierarchy until someone handles it. */
2790 if (dev->parent)
2791 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2792 else
2793 return(EINVAL);
2796 struct resource *
2797 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2798 u_long start, u_long end, u_long count, u_int flags, int cpuid)
2800 /* Propagate up the bus hierarchy until someone handles it. */
2801 if (dev->parent)
2802 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
2803 start, end, count, flags, cpuid));
2804 else
2805 return(NULL);
2809 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2810 struct resource *r)
2812 /* Propagate up the bus hierarchy until someone handles it. */
2813 if (dev->parent)
2814 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2815 else
2816 return(EINVAL);
2820 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2821 struct resource *r)
2823 /* Propagate up the bus hierarchy until someone handles it. */
2824 if (dev->parent)
2825 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2826 else
2827 return(EINVAL);
2831 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2832 int rid, struct resource *r)
2834 /* Propagate up the bus hierarchy until someone handles it. */
2835 if (dev->parent)
2836 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2837 r));
2838 else
2839 return(EINVAL);
2843 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2844 u_long *startp, u_long *countp)
2846 int error;
2848 error = ENOENT;
2849 if (dev->parent) {
2850 error = BUS_GET_RESOURCE(dev->parent, child, type, rid,
2851 startp, countp);
2853 return (error);
2857 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2858 u_long start, u_long count, int cpuid)
2860 int error;
2862 error = EINVAL;
2863 if (dev->parent) {
2864 error = BUS_SET_RESOURCE(dev->parent, child, type, rid,
2865 start, count, cpuid);
2867 return (error);
2870 void
2871 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2873 if (dev->parent)
2874 BUS_DELETE_RESOURCE(dev, child, type, rid);
2878 * @brief Helper function for implementing BUS_GET_DMA_TAG().
2880 * This simple implementation of BUS_GET_DMA_TAG() simply calls the
2881 * BUS_GET_DMA_TAG() method of the parent of @p dev.
2883 bus_dma_tag_t
2884 bus_generic_get_dma_tag(device_t dev, device_t child)
2887 /* Propagate up the bus hierarchy until someone handles it. */
2888 if (dev->parent != NULL)
2889 return (BUS_GET_DMA_TAG(dev->parent, child));
2890 return (NULL);
2894 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2895 u_long *startp, u_long *countp)
2897 struct resource_list *rl = NULL;
2898 struct resource_list_entry *rle = NULL;
2900 rl = BUS_GET_RESOURCE_LIST(dev, child);
2901 if (!rl)
2902 return(EINVAL);
2904 rle = resource_list_find(rl, type, rid);
2905 if (!rle)
2906 return(ENOENT);
2908 if (startp)
2909 *startp = rle->start;
2910 if (countp)
2911 *countp = rle->count;
2913 return(0);
2917 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2918 u_long start, u_long count, int cpuid)
2920 struct resource_list *rl = NULL;
2922 rl = BUS_GET_RESOURCE_LIST(dev, child);
2923 if (!rl)
2924 return(EINVAL);
2926 resource_list_add(rl, type, rid, start, (start + count - 1), count,
2927 cpuid);
2929 return(0);
2932 void
2933 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2935 struct resource_list *rl = NULL;
2937 rl = BUS_GET_RESOURCE_LIST(dev, child);
2938 if (!rl)
2939 return;
2941 resource_list_delete(rl, type, rid);
2945 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2946 int rid, struct resource *r)
2948 struct resource_list *rl = NULL;
2950 rl = BUS_GET_RESOURCE_LIST(dev, child);
2951 if (!rl)
2952 return(EINVAL);
2954 return(resource_list_release(rl, dev, child, type, rid, r));
2957 struct resource *
2958 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2959 int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
2961 struct resource_list *rl = NULL;
2963 rl = BUS_GET_RESOURCE_LIST(dev, child);
2964 if (!rl)
2965 return(NULL);
2967 return(resource_list_alloc(rl, dev, child, type, rid,
2968 start, end, count, flags, cpuid));
2972 bus_generic_child_present(device_t bus, device_t child)
2974 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2979 * Some convenience functions to make it easier for drivers to use the
2980 * resource-management functions. All these really do is hide the
2981 * indirection through the parent's method table, making for slightly
2982 * less-wordy code. In the future, it might make sense for this code
2983 * to maintain some sort of a list of resources allocated by each device.
2986 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2987 struct resource **res)
2989 int i;
2991 for (i = 0; rs[i].type != -1; i++)
2992 res[i] = NULL;
2993 for (i = 0; rs[i].type != -1; i++) {
2994 res[i] = bus_alloc_resource_any(dev,
2995 rs[i].type, &rs[i].rid, rs[i].flags);
2996 if (res[i] == NULL) {
2997 bus_release_resources(dev, rs, res);
2998 return (ENXIO);
3001 return (0);
3004 void
3005 bus_release_resources(device_t dev, const struct resource_spec *rs,
3006 struct resource **res)
3008 int i;
3010 for (i = 0; rs[i].type != -1; i++)
3011 if (res[i] != NULL) {
3012 bus_release_resource(
3013 dev, rs[i].type, rs[i].rid, res[i]);
3014 res[i] = NULL;
3018 struct resource *
3019 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
3020 u_long count, u_int flags)
3022 if (dev->parent == NULL)
3023 return(0);
3024 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
3025 count, flags, -1));
3028 struct resource *
3029 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
3031 if (dev->parent == NULL)
3032 return(0);
3033 return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
3034 irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
3038 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
3040 if (dev->parent == NULL)
3041 return(EINVAL);
3042 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3046 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
3048 if (dev->parent == NULL)
3049 return(EINVAL);
3050 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
3054 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
3056 if (dev->parent == NULL)
3057 return(EINVAL);
3058 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
3062 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
3063 driver_intr_t handler, void *arg, void **cookiep,
3064 lwkt_serialize_t serializer, const char *desc)
3066 if (dev->parent == NULL)
3067 return EINVAL;
3068 return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
3069 cookiep, serializer, desc);
3073 bus_setup_intr(device_t dev, struct resource *r, int flags,
3074 driver_intr_t handler, void *arg, void **cookiep,
3075 lwkt_serialize_t serializer)
3077 return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
3078 serializer, NULL);
3082 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
3084 if (dev->parent == NULL)
3085 return(EINVAL);
3086 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
3089 void
3090 bus_enable_intr(device_t dev, void *cookie)
3092 if (dev->parent)
3093 BUS_ENABLE_INTR(dev->parent, dev, cookie);
3097 bus_disable_intr(device_t dev, void *cookie)
3099 if (dev->parent)
3100 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
3101 else
3102 return(0);
3106 bus_set_resource(device_t dev, int type, int rid,
3107 u_long start, u_long count, int cpuid)
3109 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
3110 start, count, cpuid));
3114 bus_get_resource(device_t dev, int type, int rid,
3115 u_long *startp, u_long *countp)
3117 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3118 startp, countp));
3121 u_long
3122 bus_get_resource_start(device_t dev, int type, int rid)
3124 u_long start, count;
3125 int error;
3127 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3128 &start, &count);
3129 if (error)
3130 return(0);
3131 return(start);
3134 u_long
3135 bus_get_resource_count(device_t dev, int type, int rid)
3137 u_long start, count;
3138 int error;
3140 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
3141 &start, &count);
3142 if (error)
3143 return(0);
3144 return(count);
3147 void
3148 bus_delete_resource(device_t dev, int type, int rid)
3150 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
3154 bus_child_present(device_t child)
3156 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3160 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3162 device_t parent;
3164 parent = device_get_parent(child);
3165 if (parent == NULL) {
3166 *buf = '\0';
3167 return (0);
3169 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3173 bus_child_location_str(device_t child, char *buf, size_t buflen)
3175 device_t parent;
3177 parent = device_get_parent(child);
3178 if (parent == NULL) {
3179 *buf = '\0';
3180 return (0);
3182 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3186 * @brief Wrapper function for BUS_GET_DMA_TAG().
3188 * This function simply calls the BUS_GET_DMA_TAG() method of the
3189 * parent of @p dev.
3191 bus_dma_tag_t
3192 bus_get_dma_tag(device_t dev)
3194 device_t parent;
3196 parent = device_get_parent(dev);
3197 if (parent == NULL)
3198 return (NULL);
3199 return (BUS_GET_DMA_TAG(parent, dev));
3202 static int
3203 root_print_child(device_t dev, device_t child)
3205 return(0);
3208 static int
3209 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3210 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3213 * If an interrupt mapping gets to here something bad has happened.
3215 panic("root_setup_intr");
3219 * If we get here, assume that the device is permanant and really is
3220 * present in the system. Removable bus drivers are expected to intercept
3221 * this call long before it gets here. We return -1 so that drivers that
3222 * really care can check vs -1 or some ERRNO returned higher in the food
3223 * chain.
3225 static int
3226 root_child_present(device_t dev, device_t child)
3228 return(-1);
3232 * XXX NOTE! other defaults may be set in bus_if.m
3234 static kobj_method_t root_methods[] = {
3235 /* Device interface */
3236 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
3237 KOBJMETHOD(device_suspend, bus_generic_suspend),
3238 KOBJMETHOD(device_resume, bus_generic_resume),
3240 /* Bus interface */
3241 KOBJMETHOD(bus_add_child, bus_generic_add_child),
3242 KOBJMETHOD(bus_print_child, root_print_child),
3243 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
3244 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
3245 KOBJMETHOD(bus_setup_intr, root_setup_intr),
3246 KOBJMETHOD(bus_child_present, root_child_present),
3248 KOBJMETHOD_END
3251 static driver_t root_driver = {
3252 "root",
3253 root_methods,
3254 1, /* no softc */
3257 device_t root_bus;
3258 devclass_t root_devclass;
3260 static int
3261 root_bus_module_handler(module_t mod, int what, void* arg)
3263 switch (what) {
3264 case MOD_LOAD:
3265 TAILQ_INIT(&bus_data_devices);
3266 root_bus = make_device(NULL, "root", 0);
3267 root_bus->desc = "System root bus";
3268 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3269 root_bus->driver = &root_driver;
3270 root_bus->state = DS_ALIVE;
3271 root_devclass = devclass_find_internal("root", NULL, FALSE);
3272 devinit();
3273 return(0);
3275 case MOD_SHUTDOWN:
3276 device_shutdown(root_bus);
3277 return(0);
3278 default:
3279 return(0);
3283 static moduledata_t root_bus_mod = {
3284 "rootbus",
3285 root_bus_module_handler,
3288 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3290 void
3291 root_bus_configure(void)
3293 int warncount;
3294 device_t dev;
3296 PDEBUG(("."));
3299 * handle device_identify based device attachments to the root_bus
3300 * (typically nexus).
3302 bus_generic_probe(root_bus);
3305 * Probe and attach the devices under root_bus.
3307 TAILQ_FOREACH(dev, &root_bus->children, link) {
3308 device_probe_and_attach(dev);
3312 * Wait for all asynchronous attaches to complete. If we don't
3313 * our legacy ISA bus scan could steal device unit numbers or
3314 * even I/O ports.
3316 warncount = 10;
3317 if (numasyncthreads)
3318 kprintf("Waiting for async drivers to attach\n");
3319 while (numasyncthreads > 0) {
3320 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3321 --warncount;
3322 if (warncount == 0) {
3323 kprintf("Warning: Still waiting for %d "
3324 "drivers to attach\n", numasyncthreads);
3325 } else if (warncount == -30) {
3326 kprintf("Giving up on %d drivers\n", numasyncthreads);
3327 break;
3330 root_bus->state = DS_ATTACHED;
3334 driver_module_handler(module_t mod, int what, void *arg)
3336 int error;
3337 struct driver_module_data *dmd;
3338 devclass_t bus_devclass;
3339 kobj_class_t driver;
3340 const char *parentname;
3342 dmd = (struct driver_module_data *)arg;
3343 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3344 error = 0;
3346 switch (what) {
3347 case MOD_LOAD:
3348 if (dmd->dmd_chainevh)
3349 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3351 driver = dmd->dmd_driver;
3352 PDEBUG(("Loading module: driver %s on bus %s",
3353 DRIVERNAME(driver), dmd->dmd_busname));
3356 * If the driver has any base classes, make the
3357 * devclass inherit from the devclass of the driver's
3358 * first base class. This will allow the system to
3359 * search for drivers in both devclasses for children
3360 * of a device using this driver.
3362 if (driver->baseclasses)
3363 parentname = driver->baseclasses[0]->name;
3364 else
3365 parentname = NULL;
3366 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3367 parentname, TRUE);
3369 error = devclass_add_driver(bus_devclass, driver);
3370 if (error)
3371 break;
3372 break;
3374 case MOD_UNLOAD:
3375 PDEBUG(("Unloading module: driver %s from bus %s",
3376 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3377 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3379 if (!error && dmd->dmd_chainevh)
3380 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3381 break;
3384 return (error);
3387 #ifdef BUS_DEBUG
3390 * The _short versions avoid iteration by not calling anything that prints
3391 * more than oneliners. I love oneliners.
3394 static void
3395 print_device_short(device_t dev, int indent)
3397 if (!dev)
3398 return;
3400 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3401 dev->unit, dev->desc,
3402 (dev->parent? "":"no "),
3403 (TAILQ_EMPTY(&dev->children)? "no ":""),
3404 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3405 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3406 (dev->flags&DF_WILDCARD? "wildcard,":""),
3407 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3408 (dev->ivars? "":"no "),
3409 (dev->softc? "":"no "),
3410 dev->busy));
3413 static void
3414 print_device(device_t dev, int indent)
3416 if (!dev)
3417 return;
3419 print_device_short(dev, indent);
3421 indentprintf(("Parent:\n"));
3422 print_device_short(dev->parent, indent+1);
3423 indentprintf(("Driver:\n"));
3424 print_driver_short(dev->driver, indent+1);
3425 indentprintf(("Devclass:\n"));
3426 print_devclass_short(dev->devclass, indent+1);
3430 * Print the device and all its children (indented).
3432 void
3433 print_device_tree_short(device_t dev, int indent)
3435 device_t child;
3437 if (!dev)
3438 return;
3440 print_device_short(dev, indent);
3442 TAILQ_FOREACH(child, &dev->children, link)
3443 print_device_tree_short(child, indent+1);
3447 * Print the device and all its children (indented).
3449 void
3450 print_device_tree(device_t dev, int indent)
3452 device_t child;
3454 if (!dev)
3455 return;
3457 print_device(dev, indent);
3459 TAILQ_FOREACH(child, &dev->children, link)
3460 print_device_tree(child, indent+1);
3463 static void
3464 print_driver_short(driver_t *driver, int indent)
3466 if (!driver)
3467 return;
3469 indentprintf(("driver %s: softc size = %zu\n",
3470 driver->name, driver->size));
3473 static void
3474 print_driver(driver_t *driver, int indent)
3476 if (!driver)
3477 return;
3479 print_driver_short(driver, indent);
3483 static void
3484 print_driver_list(driver_list_t drivers, int indent)
3486 driverlink_t driver;
3488 TAILQ_FOREACH(driver, &drivers, link)
3489 print_driver(driver->driver, indent);
3492 static void
3493 print_devclass_short(devclass_t dc, int indent)
3495 if (!dc)
3496 return;
3498 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3501 static void
3502 print_devclass(devclass_t dc, int indent)
3504 int i;
3506 if (!dc)
3507 return;
3509 print_devclass_short(dc, indent);
3510 indentprintf(("Drivers:\n"));
3511 print_driver_list(dc->drivers, indent+1);
3513 indentprintf(("Devices:\n"));
3514 for (i = 0; i < dc->maxunit; i++)
3515 if (dc->devices[i])
3516 print_device(dc->devices[i], indent+1);
3519 void
3520 print_devclass_list_short(void)
3522 devclass_t dc;
3524 kprintf("Short listing of devclasses, drivers & devices:\n");
3525 TAILQ_FOREACH(dc, &devclasses, link) {
3526 print_devclass_short(dc, 0);
3530 void
3531 print_devclass_list(void)
3533 devclass_t dc;
3535 kprintf("Full listing of devclasses, drivers & devices:\n");
3536 TAILQ_FOREACH(dc, &devclasses, link) {
3537 print_devclass(dc, 0);
3541 #endif
3544 * Check to see if a device is disabled via a disabled hint.
3547 resource_disabled(const char *name, int unit)
3549 int error, value;
3551 error = resource_int_value(name, unit, "disabled", &value);
3552 if (error)
3553 return(0);
3554 return(value);
3558 * User-space access to the device tree.
3560 * We implement a small set of nodes:
3562 * hw.bus Single integer read method to obtain the
3563 * current generation count.
3564 * hw.bus.devices Reads the entire device tree in flat space.
3565 * hw.bus.rman Resource manager interface
3567 * We might like to add the ability to scan devclasses and/or drivers to
3568 * determine what else is currently loaded/available.
3571 static int
3572 sysctl_bus(SYSCTL_HANDLER_ARGS)
3574 struct u_businfo ubus;
3576 ubus.ub_version = BUS_USER_VERSION;
3577 ubus.ub_generation = bus_data_generation;
3579 return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3581 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3582 "bus-related data");
3584 static int
3585 sysctl_devices(SYSCTL_HANDLER_ARGS)
3587 int *name = (int *)arg1;
3588 u_int namelen = arg2;
3589 int index;
3590 struct device *dev;
3591 struct u_device udev; /* XXX this is a bit big */
3592 int error;
3594 if (namelen != 2)
3595 return (EINVAL);
3597 if (bus_data_generation_check(name[0]))
3598 return (EINVAL);
3600 index = name[1];
3603 * Scan the list of devices, looking for the requested index.
3605 TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3606 if (index-- == 0)
3607 break;
3609 if (dev == NULL)
3610 return (ENOENT);
3613 * Populate the return array.
3615 bzero(&udev, sizeof(udev));
3616 udev.dv_handle = (uintptr_t)dev;
3617 udev.dv_parent = (uintptr_t)dev->parent;
3618 if (dev->nameunit != NULL)
3619 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3620 if (dev->desc != NULL)
3621 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3622 if (dev->driver != NULL && dev->driver->name != NULL)
3623 strlcpy(udev.dv_drivername, dev->driver->name,
3624 sizeof(udev.dv_drivername));
3625 bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3626 bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3627 udev.dv_devflags = dev->devflags;
3628 udev.dv_flags = dev->flags;
3629 udev.dv_state = dev->state;
3630 error = SYSCTL_OUT(req, &udev, sizeof(udev));
3631 return (error);
3634 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3635 "system device tree");
3638 bus_data_generation_check(int generation)
3640 if (generation != bus_data_generation)
3641 return (1);
3643 /* XXX generate optimised lists here? */
3644 return (0);
3647 void
3648 bus_data_generation_update(void)
3650 bus_data_generation++;
3653 const char *
3654 intr_str_polarity(enum intr_polarity pola)
3656 switch (pola) {
3657 case INTR_POLARITY_LOW:
3658 return "low";
3660 case INTR_POLARITY_HIGH:
3661 return "high";
3663 case INTR_POLARITY_CONFORM:
3664 return "conform";
3666 return "unknown";
3669 const char *
3670 intr_str_trigger(enum intr_trigger trig)
3672 switch (trig) {
3673 case INTR_TRIGGER_EDGE:
3674 return "edge";
3676 case INTR_TRIGGER_LEVEL:
3677 return "level";
3679 case INTR_TRIGGER_CONFORM:
3680 return "conform";
3682 return "unknown";
3686 device_getenv_int(device_t dev, const char *knob, int def)
3688 char env[128];
3690 /* Deprecated; for compat */
3691 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3692 kgetenv_int(env, &def);
3694 /* Prefer dev.driver.unit.knob */
3695 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3696 device_get_name(dev), device_get_unit(dev), knob);
3697 kgetenv_int(env, &def);
3699 return def;
3702 void
3703 device_getenv_string(device_t dev, const char *knob, char * __restrict data,
3704 int dlen, const char * __restrict def)
3706 char env[128];
3708 strlcpy(data, def, dlen);
3710 /* Deprecated; for compat */
3711 ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3712 kgetenv_string(env, data, dlen);
3714 /* Prefer dev.driver.unit.knob */
3715 ksnprintf(env, sizeof(env), "dev.%s.%d.%s",
3716 device_get_name(dev), device_get_unit(dev), knob);
3717 kgetenv_string(env, data, dlen);