Skip to content

Commit ea4e48d

Browse files
committed
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>
1 parent 46d0ddb commit ea4e48d

File tree

2 files changed

+6
-14
lines changed

2 files changed

+6
-14
lines changed

include/zephyr/device.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -835,13 +835,12 @@ __syscall bool device_is_ready(const struct device *dev);
835835
*
836836
* A device whose initialization was deferred (by marking it as
837837
* ``zephyr,deferred-init`` on devicetree) needs to be initialized manually via
838-
* this call. Note that only devices whose initialization was deferred can be
839-
* initialized via this call - one can not try to initialize a non
840-
* initialization deferred device that failed initialization with this call.
838+
* this call. De-initialized devices can also be initialized again via this
839+
* call.
841840
*
842841
* @param dev device to be initialized.
843842
*
844-
* @retval -ENOENT If device was not found - or isn't a deferred one.
843+
* @retval -EALREADY Device is already initialized.
845844
* @retval -errno For other errors.
846845
*/
847846
__syscall int device_init(const struct device *dev);

kernel/init.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,18 +376,11 @@ static void z_sys_init_run_level(enum init_level level)
376376

377377
int z_impl_device_init(const struct device *dev)
378378
{
379-
const struct device *devs;
380-
size_t devc;
381-
382-
devc = z_device_get_all_static(&devs);
383-
384-
for (const struct device *dev_ = devs; dev_ < (devs + devc); dev_++) {
385-
if ((dev_ == dev) && ((dev->flags & DEVICE_FLAG_INIT_DEFERRED) != 0U)) {
386-
return do_device_init(dev);
387-
}
379+
if (dev->state->initialized) {
380+
return -EALREADY;
388381
}
389382

390-
return -ENOENT;
383+
return do_device_init(dev);
391384
}
392385

393386
#ifdef CONFIG_USERSPACE

0 commit comments

Comments
 (0)