More -Wwrite-strings cleanup and make sure you can actually play it.
[dragonfly.git] / sys / kern / subr_bus.c
blob671c39c0f244ecfa993c2762cd7fc4d3541e3ad3
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 $
27 * $DragonFly: src/sys/kern/subr_bus.c,v 1.24 2005/02/17 13:59:36 joerg Exp $
30 #include "opt_bus.h"
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #ifdef DEVICE_SYSCTLS
38 #include <sys/sysctl.h>
39 #endif
40 #include <sys/kobj.h>
41 #include <sys/bus_private.h>
42 #include <sys/systm.h>
43 #include <machine/bus.h>
44 #include <sys/rman.h>
45 #include <machine/stdarg.h> /* for device_printf() */
47 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
49 #ifdef BUS_DEBUG
50 #define PDEBUG(a) (printf("%s:%d: ", __func__, __LINE__), printf a, printf("\n"))
51 #define DEVICENAME(d) ((d)? device_get_name(d): "no device")
52 #define DRIVERNAME(d) ((d)? d->name : "no driver")
53 #define DEVCLANAME(d) ((d)? d->name : "no devclass")
55 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to
56 * prevent syslog from deleting initial spaces
58 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf(" "); printf p ; } while(0)
60 static void print_device_short(device_t dev, int indent);
61 static void print_device(device_t dev, int indent);
62 void print_device_tree_short(device_t dev, int indent);
63 void print_device_tree(device_t dev, int indent);
64 static void print_driver_short(driver_t *driver, int indent);
65 static void print_driver(driver_t *driver, int indent);
66 static void print_driver_list(driver_list_t drivers, int indent);
67 static void print_devclass_short(devclass_t dc, int indent);
68 static void print_devclass(devclass_t dc, int indent);
69 void print_devclass_list_short(void);
70 void print_devclass_list(void);
72 #else
73 /* Make the compiler ignore the function calls */
74 #define PDEBUG(a) /* nop */
75 #define DEVICENAME(d) /* nop */
76 #define DRIVERNAME(d) /* nop */
77 #define DEVCLANAME(d) /* nop */
79 #define print_device_short(d,i) /* nop */
80 #define print_device(d,i) /* nop */
81 #define print_device_tree_short(d,i) /* nop */
82 #define print_device_tree(d,i) /* nop */
83 #define print_driver_short(d,i) /* nop */
84 #define print_driver(d,i) /* nop */
85 #define print_driver_list(d,i) /* nop */
86 #define print_devclass_short(d,i) /* nop */
87 #define print_devclass(d,i) /* nop */
88 #define print_devclass_list_short() /* nop */
89 #define print_devclass_list() /* nop */
90 #endif
92 #ifdef DEVICE_SYSCTLS
93 static void device_register_oids(device_t dev);
94 static void device_unregister_oids(device_t dev);
95 #endif
97 kobj_method_t null_methods[] = {
98 { 0, 0 }
101 DEFINE_CLASS(null, null_methods, 0);
104 * Devclass implementation
107 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
109 static devclass_t
110 devclass_find_internal(const char *classname, const char *parentname,
111 int create)
113 devclass_t dc;
115 PDEBUG(("looking for %s", classname));
116 if (classname == NULL)
117 return(NULL);
119 TAILQ_FOREACH(dc, &devclasses, link)
120 if (!strcmp(dc->name, classname))
121 break;
123 if (create && !dc) {
124 PDEBUG(("creating %s", classname));
125 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
126 M_BUS, M_INTWAIT | M_ZERO);
127 if (!dc)
128 return(NULL);
129 dc->parent = NULL;
130 dc->name = (char*) (dc + 1);
131 strcpy(dc->name, classname);
132 dc->devices = NULL;
133 dc->maxunit = 0;
134 TAILQ_INIT(&dc->drivers);
135 TAILQ_INSERT_TAIL(&devclasses, dc, link);
137 if (parentname && dc && !dc->parent)
138 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
140 return(dc);
143 devclass_t
144 devclass_create(const char *classname)
146 return(devclass_find_internal(classname, NULL, TRUE));
149 devclass_t
150 devclass_find(const char *classname)
152 return(devclass_find_internal(classname, NULL, FALSE));
156 devclass_add_driver(devclass_t dc, driver_t *driver)
158 driverlink_t dl;
159 int i;
161 PDEBUG(("%s", DRIVERNAME(driver)));
163 dl = malloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
164 if (!dl)
165 return(ENOMEM);
168 * Compile the driver's methods. Also increase the reference count
169 * so that the class doesn't get freed when the last instance
170 * goes. This means we can safely use static methods and avoids a
171 * double-free in devclass_delete_driver.
173 kobj_class_instantiate(driver);
176 * Make sure the devclass which the driver is implementing exists.
178 devclass_find_internal(driver->name, NULL, TRUE);
180 dl->driver = driver;
181 TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
184 * Call BUS_DRIVER_ADDED for any existing busses in this class.
186 for (i = 0; i < dc->maxunit; i++)
187 if (dc->devices[i])
188 BUS_DRIVER_ADDED(dc->devices[i], driver);
190 return(0);
194 devclass_delete_driver(devclass_t busclass, driver_t *driver)
196 devclass_t dc = devclass_find(driver->name);
197 driverlink_t dl;
198 device_t dev;
199 int i;
200 int error;
202 PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
204 if (!dc)
205 return(0);
208 * Find the link structure in the bus' list of drivers.
210 TAILQ_FOREACH(dl, &busclass->drivers, link)
211 if (dl->driver == driver)
212 break;
214 if (!dl) {
215 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
216 return(ENOENT);
220 * Disassociate from any devices. We iterate through all the
221 * devices in the devclass of the driver and detach any which are
222 * using the driver and which have a parent in the devclass which
223 * we are deleting from.
225 * Note that since a driver can be in multiple devclasses, we
226 * should not detach devices which are not children of devices in
227 * the affected devclass.
229 for (i = 0; i < dc->maxunit; i++)
230 if (dc->devices[i]) {
231 dev = dc->devices[i];
232 if (dev->driver == driver && dev->parent &&
233 dev->parent->devclass == busclass) {
234 if ((error = device_detach(dev)) != 0)
235 return(error);
236 device_set_driver(dev, NULL);
240 TAILQ_REMOVE(&busclass->drivers, dl, link);
241 free(dl, M_BUS);
243 kobj_class_uninstantiate(driver);
245 return(0);
248 static driverlink_t
249 devclass_find_driver_internal(devclass_t dc, const char *classname)
251 driverlink_t dl;
253 PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
255 TAILQ_FOREACH(dl, &dc->drivers, link)
256 if (!strcmp(dl->driver->name, classname))
257 return(dl);
259 PDEBUG(("not found"));
260 return(NULL);
263 kobj_class_t
264 devclass_find_driver(devclass_t dc, const char *classname)
266 driverlink_t dl;
268 dl = devclass_find_driver_internal(dc, classname);
269 if (dl)
270 return(dl->driver);
271 else
272 return(NULL);
275 const char *
276 devclass_get_name(devclass_t dc)
278 return(dc->name);
281 device_t
282 devclass_get_device(devclass_t dc, int unit)
284 if (dc == NULL || unit < 0 || unit >= dc->maxunit)
285 return(NULL);
286 return(dc->devices[unit]);
289 void *
290 devclass_get_softc(devclass_t dc, int unit)
292 device_t dev;
294 dev = devclass_get_device(dc, unit);
295 if (!dev)
296 return(NULL);
298 return(device_get_softc(dev));
302 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
304 int i;
305 int count;
306 device_t *list;
308 count = 0;
309 for (i = 0; i < dc->maxunit; i++)
310 if (dc->devices[i])
311 count++;
313 list = malloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
314 if (list == NULL)
315 return(ENOMEM);
317 count = 0;
318 for (i = 0; i < dc->maxunit; i++)
319 if (dc->devices[i]) {
320 list[count] = dc->devices[i];
321 count++;
324 *devlistp = list;
325 *devcountp = count;
327 return(0);
331 devclass_get_maxunit(devclass_t dc)
333 return(dc->maxunit);
336 void
337 devclass_set_parent(devclass_t dc, devclass_t pdc)
339 dc->parent = pdc;
342 devclass_t
343 devclass_get_parent(devclass_t dc)
345 return(dc->parent);
348 static int
349 devclass_alloc_unit(devclass_t dc, int *unitp)
351 int unit = *unitp;
353 PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
355 /* If we have been given a wired unit number, check for existing device */
356 if (unit != -1) {
357 if (unit >= 0 && unit < dc->maxunit &&
358 dc->devices[unit] != NULL) {
359 if (bootverbose)
360 printf("%s-: %s%d exists, using next available unit number\n",
361 dc->name, dc->name, unit);
362 /* find the next available slot */
363 while (++unit < dc->maxunit && dc->devices[unit] != NULL)
366 } else {
367 /* Unwired device, find the next available slot for it */
368 unit = 0;
369 while (unit < dc->maxunit && dc->devices[unit] != NULL)
370 unit++;
374 * We've selected a unit beyond the length of the table, so let's
375 * extend the table to make room for all units up to and including
376 * this one.
378 if (unit >= dc->maxunit) {
379 device_t *newlist;
380 int newsize;
382 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
383 newlist = malloc(sizeof(device_t) * newsize, M_BUS,
384 M_INTWAIT | M_ZERO);
385 if (newlist == NULL)
386 return(ENOMEM);
387 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
388 if (dc->devices)
389 free(dc->devices, M_BUS);
390 dc->devices = newlist;
391 dc->maxunit = newsize;
393 PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
395 *unitp = unit;
396 return(0);
399 static int
400 devclass_add_device(devclass_t dc, device_t dev)
402 int buflen, error;
404 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
406 buflen = strlen(dc->name) + 5;
407 dev->nameunit = malloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
408 if (!dev->nameunit)
409 return(ENOMEM);
411 if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
412 free(dev->nameunit, M_BUS);
413 dev->nameunit = NULL;
414 return(error);
416 dc->devices[dev->unit] = dev;
417 dev->devclass = dc;
418 snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
420 #ifdef DEVICE_SYSCTLS
421 device_register_oids(dev);
422 #endif
424 return(0);
427 static int
428 devclass_delete_device(devclass_t dc, device_t dev)
430 if (!dc || !dev)
431 return(0);
433 PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
435 if (dev->devclass != dc || dc->devices[dev->unit] != dev)
436 panic("devclass_delete_device: inconsistent device class");
437 dc->devices[dev->unit] = NULL;
438 if (dev->flags & DF_WILDCARD)
439 dev->unit = -1;
440 dev->devclass = NULL;
441 free(dev->nameunit, M_BUS);
442 dev->nameunit = NULL;
444 #ifdef DEVICE_SYSCTLS
445 device_unregister_oids(dev);
446 #endif
448 return(0);
451 static device_t
452 make_device(device_t parent, const char *name, int unit)
454 device_t dev;
455 devclass_t dc;
457 PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
459 if (name != NULL) {
460 dc = devclass_find_internal(name, NULL, TRUE);
461 if (!dc) {
462 printf("make_device: can't find device class %s\n", name);
463 return(NULL);
465 } else
466 dc = NULL;
468 dev = malloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
469 if (!dev)
470 return(0);
472 dev->parent = parent;
473 TAILQ_INIT(&dev->children);
474 kobj_init((kobj_t) dev, &null_class);
475 dev->driver = NULL;
476 dev->devclass = NULL;
477 dev->unit = unit;
478 dev->nameunit = NULL;
479 dev->desc = NULL;
480 dev->busy = 0;
481 dev->devflags = 0;
482 dev->flags = DF_ENABLED;
483 dev->order = 0;
484 if (unit == -1)
485 dev->flags |= DF_WILDCARD;
486 if (name) {
487 dev->flags |= DF_FIXEDCLASS;
488 if (devclass_add_device(dc, dev) != 0) {
489 kobj_delete((kobj_t)dev, M_BUS);
490 return(NULL);
493 dev->ivars = NULL;
494 dev->softc = NULL;
496 dev->state = DS_NOTPRESENT;
498 return(dev);
501 static int
502 device_print_child(device_t dev, device_t child)
504 int retval = 0;
506 if (device_is_alive(child))
507 retval += BUS_PRINT_CHILD(dev, child);
508 else
509 retval += device_printf(child, " not found\n");
511 return(retval);
514 device_t
515 device_add_child(device_t dev, const char *name, int unit)
517 return device_add_child_ordered(dev, 0, name, unit);
520 device_t
521 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
523 device_t child;
524 device_t place;
526 PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
527 order, unit));
529 child = make_device(dev, name, unit);
530 if (child == NULL)
531 return child;
532 child->order = order;
534 TAILQ_FOREACH(place, &dev->children, link)
535 if (place->order > order)
536 break;
538 if (place) {
540 * The device 'place' is the first device whose order is
541 * greater than the new child.
543 TAILQ_INSERT_BEFORE(place, child, link);
544 } else {
546 * The new child's order is greater or equal to the order of
547 * any existing device. Add the child to the tail of the list.
549 TAILQ_INSERT_TAIL(&dev->children, child, link);
552 return(child);
556 device_delete_child(device_t dev, device_t child)
558 int error;
559 device_t grandchild;
561 PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
563 /* remove children first */
564 while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
565 error = device_delete_child(child, grandchild);
566 if (error)
567 return(error);
570 if ((error = device_detach(child)) != 0)
571 return(error);
572 if (child->devclass)
573 devclass_delete_device(child->devclass, child);
574 TAILQ_REMOVE(&dev->children, child, link);
575 device_set_desc(child, NULL);
576 kobj_delete((kobj_t)child, M_BUS);
578 return(0);
582 * Find only devices attached to this bus.
584 device_t
585 device_find_child(device_t dev, const char *classname, int unit)
587 devclass_t dc;
588 device_t child;
590 dc = devclass_find(classname);
591 if (!dc)
592 return(NULL);
594 child = devclass_get_device(dc, unit);
595 if (child && child->parent == dev)
596 return(child);
597 return(NULL);
600 static driverlink_t
601 first_matching_driver(devclass_t dc, device_t dev)
603 if (dev->devclass)
604 return(devclass_find_driver_internal(dc, dev->devclass->name));
605 else
606 return(TAILQ_FIRST(&dc->drivers));
609 static driverlink_t
610 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
612 if (dev->devclass) {
613 driverlink_t dl;
614 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
615 if (!strcmp(dev->devclass->name, dl->driver->name))
616 return(dl);
617 return(NULL);
618 } else
619 return(TAILQ_NEXT(last, link));
622 static int
623 device_probe_child(device_t dev, device_t child)
625 devclass_t dc;
626 driverlink_t best = 0;
627 driverlink_t dl;
628 int result, pri = 0;
629 int hasclass = (child->devclass != 0);
631 dc = dev->devclass;
632 if (!dc)
633 panic("device_probe_child: parent device has no devclass");
635 if (child->state == DS_ALIVE)
636 return(0);
638 for (; dc; dc = dc->parent) {
639 for (dl = first_matching_driver(dc, child); dl;
640 dl = next_matching_driver(dc, child, dl)) {
641 PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
642 device_set_driver(child, dl->driver);
643 if (!hasclass)
644 device_set_devclass(child, dl->driver->name);
645 result = DEVICE_PROBE(child);
646 if (!hasclass)
647 device_set_devclass(child, 0);
650 * If the driver returns SUCCESS, there can be
651 * no higher match for this device.
653 if (result == 0) {
654 best = dl;
655 pri = 0;
656 break;
660 * The driver returned an error so it
661 * certainly doesn't match.
663 if (result > 0) {
664 device_set_driver(child, 0);
665 continue;
669 * A priority lower than SUCCESS, remember the
670 * best matching driver. Initialise the value
671 * of pri for the first match.
673 if (best == 0 || result > pri) {
674 best = dl;
675 pri = result;
676 continue;
680 * If we have unambiguous match in this devclass,
681 * don't look in the parent.
683 if (best && pri == 0)
684 break;
688 * If we found a driver, change state and initialise the devclass.
690 if (best) {
691 if (!child->devclass)
692 device_set_devclass(child, best->driver->name);
693 device_set_driver(child, best->driver);
694 if (pri < 0) {
696 * A bit bogus. Call the probe method again to make
697 * sure that we have the right description.
699 DEVICE_PROBE(child);
701 child->state = DS_ALIVE;
702 return(0);
705 return(ENXIO);
708 device_t
709 device_get_parent(device_t dev)
711 return dev->parent;
715 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
717 int count;
718 device_t child;
719 device_t *list;
721 count = 0;
722 TAILQ_FOREACH(child, &dev->children, link)
723 count++;
725 list = malloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
726 if (!list)
727 return(ENOMEM);
729 count = 0;
730 TAILQ_FOREACH(child, &dev->children, link) {
731 list[count] = child;
732 count++;
735 *devlistp = list;
736 *devcountp = count;
738 return(0);
741 driver_t *
742 device_get_driver(device_t dev)
744 return(dev->driver);
747 devclass_t
748 device_get_devclass(device_t dev)
750 return(dev->devclass);
753 const char *
754 device_get_name(device_t dev)
756 if (dev->devclass)
757 return devclass_get_name(dev->devclass);
758 return(NULL);
761 const char *
762 device_get_nameunit(device_t dev)
764 return(dev->nameunit);
768 device_get_unit(device_t dev)
770 return(dev->unit);
773 const char *
774 device_get_desc(device_t dev)
776 return(dev->desc);
779 uint32_t
780 device_get_flags(device_t dev)
782 return(dev->devflags);
786 device_print_prettyname(device_t dev)
788 const char *name = device_get_name(dev);
790 if (name == 0)
791 return printf("unknown: ");
792 else
793 return printf("%s%d: ", name, device_get_unit(dev));
797 device_printf(device_t dev, const char * fmt, ...)
799 __va_list ap;
800 int retval;
802 retval = device_print_prettyname(dev);
803 __va_start(ap, fmt);
804 retval += vprintf(fmt, ap);
805 __va_end(ap);
806 return retval;
809 static void
810 device_set_desc_internal(device_t dev, const char* desc, int copy)
812 if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
813 free(dev->desc, M_BUS);
814 dev->flags &= ~DF_DESCMALLOCED;
815 dev->desc = NULL;
818 if (copy && desc) {
819 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
820 if (dev->desc) {
821 strcpy(dev->desc, desc);
822 dev->flags |= DF_DESCMALLOCED;
824 } else
825 /* Avoid a -Wcast-qual warning */
826 dev->desc = (char *)(uintptr_t) desc;
828 #ifdef DEVICE_SYSCTLS
830 struct sysctl_oid *oid = &dev->oid[1];
831 oid->oid_arg1 = dev->desc ? dev->desc : "";
832 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
834 #endif
837 void
838 device_set_desc(device_t dev, const char* desc)
840 device_set_desc_internal(dev, desc, FALSE);
843 void
844 device_set_desc_copy(device_t dev, const char* desc)
846 device_set_desc_internal(dev, desc, TRUE);
849 void
850 device_set_flags(device_t dev, uint32_t flags)
852 dev->devflags = flags;
855 void *
856 device_get_softc(device_t dev)
858 return dev->softc;
861 void
862 device_set_softc(device_t dev, void *softc)
864 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
865 free(dev->softc, M_BUS);
866 dev->softc = softc;
867 if (dev->softc)
868 dev->flags |= DF_EXTERNALSOFTC;
869 else
870 dev->flags &= ~DF_EXTERNALSOFTC;
873 void *
874 device_get_ivars(device_t dev)
876 return dev->ivars;
879 void
880 device_set_ivars(device_t dev, void * ivars)
882 if (!dev)
883 return;
885 dev->ivars = ivars;
888 device_state_t
889 device_get_state(device_t dev)
891 return(dev->state);
894 void
895 device_enable(device_t dev)
897 dev->flags |= DF_ENABLED;
900 void
901 device_disable(device_t dev)
903 dev->flags &= ~DF_ENABLED;
907 * YYY cannot block
909 void
910 device_busy(device_t dev)
912 if (dev->state < DS_ATTACHED)
913 panic("device_busy: called for unattached device");
914 if (dev->busy == 0 && dev->parent)
915 device_busy(dev->parent);
916 dev->busy++;
917 dev->state = DS_BUSY;
921 * YYY cannot block
923 void
924 device_unbusy(device_t dev)
926 if (dev->state != DS_BUSY)
927 panic("device_unbusy: called for non-busy device");
928 dev->busy--;
929 if (dev->busy == 0) {
930 if (dev->parent)
931 device_unbusy(dev->parent);
932 dev->state = DS_ATTACHED;
936 void
937 device_quiet(device_t dev)
939 dev->flags |= DF_QUIET;
942 void
943 device_verbose(device_t dev)
945 dev->flags &= ~DF_QUIET;
949 device_is_quiet(device_t dev)
951 return((dev->flags & DF_QUIET) != 0);
955 device_is_enabled(device_t dev)
957 return((dev->flags & DF_ENABLED) != 0);
961 device_is_alive(device_t dev)
963 return(dev->state >= DS_ALIVE);
967 device_is_attached(device_t dev)
969 return(dev->state >= DS_ATTACHED);
973 device_set_devclass(device_t dev, const char *classname)
975 devclass_t dc;
977 if (!classname) {
978 if (dev->devclass)
979 devclass_delete_device(dev->devclass, dev);
980 return(0);
983 if (dev->devclass) {
984 printf("device_set_devclass: device class already set\n");
985 return(EINVAL);
988 dc = devclass_find_internal(classname, NULL, TRUE);
989 if (!dc)
990 return(ENOMEM);
992 return(devclass_add_device(dc, dev));
996 device_set_driver(device_t dev, driver_t *driver)
998 if (dev->state >= DS_ATTACHED)
999 return(EBUSY);
1001 if (dev->driver == driver)
1002 return(0);
1004 if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1005 free(dev->softc, M_BUS);
1006 dev->softc = NULL;
1008 kobj_delete((kobj_t) dev, 0);
1009 dev->driver = driver;
1010 if (driver) {
1011 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1012 if (!(dev->flags & DF_EXTERNALSOFTC)) {
1013 dev->softc = malloc(driver->size, M_BUS,
1014 M_INTWAIT | M_ZERO);
1015 if (!dev->softc) {
1016 kobj_delete((kobj_t)dev, 0);
1017 kobj_init((kobj_t) dev, &null_class);
1018 dev->driver = NULL;
1019 return(ENOMEM);
1022 } else
1023 kobj_init((kobj_t) dev, &null_class);
1024 return(0);
1028 device_probe_and_attach(device_t dev)
1030 device_t bus = dev->parent;
1031 int error = 0;
1032 int hasclass = (dev->devclass != 0);
1034 if (dev->state >= DS_ALIVE)
1035 return(0);
1037 if ((dev->flags & DF_ENABLED) == 0) {
1038 if (bootverbose) {
1039 device_print_prettyname(dev);
1040 printf("not probed (disabled)\n");
1042 return(0);
1045 error = device_probe_child(bus, dev);
1046 if (error) {
1047 if (!(dev->flags & DF_DONENOMATCH)) {
1048 BUS_PROBE_NOMATCH(bus, dev);
1049 dev->flags |= DF_DONENOMATCH;
1051 return(error);
1055 * Output the exact device chain prior to the attach in case the
1056 * system locks up during attach, and generate the full info after
1057 * the attach so correct irq and other information is displayed.
1059 if (bootverbose && !device_is_quiet(dev)) {
1060 device_t tmp;
1062 printf("%s", device_get_nameunit(dev));
1063 for (tmp = dev->parent; tmp; tmp = tmp->parent) {
1064 const char *desc;
1066 if ((desc = device_get_desc(tmp)) != NULL)
1067 printf(".%s[%s]", device_get_nameunit(tmp), desc);
1068 else
1069 printf(".%s", device_get_nameunit(tmp));
1071 printf("\n");
1073 if (!device_is_quiet(dev))
1074 device_print_child(bus, dev);
1075 error = DEVICE_ATTACH(dev);
1076 if (error == 0) {
1077 dev->state = DS_ATTACHED;
1078 if (bootverbose && !device_is_quiet(dev))
1079 device_print_child(bus, dev);
1080 } else {
1081 printf("device_probe_and_attach: %s%d attach returned %d\n",
1082 dev->driver->name, dev->unit, error);
1083 /* Unset the class that was set in device_probe_child */
1084 if (!hasclass)
1085 device_set_devclass(dev, 0);
1086 device_set_driver(dev, NULL);
1087 dev->state = DS_NOTPRESENT;
1090 return(error);
1094 device_detach(device_t dev)
1096 int error;
1098 PDEBUG(("%s", DEVICENAME(dev)));
1099 if (dev->state == DS_BUSY)
1100 return(EBUSY);
1101 if (dev->state != DS_ATTACHED)
1102 return(0);
1104 if ((error = DEVICE_DETACH(dev)) != 0)
1105 return(error);
1106 device_printf(dev, "detached\n");
1107 if (dev->parent)
1108 BUS_CHILD_DETACHED(dev->parent, dev);
1110 if (!(dev->flags & DF_FIXEDCLASS))
1111 devclass_delete_device(dev->devclass, dev);
1113 dev->state = DS_NOTPRESENT;
1114 device_set_driver(dev, NULL);
1116 return(0);
1120 device_shutdown(device_t dev)
1122 if (dev->state < DS_ATTACHED)
1123 return 0;
1124 PDEBUG(("%s", DEVICENAME(dev)));
1125 return DEVICE_SHUTDOWN(dev);
1129 device_set_unit(device_t dev, int unit)
1131 devclass_t dc;
1132 int err;
1134 dc = device_get_devclass(dev);
1135 if (unit < dc->maxunit && dc->devices[unit])
1136 return(EBUSY);
1137 err = devclass_delete_device(dc, dev);
1138 if (err)
1139 return(err);
1140 dev->unit = unit;
1141 err = devclass_add_device(dc, dev);
1142 return(err);
1145 #ifdef DEVICE_SYSCTLS
1148 * Sysctl nodes for devices.
1151 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1153 static int
1154 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1156 device_t dev = arg1;
1157 device_t child;
1158 int first = 1, error = 0;
1160 TAILQ_FOREACH(child, &dev->children, link)
1161 if (child->nameunit) {
1162 if (!first) {
1163 error = SYSCTL_OUT(req, ",", 1);
1164 if (error)
1165 return error;
1166 } else
1167 first = 0;
1168 error = SYSCTL_OUT(req, child->nameunit,
1169 strlen(child->nameunit));
1170 if (error)
1171 return(error);
1174 error = SYSCTL_OUT(req, "", 1);
1176 return(error);
1179 static int
1180 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1182 device_t dev = arg1;
1184 switch (dev->state) {
1185 case DS_NOTPRESENT:
1186 return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1187 case DS_ALIVE:
1188 return SYSCTL_OUT(req, "alive", sizeof("alive"));
1189 case DS_ATTACHED:
1190 return SYSCTL_OUT(req, "attached", sizeof("attached"));
1191 case DS_BUSY:
1192 return SYSCTL_OUT(req, "busy", sizeof("busy"));
1193 default:
1194 return (0);
1198 static void
1199 device_register_oids(device_t dev)
1201 struct sysctl_oid* oid;
1203 oid = &dev->oid[0];
1204 bzero(oid, sizeof(*oid));
1205 oid->oid_parent = &sysctl__hw_devices_children;
1206 oid->oid_number = OID_AUTO;
1207 oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1208 oid->oid_arg1 = &dev->oidlist[0];
1209 oid->oid_arg2 = 0;
1210 oid->oid_name = dev->nameunit;
1211 oid->oid_handler = 0;
1212 oid->oid_fmt = "N";
1213 SLIST_INIT(&dev->oidlist[0]);
1214 sysctl_register_oid(oid);
1216 oid = &dev->oid[1];
1217 bzero(oid, sizeof(*oid));
1218 oid->oid_parent = &dev->oidlist[0];
1219 oid->oid_number = OID_AUTO;
1220 oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1221 oid->oid_arg1 = dev->desc ? dev->desc : "";
1222 oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1223 oid->oid_name = "desc";
1224 oid->oid_handler = sysctl_handle_string;
1225 oid->oid_fmt = "A";
1226 sysctl_register_oid(oid);
1228 oid = &dev->oid[2];
1229 bzero(oid, sizeof(*oid));
1230 oid->oid_parent = &dev->oidlist[0];
1231 oid->oid_number = OID_AUTO;
1232 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1233 oid->oid_arg1 = dev;
1234 oid->oid_arg2 = 0;
1235 oid->oid_name = "children";
1236 oid->oid_handler = sysctl_handle_children;
1237 oid->oid_fmt = "A";
1238 sysctl_register_oid(oid);
1240 oid = &dev->oid[3];
1241 bzero(oid, sizeof(*oid));
1242 oid->oid_parent = &dev->oidlist[0];
1243 oid->oid_number = OID_AUTO;
1244 oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1245 oid->oid_arg1 = dev;
1246 oid->oid_arg2 = 0;
1247 oid->oid_name = "state";
1248 oid->oid_handler = sysctl_handle_state;
1249 oid->oid_fmt = "A";
1250 sysctl_register_oid(oid);
1253 static void
1254 device_unregister_oids(device_t dev)
1256 sysctl_unregister_oid(&dev->oid[0]);
1257 sysctl_unregister_oid(&dev->oid[1]);
1258 sysctl_unregister_oid(&dev->oid[2]);
1261 #endif
1263 /*======================================*/
1265 * Access functions for device resources.
1268 /* Supplied by config(8) in ioconf.c */
1269 extern struct config_device config_devtab[];
1270 extern int devtab_count;
1272 /* Runtime version */
1273 struct config_device *devtab = config_devtab;
1275 static int
1276 resource_new_name(const char *name, int unit)
1278 struct config_device *new;
1280 new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1281 M_INTWAIT | M_ZERO);
1282 if (new == NULL)
1283 return(-1);
1284 if (devtab && devtab_count > 0)
1285 bcopy(devtab, new, devtab_count * sizeof(*new));
1286 new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1287 if (new[devtab_count].name == NULL) {
1288 free(new, M_TEMP);
1289 return(-1);
1291 strcpy(new[devtab_count].name, name);
1292 new[devtab_count].unit = unit;
1293 new[devtab_count].resource_count = 0;
1294 new[devtab_count].resources = NULL;
1295 if (devtab && devtab != config_devtab)
1296 free(devtab, M_TEMP);
1297 devtab = new;
1298 return devtab_count++;
1301 static int
1302 resource_new_resname(int j, const char *resname, resource_type type)
1304 struct config_resource *new;
1305 int i;
1307 i = devtab[j].resource_count;
1308 new = malloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1309 if (new == NULL)
1310 return(-1);
1311 if (devtab[j].resources && i > 0)
1312 bcopy(devtab[j].resources, new, i * sizeof(*new));
1313 new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1314 if (new[i].name == NULL) {
1315 free(new, M_TEMP);
1316 return(-1);
1318 strcpy(new[i].name, resname);
1319 new[i].type = type;
1320 if (devtab[j].resources)
1321 free(devtab[j].resources, M_TEMP);
1322 devtab[j].resources = new;
1323 devtab[j].resource_count = i + 1;
1324 return(i);
1327 static int
1328 resource_match_string(int i, const char *resname, const char *value)
1330 int j;
1331 struct config_resource *res;
1333 for (j = 0, res = devtab[i].resources;
1334 j < devtab[i].resource_count; j++, res++)
1335 if (!strcmp(res->name, resname)
1336 && res->type == RES_STRING
1337 && !strcmp(res->u.stringval, value))
1338 return(j);
1339 return(-1);
1342 static int
1343 resource_find(const char *name, int unit, const char *resname,
1344 struct config_resource **result)
1346 int i, j;
1347 struct config_resource *res;
1350 * First check specific instances, then generic.
1352 for (i = 0; i < devtab_count; i++) {
1353 if (devtab[i].unit < 0)
1354 continue;
1355 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1356 res = devtab[i].resources;
1357 for (j = 0; j < devtab[i].resource_count; j++, res++)
1358 if (!strcmp(res->name, resname)) {
1359 *result = res;
1360 return(0);
1364 for (i = 0; i < devtab_count; i++) {
1365 if (devtab[i].unit >= 0)
1366 continue;
1367 /* XXX should this `&& devtab[i].unit == unit' be here? */
1368 /* XXX if so, then the generic match does nothing */
1369 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1370 res = devtab[i].resources;
1371 for (j = 0; j < devtab[i].resource_count; j++, res++)
1372 if (!strcmp(res->name, resname)) {
1373 *result = res;
1374 return(0);
1378 return(ENOENT);
1382 resource_int_value(const char *name, int unit, const char *resname, int *result)
1384 int error;
1385 struct config_resource *res;
1387 if ((error = resource_find(name, unit, resname, &res)) != 0)
1388 return(error);
1389 if (res->type != RES_INT)
1390 return(EFTYPE);
1391 *result = res->u.intval;
1392 return(0);
1396 resource_long_value(const char *name, int unit, const char *resname,
1397 long *result)
1399 int error;
1400 struct config_resource *res;
1402 if ((error = resource_find(name, unit, resname, &res)) != 0)
1403 return(error);
1404 if (res->type != RES_LONG)
1405 return(EFTYPE);
1406 *result = res->u.longval;
1407 return(0);
1411 resource_string_value(const char *name, int unit, const char *resname,
1412 char **result)
1414 int error;
1415 struct config_resource *res;
1417 if ((error = resource_find(name, unit, resname, &res)) != 0)
1418 return(error);
1419 if (res->type != RES_STRING)
1420 return(EFTYPE);
1421 *result = res->u.stringval;
1422 return(0);
1426 resource_query_string(int i, const char *resname, const char *value)
1428 if (i < 0)
1429 i = 0;
1430 else
1431 i = i + 1;
1432 for (; i < devtab_count; i++)
1433 if (resource_match_string(i, resname, value) >= 0)
1434 return(i);
1435 return(-1);
1439 resource_locate(int i, const char *resname)
1441 if (i < 0)
1442 i = 0;
1443 else
1444 i = i + 1;
1445 for (; i < devtab_count; i++)
1446 if (!strcmp(devtab[i].name, resname))
1447 return(i);
1448 return(-1);
1452 resource_count(void)
1454 return(devtab_count);
1457 char *
1458 resource_query_name(int i)
1460 return(devtab[i].name);
1464 resource_query_unit(int i)
1466 return(devtab[i].unit);
1469 static int
1470 resource_create(const char *name, int unit, const char *resname,
1471 resource_type type, struct config_resource **result)
1473 int i, j;
1474 struct config_resource *res = NULL;
1476 for (i = 0; i < devtab_count; i++)
1477 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1478 res = devtab[i].resources;
1479 break;
1481 if (res == NULL) {
1482 i = resource_new_name(name, unit);
1483 if (i < 0)
1484 return(ENOMEM);
1485 res = devtab[i].resources;
1487 for (j = 0; j < devtab[i].resource_count; j++, res++)
1488 if (!strcmp(res->name, resname)) {
1489 *result = res;
1490 return(0);
1492 j = resource_new_resname(i, resname, type);
1493 if (j < 0)
1494 return(ENOMEM);
1495 res = &devtab[i].resources[j];
1496 *result = res;
1497 return(0);
1501 resource_set_int(const char *name, int unit, const char *resname, int value)
1503 int error;
1504 struct config_resource *res;
1506 error = resource_create(name, unit, resname, RES_INT, &res);
1507 if (error)
1508 return(error);
1509 if (res->type != RES_INT)
1510 return(EFTYPE);
1511 res->u.intval = value;
1512 return(0);
1516 resource_set_long(const char *name, int unit, const char *resname, long value)
1518 int error;
1519 struct config_resource *res;
1521 error = resource_create(name, unit, resname, RES_LONG, &res);
1522 if (error)
1523 return(error);
1524 if (res->type != RES_LONG)
1525 return(EFTYPE);
1526 res->u.longval = value;
1527 return(0);
1531 resource_set_string(const char *name, int unit, const char *resname,
1532 const char *value)
1534 int error;
1535 struct config_resource *res;
1537 error = resource_create(name, unit, resname, RES_STRING, &res);
1538 if (error)
1539 return(error);
1540 if (res->type != RES_STRING)
1541 return(EFTYPE);
1542 if (res->u.stringval)
1543 free(res->u.stringval, M_TEMP);
1544 res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
1545 if (res->u.stringval == NULL)
1546 return(ENOMEM);
1547 strcpy(res->u.stringval, value);
1548 return(0);
1551 static void
1552 resource_cfgload(void *dummy __unused)
1554 struct config_resource *res, *cfgres;
1555 int i, j;
1556 int error;
1557 char *name, *resname;
1558 int unit;
1559 resource_type type;
1560 char *stringval;
1561 int config_devtab_count;
1563 config_devtab_count = devtab_count;
1564 devtab = NULL;
1565 devtab_count = 0;
1567 for (i = 0; i < config_devtab_count; i++) {
1568 name = config_devtab[i].name;
1569 unit = config_devtab[i].unit;
1571 for (j = 0; j < config_devtab[i].resource_count; j++) {
1572 cfgres = config_devtab[i].resources;
1573 resname = cfgres[j].name;
1574 type = cfgres[j].type;
1575 error = resource_create(name, unit, resname, type,
1576 &res);
1577 if (error) {
1578 printf("create resource %s%d: error %d\n",
1579 name, unit, error);
1580 continue;
1582 if (res->type != type) {
1583 printf("type mismatch %s%d: %d != %d\n",
1584 name, unit, res->type, type);
1585 continue;
1587 switch (type) {
1588 case RES_INT:
1589 res->u.intval = cfgres[j].u.intval;
1590 break;
1591 case RES_LONG:
1592 res->u.longval = cfgres[j].u.longval;
1593 break;
1594 case RES_STRING:
1595 if (res->u.stringval)
1596 free(res->u.stringval, M_TEMP);
1597 stringval = cfgres[j].u.stringval;
1598 res->u.stringval = malloc(strlen(stringval) + 1,
1599 M_TEMP, M_INTWAIT);
1600 if (res->u.stringval == NULL)
1601 break;
1602 strcpy(res->u.stringval, stringval);
1603 break;
1604 default:
1605 panic("unknown resource type %d", type);
1610 SYSINIT(cfgload, SI_SUB_KMEM, SI_ORDER_ANY + 50, resource_cfgload, 0)
1613 /*======================================*/
1615 * Some useful method implementations to make life easier for bus drivers.
1618 void
1619 resource_list_init(struct resource_list *rl)
1621 SLIST_INIT(rl);
1624 void
1625 resource_list_free(struct resource_list *rl)
1627 struct resource_list_entry *rle;
1629 while ((rle = SLIST_FIRST(rl)) != NULL) {
1630 if (rle->res)
1631 panic("resource_list_free: resource entry is busy");
1632 SLIST_REMOVE_HEAD(rl, link);
1633 free(rle, M_BUS);
1637 void
1638 resource_list_add(struct resource_list *rl,
1639 int type, int rid,
1640 u_long start, u_long end, u_long count)
1642 struct resource_list_entry *rle;
1644 rle = resource_list_find(rl, type, rid);
1645 if (rle == NULL) {
1646 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
1647 M_INTWAIT);
1648 if (!rle)
1649 panic("resource_list_add: can't record entry");
1650 SLIST_INSERT_HEAD(rl, rle, link);
1651 rle->type = type;
1652 rle->rid = rid;
1653 rle->res = NULL;
1656 if (rle->res)
1657 panic("resource_list_add: resource entry is busy");
1659 rle->start = start;
1660 rle->end = end;
1661 rle->count = count;
1664 struct resource_list_entry*
1665 resource_list_find(struct resource_list *rl,
1666 int type, int rid)
1668 struct resource_list_entry *rle;
1670 SLIST_FOREACH(rle, rl, link)
1671 if (rle->type == type && rle->rid == rid)
1672 return(rle);
1673 return(NULL);
1676 void
1677 resource_list_delete(struct resource_list *rl,
1678 int type, int rid)
1680 struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1682 if (rle) {
1683 SLIST_REMOVE(rl, rle, resource_list_entry, link);
1684 free(rle, M_BUS);
1688 struct resource *
1689 resource_list_alloc(struct resource_list *rl,
1690 device_t bus, device_t child,
1691 int type, int *rid,
1692 u_long start, u_long end,
1693 u_long count, u_int flags)
1695 struct resource_list_entry *rle = 0;
1696 int passthrough = (device_get_parent(child) != bus);
1697 int isdefault = (start == 0UL && end == ~0UL);
1699 if (passthrough) {
1700 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1701 type, rid,
1702 start, end, count, flags));
1705 rle = resource_list_find(rl, type, *rid);
1707 if (!rle)
1708 return(0); /* no resource of that type/rid */
1709 if (rle->res)
1710 panic("resource_list_alloc: resource entry is busy");
1712 if (isdefault) {
1713 start = rle->start;
1714 count = max(count, rle->count);
1715 end = max(rle->end, start + count - 1);
1718 rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1719 type, rid, start, end, count, flags);
1722 * Record the new range.
1724 if (rle->res) {
1725 rle->start = rman_get_start(rle->res);
1726 rle->end = rman_get_end(rle->res);
1727 rle->count = count;
1730 return(rle->res);
1734 resource_list_release(struct resource_list *rl,
1735 device_t bus, device_t child,
1736 int type, int rid, struct resource *res)
1738 struct resource_list_entry *rle = 0;
1739 int passthrough = (device_get_parent(child) != bus);
1740 int error;
1742 if (passthrough) {
1743 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1744 type, rid, res));
1747 rle = resource_list_find(rl, type, rid);
1749 if (!rle)
1750 panic("resource_list_release: can't find resource");
1751 if (!rle->res)
1752 panic("resource_list_release: resource entry is not busy");
1754 error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1755 type, rid, res);
1756 if (error)
1757 return(error);
1759 rle->res = NULL;
1760 return(0);
1764 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1765 const char *format)
1767 struct resource_list_entry *rle;
1768 int printed, retval;
1770 printed = 0;
1771 retval = 0;
1772 /* Yes, this is kinda cheating */
1773 SLIST_FOREACH(rle, rl, link) {
1774 if (rle->type == type) {
1775 if (printed == 0)
1776 retval += printf(" %s ", name);
1777 else
1778 retval += printf(",");
1779 printed++;
1780 retval += printf(format, rle->start);
1781 if (rle->count > 1) {
1782 retval += printf("-");
1783 retval += printf(format, rle->start +
1784 rle->count - 1);
1788 return(retval);
1792 * Call DEVICE_IDENTIFY for each driver.
1795 bus_generic_probe(device_t dev)
1797 devclass_t dc = dev->devclass;
1798 driverlink_t dl;
1800 TAILQ_FOREACH(dl, &dc->drivers, link)
1801 DEVICE_IDENTIFY(dl->driver, dev);
1803 return(0);
1807 bus_generic_attach(device_t dev)
1809 device_t child;
1811 TAILQ_FOREACH(child, &dev->children, link)
1812 device_probe_and_attach(child);
1814 return(0);
1818 bus_generic_detach(device_t dev)
1820 device_t child;
1821 int error;
1823 if (dev->state != DS_ATTACHED)
1824 return(EBUSY);
1826 TAILQ_FOREACH(child, &dev->children, link)
1827 if ((error = device_detach(child)) != 0)
1828 return(error);
1830 return 0;
1834 bus_generic_shutdown(device_t dev)
1836 device_t child;
1838 TAILQ_FOREACH(child, &dev->children, link)
1839 device_shutdown(child);
1841 return(0);
1845 bus_generic_suspend(device_t dev)
1847 int error;
1848 device_t child, child2;
1850 TAILQ_FOREACH(child, &dev->children, link) {
1851 error = DEVICE_SUSPEND(child);
1852 if (error) {
1853 for (child2 = TAILQ_FIRST(&dev->children);
1854 child2 && child2 != child;
1855 child2 = TAILQ_NEXT(child2, link))
1856 DEVICE_RESUME(child2);
1857 return(error);
1860 return(0);
1864 bus_generic_resume(device_t dev)
1866 device_t child;
1868 TAILQ_FOREACH(child, &dev->children, link)
1869 DEVICE_RESUME(child);
1870 /* if resume fails, there's nothing we can usefully do... */
1872 return(0);
1876 bus_print_child_header(device_t dev, device_t child)
1878 int retval = 0;
1880 if (device_get_desc(child))
1881 retval += device_printf(child, "<%s>", device_get_desc(child));
1882 else
1883 retval += printf("%s", device_get_nameunit(child));
1884 if (bootverbose) {
1885 if (child->state != DS_ATTACHED)
1886 printf(" [tentative]");
1887 else
1888 printf(" [attached!]");
1890 return(retval);
1894 bus_print_child_footer(device_t dev, device_t child)
1896 return(printf(" on %s\n", device_get_nameunit(dev)));
1900 bus_generic_print_child(device_t dev, device_t child)
1902 int retval = 0;
1904 retval += bus_print_child_header(dev, child);
1905 retval += bus_print_child_footer(dev, child);
1907 return(retval);
1911 bus_generic_read_ivar(device_t dev, device_t child, int index,
1912 uintptr_t * result)
1914 return(ENOENT);
1918 bus_generic_write_ivar(device_t dev, device_t child, int index,
1919 uintptr_t value)
1921 return(ENOENT);
1924 struct resource_list *
1925 bus_generic_get_resource_list(device_t dev, device_t child)
1927 return(NULL);
1930 void
1931 bus_generic_driver_added(device_t dev, driver_t *driver)
1933 device_t child;
1935 DEVICE_IDENTIFY(driver, dev);
1936 TAILQ_FOREACH(child, &dev->children, link)
1937 if (child->state == DS_NOTPRESENT)
1938 device_probe_and_attach(child);
1942 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
1943 int flags, driver_intr_t *intr, void *arg,
1944 void **cookiep)
1946 /* Propagate up the bus hierarchy until someone handles it. */
1947 if (dev->parent)
1948 return(BUS_SETUP_INTR(dev->parent, child, irq, flags,
1949 intr, arg, cookiep));
1950 else
1951 return(EINVAL);
1955 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
1956 void *cookie)
1958 /* Propagate up the bus hierarchy until someone handles it. */
1959 if (dev->parent)
1960 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
1961 else
1962 return(EINVAL);
1965 struct resource *
1966 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
1967 u_long start, u_long end, u_long count, u_int flags)
1969 /* Propagate up the bus hierarchy until someone handles it. */
1970 if (dev->parent)
1971 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
1972 start, end, count, flags));
1973 else
1974 return(NULL);
1978 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
1979 struct resource *r)
1981 /* Propagate up the bus hierarchy until someone handles it. */
1982 if (dev->parent)
1983 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
1984 else
1985 return(EINVAL);
1989 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
1990 struct resource *r)
1992 /* Propagate up the bus hierarchy until someone handles it. */
1993 if (dev->parent)
1994 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
1995 else
1996 return(EINVAL);
2000 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2001 int rid, struct resource *r)
2003 /* Propagate up the bus hierarchy until someone handles it. */
2004 if (dev->parent)
2005 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2006 r));
2007 else
2008 return(EINVAL);
2012 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
2013 enum intr_polarity pol)
2015 /* Propagate up the bus hierarchy until someone handles it. */
2016 if (dev->parent)
2017 return(BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
2018 else
2019 return(EINVAL);
2023 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2024 u_long *startp, u_long *countp)
2026 struct resource_list *rl = NULL;
2027 struct resource_list_entry *rle = NULL;
2029 rl = BUS_GET_RESOURCE_LIST(dev, child);
2030 if (!rl)
2031 return(EINVAL);
2033 rle = resource_list_find(rl, type, rid);
2034 if (!rle)
2035 return(ENOENT);
2037 if (startp)
2038 *startp = rle->start;
2039 if (countp)
2040 *countp = rle->count;
2042 return(0);
2046 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2047 u_long start, u_long count)
2049 struct resource_list *rl = NULL;
2051 rl = BUS_GET_RESOURCE_LIST(dev, child);
2052 if (!rl)
2053 return(EINVAL);
2055 resource_list_add(rl, type, rid, start, (start + count - 1), count);
2057 return(0);
2060 void
2061 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2063 struct resource_list *rl = NULL;
2065 rl = BUS_GET_RESOURCE_LIST(dev, child);
2066 if (!rl)
2067 return;
2069 resource_list_delete(rl, type, rid);
2073 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2074 int rid, struct resource *r)
2076 struct resource_list *rl = NULL;
2078 rl = BUS_GET_RESOURCE_LIST(dev, child);
2079 if (!rl)
2080 return(EINVAL);
2082 return(resource_list_release(rl, dev, child, type, rid, r));
2085 struct resource *
2086 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2087 int *rid, u_long start, u_long end, u_long count, u_int flags)
2089 struct resource_list *rl = NULL;
2091 rl = BUS_GET_RESOURCE_LIST(dev, child);
2092 if (!rl)
2093 return(NULL);
2095 return(resource_list_alloc(rl, dev, child, type, rid,
2096 start, end, count, flags));
2100 bus_generic_child_present(device_t bus, device_t child)
2102 return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2107 * Some convenience functions to make it easier for drivers to use the
2108 * resource-management functions. All these really do is hide the
2109 * indirection through the parent's method table, making for slightly
2110 * less-wordy code. In the future, it might make sense for this code
2111 * to maintain some sort of a list of resources allocated by each device.
2113 struct resource *
2114 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2115 u_long count, u_int flags)
2117 if (dev->parent == 0)
2118 return(0);
2119 return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2120 count, flags));
2124 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2126 if (dev->parent == 0)
2127 return(EINVAL);
2128 return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2132 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2134 if (dev->parent == 0)
2135 return(EINVAL);
2136 return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2140 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2142 if (dev->parent == 0)
2143 return(EINVAL);
2144 return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2148 bus_setup_intr(device_t dev, struct resource *r, int flags,
2149 driver_intr_t handler, void *arg, void **cookiep)
2151 if (dev->parent == 0)
2152 return(EINVAL);
2153 return(BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2154 cookiep));
2158 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2160 if (dev->parent == 0)
2161 return(EINVAL);
2162 return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2166 bus_set_resource(device_t dev, int type, int rid,
2167 u_long start, u_long count)
2169 return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2170 start, count));
2174 bus_get_resource(device_t dev, int type, int rid,
2175 u_long *startp, u_long *countp)
2177 return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2178 startp, countp));
2181 u_long
2182 bus_get_resource_start(device_t dev, int type, int rid)
2184 u_long start, count;
2185 int error;
2187 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2188 &start, &count);
2189 if (error)
2190 return(0);
2191 return(start);
2194 u_long
2195 bus_get_resource_count(device_t dev, int type, int rid)
2197 u_long start, count;
2198 int error;
2200 error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2201 &start, &count);
2202 if (error)
2203 return(0);
2204 return(count);
2207 void
2208 bus_delete_resource(device_t dev, int type, int rid)
2210 BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2214 bus_child_present(device_t child)
2216 return (BUS_CHILD_PRESENT(device_get_parent(child), child));
2220 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
2222 device_t parent;
2224 parent = device_get_parent(child);
2225 if (parent == NULL) {
2226 *buf = '\0';
2227 return (0);
2229 return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
2233 bus_child_location_str(device_t child, char *buf, size_t buflen)
2235 device_t parent;
2237 parent = device_get_parent(child);
2238 if (parent == NULL) {
2239 *buf = '\0';
2240 return (0);
2242 return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
2245 static int
2246 root_print_child(device_t dev, device_t child)
2248 return(0);
2251 static int
2252 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2253 void **cookiep)
2256 * If an interrupt mapping gets to here something bad has happened.
2258 panic("root_setup_intr");
2262 * If we get here, assume that the device is permanant and really is
2263 * present in the system. Removable bus drivers are expected to intercept
2264 * this call long before it gets here. We return -1 so that drivers that
2265 * really care can check vs -1 or some ERRNO returned higher in the food
2266 * chain.
2268 static int
2269 root_child_present(device_t dev, device_t child)
2271 return(-1);
2275 * XXX NOTE! other defaults may be set in bus_if.m
2277 static kobj_method_t root_methods[] = {
2278 /* Device interface */
2279 KOBJMETHOD(device_shutdown, bus_generic_shutdown),
2280 KOBJMETHOD(device_suspend, bus_generic_suspend),
2281 KOBJMETHOD(device_resume, bus_generic_resume),
2283 /* Bus interface */
2284 KOBJMETHOD(bus_print_child, root_print_child),
2285 KOBJMETHOD(bus_read_ivar, bus_generic_read_ivar),
2286 KOBJMETHOD(bus_write_ivar, bus_generic_write_ivar),
2287 KOBJMETHOD(bus_setup_intr, root_setup_intr),
2288 KOBJMETHOD(bus_child_present, root_child_present),
2290 { 0, 0 }
2293 static driver_t root_driver = {
2294 "root",
2295 root_methods,
2296 1, /* no softc */
2299 device_t root_bus;
2300 devclass_t root_devclass;
2302 static int
2303 root_bus_module_handler(module_t mod, int what, void* arg)
2305 switch (what) {
2306 case MOD_LOAD:
2307 root_bus = make_device(NULL, "root", 0);
2308 root_bus->desc = "System root bus";
2309 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
2310 root_bus->driver = &root_driver;
2311 root_bus->state = DS_ATTACHED;
2312 root_devclass = devclass_find_internal("root", NULL, FALSE);
2313 return(0);
2315 case MOD_SHUTDOWN:
2316 device_shutdown(root_bus);
2317 return(0);
2318 default:
2319 return(0);
2323 static moduledata_t root_bus_mod = {
2324 "rootbus",
2325 root_bus_module_handler,
2328 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2330 void
2331 root_bus_configure(void)
2333 device_t dev;
2335 PDEBUG(("."));
2337 TAILQ_FOREACH(dev, &root_bus->children, link)
2338 device_probe_and_attach(dev);
2342 driver_module_handler(module_t mod, int what, void *arg)
2344 int error;
2345 struct driver_module_data *dmd;
2346 devclass_t bus_devclass;
2347 kobj_class_t driver;
2348 const char *parentname;
2350 dmd = (struct driver_module_data *)arg;
2351 bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
2352 error = 0;
2354 switch (what) {
2355 case MOD_LOAD:
2356 if (dmd->dmd_chainevh)
2357 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2359 driver = dmd->dmd_driver;
2360 PDEBUG(("Loading module: driver %s on bus %s",
2361 DRIVERNAME(driver), dmd->dmd_busname));
2362 error = devclass_add_driver(bus_devclass, driver);
2363 if (error)
2364 break;
2367 * If the driver has any base classes, make the
2368 * devclass inherit from the devclass of the driver's
2369 * first base class. This will allow the system to
2370 * search for drivers in both devclasses for children
2371 * of a device using this driver.
2373 if (driver->baseclasses)
2374 parentname = driver->baseclasses[0]->name;
2375 else
2376 parentname = NULL;
2377 *dmd->dmd_devclass = devclass_find_internal(driver->name,
2378 parentname, TRUE);
2379 break;
2381 case MOD_UNLOAD:
2382 PDEBUG(("Unloading module: driver %s from bus %s",
2383 DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
2384 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
2386 if (!error && dmd->dmd_chainevh)
2387 error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2388 break;
2391 return (error);
2394 #ifdef BUS_DEBUG
2397 * The _short versions avoid iteration by not calling anything that prints
2398 * more than oneliners. I love oneliners.
2401 static void
2402 print_device_short(device_t dev, int indent)
2404 if (!dev)
2405 return;
2407 indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2408 dev->unit, dev->desc,
2409 (dev->parent? "":"no "),
2410 (TAILQ_EMPTY(&dev->children)? "no ":""),
2411 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2412 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2413 (dev->flags&DF_WILDCARD? "wildcard,":""),
2414 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2415 (dev->ivars? "":"no "),
2416 (dev->softc? "":"no "),
2417 dev->busy));
2420 static void
2421 print_device(device_t dev, int indent)
2423 if (!dev)
2424 return;
2426 print_device_short(dev, indent);
2428 indentprintf(("Parent:\n"));
2429 print_device_short(dev->parent, indent+1);
2430 indentprintf(("Driver:\n"));
2431 print_driver_short(dev->driver, indent+1);
2432 indentprintf(("Devclass:\n"));
2433 print_devclass_short(dev->devclass, indent+1);
2437 * Print the device and all its children (indented).
2439 void
2440 print_device_tree_short(device_t dev, int indent)
2442 device_t child;
2444 if (!dev)
2445 return;
2447 print_device_short(dev, indent);
2449 TAILQ_FOREACH(child, &dev->children, link)
2450 print_device_tree_short(child, indent+1);
2454 * Print the device and all its children (indented).
2456 void
2457 print_device_tree(device_t dev, int indent)
2459 device_t child;
2461 if (!dev)
2462 return;
2464 print_device(dev, indent);
2466 TAILQ_FOREACH(child, &dev->children, link)
2467 print_device_tree(child, indent+1);
2470 static void
2471 print_driver_short(driver_t *driver, int indent)
2473 if (!driver)
2474 return;
2476 indentprintf(("driver %s: softc size = %d\n",
2477 driver->name, driver->size));
2480 static void
2481 print_driver(driver_t *driver, int indent)
2483 if (!driver)
2484 return;
2486 print_driver_short(driver, indent);
2490 static void
2491 print_driver_list(driver_list_t drivers, int indent)
2493 driverlink_t driver;
2495 TAILQ_FOREACH(driver, &drivers, link)
2496 print_driver(driver->driver, indent);
2499 static void
2500 print_devclass_short(devclass_t dc, int indent)
2502 if (!dc)
2503 return;
2505 indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
2508 static void
2509 print_devclass(devclass_t dc, int indent)
2511 int i;
2513 if (!dc)
2514 return;
2516 print_devclass_short(dc, indent);
2517 indentprintf(("Drivers:\n"));
2518 print_driver_list(dc->drivers, indent+1);
2520 indentprintf(("Devices:\n"));
2521 for (i = 0; i < dc->maxunit; i++)
2522 if (dc->devices[i])
2523 print_device(dc->devices[i], indent+1);
2526 void
2527 print_devclass_list_short(void)
2529 devclass_t dc;
2531 printf("Short listing of devclasses, drivers & devices:\n");
2532 TAILQ_FOREACH(dc, &devclasses, link) {
2533 print_devclass_short(dc, 0);
2537 void
2538 print_devclass_list(void)
2540 devclass_t dc;
2542 printf("Full listing of devclasses, drivers & devices:\n");
2543 TAILQ_FOREACH(dc, &devclasses, link) {
2544 print_devclass(dc, 0);
2548 #endif
2551 * Check to see if a device is disabled via a disabled hint.
2554 resource_disabled(const char *name, int unit)
2556 int error, value;
2558 error = resource_int_value(name, unit, "disabled", &value);
2559 if (error)
2560 return(0);
2561 return(value);