usbh_fatfs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include "sdkconfig.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "esp_check.h"
  14. #include "diskio_impl.h"
  15. #include "ffconf.h"
  16. #include "ff.h"
  17. #include "esp_vfs_fat.h"
  18. #include "usbh_core.h"
  19. #include "usbh_msc.h"
  20. static char *TAG = "MSC";
  21. #define DRIVE_STR_LEN 3
  22. typedef struct msc_host_vfs {
  23. uint8_t pdrv;
  24. FATFS *fs;
  25. char base_path[0];
  26. } msc_host_vfs_t;
  27. static struct usbh_msc *s_mscs[FF_VOLUMES] = { NULL };
  28. static DSTATUS usb_disk_initialize(BYTE pdrv)
  29. {
  30. return RES_OK;
  31. }
  32. static DSTATUS usb_disk_status(BYTE pdrv)
  33. {
  34. return RES_OK;
  35. }
  36. static DRESULT usb_disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
  37. {
  38. struct usbh_msc *msc_class;
  39. assert(pdrv < FF_VOLUMES);
  40. msc_class = s_mscs[pdrv];
  41. assert(msc_class);
  42. if (sector >= msc_class->blocknum - count) {
  43. ESP_LOGW(TAG, "%s: sector 0x%"PRIX32" out of range", __FUNCTION__, (uint32_t)sector);
  44. return RES_PARERR;
  45. }
  46. uint8_t *dma_buff = buff;
  47. size_t len = msc_class->blocksize * count;
  48. if (((uint32_t)dma_buff & (CONFIG_USB_ALIGN_SIZE - 1)) || (len & (CONFIG_USB_ALIGN_SIZE - 1))) {
  49. dma_buff = heap_caps_aligned_alloc(CONFIG_USB_ALIGN_SIZE, len, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  50. if (dma_buff == NULL) {
  51. return RES_ERROR;
  52. }
  53. }
  54. int ret = usbh_msc_scsi_read10(msc_class, sector, dma_buff, count);
  55. if (dma_buff != buff) {
  56. if (ret == 0) {
  57. memcpy(buff, dma_buff, len);
  58. }
  59. heap_caps_free(dma_buff);
  60. }
  61. if (ret != 0) {
  62. ESP_LOGE(TAG, "usbh_msc_scsi_read10 failed (%d)", ret);
  63. return RES_ERROR;
  64. }
  65. return RES_OK;
  66. }
  67. static DRESULT usb_disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
  68. {
  69. struct usbh_msc *msc_class;
  70. assert(pdrv < FF_VOLUMES);
  71. msc_class = s_mscs[pdrv];
  72. assert(msc_class);
  73. if (sector >= msc_class->blocknum - count) {
  74. ESP_LOGW(TAG, "%s: sector 0x%"PRIX32" out of range", __FUNCTION__, (uint32_t)sector);
  75. return RES_PARERR;
  76. }
  77. const uint8_t *dma_buff = buff;
  78. size_t len = msc_class->blocksize * count;
  79. if (((uint32_t)dma_buff & (CONFIG_USB_ALIGN_SIZE - 1)) || (len & (CONFIG_USB_ALIGN_SIZE - 1))) {
  80. dma_buff = heap_caps_aligned_alloc(CONFIG_USB_ALIGN_SIZE, len, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  81. if (dma_buff == NULL) {
  82. return RES_ERROR;
  83. }
  84. memcpy((uint8_t *)dma_buff, buff, len);
  85. }
  86. int ret = usbh_msc_scsi_write10(msc_class, sector, dma_buff, count);
  87. if (dma_buff != buff) {
  88. heap_caps_free((uint8_t *)dma_buff);
  89. }
  90. if (ret != ESP_OK) {
  91. ESP_LOGE(TAG, "usbh_msc_scsi_write10 failed (%d)", ret);
  92. return RES_ERROR;
  93. }
  94. return RES_OK;
  95. }
  96. static DRESULT usb_disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
  97. {
  98. struct usbh_msc *msc_class;
  99. assert(pdrv < FF_VOLUMES);
  100. msc_class = s_mscs[pdrv];
  101. assert(msc_class);
  102. switch (cmd) {
  103. case CTRL_SYNC:
  104. return RES_OK;
  105. case GET_SECTOR_COUNT:
  106. *((DWORD *) buff) = msc_class->blocknum;
  107. return RES_OK;
  108. case GET_SECTOR_SIZE:
  109. *((WORD *) buff) = msc_class->blocksize;
  110. return RES_OK;
  111. case GET_BLOCK_SIZE:
  112. return RES_ERROR;
  113. }
  114. return RES_ERROR;
  115. }
  116. void ff_diskio_register_msc(BYTE pdrv, struct usbh_msc *msc_class)
  117. {
  118. assert(pdrv < FF_VOLUMES);
  119. static const ff_diskio_impl_t usb_disk_impl = {
  120. .init = &usb_disk_initialize,
  121. .status = &usb_disk_status,
  122. .read = &usb_disk_read,
  123. .write = &usb_disk_write,
  124. .ioctl = &usb_disk_ioctl
  125. };
  126. s_mscs[pdrv] = msc_class;
  127. ff_diskio_register(pdrv, &usb_disk_impl);
  128. }
  129. BYTE ff_diskio_get_pdrv_disk(const struct usbh_msc *msc_class)
  130. {
  131. for (int i = 0; i < FF_VOLUMES; i++) {
  132. if (msc_class == s_mscs[i]) {
  133. return i;
  134. }
  135. }
  136. return 0xff;
  137. }
  138. static esp_err_t msc_host_format(struct usbh_msc *msc_class, size_t allocation_size)
  139. {
  140. ESP_RETURN_ON_FALSE((msc_class != NULL && msc_class->user_data != NULL), ESP_ERR_INVALID_ARG, TAG, "");
  141. void *workbuf = NULL;
  142. const size_t workbuf_size = 4096;
  143. msc_host_vfs_t *vfs = (msc_host_vfs_t *)msc_class->user_data;
  144. char drive[DRIVE_STR_LEN] = {(char)('0' + vfs->pdrv), ':', 0};
  145. ESP_RETURN_ON_FALSE((workbuf = ff_memalloc(workbuf_size)), ESP_ERR_NO_MEM, TAG, "");
  146. // Valid value of cluster size is between sector_size and 128 * sector_size.
  147. size_t cluster_size = MIN(MAX(allocation_size, msc_class->blocksize), 128 * msc_class->blocksize);
  148. ESP_LOGW(TAG, "Formatting card, allocation unit size=%d", cluster_size);
  149. f_mount(0, drive, 0);
  150. #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
  151. FRESULT err = f_mkfs(drive, FM_ANY | FM_SFD, cluster_size, workbuf, workbuf_size);
  152. #else
  153. const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, cluster_size};
  154. FRESULT err = f_mkfs(drive, &opt, workbuf, workbuf_size);
  155. #endif
  156. free(workbuf);
  157. if (err != FR_OK || (err = f_mount(vfs->fs, drive, 0)) != FR_OK) {
  158. ESP_LOGE(TAG, "Formatting failed with error: %d", err);
  159. return ESP_FAIL;
  160. }
  161. return ESP_OK;
  162. }
  163. esp_err_t msc_host_vfs_register(struct usbh_msc *msc_class,
  164. const char *base_path,
  165. const esp_vfs_fat_mount_config_t *mount_config)
  166. {
  167. ESP_RETURN_ON_FALSE((msc_class != NULL && msc_class->user_data == NULL && base_path != NULL && mount_config != NULL), ESP_ERR_INVALID_ARG, TAG, "");
  168. FATFS *fs = NULL;
  169. BYTE pdrv;
  170. if (ff_diskio_get_drive(&pdrv) != ESP_OK) {
  171. ESP_LOGW(TAG, "the maximum count of volumes is already mounted");
  172. return ESP_ERR_NO_MEM;
  173. }
  174. esp_err_t ret;
  175. msc_host_vfs_t *vfs = malloc(sizeof(msc_host_vfs_t) + strlen(base_path) + 1);
  176. ESP_RETURN_ON_FALSE(vfs != NULL, ESP_ERR_NO_MEM, TAG, "");
  177. ff_diskio_register_msc(pdrv, msc_class);
  178. char drive[DRIVE_STR_LEN] = {(char)('0' + pdrv), ':', 0};
  179. strcpy(vfs->base_path, base_path);
  180. vfs->pdrv = pdrv;
  181. ret = esp_vfs_fat_register(base_path, drive, mount_config->max_files, &fs);
  182. ESP_GOTO_ON_ERROR(ret, fail, TAG, "Failed to register filesystem, error=%s", esp_err_to_name(ret));
  183. vfs->fs = fs;
  184. msc_class->user_data = vfs;
  185. if (f_mount(fs, drive, 1) != FR_OK) {
  186. if ((!mount_config->format_if_mount_failed) || msc_host_format(msc_class, mount_config->allocation_unit_size) != ESP_OK) {
  187. ret = ESP_FAIL;
  188. goto fail;
  189. }
  190. }
  191. return ESP_OK;
  192. fail:
  193. msc_class->user_data = NULL;
  194. if (fs) {
  195. f_mount(NULL, drive, 0);
  196. }
  197. esp_vfs_fat_unregister_path(base_path);
  198. ff_diskio_unregister(pdrv);
  199. s_mscs[pdrv] = NULL;
  200. return ret;
  201. }
  202. esp_err_t msc_host_vfs_unregister(struct usbh_msc *msc_class)
  203. {
  204. ESP_RETURN_ON_FALSE((msc_class != NULL && ff_diskio_get_pdrv_disk(msc_class) != 0XFF), ESP_ERR_INVALID_ARG, TAG, "");
  205. msc_host_vfs_t *vfs = (msc_host_vfs_t *)msc_class->user_data;
  206. msc_class->user_data = NULL;
  207. char drive[DRIVE_STR_LEN] = {(char)('0' + vfs->pdrv), ':', 0};
  208. f_mount(NULL, drive, 0);
  209. ff_diskio_unregister(vfs->pdrv);
  210. s_mscs[vfs->pdrv] = NULL;
  211. esp_vfs_fat_unregister_path(vfs->base_path);
  212. heap_caps_free(vfs);
  213. return ESP_OK;
  214. }
  215. static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  216. {
  217. struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  218. int ret;
  219. ret = usbh_msc_scsi_init(msc_class);
  220. if (ret < 0) {
  221. ESP_LOGE(TAG, "scsi_init error,ret:%d", ret);
  222. return;
  223. }
  224. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  225. #ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
  226. .format_if_mount_failed = true,
  227. #else
  228. .format_if_mount_failed = false,
  229. #endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
  230. .max_files = 5,
  231. .allocation_unit_size = 4 * 1024
  232. };
  233. ESP_LOGI(TAG, "Mounting msc host filesystem");
  234. if (msc_host_vfs_register(msc_class, "/usb", &mount_config) != ESP_OK) {
  235. ESP_LOGE(TAG, "msc_host_vfs_register fail");
  236. return;
  237. }
  238. ESP_LOGI(TAG, "MSC host filesystem mounted");
  239. usb_osal_thread_delete(NULL);
  240. }
  241. void usbh_msc_run(struct usbh_msc *msc_class)
  242. {
  243. usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, msc_class);
  244. }
  245. void usbh_msc_stop(struct usbh_msc *msc_class)
  246. {
  247. msc_host_vfs_unregister(msc_class);
  248. }