kernel - Fine-grain getnewbuf() and related vfs/bio data structures
[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  */
28
29 #include "opt_bus.h"
30
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/kobj.h>
37 #include <sys/bus_private.h>
38 #include <sys/sysctl.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 #include <sys/device.h>
43 #include <sys/lock.h>
44 #include <sys/conf.h>
45 #include <sys/uio.h>
46 #include <sys/filio.h>
47 #include <sys/event.h>
48 #include <sys/signalvar.h>
49 #include <sys/machintr.h>
50
51 #include <machine/stdarg.h>     /* for device_printf() */
52
53 #include <sys/thread2.h>
54 #include <sys/mplock2.h>
55
56 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
57
58 MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
59
60 #ifdef BUS_DEBUG
61 #define PDEBUG(a)       (kprintf("%s:%d: ", __func__, __LINE__), kprintf a, kprintf("\n"))
62 #define DEVICENAME(d)   ((d)? device_get_name(d): "no device")
63 #define DRIVERNAME(d)   ((d)? d->name : "no driver")
64 #define DEVCLANAME(d)   ((d)? d->name : "no devclass")
65
66 /* Produce the indenting, indent*2 spaces plus a '.' ahead of that to 
67  * prevent syslog from deleting initial spaces
68  */
69 #define indentprintf(p) do { int iJ; kprintf("."); for (iJ=0; iJ<indent; iJ++) kprintf("  "); kprintf p ; } while(0)
70
71 static void     print_device_short(device_t dev, int indent);
72 static void     print_device(device_t dev, int indent);
73 void            print_device_tree_short(device_t dev, int indent);
74 void            print_device_tree(device_t dev, int indent);
75 static void     print_driver_short(driver_t *driver, int indent);
76 static void     print_driver(driver_t *driver, int indent);
77 static void     print_driver_list(driver_list_t drivers, int indent);
78 static void     print_devclass_short(devclass_t dc, int indent);
79 static void     print_devclass(devclass_t dc, int indent);
80 void            print_devclass_list_short(void);
81 void            print_devclass_list(void);
82
83 #else
84 /* Make the compiler ignore the function calls */
85 #define PDEBUG(a)                       /* nop */
86 #define DEVICENAME(d)                   /* nop */
87 #define DRIVERNAME(d)                   /* nop */
88 #define DEVCLANAME(d)                   /* nop */
89
90 #define print_device_short(d,i)         /* nop */
91 #define print_device(d,i)               /* nop */
92 #define print_device_tree_short(d,i)    /* nop */
93 #define print_device_tree(d,i)          /* nop */
94 #define print_driver_short(d,i)         /* nop */
95 #define print_driver(d,i)               /* nop */
96 #define print_driver_list(d,i)          /* nop */
97 #define print_devclass_short(d,i)       /* nop */
98 #define print_devclass(d,i)             /* nop */
99 #define print_devclass_list_short()     /* nop */
100 #define print_devclass_list()           /* nop */
101 #endif
102
103 static void     device_attach_async(device_t dev);
104 static void     device_attach_thread(void *arg);
105 static int      device_doattach(device_t dev);
106
107 static int do_async_attach = 0;
108 static int numasyncthreads;
109 TUNABLE_INT("kern.do_async_attach", &do_async_attach);
110
111 /*
112  * /dev/devctl implementation
113  */
114
115 /*
116  * This design allows only one reader for /dev/devctl.  This is not desirable
117  * in the long run, but will get a lot of hair out of this implementation.
118  * Maybe we should make this device a clonable device.
119  *
120  * Also note: we specifically do not attach a device to the device_t tree
121  * to avoid potential chicken and egg problems.  One could argue that all
122  * of this belongs to the root node.  One could also further argue that the
123  * sysctl interface that we have not might more properly be an ioctl
124  * interface, but at this stage of the game, I'm not inclined to rock that
125  * boat.
126  *
127  * I'm also not sure that the SIGIO support is done correctly or not, as
128  * I copied it from a driver that had SIGIO support that likely hasn't been
129  * tested since 3.4 or 2.2.8!
130  */
131
132 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
133 static int devctl_disable = 0;
134 TUNABLE_INT("hw.bus.devctl_disable", &devctl_disable);
135 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, 0, 0,
136     sysctl_devctl_disable, "I", "devctl disable");
137
138 static d_open_t         devopen;
139 static d_close_t        devclose;
140 static d_read_t         devread;
141 static d_ioctl_t        devioctl;
142 static d_kqfilter_t     devkqfilter;
143
144 static struct dev_ops devctl_ops = {
145         { "devctl", 0, 0 },
146         .d_open =       devopen,
147         .d_close =      devclose,
148         .d_read =       devread,
149         .d_ioctl =      devioctl,
150         .d_kqfilter =   devkqfilter
151 };
152
153 struct dev_event_info
154 {
155         char *dei_data;
156         TAILQ_ENTRY(dev_event_info) dei_link;
157 };
158
159 TAILQ_HEAD(devq, dev_event_info);
160
161 static struct dev_softc
162 {
163         int     inuse;
164         int     nonblock;
165         struct lock lock;
166         struct kqinfo kq;
167         struct devq devq;
168         struct proc *async_proc;
169 } devsoftc;
170
171 static void
172 devinit(void)
173 {
174         make_dev(&devctl_ops, 0, UID_ROOT, GID_WHEEL, 0600, "devctl");
175         lockinit(&devsoftc.lock, "dev mtx", 0, 0);
176         TAILQ_INIT(&devsoftc.devq);
177 }
178
179 static int
180 devopen(struct dev_open_args *ap)
181 {
182         if (devsoftc.inuse)
183                 return (EBUSY);
184         /* move to init */
185         devsoftc.inuse = 1;
186         devsoftc.nonblock = 0;
187         devsoftc.async_proc = NULL;
188         return (0);
189 }
190
191 static int
192 devclose(struct dev_close_args *ap)
193 {
194         devsoftc.inuse = 0;
195         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
196         wakeup(&devsoftc);
197         lockmgr(&devsoftc.lock, LK_RELEASE);
198
199         return (0);
200 }
201
202 /*
203  * The read channel for this device is used to report changes to
204  * userland in realtime.  We are required to free the data as well as
205  * the n1 object because we allocate them separately.  Also note that
206  * we return one record at a time.  If you try to read this device a
207  * character at a time, you will lose the rest of the data.  Listening
208  * programs are expected to cope.
209  */
210 static int
211 devread(struct dev_read_args *ap)
212 {
213         struct uio *uio = ap->a_uio;
214         struct dev_event_info *n1;
215         int rv;
216
217         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
218         while (TAILQ_EMPTY(&devsoftc.devq)) {
219                 if (devsoftc.nonblock) {
220                         lockmgr(&devsoftc.lock, LK_RELEASE);
221                         return (EAGAIN);
222                 }
223                 tsleep_interlock(&devsoftc, PCATCH);
224                 lockmgr(&devsoftc.lock, LK_RELEASE);
225                 rv = tsleep(&devsoftc, PCATCH | PINTERLOCKED, "devctl", 0);
226                 lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
227                 if (rv) {
228                         /*
229                          * Need to translate ERESTART to EINTR here? -- jake
230                          */
231                         lockmgr(&devsoftc.lock, LK_RELEASE);
232                         return (rv);
233                 }
234         }
235         n1 = TAILQ_FIRST(&devsoftc.devq);
236         TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
237         lockmgr(&devsoftc.lock, LK_RELEASE);
238         rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
239         kfree(n1->dei_data, M_BUS);
240         kfree(n1, M_BUS);
241         return (rv);
242 }
243
244 static  int
245 devioctl(struct dev_ioctl_args *ap)
246 {
247         switch (ap->a_cmd) {
248
249         case FIONBIO:
250                 if (*(int*)ap->a_data)
251                         devsoftc.nonblock = 1;
252                 else
253                         devsoftc.nonblock = 0;
254                 return (0);
255         case FIOASYNC:
256                 if (*(int*)ap->a_data)
257                         devsoftc.async_proc = curproc;
258                 else
259                         devsoftc.async_proc = NULL;
260                 return (0);
261
262                 /* (un)Support for other fcntl() calls. */
263         case FIOCLEX:
264         case FIONCLEX:
265         case FIONREAD:
266         case FIOSETOWN:
267         case FIOGETOWN:
268         default:
269                 break;
270         }
271         return (ENOTTY);
272 }
273
274 static void dev_filter_detach(struct knote *);
275 static int dev_filter_read(struct knote *, long);
276
277 static struct filterops dev_filtops =
278         { FILTEROP_ISFD, NULL, dev_filter_detach, dev_filter_read };
279
280 static int
281 devkqfilter(struct dev_kqfilter_args *ap)
282 {
283         struct knote *kn = ap->a_kn;
284         struct klist *klist;
285
286         ap->a_result = 0;
287         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
288
289         switch (kn->kn_filter) {
290         case EVFILT_READ:
291                 kn->kn_fop = &dev_filtops;
292                 break;
293         default:
294                 ap->a_result = EOPNOTSUPP;
295                 lockmgr(&devsoftc.lock, LK_RELEASE);
296                 return (0);
297         }
298
299         klist = &devsoftc.kq.ki_note;
300         knote_insert(klist, kn);
301
302         lockmgr(&devsoftc.lock, LK_RELEASE);
303
304         return (0);
305 }
306
307 static void
308 dev_filter_detach(struct knote *kn)
309 {
310         struct klist *klist;
311
312         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
313         klist = &devsoftc.kq.ki_note;
314         knote_remove(klist, kn);
315         lockmgr(&devsoftc.lock, LK_RELEASE);
316 }
317
318 static int
319 dev_filter_read(struct knote *kn, long hint)
320 {
321         int ready = 0;
322
323         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
324         if (!TAILQ_EMPTY(&devsoftc.devq))
325                 ready = 1;
326         lockmgr(&devsoftc.lock, LK_RELEASE);
327
328         return (ready);
329 }
330
331
332 /**
333  * @brief Return whether the userland process is running
334  */
335 boolean_t
336 devctl_process_running(void)
337 {
338         return (devsoftc.inuse == 1);
339 }
340
341 /**
342  * @brief Queue data to be read from the devctl device
343  *
344  * Generic interface to queue data to the devctl device.  It is
345  * assumed that @p data is properly formatted.  It is further assumed
346  * that @p data is allocated using the M_BUS malloc type.
347  */
348 void
349 devctl_queue_data(char *data)
350 {
351         struct dev_event_info *n1 = NULL;
352         struct proc *p;
353
354         n1 = kmalloc(sizeof(*n1), M_BUS, M_NOWAIT);
355         if (n1 == NULL)
356                 return;
357         n1->dei_data = data;
358         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
359         TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
360         wakeup(&devsoftc);
361         lockmgr(&devsoftc.lock, LK_RELEASE);
362         get_mplock();   /* XXX */
363         KNOTE(&devsoftc.kq.ki_note, 0);
364         rel_mplock();   /* XXX */
365         p = devsoftc.async_proc;
366         if (p != NULL)
367                 ksignal(p, SIGIO);
368 }
369
370 /**
371  * @brief Send a 'notification' to userland, using standard ways
372  */
373 void
374 devctl_notify(const char *system, const char *subsystem, const char *type,
375     const char *data)
376 {
377         int len = 0;
378         char *msg;
379
380         if (system == NULL)
381                 return;         /* BOGUS!  Must specify system. */
382         if (subsystem == NULL)
383                 return;         /* BOGUS!  Must specify subsystem. */
384         if (type == NULL)
385                 return;         /* BOGUS!  Must specify type. */
386         len += strlen(" system=") + strlen(system);
387         len += strlen(" subsystem=") + strlen(subsystem);
388         len += strlen(" type=") + strlen(type);
389         /* add in the data message plus newline. */
390         if (data != NULL)
391                 len += strlen(data);
392         len += 3;       /* '!', '\n', and NUL */
393         msg = kmalloc(len, M_BUS, M_NOWAIT);
394         if (msg == NULL)
395                 return;         /* Drop it on the floor */
396         if (data != NULL)
397                 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
398                     system, subsystem, type, data);
399         else
400                 ksnprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
401                     system, subsystem, type);
402         devctl_queue_data(msg);
403 }
404
405 /*
406  * Common routine that tries to make sending messages as easy as possible.
407  * We allocate memory for the data, copy strings into that, but do not
408  * free it unless there's an error.  The dequeue part of the driver should
409  * free the data.  We don't send data when the device is disabled.  We do
410  * send data, even when we have no listeners, because we wish to avoid
411  * races relating to startup and restart of listening applications.
412  *
413  * devaddq is designed to string together the type of event, with the
414  * object of that event, plus the plug and play info and location info
415  * for that event.  This is likely most useful for devices, but less
416  * useful for other consumers of this interface.  Those should use
417  * the devctl_queue_data() interface instead.
418  */
419 static void
420 devaddq(const char *type, const char *what, device_t dev)
421 {
422         char *data = NULL;
423         char *loc = NULL;
424         char *pnp = NULL;
425         const char *parstr;
426
427         if (devctl_disable)
428                 return;
429         data = kmalloc(1024, M_BUS, M_NOWAIT);
430         if (data == NULL)
431                 goto bad;
432
433         /* get the bus specific location of this device */
434         loc = kmalloc(1024, M_BUS, M_NOWAIT);
435         if (loc == NULL)
436                 goto bad;
437         *loc = '\0';
438         bus_child_location_str(dev, loc, 1024);
439
440         /* Get the bus specific pnp info of this device */
441         pnp = kmalloc(1024, M_BUS, M_NOWAIT);
442         if (pnp == NULL)
443                 goto bad;
444         *pnp = '\0';
445         bus_child_pnpinfo_str(dev, pnp, 1024);
446
447         /* Get the parent of this device, or / if high enough in the tree. */
448         if (device_get_parent(dev) == NULL)
449                 parstr = ".";   /* Or '/' ? */
450         else
451                 parstr = device_get_nameunit(device_get_parent(dev));
452         /* String it all together. */
453         ksnprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
454           parstr);
455         kfree(loc, M_BUS);
456         kfree(pnp, M_BUS);
457         devctl_queue_data(data);
458         return;
459 bad:
460         kfree(pnp, M_BUS);
461         kfree(loc, M_BUS);
462         kfree(data, M_BUS);
463         return;
464 }
465
466 /*
467  * A device was added to the tree.  We are called just after it successfully
468  * attaches (that is, probe and attach success for this device).  No call
469  * is made if a device is merely parented into the tree.  See devnomatch
470  * if probe fails.  If attach fails, no notification is sent (but maybe
471  * we should have a different message for this).
472  */
473 static void
474 devadded(device_t dev)
475 {
476         char *pnp = NULL;
477         char *tmp = NULL;
478
479         pnp = kmalloc(1024, M_BUS, M_NOWAIT);
480         if (pnp == NULL)
481                 goto fail;
482         tmp = kmalloc(1024, M_BUS, M_NOWAIT);
483         if (tmp == NULL)
484                 goto fail;
485         *pnp = '\0';
486         bus_child_pnpinfo_str(dev, pnp, 1024);
487         ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
488         devaddq("+", tmp, dev);
489 fail:
490         if (pnp != NULL)
491                 kfree(pnp, M_BUS);
492         if (tmp != NULL)
493                 kfree(tmp, M_BUS);
494         return;
495 }
496
497 /*
498  * A device was removed from the tree.  We are called just before this
499  * happens.
500  */
501 static void
502 devremoved(device_t dev)
503 {
504         char *pnp = NULL;
505         char *tmp = NULL;
506
507         pnp = kmalloc(1024, M_BUS, M_NOWAIT);
508         if (pnp == NULL)
509                 goto fail;
510         tmp = kmalloc(1024, M_BUS, M_NOWAIT);
511         if (tmp == NULL)
512                 goto fail;
513         *pnp = '\0';
514         bus_child_pnpinfo_str(dev, pnp, 1024);
515         ksnprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
516         devaddq("-", tmp, dev);
517 fail:
518         if (pnp != NULL)
519                 kfree(pnp, M_BUS);
520         if (tmp != NULL)
521                 kfree(tmp, M_BUS);
522         return;
523 }
524
525 /*
526  * Called when there's no match for this device.  This is only called
527  * the first time that no match happens, so we don't keep getitng this
528  * message.  Should that prove to be undesirable, we can change it.
529  * This is called when all drivers that can attach to a given bus
530  * decline to accept this device.  Other errrors may not be detected.
531  */
532 static void
533 devnomatch(device_t dev)
534 {
535         devaddq("?", "", dev);
536 }
537
538 static int
539 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
540 {
541         struct dev_event_info *n1;
542         int dis, error;
543
544         dis = devctl_disable;
545         error = sysctl_handle_int(oidp, &dis, 0, req);
546         if (error || !req->newptr)
547                 return (error);
548         lockmgr(&devsoftc.lock, LK_EXCLUSIVE);
549         devctl_disable = dis;
550         if (dis) {
551                 while (!TAILQ_EMPTY(&devsoftc.devq)) {
552                         n1 = TAILQ_FIRST(&devsoftc.devq);
553                         TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
554                         kfree(n1->dei_data, M_BUS);
555                         kfree(n1, M_BUS);
556                 }
557         }
558         lockmgr(&devsoftc.lock, LK_RELEASE);
559         return (0);
560 }
561
562 /* End of /dev/devctl code */
563
564 TAILQ_HEAD(,device)     bus_data_devices;
565 static int bus_data_generation = 1;
566
567 kobj_method_t null_methods[] = {
568         { 0, 0 }
569 };
570
571 DEFINE_CLASS(null, null_methods, 0);
572
573 /*
574  * Devclass implementation
575  */
576
577 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
578
579 static devclass_t
580 devclass_find_internal(const char *classname, const char *parentname,
581                        int create)
582 {
583         devclass_t dc;
584
585         PDEBUG(("looking for %s", classname));
586         if (classname == NULL)
587                 return(NULL);
588
589         TAILQ_FOREACH(dc, &devclasses, link)
590                 if (!strcmp(dc->name, classname))
591                         break;
592
593         if (create && !dc) {
594                 PDEBUG(("creating %s", classname));
595                 dc = kmalloc(sizeof(struct devclass) + strlen(classname) + 1,
596                             M_BUS, M_INTWAIT | M_ZERO);
597                 dc->parent = NULL;
598                 dc->name = (char*) (dc + 1);
599                 strcpy(dc->name, classname);
600                 dc->devices = NULL;
601                 dc->maxunit = 0;
602                 TAILQ_INIT(&dc->drivers);
603                 TAILQ_INSERT_TAIL(&devclasses, dc, link);
604
605                 bus_data_generation_update();
606
607         }
608
609         /*
610          * If a parent class is specified, then set that as our parent so
611          * that this devclass will support drivers for the parent class as
612          * well.  If the parent class has the same name don't do this though
613          * as it creates a cycle that can trigger an infinite loop in
614          * device_probe_child() if a device exists for which there is no
615          * suitable driver.
616          */
617         if (parentname && dc && !dc->parent &&
618             strcmp(classname, parentname) != 0)
619                 dc->parent = devclass_find_internal(parentname, NULL, FALSE);
620
621         return(dc);
622 }
623
624 devclass_t
625 devclass_create(const char *classname)
626 {
627         return(devclass_find_internal(classname, NULL, TRUE));
628 }
629
630 devclass_t
631 devclass_find(const char *classname)
632 {
633         return(devclass_find_internal(classname, NULL, FALSE));
634 }
635
636 device_t
637 devclass_find_unit(const char *classname, int unit)
638 {
639         devclass_t dc;
640
641         if ((dc = devclass_find(classname)) != NULL)
642             return(devclass_get_device(dc, unit));
643         return (NULL);
644 }
645
646 int
647 devclass_add_driver(devclass_t dc, driver_t *driver)
648 {
649         driverlink_t dl;
650         device_t dev;
651         int i;
652
653         PDEBUG(("%s", DRIVERNAME(driver)));
654
655         dl = kmalloc(sizeof *dl, M_BUS, M_INTWAIT | M_ZERO);
656
657         /*
658          * Compile the driver's methods. Also increase the reference count
659          * so that the class doesn't get freed when the last instance
660          * goes. This means we can safely use static methods and avoids a
661          * double-free in devclass_delete_driver.
662          */
663         kobj_class_instantiate(driver);
664
665         /*
666          * Make sure the devclass which the driver is implementing exists.
667          */
668         devclass_find_internal(driver->name, NULL, TRUE);
669
670         dl->driver = driver;
671         TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
672
673         /*
674          * Call BUS_DRIVER_ADDED for any existing busses in this class,
675          * but only if the bus has already been attached (otherwise we
676          * might probe too early).
677          *
678          * This is what will cause a newly loaded module to be associated
679          * with hardware.  bus_generic_driver_added() is typically what ends
680          * up being called.
681          */
682         for (i = 0; i < dc->maxunit; i++) {
683                 if ((dev = dc->devices[i]) != NULL) {
684                         if (dev->state >= DS_ATTACHED)
685                                 BUS_DRIVER_ADDED(dev, driver);
686                 }
687         }
688
689         bus_data_generation_update();
690         return(0);
691 }
692
693 int
694 devclass_delete_driver(devclass_t busclass, driver_t *driver)
695 {
696         devclass_t dc = devclass_find(driver->name);
697         driverlink_t dl;
698         device_t dev;
699         int i;
700         int error;
701
702         PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
703
704         if (!dc)
705                 return(0);
706
707         /*
708          * Find the link structure in the bus' list of drivers.
709          */
710         TAILQ_FOREACH(dl, &busclass->drivers, link)
711                 if (dl->driver == driver)
712                         break;
713
714         if (!dl) {
715                 PDEBUG(("%s not found in %s list", driver->name, busclass->name));
716                 return(ENOENT);
717         }
718
719         /*
720          * Disassociate from any devices.  We iterate through all the
721          * devices in the devclass of the driver and detach any which are
722          * using the driver and which have a parent in the devclass which
723          * we are deleting from.
724          *
725          * Note that since a driver can be in multiple devclasses, we
726          * should not detach devices which are not children of devices in
727          * the affected devclass.
728          */
729         for (i = 0; i < dc->maxunit; i++)
730                 if (dc->devices[i]) {
731                         dev = dc->devices[i];
732                         if (dev->driver == driver && dev->parent &&
733                             dev->parent->devclass == busclass) {
734                                 if ((error = device_detach(dev)) != 0)
735                                         return(error);
736                                 device_set_driver(dev, NULL);
737                         }
738                 }
739
740         TAILQ_REMOVE(&busclass->drivers, dl, link);
741         kfree(dl, M_BUS);
742
743         kobj_class_uninstantiate(driver);
744
745         bus_data_generation_update();
746         return(0);
747 }
748
749 static driverlink_t
750 devclass_find_driver_internal(devclass_t dc, const char *classname)
751 {
752         driverlink_t dl;
753
754         PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
755
756         TAILQ_FOREACH(dl, &dc->drivers, link)
757                 if (!strcmp(dl->driver->name, classname))
758                         return(dl);
759
760         PDEBUG(("not found"));
761         return(NULL);
762 }
763
764 kobj_class_t
765 devclass_find_driver(devclass_t dc, const char *classname)
766 {
767         driverlink_t dl;
768
769         dl = devclass_find_driver_internal(dc, classname);
770         if (dl)
771                 return(dl->driver);
772         else
773                 return(NULL);
774 }
775
776 const char *
777 devclass_get_name(devclass_t dc)
778 {
779         return(dc->name);
780 }
781
782 device_t
783 devclass_get_device(devclass_t dc, int unit)
784 {
785         if (dc == NULL || unit < 0 || unit >= dc->maxunit)
786                 return(NULL);
787         return(dc->devices[unit]);
788 }
789
790 void *
791 devclass_get_softc(devclass_t dc, int unit)
792 {
793         device_t dev;
794
795         dev = devclass_get_device(dc, unit);
796         if (!dev)
797                 return(NULL);
798
799         return(device_get_softc(dev));
800 }
801
802 int
803 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
804 {
805         int i;
806         int count;
807         device_t *list;
808     
809         count = 0;
810         for (i = 0; i < dc->maxunit; i++)
811                 if (dc->devices[i])
812                         count++;
813
814         list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
815
816         count = 0;
817         for (i = 0; i < dc->maxunit; i++)
818                 if (dc->devices[i]) {
819                         list[count] = dc->devices[i];
820                         count++;
821                 }
822
823         *devlistp = list;
824         *devcountp = count;
825
826         return(0);
827 }
828
829 /**
830  * @brief Get a list of drivers in the devclass
831  *
832  * An array containing a list of pointers to all the drivers in the
833  * given devclass is allocated and returned in @p *listp.  The number
834  * of drivers in the array is returned in @p *countp. The caller should
835  * free the array using @c free(p, M_TEMP).
836  *
837  * @param dc            the devclass to examine
838  * @param listp         gives location for array pointer return value
839  * @param countp        gives location for number of array elements
840  *                      return value
841  *
842  * @retval 0            success
843  * @retval ENOMEM       the array allocation failed
844  */
845 int
846 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
847 {
848         driverlink_t dl;
849         driver_t **list;
850         int count;
851
852         count = 0;
853         TAILQ_FOREACH(dl, &dc->drivers, link)
854                 count++;
855         list = kmalloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
856         if (list == NULL)
857                 return (ENOMEM);
858
859         count = 0;
860         TAILQ_FOREACH(dl, &dc->drivers, link) {
861                 list[count] = dl->driver;
862                 count++;
863         }
864         *listp = list;
865         *countp = count;
866
867         return (0);
868 }
869
870 /**
871  * @brief Get the number of devices in a devclass
872  *
873  * @param dc            the devclass to examine
874  */
875 int
876 devclass_get_count(devclass_t dc)
877 {
878         int count, i;
879
880         count = 0;
881         for (i = 0; i < dc->maxunit; i++)
882                 if (dc->devices[i])
883                         count++;
884         return (count);
885 }
886
887 int
888 devclass_get_maxunit(devclass_t dc)
889 {
890         return(dc->maxunit);
891 }
892
893 void
894 devclass_set_parent(devclass_t dc, devclass_t pdc)
895 {
896         dc->parent = pdc;
897 }
898
899 devclass_t
900 devclass_get_parent(devclass_t dc)
901 {
902         return(dc->parent);
903 }
904
905 static int
906 devclass_alloc_unit(devclass_t dc, int *unitp)
907 {
908         int unit = *unitp;
909
910         PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
911
912         /* If we have been given a wired unit number, check for existing device */
913         if (unit != -1) {
914                 if (unit >= 0 && unit < dc->maxunit &&
915                     dc->devices[unit] != NULL) {
916                         if (bootverbose)
917                                 kprintf("%s-: %s%d exists, using next available unit number\n",
918                                        dc->name, dc->name, unit);
919                         /* find the next available slot */
920                         while (++unit < dc->maxunit && dc->devices[unit] != NULL)
921                                 ;
922                 }
923         } else {
924                 /* Unwired device, find the next available slot for it */
925                 unit = 0;
926                 while (unit < dc->maxunit && dc->devices[unit] != NULL)
927                         unit++;
928         }
929
930         /*
931          * We've selected a unit beyond the length of the table, so let's
932          * extend the table to make room for all units up to and including
933          * this one.
934          */
935         if (unit >= dc->maxunit) {
936                 device_t *newlist;
937                 int newsize;
938
939                 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
940                 newlist = kmalloc(sizeof(device_t) * newsize, M_BUS,
941                                  M_INTWAIT | M_ZERO);
942                 if (newlist == NULL)
943                         return(ENOMEM);
944                 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
945                 if (dc->devices)
946                         kfree(dc->devices, M_BUS);
947                 dc->devices = newlist;
948                 dc->maxunit = newsize;
949         }
950         PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
951
952         *unitp = unit;
953         return(0);
954 }
955
956 static int
957 devclass_add_device(devclass_t dc, device_t dev)
958 {
959         int buflen, error;
960
961         PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
962
963         buflen = strlen(dc->name) + 5;
964         dev->nameunit = kmalloc(buflen, M_BUS, M_INTWAIT | M_ZERO);
965         if (!dev->nameunit)
966                 return(ENOMEM);
967
968         if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
969                 kfree(dev->nameunit, M_BUS);
970                 dev->nameunit = NULL;
971                 return(error);
972         }
973         dc->devices[dev->unit] = dev;
974         dev->devclass = dc;
975         ksnprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
976
977         return(0);
978 }
979
980 static int
981 devclass_delete_device(devclass_t dc, device_t dev)
982 {
983         if (!dc || !dev)
984                 return(0);
985
986         PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
987
988         if (dev->devclass != dc || dc->devices[dev->unit] != dev)
989                 panic("devclass_delete_device: inconsistent device class");
990         dc->devices[dev->unit] = NULL;
991         if (dev->flags & DF_WILDCARD)
992                 dev->unit = -1;
993         dev->devclass = NULL;
994         kfree(dev->nameunit, M_BUS);
995         dev->nameunit = NULL;
996
997         return(0);
998 }
999
1000 static device_t
1001 make_device(device_t parent, const char *name, int unit)
1002 {
1003         device_t dev;
1004         devclass_t dc;
1005
1006         PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
1007
1008         if (name != NULL) {
1009                 dc = devclass_find_internal(name, NULL, TRUE);
1010                 if (!dc) {
1011                         kprintf("make_device: can't find device class %s\n", name);
1012                         return(NULL);
1013                 }
1014         } else
1015                 dc = NULL;
1016
1017         dev = kmalloc(sizeof(struct device), M_BUS, M_INTWAIT | M_ZERO);
1018         if (!dev)
1019                 return(0);
1020
1021         dev->parent = parent;
1022         TAILQ_INIT(&dev->children);
1023         kobj_init((kobj_t) dev, &null_class);
1024         dev->driver = NULL;
1025         dev->devclass = NULL;
1026         dev->unit = unit;
1027         dev->nameunit = NULL;
1028         dev->desc = NULL;
1029         dev->busy = 0;
1030         dev->devflags = 0;
1031         dev->flags = DF_ENABLED;
1032         dev->order = 0;
1033         if (unit == -1)
1034                 dev->flags |= DF_WILDCARD;
1035         if (name) {
1036                 dev->flags |= DF_FIXEDCLASS;
1037                 if (devclass_add_device(dc, dev) != 0) {
1038                         kobj_delete((kobj_t)dev, M_BUS);
1039                         return(NULL);
1040                 }
1041         }
1042         dev->ivars = NULL;
1043         dev->softc = NULL;
1044
1045         dev->state = DS_NOTPRESENT;
1046
1047         TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
1048         bus_data_generation_update();
1049
1050         return(dev);
1051 }
1052
1053 static int
1054 device_print_child(device_t dev, device_t child)
1055 {
1056         int retval = 0;
1057
1058         if (device_is_alive(child))
1059                 retval += BUS_PRINT_CHILD(dev, child);
1060         else
1061                 retval += device_printf(child, " not found\n");
1062
1063         return(retval);
1064 }
1065
1066 device_t
1067 device_add_child(device_t dev, const char *name, int unit)
1068 {
1069         return device_add_child_ordered(dev, 0, name, unit);
1070 }
1071
1072 device_t
1073 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
1074 {
1075         device_t child;
1076         device_t place;
1077
1078         PDEBUG(("%s at %s with order %d as unit %d", name, DEVICENAME(dev),
1079                 order, unit));
1080
1081         child = make_device(dev, name, unit);
1082         if (child == NULL)
1083                 return child;
1084         child->order = order;
1085
1086         TAILQ_FOREACH(place, &dev->children, link)
1087                 if (place->order > order)
1088                         break;
1089
1090         if (place) {
1091                 /*
1092                  * The device 'place' is the first device whose order is
1093                  * greater than the new child.
1094                  */
1095                 TAILQ_INSERT_BEFORE(place, child, link);
1096         } else {
1097                 /*
1098                  * The new child's order is greater or equal to the order of
1099                  * any existing device. Add the child to the tail of the list.
1100                  */
1101                 TAILQ_INSERT_TAIL(&dev->children, child, link);
1102         }
1103
1104         bus_data_generation_update();
1105         return(child);
1106 }
1107
1108 int
1109 device_delete_child(device_t dev, device_t child)
1110 {
1111         int error;
1112         device_t grandchild;
1113
1114         PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
1115
1116         /* remove children first */
1117         while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
1118                 error = device_delete_child(child, grandchild);
1119                 if (error)
1120                         return(error);
1121         }
1122
1123         if ((error = device_detach(child)) != 0)
1124                 return(error);
1125         if (child->devclass)
1126                 devclass_delete_device(child->devclass, child);
1127         TAILQ_REMOVE(&dev->children, child, link);
1128         TAILQ_REMOVE(&bus_data_devices, child, devlink);
1129         device_set_desc(child, NULL);
1130         kobj_delete((kobj_t)child, M_BUS);
1131
1132         bus_data_generation_update();
1133         return(0);
1134 }
1135
1136 /**
1137  * @brief Delete all children devices of the given device, if any.
1138  *
1139  * This function deletes all children devices of the given device, if
1140  * any, using the device_delete_child() function for each device it
1141  * finds. If a child device cannot be deleted, this function will
1142  * return an error code.
1143  * 
1144  * @param dev           the parent device
1145  *
1146  * @retval 0            success
1147  * @retval non-zero     a device would not detach
1148  */
1149 int
1150 device_delete_children(device_t dev)
1151 {
1152         device_t child;
1153         int error;
1154
1155         PDEBUG(("Deleting all children of %s", DEVICENAME(dev)));
1156
1157         error = 0;
1158
1159         while ((child = TAILQ_FIRST(&dev->children)) != NULL) {
1160                 error = device_delete_child(dev, child);
1161                 if (error) {
1162                         PDEBUG(("Failed deleting %s", DEVICENAME(child)));
1163                         break;
1164                 }
1165         }
1166         return (error);
1167 }
1168
1169 /**
1170  * @brief Find a device given a unit number
1171  *
1172  * This is similar to devclass_get_devices() but only searches for
1173  * devices which have @p dev as a parent.
1174  *
1175  * @param dev           the parent device to search
1176  * @param unit          the unit number to search for.  If the unit is -1,
1177  *                      return the first child of @p dev which has name
1178  *                      @p classname (that is, the one with the lowest unit.)
1179  *
1180  * @returns             the device with the given unit number or @c
1181  *                      NULL if there is no such device
1182  */
1183 device_t
1184 device_find_child(device_t dev, const char *classname, int unit)
1185 {
1186         devclass_t dc;
1187         device_t child;
1188
1189         dc = devclass_find(classname);
1190         if (!dc)
1191                 return(NULL);
1192
1193         if (unit != -1) {
1194                 child = devclass_get_device(dc, unit);
1195                 if (child && child->parent == dev)
1196                         return (child);
1197         } else {
1198                 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
1199                         child = devclass_get_device(dc, unit);
1200                         if (child && child->parent == dev)
1201                                 return (child);
1202                 }
1203         }
1204         return(NULL);
1205 }
1206
1207 static driverlink_t
1208 first_matching_driver(devclass_t dc, device_t dev)
1209 {
1210         if (dev->devclass)
1211                 return(devclass_find_driver_internal(dc, dev->devclass->name));
1212         else
1213                 return(TAILQ_FIRST(&dc->drivers));
1214 }
1215
1216 static driverlink_t
1217 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
1218 {
1219         if (dev->devclass) {
1220                 driverlink_t dl;
1221                 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
1222                         if (!strcmp(dev->devclass->name, dl->driver->name))
1223                                 return(dl);
1224                 return(NULL);
1225         } else
1226                 return(TAILQ_NEXT(last, link));
1227 }
1228
1229 int
1230 device_probe_child(device_t dev, device_t child)
1231 {
1232         devclass_t dc;
1233         driverlink_t best = NULL;
1234         driverlink_t dl;
1235         int result, pri = 0;
1236         int hasclass = (child->devclass != NULL);
1237
1238         dc = dev->devclass;
1239         if (!dc)
1240                 panic("device_probe_child: parent device has no devclass");
1241
1242         if (child->state == DS_ALIVE)
1243                 return(0);
1244
1245         for (; dc; dc = dc->parent) {
1246                 for (dl = first_matching_driver(dc, child); dl;
1247                      dl = next_matching_driver(dc, child, dl)) {
1248                         PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
1249                         device_set_driver(child, dl->driver);
1250                         if (!hasclass)
1251                                 device_set_devclass(child, dl->driver->name);
1252                         result = DEVICE_PROBE(child);
1253                         if (!hasclass)
1254                                 device_set_devclass(child, 0);
1255
1256                         /*
1257                          * If the driver returns SUCCESS, there can be
1258                          * no higher match for this device.
1259                          */
1260                         if (result == 0) {
1261                                 best = dl;
1262                                 pri = 0;
1263                                 break;
1264                         }
1265
1266                         /*
1267                          * The driver returned an error so it
1268                          * certainly doesn't match.
1269                          */
1270                         if (result > 0) {
1271                                 device_set_driver(child, 0);
1272                                 continue;
1273                         }
1274
1275                         /*
1276                          * A priority lower than SUCCESS, remember the
1277                          * best matching driver. Initialise the value
1278                          * of pri for the first match.
1279                          */
1280                         if (best == NULL || result > pri) {
1281                                 best = dl;
1282                                 pri = result;
1283                                 continue;
1284                         }
1285                 }
1286                 /*
1287                  * If we have unambiguous match in this devclass,
1288                  * don't look in the parent.
1289                  */
1290                 if (best && pri == 0)
1291                         break;
1292         }
1293
1294         /*
1295          * If we found a driver, change state and initialise the devclass.
1296          */
1297         if (best) {
1298                 if (!child->devclass)
1299                         device_set_devclass(child, best->driver->name);
1300                 device_set_driver(child, best->driver);
1301                 if (pri < 0) {
1302                         /*
1303                          * A bit bogus. Call the probe method again to make
1304                          * sure that we have the right description.
1305                          */
1306                         DEVICE_PROBE(child);
1307                 }
1308
1309                 bus_data_generation_update();
1310                 child->state = DS_ALIVE;
1311                 return(0);
1312         }
1313
1314         return(ENXIO);
1315 }
1316
1317 device_t
1318 device_get_parent(device_t dev)
1319 {
1320         return dev->parent;
1321 }
1322
1323 int
1324 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
1325 {
1326         int count;
1327         device_t child;
1328         device_t *list;
1329     
1330         count = 0;
1331         TAILQ_FOREACH(child, &dev->children, link)
1332                 count++;
1333
1334         list = kmalloc(count * sizeof(device_t), M_TEMP, M_INTWAIT | M_ZERO);
1335
1336         count = 0;
1337         TAILQ_FOREACH(child, &dev->children, link) {
1338                 list[count] = child;
1339                 count++;
1340         }
1341
1342         *devlistp = list;
1343         *devcountp = count;
1344
1345         return(0);
1346 }
1347
1348 driver_t *
1349 device_get_driver(device_t dev)
1350 {
1351         return(dev->driver);
1352 }
1353
1354 devclass_t
1355 device_get_devclass(device_t dev)
1356 {
1357         return(dev->devclass);
1358 }
1359
1360 const char *
1361 device_get_name(device_t dev)
1362 {
1363         if (dev->devclass)
1364                 return devclass_get_name(dev->devclass);
1365         return(NULL);
1366 }
1367
1368 const char *
1369 device_get_nameunit(device_t dev)
1370 {
1371         return(dev->nameunit);
1372 }
1373
1374 int
1375 device_get_unit(device_t dev)
1376 {
1377         return(dev->unit);
1378 }
1379
1380 const char *
1381 device_get_desc(device_t dev)
1382 {
1383         return(dev->desc);
1384 }
1385
1386 uint32_t
1387 device_get_flags(device_t dev)
1388 {
1389         return(dev->devflags);
1390 }
1391
1392 int
1393 device_print_prettyname(device_t dev)
1394 {
1395         const char *name = device_get_name(dev);
1396
1397         if (name == NULL)
1398                 return kprintf("unknown: ");
1399         else
1400                 return kprintf("%s%d: ", name, device_get_unit(dev));
1401 }
1402
1403 int
1404 device_printf(device_t dev, const char * fmt, ...)
1405 {
1406         __va_list ap;
1407         int retval;
1408
1409         retval = device_print_prettyname(dev);
1410         __va_start(ap, fmt);
1411         retval += kvprintf(fmt, ap);
1412         __va_end(ap);
1413         return retval;
1414 }
1415
1416 static void
1417 device_set_desc_internal(device_t dev, const char* desc, int copy)
1418 {
1419         if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
1420                 kfree(dev->desc, M_BUS);
1421                 dev->flags &= ~DF_DESCMALLOCED;
1422                 dev->desc = NULL;
1423         }
1424
1425         if (copy && desc) {
1426                 dev->desc = kmalloc(strlen(desc) + 1, M_BUS, M_INTWAIT);
1427                 if (dev->desc) {
1428                         strcpy(dev->desc, desc);
1429                         dev->flags |= DF_DESCMALLOCED;
1430                 }
1431         } else {
1432                 /* Avoid a -Wcast-qual warning */
1433                 dev->desc = (char *)(uintptr_t) desc;
1434         }
1435
1436         bus_data_generation_update();
1437 }
1438
1439 void
1440 device_set_desc(device_t dev, const char* desc)
1441 {
1442         device_set_desc_internal(dev, desc, FALSE);
1443 }
1444
1445 void
1446 device_set_desc_copy(device_t dev, const char* desc)
1447 {
1448         device_set_desc_internal(dev, desc, TRUE);
1449 }
1450
1451 void
1452 device_set_flags(device_t dev, uint32_t flags)
1453 {
1454         dev->devflags = flags;
1455 }
1456
1457 void *
1458 device_get_softc(device_t dev)
1459 {
1460         return dev->softc;
1461 }
1462
1463 void
1464 device_set_softc(device_t dev, void *softc)
1465 {
1466         if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
1467                 kfree(dev->softc, M_BUS);
1468         dev->softc = softc;
1469         if (dev->softc)
1470                 dev->flags |= DF_EXTERNALSOFTC;
1471         else
1472                 dev->flags &= ~DF_EXTERNALSOFTC;
1473 }
1474
1475 void
1476 device_set_async_attach(device_t dev, int enable)
1477 {
1478         if (enable)
1479                 dev->flags |= DF_ASYNCPROBE;
1480         else
1481                 dev->flags &= ~DF_ASYNCPROBE;
1482 }
1483
1484 void *
1485 device_get_ivars(device_t dev)
1486 {
1487         return dev->ivars;
1488 }
1489
1490 void
1491 device_set_ivars(device_t dev, void * ivars)
1492 {
1493         if (!dev)
1494                 return;
1495
1496         dev->ivars = ivars;
1497 }
1498
1499 device_state_t
1500 device_get_state(device_t dev)
1501 {
1502         return(dev->state);
1503 }
1504
1505 void
1506 device_enable(device_t dev)
1507 {
1508         dev->flags |= DF_ENABLED;
1509 }
1510
1511 void
1512 device_disable(device_t dev)
1513 {
1514         dev->flags &= ~DF_ENABLED;
1515 }
1516
1517 /*
1518  * YYY cannot block
1519  */
1520 void
1521 device_busy(device_t dev)
1522 {
1523         if (dev->state < DS_ATTACHED)
1524                 panic("device_busy: called for unattached device");
1525         if (dev->busy == 0 && dev->parent)
1526                 device_busy(dev->parent);
1527         dev->busy++;
1528         dev->state = DS_BUSY;
1529 }
1530
1531 /*
1532  * YYY cannot block
1533  */
1534 void
1535 device_unbusy(device_t dev)
1536 {
1537         if (dev->state != DS_BUSY)
1538                 panic("device_unbusy: called for non-busy device");
1539         dev->busy--;
1540         if (dev->busy == 0) {
1541                 if (dev->parent)
1542                         device_unbusy(dev->parent);
1543                 dev->state = DS_ATTACHED;
1544         }
1545 }
1546
1547 void
1548 device_quiet(device_t dev)
1549 {
1550         dev->flags |= DF_QUIET;
1551 }
1552
1553 void
1554 device_verbose(device_t dev)
1555 {
1556         dev->flags &= ~DF_QUIET;
1557 }
1558
1559 int
1560 device_is_quiet(device_t dev)
1561 {
1562         return((dev->flags & DF_QUIET) != 0);
1563 }
1564
1565 int
1566 device_is_enabled(device_t dev)
1567 {
1568         return((dev->flags & DF_ENABLED) != 0);
1569 }
1570
1571 int
1572 device_is_alive(device_t dev)
1573 {
1574         return(dev->state >= DS_ALIVE);
1575 }
1576
1577 int
1578 device_is_attached(device_t dev)
1579 {
1580         return(dev->state >= DS_ATTACHED);
1581 }
1582
1583 int
1584 device_set_devclass(device_t dev, const char *classname)
1585 {
1586         devclass_t dc;
1587         int error;
1588
1589         if (!classname) {
1590                 if (dev->devclass)
1591                         devclass_delete_device(dev->devclass, dev);
1592                 return(0);
1593         }
1594
1595         if (dev->devclass) {
1596                 kprintf("device_set_devclass: device class already set\n");
1597                 return(EINVAL);
1598         }
1599
1600         dc = devclass_find_internal(classname, NULL, TRUE);
1601         if (!dc)
1602                 return(ENOMEM);
1603
1604         error = devclass_add_device(dc, dev);
1605
1606         bus_data_generation_update();
1607         return(error);
1608 }
1609
1610 int
1611 device_set_driver(device_t dev, driver_t *driver)
1612 {
1613         if (dev->state >= DS_ATTACHED)
1614                 return(EBUSY);
1615
1616         if (dev->driver == driver)
1617                 return(0);
1618
1619         if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
1620                 kfree(dev->softc, M_BUS);
1621                 dev->softc = NULL;
1622         }
1623         kobj_delete((kobj_t) dev, 0);
1624         dev->driver = driver;
1625         if (driver) {
1626                 kobj_init((kobj_t) dev, (kobj_class_t) driver);
1627                 if (!(dev->flags & DF_EXTERNALSOFTC))
1628                         dev->softc = kmalloc(driver->size, M_BUS,
1629                                             M_INTWAIT | M_ZERO);
1630         } else {
1631                 kobj_init((kobj_t) dev, &null_class);
1632         }
1633
1634         bus_data_generation_update();
1635         return(0);
1636 }
1637
1638 int
1639 device_probe_and_attach(device_t dev)
1640 {
1641         device_t bus = dev->parent;
1642         int error = 0;
1643
1644         if (dev->state >= DS_ALIVE)
1645                 return(0);
1646
1647         if ((dev->flags & DF_ENABLED) == 0) {
1648                 if (bootverbose) {
1649                         device_print_prettyname(dev);
1650                         kprintf("not probed (disabled)\n");
1651                 }
1652                 return(0);
1653         }
1654
1655         error = device_probe_child(bus, dev);
1656         if (error) {
1657                 if (!(dev->flags & DF_DONENOMATCH)) {
1658                         BUS_PROBE_NOMATCH(bus, dev);
1659                         devnomatch(dev);
1660                         dev->flags |= DF_DONENOMATCH;
1661                 }
1662                 return(error);
1663         }
1664
1665         /*
1666          * Output the exact device chain prior to the attach in case the  
1667          * system locks up during attach, and generate the full info after
1668          * the attach so correct irq and other information is displayed.
1669          */
1670         if (bootverbose && !device_is_quiet(dev)) {
1671                 device_t tmp;
1672
1673                 kprintf("%s", device_get_nameunit(dev));
1674                 for (tmp = dev->parent; tmp; tmp = tmp->parent)
1675                         kprintf(".%s", device_get_nameunit(tmp));
1676                 kprintf("\n");
1677         }
1678         if (!device_is_quiet(dev))
1679                 device_print_child(bus, dev);
1680         if ((dev->flags & DF_ASYNCPROBE) && do_async_attach) {
1681                 kprintf("%s: probing asynchronously\n",
1682                         device_get_nameunit(dev));
1683                 dev->state = DS_INPROGRESS;
1684                 device_attach_async(dev);
1685                 error = 0;
1686         } else {
1687                 error = device_doattach(dev);
1688         }
1689         return(error);
1690 }
1691
1692 /*
1693  * Device is known to be alive, do the attach asynchronously.
1694  * However, serialize the attaches with the mp lock.
1695  */
1696 static void
1697 device_attach_async(device_t dev)
1698 {
1699         thread_t td;
1700
1701         atomic_add_int(&numasyncthreads, 1);
1702         lwkt_create(device_attach_thread, dev, &td, NULL,
1703                     0, 0, "%s", (dev->desc ? dev->desc : "devattach"));
1704 }
1705
1706 static void
1707 device_attach_thread(void *arg)
1708 {
1709         device_t dev = arg;
1710
1711         get_mplock();   /* XXX replace with devattach_token later */
1712         (void)device_doattach(dev);
1713         atomic_subtract_int(&numasyncthreads, 1);
1714         wakeup(&numasyncthreads);
1715         rel_mplock();   /* XXX replace with devattach_token later */
1716 }
1717
1718 /*
1719  * Device is known to be alive, do the attach (synchronous or asynchronous)
1720  */
1721 static int
1722 device_doattach(device_t dev)
1723 {
1724         device_t bus = dev->parent;
1725         int hasclass = (dev->devclass != NULL);
1726         int error;
1727
1728         error = DEVICE_ATTACH(dev);
1729         if (error == 0) {
1730                 dev->state = DS_ATTACHED;
1731                 if (bootverbose && !device_is_quiet(dev))
1732                         device_print_child(bus, dev);
1733                 devadded(dev);
1734         } else {
1735                 kprintf("device_probe_and_attach: %s%d attach returned %d\n",
1736                        dev->driver->name, dev->unit, error);
1737                 /* Unset the class that was set in device_probe_child */
1738                 if (!hasclass)
1739                         device_set_devclass(dev, 0);
1740                 device_set_driver(dev, NULL);
1741                 dev->state = DS_NOTPRESENT;
1742         }
1743         return(error);
1744 }
1745
1746 int
1747 device_detach(device_t dev)
1748 {
1749         int error;
1750
1751         PDEBUG(("%s", DEVICENAME(dev)));
1752         if (dev->state == DS_BUSY)
1753                 return(EBUSY);
1754         if (dev->state != DS_ATTACHED)
1755                 return(0);
1756
1757         if ((error = DEVICE_DETACH(dev)) != 0)
1758                 return(error);
1759         devremoved(dev);
1760         device_printf(dev, "detached\n");
1761         if (dev->parent)
1762                 BUS_CHILD_DETACHED(dev->parent, dev);
1763
1764         if (!(dev->flags & DF_FIXEDCLASS))
1765                 devclass_delete_device(dev->devclass, dev);
1766
1767         dev->state = DS_NOTPRESENT;
1768         device_set_driver(dev, NULL);
1769
1770         return(0);
1771 }
1772
1773 int
1774 device_shutdown(device_t dev)
1775 {
1776         if (dev->state < DS_ATTACHED)
1777                 return 0;
1778         PDEBUG(("%s", DEVICENAME(dev)));
1779         return DEVICE_SHUTDOWN(dev);
1780 }
1781
1782 int
1783 device_set_unit(device_t dev, int unit)
1784 {
1785         devclass_t dc;
1786         int err;
1787
1788         dc = device_get_devclass(dev);
1789         if (unit < dc->maxunit && dc->devices[unit])
1790                 return(EBUSY);
1791         err = devclass_delete_device(dc, dev);
1792         if (err)
1793                 return(err);
1794         dev->unit = unit;
1795         err = devclass_add_device(dc, dev);
1796         if (err)
1797                 return(err);
1798
1799         bus_data_generation_update();
1800         return(0);
1801 }
1802
1803 /*======================================*/
1804 /*
1805  * Access functions for device resources.
1806  */
1807
1808 /* Supplied by config(8) in ioconf.c */
1809 extern struct config_device config_devtab[];
1810 extern int devtab_count;
1811
1812 /* Runtime version */
1813 struct config_device *devtab = config_devtab;
1814
1815 static int
1816 resource_new_name(const char *name, int unit)
1817 {
1818         struct config_device *new;
1819
1820         new = kmalloc((devtab_count + 1) * sizeof(*new), M_TEMP,
1821                      M_INTWAIT | M_ZERO);
1822         if (devtab && devtab_count > 0)
1823                 bcopy(devtab, new, devtab_count * sizeof(*new));
1824         new[devtab_count].name = kmalloc(strlen(name) + 1, M_TEMP, M_INTWAIT);
1825         if (new[devtab_count].name == NULL) {
1826                 kfree(new, M_TEMP);
1827                 return(-1);
1828         }
1829         strcpy(new[devtab_count].name, name);
1830         new[devtab_count].unit = unit;
1831         new[devtab_count].resource_count = 0;
1832         new[devtab_count].resources = NULL;
1833         if (devtab && devtab != config_devtab)
1834                 kfree(devtab, M_TEMP);
1835         devtab = new;
1836         return devtab_count++;
1837 }
1838
1839 static int
1840 resource_new_resname(int j, const char *resname, resource_type type)
1841 {
1842         struct config_resource *new;
1843         int i;
1844
1845         i = devtab[j].resource_count;
1846         new = kmalloc((i + 1) * sizeof(*new), M_TEMP, M_INTWAIT | M_ZERO);
1847         if (devtab[j].resources && i > 0)
1848                 bcopy(devtab[j].resources, new, i * sizeof(*new));
1849         new[i].name = kmalloc(strlen(resname) + 1, M_TEMP, M_INTWAIT);
1850         if (new[i].name == NULL) {
1851                 kfree(new, M_TEMP);
1852                 return(-1);
1853         }
1854         strcpy(new[i].name, resname);
1855         new[i].type = type;
1856         if (devtab[j].resources)
1857                 kfree(devtab[j].resources, M_TEMP);
1858         devtab[j].resources = new;
1859         devtab[j].resource_count = i + 1;
1860         return(i);
1861 }
1862
1863 static int
1864 resource_match_string(int i, const char *resname, const char *value)
1865 {
1866         int j;
1867         struct config_resource *res;
1868
1869         for (j = 0, res = devtab[i].resources;
1870              j < devtab[i].resource_count; j++, res++)
1871                 if (!strcmp(res->name, resname)
1872                     && res->type == RES_STRING
1873                     && !strcmp(res->u.stringval, value))
1874                         return(j);
1875         return(-1);
1876 }
1877
1878 static int
1879 resource_find(const char *name, int unit, const char *resname, 
1880               struct config_resource **result)
1881 {
1882         int i, j;
1883         struct config_resource *res;
1884
1885         /*
1886          * First check specific instances, then generic.
1887          */
1888         for (i = 0; i < devtab_count; i++) {
1889                 if (devtab[i].unit < 0)
1890                         continue;
1891                 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1892                         res = devtab[i].resources;
1893                         for (j = 0; j < devtab[i].resource_count; j++, res++)
1894                                 if (!strcmp(res->name, resname)) {
1895                                         *result = res;
1896                                         return(0);
1897                                 }
1898                 }
1899         }
1900         for (i = 0; i < devtab_count; i++) {
1901                 if (devtab[i].unit >= 0)
1902                         continue;
1903                 /* XXX should this `&& devtab[i].unit == unit' be here? */
1904                 /* XXX if so, then the generic match does nothing */
1905                 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
1906                         res = devtab[i].resources;
1907                         for (j = 0; j < devtab[i].resource_count; j++, res++)
1908                                 if (!strcmp(res->name, resname)) {
1909                                         *result = res;
1910                                         return(0);
1911                                 }
1912                 }
1913         }
1914         return(ENOENT);
1915 }
1916
1917 static int
1918 resource_kenv(const char *name, int unit, const char *resname, long *result)
1919 {
1920         const char *env;
1921         char buf[64];
1922
1923         ksnprintf(buf, sizeof(buf), "%s%d.%s", name, unit, resname);
1924         if ((env = kgetenv(buf)) != NULL) {
1925                 *result = strtol(env, NULL, 0);
1926                 return(0);
1927         }
1928         return (ENOENT);
1929 }
1930
1931 int
1932 resource_int_value(const char *name, int unit, const char *resname, int *result)
1933 {
1934         struct config_resource *res;
1935         long kvalue = 0;
1936         int error;
1937
1938         if (resource_kenv(name, unit, resname, &kvalue) == 0) {
1939                 *result = (int)kvalue;
1940                 return 0;
1941         }
1942         if ((error = resource_find(name, unit, resname, &res)) != 0)
1943                 return(error);
1944         if (res->type != RES_INT)
1945                 return(EFTYPE);
1946         *result = res->u.intval;
1947         return(0);
1948 }
1949
1950 int
1951 resource_long_value(const char *name, int unit, const char *resname,
1952                     long *result)
1953 {
1954         struct config_resource *res;
1955         long kvalue;
1956         int error;
1957
1958         if (resource_kenv(name, unit, resname, &kvalue) == 0) {
1959                 *result = kvalue;
1960                 return 0;
1961         }
1962         if ((error = resource_find(name, unit, resname, &res)) != 0)
1963                 return(error);
1964         if (res->type != RES_LONG)
1965                 return(EFTYPE);
1966         *result = res->u.longval;
1967         return(0);
1968 }
1969
1970 int
1971 resource_string_value(const char *name, int unit, const char *resname,
1972                       char **result)
1973 {
1974         int error;
1975         struct config_resource *res;
1976
1977         if ((error = resource_find(name, unit, resname, &res)) != 0)
1978                 return(error);
1979         if (res->type != RES_STRING)
1980                 return(EFTYPE);
1981         *result = res->u.stringval;
1982         return(0);
1983 }
1984
1985 int
1986 resource_query_string(int i, const char *resname, const char *value)
1987 {
1988         if (i < 0)
1989                 i = 0;
1990         else
1991                 i = i + 1;
1992         for (; i < devtab_count; i++)
1993                 if (resource_match_string(i, resname, value) >= 0)
1994                         return(i);
1995         return(-1);
1996 }
1997
1998 int
1999 resource_locate(int i, const char *resname)
2000 {
2001         if (i < 0)
2002                 i = 0;
2003         else
2004                 i = i + 1;
2005         for (; i < devtab_count; i++)
2006                 if (!strcmp(devtab[i].name, resname))
2007                         return(i);
2008         return(-1);
2009 }
2010
2011 int
2012 resource_count(void)
2013 {
2014         return(devtab_count);
2015 }
2016
2017 char *
2018 resource_query_name(int i)
2019 {
2020         return(devtab[i].name);
2021 }
2022
2023 int
2024 resource_query_unit(int i)
2025 {
2026         return(devtab[i].unit);
2027 }
2028
2029 static int
2030 resource_create(const char *name, int unit, const char *resname,
2031                 resource_type type, struct config_resource **result)
2032 {
2033         int i, j;
2034         struct config_resource *res = NULL;
2035
2036         for (i = 0; i < devtab_count; i++)
2037                 if (!strcmp(devtab[i].name, name) && devtab[i].unit == unit) {
2038                         res = devtab[i].resources;
2039                         break;
2040                 }
2041         if (res == NULL) {
2042                 i = resource_new_name(name, unit);
2043                 if (i < 0)
2044                         return(ENOMEM);
2045                 res = devtab[i].resources;
2046         }
2047         for (j = 0; j < devtab[i].resource_count; j++, res++)
2048                 if (!strcmp(res->name, resname)) {
2049                         *result = res;
2050                         return(0);
2051                 }
2052         j = resource_new_resname(i, resname, type);
2053         if (j < 0)
2054                 return(ENOMEM);
2055         res = &devtab[i].resources[j];
2056         *result = res;
2057         return(0);
2058 }
2059
2060 int
2061 resource_set_int(const char *name, int unit, const char *resname, int value)
2062 {
2063         int error;
2064         struct config_resource *res;
2065
2066         error = resource_create(name, unit, resname, RES_INT, &res);
2067         if (error)
2068                 return(error);
2069         if (res->type != RES_INT)
2070                 return(EFTYPE);
2071         res->u.intval = value;
2072         return(0);
2073 }
2074
2075 int
2076 resource_set_long(const char *name, int unit, const char *resname, long value)
2077 {
2078         int error;
2079         struct config_resource *res;
2080
2081         error = resource_create(name, unit, resname, RES_LONG, &res);
2082         if (error)
2083                 return(error);
2084         if (res->type != RES_LONG)
2085                 return(EFTYPE);
2086         res->u.longval = value;
2087         return(0);
2088 }
2089
2090 int
2091 resource_set_string(const char *name, int unit, const char *resname,
2092                     const char *value)
2093 {
2094         int error;
2095         struct config_resource *res;
2096
2097         error = resource_create(name, unit, resname, RES_STRING, &res);
2098         if (error)
2099                 return(error);
2100         if (res->type != RES_STRING)
2101                 return(EFTYPE);
2102         if (res->u.stringval)
2103                 kfree(res->u.stringval, M_TEMP);
2104         res->u.stringval = kmalloc(strlen(value) + 1, M_TEMP, M_INTWAIT);
2105         if (res->u.stringval == NULL)
2106                 return(ENOMEM);
2107         strcpy(res->u.stringval, value);
2108         return(0);
2109 }
2110
2111 static void
2112 resource_cfgload(void *dummy __unused)
2113 {
2114         struct config_resource *res, *cfgres;
2115         int i, j;
2116         int error;
2117         char *name, *resname;
2118         int unit;
2119         resource_type type;
2120         char *stringval;
2121         int config_devtab_count;
2122
2123         config_devtab_count = devtab_count;
2124         devtab = NULL;
2125         devtab_count = 0;
2126
2127         for (i = 0; i < config_devtab_count; i++) {
2128                 name = config_devtab[i].name;
2129                 unit = config_devtab[i].unit;
2130
2131                 for (j = 0; j < config_devtab[i].resource_count; j++) {
2132                         cfgres = config_devtab[i].resources;
2133                         resname = cfgres[j].name;
2134                         type = cfgres[j].type;
2135                         error = resource_create(name, unit, resname, type,
2136                                                 &res);
2137                         if (error) {
2138                                 kprintf("create resource %s%d: error %d\n",
2139                                         name, unit, error);
2140                                 continue;
2141                         }
2142                         if (res->type != type) {
2143                                 kprintf("type mismatch %s%d: %d != %d\n",
2144                                         name, unit, res->type, type);
2145                                 continue;
2146                         }
2147                         switch (type) {
2148                         case RES_INT:
2149                                 res->u.intval = cfgres[j].u.intval;
2150                                 break;
2151                         case RES_LONG:
2152                                 res->u.longval = cfgres[j].u.longval;
2153                                 break;
2154                         case RES_STRING:
2155                                 if (res->u.stringval)
2156                                         kfree(res->u.stringval, M_TEMP);
2157                                 stringval = cfgres[j].u.stringval;
2158                                 res->u.stringval = kmalloc(strlen(stringval) + 1,
2159                                                           M_TEMP, M_INTWAIT);
2160                                 if (res->u.stringval == NULL)
2161                                         break;
2162                                 strcpy(res->u.stringval, stringval);
2163                                 break;
2164                         default:
2165                                 panic("unknown resource type %d", type);
2166                         }
2167                 }
2168         }
2169 }
2170 SYSINIT(cfgload, SI_BOOT1_POST, SI_ORDER_ANY + 50, resource_cfgload, 0)
2171
2172
2173 /*======================================*/
2174 /*
2175  * Some useful method implementations to make life easier for bus drivers.
2176  */
2177
2178 void
2179 resource_list_init(struct resource_list *rl)
2180 {
2181         SLIST_INIT(rl);
2182 }
2183
2184 void
2185 resource_list_free(struct resource_list *rl)
2186 {
2187         struct resource_list_entry *rle;
2188
2189         while ((rle = SLIST_FIRST(rl)) != NULL) {
2190                 if (rle->res)
2191                         panic("resource_list_free: resource entry is busy");
2192                 SLIST_REMOVE_HEAD(rl, link);
2193                 kfree(rle, M_BUS);
2194         }
2195 }
2196
2197 void
2198 resource_list_add(struct resource_list *rl, int type, int rid,
2199     u_long start, u_long end, u_long count, int cpuid)
2200 {
2201         struct resource_list_entry *rle;
2202
2203         rle = resource_list_find(rl, type, rid);
2204         if (rle == NULL) {
2205                 rle = kmalloc(sizeof(struct resource_list_entry), M_BUS,
2206                              M_INTWAIT);
2207                 SLIST_INSERT_HEAD(rl, rle, link);
2208                 rle->type = type;
2209                 rle->rid = rid;
2210                 rle->res = NULL;
2211                 rle->cpuid = -1;
2212         }
2213
2214         if (rle->res)
2215                 panic("resource_list_add: resource entry is busy");
2216
2217         rle->start = start;
2218         rle->end = end;
2219         rle->count = count;
2220
2221         if (cpuid != -1) {
2222                 if (rle->cpuid != -1 && rle->cpuid != cpuid) {
2223                         panic("resource_list_add: moving from cpu%d -> cpu%d",
2224                             rle->cpuid, cpuid);
2225                 }
2226                 rle->cpuid = cpuid;
2227         }
2228 }
2229
2230 struct resource_list_entry*
2231 resource_list_find(struct resource_list *rl,
2232                    int type, int rid)
2233 {
2234         struct resource_list_entry *rle;
2235
2236         SLIST_FOREACH(rle, rl, link)
2237                 if (rle->type == type && rle->rid == rid)
2238                         return(rle);
2239         return(NULL);
2240 }
2241
2242 void
2243 resource_list_delete(struct resource_list *rl,
2244                      int type, int rid)
2245 {
2246         struct resource_list_entry *rle = resource_list_find(rl, type, rid);
2247
2248         if (rle) {
2249                 if (rle->res != NULL)
2250                         panic("resource_list_delete: resource has not been released");
2251                 SLIST_REMOVE(rl, rle, resource_list_entry, link);
2252                 kfree(rle, M_BUS);
2253         }
2254 }
2255
2256 struct resource *
2257 resource_list_alloc(struct resource_list *rl,
2258                     device_t bus, device_t child,
2259                     int type, int *rid,
2260                     u_long start, u_long end,
2261                     u_long count, u_int flags, int cpuid)
2262 {
2263         struct resource_list_entry *rle = NULL;
2264         int passthrough = (device_get_parent(child) != bus);
2265         int isdefault = (start == 0UL && end == ~0UL);
2266
2267         if (passthrough) {
2268                 return(BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2269                                           type, rid,
2270                                           start, end, count, flags, cpuid));
2271         }
2272
2273         rle = resource_list_find(rl, type, *rid);
2274
2275         if (!rle)
2276                 return(0);              /* no resource of that type/rid */
2277
2278         if (rle->res)
2279                 panic("resource_list_alloc: resource entry is busy");
2280
2281         if (isdefault) {
2282                 start = rle->start;
2283                 count = max(count, rle->count);
2284                 end = max(rle->end, start + count - 1);
2285         }
2286         cpuid = rle->cpuid;
2287
2288         rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
2289                                       type, rid, start, end, count,
2290                                       flags, cpuid);
2291
2292         /*
2293          * Record the new range.
2294          */
2295         if (rle->res) {
2296                 rle->start = rman_get_start(rle->res);
2297                 rle->end = rman_get_end(rle->res);
2298                 rle->count = count;
2299         }
2300
2301         return(rle->res);
2302 }
2303
2304 int
2305 resource_list_release(struct resource_list *rl,
2306                       device_t bus, device_t child,
2307                       int type, int rid, struct resource *res)
2308 {
2309         struct resource_list_entry *rle = NULL;
2310         int passthrough = (device_get_parent(child) != bus);
2311         int error;
2312
2313         if (passthrough) {
2314                 return(BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2315                                             type, rid, res));
2316         }
2317
2318         rle = resource_list_find(rl, type, rid);
2319
2320         if (!rle)
2321                 panic("resource_list_release: can't find resource");
2322         if (!rle->res)
2323                 panic("resource_list_release: resource entry is not busy");
2324
2325         error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
2326                                      type, rid, res);
2327         if (error)
2328                 return(error);
2329
2330         rle->res = NULL;
2331         return(0);
2332 }
2333
2334 int
2335 resource_list_print_type(struct resource_list *rl, const char *name, int type,
2336                          const char *format)
2337 {
2338         struct resource_list_entry *rle;
2339         int printed, retval;
2340
2341         printed = 0;
2342         retval = 0;
2343         /* Yes, this is kinda cheating */
2344         SLIST_FOREACH(rle, rl, link) {
2345                 if (rle->type == type) {
2346                         if (printed == 0)
2347                                 retval += kprintf(" %s ", name);
2348                         else
2349                                 retval += kprintf(",");
2350                         printed++;
2351                         retval += kprintf(format, rle->start);
2352                         if (rle->count > 1) {
2353                                 retval += kprintf("-");
2354                                 retval += kprintf(format, rle->start +
2355                                                  rle->count - 1);
2356                         }
2357                 }
2358         }
2359         return(retval);
2360 }
2361
2362 /*
2363  * Generic driver/device identify functions.  These will install a device
2364  * rendezvous point under the parent using the same name as the driver
2365  * name, which will at a later time be probed and attached.
2366  *
2367  * These functions are used when the parent does not 'scan' its bus for
2368  * matching devices, or for the particular devices using these functions,
2369  * or when the device is a pseudo or synthesized device (such as can be
2370  * found under firewire and ppbus).
2371  */
2372 int
2373 bus_generic_identify(driver_t *driver, device_t parent)
2374 {
2375         if (parent->state == DS_ATTACHED)
2376                 return (0);
2377         BUS_ADD_CHILD(parent, parent, 0, driver->name, -1);
2378         return (0);
2379 }
2380
2381 int
2382 bus_generic_identify_sameunit(driver_t *driver, device_t parent)
2383 {
2384         if (parent->state == DS_ATTACHED)
2385                 return (0);
2386         BUS_ADD_CHILD(parent, parent, 0, driver->name, device_get_unit(parent));
2387         return (0);
2388 }
2389
2390 /*
2391  * Call DEVICE_IDENTIFY for each driver.
2392  */
2393 int
2394 bus_generic_probe(device_t dev)
2395 {
2396         devclass_t dc = dev->devclass;
2397         driverlink_t dl;
2398
2399         TAILQ_FOREACH(dl, &dc->drivers, link) {
2400                 DEVICE_IDENTIFY(dl->driver, dev);
2401         }
2402
2403         return(0);
2404 }
2405
2406 /*
2407  * This is an aweful hack due to the isa bus and autoconf code not
2408  * probing the ISA devices until after everything else has configured.
2409  * The ISA bus did a dummy attach long ago so we have to set it back
2410  * to an earlier state so the probe thinks its the initial probe and
2411  * not a bus rescan.
2412  *
2413  * XXX remove by properly defering the ISA bus scan.
2414  */
2415 int
2416 bus_generic_probe_hack(device_t dev)
2417 {
2418         if (dev->state == DS_ATTACHED) {
2419                 dev->state = DS_ALIVE;
2420                 bus_generic_probe(dev);
2421                 dev->state = DS_ATTACHED;
2422         }
2423         return (0);
2424 }
2425
2426 int
2427 bus_generic_attach(device_t dev)
2428 {
2429         device_t child;
2430
2431         TAILQ_FOREACH(child, &dev->children, link) {
2432                 device_probe_and_attach(child);
2433         }
2434
2435         return(0);
2436 }
2437
2438 int
2439 bus_generic_detach(device_t dev)
2440 {
2441         device_t child;
2442         int error;
2443
2444         if (dev->state != DS_ATTACHED)
2445                 return(EBUSY);
2446
2447         TAILQ_FOREACH(child, &dev->children, link)
2448                 if ((error = device_detach(child)) != 0)
2449                         return(error);
2450
2451         return 0;
2452 }
2453
2454 int
2455 bus_generic_shutdown(device_t dev)
2456 {
2457         device_t child;
2458
2459         TAILQ_FOREACH(child, &dev->children, link)
2460                 device_shutdown(child);
2461
2462         return(0);
2463 }
2464
2465 int
2466 bus_generic_suspend(device_t dev)
2467 {
2468         int error;
2469         device_t child, child2;
2470
2471         TAILQ_FOREACH(child, &dev->children, link) {
2472                 error = DEVICE_SUSPEND(child);
2473                 if (error) {
2474                         for (child2 = TAILQ_FIRST(&dev->children);
2475                              child2 && child2 != child; 
2476                              child2 = TAILQ_NEXT(child2, link))
2477                                 DEVICE_RESUME(child2);
2478                         return(error);
2479                 }
2480         }
2481         return(0);
2482 }
2483
2484 int
2485 bus_generic_resume(device_t dev)
2486 {
2487         device_t child;
2488
2489         TAILQ_FOREACH(child, &dev->children, link)
2490                 DEVICE_RESUME(child);
2491                 /* if resume fails, there's nothing we can usefully do... */
2492
2493         return(0);
2494 }
2495
2496 int
2497 bus_print_child_header(device_t dev, device_t child)
2498 {
2499         int retval = 0;
2500
2501         if (device_get_desc(child))
2502                 retval += device_printf(child, "<%s>", device_get_desc(child));
2503         else
2504                 retval += kprintf("%s", device_get_nameunit(child));
2505         if (bootverbose) {
2506                 if (child->state != DS_ATTACHED)
2507                         kprintf(" [tentative]");
2508                 else
2509                         kprintf(" [attached!]");
2510         }
2511         return(retval);
2512 }
2513
2514 int
2515 bus_print_child_footer(device_t dev, device_t child)
2516 {
2517         return(kprintf(" on %s\n", device_get_nameunit(dev)));
2518 }
2519
2520 device_t
2521 bus_generic_add_child(device_t dev, device_t child, int order,
2522                       const char *name, int unit)
2523 {
2524         if (dev->parent)
2525                 dev = BUS_ADD_CHILD(dev->parent, child, order, name, unit);
2526         else
2527                 dev = device_add_child_ordered(child, order, name, unit);
2528         return(dev);
2529                 
2530 }
2531
2532 int
2533 bus_generic_print_child(device_t dev, device_t child)
2534 {
2535         int retval = 0;
2536
2537         retval += bus_print_child_header(dev, child);
2538         retval += bus_print_child_footer(dev, child);
2539
2540         return(retval);
2541 }
2542
2543 int
2544 bus_generic_read_ivar(device_t dev, device_t child, int index, 
2545                       uintptr_t * result)
2546 {
2547         int error;
2548
2549         if (dev->parent)
2550                 error = BUS_READ_IVAR(dev->parent, child, index, result);
2551         else
2552                 error = ENOENT;
2553         return (error);
2554 }
2555
2556 int
2557 bus_generic_write_ivar(device_t dev, device_t child, int index, 
2558                        uintptr_t value)
2559 {
2560         int error;
2561
2562         if (dev->parent)
2563                 error = BUS_WRITE_IVAR(dev->parent, child, index, value);
2564         else
2565                 error = ENOENT;
2566         return (error);
2567 }
2568
2569 /*
2570  * Resource list are used for iterations, do not recurse.
2571  */
2572 struct resource_list *
2573 bus_generic_get_resource_list(device_t dev, device_t child)
2574 {
2575         return (NULL);
2576 }
2577
2578 void
2579 bus_generic_driver_added(device_t dev, driver_t *driver)
2580 {
2581         device_t child;
2582
2583         DEVICE_IDENTIFY(driver, dev);
2584         TAILQ_FOREACH(child, &dev->children, link) {
2585                 if (child->state == DS_NOTPRESENT)
2586                         device_probe_and_attach(child);
2587         }
2588 }
2589
2590 int
2591 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
2592     int flags, driver_intr_t *intr, void *arg, void **cookiep,
2593     lwkt_serialize_t serializer, const char *desc)
2594 {
2595         /* Propagate up the bus hierarchy until someone handles it. */
2596         if (dev->parent) {
2597                 return BUS_SETUP_INTR(dev->parent, child, irq, flags,
2598                     intr, arg, cookiep, serializer, desc);
2599         } else {
2600                 return EINVAL;
2601         }
2602 }
2603
2604 int
2605 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
2606                           void *cookie)
2607 {
2608         /* Propagate up the bus hierarchy until someone handles it. */
2609         if (dev->parent)
2610                 return(BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
2611         else
2612                 return(EINVAL);
2613 }
2614
2615 int
2616 bus_generic_disable_intr(device_t dev, device_t child, void *cookie)
2617 {
2618         if (dev->parent)
2619                 return(BUS_DISABLE_INTR(dev->parent, child, cookie));
2620         else
2621                 return(0);
2622 }
2623
2624 void
2625 bus_generic_enable_intr(device_t dev, device_t child, void *cookie)
2626 {
2627         if (dev->parent)
2628                 BUS_ENABLE_INTR(dev->parent, child, cookie);
2629 }
2630
2631 int
2632 bus_generic_config_intr(device_t dev, device_t child, int irq, enum intr_trigger trig,
2633     enum intr_polarity pol)
2634 {
2635         /* Propagate up the bus hierarchy until someone handles it. */
2636         if (dev->parent)
2637                 return(BUS_CONFIG_INTR(dev->parent, child, irq, trig, pol));
2638         else
2639                 return(EINVAL);
2640 }
2641
2642 struct resource *
2643 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
2644     u_long start, u_long end, u_long count, u_int flags, int cpuid)
2645 {
2646         /* Propagate up the bus hierarchy until someone handles it. */
2647         if (dev->parent)
2648                 return(BUS_ALLOC_RESOURCE(dev->parent, child, type, rid, 
2649                                            start, end, count, flags, cpuid));
2650         else
2651                 return(NULL);
2652 }
2653
2654 int
2655 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
2656                              struct resource *r)
2657 {
2658         /* Propagate up the bus hierarchy until someone handles it. */
2659         if (dev->parent)
2660                 return(BUS_RELEASE_RESOURCE(dev->parent, child, type, rid, r));
2661         else
2662                 return(EINVAL);
2663 }
2664
2665 int
2666 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
2667                               struct resource *r)
2668 {
2669         /* Propagate up the bus hierarchy until someone handles it. */
2670         if (dev->parent)
2671                 return(BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid, r));
2672         else
2673                 return(EINVAL);
2674 }
2675
2676 int
2677 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
2678                                 int rid, struct resource *r)
2679 {
2680         /* Propagate up the bus hierarchy until someone handles it. */
2681         if (dev->parent)
2682                 return(BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
2683                                                r));
2684         else
2685                 return(EINVAL);
2686 }
2687
2688 int
2689 bus_generic_get_resource(device_t dev, device_t child, int type, int rid,
2690                          u_long *startp, u_long *countp)
2691 {
2692         int error;
2693
2694         error = ENOENT;
2695         if (dev->parent) {
2696                 error = BUS_GET_RESOURCE(dev->parent, child, type, rid, 
2697                                          startp, countp);
2698         }
2699         return (error);
2700 }
2701
2702 int
2703 bus_generic_set_resource(device_t dev, device_t child, int type, int rid,
2704                         u_long start, u_long count, int cpuid)
2705 {
2706         int error;
2707
2708         error = EINVAL;
2709         if (dev->parent) {
2710                 error = BUS_SET_RESOURCE(dev->parent, child, type, rid, 
2711                                          start, count, cpuid);
2712         }
2713         return (error);
2714 }
2715
2716 void
2717 bus_generic_delete_resource(device_t dev, device_t child, int type, int rid)
2718 {
2719         if (dev->parent)
2720                 BUS_DELETE_RESOURCE(dev, child, type, rid);
2721 }
2722
2723 /**
2724  * @brief Helper function for implementing BUS_GET_DMA_TAG().
2725  *
2726  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
2727  * BUS_GET_DMA_TAG() method of the parent of @p dev.
2728  */
2729 bus_dma_tag_t
2730 bus_generic_get_dma_tag(device_t dev, device_t child)
2731 {
2732
2733         /* Propagate up the bus hierarchy until someone handles it. */
2734         if (dev->parent != NULL)
2735                 return (BUS_GET_DMA_TAG(dev->parent, child));
2736         return (NULL);
2737 }
2738
2739 int
2740 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
2741     u_long *startp, u_long *countp)
2742 {
2743         struct resource_list *rl = NULL;
2744         struct resource_list_entry *rle = NULL;
2745
2746         rl = BUS_GET_RESOURCE_LIST(dev, child);
2747         if (!rl)
2748                 return(EINVAL);
2749
2750         rle = resource_list_find(rl, type, rid);
2751         if (!rle)
2752                 return(ENOENT);
2753
2754         if (startp)
2755                 *startp = rle->start;
2756         if (countp)
2757                 *countp = rle->count;
2758
2759         return(0);
2760 }
2761
2762 int
2763 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
2764     u_long start, u_long count, int cpuid)
2765 {
2766         struct resource_list *rl = NULL;
2767
2768         rl = BUS_GET_RESOURCE_LIST(dev, child);
2769         if (!rl)
2770                 return(EINVAL);
2771
2772         resource_list_add(rl, type, rid, start, (start + count - 1), count,
2773             cpuid);
2774
2775         return(0);
2776 }
2777
2778 void
2779 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
2780 {
2781         struct resource_list *rl = NULL;
2782
2783         rl = BUS_GET_RESOURCE_LIST(dev, child);
2784         if (!rl)
2785                 return;
2786
2787         resource_list_delete(rl, type, rid);
2788 }
2789
2790 int
2791 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
2792     int rid, struct resource *r)
2793 {
2794         struct resource_list *rl = NULL;
2795
2796         rl = BUS_GET_RESOURCE_LIST(dev, child);
2797         if (!rl)
2798                 return(EINVAL);
2799
2800         return(resource_list_release(rl, dev, child, type, rid, r));
2801 }
2802
2803 struct resource *
2804 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
2805     int *rid, u_long start, u_long end, u_long count, u_int flags, int cpuid)
2806 {
2807         struct resource_list *rl = NULL;
2808
2809         rl = BUS_GET_RESOURCE_LIST(dev, child);
2810         if (!rl)
2811                 return(NULL);
2812
2813         return(resource_list_alloc(rl, dev, child, type, rid,
2814             start, end, count, flags, cpuid));
2815 }
2816
2817 int
2818 bus_generic_child_present(device_t bus, device_t child)
2819 {
2820         return(BUS_CHILD_PRESENT(device_get_parent(bus), bus));
2821 }
2822
2823
2824 /*
2825  * Some convenience functions to make it easier for drivers to use the
2826  * resource-management functions.  All these really do is hide the
2827  * indirection through the parent's method table, making for slightly
2828  * less-wordy code.  In the future, it might make sense for this code
2829  * to maintain some sort of a list of resources allocated by each device.
2830  */
2831 int
2832 bus_alloc_resources(device_t dev, struct resource_spec *rs,
2833     struct resource **res)
2834 {
2835         int i;
2836
2837         for (i = 0; rs[i].type != -1; i++)
2838                 res[i] = NULL;
2839         for (i = 0; rs[i].type != -1; i++) {
2840                 res[i] = bus_alloc_resource_any(dev,
2841                     rs[i].type, &rs[i].rid, rs[i].flags);
2842                 if (res[i] == NULL) {
2843                         bus_release_resources(dev, rs, res);
2844                         return (ENXIO);
2845                 }
2846         }
2847         return (0);
2848 }
2849
2850 void
2851 bus_release_resources(device_t dev, const struct resource_spec *rs,
2852     struct resource **res)
2853 {
2854         int i;
2855
2856         for (i = 0; rs[i].type != -1; i++)
2857                 if (res[i] != NULL) {
2858                         bus_release_resource(
2859                             dev, rs[i].type, rs[i].rid, res[i]);
2860                         res[i] = NULL;
2861                 }
2862 }
2863
2864 struct resource *
2865 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
2866                    u_long count, u_int flags)
2867 {
2868         if (dev->parent == NULL)
2869                 return(0);
2870         return(BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
2871                                   count, flags, -1));
2872 }
2873
2874 struct resource *
2875 bus_alloc_legacy_irq_resource(device_t dev, int *rid, u_long irq, u_int flags)
2876 {
2877         if (dev->parent == NULL)
2878                 return(0);
2879         return BUS_ALLOC_RESOURCE(dev->parent, dev, SYS_RES_IRQ, rid,
2880             irq, irq, 1, flags, machintr_legacy_intr_cpuid(irq));
2881 }
2882
2883 int
2884 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
2885 {
2886         if (dev->parent == NULL)
2887                 return(EINVAL);
2888         return(BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2889 }
2890
2891 int
2892 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
2893 {
2894         if (dev->parent == NULL)
2895                 return(EINVAL);
2896         return(BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
2897 }
2898
2899 int
2900 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
2901 {
2902         if (dev->parent == NULL)
2903                 return(EINVAL);
2904         return(BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
2905 }
2906
2907 int
2908 bus_setup_intr_descr(device_t dev, struct resource *r, int flags,
2909     driver_intr_t handler, void *arg, void **cookiep,
2910     lwkt_serialize_t serializer, const char *desc)
2911 {
2912         if (dev->parent == NULL)
2913                 return EINVAL;
2914         return BUS_SETUP_INTR(dev->parent, dev, r, flags, handler, arg,
2915             cookiep, serializer, desc);
2916 }
2917
2918 int
2919 bus_setup_intr(device_t dev, struct resource *r, int flags,
2920     driver_intr_t handler, void *arg, void **cookiep,
2921     lwkt_serialize_t serializer)
2922 {
2923         return bus_setup_intr_descr(dev, r, flags, handler, arg, cookiep,
2924             serializer, NULL);
2925 }
2926
2927 int
2928 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
2929 {
2930         if (dev->parent == NULL)
2931                 return(EINVAL);
2932         return(BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
2933 }
2934
2935 void
2936 bus_enable_intr(device_t dev, void *cookie)
2937 {
2938         if (dev->parent)
2939                 BUS_ENABLE_INTR(dev->parent, dev, cookie);
2940 }
2941
2942 int
2943 bus_disable_intr(device_t dev, void *cookie)
2944 {
2945         if (dev->parent)
2946                 return(BUS_DISABLE_INTR(dev->parent, dev, cookie));
2947         else
2948                 return(0);
2949 }
2950
2951 int
2952 bus_set_resource(device_t dev, int type, int rid,
2953                  u_long start, u_long count, int cpuid)
2954 {
2955         return(BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
2956                                 start, count, cpuid));
2957 }
2958
2959 int
2960 bus_get_resource(device_t dev, int type, int rid,
2961                  u_long *startp, u_long *countp)
2962 {
2963         return(BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2964                                 startp, countp));
2965 }
2966
2967 u_long
2968 bus_get_resource_start(device_t dev, int type, int rid)
2969 {
2970         u_long start, count;
2971         int error;
2972
2973         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2974                                  &start, &count);
2975         if (error)
2976                 return(0);
2977         return(start);
2978 }
2979
2980 u_long
2981 bus_get_resource_count(device_t dev, int type, int rid)
2982 {
2983         u_long start, count;
2984         int error;
2985
2986         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
2987                                  &start, &count);
2988         if (error)
2989                 return(0);
2990         return(count);
2991 }
2992
2993 void
2994 bus_delete_resource(device_t dev, int type, int rid)
2995 {
2996         BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
2997 }
2998
2999 int
3000 bus_child_present(device_t child)
3001 {
3002         return (BUS_CHILD_PRESENT(device_get_parent(child), child));
3003 }
3004
3005 int
3006 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
3007 {
3008         device_t parent;
3009
3010         parent = device_get_parent(child);
3011         if (parent == NULL) {
3012                 *buf = '\0';
3013                 return (0);
3014         }
3015         return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
3016 }
3017
3018 int
3019 bus_child_location_str(device_t child, char *buf, size_t buflen)
3020 {
3021         device_t parent;
3022
3023         parent = device_get_parent(child);
3024         if (parent == NULL) {
3025                 *buf = '\0';
3026                 return (0);
3027         }
3028         return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
3029 }
3030
3031 /**
3032  * @brief Wrapper function for BUS_GET_DMA_TAG().
3033  *
3034  * This function simply calls the BUS_GET_DMA_TAG() method of the
3035  * parent of @p dev.
3036  */
3037 bus_dma_tag_t
3038 bus_get_dma_tag(device_t dev)
3039 {
3040         device_t parent;
3041
3042         parent = device_get_parent(dev);
3043         if (parent == NULL)
3044                 return (NULL);
3045         return (BUS_GET_DMA_TAG(parent, dev));
3046 }
3047
3048 static int
3049 root_print_child(device_t dev, device_t child)
3050 {
3051         return(0);
3052 }
3053
3054 static int
3055 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
3056                 void **cookiep, lwkt_serialize_t serializer, const char *desc)
3057 {
3058         /*
3059          * If an interrupt mapping gets to here something bad has happened.
3060          */
3061         panic("root_setup_intr");
3062 }
3063
3064 /*
3065  * If we get here, assume that the device is permanant and really is
3066  * present in the system.  Removable bus drivers are expected to intercept
3067  * this call long before it gets here.  We return -1 so that drivers that
3068  * really care can check vs -1 or some ERRNO returned higher in the food
3069  * chain.
3070  */
3071 static int
3072 root_child_present(device_t dev, device_t child)
3073 {
3074         return(-1);
3075 }
3076
3077 /*
3078  * XXX NOTE! other defaults may be set in bus_if.m
3079  */
3080 static kobj_method_t root_methods[] = {
3081         /* Device interface */
3082         KOBJMETHOD(device_shutdown,     bus_generic_shutdown),
3083         KOBJMETHOD(device_suspend,      bus_generic_suspend),
3084         KOBJMETHOD(device_resume,       bus_generic_resume),
3085
3086         /* Bus interface */
3087         KOBJMETHOD(bus_add_child,       bus_generic_add_child),
3088         KOBJMETHOD(bus_print_child,     root_print_child),
3089         KOBJMETHOD(bus_read_ivar,       bus_generic_read_ivar),
3090         KOBJMETHOD(bus_write_ivar,      bus_generic_write_ivar),
3091         KOBJMETHOD(bus_setup_intr,      root_setup_intr),
3092         KOBJMETHOD(bus_child_present,   root_child_present),
3093
3094         KOBJMETHOD_END
3095 };
3096
3097 static driver_t root_driver = {
3098         "root",
3099         root_methods,
3100         1,                      /* no softc */
3101 };
3102
3103 device_t        root_bus;
3104 devclass_t      root_devclass;
3105
3106 static int
3107 root_bus_module_handler(module_t mod, int what, void* arg)
3108 {
3109         switch (what) {
3110         case MOD_LOAD:
3111                 TAILQ_INIT(&bus_data_devices);
3112                 root_bus = make_device(NULL, "root", 0);
3113                 root_bus->desc = "System root bus";
3114                 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
3115                 root_bus->driver = &root_driver;
3116                 root_bus->state = DS_ALIVE;
3117                 root_devclass = devclass_find_internal("root", NULL, FALSE);
3118                 devinit();
3119                 return(0);
3120
3121         case MOD_SHUTDOWN:
3122                 device_shutdown(root_bus);
3123                 return(0);
3124         default:
3125                 return(0);
3126         }
3127 }
3128
3129 static moduledata_t root_bus_mod = {
3130         "rootbus",
3131         root_bus_module_handler,
3132         0
3133 };
3134 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
3135
3136 void
3137 root_bus_configure(void)
3138 {
3139         int warncount;
3140         device_t dev;
3141
3142         PDEBUG(("."));
3143
3144         /*
3145          * handle device_identify based device attachments to the root_bus
3146          * (typically nexus).
3147          */
3148         bus_generic_probe(root_bus);
3149
3150         /*
3151          * Probe and attach the devices under root_bus.
3152          */
3153         TAILQ_FOREACH(dev, &root_bus->children, link) {
3154                 device_probe_and_attach(dev);
3155         }
3156
3157         /*
3158          * Wait for all asynchronous attaches to complete.  If we don't
3159          * our legacy ISA bus scan could steal device unit numbers or
3160          * even I/O ports.
3161          */
3162         warncount = 10;
3163         if (numasyncthreads)
3164                 kprintf("Waiting for async drivers to attach\n");
3165         while (numasyncthreads > 0) {
3166                 if (tsleep(&numasyncthreads, 0, "rootbus", hz) == EWOULDBLOCK)
3167                         --warncount;
3168                 if (warncount == 0) {
3169                         kprintf("Warning: Still waiting for %d "
3170                                 "drivers to attach\n", numasyncthreads);
3171                 } else if (warncount == -30) {
3172                         kprintf("Giving up on %d drivers\n", numasyncthreads);
3173                         break;
3174                 }
3175         }
3176         root_bus->state = DS_ATTACHED;
3177 }
3178
3179 int
3180 driver_module_handler(module_t mod, int what, void *arg)
3181 {
3182         int error;
3183         struct driver_module_data *dmd;
3184         devclass_t bus_devclass;
3185         kobj_class_t driver;
3186         const char *parentname;
3187
3188         dmd = (struct driver_module_data *)arg;
3189         bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
3190         error = 0;
3191
3192         switch (what) {
3193         case MOD_LOAD:
3194                 if (dmd->dmd_chainevh)
3195                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3196
3197                 driver = dmd->dmd_driver;
3198                 PDEBUG(("Loading module: driver %s on bus %s",
3199                         DRIVERNAME(driver), dmd->dmd_busname));
3200
3201                 /*
3202                  * If the driver has any base classes, make the
3203                  * devclass inherit from the devclass of the driver's
3204                  * first base class. This will allow the system to
3205                  * search for drivers in both devclasses for children
3206                  * of a device using this driver.
3207                  */
3208                 if (driver->baseclasses)
3209                         parentname = driver->baseclasses[0]->name;
3210                 else
3211                         parentname = NULL;
3212                 *dmd->dmd_devclass = devclass_find_internal(driver->name,
3213                                                             parentname, TRUE);
3214
3215                 error = devclass_add_driver(bus_devclass, driver);
3216                 if (error)
3217                         break;
3218                 break;
3219
3220         case MOD_UNLOAD:
3221                 PDEBUG(("Unloading module: driver %s from bus %s",
3222                         DRIVERNAME(dmd->dmd_driver), dmd->dmd_busname));
3223                 error = devclass_delete_driver(bus_devclass, dmd->dmd_driver);
3224
3225                 if (!error && dmd->dmd_chainevh)
3226                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
3227                 break;
3228         }
3229
3230         return (error);
3231 }
3232
3233 #ifdef BUS_DEBUG
3234
3235 /*
3236  * The _short versions avoid iteration by not calling anything that prints
3237  * more than oneliners. I love oneliners.
3238  */
3239
3240 static void
3241 print_device_short(device_t dev, int indent)
3242 {
3243         if (!dev)
3244                 return;
3245
3246         indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
3247                       dev->unit, dev->desc,
3248                       (dev->parent? "":"no "),
3249                       (TAILQ_EMPTY(&dev->children)? "no ":""),
3250                       (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
3251                       (dev->flags&DF_FIXEDCLASS? "fixed,":""),
3252                       (dev->flags&DF_WILDCARD? "wildcard,":""),
3253                       (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
3254                       (dev->ivars? "":"no "),
3255                       (dev->softc? "":"no "),
3256                       dev->busy));
3257 }
3258
3259 static void
3260 print_device(device_t dev, int indent)
3261 {
3262         if (!dev)
3263                 return;
3264
3265         print_device_short(dev, indent);
3266
3267         indentprintf(("Parent:\n"));
3268         print_device_short(dev->parent, indent+1);
3269         indentprintf(("Driver:\n"));
3270         print_driver_short(dev->driver, indent+1);
3271         indentprintf(("Devclass:\n"));
3272         print_devclass_short(dev->devclass, indent+1);
3273 }
3274
3275 /*
3276  * Print the device and all its children (indented).
3277  */
3278 void
3279 print_device_tree_short(device_t dev, int indent)
3280 {
3281         device_t child;
3282
3283         if (!dev)
3284                 return;
3285
3286         print_device_short(dev, indent);
3287
3288         TAILQ_FOREACH(child, &dev->children, link)
3289                 print_device_tree_short(child, indent+1);
3290 }
3291
3292 /*
3293  * Print the device and all its children (indented).
3294  */
3295 void
3296 print_device_tree(device_t dev, int indent)
3297 {
3298         device_t child;
3299
3300         if (!dev)
3301                 return;
3302
3303         print_device(dev, indent);
3304
3305         TAILQ_FOREACH(child, &dev->children, link)
3306                 print_device_tree(child, indent+1);
3307 }
3308
3309 static void
3310 print_driver_short(driver_t *driver, int indent)
3311 {
3312         if (!driver)
3313                 return;
3314
3315         indentprintf(("driver %s: softc size = %zu\n",
3316                       driver->name, driver->size));
3317 }
3318
3319 static void
3320 print_driver(driver_t *driver, int indent)
3321 {
3322         if (!driver)
3323                 return;
3324
3325         print_driver_short(driver, indent);
3326 }
3327
3328
3329 static void
3330 print_driver_list(driver_list_t drivers, int indent)
3331 {
3332         driverlink_t driver;
3333
3334         TAILQ_FOREACH(driver, &drivers, link)
3335                 print_driver(driver->driver, indent);
3336 }
3337
3338 static void
3339 print_devclass_short(devclass_t dc, int indent)
3340 {
3341         if (!dc)
3342                 return;
3343
3344         indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
3345 }
3346
3347 static void
3348 print_devclass(devclass_t dc, int indent)
3349 {
3350         int i;
3351
3352         if (!dc)
3353                 return;
3354
3355         print_devclass_short(dc, indent);
3356         indentprintf(("Drivers:\n"));
3357         print_driver_list(dc->drivers, indent+1);
3358
3359         indentprintf(("Devices:\n"));
3360         for (i = 0; i < dc->maxunit; i++)
3361                 if (dc->devices[i])
3362                         print_device(dc->devices[i], indent+1);
3363 }
3364
3365 void
3366 print_devclass_list_short(void)
3367 {
3368         devclass_t dc;
3369
3370         kprintf("Short listing of devclasses, drivers & devices:\n");
3371         TAILQ_FOREACH(dc, &devclasses, link) {
3372                 print_devclass_short(dc, 0);
3373         }
3374 }
3375
3376 void
3377 print_devclass_list(void)
3378 {
3379         devclass_t dc;
3380
3381         kprintf("Full listing of devclasses, drivers & devices:\n");
3382         TAILQ_FOREACH(dc, &devclasses, link) {
3383                 print_devclass(dc, 0);
3384         }
3385 }
3386
3387 #endif
3388
3389 /*
3390  * Check to see if a device is disabled via a disabled hint.
3391  */
3392 int
3393 resource_disabled(const char *name, int unit)
3394 {
3395         int error, value;
3396
3397         error = resource_int_value(name, unit, "disabled", &value);
3398         if (error)
3399                return(0);
3400         return(value);
3401 }
3402
3403 /*
3404  * User-space access to the device tree.
3405  *
3406  * We implement a small set of nodes:
3407  *
3408  * hw.bus                       Single integer read method to obtain the
3409  *                              current generation count.
3410  * hw.bus.devices               Reads the entire device tree in flat space.
3411  * hw.bus.rman                  Resource manager interface
3412  *
3413  * We might like to add the ability to scan devclasses and/or drivers to
3414  * determine what else is currently loaded/available.
3415  */
3416
3417 static int
3418 sysctl_bus(SYSCTL_HANDLER_ARGS)
3419 {
3420         struct u_businfo        ubus;
3421
3422         ubus.ub_version = BUS_USER_VERSION;
3423         ubus.ub_generation = bus_data_generation;
3424
3425         return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
3426 }
3427 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
3428     "bus-related data");
3429
3430 static int
3431 sysctl_devices(SYSCTL_HANDLER_ARGS)
3432 {
3433         int                     *name = (int *)arg1;
3434         u_int                   namelen = arg2;
3435         int                     index;
3436         struct device           *dev;
3437         struct u_device         udev;   /* XXX this is a bit big */
3438         int                     error;
3439
3440         if (namelen != 2)
3441                 return (EINVAL);
3442
3443         if (bus_data_generation_check(name[0]))
3444                 return (EINVAL);
3445
3446         index = name[1];
3447
3448         /*
3449          * Scan the list of devices, looking for the requested index.
3450          */
3451         TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
3452                 if (index-- == 0)
3453                         break;
3454         }
3455         if (dev == NULL)
3456                 return (ENOENT);
3457
3458         /*
3459          * Populate the return array.
3460          */
3461         bzero(&udev, sizeof(udev));
3462         udev.dv_handle = (uintptr_t)dev;
3463         udev.dv_parent = (uintptr_t)dev->parent;
3464         if (dev->nameunit != NULL)
3465                 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
3466         if (dev->desc != NULL)
3467                 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
3468         if (dev->driver != NULL && dev->driver->name != NULL)
3469                 strlcpy(udev.dv_drivername, dev->driver->name,
3470                     sizeof(udev.dv_drivername));
3471         bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
3472         bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
3473         udev.dv_devflags = dev->devflags;
3474         udev.dv_flags = dev->flags;
3475         udev.dv_state = dev->state;
3476         error = SYSCTL_OUT(req, &udev, sizeof(udev));
3477         return (error);
3478 }
3479
3480 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
3481     "system device tree");
3482
3483 int
3484 bus_data_generation_check(int generation)
3485 {
3486         if (generation != bus_data_generation)
3487                 return (1);
3488
3489         /* XXX generate optimised lists here? */
3490         return (0);
3491 }
3492
3493 void
3494 bus_data_generation_update(void)
3495 {
3496         bus_data_generation++;
3497 }
3498
3499 const char *
3500 intr_str_polarity(enum intr_polarity pola)
3501 {
3502         switch (pola) {
3503         case INTR_POLARITY_LOW:
3504                 return "low";
3505
3506         case INTR_POLARITY_HIGH:
3507                 return "high";
3508
3509         case INTR_POLARITY_CONFORM:
3510                 return "conform";
3511         }
3512         return "unknown";
3513 }
3514
3515 const char *
3516 intr_str_trigger(enum intr_trigger trig)
3517 {
3518         switch (trig) {
3519         case INTR_TRIGGER_EDGE:
3520                 return "edge";
3521
3522         case INTR_TRIGGER_LEVEL:
3523                 return "level";
3524
3525         case INTR_TRIGGER_CONFORM:
3526                 return "conform";
3527         }
3528         return "unknown";
3529 }
3530
3531 int
3532 device_getenv_int(device_t dev, const char *knob, int def)
3533 {
3534         char env[128];
3535
3536         ksnprintf(env, sizeof(env), "hw.%s.%s", device_get_nameunit(dev), knob);
3537         kgetenv_int(env, &def);
3538         return def;
3539 }