Esempio n. 1
0
///////////////////////////////////////////////////////////////////////////////////////////////////////
//Return Value:
//	1: Fail, 0: Success
///////////////////////////////////////////////////////////////////////////////////////////////////////
int Write_10(unsigned long	lUSB_LUN_Write)
{
	int j;
	unsigned long SCSI_LBA;
	unsigned int SCSI_Transfer_Length;
	unsigned long USB_RW_Buffer_PTR;
	unsigned long USB_RW_Buffer_PTR_A,USB_RW_Buffer_PTR_B,USB_RW_Buffer_PTR_Temp;
	unsigned int USBDMA_CHANNEL0, USBDMA_CHANNEL1, USBDMA_CHANNEL2, USBDMA_CHANNEL3;
	int ret= 0xffff, stage = 0, *p;
	int ret1=0xffff;
	unsigned int Erro_Flag = 0;
	extern unsigned int *RHM_FlashBuf, RHM_USBreprogBuf_Full;
	unsigned int idx;
	extern unsigned int USBreprogBuf[];
	str_USB_Lun_Info*  ptr_USB_LUN_Write;

	ptr_USB_LUN_Write = (str_USB_Lun_Info*)lUSB_LUN_Write;
	R_USB_RW_Busy = 1;
	Sense_Code = 0x00;
	SCSI_LBA=GetLBA();
	SCSI_Transfer_Length=GetTransLeng();
	USB_RW_Buffer_PTR=GetBufferPointer();

	USB_RW_Buffer_PTR_A=GetBufferPointer();   //add by zhangxh 2006/01/19
	USB_RW_Buffer_PTR_B=USB_RW_Buffer_PTR_A + 256;

#ifdef OS_vension
	USBDMA_CHANNEL0 = MallocChannel();	 //by zhangxh 2006/01/19
	USBDMA_CHANNEL1 = MallocChannel();	 //by zhangxh 2006/01/19
	USBDMA_CHANNEL2 = MallocChannel();	 //by zhangxh 2006/01/19
	USBDMA_CHANNEL3 = MallocChannel();	 //by zhangxh 2006/01/19
#else
	USBDMA_CHANNEL0 = 0;
	USBDMA_CHANNEL1 = 1;
	USBDMA_CHANNEL2 = 2;
	USBDMA_CHANNEL3 = 3;
#endif	

	if(ptr_USB_LUN_Write->unLunType == LunType_SDCardReader)
	{
#ifdef SD_Used	
		if ((SCSI_LBA + SCSI_Transfer_Length)>SDCTotalSector)	//huck	
		{
			Sense_Code = 0x1B; //LOGICAL BLOCK ADDRESS OUT OF RANGE
			ret = 1;
			goto End;			
		}

		for(j=0; j<SCSI_Transfer_Length; j++)
		{							
			//=================================================================================================
			// Enable DMA mode. And using USB 128x8 bytes dual FIFO			
			ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B,USBDMA_CHANNEL3);			
			if(ret != 0x00)
			{
				*P_USBD_EPEvntClear=0x0010;
				*P_USBD_EPEvent=0x2000;		// Bulk OUT NACK Reset
				break;
			}

			*P_USBD_EPEvntClear=0x0010;
			*P_USBD_EPEvent=0x2000;		// Bulk OUT NACK Reset

			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;
			if (j>0 )
			{
				stage = 1;				
			}
			if (!Erro_Flag)
				ret1 = DrvSDCMultiWriteSector_USB((unsigned long)SCSI_LBA+j, (unsigned long)USB_RW_Buffer_PTR_B,(unsigned int)USBDMA_CHANNEL2,stage);			

			if(ret1 != 0x00)
			{
				ret = ret1;
				break;
			}			
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;

		}//j

		if((ret1 == 0x00)&&(!Erro_Flag))
			ret1=DrvSDCMultiWriteSector_USB((unsigned long)SCSI_LBA+j, (unsigned long)USB_RW_Buffer_PTR_B,(unsigned int)USBDMA_CHANNEL2,2);		

		if ((ret1 != 0x00)&&(!Erro_Flag))
		{
			DrvSDCMultiWriteSector_USB((unsigned long)SCSI_LBA+j, (unsigned long)USB_RW_Buffer_PTR_B,(unsigned int)USBDMA_CHANNEL2,4);		
			ret = ret1;
		}

		if( (ret != 0x00) || (ret1 != 0x00) )
		{
			CSW_Residue=SCSI_Transfer_Length-j-1;
			CSW_Residue=CSW_Residue << 9;
			Sense_Code=0x10;	   
		}
		else
			CSW_Residue=0;
#endif
	}
	else if(ptr_USB_LUN_Write->unLunType == LunType_NOR) {  //rhm
	
		flash *pflash = (flash *)ptr_USB_LUN_Write->rhmLunData;

		ptr_USB_LUN_Write->ulNandSizeSec = 0xaabbccdd;
		ptr_USB_LUN_Write->ulNandSizeSec = 2048;
	
//		long xx = ptr_USB_LUN_Write->ulNandSizeSec;
//		if ((SCSI_LBA + (SCSI_Transfer_Length*512)) > (ptr_USB_LUN_Write->ulNandSizeSec))	//check address overflow	
//		{
//			Sense_Code = 0x1B;//LOGICAL BLOCK ADDRESS OUT OF RANGE
//			ret = 1;			
//			goto End;
//		}

//		SCSI_LBA += ptr_USB_LUN_Write->ulNandStartAddr;	
		
		//rhm nor erase sector here	
		
		*P_WatchDog_Ctrl = 0;		// disable watchdog

		pflash->pflash = SCSI_LBA;
		pflash->bytesAvail = 0;
//rhm1		pflash->erasesector(pflash);

		for(j=0; j<SCSI_Transfer_Length; j++)
		{				
			//=================================================================================================
			// Enable DMA mode. And using USB 128x8 bytes dual FIFO
			//ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B,DMA_CHANNEL2);
//RHM			ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B, USBDMA_CHANNEL1);

		    ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B, USBDMA_CHANNEL3);

			if (ret != 0x00)
			{
				*P_USBD_EPEvntClear=0x0010;			
				*P_USBD_EPEvent=0x2000;		// Bulk OUT NACK Reset
				break;
			}
			*P_USBD_EPEvntClear=0x0010;			
			*P_USBD_EPEvent=0x2000;		// Bulk OUT NACK Reset

			if (j==0)
				stage = 0;
			else 
				stage = 1;
				
			{
			unsigned short *sp = (unsigned short *)USB_RW_Buffer_PTR_B;
			if(RHM_FlashBuf) {
				for(idx=0; idx<256; idx++) {
					RHM_FlashBuf[(j*256)+idx] = *sp++;
				}
				pflash->bytesAvail += 256;
			}
			}

			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;
		}//j

		if(ret != 0x00)
		{
			CSW_Residue=SCSI_Transfer_Length-j-1;
			CSW_Residue=CSW_Residue << 9;
			Sense_Code=0x10;	//WRITE FAULT
		}
		else {
			CSW_Residue=0;			
			USB_Status =  BULK_IN_NACK;  //TEST			
			RHM_USBreprogBuf_Full = j * 256;
		}
	} // rhm end LunType_NOR
	else { //  rhm LunType_RAM
		
//RHM #ifdef Nand_Used	
		j=0;

		if ((SCSI_LBA + (SCSI_Transfer_Length*512))>ptr_USB_LUN_Write->ulNandSizeSec)	//check address overflow	
		{
			Sense_Code = 0x1B;//LOGICAL BLOCK ADDRESS OUT OF RANGE
			ret = 1;			
			goto End;
		}

		SCSI_LBA += ptr_USB_LUN_Write->ulNandStartAddr;		

		for(j=0; j<SCSI_Transfer_Length; j++)
		{				
			//=================================================================================================
			// Enable DMA mode. And using USB 128x8 bytes dual FIFO
			//ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B,DMA_CHANNEL2);
//RHM			ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B, USBDMA_CHANNEL1);
		    ret=Receive_From_USB_DMA_USB(USB_RW_Buffer_PTR_B, USBDMA_CHANNEL3);

			if (ret != 0x00)
			{
				*P_USBD_EPEvntClear=0x0010;			
				*P_USBD_EPEvent=0x2000;		// Bulk OUT NACK Reset
				break;
			}
			*P_USBD_EPEvntClear=0x0010;			
			*P_USBD_EPEvent=0x2000;		// Bulk OUT NACK Reset

			if (j==0)
				stage = 0;
			else 
				stage = 1;

//			if (!Erro_Flag)				
//RHM				ret1 = _NAND_WriteSector_USB((unsigned long)SCSI_LBA+j, 1, (unsigned long)USB_RW_Buffer_PTR_B,(unsigned int)USBDMA_CHANNEL0,stage);
					memcpy(SCSI_LBA+(j*256), (unsigned long)USB_RW_Buffer_PTR_B, 256);
//			if (ret1 != 0x00)
//				break;

			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;
		}//j

		if(ret != 0x00)
		{
			CSW_Residue=SCSI_Transfer_Length-j-1;
			CSW_Residue=CSW_Residue << 9;
			Sense_Code=0x10;	//WRITE FAULT
		}
		else
			CSW_Residue=0;
//RHM #endif
	}

