Skip to content

Commit c827c2e

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 c827c2e

File tree

6 files changed

+63
-61
lines changed

6 files changed

+63
-61
lines changed

class/msc/usbh_msc.c

Lines changed: 40 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,46 @@ 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+
cnt = 0;
361+
while (usbh_msc_scsi_testunitready(msc_class) < 0) {
362+
USB_LOG_WRN("Device not ready, try again...\r\n");
363+
ret = usbh_msc_scsi_requestsense(msc_class);
364+
if (ret < 0) {
365+
USB_LOG_ERR("Fail to scsi_testunitready\r\n");
366+
}
367+
cnt++;
368+
if (cnt > CONFIG_USBHOST_MSC_READY_CHECK_TIMES) {
369+
return -USB_ERR_NODEV;
370+
}
371+
}
372+
ret = usbh_msc_scsi_inquiry(msc_class);
373+
if (ret < 0) {
374+
USB_LOG_ERR("Fail to scsi_inquiry\r\n");
375+
return ret;
376+
}
377+
378+
ret = usbh_msc_scsi_readcapacity10(msc_class);
379+
if (ret < 0) {
380+
USB_LOG_ERR("Fail to scsi_readcapacity10\r\n");
381+
return ret;
382+
}
383+
384+
if (msc_class->blocksize > 0) {
385+
USB_LOG_INFO("Capacity info:\r\n");
386+
USB_LOG_INFO("Block num:%d,block size:%d\r\n", (unsigned int)msc_class->blocknum, (unsigned int)msc_class->blocksize);
387+
} else {
388+
USB_LOG_ERR("Invalid block size\r\n");
389+
return -USB_ERR_RANGE;
390+
}
391+
392+
return 0;
393+
}
394+
388395
int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors)
389396
{
390397
struct CBW *cbw;

class/msc/usbh_msc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct usbh_msc_modeswitch_config {
3232
};
3333

3434
void usbh_msc_modeswitch_enable(struct usbh_msc_modeswitch_config *config);
35+
int usbh_msc_scsi_init(struct usbh_msc *msc_class);
3536
int usbh_msc_scsi_write10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
3637
int usbh_msc_scsi_read10(struct usbh_msc *msc_class, uint32_t start_sector, const uint8_t *buffer, uint32_t nsectors);
3738

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: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t msc_sector[512];
4242

4343
static rt_err_t rt_udisk_init(rt_device_t dev)
4444
{
45+
struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
46+
47+
if (usbh_msc_scsi_init(msc_class) < 0) {
48+
rt_kprintf("scsi_init error,ret:%d\r\n", ret);
49+
return -RT_ERROR;
50+
}
51+
4552
return RT_EOK;
4653
}
4754

@@ -161,7 +168,6 @@ int udisk_init(struct usbh_msc *msc_class)
161168
{
162169
rt_err_t ret = 0;
163170
rt_uint8_t i;
164-
struct dfs_partition part0;
165171
struct rt_device *dev;
166172
char name[CONFIG_USBHOST_DEV_NAMELEN];
167173
char mount_point[CONFIG_USBHOST_DEV_NAMELEN];
@@ -172,25 +178,6 @@ int udisk_init(struct usbh_msc *msc_class)
172178
snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
173179
snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar);
174180

175-
ret = usbh_msc_scsi_read10(msc_class, 0, msc_sector, 1);
176-
if (ret != RT_EOK) {
177-
rt_kprintf("usb mass_storage read failed\n");
178-
rt_free(dev);
179-
return ret;
180-
}
181-
182-
for (i = 0; i < 4; i++) {
183-
/* Get the first partition */
184-
ret = dfs_filesystem_get_partition(&part0, msc_sector, i);
185-
if (ret == RT_EOK) {
186-
rt_kprintf("Found partition %d: type = %d, offet=0x%x, size=0x%x\n",
187-
i, part0.type, part0.offset, part0.size);
188-
break;
189-
} else {
190-
break;
191-
}
192-
}
193-
194181
dev->type = RT_Device_Class_Block;
195182
#ifdef RT_USING_DEVICE_OPS
196183
dev->ops = &udisk_device_ops;

0 commit comments

Comments
 (0)