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