summaryrefslogtreecommitdiff
path: root/bsp/synwit/libraries/SWM341_CSL/SWM341_UsbHost_Lib/MSC/usbh_msc_scsi.c
blob: 05ec905d0c6cb500877b349963c12cd90716c38f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/******************************************************************************************************************************************
* 文件名称: usbh_msc_scsi.c
* 功能说明:
* 技术支持: http://www.synwit.com.cn/e/tool/gbook/?bid=1
* 注意事项:
* 版本日期: V1.1.0      2020年11月3日
* 升级记录:
*
*
*******************************************************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM341.h"
#include "usbh_core.h"
#include "usbh_msc_bot.h"
#include "usbh_msc_core.h"
#include "usbh_msc_scsi.h"


#define OPCODE_TEST_UNIT_READY      0X00
#define OPCODE_READ_CAPACITY10      0x25
#define OPCODE_MODE_SENSE6          0x1A
#define OPCODE_READ10               0x28
#define OPCODE_WRITE10              0x2A
#define OPCODE_REQUEST_SENSE        0x03

#define DATLEN_READ_CAPACITY10      8       // Data Stage Length
#define DATLEN_MODE_SENSE6          63
#define DATLEN_REQUEST_SENSE        18


static uint8_t SCSI_Busy = 0;

static uint8_t DataInBuffer[64];


/******************************************************************************************************************************************
* 函数名称: USBH_MSC_TestUnitReady()
* 功能说明: Issues 'Test unit ready' command to the device.
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_MSC_Status USBH_MSC_TestUnitReady(void)
{
    USBH_MSC_Status status = USBH_MSC_BUSY;

    if(!SCSI_Busy)
    {
        USBH_CBWData.CBWTransferLength = 0; // No Data Stage
        USBH_CBWData.CBWCBLength = 6;
        USBH_CBWData.CBWCB[0] = OPCODE_TEST_UNIT_READY;
        for(uint8_t i = 1; i < 16; i++) USBH_CBWData.CBWCB[i] = 0;

        USBH_BOTXfer.bCSWStatus = USBH_MSC_BUSY;
        USBH_BOTXfer.BOTState = USBH_MSC_BOT_CBW;

        USBH_BOTXfer.MSCStateBkp = USBH_BOTXfer.MSCState;
        USBH_BOTXfer.MSCState = USBH_MSC_BOT_TRANSFER;

        SCSI_Busy = 1;
    }
    else
    {
        if(USBH_BOTXfer.bCSWStatus != USBH_MSC_BUSY)
        {
            status = USBH_BOTXfer.bCSWStatus;

            SCSI_Busy = 0;
        }
    }

    return status;
}


/******************************************************************************************************************************************
* 函数名称: USBH_MSC_ReadCapacity10()
* 功能说明: Issue the read capacity command to the device.
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_MSC_Status USBH_MSC_ReadCapacity10(void)
{
    USBH_MSC_Status status = USBH_MSC_BUSY;

    if(!SCSI_Busy)
    {
        USBH_CBWData.CBWTransferLength = DATLEN_READ_CAPACITY10;
        USBH_CBWData.CBWFlags = USB_REQ_D2H;
        USBH_CBWData.CBWCBLength = 10;
        USBH_CBWData.CBWCB[0]  = OPCODE_READ_CAPACITY10;
        for(uint8_t i = 1; i < 16; i++) USBH_CBWData.CBWCB[i] = 0;

        USBH_BOTXfer.pDataBuffer = DataInBuffer;

        USBH_BOTXfer.bCSWStatus = USBH_MSC_BUSY;
        USBH_BOTXfer.BOTState = USBH_MSC_BOT_CBW;

        USBH_BOTXfer.MSCStateBkp = USBH_BOTXfer.MSCState;
        USBH_BOTXfer.MSCState = USBH_MSC_BOT_TRANSFER;

        SCSI_Busy = 1;
    }
    else
    {
        if(USBH_BOTXfer.bCSWStatus != USBH_MSC_BUSY)
        {
            if(USBH_BOTXfer.bCSWStatus == USBH_MSC_OK)
            {
                /* assign the capacity */
                ((uint8_t *)&USBH_MSC_Info.Capacity)[3] = DataInBuffer[0];
                ((uint8_t *)&USBH_MSC_Info.Capacity)[2] = DataInBuffer[1];
                ((uint8_t *)&USBH_MSC_Info.Capacity)[1] = DataInBuffer[2];
                ((uint8_t *)&USBH_MSC_Info.Capacity)[0] = DataInBuffer[3];

                /* assign the page length */
                ((uint8_t *)&USBH_MSC_Info.PageSize)[1] = DataInBuffer[6];
                ((uint8_t *)&USBH_MSC_Info.PageSize)[0] = DataInBuffer[7];
            }

            status = USBH_BOTXfer.bCSWStatus;

            SCSI_Busy = 0;
        }
    }

    return status;
}


