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