fusb_msc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fusb_msc.h
  15. * Date: 2022-02-11 13:33:09
  16. * LastEditTime: 2022-02-17 17:50:46
  17. * Description:  This files is for definition of USB mass storage function
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 Zhugengyu 2022/2/7 init commit
  23. */
  24. #ifndef DRIVERS_USB_MSC_H
  25. #define DRIVERS_USB_MSC_H
  26. #ifdef __cplusplus
  27. extern "C"
  28. {
  29. #endif
  30. /***************************** Include Files *********************************/
  31. #include "fusb.h"
  32. /************************** Constant Definitions *****************************/
  33. /* Possible values for quirks field. */
  34. enum
  35. {
  36. /* Don't check for LUNs (force assumption that there's only one LUN). */
  37. FUSB_MSC_QUIRK_NO_LUNS = 1 << 0,
  38. /* Never do a BULK_ONLY reset, just continue. This means that the device
  39. cannot recover from phase errors and won't detach automatically for
  40. unrecoverable errors. Do not use unless you have to. */
  41. FUSB_MSC_QUIRK_NO_RESET = 1 << 1,
  42. };
  43. /* Possible values for ready field. */
  44. enum
  45. {
  46. FUSB_MSC_DETACHED = -1, /* Disk detached or out to lunch. */
  47. FUSB_MSC_NOT_READY = 0, /* Disk not ready yet -- empty card reader */
  48. FUSB_MSC_READY = 1, /* Disk ready to communicate. */
  49. };
  50. enum
  51. {
  52. FUSB_MSC_SUBCLASS_ATAPI_8020 = 0x2,
  53. FUSB_MSC_SUBCLASS_ATAPI_8070 = 0x5,
  54. FUSB_MSC_SUBCLASS_SCSI = 0x6
  55. };
  56. /* Protocols of MSC */
  57. enum
  58. {
  59. FUSB_MSC_PROTOCOL_BULK_ONLY = 0x50 /* Usb bulk-only transfer protocol */
  60. };
  61. typedef enum
  62. {
  63. FUSB_DIR_DATA_IN = 0x80, /* data from the device to the host */
  64. FUSB_DIR_DATA_OUT = 0 /* data from the host to the device */
  65. } FUsbMassStorageDirection;
  66. /**************************** Type Definitions *******************************/
  67. typedef struct
  68. {
  69. unsigned int blocksize;
  70. unsigned int numblocks;
  71. FUsbEndpoint *bulk_in;
  72. FUsbEndpoint *bulk_out;
  73. u8 quirks : 7;
  74. u8 usbdisk_created : 1;
  75. s8 ready;
  76. u8 lun;
  77. u8 num_luns;
  78. void *data; /* For use by consumers of libpayload. */
  79. } FUsbMassStorage;
  80. /************************** Variable Definitions *****************************/
  81. /***************** Macros (Inline Functions) Definitions *********************/
  82. #define MSC_INST(dev) ((FUsbMassStorage*)(dev)->data)
  83. /**
  84. * @name: FUsbMscGetCapcityMB
  85. * @msg: 获取USB大容量存储设备的容量,单位MB
  86. * @return {fsize_t} 容量,MB
  87. * @param {FUsbDev} *dev, USB大容量存储设备实例
  88. */
  89. static inline u32 FUsbMscGetCapcityMB(FUsbDev *dev)
  90. {
  91. FASSERT(dev);
  92. return (u32)(MSC_INST(dev)->numblocks > 1000000
  93. ? (MSC_INST(dev)->numblocks / 1024) * MSC_INST(dev)->blocksize / 1024
  94. : MSC_INST(dev)->numblocks * MSC_INST(dev)->blocksize / 1024 / 1024);
  95. }
  96. /**
  97. * @name: FUsbMscGetBlkSize
  98. * @msg: 获取USB大容量存储设备的块大小
  99. * @return {*} 块大小,字节数
  100. * @param {FUsbDev} *dev, USB大容量存储设备实例
  101. */
  102. static inline u32 FUsbMscGetBlkSize(FUsbDev *dev)
  103. {
  104. FASSERT(dev);
  105. return (u32)MSC_INST(dev)->blocksize;
  106. }
  107. /**
  108. * @name: FUsbMscGetBlkNum
  109. * @msg: 获取USB大容量存储设备块数目
  110. * @return {*} 块数目
  111. * @param {FUsbDev} *dev, USB大容量存储设备实例
  112. */
  113. static inline u32 FUsbMscGetBlkNum(FUsbDev *dev)
  114. {
  115. FASSERT(dev);
  116. return (u32)MSC_INST(dev)->numblocks;
  117. }
  118. /************************** Function Prototypes ******************************/
  119. /* 读写USB大容量存储设备,以512字节为一块 */
  120. int FUsbMscRwBlk512(FUsbDev *dev, int start, int n, FUsbMassStorageDirection dir, u8 *buf);
  121. /* USB大容量存储设备的初始化函数,由应用程序注册到FUSB框架中 */
  122. void FUsbMassStorageInit(FUsbDev *dev);
  123. /**
  124. * To be implemented by application. It's called by the USB stack
  125. * when a new USB storage device is found, so the client has the chance
  126. * to know about it.
  127. *
  128. * @param dev descriptor for the USB storage device
  129. */
  130. void __attribute__((weak)) FUsbDiskCreate(FUsbDev *dev);
  131. /**
  132. * To be implemented by application. It's called by the USB stack
  133. * when it finds out that a USB storage device is removed.
  134. *
  135. * @param dev descriptor for the USB storage device
  136. */
  137. void __attribute__((weak)) FUsbDiskRemove(FUsbDev *dev);
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif