1 /*!
2     \file    usbh_msc_bbb.h
3     \brief   header file for usbh_msc_bbb.c
4 
5     \version 2020-08-04, V1.1.0, firmware for GD32VF103
6 */
7 
8 /*
9     Copyright (c) 2020, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #ifndef __USBH_MSC_BBB_H
36 #define __USBH_MSC_BBB_H
37 
38 #include "usbh_enum.h"
39 #include "msc_bbb.h"
40 
41 typedef union {
42     msc_bbb_cbw field;
43 
44     uint8_t CBWArray[31];
45 }usbh_cbw_pkt;
46 
47 typedef union {
48     msc_bbb_csw field;
49 
50     uint8_t CSWArray[13];
51 }usbh_csw_pkt;
52 
53 enum usbh_msc_state {
54     USBH_MSC_BOT_INIT_STATE = 0U,
55     USBH_MSC_BOT_RESET,
56     USBH_MSC_GET_MAX_LUN,
57     USBH_MSC_TEST_UNIT_READY,
58     USBH_MSC_READ_CAPACITY10,
59     USBH_MSC_MODE_SENSE6,
60     USBH_MSC_REQUEST_SENSE,
61     USBH_MSC_BOT_USB_TRANSFERS,
62     USBH_MSC_DEFAULT_APPLI_STATE,
63     USBH_MSC_CTRL_ERROR_STATE,
64     USBH_MSC_UNRECOVERED_STATE
65 };
66 
67 typedef enum
68 {
69     BOT_OK = 0U,
70     BOT_FAIL,
71     BOT_PHASE_ERROR,
72     BOT_BUSY
73 } bot_status;
74 
75 typedef enum
76 {
77     BOT_CMD_IDLE = 0U,
78     BOT_CMD_SEND,
79     BOT_CMD_WAIT,
80 } bot_cmd_state;
81 
82 /* csw status definitions */
83 typedef enum
84 {
85     BOT_CSW_CMD_PASSED = 0U,
86     BOT_CSW_CMD_FAILED,
87     BOT_CSW_PHASE_ERROR,
88 } bot_csw_status;
89 
90 typedef enum
91 {
92     BOT_SEND_CBW = 1U,
93     BOT_SEND_CBW_WAIT,
94     BOT_DATA_IN,
95     BOT_DATA_IN_WAIT,
96     BOT_DATA_OUT,
97     BOT_DATA_OUT_WAIT,
98     BOT_RECEIVE_CSW,
99     BOT_RECEIVE_CSW_WAIT,
100     BOT_ERROR_IN,
101     BOT_ERROR_OUT,
102     BOT_UNRECOVERED_ERROR
103 } bot_state;
104 
105 typedef struct
106 {
107     uint8_t                *pbuf;
108     uint32_t                data[16];
109     bot_state               state;
110     bot_state               prev_state;
111     bot_cmd_state           cmd_state;
112     usbh_cbw_pkt            cbw;
113     usbh_csw_pkt            csw;
114 } bot_handle;
115 
116 #define USBH_MSC_BOT_CBW_TAG                0x20304050U
117 
118 #define USBH_MSC_CSW_MAX_LENGTH             63U
119 
120 #define USBH_MSC_SEND_CSW_DISABLE           0U
121 #define USBH_MSC_SEND_CSW_ENABLE            1U
122 
123 #define USBH_MSC_DIR_IN                     0U
124 #define USBH_MSC_DIR_OUT                    1U
125 #define USBH_MSC_BOTH_DIR                   2U
126 
127 #define USBH_MSC_PAGE_LENGTH                512U
128 
129 #define CBW_CB_LENGTH                       16U
130 #define CBW_LENGTH                          10U
131 #define CBW_LENGTH_TEST_UNIT_READY          0U
132 
133 #define MAX_BULK_STALL_COUNT_LIMIT          0x04U   /*!< If STALL is seen on Bulk
134                                                       Endpoint continously, this means
135                                                       that device and Host has phase error
136                                                       Hence a Reset is needed */
137 
138 /* function declarations */
139 /* initialize the mass storage parameters */
140 void usbh_msc_bot_init (usbh_host *puhost);
141 /* manage the different states of BOT transfer and updates the status to upper layer */
142 usbh_status usbh_msc_bot_process (usbh_host *puhost, uint8_t lun);
143 /* manages the different error handling for stall */
144 usbh_status usbh_msc_bot_abort (usbh_host *puhost, uint8_t direction);
145 /* reset msc bot request struct */
146 usbh_status usbh_msc_bot_reset (usbh_host *puhost);
147 /* decode the CSW received by the device and updates the same to upper layer */
148 bot_csw_status usbh_msc_csw_decode (usbh_host *puhost);
149 
150 #endif /* __USBH_MSC_BBB_H */
151