usb_msc.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_MSC_H
  7. #define USB_MSC_H
  8. /* MSC Subclass Codes */
  9. #define MSC_SUBCLASS_RBC 0x01 /* Reduced block commands (e.g., flash devices) */
  10. #define MSC_SUBCLASS_SFF8020I_MMC2 0x02 /* SFF-8020i/MMC-2 (ATAPI) (e.g., C/DVD) */
  11. #define MSC_SUBCLASS_QIC157 0x03 /* QIC-157 (e.g., tape device) */
  12. #define MSC_SUBCLASS_UFI 0x04 /* e.g. floppy device */
  13. #define MSC_SUBCLASS_SFF8070I 0x05 /* SFF-8070i (e.g. floppy disk) */
  14. #define MSC_SUBCLASS_SCSI 0x06 /* SCSI transparent */
  15. /* MSC Protocol Codes */
  16. #define MSC_PROTOCOL_CBI_INT 0x00 /* CBI transport with command completion interrupt */
  17. #define MSC_PROTOCOL_CBI_NOINT 0x01 /* CBI transport without command completion interrupt */
  18. #define MSC_PROTOCOL_BULK_ONLY 0x50 /* Bulk only transport */
  19. /* MSC Request Codes */
  20. #define MSC_REQUEST_RESET 0xFF
  21. #define MSC_REQUEST_GET_MAX_LUN 0xFE
  22. /** MSC Command Block Wrapper (CBW) Signature */
  23. #define MSC_CBW_Signature 0x43425355
  24. /** Bulk-only Command Status Wrapper (CSW) Signature */
  25. #define MSC_CSW_Signature 0x53425355
  26. /** MSC Command Block Status Values */
  27. #define CSW_STATUS_CMD_PASSED 0x00
  28. #define CSW_STATUS_CMD_FAILED 0x01
  29. #define CSW_STATUS_PHASE_ERROR 0x02
  30. #define MSC_MAX_CDB_LEN (16) /* Max length of SCSI Command Data Block */
  31. /** MSC Bulk-Only Command Block Wrapper (CBW) */
  32. struct CBW {
  33. uint32_t dSignature; /* 'USBC' = 0x43425355 */
  34. uint32_t dTag; /* Depends on command id */
  35. uint32_t dDataLength; /* Number of bytes that host expects to transfer */
  36. uint8_t bmFlags; /* Bit 7: Direction=IN (other obsolete or reserved) */
  37. uint8_t bLUN; /* LUN (normally 0) */
  38. uint8_t bCBLength; /* len of cdb[] */
  39. uint8_t CB[MSC_MAX_CDB_LEN]; /* Command Data Block */
  40. } __PACKED;
  41. #define USB_SIZEOF_MSC_CBW 31
  42. /** MSC Bulk-Only Command Status Wrapper (CSW) */
  43. struct CSW {
  44. uint32_t dSignature; /* 'USBS' = 0x53425355 */
  45. uint32_t dTag; /* Same tag as original command */
  46. uint32_t dDataResidue; /* Amount not transferred */
  47. uint8_t bStatus; /* Status of transfer */
  48. } __PACKED;
  49. #define USB_SIZEOF_MSC_CSW 13
  50. /*Length of template descriptor: 23 bytes*/
  51. #define MSC_DESCRIPTOR_LEN (9 + 7 + 7)
  52. // clang-format off
  53. #define MSC_DESCRIPTOR_INIT(bFirstInterface, out_ep, in_ep, wMaxPacketSize, str_idx) \
  54. /* Interface */ \
  55. 0x09, /* bLength */ \
  56. USB_DESCRIPTOR_TYPE_INTERFACE, /* bDescriptorType */ \
  57. bFirstInterface, /* bInterfaceNumber */ \
  58. 0x00, /* bAlternateSetting */ \
  59. 0x02, /* bNumEndpoints */ \
  60. USB_DEVICE_CLASS_MASS_STORAGE, /* bInterfaceClass */ \
  61. MSC_SUBCLASS_SCSI, /* bInterfaceSubClass */ \
  62. MSC_PROTOCOL_BULK_ONLY, /* bInterfaceProtocol */ \
  63. str_idx, /* iInterface */ \
  64. 0x07, /* bLength */ \
  65. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  66. out_ep, /* bEndpointAddress */ \
  67. 0x02, /* bmAttributes */ \
  68. WBVAL(wMaxPacketSize), /* wMaxPacketSize */ \
  69. 0x00, /* bInterval */ \
  70. 0x07, /* bLength */ \
  71. USB_DESCRIPTOR_TYPE_ENDPOINT, /* bDescriptorType */ \
  72. in_ep, /* bEndpointAddress */ \
  73. 0x02, /* bmAttributes */ \
  74. WBVAL(wMaxPacketSize), /* wMaxPacketSize */ \
  75. 0x00 /* bInterval */
  76. // clang-format on
  77. #endif /* USB_MSC_H */