STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
usbh_msc_scsi.c
Go to the documentation of this file.
1 
28 /* Includes ------------------------------------------------------------------*/
29 #include "usbh_msc.h"
30 #include "usbh_msc_scsi.h"
31 #include "usbh_msc_bot.h"
32 
33 
105  uint8_t lun)
106 {
107  USBH_StatusTypeDef error = USBH_FAIL ;
108  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
109 
110  switch(MSC_Handle->hbot.cmd_state)
111  {
112  case BOT_CMD_SEND:
113 
114  /*Prepare the CBW and relevent field*/
116  MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT;
117  MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH;
118 
119  USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH);
120  MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_TEST_UNIT_READY;
121 
122  MSC_Handle->hbot.state = BOT_SEND_CBW;
123  MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT;
124  error = USBH_BUSY;
125  break;
126 
127  case BOT_CMD_WAIT:
128  error = USBH_MSC_BOT_Process(phost, lun);
129  break;
130 
131  default:
132  break;
133  }
134 
135  return error;
136 }
137 
147  uint8_t lun,
148  SCSI_CapacityTypeDef *capacity)
149 {
150  USBH_StatusTypeDef error = USBH_BUSY ;
151  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
152 
153  switch(MSC_Handle->hbot.cmd_state)
154  {
155  case BOT_CMD_SEND:
156 
157  /*Prepare the CBW and relevent field*/
159  MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN;
160  MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH;
161 
162  USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH);
163  MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ_CAPACITY10;
164 
165  MSC_Handle->hbot.state = BOT_SEND_CBW;
166 
167  MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT;
168  MSC_Handle->hbot.pbuf = (uint8_t *)MSC_Handle->hbot.data;
169  error = USBH_BUSY;
170  break;
171 
172  case BOT_CMD_WAIT:
173 
174  error = USBH_MSC_BOT_Process(phost, lun);
175 
176  if(error == USBH_OK)
177  {
178  /*assign the capacity*/
179  capacity->block_nbr = MSC_Handle->hbot.pbuf[3] | (MSC_Handle->hbot.pbuf[2] << 8) |\
180  (MSC_Handle->hbot.pbuf[1] << 16) | (MSC_Handle->hbot.pbuf[0] << 24);
181 
182  /*assign the page length*/
183  capacity->block_size = MSC_Handle->hbot.pbuf[7] | (MSC_Handle->hbot.pbuf[6] << 8);
184  }
185  break;
186 
187  default:
188  break;
189  }
190 
191  return error;
192 }
193 
203  uint8_t lun,
205 {
206  USBH_StatusTypeDef error = USBH_FAIL ;
207  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
208  switch(MSC_Handle->hbot.cmd_state)
209  {
210  case BOT_CMD_SEND:
211 
212  /*Prepare the CBW and relevent field*/
214  MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN;
215  MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH;
216 
217  USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_LENGTH);
218  MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_INQUIRY;
219  MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5);
220  MSC_Handle->hbot.cbw.field.CB[2] = 0;
221  MSC_Handle->hbot.cbw.field.CB[3] = 0;
222  MSC_Handle->hbot.cbw.field.CB[4] = 0x24;
223  MSC_Handle->hbot.cbw.field.CB[5] = 0;
224 
225  MSC_Handle->hbot.state = BOT_SEND_CBW;
226 
227  MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT;
228  MSC_Handle->hbot.pbuf = (uint8_t *)MSC_Handle->hbot.data;
229  error = USBH_BUSY;
230  break;
231 
232  case BOT_CMD_WAIT:
233 
234  error = USBH_MSC_BOT_Process(phost, lun);
235 
236  if(error == USBH_OK)
237  {
238  USBH_memset(inquiry, 0, sizeof(SCSI_StdInquiryDataTypeDef));
239  /*assign Inquiry Data */
240  inquiry->DeviceType = MSC_Handle->hbot.pbuf[0] & 0x1F;
241  inquiry->PeripheralQualifier = MSC_Handle->hbot.pbuf[0] >> 5;
242  inquiry->RemovableMedia = (MSC_Handle->hbot.pbuf[1] & 0x80)== 0x80;
243  USBH_memcpy (inquiry->vendor_id, &MSC_Handle->hbot.pbuf[8], 8);
244  USBH_memcpy (inquiry->product_id, &MSC_Handle->hbot.pbuf[16], 16);
245  USBH_memcpy (inquiry->revision_id, &MSC_Handle->hbot.pbuf[32], 4);
246  }
247  break;
248 
249  default:
250  break;
251  }
252 
253  return error;
254 }
255 
265  uint8_t lun,
266  SCSI_SenseTypeDef *sense_data)
267 {
268  USBH_StatusTypeDef error = USBH_FAIL ;
269  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
270 
271  switch(MSC_Handle->hbot.cmd_state)
272  {
273  case BOT_CMD_SEND:
274 
275  /*Prepare the CBW and relevent field*/
277  MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN;
278  MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH;
279 
280  USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH);
281  MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_REQUEST_SENSE;
282  MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5);
283  MSC_Handle->hbot.cbw.field.CB[2] = 0;
284  MSC_Handle->hbot.cbw.field.CB[3] = 0;
285  MSC_Handle->hbot.cbw.field.CB[4] = DATA_LEN_REQUEST_SENSE;
286  MSC_Handle->hbot.cbw.field.CB[5] = 0;
287 
288  MSC_Handle->hbot.state = BOT_SEND_CBW;
289  MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT;
290  MSC_Handle->hbot.pbuf = (uint8_t *)MSC_Handle->hbot.data;
291  error = USBH_BUSY;
292  break;
293 
294  case BOT_CMD_WAIT:
295 
296  error = USBH_MSC_BOT_Process(phost, lun);
297 
298  if(error == USBH_OK)
299  {
300  sense_data->key = MSC_Handle->hbot.pbuf[2] & 0x0F;
301  sense_data->asc = MSC_Handle->hbot.pbuf[12];
302  sense_data->ascq = MSC_Handle->hbot.pbuf[13];
303  }
304  break;
305 
306  default:
307  break;
308  }
309 
310  return error;
311 }
312 
324  uint8_t lun,
325  uint32_t address,
326  uint8_t *pbuf,
327  uint32_t length)
328 {
329  USBH_StatusTypeDef error = USBH_FAIL ;
330 
331  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
332 
333  switch(MSC_Handle->hbot.cmd_state)
334  {
335  case BOT_CMD_SEND:
336 
337  /*Prepare the CBW and relevent field*/
338  MSC_Handle->hbot.cbw.field.DataTransferLength = length * 512;
339  MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT;
340  MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH;
341 
342  USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH);
343  MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_WRITE10;
344 
345  /*logical block address*/
346  MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)&address)[3]);
347  MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)&address)[2]);
348  MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)&address)[1]);
349  MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)&address)[0]);
350 
351 
352  /*Transfer length */
353  MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)&length)[1]) ;
354  MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)&length)[0]) ;
355 
356 
357  MSC_Handle->hbot.state = BOT_SEND_CBW;
358  MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT;
359  MSC_Handle->hbot.pbuf = pbuf;
360  error = USBH_BUSY;
361  break;
362 
363  case BOT_CMD_WAIT:
364  error = USBH_MSC_BOT_Process(phost, lun);
365  break;
366 
367  default:
368  break;
369  }
370 
371  return error;
372 }
373 
385  uint8_t lun,
386  uint32_t address,
387  uint8_t *pbuf,
388  uint32_t length)
389 {
390  USBH_StatusTypeDef error = USBH_FAIL ;
391  MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData;
392 
393  switch(MSC_Handle->hbot.cmd_state)
394  {
395  case BOT_CMD_SEND:
396 
397  /*Prepare the CBW and relevent field*/
398  MSC_Handle->hbot.cbw.field.DataTransferLength = length * 512;
399  MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN;
400  MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH;
401 
402  USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH);
403  MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ10;
404 
405  /*logical block address*/
406  MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)&address)[3]);
407  MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)&address)[2]);
408  MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)&address)[1]);
409  MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)&address)[0]);
410 
411 
412  /*Transfer length */
413  MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)&length)[1]) ;
414  MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)&length)[0]) ;
415 
416 
417  MSC_Handle->hbot.state = BOT_SEND_CBW;
418  MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT;
419  MSC_Handle->hbot.pbuf = pbuf;
420  error = USBH_BUSY;
421  break;
422 
423  case BOT_CMD_WAIT:
424  error = USBH_MSC_BOT_Process(phost, lun);
425  break;
426 
427  default:
428  break;
429  }
430 
431  return error;
432 }
433 
434 
455 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
456 
457 
458 
#define DATA_LEN_INQUIRY
Definition: usbh_msc_scsi.h:97
#define USBH_memset
BOT_CBWTypeDef cbw
Definition: usbh_msc_bot.h:137
Header file for usbh_msc_bot.c.
#define CBW_LENGTH
USBH_StatusTypeDef USBH_MSC_BOT_Process(USBH_HandleTypeDef *phost, uint8_t lun)
USBH_MSC_BOT_Process The function handle the BOT protocol.
Definition: usbh_msc_bot.c:173
#define OPCODE_READ10
Definition: usbh_msc_scsi.h:90
#define OPCODE_INQUIRY
Definition: usbh_msc_scsi.h:93
#define CBW_CB_LENGTH
This file contains all the prototypes for the usbh_msc.c.
uint32_t data[16]
Definition: usbh_msc_bot.h:133
USBH_StatusTypeDef USBH_MSC_SCSI_Inquiry(USBH_HandleTypeDef *phost, uint8_t lun, SCSI_StdInquiryDataTypeDef *inquiry)
USBH_MSC_SCSI_Inquiry Issue Inquiry command.
BOT_StateTypeDef state
Definition: usbh_msc_bot.h:134
USBH_StatusTypeDef USBH_MSC_SCSI_Write(USBH_HandleTypeDef *phost, uint8_t lun, uint32_t address, uint8_t *pbuf, uint32_t length)
USBH_MSC_SCSI_Write Issue write10 command.
Definition: pbuf.h:108
#define USBH_memcpy
#define USB_EP_DIR_OUT
Definition: usbh_def.h:175
Header file for usbh_msc_scsi.c.
BOT_CMDStateTypeDef cmd_state
Definition: usbh_msc_bot.h:136
#define OPCODE_READ_CAPACITY10
Definition: usbh_msc_scsi.h:89
USBH_StatusTypeDef USBH_MSC_SCSI_TestUnitReady(USBH_HandleTypeDef *phost, uint8_t lun)
USBH_MSC_SCSI_TestUnitReady Issue TestUnitReady command.
BOT_HandleTypeDef hbot
Definition: usbh_msc.h:129
#define OPCODE_REQUEST_SENSE
Definition: usbh_msc_scsi.h:92
USBH_StatusTypeDef USBH_MSC_SCSI_Read(USBH_HandleTypeDef *phost, uint8_t lun, uint32_t address, uint8_t *pbuf, uint32_t length)
USBH_MSC_SCSI_Read Issue Read10 command.
USBH_StatusTypeDef USBH_MSC_SCSI_RequestSense(USBH_HandleTypeDef *phost, uint8_t lun, SCSI_SenseTypeDef *sense_data)
USBH_MSC_SCSI_RequestSense Issue RequestSense command.
USBH_StatusTypeDef
Definition: usbh_def.h:302
#define USB_EP_DIR_IN
Definition: usbh_def.h:176
#define OPCODE_TEST_UNIT_READY
Definition: usbh_msc_scsi.h:88
struct BOT_CBWTypeDef::__CBW field
USBH_StatusTypeDef USBH_MSC_SCSI_ReadCapacity(USBH_HandleTypeDef *phost, uint8_t lun, SCSI_CapacityTypeDef *capacity)
USBH_MSC_SCSI_ReadCapacity Issue Read Capacity command.
#define OPCODE_WRITE10
Definition: usbh_msc_scsi.h:91
USBH_ClassTypeDef * pActiveClass
Definition: usbh_def.h:458
#define DATA_LEN_READ_CAPACITY10
Definition: usbh_msc_scsi.h:96
#define DATA_LEN_MODE_TEST_UNIT_READY
Definition: usbh_msc_scsi.h:95
#define DATA_LEN_REQUEST_SENSE
Definition: usbh_msc_scsi.h:98