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

Cache object: 0717ba961e88c7930659b57aeae49c46


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