You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * MSC Teensy36 USB Host Mass Storage library
  3. * Copyright (c) 2017-2019 Warren Watson.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. // msc.h
  26. #ifndef _MSC_H_
  27. #define _MSC_H_
  28. // SCSI Commands (minimal set)
  29. #define CBW_SIGNATURE 0x43425355UL
  30. #define CSW_SIGNATURE 0x53425355UL
  31. #define CMD_DIR_DATA_OUT 0x00
  32. #define CMD_DIR_DATA_IN 0x80
  33. #define CMD_RD_CAPACITY_10 0x25
  34. #define CMD_INQUIRY 0x12
  35. #define CMD_RD_10 0x28
  36. #define CMD_WR_10 0x2A
  37. #define CMD_TEST_UNIT_READY 0x00
  38. #define CMD_REQUEST_SENSE 0x03
  39. #define CMD_START_STOP_UNIT 0x1B
  40. #define CMD_REPORT_LUNS 0xA0
  41. #define NO_RD_WR 0x00 // transfer type is not read or write blocks.
  42. // Command Status Wrapper Error Codes
  43. #define MS_CBW_PASS 0
  44. #define MS_CBW_FAIL 1
  45. #define MS_CBW_PHASE_ERROR 2
  46. #define MS_CSW_TAG_ERROR 253
  47. #define MS_CSW_SIG_ERROR 254
  48. #define MS_SCSI_ERROR 255
  49. // SCSI Sense Key codes
  50. #define MS_NOT_READY 0x02
  51. #define MS_MEDIUM_ERROR 0x03
  52. #define MS_ILLEGAL_REQUEST 0x05
  53. #define MS_UNIT_ATTENTION 0x06
  54. #define MS_LBA_OUT_OF_RANGE 0x21
  55. #define MS_MEDIA_CHANGED 0x28
  56. #define MS_MEDIUM_NOT_PRESENT 0x3A
  57. // SCSI Error Codes
  58. #define MS_MEDIA_CHANGED_ERR 0x2A
  59. #define MS_NO_MEDIA_ERR 0x28
  60. #define MS_UNIT_NOT_READY 0x23
  61. #define MS_BAD_LBA_ERR 0x29
  62. #define MS_CMD_ERR 0x26
  63. #define MS_INIT_PASS 0
  64. #define MAXLUNS 16
  65. #define MEDIA_READY_TIMEOUT 1000
  66. #define MSC_CONNECT_TIMEOUT 4000
  67. // Command Block Wrapper Struct
  68. typedef struct
  69. {
  70. uint32_t Signature;
  71. uint32_t Tag;
  72. uint32_t TransferLength;
  73. uint8_t Flags;
  74. uint8_t LUN;
  75. uint8_t CommandLength;
  76. uint8_t CommandData[16];
  77. } __attribute__((packed)) msCommandBlockWrapper_t;
  78. // MSC Command Status Wrapper Struct
  79. typedef struct
  80. {
  81. uint32_t Signature;
  82. uint32_t Tag;
  83. uint32_t DataResidue;
  84. uint8_t Status;
  85. } __attribute__((packed)) msCommandStatusWrapper_t;
  86. // MSC Device Capacity Struct
  87. typedef struct
  88. {
  89. uint32_t Blocks;
  90. uint32_t BlockSize;
  91. } msSCSICapacity_t;
  92. // MSC Inquiry Command Reponse Struct
  93. typedef struct
  94. {
  95. unsigned DeviceType : 5;
  96. unsigned PeripheralQualifier : 3;
  97. unsigned Reserved : 7;
  98. unsigned Removable : 1;
  99. uint8_t Version;
  100. unsigned ResponseDataFormat : 4;
  101. unsigned Reserved2 : 1;
  102. unsigned NormACA : 1;
  103. unsigned TrmTsk : 1;
  104. unsigned AERC : 1;
  105. uint8_t AdditionalLength;
  106. uint8_t Reserved3[2];
  107. unsigned SoftReset : 1;
  108. unsigned CmdQue : 1;
  109. unsigned Reserved4 : 1;
  110. unsigned Linked : 1;
  111. unsigned Sync : 1;
  112. unsigned WideBus16Bit : 1;
  113. unsigned WideBus32Bit : 1;
  114. unsigned RelAddr : 1;
  115. uint8_t VendorID[8];
  116. uint8_t ProductID[16];
  117. uint8_t RevisionID[4];
  118. } __attribute__((packed)) msInquiryResponse_t;
  119. // Request Sense Response Struct
  120. typedef struct
  121. {
  122. uint8_t ResponseCode;
  123. uint8_t SegmentNumber;
  124. unsigned SenseKey : 4;
  125. unsigned Reserved : 1;
  126. unsigned ILI : 1;
  127. unsigned EOM : 1;
  128. unsigned FileMark : 1;
  129. uint8_t Information[4];
  130. uint8_t AdditionalLength;
  131. uint8_t CmdSpecificInformation[4];
  132. uint8_t AdditionalSenseCode;
  133. uint8_t AdditionalSenseQualifier;
  134. uint8_t FieldReplaceableUnitCode;
  135. uint8_t SenseKeySpecific[3];
  136. uint8_t padding[234];
  137. } __attribute__((packed)) msRequestSenseResponse_t;
  138. // MSC Drive status/info struct
  139. typedef struct {
  140. bool connected = false; // Device is connected
  141. bool initialized = false; // Device is initialized
  142. bool mounted = false; // Device is mounted
  143. const char * drvName = 0;
  144. uint32_t bufferSize = 0;
  145. uint8_t hubNumber;
  146. uint8_t hubPort;
  147. uint8_t deviceAddress;
  148. uint16_t idVendor;
  149. uint16_t idProduct;
  150. msSCSICapacity_t capacity;
  151. msInquiryResponse_t inquiry;
  152. } __attribute__((packed)) msDriveInfo_t;
  153. #endif //_MSC_H_