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