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

Cache object: f3dd197b0fc80716af7a8a605151ed08


[ 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.