Skip to content

device_init doesn't pass on negative error codes from drivers #91095

@madone8

Description

@madone8

Describe the bug

Context: I have a custom device driver which is configured with the zephyr,deferred-init property in the devicetree.

I noticed when checking the return code of device_init() that although the driver's initialisation was failing and should be returning -EIO, this value was not being passed on to my application.

Looking through the codebase, device_init() ultimately calls do_device_init(), which converts negative error codes returned by the driver into positive codes.

Is this behaviour correct? It does seem contrary to the documentation.

This function also sets the initialised state to true irrespective of the return code from the driver.

static int do_device_init(const struct device *dev)
{
	int rc = 0;

	if (dev->ops.init != NULL) {
		rc = dev->ops.init(dev);
		/* Mark device initialized. If initialization
		 * failed, record the error condition.
		 */
		if (rc != 0) {
			if (rc < 0) {
				rc = -rc;
			}
			if (rc > UINT8_MAX) {
				rc = UINT8_MAX;
			}
			dev->state->init_res = rc;
		}
	}

	dev->state->initialized = true;

	if (rc == 0) {
		/* Run automatic device runtime enablement */
		(void)pm_device_runtime_auto_enable(dev);
	}

	return rc;
}

Impact

Annoyance – Minor irritation; no significant impact on usability or functionality.

Environment

zephyr 3.7.1

Metadata

Metadata

Labels

area: DocumentationbugThe issue is a bug, or the PR is fixing a bugpriority: lowLow impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions