Skip to content

Commit 0d4b957

Browse files
gmarullhenrikbrixandersen
authored andcommitted
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 <[email protected]>
1 parent 8e59d4c commit 0d4b957

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
@@ -825,13 +825,12 @@ __syscall bool device_is_ready(const struct device *dev);
825825
*
826826
* A device whose initialization was deferred (by marking it as
827827
* ``zephyr,deferred-init`` on devicetree) needs to be initialized manually via
828-
* this call. Note that only devices whose initialization was deferred can be
829-
* initialized via this call - one can not try to initialize a non
830-
* initialization deferred device that failed initialization with this call.
828+
* this call. De-initialized devices can also be initialized again via this
829+
* call.
831830
*
832831
* @param dev device to be initialized.
833832
*
834-
* @retval -ENOENT If device was not found - or isn't a deferred one.
833+
* @retval -EALREADY Device is already initialized.
835834
* @retval -errno For other errors.
836835
*/
837836
__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
@@ -378,18 +378,11 @@ static void z_sys_init_run_level(enum init_level level)
378378

379379
int z_impl_device_init(const struct device *dev)
380380
{
381-
const struct device *devs;
382-
size_t devc;
383-
384-
devc = z_device_get_all_static(&devs);
385-
386-
for (const struct device *dev_ = devs; dev_ < (devs + devc); dev_++) {
387-
if ((dev_ == dev) && ((dev->flags & DEVICE_FLAG_INIT_DEFERRED) != 0U)) {
388-
return do_device_init(dev);
389-
}
381+
if (dev->state->initialized) {
382+
return -EALREADY;
390383
}
391384

392-
return -ENOENT;
385+
return do_device_init(dev);
393386
}
394387

395388
#ifdef CONFIG_USERSPACE

0 commit comments

Comments
 (0)