-
Notifications
You must be signed in to change notification settings - Fork 439
[update] zephyr: update usbh_msc_disk.c to support multiple disks #435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,23 +13,50 @@ | |
| #endif | ||
| #endif | ||
|
|
||
| struct usbh_msc *active_msc_class; | ||
| struct usbh_msc_disk_context { | ||
| struct usbh_msc *msc_class; | ||
| struct disk_info disk; | ||
| char name[CONFIG_USBHOST_DEV_NAMELEN]; | ||
| char devname[CONFIG_USBHOST_DEV_NAMELEN]; | ||
| }; | ||
|
|
||
| static struct usbh_msc_disk_context | ||
| msc_disk_context[CONFIG_USBHOST_MAX_MSC_CLASS]; | ||
|
|
||
| static struct usbh_msc_disk_context *disk_msc_context(struct disk_info *disk) | ||
| { | ||
| return CONTAINER_OF(disk, struct usbh_msc_disk_context, disk); | ||
| } | ||
|
|
||
| static bool disk_msc_connected(const struct usbh_msc *msc_class) | ||
| { | ||
| return msc_class != NULL && msc_class->hport != NULL && | ||
| msc_class->hport->connected; | ||
| } | ||
|
|
||
| static int disk_msc_access_init(struct disk_info *disk) | ||
| { | ||
| active_msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda"); | ||
| if (active_msc_class == NULL) { | ||
| printf("do not find /dev/sda\r\n"); | ||
| struct usbh_msc_disk_context *ctx = disk_msc_context(disk); | ||
|
|
||
| ctx->msc_class = (struct usbh_msc *)usbh_find_class_instance(ctx->devname); | ||
| if (ctx->msc_class == NULL) { | ||
| printf("do not find %s\r\n", ctx->devname); | ||
| return -ENODEV; | ||
| } | ||
| if (usbh_msc_scsi_init(active_msc_class) < 0) { | ||
| if (usbh_msc_scsi_init(ctx->msc_class) < 0) { | ||
| return -EIO; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| static int disk_msc_access_status(struct disk_info *disk) | ||
| { | ||
| struct usbh_msc_disk_context *ctx = disk_msc_context(disk); | ||
|
|
||
| if (!disk_msc_connected(ctx->msc_class)) { | ||
| return DISK_STATUS_NOMEDIA; | ||
| } | ||
|
|
||
| return DISK_STATUS_OK; | ||
| } | ||
|
|
||
|
|
@@ -38,25 +65,30 @@ static int disk_msc_access_read(struct disk_info *disk, uint8_t *buff, | |
| { | ||
| int ret; | ||
| uint8_t *align_buf; | ||
| struct usbh_msc_disk_context *ctx = disk_msc_context(disk); | ||
|
|
||
| if (!disk_msc_connected(ctx->msc_class)) { | ||
| return -ENODEV; | ||
| } | ||
|
|
||
| align_buf = (uint8_t *)buff; | ||
| #ifdef CONFIG_DCACHE | ||
| if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) { | ||
| align_buf = (uint8_t *)k_aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize); | ||
| align_buf = (uint8_t *)k_aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * ctx->msc_class->blocksize); | ||
| if (!align_buf) { | ||
| printf("msc get align buf failed\r\n"); | ||
| return -ENOMEM; | ||
| } | ||
| } | ||
| #endif | ||
| if (usbh_msc_scsi_read10(active_msc_class, sector, align_buf, count) < 0) { | ||
| if (usbh_msc_scsi_read10(ctx->msc_class, sector, align_buf, count) < 0) { | ||
| ret = -EIO; | ||
| } else { | ||
| ret = 0; | ||
| } | ||
| #ifdef CONFIG_DCACHE | ||
| if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) { | ||
| usb_memcpy(buff, align_buf, count * active_msc_class->blocksize); | ||
| usb_memcpy(buff, align_buf, count * ctx->msc_class->blocksize); | ||
| k_free(align_buf); | ||
| } | ||
| #endif | ||
|
|
@@ -68,19 +100,24 @@ static int disk_msc_access_write(struct disk_info *disk, const uint8_t *buff, | |
| { | ||
| int ret; | ||
| uint8_t *align_buf; | ||
| struct usbh_msc_disk_context *ctx = disk_msc_context(disk); | ||
|
|
||
| if (!disk_msc_connected(ctx->msc_class)) { | ||
| return -ENODEV; | ||
| } | ||
|
|
||
| align_buf = (uint8_t *)buff; | ||
| #ifdef CONFIG_DCACHE | ||
| if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) { | ||
| align_buf = (uint8_t *)k_aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize); | ||
| align_buf = (uint8_t *)k_aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * ctx->msc_class->blocksize); | ||
| if (!align_buf) { | ||
| printf("msc get align buf failed\r\n"); | ||
| return -ENOMEM; | ||
| } | ||
| usb_memcpy(align_buf, buff, count * active_msc_class->blocksize); | ||
| usb_memcpy(align_buf, buff, count * ctx->msc_class->blocksize); | ||
| } | ||
| #endif | ||
| if (usbh_msc_scsi_write10(active_msc_class, sector, align_buf, count) < 0) { | ||
| if (usbh_msc_scsi_write10(ctx->msc_class, sector, align_buf, count) < 0) { | ||
| ret = -EIO; | ||
| } else { | ||
| ret = 0; | ||
|
|
@@ -95,14 +132,22 @@ static int disk_msc_access_write(struct disk_info *disk, const uint8_t *buff, | |
|
|
||
| static int disk_msc_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff) | ||
| { | ||
| struct usbh_msc_disk_context *ctx = disk_msc_context(disk); | ||
|
|
||
| switch (cmd) { | ||
| case DISK_IOCTL_CTRL_SYNC: | ||
| break; | ||
| case DISK_IOCTL_GET_SECTOR_COUNT: | ||
| *(uint32_t *)buff = active_msc_class->blocknum; | ||
| if (!disk_msc_connected(ctx->msc_class)) { | ||
| return -ENODEV; | ||
| } | ||
| *(uint32_t *)buff = ctx->msc_class->blocknum; | ||
| break; | ||
| case DISK_IOCTL_GET_SECTOR_SIZE: | ||
| *(uint32_t *)buff = active_msc_class->blocksize; | ||
| if (!disk_msc_connected(ctx->msc_class)) { | ||
| return -ENODEV; | ||
| } | ||
| *(uint32_t *)buff = ctx->msc_class->blocksize; | ||
| break; | ||
| case DISK_IOCTL_GET_ERASE_BLOCK_SZ: | ||
| *(uint32_t *)buff = 1U; | ||
|
|
@@ -126,11 +171,6 @@ static const struct disk_operations msc_disk_ops = { | |
| .ioctl = disk_msc_access_ioctl, | ||
| }; | ||
|
|
||
| static struct disk_info usbh_msc_disk = { | ||
| .name = "USB", | ||
| .ops = &msc_disk_ops, | ||
| }; | ||
|
|
||
| __WEAK void usbh_msc_app_run(struct usbh_msc *msc_class) | ||
| { | ||
| (void)msc_class; | ||
|
|
@@ -143,14 +183,44 @@ __WEAK void usbh_msc_app_stop(struct usbh_msc *msc_class) | |
|
|
||
| void usbh_msc_run(struct usbh_msc *msc_class) | ||
| { | ||
| disk_access_register(&usbh_msc_disk); | ||
| uint8_t index; | ||
| struct usbh_msc_disk_context *ctx; | ||
|
|
||
| if (msc_class == NULL || msc_class->sdchar < 'a' || | ||
| msc_class->sdchar >= 'a' + CONFIG_USBHOST_MAX_MSC_CLASS) { | ||
| return; | ||
| } | ||
|
|
||
| index = msc_class->sdchar - 'a'; | ||
| ctx = &msc_disk_context[index]; | ||
| memset(ctx, 0, sizeof(*ctx)); | ||
| snprintf(ctx->name, sizeof(ctx->name), "USB%u", index); | ||
| snprintf(ctx->devname, sizeof(ctx->devname), "/dev/sd%c", msc_class->sdchar); | ||
| ctx->msc_class = msc_class; | ||
| ctx->disk.name = ctx->name; | ||
| ctx->disk.ops = &msc_disk_ops; | ||
|
|
||
| if (disk_access_register(&ctx->disk) < 0) { | ||
| return; | ||
| } | ||
|
|
||
| usbh_msc_app_run(msc_class); | ||
| } | ||
|
|
||
| void usbh_msc_stop(struct usbh_msc *msc_class) | ||
| { | ||
| uint8_t index; | ||
| struct usbh_msc_disk_context *ctx; | ||
|
|
||
| usbh_msc_app_stop(msc_class); | ||
|
|
||
| disk_access_unregister(&usbh_msc_disk); | ||
| if (msc_class == NULL || msc_class->sdchar < 'a' || | ||
| msc_class->sdchar >= 'a' + CONFIG_USBHOST_MAX_MSC_CLASS) { | ||
| return; | ||
| } | ||
|
Comment on lines
+217
to
+220
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win Validate
🤖 Prompt for AI Agents |
||
|
|
||
| index = msc_class->sdchar - 'a'; | ||
| ctx = &msc_disk_context[index]; | ||
| disk_access_unregister(&ctx->disk); | ||
| ctx->msc_class = NULL; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Serialize context teardown with disk operations.
The connection check and later
ctx->msc_classdereferences are not atomic.usbh_msc_stopcan clear the pointer after an I/O operation passes its check and before it calls SCSI. This can cause a null-pointer dereference or use a disconnected MSC instance.platform/zephyr/usbh_msc_disk.c#L68-L91: hold a context lifetime reference or lock from validation through the read transfer.platform/zephyr/usbh_msc_disk.c#L37-L49: synchronize reassignment ofctx->msc_classwith I/O and teardown.platform/zephyr/usbh_msc_disk.c#L103-L120: hold the same protection through the write transfer.platform/zephyr/usbh_msc_disk.c#L135-L150: protect ioctl validation and metadata reads.platform/zephyr/usbh_msc_disk.c#L222-L225: wait for in-flight operations before unregistering and clearing the context.📍 Affects 1 file
platform/zephyr/usbh_msc_disk.c#L68-L91(this comment)platform/zephyr/usbh_msc_disk.c#L37-L49platform/zephyr/usbh_msc_disk.c#L103-L120platform/zephyr/usbh_msc_disk.c#L135-L150platform/zephyr/usbh_msc_disk.c#L222-L225🤖 Prompt for AI Agents