blob: 37233f583722427b311d55983fbcd6843b480905 [file] [log] [blame]
Kumar Galad12d8af2016-10-05 12:01:54 -05001/*
2 * Copyright (c) 2015-2016 Intel Corporation.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Kumar Galad12d8af2016-10-05 12:01:54 -05005 */
6
Kumar Galad12d8af2016-10-05 12:01:54 -05007#include <string.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02008#include <zephyr/device.h>
9#include <zephyr/sys/atomic.h>
Gerard Marull-Paretasdacb3db2023-05-15 15:50:28 +020010#include <zephyr/sys/iterable_sections.h>
Gerard Marull-Paretas27845882022-10-04 14:46:10 +020011#include <zephyr/sys/kobject.h>
Anas Nashif4e396172023-09-26 22:46:01 +000012#include <zephyr/internal/syscall_handler.h>
Gerard Marull-Paretasd0e58ad2023-04-26 10:30:28 +020013#include <zephyr/toolchain.h>
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +020014
Kumar Galad12d8af2016-10-05 12:01:54 -050015/**
Peter Bigot1cadd8b2021-02-02 10:07:18 -060016 * @brief Initialize state for all static devices.
17 *
18 * The state object is always zero-initialized, but this may not be
19 * sufficient.
20 */
21void z_device_state_init(void)
22{
Gerard Marull-Paretasd0e58ad2023-04-26 10:30:28 +020023 STRUCT_SECTION_FOREACH(device, dev) {
Anas Nashifc91cad72023-09-26 21:32:13 +000024 k_object_init(dev);
Peter Bigot1cadd8b2021-02-02 10:07:18 -060025 }
26}
27
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020028const struct device *z_impl_device_get_binding(const char *name)
Kumar Galad12d8af2016-10-05 12:01:54 -050029{
Peter Bigot5b36a012021-02-12 06:19:08 -060030 /* A null string identifies no device. So does an empty
31 * string.
32 */
Anas Nashif06304522021-03-29 17:13:18 -040033 if ((name == NULL) || (name[0] == '\0')) {
Peter Bigot5b36a012021-02-12 06:19:08 -060034 return NULL;
35 }
36
Leandro Pereirab55eb032018-02-14 14:47:11 -080037 /* 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 Bursztyka8d7bb8f2020-03-09 11:02:20 +010040 * performed. Reserve string comparisons for a fallback.
Leandro Pereirab55eb032018-02-14 14:47:11 -080041 */
Gerard Marull-Paretasd0e58ad2023-04-26 10:30:28 +020042 STRUCT_SECTION_FOREACH(device, dev) {
Flavio Ceolin65fc5b72024-05-16 15:22:44 -070043 if (z_impl_device_is_ready(dev) && (dev->name == name)) {
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010044 return dev;
Leandro Pereirab55eb032018-02-14 14:47:11 -080045 }
46 }
47
Gerard Marull-Paretasd0e58ad2023-04-26 10:30:28 +020048 STRUCT_SECTION_FOREACH(device, dev) {
Flavio Ceolin65fc5b72024-05-16 15:22:44 -070049 if (z_impl_device_is_ready(dev) && (strcmp(name, dev->name) == 0)) {
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010050 return dev;
Kumar Galad12d8af2016-10-05 12:01:54 -050051 }
52 }
53
54 return NULL;
55}
56
Andrew Boie9d148742018-11-12 10:25:12 -080057#ifdef CONFIG_USERSPACE
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020058static inline const struct device *z_vrfy_device_get_binding(const char *name)
Andrew Boie9d148742018-11-12 10:25:12 -080059{
60 char name_copy[Z_DEVICE_MAX_NAME_LEN];
61
Hess Nathane05c4a82024-05-07 13:22:50 +020062 if (k_usermode_string_copy(name_copy, name, sizeof(name_copy))
Andrew Boie9d148742018-11-12 10:25:12 -080063 != 0) {
Daniel Leung07734412021-04-27 11:39:33 -070064 return NULL;
Andrew Boie9d148742018-11-12 10:25:12 -080065 }
66
Andy Ross65649742019-08-06 13:34:31 -070067 return z_impl_device_get_binding(name_copy);
Andrew Boie9d148742018-11-12 10:25:12 -080068}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080069#include <zephyr/syscalls/device_get_binding_mrsh.c>
Andrzej Głąbek6de16d02021-03-12 16:25:59 +010070
Gerard Marull-Paretas47bfb6f2021-12-23 17:21:41 +010071static inline bool z_vrfy_device_is_ready(const struct device *dev)
72{
Anas Nashifa08bfeb2023-09-27 11:20:28 +000073 K_OOPS(K_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
Gerard Marull-Paretas47bfb6f2021-12-23 17:21:41 +010074
75 return z_impl_device_is_ready(dev);
76}
Yong Cong Sinbbe5e1e2024-01-24 17:35:04 +080077#include <zephyr/syscalls/device_is_ready_mrsh.c>
Andrew Boie9d148742018-11-12 10:25:12 -080078#endif /* CONFIG_USERSPACE */
79
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020080size_t z_device_get_all_static(struct device const **devices)
Peter Bigot219a3ca2020-06-22 08:55:37 -050081{
Gerard Marull-Paretasd0e58ad2023-04-26 10:30:28 +020082 size_t cnt;
83
84 STRUCT_SECTION_GET(device, 0, devices);
85 STRUCT_SECTION_COUNT(device, &cnt);
86
87 return cnt;
Peter Bigot219a3ca2020-06-22 08:55:37 -050088}
89
Flavio Ceolin65fc5b72024-05-16 15:22:44 -070090bool z_impl_device_is_ready(const struct device *dev)
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +020091{
Armando Visconti4b4068c2021-05-13 18:24:25 +020092 /*
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 Nashifbbbc38b2021-03-29 10:03:49 -0400100 return dev->state->initialized && (dev->state->init_res == 0U);
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +0200101}
102
Gerard Marull-Paretas48b201c2023-06-14 14:30:41 +0200103#ifdef CONFIG_DEVICE_DEPS
104
Jordan Yatesf515aa02021-08-20 19:29:39 +1000105static int device_visitor(const device_handle_t *handles,
106 size_t handle_count,
107 device_visitor_callback_t visitor_cb,
108 void *context)
Peter Bigotb29abe32021-02-22 11:42:08 -0600109{
Peter Bigotb29abe32021-02-22 11:42:08 -0600110 /* 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 Yatesf515aa02021-08-20 19:29:39 +1000123
124int 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
134int 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-Paretas48b201c2023-06-14 14:30:41 +0200143
144#endif /* CONFIG_DEVICE_DEPS */