End:	
#ifdef OS_vension
	FreeChannel(USBDMA_CHANNEL0);
	FreeChannel(USBDMA_CHANNEL1);
	FreeChannel(USBDMA_CHANNEL2);
	FreeChannel(USBDMA_CHANNEL3);		
#endif

	return ret;	
}
Esempio n. 2
0
///////////////////////////////////////////////////////////////////////////////////////////////////////
//Return Value:
//	1: Fail, 0: Success
///////////////////////////////////////////////////////////////////////////////////////////////////////
int Read_10(unsigned long  lUSB_LUN_Read)
{
	int i;
	unsigned long SCSI_LBA;
	unsigned int SCSI_Transfer_Length;
	unsigned long USB_RW_Buffer_PTR, USB_RW_Buffer_PTR_A,USB_RW_Buffer_PTR_B,USB_RW_Buffer_PTR_Temp;
	int ret,stage;
	unsigned long num;
	unsigned int USBDMA_CHANNEL0, USBDMA_CHANNEL1, USBDMA_CHANNEL2, USBDMA_CHANNEL3;
	str_USB_Lun_Info*  ptr_USB_LUN_Read;

	ptr_USB_LUN_Read = (str_USB_Lun_Info*)lUSB_LUN_Read;
	ulTimeout = 0x4FFFF;
	ret=0x00;
	stage = 0x00;
	Sense_Code = 0;
	R_USB_RW_Busy = 1;

	SCSI_LBA=GetLBA();

	SCSI_Transfer_Length=GetTransLeng();

	USB_RW_Buffer_PTR=GetBufferPointer();
	USB_RW_Buffer_PTR_A=USB_RW_Buffer_PTR;   //add by zhangxh 2006/01/19
	USB_RW_Buffer_PTR_B=USB_RW_Buffer_PTR_A + 256;	

#ifdef OS_vension
	USBDMA_CHANNEL0 = MallocChannel();	 //by zhangxh 2006/01/19
	USBDMA_CHANNEL1 = MallocChannel();	 //by zhangxh 2006/01/19
	USBDMA_CHANNEL2 = MallocChannel();	 //by zhangxh 2006/01/19
	USBDMA_CHANNEL3 = MallocChannel();	 //by zhangxh 2006/01/19
#else
	USBDMA_CHANNEL0 = 0;
	USBDMA_CHANNEL1 = 1;
	USBDMA_CHANNEL2 = 2;
	USBDMA_CHANNEL3 = 3;
#endif

	if(ptr_USB_LUN_Read->unLunType == LunType_SDCardReader)  //SD Card
	{
#ifdef SD_Used	
		if ((SCSI_LBA + SCSI_Transfer_Length)>SDCTotalSector)	//huck
		{
			Sense_Code = 0x1B;  //LOGICAL BLOCK ADDRESS OUT OF RANGE
			ret = 1;
			goto End;
		}

		for(i=0 ; i < SCSI_Transfer_Length ; i++)
		{			

			if (i == 0)

				ret = DrvSDCReadMultiSector_USB((unsigned long)SCSI_LBA+i, (unsigned long)USB_RW_Buffer_PTR_A,USBDMA_CHANNEL0,0);
			else 
				ret = DrvSDCReadMultiSector_USB((unsigned long)SCSI_LBA+i, (unsigned long)USB_RW_Buffer_PTR_A,USBDMA_CHANNEL0,1);

			if (i == SCSI_Transfer_Length-1)
				ret = DrvSDCReadMultiSector_USB((unsigned long)SCSI_LBA+i, (unsigned long)USB_RW_Buffer_PTR_A,USBDMA_CHANNEL0,2);


			if(ret != 0x00)
			{
				DrvSDCReadMultiSector_USB((unsigned long)SCSI_LBA+i, (unsigned long)USB_RW_Buffer_PTR_A,USBDMA_CHANNEL0,4); //4,stop the SDC Only					
				Sense_Code=0x08;
				goto End;
			}			

			if (stage)
			{
				ret = Send_To_USB_DMA_USB(USB_RW_Buffer_PTR_A,USBDMA_CHANNEL1,1);
				for(num=0;num<ulTimeout;num++)
				{
					if(R_USB_Suspend == 1)
						break;
					else
					{
						if ((*P_USBD_EPEvent & 0x0100) != 0)
							ret = 0xffff;
						else
						{
							ret = 0;
							*P_USBD_EPEvent=0x0600;
							break;
						}
					}
				}

				if(num == ulTimeout)
				{
					Sense_Code=0x12;
					ret = 0xffff;
					return ret;					
				}
			}		

			ret = Send_To_USB_DMA_USB(USB_RW_Buffer_PTR_A,USBDMA_CHANNEL1,0);									
			if (ret != 0x00)
			{
				ret = 0xffff;	
				Sense_Code = 0x12; 			
				goto End;
			}

			stage=1;		

			//for last data 
			if (i == SCSI_Transfer_Length-1)
			{
				ret = Send_To_USB_DMA_USB(USB_RW_Buffer_PTR_A,USBDMA_CHANNEL1,1);

				if (ret != 0x00)
				{
					ret = 0xffff;	
					Sense_Code = 0x12; 			
					goto End;
				}

				for(num=0;num<ulTimeout;num++)
				{
					if(R_USB_Suspend == 1)
						break;
					else
					{
						if ((*P_USBD_EPEvent & 0x0100) != 0)
							ret = 0xffff;
						else
						{
							ret = 0;
							*P_USBD_EPEvent=0x0600;
							break;
						}
					}
				}
				if(num == ulTimeout)
				{
					ret = 0xffff;	
					Sense_Code = 0x12; 			
					goto End;
				}

			}
			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;			
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;			

		}//for		

		if(ret != 0x00)
		{			
			CSW_Residue=SCSI_Transfer_Length-i;			
			CSW_Residue=CSW_Residue << 9;	
		}
		else
			CSW_Residue=0;	
#endif
	}	
	else //Nand Drive
	{
#ifdef Nand_Used
		if ((SCSI_LBA + SCSI_Transfer_Length)>ptr_USB_LUN_Read->ulNandSizeSec)	//check read address overflow
		{
			Sense_Code = 0x1B;
			ret = 1;
			goto End;
		}

		if(ptr_USB_LUN_Read->unLunType == LunType_CDROM)	//CDROM, do not modify below CDROM code
		{
			SCSI_LBA ++;						
			SCSI_LBA = SCSI_LBA << 2;						//1 sector = 4 *512
			SCSI_LBA += ptr_USB_LUN_Read->ulNandStartAddr;		
			SCSI_Transfer_Length = SCSI_Transfer_Length << 2; //length *4
		}
		else
			SCSI_LBA += ptr_USB_LUN_Read->ulNandStartAddr;		

		for(i=0 ; i < SCSI_Transfer_Length ; i++)
		{
			ret = _NAND_ReadSector_USB((unsigned long)(SCSI_LBA+i), 1, (unsigned long)USB_RW_Buffer_PTR_A, USBDMA_CHANNEL2);

			if (stage)
			{
				TXToUSB_USB(USB_RW_Buffer_PTR_A, 0x01, USBDMA_CHANNEL1, 0x01);

				ulTimeout = 0x4FFFF;
				while((*P_USBD_EPEvent & 0x0100) != 0)
				{						
					if (!(ulTimeout--))	
					{
						*P_USBD_EPEvent=0x0600;	
						Sense_Code = 0x12; 
						ret = 1;
						goto End;
					}
				}
				*P_USBD_EPEvent=0x0600;
			}

			TXToUSB_USB(USB_RW_Buffer_PTR_A, 0x01, USBDMA_CHANNEL1, 0x00);
			stage=1;

			if (i == SCSI_Transfer_Length-1)
			{
				TXToUSB_USB(USB_RW_Buffer_PTR_A, 0x01, USBDMA_CHANNEL1, 0x01);
				ulTimeout = 0x4FFFF;
				while((*P_USBD_EPEvent & 0x0100) != 0)
				{						
					if (!(ulTimeout--))	
					{
						*P_USBD_EPEvent=0x0600;	
						Sense_Code = 0x12; 
						ret = 1;
						goto End;
					}
				}
				*P_USBD_EPEvent=0x0600;
			}
			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;			
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;
		}		

		CSW_Residue=0x00;
#endif
	}
End:	
#ifdef OS_vension
	FreeChannel(USBDMA_CHANNEL0);
	FreeChannel(USBDMA_CHANNEL1);
	FreeChannel(USBDMA_CHANNEL2);
	FreeChannel(USBDMA_CHANNEL3);		
#endif

	return ret;
}
Esempio n. 3
0
//unsigned int ModeSense6_CallBack(void)
//{
//	return 0;
//
//////////////////////////////////////////////////////////////
INT32S Read_10(str_USB_Lun_Info* lUSB_LUN_Read)
{
    INT16U  i=0,j=0,stage =0;
    INT32S  ret;
    INT32U  SCSI_LBA,SCSI_Transfer_Length;
    INT32U  AB_Counts,Ren_Sectors;
    INT16U* USB_RW_Buffer_PTR_A;
    INT16U* USB_RW_Buffer_PTR_B;
    INT16U* USB_RW_Buffer_PTR_Temp;
    //Checking curent LUN
    
    if (usbd_current_lun != lUSB_LUN_Read->unLunNum)
    {
      if (usbd_msdc_lun_change(lUSB_LUN_Read))
      {
            //DBG_PRINT("LUN FAIL\r\n");
   			Sense_Code = 0x1B;
			return -1;
      }
      usbd_current_lun = lUSB_LUN_Read->unLunNum;
    }
    stage =0;
	SCSI_LBA=GetLBA();
	SCSI_Transfer_Length=GetTransLeng();
	USB_RW_Buffer_PTR_A=(INT16U*) BUF ;
	USB_RW_Buffer_PTR_B=USB_RW_Buffer_PTR_A+ (256*READWRITE_SECTOR);
	if(SCSI_LBA == 0x3ffd)
	SCSI_LBA= 0x3ffd;

	//DBG_PRINT("READ LBA = %x ,len = %x\r\n",SCSI_LBA,SCSI_Transfer_Length);
	if ((SCSI_LBA + SCSI_Transfer_Length)>lUSB_LUN_Read->ulSecSize)
	{
			Sense_Code = 0x1B;
			return -1;
	}
    
	AB_Counts   = SCSI_Transfer_Length / READWRITE_SECTOR;
	Ren_Sectors = SCSI_Transfer_Length % READWRITE_SECTOR;	
	
	// #if CDNF_EN|CDSPI_EN      //add by erichan 20091031   for nandcdrom      
/*	 #if CDNF_EN      //add by erichan 20091031   for nandcdrom            
	 if(lUSB_LUN_Read->unLunType == LUNTYPE_CDROM_NF)
	// if((lUSB_LUN_Read->unLunType == LUNTYPE_CDROM_NF)||(lUSB_LUN_Read->unLunType == LUNTYPE_CDROM_SPI))	//CDROM, do not modify below CDROM code
	 {
	 
	  SCSI_LBA ++;
	  SCSI_LBA = SCSI_LBA << 2;						//1 sector = 4 *512
	  AB_Counts = AB_Counts <<2;
	  }
	#endif
*/			
    //ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_cmd_phase(SCSI_LBA,SCSI_Transfer_Length); del by erichan
    ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_cmd_phase(SCSI_LBA,SCSI_Transfer_Length); 
	if(ret != 0)
	{
  	    //DBG_PRINT("e1\r\n");
    	CSW_Residue=SCSI_Transfer_Length;
		CSW_Residue=CSW_Residue << 9;
		Sense_Code=0x12;
		return -1;
	}
	for(i=0 ; i < AB_Counts ; i++)
	{
            if (i == 0)
              ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_dma_phase((INT32U*)USB_RW_Buffer_PTR_A,1,READWRITE_SECTOR);
            else
              ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_dma_phase((INT32U*)USB_RW_Buffer_PTR_A,0,READWRITE_SECTOR);
            //if(ret != 0)  {	DBG_PRINT("e5 %x \r\n",i);break; }
			if (ret != 0)	 break;

            if (i != 0)
              ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_dma_phase(NULL,2,READWRITE_SECTOR); //wait and check storage DMA
            if (stage)
			{
				ret = Send_To_USB_DMA_USB((INT32U*)USB_RW_Buffer_PTR_A,READWRITE_SECTOR,1);
				//Check if timeout
				//if(ret != 0)  {	DBG_PRINT("e1\r\n");break; }
				if(ret != 0)  break;
			}
        	//if(ret != 0)  {	DBG_PRINT("e2\r\n");break; }
			if(ret != 0)  break;
  			//if (i == AB_Counts-1)
            //   UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_cmdend_phase();//Storage end

			ret = Send_To_USB_DMA_USB((INT32U*)USB_RW_Buffer_PTR_A,READWRITE_SECTOR,0);
		    //if(ret != 0)  {	DBG_PRINT("e3\r\n");break; }
			if(ret != 0)  break;
			stage=1;
			//for last data
			if (i == AB_Counts-1)
			{
				ret = Send_To_USB_DMA_USB((INT32U*)USB_RW_Buffer_PTR_A,READWRITE_SECTOR,1);
			    //if(ret != 0)  {	DBG_PRINT("e4\r\n");break; }
				if(ret != 0)  break;
			}
			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;

	}//end for
	
	if( (ret == 0x00) && (Ren_Sectors> 0))
	{
    	  for(j=0 ; j < Ren_Sectors ; j++)
     	  {
            if (j == 0)
              ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_dma_phase((INT32U*)USB_RW_Buffer_PTR_A,1,1);
            else
              ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_dma_phase((INT32U*)USB_RW_Buffer_PTR_A,0,1);
            //if(ret != 0)  {	DBG_PRINT("e5 %x \r\n",i);break; }
			if (ret != 0)	 break;
            if (j != 0)
              ret = UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_dma_phase(NULL,2,1); //wait and check storage DMA
            if (stage)
			{
				ret = Send_To_USB_DMA_USB((INT32U*)USB_RW_Buffer_PTR_A,1,1);
				//Check if timeout
				//if(ret != 0)  {	DBG_PRINT("e1\r\n");break; }
				if(ret != 0)  break;
			}
        	//if(ret != 0)  {	DBG_PRINT("e2\r\n");break; }
			if(ret != 0)  break;
  			ret = Send_To_USB_DMA_USB((INT32U*)USB_RW_Buffer_PTR_A,1,0);
		    //if(ret != 0)  {	DBG_PRINT("e3\r\n");break; }
			if(ret != 0)  break;
			stage=1;
			//for last data
			if (j == Ren_Sectors-1)
			{
				ret = Send_To_USB_DMA_USB((INT32U*)USB_RW_Buffer_PTR_A,1,1);
			    //if(ret != 0)  {	DBG_PRINT("e4\r\n");break; }
				if(ret != 0)  break;
			}
			USB_RW_Buffer_PTR_Temp = USB_RW_Buffer_PTR_B;
			USB_RW_Buffer_PTR_B = USB_RW_Buffer_PTR_A;
			USB_RW_Buffer_PTR_A = USB_RW_Buffer_PTR_Temp;
		 }//end for	
	}
    UsbReadWrite[lUSB_LUN_Read->unLunType]->usdc_read_cmdend_phase();//Storage end
	
	if(ret != 0x00)
	{
       	    //DBG_PRINT("READ10 %x %x Fail!\r\n",i,SCSI_Transfer_Length);
			CSW_Residue=SCSI_Transfer_Length-(i*READWRITE_SECTOR)-j;
			CSW_Residue=CSW_Residue << 9;
			Sense_Code = 0x12;
#if (MCU_VERSION == GPL32_B)			
            if usbd_check_bi_dma()
            {
                 USBRWError_Flag = 0xfefe; 
                 return 1;
            }
#endif
	}