From 59b455eb9c9729d5f0d27099866f2e2532a433b4 Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Thu, 25 Nov 2021 11:36:49 +0100 Subject: [PATCH] pm: device_runtime: simplify error handling for get/put In case runtime PM is not enabled (or not built-in), the get/put functions always return 0 (instead of -ENOTSUP/-ENOSYS). When runtime PM is disabled, a device is left into active state. Similarly, when device runtime PM is not built-in, it is safe to assume that a device will be active when it is called. If a user implements a custom solution, it is its responsability to make sure that a device is active when using it. For all these reasons, the -ENOTSUP/-ENOSYS are error codes that should always be ignored by devices using get/put, since in practice it means that: device is active, function is a no-op. The example below illustrates how error handling is simplified: ```c /* before: safe to ignore -ENOSYS/-ENOTSUP since device is active (we * can continue) */ ret = pm_device_runtime_get(dev); if ((ret < 0) && (ret != -ENOSYS) && (ret != -ENOTSUP)) { return ret; } /* now */ ret = pm_device_runtime_get(dev); if (ret < 0) { return ret; } ``` Signed-off-by: Gerard Marull-Paretas --- include/pm/device_runtime.h | 21 ++++++++----------- subsys/pm/device_runtime.c | 3 --- tests/subsys/pm/device_runtime_api/src/main.c | 8 +++---- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/include/pm/device_runtime.h b/include/pm/device_runtime.h index 6e227a4e410..2f467e8b1c5 100644 --- a/include/pm/device_runtime.h +++ b/include/pm/device_runtime.h @@ -65,9 +65,8 @@ int pm_device_runtime_disable(const struct device *dev); * * @param dev Device instance. * - * @retval 0 If the device has been resumed successfully. - * @retval -ENOSTUP If runtime PM is not enabled for the device. - * @retval -ENOSYS If the functionality is not available. + * @retval 0 If it succeeds. In case device runtime PM is not enabled or not + * available this function will be a no-op and will also return 0. * @retval -errno Other negative errno, result of the PM action callback. */ int pm_device_runtime_get(const struct device *dev); @@ -84,9 +83,8 @@ int pm_device_runtime_get(const struct device *dev); * * @param dev Device instance. * - * @retval 0 If device has been suspended successfully. - * @retval -ENOSTUP If runtime PM is not enabled for the device. - * @retval -ENOSYS If the functionality is not available. + * @retval 0 If it succeeds. In case device runtime PM is not enabled or not + * available this function will be a no-op and will also return 0. * @retval -EALREADY If device is already suspended (can only happen if get/put * calls are unbalanced). * @retval -errno Other negative errno, result of the action callback. @@ -110,9 +108,8 @@ int pm_device_runtime_put(const struct device *dev); * * @param dev Device instance. * - * @retval 0 If device has queued for suspend. - * @retval -ENOSTUP If runtime PM is not enabled for the device. - * @retval -ENOSYS If the functionality is not available. + * @retval 0 If it succeeds. In case device runtime PM is not enabled or not + * available this function will be a no-op and will also return 0. * @retval -EALREADY If device is already suspended (can only happen if get/put * calls are unbalanced). * @@ -137,9 +134,9 @@ bool pm_device_runtime_is_enabled(const struct device *dev); #else static inline int pm_device_runtime_enable(const struct device *dev) { return -ENOSYS; } static inline int pm_device_runtime_disable(const struct device *dev) { return -ENOSYS; } -static inline int pm_device_runtime_get(const struct device *dev) { return -ENOSYS; } -static inline int pm_device_runtime_put(const struct device *dev) { return -ENOSYS; } -static inline int pm_device_runtime_put_async(const struct device *dev) { return -ENOSYS; } +static inline int pm_device_runtime_get(const struct device *dev) { return 0; } +static inline int pm_device_runtime_put(const struct device *dev) { return 0; } +static inline int pm_device_runtime_put_async(const struct device *dev) { return 0; } static inline bool pm_device_runtime_is_enabled(const struct device *dev) { return false; } #endif diff --git a/subsys/pm/device_runtime.c b/subsys/pm/device_runtime.c index ec6cf4365e0..8562158c1f2 100644 --- a/subsys/pm/device_runtime.c +++ b/subsys/pm/device_runtime.c @@ -25,7 +25,6 @@ LOG_MODULE_DECLARE(pm_device, CONFIG_PM_DEVICE_LOG_LEVEL); * @param async Perform operation asynchronously. * * @retval 0 If device has been suspended or queued for suspend. - * @retval -ENOSTUP If runtime PM is not enabled for the device. * @retval -EALREADY If device is already suspended (can only happen if get/put * calls are unbalanced). * @retval -errno Other negative errno, result of the action callback. @@ -42,7 +41,6 @@ static int runtime_suspend(const struct device *dev, bool async) } if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) { - ret = -ENOTSUP; goto unlock; } @@ -109,7 +107,6 @@ int pm_device_runtime_get(const struct device *dev) } if ((pm->flags & BIT(PM_DEVICE_FLAG_RUNTIME_ENABLED)) == 0U) { - ret = -ENOTSUP; goto unlock; } diff --git a/tests/subsys/pm/device_runtime_api/src/main.c b/tests/subsys/pm/device_runtime_api/src/main.c index eb128c0f5bb..35d2c53b20a 100644 --- a/tests/subsys/pm/device_runtime_api/src/main.c +++ b/tests/subsys/pm/device_runtime_api/src/main.c @@ -36,13 +36,13 @@ static void test_api_setup(void) { int ret; - /* make sure API is not usable (runtime PM disabled) */ + /* check API always returns 0 when runtime PM is disabled */ ret = pm_device_runtime_get(dev); - zassert_equal(ret, -ENOTSUP, NULL); + zassert_equal(ret, 0, NULL); ret = pm_device_runtime_put(dev); - zassert_equal(ret, -ENOTSUP, NULL); + zassert_equal(ret, 0, NULL); ret = pm_device_runtime_put_async(dev); - zassert_equal(ret, -ENOTSUP, NULL); + zassert_equal(ret, 0, NULL); /* enable runtime PM */ ret = pm_device_runtime_enable(dev);