📄 ms_rbc.c
字号:
USBMS_RETURN rbc_read_capacity(void)
{
ULONG _last_block_nmb;
USHORT _block_size;
/* fill the read capability structure */
_last_block_nmb = usbms_context.usb_lun[usbms_context.current_lun].TotBlockNumber-1;
_block_size = usbms_context.usb_lun[usbms_context.current_lun].BlockSize;
usbms_context.read_capacity_data.block_address_0 = (UCHAR)(_last_block_nmb);
usbms_context.read_capacity_data.block_address_1 = (UCHAR)(_last_block_nmb>>8);
usbms_context.read_capacity_data.block_address_2 = (UCHAR)(_last_block_nmb>>16);
usbms_context.read_capacity_data.block_address_3 = (UCHAR)(_last_block_nmb>>24);
usbms_context.read_capacity_data.block_len_0 = (UCHAR)(_block_size);
usbms_context.read_capacity_data.block_len_1 = (UCHAR)(_block_size>>8);
usbms_context.read_capacity_data.block_len_2 = 0;
usbms_context.read_capacity_data.block_len_3 = 0;
TRACE_DEBUG_L( "RC:dba0 0x%X \n\r", usbms_context.read_capacity_data.block_address_0);
TRACE_DEBUG_L( "RC:dba1 0x%X \n\r", usbms_context.read_capacity_data.block_address_1);
TRACE_DEBUG_L( "RC:dba2 0x%X \n\r", usbms_context.read_capacity_data.block_address_2);
TRACE_DEBUG_L( "RC:dba3 0x%X \n\r", usbms_context.read_capacity_data.block_address_3);
TRACE_DEBUG_L( "RC:dbl0 0x%X \n\r", usbms_context.read_capacity_data.block_len_0);
TRACE_DEBUG_L( "RC:dbl1 0x%X \n\r", usbms_context.read_capacity_data.block_len_1);
TRACE_DEBUG_L( "RC:dbl2 0x%X \n\r", usbms_context.read_capacity_data.block_len_2);
TRACE_DEBUG_L( "RC:dbl3 0x%X \n\r", usbms_context.read_capacity_data.block_len_3);
usb_send((char*)&usbms_context.read_capacity_data, sizeof(USBMS_READ_CAPACITY_DATA) );
usbms_context.state = USB_SEND_NO_FLASH;
return USBMS_OK;
}
/*****************************************************************
*
* ROUTINE : rbc_read_10
*
*-----------------------------------------------------------------
*
* Purpose : processes the rbc READ_10 command
*
* Input Parameters : Structure defining the READ_10 command
*
* Output Parameters : status of the operation
*
*****************************************************************/
USBMS_RETURN rbc_read_10(USBMS_READ_10 *read_10)
{
USBMS_XFER *_request = NULL;
/* initialize the temporary variables */
_request = &usbms_context.usb_lun[usbms_context.current_lun].cur_request;
/* Initialization of the request parameter */
_request->current_block = VALUE_32( read_10->block_address_0,
read_10->block_address_1,
read_10->block_address_2,
read_10->block_address_3 );
/* request processing */
ms_sendTx();
return USBMS_OK;
}
/*****************************************************************
*
* ROUTINE : rbc_commun_write
*
*-----------------------------------------------------------------
*
* Purpose : retreive data from the USB module and write to flash
*
* Input Parameters :
* USBMS_XFER *request, the current SCSI request being processed
* USB_LUN *lun, the targetted LUN associated with the request
*
* Output Parameters :
*
*****************************************************************/
USBMS_RETURN rbc_commun_write( USBMS_XFER *request, USB_LUN *lun )
{
USHORT _recLength;
USBMS_RETURN _status = USBMS_ERROR;
UCHAR* _buff;
USHORT _length;
ULONG _expected,_nbBlock;
/* How much data do we want from USB */
_expected = request->buffer_size - request->transferred_size;
if(_expected > request->data_buff.NbOctetsLibres)
{
_expected = request->data_buff.NbOctetsLibres;
}
/* Try to retreive data if already received */
_buff = po_malloc(_expected);
if(_buff == NULL)
{
/* Not enough memory */
TRACE_ERROR( "Allocfaild: %i \n\r", (int)_expected);
ms_stallPipe(FW_EP_BULK_OUT);
ms_computeCSW(COMMAND_FAILED,_expected);
usbms_context.state = RESET_IN_PROGRESS;
return USBMS_ERROR;
}
_recLength = ms_retreiveTb((char*)_buff , _expected);
if(_recLength == 0)
{
/* don't received anything yet */
/* end of request ? */
if(request->buffer_size == request->transferred_size)
{
/* compute CSW and send it */
ms_send_bot_status(GOOD_STATUS,0);
/* STATE = PROCESS CSW */
usbms_context.state = PROCESS_CSW;
}
else
{
/* the processing is in progress */
usbms_context.state = GET_FROM_USB;
}
po_free(_buff);
return USBMS_OK;
}
po_rngBufPut(&request->data_buff, (char*)_buff, _recLength);
po_free(_buff);
/* How much can we write to Flash ? */
_recLength = MAX_TRANSFER_SIZE - request->data_buff.NbOctetsLibres;
/* Reinit expected length */
_expected = request->buffer_size - request->transferred_size;
if( _expected < lun->BlockSize )
{
/* remainig data size of the command is less than a BLOCK */
if( _expected > _recLength )
{
_length = _recLength;
}
else
{
_length = _expected;
}
/* current block index will not update */
_nbBlock = 0;
}
else
{
/* we are not at the end of the SCSI command */
/* only send full blocks */
_length = (_recLength / lun->BlockSize)*lun->BlockSize ; /* Size in blocks */
_nbBlock = (_recLength / lun->BlockSize);
}
if(_length == 0)
{
/* Not a full block received yet, return */
usbms_context.state = GET_FROM_USB;
return USBMS_OK;
}
/* Retreive a temp buffer */
_buff = po_malloc(_length);
if(_buff == NULL)
{
/* Not enough memory */
TRACE_ERROR( "Allocfail2: %d \n\r", _length);
ms_stallPipe(FW_EP_BULK_OUT);
ms_computeCSW(COMMAND_FAILED,_expected);
usbms_context.state = RESET_IN_PROGRESS;
return USBMS_ERROR;
}
_recLength = po_rngBufGet(&request->data_buff, (char*)_buff, _length);
if( true == AT91F_Flash_Write_Address( (unsigned int)(FILE_MGR_BEGIN_ADDRESS + (request->current_block*usbms_context.usb_lun[usbms_context.current_lun].BlockSize)),
_recLength,
(unsigned char*)_buff, FALSE) )
{
_status = USBMS_OK;
/* buffer (index) */
request->current_block += _nbBlock;
/* size to be send */
request->transferred_size += _recLength;
/* end of request ? */
if(request->buffer_size == request->transferred_size)
{
/* compute CSW and send it */
ms_send_bot_status(GOOD_STATUS,0);
usbms_context.state = PROCESS_CSW;
}
else
{
/* the processing is in progress */
usbms_context.state = GET_FROM_USB;
}
}
else
{
/* Error write flash */
_status = USBMS_ERROR;
ms_stallPipe(FW_EP_BULK_OUT);
ms_computeCSW(COMMAND_FAILED,_expected);
usbms_context.state = RESET_IN_PROGRESS;
}
po_free(_buff);
return _status;
}
/*****************************************************************
*
* ROUTINE : rbc_write_10
*
*-----------------------------------------------------------------
*
* Purpose : processes the rbc WRITE_10 command
*
* Input Parameters : Structure defining the WRITE_10 command
*
* Output Parameters : status of the operation
*
*****************************************************************/
USBMS_RETURN rbc_write_10(USBMS_WRITE_10 *write_10)
{
USBMS_XFER *_request = NULL;
USB_LUN *_lun = NULL;
/* Initialize the temporary variables */
_request = &usbms_context.usb_lun[usbms_context.current_lun].cur_request;
_lun = &usbms_context.usb_lun[usbms_context.current_lun];
/* Transaction Initialization */
_request->current_block = VALUE_32( write_10->block_address_0,
write_10->block_address_1,
write_10->block_address_2,
write_10->block_address_3 );
/* Retreive total amount of blocks to transfer */
_request->command_size = (write_10->xfer_len_1<<8) | (write_10->xfer_len_0);
/* COMMUN WRITE */
rbc_commun_write( _request, _lun );
return( USBMS_OK );
}
/*****************************************************************
*
* ROUTINE : rbc_verify_10
*
*-----------------------------------------------------------------
*
* Purpose : processes the rbc VERIFY command
*
* Input Parameters : Structure defining the VERIFY command
*
* Output Parameters : status of the operation
*
*****************************************************************/
USBMS_RETURN rbc_verify_10(void)
{
return USBMS_OK;
}
/*****************************************************************
*
* ROUTINE : rbc_request_sense
*
*-----------------------------------------------------------------
*
* Purpose : processes the rbc REQUEST_SENSE command
*
* Input Parameters : Structure defining the REQUEST_SENSE command
*
* Output Parameters : status of the operation
*
*****************************************************************/
USBMS_RETURN rbc_request_sense(void)
{
usb_send((char *)&usbms_request_sense_data,sizeof(usbms_request_sense_data));
return( USBMS_OK );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -