From d0a2fa626a324bec85e32150e5203ae11de1aab7 Mon Sep 17 00:00:00 2001 From: xucheng5 Date: Thu, 6 Apr 2023 15:53:02 +0800 Subject: [PATCH] driver/mtd_config : add mtdconfig_register_by_path() Signed-off-by: xucheng5 --- drivers/mtd/mtd_config_fs.c | 76 +++++++++++++++++++++++----------- include/nuttx/mtd/configdata.h | 38 +++++++++++++++++ 2 files changed, 89 insertions(+), 25 deletions(-) diff --git a/drivers/mtd/mtd_config_fs.c b/drivers/mtd/mtd_config_fs.c index 8c425fe89b..ec82e71295 100644 --- a/drivers/mtd/mtd_config_fs.c +++ b/drivers/mtd/mtd_config_fs.c @@ -2000,14 +2000,15 @@ static int mtdconfig_poll(FAR struct file *filep, FAR struct pollfd *fds, ****************************************************************************/ /**************************************************************************** - * Name: mtdconfig_register + * Name: mtdconfig_register_by_path * * Description: - * Register a /dev/config device backed by an fail-safe NVS. + * Register a "path" device backed by an fail-safe NVS. * ****************************************************************************/ -int mtdconfig_register(FAR struct mtd_dev_s *mtd) +int mtdconfig_register_by_path(FAR struct mtd_dev_s *mtd, + FAR const char *path) { int ret; FAR struct nvs_fs *fs; @@ -2035,7 +2036,7 @@ int mtdconfig_register(FAR struct mtd_dev_s *mtd) goto mutex_err; } - ret = register_driver("/dev/config", &g_mtdnvs_fops, 0666, fs); + ret = register_driver(path, &g_mtdnvs_fops, 0666, fs); if (ret < 0) { ferr("ERROR: register mtd config failed: %d\n", ret); @@ -2052,6 +2053,51 @@ errout: return ret; } +/**************************************************************************** + * Name: mtdconfig_register + * + * Description: + * Register a /dev/config device backed by an fail-safe NVS. + * + ****************************************************************************/ + +int mtdconfig_register(FAR struct mtd_dev_s *mtd) +{ + return mtdconfig_register_by_path(mtd, "/dev/config"); +} + +/**************************************************************************** + * Name: mtdconfig_unregister_by_path + * + * Description: + * Unregister a MTD device backed by an fail-safe NVS. + * + ****************************************************************************/ + +int mtdconfig_unregister_by_path(FAR const char *path) +{ + int ret; + struct file file; + FAR struct inode *inode; + FAR struct nvs_fs *fs; + + ret = file_open(&file, path, 0); + if (ret < 0) + { + ferr("ERROR: open file %s err: %d\n", path, ret); + return ret; + } + + inode = file.f_inode; + fs = (FAR struct nvs_fs *)inode->i_private; + nxmutex_destroy(&fs->nvs_lock); + kmm_free(fs); + file_close(&file); + unregister_driver(path); + + return OK; +} + /**************************************************************************** * Name: mtdconfig_unregister * @@ -2062,25 +2108,5 @@ errout: int mtdconfig_unregister(void) { - int ret; - struct file file; - FAR struct inode *inode; - FAR struct nvs_fs *fs; - - ret = file_open(&file, "/dev/config", 0); - if (ret < 0) - { - ferr("ERROR: open /dev/config failed: %d\n", ret); - return ret; - } - - inode = file.f_inode; - fs = (FAR struct nvs_fs *)inode->i_private; - nxmutex_destroy(&fs->nvs_lock); - kmm_free(fs); - file_close(&file); - unregister_driver("/dev/config"); - - return OK; + return mtdconfig_unregister_by_path("/dev/config"); } - diff --git a/include/nuttx/mtd/configdata.h b/include/nuttx/mtd/configdata.h index af76333977..275370238c 100644 --- a/include/nuttx/mtd/configdata.h +++ b/include/nuttx/mtd/configdata.h @@ -135,6 +135,44 @@ int mtdconfig_register(FAR struct mtd_dev_s *mtd); int mtdconfig_unregister(void); +/**************************************************************************** + * Name: mtdconfig_register_by_path + * + * Description: + * This function binds an instance of an MTD device to the path specified + * device. + * + * When this function is called, the MTD device pass in should already + * be initialized appropriately to access the physical device or partition. + * + * Input Parameters: + * mtd - Pointer to the MTD device to bind with the path device + * path - Path name of the file backing the MTD device + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +int mtdconfig_register_by_path(FAR struct mtd_dev_s *mtd, + FAR const char *path); + +/**************************************************************************** + * Name: mtdconfig_unregister_by_path + * + * Description: + * This function unregisters path device. + * + * Input Parameters: + * path - Path name of the file backing the MTD device + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +int mtdconfig_unregister_by_path(FAR const char *path); + #undef EXTERN #ifdef __cplusplus }