/**
* @brief  MSC_BOT_DataIn
*         Handle BOT IN data stage
* @param  pdev: device instance
* @param  epnum: endpoint index
* @retval None
*/
void MSC_BOT_DataIn (USBD_HandleTypeDef  *pdev, 
                     uint8_t epnum)
{
  USBD_MSC_BOT_HandleTypeDef  *hmsc = pdev->pClassData;  
  
  switch (hmsc->bot_state)
  {
  case USBD_BOT_DATA_IN:
    if(SCSI_ProcessCmd(pdev,
                        hmsc->cbw.bLUN,
                        &hmsc->cbw.CB[0]) < 0)
    {
      MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED);
    }
    break;
    
  case USBD_BOT_SEND_DATA:
  case USBD_BOT_LAST_DATA_IN:
    MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED);
    
    break;
    
  default:
    break;
  }
}
Exemple #2
0
/**
* @brief  MSC_BOT_DataIn
*         Handle BOT IN data stage
* @param  pdev: device instance
* @param  epnum: endpoint index
* @retval None
*/
void MSC_BOT_DataIn (USB_OTG_CORE_HANDLE  *pdev,
                     uint8_t epnum)
{

  switch (MSC_BOT_State)
  {
  case BOT_DATA_IN:
    if(SCSI_ProcessCmd(pdev,
                        MSC_BOT_cbw.bLUN,
                        &MSC_BOT_cbw.CB[0]) < 0)
    {
      MSC_BOT_SendCSW (pdev, CSW_CMD_FAILED);
    }
    break;

  case BOT_SEND_DATA:
  case BOT_LAST_DATA_IN:
    MSC_BOT_SendCSW (pdev, CSW_CMD_PASSED);

    break;

  default:
    break;
  }
}
/**
* @brief  MSC_BOT_CBW_Decode
*         Decode the CBW command and set the BOT state machine accordingtly  
* @param  pdev: device instance
* @retval None
*/
static void  MSC_BOT_CBW_Decode (USBD_HandleTypeDef  *pdev)
{
  USBD_MSC_BOT_HandleTypeDef  *hmsc = pdev->pClassData;  
  
  hmsc->csw.dTag = hmsc->cbw.dTag;
  hmsc->csw.dDataResidue = hmsc->cbw.dDataLength;
  
  if ((USBD_LL_GetRxDataSize (pdev ,MSC_EPOUT_ADDR) != USBD_BOT_CBW_LENGTH) ||
      (hmsc->cbw.dSignature != USBD_BOT_CBW_SIGNATURE)||
        (hmsc->cbw.bLUN > 1) || 
          (hmsc->cbw.bCBLength < 1) || 
            (hmsc->cbw.bCBLength > 16))
  {
    
    SCSI_SenseCode(pdev,
                   hmsc->cbw.bLUN, 
                   ILLEGAL_REQUEST, 
                   INVALID_CDB);
    
    hmsc->bot_status = USBD_BOT_STATUS_ERROR;   
    MSC_BOT_Abort(pdev);
 
  }
  else
  {
    if(SCSI_ProcessCmd(pdev,
                       hmsc->cbw.bLUN,
                       &hmsc->cbw.CB[0]) < 0)
    {
      if(hmsc->bot_state == USBD_BOT_NO_DATA)
      {
       MSC_BOT_SendCSW (pdev,
                         USBD_CSW_CMD_FAILED); 
      }
      else
      {
        MSC_BOT_Abort(pdev);
      }
    }
    /*Burst xfer handled internally*/
    else if ((hmsc->bot_state != USBD_BOT_DATA_IN) && 
             (hmsc->bot_state != USBD_BOT_DATA_OUT) &&
             (hmsc->bot_state != USBD_BOT_LAST_DATA_IN)) 
    {
      if (hmsc->bot_data_length > 0)
      {
        MSC_BOT_SendData(pdev,
                         hmsc->bot_data, 
                         hmsc->bot_data_length);
      }
      else if (hmsc->bot_data_length == 0) 
      {
        MSC_BOT_SendCSW (pdev,
                         USBD_CSW_CMD_PASSED);
      }
    }
  }
}
/**
* @brief  MSC_BOT_DataOut
*         Proccess MSC OUT data
* @param  pdev: device instance
* @param  epnum: endpoint index
* @retval None
*/
void MSC_BOT_DataOut (USBD_HandleTypeDef  *pdev, 
                      uint8_t epnum)
{
  USBD_MSC_BOT_HandleTypeDef  *hmsc = pdev->pClassData;
  
  switch (hmsc->bot_state)
  {
  case USBD_BOT_IDLE:
    MSC_BOT_CBW_Decode(pdev);
    break;
    
  case USBD_BOT_DATA_OUT:
    
    if(SCSI_ProcessCmd(pdev,
                        hmsc->cbw.bLUN,
                        &hmsc->cbw.CB[0]) < 0)
    {
      MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED);
    }

    break;
    
  default:
    break;
  }
}
Exemple #5
0
// Handle write process
// input:
//   pdev - pointer to the USB device handle
//   lun - logical unit number
static int8_t SCSI_ProcessWrite(USBD_HandleTypeDef *pdev, uint8_t lun) {
	USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef *)pdev->pClassData;
	uint32_t len;

	len = MIN(hmsc->scsi_blk_len,MSC_MEDIA_PACKET);

	if (((USBD_StorageTypeDef *)pdev->pUserData)->Write(lun,hmsc->bot_data,hmsc->scsi_blk_addr / hmsc->scsi_blk_size,len / hmsc->scsi_blk_size) < 0) {
		SCSI_SenseCode(pdev,lun,HARDWARE_ERROR,WRITE_FAULT);

		return -1;
	}

	hmsc->scsi_blk_addr += len;
	hmsc->scsi_blk_len  -= len;
  
	// case 12 : Ho = Do
	hmsc->csw.dDataResidue -= len;
	if (hmsc->scsi_blk_len == 0) {
		MSC_BOT_SendCSW(pdev,USBD_CSW_CMD_PASSED);
	} else {
		// Prepare endpoint to receive next packet
		USBD_LL_PrepareReceive(pdev,MSC_EPOUT_ADDR,hmsc->bot_data,MIN(hmsc->scsi_blk_len,MSC_MEDIA_PACKET));
	}

	return 0;
}
Exemple #6
0
/**
* @brief  MSC_BOT_DataOut
*         Proccess MSC OUT data
* @param  pdev: device instance
* @param  epnum: endpoint index
* @retval None
*/
void MSC_BOT_DataOut (USB_OTG_CORE_HANDLE  *pdev,
                      uint8_t epnum)
{
  switch (MSC_BOT_State)
  {
  case BOT_IDLE:
    MSC_BOT_CBW_Decode(pdev);
    break;

  case BOT_DATA_OUT:

    if(SCSI_ProcessCmd(pdev,
                        MSC_BOT_cbw.bLUN,
                        &MSC_BOT_cbw.CB[0]) < 0)
    {
      MSC_BOT_SendCSW (pdev, CSW_CMD_FAILED);
    }

    break;

  default:
    break;
  }

}
Exemple #7
0
void  MSC_BOT_CplClrFeature (USB_OTG_CORE_HANDLE  *pdev, uint8_t epnum) {
	if(MSC_BOT_Status == BOT_STATE_ERROR ) { /* Bad CBW Signature */
		DCD_EP_Stall(pdev, MSC_IN_EP);
		MSC_BOT_Status = BOT_STATE_NORMAL;
	} else if(((epnum & 0x80) == 0x80) && ( MSC_BOT_Status != BOT_STATE_RECOVERY)) {
		MSC_BOT_SendCSW (pdev, CSW_CMD_FAILED);
	}

}
void  MSC_BOT_CplClrFeature (USBD_HandleTypeDef  *pdev, uint8_t epnum)
{
  USBD_MSC_BOT_HandleTypeDef  *hmsc = pdev->pClassData; 
  
  if(hmsc->bot_status == USBD_BOT_STATUS_ERROR )/* Bad CBW Signature */
  {
    USBD_LL_StallEP(pdev, MSC_EPIN_ADDR);
    hmsc->bot_status = USBD_BOT_STATUS_NORMAL;    
  }
  else if(((epnum & 0x80) == 0x80) && ( hmsc->bot_status != USBD_BOT_STATUS_RECOVERY))
  {
    MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED);
  }
  
}
Exemple #9
0
/**
* @brief  MSC_BOT_CBW_Decode
*         Decode the CBW command and set the BOT state machine accordingtly
* @param  pdev: device instance
* @retval None
*/
static void  MSC_BOT_CBW_Decode (USB_OTG_CORE_HANDLE  *pdev)
{

  MSC_BOT_csw.dTag = MSC_BOT_cbw.dTag;
  MSC_BOT_csw.dDataResidue = MSC_BOT_cbw.dDataLength;

  if ((USBD_GetRxCount (pdev ,MSC_OUT_EP) != BOT_CBW_LENGTH) ||
      (MSC_BOT_cbw.dSignature != BOT_CBW_SIGNATURE)||
        (MSC_BOT_cbw.bLUN > 1) ||
          (MSC_BOT_cbw.bCBLength < 1) ||
            (MSC_BOT_cbw.bCBLength > 16))
  {

    SCSI_SenseCode(MSC_BOT_cbw.bLUN,
                   ILLEGAL_REQUEST,
                   INVALID_CDB);
     MSC_BOT_Status = BOT_STATE_ERROR;
    MSC_BOT_Abort(pdev);

  }
  else
  {
    if(SCSI_ProcessCmd(pdev,
                              MSC_BOT_cbw.bLUN,
                              &MSC_BOT_cbw.CB[0]) < 0)
    {
      MSC_BOT_Abort(pdev);
    }
    /*Burst xfer handled internally*/
    else if ((MSC_BOT_State != BOT_DATA_IN) &&
             (MSC_BOT_State != BOT_DATA_OUT) &&
             (MSC_BOT_State != BOT_LAST_DATA_IN))
    {
      if (MSC_BOT_DataLen > 0)
      {
        MSC_BOT_SendData(pdev,
                         MSC_BOT_Data,
                         MSC_BOT_DataLen);
      }
      else if (MSC_BOT_DataLen == 0)
      {
        MSC_BOT_SendCSW (pdev,
                         CSW_CMD_PASSED);
      }
    }
  }
}
Exemple #10
0
void SCSI_ProcessWriteComplete(uint32_t len) {
	SCSI_blk_addr  += len;
	SCSI_blk_len   -= len;

	/* case 12 : Ho = Do */
	MSC_BOT_csw.dDataResidue -= len;

	if (SCSI_blk_len == 0) {
		MSC_BOT_SendCSW (cdev, CSW_CMD_PASSED);
	} else {
		/* Prapare EP to Receive next packet */
		DCD_EP_PrepareRx (cdev,
						  MSC_OUT_EP,
						  MSC_BOT_Data,
						  MIN (SCSI_blk_len, MSC_MEDIA_PACKET));
	}
}
Exemple #11
0
static int8_t SCSI_ProcessWrite (uint8_t lun)
{
  uint32_t len;
  
  len = MIN(SCSI_blk_len , MSC_MEDIA_PACKET); 
  
  if(USBD_STORAGE_fops->Write(lun ,
                              MSC_BOT_Data, 
                              SCSI_blk_addr / SCSI_blk_size, 
                              len / SCSI_blk_size) < 0)
  {
    SCSI_SenseCode(lun, HARDWARE_ERROR, WRITE_FAULT);     
    return -1; 
  }
  
  
  SCSI_blk_addr  += len; 
  SCSI_blk_len   -= len; 
  
  /* case 12 : Ho = Do */
  MSC_BOT_csw.dDataResidue -= len;
  
  if (SCSI_blk_len == 0)
  {
    MSC_BOT_SendCSW (cdev, CSW_CMD_PASSED);
  }
  else
  {
    /* Prapare EP to Receive next packet */
    DCD_EP_PrepareRx (cdev,
                      MSC_OUT_EP,
                      MSC_BOT_Data, 
                      MIN (SCSI_blk_len, MSC_MEDIA_PACKET)); 
  }
  
  return 0;
}