Logo
MSP430USBDevelopersPackage_5_20_06_02
UsbMscScsi.h
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2016, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*
33  * ======== UsbMscScsi.h ========
34  */
35 #include <stdint.h>
36 
37 #ifndef _UMSC_SCSI_H_
38 #define _UMSC_SCSI_H_
39 
40 #include <descriptors.h>
41 
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #endif
46 
47 /*----------------------------------------------------------------------------
48  * The following function names and macro names are deprecated. These were
49  * updated to new names to follow OneMCU naming convention.
50  +---------------------------------------------------------------------------*/
51 #ifndef DEPRECATED
52 #define kUSBMSC_RWSuccess USBMSC_RW_SUCCESS
53 #define kUSBMSC_RWNotReady USBMSC_RW_NOT_READY
54 #define kUSBMSC_RWIllegalReq USBMSC_RW_ILLEGAL_REQUEST
55 #define kUSBMSC_RWUnitAttn USBMSC_RW_UNIT_ATTENTION
56 #define kUSBMSC_RWLbaOutOfRange USBMSC_RW_LBA_OUT_OF_RANGE
57 #define kUSBMSC_RWMedNotPresent USBMSC_RW_MEDIA_NOT_PRESENT
58 #define kUSBMSC_RWDevWriteFault USBMSC_RW_DEVICE_WRITE_FAULT
59 #define kUSBMSC_RWUnrecoveredRead USBMSC_RW_UNRECOVERED_READ
60 #define kUSBMSC_RWWriteProtected USBMSC_RW_WRITE_PROTECTED
61 #define kUSBMSC_READ USBMSC_READ
62 #define kUSBMSC_WRITE USBMSC_WRITE
63 #define kUSBMSC_MEDIA_PRESENT USBMSC_MEDIA_PRESENT
64 #define kUSBMSC_MEDIA_NOT_PRESENT USBMSC_MEDIA_NOT_PRESENT
65 #define kUSBMSC_WRITE_PROTECTED USBMSC_WRITE_PROTECTED
66 
67 #define USBMSC_fetchInfoStruct USBMSC_fetchInformationStructure
68 #endif
69 
70 
71 /*Macros for CBW, CSW signatures */
72 #define CBW_SIGNATURE 0x43425355u
73 #define CSW_SIGNATURE 0x53425355u
74 
75 /*CBW, CSW length in bytes */
76 #define CBW_LENGTH 31
77 #define CSW_LENGTH 13
78 
79 /*SCSI Commands - Mandatory only implemented */
80 #define SCSI_TEST_UNIT_READY 0x00
81 #define SCSI_REQUEST_SENSE 0x03
82 #define SCSI_INQUIRY 0x12
83 #define SCSI_MODE_SENSE_6 0x1A
84 #define SCSI_MODE_SENSE_10 0x5A
85 #define SCSI_READ_CAPACITY_10 0x25
86 #define SCSI_READ_10 0x28
87 #define SCSI_WRITE_10 0x2A
88 #define SCSI_READ_FORMAT_CAPACITIES 0x23
89 #define SCSI_MODE_SELECT_6 0x15
90 #define SCSI_MODE_SELECT_10 0x55
91 #define PREVENT_ALLW_MDM 0x1E
92 #define START_STOP_UNIT 0x1B
93 #define SCSI_REPORT_LUNS 0xA0
94 #define SCSI_VERIFY 0x2F
95 
96 #define SCSI_READ_TOC_PMA_ATIP 0x43
97 #define Scsi_Read_TOC_PMA_ATIP_F1_LEN 20
98 #define Scsi_Read_TOC_PMA_ATIP_F2_LEN 48
99 #define SCSI_GET_CONFIGURATION 0x46
100 #define SCSI_GET_CONFIGURATION_LEN 4
101 #define SCSI_EVENT_STATUS 0x4A
102 #define SCSI_EVENT_STATUS_LEN 8
103 #define SCSI_READ_DISC_INFORMATION 0x51
104 #define SCSI_SET_CD_SPEED 0xBB
105 #define SCSI_READ_DISC_INFORMATION_LEN 36
106 
107 /*SCSI Status codes. Used in CSW response */
108 #define SCSI_PASSED 0
109 #define SCSI_FAILED 1
110 #define SCSI_PHASE_ERROR 2
111 #define SCSI_READWRITE_FAIL 2
112 
113 #define USBMSC_RW_SUCCESS 0
114 #define USBMSC_RW_NOT_READY 1
115 #define USBMSC_RW_ILLEGAL_REQUEST 2
116 #define USBMSC_RW_UNIT_ATTENTION 3
117 #define USBMSC_RW_LBA_OUT_OF_RANGE 4
118 #define USBMSC_RW_MEDIA_NOT_PRESENT 5
119 #define USBMSC_RW_DEVICE_WRITE_FAULT 6
120 #define USBMSC_RW_UNRECOVERED_READ 7
121 #define USBMSC_RW_WRITE_PROTECTED 8
122 
123 
124 /* Macros to indicate READ or WRITE operation */
125 #define USBMSC_READ 1
126 #define USBMSC_WRITE 2
127 
128 #define USBMSC_MEDIA_PRESENT 0x81
129 #define USBMSC_MEDIA_NOT_PRESENT 0x82
130 
131 #define USBMSC_WRITE_PROTECTED 0x00
132 
133 /* Defines for MSC SCSI State-Machine */
134 #define MSC_READY 0x00
135 #define MSC_COMMAND_TRANSPORT 0x01
136 #define MSC_DATA_IN 0x02
137 #define MSC_DATA_OUT 0x03
138 #define MSC_STATUS_TRANSPORT 0x04
139 #define MSC_DATA 0x05
140 #define MSC_WAIT4RESET 0x06
141 
142 /*Lengths of SCSI commands(in bytes) */
143 #define SCSI_SCSI_INQUIRY_CMD_LEN 36
144 #define SCSI_READ_CAPACITY_CMD_LEN 8
145 #define SCSI_MODE_SENSE_6_CMD_LEN 4
146 #define SCSI_MODE_SENSE_10_CMD_LEN 8
147 #define SCSI_REQ_SENSE_CMD_LEN 18
148 #define SCSI_READ_FORMAT_CAPACITY_CMD_LEN 12
149 #define SCSI_REPORT_LUNS_CMD_LEN 16
150 
151 /*----------------------------------------------------------------------------+
152  | Type defines and structures |
153  +----------------------------------------------------------------------------*/
154 /*CBW Structure */
155 typedef struct {
156  uint32_t dCBWSignature;
157  uint32_t dCBWTag;
158  uint32_t dCBWDataTransferLength;
159  uint8_t bmCBWFlags;
160  uint8_t bCBWLUN;
161  uint8_t bCBWCBLength;
162  uint8_t CBWCB[16];
163 } CBW, *pCBW;
164 
165 /*CSW structure */
166 typedef struct {
167  uint32_t dCSWSignature;
168  uint32_t dCSWTag;
169  uint32_t dCSWDataResidue;
170  uint8_t bCSWStatus;
171 } CSW, *pCSW;
172 
173 /*Request Response union(Required for Request sense command) */
174 typedef struct {
175  uint8_t ResponseCode : 7;
176  uint8_t VALID : 1;
177  uint8_t Obsolete;
178  uint8_t SenseKey : 4;
179  uint8_t Resv : 1;
180  uint8_t ILI : 1;
181  uint8_t EOM : 1;
182  uint8_t FILEMARK : 1;
183  uint8_t Information[4];
184  uint8_t AddSenseLen;
185  uint8_t CmdSpecificInfo[4];
186  uint8_t ASC;
187  uint8_t ASCQ;
188  uint8_t FRUC;
189  uint8_t SenseKeySpecific[3];
190  uint8_t padding[14]; /* padding to cover case where host requests 24 bytes of sense data */
192 
193 /*Read capacity union(Required for READ CAPACITY command)*/
194 typedef struct {
195  uint32_t Last_LBA;
196  uint8_t Resv;
197  uint8_t Size_LBA[3];
199 
200 /*Structure internal to stack for holding LBA,buffer addr etc information*/
201 typedef struct {
202  //uint8_t intfNum;
203  uint8_t lun;
204  uint8_t operation;
205  uint32_t lba;
206  uint8_t lbCount;
207  uint8_t *bufferAddr;
208  uint8_t returnCode;
209  uint8_t XorY;
210  uint8_t xBufFull;
211  uint16_t xWordCnt;
212  uint8_t yBufFull;
213  uint16_t yWordCnt;
214  uint8_t bufferProcessed;
215  uint8_t firstFlag;
216  uint32_t xlba;
217  uint8_t xlbaCount;
218  uint32_t ylba;
219  uint8_t ylbaCount;
220 
222 
223 /*Media info structure */
225  uint32_t lastBlockLba;
226  uint32_t bytesPerBlock;
227  uint8_t mediaPresent;
228  uint8_t mediaChanged;
229  uint8_t writeProtected;
230 };
231 
232 /*Lun entry Structures */
234  uint8_t number;
235  uint8_t PDT;
236  uint8_t removable;
237  char t10VID[8];
238  char t10PID[16];
239  char t10rev[4];
240 };
241 
243  struct _LUN_entry_struct LUN[MSC_MAX_LUN_NUMBER];
244 };
245 
246 struct _Report_Luns {
247  uint8_t LunListLength[4];
248  uint8_t Reserved[4];
249  uint8_t LunList1[8];
250 };
251 
253  uint8_t lLba[4]; //Last logical block address
254  uint8_t bLength[4]; //Block length, in this case 0x200 = 512 bytes for each Logical Block
255 };
256 
257 //structure for controlling WRITE phase (HOST to MSP430)
259  uint32_t dwBytesToReceiveLeft; //holds how many bytes is still requested by WRITE operation:
260  //Host to MSP430.
261  uint16_t wFreeBytesLeft; //free bytes left in UserBuffer
262  uint32_t lba; //holds the current LBA number. This is the first LBA in the UserBuffer
263  uint8_t *pUserBuffer; //holds the current position of user's receiving buffer.
264  //If NULL- no receiving operation started
265  uint16_t wCurrentByte; //how many bytes in current LBA are received
266  uint16_t lbaCount; //how many LBA we have received in current User Buffer
267  uint8_t * pCT1; //holds current EPBCTxx register
268  uint8_t * pCT2; //holds next EPBCTxx register
269  uint8_t * pEP2; //holds addr of the next EP buffer
270  uint8_t bCurrentBufferXY; //indicates which buffer is used by host to transmit data via OUT
271  uint8_t bWriteProcessing; //indicated if the current state is DATA WRITE phase or CBW receiwing
272  uint8_t XorY;
273 };
274 
275 //structure for controlling READ phase (MSP430 to HOST)
277  uint32_t dwBytesToSendLeft; //holds how many bytes is still requested by WRITE operation (Host to MSP430)
278  uint8_t *pUserBuffer; //holds the current position of user's receiving buffer.
279  //If NULL- no receiving operation started
280  uint32_t lba; //holds the current LBA number. This is the first LBA in the UserBuffer.
281  uint8_t * pCT1; //holds current EPBCTxx register
282  uint8_t * pCT2; //holds next EPBCTxx register
283  uint8_t * pEP2; //holds addr of the next EP buffer
284  uint16_t lbaCount; //how many LBA we have to send to Host
285  uint8_t bCurrentBufferXY; //indicates which buffer is used by host to transmit data via OUT
286  uint8_t bReadProcessing; //indicated if the current state is DATA READ phase or CSW sending
287  //initiated by McsDataSend()
288  uint8_t XorY;
289 };
290 
291 //structure for common control of MSC stack
292 struct _MscControl {
293  uint16_t wMscUserBufferSize;
294  uint16_t lbaSize; //limitid to uint16_t, but could be increased if required.
295  uint8_t lbaBufCapacity; //how many LBAs (max) contains UserBuffer for read/write operation (>=1)
296  uint8_t *xBufferAddr;
297  uint8_t *yBufferAddr;
298  uint8_t bMediaPresent;
299  uint8_t bWriteProtected;
300 };
301 
302 struct _MscState {
303  volatile uint32_t Scsi_Residue;
304  volatile uint8_t Scsi_Status; /*Variable to track command status */
305  int16_t bMcsCommandSupported; /*Flag to indicate read/write command is recieved from host */
306  int16_t bMscCbwReceived; /*Flag to inidicate whether any CBW recieved from host*/
307  int16_t bMscSendCsw;
308  int16_t isMSCConfigured;
309  uint8_t bUnitAttention;
310  uint8_t bMscCbwFailed;
311  uint8_t bMscResetRequired;
312  uint8_t stallEndpoint;
313  uint8_t stallAtEndofTx;
314 };
315 
316 extern struct _MscWriteControl MscWriteControl;
317 extern struct _MscReadControl MscReadControl;
318 extern struct _MscControl MscControl[];
319 
320 /*----------------------------------------------------------------------------+
321  | Extern Variables |
322  +----------------------------------------------------------------------------*/
323 
324 extern CBW cbw;
325 extern CSW csw;
326 extern REQUEST_SENSE_RESPONSE RequestSenseResponse;
327 
328 /*----------------------------------------------------------------------------+
329  | Function Prototypes |
330  +----------------------------------------------------------------------------*/
331 
332 /*SCSI Wrapper functions */
333 //*****************************************************************************
334 //
337 //
338 //******************************************************************************
339 uint8_t Scsi_Cmd_Parser (uint8_t opcode);
340 uint8_t Scsi_Send_CSW (uint8_t intfNum);
341 
342 /*Function to reset MSC SCSI state machine */
343 void Msc_ResetStateMachine(void);
344 void Msc_ResetFlags(void);
345 void Msc_ResetStruct(void);
346 void SET_RequestsenseNotReady(void);
347 void SET_RequestsenseMediaNotPresent(void);
348 void MscResetCtrlLun(void);
349 
350 //*****************************************************************************
351 //
362 //
363 //*****************************************************************************
365 //******************************************************************************
366 //
367 // Close the Doxygen group.
369 //
370 //******************************************************************************
371 #ifdef __cplusplus
372 }
373 #endif
374 #endif //_MSC_SCSI_H_
375 
Definition: UsbMscScsi.h:246
Definition: UsbMscScsi.h:276
Definition: UsbMscScsi.h:224
Definition: UsbMscScsi.h:155
USBMSC_RWbuf_Info * USBMSC_fetchInformationStructure(void)
Definition: UsbMscScsi.h:166
Definition: UsbMscScsi.h:302
Definition: UsbMscScsi.h:258
Definition: UsbMscScsi.h:242
Definition: UsbMscScsi.h:174
Definition: UsbMscScsi.h:194
Definition: UsbMscScsi.h:252
Definition: UsbMscScsi.h:292
Definition: UsbMscScsi.h:201
Definition: UsbMscScsi.h:233