#include <iom162v.h>
#include <macros.h>
#include "SCSI.H"
#include "SCSIDef.H"
#include "BulkOnly.H"
#include "MAIN.H"
#include "Fat.h"
extern BULK_STRUCT Bulk_Block;
extern CONTROL_XFER ControlData;
extern EPPFLAGS bEPPflags;
BULK_CONTROL Bulk_Control_r;
BULK_CONTROL Bulk_Control_w;
unsigned char PageBuf[512];
#define CBW_bCBWCBLength Bulk_Block.cbw.dCBWDataTransferLength
const STD_INQUIRY_DATA inquiryData={
0,//unsigned DeviceType :5;
0,//unsigned Reserved0 :3;
0,//unsigned Reserved1 :7;
1,//unsigned RemovableMedia :1;
2,//unsigned char Reserved2;
2,//unsigned Reserved3 :5;
0,//unsigned NormACA :1;
0,//unsigned Obsolete0 :1;
0,//unsigned AERC :1;
0x1f,0,0,//unsigned char Reserved4[3];
0,//unsigned SoftReset :1;
0,//unsigned CommandQueue :1;
0,//unsigned Reserved5 :1;
0,//unsigned LinkedCommands :1;
0,//unsigned Synchronous :1;
0,//unsigned Wide16Bit :1;
0,//unsigned Wide32Bit :1;
0,//unsigned RelativeAddressing :1;
'Y','U','X','K', //VendorInformation[8];
'2','0','0','5',
'U','S','B','-', //ProductInformation[16];
'M','A','S','S',
'-','S','T','O',
'R','A','G','E',
'0','.','0','0', //ProductReisionLevel[4];
};
const MODE_DATA ModeData={
3,//unsigned char OperationCode;
0,//unsigned char Reserved0;
0,//unsigned Reserved1 :7;
0,//unsigned WP :1;
0 //unsigned char Reserved2;
};
const READ_CAPACITY_DATA ReadCapacityData={
0,0,0xfb,0xff,//unsigned char LogicalBlockAddress[4];
0,0,0x02,0//unsigned char LogicalLength[4];
};
#define CAPACITY_LIST_LENGTH sizeof(CAPACITYLIST_HEAD)+sizeof(CM_FORMAT_CAPACITY_DATA)
const CAPACITYLIST_HEAD CapacitylistHead={
0,0,0,//unsigned char Reserved0[3];
sizeof(CM_FORMAT_CAPACITY_DATA) //unsigned char CapacityLength;
};
const CM_FORMAT_CAPACITY_DATA CMFormatCapacityData={
0,0x10,0,0,//unsigned char LogicalBlockAddress[4];
3, //unsigned DescriptorCode :2;
0, //unsigned Reserved0 :6;
0,2,0 //unsigned char LogicalLength[3];
};
BOOLEAN RBC_Hander(void)
{
#define cdbGeneric Bulk_Block.cbw.CBWCB.RbcCdb_Generic
BOOLEAN result=FALSE;
ControlData.wLength=0;
switch(cdbGeneric.OperationCode)
{
case SPC_CMD_INQUIRY:
result=SPC_InQuiry();
break;
case SPC_CMD_MODESENSE6:
result=SPC_ModeSense();
break;
case RBC_CMD_READ10:
result=RBC_Read10();
break;
case RBC_CMD_READCAPACITY:
result=RBC_ReadCapacity();
break;
case SPC_CMD_FORMAT_CAPACITY:
result=SPC_FormatCapacity();
break;
case SPC_CMD_TESTUNITREADY:
result=SPC_TestUnit();
break;
case RBC_CMD_VERIFY10:
result=RBC_Verify10();
break;
case RBC_CMD_WRITE10:
result=RBC_Write10();
break;
case SPC_CMD_PRVENTALLOWMEDIUMREMOVAL:
result=SPC_LockMedia();
break;
case SPC_CMD_RESERVE6:
default:
result=SPC_Reserve6();
break;
}
return result;
#undef cdbGeneric
}
BOOLEAN SPC_InQuiry(void)
{
Bulk_code_transmit((const unsigned char*)&inquiryData,
sizeof(STD_INQUIRY_DATA));
return TRUE;
}
BOOLEAN SPC_ModeSense(void)
{
Bulk_code_transmit((const unsigned char*)&ModeData,
sizeof(MODE_DATA));
return TRUE;
}
BOOLEAN RBC_Read10(void)
{
#define cdbRead10 Bulk_Block.cbw.CBWCB.RbcCdb_Read10
unsigned long Logical_Block_Address;
Logical_Block_Address=cdbRead10.LogicalBlockAddress[0]*0x1000000;
Logical_Block_Address+=cdbRead10.LogicalBlockAddress[1]*0x10000;
Logical_Block_Address+=cdbRead10.LogicalBlockAddress[2]*0x100;
Logical_Block_Address+=cdbRead10.LogicalBlockAddress[3];
Bulk_Control_r.Transfer_Length=cdbRead10.TransferLength[0]*256+cdbRead10.TransferLength[1];
Bulk_Control_r.CurrentPage=Logical_Block_Address%0x20;
Bulk_Control_r.CurrentBlock=Logical_Block_Address/0x20;
ReadPage(Bulk_Control_r.CurrentBlock,Bulk_Control_r.CurrentPage,PageBuf);
Bulk_Control_r.Transfer_Length--;
Bulk_Control_r.CurrentPage++;
Bulk_Control_r.BufCount=0;
bEPPflags.bits.bulk_state=BULK_DATA_TRANS;
Bulk_ErrorHandler(CSW_GOOD,CBW_bCBWCBLength);
D12_WriteEndpoint(5,EP2_PACKET_SIZE,PageBuf);
return TRUE;
#undef cdbRead10
}
BOOLEAN RBC_ReadCapacity(void)
{
Bulk_code_transmit((const unsigned char*)&ReadCapacityData,
sizeof(READ_CAPACITY_DATA));
return TRUE;
}
BOOLEAN SPC_FormatCapacity(void)
{
Bulk_code_transmit((const unsigned char*)&CapacitylistHead,
CAPACITY_LIST_LENGTH);
return TRUE;
}
BOOLEAN SPC_TestUnit(void)
{
Bulk_ErrorHandler(CSW_GOOD,0);
Bulk_CSWHandler();
return TRUE;
}
BOOLEAN RBC_Verify10(void)
{
Bulk_ErrorHandler(CSW_GOOD,0);
Bulk_CSWHandler();
return TRUE;
}
BOOLEAN RBC_Write10(void)
{
#define cdbWrite10 Bulk_Block.cbw.CBWCB.RbcCdb_Write10
unsigned char i,j,pageNum;
unsigned int blockNum;
unsigned long Logical_Block_Address;
Logical_Block_Address=cdbWrite10.LogicalBlockAddress[0]*0x1000000;
Logical_Block_Address+=cdbWrite10.LogicalBlockAddress[1]*0x10000;
Logical_Block_Address+=cdbWrite10.LogicalBlockAddress[2]*0x100;
Logical_Block_Address+=cdbWrite10.LogicalBlockAddress[3];
Bulk_Control_w.Transfer_Length=cdbWrite10.TransferLength[0]*256+cdbWrite10.TransferLength[1];
Bulk_Control_w.CurrentPage=Logical_Block_Address%32;
Bulk_Control_w.CurrentBlock=Logical_Block_Address/32;
Bulk_Control_w.BeginPage=Bulk_Control_w.CurrentPage;
Bulk_Control_w.BufCount=0;
bEPPflags.bits.bulk_state=BULK_DATA_RECIEVE;
Bulk_ErrorHandler(CSW_GOOD,CBW_bCBWCBLength);
pageNum=(Bulk_Control_w.BeginPage+Bulk_Control_w.Transfer_Length)%32;
blockNum=(Bulk_Control_w.BeginPage+Bulk_Control_w.Transfer_Length)/32;
blockNum+=Bulk_Control_w.CurrentBlock;
Erase_Cluster(CANCEL_BLOCK);
if(blockNum==Bulk_Control_w.CurrentBlock)j=32;
else j=Bulk_Control_w.BeginPage;
for(i=0;i<j;i++)
{
ReadPage(Bulk_Control_w.CurrentBlock,i,PageBuf);
WritePage(CANCEL_BLOCK,i,PageBuf);
}
Erase_Cluster(Bulk_Control_w.CurrentBlock);
for(i=0;i<Bulk_Control_w.BeginPage;i++)
{
ReadPage(CANCEL_BLOCK,i,PageBuf);
WritePage(Bulk_Control_w.CurrentBlock,i,PageBuf);
}
if(blockNum>Bulk_Control_w.CurrentBlock)
{
Erase_Cluster(CANCEL_BLOCK);
for(i=pageNum;i<32;i++)
{
ReadPage(blockNum,i,PageBuf);
WritePage(CANCEL_BLOCK,i,PageBuf);
}
}
return TRUE;
#undef cdbWrite10
}
BOOLEAN SPC_LockMedia(void)
{
Bulk_ErrorHandler(CSW_GOOD,0);
Bulk_CSWHandler();
return TRUE;
}
BOOLEAN SPC_Reserve6(void)
{
Bulk_ErrorHandler(CSW_FAIL,0);
Bulk_CSWHandler();
return TRUE;
}