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