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