/******************************************************************************************************************************************
* 函数名称: USBH_MSC_ModeSense6()
* 功能说明: Issue the Mode Sense6 Command to the device.
*           Used for reading the WriteProtect Status of the Mass-Storage device.
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_MSC_Status USBH_MSC_ModeSense6(void)
{
    USBH_MSC_Status status = USBH_MSC_BUSY;

    if(!SCSI_Busy)
    {
        USBH_CBWData.CBWTransferLength = DATLEN_MODE_SENSE6;
        USBH_CBWData.CBWFlags = USB_REQ_D2H;
        USBH_CBWData.CBWCBLength = 10;
        USBH_CBWData.CBWCB[0] = OPCODE_MODE_SENSE6;
        USBH_CBWData.CBWCB[1] = 0x00;
        USBH_CBWData.CBWCB[2] = 0x3F;
        USBH_CBWData.CBWCB[3] = 0x00;
        USBH_CBWData.CBWCB[4] = DATLEN_MODE_SENSE6;
        for(uint8_t i = 5; i < 16; i++) USBH_CBWData.CBWCB[i] = 0;

        USBH_BOTXfer.pDataBuffer = DataInBuffer;

        USBH_BOTXfer.bCSWStatus = USBH_MSC_BUSY;
        USBH_BOTXfer.BOTState = USBH_MSC_BOT_CBW;

        USBH_BOTXfer.MSCStateBkp = USBH_BOTXfer.MSCState;
        USBH_BOTXfer.MSCState = USBH_MSC_BOT_TRANSFER;

        SCSI_Busy = 1;
    }
    else
    {
        if(USBH_BOTXfer.bCSWStatus != USBH_MSC_BUSY)
        {
            if(USBH_BOTXfer.bCSWStatus == USBH_MSC_OK)
            {
                /* Assign the Write Protect status */
                if(DataInBuffer[2] & 0x80)
                {
                    USBH_MSC_Info.WProtect = 1;
                }
                else
                {
                    USBH_MSC_Info.WProtect = 0;
                }
            }

            status = USBH_BOTXfer.bCSWStatus;

            SCSI_Busy = 0;
        }
    }

    return status;
}


/******************************************************************************************************************************************
* 函数名称: USBH_MSC_Read10()
* 功能说明: Issue the read command to the device.
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_MSC_Status USBH_MSC_Read10(uint8_t *buffer, uint32_t address, uint32_t nbOfbytes)
{
    USBH_MSC_Status status = USBH_MSC_BUSY;
    uint16_t nbOfPages;

    if(!SCSI_Busy)
    {
        USBH_CBWData.CBWTransferLength = nbOfbytes;
        USBH_CBWData.CBWFlags = USB_REQ_D2H;
        USBH_CBWData.CBWCBLength = 10;
        USBH_CBWData.CBWCB[0] = OPCODE_READ10;
        USBH_CBWData.CBWCB[1] = 0x00;
        /*logical block address*/
        USBH_CBWData.CBWCB[2] = ((uint8_t *)&address)[3];
        USBH_CBWData.CBWCB[3] = ((uint8_t *)&address)[2];
        USBH_CBWData.CBWCB[4] = ((uint8_t *)&address)[1];
        USBH_CBWData.CBWCB[5] = ((uint8_t *)&address)[0];
        USBH_CBWData.CBWCB[6] = 0x00;
        /*Transfer length */
        nbOfPages = nbOfbytes / USBH_MSC_Info.PageSize;
        USBH_CBWData.CBWCB[7] = ((uint8_t *)&nbOfPages)[1];
        USBH_CBWData.CBWCB[8] = ((uint8_t *)&nbOfPages)[0];
        for(uint8_t i = 9; i < 16; i++) USBH_CBWData.CBWCB[i] = 0;

        USBH_BOTXfer.pDataBuffer = buffer;

        USBH_BOTXfer.bCSWStatus = USBH_MSC_BUSY;
        USBH_BOTXfer.BOTState = USBH_MSC_BOT_CBW;

        USBH_BOTXfer.MSCStateBkp = USBH_BOTXfer.MSCState;
        USBH_BOTXfer.MSCState = USBH_MSC_BOT_TRANSFER;

        SCSI_Busy = 1;
    }
    else
    {
        if(USBH_BOTXfer.bCSWStatus != USBH_MSC_BUSY)
        {
            status = USBH_BOTXfer.bCSWStatus;

            SCSI_Busy = 0;
        }
    }

    return status;
}


