summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2025-01-26 00:15:33 +0800
committersakumisu <[email protected]>2025-01-26 12:50:48 +0800
commitc827c2e50b90521fad36113d0f0c8827be6a96f1 (patch)
tree66a6f6e4965f8fe71a5dae17081b6028e538e3b0
parent49d9775a1bf2848298f966cae8432e8f931832d1 (diff)
update(class/msc/usbh_msc): move msc scsi commands out to prevent blocking enum thread
Signed-off-by: sakumisu <[email protected]>
-rw-r--r--class/msc/usbh_msc.c73
-rw-r--r--class/msc/usbh_msc.h1
-rw-r--r--demo/usb_host.c12
-rw-r--r--platform/none/usbh_fatfs.c5
-rw-r--r--platform/nuttx/usbh_fs.c6
-rw-r--r--platform/rtthread/usbh_dfs.c27
6 files changed, 63 insertions, 61 deletions
diff --git a/class/msc/usbh_msc.c b/class/msc/usbh_msc.c
index 664177ec..88c90b0b 100644
--- a/class/msc/usbh_msc.c
+++ b/class/msc/usbh_msc.c
@@ -267,7 +267,6 @@ static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
struct usb_endpoint_descriptor *ep_desc;
struct usbh_msc_modeswitch_config *config;
int ret;
- int cnt;
struct usbh_msc *msc_class = usbh_msc_class_alloc();
if (msc_class == NULL) {
@@ -319,38 +318,6 @@ static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
}
}
- cnt = 0;
- while (usbh_msc_scsi_testunitready(msc_class) < 0) {
- USB_LOG_WRN("Device not ready, try again...\r\n");
- ret = usbh_msc_scsi_requestsense(msc_class);
- if (ret < 0) {
- USB_LOG_ERR("Fail to scsi_testunitready\r\n");
- }
- cnt++;
- if (cnt > CONFIG_USBHOST_MSC_READY_CHECK_TIMES) {
- return -USB_ERR_BUSY;
- }
- }
-
- ret = usbh_msc_scsi_inquiry(msc_class);
- if (ret < 0) {
- USB_LOG_ERR("Fail to scsi_inquiry\r\n");
- return ret;
- }
- ret = usbh_msc_scsi_readcapacity10(msc_class);
- if (ret < 0) {
- USB_LOG_ERR("Fail to scsi_readcapacity10\r\n");
- return ret;
- }
-
- if (msc_class->blocksize > 0) {
- USB_LOG_INFO("Capacity info:\r\n");
- USB_LOG_INFO("Block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
- } else {
- USB_LOG_ERR("Invalid block size\r\n");
- return -USB_ERR_RANGE;
- }
-
snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
USB_LOG_INFO("Register MSC Class:%s\r\n", hport->config.intf[intf].devname);
@@ -385,6 +352,46 @@ static int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf)
return ret;
}
+int usbh_msc_scsi_init(struct usbh_msc *msc_class)
+{
+ int ret;
+ uint16_t cnt;
+
+ cnt = 0;
+ while (usbh_msc_scsi_testunitready(msc_class) < 0) {
+ USB_LOG_WRN("Device not ready, try again...\r\n");
+ ret = usbh_msc_scsi_requestsense(msc_class);
+ if (ret < 0) {
+ USB_LOG_ERR("Fail to scsi_testunitready\r\n");
+ }
+ cnt++;
+ if (cnt > CONFIG_USBHOST_MSC_READY_CHECK_TIMES) {
+ return -USB_ERR_NODEV;
+ }
+ }
+ ret = usbh_msc_scsi_inquiry(msc_class);
+ if (ret < 0) {
+ USB_LOG_ERR("Fail to scsi_inquiry\r\n");
+ return ret;
+ }
+
+ ret = usbh_msc_scsi_readcapacity10(msc_class);
+ if (ret < 0) {
+ USB_LOG_ERR("Fail to scsi_readcapacity10\r\n");
+ return ret;
+ }
+
+ if (msc_class->blocksize > 0) {
+ USB_LOG_INFO("Capacity info:\r\n");
+ USB_LOG_INFO("Block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
+ } else {
+ USB_LOG_ERR("Invalid block size\r\n");
+ return -USB_ERR_RANGE;
+ }
+
+ return 0;
+}
+
int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors)
{
struct CBW *cbw;
diff --git a/class/msc/usbh_msc.h b/class/msc/usbh_msc.h
index 50a61a9c..c4aee8a8 100644
--- a/class/msc/usbh_msc.h
+++ b/class/msc/usbh_msc.h
@@ -32,6 +32,7 @@ struct usbh_msc_modeswitch_config {
};
void usbh_msc_modeswitch_enable(struct usbh_msc_modeswitch_config *config);
+int usbh_msc_scsi_init(struct usbh_msc *msc_class);
int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
diff --git a/demo/usb_host.c b/demo/usb_host.c
index 400d9889..c891d67a 100644
--- a/demo/usb_host.c
+++ b/demo/usb_host.c
@@ -228,7 +228,12 @@ static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
/* test with only one buffer, if you have more msc class, modify by yourself */
-#if 1
+#if TEST_USBH_MSC_FATFS == 0
+ ret = usbh_msc_scsi_init(msc_class);
+ if (ret < 0) {
+ USB_LOG_RAW("scsi_init error,ret:%d\r\n", ret);
+ goto delete;
+ }
/* get the partition table */
ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
if (ret < 0) {
@@ -242,11 +247,10 @@ static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
USB_LOG_RAW("%02x ", partition_table[i]);
}
USB_LOG_RAW("\r\n");
-#endif
-
-#if TEST_USBH_MSC_FATFS
+#else
usb_msc_fatfs_test();
#endif
+
// clang-format off
delete:
usb_osal_thread_delete(NULL);
diff --git a/platform/none/usbh_fatfs.c b/platform/none/usbh_fatfs.c
index ba76c12c..5e164a7b 100644
--- a/platform/none/usbh_fatfs.c
+++ b/platform/none/usbh_fatfs.c
@@ -12,7 +12,7 @@ struct usbh_msc *active_msc_class;
int USB_disk_status(void)
{
- return 0;
+ return RES_OK;
}
int USB_disk_initialize(void)
@@ -22,6 +22,9 @@ int USB_disk_initialize(void)
printf("do not find /dev/sda\r\n");
return RES_NOTRDY;
}
+ if (usbh_msc_scsi_init(active_msc_class) < 0) {
+ return RES_NOTRDY;
+ }
return RES_OK;
}
diff --git a/platform/nuttx/usbh_fs.c b/platform/nuttx/usbh_fs.c
index 9f0e2b19..bd5afc67 100644
--- a/platform/nuttx/usbh_fs.c
+++ b/platform/nuttx/usbh_fs.c
@@ -101,11 +101,11 @@ static int usbhost_open(FAR struct inode *inode)
DEBUGASSERT(inode->i_private);
msc_class = (struct usbh_msc *)inode->i_private;
- if (msc_class->hport && msc_class->hport->connected) {
- return OK;
- } else {
+ if (usbh_msc_scsi_init(msc_class) < 0) {
return -ENODEV;
}
+
+ return OK;
}
static int usbhost_close(FAR struct inode *inode)
diff --git a/platform/rtthread/usbh_dfs.c b/platform/rtthread/usbh_dfs.c
index 073d52ea..9fba3915 100644
--- a/platform/rtthread/usbh_dfs.c
+++ b/platform/rtthread/usbh_dfs.c
@@ -42,6 +42,13 @@ USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t msc_sector[512];
static rt_err_t rt_udisk_init(rt_device_t dev)
{
+ struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
+
+ if (usbh_msc_scsi_init(msc_class) < 0) {
+ rt_kprintf("scsi_init error,ret:%d\r\n", ret);
+ return -RT_ERROR;
+ }
+
return RT_EOK;
}
@@ -161,7 +168,6 @@ int udisk_init(struct usbh_msc *msc_class)
{
rt_err_t ret = 0;
rt_uint8_t i;
- struct dfs_partition part0;
struct rt_device *dev;
char name[CONFIG_USBHOST_DEV_NAMELEN];
char mount_point[CONFIG_USBHOST_DEV_NAMELEN];
@@ -172,25 +178,6 @@ int udisk_init(struct usbh_msc *msc_class)
snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar);
- ret = usbh_msc_scsi_read10(msc_class, 0, msc_sector, 1);
- if (ret != RT_EOK) {
- rt_kprintf("usb mass_storage read failed\n");
- rt_free(dev);
- return ret;
- }
-
- for (i = 0; i < 4; i++) {
- /* Get the first partition */
- ret = dfs_filesystem_get_partition(&part0, msc_sector, i);
- if (ret == RT_EOK) {
- rt_kprintf("Found partition %d: type = %d, offet=0x%x, size=0x%x\n",
- i, part0.type, part0.offset, part0.size);
- break;
- } else {
- break;
- }
- }
-
dev->type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
dev->ops = &udisk_device_ops;