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