Skip to content

Commit de63a61

Browse files
committed
kernel: fix error propagation for device deferred initialization
This fix propagates the failure code returned from the device initialization function to the application. Previously, do_device_init() mistakely returned errno instead of -errno. Signed-off-by: Loic Domaigne <tech@domaigne.com>
1 parent a10f807 commit de63a61

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

kernel/init.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
#include <zephyr/internal/syscall_handler.h>
4242
LOG_MODULE_REGISTER(os, CONFIG_KERNEL_LOG_LEVEL);
4343

44+
/* avoid pulling in stdlib */
45+
#undef _abs
46+
#define _abs(x) ((x) < 0 ? (-(x)) : (x))
47+
48+
4449
/* the only struct z_kernel instance */
4550
__pinned_bss
4651
struct z_kernel _kernel;
@@ -313,20 +318,14 @@ static int do_device_init(const struct device *dev)
313318

314319
if (dev->ops.init != NULL) {
315320
rc = dev->ops.init(dev);
316-
/* Mark device initialized. If initialization
317-
* failed, record the error condition.
318-
*/
321+
/* If initialization failed, record the error condition. */
319322
if (rc != 0) {
320-
if (rc < 0) {
321-
rc = -rc;
322-
}
323-
if (rc > UINT8_MAX) {
324-
rc = UINT8_MAX;
325-
}
326-
dev->state->init_res = rc;
323+
__ASSERT(rc != INT_MIN, "init(dev) failed, but init_res will be 0");
324+
dev->state->init_res = CLAMP(_abs(rc), 0, UINT8_MAX);
327325
}
328326
}
329327

328+
/* device initialization has been invoked */
330329
dev->state->initialized = true;
331330

332331
if (rc == 0) {

0 commit comments

Comments
 (0)