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

Cache object: 2e47d67de3cf2cee4801e8a37538e8fd


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