From 1c68bd9d3cef3e6a935e520de57cb0acfc2c0a5e Mon Sep 17 00:00:00 2001 From: Cliff Chen Date: Mon, 16 Mar 2020 10:22:37 +0800 Subject: [PATCH] Support msc mode automatic switching Signed-off-by: Cliff Chen --- components/dfs/include/dfs_fs.h | 1 + components/dfs/src/dfs_fs.c | 48 +++++++++++++++++++ .../drivers/usb/usbdevice/class/mstorage.c | 13 ++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/components/dfs/include/dfs_fs.h b/components/dfs/include/dfs_fs.h index 58a9e5cf55..a35a513044 100644 --- a/components/dfs/include/dfs_fs.h +++ b/components/dfs/include/dfs_fs.h @@ -91,6 +91,7 @@ int dfs_unmount(const char *specialfile); int dfs_mkfs(const char *fs_name, const char *device_name); int dfs_statfs(const char *path, struct statfs *buffer); int dfs_mount_device(rt_device_t dev); +int dfs_unmount_device(rt_device_t dev); #ifdef __cplusplus } diff --git a/components/dfs/src/dfs_fs.c b/components/dfs/src/dfs_fs.c index 164beed719..476e47fc06 100644 --- a/components/dfs/src/dfs_fs.c +++ b/components/dfs/src/dfs_fs.c @@ -555,6 +555,54 @@ int dfs_mount_device(rt_device_t dev) rt_kprintf("can't find device:%s to be mounted.\n", dev->parent.name); return -RT_ERROR; } + +int dfs_unmount_device(rt_device_t dev) +{ + struct dfs_filesystem *iter; + struct dfs_filesystem *fs = NULL; + + /* lock filesystem */ + dfs_lock(); + + for (iter = &filesystem_table[0]; + iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++) + { + /* check if the PATH is mounted */ + if ((iter->dev_id->parent.name != NULL) + && (strcmp(iter->dev_id->parent.name, dev->parent.name) == 0)) + { + fs = iter; + break; + } + } + + if (fs == NULL || + fs->ops->unmount == NULL || + fs->ops->unmount(fs) < 0) + { + goto err1; + } + + /* close device, but do not check the status of device */ + if (fs->dev_id != NULL) + rt_device_close(fs->dev_id); + + if (fs->path != NULL) + rt_free(fs->path); + + /* clear this filesystem table entry */ + memset(fs, 0, sizeof(struct dfs_filesystem)); + + dfs_unlock(); + + return 0; + +err1: + dfs_unlock(); + + return -1; +} + #endif #ifdef RT_USING_FINSH diff --git a/components/drivers/usb/usbdevice/class/mstorage.c b/components/drivers/usb/usbdevice/class/mstorage.c index 6a6e92415b..43fb25afe4 100644 --- a/components/drivers/usb/usbdevice/class/mstorage.c +++ b/components/drivers/usb/usbdevice/class/mstorage.c @@ -16,6 +16,10 @@ #include "drivers/usb_device.h" #include "mstorage.h" +#ifdef RT_USING_DFS_MNTTABLE +#include "dfs_fs.h" +#endif + #ifdef RT_USB_DEVICE_MSTORAGE enum STAT @@ -955,7 +959,7 @@ static rt_err_t _function_enable(ufunction_t func) struct mstorage *data; RT_ASSERT(func != RT_NULL); RT_DEBUG_LOG(RT_DEBUG_USB, ("Mass storage function enabled\n")); - data = (struct mstorage*)func->user_data; + data = (struct mstorage*)func->user_data; data->disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME); if(data->disk == RT_NULL) @@ -964,6 +968,10 @@ static rt_err_t _function_enable(ufunction_t func) return -RT_ERROR; } +#ifdef RT_USING_DFS_MNTTABLE + dfs_unmount_device(data->disk); +#endif + if(rt_device_open(data->disk, RT_DEVICE_OFLAG_RDWR) != RT_EOK) { rt_kprintf("disk open error\n"); @@ -1029,6 +1037,9 @@ static rt_err_t _function_disable(ufunction_t func) if(data->disk != RT_NULL) { rt_device_close(data->disk); +#ifdef RT_USING_DFS_MNTTABLE + dfs_mount_device(data->disk); +#endif data->disk = RT_NULL; }