Browse Source

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 <gerard.marull@nordicsemi.no>
pull/85182/head
Gerard Marull-Paretas 4 months ago committed by Henrik Brix Andersen
parent
commit
0d4b957b11
  1. 7
      include/zephyr/device.h
  2. 13
      kernel/init.c

7
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 * A device whose initialization was deferred (by marking it as
* ``zephyr,deferred-init`` on devicetree) needs to be initialized manually via * ``zephyr,deferred-init`` on devicetree) needs to be initialized manually via
* this call. Note that only devices whose initialization was deferred can be * this call. De-initialized devices can also be initialized again via this
* initialized via this call - one can not try to initialize a non * call.
* initialization deferred device that failed initialization with this call.
* *
* @param dev device to be initialized. * @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. * @retval -errno For other errors.
*/ */
__syscall int device_init(const struct device *dev); __syscall int device_init(const struct device *dev);

13
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) int z_impl_device_init(const struct device *dev)
{ {
const struct device *devs; if (dev->state->initialized) {
size_t devc; return -EALREADY;
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);
}
} }
return -ENOENT; return do_device_init(dev);
} }
#ifdef CONFIG_USERSPACE #ifdef CONFIG_USERSPACE

Loading…
Cancel
Save