driver/mtd_config : add mtdconfig_register_by_path()

Signed-off-by: xucheng5 <xucheng5@xiaomi.com>
This commit is contained in:
xucheng5 2023-04-06 15:53:02 +08:00 committed by Xiang Xiao
parent 8c19fc547d
commit d0a2fa626a
2 changed files with 89 additions and 25 deletions

View File

@ -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");
}

View File

@ -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
}