mxpopstar
2022-05-04 fc9a39919da8db88c68ee1c41c01f6c26b88f12c
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
367
368
369
370
371
372
/*!
    \file    usbh_msc_bbb.c
    \brief   USB MSC BBB protocol related functions
 
    \version 2020-08-01, V3.0.0, firmware for GD32F30x
*/
 
/*
    Copyright (c) 2020, GigaDevice Semiconductor Inc.
 
    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
 
    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.
 
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/
 
#include "usbh_pipe.h"
#include "usbh_msc_core.h"
#include "usbh_msc_scsi.h"
#include "usbh_msc_bbb.h"
#include "usbh_transc.h"
#include "drv_usbh_int.h"
#include <string.h>
 
/*!
    \brief      initialize the mass storage parameters
    \param[in]  puhost: pointer to usb host handler
    \param[out] none
    \retval     none
*/
void usbh_msc_bot_init (usbh_host *puhost)
{
    usbh_msc_handler *msc = (usbh_msc_handler *)puhost->active_class->class_data;
 
    msc->bot.cbw.field.dCBWSignature = BBB_CBW_SIGNATURE;
    msc->bot.cbw.field.dCBWTag = USBH_MSC_BOT_CBW_TAG;
    msc->bot.state = BOT_SEND_CBW;
    msc->bot.cmd_state = BOT_CMD_SEND;
}
 
