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.4/sys/kern/subr_bus.c 145955 2005-05-06 02:51:10Z 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 count, i;
 1043         device_t *list;
 1044 
 1045         count = devclass_get_count(dc);
 1046         list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
 1047         if (!list)
 1048                 return (ENOMEM);
 1049 
 1050         count = 0;
 1051         for (i = 0; i < dc->maxunit; i++) {
 1052                 if (dc->devices[i]) {
 1053                         list[count] = dc->devices[i];
 1054                         count++;
 1055                 }
 1056         }
 1057 
 1058         *devlistp = list;
 1059         *devcountp = count;
 1060 
 1061         return (0);
 1062 }
 1063 
 1064 /**
 1065  * @brief Get the number of devices in a devclass
 1066  *
 1067  * @param dc            the devclass to examine
 1068  */
 1069 int
 1070 devclass_get_count(devclass_t dc)
 1071 {
 1072         int count, i;
 1073 
 1074         count = 0;
 1075         for (i = 0; i < dc->maxunit; i++)
 1076                 if (dc->devices[i])
 1077                         count++;
 1078         return (count);
 1079 }
 1080 
 1081 /**
 1082  * @brief Get the maximum unit number used in a devclass
 1083  *
 1084  * @param dc            the devclass to examine
 1085  */
 1086 int
 1087 devclass_get_maxunit(devclass_t dc)
 1088 {
 1089         return (dc->maxunit);
 1090 }
 1091 
 1092 /**
 1093  * @brief Find a free unit number in a devclass
 1094  *
 1095  * This function searches for the first unused unit number greater
 1096  * that or equal to @p unit.
 1097  *
 1098  * @param dc            the devclass to examine
 1099  * @param unit          the first unit number to check
 1100  */
 1101 int
 1102 devclass_find_free_unit(devclass_t dc, int unit)
 1103 {
 1104         if (dc == NULL)
 1105                 return (unit);
 1106         while (unit < dc->maxunit && dc->devices[unit] != NULL)
 1107                 unit++;
 1108         return (unit);
 1109 }
 1110 
 1111 /**
 1112  * @brief Set the parent of a devclass
 1113  *
 1114  * The parent class is normally initialised automatically by
 1115  * DRIVER_MODULE().
 1116  *
 1117  * @param dc            the devclass to edit
 1118  * @param pdc           the new parent devclass
 1119  */
 1120 void
 1121 devclass_set_parent(devclass_t dc, devclass_t pdc)
 1122 {
 1123         dc->parent = pdc;
 1124 }
 1125 
 1126 /**
 1127  * @brief Get the parent of a devclass
 1128  *
 1129  * @param dc            the devclass to examine
 1130  */
 1131 devclass_t
 1132 devclass_get_parent(devclass_t dc)
 1133 {
 1134         return (dc->parent);
 1135 }
 1136 
 1137 struct sysctl_ctx_list *
 1138 devclass_get_sysctl_ctx(devclass_t dc)
 1139 {
 1140         return (&dc->sysctl_ctx);
 1141 }
 1142 
 1143 struct sysctl_oid *
 1144 devclass_get_sysctl_tree(devclass_t dc)
 1145 {
 1146         return (dc->sysctl_tree);
 1147 }
 1148 
 1149 /**
 1150  * @internal
 1151  * @brief Allocate a unit number
 1152  *
 1153  * On entry, @p *unitp is the desired unit number (or @c -1 if any
 1154  * will do). The allocated unit number is returned in @p *unitp.
 1155 
 1156  * @param dc            the devclass to allocate from
 1157  * @param unitp         points at the location for the allocated unit
 1158  *                      number
 1159  *
 1160  * @retval 0            success
 1161  * @retval EEXIST       the requested unit number is already allocated
 1162  * @retval ENOMEM       memory allocation failure
 1163  */
 1164 static int
 1165 devclass_alloc_unit(devclass_t dc, int *unitp)
 1166 {
 1167         int unit = *unitp;
 1168 
 1169         PDEBUG(("unit %d in devclass %s", unit, DEVCLANAME(dc)));
 1170 
 1171         /* If we were given a wired unit number, check for existing device */
 1172         /* XXX imp XXX */
 1173         if (unit != -1) {
 1174                 if (unit >= 0 && unit < dc->maxunit &&
 1175                     dc->devices[unit] != NULL) {
 1176                         if (bootverbose)
 1177                                 printf("%s: %s%d already exists; skipping it\n",
 1178                                     dc->name, dc->name, *unitp);
 1179                         return (EEXIST);
 1180                 }
 1181         } else {
 1182                 /* Unwired device, find the next available slot for it */
 1183                 unit = 0;
 1184                 while (unit < dc->maxunit && dc->devices[unit] != NULL)
 1185                         unit++;
 1186         }
 1187 
 1188         /*
 1189          * We've selected a unit beyond the length of the table, so let's
 1190          * extend the table to make room for all units up to and including
 1191          * this one.
 1192          */
 1193         if (unit >= dc->maxunit) {
 1194                 device_t *newlist;
 1195                 int newsize;
 1196 
 1197                 newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
 1198                 newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
 1199                 if (!newlist)
 1200                         return (ENOMEM);
 1201                 bcopy(dc->devices, newlist, sizeof(device_t) * dc->maxunit);
 1202                 bzero(newlist + dc->maxunit,
 1203                     sizeof(device_t) * (newsize - dc->maxunit));
 1204                 if (dc->devices)
 1205                         free(dc->devices, M_BUS);
 1206                 dc->devices = newlist;
 1207                 dc->maxunit = newsize;
 1208         }
 1209         PDEBUG(("now: unit %d in devclass %s", unit, DEVCLANAME(dc)));
 1210 
 1211         *unitp = unit;
 1212         return (0);
 1213 }
 1214 
 1215 /**
 1216  * @internal
 1217  * @brief Add a device to a devclass
 1218  *
 1219  * A unit number is allocated for the device (using the device's
 1220  * preferred unit number if any) and the device is registered in the
 1221  * devclass. This allows the device to be looked up by its unit
 1222  * number, e.g. by decoding a dev_t minor number.
 1223  *
 1224  * @param dc            the devclass to add to
 1225  * @param dev           the device to add
 1226  *
 1227  * @retval 0            success
 1228  * @retval EEXIST       the requested unit number is already allocated
 1229  * @retval ENOMEM       memory allocation failure
 1230  */
 1231 static int
 1232 devclass_add_device(devclass_t dc, device_t dev)
 1233 {
 1234         int buflen, error;
 1235 
 1236         PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
 1237 
 1238         buflen = snprintf(NULL, 0, "%s%d$", dc->name, dev->unit);
 1239         if (buflen < 0)
 1240                 return (ENOMEM);
 1241         dev->nameunit = malloc(buflen, M_BUS, M_NOWAIT|M_ZERO);
 1242         if (!dev->nameunit)
 1243                 return (ENOMEM);
 1244 
 1245         if ((error = devclass_alloc_unit(dc, &dev->unit)) != 0) {
 1246                 free(dev->nameunit, M_BUS);
 1247                 dev->nameunit = NULL;
 1248                 return (error);
 1249         }
 1250         dc->devices[dev->unit] = dev;
 1251         dev->devclass = dc;
 1252         snprintf(dev->nameunit, buflen, "%s%d", dc->name, dev->unit);
 1253 
 1254         return (0);
 1255 }
 1256 
 1257 /**
 1258  * @internal
 1259  * @brief Delete a device from a devclass
 1260  *
 1261  * The device is removed from the devclass's device list and its unit
 1262  * number is freed.
 1263 
 1264  * @param dc            the devclass to delete from
 1265  * @param dev           the device to delete
 1266  *
 1267  * @retval 0            success
 1268  */
 1269 static int
 1270 devclass_delete_device(devclass_t dc, device_t dev)
 1271 {
 1272         if (!dc || !dev)
 1273                 return (0);
 1274 
 1275         PDEBUG(("%s in devclass %s", DEVICENAME(dev), DEVCLANAME(dc)));
 1276 
 1277         if (dev->devclass != dc || dc->devices[dev->unit] != dev)
 1278                 panic("devclass_delete_device: inconsistent device class");
 1279         dc->devices[dev->unit] = NULL;
 1280         if (dev->flags & DF_WILDCARD)
 1281                 dev->unit = -1;
 1282         dev->devclass = NULL;
 1283         free(dev->nameunit, M_BUS);
 1284         dev->nameunit = NULL;
 1285 
 1286         return (0);
 1287 }
 1288 
 1289 /**
 1290  * @internal
 1291  * @brief Make a new device and add it as a child of @p parent
 1292  *
 1293  * @param parent        the parent of the new device
 1294  * @param name          the devclass name of the new device or @c NULL
 1295  *                      to leave the devclass unspecified
 1296  * @parem unit          the unit number of the new device of @c -1 to
 1297  *                      leave the unit number unspecified
 1298  *
 1299  * @returns the new device
 1300  */
 1301 static device_t
 1302 make_device(device_t parent, const char *name, int unit)
 1303 {
 1304         device_t dev;
 1305         devclass_t dc;
 1306 
 1307         PDEBUG(("%s at %s as unit %d", name, DEVICENAME(parent), unit));
 1308 
 1309         if (name) {
 1310                 dc = devclass_find_internal(name, 0, TRUE);
 1311                 if (!dc) {
 1312                         printf("make_device: can't find device class %s\n",
 1313                             name);
 1314                         return (NULL);
 1315                 }
 1316         } else {
 1317                 dc = NULL;
 1318         }
 1319 
 1320         dev = malloc(sizeof(struct device), M_BUS, M_NOWAIT|M_ZERO);
 1321         if (!dev)
 1322                 return (NULL);
 1323 
 1324         dev->parent = parent;
 1325         TAILQ_INIT(&dev->children);
 1326         kobj_init((kobj_t) dev, &null_class);
 1327         dev->driver = NULL;
 1328         dev->devclass = NULL;
 1329         dev->unit = unit;
 1330         dev->nameunit = NULL;
 1331         dev->desc = NULL;
 1332         dev->busy = 0;
 1333         dev->devflags = 0;
 1334         dev->flags = DF_ENABLED;
 1335         dev->order = 0;
 1336         if (unit == -1)
 1337                 dev->flags |= DF_WILDCARD;
 1338         if (name) {
 1339                 dev->flags |= DF_FIXEDCLASS;
 1340                 if (devclass_add_device(dc, dev)) {
 1341                         kobj_delete((kobj_t) dev, M_BUS);
 1342                         return (NULL);
 1343                 }
 1344         }
 1345         dev->ivars = NULL;
 1346         dev->softc = NULL;
 1347 
 1348         dev->state = DS_NOTPRESENT;
 1349 
 1350         TAILQ_INSERT_TAIL(&bus_data_devices, dev, devlink);
 1351         bus_data_generation_update();
 1352 
 1353         return (dev);
 1354 }
 1355 
 1356 /**
 1357  * @internal
 1358  * @brief Print a description of a device.
 1359  */
 1360 static int
 1361 device_print_child(device_t dev, device_t child)
 1362 {
 1363         int retval = 0;
 1364 
 1365         if (device_is_alive(child))
 1366                 retval += BUS_PRINT_CHILD(dev, child);
 1367         else
 1368                 retval += device_printf(child, " not found\n");
 1369 
 1370         return (retval);
 1371 }
 1372 
 1373 /**
 1374  * @brief Create a new device
 1375  *
 1376  * This creates a new device and adds it as a child of an existing
 1377  * parent device. The new device will be added after the last existing
 1378  * child with order zero.
 1379  * 
 1380  * @param dev           the device which will be the parent of the
 1381  *                      new child device
 1382  * @param name          devclass name for new device or @c NULL if not
 1383  *                      specified
 1384  * @param unit          unit number for new device or @c -1 if not
 1385  *                      specified
 1386  * 
 1387  * @returns             the new device
 1388  */
 1389 device_t
 1390 device_add_child(device_t dev, const char *name, int unit)
 1391 {
 1392         return (device_add_child_ordered(dev, 0, name, unit));
 1393 }
 1394 
 1395 /**
 1396  * @brief Create a new device
 1397  *
 1398  * This creates a new device and adds it as a child of an existing
 1399  * parent device. The new device will be added after the last existing
 1400  * child with the same order.
 1401  * 
 1402  * @param dev           the device which will be the parent of the
 1403  *                      new child device
 1404  * @param order         a value which is used to partially sort the
 1405  *                      children of @p dev - devices created using
 1406  *                      lower values of @p order appear first in @p
 1407  *                      dev's list of children
 1408  * @param name          devclass name for new device or @c NULL if not
 1409  *                      specified
 1410  * @param unit          unit number for new device or @c -1 if not
 1411  *                      specified
 1412  * 
 1413  * @returns             the new device
 1414  */
 1415 device_t
 1416 device_add_child_ordered(device_t dev, int order, const char *name, int unit)
 1417 {
 1418         device_t child;
 1419         device_t place;
 1420 
 1421         PDEBUG(("%s at %s with order %d as unit %d",
 1422             name, DEVICENAME(dev), order, unit));
 1423 
 1424         child = make_device(dev, name, unit);
 1425         if (child == NULL)
 1426                 return (child);
 1427         child->order = order;
 1428 
 1429         TAILQ_FOREACH(place, &dev->children, link) {
 1430                 if (place->order > order)
 1431                         break;
 1432         }
 1433 
 1434         if (place) {
 1435                 /*
 1436                  * The device 'place' is the first device whose order is
 1437                  * greater than the new child.
 1438                  */
 1439                 TAILQ_INSERT_BEFORE(place, child, link);
 1440         } else {
 1441                 /*
 1442                  * The new child's order is greater or equal to the order of
 1443                  * any existing device. Add the child to the tail of the list.
 1444                  */
 1445                 TAILQ_INSERT_TAIL(&dev->children, child, link);
 1446         }
 1447 
 1448         bus_data_generation_update();
 1449         return (child);
 1450 }
 1451 
 1452 /**
 1453  * @brief Delete a device
 1454  *
 1455  * This function deletes a device along with all of its children. If
 1456  * the device currently has a driver attached to it, the device is
 1457  * detached first using device_detach().
 1458  * 
 1459  * @param dev           the parent device
 1460  * @param child         the device to delete
 1461  *
 1462  * @retval 0            success
 1463  * @retval non-zero     a unit error code describing the error
 1464  */
 1465 int
 1466 device_delete_child(device_t dev, device_t child)
 1467 {
 1468         int error;
 1469         device_t grandchild;
 1470 
 1471         PDEBUG(("%s from %s", DEVICENAME(child), DEVICENAME(dev)));
 1472 
 1473         /* remove children first */
 1474         while ( (grandchild = TAILQ_FIRST(&child->children)) ) {
 1475                 error = device_delete_child(child, grandchild);
 1476                 if (error)
 1477                         return (error);
 1478         }
 1479 
 1480         if ((error = device_detach(child)) != 0)
 1481                 return (error);
 1482         if (child->devclass)
 1483                 devclass_delete_device(child->devclass, child);
 1484         TAILQ_REMOVE(&dev->children, child, link);
 1485         TAILQ_REMOVE(&bus_data_devices, child, devlink);
 1486         kobj_delete((kobj_t) child, M_BUS);
 1487 
 1488         bus_data_generation_update();
 1489         return (0);
 1490 }
 1491 
 1492 /**
 1493  * @brief Find a device given a unit number
 1494  *
 1495  * This is similar to devclass_get_devices() but only searches for
 1496  * devices which have @p dev as a parent.
 1497  *
 1498  * @param dev           the parent device to search
 1499  * @param unit          the unit number to search for.  If the unit is -1,
 1500  *                      return the first child of @p dev which has name
 1501  *                      @p classname (that is, the one with the lowest unit.)
 1502  *
 1503  * @returns             the device with the given unit number or @c
 1504  *                      NULL if there is no such device
 1505  */
 1506 device_t
 1507 device_find_child(device_t dev, const char *classname, int unit)
 1508 {
 1509         devclass_t dc;
 1510         device_t child;
 1511 
 1512         dc = devclass_find(classname);
 1513         if (!dc)
 1514                 return (NULL);
 1515 
 1516         if (unit != -1) {
 1517                 child = devclass_get_device(dc, unit);
 1518                 if (child && child->parent == dev)
 1519                         return (child);
 1520         } else {
 1521                 for (unit = 0; unit <= devclass_get_maxunit(dc); unit++) {
 1522                         child = devclass_get_device(dc, unit);
 1523                         if (child && child->parent == dev)
 1524                                 return (child);
 1525                 }
 1526         }
 1527         return (NULL);
 1528 }
 1529 
 1530 /**
 1531  * @internal
 1532  */
 1533 static driverlink_t
 1534 first_matching_driver(devclass_t dc, device_t dev)
 1535 {
 1536         if (dev->devclass)
 1537                 return (devclass_find_driver_internal(dc, dev->devclass->name));
 1538         return (TAILQ_FIRST(&dc->drivers));
 1539 }
 1540 
 1541 /**
 1542  * @internal
 1543  */
 1544 static driverlink_t
 1545 next_matching_driver(devclass_t dc, device_t dev, driverlink_t last)
 1546 {
 1547         if (dev->devclass) {
 1548                 driverlink_t dl;
 1549                 for (dl = TAILQ_NEXT(last, link); dl; dl = TAILQ_NEXT(dl, link))
 1550                         if (!strcmp(dev->devclass->name, dl->driver->name))
 1551                                 return (dl);
 1552                 return (NULL);
 1553         }
 1554         return (TAILQ_NEXT(last, link));
 1555 }
 1556 
 1557 /**
 1558  * @internal
 1559  */
 1560 static int
 1561 device_probe_child(device_t dev, device_t child)
 1562 {
 1563         devclass_t dc;
 1564         driverlink_t best = 0;
 1565         driverlink_t dl;
 1566         int result, pri = 0;
 1567         int hasclass = (child->devclass != 0);
 1568 
 1569         dc = dev->devclass;
 1570         if (!dc)
 1571                 panic("device_probe_child: parent device has no devclass");
 1572 
 1573         if (child->state == DS_ALIVE)
 1574                 return (0);
 1575 
 1576         for (; dc; dc = dc->parent) {
 1577                 for (dl = first_matching_driver(dc, child);
 1578                      dl;
 1579                      dl = next_matching_driver(dc, child, dl)) {
 1580                         PDEBUG(("Trying %s", DRIVERNAME(dl->driver)));
 1581                         device_set_driver(child, dl->driver);
 1582                         if (!hasclass)
 1583                                 device_set_devclass(child, dl->driver->name);
 1584 
 1585                         /* Fetch any flags for the device before probing. */
 1586                         resource_int_value(dl->driver->name, child->unit,
 1587                             "flags", &child->devflags);
 1588 
 1589                         result = DEVICE_PROBE(child);
 1590 
 1591                         /* Reset flags and devclass before the next probe. */
 1592                         child->devflags = 0;
 1593                         if (!hasclass)
 1594                                 device_set_devclass(child, 0);
 1595 
 1596                         /*
 1597                          * If the driver returns SUCCESS, there can be
 1598                          * no higher match for this device.
 1599                          */
 1600                         if (result == 0) {
 1601                                 best = dl;
 1602                                 pri = 0;
 1603                                 break;
 1604                         }
 1605 
 1606                         /*
 1607                          * The driver returned an error so it
 1608                          * certainly doesn't match.
 1609                          */
 1610                         if (result > 0) {
 1611                                 device_set_driver(child, 0);
 1612                                 continue;
 1613                         }
 1614 
 1615                         /*
 1616                          * A priority lower than SUCCESS, remember the
 1617                          * best matching driver. Initialise the value
 1618                          * of pri for the first match.
 1619                          */
 1620                         if (best == 0 || result > pri) {
 1621                                 best = dl;
 1622                                 pri = result;
 1623                                 continue;
 1624                         }
 1625                 }
 1626                 /*
 1627                  * If we have an unambiguous match in this devclass,
 1628                  * don't look in the parent.
 1629                  */
 1630                 if (best && pri == 0)
 1631                         break;
 1632         }
 1633 
 1634         /*
 1635          * If we found a driver, change state and initialise the devclass.
 1636          */
 1637         if (best) {
 1638                 /* Set the winning driver, devclass, and flags. */
 1639                 if (!child->devclass)
 1640                         device_set_devclass(child, best->driver->name);
 1641                 device_set_driver(child, best->driver);
 1642                 resource_int_value(best->driver->name, child->unit,
 1643                     "flags", &child->devflags);
 1644 
 1645                 if (pri < 0) {
 1646                         /*
 1647                          * A bit bogus. Call the probe method again to make
 1648                          * sure that we have the right description.
 1649                          */
 1650                         DEVICE_PROBE(child);
 1651                 }
 1652                 child->state = DS_ALIVE;
 1653 
 1654                 bus_data_generation_update();
 1655                 return (0);
 1656         }
 1657 
 1658         return (ENXIO);
 1659 }
 1660 
 1661 /**
 1662  * @brief Return the parent of a device
 1663  */
 1664 device_t
 1665 device_get_parent(device_t dev)
 1666 {
 1667         return (dev->parent);
 1668 }
 1669 
 1670 /**
 1671  * @brief Get a list of children of a device
 1672  *
 1673  * An array containing a list of all the children of the given device
 1674  * is allocated and returned in @p *devlistp. The number of devices
 1675  * in the array is returned in @p *devcountp. The caller should free
 1676  * the array using @c free(p, M_TEMP).
 1677  *
 1678  * @param dev           the device to examine
 1679  * @param devlistp      points at location for array pointer return
 1680  *                      value
 1681  * @param devcountp     points at location for array size return value
 1682  *
 1683  * @retval 0            success
 1684  * @retval ENOMEM       the array allocation failed
 1685  */
 1686 int
 1687 device_get_children(device_t dev, device_t **devlistp, int *devcountp)
 1688 {
 1689         int count;
 1690         device_t child;
 1691         device_t *list;
 1692 
 1693         count = 0;
 1694         TAILQ_FOREACH(child, &dev->children, link) {
 1695                 count++;
 1696         }
 1697 
 1698         list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
 1699         if (!list)
 1700                 return (ENOMEM);
 1701 
 1702         count = 0;
 1703         TAILQ_FOREACH(child, &dev->children, link) {
 1704                 list[count] = child;
 1705                 count++;
 1706         }
 1707 
 1708         *devlistp = list;
 1709         *devcountp = count;
 1710 
 1711         return (0);
 1712 }
 1713 
 1714 /**
 1715  * @brief Return the current driver for the device or @c NULL if there
 1716  * is no driver currently attached
 1717  */
 1718 driver_t *
 1719 device_get_driver(device_t dev)
 1720 {
 1721         return (dev->driver);
 1722 }
 1723 
 1724 /**
 1725  * @brief Return the current devclass for the device or @c NULL if
 1726  * there is none.
 1727  */
 1728 devclass_t
 1729 device_get_devclass(device_t dev)
 1730 {
 1731         return (dev->devclass);
 1732 }
 1733 
 1734 /**
 1735  * @brief Return the name of the device's devclass or @c NULL if there
 1736  * is none.
 1737  */
 1738 const char *
 1739 device_get_name(device_t dev)
 1740 {
 1741         if (dev != NULL && dev->devclass)
 1742                 return (devclass_get_name(dev->devclass));
 1743         return (NULL);
 1744 }
 1745 
 1746 /**
 1747  * @brief Return a string containing the device's devclass name
 1748  * followed by an ascii representation of the device's unit number
 1749  * (e.g. @c "foo2").
 1750  */
 1751 const char *
 1752 device_get_nameunit(device_t dev)
 1753 {
 1754         return (dev->nameunit);
 1755 }
 1756 
 1757 /**
 1758  * @brief Return the device's unit number.
 1759  */
 1760 int
 1761 device_get_unit(device_t dev)
 1762 {
 1763         return (dev->unit);
 1764 }
 1765 
 1766 /**
 1767  * @brief Return the device's description string
 1768  */
 1769 const char *
 1770 device_get_desc(device_t dev)
 1771 {
 1772         return (dev->desc);
 1773 }
 1774 
 1775 /**
 1776  * @brief Return the device's flags
 1777  */
 1778 u_int32_t
 1779 device_get_flags(device_t dev)
 1780 {
 1781         return (dev->devflags);
 1782 }
 1783 
 1784 struct sysctl_ctx_list *
 1785 device_get_sysctl_ctx(device_t dev)
 1786 {
 1787         return (&dev->sysctl_ctx);
 1788 }
 1789 
 1790 struct sysctl_oid *
 1791 device_get_sysctl_tree(device_t dev)
 1792 {
 1793         return (dev->sysctl_tree);
 1794 }
 1795 
 1796 /**
 1797  * @brief Print the name of the device followed by a colon and a space
 1798  *
 1799  * @returns the number of characters printed
 1800  */
 1801 int
 1802 device_print_prettyname(device_t dev)
 1803 {
 1804         const char *name = device_get_name(dev);
 1805 
 1806         if (name == 0)
 1807                 return (printf("unknown: "));
 1808         return (printf("%s%d: ", name, device_get_unit(dev)));
 1809 }
 1810 
 1811 /**
 1812  * @brief Print the name of the device followed by a colon, a space
 1813  * and the result of calling vprintf() with the value of @p fmt and
 1814  * the following arguments.
 1815  *
 1816  * @returns the number of characters printed
 1817  */
 1818 int
 1819 device_printf(device_t dev, const char * fmt, ...)
 1820 {
 1821         va_list ap;
 1822         int retval;
 1823 
 1824         retval = device_print_prettyname(dev);
 1825         va_start(ap, fmt);
 1826         retval += vprintf(fmt, ap);
 1827         va_end(ap);
 1828         return (retval);
 1829 }
 1830 
 1831 /**
 1832  * @internal
 1833  */
 1834 static void
 1835 device_set_desc_internal(device_t dev, const char* desc, int copy)
 1836 {
 1837         if (dev->desc && (dev->flags & DF_DESCMALLOCED)) {
 1838                 free(dev->desc, M_BUS);
 1839                 dev->flags &= ~DF_DESCMALLOCED;
 1840                 dev->desc = NULL;
 1841         }
 1842 
 1843         if (copy && desc) {
 1844                 dev->desc = malloc(strlen(desc) + 1, M_BUS, M_NOWAIT);
 1845                 if (dev->desc) {
 1846                         strcpy(dev->desc, desc);
 1847                         dev->flags |= DF_DESCMALLOCED;
 1848                 }
 1849         } else {
 1850                 /* Avoid a -Wcast-qual warning */
 1851                 dev->desc = (char *)(uintptr_t) desc;
 1852         }
 1853 
 1854         bus_data_generation_update();
 1855 }
 1856 
 1857 /**
 1858  * @brief Set the device's description
 1859  *
 1860  * The value of @c desc should be a string constant that will not
 1861  * change (at least until the description is changed in a subsequent
 1862  * call to device_set_desc() or device_set_desc_copy()).
 1863  */
 1864 void
 1865 device_set_desc(device_t dev, const char* desc)
 1866 {
 1867         device_set_desc_internal(dev, desc, FALSE);
 1868 }
 1869 
 1870 /**
 1871  * @brief Set the device's description
 1872  *
 1873  * The string pointed to by @c desc is copied. Use this function if
 1874  * the device description is generated, (e.g. with sprintf()).
 1875  */
 1876 void
 1877 device_set_desc_copy(device_t dev, const char* desc)
 1878 {
 1879         device_set_desc_internal(dev, desc, TRUE);
 1880 }
 1881 
 1882 /**
 1883  * @brief Set the device's flags
 1884  */
 1885 void
 1886 device_set_flags(device_t dev, u_int32_t flags)
 1887 {
 1888         dev->devflags = flags;
 1889 }
 1890 
 1891 /**
 1892  * @brief Return the device's softc field
 1893  *
 1894  * The softc is allocated and zeroed when a driver is attached, based
 1895  * on the size field of the driver.
 1896  */
 1897 void *
 1898 device_get_softc(device_t dev)
 1899 {
 1900         return (dev->softc);
 1901 }
 1902 
 1903 /**
 1904  * @brief Set the device's softc field
 1905  *
 1906  * Most drivers do not need to use this since the softc is allocated
 1907  * automatically when the driver is attached.
 1908  */
 1909 void
 1910 device_set_softc(device_t dev, void *softc)
 1911 {
 1912         if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC))
 1913                 free(dev->softc, M_BUS_SC);
 1914         dev->softc = softc;
 1915         if (dev->softc)
 1916                 dev->flags |= DF_EXTERNALSOFTC;
 1917         else
 1918                 dev->flags &= ~DF_EXTERNALSOFTC;
 1919 }
 1920 
 1921 /**
 1922  * @brief Get the device's ivars field
 1923  *
 1924  * The ivars field is used by the parent device to store per-device
 1925  * state (e.g. the physical location of the device or a list of
 1926  * resources).
 1927  */
 1928 void *
 1929 device_get_ivars(device_t dev)
 1930 {
 1931 
 1932         KASSERT(dev != NULL, ("device_get_ivars(NULL, ...)"));
 1933         return (dev->ivars);
 1934 }
 1935 
 1936 /**
 1937  * @brief Set the device's ivars field
 1938  */
 1939 void
 1940 device_set_ivars(device_t dev, void * ivars)
 1941 {
 1942 
 1943         KASSERT(dev != NULL, ("device_set_ivars(NULL, ...)"));
 1944         dev->ivars = ivars;
 1945 }
 1946 
 1947 /**
 1948  * @brief Return the device's state
 1949  */
 1950 device_state_t
 1951 device_get_state(device_t dev)
 1952 {
 1953         return (dev->state);
 1954 }
 1955 
 1956 /**
 1957  * @brief Set the DF_ENABLED flag for the device
 1958  */
 1959 void
 1960 device_enable(device_t dev)
 1961 {
 1962         dev->flags |= DF_ENABLED;
 1963 }
 1964 
 1965 /**
 1966  * @brief Clear the DF_ENABLED flag for the device
 1967  */
 1968 void
 1969 device_disable(device_t dev)
 1970 {
 1971         dev->flags &= ~DF_ENABLED;
 1972 }
 1973 
 1974 /**
 1975  * @brief Increment the busy counter for the device
 1976  */
 1977 void
 1978 device_busy(device_t dev)
 1979 {
 1980         if (dev->state < DS_ATTACHED)
 1981                 panic("device_busy: called for unattached device");
 1982         if (dev->busy == 0 && dev->parent)
 1983                 device_busy(dev->parent);
 1984         dev->busy++;
 1985         dev->state = DS_BUSY;
 1986 }
 1987 
 1988 /**
 1989  * @brief Decrement the busy counter for the device
 1990  */
 1991 void
 1992 device_unbusy(device_t dev)
 1993 {
 1994         if (dev->state != DS_BUSY)
 1995                 panic("device_unbusy: called for non-busy device");
 1996         dev->busy--;
 1997         if (dev->busy == 0) {
 1998                 if (dev->parent)
 1999                         device_unbusy(dev->parent);
 2000                 dev->state = DS_ATTACHED;
 2001         }
 2002 }
 2003 
 2004 /**
 2005  * @brief Set the DF_QUIET flag for the device
 2006  */
 2007 void
 2008 device_quiet(device_t dev)
 2009 {
 2010         dev->flags |= DF_QUIET;
 2011 }
 2012 
 2013 /**
 2014  * @brief Clear the DF_QUIET flag for the device
 2015  */
 2016 void
 2017 device_verbose(device_t dev)
 2018 {
 2019         dev->flags &= ~DF_QUIET;
 2020 }
 2021 
 2022 /**
 2023  * @brief Return non-zero if the DF_QUIET flag is set on the device
 2024  */
 2025 int
 2026 device_is_quiet(device_t dev)
 2027 {
 2028         return ((dev->flags & DF_QUIET) != 0);
 2029 }
 2030 
 2031 /**
 2032  * @brief Return non-zero if the DF_ENABLED flag is set on the device
 2033  */
 2034 int
 2035 device_is_enabled(device_t dev)
 2036 {
 2037         return ((dev->flags & DF_ENABLED) != 0);
 2038 }
 2039 
 2040 /**
 2041  * @brief Return non-zero if the device was successfully probed
 2042  */
 2043 int
 2044 device_is_alive(device_t dev)
 2045 {
 2046         return (dev->state >= DS_ALIVE);
 2047 }
 2048 
 2049 /**
 2050  * @brief Return non-zero if the device currently has a driver
 2051  * attached to it
 2052  */
 2053 int
 2054 device_is_attached(device_t dev)
 2055 {
 2056         return (dev->state >= DS_ATTACHED);
 2057 }
 2058 
 2059 /**
 2060  * @brief Set the devclass of a device
 2061  * @see devclass_add_device().
 2062  */
 2063 int
 2064 device_set_devclass(device_t dev, const char *classname)
 2065 {
 2066         devclass_t dc;
 2067         int error;
 2068 
 2069         if (!classname) {
 2070                 if (dev->devclass)
 2071                         devclass_delete_device(dev->devclass, dev);
 2072                 return (0);
 2073         }
 2074 
 2075         if (dev->devclass) {
 2076                 printf("device_set_devclass: device class already set\n");
 2077                 return (EINVAL);
 2078         }
 2079 
 2080         dc = devclass_find_internal(classname, 0, TRUE);
 2081         if (!dc)
 2082                 return (ENOMEM);
 2083 
 2084         error = devclass_add_device(dc, dev);
 2085 
 2086         bus_data_generation_update();
 2087         return (error);
 2088 }
 2089 
 2090 /**
 2091  * @brief Set the driver of a device
 2092  *
 2093  * @retval 0            success
 2094  * @retval EBUSY        the device already has a driver attached
 2095  * @retval ENOMEM       a memory allocation failure occurred
 2096  */
 2097 int
 2098 device_set_driver(device_t dev, driver_t *driver)
 2099 {
 2100         if (dev->state >= DS_ATTACHED)
 2101                 return (EBUSY);
 2102 
 2103         if (dev->driver == driver)
 2104                 return (0);
 2105 
 2106         if (dev->softc && !(dev->flags & DF_EXTERNALSOFTC)) {
 2107                 free(dev->softc, M_BUS_SC);
 2108                 dev->softc = NULL;
 2109         }
 2110         kobj_delete((kobj_t) dev, 0);
 2111         dev->driver = driver;
 2112         if (driver) {
 2113                 kobj_init((kobj_t) dev, (kobj_class_t) driver);
 2114                 if (!(dev->flags & DF_EXTERNALSOFTC) && driver->size > 0) {
 2115                         dev->softc = malloc(driver->size, M_BUS_SC,
 2116                             M_NOWAIT | M_ZERO);
 2117                         if (!dev->softc) {
 2118                                 kobj_delete((kobj_t) dev, 0);
 2119                                 kobj_init((kobj_t) dev, &null_class);
 2120                                 dev->driver = NULL;
 2121                                 return (ENOMEM);
 2122                         }
 2123                 }
 2124         } else {
 2125                 kobj_init((kobj_t) dev, &null_class);
 2126         }
 2127 
 2128         bus_data_generation_update();
 2129         return (0);
 2130 }
 2131 
 2132 /**
 2133  * @brief Probe a device and attach a driver if possible
 2134  *
 2135  * This function is the core of the device autoconfiguration
 2136  * system. Its purpose is to select a suitable driver for a device and
 2137  * then call that driver to initialise the hardware appropriately. The
 2138  * driver is selected by calling the DEVICE_PROBE() method of a set of
 2139  * candidate drivers and then choosing the driver which returned the
 2140  * best value. This driver is then attached to the device using
 2141  * device_attach().
 2142  *
 2143  * The set of suitable drivers is taken from the list of drivers in
 2144  * the parent device's devclass. If the device was originally created
 2145  * with a specific class name (see device_add_child()), only drivers
 2146  * with that name are probed, otherwise all drivers in the devclass
 2147  * are probed. If no drivers return successful probe values in the
 2148  * parent devclass, the search continues in the parent of that
 2149  * devclass (see devclass_get_parent()) if any.
 2150  *
 2151  * @param dev           the device to initialise
 2152  *
 2153  * @retval 0            success
 2154  * @retval ENXIO        no driver was found
 2155  * @retval ENOMEM       memory allocation failure
 2156  * @retval non-zero     some other unix error code
 2157  */
 2158 int
 2159 device_probe_and_attach(device_t dev)
 2160 {
 2161         int error;
 2162 
 2163         if (dev->state >= DS_ALIVE)
 2164                 return (0);
 2165 
 2166         if (!(dev->flags & DF_ENABLED)) {
 2167                 if (bootverbose) {
 2168                         device_print_prettyname(dev);
 2169                         printf("not probed (disabled)\n");
 2170                 }
 2171                 return (0);
 2172         }
 2173         if ((error = device_probe_child(dev->parent, dev)) != 0) {
 2174                 if (!(dev->flags & DF_DONENOMATCH)) {
 2175                         BUS_PROBE_NOMATCH(dev->parent, dev);
 2176                         devnomatch(dev);
 2177                         dev->flags |= DF_DONENOMATCH;
 2178                 }
 2179                 return (error);
 2180         }
 2181         error = device_attach(dev);
 2182 
 2183         return (error);
 2184 }
 2185 
 2186 /**
 2187  * @brief Attach a device driver to a device
 2188  *
 2189  * This function is a wrapper around the DEVICE_ATTACH() driver
 2190  * method. In addition to calling DEVICE_ATTACH(), it initialises the
 2191  * device's sysctl tree, optionally prints a description of the device
 2192  * and queues a notification event for user-based device management
 2193  * services.
 2194  *
 2195  * Normally this function is only called internally from
 2196  * device_probe_and_attach().
 2197  *
 2198  * @param dev           the device to initialise
 2199  *
 2200  * @retval 0            success
 2201  * @retval ENXIO        no driver was found
 2202  * @retval ENOMEM       memory allocation failure
 2203  * @retval non-zero     some other unix error code
 2204  */
 2205 int
 2206 device_attach(device_t dev)
 2207 {
 2208         int error;
 2209 
 2210         device_sysctl_init(dev);
 2211         if (!device_is_quiet(dev))
 2212                 device_print_child(dev->parent, dev);
 2213         if ((error = DEVICE_ATTACH(dev)) != 0) {
 2214                 printf("device_attach: %s%d attach returned %d\n",
 2215                     dev->driver->name, dev->unit, error);
 2216                 /* Unset the class; set in device_probe_child */
 2217                 if (dev->devclass == 0)
 2218                         device_set_devclass(dev, 0);
 2219                 device_set_driver(dev, NULL);
 2220                 device_sysctl_fini(dev);
 2221                 dev->state = DS_NOTPRESENT;
 2222                 return (error);
 2223         }
 2224         dev->state = DS_ATTACHED;
 2225         devadded(dev);
 2226         return (0);
 2227 }
 2228 
 2229 /**
 2230  * @brief Detach a driver from a device
 2231  *
 2232  * This function is a wrapper around the DEVICE_DETACH() driver
 2233  * method. If the call to DEVICE_DETACH() succeeds, it calls
 2234  * BUS_CHILD_DETACHED() for the parent of @p dev, queues a
 2235  * notification event for user-based device management services and
 2236  * cleans up the device's sysctl tree.
 2237  *
 2238  * @param dev           the device to un-initialise
 2239  *
 2240  * @retval 0            success
 2241  * @retval ENXIO        no driver was found
 2242  * @retval ENOMEM       memory allocation failure
 2243  * @retval non-zero     some other unix error code
 2244  */
 2245 int
 2246 device_detach(device_t dev)
 2247 {
 2248         int error;
 2249 
 2250         PDEBUG(("%s", DEVICENAME(dev)));
 2251         if (dev->state == DS_BUSY)
 2252                 return (EBUSY);
 2253         if (dev->state != DS_ATTACHED)
 2254                 return (0);
 2255 
 2256         if ((error = DEVICE_DETACH(dev)) != 0)
 2257                 return (error);
 2258         devremoved(dev);
 2259         device_printf(dev, "detached\n");
 2260         if (dev->parent)
 2261                 BUS_CHILD_DETACHED(dev->parent, dev);
 2262 
 2263         if (!(dev->flags & DF_FIXEDCLASS))
 2264                 devclass_delete_device(dev->devclass, dev);
 2265 
 2266         dev->state = DS_NOTPRESENT;
 2267         device_set_driver(dev, NULL);
 2268         device_set_desc(dev, NULL);
 2269         device_sysctl_fini(dev);
 2270 
 2271         return (0);
 2272 }
 2273 
 2274 /**
 2275  * @brief Notify a device of system shutdown
 2276  *
 2277  * This function calls the DEVICE_SHUTDOWN() driver method if the
 2278  * device currently has an attached driver.
 2279  *
 2280  * @returns the value returned by DEVICE_SHUTDOWN()
 2281  */
 2282 int
 2283 device_shutdown(device_t dev)
 2284 {
 2285         if (dev->state < DS_ATTACHED)
 2286                 return (0);
 2287         return (DEVICE_SHUTDOWN(dev));
 2288 }
 2289 
 2290 /**
 2291  * @brief Set the unit number of a device
 2292  *
 2293  * This function can be used to override the unit number used for a
 2294  * device (e.g. to wire a device to a pre-configured unit number).
 2295  */
 2296 int
 2297 device_set_unit(device_t dev, int unit)
 2298 {
 2299         devclass_t dc;
 2300         int err;
 2301 
 2302         dc = device_get_devclass(dev);
 2303         if (unit < dc->maxunit && dc->devices[unit])
 2304                 return (EBUSY);
 2305         err = devclass_delete_device(dc, dev);
 2306         if (err)
 2307                 return (err);
 2308         dev->unit = unit;
 2309         err = devclass_add_device(dc, dev);
 2310         if (err)
 2311                 return (err);
 2312 
 2313         bus_data_generation_update();
 2314         return (0);
 2315 }
 2316 
 2317 /*======================================*/
 2318 /*
 2319  * Some useful method implementations to make life easier for bus drivers.
 2320  */
 2321 
 2322 /**
 2323  * @brief Initialise a resource list.
 2324  *
 2325  * @param rl            the resource list to initialise
 2326  */
 2327 void
 2328 resource_list_init(struct resource_list *rl)
 2329 {
 2330         SLIST_INIT(rl);
 2331 }
 2332 
 2333 /**
 2334  * @brief Reclaim memory used by a resource list.
 2335  *
 2336  * This function frees the memory for all resource entries on the list
 2337  * (if any).
 2338  *
 2339  * @param rl            the resource list to free               
 2340  */
 2341 void
 2342 resource_list_free(struct resource_list *rl)
 2343 {
 2344         struct resource_list_entry *rle;
 2345 
 2346         while ((rle = SLIST_FIRST(rl)) != NULL) {
 2347                 if (rle->res)
 2348                         panic("resource_list_free: resource entry is busy");
 2349                 SLIST_REMOVE_HEAD(rl, link);
 2350                 free(rle, M_BUS);
 2351         }
 2352 }
 2353 
 2354 /**
 2355  * @brief Add a resource entry.
 2356  *
 2357  * This function adds a resource entry using the given @p type, @p
 2358  * start, @p end and @p count values. A rid value is chosen by
 2359  * searching sequentially for the first unused rid starting at zero.
 2360  *
 2361  * @param rl            the resource list to edit
 2362  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2363  * @param start         the start address of the resource
 2364  * @param end           the end address of the resource
 2365  * @param count         XXX end-start+1
 2366  */
 2367 int
 2368 resource_list_add_next(struct resource_list *rl, int type, u_long start,
 2369     u_long end, u_long count)
 2370 {
 2371         int rid;
 2372 
 2373         rid = 0;
 2374         while (resource_list_find(rl, type, rid) != NULL)
 2375                 rid++;
 2376         resource_list_add(rl, type, rid, start, end, count);
 2377         return (rid);
 2378 }
 2379 
 2380 /**
 2381  * @brief Add or modify a resource entry.
 2382  *
 2383  * If an existing entry exists with the same type and rid, it will be
 2384  * modified using the given values of @p start, @p end and @p
 2385  * count. If no entry exists, a new one will be created using the
 2386  * given values.
 2387  *
 2388  * @param rl            the resource list to edit
 2389  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2390  * @param rid           the resource identifier
 2391  * @param start         the start address of the resource
 2392  * @param end           the end address of the resource
 2393  * @param count         XXX end-start+1
 2394  */
 2395 void
 2396 resource_list_add(struct resource_list *rl, int type, int rid,
 2397     u_long start, u_long end, u_long count)
 2398 {
 2399         struct resource_list_entry *rle;
 2400 
 2401         rle = resource_list_find(rl, type, rid);
 2402         if (!rle) {
 2403                 rle = malloc(sizeof(struct resource_list_entry), M_BUS,
 2404                     M_NOWAIT);
 2405                 if (!rle)
 2406                         panic("resource_list_add: can't record entry");
 2407                 SLIST_INSERT_HEAD(rl, rle, link);
 2408                 rle->type = type;
 2409                 rle->rid = rid;
 2410                 rle->res = NULL;
 2411         }
 2412 
 2413         if (rle->res)
 2414                 panic("resource_list_add: resource entry is busy");
 2415 
 2416         rle->start = start;
 2417         rle->end = end;
 2418         rle->count = count;
 2419 }
 2420 
 2421 /**
 2422  * @brief Find a resource entry by type and rid.
 2423  *
 2424  * @param rl            the resource list to search
 2425  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2426  * @param rid           the resource identifier
 2427  *
 2428  * @returns the resource entry pointer or NULL if there is no such
 2429  * entry.
 2430  */
 2431 struct resource_list_entry *
 2432 resource_list_find(struct resource_list *rl, int type, int rid)
 2433 {
 2434         struct resource_list_entry *rle;
 2435 
 2436         SLIST_FOREACH(rle, rl, link) {
 2437                 if (rle->type == type && rle->rid == rid)
 2438                         return (rle);
 2439         }
 2440         return (NULL);
 2441 }
 2442 
 2443 /**
 2444  * @brief Delete a resource entry.
 2445  *
 2446  * @param rl            the resource list to edit
 2447  * @param type          the resource entry type (e.g. SYS_RES_MEMORY)
 2448  * @param rid           the resource identifier
 2449  */
 2450 void
 2451 resource_list_delete(struct resource_list *rl, int type, int rid)
 2452 {
 2453         struct resource_list_entry *rle = resource_list_find(rl, type, rid);
 2454 
 2455         if (rle) {
 2456                 if (rle->res != NULL)
 2457                         panic("resource_list_delete: resource has not been released");
 2458                 SLIST_REMOVE(rl, rle, resource_list_entry, link);
 2459                 free(rle, M_BUS);
 2460         }
 2461 }
 2462 
 2463 /**
 2464  * @brief Helper function for implementing BUS_ALLOC_RESOURCE()
 2465  *
 2466  * Implement BUS_ALLOC_RESOURCE() by looking up a resource from the list
 2467  * and passing the allocation up to the parent of @p bus. This assumes
 2468  * that the first entry of @c device_get_ivars(child) is a struct
 2469  * resource_list. This also handles 'passthrough' allocations where a
 2470  * child is a remote descendant of bus by passing the allocation up to
 2471  * the parent of bus.
 2472  *
 2473  * Typically, a bus driver would store a list of child resources
 2474  * somewhere in the child device's ivars (see device_get_ivars()) and
 2475  * its implementation of BUS_ALLOC_RESOURCE() would find that list and
 2476  * then call resource_list_alloc() to perform the allocation.
 2477  *
 2478  * @param rl            the resource list to allocate from
 2479  * @param bus           the parent device of @p child
 2480  * @param child         the device which is requesting an allocation
 2481  * @param type          the type of resource to allocate
 2482  * @param rid           a pointer to the resource identifier
 2483  * @param start         hint at the start of the resource range - pass
 2484  *                      @c 0UL for any start address
 2485  * @param end           hint at the end of the resource range - pass
 2486  *                      @c ~0UL for any end address
 2487  * @param count         hint at the size of range required - pass @c 1
 2488  *                      for any size
 2489  * @param flags         any extra flags to control the resource
 2490  *                      allocation - see @c RF_XXX flags in
 2491  *                      <sys/rman.h> for details
 2492  * 
 2493  * @returns             the resource which was allocated or @c NULL if no
 2494  *                      resource could be allocated
 2495  */
 2496 struct resource *
 2497 resource_list_alloc(struct resource_list *rl, device_t bus, device_t child,
 2498     int type, int *rid, u_long start, u_long end, u_long count, u_int flags)
 2499 {
 2500         struct resource_list_entry *rle = 0;
 2501         int passthrough = (device_get_parent(child) != bus);
 2502         int isdefault = (start == 0UL && end == ~0UL);
 2503 
 2504         if (passthrough) {
 2505                 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
 2506                     type, rid, start, end, count, flags));
 2507         }
 2508 
 2509         rle = resource_list_find(rl, type, *rid);
 2510 
 2511         if (!rle)
 2512                 return (NULL);          /* no resource of that type/rid */
 2513 
 2514         if (rle->res)
 2515                 panic("resource_list_alloc: resource entry is busy");
 2516 
 2517         if (isdefault) {
 2518                 start = rle->start;
 2519                 count = ulmax(count, rle->count);
 2520                 end = ulmax(rle->end, start + count - 1);
 2521         }
 2522 
 2523         rle->res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child,
 2524             type, rid, start, end, count, flags);
 2525 
 2526         /*
 2527          * Record the new range.
 2528          */
 2529         if (rle->res) {
 2530                 rle->start = rman_get_start(rle->res);
 2531                 rle->end = rman_get_end(rle->res);
 2532                 rle->count = count;
 2533         }
 2534 
 2535         return (rle->res);
 2536 }
 2537 
 2538 /**
 2539  * @brief Helper function for implementing BUS_RELEASE_RESOURCE()
 2540  * 
 2541  * Implement BUS_RELEASE_RESOURCE() using a resource list. Normally
 2542  * used with resource_list_alloc().
 2543  * 
 2544  * @param rl            the resource list which was allocated from
 2545  * @param bus           the parent device of @p child
 2546  * @param child         the device which is requesting a release
 2547  * @param type          the type of resource to allocate
 2548  * @param rid           the resource identifier
 2549  * @param res           the resource to release
 2550  * 
 2551  * @retval 0            success
 2552  * @retval non-zero     a standard unix error code indicating what
 2553  *                      error condition prevented the operation
 2554  */
 2555 int
 2556 resource_list_release(struct resource_list *rl, device_t bus, device_t child,
 2557     int type, int rid, struct resource *res)
 2558 {
 2559         struct resource_list_entry *rle = 0;
 2560         int passthrough = (device_get_parent(child) != bus);
 2561         int error;
 2562 
 2563         if (passthrough) {
 2564                 return (BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
 2565                     type, rid, res));
 2566         }
 2567 
 2568         rle = resource_list_find(rl, type, rid);
 2569 
 2570         if (!rle)
 2571                 panic("resource_list_release: can't find resource");
 2572         if (!rle->res)
 2573                 panic("resource_list_release: resource entry is not busy");
 2574 
 2575         error = BUS_RELEASE_RESOURCE(device_get_parent(bus), child,
 2576             type, rid, res);
 2577         if (error)
 2578                 return (error);
 2579 
 2580         rle->res = NULL;
 2581         return (0);
 2582 }
 2583 
 2584 /**
 2585  * @brief Print a description of resources in a resource list
 2586  *
 2587  * Print all resources of a specified type, for use in BUS_PRINT_CHILD().
 2588  * The name is printed if at least one resource of the given type is available.
 2589  * The format is used to print resource start and end.
 2590  *
 2591  * @param rl            the resource list to print
 2592  * @param name          the name of @p type, e.g. @c "memory"
 2593  * @param type          type type of resource entry to print
 2594  * @param format        printf(9) format string to print resource
 2595  *                      start and end values
 2596  * 
 2597  * @returns             the number of characters printed
 2598  */
 2599 int
 2600 resource_list_print_type(struct resource_list *rl, const char *name, int type,
 2601     const char *format)
 2602 {
 2603         struct resource_list_entry *rle;
 2604         int printed, retval;
 2605 
 2606         printed = 0;
 2607         retval = 0;
 2608         /* Yes, this is kinda cheating */
 2609         SLIST_FOREACH(rle, rl, link) {
 2610                 if (rle->type == type) {
 2611                         if (printed == 0)
 2612                                 retval += printf(" %s ", name);
 2613                         else
 2614                                 retval += printf(",");
 2615                         printed++;
 2616                         retval += printf(format, rle->start);
 2617                         if (rle->count > 1) {
 2618                                 retval += printf("-");
 2619                                 retval += printf(format, rle->start +
 2620                                                  rle->count - 1);
 2621                         }
 2622                 }
 2623         }
 2624         return (retval);
 2625 }
 2626 
 2627 /**
 2628  * @brief Helper function for implementing DEVICE_PROBE()
 2629  *
 2630  * This function can be used to help implement the DEVICE_PROBE() for
 2631  * a bus (i.e. a device which has other devices attached to it). It
 2632  * calls the DEVICE_IDENTIFY() method of each driver in the device's
 2633  * devclass.
 2634  */
 2635 int
 2636 bus_generic_probe(device_t dev)
 2637 {
 2638         devclass_t dc = dev->devclass;
 2639         driverlink_t dl;
 2640 
 2641         TAILQ_FOREACH(dl, &dc->drivers, link) {
 2642                 DEVICE_IDENTIFY(dl->driver, dev);
 2643         }
 2644 
 2645         return (0);
 2646 }
 2647 
 2648 /**
 2649  * @brief Helper function for implementing DEVICE_ATTACH()
 2650  *
 2651  * This function can be used to help implement the DEVICE_ATTACH() for
 2652  * a bus. It calls device_probe_and_attach() for each of the device's
 2653  * children.
 2654  */
 2655 int
 2656 bus_generic_attach(device_t dev)
 2657 {
 2658         device_t child;
 2659 
 2660         TAILQ_FOREACH(child, &dev->children, link) {
 2661                 device_probe_and_attach(child);
 2662         }
 2663 
 2664         return (0);
 2665 }
 2666 
 2667 /**
 2668  * @brief Helper function for implementing DEVICE_DETACH()
 2669  *
 2670  * This function can be used to help implement the DEVICE_DETACH() for
 2671  * a bus. It calls device_detach() for each of the device's
 2672  * children.
 2673  */
 2674 int
 2675 bus_generic_detach(device_t dev)
 2676 {
 2677         device_t child;
 2678         int error;
 2679 
 2680         if (dev->state != DS_ATTACHED)
 2681                 return (EBUSY);
 2682 
 2683         TAILQ_FOREACH(child, &dev->children, link) {
 2684                 if ((error = device_detach(child)) != 0)
 2685                         return (error);
 2686         }
 2687 
 2688         return (0);
 2689 }
 2690 
 2691 /**
 2692  * @brief Helper function for implementing DEVICE_SHUTDOWN()
 2693  *
 2694  * This function can be used to help implement the DEVICE_SHUTDOWN()
 2695  * for a bus. It calls device_shutdown() for each of the device's
 2696  * children.
 2697  */
 2698 int
 2699 bus_generic_shutdown(device_t dev)
 2700 {
 2701         device_t child;
 2702 
 2703         TAILQ_FOREACH(child, &dev->children, link) {
 2704                 device_shutdown(child);
 2705         }
 2706 
 2707         return (0);
 2708 }
 2709 
 2710 /**
 2711  * @brief Helper function for implementing DEVICE_SUSPEND()
 2712  *
 2713  * This function can be used to help implement the DEVICE_SUSPEND()
 2714  * for a bus. It calls DEVICE_SUSPEND() for each of the device's
 2715  * children. If any call to DEVICE_SUSPEND() fails, the suspend
 2716  * operation is aborted and any devices which were suspended are
 2717  * resumed immediately by calling their DEVICE_RESUME() methods.
 2718  */
 2719 int
 2720 bus_generic_suspend(device_t dev)
 2721 {
 2722         int             error;
 2723         device_t        child, child2;
 2724 
 2725         TAILQ_FOREACH(child, &dev->children, link) {
 2726                 error = DEVICE_SUSPEND(child);
 2727                 if (error) {
 2728                         for (child2 = TAILQ_FIRST(&dev->children);
 2729                              child2 && child2 != child;
 2730                              child2 = TAILQ_NEXT(child2, link))
 2731                                 DEVICE_RESUME(child2);
 2732                         return (error);
 2733                 }
 2734         }
 2735         return (0);
 2736 }
 2737 
 2738 /**
 2739  * @brief Helper function for implementing DEVICE_RESUME()
 2740  *
 2741  * This function can be used to help implement the DEVICE_RESUME() for
 2742  * a bus. It calls DEVICE_RESUME() on each of the device's children.
 2743  */
 2744 int
 2745 bus_generic_resume(device_t dev)
 2746 {
 2747         device_t        child;
 2748 
 2749         TAILQ_FOREACH(child, &dev->children, link) {
 2750                 DEVICE_RESUME(child);
 2751                 /* if resume fails, there's nothing we can usefully do... */
 2752         }
 2753         return (0);
 2754 }
 2755 
 2756 /**
 2757  * @brief Helper function for implementing BUS_PRINT_CHILD().
 2758  *
 2759  * This function prints the first part of the ascii representation of
 2760  * @p child, including its name, unit and description (if any - see
 2761  * device_set_desc()).
 2762  *
 2763  * @returns the number of characters printed
 2764  */
 2765 int
 2766 bus_print_child_header(device_t dev, device_t child)
 2767 {
 2768         int     retval = 0;
 2769 
 2770         if (device_get_desc(child)) {
 2771                 retval += device_printf(child, "<%s>", device_get_desc(child));
 2772         } else {
 2773                 retval += printf("%s", device_get_nameunit(child));
 2774         }
 2775 
 2776         return (retval);
 2777 }
 2778 
 2779 /**
 2780  * @brief Helper function for implementing BUS_PRINT_CHILD().
 2781  *
 2782  * This function prints the last part of the ascii representation of
 2783  * @p child, which consists of the string @c " on " followed by the
 2784  * name and unit of the @p dev.
 2785  *
 2786  * @returns the number of characters printed
 2787  */
 2788 int
 2789 bus_print_child_footer(device_t dev, device_t child)
 2790 {
 2791         return (printf(" on %s\n", device_get_nameunit(dev)));
 2792 }
 2793 
 2794 /**
 2795  * @brief Helper function for implementing BUS_PRINT_CHILD().
 2796  *
 2797  * This function simply calls bus_print_child_header() followed by
 2798  * bus_print_child_footer().
 2799  *
 2800  * @returns the number of characters printed
 2801  */
 2802 int
 2803 bus_generic_print_child(device_t dev, device_t child)
 2804 {
 2805         int     retval = 0;
 2806 
 2807         retval += bus_print_child_header(dev, child);
 2808         retval += bus_print_child_footer(dev, child);
 2809 
 2810         return (retval);
 2811 }
 2812 
 2813 /**
 2814  * @brief Stub function for implementing BUS_READ_IVAR().
 2815  * 
 2816  * @returns ENOENT
 2817  */
 2818 int
 2819 bus_generic_read_ivar(device_t dev, device_t child, int index,
 2820     uintptr_t * result)
 2821 {
 2822         return (ENOENT);
 2823 }
 2824 
 2825 /**
 2826  * @brief Stub function for implementing BUS_WRITE_IVAR().
 2827  * 
 2828  * @returns ENOENT
 2829  */
 2830 int
 2831 bus_generic_write_ivar(device_t dev, device_t child, int index,
 2832     uintptr_t value)
 2833 {
 2834         return (ENOENT);
 2835 }
 2836 
 2837 /**
 2838  * @brief Stub function for implementing BUS_GET_RESOURCE_LIST().
 2839  * 
 2840  * @returns NULL
 2841  */
 2842 struct resource_list *
 2843 bus_generic_get_resource_list(device_t dev, device_t child)
 2844 {
 2845         return (NULL);
 2846 }
 2847 
 2848 /**
 2849  * @brief Helper function for implementing BUS_DRIVER_ADDED().
 2850  *
 2851  * This implementation of BUS_DRIVER_ADDED() simply calls the driver's
 2852  * DEVICE_IDENTIFY() method to allow it to add new children to the bus
 2853  * and then calls device_probe_and_attach() for each unattached child.
 2854  */
 2855 void
 2856 bus_generic_driver_added(device_t dev, driver_t *driver)
 2857 {
 2858         device_t child;
 2859 
 2860         DEVICE_IDENTIFY(driver, dev);
 2861         TAILQ_FOREACH(child, &dev->children, link) {
 2862                 if (child->state == DS_NOTPRESENT)
 2863                         device_probe_and_attach(child);
 2864         }
 2865 }
 2866 
 2867 /**
 2868  * @brief Helper function for implementing BUS_SETUP_INTR().
 2869  *
 2870  * This simple implementation of BUS_SETUP_INTR() simply calls the
 2871  * BUS_SETUP_INTR() method of the parent of @p dev.
 2872  */
 2873 int
 2874 bus_generic_setup_intr(device_t dev, device_t child, struct resource *irq,
 2875     int flags, driver_intr_t *intr, void *arg, void **cookiep)
 2876 {
 2877         /* Propagate up the bus hierarchy until someone handles it. */
 2878         if (dev->parent)
 2879                 return (BUS_SETUP_INTR(dev->parent, child, irq, flags,
 2880                     intr, arg, cookiep));
 2881         return (EINVAL);
 2882 }
 2883 
 2884 /**
 2885  * @brief Helper function for implementing BUS_TEARDOWN_INTR().
 2886  *
 2887  * This simple implementation of BUS_TEARDOWN_INTR() simply calls the
 2888  * BUS_TEARDOWN_INTR() method of the parent of @p dev.
 2889  */
 2890 int
 2891 bus_generic_teardown_intr(device_t dev, device_t child, struct resource *irq,
 2892     void *cookie)
 2893 {
 2894         /* Propagate up the bus hierarchy until someone handles it. */
 2895         if (dev->parent)
 2896                 return (BUS_TEARDOWN_INTR(dev->parent, child, irq, cookie));
 2897         return (EINVAL);
 2898 }
 2899 
 2900 /**
 2901  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
 2902  *
 2903  * This simple implementation of BUS_ALLOC_RESOURCE() simply calls the
 2904  * BUS_ALLOC_RESOURCE() method of the parent of @p dev.
 2905  */
 2906 struct resource *
 2907 bus_generic_alloc_resource(device_t dev, device_t child, int type, int *rid,
 2908     u_long start, u_long end, u_long count, u_int flags)
 2909 {
 2910         /* Propagate up the bus hierarchy until someone handles it. */
 2911         if (dev->parent)
 2912                 return (BUS_ALLOC_RESOURCE(dev->parent, child, type, rid,
 2913                     start, end, count, flags));
 2914         return (NULL);
 2915 }
 2916 
 2917 /**
 2918  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
 2919  *
 2920  * This simple implementation of BUS_RELEASE_RESOURCE() simply calls the
 2921  * BUS_RELEASE_RESOURCE() method of the parent of @p dev.
 2922  */
 2923 int
 2924 bus_generic_release_resource(device_t dev, device_t child, int type, int rid,
 2925     struct resource *r)
 2926 {
 2927         /* Propagate up the bus hierarchy until someone handles it. */
 2928         if (dev->parent)
 2929                 return (BUS_RELEASE_RESOURCE(dev->parent, child, type, rid,
 2930                     r));
 2931         return (EINVAL);
 2932 }
 2933 
 2934 /**
 2935  * @brief Helper function for implementing BUS_ACTIVATE_RESOURCE().
 2936  *
 2937  * This simple implementation of BUS_ACTIVATE_RESOURCE() simply calls the
 2938  * BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
 2939  */
 2940 int
 2941 bus_generic_activate_resource(device_t dev, device_t child, int type, int rid,
 2942     struct resource *r)
 2943 {
 2944         /* Propagate up the bus hierarchy until someone handles it. */
 2945         if (dev->parent)
 2946                 return (BUS_ACTIVATE_RESOURCE(dev->parent, child, type, rid,
 2947                     r));
 2948         return (EINVAL);
 2949 }
 2950 
 2951 /**
 2952  * @brief Helper function for implementing BUS_DEACTIVATE_RESOURCE().
 2953  *
 2954  * This simple implementation of BUS_DEACTIVATE_RESOURCE() simply calls the
 2955  * BUS_DEACTIVATE_RESOURCE() method of the parent of @p dev.
 2956  */
 2957 int
 2958 bus_generic_deactivate_resource(device_t dev, device_t child, int type,
 2959     int rid, struct resource *r)
 2960 {
 2961         /* Propagate up the bus hierarchy until someone handles it. */
 2962         if (dev->parent)
 2963                 return (BUS_DEACTIVATE_RESOURCE(dev->parent, child, type, rid,
 2964                     r));
 2965         return (EINVAL);
 2966 }
 2967 
 2968 /**
 2969  * @brief Helper function for implementing BUS_CONFIG_INTR().
 2970  *
 2971  * This simple implementation of BUS_CONFIG_INTR() simply calls the
 2972  * BUS_CONFIG_INTR() method of the parent of @p dev.
 2973  */
 2974 int
 2975 bus_generic_config_intr(device_t dev, int irq, enum intr_trigger trig,
 2976     enum intr_polarity pol)
 2977 {
 2978 
 2979         /* Propagate up the bus hierarchy until someone handles it. */
 2980         if (dev->parent)
 2981                 return (BUS_CONFIG_INTR(dev->parent, irq, trig, pol));
 2982         return (EINVAL);
 2983 }
 2984 
 2985 /**
 2986  * @brief Helper function for implementing BUS_GET_RESOURCE().
 2987  *
 2988  * This implementation of BUS_GET_RESOURCE() uses the
 2989  * resource_list_find() function to do most of the work. It calls
 2990  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
 2991  * search.
 2992  */
 2993 int
 2994 bus_generic_rl_get_resource(device_t dev, device_t child, int type, int rid,
 2995     u_long *startp, u_long *countp)
 2996 {
 2997         struct resource_list *          rl = NULL;
 2998         struct resource_list_entry *    rle = NULL;
 2999 
 3000         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3001         if (!rl)
 3002                 return (EINVAL);
 3003 
 3004         rle = resource_list_find(rl, type, rid);
 3005         if (!rle)
 3006                 return (ENOENT);
 3007 
 3008         if (startp)
 3009                 *startp = rle->start;
 3010         if (countp)
 3011                 *countp = rle->count;
 3012 
 3013         return (0);
 3014 }
 3015 
 3016 /**
 3017  * @brief Helper function for implementing BUS_SET_RESOURCE().
 3018  *
 3019  * This implementation of BUS_SET_RESOURCE() uses the
 3020  * resource_list_add() function to do most of the work. It calls
 3021  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
 3022  * edit.
 3023  */
 3024 int
 3025 bus_generic_rl_set_resource(device_t dev, device_t child, int type, int rid,
 3026     u_long start, u_long count)
 3027 {
 3028         struct resource_list *          rl = NULL;
 3029 
 3030         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3031         if (!rl)
 3032                 return (EINVAL);
 3033 
 3034         resource_list_add(rl, type, rid, start, (start + count - 1), count);
 3035 
 3036         return (0);
 3037 }
 3038 
 3039 /**
 3040  * @brief Helper function for implementing BUS_DELETE_RESOURCE().
 3041  *
 3042  * This implementation of BUS_DELETE_RESOURCE() uses the
 3043  * resource_list_delete() function to do most of the work. It calls
 3044  * BUS_GET_RESOURCE_LIST() to find a suitable resource list to
 3045  * edit.
 3046  */
 3047 void
 3048 bus_generic_rl_delete_resource(device_t dev, device_t child, int type, int rid)
 3049 {
 3050         struct resource_list *          rl = NULL;
 3051 
 3052         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3053         if (!rl)
 3054                 return;
 3055 
 3056         resource_list_delete(rl, type, rid);
 3057 
 3058         return;
 3059 }
 3060 
 3061 /**
 3062  * @brief Helper function for implementing BUS_RELEASE_RESOURCE().
 3063  *
 3064  * This implementation of BUS_RELEASE_RESOURCE() uses the
 3065  * resource_list_release() function to do most of the work. It calls
 3066  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
 3067  */
 3068 int
 3069 bus_generic_rl_release_resource(device_t dev, device_t child, int type,
 3070     int rid, struct resource *r)
 3071 {
 3072         struct resource_list *          rl = NULL;
 3073 
 3074         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3075         if (!rl)
 3076                 return (EINVAL);
 3077 
 3078         return (resource_list_release(rl, dev, child, type, rid, r));
 3079 }
 3080 
 3081 /**
 3082  * @brief Helper function for implementing BUS_ALLOC_RESOURCE().
 3083  *
 3084  * This implementation of BUS_ALLOC_RESOURCE() uses the
 3085  * resource_list_alloc() function to do most of the work. It calls
 3086  * BUS_GET_RESOURCE_LIST() to find a suitable resource list.
 3087  */
 3088 struct resource *
 3089 bus_generic_rl_alloc_resource(device_t dev, device_t child, int type,
 3090     int *rid, u_long start, u_long end, u_long count, u_int flags)
 3091 {
 3092         struct resource_list *          rl = NULL;
 3093 
 3094         rl = BUS_GET_RESOURCE_LIST(dev, child);
 3095         if (!rl)
 3096                 return (NULL);
 3097 
 3098         return (resource_list_alloc(rl, dev, child, type, rid,
 3099             start, end, count, flags));
 3100 }
 3101 
 3102 /**
 3103  * @brief Helper function for implementing BUS_CHILD_PRESENT().
 3104  *
 3105  * This simple implementation of BUS_CHILD_PRESENT() simply calls the
 3106  * BUS_CHILD_PRESENT() method of the parent of @p dev.
 3107  */
 3108 int
 3109 bus_generic_child_present(device_t dev, device_t child)
 3110 {
 3111         return (BUS_CHILD_PRESENT(device_get_parent(dev), dev));
 3112 }
 3113 
 3114 /*
 3115  * Some convenience functions to make it easier for drivers to use the
 3116  * resource-management functions.  All these really do is hide the
 3117  * indirection through the parent's method table, making for slightly
 3118  * less-wordy code.  In the future, it might make sense for this code
 3119  * to maintain some sort of a list of resources allocated by each device.
 3120  */
 3121 
 3122 /**
 3123  * @brief Wrapper function for BUS_ALLOC_RESOURCE().
 3124  *
 3125  * This function simply calls the BUS_ALLOC_RESOURCE() method of the
 3126  * parent of @p dev.
 3127  */
 3128 struct resource *
 3129 bus_alloc_resource(device_t dev, int type, int *rid, u_long start, u_long end,
 3130     u_long count, u_int flags)
 3131 {
 3132         if (dev->parent == 0)
 3133                 return (0);
 3134         return (BUS_ALLOC_RESOURCE(dev->parent, dev, type, rid, start, end,
 3135             count, flags));
 3136 }
 3137 
 3138 /**
 3139  * @brief Wrapper function for BUS_ACTIVATE_RESOURCE().
 3140  *
 3141  * This function simply calls the BUS_ACTIVATE_RESOURCE() method of the
 3142  * parent of @p dev.
 3143  */
 3144 int
 3145 bus_activate_resource(device_t dev, int type, int rid, struct resource *r)
 3146 {
 3147         if (dev->parent == 0)
 3148                 return (EINVAL);
 3149         return (BUS_ACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
 3150 }
 3151 
 3152 /**
 3153  * @brief Wrapper function for BUS_DEACTIVATE_RESOURCE().
 3154  *
 3155  * This function simply calls the BUS_DEACTIVATE_RESOURCE() method of the
 3156  * parent of @p dev.
 3157  */
 3158 int
 3159 bus_deactivate_resource(device_t dev, int type, int rid, struct resource *r)
 3160 {
 3161         if (dev->parent == 0)
 3162                 return (EINVAL);
 3163         return (BUS_DEACTIVATE_RESOURCE(dev->parent, dev, type, rid, r));
 3164 }
 3165 
 3166 /**
 3167  * @brief Wrapper function for BUS_RELEASE_RESOURCE().
 3168  *
 3169  * This function simply calls the BUS_RELEASE_RESOURCE() method of the
 3170  * parent of @p dev.
 3171  */
 3172 int
 3173 bus_release_resource(device_t dev, int type, int rid, struct resource *r)
 3174 {
 3175         if (dev->parent == 0)
 3176                 return (EINVAL);
 3177         return (BUS_RELEASE_RESOURCE(dev->parent, dev, type, rid, r));
 3178 }
 3179 
 3180 /**
 3181  * @brief Wrapper function for BUS_SETUP_INTR().
 3182  *
 3183  * This function simply calls the BUS_SETUP_INTR() method of the
 3184  * parent of @p dev.
 3185  */
 3186 int
 3187 bus_setup_intr(device_t dev, struct resource *r, int flags,
 3188     driver_intr_t handler, void *arg, void **cookiep)
 3189 {
 3190         int error;
 3191 
 3192         if (dev->parent != 0) {
 3193                 if ((flags &~ INTR_ENTROPY) == (INTR_TYPE_NET | INTR_MPSAFE) &&
 3194                     !debug_mpsafenet)
 3195                         flags &= ~INTR_MPSAFE;
 3196                 error = BUS_SETUP_INTR(dev->parent, dev, r, flags,
 3197                     handler, arg, cookiep);
 3198                 if (error == 0) {
 3199                         if (bootverbose && !(flags & (INTR_MPSAFE | INTR_FAST)))
 3200                                 device_printf(dev, "[GIANT-LOCKED]\n");
 3201                         if (bootverbose && (flags & INTR_MPSAFE))
 3202                                 device_printf(dev, "[MPSAFE]\n");
 3203                         if (bootverbose && (flags & INTR_FAST))
 3204                                 device_printf(dev, "[FAST]\n");
 3205                 }
 3206         } else
 3207                 error = EINVAL;
 3208         return (error);
 3209 }
 3210 
 3211 /**
 3212  * @brief Wrapper function for BUS_TEARDOWN_INTR().
 3213  *
 3214  * This function simply calls the BUS_TEARDOWN_INTR() method of the
 3215  * parent of @p dev.
 3216  */
 3217 int
 3218 bus_teardown_intr(device_t dev, struct resource *r, void *cookie)
 3219 {
 3220         if (dev->parent == 0)
 3221                 return (EINVAL);
 3222         return (BUS_TEARDOWN_INTR(dev->parent, dev, r, cookie));
 3223 }
 3224 
 3225 /**
 3226  * @brief Wrapper function for BUS_SET_RESOURCE().
 3227  *
 3228  * This function simply calls the BUS_SET_RESOURCE() method of the
 3229  * parent of @p dev.
 3230  */
 3231 int
 3232 bus_set_resource(device_t dev, int type, int rid,
 3233     u_long start, u_long count)
 3234 {
 3235         return (BUS_SET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3236             start, count));
 3237 }
 3238 
 3239 /**
 3240  * @brief Wrapper function for BUS_GET_RESOURCE().
 3241  *
 3242  * This function simply calls the BUS_GET_RESOURCE() method of the
 3243  * parent of @p dev.
 3244  */
 3245 int
 3246 bus_get_resource(device_t dev, int type, int rid,
 3247     u_long *startp, u_long *countp)
 3248 {
 3249         return (BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3250             startp, countp));
 3251 }
 3252 
 3253 /**
 3254  * @brief Wrapper function for BUS_GET_RESOURCE().
 3255  *
 3256  * This function simply calls the BUS_GET_RESOURCE() method of the
 3257  * parent of @p dev and returns the start value.
 3258  */
 3259 u_long
 3260 bus_get_resource_start(device_t dev, int type, int rid)
 3261 {
 3262         u_long start, count;
 3263         int error;
 3264 
 3265         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3266             &start, &count);
 3267         if (error)
 3268                 return (0);
 3269         return (start);
 3270 }
 3271 
 3272 /**
 3273  * @brief Wrapper function for BUS_GET_RESOURCE().
 3274  *
 3275  * This function simply calls the BUS_GET_RESOURCE() method of the
 3276  * parent of @p dev and returns the count value.
 3277  */
 3278 u_long
 3279 bus_get_resource_count(device_t dev, int type, int rid)
 3280 {
 3281         u_long start, count;
 3282         int error;
 3283 
 3284         error = BUS_GET_RESOURCE(device_get_parent(dev), dev, type, rid,
 3285             &start, &count);
 3286         if (error)
 3287                 return (0);
 3288         return (count);
 3289 }
 3290 
 3291 /**
 3292  * @brief Wrapper function for BUS_DELETE_RESOURCE().
 3293  *
 3294  * This function simply calls the BUS_DELETE_RESOURCE() method of the
 3295  * parent of @p dev.
 3296  */
 3297 void
 3298 bus_delete_resource(device_t dev, int type, int rid)
 3299 {
 3300         BUS_DELETE_RESOURCE(device_get_parent(dev), dev, type, rid);
 3301 }
 3302 
 3303 /**
 3304  * @brief Wrapper function for BUS_CHILD_PRESENT().
 3305  *
 3306  * This function simply calls the BUS_CHILD_PRESENT() method of the
 3307  * parent of @p dev.
 3308  */
 3309 int
 3310 bus_child_present(device_t child)
 3311 {
 3312         return (BUS_CHILD_PRESENT(device_get_parent(child), child));
 3313 }
 3314 
 3315 /**
 3316  * @brief Wrapper function for BUS_CHILD_PNPINFO_STR().
 3317  *
 3318  * This function simply calls the BUS_CHILD_PNPINFO_STR() method of the
 3319  * parent of @p dev.
 3320  */
 3321 int
 3322 bus_child_pnpinfo_str(device_t child, char *buf, size_t buflen)
 3323 {
 3324         device_t parent;
 3325 
 3326         parent = device_get_parent(child);
 3327         if (parent == NULL) {
 3328                 *buf = '\0';
 3329                 return (0);
 3330         }
 3331         return (BUS_CHILD_PNPINFO_STR(parent, child, buf, buflen));
 3332 }
 3333 
 3334 /**
 3335  * @brief Wrapper function for BUS_CHILD_LOCATION_STR().
 3336  *
 3337  * This function simply calls the BUS_CHILD_LOCATION_STR() method of the
 3338  * parent of @p dev.
 3339  */
 3340 int
 3341 bus_child_location_str(device_t child, char *buf, size_t buflen)
 3342 {
 3343         device_t parent;
 3344 
 3345         parent = device_get_parent(child);
 3346         if (parent == NULL) {
 3347                 *buf = '\0';
 3348                 return (0);
 3349         }
 3350         return (BUS_CHILD_LOCATION_STR(parent, child, buf, buflen));
 3351 }
 3352 
 3353 static int
 3354 root_print_child(device_t dev, device_t child)
 3355 {
 3356         int     retval = 0;
 3357 
 3358         retval += bus_print_child_header(dev, child);
 3359         retval += printf("\n");
 3360 
 3361         return (retval);
 3362 }
 3363 
 3364 static int
 3365 root_setup_intr(device_t dev, device_t child, driver_intr_t *intr, void *arg,
 3366     void **cookiep)
 3367 {
 3368         /*
 3369          * If an interrupt mapping gets to here something bad has happened.
 3370          */
 3371         panic("root_setup_intr");
 3372 }
 3373 
 3374 /*
 3375  * If we get here, assume that the device is permanant and really is
 3376  * present in the system.  Removable bus drivers are expected to intercept
 3377  * this call long before it gets here.  We return -1 so that drivers that
 3378  * really care can check vs -1 or some ERRNO returned higher in the food
 3379  * chain.
 3380  */
 3381 static int
 3382 root_child_present(device_t dev, device_t child)
 3383 {
 3384         return (-1);
 3385 }
 3386 
 3387 static kobj_method_t root_methods[] = {
 3388         /* Device interface */
 3389         KOBJMETHOD(device_shutdown,     bus_generic_shutdown),
 3390         KOBJMETHOD(device_suspend,      bus_generic_suspend),
 3391         KOBJMETHOD(device_resume,       bus_generic_resume),
 3392 
 3393         /* Bus interface */
 3394         KOBJMETHOD(bus_print_child,     root_print_child),
 3395         KOBJMETHOD(bus_read_ivar,       bus_generic_read_ivar),
 3396         KOBJMETHOD(bus_write_ivar,      bus_generic_write_ivar),
 3397         KOBJMETHOD(bus_setup_intr,      root_setup_intr),
 3398         KOBJMETHOD(bus_child_present,   root_child_present),
 3399 
 3400         { 0, 0 }
 3401 };
 3402 
 3403 static driver_t root_driver = {
 3404         "root",
 3405         root_methods,
 3406         1,                      /* no softc */
 3407 };
 3408 
 3409 device_t        root_bus;
 3410 devclass_t      root_devclass;
 3411 
 3412 static int
 3413 root_bus_module_handler(module_t mod, int what, void* arg)
 3414 {
 3415         switch (what) {
 3416         case MOD_LOAD:
 3417                 TAILQ_INIT(&bus_data_devices);
 3418                 kobj_class_compile((kobj_class_t) &root_driver);
 3419                 root_bus = make_device(NULL, "root", 0);
 3420                 root_bus->desc = "System root bus";
 3421                 kobj_init((kobj_t) root_bus, (kobj_class_t) &root_driver);
 3422                 root_bus->driver = &root_driver;
 3423                 root_bus->state = DS_ATTACHED;
 3424                 root_devclass = devclass_find_internal("root", 0, FALSE);
 3425                 devinit();
 3426                 return (0);
 3427 
 3428         case MOD_SHUTDOWN:
 3429                 device_shutdown(root_bus);
 3430                 return (0);
 3431         default:
 3432                 return (EOPNOTSUPP);
 3433         }
 3434 
 3435         return (0);
 3436 }
 3437 
 3438 static moduledata_t root_bus_mod = {
 3439         "rootbus",
 3440         root_bus_module_handler,
 3441         0
 3442 };
 3443 DECLARE_MODULE(rootbus, root_bus_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
 3444 
 3445 /**
 3446  * @brief Automatically configure devices
 3447  *
 3448  * This function begins the autoconfiguration process by calling
 3449  * device_probe_and_attach() for each child of the @c root0 device.
 3450  */ 
 3451 void
 3452 root_bus_configure(void)
 3453 {
 3454         device_t dev;
 3455 
 3456         PDEBUG(("."));
 3457 
 3458         TAILQ_FOREACH(dev, &root_bus->children, link) {
 3459                 device_probe_and_attach(dev);
 3460         }
 3461 }
 3462 
 3463 /**
 3464  * @brief Module handler for registering device drivers
 3465  *
 3466  * This module handler is used to automatically register device
 3467  * drivers when modules are loaded. If @p what is MOD_LOAD, it calls
 3468  * devclass_add_driver() for the driver described by the
 3469  * driver_module_data structure pointed to by @p arg
 3470  */
 3471 int
 3472 driver_module_handler(module_t mod, int what, void *arg)
 3473 {
 3474         int error;
 3475         struct driver_module_data *dmd;
 3476         devclass_t bus_devclass;
 3477         kobj_class_t driver;
 3478 
 3479         dmd = (struct driver_module_data *)arg;
 3480         bus_devclass = devclass_find_internal(dmd->dmd_busname, 0, TRUE);
 3481         error = 0;
 3482 
 3483         switch (what) {
 3484         case MOD_LOAD:
 3485                 if (dmd->dmd_chainevh)
 3486                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
 3487 
 3488                 driver = dmd->dmd_driver;
 3489                 PDEBUG(("Loading module: driver %s on bus %s",
 3490                     DRIVERNAME(driver), dmd->dmd_busname));
 3491                 error = devclass_add_driver(bus_devclass, driver);
 3492                 if (error)
 3493                         break;
 3494 
 3495                 /*
 3496                  * If the driver has any base classes, make the
 3497                  * devclass inherit from the devclass of the driver's
 3498                  * first base class. This will allow the system to
 3499                  * search for drivers in both devclasses for children
 3500                  * of a device using this driver.
 3501                  */
 3502                 if (driver->baseclasses) {
 3503                         const char *parentname;
 3504                         parentname = driver->baseclasses[0]->name;
 3505                         *dmd->dmd_devclass =
 3506                                 devclass_find_internal(driver->name,
 3507                                     parentname, TRUE);
 3508                 } else {
 3509                         *dmd->dmd_devclass =
 3510                                 devclass_find_internal(driver->name, 0, TRUE);
 3511                 }
 3512                 break;
 3513 
 3514         case MOD_UNLOAD:
 3515                 PDEBUG(("Unloading module: driver %s from bus %s",
 3516                     DRIVERNAME(dmd->dmd_driver),
 3517                     dmd->dmd_busname));
 3518                 error = devclass_delete_driver(bus_devclass,
 3519                     dmd->dmd_driver);
 3520 
 3521                 if (!error && dmd->dmd_chainevh)
 3522                         error = dmd->dmd_chainevh(mod,what,dmd->dmd_chainarg);
 3523                 break;
 3524         default:
 3525                 error = EOPNOTSUPP;
 3526                 break;
 3527         }
 3528 
 3529         return (error);
 3530 }
 3531 
 3532 #ifdef BUS_DEBUG
 3533 
 3534 /* the _short versions avoid iteration by not calling anything that prints
 3535  * more than oneliners. I love oneliners.
 3536  */
 3537 
 3538 static void
 3539 print_device_short(device_t dev, int indent)
 3540 {
 3541         if (!dev)
 3542                 return;
 3543 
 3544         indentprintf(("device %d: <%s> %sparent,%schildren,%s%s%s%s,%sivars,%ssoftc,busy=%d\n",
 3545             dev->unit, dev->desc,
 3546             (dev->parent? "":"no "),
 3547             (TAILQ_EMPTY(&dev->children)? "no ":""),
 3548             (dev->flags&DF_ENABLED? "enabled,":"disabled,"),
 3549             (dev->flags&DF_FIXEDCLASS? "fixed,":""),
 3550             (dev->flags&DF_WILDCARD? "wildcard,":""),
 3551             (dev->flags&DF_DESCMALLOCED? "descmalloced,":""),
 3552             (dev->ivars? "":"no "),
 3553             (dev->softc? "":"no "),
 3554             dev->busy));
 3555 }
 3556 
 3557 static void
 3558 print_device(device_t dev, int indent)
 3559 {
 3560         if (!dev)
 3561                 return;
 3562 
 3563         print_device_short(dev, indent);
 3564 
 3565         indentprintf(("Parent:\n"));
 3566         print_device_short(dev->parent, indent+1);
 3567         indentprintf(("Driver:\n"));
 3568         print_driver_short(dev->driver, indent+1);
 3569         indentprintf(("Devclass:\n"));
 3570         print_devclass_short(dev->devclass, indent+1);
 3571 }
 3572 
 3573 void
 3574 print_device_tree_short(device_t dev, int indent)
 3575 /* print the device and all its children (indented) */
 3576 {
 3577         device_t child;
 3578 
 3579         if (!dev)
 3580                 return;
 3581 
 3582         print_device_short(dev, indent);
 3583 
 3584         TAILQ_FOREACH(child, &dev->children, link) {
 3585                 print_device_tree_short(child, indent+1);
 3586         }
 3587 }
 3588 
 3589 void
 3590 print_device_tree(device_t dev, int indent)
 3591 /* print the device and all its children (indented) */
 3592 {
 3593         device_t child;
 3594 
 3595         if (!dev)
 3596                 return;
 3597 
 3598         print_device(dev, indent);
 3599 
 3600         TAILQ_FOREACH(child, &dev->children, link) {
 3601                 print_device_tree(child, indent+1);
 3602         }
 3603 }
 3604 
 3605 static void
 3606 print_driver_short(driver_t *driver, int indent)
 3607 {
 3608         if (!driver)
 3609                 return;
 3610 
 3611         indentprintf(("driver %s: softc size = %zd\n",
 3612             driver->name, driver->size));
 3613 }
 3614 
 3615 static void
 3616 print_driver(driver_t *driver, int indent)
 3617 {
 3618         if (!driver)
 3619                 return;
 3620 
 3621         print_driver_short(driver, indent);
 3622 }
 3623 
 3624 
 3625 static void
 3626 print_driver_list(driver_list_t drivers, int indent)
 3627 {
 3628         driverlink_t driver;
 3629 
 3630         TAILQ_FOREACH(driver, &drivers, link) {
 3631                 print_driver(driver->driver, indent);
 3632         }
 3633 }
 3634 
 3635 static void
 3636 print_devclass_short(devclass_t dc, int indent)
 3637 {
 3638         if ( !dc )
 3639                 return;
 3640 
 3641         indentprintf(("devclass %s: max units = %d\n", dc->name, dc->maxunit));
 3642 }
 3643 
 3644 static void
 3645 print_devclass(devclass_t dc, int indent)
 3646 {
 3647         int i;
 3648 
 3649         if ( !dc )
 3650                 return;
 3651 
 3652         print_devclass_short(dc, indent);
 3653         indentprintf(("Drivers:\n"));
 3654         print_driver_list(dc->drivers, indent+1);
 3655 
 3656         indentprintf(("Devices:\n"));
 3657         for (i = 0; i < dc->maxunit; i++)
 3658                 if (dc->devices[i])
 3659                         print_device(dc->devices[i], indent+1);
 3660 }
 3661 
 3662 void
 3663 print_devclass_list_short(void)
 3664 {
 3665         devclass_t dc;
 3666 
 3667         printf("Short listing of devclasses, drivers & devices:\n");
 3668         TAILQ_FOREACH(dc, &devclasses, link) {
 3669                 print_devclass_short(dc, 0);
 3670         }
 3671 }
 3672 
 3673 void
 3674 print_devclass_list(void)
 3675 {
 3676         devclass_t dc;
 3677 
 3678         printf("Full listing of devclasses, drivers & devices:\n");
 3679         TAILQ_FOREACH(dc, &devclasses, link) {
 3680                 print_devclass(dc, 0);
 3681         }
 3682 }
 3683 
 3684 #endif
 3685 
 3686 /*
 3687  * User-space access to the device tree.
 3688  *
 3689  * We implement a small set of nodes:
 3690  *
 3691  * hw.bus                       Single integer read method to obtain the
 3692  *                              current generation count.
 3693  * hw.bus.devices               Reads the entire device tree in flat space.
 3694  * hw.bus.rman                  Resource manager interface
 3695  *
 3696  * We might like to add the ability to scan devclasses and/or drivers to
 3697  * determine what else is currently loaded/available.
 3698  */
 3699 
 3700 static int
 3701 sysctl_bus(SYSCTL_HANDLER_ARGS)
 3702 {
 3703         struct u_businfo        ubus;
 3704 
 3705         ubus.ub_version = BUS_USER_VERSION;
 3706         ubus.ub_generation = bus_data_generation;
 3707 
 3708         return (SYSCTL_OUT(req, &ubus, sizeof(ubus)));
 3709 }
 3710 SYSCTL_NODE(_hw_bus, OID_AUTO, info, CTLFLAG_RW, sysctl_bus,
 3711     "bus-related data");
 3712 
 3713 static int
 3714 sysctl_devices(SYSCTL_HANDLER_ARGS)
 3715 {
 3716         int                     *name = (int *)arg1;
 3717         u_int                   namelen = arg2;
 3718         int                     index;
 3719         struct device           *dev;
 3720         struct u_device         udev;   /* XXX this is a bit big */
 3721         int                     error;
 3722 
 3723         if (namelen != 2)
 3724                 return (EINVAL);
 3725 
 3726         if (bus_data_generation_check(name[0]))
 3727                 return (EINVAL);
 3728 
 3729         index = name[1];
 3730 
 3731         /*
 3732          * Scan the list of devices, looking for the requested index.
 3733          */
 3734         TAILQ_FOREACH(dev, &bus_data_devices, devlink) {
 3735                 if (index-- == 0)
 3736                         break;
 3737         }
 3738         if (dev == NULL)
 3739                 return (ENOENT);
 3740 
 3741         /*
 3742          * Populate the return array.
 3743          */
 3744         bzero(&udev, sizeof(udev));
 3745         udev.dv_handle = (uintptr_t)dev;
 3746         udev.dv_parent = (uintptr_t)dev->parent;
 3747         if (dev->nameunit == NULL)
 3748                 udev.dv_name[0] = '\0';
 3749         else
 3750                 strlcpy(udev.dv_name, dev->nameunit, sizeof(udev.dv_name));
 3751 
 3752         if (dev->desc == NULL)
 3753                 udev.dv_desc[0] = '\0';
 3754         else
 3755                 strlcpy(udev.dv_desc, dev->desc, sizeof(udev.dv_desc));
 3756         if (dev->driver == NULL || dev->driver->name == NULL)
 3757                 udev.dv_drivername[0] = '\0';
 3758         else
 3759                 strlcpy(udev.dv_drivername, dev->driver->name,
 3760                     sizeof(udev.dv_drivername));
 3761         udev.dv_pnpinfo[0] = '\0';
 3762         udev.dv_location[0] = '\0';
 3763         bus_child_pnpinfo_str(dev, udev.dv_pnpinfo, sizeof(udev.dv_pnpinfo));
 3764         bus_child_location_str(dev, udev.dv_location, sizeof(udev.dv_location));
 3765         udev.dv_devflags = dev->devflags;
 3766         udev.dv_flags = dev->flags;
 3767         udev.dv_state = dev->state;
 3768         error = SYSCTL_OUT(req, &udev, sizeof(udev));
 3769         return (error);
 3770 }
 3771 
 3772 SYSCTL_NODE(_hw_bus, OID_AUTO, devices, CTLFLAG_RD, sysctl_devices,
 3773     "system device tree");
 3774 
 3775 /*
 3776  * Sysctl interface for scanning the resource lists.
 3777  *
 3778  * We take two input parameters; the index into the list of resource
 3779  * managers, and the resource offset into the list.
 3780  */
 3781 static int
 3782 sysctl_rman(SYSCTL_HANDLER_ARGS)
 3783 {
 3784         int                     *name = (int *)arg1;
 3785         u_int                   namelen = arg2;
 3786         int                     rman_idx, res_idx;
 3787         struct rman             *rm;
 3788         struct resource         *res;
 3789         struct u_rman           urm;
 3790         struct u_resource       ures;
 3791         int                     error;
 3792 
 3793         if (namelen != 3)
 3794                 return (EINVAL);
 3795 
 3796         if (bus_data_generation_check(name[0]))
 3797                 return (EINVAL);
 3798         rman_idx = name[1];
 3799         res_idx = name[2];
 3800 
 3801         /*
 3802          * Find the indexed resource manager
 3803          */
 3804         TAILQ_FOREACH(rm, &rman_head, rm_link) {
 3805                 if (rman_idx-- == 0)
 3806                         break;
 3807         }
 3808         if (rm == NULL)
 3809                 return (ENOENT);
 3810 
 3811         /*
 3812          * If the resource index is -1, we want details on the
 3813          * resource manager.
 3814          */
 3815         if (res_idx == -1) {
 3816                 bzero(&urm, sizeof(urm));
 3817                 urm.rm_handle = (uintptr_t)rm;
 3818                 strlcpy(urm.rm_descr, rm->rm_descr, RM_TEXTLEN);
 3819                 urm.rm_start = rm->rm_start;
 3820                 urm.rm_size = rm->rm_end - rm->rm_start + 1;
 3821                 urm.rm_type = rm->rm_type;
 3822 
 3823                 error = SYSCTL_OUT(req, &urm, sizeof(urm));
 3824                 return (error);
 3825         }
 3826 
 3827         /*
 3828          * Find the indexed resource and return it.
 3829          */
 3830         TAILQ_FOREACH(res, &rm->rm_list, r_link) {
 3831                 if (res_idx-- == 0) {
 3832                         bzero(&ures, sizeof(ures));
 3833                         ures.r_handle = (uintptr_t)res;
 3834                         ures.r_parent = (uintptr_t)res->r_rm;
 3835                         ures.r_device = (uintptr_t)res->r_dev;
 3836                         if (res->r_dev != NULL) {
 3837                                 if (device_get_name(res->r_dev) != NULL) {
 3838                                         snprintf(ures.r_devname, RM_TEXTLEN,
 3839                                             "%s%d",
 3840                                             device_get_name(res->r_dev),
 3841                                             device_get_unit(res->r_dev));
 3842                                 } else {
 3843                                         strlcpy(ures.r_devname, "nomatch",
 3844                                             RM_TEXTLEN);
 3845                                 }
 3846                         } else {
 3847                                 ures.r_devname[0] = '\0';
 3848                         }
 3849                         ures.r_start = res->r_start;
 3850                         ures.r_size = res->r_end - res->r_start + 1;
 3851                         ures.r_flags = res->r_flags;
 3852 
 3853                         error = SYSCTL_OUT(req, &ures, sizeof(ures));
 3854                         return (error);
 3855                 }
 3856         }
 3857         return (ENOENT);
 3858 }
 3859 
 3860 SYSCTL_NODE(_hw_bus, OID_AUTO, rman, CTLFLAG_RD, sysctl_rman,
 3861     "kernel resource manager");
 3862 
 3863 int
 3864 bus_data_generation_check(int generation)
 3865 {
 3866         if (generation != bus_data_generation)
 3867                 return (1);
 3868 
 3869         /* XXX generate optimised lists here? */
 3870         return (0);
 3871 }
 3872 
 3873 void
 3874 bus_data_generation_update(void)
 3875 {
 3876         bus_data_generation++;
 3877 }

Cache object: 9c27cc636b3e33d7021c743abf0ffb4c


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