The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_bus.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1997,1998,2003 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 
   27 #include <sys/cdefs.h>
   28 __FBSDID("$FreeBSD: releng/8.2/sys/kern/subr_bus.c 212894 2010-09-20 11:15:23Z avg $");
   29 
   30 #include "opt_bus.h"
   31 
   32 #include <sys/param.h>
   33 #include <sys/conf.h>
   34 #include <sys/filio.h>
   35 #include <sys/lock.h>
   36 #include <sys/kernel.h>
   37 #include <sys/kobj.h>
   38 #include <sys/limits.h>
   39 #include <sys/malloc.h>
   40 #include <sys/module.h>
   41 #include <sys/mutex.h>
   42 #include <sys/poll.h>
   43 #include <sys/proc.h>
   44 #include <sys/condvar.h>
   45 #include <sys/queue.h>
   46 #include <machine/bus.h>
   47 #include <sys/rman.h>
   48 #include <sys/selinfo.h>
   49 #include <sys/signalvar.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/systm.h>
   52 #include <sys/uio.h>
   53 #include <sys/bus.h>
   54 #include <sys/interrupt.h>
   55 
   56 #include <machine/stdarg.h>
   57 
   58 #include <vm/uma.h>
   59 
   60 SYSCTL_NODE(_hw, OID_AUTO, bus, CTLFLAG_RW, NULL, NULL);
   61 SYSCTL_NODE(, OID_AUTO, dev, CTLFLAG_RW, NULL, NULL);
   62 
   63 /*
   64  * Used to attach drivers to devclasses.
   65  */
   66 typedef struct driverlink *driverlink_t;
   67 struct driverlink {
   68         kobj_class_t    driver;
   69         TAILQ_ENTRY(driverlink) link;   /* list of drivers in devclass */
   70         int             pass;
   71         TAILQ_ENTRY(driverlink) passlink;
   72 };
   73 
   74 /*
   75  * Forward declarations
   76  */
   77 typedef TAILQ_HEAD(devclass_list, devclass) devclass_list_t;
   78 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
   79 typedef TAILQ_HEAD(device_list, device) device_list_t;
   80 
   81 struct devclass {
   82         TAILQ_ENTRY(devclass) link;
   83         devclass_t      parent;         /* parent in devclass hierarchy */
   84         driver_list_t   drivers;     /* bus devclasses store drivers for bus */
   85         char            *name;
   86         device_t        *devices;       /* array of devices indexed by unit */
   87         int             maxunit;        /* size of devices array */
   88         int             flags;
   89 #define DC_HAS_CHILDREN         1
   90 
   91         struct sysctl_ctx_list sysctl_ctx;
   92         struct sysctl_oid *sysctl_tree;
   93 };
   94 
   95 /**
   96  * @brief Implementation of device.
   97  */
   98 struct device {
   99         /*
  100          * A device is a kernel object. The first field must be the
  101          * current ops table for the object.
  102          */
  103         KOBJ_FIELDS;
  104 
  105         /*
  106          * Device hierarchy.
  107          */
  108         TAILQ_ENTRY(device)     link;   /**< list of devices in parent */
  109         TAILQ_ENTRY(device)     devlink; /**< global device list membership */
  110         device_t        parent;         /**< parent of this device  */
  111         device_list_t   children;       /**< list of child devices */
  112 
  113         /*
  114          * Details of this device.
  115          */
  116         driver_t        *driver;        /**< current driver */
  117         devclass_t      devclass;       /**< current device class */
  118         int             unit;           /**< current unit number */
  119         char*           nameunit;       /**< name+unit e.g. foodev0 */
  120         char*           desc;           /**< driver specific description */
  121         int             busy;           /**< count of calls to device_busy() */
  122         device_state_t  state;          /**< current device state  */
  123         u_int32_t       devflags;       /**< api level flags for device_get_flags() */
  124         u_int           flags;          /**< internal device flags  */
  125 #define DF_ENABLED      0x01            /* device should be probed/attached */
  126 #define DF_FIXEDCLASS   0x02            /* devclass specified at create time */
  127 #define DF_WILDCARD     0x04            /* unit was originally wildcard */
  128 #define DF_DESCMALLOCED 0x08            /* description was malloced */
  129 #define DF_QUIET        0x10            /* don't print verbose attach message */
  130 #define DF_DONENOMATCH  0x20            /* don't execute DEVICE_NOMATCH again */
  131 #define DF_EXTERNALSOFTC 0x40           /* softc not allocated by us */
  132 #define DF_REBID        0x80            /* Can rebid after attach */
  133         u_int   order;                  /**< order from device_add_child_ordered() */
  134         void    *ivars;                 /**< instance variables  */
  135         void    *softc;                 /**< current driver's variables  */
  136 
  137         struct sysctl_ctx_list sysctl_ctx; /**< state for sysctl variables  */
  138         struct sysctl_oid *sysctl_tree; /**< state for sysctl variables */
  139 };
  140 
  141 static MALLOC_DEFINE(M_BUS, "bus", "Bus data structures");
  142 static MALLOC_DEFINE(M_BUS_SC, "bus-sc", "Bus data structures, softc");
  143 
  144 #ifdef BUS_DEBUG
  145 
  146 static int bus_debug = 1;
  147 TUNABLE_INT("bus.debug", &bus_debug);
  148 SYSCTL_INT(_debug, OID_AUTO, bus_debug, CTLFLAG_RW, &bus_debug, 0,
  149     "Debug bus code");
  150 
  151 #define PDEBUG(a)       if (bus_debug) {printf("%s:%d: ", __func__, __LINE__), printf a; printf("\n");}
  152 #define DEVICENAME(d)   ((d)? device_get_name(d): "no device")
  153 #define DRIVERNAME(d)   ((d)? d->name : "no driver")
  154 #define DEVCLANAME(d)   ((d)? d->name : "no devclass")
  155 
  156 /**
  157  * Produce the indenting, indent*2 spaces plus a '.' ahead of that to
  158  * prevent syslog from deleting initial spaces
  159  */
  160 #define indentprintf(p) do { int iJ; printf("."); for (iJ=0; iJ<indent; iJ++) printf("  "); printf p ; } while (0)
  161 
  162 static void print_device_short(device_t dev, int indent);
  163 static void print_device(device_t dev, int indent);
  164 void print_device_tree_short(device_t dev, int indent);
  165 void print_device_tree(device_t dev, int indent);
  166 static void print_driver_short(driver_t *driver, int indent);
  167 static void print_driver(driver_t *driver, int indent);
  168 static void print_driver_list(driver_list_t drivers, int indent);
  169 static void print_devclass_short(devclass_t dc, int indent);
  170 static void print_devclass(devclass_t dc, int indent);
  171 void print_devclass_list_short(void);
  172 void print_devclass_list(void);
  173 
  174 #else
  175 /* Make the compiler ignore the function calls */
  176 #define PDEBUG(a)                       /* nop */
  177 #define DEVICENAME(d)                   /* nop */
  178 #define DRIVERNAME(d)                   /* nop */
  179 #define DEVCLANAME(d)                   /* nop */
  180 
  181 #define print_device_short(d,i)         /* nop */
  182 #define print_device(d,i)               /* nop */
  183 #define print_device_tree_short(d,i)    /* nop */
  184 #define print_device_tree(d,i)          /* nop */
  185 #define print_driver_short(d,i)         /* nop */
  186 #define print_driver(d,i)               /* nop */
  187 #define print_driver_list(d,i)          /* nop */
  188 #define print_devclass_short(d,i)       /* nop */
  189 #define print_devclass(d,i)             /* nop */
  190 #define print_devclass_list_short()     /* nop */
  191 #define print_devclass_list()           /* nop */
  192 #endif
  193 
  194 /*
  195  * dev sysctl tree
  196  */
  197 
  198 enum {
  199         DEVCLASS_SYSCTL_PARENT,
  200 };
  201 
  202 static int
  203 devclass_sysctl_handler(SYSCTL_HANDLER_ARGS)
  204 {
  205         devclass_t dc = (devclass_t)arg1;
  206         const char *value;
  207 
  208         switch (arg2) {
  209         case DEVCLASS_SYSCTL_PARENT:
  210                 value = dc->parent ? dc->parent->name : "";
  211                 break;
  212         default:
  213                 return (EINVAL);
  214         }
  215         return (SYSCTL_OUT(req, value, strlen(value)));
  216 }
  217 
  218 static void
  219 devclass_sysctl_init(devclass_t dc)
  220 {
  221 
  222         if (dc->sysctl_tree != NULL)
  223                 return;
  224         sysctl_ctx_init(&dc->sysctl_ctx);
  225         dc->sysctl_tree = SYSCTL_ADD_NODE(&dc->sysctl_ctx,
  226             SYSCTL_STATIC_CHILDREN(_dev), OID_AUTO, dc->name,
  227             CTLFLAG_RD, NULL, "");
  228         SYSCTL_ADD_PROC(&dc->sysctl_ctx, SYSCTL_CHILDREN(dc->sysctl_tree),
  229             OID_AUTO, "%parent", CTLFLAG_RD,
  230             dc, DEVCLASS_SYSCTL_PARENT, devclass_sysctl_handler, "A",
  231             "parent class");
  232 }
  233 
  234 enum {
  235         DEVICE_SYSCTL_DESC,
  236         DEVICE_SYSCTL_DRIVER,
  237         DEVICE_SYSCTL_LOCATION,
  238         DEVICE_SYSCTL_PNPINFO,
  239         DEVICE_SYSCTL_PARENT,
  240 };
  241 
  242 static int
  243 device_sysctl_handler(SYSCTL_HANDLER_ARGS)
  244 {
  245         device_t dev = (device_t)arg1;
  246         const char *value;
  247         char *buf;
  248         int error;
  249 
  250         buf = NULL;
  251         switch (arg2) {
  252         case DEVICE_SYSCTL_DESC:
  253                 value = dev->desc ? dev->desc : "";
  254                 break;
  255         case DEVICE_SYSCTL_DRIVER:
  256                 value = dev->driver ? dev->driver->name : "";
  257                 break;
  258         case DEVICE_SYSCTL_LOCATION:
  259                 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
  260                 bus_child_location_str(dev, buf, 1024);
  261                 break;
  262         case DEVICE_SYSCTL_PNPINFO:
  263                 value = buf = malloc(1024, M_BUS, M_WAITOK | M_ZERO);
  264                 bus_child_pnpinfo_str(dev, buf, 1024);
  265                 break;
  266         case DEVICE_SYSCTL_PARENT:
  267                 value = dev->parent ? dev->parent->nameunit : "";
  268                 break;
  269         default:
  270                 return (EINVAL);
  271         }
  272         error = SYSCTL_OUT(req, value, strlen(value));
  273         if (buf != NULL)
  274                 free(buf, M_BUS);
  275         return (error);
  276 }
  277 
  278 static void
  279 device_sysctl_init(device_t dev)
  280 {
  281         devclass_t dc = dev->devclass;
  282 
  283         if (dev->sysctl_tree != NULL)
  284                 return;
  285         devclass_sysctl_init(dc);
  286         sysctl_ctx_init(&dev->sysctl_ctx);
  287         dev->sysctl_tree = SYSCTL_ADD_NODE(&dev->sysctl_ctx,
  288             SYSCTL_CHILDREN(dc->sysctl_tree), OID_AUTO,
  289             dev->nameunit + strlen(dc->name),
  290             CTLFLAG_RD, NULL, "");
  291         SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
  292             OID_AUTO, "%desc", CTLFLAG_RD,
  293             dev, DEVICE_SYSCTL_DESC, device_sysctl_handler, "A",
  294             "device description");
  295         SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
  296             OID_AUTO, "%driver", CTLFLAG_RD,
  297             dev, DEVICE_SYSCTL_DRIVER, device_sysctl_handler, "A",
  298             "device driver name");
  299         SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
  300             OID_AUTO, "%location", CTLFLAG_RD,
  301             dev, DEVICE_SYSCTL_LOCATION, device_sysctl_handler, "A",
  302             "device location relative to parent");
  303         SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
  304             OID_AUTO, "%pnpinfo", CTLFLAG_RD,
  305             dev, DEVICE_SYSCTL_PNPINFO, device_sysctl_handler, "A",
  306             "device identification");
  307         SYSCTL_ADD_PROC(&dev->sysctl_ctx, SYSCTL_CHILDREN(dev->sysctl_tree),
  308             OID_AUTO, "%parent", CTLFLAG_RD,
  309             dev, DEVICE_SYSCTL_PARENT, device_sysctl_handler, "A",
  310             "parent device");
  311 }
  312 
  313 static void
  314 device_sysctl_update(device_t dev)
  315 {
  316         devclass_t dc = dev->devclass;
  317 
  318         if (dev->sysctl_tree == NULL)
  319                 return;
  320         sysctl_rename_oid(dev->sysctl_tree, dev->nameunit + strlen(dc->name));
  321 }
  322 
  323 static void
  324 device_sysctl_fini(device_t dev)
  325 {
  326         if (dev->sysctl_tree == NULL)
  327                 return;
  328         sysctl_ctx_free(&dev->sysctl_ctx);
  329         dev->sysctl_tree = NULL;
  330 }
  331 
  332 /*
  333  * /dev/devctl implementation
  334  */
  335 
  336 /*
  337  * This design allows only one reader for /dev/devctl.  This is not desirable
  338  * in the long run, but will get a lot of hair out of this implementation.
  339  * Maybe we should make this device a clonable device.
  340  *
  341  * Also note: we specifically do not attach a device to the device_t tree
  342  * to avoid potential chicken and egg problems.  One could argue that all
  343  * of this belongs to the root node.  One could also further argue that the
  344  * sysctl interface that we have not might more properly be an ioctl
  345  * interface, but at this stage of the game, I'm not inclined to rock that
  346  * boat.
  347  *
  348  * I'm also not sure that the SIGIO support is done correctly or not, as
  349  * I copied it from a driver that had SIGIO support that likely hasn't been
  350  * tested since 3.4 or 2.2.8!
  351  */
  352 
  353 /* Deprecated way to adjust queue length */
  354 static int sysctl_devctl_disable(SYSCTL_HANDLER_ARGS);
  355 /* XXX Need to support old-style tunable hw.bus.devctl_disable" */
  356 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_disable, CTLTYPE_INT | CTLFLAG_RW, NULL,
  357     0, sysctl_devctl_disable, "I", "devctl disable -- deprecated");
  358 
  359 #define DEVCTL_DEFAULT_QUEUE_LEN 1000
  360 static int sysctl_devctl_queue(SYSCTL_HANDLER_ARGS);
  361 static int devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
  362 TUNABLE_INT("hw.bus.devctl_queue", &devctl_queue_length);
  363 SYSCTL_PROC(_hw_bus, OID_AUTO, devctl_queue, CTLTYPE_INT | CTLFLAG_RW, NULL,
  364     0, sysctl_devctl_queue, "I", "devctl queue length");
  365 
  366 static d_open_t         devopen;
  367 static d_close_t        devclose;
  368 static d_read_t         devread;
  369 static d_ioctl_t        devioctl;
  370 static d_poll_t         devpoll;
  371 
  372 static struct cdevsw dev_cdevsw = {
  373         .d_version =    D_VERSION,
  374         .d_flags =      D_NEEDGIANT,
  375         .d_open =       devopen,
  376         .d_close =      devclose,
  377         .d_read =       devread,
  378         .d_ioctl =      devioctl,
  379         .d_poll =       devpoll,
  380         .d_name =       "devctl",
  381 };
  382 
  383 struct dev_event_info
  384 {
  385         char *dei_data;
  386         TAILQ_ENTRY(dev_event_info) dei_link;
  387 };
  388 
  389 TAILQ_HEAD(devq, dev_event_info);
  390 
  391 static struct dev_softc
  392 {
  393         int     inuse;
  394         int     nonblock;
  395         int     queued;
  396         struct mtx mtx;
  397         struct cv cv;
  398         struct selinfo sel;
  399         struct devq devq;
  400         struct proc *async_proc;
  401 } devsoftc;
  402 
  403 static struct cdev *devctl_dev;
  404 
  405 static void
  406 devinit(void)
  407 {
  408         devctl_dev = make_dev(&dev_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
  409             "devctl");
  410         mtx_init(&devsoftc.mtx, "dev mtx", "devd", MTX_DEF);
  411         cv_init(&devsoftc.cv, "dev cv");
  412         TAILQ_INIT(&devsoftc.devq);
  413 }
  414 
  415 static int
  416 devopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
  417 {
  418         if (devsoftc.inuse)
  419                 return (EBUSY);
  420         /* move to init */
  421         devsoftc.inuse = 1;
  422         devsoftc.nonblock = 0;
  423         devsoftc.async_proc = NULL;
  424         return (0);
  425 }
  426 
  427 static int
  428 devclose(struct cdev *dev, int fflag, int devtype, struct thread *td)
  429 {
  430         devsoftc.inuse = 0;
  431         mtx_lock(&devsoftc.mtx);
  432         cv_broadcast(&devsoftc.cv);
  433         mtx_unlock(&devsoftc.mtx);
  434         devsoftc.async_proc = NULL;
  435         return (0);
  436 }
  437 
  438 /*
  439  * The read channel for this device is used to report changes to
  440  * userland in realtime.  We are required to free the data as well as
  441  * the n1 object because we allocate them separately.  Also note that
  442  * we return one record at a time.  If you try to read this device a
  443  * character at a time, you will lose the rest of the data.  Listening
  444  * programs are expected to cope.
  445  */
  446 static int
  447 devread(struct cdev *dev, struct uio *uio, int ioflag)
  448 {
  449         struct dev_event_info *n1;
  450         int rv;
  451 
  452         mtx_lock(&devsoftc.mtx);
  453         while (TAILQ_EMPTY(&devsoftc.devq)) {
  454                 if (devsoftc.nonblock) {
  455                         mtx_unlock(&devsoftc.mtx);
  456                         return (EAGAIN);
  457                 }
  458                 rv = cv_wait_sig(&devsoftc.cv, &devsoftc.mtx);
  459                 if (rv) {
  460                         /*
  461                          * Need to translate ERESTART to EINTR here? -- jake
  462                          */
  463                         mtx_unlock(&devsoftc.mtx);
  464                         return (rv);
  465                 }
  466         }
  467         n1 = TAILQ_FIRST(&devsoftc.devq);
  468         TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
  469         devsoftc.queued--;
  470         mtx_unlock(&devsoftc.mtx);
  471         rv = uiomove(n1->dei_data, strlen(n1->dei_data), uio);
  472         free(n1->dei_data, M_BUS);
  473         free(n1, M_BUS);
  474         return (rv);
  475 }
  476 
  477 static  int
  478 devioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
  479 {
  480         switch (cmd) {
  481 
  482         case FIONBIO:
  483                 if (*(int*)data)
  484                         devsoftc.nonblock = 1;
  485                 else
  486                         devsoftc.nonblock = 0;
  487                 return (0);
  488         case FIOASYNC:
  489                 if (*(int*)data)
  490                         devsoftc.async_proc = td->td_proc;
  491                 else
  492                         devsoftc.async_proc = NULL;
  493                 return (0);
  494 
  495                 /* (un)Support for other fcntl() calls. */
  496         case FIOCLEX:
  497         case FIONCLEX:
  498         case FIONREAD:
  499         case FIOSETOWN:
  500         case FIOGETOWN:
  501         default:
  502                 break;
  503         }
  504         return (ENOTTY);
  505 }
  506 
  507 static  int
  508 devpoll(struct cdev *dev, int events, struct thread *td)
  509 {
  510         int     revents = 0;
  511 
  512         mtx_lock(&devsoftc.mtx);
  513         if (events & (POLLIN | POLLRDNORM)) {
  514                 if (!TAILQ_EMPTY(&devsoftc.devq))
  515                         revents = events & (POLLIN | POLLRDNORM);
  516                 else
  517                         selrecord(td, &devsoftc.sel);
  518         }
  519         mtx_unlock(&devsoftc.mtx);
  520 
  521         return (revents);
  522 }
  523 
  524 /**
  525  * @brief Return whether the userland process is running
  526  */
  527 boolean_t
  528 devctl_process_running(void)
  529 {
  530         return (devsoftc.inuse == 1);
  531 }
  532 
  533 /**
  534  * @brief Queue data to be read from the devctl device
  535  *
  536  * Generic interface to queue data to the devctl device.  It is
  537  * assumed that @p data is properly formatted.  It is further assumed
  538  * that @p data is allocated using the M_BUS malloc type.
  539  */
  540 void
  541 devctl_queue_data_f(char *data, int flags)
  542 {
  543         struct dev_event_info *n1 = NULL, *n2 = NULL;
  544         struct proc *p;
  545 
  546         if (strlen(data) == 0)
  547                 goto out;
  548         if (devctl_queue_length == 0)
  549                 goto out;
  550         n1 = malloc(sizeof(*n1), M_BUS, flags);
  551         if (n1 == NULL)
  552                 goto out;
  553         n1->dei_data = data;
  554         mtx_lock(&devsoftc.mtx);
  555         if (devctl_queue_length == 0) {
  556                 mtx_unlock(&devsoftc.mtx);
  557                 free(n1->dei_data, M_BUS);
  558                 free(n1, M_BUS);
  559                 return;
  560         }
  561         /* Leave at least one spot in the queue... */
  562         while (devsoftc.queued > devctl_queue_length - 1) {
  563                 n2 = TAILQ_FIRST(&devsoftc.devq);
  564                 TAILQ_REMOVE(&devsoftc.devq, n2, dei_link);
  565                 free(n2->dei_data, M_BUS);
  566                 free(n2, M_BUS);
  567                 devsoftc.queued--;
  568         }
  569         TAILQ_INSERT_TAIL(&devsoftc.devq, n1, dei_link);
  570         devsoftc.queued++;
  571         cv_broadcast(&devsoftc.cv);
  572         mtx_unlock(&devsoftc.mtx);
  573         selwakeup(&devsoftc.sel);
  574         p = devsoftc.async_proc;
  575         if (p != NULL) {
  576                 PROC_LOCK(p);
  577                 psignal(p, SIGIO);
  578                 PROC_UNLOCK(p);
  579         }
  580         return;
  581 out:
  582         /*
  583          * We have to free data on all error paths since the caller
  584          * assumes it will be free'd when this item is dequeued.
  585          */
  586         free(data, M_BUS);
  587         return;
  588 }
  589 
  590 void
  591 devctl_queue_data(char *data)
  592 {
  593 
  594         devctl_queue_data_f(data, M_NOWAIT);
  595 }
  596 
  597 /**
  598  * @brief Send a 'notification' to userland, using standard ways
  599  */
  600 void
  601 devctl_notify_f(const char *system, const char *subsystem, const char *type,
  602     const char *data, int flags)
  603 {
  604         int len = 0;
  605         char *msg;
  606 
  607         if (system == NULL)
  608                 return;         /* BOGUS!  Must specify system. */
  609         if (subsystem == NULL)
  610                 return;         /* BOGUS!  Must specify subsystem. */
  611         if (type == NULL)
  612                 return;         /* BOGUS!  Must specify type. */
  613         len += strlen(" system=") + strlen(system);
  614         len += strlen(" subsystem=") + strlen(subsystem);
  615         len += strlen(" type=") + strlen(type);
  616         /* add in the data message plus newline. */
  617         if (data != NULL)
  618                 len += strlen(data);
  619         len += 3;       /* '!', '\n', and NUL */
  620         msg = malloc(len, M_BUS, flags);
  621         if (msg == NULL)
  622                 return;         /* Drop it on the floor */
  623         if (data != NULL)
  624                 snprintf(msg, len, "!system=%s subsystem=%s type=%s %s\n",
  625                     system, subsystem, type, data);
  626         else
  627                 snprintf(msg, len, "!system=%s subsystem=%s type=%s\n",
  628                     system, subsystem, type);
  629         devctl_queue_data_f(msg, flags);
  630 }
  631 
  632 void
  633 devctl_notify(const char *system, const char *subsystem, const char *type,
  634     const char *data)
  635 {
  636 
  637         devctl_notify_f(system, subsystem, type, data, M_NOWAIT);
  638 }
  639 
  640 /*
  641  * Common routine that tries to make sending messages as easy as possible.
  642  * We allocate memory for the data, copy strings into that, but do not
  643  * free it unless there's an error.  The dequeue part of the driver should
  644  * free the data.  We don't send data when the device is disabled.  We do
  645  * send data, even when we have no listeners, because we wish to avoid
  646  * races relating to startup and restart of listening applications.
  647  *
  648  * devaddq is designed to string together the type of event, with the
  649  * object of that event, plus the plug and play info and location info
  650  * for that event.  This is likely most useful for devices, but less
  651  * useful for other consumers of this interface.  Those should use
  652  * the devctl_queue_data() interface instead.
  653  */
  654 static void
  655 devaddq(const char *type, const char *what, device_t dev)
  656 {
  657         char *data = NULL;
  658         char *loc = NULL;
  659         char *pnp = NULL;
  660         const char *parstr;
  661 
  662         if (!devctl_queue_length)/* Rare race, but lost races safely discard */
  663                 return;
  664         data = malloc(1024, M_BUS, M_NOWAIT);
  665         if (data == NULL)
  666                 goto bad;
  667 
  668         /* get the bus specific location of this device */
  669         loc = malloc(1024, M_BUS, M_NOWAIT);
  670         if (loc == NULL)
  671                 goto bad;
  672         *loc = '\0';
  673         bus_child_location_str(dev, loc, 1024);
  674 
  675         /* Get the bus specific pnp info of this device */
  676         pnp = malloc(1024, M_BUS, M_NOWAIT);
  677         if (pnp == NULL)
  678                 goto bad;
  679         *pnp = '\0';
  680         bus_child_pnpinfo_str(dev, pnp, 1024);
  681 
  682         /* Get the parent of this device, or / if high enough in the tree. */
  683         if (device_get_parent(dev) == NULL)
  684                 parstr = ".";   /* Or '/' ? */
  685         else
  686                 parstr = device_get_nameunit(device_get_parent(dev));
  687         /* String it all together. */
  688         snprintf(data, 1024, "%s%s at %s %s on %s\n", type, what, loc, pnp,
  689           parstr);
  690         free(loc, M_BUS);
  691         free(pnp, M_BUS);
  692         devctl_queue_data(data);
  693         return;
  694 bad:
  695         free(pnp, M_BUS);
  696         free(loc, M_BUS);
  697         free(data, M_BUS);
  698         return;
  699 }
  700 
  701 /*
  702  * A device was added to the tree.  We are called just after it successfully
  703  * attaches (that is, probe and attach success for this device).  No call
  704  * is made if a device is merely parented into the tree.  See devnomatch
  705  * if probe fails.  If attach fails, no notification is sent (but maybe
  706  * we should have a different message for this).
  707  */
  708 static void
  709 devadded(device_t dev)
  710 {
  711         char *pnp = NULL;
  712         char *tmp = NULL;
  713 
  714         pnp = malloc(1024, M_BUS, M_NOWAIT);
  715         if (pnp == NULL)
  716                 goto fail;
  717         tmp = malloc(1024, M_BUS, M_NOWAIT);
  718         if (tmp == NULL)
  719                 goto fail;
  720         *pnp = '\0';
  721         bus_child_pnpinfo_str(dev, pnp, 1024);
  722         snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
  723         devaddq("+", tmp, dev);
  724 fail:
  725         if (pnp != NULL)
  726                 free(pnp, M_BUS);
  727         if (tmp != NULL)
  728                 free(tmp, M_BUS);
  729         return;
  730 }
  731 
  732 /*
  733  * A device was removed from the tree.  We are called just before this
  734  * happens.
  735  */
  736 static void
  737 devremoved(device_t dev)
  738 {
  739         char *pnp = NULL;
  740         char *tmp = NULL;
  741 
  742         pnp = malloc(1024, M_BUS, M_NOWAIT);
  743         if (pnp == NULL)
  744                 goto fail;
  745         tmp = malloc(1024, M_BUS, M_NOWAIT);
  746         if (tmp == NULL)
  747                 goto fail;
  748         *pnp = '\0';
  749         bus_child_pnpinfo_str(dev, pnp, 1024);
  750         snprintf(tmp, 1024, "%s %s", device_get_nameunit(dev), pnp);
  751         devaddq("-", tmp, dev);
  752 fail:
  753         if (pnp != NULL)
  754                 free(pnp, M_BUS);
  755         if (tmp != NULL)
  756                 free(tmp, M_BUS);
  757         return;
  758 }
  759 
  760 /*
  761  * Called when there's no match for this device.  This is only called
  762  * the first time that no match happens, so we don't keep getting this
  763  * message.  Should that prove to be undesirable, we can change it.
  764  * This is called when all drivers that can attach to a given bus
  765  * decline to accept this device.  Other errrors may not be detected.
  766  */
  767 static void
  768 devnomatch(device_t dev)
  769 {
  770         devaddq("?", "", dev);
  771 }
  772 
  773 static int
  774 sysctl_devctl_disable(SYSCTL_HANDLER_ARGS)
  775 {
  776         struct dev_event_info *n1;
  777         int dis, error;
  778 
  779         dis = devctl_queue_length == 0;
  780         error = sysctl_handle_int(oidp, &dis, 0, req);
  781         if (error || !req->newptr)
  782                 return (error);
  783         mtx_lock(&devsoftc.mtx);
  784         if (dis) {
  785                 while (!TAILQ_EMPTY(&devsoftc.devq)) {
  786                         n1 = TAILQ_FIRST(&devsoftc.devq);
  787                         TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
  788                         free(n1->dei_data, M_BUS);
  789                         free(n1, M_BUS);
  790                 }
  791                 devsoftc.queued = 0;
  792                 devctl_queue_length = 0;
  793         } else {
  794                 devctl_queue_length = DEVCTL_DEFAULT_QUEUE_LEN;
  795         }
  796         mtx_unlock(&devsoftc.mtx);
  797         return (0);
  798 }
  799 
  800 static int
  801 sysctl_devctl_queue(SYSCTL_HANDLER_ARGS)
  802 {
  803         struct dev_event_info *n1;
  804         int q, error;
  805 
  806         q = devctl_queue_length;
  807         error = sysctl_handle_int(oidp, &q, 0, req);
  808         if (error || !req->newptr)
  809                 return (error);
  810         if (q < 0)
  811                 return (EINVAL);
  812         mtx_lock(&devsoftc.mtx);
  813         devctl_queue_length = q;
  814         while (devsoftc.queued > devctl_queue_length) {
  815                 n1 = TAILQ_FIRST(&devsoftc.devq);
  816                 TAILQ_REMOVE(&devsoftc.devq, n1, dei_link);
  817                 free(n1->dei_data, M_BUS);
  818                 free(n1, M_BUS);
  819                 devsoftc.queued--;
  820         }
  821         mtx_unlock(&devsoftc.mtx);
  822         return (0);
  823 }
  824 
  825 /* End of /dev/devctl code */
  826 
  827 static TAILQ_HEAD(,device)      bus_data_devices;
  828 static int bus_data_generation = 1;
  829 
  830 static kobj_method_t null_methods[] = {
  831         KOBJMETHOD_END
  832 };
  833 
  834 DEFINE_CLASS(null, null_methods, 0);
  835 
  836 /*
  837  * Bus pass implementation
  838  */
  839 
  840 static driver_list_t passes = TAILQ_HEAD_INITIALIZER(passes);
  841 int bus_current_pass = BUS_PASS_ROOT;
  842 
  843 /**
  844  * @internal
  845  * @brief Register the pass level of a new driver attachment
  846  *
  847  * Register a new driver attachment's pass level.  If no driver
  848  * attachment with the same pass level has been added, then @p new
  849  * will be added to the global passes list.
  850  *
  851  * @param new           the new driver attachment
  852  */
  853 static void
  854 driver_register_pass(struct driverlink *new)
  855 {
  856         struct driverlink *dl;
  857 
  858         /* We only consider pass numbers during boot. */
  859         if (bus_current_pass == BUS_PASS_DEFAULT)
  860                 return;
  861 
  862         /*
  863          * Walk the passes list.  If we already know about this pass
  864          * then there is nothing to do.  If we don't, then insert this
  865          * driver link into the list.
  866          */
  867         TAILQ_FOREACH(dl, &passes, passlink) {
  868                 if (dl->pass < new->pass)
  869                         continue;
  870                 if (dl->pass == new->pass)
  871                         return;
  872                 TAILQ_INSERT_BEFORE(dl, new, passlink);
  873                 return;
  874         }
  875         TAILQ_INSERT_TAIL(&passes, new, passlink);
  876 }
  877 
  878 /**
  879  * @brief Raise the current bus pass
  880  *
  881  * Raise the current bus pass level to @p pass.  Call the BUS_NEW_PASS()
  882  * method on the root bus to kick off a new device tree scan for each
  883  * new pass level that has at least one driver.
  884  */
  885 void
  886 bus_set_pass(int pass)
  887 {
  888         struct driverlink *dl;
  889 
  890         if (bus_current_pass > pass)
  891                 panic("Attempt to lower bus pass level");
  892 
  893         TAILQ_FOREACH(dl, &passes, passlink) {
  894                 /* Skip pass values below the current pass level. */
  895                 if (dl->pass <= bus_current_pass)
  896                         continue;
  897 
  898                 /*
  899                  * Bail once we hit a driver with a pass level that is
  900                  * too high.
  901                  */
  902                 if (dl->pass > pass)
  903                         break;
  904 
  905                 /*
  906                  * Raise the pass level to the next level and rescan
  907                  * the tree.
  908                  */
  909                 bus_current_pass = dl->pass;
  910                 BUS_NEW_PASS(root_bus);
  911         }
  912 
  913         /*
  914          * If there isn't a driver registered for the requested pass,
  915          * then bus_current_pass might still be less than 'pass'.  Set
  916          * it to 'pass' in that case.
  917          */
  918         if (bus_current_pass < pass)
  919                 bus_current_pass = pass;
  920         KASSERT(bus_current_pass == pass, ("Failed to update bus pass level"));
  921 }
  922 
  923 /*
  924  * Devclass implementation
  925  */
  926 
  927 static devclass_list_t devclasses = TAILQ_HEAD_INITIALIZER(devclasses);
  928 
  929 /**
  930  * @internal
  931  * @brief Find or create a device class
  932  *
  933  * If a device class with the name @p classname exists, return it,
  934  * otherwise if @p create is non-zero create and return a new device
  935  * class.
  936  *
  937  * If @p parentname is non-NULL, the parent of the devclass is set to
  938  * the devclass of that name.
  939  *
  940  * @param classname     the devclass name to find or create
  941  * @param parentname    the parent devclass name or @c NULL
  942  * @param create        non-zero to create a devclass
  943  */
  944 static devclass_t
  945 devclass_find_internal(const char *classname, const char *parentname,
  946                        int create)
  947 {
  948         devclass_t dc;
  949 
  950         PDEBUG(("looking for %s", classname));
  951         if (!classname)
  952                 return (NULL);
  953 
  954         TAILQ_FOREACH(dc, &devclasses, link) {
  955                 if (!strcmp(dc->name, classname))
  956                         break;
  957         }
  958 
  959         if (create && !dc) {
  960                 PDEBUG(("creating %s", classname));
  961                 dc = malloc(sizeof(struct devclass) + strlen(classname) + 1,
  962                     M_BUS, M_NOWAIT | M_ZERO);
  963                 if (!dc)
  964                         return (NULL);
  965                 dc->parent = NULL;
  966                 dc->name = (char*) (dc + 1);
  967                 strcpy(dc->name, classname);
  968                 TAILQ_INIT(&dc->drivers);
  969                 TAILQ_INSERT_TAIL(&devclasses, dc, link);
  970 
  971                 bus_data_generation_update();
  972         }
  973 
  974         /*
  975          * If a parent class is specified, then set that as our parent so
  976          * that this devclass will support drivers for the parent class as
  977          * well.  If the parent class has the same name don't do this though
  978          * as it creates a cycle that can trigger an infinite loop in
  979          * device_probe_child() if a device exists for which there is no
  980          * suitable driver.
  981          */
  982         if (parentname && dc && !dc->parent &&
  983             strcmp(classname, parentname) != 0) {
  984                 dc->parent = devclass_find_internal(parentname, NULL, TRUE);
  985                 dc->parent->flags |= DC_HAS_CHILDREN;
  986         }
  987 
  988         return (dc);
  989 }
  990 
  991 /**
  992  * @brief Create a device class
  993  *
  994  * If a device class with the name @p classname exists, return it,
  995  * otherwise create and return a new device class.
  996  *
  997  * @param classname     the devclass name to find or create
  998  */
  999 devclass_t
 1000 devclass_create(const char *classname)
 1001 {
 1002         return (devclass_find_internal(classname, NULL, TRUE));
 1003 }
 1004 
 1005 /**
 1006  * @brief Find a device class
 1007  *
 1008  * If a device class with the name @p classname exists, return it,
 1009  * otherwise return @c NULL.
 1010  *
 1011  * @param classname     the devclass name to find
 1012  */
 1013 devclass_t
 1014 devclass_find(const char *classname)
 1015 {
 1016         return (devclass_find_internal(classname, NULL, FALSE));
 1017 }
 1018 
 1019 /**
 1020  * @brief Register that a device driver has been added to a devclass
 1021  *
 1022  * Register that a device driver has been added to a devclass.  This
 1023  * is called by devclass_add_driver to accomplish the recursive
 1024  * notification of all the children classes of dc, as well as dc.
 1025  * Each layer will have BUS_DRIVER_ADDED() called for all instances of
 1026  * the devclass.  We do a full search here of the devclass list at
 1027  * each iteration level to save storing children-lists in the devclass
 1028  * structure.  If we ever move beyond a few dozen devices doing this,
 1029  * we may need to reevaluate...
 1030  *
 1031  * @param dc            the devclass to edit
 1032  * @param driver        the driver that was just added
 1033  */
 1034 static void
 1035 devclass_driver_added(devclass_t dc, driver_t *driver)
 1036 {
 1037         devclass_t parent;
 1038         int i;
 1039 
 1040         /*
 1041          * Call BUS_DRIVER_ADDED for any existing busses in this class.
 1042          */
 1043         for (i = 0; i < dc->maxunit; i++)
 1044                 if (dc->devices[i] && device_is_attached(dc->devices[i]))
 1045                         BUS_DRIVER_ADDED(dc->devices[i], driver);
 1046 
 1047         /*
 1048          * Walk through the children classes.  Since we only keep a
 1049          * single parent pointer around, we walk the entire list of
 1050          * devclasses looking for children.  We set the
 1051          * DC_HAS_CHILDREN flag when a child devclass is created on
 1052          * the parent, so we only walk the list for those devclasses
 1053          * that have children.
 1054          */
 1055         if (!(dc->flags & DC_HAS_CHILDREN))
 1056                 return;
 1057         parent = dc;
 1058         TAILQ_FOREACH(dc, &devclasses, link) {
 1059                 if (dc->parent == parent)
 1060                         devclass_driver_added(dc, driver);
 1061         }
 1062 }
 1063 
 1064 /**
 1065  * @brief Add a device driver to a device class
 1066  *
 1067  * Add a device driver to a devclass. This is normally called
 1068  * automatically by DRIVER_MODULE(). The BUS_DRIVER_ADDED() method of
 1069  * all devices in the devclass will be called to allow them to attempt
 1070  * to re-probe any unmatched children.
 1071  *
 1072  * @param dc            the devclass to edit
 1073  * @param driver        the driver to register
 1074  */
 1075 static int
 1076 devclass_add_driver(devclass_t dc, driver_t *driver, int pass, devclass_t *dcp)
 1077 {
 1078         driverlink_t dl;
 1079         const char *parentname;
 1080 
 1081         PDEBUG(("%s", DRIVERNAME(driver)));
 1082 
 1083         /* Don't allow invalid pass values. */
 1084         if (pass <= BUS_PASS_ROOT)
 1085                 return (EINVAL);
 1086 
 1087         dl = malloc(sizeof *dl, M_BUS, M_NOWAIT|M_ZERO);
 1088         if (!dl)
 1089                 return (ENOMEM);
 1090 
 1091         /*
 1092          * Compile the driver's methods. Also increase the reference count
 1093          * so that the class doesn't get freed when the last instance
 1094          * goes. This means we can safely use static methods and avoids a
 1095          * double-free in devclass_delete_driver.
 1096          */
 1097         kobj_class_compile((kobj_class_t) driver);
 1098 
 1099         /*
 1100          * If the driver has any base classes, make the
 1101          * devclass inherit from the devclass of the driver's
 1102          * first base class. This will allow the system to
 1103          * search for drivers in both devclasses for children
 1104          * of a device using this driver.
 1105          */
 1106         if (driver->baseclasses)
 1107                 parentname = driver->baseclasses[0]->name;
 1108         else
 1109                 parentname = NULL;
 1110         *dcp = devclass_find_internal(driver->name, parentname, TRUE);
 1111 
 1112         dl->driver = driver;
 1113         TAILQ_INSERT_TAIL(&dc->drivers, dl, link);
 1114         driver->refs++;         /* XXX: kobj_mtx */
 1115         dl->pass = pass;
 1116         driver_register_pass(dl);
 1117 
 1118         devclass_driver_added(dc, driver);
 1119         bus_data_generation_update();
 1120         return (0);
 1121 }
 1122 
 1123 /**
 1124  * @brief Delete a device driver from a device class
 1125  *
 1126  * Delete a device driver from a devclass. This is normally called
 1127  * automatically by DRIVER_MODULE().
 1128  *
 1129  * If the driver is currently attached to any devices,
 1130  * devclass_delete_driver() will first attempt to detach from each
 1131  * device. If one of the detach calls fails, the driver will not be
 1132  * deleted.
 1133  *
 1134  * @param dc            the devclass to edit
 1135  * @param driver        the driver to unregister
 1136  */
 1137 static int
 1138 devclass_delete_driver(devclass_t busclass, driver_t *driver)
 1139 {
 1140         devclass_t dc = devclass_find(driver->name);
 1141         driverlink_t dl;
 1142         device_t dev;
 1143         int i;
 1144         int error;
 1145 
 1146         PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
 1147 
 1148         if (!dc)
 1149                 return (0);
 1150 
 1151         /*
 1152          * Find the link structure in the bus' list of drivers.
 1153          */
 1154         TAILQ_FOREACH(dl, &busclass->drivers, link) {
 1155                 if (dl->driver == driver)
 1156                         break;
 1157         }
 1158 
 1159         if (!dl) {
 1160                 PDEBUG(("%s not found in %s list", driver->name,
 1161                     busclass->name));
 1162                 return (ENOENT);
 1163         }
 1164 
 1165         /*
 1166          * Disassociate from any devices.  We iterate through all the
 1167          * devices in the devclass of the driver and detach any which are
 1168          * using the driver and which have a parent in the devclass which
 1169          * we are deleting from.
 1170          *
 1171          * Note that since a driver can be in multiple devclasses, we
 1172          * should not detach devices which are not children of devices in
 1173          * the affected devclass.
 1174          */
 1175         for (i = 0; i < dc->maxunit; i++) {
 1176                 if (dc->devices[i]) {
 1177                         dev = dc->devices[i];
 1178                         if (dev->driver == driver && dev->parent &&
 1179                             dev->parent->devclass == busclass) {
 1180                                 if ((error = device_detach(dev)) != 0)
 1181                                         return (error);
 1182                                 device_set_driver(dev, NULL);
 1183                         }
 1184                 }
 1185         }
 1186 
 1187         TAILQ_REMOVE(&busclass->drivers, dl, link);
 1188         free(dl, M_BUS);
 1189 
 1190         /* XXX: kobj_mtx */
 1191         driver->refs--;
 1192         if (driver->refs == 0)
 1193                 kobj_class_free((kobj_class_t) driver);
 1194 
 1195         bus_data_generation_update();
 1196         return (0);
 1197 }
 1198 
 1199 /**
 1200  * @brief Quiesces a set of device drivers from a device class
 1201  *
 1202  * Quiesce a device driver from a devclass. This is normally called
 1203  * automatically by DRIVER_MODULE().
 1204  *
 1205  * If the driver is currently attached to any devices,
 1206  * devclass_quiesece_driver() will first attempt to quiesce each
 1207  * device.
 1208  *
 1209  * @param dc            the devclass to edit
 1210  * @param driver        the driver to unregister
 1211  */
 1212 static int
 1213 devclass_quiesce_driver(devclass_t busclass, driver_t *driver)
 1214 {
 1215         devclass_t dc = devclass_find(driver->name);
 1216         driverlink_t dl;
 1217         device_t dev;
 1218         int i;
 1219         int error;
 1220 
 1221         PDEBUG(("%s from devclass %s", driver->name, DEVCLANAME(busclass)));
 1222 
 1223         if (!dc)
 1224                 return (0);
 1225 
 1226         /*
 1227          * Find the link structure in the bus' list of drivers.
 1228          */
 1229         TAILQ_FOREACH(dl, &busclass->drivers, link) {
 1230                 if (dl->driver == driver)
 1231                         break;
 1232         }
 1233 
 1234         if (!dl) {
 1235                 PDEBUG(("%s not found in %s list", driver->name,
 1236                     busclass->name));
 1237                 return (ENOENT);
 1238         }
 1239 
 1240         /*
 1241          * Quiesce all devices.  We iterate through all the devices in
 1242          * the devclass of the driver and quiesce any which are using
 1243          * the driver and which have a parent in the devclass which we
 1244          * are quiescing.
 1245          *
 1246          * Note that since a driver can be in multiple devclasses, we
 1247          * should not quiesce devices which are not children of
 1248          * devices in the affected devclass.
 1249          */
 1250         for (i = 0; i < dc->maxunit; i++) {
 1251                 if (dc->devices[i]) {
 1252                         dev = dc->devices[i];
 1253                         if (dev->driver == driver && dev->parent &&
 1254                             dev->parent->devclass == busclass) {
 1255                                 if ((error = device_quiesce(dev)) != 0)
 1256                                         return (error);
 1257                         }
 1258                 }
 1259         }
 1260 
 1261         return (0);
 1262 }
 1263 
 1264 /**
 1265  * @internal
 1266  */
 1267 static driverlink_t
 1268 devclass_find_driver_internal(devclass_t dc, const char *classname)
 1269 {
 1270         driverlink_t dl;
 1271 
 1272         PDEBUG(("%s in devclass %s", classname, DEVCLANAME(dc)));
 1273 
 1274         TAILQ_FOREACH(dl, &dc->drivers, link) {
 1275                 if (!strcmp(dl->driver->name, classname))
 1276                         return (dl);
 1277         }
 1278 
 1279         PDEBUG(("not found"));
 1280         return (NULL);
 1281 }
 1282 
 1283 /**
 1284  * @brief Return the name of the devclass
 1285  */
 1286 const char *
 1287 devclass_get_name(devclass_t dc)
 1288 {
 1289         return (dc->name);
 1290 }
 1291 
 1292 /**
 1293  * @brief Find a device given a unit number
 1294  *
 1295  * @param dc            the devclass to search
 1296  * @param unit          the unit number to search for
 1297  * 
 1298  * @returns             the device with the given unit number or @c
 1299  *                      NULL if there is no such device
 1300  */
 1301 device_t
 1302 devclass_get_device(devclass_t dc, int unit)
 1303 {
 1304         if (dc == NULL || unit < 0 || unit >= dc->maxunit)
 1305                 return (NULL);
 1306         return (dc->devices[unit]);
 1307 }
 1308 
 1309 /**
 1310  * @brief Find the softc field of a device given a unit number
 1311  *
 1312  * @param dc            the devclass to search
 1313  * @param unit          the unit number to search for
 1314  * 
 1315  * @returns             the softc field of the device with the given
 1316  *                      unit number or @c NULL if there is no such
 1317  *                      device
 1318  */
 1319 void *
 1320 devclass_get_softc(devclass_t dc, int unit)
 1321 {
 1322         device_t dev;
 1323 
 1324         dev = devclass_get_device(dc, unit);
 1325         if (!dev)
 1326                 return (NULL);
 1327 
 1328         return (device_get_softc(dev));
 1329 }
 1330 
 1331 /**
 1332  * @brief Get a list of devices in the devclass
 1333  *
 1334  * An array containing a list of all the devices in the given devclass
 1335  * is allocated and returned in @p *devlistp. The number of devices
 1336  * in the array is returned in @p *devcountp. The caller should free
 1337  * the array using @c free(p, M_TEMP), even if @p *devcountp is 0.
 1338  *
 1339  * @param dc            the devclass to examine
 1340  * @param devlistp      points at location for array pointer return
 1341  *                      value
 1342  * @param devcountp     points at location for array size return value
 1343  *
 1344  * @retval 0            success
 1345  * @retval ENOMEM       the array allocation failed
 1346  */
 1347 int
 1348 devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
 1349 {
 1350         int count, i;
 1351         device_t *list;
 1352 
 1353         count = devclass_get_count(dc);
 1354         list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
 1355         if (!list)
 1356                 return (ENOMEM);
 1357 
 1358         count = 0;
 1359         for (i = 0; i < dc->maxunit; i++) {
 1360                 if (dc->devices[i]) {
 1361                         list[count] = dc->devices[i];
 1362                         count++;
 1363                 }
 1364         }
 1365 
 1366         *devlistp = list;
 1367         *devcountp = count;
 1368 
 1369         return (0);
 1370 }
 1371 
 1372 /**
 1373  * @brief Get a list of drivers in the devclass
 1374  *
 1375  * An array containing a list of pointers to all the drivers in the
 1376  * given devclass is allocated and returned in @p *listp.  The number
 1377  * of drivers in the array is returned in @p *countp. The caller should
 1378  * free the array using @c free(p, M_TEMP).
 1379  *
 1380  * @param dc            the devclass to examine
 1381  * @param listp         gives location for array pointer return value
 1382  * @param countp        gives location for number of array elements
 1383  *                      return value
 1384  *
 1385  * @retval 0            success
 1386  * @retval ENOMEM       the array allocation failed
 1387  */
 1388 int
 1389 devclass_get_drivers(devclass_t dc, driver_t ***listp, int *countp)
 1390 {
 1391         driverlink_t dl;
 1392         driver_t **list;
 1393         int count;
 1394 
 1395         count = 0;
 1396         TAILQ_FOREACH(dl, &dc->drivers, link)
 1397                 count++;
 1398         list = malloc(count * sizeof(driver_t *), M_TEMP, M_NOWAIT);
 1399         if (list == NULL)
 1400                 return (ENOMEM);
 1401 
 1402         count = 0;
 1403         TAILQ_FOREACH(dl, &dc->drivers, link) {
 1404                 list[count] = dl->driver;
 1405                 count++;
 1406         }
 1407         *listp = list;
 1408         *countp = count;
 1409 
 1410         return (0);
 1411 }
 1412 
 1413 /**
 1414  * @brief Get the number of devices in a devclass
 1415  *
 1416  * @param dc            the devclass to examine
 1417  */
 1418 int
 1419 devclass_get_count(devclass_t dc)
 1420 {
 1421         int count, i;
 1422 
 1423         count = 0;
 1424         for (i = 0; i < dc->maxunit; i++)
 1425                 if (dc->devices[i])
 1426                         count++;
 1427         return (count);
 1428 }
 1429 
 1430 /**
 1431  * @brief Get the maximum unit number used in a devclass
 1432  *
 1433  * Note that this is one greater than the highest currently-allocated
 1434  * unit.  If a null devclass_t is passed in, -1 is returned to indicate
 1435  * that not even the devclass has been allocated yet.
 1436  *
 1437  * @param dc            the devclass to examine
 1438  */
 1439 int
 1440 devclass_get_maxunit(devclass_t dc)
 1441 {
 1442         if (dc == NULL)
 1443                 return (-1);
 1444         return (dc->maxunit);
 1445 }
 1446 
 1447 /**
 1448  * @brief Find a free unit number in a devclass
 1449  *
 1450  * This function searches for the first unused unit number greater
 1451  * that or equal to @p unit.
 1452  *
 1453  * @param dc            the devclass to examine
 1454  * @param unit          the first unit number to check
 1455  */
 1456 int
 1457 devclass_find_free_unit(devclass_t dc, int unit)
 1458 {
 1459         if (dc == NULL)
 1460                 return (unit);
 1461         while (unit < dc->maxunit && dc->devices[unit] != NULL)
 1462                 unit++;
 1463         return (unit);
 1464 }
 1465 
 1466 /**
 1467  * @brief Set the parent of a devclass
 1468  *
 1469  * The parent class is normally initialised automatically by
 1470  * DRIVER_MODULE().
 1471  *
 1472  * @param dc            the devclass to edit
 1473  * @param pdc           the new parent devclass
 1474  */
 1475 void
 1476 devclass_set_parent(devclass_t dc, devclass_t pdc)
 1477 {
 1478         dc->parent = pdc;
 1479 }
 1480 
 1481 /**
 1482  * @brief Get the parent of a devclass
 1483  *
 1484  * @param dc            the devclass to examine
 1485  */
 1486 devclass_t
 1487 devclass_get_parent(devclass_t dc)
 1488 {
 1489         return (dc->parent);
 1490 }
 1491 
 1492 struct sysctl_ctx_list *
 1493 devclass_get_sysctl_ctx(devclass_t dc)
 1494 {
 1495         return (&dc->sysctl_ctx);
 1496 }
 1497 
 1498 struct sysctl_oid *
 1499 devclass_get_sysctl_tree(devclass_t dc)
 1500 {
 1501         return (dc->sysctl_tree);
 1502 }
 1503 
 1504 /**
 1505  * @internal
 1506  * @brief Allocate a unit number
 1507  *
 1508  * On entry, @p *unitp is the desired unit number (or @c -1 if any
 1509  * will do). The allocated unit number is returned in @p *unitp.
 1510 
 1511  * @param dc            the devclass to allocate from
 1512  * @param unitp         points at the location for the allocated unit
 1513  *                      number
 1514  *
 1515  * @retval 0            success
 1516  * @retval EEXIST       the requested unit number is already allocated
 1517  * @retval ENOMEM       memory allocation failure
 1518  */
 1519 static int
 1520 devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
 1521 {
 1522         const char *s;
 1523         int unit = *unitp;
 1524 
 1525         PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
 1526 
 1527         /* Ask the parent bus if it wants to wire this device. */
 1528         if (unit == -1)
 1529                 BUS_HINT_DEVICE_UNIT(device_get_parent(dev), dev, dc->name,
 1530                     &unit);
 1531 
 1532         /* If we were given a wired unit number, check for existing device */
 1533         /* XXX imp XXX */
 1534         if (unit != -1) {
 1535                 if (unit >= 0 && unit < dc->maxunit &&
 1536                     dc->devices[unit] != NULL) {
 1537                         if (bootverbose)
 1538                                 printf("%s: %s%d already exists; skipping it\n",
 1539                                     dc->name, dc->name, *unitp);
 1540                         return (EEXIST);
 1541                 }
 1542         } else {
 1543                 /* Unwired device, find the next available slot for it */
 1544                 unit = 0;
 1545                 for (unit = 0;; unit++) {
 1546                         /* If there is an "at" hint for a unit then skip it. */
 1547                         if (resource_string_value(dc->name, unit, "at", &s) ==
 1548                             0)
 1549                                 continue;
 1550 
 1551                         /* If this device slot is already in use, skip it. */
 1552                         if (unit < dc->maxunit && dc->devices[unit] != NULL)
 1553                                 continue;
 1554 
 1555                         break;
 1556                 }
 1557         }
 1558 
 1559         /*
 1560          * We've selected a unit beyond the length of the table, so let's
 1561          * extend the table to make room for all units up to and including
 1562          * this one.
 1563          */
 1564         if (unit >= dc->maxunit) {
 1565                 device_t *newlist, *oldlist;
 1566                 int newsize;
 1567 
 1568                 oldlist = dc->devices;
 1569                 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
 1570                 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
 1571                 if (!newlist)
 1572                         return (ENOMEM);
 1573                 if (oldlist != NULL)
 1574                         bcopy(oldlist, newlist, sizeof(device_t) * dc->maxunit);
 1575                 bzero(newlist + dc->maxunit,
 1576                     sizeof(device_t) * (newsize - dc->maxunit));
 1577                 dc->devices = newlist;
 1578                 dc->maxunit = newsize;
 1579                 if (oldlist != NULL)
 1580                         free(oldlist, M_BUS);
 1581         }
 1582         PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
 1583 
 1584         *unitp = unit;
 1585         return (0);
 1586 }
 1587 
 1588 /**
 1589  * @internal
 1590  * @brief Add a device to a devclass
 1591  *
 1592  * A unit number is allocated for the device (using the device's
 1593  * preferred unit number if any) and the device is registered in the
 1594  * devclass. This allows the device to be looked up by its unit
 1595  * number, e.g. by decoding a dev_t minor number.
 1596  *
 1597  * @param dc            the devclass to add to
 1598  * @param dev           the device to add
 1599  *
 1600  * @retval 0            success
 1601  * @retval EEXIST       the requested unit number is already allocated
 1602  * @retval ENOMEM       memory allocation failure
 1603  */
 1604 static int
 1605 devclass_add_device(devclass_t dc, device_t dev)
 1606 {
 1607         int buflen, error;
 1608 
 1609         PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
 1610 
 1611         buflen = snprintf(NULL, 0, "%s%d$", dc->name, INT_MAX);
 1612         if (buflen < 0)
 1613                 return (ENOMEM);
 1614         dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
 1615         if (!dev->nameunit)
 1616                 return (ENOMEM);
 1617 
 1618         if ((error = devclass_alloc_unit(dc, dev, &dev->unit)) != 0) {
 1619                 free(dev->nameunit, M_BUS);
 1620                 dev->nameunit = NULL;
 1621                 return (error);
 1622         }
 1623         dc->devices[dev->unit] = dev;
 1624         dev->devclass = dc;
 1625         snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
 1626 
 1627         return (0);
 1628 }
 1629 
 1630 /**
 1631  * @internal
 1632  * @brief Delete a device from a devclass
 1633  *
 1634  * The device is removed from the devclass's device list and its unit
 1635  * number is freed.
 1636 
 1637  * @param dc            the devclass to delete from
 1638  * @param dev           the device to delete
 1639  *
 1640  * @retval 0            success
 1641  */
 1642 static int
 1643 devclass_delete_device(devclass_t dc, device_t dev)
 1644 {
 1645         if (!dc || !dev)
 1646                 return (0);
 1647 
 1648         PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
 1649 
 1650         if (dev->devclass != dc || dc->devices[dev->unit] != dev)
 1651                 panic("devclass_delete_device: inconsistent device class");
 1652         dc->devices[dev->unit] = NULL;
 1653         if (dev->flags & DF_WILDCARD)
 1654                 dev->unit = -1;
 1655         dev->devclass = NULL;
 1656         free(dev->nameunit, M_BUS);
 1657         dev->nameunit = NULL;
 1658 
 1659         return (0);
 1660 }
 1661 
 1662 /**
 1663  * @internal
 1664  * @brief Make a new device and add it as a child of @p parent
 1665  *
 1666  * @param parent        the parent of the new device
 1667  * @param name          the devclass name of the new device or @c NULL
 1668  *                      to leave the devclass unspecified
 1669  * @parem unit          the unit number of the new device of @c -1 to
 1670  *                      leave the unit number unspecified
 1671  *
 1672  * @returns the new device
 1673  */
 1674 static device_t
 1675 make_device(device_t parent, const char *name, int unit)
 1676 {
 1677         device_t dev;
 1678         devclass_t dc;
 1679 
 1680         PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
 1681 
 1682         if (name) {
 1683                 dc = devclass_find_internal(name, NULL, TRUE);
 1684                 if (!dc) {
 1685                         printf("make_device: can't find device class %s\n",
 1686                             name);
 1687                         return (NULL);
 1688                 }
 1689         } else {
 1690                 dc = NULL;
 1691         }
 1692 
 1693         dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
 1694         if (!dev)
 1695                 return (NULL);
 1696 
 1697         dev->parent = parent;
 1698         TAILQ_INIT(&dev->children);
 1699         kobj_init((kobj_t) dev, &null_class);
 1700         dev->driver = NULL;
 1701         dev->devclass = NULL;
 1702         dev->unit = unit;
 1703         dev->nameunit = NULL;
 1704         dev->desc = NULL;
 1705         dev->busy = 0;
 1706         dev->devflags = 0;
 1707         dev->flags = DF_ENABLED;
 1708         dev->order = 0;
 1709         if (unit == -1)
 1710                 dev->flags |= DF_WILDCARD;
 1711         if (name) {
 1712                 dev->flags |= DF_FIXEDCLASS;
 1713                 if (devclass_add_device(dc, dev)) {
 1714                         kobj_delete((kobj_t) dev, M_BUS);
 1715                         return (NULL);
 1716                 }
 1717         }
 1718         dev->ivars = NULL;
 1719         dev->softc = NULL;
 1720 
 1721         dev->state = DS_NOTPRESENT;
 1722 
 1723         TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
 1724         bus_data_generation_update();
 1725 
 1726         return (dev);
 1727 }
 1728 
 1729 /**
 1730  * @internal
 1731  * @brief Print a description of a device.
 1732  */
 1733 static int
 1734 device_print_child(device_t dev, device_t child)
 1735 {
 1736         int retval = 0;
 1737 
 1738         if (device_is_alive(child))
 1739                 retval += BUS_PRINT_CHILD(dev, child);
 1740         else
 1741                 retval += device_printf(child, " not found\n");
 1742 
 1743         return (retval);
 1744 }
 1745 
 1746 /**
 1747  * @brief Create a new device
 1748  *
 1749  * This creates a new device and adds it as a child of an existing
 1750  * parent device. The new device will be added after the last existing
 1751  * child with order zero.
 1752  * 
 1753  * @param dev           the device which will be the parent of the
 1754  *                      new child device
 1755  * @param name          devclass name for new device or @c NULL if not
 1756  *                      specified
 1757  * @param unit          unit number for new device or @c -1 if not
 1758  *                      specified
 1759  * 
 1760  * @returns             the new device
 1761  */
 1762 device_t
 1763 device_add_child(device_t dev, const char *name, int unit)
 1764 {
 1765         return (device_add_child_ordered(dev, 0, name, unit));
 1766 }
 1767 
 1768 /**
 1769  * @brief Create a new device
 1770  *
 1771  * This creates a new device and adds it as a child of an existing
 1772  * parent device. The new device will be added after the last existing
 1773  * child with the same order.
 1774  * 
 1775  * @param dev           the device which will be the parent of the
 1776  *                      new child device
 1777  * @param order         a value which is used to partially sort the
 1778  *                      children of @p dev - devices created using
 1779  *                      lower values of @p order appear first in @p
 1780  *                      dev's list of children
 1781  * @param name          devclass name for new device or @c NULL if not
 1782  *                      specified
 1783  * @param unit          unit number for new device or @c -1 if not
 1784  *                      specified
 1785  * 
 1786  * @returns             the new device
 1787  */
 1788 device_t
 1789 device_add_child_ordered(device_t dev, u_int order, const char *name, int unit)
 1790 {
 1791         device_t child;
 1792         device_t place;
 1793 
 1794         PDEBUG(("%s at %s with order %u as unit %d",
 1795             name, DEVICENAME(dev), order, unit));
 1796 
 1797         child = make_device(dev, name, unit);
 1798         if (child == NULL)
 1799                 return (child);
 1800         child->order = order;
 1801 
 1802         TAILQ_FOREACH(place, &dev->children, link) {
 1803                 if (place->order > order)
 1804                         break;
 1805         }
 1806 
 1807         if (place) {
 1808                 /*
 1809                  * The device 'place' is the first device whose order is
 1810                  * greater than the new child.
 1811                  */
 1812                 TAILQ_INSERT_BEFORE(place, child, link);
 1813         } else {
 1814                 /*
 1815                  * The new child's order is greater or equal to the order of
 1816                  * any existing device. Add the child to the tail of the list.
 1817                  */
 1818                 TAILQ_INSERT_TAIL(&dev->children, child, link);
 1819         }
 1820 
 1821         bus_data_generation_update();
 1822         return (child);
 1823 }
 1824 
 1825 /**
 1826  * @brief Delete a device
 1827  *
 1828  * This function deletes a device along with all of its children. If
 1829  * the device currently has a driver attached to it, the device is
 1830  * detached first using device_detach().
 1831  * 
 1832  * @param dev           the parent device
 1833  * @param child         the device to delete
 1834  *
 1835  * @retval 0            success
 1836  * @retval non-zero     a unit error code describing the error
 1837  */
 1838 int
 1839 device_delete_child(device_t dev, device_t child)
 1840 {
 1841         int error;
 1842         device_t grandchild;
 1843 
 1844         PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
 1845 
 1846         /* remove children first */
 1847         while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
 1848                 error = device_delete_child(child, grandchild);
 1849                 if (error)
 1850                         return (error);
 1851         }
 1852 
 1853         if ((error = device_detach(child)) != 0)
 1854                 return (error);
 1855         if (child->devclass)
 1856                 devclass_delete_device(child->devclass, child);
 1857         TAILQ_REMOVE(&dev->children, child, link);
 1858         TAILQ_REMOVE(&bus_data_devices, child, devlink);
 1859         kobj_delete((kobj_t) child, M_BUS);
 1860 
 1861         bus_data_generation_update();
 1862         return (0);
 1863 }
 1864 
 1865 /**
 1866  * @brief Find a device given a unit number
 1867  *
 1868  * This is similar to devclass_get_devices() but only searches for
 1869  * devices which have @p dev as a parent.
 1870  *
 1871  * @param dev           the parent device to search
 1872  * @param unit          the unit number to search for.  If the unit is -1,
 1873  *                      return the first child of @p dev which has name
 1874  *                      @p classname (that is, the one with the lowest unit.)
 1875  *
 1876  * @returns             the device with the given unit number or @c
 1877  *                      NULL if there is no such device
 1878  */
 1879 device_t
 1880 device_find_child(device_t dev, const char *classname, int unit)
 1881 {
 1882         devclass_t dc;
 1883         device_t child;
 1884 
 1885         dc = devclass_find(classname);
 1886         if (!dc)
 1887                 return (NULL);
 1888 
 1889         if (unit != -1) {
 1890                 child = devclass_get_device(dc, unit);
 1891                 if (child && child->parent == dev)
 1892                         return (child);
 1893         } else {
 1894                 for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
 1895                         child = devclass_get_device(dc, unit);
 1896                         if (child && child->parent == dev)
 1897                                 return (child);
 1898                 }
 1899         }
 1900         return (NULL);
 1901 }
 1902 
 1903 /**
 1904  * @internal
 1905  */
 1906 static driverlink_t
 1907 first_matching_driver(devclass_t dc, device_t dev)
 1908 {
 1909         if (dev->devclass)
 1910                 return (devclass_find_driver_internal(dc, dev->devclass->name));
 1911         return (TAILQ_FIRST(&dc->drivers));
 1912 }
 1913 
 1914 /**
 1915  * @internal
 1916  */
 1917 static driverlink_t
 1918 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
 1919 {
 1920         if (dev->devclass) {
 1921                 driverlink_t dl;
 1922                 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
 1923                         if (!strcmp(dev->devclass->name, dl->driver->name))
 1924                                 return (dl);
 1925                 return (NULL);
 1926         }
 1927         return (TAILQ_NEXT(last, link));
 1928 }
 1929 
 1930 /**
 1931  * @internal
 1932  */
 1933 int
 1934 device_probe_child(device_t dev, device_t child)
 1935 {
 1936         devclass_t dc;
 1937         driverlink_t best = NULL;
 1938         driverlink_t dl;
 1939         int result, pri = 0;
 1940         int hasclass = (child->devclass != NULL);
 1941 
 1942         GIANT_REQUIRED;
 1943 
 1944         dc = dev->devclass;
 1945         if (!dc)
 1946                 panic("device_probe_child: parent device has no devclass");
 1947 
 1948         /*
 1949          * If the state is already probed, then return.  However, don't
 1950          * return if we can rebid this object.
 1951          */
 1952         if (child->state == DS_ALIVE && (child->flags & DF_REBID) == 0)
 1953                 return (0);
 1954 
 1955         for (; dc; dc = dc->parent) {
 1956                 for (dl = first_matching_driver(dc, child);
 1957                      dl;
 1958                      dl = next_matching_driver(dc, child, dl)) {
 1959 
 1960                         /* If this driver's pass is too high, then ignore it. */
 1961                         if (dl->pass > bus_current_pass)
 1962                                 continue;
 1963 
 1964                         PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
 1965                         device_set_driver(child, dl->driver);
 1966                         if (!hasclass) {
 1967                                 if (device_set_devclass(child, dl->driver->name)) {
 1968                                         printf("driver bug: Unable to set devclass (devname: %s)\n",
 1969                                             (child ? device_get_name(child) :
 1970                                                 "no device"));
 1971                                         device_set_driver(child, NULL);
 1972                                         continue;
 1973                                 }
 1974                         }
 1975 
 1976                         /* Fetch any flags for the device before probing. */
 1977                         resource_int_value(dl->driver->name, child->unit,
 1978                             "flags", &child->devflags);
 1979 
 1980                         result = DEVICE_PROBE(child);
 1981 
 1982                         /* Reset flags and devclass before the next probe. */
 1983                         child->devflags = 0;
 1984                         if (!hasclass)
 1985                                 device_set_devclass(child, NULL);
 1986 
 1987                         /*
 1988                          * If the driver returns SUCCESS, there can be
 1989                          * no higher match for this device.
 1990                          */
 1991                         if (result == 0) {
 1992                                 best = dl;
 1993                                 pri = 0;
 1994                                 break;
 1995                         }
 1996 
 1997                         /*
 1998                          * The driver returned an error so it
 1999                          * certainly doesn't match.
 2000                          */
 2001                         if (result > 0) {
 2002                                 device_set_driver(child, NULL);
 2003                                 continue;
 2004                         }
 2005 
 2006                         /*
 2007                          * A priority lower than SUCCESS, remember the
 2008                          * best matching driver. Initialise the value
 2009                          * of pri for the first match.
 2010                          */
 2011                         if (best == NULL || result > pri) {
 2012                                 /*
 2013                                  * Probes that return BUS_PROBE_NOWILDCARD
 2014                                  * or lower only match when they are set
 2015                                  * in stone by the parent bus.
 2016                                  */
 2017                                 if (result <= BUS_PROBE_NOWILDCARD &&
 2018                                     child->flags & DF_WILDCARD)
 2019                                         continue;
 2020                                 best = dl;
 2021                                 pri = result;
 2022                                 continue;
 2023                         }
 2024                 }
 2025                 /*
 2026                  * If we have an unambiguous match in this devclass,
 2027                  * don't look in the parent.
 2028                  */
 2029                 if (best && pri == 0)
 2030                         break;
 2031         }
 2032 
 2033         /*
 2034          * If we found a driver, change state and initialise the devclass.
 2035          */
 2036         /* XXX What happens if we rebid and got no best? */
 2037         if (best) {
 2038                 /*
 2039                  * If this device was atached, and we were asked to
 2040                  * rescan, and it is a different driver, then we have
 2041                  * to detach the old driver and reattach this new one.
 2042                  * Note, we don't have to check for DF_REBID here
 2043                  * because if the state is > DS_ALIVE, we know it must
 2044                  * be.
 2045                  *
 2046                  * This assumes that all DF_REBID drivers can have
 2047                  * their probe routine called at any time and that
 2048                  * they are idempotent as well as completely benign in
 2049                  * normal operations.
 2050                  *
 2051                  * We also have to make sure that the detach
 2052                  * succeeded, otherwise we fail the operation (or
 2053                  * maybe it should just fail silently?  I'm torn).
 2054                  */
 2055                 if (child->state > DS_ALIVE && best->driver != child->driver)
 2056                         if ((result = device_detach(dev)) != 0)
 2057                                 return (result);
 2058 
 2059                 /* Set the winning driver, devclass, and flags. */
 2060                 if (!child->devclass) {
 2061                         result = device_set_devclass(child, best->driver->name);
 2062                         if (result != 0)
 2063                                 return (result);
 2064                 }
 2065                 device_set_driver(child, best->driver);
 2066                 resource_int_value(best->driver->name, child->unit,
 2067                     "flags", &child->devflags);
 2068 
 2069                 if (pri < 0) {
 2070                         /*
 2071                          * A bit bogus. Call the probe method again to make
 2072                          * sure that we have the right description.
 2073                          */
 2074                         DEVICE_PROBE(child);
 2075 #if 0
 2076                         child->flags |= DF_REBID;
 2077 #endif
 2078                 } else
 2079                         child->flags &= ~DF_REBID;
 2080                 child->state = DS_ALIVE;
 2081 
 2082                 bus_data_generation_update();
 2083                 return (0);
 2084         }
 2085 
 2086         return (ENXIO);
 2087 }
 2088 
 2089 /**
 2090  * @brief Return the parent of a device
 2091  */
 2092 device_t
 2093 device_get_parent(device_t dev)
 2094 {
 2095         return (dev->parent);
 2096 }
 2097 
 2098 /**
 2099  * @brief Get a list of children of a device
 2100  *
 2101  * An array containing a list of all the children of the given device
 2102  * is allocated and returned in @p *devlistp. The number of devices
 2103  * in the array is returned in @p *devcountp. The caller should free
 2104  * the array using @c free(p, M_TEMP).
 2105  *
 2106  * @param dev           the device to examine
 2107  * @param devlistp      points at location for array pointer return
 2108  *                      value
 2109  * @param devcountp     points at location for array size return value
 2110  *
 2111  * @retval 0            success
 2112  * @retval ENOMEM       the array allocation failed
 2113  */
 2114 int
 2115 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
 2116 {
 2117         int count;
 2118         device_t child;
 2119         device_t *list;
 2120 
 2121         count = 0;
 2122         TAILQ_FOREACH(child, &dev->children, link) {
 2123                 count++;
 2124         }
 2125 
 2126         list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
 2127         if (!list)
 2128                 return (ENOMEM);
 2129 
 2130         count = 0;
 2131         TAILQ_FOREACH(child, &dev->children, link) {
 2132                 list[count] = child;
 2133                 count++;
 2134         }
 2135 
 2136         *devlistp = list;
 2137         *devcountp = count;
 2138 
 2139         return (0);
 2140 }
 2141 
 2142 /**
 2143  * @brief Return the current driver for the device or @c NULL if there
 2144  * is no driver currently attached
 2145  */
 2146 driver_t *
 2147 device_get_driver(device_t dev)
 2148 {
 2149         return (dev->driver);
 2150 }
 2151 
 2152 /**
 2153  * @brief Return the current devclass for the device or @c NULL if
 2154  * there is none.
 2155  */
 2156 devclass_t
 2157 device_get_devclass(device_t dev)
 2158 {
 2159         return (dev->devclass);
 2160 }
 2161 
 2162 /**
 2163  * @brief Return the name of the device's devclass or @c NULL if there
 2164  * is none.
 2165  */
 2166 const char *
 2167 device_get_name(device_t dev)
 2168 {
 2169         if (dev != NULL && dev->devclass)
 2170                 return (devclass_get_name(dev->devclass));
 2171         return (NULL);
 2172 }
 2173 
 2174 /**
 2175  * @brief Return a string containing the device's devclass name
 2176  * followed by an ascii representation of the device's unit number
 2177  * (e.g. @c "foo2").
 2178  */
 2179 const char *
 2180 device_get_nameunit(device_t dev)
 2181 {
 2182         return (dev->nameunit);
 2183 }
 2184 
 2185 /**
 2186  * @brief Return the device's unit number.
 2187  */
 2188 int
 2189 device_get_unit(device_t dev)
 2190 {
 2191         return (dev->unit);
 2192 }
 2193 
 2194 /**
 2195  * @brief Return the device's description string
 2196  */
 2197 const char *
 2198 device_get_desc(device_t dev)
 2199 {
 2200         return (dev->desc);
 2201 }
 2202 
 2203 /**
 2204  * @brief Return the device's flags
 2205  */
 2206 u_int32_t
 2207 device_get_flags(device_t dev)
 2208 {
 2209         return (dev->devflags);
 2210 }
 2211 
 2212 struct sysctl_ctx_list *
 2213 device_get_sysctl_ctx(device_t dev)
 2214 {
 2215         return (&dev->sysctl_ctx);
 2216 }
 2217 
 2218 struct sysctl_oid *
 2219 device_get_sysctl_tree(device_t dev)
 2220 {
 2221         return (dev->sysctl_tree);
 2222 }
 2223 
 2224 /**
 2225  * @brief Print the name of the device followed by a colon and a space
 2226  *
 2227  * @returns the number of characters printed
 2228  */
 2229 int
 2230 device_print_prettyname(device_t dev)
 2231 {
 2232         const char *name = device_get_name(dev);
 2233 
 2234         if (name == NULL)
 2235                 return (printf("unknown: "));
 2236         return (printf("%s%d: ", name, device_get_unit(dev)));
 2237 }
 2238 
 2239 /**
 2240  * @brief Print the name of the device followed by a colon, a space
 2241  * and the result of calling vprintf() with the value of @p fmt and
 2242  * the following arguments.
 2243  *
 2244  * @returns the number of characters printed
 2245  */
 2246 int
 2247 device_printf(device_t dev, const char * fmt, ...)
 2248 {
 2249         va_list ap;
 2250         int retval;
 2251 
 2252         retval = device_print_prettyname(dev);
 2253         va_start(ap, fmt);
 2254         retval += vprintf(fmt, ap);
 2255         va_end(ap);
 2256         return (retval);
 2257 }
 2258 
 2259 /**
 2260  * @internal
 2261  */
 2262 static void
 2263 device_set_desc_internal(device_t dev, const char* desc, int copy)
 2264 {
 2265         if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
 2266                 free(dev->desc, M_BUS);
 2267                 dev->flags &= ~DF_DESCMALLOCED;
 2268                 dev->desc = NULL;
 2269         }
 2270 
 2271         if (copy && desc) {
 2272                 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
 2273                 if (dev->desc) {
 2274                         strcpy(dev->desc, desc);
 2275                         dev->flags |= DF_DESCMALLOCED;
 2276                 }
 2277         } else {
 2278                 /* Avoid a -Wcast-qual warning */
 2279                 dev->desc = (char *)(uintptr_t) desc;
 2280         }
 2281 
 2282         bus_data_generation_update();
 2283 }
 2284 
 2285 /**
 2286  * @brief Set the device's description
 2287  *
 2288  * The value of @c desc should be a string constant that will not
 2289  * change (at least until the description is changed in a subsequent
 2290  * call to device_set_desc() or device_set_desc_copy()).
 2291  */
 2292 void
 2293 device_set_desc(device_t dev, const char* desc)
 2294 {
 2295         device_set_desc_internal(dev, desc, FALSE);
 2296 }
 2297 
 2298 /**
 2299  * @brief Set the device's description
 2300  *
 2301  * The string pointed to by @c desc is copied. Use this function if
 2302  * the device description is generated, (e.g. with sprintf()).
 2303  */
 2304 void
 2305 device_set_desc_copy(device_t dev, const char* desc)
 2306 {
 2307         device_set_desc_internal(dev, desc, TRUE);
 2308 }
 2309 
 2310 /**
 2311  * @brief Set the device's flags
 2312  */
 2313 void
 2314 device_set_flags(device_t dev, u_int32_t flags)
 2315 {
 2316         dev->devflags = flags;
 2317 }
 2318 
 2319 /**
 2320  * @brief Return the device's softc field
 2321  *
 2322  * The softc is allocated and zeroed when a driver is attached, based
 2323  * on the size field of the driver.
 2324  */
 2325 void *
 2326 device_get_softc(device_t dev)
 2327 {
 2328         return (dev->softc);
 2329 }
 2330 
 2331 /**
 2332  * @brief Set the device's softc field
 2333  *
 2334  * Most drivers do not need to use this since the softc is allocated
 2335  * automatically when the driver is attached.
 2336  */
 2337 void
 2338 device_set_softc(device_t dev, void *softc)
 2339 {
 2340         if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
 2341                 free(dev->softc, M_BUS_SC);
 2342         dev->softc = softc;
 2343         if (dev->softc)
 2344                 dev->flags |= DF_EXTERNALSOFTC;
 2345         else
 2346                 dev->flags &= ~DF_EXTERNALSOFTC;
 2347 }
 2348 
 2349 /**
 2350  * @brief Get the device's ivars field
 2351  *
 2352  * The ivars field is used by the parent device to store per-device
 2353  * state (e.g. the physical location of the device or a list of
 2354  * resources).
 2355  */
 2356 void *
 2357 device_get_ivars(device_t dev)
 2358 {
 2359 
 2360         KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
 2361         return (dev->ivars);
 2362 }
 2363 
 2364 /**
 2365  * @brief Set the device's ivars field
 2366  */
 2367 void
 2368 device_set_ivars(device_t dev, void * ivars)
 2369 {
 2370 
 2371         KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
 2372         dev->ivars = ivars;
 2373 }
 2374 
 2375 /**
 2376  * @brief Return the device's state
 2377  */
 2378 device_state_t
 2379 device_get_state(device_t dev)
 2380 {
 2381         return (dev->state);
 2382 }
 2383 
 2384 /**
 2385  * @brief Set the DF_ENABLED flag for the device
 2386  */
 2387 void
 2388 device_enable(device_t dev)
 2389 {
 2390         dev->flags |= DF_ENABLED;
 2391 }
 2392 
 2393 /**
 2394  * @brief Clear the DF_ENABLED flag for the device
 2395  */
 2396 void
 2397 device_disable(device_t dev)
 2398 {
 2399         dev->flags &= ~DF_ENABLED;
 2400 }
 2401 
 2402 /**
 2403  * @brief Increment the busy counter for the device
 2404  */
 2405 void
 2406 device_busy(device_t dev)
 2407 {
 2408         if (dev->state < DS_ATTACHED)
 2409                 panic("device_busy: called for unattached device");
 2410         if (dev->busy == 0 && dev->parent)
 2411                 device_busy(dev->parent);
 2412         dev->busy++;
 2413         dev->state = DS_BUSY;
 2414 }
 2415 
 2416 /**
 2417  * @brief Decrement the busy counter for the device
 2418  */
 2419 void
 2420 device_unbusy(device_t dev)
 2421 {
 2422         if (dev->state != DS_BUSY)
 2423                 panic("device_unbusy: called for non-busy device %s",
 2424                     device_get_nameunit(dev));
 2425         dev->busy--;
 2426         if (dev->busy == 0) {
 2427                 if (dev->parent)
 2428                         device_unbusy(dev->parent);
 2429                 dev->state = DS_ATTACHED;
 2430         }
 2431 }
 2432 
 2433 /**
 2434  * @brief Set the DF_QUIET flag for the device
 2435  */
 2436 void
 2437 device_quiet(device_t dev)
 2438 {
 2439         dev->flags |= DF_QUIET;
 2440 }
 2441 
 2442 /**
 2443  * @brief Clear the DF_QUIET flag for the device
 2444  */
 2445 void
 2446 device_verbose(device_t dev)
 2447 {
 2448         dev->flags &= ~DF_QUIET;
 2449 }
 2450 
 2451 /**
 2452  * @brief Return non-zero if the DF_QUIET flag is set on the device
 2453  */
 2454 int
 2455 device_is_quiet(device_t dev)
 2456 {
 2457         return ((dev->flags & DF_QUIET) != 0);
 2458 }
 2459 
 2460 /**
 2461  * @brief Return non-zero if the DF_ENABLED flag is set on the device
 2462  */
 2463 int
 2464 device_is_enabled(device_t dev)
 2465 {
 2466         return ((dev->flags & DF_ENABLED) != 0);
 2467 }
 2468 
 2469 /**
 2470  * @brief Return non-zero if the device was successfully probed
 2471  */
 2472 int
 2473 device_is_alive(device_t dev)
 2474 {
 2475         return (dev->state >= DS_ALIVE);
 2476 }
 2477 
 2478 /**
 2479  * @brief Return non-zero if the device currently has a driver
 2480  * attached to it
 2481  */
 2482 int
 2483 device_is_attached(device_t dev)
 2484 {
 2485         return (dev->state >= DS_ATTACHED);
 2486 }
 2487 
 2488 /**
 2489  * @brief Set the devclass of a device
 2490  * @see devclass_add_device().
 2491  */
 2492 int
 2493 device_set_devclass(device_t dev, const char *classname)
 2494 {
 2495         devclass_t dc;
 2496         int error;
 2497 
 2498         if (!classname) {
 2499                 if (dev->devclass)
 2500                         devclass_delete_device(dev->devclass, dev);
 2501                 return (0);
 2502         }
 2503 
 2504         if (dev->devclass) {
 2505                 printf("device_set_devclass: device class already set\n");
 2506                 return (EINVAL);
 2507         }
 2508 
 2509         dc = devclass_find_internal(classname, NULL, TRUE);
 2510         if (!dc)
 2511                 return (ENOMEM);
 2512 
 2513         error = devclass_add_device(dc, dev);
 2514 
 2515         bus_data_generation_update();
 2516         return (error);
 2517 }
 2518 
 2519 /**
 2520  * @brief Set the driver of a device
 2521  *
 2522  * @retval 0            success
 2523  * @retval EBUSY        the device already has a driver attached
 2524  * @retval ENOMEM       a memory allocation failure occurred
 2525  */
 2526 int
 2527 device_set_driver(device_t dev, driver_t *driver)
 2528 {
 2529         if (dev->state >= DS_ATTACHED)
 2530                 return (EBUSY);
 2531 
 2532         if (dev->driver == driver)
 2533                 return (0);
 2534 
 2535         if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
 2536                 free(dev->softc, M_BUS_SC);
 2537                 dev->softc = NULL;
 2538         }
 2539         kobj_delete((kobj_t) dev, NULL);
 2540         dev->driver = driver;
 2541         if (driver) {
 2542                 kobj_init((kobj_t) dev, (kobj_class_t) driver);
 2543                 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
 2544                         dev->softc = malloc(driver->size, M_BUS_SC,
 2545                             M_NOWAIT | M_ZERO);
 2546                         if (!dev->softc) {
 2547                                 kobj_delete((kobj_t) dev, NULL);
 2548                                 kobj_init((kobj_t) dev, &null_class);
 2549                                 dev->driver = NULL;
 2550                                 return (ENOMEM);
 2551                         }
 2552                 }
 2553         } else {
 2554                 kobj_init((kobj_t) dev, &null_class);
 2555         }
 2556 
 2557         bus_data_generation_update();
 2558         return (0);
 2559 }
 2560 
 2561 /**
 2562  * @brief Probe a device, and return this status.
 2563  *
 2564  * This function is the core of the device autoconfiguration
 2565  * system. Its purpose is to select a suitable driver for a device and
 2566  * then call that driver to initialise the hardware appropriately. The
 2567  * driver is selected by calling the DEVICE_PROBE() method of a set of
 2568  * candidate drivers and then choosing the driver which returned the
 2569  * best value. This driver is then attached to the device using
 2570  * device_attach().
 2571  *
 2572  * The set of suitable drivers is taken from the list of drivers in
 2573  * the parent device's devclass. If the device was originally created
 2574  * with a specific class name (see device_add_child()), only drivers
 2575  * with that name are probed, otherwise all drivers in the devclass
 2576  * are probed. If no drivers return successful probe values in the
 2577  * parent devclass, the search continues in the parent of that
 2578  * devclass (see devclass_get_parent()) if any.
 2579  *
 2580  * @param dev           the device to initialise
 2581  *
 2582  * @retval 0            success
 2583  * @retval ENXIO        no driver was found
 2584  * @retval ENOMEM       memory allocation failure
 2585  * @retval non-zero     some other unix error code
 2586  * @retval -1           Device already attached
 2587  */
 2588 int
 2589 device_probe(device_t dev)
 2590 {
 2591         int error;
 2592 
 2593         GIANT_REQUIRED;
 2594 
 2595         if (dev->state >= DS_ALIVE && (dev->flags & DF_REBID) == 0)
 2596                 return (-1);
 2597 
 2598         if (!(dev->flags & DF_ENABLED)) {
 2599                 if (bootverbose && device_get_name(dev) != NULL) {
 2600                         device_print_prettyname(dev);
 2601                         printf("not probed (disabled)\n");
 2602                 }
 2603                 return (-1);
 2604         }
 2605         if ((error = device_probe_child(dev->parent, dev)) != 0) {              
 2606                 if (bus_current_pass == BUS_PASS_DEFAULT &&
 2607                     !(dev->flags & DF_DONENOMATCH)) {
 2608                         BUS_PROBE_NOMATCH(dev->parent, dev);
 2609                         devnomatch(dev);
 2610                         dev->flags |= DF_DONENOMATCH;
 2611                 }
 2612                 return (error);
 2613         }
 2614         return (0);
 2615 }
 2616 
 2617 /**
 2618  * @brief Probe a device and attach a driver if possible
 2619  *
 2620  * calls device_probe() and attaches if that was successful.
 2621  */
 2622 int
 2623 device_probe_and_attach(device_t dev)
 2624 {
 2625         int error;
 2626 
 2627         GIANT_REQUIRED;
 2628 
 2629         error = device_probe(dev);
 2630         if (error == -1)
 2631                 return (0);
 2632         else if (error != 0)
 2633                 return (error);
 2634         return (device_attach(dev));
 2635 }
 2636 
 2637 /**
 2638  * @brief Attach a device driver to a device
 2639  *
 2640  * This function is a wrapper around the DEVICE_ATTACH() driver
 2641  * method. In addition to calling DEVICE_ATTACH(), it initialises the
 2642  * device's sysctl tree, optionally prints a description of the device
 2643  * and queues a notification event for user-based device management
 2644  * services.
 2645  *
 2646  * Normally this function is only called internally from
 2647  * device_probe_and_attach().
 2648  *
 2649  * @param dev           the device to initialise
 2650  *
 2651  * @retval 0            success
 2652  * @retval ENXIO        no driver was found
 2653  * @retval ENOMEM       memory allocation failure
 2654  * @retval non-zero     some other unix error code
 2655  */
 2656 int
 2657 device_attach(device_t dev)
 2658 {
 2659         int error;
 2660 
 2661         device_sysctl_init(dev);
 2662         if (!device_is_quiet(dev))
 2663                 device_print_child(dev->parent, dev);
 2664         if ((error = DEVICE_ATTACH(dev)) != 0) {
 2665                 printf("device_attach: %s%d attach returned %d\n",
 2666                     dev->driver->name, dev->unit, error);
 2667                 /* Unset the class; set in device_probe_child */
 2668                 if (dev->devclass == NULL)
 2669                         device_set_devclass(dev, NULL);
 2670                 device_set_driver(dev, NULL);
 2671                 device_sysctl_fini(dev);
 2672                 dev->state = DS_NOTPRESENT;
 2673                 return (error);
 2674         }
 2675         device_sysctl_update(dev);
 2676         dev->state = DS_ATTACHED;
 2677         devadded(dev);
 2678         return (0);
 2679 }
 2680 
 2681 /**
 2682  * @brief Detach a driver from a device
 2683  *
 2684  * This function is a wrapper around the DEVICE_DETACH() driver
 2685  * method. If the call to DEVICE_DETACH() succeeds, it calls
 2686  * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
 2687  * notification event for user-based device management services and
 2688  * cleans up the device's sysctl tree.
 2689  *
 2690  * @param dev           the device to un-initialise
 2691  *
 2692  * @retval 0            success
 2693  * @retval ENXIO        no driver was found
 2694  * @retval ENOMEM       memory allocation failure
 2695  * @retval non-zero     some other unix error code
 2696  */
 2697 int
 2698 device_detach(device_t dev)
 2699 {
 2700         int error;
 2701 
 2702         GIANT_REQUIRED;
 2703 
 2704         PDEBUG(("%s", DEVICENAME(dev)));
 2705         if (dev->state == DS_BUSY)
 2706                 return (EBUSY);
 2707         if (dev->state != DS_ATTACHED)
 2708                 return (0);
 2709 
 2710         if ((error = DEVICE_DETACH(dev)) != 0)
 2711                 return (error);
 2712         devremoved(dev);
 2713         if (!device_is_quiet(dev))
 2714                 device_printf(dev, "detached\n");
 2715         if (dev->parent)
 2716                 BUS_CHILD_DETACHED(dev->parent, dev);
 2717 
 2718         if (!(dev->flags & DF_FIXEDCLASS))
 2719                 devclass_delete_device(dev->devclass, dev);
 2720 
 2721         dev->state = DS_NOTPRESENT;
 2722         device_set_driver(dev, NULL);
 2723         device_set_desc(dev, NULL);
 2724         device_sysctl_fini(dev);
 2725 
 2726         return (0);
 2727 }
 2728 
 2729 /**
 2730  * @brief Tells a driver to quiesce itself.
 2731  *
 2732  * This function is a wrapper around the DEVICE_QUIESCE() driver
 2733  * method. If the call to DEVICE_QUIESCE() succeeds.
 2734  *
 2735  * @param dev           the device to quiesce
 2736  *
 2737  * @retval 0            success
 2738  * @retval ENXIO        no driver was found
 2739  * @retval ENOMEM       memory allocation failure
 2740  * @retval non-zero     some other unix error code
 2741  */
 2742 int
 2743 device_quiesce(device_t dev)
 2744 {
 2745 
 2746         PDEBUG(("%s", DEVICENAME(dev)));
 2747         if (dev->state == DS_BUSY)
 2748                 return (EBUSY);
 2749         if (dev->state != DS_ATTACHED)
 2750                 return (0);
 2751 
 2752         return (DEVICE_QUIESCE(dev));
 2753 }
 2754 
 2755 /**
 2756  * @brief Notify a device of system shutdown
 2757  *
 2758  * This function calls the DEVICE_SHUTDOWN() driver method if the
 2759  * device currently has an attached driver.
 2760  *
 2761  * @returns the value returned by DEVICE_SHUTDOWN()
 2762  */
 2763 int
 2764 device_shutdown(device_t dev)
 2765 {
 2766         if (dev->state < DS_ATTACHED)
 2767                 return (0);
 2768         return (DEVICE_SHUTDOWN(dev));
 2769 }
 2770 
 2771 /**
 2772  * @brief Set the unit number of a device
 2773  *
 2774  * This function can be used to override the unit number used for a
 2775  * device (e.g. to wire a device to a pre-configured unit number).
 2776  */
 2777 int
 2778 device_set_unit(device_t dev, int unit)
 2779 {
 2780         devclass_t dc;
 2781         int err;
 2782 
 2783         dc = device_get_devclass(dev);
 2784         if (unit < dc->maxunit && dc->devices[unit])
 2785                 return (EBUSY);
 2786         err = devclass_delete_device(dc, dev);
 2787         if (err)
 2788                 return (err);
 2789         dev->unit = unit;
 2790         err = devclass_add_device(dc, dev);
 2791         if (err)
 2792                 return (err);
 2793 
 2794         bus_data_generation_update();
 2795         return (0);
 2796 }
 2797 
 2798 /*======================================*/
 2799 /*
 2800  * Some useful method implementations to make life easier for bus drivers.
 2801  */
 2802 
 2803 /**
 2804  * @brief Initialise a resource list.
 2805  *
 2806  * @param rl            the resource list to initialise
 2807  */
 2808 void
 2809 resource_list_init(struct resource_list *rl)
 2810 {
 2811         STAILQ_INIT(rl);
 2812 }
 2813 
 2814 /**
 2815  * @brief Reclaim memory used by a resource list.
 2816  *
 2817  * This function frees the memory for all resource entries on the list
 2818  * (if any).
 2819  *
 2820  * @param rl            the resource list to free               
 2821  */
 2822 void
 2823 resource_list_free(struct resource_list *rl)
 2824 {
 2825         struct resource_list_entry *rle;
 2826 
 2827         while ((rle = STAILQ_FIRST(rl)) != NULL) {
 2828                 if (rle->res)
 2829                         panic("resource_list_free: resource entry is busy");
 2830                 STAILQ_REMOVE_HEAD(rl, link);
 2831                 free(rle, M_BUS);
 2832         }
 2833 }
 2834 
 2835 /**
 2836  * @brief Add a resource entry.
 2837  *
 2838  * This function adds a resource entry using the given @p type, @p
 2839  * start, @p end and @p count values. A rid value is chosen by
 2840  * searching sequentially for the first unused rid starting at zero.
 2841  *
 2842  * @param rl            the resource list to edit
 2843  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2844  * @param start         the start address of the resource
 2845  * @param end           the end address of the resource
 2846  * @param count         XXX end-start+1
 2847  */
 2848 int
 2849 resource_list_add_next(struct resource_list *rl, int type, u_long start,
 2850     u_long end, u_long count)
 2851 {
 2852         int rid;
 2853 
 2854         rid = 0;
 2855         while (resource_list_find(rl, type, rid) != NULL)
 2856                 rid++;
 2857         resource_list_add(rl, type, rid, start, end, count);
 2858         return (rid);
 2859 }
 2860 
 2861 /**
 2862  * @brief Add or modify a resource entry.
 2863  *
 2864  * If an existing entry exists with the same type and rid, it will be
 2865  * modified using the given values of @p start, @p end and @p
 2866  * count. If no entry exists, a new one will be created using the
 2867  * given values.  The resource list entry that matches is then returned.
 2868  *
 2869  * @param rl            the resource list to edit
 2870  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2871  * @param rid           the resource identifier
 2872  * @param start         the start address of the resource
 2873  * @param end           the end address of the resource
 2874  * @param count         XXX end-start+1
 2875  */
 2876 struct resource_list_entry *
 2877 resource_list_add(struct resource_list *rl, int type, int rid,
 2878     u_long start, u_long end, u_long count)
 2879 {
 2880         struct resource_list_entry *rle;
 2881 
 2882         rle = resource_list_find(rl, type, rid);
 2883         if (!rle) {
 2884                 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
 2885                     M_NOWAIT);
 2886                 if (!rle)
 2887                         panic("resource_list_add: can't record entry");
 2888                 STAILQ_INSERT_TAIL(rl, rle, link);
 2889                 rle->type = type;
 2890                 rle->rid = rid;
 2891                 rle->res = NULL;
 2892         }
 2893 
 2894         if (rle->res)
 2895                 panic("resource_list_add: resource entry is busy");
 2896 
 2897         rle->start = start;
 2898         rle->end = end;
 2899         rle->count = count;
 2900         return (rle);
 2901 }
 2902 
 2903 /**
 2904  * @brief Find a resource entry by type and rid.
 2905  *
 2906  * @param rl            the resource list to search
 2907  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2908  * @param rid           the resource identifier
 2909  *
 2910  * @returns the resource entry pointer or NULL if there is no such
 2911  * entry.
 2912  */
 2913 struct resource_list_entry *
 2914 resource_list_find(struct resource_list *rl, int type, int rid)
 2915 {
 2916         struct resource_list_entry *rle;
 2917 
 2918         STAILQ_FOREACH(rle, rl, link) {
 2919                 if (rle->type == type && rle->rid == rid)
 2920                         return (rle);
 2921         }
 2922         return (NULL);
 2923 }
 2924 
 2925 /**
 2926  * @brief Delete a resource entry.
 2927  *
 2928  * @param rl            the resource list to edit
 2929  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2930  * @param rid           the resource identifier
 2931  */
 2932 void
 2933 resource_list_delete(struct resource_list *rl, int type, int rid)
 2934 {
 2935         struct resource_list_entry *rle = resource_list_find(rl, type, rid);
 2936 
 2937         if (rle) {
 2938                 if (rle->res != NULL)
 2939                         panic("resource_list_delete: resource has not been released");
 2940                 STAILQ_REMOVE(rl, rle, resource_list_entry, link);
 2941                 free(rle, M_BUS);
 2942         }
 2943 }
 2944 
 2945 /**
 2946  * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
 2947  *
 2948  * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
 2949  * and passing the allocation up to the parent of @p bus. This assumes
 2950  * that the first entry of @c device_get_ivars(child) is a struct
 2951  * resource_list. This also handles 'passthrough' allocations where a
 2952  * child is a remote descendant of bus by passing the allocation up to
 2953  * the parent of bus.
 2954  *
 2955  * Typically, a bus driver would store a list of child resources
 2956  * somewhere in the child device's ivars (see device_get_ivars()) and
 2957  * its implementation of BUS_ALLOC_RESOURCE() would find that list and
 2958  * then call resource_list_alloc() to perform the allocation.
 2959  *
 2960  * @param rl            the resource list to allocate from
 2961  * @param bus           the parent device of @p child
 2962  * @param child         the device which is requesting an allocation
 2963  * @param type          the type of resource to allocate
 2964  * @param rid           a pointer to the resource identifier
 2965  * @param start         hint at the start of the resource range - pass
 2966  *                      @c 0UL for any start address
 2967  * @param end           hint at the end of the resource range - pass
 2968  *                      @c ~0UL for any end address
 2969  * @param count         hint at the size of range required - pass @c 1
 2970  *                      for any size
 2971  * @param flags         any extra flags to control the resource
 2972  *                      allocation - see @c RF_XXX flags in
 2973  *                      <sys/rman.h> for details
 2974  * 
 2975  * @returns             the resource which was allocated or @c NULL if no
 2976  *                      resource could be allocated
 2977  */
 2978 struct resource *
 2979 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
 2980     int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
 2981 {
 2982         struct resource_list_entry *rle = NULL;
 2983         int passthrough = (device_get_parent(child) != bus);
 2984         int isdefault = (start == 0UL && end == ~0UL);
 2985 
 2986         if (passthrough) {
 2987                 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
 2988                     type, rid, start, end, count, flags));
 2989         }
 2990 
 2991         rle = resource_list_find(rl, type, *rid);
 2992 
 2993         if (!rle)
 2994                 return (NULL);          /* no resource of that type/rid */
 2995 
 2996         if (rle->res)
 2997                 panic("resource_list_alloc: resource entry is busy");
 2998 
 2999         if (isdefault) {
 3000                 start = rle->start;
 3001                 count = ulmax(count, rle->count);
 3002                 end = ulmax(rle->end, start + count - 1);
 3003         }
 3004 
 3005         rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
 3006             type, rid, start, end, count, flags);
 3007 
 3008         /*
 3009          * Record the new range.
 3010          */
 3011         if (rle->res) {
 3012                 rle->start = rman_get_start(rle->res);
 3013                 rle->end = rman_get_end(rle->res);
 3014                 rle->count = count;
 3015         }
 3016 
 3017         return (rle->res);
 3018 }
 3019 
 3020 /**
 3021  * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
 3022  * 
 3023  * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
 3024  * used with resource_list_alloc().
 3025  * 
 3026  * @param rl            the resource list which was allocated from
 3027  * @param bus           the parent device of @p child
 3028  * @param child         the device which is requesting a release
 3029  * @param type          the type of resource to allocate
 3030  * @param rid           the resource identifier
 3031  * @param res           the resource to release
 3032  * 
 3033  * @retval 0            success
 3034  * @retval non-zero     a standard unix error code indicating what
 3035  *                      error condition prevented the operation
 3036  */
 3037 int
 3038 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
 3039     int type, int rid, struct resource *res)
 3040 {
 3041         struct resource_list_entry *rle = NULL;
 3042         int passthrough = (device_get_parent(child) != bus);
 3043         int error;
 3044 
 3045         if (passthrough) {
 3046                 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
 3047                     type, rid, res));
 3048         }
 3049 
 3050         rle = resource_list_find(rl, type, rid);
 3051 
 3052         if (!rle)
 3053                 panic("resource_list_release: can't find resource");
 3054         if (!rle->res)
 3055                 panic("resource_list_release: resource entry is not busy");
 3056 
 3057         error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
 3058             type, rid, res);
 3059         if (error)
 3060                 return (error);
 3061 
 3062         rle->res = NULL;
 3063         return (0);
 3064 }
 3065 
 3066 /**
 3067  * @brief Print a description of resources in a resource list
 3068  *
 3069  * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
 3070  * The name is printed if at least one resource of the given type is available.
 3071  * The format is used to print resource start and end.
 3072  *
 3073  * @param rl            the resource list to print
 3074  * @param name          the name of @p type, e.g. @c "memory"
 3075  * @param type          type type of resource entry to print
 3076  * @param format        printf(9) format string to print resource
 3077  *                      start and end values
 3078  * 
 3079  * @returns             the number of characters printed
 3080  */
 3081 int
 3082 resource_list_print_type(struct resource_list *rl, const char *name, int type,
 3083     const char *format)
 3084 {
 3085         struct resource_list_entry *rle;
 3086         int printed, retval;
 3087 
 3088         printed = 0;
 3089         retval = 0;
 3090         /* Yes, this is kinda cheating */
 3091         STAILQ_FOREACH(rle, rl, link) {
 3092                 if (rle->type == type) {
 3093                         if (printed == 0)
 3094                                 retval += printf(" %s ", name);
 3095                         else
 3096                                 retval += printf(",");
 3097                         printed++;
 3098                         retval += printf(format, rle->start);
 3099                         if (rle->count > 1) {
 3100                                 retval += printf("-");
 3101                                 retval += printf(format, rle->start +
 3102                                                  rle->count - 1);
 3103                         }
 3104                 }
 3105         }
 3106         return (retval);
 3107 }
 3108 
 3109 /**
 3110  * @brief Releases all the resources in a list.
 3111  *
 3112  * @param rl            The resource list to purge.
 3113  * 
 3114  * @returns             nothing
 3115  */
 3116 void
 3117 resource_list_purge(struct resource_list *rl)
 3118 {
 3119         struct resource_list_entry *rle;
 3120 
 3121         while ((rle = STAILQ_FIRST(rl)) != NULL) {
 3122                 if (rle->res)
 3123                         bus_release_resource(rman_get_device(rle->res),
 3124                             rle->type, rle->rid, rle->res);
 3125                 STAILQ_REMOVE_HEAD(rl, link);
 3126                 free(rle, M_BUS);
 3127         }
 3128 }
 3129 
 3130 device_t
 3131 bus_generic_add_child(device_t dev, u_int order, const char *name, int unit)
 3132 {
 3133 
 3134         return (device_add_child_ordered(dev, order, name, unit));
 3135 }
 3136 
 3137 /**
 3138  * @brief Helper function for implementing DEVICE_PROBE()
 3139  *
 3140  * This function can be used to help implement the DEVICE_PROBE() for
 3141  * a bus (i.e. a device which has other devices attached to it). It
 3142  * calls the DEVICE_IDENTIFY() method of each driver in the device's
 3143  * devclass.
 3144  */
 3145 int
 3146 bus_generic_probe(device_t dev)
 3147 {
 3148         devclass_t dc = dev->devclass;
 3149         driverlink_t dl;
 3150 
 3151         TAILQ_FOREACH(dl, &dc->drivers, link) {
 3152                 /*
 3153                  * If this driver's pass is too high, then ignore it.
 3154                  * For most drivers in the default pass, this will
 3155                  * never be true.  For early-pass drivers they will
 3156                  * only call the identify routines of eligible drivers
 3157                  * when this routine is called.  Drivers for later
 3158                  * passes should have their identify routines called
 3159                  * on early-pass busses during BUS_NEW_PASS().
 3160                  */
 3161                 if (dl->pass > bus_current_pass)
 3162                                 continue;
 3163                 DEVICE_IDENTIFY(dl->driver, dev);
 3164         }
 3165 
 3166         return (0);
 3167 }
 3168 
 3169 /**
 3170  * @brief Helper function for implementing DEVICE_ATTACH()
 3171  *
 3172  * This function can be used to help implement the DEVICE_ATTACH() for
 3173  * a bus. It calls device_probe_and_attach() for each of the device's
 3174  * children.
 3175  */
 3176 int
 3177 bus_generic_attach(device_t dev)
 3178 {
 3179         device_t child;
 3180 
 3181         TAILQ_FOREACH(child, &dev->children, link) {
 3182                 device_probe_and_attach(child);
 3183         }
 3184 
 3185         return (0);
 3186 }
 3187 
 3188 /**
 3189  * @brief Helper function for implementing DEVICE_DETACH()
 3190  *
 3191  * This function can be used to help implement the DEVICE_DETACH() for
 3192  * a bus. It calls device_detach() for each of the device's
 3193  * children.
 3194  */
 3195 int
 3196 bus_generic_detach(device_t dev)
 3197 {
 3198         device_t child;
 3199         int error;
 3200 
 3201         if (dev->state != DS_ATTACHED)
 3202                 return (EBUSY);
 3203 
 3204         TAILQ_FOREACH(child, &dev->children, link) {
 3205                 if ((error = device_detach(child)) != 0)
 3206                         return (error);
 3207         }
 3208 
 3209         return (0);
 3210 }
 3211 
 3212 /**
 3213  * @brief Helper function for implementing DEVICE_SHUTDOWN()
 3214  *
 3215  * This function can be used to help implement the DEVICE_SHUTDOWN()
 3216  * for a bus. It calls device_shutdown() for each of the device's
 3217  * children.
 3218  */
 3219 int
 3220 bus_generic_shutdown(device_t dev)
 3221 {
 3222         device_t child;
 3223 
 3224         TAILQ_FOREACH(child, &dev->children, link) {
 3225                 device_shutdown(child);
 3226         }
 3227 
 3228         return (0);
 3229 }
 3230 
 3231 /**
 3232  * @brief Helper function for implementing DEVICE_SUSPEND()
 3233  *
 3234  * This function can be used to help implement the DEVICE_SUSPEND()
 3235  * for a bus. It calls DEVICE_SUSPEND() for each of the device's
 3236  * children. If any call to DEVICE_SUSPEND() fails, the suspend
 3237  * operation is aborted and any devices which were suspended are
 3238  * resumed immediately by calling their DEVICE_RESUME() methods.
 3239  */
 3240 int
 3241 bus_generic_suspend(device_t dev)
 3242 {
 3243         int             error;
 3244         device_t        child, child2;
 3245 
 3246         TAILQ_FOREACH(child, &dev->children, link) {
 3247                 error = DEVICE_SUSPEND(child);
 3248                 if (error) {
 3249                         for (child2 = TAILQ_FIRST(&dev->children);
 3250                              child2 && child2 != child;
 3251                              child2 = TAILQ_NEXT(child2, link))
 3252                                 DEVICE_RESUME(child2);
 3253                         return (error);
 3254                 }
 3255         }
 3256         return (0);
 3257 }
 3258 
 3259 /**
 3260  * @brief Helper function for implementing DEVICE_RESUME()
 3261  *
 3262  * This function can be used to help implement the DEVICE_RESUME() for
 3263  * a bus. It calls DEVICE_RESUME() on each of the device's children.
 3264  */
 3265 int
 3266 bus_generic_resume(device_t dev)
 3267 {
 3268         device_t        child;
 3269 
 3270         TAILQ_FOREACH(child, &dev->children, link) {
 3271                 DEVICE_RESUME(child);
 3272                 /* if resume fails, there's nothing we can usefully do... */
 3273         }
 3274         return (0);
 3275 }
 3276 
 3277 /**
 3278  * @brief Helper function for implementing BUS_PRINT_CHILD().
 3279  *
 3280  * This function prints the first part of the ascii representation of
 3281  * @p child, including its name, unit and description (if any - see
 3282  * device_set_desc()).
 3283  *
 3284  * @returns the number of characters printed
 3285  */
 3286 int
 3287 bus_print_child_header(device_t dev, device_t child)
 3288 {
 3289         int     retval = 0;
 3290 
 3291         if (device_get_desc(child)) {
 3292                 retval += device_printf(child, "<%s>", device_get_desc(child));
 3293         } else {
 3294                 retval += printf("%s", device_get_nameunit(child));
 3295         }
 3296 
 3297         return (retval);
 3298 }
 3299 
 3300 /**
 3301  * @brief Helper function for implementing BUS_PRINT_CHILD().
 3302  *
 3303  * This function prints the last part of the ascii representation of
 3304  * @p child, which consists of the string @c " on " followed by the
 3305  * name and unit of the @p dev.
 3306  *
 3307  * @returns the number of characters printed
 3308  */
 3309 int
 3310 bus_print_child_footer(device_t dev, device_t child)
 3311 {
 3312         return (printf(" on %s\n", device_get_nameunit(dev)));
 3313 }
 3314 
 3315 /**
 3316  * @brief Helper function for implementing BUS_PRINT_CHILD().
 3317  *
 3318  * This function simply calls bus_print_child_header() followed by
 3319  * bus_print_child_footer().
 3320  *
 3321  * @returns the number of characters printed
 3322  */
 3323 int
 3324 bus_generic_print_child(device_t dev, device_t child)
 3325 {
 3326         int     retval = 0;
 3327 
 3328         retval += bus_print_child_header(dev, child);
 3329         retval += bus_print_child_footer(dev, child);
 3330 
 3331         return (retval);
 3332 }
 3333 
 3334 /**
 3335  * @brief Stub function for implementing BUS_READ_IVAR().
 3336  * 
 3337  * @returns ENOENT
 3338  */
 3339 int
 3340 bus_generic_read_ivar(device_t dev, device_t child, int index,
 3341     uintptr_t * result)
 3342 {
 3343         return (ENOENT);
 3344 }
 3345 
 3346 /**
 3347  * @brief Stub function for implementing BUS_WRITE_IVAR().
 3348  * 
 3349  * @returns ENOENT
 3350  */
 3351 int
 3352 bus_generic_write_ivar(device_t dev, device_t child, int index,
 3353     uintptr_t value)
 3354 {
 3355         return (ENOENT);
 3356 }
 3357 
 3358 /**
 3359  * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
 3360  * 
 3361  * @returns NULL
 3362  */
 3363 struct resource_list *
 3364 bus_generic_get_resource_list(device_t dev, device_t child)
 3365 {
 3366         return (NULL);
 3367 }
 3368 
 3369 /**
 3370  * @brief Helper function for implementing BUS_DRIVER_ADDED().
 3371  *
 3372  * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
 3373  * DEVICE_IDENTIFY() method to allow it to add new children to the bus
 3374  * and then calls device_probe_and_attach() for each unattached child.
 3375  */
 3376 void
 3377 bus_generic_driver_added(device_t dev, driver_t *driver)
 3378 {
 3379         device_t child;
 3380 
 3381         DEVICE_IDENTIFY(driver, dev);
 3382         TAILQ_FOREACH(child, &dev->children, link) {
 3383                 if (child->state == DS_NOTPRESENT ||
 3384                     (child->flags & DF_REBID))
 3385                         device_probe_and_attach(child);
 3386         }
 3387 }
 3388 
 3389 /**
 3390  * @brief Helper function for implementing BUS_NEW_PASS().
 3391  *
 3392  * This implementing of BUS_NEW_PASS() first calls the identify
 3393  * routines for any drivers that probe at the current pass.  Then it
 3394  * walks the list of devices for this bus.  If a device is already
 3395  * attached, then it calls BUS_NEW_PASS() on that device.  If the
 3396  * device is not already attached, it attempts to attach a driver to
 3397  * it.
 3398  */
 3399 void
 3400 bus_generic_new_pass(device_t dev)
 3401 {
 3402         driverlink_t dl;
 3403         devclass_t dc;
 3404         device_t child;
 3405 
 3406         dc = dev->devclass;
 3407         TAILQ_FOREACH(dl, &dc->drivers, link) {
 3408                 if (dl->pass == bus_current_pass)
 3409                         DEVICE_IDENTIFY(dl->driver, dev);
 3410         }
 3411         TAILQ_FOREACH(child, &dev->children, link) {
 3412                 if (child->state >= DS_ATTACHED)
 3413                         BUS_NEW_PASS(child);
 3414                 else if (child->state == DS_NOTPRESENT)
 3415                         device_probe_and_attach(child);
 3416         }
 3417 }
 3418 
 3419 /**
 3420  * @brief Helper function for implementing BUS_SETUP_INTR().
 3421  *
 3422  * This simple implementation of BUS_SETUP_INTR() simply calls the
 3423  * BUS_SETUP_INTR() method of the parent of @p dev.
 3424  */
 3425 int
 3426 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
 3427     int flags, driver_filter_t *filter, driver_intr_t *intr, void *arg, 
 3428     void **cookiep)
 3429 {
 3430         /* Propagate up the bus hierarchy until someone handles it. */
 3431         if (dev->parent)
 3432                 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
 3433                     filter, intr, arg, cookiep));
 3434         return (EINVAL);
 3435 }
 3436 
 3437 /**
 3438  * @brief Helper function for implementing BUS_TEARDOWN_INTR().
 3439  *
 3440  * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
 3441  * BUS_TEARDOWN_INTR() method of the parent of @p dev.
 3442  */
 3443 int
 3444 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
 3445     void *cookie)
 3446 {
 3447         /* Propagate up the bus hierarchy until someone handles it. */
 3448         if (dev->parent)
 3449                 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
 3450         return (EINVAL);
 3451 }
 3452 
 3453 /**
 3454  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
 3455  *
 3456  * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
 3457  * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
 3458  */
 3459 struct resource *
 3460 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
 3461     u_long start, u_long end, u_long count, u_int flags)
 3462 {
 3463         /* Propagate up the bus hierarchy until someone handles it. */
 3464         if (dev->parent)
 3465                 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
 3466                     start, end, count, flags));
 3467         return (NULL);
 3468 }
 3469 
 3470 /**
 3471  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
 3472  *
 3473  * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
 3474  * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
 3475  */
 3476 int
 3477 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
 3478     struct resource *r)
 3479 {
 3480         /* Propagate up the bus hierarchy until someone handles it. */
 3481         if (dev->parent)
 3482                 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
 3483                     r));
 3484         return (EINVAL);
 3485 }
 3486 
 3487 /**
 3488  * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
 3489  *
 3490  * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
 3491  * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
 3492  */
 3493 int
 3494 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
 3495     struct resource *r)
 3496 {
 3497         /* Propagate up the bus hierarchy until someone handles it. */
 3498         if (dev->parent)
 3499                 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
 3500                     r));
 3501         return (EINVAL);
 3502 }
 3503 
 3504 /**
 3505  * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
 3506  *
 3507  * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
 3508  * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
 3509  */
 3510 int
 3511 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
 3512     int rid, struct resource *r)
 3513 {
 3514         /* Propagate up the bus hierarchy until someone handles it. */
 3515         if (dev->parent)
 3516                 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
 3517                     r));
 3518         return (EINVAL);
 3519 }
 3520 
 3521 /**
 3522  * @brief Helper function for implementing BUS_BIND_INTR().
 3523  *
 3524  * This simple implementation of BUS_BIND_INTR() simply calls the
 3525  * BUS_BIND_INTR() method of the parent of @p dev.
 3526  */
 3527 int
 3528 bus_generic_bind_intr(device_t dev, device_t child, struct resource *irq,
 3529     int cpu)
 3530 {
 3531 
 3532         /* Propagate up the bus hierarchy until someone handles it. */
 3533         if (dev->parent)
 3534                 return (BUS_BIND_INTR(dev->parent, child, irq, cpu));
 3535         return (EINVAL);
 3536 }
 3537 
 3538 /**
 3539  * @brief Helper function for implementing BUS_CONFIG_INTR().
 3540  *
 3541  * This simple implementation of BUS_CONFIG_INTR() simply calls the
 3542  * BUS_CONFIG_INTR() method of the parent of @p dev.
 3543  */
 3544 int
 3545 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
 3546     enum intr_polarity pol)
 3547 {
 3548 
 3549         /* Propagate up the bus hierarchy until someone handles it. */
 3550         if (dev->parent)
 3551                 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
 3552         return (EINVAL);
 3553 }
 3554 
 3555 /**
 3556  * @brief Helper function for implementing BUS_DESCRIBE_INTR().
 3557  *
 3558  * This simple implementation of BUS_DESCRIBE_INTR() simply calls the
 3559  * BUS_DESCRIBE_INTR() method of the parent of @p dev.
 3560  */
 3561 int
 3562 bus_generic_describe_intr(device_t dev, device_t child, struct resource *irq,
 3563     void *cookie, const char *descr)
 3564 {
 3565 
 3566         /* Propagate up the bus hierarchy until someone handles it. */
 3567         if (dev->parent)
 3568                 return (BUS_DESCRIBE_INTR(dev->parent, child, irq, cookie,
 3569                     descr));
 3570         return (EINVAL);
 3571 }
 3572 
 3573 /**
 3574  * @brief Helper function for implementing BUS_GET_DMA_TAG().
 3575  *
 3576  * This simple implementation of BUS_GET_DMA_TAG() simply calls the
 3577  * BUS_GET_DMA_TAG() method of the parent of @p dev.
 3578  */
 3579 bus_dma_tag_t
 3580 bus_generic_get_dma_tag(device_t dev, device_t child)
 3581 {
 3582 
 3583         /* Propagate up the bus hierarchy until someone handles it. */
 3584         if (dev->parent != NULL)
 3585                 return (BUS_GET_DMA_TAG(dev->parent, child));
 3586         return (NULL);
 3587 }
 3588 
 3589 /**
 3590  * @brief Helper function for implementing BUS_GET_RESOURCE().
 3591  *
 3592  * This implementation of BUS_GET_RESOURCE() uses the
 3593  * resource_list_find() function to do most of the work. It calls
 3594  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
 3595  * search.
 3596  */
 3597 int
 3598 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
 3599     u_long *startp, u_long *countp)
 3600 {
 3601         struct resource_list *          rl = NULL;
 3602         struct resource_list_entry *    rle = NULL;
 3603 
 3604         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3605         if (!rl)
 3606                 return (EINVAL);
 3607 
 3608         rle = resource_list_find(rl, type, rid);
 3609         if (!rle)
 3610                 return (ENOENT);
 3611 
 3612         if (startp)
 3613                 *startp = rle->start;
 3614         if (countp)
 3615                 *countp = rle->count;
 3616 
 3617         return (0);
 3618 }
 3619 
 3620 /**
 3621  * @brief Helper function for implementing BUS_SET_RESOURCE().
 3622  *
 3623  * This implementation of BUS_SET_RESOURCE() uses the
 3624  * resource_list_add() function to do most of the work. It calls
 3625  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
 3626  * edit.
 3627  */
 3628 int
 3629 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
 3630     u_long start, u_long count)
 3631 {
 3632         struct resource_list *          rl = NULL;
 3633 
 3634         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3635         if (!rl)
 3636                 return (EINVAL);
 3637 
 3638         resource_list_add(rl, type, rid, start, (start + count - 1), count);
 3639 
 3640         return (0);
 3641 }
 3642 
 3643 /**
 3644  * @brief Helper function for implementing BUS_DELETE_RESOURCE().
 3645  *
 3646  * This implementation of BUS_DELETE_RESOURCE() uses the
 3647  * resource_list_delete() function to do most of the work. It calls
 3648  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
 3649  * edit.
 3650  */
 3651 void
 3652 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
 3653 {
 3654         struct resource_list *          rl = NULL;
 3655 
 3656         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3657         if (!rl)
 3658                 return;
 3659 
 3660         resource_list_delete(rl, type, rid);
 3661 
 3662         return;
 3663 }
 3664 
 3665 /**
 3666  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
 3667  *
 3668  * This implementation of BUS_RELEASE_RESOURCE() uses the
 3669  * resource_list_release() function to do most of the work. It calls
 3670  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
 3671  */
 3672 int
 3673 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
 3674     int rid, struct resource *r)
 3675 {
 3676         struct resource_list *          rl = NULL;
 3677 
 3678         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3679         if (!rl)
 3680                 return (EINVAL);
 3681 
 3682         return (resource_list_release(rl, dev, child, type, rid, r));
 3683 }
 3684 
 3685 /**
 3686  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
 3687  *
 3688  * This implementation of BUS_ALLOC_RESOURCE() uses the
 3689  * resource_list_alloc() function to do most of the work. It calls
 3690  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
 3691  */
 3692 struct resource *
 3693 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
 3694     int *rid, u_long start, u_long end, u_long count, u_int flags)
 3695 {
 3696         struct resource_list *          rl = NULL;
 3697 
 3698         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3699         if (!rl)
 3700                 return (NULL);
 3701 
 3702         return (resource_list_alloc(rl, dev, child, type, rid,
 3703             start, end, count, flags));
 3704 }
 3705 
 3706 /**
 3707  * @brief Helper function for implementing BUS_CHILD_PRESENT().
 3708  *
 3709  * This simple implementation of BUS_CHILD_PRESENT() simply calls the
 3710  * BUS_CHILD_PRESENT() method of the parent of @p dev.
 3711  */
 3712 int
 3713 bus_generic_child_present(device_t dev, device_t child)
 3714 {
 3715         return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
 3716 }
 3717 
 3718 /*
 3719  * Some convenience functions to make it easier for drivers to use the
 3720  * resource-management functions.  All these really do is hide the
 3721  * indirection through the parent's method table, making for slightly
 3722  * less-wordy code.  In the future, it might make sense for this code
 3723  * to maintain some sort of a list of resources allocated by each device.
 3724  */
 3725 
 3726 int
 3727 bus_alloc_resources(device_t dev, struct resource_spec *rs,
 3728     struct resource **res)
 3729 {
 3730         int i;
 3731 
 3732         for (i = 0; rs[i].type != -1; i++)
 3733                 res[i] = NULL;
 3734         for (i = 0; rs[i].type != -1; i++) {
 3735                 res[i] = bus_alloc_resource_any(dev,
 3736                     rs[i].type, &rs[i].rid, rs[i].flags);
 3737                 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
 3738                         bus_release_resources(dev, rs, res);
 3739                         return (ENXIO);
 3740                 }
 3741         }
 3742         return (0);
 3743 }
 3744 
 3745 void
 3746 bus_release_resources(device_t dev, const struct resource_spec *rs,
 3747     struct resource **res)
 3748 {
 3749         int i;
 3750 
 3751         for (i = 0; rs[i].type != -1; i++)
 3752                 if (res[i] != NULL) {
 3753                         bus_release_resource(
 3754                             dev, rs[i].type, rs[i].rid, res[i]);
 3755                         res[i] = NULL;
 3756                 }
 3757 }
 3758 
 3759 /**
 3760  * @brief Wrapper function for BUS_ALLOC_RESOURCE().
 3761  *
 3762  * This function simply calls the BUS_ALLOC_RESOURCE() method of the
 3763  * parent of @p dev.
 3764  */
 3765 struct resource *
 3766 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
 3767     u_long count, u_int flags)
 3768 {
 3769         if (dev->parent == NULL)
 3770                 return (NULL);
 3771         return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
 3772             count, flags));
 3773 }
 3774 
 3775 /**
 3776  * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
 3777  *
 3778  * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
 3779  * parent of @p dev.
 3780  */
 3781 int
 3782 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
 3783 {
 3784         if (dev->parent == NULL)
 3785                 return (EINVAL);
 3786         return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
 3787 }
 3788 
 3789 /**
 3790  * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
 3791  *
 3792  * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
 3793  * parent of @p dev.
 3794  */
 3795 int
 3796 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
 3797 {
 3798         if (dev->parent == NULL)
 3799                 return (EINVAL);
 3800         return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
 3801 }
 3802 
 3803 /**
 3804  * @brief Wrapper function for BUS_RELEASE_RESOURCE().
 3805  *
 3806  * This function simply calls the BUS_RELEASE_RESOURCE() method of the
 3807  * parent of @p dev.
 3808  */
 3809 int
 3810 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
 3811 {
 3812         if (dev->parent == NULL)
 3813                 return (EINVAL);
 3814         return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
 3815 }
 3816 
 3817 /**
 3818  * @brief Wrapper function for BUS_SETUP_INTR().
 3819  *
 3820  * This function simply calls the BUS_SETUP_INTR() method of the
 3821  * parent of @p dev.
 3822  */
 3823 int
 3824 bus_setup_intr(device_t dev, struct resource *r, int flags,
 3825     driver_filter_t filter, driver_intr_t handler, void *arg, void **cookiep)
 3826 {
 3827         int error;
 3828 
 3829         if (dev->parent == NULL)
 3830                 return (EINVAL);
 3831         error = BUS_SETUP_INTR(dev->parent, dev, r, flags, filter, handler,
 3832             arg, cookiep);
 3833         if (error != 0)
 3834                 return (error);
 3835         if (handler != NULL && !(flags & INTR_MPSAFE))
 3836                 device_printf(dev, "[GIANT-LOCKED]\n");
 3837         if (bootverbose && (flags & INTR_MPSAFE))
 3838                 device_printf(dev, "[MPSAFE]\n");
 3839         if (filter != NULL) {
 3840                 if (handler == NULL)
 3841                         device_printf(dev, "[FILTER]\n");
 3842                 else 
 3843                         device_printf(dev, "[FILTER+ITHREAD]\n");
 3844         } else 
 3845                 device_printf(dev, "[ITHREAD]\n");
 3846         return (0);
 3847 }
 3848 
 3849 /**
 3850  * @brief Wrapper function for BUS_TEARDOWN_INTR().
 3851  *
 3852  * This function simply calls the BUS_TEARDOWN_INTR() method of the
 3853  * parent of @p dev.
 3854  */
 3855 int
 3856 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
 3857 {
 3858         if (dev->parent == NULL)
 3859                 return (EINVAL);
 3860         return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
 3861 }
 3862 
 3863 /**
 3864  * @brief Wrapper function for BUS_BIND_INTR().
 3865  *
 3866  * This function simply calls the BUS_BIND_INTR() method of the
 3867  * parent of @p dev.
 3868  */
 3869 int
 3870 bus_bind_intr(device_t dev, struct resource *r, int cpu)
 3871 {
 3872         if (dev->parent == NULL)
 3873                 return (EINVAL);
 3874         return (BUS_BIND_INTR(dev->parent, dev, r, cpu));
 3875 }
 3876 
 3877 /**
 3878  * @brief Wrapper function for BUS_DESCRIBE_INTR().
 3879  *
 3880  * This function first formats the requested description into a
 3881  * temporary buffer and then calls the BUS_DESCRIBE_INTR() method of
 3882  * the parent of @p dev.
 3883  */
 3884 int
 3885 bus_describe_intr(device_t dev, struct resource *irq, void *cookie,
 3886     const char *fmt, ...)
 3887 {
 3888         va_list ap;
 3889         char descr[MAXCOMLEN + 1];
 3890 
 3891         if (dev->parent == NULL)
 3892                 return (EINVAL);
 3893         va_start(ap, fmt);
 3894         vsnprintf(descr, sizeof(descr), fmt, ap);
 3895         va_end(ap);
 3896         return (BUS_DESCRIBE_INTR(dev->parent, dev, irq, cookie, descr));
 3897 }
 3898 
 3899 /**
 3900  * @brief Wrapper function for BUS_SET_RESOURCE().
 3901  *
 3902  * This function simply calls the BUS_SET_RESOURCE() method of the
 3903  * parent of @p dev.
 3904  */
 3905 int
 3906 bus_set_resource(device_t dev, int type, int rid,
 3907     u_long start, u_long count)
 3908 {
 3909         return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3910             start, count));
 3911 }
 3912 
 3913 /**
 3914  * @brief Wrapper function for BUS_GET_RESOURCE().
 3915  *
 3916  * This function simply calls the BUS_GET_RESOURCE() method of the
 3917  * parent of @p dev.
 3918  */
 3919 int
 3920 bus_get_resource(device_t dev, int type, int rid,
 3921     u_long *startp, u_long *countp)
 3922 {
 3923         return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3924             startp, countp));
 3925 }
 3926 
 3927 /**
 3928  * @brief Wrapper function for BUS_GET_RESOURCE().
 3929  *
 3930  * This function simply calls the BUS_GET_RESOURCE() method of the
 3931  * parent of @p dev and returns the start value.
 3932  */
 3933 u_long
 3934 bus_get_resource_start(device_t dev, int type, int rid)
 3935 {
 3936         u_long start, count;
 3937         int error;
 3938 
 3939         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3940             &start, &count);
 3941         if (error)
 3942                 return (0);
 3943         return (start);
 3944 }
 3945 
 3946 /**
 3947  * @brief Wrapper function for BUS_GET_RESOURCE().
 3948  *
 3949  * This function simply calls the BUS_GET_RESOURCE() method of the
 3950  * parent of @p dev and returns the count value.
 3951  */
 3952 u_long
 3953 bus_get_resource_count(device_t dev, int type, int rid)
 3954 {
 3955         u_long start, count;
 3956         int error;
 3957 
 3958         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3959             &start, &count);
 3960         if (error)
 3961                 return (0);
 3962         return (count);
 3963 }
 3964 
 3965 /**
 3966  * @brief Wrapper function for BUS_DELETE_RESOURCE().
 3967  *
 3968  * This function simply calls the BUS_DELETE_RESOURCE() method of the
 3969  * parent of @p dev.
 3970  */
 3971 void
 3972 bus_delete_resource(device_t dev, int type, int rid)
 3973 {
 3974         BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
 3975 }
 3976 
 3977 /**
 3978  * @brief Wrapper function for BUS_CHILD_PRESENT().
 3979  *
 3980  * This function simply calls the BUS_CHILD_PRESENT() method of the
 3981  * parent of @p dev.
 3982  */
 3983 int
 3984 bus_child_present(device_t child)
 3985 {
 3986         return (BUS_CHILD_PRESENT(device_get_parent(child), child));
 3987 }
 3988 
 3989 /**
 3990  * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
 3991  *
 3992  * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
 3993  * parent of @p dev.
 3994  */
 3995 int
 3996 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
 3997 {
 3998         device_t parent;
 3999 
 4000         parent = device_get_parent(child);
 4001         if (parent == NULL) {
 4002                 *buf = '\0';
 4003                 return (0);
 4004         }
 4005         return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
 4006 }
 4007 
 4008 /**
 4009  * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
 4010  *
 4011  * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
 4012  * parent of @p dev.
 4013  */
 4014 int
 4015 bus_child_location_str(device_t child, char *buf, size_t buflen)
 4016 {
 4017         device_t parent;
 4018 
 4019         parent = device_get_parent(child);
 4020         if (parent == NULL) {
 4021                 *buf = '\0';
 4022                 return (0);
 4023         }
 4024         return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
 4025 }
 4026 
 4027 /**
 4028  * @brief Wrapper function for BUS_GET_DMA_TAG().
 4029  *
 4030  * This function simply calls the BUS_GET_DMA_TAG() method of the
 4031  * parent of @p dev.
 4032  */
 4033 bus_dma_tag_t
 4034 bus_get_dma_tag(device_t dev)
 4035 {
 4036         device_t parent;
 4037 
 4038         parent = device_get_parent(dev);
 4039         if (parent == NULL)
 4040                 return (NULL);
 4041         return (BUS_GET_DMA_TAG(parent, dev));
 4042 }
 4043 
 4044 /* Resume all devices and then notify userland that we're up again. */
 4045 static int
 4046 root_resume(device_t dev)
 4047 {
 4048         int error;
 4049 
 4050         error = bus_generic_resume(dev);
 4051         if (error == 0)
 4052                 devctl_notify("kern", "power", "resume", NULL);
 4053         return (error);
 4054 }
 4055 
 4056 static int
 4057 root_print_child(device_t dev, device_t child)
 4058 {
 4059         int     retval = 0;
 4060 
 4061         retval += bus_print_child_header(dev, child);
 4062         retval += printf("\n");
 4063 
 4064         return (retval);
 4065 }
 4066 
 4067 static int
 4068 root_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
 4069     driver_filter_t *filter, driver_intr_t *intr, void *arg, void **cookiep)
 4070 {
 4071         /*
 4072          * If an interrupt mapping gets to here something bad has happened.
 4073          */
 4074         panic("root_setup_intr");
 4075 }
 4076 
 4077 /*
 4078  * If we get here, assume that the device is permanant and really is
 4079  * present in the system.  Removable bus drivers are expected to intercept
 4080  * this call long before it gets here.  We return -1 so that drivers that
 4081  * really care can check vs -1 or some ERRNO returned higher in the food
 4082  * chain.
 4083  */
 4084 static int
 4085 root_child_present(device_t dev, device_t child)
 4086 {
 4087         return (-1);
 4088 }
 4089 
 4090 static kobj_method_t root_methods[] = {
 4091         /* Device interface */
 4092         KOBJMETHOD(device_shutdown,     bus_generic_shutdown),
 4093         KOBJMETHOD(device_suspend,      bus_generic_suspend),
 4094         KOBJMETHOD(device_resume,       root_resume),
 4095 
 4096         /* Bus interface */
 4097         KOBJMETHOD(bus_print_child,     root_print_child),
 4098         KOBJMETHOD(bus_read_ivar,       bus_generic_read_ivar),
 4099         KOBJMETHOD(bus_write_ivar,      bus_generic_write_ivar),
 4100         KOBJMETHOD(bus_setup_intr,      root_setup_intr),
 4101         KOBJMETHOD(bus_child_present,   root_child_present),
 4102 
 4103         KOBJMETHOD_END
 4104 };
 4105 
 4106 static driver_t root_driver = {
 4107         "root",
 4108         root_methods,
 4109         1,                      /* no softc */
 4110 };
 4111 
 4112 device_t        root_bus;
 4113 devclass_t      root_devclass;
 4114 
 4115 static int
 4116 root_bus_module_handler(module_t mod, int what, void* arg)
 4117 {
 4118         switch (what) {
 4119         case MOD_LOAD:
 4120                 TAILQ_INIT(&bus_data_devices);
 4121                 kobj_class_compile((kobj_class_t) &root_driver);
 4122                 root_bus = make_device(NULL, "root", 0);
 4123                 root_bus->desc = "System root bus";
 4124                 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
 4125                 root_bus->driver = &root_driver;
 4126                 root_bus->state = DS_ATTACHED;
 4127                 root_devclass = devclass_find_internal("root", NULL, FALSE);
 4128                 devinit();
 4129                 return (0);
 4130 
 4131         case MOD_SHUTDOWN:
 4132                 device_shutdown(root_bus);
 4133                 return (0);
 4134         default:
 4135                 return (EOPNOTSUPP);
 4136         }
 4137 
 4138         return (0);
 4139 }
 4140 
 4141 static moduledata_t root_bus_mod = {
 4142         "rootbus",
 4143         root_bus_module_handler,
 4144         NULL
 4145 };
 4146 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
 4147 
 4148 /**
 4149  * @brief Automatically configure devices
 4150  *
 4151  * This function begins the autoconfiguration process by calling
 4152  * device_probe_and_attach() for each child of the @c root0 device.
 4153  */ 
 4154 void
 4155 root_bus_configure(void)
 4156 {
 4157 
 4158         PDEBUG(("."));
 4159 
 4160         /* Eventually this will be split up, but this is sufficient for now. */
 4161         bus_set_pass(BUS_PASS_DEFAULT);
 4162 }
 4163 
 4164 /**
 4165  * @brief Module handler for registering device drivers
 4166  *
 4167  * This module handler is used to automatically register device
 4168  * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
 4169  * devclass_add_driver() for the driver described by the
 4170  * driver_module_data structure pointed to by @p arg
 4171  */
 4172 int
 4173 driver_module_handler(module_t mod, int what, void *arg)
 4174 {
 4175         struct driver_module_data *dmd;
 4176         devclass_t bus_devclass;
 4177         kobj_class_t driver;
 4178         int error, pass;
 4179 
 4180         dmd = (struct driver_module_data *)arg;
 4181         bus_devclass = devclass_find_internal(dmd->dmd_busname, NULL, TRUE);
 4182         error = 0;
 4183 
 4184         switch (what) {
 4185         case MOD_LOAD:
 4186                 if (dmd->dmd_chainevh)
 4187                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
 4188 
 4189                 pass = dmd->dmd_pass;
 4190                 driver = dmd->dmd_driver;
 4191                 PDEBUG(("Loading module: driver %s on bus %s (pass %d)",
 4192                     DRIVERNAME(driver), dmd->dmd_busname, pass));
 4193                 error = devclass_add_driver(bus_devclass, driver, pass,
 4194                     dmd->dmd_devclass);
 4195                 break;
 4196 
 4197         case MOD_UNLOAD:
 4198                 PDEBUG(("Unloading module: driver %s from bus %s",
 4199                     DRIVERNAME(dmd->dmd_driver),
 4200                     dmd->dmd_busname));
 4201                 error = devclass_delete_driver(bus_devclass,
 4202                     dmd->dmd_driver);
 4203 
 4204                 if (!error && dmd->dmd_chainevh)
 4205                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
 4206                 break;
 4207         case MOD_QUIESCE:
 4208                 PDEBUG(("Quiesce module: driver %s from bus %s",
 4209                     DRIVERNAME(dmd->dmd_driver),
 4210                     dmd->dmd_busname));
 4211                 error = devclass_quiesce_driver(bus_devclass,
 4212                     dmd->dmd_driver);
 4213 
 4214                 if (!error && dmd->dmd_chainevh)
 4215                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
 4216                 break;
 4217         default:
 4218                 error = EOPNOTSUPP;
 4219                 break;
 4220         }
 4221 
 4222         return (error);
 4223 }
 4224 
 4225 /**
 4226  * @brief Enumerate all hinted devices for this bus.
 4227  *
 4228  * Walks through the hints for this bus and calls the bus_hinted_child
 4229  * routine for each one it fines.  It searches first for the specific
 4230  * bus that's being probed for hinted children (eg isa0), and then for
 4231  * generic children (eg isa).
 4232  *
 4233  * @param       dev     bus device to enumerate
 4234  */
 4235 void
 4236 bus_enumerate_hinted_children(device_t bus)
 4237 {
 4238         int i;
 4239         const char *dname, *busname;
 4240         int dunit;
 4241 
 4242         /*
 4243          * enumerate all devices on the specific bus
 4244          */
 4245         busname = device_get_nameunit(bus);
 4246         i = 0;
 4247         while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
 4248                 BUS_HINTED_CHILD(bus, dname, dunit);
 4249 
 4250         /*
 4251          * and all the generic ones.
 4252          */
 4253         busname = device_get_name(bus);
 4254         i = 0;
 4255         while (resource_find_match(&i, &dname, &dunit, "at", busname) == 0)
 4256                 BUS_HINTED_CHILD(bus, dname, dunit);
 4257 }
 4258 
 4259 #ifdef BUS_DEBUG
 4260 
 4261 /* the _short versions avoid iteration by not calling anything that prints
 4262  * more than oneliners. I love oneliners.
 4263  */
 4264 
 4265 static void
 4266 print_device_short(device_t dev, int indent)
 4267 {
 4268         if (!dev)
 4269                 return;
 4270 
 4271         indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
 4272             dev->unit, dev->desc,
 4273             (dev->parent? "":"no "),
 4274             (TAILQ_EMPTY(&dev->children)? "no ":""),
 4275             (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
 4276             (dev->flags&DF_FIXEDCLASS? "fixed,":""),
 4277             (dev->flags&DF_WILDCARD? "wildcard,":""),
 4278             (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
 4279             (dev->flags&DF_REBID? "rebiddable,":""),
 4280             (dev->ivars? "":"no "),
 4281             (dev->softc? "":"no "),
 4282             dev->busy));
 4283 }
 4284 
 4285 static void
 4286 print_device(device_t dev, int indent)
 4287 {
 4288         if (!dev)
 4289                 return;
 4290 
 4291         print_device_short(dev, indent);
 4292 
 4293         indentprintf(("Parent:\n"));
 4294         print_device_short(dev->parent, indent+1);
 4295         indentprintf(("Driver:\n"));
 4296         print_driver_short(dev->driver, indent+1);
 4297         indentprintf(("Devclass:\n"));
 4298         print_devclass_short(dev->devclass, indent+1);
 4299 }
 4300 
 4301 void
 4302 print_device_tree_short(device_t dev, int indent)
 4303 /* print the device and all its children (indented) */
 4304 {
 4305         device_t child;
 4306 
 4307         if (!dev)
 4308                 return;
 4309 
 4310         print_device_short(dev, indent);
 4311 
 4312         TAILQ_FOREACH(child, &dev->children, link) {
 4313                 print_device_tree_short(child, indent+1);
 4314         }
 4315 }
 4316 
 4317 void
 4318 print_device_tree(device_t dev, int indent)
 4319 /* print the device and all its children (indented) */
 4320 {
 4321         device_t child;
 4322 
 4323         if (!dev)
 4324                 return;
 4325 
 4326         print_device(dev, indent);
 4327 
 4328         TAILQ_FOREACH(child, &dev->children, link) {
 4329                 print_device_tree(child, indent+1);
 4330         }
 4331 }
 4332 
 4333 static void
 4334 print_driver_short(driver_t *driver, int indent)
 4335 {
 4336         if (!driver)
 4337                 return;
 4338 
 4339         indentprintf(("driver %s: softc size = %zd\n",
 4340             driver->name, driver->size));
 4341 }
 4342 
 4343 static void
 4344 print_driver(driver_t *driver, int indent)
 4345 {
 4346         if (!driver)
 4347                 return;
 4348 
 4349         print_driver_short(driver, indent);
 4350 }
 4351 
 4352 
 4353 static void
 4354 print_driver_list(driver_list_t drivers, int indent)
 4355 {
 4356         driverlink_t driver;
 4357 
 4358         TAILQ_FOREACH(driver, &drivers, link) {
 4359                 print_driver(driver->driver, indent);
 4360         }
 4361 }
 4362 
 4363 static void
 4364 print_devclass_short(devclass_t dc, int indent)
 4365 {
 4366         if ( !dc )
 4367                 return;
 4368 
 4369         indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
 4370 }
 4371 
 4372 static void
 4373 print_devclass(devclass_t dc, int indent)
 4374 {
 4375         int i;
 4376 
 4377         if ( !dc )
 4378                 return;
 4379 
 4380         print_devclass_short(dc, indent);
 4381         indentprintf(("Drivers:\n"));
 4382         print_driver_list(dc->drivers, indent+1);
 4383 
 4384         indentprintf(("Devices:\n"));
 4385         for (i = 0; i < dc->maxunit; i++)
 4386                 if (dc->devices[i])
 4387                         print_device(dc->devices[i], indent+1);
 4388 }
 4389 
 4390 void
 4391 print_devclass_list_short(void)
 4392 {
 4393         devclass_t dc;
 4394 
 4395         printf("Short listing of devclasses, drivers & devices:\n");
 4396         TAILQ_FOREACH(dc, &devclasses, link) {
 4397                 print_devclass_short(dc, 0);
 4398         }
 4399 }
 4400 
 4401 void
 4402 print_devclass_list(void)
 4403 {
 4404         devclass_t dc;
 4405 
 4406         printf("Full listing of devclasses, drivers & devices:\n");
 4407         TAILQ_FOREACH(dc, &devclasses, link) {
 4408                 print_devclass(dc, 0);
 4409         }
 4410 }
 4411 
 4412 #endif
 4413 
 4414 /*
 4415  * User-space access to the device tree.
 4416  *
 4417  * We implement a small set of nodes:
 4418  *
 4419  * hw.bus                       Single integer read method to obtain the
 4420  *                              current generation count.
 4421  * hw.bus.devices               Reads the entire device tree in flat space.
 4422  * hw.bus.rman                  Resource manager interface
 4423  *
 4424  * We might like to add the ability to scan devclasses and/or drivers to
 4425  * determine what else is currently loaded/available.
 4426  */
 4427 
 4428 static int
 4429 sysctl_bus(SYSCTL_HANDLER_ARGS)
 4430 {
 4431         struct u_businfo        ubus;
 4432 
 4433         ubus.ub_version = BUS_USER_VERSION;
 4434         ubus.ub_generation = bus_data_generation;
 4435 
 4436         return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
 4437 }
 4438 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
 4439     "bus-related data");
 4440 
 4441 static int
 4442 sysctl_devices(SYSCTL_HANDLER_ARGS)
 4443 {
 4444         int                     *name = (int *)arg1;
 4445         u_int                   namelen = arg2;
 4446         int                     index;
 4447         struct device           *dev;
 4448         struct u_device         udev;   /* XXX this is a bit big */
 4449         int                     error;
 4450 
 4451         if (namelen != 2)
 4452                 return (EINVAL);
 4453 
 4454         if (bus_data_generation_check(name[0]))
 4455                 return (EINVAL);
 4456 
 4457         index = name[1];
 4458 
 4459         /*
 4460          * Scan the list of devices, looking for the requested index.
 4461          */
 4462         TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
 4463                 if (index-- == 0)
 4464                         break;
 4465         }
 4466         if (dev == NULL)
 4467                 return (ENOENT);
 4468 
 4469         /*
 4470          * Populate the return array.
 4471          */
 4472         bzero(&udev, sizeof(udev));
 4473         udev.dv_handle = (uintptr_t)dev;
 4474         udev.dv_parent = (uintptr_t)dev->parent;
 4475         if (dev->nameunit != NULL)
 4476                 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
 4477         if (dev->desc != NULL)
 4478                 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
 4479         if (dev->driver != NULL && dev->driver->name != NULL)
 4480                 strlcpy(udev.dv_drivername, dev->driver->name,
 4481                     sizeof(udev.dv_drivername));
 4482         bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
 4483         bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
 4484         udev.dv_devflags = dev->devflags;
 4485         udev.dv_flags = dev->flags;
 4486         udev.dv_state = dev->state;
 4487         error = SYSCTL_OUT(req, &udev, sizeof(udev));
 4488         return (error);
 4489 }
 4490 
 4491 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
 4492     "system device tree");
 4493 
 4494 int
 4495 bus_data_generation_check(int generation)
 4496 {
 4497         if (generation != bus_data_generation)
 4498                 return (1);
 4499 
 4500         /* XXX generate optimised lists here? */
 4501         return (0);
 4502 }
 4503 
 4504 void
 4505 bus_data_generation_update(void)
 4506 {
 4507         bus_data_generation++;
 4508 }
 4509 
 4510 int
 4511 bus_free_resource(device_t dev, int type, struct resource *r)
 4512 {
 4513         if (r == NULL)
 4514                 return (0);
 4515         return (bus_release_resource(dev, type, rman_get_rid(r), r));
 4516 }

Cache object: e55f610c082d74b14d23b0d0da6e3190


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.