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