From 0d4b957b119bd6fda232e4b20b1a83e736f5935f Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Mon, 24 Feb 2025 11:12:15 +0100 Subject: [PATCH] device: allow initializing any device Remove restrictions from device_init by allowing to perform device initialization if the device state flags it being not initialized. This makes the API usable in contexts where device_deinit has been called before. Signed-off-by: Gerard Marull-Paretas --- include/zephyr/device.h | 7 +++---- kernel/init.c | 13 +++---------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/zephyr/device.h b/include/zephyr/device.h index fec9eed8ee5..e4c7a6e938a 100644 --- a/include/zephyr/device.h +++ b/include/zephyr/device.h @@ -825,13 +825,12 @@ __syscall bool device_is_ready(const struct device *dev); * * A device whose initialization was deferred (by marking it as * ``zephyr,deferred-init`` on devicetree) needs to be initialized manually via - * this call. Note that only devices whose initialization was deferred can be - * initialized via this call - one can not try to initialize a non - * initialization deferred device that failed initialization with this call. + * this call. De-initialized devices can also be initialized again via this + * call. * * @param dev device to be initialized. * - * @retval -ENOENT If device was not found - or isn't a deferred one. + * @retval -EALREADY Device is already initialized. * @retval -errno For other errors. */ __syscall int device_init(const struct device *dev); diff --git a/kernel/init.c b/kernel/init.c index 066b11bbf59..564e19da1a0 100644 --- a/kernel/init.c +++ b/kernel/init.c @@ -378,18 +378,11 @@ static void z_sys_init_run_level(enum init_level level) int z_impl_device_init(const struct device *dev) { - const struct device *devs; - size_t devc; - - devc = z_device_get_all_static(&devs); - - for (const struct device *dev_ = devs; dev_ < (devs + devc); dev_++) { - if ((dev_ == dev) && ((dev->flags & DEVICE_FLAG_INIT_DEFERRED) != 0U)) { - return do_device_init(dev); - } + if (dev->state->initialized) { + return -EALREADY; } - return -ENOENT; + return do_device_init(dev); } #ifdef CONFIG_USERSPACE