LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel - device.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 0.0 % 30 0
Test Date: 2026-11-06 14:43:19 Functions: 0.0 % 5 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 24 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2015-2016 Intel Corporation.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : #include <errno.h>
       8                 :             : #include <stddef.h>
       9                 :             : #include <string.h>
      10                 :             : #include <zephyr/device.h>
      11                 :             : #include <zephyr/sys/atomic.h>
      12                 :             : #include <zephyr/sys/iterable_sections.h>
      13                 :             : #include <zephyr/sys/kobject.h>
      14                 :             : #include <zephyr/internal/syscall_handler.h>
      15                 :             : #include <zephyr/toolchain.h>
      16                 :             : #include <zephyr/pm/device_runtime.h>
      17                 :             : 
      18                 :           0 : int do_device_init(const struct device *dev)
      19                 :             : {
      20                 :           0 :         int rc = 0;
      21                 :             : 
      22         [ #  # ]:           0 :         if (dev->ops.init != NULL) {
      23                 :           0 :                 rc = dev->ops.init(dev);
      24                 :             :                 /* If initialization failed, record in dev->state->init_res
      25                 :             :                  * the POSITIVE value of the resulting errno
      26                 :             :                  */
      27         [ #  # ]:           0 :                 if (rc != 0) {
      28                 :             :                         /* device's init function should return:
      29                 :             :                          *   0 on success
      30                 :             :                          *   a negative value on failure (-errno)
      31                 :             :                          * errno value maps to an uint8_t range as of now.
      32                 :             :                          */
      33         [ #  # ]:           0 :                         __ASSERT(rc >= -UINT8_MAX && rc < 0, "device %s init: invalid error (%d)",
      34                 :             :                                  dev->name, rc);
      35                 :             : 
      36                 :           0 :                         if (rc < 0) {
      37                 :           0 :                                 rc = -rc;
      38                 :             :                         }
      39                 :             :                         /* handle error value overflow in production
      40                 :             :                          * this is likely a bug in the device's init function. Signals it
      41                 :             :                          */
      42                 :           0 :                         if (rc > UINT8_MAX) {
      43                 :             :                                 rc = UINT8_MAX;
      44                 :             :                         }
      45                 :             :                 }
      46                 :             : 
      47                 :             :                 /* Always record the result: a device that failed to initialize
      48                 :             :                  * earlier must not keep that error once it initializes
      49                 :             :                  * successfully.
      50                 :             :                  */
      51                 :           0 :                 dev->state->init_res = rc;
      52                 :             :         }
      53                 :             : 
      54         [ #  # ]:           0 :         if (rc == 0) {
      55                 :             :                 /* A failed init leaves the device uninitialized, so it can be
      56                 :             :                  * retried by device_init() and nothing can be released by
      57                 :             :                  * device_deinit().
      58                 :             :                  */
      59                 :           0 :                 dev->state->initialized = true;
      60                 :             : 
      61                 :             :                 /* Run automatic device runtime enablement */
      62                 :           0 :                 (void)pm_device_runtime_auto_enable(dev);
      63                 :             :         }
      64                 :             : 
      65                 :             :         /* here, the value of rc is either 0 or +errno
      66                 :             :          * flip the sign to return a negative value on failure as expected
      67                 :             :          */
      68                 :           0 :         return -rc;
      69                 :             : }
      70                 :             : 
      71                 :           0 : int z_impl_device_init(const struct device *dev)
      72                 :             : {
      73         [ #  # ]:           0 :         if (dev->state->initialized) {
      74                 :             :                 return -EALREADY;
      75                 :             :         }
      76                 :             : 
      77                 :           0 :         return do_device_init(dev);
      78                 :             : }
      79                 :             : 
      80                 :             : #ifdef CONFIG_USERSPACE
      81                 :             : static inline int z_vrfy_device_init(const struct device *dev)
      82                 :             : {
      83                 :             :         K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_DRIVER_ANY));
      84                 :             : 
      85                 :             :         return z_impl_device_init(dev);
      86                 :             : }
      87                 :             : #include <zephyr/syscalls/device_init_mrsh.c>
      88                 :             : #endif
      89                 :             : 
      90                 :             : 
      91                 :           0 : const struct device *z_impl_device_get_binding(const char *name)
      92                 :             : {
      93                 :             :         /* A null string identifies no device.  So does an empty
      94                 :             :          * string.
      95                 :             :          */
      96   [ #  #  #  # ]:           0 :         if ((name == NULL) || (name[0] == '\0')) {
      97                 :             :                 return NULL;
      98                 :             :         }
      99                 :             : 
     100                 :             :         /* Return NULL if the device matching 'name' is not ready. */
     101   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(device, dev) {
     102   [ #  #  #  # ]:           0 :                 if ((dev->name == name) || (strcmp(name, dev->name) == 0)) {
     103         [ #  # ]:           0 :                         return z_impl_device_is_ready(dev) ? dev : NULL;
     104                 :             :                 }
     105                 :             :         }
     106                 :             : 
     107                 :             :         return NULL;
     108                 :             : }
     109                 :             : 
     110                 :             : #ifdef CONFIG_USERSPACE
     111                 :             : static inline const struct device *z_vrfy_device_get_binding(const char *name)
     112                 :             : {
     113                 :             :         char name_copy[Z_DEVICE_MAX_NAME_LEN];
     114                 :             : 
     115                 :             :         if (k_usermode_string_copy(name_copy, name, sizeof(name_copy))
     116                 :             :             != 0) {
     117                 :             :                 return NULL;
     118                 :             :         }
     119                 :             : 
     120                 :             :         return z_impl_device_get_binding(name_copy);
     121                 :             : }
     122                 :             : #include <zephyr/syscalls/device_get_binding_mrsh.c>
     123                 :             : 
     124                 :             : static inline bool z_vrfy_device_is_ready(const struct device *dev)
     125                 :             : {
     126                 :             :         /* NULL is a documented argument, not an invalid kernel object */
     127                 :             :         if (dev == NULL) {
     128                 :             :                 return false;
     129                 :             :         }
     130                 :             : 
     131                 :             :         K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_DRIVER_ANY));
     132                 :             : 
     133                 :             :         return z_impl_device_is_ready(dev);
     134                 :             : }
     135                 :             : #include <zephyr/syscalls/device_is_ready_mrsh.c>
     136                 :             : #endif /* CONFIG_USERSPACE */
     137                 :             : 
     138                 :             : #ifdef CONFIG_DEVICE_DT_METADATA
     139                 :             : const struct device *z_impl_device_get_by_dt_nodelabel(const char *nodelabel)
     140                 :             : {
     141                 :             :         /* For consistency with device_get_binding(). */
     142                 :             :         if ((nodelabel == NULL) || (nodelabel[0] == '\0')) {
     143                 :             :                 return NULL;
     144                 :             :         }
     145                 :             : 
     146                 :             :         /* Unlike device_get_binding(), which has a history of being
     147                 :             :          * used in application code, we don't expect
     148                 :             :          * device_get_by_dt_nodelabel() to be used outside of
     149                 :             :          * scenarios where a human is in the loop. The shell is the
     150                 :             :          * main expected use case. Therefore, nodelabel is probably
     151                 :             :          * not the same pointer as any of the entry->nodelabel
     152                 :             :          * elements. We therefore skip the pointer comparison that
     153                 :             :          * device_get_binding() does.
     154                 :             :          */
     155                 :             :         STRUCT_SECTION_FOREACH(device, dev) {
     156                 :             :                 const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
     157                 :             : 
     158                 :             :                 if (!z_impl_device_is_ready(dev) || nl == NULL) {
     159                 :             :                         continue;
     160                 :             :                 }
     161                 :             : 
     162                 :             :                 for (size_t i = 0; i < nl->num_nodelabels; i++) {
     163                 :             :                         const char *dev_nodelabel = nl->nodelabels[i];
     164                 :             : 
     165                 :             :                         if (strcmp(nodelabel, dev_nodelabel) == 0) {
     166                 :             :                                 return dev;
     167                 :             :                         }
     168                 :             :                 }
     169                 :             :         }
     170                 :             : 
     171                 :             :         return NULL;
     172                 :             : }
     173                 :             : 
     174                 :             : #ifdef CONFIG_USERSPACE
     175                 :             : static inline const struct device *z_vrfy_device_get_by_dt_nodelabel(const char *nodelabel)
     176                 :             : {
     177                 :             :         char nl_copy[Z_DEVICE_MAX_NODELABEL_LEN];
     178                 :             : 
     179                 :             :         if (k_usermode_string_copy(nl_copy, (char *)nodelabel, sizeof(nl_copy)) != 0) {
     180                 :             :                 return NULL;
     181                 :             :         }
     182                 :             : 
     183                 :             :         return z_impl_device_get_by_dt_nodelabel(nl_copy);
     184                 :             : }
     185                 :             : #include <zephyr/syscalls/device_get_by_dt_nodelabel_mrsh.c>
     186                 :             : #endif /* CONFIG_USERSPACE */
     187                 :             : #endif /* CONFIG_DEVICE_DT_METADATA */
     188                 :             : 
     189                 :           0 : size_t z_device_get_all_static(struct device const **devices)
     190                 :             : {
     191                 :           0 :         size_t cnt;
     192                 :             : 
     193                 :           0 :         STRUCT_SECTION_GET(device, 0, devices);
     194                 :           0 :         STRUCT_SECTION_COUNT(device, &cnt);
     195                 :             : 
     196                 :           0 :         return cnt;
     197                 :             : }
     198                 :             : 
     199                 :           0 : int z_impl_device_deinit(const struct device *dev)
     200                 :             : {
     201                 :             : #ifdef CONFIG_DEVICE_DEINIT_SUPPORT
     202                 :             :         int ret;
     203                 :             : 
     204                 :             :         if (!dev->state->initialized) {
     205                 :             :                 return -EPERM;
     206                 :             :         }
     207                 :             : 
     208                 :             :         if (dev->ops.deinit == NULL) {
     209                 :             :                 return -ENOTSUP;
     210                 :             :         }
     211                 :             : 
     212                 :             :         ret = dev->ops.deinit(dev);
     213                 :             :         if (ret < 0) {
     214                 :             :                 return ret;
     215                 :             :         }
     216                 :             : 
     217                 :             :         dev->state->initialized = false;
     218                 :             : 
     219                 :             :         return 0;
     220                 :             : #else
     221                 :           0 :         ARG_UNUSED(dev);
     222                 :           0 :         return -ENOTSUP;
     223                 :             : #endif /* CONFIG_DEVICE_DEINIT_SUPPORT */
     224                 :             : }
     225                 :             : 
     226                 :             : #ifdef CONFIG_USERSPACE
     227                 :             : static inline int z_vrfy_device_deinit(const struct device *dev)
     228                 :             : {
     229                 :             :         K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_DRIVER_ANY));
     230                 :             : 
     231                 :             :         return z_impl_device_deinit(dev);
     232                 :             : }
     233                 :             : #include <zephyr/syscalls/device_deinit_mrsh.c>
     234                 :             : #endif
     235                 :             : 
     236                 :             : #ifdef CONFIG_DEVICE_DEPS
     237                 :             : 
     238                 :             : static int device_visitor(const device_handle_t *handles,
     239                 :             :                            size_t handle_count,
     240                 :             :                            device_visitor_callback_t visitor_cb,
     241                 :             :                            void *context)
     242                 :             : {
     243                 :             :         /* Iterate over fixed devices */
     244                 :             :         for (size_t i = 0; i < handle_count; ++i) {
     245                 :             :                 device_handle_t dh = handles[i];
     246                 :             :                 const struct device *rdev = device_from_handle(dh);
     247                 :             :                 int rc = visitor_cb(rdev, context);
     248                 :             : 
     249                 :             :                 if (rc < 0) {
     250                 :             :                         return rc;
     251                 :             :                 }
     252                 :             :         }
     253                 :             : 
     254                 :             :         return handle_count;
     255                 :             : }
     256                 :             : 
     257                 :             : int device_required_foreach(const struct device *dev,
     258                 :             :                             device_visitor_callback_t visitor_cb,
     259                 :             :                             void *context)
     260                 :             : {
     261                 :             :         size_t handle_count = 0;
     262                 :             :         const device_handle_t *handles = device_required_handles_get(dev, &handle_count);
     263                 :             : 
     264                 :             :         return device_visitor(handles, handle_count, visitor_cb, context);
     265                 :             : }
     266                 :             : 
     267                 :             : int device_supported_foreach(const struct device *dev,
     268                 :             :                              device_visitor_callback_t visitor_cb,
     269                 :             :                              void *context)
     270                 :             : {
     271                 :             :         size_t handle_count = 0;
     272                 :             :         const device_handle_t *handles = device_supported_handles_get(dev, &handle_count);
     273                 :             : 
     274                 :             :         return device_visitor(handles, handle_count, visitor_cb, context);
     275                 :             : }
     276                 :             : 
     277                 :             : #endif /* CONFIG_DEVICE_DEPS */
        

Generated by: LCOV version 2.0-1