usbh_fatfs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "ff.h"
  7. #include "diskio.h"
  8. #include "usbh_core.h"
  9. #include "usbh_msc.h"
  10. struct usbh_msc *active_msc_class;
  11. int USB_disk_initialize(void)
  12. {
  13. active_msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
  14. if (active_msc_class == NULL) {
  15. printf("do not find /dev/sda\r\n");
  16. return RES_NOTRDY;
  17. }
  18. if (usbh_msc_scsi_init(active_msc_class) < 0) {
  19. return RES_NOTRDY;
  20. }
  21. return RES_OK;
  22. }
  23. int USB_disk_status(void)
  24. {
  25. return RES_OK;
  26. }
  27. int USB_disk_read(BYTE *buff, LBA_t sector, UINT count)
  28. {
  29. int ret;
  30. uint8_t *align_buf;
  31. align_buf = (uint8_t *)buff;
  32. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  33. align_buf = (uint8_t *)aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
  34. if (!align_buf) {
  35. printf("msc get align buf failed\r\n");
  36. return -USB_ERR_NOMEM;
  37. }
  38. }
  39. ret = usbh_msc_scsi_read10(active_msc_class, sector, align_buf, count);
  40. if (ret < 0) {
  41. ret = RES_ERROR;
  42. } else {
  43. ret = RES_OK;
  44. }
  45. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  46. usb_memcpy(buff, align_buf, count * active_msc_class->blocksize);
  47. free(align_buf);
  48. }
  49. return ret;
  50. }
  51. int USB_disk_write(const BYTE *buff, LBA_t sector, UINT count)
  52. {
  53. int ret;
  54. uint8_t *align_buf;
  55. align_buf = (uint8_t *)buff;
  56. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  57. align_buf = (uint8_t *)aligned_alloc(CONFIG_USB_ALIGN_SIZE, count * active_msc_class->blocksize);
  58. if (!align_buf) {
  59. printf("msc get align buf failed\r\n");
  60. return -USB_ERR_NOMEM;
  61. }
  62. usb_memcpy(align_buf, buff, count * active_msc_class->blocksize);
  63. }
  64. ret = usbh_msc_scsi_write10(active_msc_class, sector, align_buf, count);
  65. if (ret < 0) {
  66. ret = RES_ERROR;
  67. } else {
  68. ret = RES_OK;
  69. }
  70. if ((uint32_t)buff & (CONFIG_USB_ALIGN_SIZE - 1)) {
  71. free(align_buf);
  72. }
  73. return ret;
  74. }
  75. int USB_disk_ioctl(BYTE cmd, void *buff)
  76. {
  77. int result = 0;
  78. switch (cmd) {
  79. case CTRL_SYNC:
  80. result = RES_OK;
  81. break;
  82. case GET_SECTOR_SIZE:
  83. *(WORD *)buff = active_msc_class->blocksize;
  84. result = RES_OK;
  85. break;
  86. case GET_BLOCK_SIZE:
  87. *(DWORD *)buff = 1;
  88. result = RES_OK;
  89. break;
  90. case GET_SECTOR_COUNT:
  91. *(DWORD *)buff = active_msc_class->blocknum;
  92. result = RES_OK;
  93. break;
  94. default:
  95. result = RES_PARERR;
  96. break;
  97. }
  98. return result;
  99. }