/******************************************************************************************************************************************
* 函数名称: USBH_MSC_Write10()
* 功能说明: Issue the write command to the device.
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_MSC_Status USBH_MSC_Write10(uint8_t *buffer, uint32_t address, uint32_t nbOfbytes)
{
    USBH_MSC_Status status = USBH_MSC_BUSY;
    uint16_t nbOfPages;

    if(!SCSI_Busy)
    {
        USBH_CBWData.CBWTransferLength = nbOfbytes;
        USBH_CBWData.CBWFlags = USB_REQ_H2D;
        USBH_CBWData.CBWCBLength = 10;
        USBH_CBWData.CBWCB[0] = OPCODE_WRITE10;
        USBH_CBWData.CBWCB[1] = 0x00;
        /*logical block address*/
        USBH_CBWData.CBWCB[2] = ((uint8_t *)&address)[3];
        USBH_CBWData.CBWCB[3] = ((uint8_t *)&address)[2];
        USBH_CBWData.CBWCB[4] = ((uint8_t *)&address)[1];
        USBH_CBWData.CBWCB[5] = ((uint8_t *)&address)[0];
        USBH_CBWData.CBWCB[6] = 0x00;
        /*Transfer length */
        nbOfPages = nbOfbytes / USBH_MSC_Info.PageSize;
        USBH_CBWData.CBWCB[7] = ((uint8_t *)&nbOfPages)[1];
        USBH_CBWData.CBWCB[8] = ((uint8_t *)&nbOfPages)[0];
        for(uint8_t i = 9; i < 16; i++) USBH_CBWData.CBWCB[i] = 0;

        USBH_BOTXfer.pDataBuffer = buffer;

        USBH_BOTXfer.bCSWStatus = USBH_MSC_BUSY;
        USBH_BOTXfer.BOTState = USBH_MSC_BOT_CBW;

        USBH_BOTXfer.MSCStateBkp = USBH_BOTXfer.MSCState;
        USBH_BOTXfer.MSCState = USBH_MSC_BOT_TRANSFER;

        SCSI_Busy = 1;
    }
    else
    {
        if(USBH_BOTXfer.bCSWStatus != USBH_MSC_BUSY)
        {
            status = USBH_BOTXfer.bCSWStatus;

            SCSI_Busy = 0;
        }
    }

    return status;
}


/******************************************************************************************************************************************
* 函数名称: USBH_MSC_RequestSense()
* 功能说明: Issues the Request Sense command to the device.
* 输    入: 无
* 输    出: 无
* 注意事项: 无
******************************************************************************************************************************************/
USBH_MSC_Status USBH_MSC_RequestSense(void)
{
    USBH_MSC_Status status = USBH_MSC_BUSY;

    if(!SCSI_Busy)
    {
        USBH_CBWData.CBWTransferLength = DATLEN_REQUEST_SENSE;
        USBH_CBWData.CBWFlags = USB_REQ_D2H;
        USBH_CBWData.CBWCBLength = 6;
        USBH_CBWData.CBWCB[0]  = OPCODE_REQUEST_SENSE;
        USBH_CBWData.CBWCB[1]  = 0x00;
        USBH_CBWData.CBWCB[2]  = 0x00;
        USBH_CBWData.CBWCB[3]  = 0x00;
        USBH_CBWData.CBWCB[4]  = DATLEN_REQUEST_SENSE;
        for(uint8_t i = 5; i < 16; i++) USBH_CBWData.CBWCB[i] = 0;

        USBH_BOTXfer.pDataBuffer = DataInBuffer;

        USBH_BOTXfer.bCSWStatus = USBH_MSC_BUSY;
        USBH_BOTXfer.BOTState = USBH_MSC_BOT_CBW;

        USBH_BOTXfer.MSCState = USBH_MSC_BOT_TRANSFER;

        SCSI_Busy = 1;
    }
    else
    {
        if(USBH_BOTXfer.bCSWStatus != USBH_MSC_BUSY)
        {
            if(USBH_BOTXfer.bCSWStatus == USBH_MSC_OK)
            {
                /* Assign Sense data*/
                ((uint8_t *)&USBH_MSC_Info.SenseKey)[3] = DataInBuffer[0];
                ((uint8_t *)&USBH_MSC_Info.SenseKey)[2] = DataInBuffer[1];
                ((uint8_t *)&USBH_MSC_Info.SenseKey)[1] = DataInBuffer[2];
                ((uint8_t *)&USBH_MSC_Info.SenseKey)[0] = DataInBuffer[3];

                status = USBH_MSC_OK;
            }

            status = USBH_BOTXfer.bCSWStatus;

            SCSI_Busy = 0;
        }
    }

    return status;
}