GD32F1x0: USB/GD32_USB_Device_Library/Class/msc/src/usbd_msc_bot.c Source File

GD32F1x0

usbd_msc_bot.c
Go to the documentation of this file.
1 
12 /* Includes ------------------------------------------------------------------*/
13 #include "usbd_msc_bot.h"
14 
15 /* Private variables ---------------------------------------------------------*/
16 uint16_t MSC_BBB_DataLen;
17 uint8_t MSC_BBB_State;
18 uint8_t MSC_BBB_Status;
19 uint8_t MSC_BBB_Data[MSC_MEDIA_PACKET_SIZE];
20 
21 MSC_BBB_CBW_TypeDef MSC_BBB_cbw;
22 MSC_BBB_CSW_TypeDef MSC_BBB_csw;
23 
24 /* Private function prototypes -----------------------------------------------*/
25 static void MSC_BBB_CBW_Decode (USB_DEVICE_HANDLE *pudev);
26 
27 static void MSC_BBB_SendData (USB_DEVICE_HANDLE *pudev,
28  uint8_t *pbuf,
29  uint16_t Len);
30 
31 static void MSC_BBB_Abort (USB_DEVICE_HANDLE *pudev);
32 
33 /* Private functions ---------------------------------------------------------*/
40 {
41  MSC_BBB_State = BBB_IDLE;
42  MSC_BBB_Status = BBB_STATUS_NORMAL;
43 
44  USBD_STORAGE_fops->Init(0);
45 
46  /* Prepare endpoint to receive first BBB CBW */
47  USB_EP_Rx (pudev,
48  MSC_OUT_EP,
49  (uint8_t *)&MSC_BBB_cbw,
50  BBB_CBW_LENGTH);
51 }
52 
59 {
60  MSC_BBB_State = BBB_IDLE;
61  MSC_BBB_Status = BBB_STATUS_RECOVERY;
62 
63  USB_EP_Rx (pudev,
64  MSC_OUT_EP,
65  (uint8_t *)&MSC_BBB_cbw,
66  BBB_CBW_LENGTH);
67 }
68 
75 {
76  MSC_BBB_State = BBB_IDLE;
77 }
78 
85 void MSC_BBB_DataIn (USB_DEVICE_HANDLE *pudev, uint8_t EpID)
86 {
87  switch (MSC_BBB_State)
88  {
89  case BBB_DATA_IN:
90  SCSI_ProcessRead(MSC_BBB_cbw.bCBWLUN);
91  break;
92 
93  case BBB_SEND_DATA:
94  case BBB_LAST_DATA_IN:
95  MSC_BBB_SendCSW (pudev, CSW_CMD_PASSED);
96  break;
97 
98  default:
99  break;
100  }
101 }
108 void MSC_BBB_DataOut (USB_DEVICE_HANDLE *pudev, uint8_t EpID)
109 {
110  switch (MSC_BBB_State)
111  {
112  case BBB_IDLE:
113  MSC_BBB_CBW_Decode(pudev);
114  break;
115 
116  case BBB_DATA_OUT:
117  SCSI_ProcessWrite(MSC_BBB_cbw.bCBWLUN);
118  break;
119 
120  default:
121  break;
122  }
123 }
124 
130 static void MSC_BBB_CBW_Decode (USB_DEVICE_HANDLE *pudev)
131 {
132  uint16_t RxLen = pudev->dev.out_ep[MSC_OUT_EP].xfer_count;
133 
134  MSC_BBB_csw.dCSWTag = MSC_BBB_cbw.dCBWTag;
135  MSC_BBB_csw.dCSWDataResidue = MSC_BBB_cbw.dCBWDataTransferLength;
136 
137  if ((RxLen != BBB_CBW_LENGTH) ||
138  (MSC_BBB_cbw.dCBWSignature != BBB_CBW_SIGNATURE) ||
139  (MSC_BBB_cbw.bCBWLUN > 1) ||
140  (MSC_BBB_cbw.bCBWCBLength < 1) ||
141  (MSC_BBB_cbw.bCBWCBLength > 16))
142  {
143  SCSI_SenseCode(MSC_BBB_cbw.bCBWLUN,
144  ILLEGAL_REQUEST,
145  INVALID_CDB);
146 
147  MSC_BBB_Status = BBB_STATUS_ERROR;
148  MSC_BBB_Abort(pudev);
149  }
150  else
151  {
152  if(SCSI_ProcessCmd(pudev,
153  MSC_BBB_cbw.bCBWLUN,
154  &MSC_BBB_cbw.CBWCB[0]) < 0)
155  {
156  MSC_BBB_Abort(pudev);
157  }
158  else if ((MSC_BBB_State != BBB_DATA_IN) &&
159  (MSC_BBB_State != BBB_DATA_OUT) &&
160  (MSC_BBB_State != BBB_LAST_DATA_IN))
161  {
162  if(MSC_BBB_DataLen > 0)
163  {
164  MSC_BBB_SendData(pudev,
165  MSC_BBB_Data,
166  MSC_BBB_DataLen);
167  }
168  else if (MSC_BBB_DataLen == 0)
169  {
170  MSC_BBB_SendCSW (pudev, CSW_CMD_PASSED);
171  }
172  }
173  }
174 }
175 
183 static void MSC_BBB_SendData (USB_DEVICE_HANDLE *pudev,
184  uint8_t* buf,
185  uint16_t Len)
186 {
187  Len = MIN(MSC_BBB_cbw.dCBWDataTransferLength, Len);
188 
189  MSC_BBB_csw.dCSWDataResidue -= Len;
190  MSC_BBB_csw.bCSWStatus = CSW_CMD_PASSED;
191  MSC_BBB_State = BBB_SEND_DATA;
192 
193  USB_EP_Tx(pudev, MSC_IN_EP, buf, Len);
194 }
195 
202 void MSC_BBB_SendCSW (USB_DEVICE_HANDLE *pudev, uint8_t CSW_Status)
203 {
204  MSC_BBB_csw.dCSWSignature = BBB_CSW_SIGNATURE;
205  MSC_BBB_csw.bCSWStatus = CSW_Status;
206  MSC_BBB_State = BBB_IDLE;
207 
208  USB_EP_Tx(pudev,
209  MSC_IN_EP,
210  (uint8_t *)&MSC_BBB_csw,
211  BBB_CSW_LENGTH);
212 
213  USB_EP_Rx(pudev,
214  MSC_OUT_EP,
215  (uint8_t *)&MSC_BBB_cbw,
216  BBB_CBW_LENGTH);
217 }
218 
224 static void MSC_BBB_Abort (USB_DEVICE_HANDLE *pudev)
225 {
226  if ((MSC_BBB_cbw.bmCBWFlags == 0) &&
227  (MSC_BBB_cbw.dCBWDataTransferLength != 0) &&
228  (MSC_BBB_Status == BBB_STATUS_NORMAL))
229  {
230  USB_EP_Stall(pudev, MSC_OUT_EP);
231  }
232 
233  USB_EP_Stall(pudev, MSC_IN_EP);
234 
235  if(MSC_BBB_Status == BBB_STATUS_ERROR)
236  {
237  USB_EP_Rx (pudev,
238  MSC_OUT_EP,
239  (uint8_t *)&MSC_BBB_cbw,
240  BBB_CBW_LENGTH);
241  }
242 }
243 
251 void MSC_BBB_CplClrFeature (USB_DEVICE_HANDLE *pudev, uint8_t EpID)
252 {
253  if(MSC_BBB_Status == BBB_STATUS_ERROR)/* Bad CBW Signature */
254  {
255  USB_EP_Stall(pudev, MSC_IN_EP);
256  MSC_BBB_Status = BBB_STATUS_NORMAL;
257  }
258  else if(((EpID & 0x80) == 0x80) && (MSC_BBB_Status != BBB_STATUS_RECOVERY))
259  {
260  MSC_BBB_SendCSW (pudev, CSW_CMD_FAILED);
261  }
262 }
263 
264 /************************ (C) COPYRIGHT 2014 GIGADEVICE *****END OF FILE****/
int8_t SCSI_ProcessWrite(uint8_t Lun)
Handle write process.
void USB_EP_Tx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to transmit data.
Definition: usb_core.c:376
void MSC_BBB_DataIn(USB_DEVICE_HANDLE *pudev, uint8_t EpID)
Handle BBB IN data stage.
Definition: usbd_msc_bot.c:85
void SCSI_SenseCode(uint8_t Lun, uint8_t SKey, uint8_t ASC)
Load the last error code in the error list.
void MSC_BBB_DataOut(USB_DEVICE_HANDLE *pudev, uint8_t EpID)
Handle BBB OUT data stage.
Definition: usbd_msc_bot.c:108
void MSC_BBB_Reset(USB_DEVICE_HANDLE *pudev)
Reset the BBB machine.
Definition: usbd_msc_bot.c:58
int8_t SCSI_ProcessRead(uint8_t Lun)
Handle Read Process.
void MSC_BBB_SendCSW(USB_DEVICE_HANDLE *pudev, uint8_t CSW_Status)
Send the CSW(command status wrapper)
Definition: usbd_msc_bot.c:202
void MSC_BBB_CplClrFeature(USB_DEVICE_HANDLE *pudev, uint8_t EpID)
Complete the clear feature request.
Definition: usbd_msc_bot.c:251
void MSC_BBB_DeInit(USB_DEVICE_HANDLE *pudev)
De-Initialize the BBB Machine.
Definition: usbd_msc_bot.c:74
void MSC_BBB_Init(USB_DEVICE_HANDLE *pudev)
Initialize the BBB process.
Definition: usbd_msc_bot.c:39
void USB_EP_Stall(USB_CORE_HANDLE *pudev, uint8_t EpID)
Set an endpoint to STALL status.
Definition: usb_core.c:438
int8_t SCSI_ProcessCmd(USB_DEVICE_HANDLE *pudev, uint8_t Lun, uint8_t *cmd)
Process SCSI commands.
Definition: usbd_msc_scsi.c:57
Header file for the usbd_msc_bot.c file.
void USB_EP_Rx(USB_CORE_HANDLE *pudev, uint8_t EpAddr, uint8_t *pbuf, uint16_t BufLen)
Endpoint prepare to receive data.
Definition: usb_core.c:336
Generated by   doxygen 1.8.10