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