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