Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / kern / subr_bus.c
1 /*
2  * Copyright (c) 1997,1998 Doug Rabson
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  * $FreeBSD: src/sys/kern/subr_bus.c,v 1.54.2.9 2002/10/10 15:13:32 jhb Exp $
27  */
28
29 #include "opt_bus.h"
30
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 #ifdef DEVICE_SYSCTLS
37 #include <sys/sysctl.h>
38 #endif
39 #include <sys/bus_private.h>
40 #include <sys/systm.h>
41 #include <machine/bus.h>
42 #include <sys/rman.h>
43 #include <machine/stdarg.h>     /* for device_printf() */
44
45 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
46
47 #ifdef BUS_DEBUG
48 #define PDEBUG(a)       (printf(__FUNCTION__ ":%d: ", __LINE__), printf a, printf("\n"))
49 #define DEVICENAME(d)   ((d)? device_get_name(d): "no device")
50 #define DRIVERNAME(d)   ((d)? d->name : "no driver")
51 #define DEVCLANAME(d)   ((d)? d->name : "no devclass")
52
53 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 
54  * prevent syslog from deleting initial spaces
55  */
56 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while(0)
57
58 static void print_method_list(device_method_t *m, int indent);
59 static void print_device_ops(device_ops_t ops, int indent);
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);
71
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 */
78
79 #define print_method_list(m,i)          /* nop */
80 #define print_device_ops(o,i)           /* nop */
81 #define print_device_short(d,i)         /* nop */
82 #define print_device(d,i)               /* nop */
83 #define print_device_tree_short(d,i)    /* nop */
84 #define print_device_tree(d,i)          /* nop */
85 #define print_driver_short(d,i)         /* nop */
86 #define print_driver(d,i)               /* nop */
87 #define print_driver_list(d,i)          /* nop */
88 #define print_devclass_short(d,i)       /* nop */
89 #define print_devclass(d,i)             /* nop */
90 #define print_devclass_list_short()     /* nop */
91 #define print_devclass_list()           /* nop */
92 #endif
93
94 #ifdef DEVICE_SYSCTLS
95 static void device_register_oids(device_t dev);
96 static void device_unregister_oids(device_t dev);
97 #endif
98
99 /*
100  * Method table handling
101  */
102 static int error_method(void);
103 static int next_method_offset = 1;
104
105 LIST_HEAD(methodlist, method) methods;
106 struct method {
107     LIST_ENTRY(method) link;    /* linked list of methods */
108     int offset;                 /* offset in method table */
109     int refs;                   /* count of device_op_desc users */
110     devop_t deflt;              /* default implementation */
111     char* name;                 /* unique name of method */
112 };
113
114 static void
115 register_method(struct device_op_desc *desc)
116 {
117     struct method* m;
118
119     if (desc->method) {
120         desc->method->refs++;
121         return;
122     }
123
124     /*
125      * Make sure that desc->deflt is always valid to simplify dispatch.
126      */
127     if (!desc->deflt)
128         desc->deflt = error_method;
129
130     for (m = LIST_FIRST(&methods); m; m = LIST_NEXT(m, link)) {
131         if (!strcmp(m->name, desc->name)) {
132             desc->offset = m->offset;
133             desc->method = m;
134             m->refs++;
135             PDEBUG(("method %p has the same name, %s, with offset %d",
136                     (void *)m, desc->name, desc->offset));
137             return;
138         }
139     }
140
141     m = (struct method *) malloc(sizeof(struct method)
142                                  + strlen(desc->name) + 1,
143                                  M_BUS, M_NOWAIT);
144     if (!m)
145             panic("register_method: out of memory");
146     bzero(m, sizeof(struct method) + strlen(desc->name) + 1);
147     m->offset = next_method_offset++;
148     m->refs = 1;
149     m->deflt = desc->deflt;
150     m->name = (char*) (m + 1);
151     strcpy(m->name, desc->name);
152     LIST_INSERT_HEAD(&methods, m, link);
153
154     desc->offset = m->offset;
155     desc->method = m;
156 }
157
158 static void
159 unregister_method(struct device_op_desc *desc)
160 {
161     struct method *m = desc->method;
162     m->refs--;
163     if (m->refs == 0) {
164         PDEBUG(("method %s, reached refcount 0", desc->name));
165         LIST_REMOVE(m, link);
166         free(m, M_BUS);
167         desc->method = 0;
168     }
169 }
170
171 static int error_method(void)
172 {
173     return ENXIO;
174 }
175
176 static struct device_ops null_ops = {
177     1, 
178     { error_method }
179 };
180
181 static void
182 compile_methods(driver_t *driver)
183 {
184     device_ops_t ops;
185     struct device_method *m;
186     struct method *cm;
187     int i;
188
189     /*
190      * First register any methods which need it.
191      */
192     for (i = 0, m = driver->methods; m->desc; i++, m++)
193         register_method(m->desc);
194
195     /*
196      * Then allocate the compiled op table.
197      */
198     ops = malloc(sizeof(struct device_ops) + (next_method_offset-1) * sizeof(devop_t),
199                  M_BUS, M_NOWAIT);
200     if (!ops)
201         panic("compile_methods: out of memory");
202     bzero(ops, sizeof(struct device_ops) + (next_method_offset-1) * sizeof(devop_t));
203
204     ops->maxoffset = next_method_offset;
205     /* Fill in default methods and then overwrite with driver methods */
206     for (i = 0; i < next_method_offset; i++)
207         ops->methods[i] = error_method;
208     for (cm = LIST_FIRST(&methods); cm; cm = LIST_NEXT(cm, link)) {
209         if (cm->deflt)
210             ops->methods[cm->offset] = cm->deflt;
211     }
212     for (i = 0, m = driver->methods; m->desc; i++, m++)
213         ops->methods[m->desc->offset] = m->func;
214     PDEBUG(("%s has %d method%s, wasting %d bytes",
215                 DRIVERNAME(driver), i, (i==1?"":"s"),
216                 (next_method_offset-i)*sizeof(devop_t)));
217
218     driver->ops = ops;
219 }
220
221 static void
222 free_methods(driver_t *driver)
223 {
224     int i;
225     struct device_method *m;
226
227     /*
228      * Unregister any methods which are no longer used.
229      */
230     for (i = 0, m = driver->methods; m->desc; i++, m++)
231         unregister_method(m->desc);
232
233     /*
234      * Free memory and clean up.
235      */
236     free(driver->ops, M_BUS);
237     driver->ops = 0;
238 }
239
240 /*
241  * Devclass implementation
242  */
243
244 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
245
246 static devclass_t
247 devclass_find_internal(const char *classname, int create)
248 {
249     devclass_t dc;
250
251     PDEBUG(("looking for %s", classname));
252     if (!classname)
253         return NULL;
254
255     for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
256         if (!strcmp(dc->name, classname))
257             return dc;
258
259     PDEBUG(("%s not found%s", classname, (create? ", creating": "")));
260     if (create) {
261         dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
262                     M_BUS, M_NOWAIT);
263         if (!dc)
264             return NULL;
265         bzero(dc, sizeof(struct devclass) + strlen(classname) + 1);
266         dc->name = (char*) (dc + 1);
267         strcpy(dc->name, classname);
268         dc->devices = NULL;
269         dc->maxunit = 0;
270         TAILQ_INIT(&dc->drivers);
271         TAILQ_INSERT_TAIL(&devclasses, dc, link);
272     }
273
274     return dc;
275 }
276
277 devclass_t
278 devclass_create(const char *classname)
279 {
280     return devclass_find_internal(classname, TRUE);
281 }
282
283 devclass_t
284 devclass_find(const char *classname)
285 {
286     return devclass_find_internal(classname, FALSE);
287 }
288
289 int
290 devclass_add_driver(devclass_t dc, driver_t *driver)
291 {
292     driverlink_t dl;
293     int i;
294
295     PDEBUG(("%s", DRIVERNAME(driver)));
296
297     dl = malloc(sizeof *dl, M_BUS, M_NOWAIT);
298     if (!dl)
299         return ENOMEM;
300     bzero(dl, sizeof *dl);
301
302     /*
303      * Compile the driver's methods.
304      */
305     if (!driver->ops)
306         compile_methods(driver);
307
308     /*
309      * Make sure the devclass which the driver is implementing exists.
310      */
311     devclass_find_internal(driver->name, TRUE);
312
313     dl->driver = driver;
314     TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
315     driver->refs++;
316
317     /*
318      * Call BUS_DRIVER_ADDED for any existing busses in this class.
319      */
320     for (i = 0; i < dc->maxunit; i++)
321         if (dc->devices[i])
322             BUS_DRIVER_ADDED(dc->devices[i], driver);
323
324     return 0;
325 }
326
327 int
328 devclass_delete_driver(devclass_t busclass, driver_t *driver)
329 {
330     devclass_t dc = devclass_find(driver->name);
331     driverlink_t dl;
332     device_t dev;
333     int i;
334     int error;
335
336     PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
337
338     if (!dc)
339         return 0;
340
341     /*
342      * Find the link structure in the bus' list of drivers.
343      */
344     for (dl = TAILQ_FIRST(&busclass->drivers); dl;
345          dl = TAILQ_NEXT(dl, link)) {
346         if (dl->driver == driver)
347             break;
348     }
349
350     if (!dl) {
351         PDEBUG(("%s not found in %s list", driver->name, busclass->name));
352         return ENOENT;
353     }
354
355     /*
356      * Disassociate from any devices.  We iterate through all the
357      * devices in the devclass of the driver and detach any which are
358      * using the driver and which have a parent in the devclass which
359      * we are deleting from.
360      *
361      * Note that since a driver can be in multiple devclasses, we
362      * should not detach devices which are not children of devices in
363      * the affected devclass.
364      */
365     for (i = 0; i < dc->maxunit; i++) {
366         if (dc->devices[i]) {
367             dev = dc->devices[i];
368             if (dev->driver == driver
369                 && dev->parent && dev->parent->devclass == busclass) {
370                 if ((error = device_detach(dev)) != 0)
371                     return error;
372                 device_set_driver(dev, NULL);
373             }
374         }
375     }
376
377     TAILQ_REMOVE(&busclass->drivers, dl, link);
378     free(dl, M_BUS);
379
380     driver->refs--;
381     if (driver->refs == 0)
382         free_methods(driver);
383
384     return 0;
385 }
386
387 static driverlink_t
388 devclass_find_driver_internal(devclass_t dc, const char *classname)
389 {
390     driverlink_t dl;
391
392     PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
393
394     for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link)) {
395         if (!strcmp(dl->driver->name, classname))
396             return dl;
397     }
398
399     PDEBUG(("not found"));
400     return NULL;
401 }
402
403 driver_t *
404 devclass_find_driver(devclass_t dc, const char *classname)
405 {
406     driverlink_t dl;
407
408     dl = devclass_find_driver_internal(dc, classname);
409     if (dl)
410         return dl->driver;
411     else
412         return NULL;
413 }
414
415 const char *
416 devclass_get_name(devclass_t dc)
417 {
418     return dc->name;
419 }
420
421 device_t
422 devclass_get_device(devclass_t dc, int unit)
423 {
424     if (dc == NULL || unit < 0 || unit >= dc->maxunit)
425         return NULL;
426     return dc->devices[unit];
427 }
428
429 void *
430 devclass_get_softc(devclass_t dc, int unit)
431 {
432     device_t dev;
433
434     dev = devclass_get_device(dc, unit);
435     if (!dev)
436         return (NULL);
437
438     return (device_get_softc(dev));
439 }
440
441 int
442 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
443 {
444     int i;
445     int count;
446     device_t *list;
447     
448     count = 0;
449     for (i = 0; i < dc->maxunit; i++)
450         if (dc->devices[i])
451             count++;
452
453     list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
454     if (!list)
455         return ENOMEM;
456     bzero(list, count * sizeof(device_t));
457
458     count = 0;
459     for (i = 0; i < dc->maxunit; i++)
460         if (dc->devices[i]) {
461             list[count] = dc->devices[i];
462             count++;
463         }
464
465     *devlistp = list;
466     *devcountp = count;
467
468     return 0;
469 }
470
471 int
472 devclass_get_maxunit(devclass_t dc)
473 {
474     return dc->maxunit;
475 }
476
477 static int
478 devclass_alloc_unit(devclass_t dc, int *unitp)
479 {
480     int unit = *unitp;
481
482     PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
483
484     /* If we have been given a wired unit number, check for existing device */
485     if (unit != -1) {
486         if (unit >= 0 && unit < dc->maxunit && dc->devices[unit] != NULL) {
487             if (bootverbose)
488                 printf("%s-: %s%d exists, using next available unit number\n",
489                        dc->name, dc->name, unit);
490             /* find the next available slot */
491             while (++unit < dc->maxunit && dc->devices[unit] != NULL)
492                 ;
493         }
494     }
495     else {
496         /* Unwired device, find the next available slot for it */
497         unit = 0;
498         while (unit < dc->maxunit && dc->devices[unit] != NULL)
499             unit++;
500     }
501
502     /*
503      * We've selected a unit beyond the length of the table, so let's extend
504      * the table to make room for all units up to and including this one.
505      */
506     if (unit >= dc->maxunit) {
507         device_t *newlist;
508         int newsize;
509
510         newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
511         newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
512         if (!newlist)
513             return ENOMEM;
514         bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
515         bzero(newlist + dc->maxunit,
516               sizeof(device_t) * (newsize - dc->maxunit));
517         if (dc->devices)
518             free(dc->devices, M_BUS);
519         dc->devices = newlist;
520         dc->maxunit = newsize;
521     }
522     PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
523
524     *unitp = unit;
525     return 0;
526 }
527
528 static int
529 devclass_add_device(devclass_t dc, device_t dev)
530 {
531     int buflen, error;
532
533     PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
534
535     buflen = strlen(dc->name) + 5;
536     dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT);
537     if (!dev->nameunit)
538         return ENOMEM;
539     bzero(dev->nameunit, buflen);
540
541     if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
542         free(dev->nameunit, M_BUS);
543         dev->nameunit = NULL;
544         return error;
545     }
546     dc->devices[dev->unit] = dev;
547     dev->devclass = dc;
548     snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
549
550 #ifdef DEVICE_SYSCTLS
551     device_register_oids(dev);
552 #endif
553
554     return 0;
555 }
556
557 static int
558 devclass_delete_device(devclass_t dc, device_t dev)
559 {
560     if (!dc || !dev)
561         return 0;
562
563     PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
564
565     if (dev->devclass != dc
566         || dc->devices[dev->unit] != dev)
567         panic("devclass_delete_device: inconsistent device class");
568     dc->devices[dev->unit] = NULL;
569     if (dev->flags & DF_WILDCARD)
570         dev->unit = -1;
571     dev->devclass = NULL;
572     free(dev->nameunit, M_BUS);
573     dev->nameunit = NULL;
574
575 #ifdef DEVICE_SYSCTLS
576     device_unregister_oids(dev);
577 #endif
578
579     return 0;
580 }
581
582 static device_t
583 make_device(device_t parent, const char *name, int unit)
584 {
585     device_t dev;
586     devclass_t dc;
587
588     PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
589
590     if (name) {
591         dc = devclass_find_internal(name, TRUE);
592         if (!dc) {
593             printf("make_device: can't find device class %s\n", name);
594             return NULL;
595         }
596     } else
597         dc = NULL;
598
599     dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT);
600     if (!dev)
601         return 0;
602     bzero(dev, sizeof(struct device));
603
604     dev->parent = parent;
605     TAILQ_INIT(&dev->children);
606     dev->ops = &null_ops;
607     dev->driver = NULL;
608     dev->devclass = NULL;
609     dev->unit = unit;
610     dev->nameunit = NULL;
611     dev->desc = NULL;
612     dev->busy = 0;
613     dev->devflags = 0;
614     dev->flags = DF_ENABLED;
615     dev->order = 0;
616     if (unit == -1)
617         dev->flags |= DF_WILDCARD;
618     if (name) {
619         dev->flags |= DF_FIXEDCLASS;
620         devclass_add_device(dc, dev);
621     }
622     dev->ivars = NULL;
623     dev->softc = NULL;
624
625     dev->state = DS_NOTPRESENT;
626
627     return dev;
628 }
629
630 static int
631 device_print_child(device_t dev, device_t child)
632 {
633     int retval = 0;
634
635     if (device_is_alive(child)) {
636         retval += BUS_PRINT_CHILD(dev, child);
637     } else
638         retval += device_printf(child, " not found\n");
639
640     return (retval);
641 }
642
643 device_t
644 device_add_child(device_t dev, const char *name, int unit)
645 {
646     return device_add_child_ordered(dev, 0, name, unit);
647 }
648
649 device_t
650 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
651 {
652     device_t child;
653     device_t place;
654
655     PDEBUG(("%s at %s with order %d as unit %d",
656             name, DEVICENAME(dev), order, unit));
657
658     child = make_device(dev, name, unit);
659     if (child == NULL)
660         return child;
661     child->order = order;
662
663     TAILQ_FOREACH(place, &dev->children, link)
664         if (place->order > order)
665             break;
666
667     if (place) {
668         /*
669          * The device 'place' is the first device whose order is
670          * greater than the new child.
671          */
672         TAILQ_INSERT_BEFORE(place, child, link);
673     } else {
674         /*
675          * The new child's order is greater or equal to the order of
676          * any existing device. Add the child to the tail of the list.
677          */
678         TAILQ_INSERT_TAIL(&dev->children, child, link);
679     }
680
681     return child;
682 }
683
684 int
685 device_delete_child(device_t dev, device_t child)
686 {
687     int error;
688     device_t grandchild;
689
690     PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
691
692     /* remove children first */
693     while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
694         error = device_delete_child(child, grandchild);
695         if (error)
696             return error;
697     }
698
699     if ((error = device_detach(child)) != 0)
700         return error;
701     if (child->devclass)
702         devclass_delete_device(child->devclass, child);
703     TAILQ_REMOVE(&dev->children, child, link);
704     device_set_desc(child, NULL);
705     free(child, M_BUS);
706
707     return 0;
708 }
709
710 /*
711  * Find only devices attached to this bus.
712  */
713 device_t
714 device_find_child(device_t dev, const char *classname, int unit)
715 {
716     devclass_t dc;
717     device_t child;
718
719     dc = devclass_find(classname);
720     if (!dc)
721         return NULL;
722
723     child = devclass_get_device(dc, unit);
724     if (child && child->parent == dev)
725         return child;
726     return NULL;
727 }
728
729 static driverlink_t
730 first_matching_driver(devclass_t dc, device_t dev)
731 {
732     if (dev->devclass)
733         return devclass_find_driver_internal(dc, dev->devclass->name);
734     else
735         return TAILQ_FIRST(&dc->drivers);
736 }
737
738 static driverlink_t
739 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
740 {
741     if (dev->devclass) {
742         driverlink_t dl;
743         for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
744             if (!strcmp(dev->devclass->name, dl->driver->name))
745                 return dl;
746         return NULL;
747     } else
748         return TAILQ_NEXT(last, link);
749 }
750
751 static int
752 device_probe_child(device_t dev, device_t child)
753 {
754     devclass_t dc;
755     driverlink_t best = 0;
756     driverlink_t dl;
757     int result, pri = 0;
758     int hasclass = (child->devclass != 0);
759
760     dc = dev->devclass;
761     if (!dc)
762         panic("device_probe_child: parent device has no devclass");
763
764     if (child->state == DS_ALIVE)
765         return 0;
766
767     for (dl = first_matching_driver(dc, child);
768          dl;
769          dl = next_matching_driver(dc, child, dl)) {
770         PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
771         device_set_driver(child, dl->driver);
772         if (!hasclass)
773             device_set_devclass(child, dl->driver->name);
774         result = DEVICE_PROBE(child);
775         if (!hasclass)
776             device_set_devclass(child, 0);
777
778         /*
779          * If the driver returns SUCCESS, there can be no higher match
780          * for this device.
781          */
782         if (result == 0) {
783             best = dl;
784             pri = 0;
785             break;
786         }
787
788         /*
789          * The driver returned an error so it certainly doesn't match.
790          */
791         if (result > 0) {
792             device_set_driver(child, 0);
793             continue;
794         }
795
796         /*
797          * A priority lower than SUCCESS, remember the best matching
798          * driver. Initialise the value of pri for the first match.
799          */
800         if (best == 0 || result > pri) {
801             best = dl;
802             pri = result;
803             continue;
804         }
805     }
806
807     /*
808      * If we found a driver, change state and initialise the devclass.
809      */
810     if (best) {
811         if (!child->devclass)
812             device_set_devclass(child, best->driver->name);
813         device_set_driver(child, best->driver);
814         if (pri < 0) {
815             /*
816              * A bit bogus. Call the probe method again to make sure
817              * that we have the right description.
818              */
819             DEVICE_PROBE(child);
820         }
821         child->state = DS_ALIVE;
822         return 0;
823     }
824
825     return ENXIO;
826 }
827
828 device_t
829 device_get_parent(device_t dev)
830 {
831     return dev->parent;
832 }
833
834 int
835 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
836 {
837     int count;
838     device_t child;
839     device_t *list;
840     
841     count = 0;
842     for (child = TAILQ_FIRST(&dev->children); child;
843          child = TAILQ_NEXT(child, link))
844         count++;
845
846     list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT);
847     if (!list)
848         return ENOMEM;
849     bzero(list, count * sizeof(device_t));
850
851     count = 0;
852     for (child = TAILQ_FIRST(&dev->children); child;
853          child = TAILQ_NEXT(child, link)) {
854         list[count] = child;
855         count++;
856     }
857
858     *devlistp = list;
859     *devcountp = count;
860
861     return 0;
862 }
863
864 driver_t *
865 device_get_driver(device_t dev)
866 {
867     return dev->driver;
868 }
869
870 devclass_t
871 device_get_devclass(device_t dev)
872 {
873     return dev->devclass;
874 }
875
876 const char *
877 device_get_name(device_t dev)
878 {
879     if (dev->devclass)
880         return devclass_get_name(dev->devclass);
881     return NULL;
882 }
883
884 const char *
885 device_get_nameunit(device_t dev)
886 {
887     return dev->nameunit;
888 }
889
890 int
891 device_get_unit(device_t dev)
892 {
893     return dev->unit;
894 }
895
896 const char *
897 device_get_desc(device_t dev)
898 {
899     return dev->desc;
900 }
901
902 u_int32_t
903 device_get_flags(device_t dev)
904 {
905     return dev->devflags;
906 }
907
908 int
909 device_print_prettyname(device_t dev)
910 {
911     const char *name = device_get_name(dev);
912
913     if (name == 0)
914         return printf("unknown: ");
915     else
916         return printf("%s%d: ", name, device_get_unit(dev));
917 }
918
919 int
920 device_printf(device_t dev, const char * fmt, ...)
921 {
922     va_list ap;
923     int retval;
924
925     retval = device_print_prettyname(dev);
926     va_start(ap, fmt);
927     retval += vprintf(fmt, ap);
928     va_end(ap);
929     return retval;
930 }
931
932 static void
933 device_set_desc_internal(device_t dev, const char* desc, int copy)
934 {
935     if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
936         free(dev->desc, M_BUS);
937         dev->flags &= ~DF_DESCMALLOCED;
938         dev->desc = NULL;
939     }
940
941     if (copy && desc) {
942         dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
943         if (dev->desc) {
944             strcpy(dev->desc, desc);
945             dev->flags |= DF_DESCMALLOCED;
946         }
947     } else
948         /* Avoid a -Wcast-qual warning */
949         dev->desc = (char *)(uintptr_t) desc;
950
951 #ifdef DEVICE_SYSCTLS
952     {
953         struct sysctl_oid *oid = &dev->oid[1];
954         oid->oid_arg1 = dev->desc ? dev->desc : "";
955         oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
956     }
957 #endif
958 }
959
960 void
961 device_set_desc(device_t dev, const char* desc)
962 {
963     device_set_desc_internal(dev, desc, FALSE);
964 }
965
966 void
967 device_set_desc_copy(device_t dev, const char* desc)
968 {
969     device_set_desc_internal(dev, desc, TRUE);
970 }
971
972 void
973 device_set_flags(device_t dev, u_int32_t flags)
974 {
975     dev->devflags = flags;
976 }
977
978 void *
979 device_get_softc(device_t dev)
980 {
981     return dev->softc;
982 }
983
984 void
985 device_set_softc(device_t dev, void *softc)
986 {
987     if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
988         free(dev->softc, M_BUS);
989     dev->softc = softc;
990     if (dev->softc)
991         dev->flags |= DF_EXTERNALSOFTC;
992     else
993         dev->flags &= ~DF_EXTERNALSOFTC;
994 }
995
996 void *
997 device_get_ivars(device_t dev)
998 {
999     return dev->ivars;
1000 }
1001
1002 void
1003 device_set_ivars(device_t dev, void * ivars)
1004 {
1005     if (!dev)
1006         return;
1007
1008     dev->ivars = ivars;
1009
1010     return;
1011 }
1012
1013 device_state_t
1014 device_get_state(device_t dev)
1015 {
1016     return dev->state;
1017 }
1018
1019 void
1020 device_enable(device_t dev)
1021 {
1022     dev->flags |= DF_ENABLED;
1023 }
1024
1025 void
1026 device_disable(device_t dev)
1027 {
1028     dev->flags &= ~DF_ENABLED;
1029 }
1030
1031 void
1032 device_busy(device_t dev)
1033 {
1034     if (dev->state < DS_ATTACHED)
1035         panic("device_busy: called for unattached device");
1036     if (dev->busy == 0 && dev->parent)
1037         device_busy(dev->parent);
1038     dev->busy++;
1039     dev->state = DS_BUSY;
1040 }
1041
1042 void
1043 device_unbusy(device_t dev)
1044 {
1045     if (dev->state != DS_BUSY)
1046         panic("device_unbusy: called for non-busy device");
1047     dev->busy--;
1048     if (dev->busy == 0) {
1049         if (dev->parent)
1050             device_unbusy(dev->parent);
1051         dev->state = DS_ATTACHED;
1052     }
1053 }
1054
1055 void
1056 device_quiet(device_t dev)
1057 {
1058     dev->flags |= DF_QUIET;
1059 }
1060
1061 void
1062 device_verbose(device_t dev)
1063 {
1064     dev->flags &= ~DF_QUIET;
1065 }
1066
1067 int
1068 device_is_quiet(device_t dev)
1069 {
1070     return (dev->flags & DF_QUIET) != 0;
1071 }
1072
1073 int
1074 device_is_enabled(device_t dev)
1075 {
1076     return (dev->flags & DF_ENABLED) != 0;
1077 }
1078
1079 int
1080 device_is_alive(device_t dev)
1081 {
1082     return dev->state >= DS_ALIVE;
1083 }
1084
1085 int
1086 device_set_devclass(device_t dev, const char *classname)
1087 {
1088     devclass_t dc;
1089
1090     if (!classname) {
1091         if (dev->devclass)
1092             devclass_delete_device(dev->devclass, dev);
1093         return 0;
1094     }
1095
1096     if (dev->devclass) {
1097         printf("device_set_devclass: device class already set\n");
1098         return EINVAL;
1099     }
1100
1101     dc = devclass_find_internal(classname, TRUE);
1102     if (!dc)
1103         return ENOMEM;
1104
1105     return devclass_add_device(dc, dev);
1106 }
1107
1108 int
1109 device_set_driver(device_t dev, driver_t *driver)
1110 {
1111     if (dev->state >= DS_ATTACHED)
1112         return EBUSY;
1113
1114     if (dev->driver == driver)
1115         return 0;
1116
1117     if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1118         free(dev->softc, M_BUS);
1119         dev->softc = NULL;
1120     }
1121     dev->ops = &null_ops;
1122     dev->driver = driver;
1123     if (driver) {
1124         dev->ops = driver->ops;
1125         if (!(dev->flags & DF_EXTERNALSOFTC)) {
1126             dev->softc = malloc(driver->softc, M_BUS, M_NOWAIT);
1127             if (!dev->softc) {
1128                 dev->ops = &null_ops;
1129                 dev->driver = NULL;
1130                 return ENOMEM;
1131             }
1132             bzero(dev->softc, driver->softc);
1133         }
1134     }
1135     return 0;
1136 }
1137
1138 int
1139 device_probe_and_attach(device_t dev)
1140 {
1141     device_t bus = dev->parent;
1142     int error = 0;
1143     int hasclass = (dev->devclass != 0);
1144
1145     if (dev->state >= DS_ALIVE)
1146         return 0;
1147
1148     if (dev->flags & DF_ENABLED) {
1149         error = device_probe_child(bus, dev);
1150         if (!error) {
1151             if (!device_is_quiet(dev))
1152                 device_print_child(bus, dev);
1153             error = DEVICE_ATTACH(dev);
1154             if (!error)
1155                 dev->state = DS_ATTACHED;
1156             else {
1157                 printf("device_probe_and_attach: %s%d attach returned %d\n",
1158                        dev->driver->name, dev->unit, error);
1159                 /* Unset the class that was set in device_probe_child */
1160                 if (!hasclass)
1161                     device_set_devclass(dev, 0);
1162                 device_set_driver(dev, NULL);
1163                 dev->state = DS_NOTPRESENT;
1164             }
1165         } else {
1166             if (!(dev->flags & DF_DONENOMATCH)) {
1167                 BUS_PROBE_NOMATCH(bus, dev);
1168                 dev->flags |= DF_DONENOMATCH;
1169             }
1170         }
1171     } else {
1172         if (bootverbose) {
1173             device_print_prettyname(dev);
1174             printf("not probed (disabled)\n");
1175         }
1176     }
1177
1178     return error;
1179 }
1180
1181 int
1182 device_detach(device_t dev)
1183 {
1184     int error;
1185
1186     PDEBUG(("%s", DEVICENAME(dev)));
1187     if (dev->state == DS_BUSY)
1188         return EBUSY;
1189     if (dev->state != DS_ATTACHED)
1190         return 0;
1191
1192     if ((error = DEVICE_DETACH(dev)) != 0)
1193         return error;
1194     device_printf(dev, "detached\n");
1195     if (dev->parent)
1196         BUS_CHILD_DETACHED(dev->parent, dev);
1197
1198     if (!(dev->flags & DF_FIXEDCLASS))
1199         devclass_delete_device(dev->devclass, dev);
1200
1201     dev->state = DS_NOTPRESENT;
1202     device_set_driver(dev, NULL);
1203
1204     return 0;
1205 }
1206
1207 int
1208 device_shutdown(device_t dev)
1209 {
1210     if (dev->state < DS_ATTACHED)
1211         return 0;
1212     return DEVICE_SHUTDOWN(dev);
1213 }
1214
1215 int
1216 device_set_unit(device_t dev, int unit)
1217 {
1218     devclass_t dc;
1219     int err;
1220
1221     dc = device_get_devclass(dev);
1222     if (unit < dc->maxunit && dc->devices[unit])
1223         return EBUSY;
1224     err = devclass_delete_device(dc, dev);
1225     if (err)
1226         return err;
1227     dev->unit = unit;
1228     err = devclass_add_device(dc, dev);
1229     if (err)
1230         return err;
1231     return 0;
1232 }
1233
1234 #ifdef DEVICE_SYSCTLS
1235
1236 /*
1237  * Sysctl nodes for devices.
1238  */
1239
1240 SYSCTL_NODE(_hw, OID_AUTO, devices, CTLFLAG_RW, 0, "A list of all devices");
1241
1242 static int
1243 sysctl_handle_children(SYSCTL_HANDLER_ARGS)
1244 {
1245     device_t dev = arg1;
1246     device_t child;
1247     int first = 1, error = 0;
1248
1249     for (child = TAILQ_FIRST(&dev->children); child;
1250          child = TAILQ_NEXT(child, link)) {
1251         if (child->nameunit) {
1252             if (!first) {
1253                 error = SYSCTL_OUT(req, ",", 1);
1254                 if (error) return error;
1255             } else {
1256                 first = 0;
1257             }
1258             error = SYSCTL_OUT(req, child->nameunit, strlen(child->nameunit));
1259             if (error) return error;
1260         }
1261     }
1262
1263     error = SYSCTL_OUT(req, "", 1);
1264
1265     return error;
1266 }
1267
1268 static int
1269 sysctl_handle_state(SYSCTL_HANDLER_ARGS)
1270 {
1271     device_t dev = arg1;
1272
1273     switch (dev->state) {
1274     case DS_NOTPRESENT:
1275         return SYSCTL_OUT(req, "notpresent", sizeof("notpresent"));
1276     case DS_ALIVE:
1277         return SYSCTL_OUT(req, "alive", sizeof("alive"));
1278     case DS_ATTACHED:
1279         return SYSCTL_OUT(req, "attached", sizeof("attached"));
1280     case DS_BUSY:
1281         return SYSCTL_OUT(req, "busy", sizeof("busy"));
1282     }
1283
1284     return 0;
1285 }
1286
1287 static void
1288 device_register_oids(device_t dev)
1289 {
1290     struct sysctl_oid* oid;
1291
1292     oid = &dev->oid[0];
1293     bzero(oid, sizeof(*oid));
1294     oid->oid_parent = &sysctl__hw_devices_children;
1295     oid->oid_number = OID_AUTO;
1296     oid->oid_kind = CTLTYPE_NODE | CTLFLAG_RW;
1297     oid->oid_arg1 = &dev->oidlist[0];
1298     oid->oid_arg2 = 0;
1299     oid->oid_name = dev->nameunit;
1300     oid->oid_handler = 0;
1301     oid->oid_fmt = "N";
1302     SLIST_INIT(&dev->oidlist[0]);
1303     sysctl_register_oid(oid);
1304
1305     oid = &dev->oid[1];
1306     bzero(oid, sizeof(*oid));
1307     oid->oid_parent = &dev->oidlist[0];
1308     oid->oid_number = OID_AUTO;
1309     oid->oid_kind = CTLTYPE_STRING | CTLFLAG_RD;
1310     oid->oid_arg1 = dev->desc ? dev->desc : "";
1311     oid->oid_arg2 = dev->desc ? strlen(dev->desc) : 0;
1312     oid->oid_name = "desc";
1313     oid->oid_handler = sysctl_handle_string;
1314     oid->oid_fmt = "A";
1315     sysctl_register_oid(oid);
1316
1317     oid = &dev->oid[2];
1318     bzero(oid, sizeof(*oid));
1319     oid->oid_parent = &dev->oidlist[0];
1320     oid->oid_number = OID_AUTO;
1321     oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1322     oid->oid_arg1 = dev;
1323     oid->oid_arg2 = 0;
1324     oid->oid_name = "children";
1325     oid->oid_handler = sysctl_handle_children;
1326     oid->oid_fmt = "A";
1327     sysctl_register_oid(oid);
1328
1329     oid = &dev->oid[3];
1330     bzero(oid, sizeof(*oid));
1331     oid->oid_parent = &dev->oidlist[0];
1332     oid->oid_number = OID_AUTO;
1333     oid->oid_kind = CTLTYPE_INT | CTLFLAG_RD;
1334     oid->oid_arg1 = dev;
1335     oid->oid_arg2 = 0;
1336     oid->oid_name = "state";
1337     oid->oid_handler = sysctl_handle_state;
1338     oid->oid_fmt = "A";
1339     sysctl_register_oid(oid);
1340 }
1341
1342 static void
1343 device_unregister_oids(device_t dev)
1344 {
1345     sysctl_unregister_oid(&dev->oid[0]);
1346     sysctl_unregister_oid(&dev->oid[1]);
1347     sysctl_unregister_oid(&dev->oid[2]);
1348 }
1349
1350 #endif
1351
1352 /*======================================*/
1353 /*
1354  * Access functions for device resources.
1355  */
1356
1357 /* Supplied by config(8) in ioconf.c */
1358 extern struct config_device config_devtab[];
1359 extern int devtab_count;
1360
1361 /* Runtime version */
1362 struct config_device *devtab = config_devtab;
1363
1364 static int
1365 resource_new_name(const char *name, int unit)
1366 {
1367         struct config_device *new;
1368
1369         new = malloc((devtab_count + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
1370         if (new == NULL)
1371                 return -1;
1372         if (devtab && devtab_count > 0)
1373                 bcopy(devtab, new, devtab_count * sizeof(*new));
1374         bzero(&new[devtab_count], sizeof(*new));
1375         new[devtab_count].name = malloc(strlen(name) + 1, M_TEMP, M_NOWAIT);
1376         if (new[devtab_count].name == NULL) {
1377                 free(new, M_TEMP);
1378                 return -1;
1379         }
1380         strcpy(new[devtab_count].name, name);
1381         new[devtab_count].unit = unit;
1382         new[devtab_count].resource_count = 0;
1383         new[devtab_count].resources = NULL;
1384         devtab = new;
1385         return devtab_count++;
1386 }
1387
1388 static int
1389 resource_new_resname(int j, const char *resname, resource_type type)
1390 {
1391         struct config_resource *new;
1392         int i;
1393
1394         i = devtab[j].resource_count;
1395         new = malloc((i + 1) * sizeof(*new), M_TEMP, M_NOWAIT);
1396         if (new == NULL)
1397                 return -1;
1398         if (devtab[j].resources && i > 0)
1399                 bcopy(devtab[j].resources, new, i * sizeof(*new));
1400         bzero(&new[i], sizeof(*new));
1401         new[i].name = malloc(strlen(resname) + 1, M_TEMP, M_NOWAIT);
1402         if (new[i].name == NULL) {
1403                 free(new, M_TEMP);
1404                 return -1;
1405         }
1406         strcpy(new[i].name, resname);
1407         new[i].type = type;
1408         if (devtab[j].resources)
1409                 free(devtab[j].resources, M_TEMP);
1410         devtab[j].resources = new;
1411         devtab[j].resource_count = i + 1;
1412         return i;
1413 }
1414
1415 static int
1416 resource_match_string(int i, const char *resname, const char *value)
1417 {
1418         int j;
1419         struct config_resource *res;
1420
1421         for (j = 0, res = devtab[i].resources;
1422              j < devtab[i].resource_count; j++, res++)
1423                 if (!strcmp(res->name, resname)
1424                     && res->type == RES_STRING
1425                     && !strcmp(res->u.stringval, value))
1426                         return j;
1427         return -1;
1428 }
1429
1430 static int
1431 resource_find(const char *name, int unit, const char *resname, 
1432               struct config_resource **result)
1433 {
1434         int i, j;
1435         struct config_resource *res;
1436
1437         /*
1438          * First check specific instances, then generic.
1439          */
1440         for (i = 0; i < devtab_count; i++) {
1441                 if (devtab[i].unit < 0)
1442                         continue;
1443                 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1444                         res = devtab[i].resources;
1445                         for (j = 0; j < devtab[i].resource_count; j++, res++)
1446                                 if (!strcmp(res->name, resname)) {
1447                                         *result = res;
1448                                         return 0;
1449                                 }
1450                 }
1451         }
1452         for (i = 0; i < devtab_count; i++) {
1453                 if (devtab[i].unit >= 0)
1454                         continue;
1455                 /* XXX should this `&& devtab[i].unit == unit' be here? */
1456                 /* XXX if so, then the generic match does nothing */
1457                 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1458                         res = devtab[i].resources;
1459                         for (j = 0; j < devtab[i].resource_count; j++, res++)
1460                                 if (!strcmp(res->name, resname)) {
1461                                         *result = res;
1462                                         return 0;
1463                                 }
1464                 }
1465         }
1466         return ENOENT;
1467 }
1468
1469 int
1470 resource_int_value(const char *name, int unit, const char *resname, int *result)
1471 {
1472         int error;
1473         struct config_resource *res;
1474
1475         if ((error = resource_find(name, unit, resname, &res)) != 0)
1476                 return error;
1477         if (res->type != RES_INT)
1478                 return EFTYPE;
1479         *result = res->u.intval;
1480         return 0;
1481 }
1482
1483 int
1484 resource_long_value(const char *name, int unit, const char *resname,
1485                     long *result)
1486 {
1487         int error;
1488         struct config_resource *res;
1489
1490         if ((error = resource_find(name, unit, resname, &res)) != 0)
1491                 return error;
1492         if (res->type != RES_LONG)
1493                 return EFTYPE;
1494         *result = res->u.longval;
1495         return 0;
1496 }
1497
1498 int
1499 resource_string_value(const char *name, int unit, const char *resname,
1500                       char **result)
1501 {
1502         int error;
1503         struct config_resource *res;
1504
1505         if ((error = resource_find(name, unit, resname, &res)) != 0)
1506                 return error;
1507         if (res->type != RES_STRING)
1508                 return EFTYPE;
1509         *result = res->u.stringval;
1510         return 0;
1511 }
1512
1513 int
1514 resource_query_string(int i, const char *resname, const char *value)
1515 {
1516         if (i < 0)
1517                 i = 0;
1518         else
1519                 i = i + 1;
1520         for (; i < devtab_count; i++)
1521                 if (resource_match_string(i, resname, value) >= 0)
1522                         return i;
1523         return -1;
1524 }
1525
1526 int
1527 resource_locate(int i, const char *resname)
1528 {
1529         if (i < 0)
1530                 i = 0;
1531         else
1532                 i = i + 1;
1533         for (; i < devtab_count; i++)
1534                 if (!strcmp(devtab[i].name, resname))
1535                         return i;
1536         return -1;
1537 }
1538
1539 int
1540 resource_count(void)
1541 {
1542         return devtab_count;
1543 }
1544
1545 char *
1546 resource_query_name(int i)
1547 {
1548         return devtab[i].name;
1549 }
1550
1551 int
1552 resource_query_unit(int i)
1553 {
1554         return devtab[i].unit;
1555 }
1556
1557 static int
1558 resource_create(const char *name, int unit, const char *resname,
1559                 resource_type type, struct config_resource **result)
1560 {
1561         int i, j;
1562         struct config_resource *res = NULL;
1563
1564         for (i = 0; i < devtab_count; i++) {
1565                 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1566                         res = devtab[i].resources;
1567                         break;
1568                 }
1569         }
1570         if (res == NULL) {
1571                 i = resource_new_name(name, unit);
1572                 if (i < 0)
1573                         return ENOMEM;
1574                 res = devtab[i].resources;
1575         }
1576         for (j = 0; j < devtab[i].resource_count; j++, res++) {
1577                 if (!strcmp(res->name, resname)) {
1578                         *result = res;
1579                         return 0;
1580                 }
1581         }
1582         j = resource_new_resname(i, resname, type);
1583         if (j < 0)
1584                 return ENOMEM;
1585         res = &devtab[i].resources[j];
1586         *result = res;
1587         return 0;
1588 }
1589
1590 int
1591 resource_set_int(const char *name, int unit, const char *resname, int value)
1592 {
1593         int error;
1594         struct config_resource *res;
1595
1596         error = resource_create(name, unit, resname, RES_INT, &res);
1597         if (error)
1598                 return error;
1599         if (res->type != RES_INT)
1600                 return EFTYPE;
1601         res->u.intval = value;
1602         return 0;
1603 }
1604
1605 int
1606 resource_set_long(const char *name, int unit, const char *resname, long value)
1607 {
1608         int error;
1609         struct config_resource *res;
1610
1611         error = resource_create(name, unit, resname, RES_LONG, &res);
1612         if (error)
1613                 return error;
1614         if (res->type != RES_LONG)
1615                 return EFTYPE;
1616         res->u.longval = value;
1617         return 0;
1618 }
1619
1620 int
1621 resource_set_string(const char *name, int unit, const char *resname,
1622                     const char *value)
1623 {
1624         int error;
1625         struct config_resource *res;
1626
1627         error = resource_create(name, unit, resname, RES_STRING, &res);
1628         if (error)
1629                 return error;
1630         if (res->type != RES_STRING)
1631                 return EFTYPE;
1632         if (res->u.stringval)
1633                 free(res->u.stringval, M_TEMP);
1634         res->u.stringval = malloc(strlen(value) + 1, M_TEMP, M_NOWAIT);
1635         if (res->u.stringval == NULL)
1636                 return ENOMEM;
1637         strcpy(res->u.stringval, value);
1638         return 0;
1639 }
1640
1641
1642 static void
1643 resource_cfgload(void *dummy __unused)
1644 {
1645         struct config_resource *res, *cfgres;
1646         int i, j;
1647         int error;
1648         char *name, *resname;
1649         int unit;
1650         resource_type type;
1651         char *stringval;
1652         int config_devtab_count;
1653
1654         config_devtab_count = devtab_count;
1655         devtab = NULL;
1656         devtab_count = 0;
1657
1658         for (i = 0; i < config_devtab_count; i++) {
1659                 name = config_devtab[i].name;
1660                 unit = config_devtab[i].unit;
1661
1662                 for (j = 0; j < config_devtab[i].resource_count; j++) {
1663                         cfgres = config_devtab[i].resources;
1664                         resname = cfgres[j].name;
1665                         type = cfgres[j].type;
1666                         error = resource_create(name, unit, resname, type,
1667                                                 &res);
1668                         if (error) {
1669                                 printf("create resource %s%d: error %d\n",
1670                                         name, unit, error);
1671                                 continue;
1672                         }
1673                         if (res->type != type) {
1674                                 printf("type mismatch %s%d: %d != %d\n",
1675                                         name, unit, res->type, type);
1676                                 continue;
1677                         }
1678                         switch (type) {
1679                         case RES_INT:
1680                                 res->u.intval = cfgres[j].u.intval;
1681                                 break;
1682                         case RES_LONG:
1683                                 res->u.longval = cfgres[j].u.longval;
1684                                 break;
1685                         case RES_STRING:
1686                                 if (res->u.stringval)
1687                                         free(res->u.stringval, M_TEMP);
1688                                 stringval = cfgres[j].u.stringval;
1689                                 res->u.stringval = malloc(strlen(stringval) + 1,
1690                                                           M_TEMP, M_NOWAIT);
1691                                 if (res->u.stringval == NULL)
1692                                         break;
1693                                 strcpy(res->u.stringval, stringval);
1694                                 break;
1695                         default:
1696                                 panic("unknown resource type %d\n", type);
1697                         }
1698                 }
1699         }
1700 }
1701 SYSINIT(cfgload, SI_SUB_KMEM, SI_ORDER_ANY + 50, resource_cfgload, 0)
1702
1703
1704 /*======================================*/
1705 /*
1706  * Some useful method implementations to make life easier for bus drivers.
1707  */
1708
1709 void
1710 resource_list_init(struct resource_list *rl)
1711 {
1712         SLIST_INIT(rl);
1713 }
1714
1715 void
1716 resource_list_free(struct resource_list *rl)
1717 {
1718     struct resource_list_entry *rle;
1719
1720     while ((rle = SLIST_FIRST(rl)) != NULL) {
1721         if (rle->res)
1722             panic("resource_list_free: resource entry is busy");
1723         SLIST_REMOVE_HEAD(rl, link);
1724         free(rle, M_BUS);
1725     }
1726 }
1727
1728 void
1729 resource_list_add(struct resource_list *rl,
1730                   int type, int rid,
1731                   u_long start, u_long end, u_long count)
1732 {
1733     struct resource_list_entry *rle;
1734
1735     rle = resource_list_find(rl, type, rid);
1736     if (!rle) {
1737         rle = malloc(sizeof(struct resource_list_entry), M_BUS, M_NOWAIT);
1738         if (!rle)
1739             panic("resource_list_add: can't record entry");
1740         SLIST_INSERT_HEAD(rl, rle, link);
1741         rle->type = type;
1742         rle->rid = rid;
1743         rle->res = NULL;
1744     }
1745
1746     if (rle->res)
1747         panic("resource_list_add: resource entry is busy");
1748
1749     rle->start = start;
1750     rle->end = end;
1751     rle->count = count;
1752 }
1753
1754 struct resource_list_entry*
1755 resource_list_find(struct resource_list *rl,
1756                    int type, int rid)
1757 {
1758     struct resource_list_entry *rle;
1759
1760     SLIST_FOREACH(rle, rl, link)
1761         if (rle->type == type && rle->rid == rid)
1762             return rle;
1763     return NULL;
1764 }
1765
1766 void
1767 resource_list_delete(struct resource_list *rl,
1768                      int type, int rid)
1769 {
1770     struct resource_list_entry *rle = resource_list_find(rl, type, rid);
1771
1772     if (rle) {
1773         SLIST_REMOVE(rl, rle, resource_list_entry, link);
1774         free(rle, M_BUS);
1775     }
1776 }
1777
1778 struct resource *
1779 resource_list_alloc(struct resource_list *rl,
1780                     device_t bus, device_t child,
1781                     int type, int *rid,
1782                     u_long start, u_long end,
1783                     u_long count, u_int flags)
1784 {
1785     struct resource_list_entry *rle = 0;
1786     int passthrough = (device_get_parent(child) != bus);
1787     int isdefault = (start == 0UL && end == ~0UL);
1788
1789     if (passthrough) {
1790         return BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1791                                   type, rid,
1792                                   start, end, count, flags);
1793     }
1794
1795     rle = resource_list_find(rl, type, *rid);
1796
1797     if (!rle)
1798         return 0;               /* no resource of that type/rid */
1799     if (rle->res)
1800         panic("resource_list_alloc: resource entry is busy");
1801
1802     if (isdefault) {
1803         start = rle->start;
1804         count = max(count, rle->count);
1805         end = max(rle->end, start + count - 1);
1806     }
1807
1808     rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
1809                                   type, rid, start, end, count, flags);
1810
1811     /*
1812      * Record the new range.
1813      */
1814     if (rle->res) {
1815             rle->start = rman_get_start(rle->res);
1816             rle->end = rman_get_end(rle->res);
1817             rle->count = count;
1818     }
1819
1820     return rle->res;
1821 }
1822
1823 int
1824 resource_list_release(struct resource_list *rl,
1825                       device_t bus, device_t child,
1826                       int type, int rid, struct resource *res)
1827 {
1828     struct resource_list_entry *rle = 0;
1829     int passthrough = (device_get_parent(child) != bus);
1830     int error;
1831
1832     if (passthrough) {
1833         return BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1834                                     type, rid, res);
1835     }
1836
1837     rle = resource_list_find(rl, type, rid);
1838
1839     if (!rle)
1840         panic("resource_list_release: can't find resource");
1841     if (!rle->res)
1842         panic("resource_list_release: resource entry is not busy");
1843
1844     error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
1845                                  type, rid, res);
1846     if (error)
1847         return error;
1848
1849     rle->res = NULL;
1850     return 0;
1851 }
1852
1853 int
1854 resource_list_print_type(struct resource_list *rl, const char *name, int type,
1855     const char *format)
1856 {
1857         struct resource_list_entry *rle;
1858         int printed, retval;
1859
1860         printed = 0;
1861         retval = 0;
1862         /* Yes, this is kinda cheating */
1863         SLIST_FOREACH(rle, rl, link) {
1864                 if (rle->type == type) {
1865                         if (printed == 0)
1866                                 retval += printf(" %s ", name);
1867                         else
1868                                 retval += printf(",");
1869                         printed++;
1870                         retval += printf(format, rle->start);
1871                         if (rle->count > 1) {
1872                                 retval += printf("-");
1873                                 retval += printf(format, rle->start +
1874                                                  rle->count - 1);
1875                         }
1876                 }
1877         }
1878         return (retval);
1879 }
1880
1881 /*
1882  * Call DEVICE_IDENTIFY for each driver.
1883  */
1884 int
1885 bus_generic_probe(device_t dev)
1886 {
1887     devclass_t dc = dev->devclass;
1888     driverlink_t dl;
1889
1890     for (dl = TAILQ_FIRST(&dc->drivers); dl; dl = TAILQ_NEXT(dl, link))
1891         DEVICE_IDENTIFY(dl->driver, dev);
1892
1893     return 0;
1894 }
1895
1896 int
1897 bus_generic_attach(device_t dev)
1898 {
1899     device_t child;
1900
1901     for (child = TAILQ_FIRST(&dev->children);
1902          child; child = TAILQ_NEXT(child, link))
1903         device_probe_and_attach(child);
1904
1905     return 0;
1906 }
1907
1908 int
1909 bus_generic_detach(device_t dev)
1910 {
1911     device_t child;
1912     int error;
1913
1914     if (dev->state != DS_ATTACHED)
1915         return EBUSY;
1916
1917     for (child = TAILQ_FIRST(&dev->children);
1918          child; child = TAILQ_NEXT(child, link))
1919         if ((error = device_detach(child)) != 0)
1920             return error;
1921
1922     return 0;
1923 }
1924
1925 int
1926 bus_generic_shutdown(device_t dev)
1927 {
1928     device_t child;
1929
1930     for (child = TAILQ_FIRST(&dev->children);
1931          child; child = TAILQ_NEXT(child, link))
1932         device_shutdown(child);
1933
1934     return 0;
1935 }
1936
1937 int
1938 bus_generic_suspend(device_t dev)
1939 {
1940         int             error;
1941         device_t        child, child2;
1942
1943         for (child = TAILQ_FIRST(&dev->children);
1944              child; child = TAILQ_NEXT(child, link)) {
1945                 error = DEVICE_SUSPEND(child);
1946                 if (error) {
1947                         for (child2 = TAILQ_FIRST(&dev->children);
1948                              child2 && child2 != child; 
1949                              child2 = TAILQ_NEXT(child2, link))
1950                                 DEVICE_RESUME(child2);
1951                         return (error);
1952                 }
1953         }
1954         return 0;
1955 }
1956
1957 int
1958 bus_generic_resume(device_t dev)
1959 {
1960         device_t        child;
1961
1962         for (child = TAILQ_FIRST(&dev->children);
1963              child; child = TAILQ_NEXT(child, link)) {
1964                 DEVICE_RESUME(child);
1965                 /* if resume fails, there's nothing we can usefully do... */
1966         }
1967         return 0;
1968 }
1969
1970 int
1971 bus_print_child_header (device_t dev, device_t child)
1972 {
1973         int     retval = 0;
1974
1975         if (device_get_desc(child)) { 
1976                 retval += device_printf(child, "<%s>",
1977                                        device_get_desc(child));      
1978         } else {
1979                 retval += printf("%s", device_get_nameunit(child));
1980         }
1981
1982         return (retval);
1983 }
1984
1985 int
1986 bus_print_child_footer (device_t dev, device_t child)
1987 {
1988         return(printf(" on %s\n", device_get_nameunit(dev)));
1989 }
1990
1991 int
1992 bus_generic_print_child(device_t dev, device_t child)
1993 {
1994         int     retval = 0;
1995
1996         retval += bus_print_child_header(dev, child);
1997         retval += bus_print_child_footer(dev, child);
1998
1999         return (retval);
2000 }
2001
2002 int
2003 bus_generic_read_ivar(device_t dev, device_t child, int index, 
2004                       uintptr_t * result)
2005 {
2006     return ENOENT;
2007 }
2008
2009 int
2010 bus_generic_write_ivar(device_t dev, device_t child, int index, 
2011                        uintptr_t value)
2012 {
2013     return ENOENT;
2014 }
2015
2016 void
2017 bus_generic_driver_added(device_t dev, driver_t *driver)
2018 {
2019     device_t child;
2020
2021     DEVICE_IDENTIFY(driver, dev);
2022     for (child = TAILQ_FIRST(&dev->children);
2023          child; child = TAILQ_NEXT(child, link))
2024         if (child->state == DS_NOTPRESENT)
2025             device_probe_and_attach(child);
2026 }
2027
2028 int
2029 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq, 
2030                        int flags, driver_intr_t *intr, void *arg,
2031                        void **cookiep)
2032 {
2033         /* Propagate up the bus hierarchy until someone handles it. */
2034         if (dev->parent)
2035                 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
2036                                        intr, arg, cookiep));
2037         else
2038                 return (EINVAL);
2039 }
2040
2041 int
2042 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2043                           void *cookie)
2044 {
2045         /* Propagate up the bus hierarchy until someone handles it. */
2046         if (dev->parent)
2047                 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2048         else
2049                 return (EINVAL);
2050 }
2051
2052 struct resource *
2053 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2054                            u_long start, u_long end, u_long count, u_int flags)
2055 {
2056         /* Propagate up the bus hierarchy until someone handles it. */
2057         if (dev->parent)
2058                 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 
2059                                            start, end, count, flags));
2060         else
2061                 return (NULL);
2062 }
2063
2064 int
2065 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2066                              struct resource *r)
2067 {
2068         /* Propagate up the bus hierarchy until someone handles it. */
2069         if (dev->parent)
2070                 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, 
2071                                              r));
2072         else
2073                 return (EINVAL);
2074 }
2075
2076 int
2077 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2078                               struct resource *r)
2079 {
2080         /* Propagate up the bus hierarchy until someone handles it. */
2081         if (dev->parent)
2082                 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, 
2083                                               r));
2084         else
2085                 return (EINVAL);
2086 }
2087
2088 int
2089 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2090                                 int rid, struct resource *r)
2091 {
2092         /* Propagate up the bus hierarchy until someone handles it. */
2093         if (dev->parent)
2094                 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2095                                                 r));
2096         else
2097                 return (EINVAL);
2098 }
2099
2100 /*
2101  * Some convenience functions to make it easier for drivers to use the
2102  * resource-management functions.  All these really do is hide the
2103  * indirection through the parent's method table, making for slightly
2104  * less-wordy code.  In the future, it might make sense for this code
2105  * to maintain some sort of a list of resources allocated by each device.
2106  */
2107 struct resource *
2108 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2109                    u_long count, u_int flags)
2110 {
2111         if (dev->parent == 0)
2112                 return (0);
2113         return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2114                                    count, flags));
2115 }
2116
2117 int
2118 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2119 {
2120         if (dev->parent == 0)
2121                 return (EINVAL);
2122         return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2123 }
2124
2125 int
2126 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2127 {
2128         if (dev->parent == 0)
2129                 return (EINVAL);
2130         return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2131 }
2132
2133 int
2134 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2135 {
2136         if (dev->parent == 0)
2137                 return (EINVAL);
2138         return (BUS_RELEASE_RESOURCE(dev->parent, dev,
2139                                      type, rid, r));
2140 }
2141
2142 int
2143 bus_setup_intr(device_t dev, struct resource *r, int flags,
2144                driver_intr_t handler, void *arg, void **cookiep)
2145 {
2146         if (dev->parent == 0)
2147                 return (EINVAL);
2148         return (BUS_SETUP_INTR(dev->parent, dev, r, flags,
2149                                handler, arg, cookiep));
2150 }
2151
2152 int
2153 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2154 {
2155         if (dev->parent == 0)
2156                 return (EINVAL);
2157         return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2158 }
2159
2160 int
2161 bus_set_resource(device_t dev, int type, int rid,
2162                  u_long start, u_long count)
2163 {
2164         return BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2165                                 start, count);
2166 }
2167
2168 int
2169 bus_get_resource(device_t dev, int type, int rid,
2170                  u_long *startp, u_long *countp)
2171 {
2172         return BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2173                                 startp, countp);
2174 }
2175
2176 u_long
2177 bus_get_resource_start(device_t dev, int type, int rid)
2178 {
2179         u_long start, count;
2180         int error;
2181
2182         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2183                                  &start, &count);
2184         if (error)
2185                 return 0;
2186         return start;
2187 }
2188
2189 u_long
2190 bus_get_resource_count(device_t dev, int type, int rid)
2191 {
2192         u_long start, count;
2193         int error;
2194
2195         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2196                                  &start, &count);
2197         if (error)
2198                 return 0;
2199         return count;
2200 }
2201
2202 void
2203 bus_delete_resource(device_t dev, int type, int rid)
2204 {
2205         BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2206 }
2207
2208 static int
2209 root_print_child(device_t dev, device_t child)
2210 {
2211         return (0);
2212 }
2213
2214 static int
2215 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
2216                 void **cookiep)
2217 {
2218         /*
2219          * If an interrupt mapping gets to here something bad has happened.
2220          */
2221         panic("root_setup_intr");
2222 }
2223
2224 static device_method_t root_methods[] = {
2225         /* Device interface */
2226         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
2227         DEVMETHOD(device_suspend,       bus_generic_suspend),
2228         DEVMETHOD(device_resume,        bus_generic_resume),
2229
2230         /* Bus interface */
2231         DEVMETHOD(bus_print_child,      root_print_child),
2232         DEVMETHOD(bus_read_ivar,        bus_generic_read_ivar),
2233         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
2234         DEVMETHOD(bus_setup_intr,       root_setup_intr),
2235
2236         { 0, 0 }
2237 };
2238
2239 static driver_t root_driver = {
2240         "root",
2241         root_methods,
2242         1,                      /* no softc */
2243 };
2244
2245 device_t        root_bus;
2246 devclass_t      root_devclass;
2247
2248 static int
2249 root_bus_module_handler(module_t mod, int what, void* arg)
2250 {
2251     switch (what) {
2252     case MOD_LOAD:
2253         compile_methods(&root_driver);
2254         root_bus = make_device(NULL, "root", 0);
2255         root_bus->desc = "System root bus";
2256         root_bus->ops = root_driver.ops;
2257         root_bus->driver = &root_driver;
2258         root_bus->state = DS_ATTACHED;
2259         root_devclass = devclass_find_internal("root", FALSE);
2260         return 0;
2261
2262     case MOD_SHUTDOWN:
2263         device_shutdown(root_bus);
2264         return 0;
2265     }
2266
2267     return 0;
2268 }
2269
2270 static moduledata_t root_bus_mod = {
2271         "rootbus",
2272         root_bus_module_handler,
2273         0
2274 };
2275 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
2276
2277 void
2278 root_bus_configure(void)
2279 {
2280     device_t dev;
2281
2282     PDEBUG(("."));
2283
2284     for (dev = TAILQ_FIRST(&root_bus->children); dev;
2285          dev = TAILQ_NEXT(dev, link)) {
2286         device_probe_and_attach(dev);
2287     }
2288 }
2289
2290 int
2291 driver_module_handler(module_t mod, int what, void *arg)
2292 {
2293         int error, i;
2294         struct driver_module_data *dmd;
2295         devclass_t bus_devclass;
2296
2297         dmd = (struct driver_module_data *)arg;
2298         bus_devclass = devclass_find_internal(dmd->dmd_busname, TRUE);
2299         error = 0;
2300
2301         switch (what) {
2302         case MOD_LOAD:
2303                 if (dmd->dmd_chainevh)
2304                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2305
2306                 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2307                         PDEBUG(("Loading module: driver %s on bus %s",
2308                                 DRIVERNAME(dmd->dmd_drivers[i]), 
2309                                 dmd->dmd_busname));
2310                         error = devclass_add_driver(bus_devclass,
2311                                                     dmd->dmd_drivers[i]);
2312                 }
2313                 if (error)
2314                         break;
2315
2316                 /*
2317                  * The drivers loaded in this way are assumed to all
2318                  * implement the same devclass.
2319                  */
2320                 *dmd->dmd_devclass =
2321                         devclass_find_internal(dmd->dmd_drivers[0]->name,
2322                                                TRUE);
2323                 break;
2324
2325         case MOD_UNLOAD:
2326                 for (i = 0; !error && i < dmd->dmd_ndrivers; i++) {
2327                         PDEBUG(("Unloading module: driver %s from bus %s",
2328                                 DRIVERNAME(dmd->dmd_drivers[i]), 
2329                                 dmd->dmd_busname));
2330                         error = devclass_delete_driver(bus_devclass,
2331                                                        dmd->dmd_drivers[i]);
2332                 }
2333
2334                 if (!error && dmd->dmd_chainevh)
2335                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
2336                 break;
2337         }
2338
2339         return (error);
2340 }
2341
2342 #ifdef BUS_DEBUG
2343
2344 /* the _short versions avoid iteration by not calling anything that prints
2345  * more than oneliners. I love oneliners.
2346  */
2347
2348 static void
2349 print_method_list(device_method_t *m, int indent)
2350 {
2351         int i;
2352
2353         if (!m)
2354                 return;
2355
2356         for (i = 0; m->desc; i++, m++)
2357                 indentprintf(("method %d: %s, offset=%d\n",
2358                         i, m->desc->name, m->desc->offset));
2359 }
2360
2361 static void
2362 print_device_ops(device_ops_t ops, int indent)
2363 {
2364         int i;
2365         int count = 0;
2366
2367         if (!ops)
2368                 return;
2369
2370         /* we present a list of the methods that are pointing to the
2371          * error_method, but ignore the 0'th elements; it is always
2372          * error_method.
2373          */
2374         for (i = 1; i < ops->maxoffset; i++) {
2375                 if (ops->methods[i] == error_method) {
2376                         if (count == 0)
2377                                 indentprintf(("error_method:"));
2378                         printf(" %d", i);
2379                         count++;
2380                 }
2381         }
2382         if (count)
2383                 printf("\n");
2384
2385         indentprintf(("(%d method%s, %d valid, %d error_method%s)\n",
2386                 ops->maxoffset-1, (ops->maxoffset-1 == 1? "":"s"),
2387                 ops->maxoffset-1-count,
2388                 count, (count == 1? "":"'s")));
2389 }
2390
2391 static void
2392 print_device_short(device_t dev, int indent)
2393 {
2394         if (!dev)
2395                 return;
2396
2397         indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
2398                 dev->unit, dev->desc,
2399                 (dev->parent? "":"no "),
2400                 (TAILQ_EMPTY(&dev->children)? "no ":""),
2401                 (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
2402                 (dev->flags&DF_FIXEDCLASS? "fixed,":""),
2403                 (dev->flags&DF_WILDCARD? "wildcard,":""),
2404                 (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
2405                 (dev->ivars? "":"no "),
2406                 (dev->softc? "":"no "),
2407                 dev->busy));
2408 }
2409
2410 static void
2411 print_device(device_t dev, int indent)
2412 {
2413         if (!dev)
2414                 return;
2415
2416         print_device_short(dev, indent);
2417
2418         indentprintf(("Parent:\n"));
2419         print_device_short(dev->parent, indent+1);
2420         indentprintf(("Methods:\n"));
2421         print_device_ops(dev->ops, indent+1);
2422         indentprintf(("Driver:\n"));
2423         print_driver_short(dev->driver, indent+1);
2424         indentprintf(("Devclass:\n"));
2425         print_devclass_short(dev->devclass, indent+1);
2426 }
2427
2428 void
2429 print_device_tree_short(device_t dev, int indent)
2430 /* print the device and all its children (indented) */
2431 {
2432         device_t child;
2433
2434         if (!dev)
2435                 return;
2436
2437         print_device_short(dev, indent);
2438
2439         for (child = TAILQ_FIRST(&dev->children); child;
2440                  child = TAILQ_NEXT(child, link))
2441                 print_device_tree_short(child, indent+1);
2442 }
2443
2444 void
2445 print_device_tree(device_t dev, int indent)
2446 /* print the device and all its children (indented) */
2447 {
2448         device_t child;
2449
2450         if (!dev)
2451                 return;
2452
2453         print_device(dev, indent);
2454
2455         for (child = TAILQ_FIRST(&dev->children); child;
2456                  child = TAILQ_NEXT(child, link))
2457                 print_device_tree(child, indent+1);
2458 }
2459
2460 static void
2461 print_driver_short(driver_t *driver, int indent)
2462 {
2463         if (!driver)
2464                 return;
2465
2466         indentprintf(("driver %s: softc size = %d\n",
2467                 driver->name, driver->softc));
2468 }
2469
2470 static void
2471 print_driver(driver_t *driver, int indent)
2472 {
2473         if (!driver)
2474                 return;
2475
2476         print_driver_short(driver, indent);
2477         indentprintf(("Methods:\n"));
2478         print_method_list(driver->methods, indent+1);
2479         indentprintf(("Operations:\n"));
2480         print_device_ops(driver->ops, indent+1);
2481 }
2482
2483
2484 static void
2485 print_driver_list(driver_list_t drivers, int indent)
2486 {
2487         driverlink_t driver;
2488
2489         for (driver = TAILQ_FIRST(&drivers); driver;
2490              driver = TAILQ_NEXT(driver, link))
2491                 print_driver(driver->driver, indent);
2492 }
2493
2494 static void
2495 print_devclass_short(devclass_t dc, int indent)
2496 {
2497         if ( !dc )
2498                 return;
2499
2500         indentprintf(("devclass %s: max units = %d\n",
2501                 dc->name, dc->maxunit));
2502 }
2503
2504 static void
2505 print_devclass(devclass_t dc, int indent)
2506 {
2507         int i;
2508
2509         if ( !dc )
2510                 return;
2511
2512         print_devclass_short(dc, indent);
2513         indentprintf(("Drivers:\n"));
2514         print_driver_list(dc->drivers, indent+1);
2515
2516         indentprintf(("Devices:\n"));
2517         for (i = 0; i < dc->maxunit; i++)
2518                 if (dc->devices[i])
2519                         print_device(dc->devices[i], indent+1);
2520 }
2521
2522 void
2523 print_devclass_list_short(void)
2524 {
2525         devclass_t dc;
2526
2527         printf("Short listing of devclasses, drivers & devices:\n");
2528         for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
2529                 print_devclass_short(dc, 0);
2530 }
2531
2532 void
2533 print_devclass_list(void)
2534 {
2535         devclass_t dc;
2536
2537         printf("Full listing of devclasses, drivers & devices:\n");
2538         for (dc = TAILQ_FIRST(&devclasses); dc; dc = TAILQ_NEXT(dc, link))
2539                 print_devclass(dc, 0);
2540 }
2541
2542 #endif