From 59a79969e820fe9c806e94e0ccad6bb49abea1f9 Mon Sep 17 00:00:00 2001 From: Andrzej Puzdrowski Date: Thu, 18 Mar 2021 09:38:54 +0100 Subject: [PATCH] include/drivers/flash: make write_protection handler optional Made write_protection handler not mandatory. flash_write_protection_set() becomes no-operation. If write_protection handler is provided by the driver implementation it will be called within flash_writ() and flash_erase(). Signed-off-by: Andrzej Puzdrowski --- include/drivers/flash.h | 48 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/include/drivers/flash.h b/include/drivers/flash.h index 19e0a26461f..3b8867441fb 100644 --- a/include/drivers/flash.h +++ b/include/drivers/flash.h @@ -185,8 +185,27 @@ static inline int z_impl_flash_write(const struct device *dev, off_t offset, { const struct flash_driver_api *api = (const struct flash_driver_api *)dev->api; + int rc; - return api->write(dev, offset, data, len); + /* write protection management in this function exists for keeping + * compatibility with out-of-tree drivers which are not aligned jet + * with write-protection API depreciation. + * This will be removed with flash_api_write_protection handler type. + */ + if (api->write_protection != NULL) { + rc = api->write_protection(dev, false); + if (rc) { + return rc; + } + } + + rc = api->write(dev, offset, data, len); + + if (api->write_protection != NULL) { + (void) api->write_protection(dev, true); + } + + return rc; } /** @@ -217,8 +236,27 @@ static inline int z_impl_flash_erase(const struct device *dev, off_t offset, { const struct flash_driver_api *api = (const struct flash_driver_api *)dev->api; + int rc; - return api->erase(dev, offset, size); + /* write protection management in this function exists for keeping + * compatibility with out-of-tree drivers which are not aligned jet + * with write-protection API depreciation. + * This will be removed with flash_api_write_protection handler type. + */ + if (api->write_protection != NULL) { + rc = api->write_protection(dev, false); + if (rc) { + return rc; + } + } + + rc = api->erase(dev, offset, size); + + if (api->write_protection != NULL) { + (void) api->write_protection(dev, true); + } + + return rc; } /** @@ -260,10 +298,10 @@ __syscall int flash_write_protection_set(const struct device *dev, static inline int z_impl_flash_write_protection_set(const struct device *dev, bool enable) { - const struct flash_driver_api *api = - (const struct flash_driver_api *)dev->api; + ARG_UNUSED(dev); + ARG_UNUSED(enable); - return api->write_protection(dev, enable); + return 0; } struct flash_pages_info {