usbh_dfs.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_msc.h"
  8. #include "rtthread.h"
  9. #include <dfs_fs.h>
  10. #define DEV_FORMAT "/sd%c"
  11. #ifndef CONFIG_USB_DFS_MOUNT_POINT
  12. #define CONFIG_USB_DFS_MOUNT_POINT "/"
  13. #endif
  14. #if defined(SOC_SERIES_STM32H7) || defined(SOC_SERIES_STM32F7) || \
  15. defined(SOC_HPM5000) || defined(SOC_HPM6000) || defined(SOC_HPM6E00) || defined(BSP_USING_BL61X)
  16. #ifndef RT_USING_CACHE
  17. #error usbh msc must enable RT_USING_CACHE in this chip
  18. #endif
  19. #if RT_ALIGN_SIZE != 32 && RT_ALIGN_SIZE != 64
  20. #error usbh msc must set cache line to 32 or 64
  21. #endif
  22. #endif
  23. #if defined(BSP_USING_BL61X)
  24. #include "bflb_l1c.h"
  25. void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
  26. {
  27. if (ops == RT_HW_CACHE_FLUSH) {
  28. bflb_l1c_dcache_clean_range(addr, size);
  29. } else {
  30. bflb_l1c_dcache_invalidate_range(addr, size);
  31. }
  32. }
  33. #endif
  34. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t msc_sector[512];
  35. static rt_err_t rt_udisk_init(rt_device_t dev)
  36. {
  37. return RT_EOK;
  38. }
  39. static ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer,
  40. rt_size_t size)
  41. {
  42. struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
  43. int ret;
  44. #ifdef RT_USING_CACHE
  45. rt_uint32_t *align_buf;
  46. if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
  47. align_buf = rt_malloc_align(size * msc_class->blocksize, RT_ALIGN_SIZE);
  48. if (!align_buf) {
  49. rt_kprintf("msc get align buf failed\n");
  50. return 0;
  51. }
  52. } else {
  53. align_buf = (rt_uint32_t *)buffer;
  54. }
  55. ret = usbh_msc_scsi_read10(msc_class, pos, (uint8_t *)align_buf, size);
  56. if (ret < 0) {
  57. rt_kprintf("usb mass_storage read failed\n");
  58. return 0;
  59. }
  60. rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE, align_buf, size * msc_class->blocksize);
  61. if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
  62. rt_memcpy(buffer, align_buf, size * msc_class->blocksize);
  63. rt_free_align(align_buf);
  64. }
  65. #else
  66. ret = usbh_msc_scsi_read10(msc_class, pos, buffer, size);
  67. if (ret < 0) {
  68. rt_kprintf("usb mass_storage read failed\n");
  69. return 0;
  70. }
  71. #endif
  72. return size;
  73. }
  74. static ssize_t rt_udisk_write(rt_device_t dev, rt_off_t pos, const void *buffer,
  75. rt_size_t size)
  76. {
  77. struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
  78. int ret;
  79. #ifdef RT_USING_CACHE
  80. rt_uint32_t *align_buf;
  81. if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
  82. align_buf = rt_malloc_align(size * msc_class->blocksize, RT_ALIGN_SIZE);
  83. if (!align_buf) {
  84. rt_kprintf("msc get align buf failed\n");
  85. return 0;
  86. }
  87. rt_memcpy(align_buf, buffer, size * msc_class->blocksize);
  88. } else {
  89. align_buf = (rt_uint32_t *)buffer;
  90. }
  91. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, align_buf, size * msc_class->blocksize);
  92. ret = usbh_msc_scsi_write10(msc_class, pos, (uint8_t *)align_buf, size);
  93. if (ret < 0) {
  94. rt_kprintf("usb mass_storage write failed\n");
  95. return 0;
  96. }
  97. if ((uint32_t)buffer & (RT_ALIGN_SIZE - 1)) {
  98. rt_free_align(align_buf);
  99. }
  100. #else
  101. ret = usbh_msc_scsi_write10(msc_class, pos, buffer, size);
  102. if (ret < 0) {
  103. rt_kprintf("usb mass_storage write failed\n");
  104. return 0;
  105. }
  106. #endif
  107. return size;
  108. }
  109. static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args)
  110. {
  111. /* check parameter */
  112. RT_ASSERT(dev != RT_NULL);
  113. struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data;
  114. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) {
  115. struct rt_device_blk_geometry *geometry;
  116. geometry = (struct rt_device_blk_geometry *)args;
  117. if (geometry == RT_NULL)
  118. return -RT_ERROR;
  119. geometry->bytes_per_sector = msc_class->blocksize;
  120. geometry->block_size = msc_class->blocksize;
  121. geometry->sector_count = msc_class->blocknum;
  122. }
  123. return RT_EOK;
  124. }
  125. #ifdef RT_USING_DEVICE_OPS
  126. const static struct rt_device_ops udisk_device_ops = {
  127. rt_udisk_init,
  128. RT_NULL,
  129. RT_NULL,
  130. rt_udisk_read,
  131. rt_udisk_write,
  132. rt_udisk_control
  133. };
  134. #endif
  135. int udisk_init(struct usbh_msc *msc_class)
  136. {
  137. rt_err_t ret = 0;
  138. rt_uint8_t i;
  139. struct dfs_partition part0;
  140. struct rt_device *dev;
  141. char name[CONFIG_USBHOST_DEV_NAMELEN];
  142. char mount_point[CONFIG_USBHOST_DEV_NAMELEN];
  143. dev = rt_malloc(sizeof(struct rt_device));
  144. memset(dev, 0, sizeof(struct rt_device));
  145. snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
  146. snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar);
  147. ret = usbh_msc_scsi_read10(msc_class, 0, msc_sector, 1);
  148. if (ret != RT_EOK) {
  149. rt_kprintf("usb mass_storage read failed\n");
  150. return ret;
  151. }
  152. for (i = 0; i < 16; i++) {
  153. /* Get the first partition */
  154. ret = dfs_filesystem_get_partition(&part0, msc_sector, i);
  155. if (ret == RT_EOK) {
  156. rt_kprintf("Found partition %d: type = %d, offet=0x%x, size=0x%x\n",
  157. i, part0.type, part0.offset, part0.size);
  158. } else {
  159. break;
  160. }
  161. }
  162. dev->type = RT_Device_Class_Block;
  163. #ifdef RT_USING_DEVICE_OPS
  164. dev->ops = &udisk_device_ops;
  165. #else
  166. dev->init = rt_udisk_init;
  167. dev->read = rt_udisk_read;
  168. dev->write = rt_udisk_write;
  169. dev->control = rt_udisk_control;
  170. #endif
  171. dev->user_data = msc_class;
  172. rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  173. ret = dfs_mount(name, mount_point, "elm", 0, 0);
  174. if (ret == 0) {
  175. rt_kprintf("udisk: %s mount successfully\n", name);
  176. } else {
  177. rt_kprintf("udisk: %s mount failed, ret = %d\n", name, ret);
  178. }
  179. return ret;
  180. }
  181. void usbh_msc_run(struct usbh_msc *msc_class)
  182. {
  183. udisk_init(msc_class);
  184. }
  185. void usbh_msc_stop(struct usbh_msc *msc_class)
  186. {
  187. char name[CONFIG_USBHOST_DEV_NAMELEN];
  188. char mount_point[CONFIG_USBHOST_DEV_NAMELEN];
  189. snprintf(name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, msc_class->sdchar);
  190. snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar);
  191. dfs_unmount(mount_point);
  192. rt_device_unregister(rt_device_find(name));
  193. }