/*!
    \brief      manage the different states of BOT transfer and updates the status to upper layer
    \param[in]  puhost: pointer to usb host handler
    \param[in]  lun: logic unit number
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_msc_bot_process (usbh_host *puhost, uint8_t lun)
{
    bot_csw_status csw_status = BOT_CSW_CMD_FAILED;
    usbh_status status = USBH_BUSY;
    usbh_status error = USBH_BUSY;
    usb_urb_state urb_status = URB_IDLE;
    usbh_msc_handler *msc = (usbh_msc_handler *)puhost->active_class->class_data;
 
    switch (msc->bot.state) {
    case BOT_SEND_CBW:
        msc->bot.cbw.field.bCBWLUN = lun;
        msc->bot.state = BOT_SEND_CBW_WAIT;
        /* send CBW */
        usbh_data_send ((usb_core_driver *)(puhost->data),
                        msc->bot.cbw.CBWArray, 
                        msc->pipe_out, 
                        BBB_CBW_LENGTH);
        break;
 
    case BOT_SEND_CBW_WAIT:
        urb_status = usbh_urbstate_get((usb_core_driver *)(puhost->data), msc->pipe_out);
 
        if (URB_DONE == urb_status) {
            if (0U != msc->bot.cbw.field.dCBWDataTransferLength) {
                if (USB_TRX_IN == (msc->bot.cbw.field.bmCBWFlags & USB_TRX_MASK)) {
                    msc->bot.state = BOT_DATA_IN;
                } else {
                    msc->bot.state = BOT_DATA_OUT;
                }
            } else {
                msc->bot.state = BOT_RECEIVE_CSW;
            }
 
        } else if (URB_NOTREADY == urb_status) {
            msc->bot.state = BOT_SEND_CBW;
        } else {
            if (URB_STALL == urb_status) {
                msc->bot.state = BOT_ERROR_OUT;
            }
        }
        break;
 
    case BOT_DATA_IN:
        usbh_data_recev ((usb_core_driver *)(puhost->data), 
                         msc->bot.pbuf, 
                         msc->pipe_in, 
                         msc->ep_size_in);
 
        msc->bot.state = BOT_DATA_IN_WAIT;
        break;
 
    case BOT_DATA_IN_WAIT:
        urb_status = usbh_urbstate_get((usb_core_driver *)(puhost->data), msc->pipe_in);
 
        /* BOT DATA IN stage */
        if (URB_DONE == urb_status) {
            if (msc->bot.cbw.field.dCBWDataTransferLength > msc->ep_size_in) {
                msc->bot.pbuf += msc->ep_size_in;
                msc->bot.cbw.field.dCBWDataTransferLength -= msc->ep_size_in;
            } else {
                msc->bot.cbw.field.dCBWDataTransferLength = 0U;
            }
 
            if (msc->bot.cbw.field.dCBWDataTransferLength > 0U) {
                usbh_data_recev ((usb_core_driver *)(puhost->data), 
                                 msc->bot.pbuf, 
                                 msc->pipe_in, 
                                 msc->ep_size_in);
            } else {
                msc->bot.state = BOT_RECEIVE_CSW;
            }
        } else if(URB_STALL == urb_status) {
            /* this is data stage stall condition */
            msc->bot.state = BOT_ERROR_IN;
        } else {
            /* no operation */
        }
        break;
 
    case BOT_DATA_OUT:
        usbh_data_send ((usb_core_driver *)(puhost->data),
                        msc->bot.pbuf, 
                        msc->pipe_out, 
                        msc->ep_size_out);
 
        msc->bot.state = BOT_DATA_OUT_WAIT;
        break;
 
    case BOT_DATA_OUT_WAIT:
        /* BOT DATA OUT stage */
        urb_status = usbh_urbstate_get((usb_core_driver *)(puhost->data), msc->pipe_out);
        if (URB_DONE == urb_status) {
            if (msc->bot.cbw.field.dCBWDataTransferLength > msc->ep_size_out) {
                msc->bot.pbuf += msc->ep_size_out;
                msc->bot.cbw.field.dCBWDataTransferLength -= msc->ep_size_out;
            }  else {
                msc->bot.cbw.field.dCBWDataTransferLength = 0; /* reset this value and keep in same state */
            }
 
            if (msc->bot.cbw.field.dCBWDataTransferLength > 0) {
                usbh_data_send ((usb_core_driver *)(puhost->data),
                                msc->bot.pbuf, 
                                msc->pipe_out, 
                                msc->ep_size_out);
            } else {
                msc->bot.state = BOT_RECEIVE_CSW;
            }
        } else if (URB_NOTREADY == urb_status) {
            msc->bot.state = BOT_DATA_OUT;
        } else if (URB_STALL == urb_status) {
            msc->bot.state = BOT_ERROR_OUT;
        } else {
            /* no operation */
        }
        break;
 
    case BOT_RECEIVE_CSW:
        /* BOT CSW stage */
        usbh_data_recev ((usb_core_driver *)(puhost->data),
                         msc->bot.csw.CSWArray, 
                         msc->pipe_in, 
                         BBB_CSW_LENGTH);
 
        msc->bot.state = BOT_RECEIVE_CSW_WAIT;
        break;
 
    case BOT_RECEIVE_CSW_WAIT:
        urb_status = usbh_urbstate_get((usb_core_driver *)(puhost->data), msc->pipe_in);
 
        /* decode CSW */
        if (URB_DONE == urb_status) {
            msc->bot.state = BOT_SEND_CBW;
            msc->bot.cmd_state = BOT_CMD_SEND;
 
            csw_status = usbh_msc_csw_decode(puhost);
            if (BOT_CSW_CMD_PASSED == csw_status) {
                status = USBH_OK;
            } else {
                status = USBH_FAIL;
            }
        } else if (URB_STALL == urb_status) {
            msc->bot.state = BOT_ERROR_IN;
        } else {
            /* no operation */
        }
        break;
 
    case BOT_ERROR_IN: 
        error = usbh_msc_bot_abort(puhost, USBH_MSC_DIR_IN);
 
        if (USBH_OK == error) {
            msc->bot.state = BOT_RECEIVE_CSW;
        } else if (USBH_UNRECOVERED_ERROR == status) {
            /* this means that there is a stall error limit, do reset recovery */
            msc->bot.state = BOT_UNRECOVERED_ERROR;
        } else {
            /* no operation */
        }
        break;
 
    case BOT_ERROR_OUT: 
        status = usbh_msc_bot_abort (puhost, USBH_MSC_DIR_OUT);
 
        if (USBH_OK == status) {
            uint8_t toggle = usbh_pipe_toggle_get((usb_core_driver *)(puhost->data), msc->pipe_out);
            usbh_pipe_toggle_set((usb_core_driver *)(puhost->data), msc->pipe_out, 1U - toggle);
            usbh_pipe_toggle_set((usb_core_driver *)(puhost->data), msc->pipe_in, 0U);
            msc->bot.state = BOT_ERROR_IN;
        } else {
            if (USBH_UNRECOVERED_ERROR == status) {
                msc->bot.state = BOT_UNRECOVERED_ERROR;
            }
        }
        break;
 
    case BOT_UNRECOVERED_ERROR:
        status = usbh_msc_bot_reset(puhost);
        if (USBH_OK == status) {
            msc->bot.state = BOT_SEND_CBW;
        }
        break;
 
    default:
        break;
    }
 
    return status;
}
 
