usb_msc.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #ifndef __USB_MSC_H__
  22. #define __USB_MSC_H__
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #include "bsp_common.h"
  27. #include "usb_scsi.h"
  28. /* MSC Class Config */
  29. #ifndef MSC_MEDIA_PACKET
  30. #define MSC_MEDIA_PACKET 512U
  31. #endif /* MSC_MEDIA_PACKET */
  32. #define MSC_MAX_FS_PACKET 0x40U
  33. #define MSC_MAX_HS_PACKET 0x200U
  34. #define MSC_GET_MAX_LUN 0xFE
  35. #define MSC_RESET 0xFF
  36. #define USB_MSC_CONFIG_DESC_SIZ 32
  37. enum
  38. {
  39. USB_MSC_BOT_STATE_IDLE, /* Idle state */
  40. USB_MSC_BOT_STATE_DATA_OUT_TO_DEVICE, /* Data Out state */
  41. USB_MSC_BOT_STATE_DATA_IN_TO_HOST, /* Data In state */
  42. USB_MSC_BOT_STATE_CSW, /* send csw state */
  43. USB_MSC_BOT_STATE_ERROR,
  44. };
  45. #define USB_MSC_BOT_CBW_SIGNATURE 0x43425355U
  46. #define USB_MSC_BOT_CSW_SIGNATURE 0x53425355U
  47. #define USB_MSC_BOT_CBW_LENGTH 31U
  48. #define USB_MSC_BOT_CSW_LENGTH 13U
  49. #define USB_MSC_BOT_MAX_DATA 256U
  50. /* CSW Status Definitions */
  51. #define USB_MSC_CSW_CMD_PASSED 0x00U
  52. #define USB_MSC_CSW_CMD_FAILED 0x01U
  53. #define USB_MSC_CSW_PHASE_ERROR 0x02U
  54. /* BOT Status */
  55. #define USB_MSC_BOT_STATUS_NORMAL 0U
  56. #define USB_MSC_BOT_STATUS_RECOVERY 1U
  57. #define USB_MSC_BOT_STATUS_ERROR 2U
  58. #define USB_MSC_DIR_IN 0U
  59. #define USB_MSC_DIR_OUT 1U
  60. #define USB_MSC_BOTH_DIR 2U
  61. /**
  62. * @}
  63. */
  64. /** @defgroup MSC_CORE_Private_TypesDefinitions
  65. * @{
  66. */
  67. typedef struct
  68. {
  69. uint32_t dSignature;
  70. uint32_t dTag;
  71. uint32_t dDataLength;
  72. uint8_t bmFlags;
  73. uint8_t bLUN;
  74. uint8_t bCBLength;
  75. uint8_t CB[16];
  76. uint8_t ReservedForAlign;
  77. }MSC_BOT_CBWTypeDef;
  78. typedef struct
  79. {
  80. uint32_t dSignature;
  81. uint32_t dTag;
  82. uint32_t dDataResidue;
  83. uint8_t bStatus;
  84. uint8_t ReservedForAlign[3];
  85. }MSC_BOT_CSWTypeDef;
  86. typedef struct
  87. {
  88. MSC_BOT_CBWTypeDef CBW;
  89. MSC_BOT_CSWTypeDef CSW;
  90. Buffer_Struct BotDataBuffer;
  91. SCSI_SenseTypeDef Sense;
  92. uint64_t TestTime;
  93. HANDLE pSCSIUserFunList;
  94. Timer_t *ReadTimer;
  95. void *pUserData;
  96. uint32_t ReadTimeout;
  97. uint32_t XferDoneLen;
  98. uint32_t LastXferLen;
  99. uint32_t XferTotalLen;//实际上需要发送的总数据量,有可能和CBW要求的不一样
  100. uint8_t BotState;
  101. uint8_t CSWStatus;
  102. uint8_t MediumState;
  103. uint8_t ToHostEpIndex;
  104. uint8_t ToDeviceEpIndex;
  105. uint8_t LogicalUnitNum;
  106. uint8_t USB_ID;
  107. }MSC_SCSICtrlStruct;
  108. typedef struct
  109. {
  110. int32_t (* GetMaxLUN)(uint8_t *LUN, void *pUserData);
  111. int32_t (* Init)(uint8_t LUN, void *pUserData);
  112. int32_t (* GetCapacity)(uint8_t LUN, uint32_t *BlockNum, uint32_t *BlockSize, void *pUserData);
  113. int32_t (* IsReady)(uint8_t LUN, void *pUserData);
  114. int32_t (* IsWriteProtected)(uint8_t LUN, void *pUserData);
  115. int32_t (* PreRead)(uint8_t LUN, uint32_t BlockAddress, uint32_t BlockNums, void *pUserData);
  116. int32_t (* Read)(uint8_t LUN, uint32_t Len, void **pOutData, uint32_t *OutLen, void *pUserData);
  117. int32_t (* ReadNext)(uint8_t LUN, void *pUserData);
  118. int32_t (* PreWrite)(uint8_t LUN, uint32_t BlockAddress, uint32_t BlockNums, void *pUserData);
  119. int32_t (* Write)(uint8_t LUN, uint8_t *Data, uint32_t Len, void *pUserData);
  120. int32_t (* UserCmd)(USB_EndpointDataStruct *pEpData, MSC_SCSICtrlStruct *pMSC);
  121. void *pStandardInquiry;
  122. void *pPage00InquiryData;
  123. void *pPage80InquiryData;
  124. void *pModeSense6Data;
  125. void *pModeSense10Data;
  126. }USB_StorageSCSITypeDef;
  127. void USB_MSCHandle(USB_EndpointDataStruct *pEpData, MSC_SCSICtrlStruct *pMSC);
  128. void USB_MSCReset(MSC_SCSICtrlStruct *pMSC);
  129. void USB_MSCSetToDeviceData(MSC_SCSICtrlStruct *pMSC, uint8_t USB_ID, uint32_t Len);
  130. void USB_MSCSetToHostData(MSC_SCSICtrlStruct *pMSC, uint8_t USB_ID, const void *pData, uint32_t Len);
  131. void USB_SCSISetSenseState(MSC_SCSICtrlStruct *pMSC, uint8_t Skey, uint8_t ASC, uint8_t ASCQ, uint8_t *pData);
  132. /**
  133. * @}
  134. */
  135. /**
  136. * @}
  137. */
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif /* __USBD_MSC_H */
  142. /**
  143. * @}
  144. */
  145. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/