usbd_msc_blkdev.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbd_core.h"
  7. #include "usbd_msc.h"
  8. #ifndef CONFIG_USBDEV_MSC_THREAD
  9. #error "Please enable CONFIG_USBDEV_MSC_THREAD, move msc read & write from isr to thread"
  10. #endif
  11. #define MSC_IN_EP 0x81
  12. #define MSC_OUT_EP 0x02
  13. #define USBD_VID 0xFFFF
  14. #define USBD_PID 0xFFFF
  15. #define USBD_MAX_POWER 100
  16. #define USBD_LANGID_STRING 1033
  17. #define USB_CONFIG_SIZE (9 + MSC_DESCRIPTOR_LEN)
  18. #ifdef CONFIG_USB_HS
  19. #define MSC_MAX_MPS 512
  20. #else
  21. #define MSC_MAX_MPS 64
  22. #endif
  23. const uint8_t msc_storage_descriptor[] = {
  24. USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0x00, 0x00, 0x00, USBD_VID, USBD_PID, 0x0200, 0x01),
  25. USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x01, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER),
  26. MSC_DESCRIPTOR_INIT(0x00, MSC_OUT_EP, MSC_IN_EP, MSC_MAX_MPS, 0x02),
  27. ///////////////////////////////////////
  28. /// string0 descriptor
  29. ///////////////////////////////////////
  30. USB_LANGID_INIT(USBD_LANGID_STRING),
  31. ///////////////////////////////////////
  32. /// string1 descriptor
  33. ///////////////////////////////////////
  34. 0x14, /* bLength */
  35. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  36. 'C', 0x00, /* wcChar0 */
  37. 'h', 0x00, /* wcChar1 */
  38. 'e', 0x00, /* wcChar2 */
  39. 'r', 0x00, /* wcChar3 */
  40. 'r', 0x00, /* wcChar4 */
  41. 'y', 0x00, /* wcChar5 */
  42. 'U', 0x00, /* wcChar6 */
  43. 'S', 0x00, /* wcChar7 */
  44. 'B', 0x00, /* wcChar8 */
  45. ///////////////////////////////////////
  46. /// string2 descriptor
  47. ///////////////////////////////////////
  48. 0x26, /* bLength */
  49. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  50. 'C', 0x00, /* wcChar0 */
  51. 'h', 0x00, /* wcChar1 */
  52. 'e', 0x00, /* wcChar2 */
  53. 'r', 0x00, /* wcChar3 */
  54. 'r', 0x00, /* wcChar4 */
  55. 'y', 0x00, /* wcChar5 */
  56. 'U', 0x00, /* wcChar6 */
  57. 'S', 0x00, /* wcChar7 */
  58. 'B', 0x00, /* wcChar8 */
  59. ' ', 0x00, /* wcChar9 */
  60. 'M', 0x00, /* wcChar10 */
  61. 'S', 0x00, /* wcChar11 */
  62. 'C', 0x00, /* wcChar12 */
  63. ' ', 0x00, /* wcChar13 */
  64. 'D', 0x00, /* wcChar14 */
  65. 'E', 0x00, /* wcChar15 */
  66. 'M', 0x00, /* wcChar16 */
  67. 'O', 0x00, /* wcChar17 */
  68. ///////////////////////////////////////
  69. /// string3 descriptor
  70. ///////////////////////////////////////
  71. 0x16, /* bLength */
  72. USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */
  73. '2', 0x00, /* wcChar0 */
  74. '0', 0x00, /* wcChar1 */
  75. '2', 0x00, /* wcChar2 */
  76. '2', 0x00, /* wcChar3 */
  77. '1', 0x00, /* wcChar4 */
  78. '2', 0x00, /* wcChar5 */
  79. '3', 0x00, /* wcChar6 */
  80. '4', 0x00, /* wcChar7 */
  81. '5', 0x00, /* wcChar8 */
  82. '6', 0x00, /* wcChar9 */
  83. #ifdef CONFIG_USB_HS
  84. ///////////////////////////////////////
  85. /// device qualifier descriptor
  86. ///////////////////////////////////////
  87. 0x0a,
  88. USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER,
  89. 0x00,
  90. 0x02,
  91. 0x00,
  92. 0x00,
  93. 0x00,
  94. 0x40,
  95. 0x00,
  96. 0x00,
  97. #endif
  98. 0x00
  99. };
  100. struct usbd_interface intf0;
  101. #ifndef CONFIG_USBDEV_MSC_BLOCK_DEV_NAME
  102. #define CONFIG_USBDEV_MSC_BLOCK_DEV_NAME "sd0"
  103. #endif
  104. static rt_device_t blk_dev = RT_NULL;
  105. struct rt_device_blk_geometry geometry = { 0 };
  106. static void usbd_event_handler(uint8_t busid, uint8_t event)
  107. {
  108. switch (event) {
  109. case USBD_EVENT_RESET:
  110. break;
  111. case USBD_EVENT_CONNECTED:
  112. break;
  113. case USBD_EVENT_DISCONNECTED:
  114. break;
  115. case USBD_EVENT_RESUME:
  116. break;
  117. case USBD_EVENT_SUSPEND:
  118. break;
  119. case USBD_EVENT_CONFIGURED:
  120. break;
  121. case USBD_EVENT_SET_REMOTE_WAKEUP:
  122. break;
  123. case USBD_EVENT_CLR_REMOTE_WAKEUP:
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. void usbd_msc_get_cap(uint8_t busid, uint8_t lun, uint32_t *block_num, uint32_t *block_size)
  130. {
  131. rt_device_control(blk_dev, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  132. *block_num = geometry.sector_count;
  133. *block_size = geometry.bytes_per_sector;
  134. }
  135. int usbd_msc_sector_read(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  136. {
  137. rt_device_read(blk_dev, sector, buffer, length / geometry.bytes_per_sector);
  138. return 0;
  139. }
  140. int usbd_msc_sector_write(uint8_t busid, uint8_t lun, uint32_t sector, uint8_t *buffer, uint32_t length)
  141. {
  142. rt_device_write(blk_dev, sector, buffer, length / geometry.bytes_per_sector);
  143. return 0;
  144. }
  145. void msc_storage_init(uint8_t busid, uintptr_t reg_base)
  146. {
  147. rt_err_t res;
  148. blk_dev = rt_device_find(CONFIG_USBDEV_MSC_BLOCK_DEV_NAME);
  149. RT_ASSERT(blk_dev);
  150. res = rt_device_open(blk_dev, RT_DEVICE_OFLAG_RDWR);
  151. RT_ASSERT(res == RT_EOK);
  152. usbd_desc_register(busid, msc_storage_descriptor);
  153. usbd_add_interface(busid, usbd_msc_init_intf(busid, &intf0, MSC_OUT_EP, MSC_IN_EP));
  154. usbd_initialize(busid, reg_base, usbd_event_handler);
  155. }