/*!
    \brief      manages the different error handling for stall
    \param[in]  puhost: pointer to usb host handler
    \param[in]  direction: data IN or OUT
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_msc_bot_abort (usbh_host *puhost, uint8_t direction)
{
    usbh_status status = USBH_BUSY;
    usbh_msc_handler *msc = (usbh_msc_handler *)puhost->active_class->class_data;
 
    switch (direction) {
    case USBH_MSC_DIR_IN :
        /* send clrfeture command on bulk IN endpoint */
        status = usbh_clrfeature(puhost,
                                 msc->ep_in,
                                 msc->pipe_in);
        break;
 
    case USBH_MSC_DIR_OUT :
        /*send clrfeature command on bulk OUT endpoint */
        status = usbh_clrfeature(puhost,
                                 msc->ep_out,
                                 msc->pipe_out);
        break;
 
    default:
        break;
    }
 
    return status;
}
 
/*!
    \brief      reset msc bot transfer
    \param[in]  puhost: pointer to usb host handler
    \param[out] none
    \retval     operation status
*/
usbh_status usbh_msc_bot_reset (usbh_host *puhost)
{
    usbh_status status = USBH_BUSY;
 
    if (CTL_IDLE == puhost->control.ctl_state) {
        /*
        puhost->control.setup.req = (usb_req) {
            .bmRequestType = USB_TRX_OUT | USB_REQTYPE_CLASS | USB_RECPTYPE_ITF,
            .bRequest      = BBB_RESET,
            .wValue        = 0U,
            .wIndex        = 0U,
            .wLength       = 0U
        };
        */
        {
            puhost->control.setup.req.bmRequestType = USB_TRX_OUT | USB_REQTYPE_CLASS | USB_RECPTYPE_ITF;
            puhost->control.setup.req.bRequest      = BBB_RESET;
            puhost->control.setup.req.wValue        = 0U;
            puhost->control.setup.req.wIndex        = 0U;
            puhost->control.setup.req.wLength       = 0U;
        }
 
        usbh_ctlstate_config (puhost, NULL, 0U);
    } 
 
    status = usbh_ctl_handler (puhost);
 
    return status;
}
 
/*!
    \brief      decode the CSW received by the device and updates the same to upper layer
    \param[in]  puhost: pointer to usb host
    \param[out] none
    \retval     on success USBH_MSC_OK, on failure USBH_MSC_FAIL
    \notes
          Refer to USB Mass-Storage Class: BOT (www.usb.org)
          6.3.1 Valid CSW Conditions :
          The host shall consider the CSW valid when:
          1. dCSWSignature is equal to 53425355h
          2. the CSW is 13 (Dh) bytes in length,
          3. dCSWTag matches the dCBWTag from the corresponding CBW.
*/
bot_csw_status usbh_msc_csw_decode (usbh_host *puhost)
{
    bot_csw_status status = BOT_CSW_CMD_FAILED;
    usbh_msc_handler *msc = (usbh_msc_handler *)puhost->active_class->class_data;
 
    /* checking if the transfer length is different than 13 */
    if (BBB_CSW_LENGTH != usbh_xfercount_get ((usb_core_driver *)(puhost->data), msc->pipe_in)) {
        status = BOT_CSW_PHASE_ERROR;
    } else {
        /* CSW length is correct */
 
        /* check validity of the CSW Signature and CSWStatus */
        if (BBB_CSW_SIGNATURE == msc->bot.csw.field.dCSWSignature) {
            /* check condition 1. dCSWSignature is equal to 53425355h */
            if (msc->bot.csw.field.dCSWTag == msc->bot.cbw.field.dCBWTag) {
                /* check condition 3. dCSWTag matches the dCBWTag from the corresponding CBW */
                if (0U == msc->bot.csw.field.bCSWStatus) {
                    status = BOT_CSW_CMD_PASSED;
                } else if (1U == msc->bot.csw.field.bCSWStatus) {
                    status = BOT_CSW_CMD_FAILED;
                } else if (2U == msc->bot.csw.field.bCSWStatus) {
                    status = BOT_CSW_PHASE_ERROR;
                } else {
                    /* no operation */
                }
            }
        } else {
            /* If the CSW signature is not valid, we will return the phase error to
               upper layers for reset recovery */
            status = BOT_CSW_PHASE_ERROR;
        }
    }
 
    return status;
}