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_device.c

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

    1 /*      $NetBSD: subr_device.c,v 1.13 2022/03/28 12:38:59 riastradh Exp $       */
    2 
    3 /*
    4  * Copyright (c) 2006, 2021 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   26  * POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __KERNEL_RCSID(0, "$NetBSD: subr_device.c,v 1.13 2022/03/28 12:38:59 riastradh Exp $");
   31 
   32 #include <sys/param.h>
   33 #include <sys/device.h>
   34 #include <sys/device_impl.h>
   35 #include <sys/systm.h>
   36 
   37 #include <sys/device_calls.h>
   38 
   39 /* Root device. */
   40 device_t                        root_device;
   41 
   42 /*
   43  * devhandle_t accessors / mutators.
   44  */
   45 
   46 static bool
   47 devhandle_is_valid_internal(const devhandle_t * const handlep)
   48 {
   49         if (handlep->impl == NULL) {
   50                 return false;
   51         }
   52         return handlep->impl->type != DEVHANDLE_TYPE_INVALID;
   53 }
   54 
   55 bool
   56 devhandle_is_valid(devhandle_t handle)
   57 {
   58         return devhandle_is_valid_internal(&handle);
   59 }
   60 
   61 devhandle_t
   62 devhandle_invalid(void)
   63 {
   64         static const devhandle_t invalid_devhandle = {
   65                 .impl = NULL,
   66                 .uintptr = 0,
   67         };
   68         return invalid_devhandle;
   69 }
   70 
   71 devhandle_type_t
   72 devhandle_type(devhandle_t handle)
   73 {
   74         if (!devhandle_is_valid_internal(&handle)) {
   75                 return DEVHANDLE_TYPE_INVALID;
   76         }
   77 
   78         return handle.impl->type;
   79 }
   80 
   81 int
   82 devhandle_compare(devhandle_t handle1, devhandle_t handle2)
   83 {
   84         devhandle_type_t type1 = devhandle_type(handle1);
   85         devhandle_type_t type2 = devhandle_type(handle2);
   86 
   87         if (type1 == DEVHANDLE_TYPE_INVALID) {
   88                 return -1;
   89         }
   90         if (type2 == DEVHANDLE_TYPE_INVALID) {
   91                 return 1;
   92         }
   93 
   94         if (type1 < type2) {
   95                 return -1;
   96         }
   97         if (type1 > type2) {
   98                 return 1;
   99         }
  100 
  101         /* For private handles, we also compare the impl pointers. */
  102         if (type1 == DEVHANDLE_TYPE_PRIVATE) {
  103                 intptr_t impl1 = (intptr_t)handle1.impl;
  104                 intptr_t impl2 = (intptr_t)handle2.impl;
  105 
  106                 if (impl1 < impl2) {
  107                         return -1;
  108                 }
  109                 if (impl1 > impl2) {
  110                         return 1;
  111                 }
  112         }
  113 
  114         if (handle1.integer < handle2.integer) {
  115                 return -1;
  116         }
  117         if (handle1.integer > handle2.integer) {
  118                 return 1;
  119         }
  120 
  121         return 0;
  122 }
  123 
  124 device_call_t
  125 devhandle_lookup_device_call(devhandle_t handle, const char *name,
  126     devhandle_t *call_handlep)
  127 {
  128         const struct devhandle_impl *impl;
  129         device_call_t call;
  130 
  131         /*
  132          * The back-end can override the handle to use for the call,
  133          * if needed.
  134          */
  135         *call_handlep = handle;
  136 
  137         for (impl = handle.impl; impl != NULL; impl = impl->super) {
  138                 if (impl->lookup_device_call != NULL) {
  139                         call = impl->lookup_device_call(handle, name,
  140                             call_handlep);
  141                         if (call != NULL) {
  142                                 return call;
  143                         }
  144                 }
  145         }
  146         return NULL;
  147 }
  148 
  149 void
  150 devhandle_impl_inherit(struct devhandle_impl *impl,
  151     const struct devhandle_impl *super)
  152 {
  153         memcpy(impl, super, sizeof(*impl));
  154         impl->super = super;
  155 }
  156 
  157 /*
  158  * Accessor functions for the device_t type.
  159  */
  160 
  161 devclass_t
  162 device_class(device_t dev)
  163 {
  164 
  165         return dev->dv_class;
  166 }
  167 
  168 cfdata_t
  169 device_cfdata(device_t dev)
  170 {
  171 
  172         return dev->dv_cfdata;
  173 }
  174 
  175 cfdriver_t
  176 device_cfdriver(device_t dev)
  177 {
  178 
  179         return dev->dv_cfdriver;
  180 }
  181 
  182 cfattach_t
  183 device_cfattach(device_t dev)
  184 {
  185 
  186         return dev->dv_cfattach;
  187 }
  188 
  189 int
  190 device_unit(device_t dev)
  191 {
  192 
  193         return dev->dv_unit;
  194 }
  195 
  196 const char *
  197 device_xname(device_t dev)
  198 {
  199 
  200         return dev->dv_xname;
  201 }
  202 
  203 device_t
  204 device_parent(device_t dev)
  205 {
  206 
  207         return dev->dv_parent;
  208 }
  209 
  210 bool
  211 device_activation(device_t dev, devact_level_t level)
  212 {
  213         int active_flags;
  214 
  215         active_flags = DVF_ACTIVE;
  216         switch (level) {
  217         case DEVACT_LEVEL_FULL:
  218                 active_flags |= DVF_CLASS_SUSPENDED;
  219                 /*FALLTHROUGH*/
  220         case DEVACT_LEVEL_DRIVER:
  221                 active_flags |= DVF_DRIVER_SUSPENDED;
  222                 /*FALLTHROUGH*/
  223         case DEVACT_LEVEL_BUS:
  224                 active_flags |= DVF_BUS_SUSPENDED;
  225                 break;
  226         }
  227 
  228         return (dev->dv_flags & active_flags) == DVF_ACTIVE;
  229 }
  230 
  231 bool
  232 device_is_active(device_t dev)
  233 {
  234         int active_flags;
  235 
  236         active_flags = DVF_ACTIVE;
  237         active_flags |= DVF_CLASS_SUSPENDED;
  238         active_flags |= DVF_DRIVER_SUSPENDED;
  239         active_flags |= DVF_BUS_SUSPENDED;
  240 
  241         return (dev->dv_flags & active_flags) == DVF_ACTIVE;
  242 }
  243 
  244 bool
  245 device_is_enabled(device_t dev)
  246 {
  247         return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
  248 }
  249 
  250 bool
  251 device_has_power(device_t dev)
  252 {
  253         int active_flags;
  254 
  255         active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
  256 
  257         return (dev->dv_flags & active_flags) == DVF_ACTIVE;
  258 }
  259 
  260 int
  261 device_locator(device_t dev, u_int locnum)
  262 {
  263 
  264         KASSERT(dev->dv_locators != NULL);
  265         return dev->dv_locators[locnum];
  266 }
  267 
  268 void *
  269 device_private(device_t dev)
  270 {
  271 
  272         /*
  273          * The reason why device_private(NULL) is allowed is to simplify the
  274          * work of a lot of userspace request handlers (i.e., c/bdev
  275          * handlers) which grab cfdriver_t->cd_units[n].
  276          * It avoids having them test for it to be NULL and only then calling
  277          * device_private.
  278          */
  279         return dev == NULL ? NULL : dev->dv_private;
  280 }
  281 
  282 void
  283 device_set_private(device_t dev, void *private)
  284 {
  285 
  286         KASSERTMSG(dev->dv_private == NULL, "device_set_private(%p, %p):"
  287             " device %s already has private set to %p",
  288             dev, private, device_xname(dev), device_private(dev));
  289         KASSERT(private != NULL);
  290         dev->dv_private = private;
  291 }
  292 
  293 prop_dictionary_t
  294 device_properties(device_t dev)
  295 {
  296 
  297         return dev->dv_properties;
  298 }
  299 
  300 /*
  301  * device_is_a:
  302  *
  303  *      Returns true if the device is an instance of the specified
  304  *      driver.
  305  */
  306 bool
  307 device_is_a(device_t dev, const char *dname)
  308 {
  309         if (dev == NULL || dev->dv_cfdriver == NULL) {
  310                 return false;
  311         }
  312 
  313         return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
  314 }
  315 
  316 /*
  317  * device_attached_to_iattr:
  318  *
  319  *      Returns true if the device attached to the specified interface
  320  *      attribute.
  321  */
  322 bool
  323 device_attached_to_iattr(device_t dev, const char *iattr)
  324 {
  325         cfdata_t cfdata = device_cfdata(dev);
  326         const struct cfparent *pspec;
  327 
  328         if (cfdata == NULL || (pspec = cfdata->cf_pspec) == NULL) {
  329                 return false;
  330         }
  331 
  332         return strcmp(pspec->cfp_iattr, iattr) == 0;
  333 }
  334 
  335 void
  336 device_set_handle(device_t dev, devhandle_t handle)
  337 {
  338         dev->dv_handle = handle;
  339 }
  340 
  341 devhandle_t
  342 device_handle(device_t dev)
  343 {
  344         return dev->dv_handle;
  345 }
  346 
  347 int
  348 device_call_generic(device_t dev, const struct device_call_generic *gen)
  349 {
  350         devhandle_t handle = device_handle(dev);
  351         device_call_t call;
  352         devhandle_t call_handle;
  353 
  354         call = devhandle_lookup_device_call(handle, gen->name, &call_handle);
  355         if (call == NULL) {
  356                 return ENOTSUP;
  357         }
  358         return call(dev, call_handle, gen->args);
  359 }
  360 
  361 int
  362 device_enumerate_children(device_t dev,
  363     bool (*callback)(device_t, devhandle_t, void *),
  364     void *callback_arg)
  365 {
  366         struct device_enumerate_children_args args = {
  367                 .callback = callback,
  368                 .callback_arg = callback_arg,
  369         };
  370 
  371         return device_call(dev, DEVICE_ENUMERATE_CHILDREN(&args));
  372 }

Cache object: d89397fc3e2e5ee7691b2bc8f58a20bc


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