Skip to content

Commit b6ec680

Browse files
committed
update(class/msc/usbh_msc): move msc scsi commands out to prevent blocking enum thread
Signed-off-by: sakumisu <1203593632@qq.com>
1 parent 49d9775 commit b6ec680

File tree

6 files changed

+67
-41
lines changed

6 files changed

+67
-41
lines changed

class/msc/usbh_msc.c

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
267267
struct usb_endpoint_descriptor *ep_desc;
268268
struct usbh_msc_modeswitch_config *config;
269269
int ret;
270-
int cnt;
271270

272271
struct usbh_msc *msc_class = usbh_msc_class_alloc();
273272
if (msc_class == NULL) {
@@ -319,38 +318,6 @@ static int usbh_msc_connect(struct usbh_hubport *hport, uint8_t intf)
319318
}
320319
}
321320

322-
cnt = 0;
323-
while (usbh_msc_scsi_testunitready(msc_class) < 0) {
324-
USB_LOG_WRN("Device not ready, try again...\r\n");
325-
ret = usbh_msc_scsi_requestsense(msc_class);
326-
if (ret < 0) {
327-
USB_LOG_ERR("Fail to scsi_testunitready\r\n");
328-
}
329-
cnt++;
330-
if (cnt > CONFIG_USBHOST_MSC_READY_CHECK_TIMES) {
331-
return -USB_ERR_BUSY;
332-
}
333-
}
334-
335-
ret = usbh_msc_scsi_inquiry(msc_class);
336-
if (ret < 0) {
337-
USB_LOG_ERR("Fail to scsi_inquiry\r\n");
338-
return ret;
339-
}
340-
ret = usbh_msc_scsi_readcapacity10(msc_class);
341-
if (ret < 0) {
342-
USB_LOG_ERR("Fail to scsi_readcapacity10\r\n");
343-
return ret;
344-
}
345-
346-
if (msc_class->blocksize > 0) {
347-
USB_LOG_INFO("Capacity info:\r\n");
348-
USB_LOG_INFO("Block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
349-
} else {
350-
USB_LOG_ERR("Invalid block size\r\n");
351-
return -USB_ERR_RANGE;
352-
}
353-
354321
snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
355322

356323
USB_LOG_INFO("Register MSC Class:%s\r\n", hport->config.intf[intf].devname);
@@ -385,6 +352,50 @@ static int usbh_msc_disconnect(struct usbh_hubport *hport, uint8_t intf)
385352
return ret;
386353
}
387354

355+
int usbh_msc_scsi_init(struct usbh_msc *msc_class)
356+
{
357+
int ret;
358+
uint16_t cnt;
359+
360+
if (msc_class->is_ready) {
361+
return 0;
362+
}
363+
364+
cnt = 0;
365+
while (usbh_msc_scsi_testunitready(msc_class) < 0) {
366+
USB_LOG_WRN("Device not ready, try again...\r\n");
367+
ret = usbh_msc_scsi_requestsense(msc_class);
368+
if (ret < 0) {
369+
USB_LOG_ERR("Fail to scsi_testunitready\r\n");
370+
}
371+
cnt++;
372+
if (cnt > CONFIG_USBHOST_MSC_READY_CHECK_TIMES) {
373+
return -USB_ERR_NODEV;
374+
}
375+
}
376+
ret = usbh_msc_scsi_inquiry(msc_class);
377+
if (ret < 0) {
378+
USB_LOG_ERR("Fail to scsi_inquiry\r\n");
379+
return ret;
380+
}
381+
382+
ret = usbh_msc_scsi_readcapacity10(msc_class);
383+
if (ret < 0) {
384+
USB_LOG_ERR("Fail to scsi_readcapacity10\r\n");
385+
return ret;
386+
}
387+
388+
if (msc_class->blocksize > 0) {
389+
USB_LOG_INFO("Capacity info:\r\n");
390+
USB_LOG_INFO("Block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
391+
} else {
392+
USB_LOG_ERR("Invalid block size\r\n");
393+
return -USB_ERR_RANGE;
394+
}
395+
msc_class->is_ready = true;
396+
return 0;
397+
}
398+
388399
int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors)
389400
{
390401
struct CBW *cbw;

class/msc/usbh_msc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ struct usbh_msc {
2020
uint8_t sdchar;
2121
uint32_t blocknum; /* Number of blocks on the USB mass storage device */
2222
uint16_t blocksize; /* Block size of USB mass storage device */
23+
bool is_ready;
2324

2425
void *user_data;
2526
};
@@ -32,6 +33,7 @@ struct usbh_msc_modeswitch_config {
3233
};
3334

3435
void usbh_msc_modeswitch_enable(struct usbh_msc_modeswitch_config *config);
36+
int usbh_msc_scsi_init(struct usbh_msc *msc_class);
3537
int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
3638
int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
3739

demo/usb_host.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,12 @@ static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
228228
struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
229229

230230
/* test with only one buffer, if you have more msc class, modify by yourself */
231-
#if 1
231+
#if TEST_USBH_MSC_FATFS == 0
232+
ret = usbh_msc_scsi_init(msc_class);
233+
if (ret < 0) {
234+
USB_LOG_RAW("scsi_init error,ret:%d\r\n", ret);
235+
goto delete;
236+
}
232237
/* get the partition table */
233238
ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
234239
if (ret < 0) {
@@ -242,11 +247,10 @@ static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
242247
USB_LOG_RAW("%02x ", partition_table[i]);
243248
}
244249
USB_LOG_RAW("\r\n");
245-
#endif
246-
247-
#if TEST_USBH_MSC_FATFS
250+
#else
248251
usb_msc_fatfs_test();
249252
#endif
253+
250254
// clang-format off
251255
delete:
252256
usb_osal_thread_delete(NULL);

platform/none/usbh_fatfs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct usbh_msc *active_msc_class;
1212

1313
int USB_disk_status(void)
1414
{
15-
return 0;
15+
return RES_OK;
1616
}
1717

1818
int USB_disk_initialize(void)
@@ -22,6 +22,9 @@ int USB_disk_initialize(void)
2222
printf("do not find /dev/sda\r\n");
2323
return RES_NOTRDY;
2424
}
25+
if (usbh_msc_scsi_init(active_msc_class) < 0) {
26+
return RES_NOTRDY;
27+
}
2528
return RES_OK;
2629
}
2730

platform/nuttx/usbh_fs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ static int usbhost_open(FAR struct inode *inode)
101101
DEBUGASSERT(inode->i_private);
102102
msc_class = (struct usbh_msc *)inode->i_private;
103103

104-
if (msc_class->hport && msc_class->hport->connected) {
105-
return OK;
106-
} else {
104+
if (usbh_msc_scsi_init(msc_class) < 0) {
107105
return -ENODEV;
108106
}
107+
108+
return OK;
109109
}
110110

111111
static int usbhost_close(FAR struct inode *inode)

platform/rtthread/usbh_dfs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ int udisk_init(struct usbh_msc *msc_class)
172172
snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
173173
snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar);
174174

175+
ret = usbh_msc_scsi_init(msc_class);
176+
if (ret < 0) {
177+
rt_kprintf("scsi_init error,ret:%d\r\n", ret);
178+
rt_free(dev);
179+
return ret;
180+
}
175181
ret = usbh_msc_scsi_read10(msc_class, 0, msc_sector, 1);
176182
if (ret != RT_EOK) {
177183
rt_kprintf("usb mass_storage read failed\n");

0 commit comments

Comments
 (0)