usb_msc.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @file
  3. * @brief USB Mass Storage Class public header
  4. *
  5. * Header follows the Mass Storage Class Specification
  6. * (Mass_Storage_Specification_Overview_v1.4_2-19-2010.pdf) and
  7. * Mass Storage Class Bulk-Only Transport Specification
  8. * (usbmassbulk_10.pdf).
  9. * Header is limited to Bulk-Only Transfer protocol.
  10. */
  11. #ifndef _USB_MSC_H__
  12. #define _USB_MSC_H__
  13. /* MSC Subclass Codes */
  14. #define MSC_SUBCLASS_RBC 0x01 /* Reduced block commands (e.g., flash devices) */
  15. #define MSC_SUBCLASS_SFF8020I_MMC2 0x02 /* SFF-8020i/MMC-2 (ATAPI) (e.g., C/DVD) */
  16. #define MSC_SUBCLASS_QIC157 0x03 /* QIC-157 (e.g., tape device) */
  17. #define MSC_SUBCLASS_UFI 0x04 /* e.g. floppy device */
  18. #define MSC_SUBCLASS_SFF8070I 0x05 /* SFF-8070i (e.g. floppy disk) */
  19. #define MSC_SUBCLASS_SCSI 0x06 /* SCSI transparent */
  20. /* MSC Protocol Codes */
  21. #define MSC_PROTOCOL_CBI_INT 0x00 /* CBI transport with command completion interrupt */
  22. #define MSC_PROTOCOL_CBI_NOINT 0x01 /* CBI transport without command completion interrupt */
  23. #define MSC_PROTOCOL_BULK_ONLY 0x50 /* Bulk only transport */
  24. /* MSC Request Codes */
  25. #define MSC_REQUEST_RESET 0xFF
  26. #define MSC_REQUEST_GET_MAX_LUN 0xFE
  27. /** MSC Command Block Wrapper (CBW) Signature */
  28. #define MSC_CBW_Signature 0x43425355
  29. /** Bulk-only Command Status Wrapper (CSW) Signature */
  30. #define MSC_CSW_Signature 0x53425355
  31. /** MSC Command Block Status Values */
  32. #define CSW_STATUS_CMD_PASSED 0x00
  33. #define CSW_STATUS_CMD_FAILED 0x01
  34. #define CSW_STATUS_PHASE_ERROR 0x02
  35. #define MSC_MAX_CDB_LEN (16) /* Max length of SCSI Command Data Block */
  36. /** MSC Bulk-Only Command Block Wrapper (CBW) */
  37. struct CBW {
  38. uint32_t dSignature; /* 'USBC' = 0x43425355 */
  39. uint32_t dTag; /* Depends on command id */
  40. uint32_t dDataLength; /* Number of bytes that host expects to transfer */
  41. uint8_t bmFlags; /* Bit 7: Direction=IN (other obsolete or reserved) */
  42. uint8_t bLUN; /* LUN (normally 0) */
  43. uint8_t bCBLength; /* len of cdb[] */
  44. uint8_t CB[MSC_MAX_CDB_LEN]; /* Command Data Block */
  45. } __PACKED;
  46. #define USB_SIZEOF_MSC_CBW 31
  47. /** MSC Bulk-Only Command Status Wrapper (CSW) */
  48. struct CSW {
  49. uint32_t dSignature; /* 'USBS' = 0x53425355 */
  50. uint32_t dTag; /* Same tag as original command */
  51. uint32_t dDataResidue; /* Amount not transferred */
  52. uint8_t bStatus; /* Status of transfer */
  53. } __PACKED;
  54. #define USB_SIZEOF_MSC_CSW 13
  55. /*Length of template descriptor: 23 bytes*/
  56. #define MSC_DESCRIPTOR_LEN (9 + 7 + 7)
  57. // clang-format off
  58. #ifndef CONFIG_USB_HS
  59. #define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \
  60. /* Interface */ \
  61. 0x09, /* bLength */ \
  62. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  63. bFirstInterface, /* bInterfaceNumber */ \
  64. 0x00, /* bAlternateSetting */ \
  65. 0x02, /* bNumEndpoints */ \
  66. USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
  67. MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
  68. MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
  69. str_idx, /* iInterface */ \
  70. 0x07, /* bLength */ \
  71. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  72. out_ep, /* bEndpointAddress */ \
  73. 0x02, /* bmAttributes */ \
  74. 0x40, 0x00, /* wMaxPacketSize */ \
  75. 0x00, /* bInterval */ \
  76. 0x07, /* bLength */ \
  77. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  78. in_ep, /* bEndpointAddress */ \
  79. 0x02, /* bmAttributes */ \
  80. 0x40, 0x00, /* wMaxPacketSize */ \
  81. 0x00 /* bInterval */
  82. #else
  83. #define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep,str_idx) \
  84. /* Interface */ \
  85. 0x09, /* bLength */ \
  86. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  87. bFirstInterface, /* bInterfaceNumber */ \
  88. 0x00, /* bAlternateSetting */ \
  89. 0x02, /* bNumEndpoints */ \
  90. USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
  91. MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
  92. MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
  93. str_idx, /* iInterface */ \
  94. 0x07, /* bLength */ \
  95. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  96. out_ep, /* bEndpointAddress */ \
  97. 0x02, /* bmAttributes */ \
  98. 0x00, 0x02, /* wMaxPacketSize */ \
  99. 0x00, /* bInterval */ \
  100. 0x07, /* bLength */ \
  101. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  102. in_ep, /* bEndpointAddress */ \
  103. 0x02, /* bmAttributes */ \
  104. 0x00, 0x02, /* wMaxPacketSize */ \
  105. 0x00 /* bInterval */
  106. #endif
  107. // clang-format on
  108. #endif /* USB_MSC_H_ */