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