Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 7 | #include <string.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 8 | #include <zephyr/device.h> |
| 9 | #include <zephyr/sys/atomic.h> |
Gerard Marull-Paretas | dacb3db | 2023-05-15 15:50:28 +0200 | [diff] [blame] | 10 | #include <zephyr/sys/iterable_sections.h> |
Gerard Marull-Paretas | 2784588 | 2022-10-04 14:46:10 +0200 | [diff] [blame] | 11 | #include <zephyr/sys/kobject.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 12 | #include <zephyr/internal/syscall_handler.h> |
Gerard Marull-Paretas | d0e58ad | 2023-04-26 10:30:28 +0200 | [diff] [blame] | 13 | #include <zephyr/toolchain.h> |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 14 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 15 | /** |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 16 | * @brief Initialize state for all static devices. |
| 17 | * |
| 18 | * The state object is always zero-initialized, but this may not be |
| 19 | * sufficient. |
| 20 | */ |
| 21 | void z_device_state_init(void) |
| 22 | { |
Gerard Marull-Paretas | d0e58ad | 2023-04-26 10:30:28 +0200 | [diff] [blame] | 23 | STRUCT_SECTION_FOREACH(device, dev) { |
Anas Nashif | c91cad7 | 2023-09-26 21:32:13 +0000 | [diff] [blame] | 24 | k_object_init(dev); |
Peter Bigot | 1cadd8b | 2021-02-02 10:07:18 -0600 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 28 | const struct device *z_impl_device_get_binding(const char *name) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 29 | { |
Peter Bigot | 5b36a01 | 2021-02-12 06:19:08 -0600 | [diff] [blame] | 30 | /* A null string identifies no device. So does an empty |
| 31 | * string. |
| 32 | */ |
Anas Nashif | 0630452 | 2021-03-29 17:13:18 -0400 | [diff] [blame] | 33 | if ((name == NULL) || (name[0] == '\0')) { |
Peter Bigot | 5b36a01 | 2021-02-12 06:19:08 -0600 | [diff] [blame] | 34 | return NULL; |
| 35 | } |
| 36 | |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 37 | /* Split the search into two loops: in the common scenario, where |
| 38 | * device names are stored in ROM (and are referenced by the user |
| 39 | * with CONFIG_* macros), only cheap pointer comparisons will be |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 40 | * performed. Reserve string comparisons for a fallback. |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 41 | */ |
Gerard Marull-Paretas | d0e58ad | 2023-04-26 10:30:28 +0200 | [diff] [blame] | 42 | STRUCT_SECTION_FOREACH(device, dev) { |
Flavio Ceolin | 65fc5b7 | 2024-05-16 15:22:44 -0700 | [diff] [blame] | 43 | if (z_impl_device_is_ready(dev) && (dev->name == name)) { |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 44 | return dev; |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Gerard Marull-Paretas | d0e58ad | 2023-04-26 10:30:28 +0200 | [diff] [blame] | 48 | STRUCT_SECTION_FOREACH(device, dev) { |
Flavio Ceolin | 65fc5b7 | 2024-05-16 15:22:44 -0700 | [diff] [blame] | 49 | if (z_impl_device_is_ready(dev) && (strcmp(name, dev->name) == 0)) { |
Tomasz Bursztyka | 8d7bb8f | 2020-03-09 11:02:20 +0100 | [diff] [blame] | 50 | return dev; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 57 | #ifdef CONFIG_USERSPACE |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 58 | static inline const struct device *z_vrfy_device_get_binding(const char *name) |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 59 | { |
| 60 | char name_copy[Z_DEVICE_MAX_NAME_LEN]; |
| 61 | |
Hess Nathan | e05c4a8 | 2024-05-07 13:22:50 +0200 | [diff] [blame] | 62 | if (k_usermode_string_copy(name_copy, name, sizeof(name_copy)) |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 63 | != 0) { |
Daniel Leung | 0773441 | 2021-04-27 11:39:33 -0700 | [diff] [blame] | 64 | return NULL; |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 67 | return z_impl_device_get_binding(name_copy); |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 68 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 69 | #include <zephyr/syscalls/device_get_binding_mrsh.c> |
Andrzej Głąbek | 6de16d0 | 2021-03-12 16:25:59 +0100 | [diff] [blame] | 70 | |
Gerard Marull-Paretas | 47bfb6f | 2021-12-23 17:21:41 +0100 | [diff] [blame] | 71 | static inline bool z_vrfy_device_is_ready(const struct device *dev) |
| 72 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 73 | K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY)); |
Gerard Marull-Paretas | 47bfb6f | 2021-12-23 17:21:41 +0100 | [diff] [blame] | 74 | |
| 75 | return z_impl_device_is_ready(dev); |
| 76 | } |
Yong Cong Sin | bbe5e1e | 2024-01-24 17:35:04 +0800 | [diff] [blame] | 77 | #include <zephyr/syscalls/device_is_ready_mrsh.c> |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 78 | #endif /* CONFIG_USERSPACE */ |
| 79 | |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 80 | size_t z_device_get_all_static(struct device const **devices) |
Peter Bigot | 219a3ca | 2020-06-22 08:55:37 -0500 | [diff] [blame] | 81 | { |
Gerard Marull-Paretas | d0e58ad | 2023-04-26 10:30:28 +0200 | [diff] [blame] | 82 | size_t cnt; |
| 83 | |
| 84 | STRUCT_SECTION_GET(device, 0, devices); |
| 85 | STRUCT_SECTION_COUNT(device, &cnt); |
| 86 | |
| 87 | return cnt; |
Peter Bigot | 219a3ca | 2020-06-22 08:55:37 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Flavio Ceolin | 65fc5b7 | 2024-05-16 15:22:44 -0700 | [diff] [blame] | 90 | bool z_impl_device_is_ready(const struct device *dev) |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 91 | { |
Armando Visconti | 4b4068c | 2021-05-13 18:24:25 +0200 | [diff] [blame] | 92 | /* |
| 93 | * if an invalid device pointer is passed as argument, this call |
| 94 | * reports the `device` as not ready for usage. |
| 95 | */ |
| 96 | if (dev == NULL) { |
| 97 | return false; |
| 98 | } |
| 99 | |
Anas Nashif | bbbc38b | 2021-03-29 10:03:49 -0400 | [diff] [blame] | 100 | return dev->state->initialized && (dev->state->init_res == 0U); |
Tomasz Bursztyka | aac9e2c | 2020-04-30 11:49:39 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Gerard Marull-Paretas | 48b201c | 2023-06-14 14:30:41 +0200 | [diff] [blame] | 103 | #ifdef CONFIG_DEVICE_DEPS |
| 104 | |
Jordan Yates | f515aa0 | 2021-08-20 19:29:39 +1000 | [diff] [blame] | 105 | static int device_visitor(const device_handle_t *handles, |
| 106 | size_t handle_count, |
| 107 | device_visitor_callback_t visitor_cb, |
| 108 | void *context) |
Peter Bigot | b29abe3 | 2021-02-22 11:42:08 -0600 | [diff] [blame] | 109 | { |
Peter Bigot | b29abe3 | 2021-02-22 11:42:08 -0600 | [diff] [blame] | 110 | /* Iterate over fixed devices */ |
| 111 | for (size_t i = 0; i < handle_count; ++i) { |
| 112 | device_handle_t dh = handles[i]; |
| 113 | const struct device *rdev = device_from_handle(dh); |
| 114 | int rc = visitor_cb(rdev, context); |
| 115 | |
| 116 | if (rc < 0) { |
| 117 | return rc; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return handle_count; |
| 122 | } |
Jordan Yates | f515aa0 | 2021-08-20 19:29:39 +1000 | [diff] [blame] | 123 | |
| 124 | int device_required_foreach(const struct device *dev, |
| 125 | device_visitor_callback_t visitor_cb, |
| 126 | void *context) |
| 127 | { |
| 128 | size_t handle_count = 0; |
| 129 | const device_handle_t *handles = device_required_handles_get(dev, &handle_count); |
| 130 | |
| 131 | return device_visitor(handles, handle_count, visitor_cb, context); |
| 132 | } |
| 133 | |
| 134 | int device_supported_foreach(const struct device *dev, |
| 135 | device_visitor_callback_t visitor_cb, |
| 136 | void *context) |
| 137 | { |
| 138 | size_t handle_count = 0; |
| 139 | const device_handle_t *handles = device_supported_handles_get(dev, &handle_count); |
| 140 | |
| 141 | return device_visitor(handles, handle_count, visitor_cb, context); |
| 142 | } |
Gerard Marull-Paretas | 48b201c | 2023-06-14 14:30:41 +0200 | [diff] [blame] | 143 | |
| 144 | #endif /* CONFIG_DEVICE_DEPS */ |