usbh_msc_disk.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <zephyr/drivers/disk.h>
  7. #include "usbh_core.h"
  8. #include "usbh_msc.h"
  9. #ifdef CONFIG_DCACHE
  10. #ifndef CONFIG_USB_DCACHE_ENABLE
  11. #error CONFIG_USB_DCACHE_ENABLE must be enabled to use msc disk
  12. #endif
  13. #endif
  14. struct usbh_msc *active_msc_class;
  15. static int disk_msc_access_init(struct disk_info *disk)
  16. {
  17. active_msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
  18. if (active_msc_class == NULL) {
  19. printf("do not find /dev/sda\r\n");
  20. return -ENODEV;
  21. }
  22. if (usbh_msc_scsi_init(active_msc_class) < 0) {
  23. return -EIO;
  24. }
  25. return 0;
  26. }
  27. static int disk_msc_access_status(struct disk_info *disk)
  28. {
  29. return DISK_STATUS_OK;
  30. }
  31. static int disk_msc_access_read(struct disk_info *disk, uint8_t *buff,
  32. uint32_t sector, uint32_t count)
  33. {
  34. int ret;
  35. uint8_t *align_buf;
  36. align_buf = (uint8_t *)buff;
  37. #ifdef CONFIG_DCACHE
  38. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  39. align_buf = (uint8_t *)k_aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
  40. if (!align_buf) {
  41. printf("msc get align buf failed\r\n");
  42. return -ENOMEM;
  43. }
  44. }
  45. #endif
  46. if (usbh_msc_scsi_read10(active_msc_class, sector, align_buf, count) < 0) {
  47. ret = -EIO;
  48. } else {
  49. ret = 0;
  50. }
  51. #ifdef CONFIG_DCACHE
  52. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  53. usb_memcpy(buff, align_buf, count * active_msc_class->blocksize);
  54. k_free(align_buf);
  55. }
  56. #endif
  57. return ret;
  58. }
  59. static int disk_msc_access_write(struct disk_info *disk, const uint8_t *buff,
  60. uint32_t sector, uint32_t count)
  61. {
  62. int ret;
  63. uint8_t *align_buf;
  64. align_buf = (uint8_t *)buff;
  65. #ifdef CONFIG_DCACHE
  66. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  67. align_buf = (uint8_t *)k_aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
  68. if (!align_buf) {
  69. printf("msc get align buf failed\r\n");
  70. return -ENOMEM;
  71. }
  72. usb_memcpy(align_buf, buff, count * active_msc_class->blocksize);
  73. }
  74. #endif
  75. if (usbh_msc_scsi_write10(active_msc_class, sector, align_buf, count) < 0) {
  76. ret = -EIO;
  77. } else {
  78. ret = 0;
  79. }
  80. #ifdef CONFIG_DCACHE
  81. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  82. k_free(align_buf);
  83. }
  84. #endif
  85. return ret;
  86. }
  87. static int disk_msc_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
  88. {
  89. switch (cmd) {
  90. case DISK_IOCTL_CTRL_SYNC:
  91. break;
  92. case DISK_IOCTL_GET_SECTOR_COUNT:
  93. *(uint32_t *)buff = active_msc_class->blocknum;
  94. break;
  95. case DISK_IOCTL_GET_SECTOR_SIZE:
  96. *(uint32_t *)buff = active_msc_class->blocksize;
  97. break;
  98. case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
  99. *(uint32_t *)buff = 1U;
  100. break;
  101. case DISK_IOCTL_CTRL_INIT:
  102. return disk_msc_access_init(disk);
  103. case DISK_IOCTL_CTRL_DEINIT:
  104. break;
  105. default:
  106. return -EINVAL;
  107. }
  108. return 0;
  109. }
  110. static const struct disk_operations msc_disk_ops = {
  111. .init = disk_msc_access_init,
  112. .status = disk_msc_access_status,
  113. .read = disk_msc_access_read,
  114. .write = disk_msc_access_write,
  115. .ioctl = disk_msc_access_ioctl,
  116. };
  117. static struct disk_info usbh_msc_disk = {
  118. .name = "USB",
  119. .ops = &msc_disk_ops,
  120. };
  121. void usbh_msc_run(struct usbh_msc *msc_class)
  122. {
  123. disk_access_register(&usbh_msc_disk);
  124. }
  125. void usbh_msc_stop(struct usbh_msc *msc_class)
  126. {
  127. disk_access_unregister(&usbh_msc_disk);
  128. }