📄 dispatchroutines.c
字号:
PULONG LongBuffer;
DebugPrint("start obtaining PORT0 base address...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
LongBuffer = (PULONG)IoBuffer;
*LongBuffer = (ULONG)pdx->BaseAddress_PortRegister.LowPart;
DebugPrint("the port0 base address returned to user is :0x%x\n",LongBuffer[0]);
// Assume a successful return status
status = STATUS_SUCCESS;
DebugPrint("PORT0 base address getting end...\n");
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_GET_3124REGISTER_PORT1_BASEADDRESS_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_GET_3124REGISTER_PORT1_BASEADDRESS
******************************************************************************************************************/
NTSTATUS IOCTL_GET_3124REGISTER_PORT1_BASEADDRESS_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PVOID IoBuffer;
PULONG LongBuffer;
DebugPrint("start obtaining PORT1 base address...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
LongBuffer = (PULONG)IoBuffer;
*LongBuffer = (ULONG)(pdx->BaseAddress_PortRegister.LowPart+0x2000);
DebugPrint("the port1 base address returned to user is :0x%x\n",LongBuffer[0]);
// Assume a successful return status
status = STATUS_SUCCESS;
DebugPrint("PORT1 base address getting end...\n");
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_GET_3124REGISTER_PORT2_BASEADDRESS_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_GET_3124REGISTER_PORT2_BASEADDRESS
******************************************************************************************************************/
NTSTATUS IOCTL_GET_3124REGISTER_PORT2_BASEADDRESS_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PVOID IoBuffer;
PULONG LongBuffer;
DebugPrint("start obtaining PORT2 base address...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
LongBuffer = (PULONG)IoBuffer;
*LongBuffer = (ULONG)(pdx->BaseAddress_PortRegister.LowPart+2*0x2000);
DebugPrint("the port2 base address returned to user is :0x%x\n",LongBuffer[0]);
// Assume a successful return status
status = STATUS_SUCCESS;
DebugPrint("PORT2 base address getting end...\n");
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_GET_3124REGISTER_PORT3_BASEADDRESS_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_GET_3124REGISTER_PORT3_BASEADDRESS
******************************************************************************************************************/
NTSTATUS IOCTL_GET_3124REGISTER_PORT3_BASEADDRESS_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PVOID IoBuffer;
PULONG LongBuffer;
DebugPrint("start obtaining PORT3 base address...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
LongBuffer = (PULONG)IoBuffer;
*LongBuffer = (ULONG)(pdx->BaseAddress_PortRegister.LowPart+3*0x2000);
DebugPrint("the port3 base address returned to user is :0x%x\n",LongBuffer[0]);
// Assume a successful return status
status = STATUS_SUCCESS;
DebugPrint("PORT3 base address getting end...\n");
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_GET_3124REGISTER_GLOBAL_BASEADDRESS_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_GET_3124REGISTER_GLOBAL_BASEADDRESS
******************************************************************************************************************/
NTSTATUS IOCTL_GET_3124REGISTER_GLOBAL_BASEADDRESS_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PVOID IoBuffer;
PULONG LongBuffer;
DebugPrint("start obtaining GLOBAL base address...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
LongBuffer = (PULONG)IoBuffer;
*LongBuffer = (ULONG)pdx->BaseAddress_GlobalRegister.LowPart;
DebugPrint("the global base address returned to user is :0x%x\n",LongBuffer[0]);
// Assume a successful return status
status = STATUS_SUCCESS;
DebugPrint("global base address getting end...\n");
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_GET_3124PORT_INDIRECT_BASEADDRESS_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_GET_3124PORT_INDIRECT_BASEADDRESS
******************************************************************************************************************/
NTSTATUS IOCTL_GET_3124PORT_INDIRECT_BASEADDRESS_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PVOID IoBuffer;
PULONG LongBuffer;
DebugPrint("start obtaining indirect access base address...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
LongBuffer = (PULONG)IoBuffer;
*LongBuffer = (ULONG)pdx->BaseAddress_Indirect_Access.LowPart;
DebugPrint("the indirect access register base address returned to user is :0x%x\n",LongBuffer[0]);
// Assume a successful return status
status = STATUS_SUCCESS;
DebugPrint("indirect access base address getting end...\n");
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_3124REGISTER_PIO_READ_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_3124REGISTER_PIO_READ
******************************************************************************************************************/
NTSTATUS IOCTL_3124REGISTER_PIO_READ_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PIO_STACK_LOCATION pIrpStack;
ULONG Slot = 0;
PVOID IoBuffer;
PULONG LongBuffer;
PUSHORT ShortBuffer;
PUCHAR CharBuffer;
ULONG PortBaseAddress;
PSATAREG48 SATAREG48Buffer;
PHYSICAL_ADDRESS PIOReadBuffer,PAddress;
ULONG PIOReadCount;
USHORT SectorCount;
ULONG i,RequestCount;
ULONG PortStatus;
PUCHAR SlotBaseAddress; //slot base address
status = STATUS_SUCCESS; // Assume a successful return status
DebugPrint("start PIO READ...\n");
pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
IoBuffer = pIrp->AssociatedIrp.SystemBuffer;
SATAREG48Buffer = (PSATAREG48)IoBuffer;
LongBuffer = (PULONG)SATAREG48Buffer->rBuf;
PortBaseAddress = LongBuffer[0];
pdx->PortBaseAddress = PortBaseAddress;
DebugPrint("the port base address is 0x%x",PortBaseAddress);
//check the PCI registers whether are all not available
if(READ_REGISTER_ULONG((PULONG)(PortBaseAddress+0x1000)) == 0xffffffff)
{
DebugPrint("the PCI register are all not available");
pdx->TansferedCount = 0;
CompleteRequestInfo(pIrp,status,pdx->TansferedCount);
IoStartNextPacket(fdo, TRUE);
return status;
}
//check the port ready
if((READ_REGISTER_ULONG((PULONG)(PortBaseAddress+0x1000)) & 0x80000000) != 0x80000000)
{
DebugPrint("the port is not ready,the Sstatus:0x%x",READ_REGISTER_UCHAR((PUCHAR)(PortBaseAddress+0x1f04)));
pdx->TansferedCount = 0;
CompleteRequestInfo(pIrp,status,pdx->TansferedCount);
IoStartNextPacket(fdo, TRUE);
return status;
}
//check whether the drive power on
if((READ_REGISTER_UCHAR((PUCHAR)(PortBaseAddress+0x1f04)) & 0x03) != 0x03)
{
DebugPrint("the device is not present and PHY communication is not established .the DET: 0x%x",READ_REGISTER_UCHAR((PUCHAR)(PortBaseAddress+0x1f04)));
pdx->TansferedCount = 0;
CompleteRequestInfo(pIrp,status,pdx->TansferedCount);
IoStartNextPacket(fdo, TRUE);
return status;
}
SlotBaseAddress = (PUCHAR)PortBaseAddress+Slot*0x80;
SectorCount = SATAREG48Buffer->secc_pre;
SectorCount <<= 8;
SectorCount |= SATAREG48Buffer->secc_cur;
RequestCount = SectorCount * 512;
DebugPrint("the Request count to be transfered is %u,and the PAGE_SIZE is %u",RequestCount,PAGE_SIZE);
/****************************************************************************
* create the region for data transfer
****************************************************************************/
pdx->ReadBuffer = (PUCHAR) ExAllocatePool(NonPagedPool,RequestCount);
PIOReadBuffer = MmGetPhysicalAddress(pdx->ReadBuffer);
PIOReadCount = RequestCount;
DebugPrint("the SGE0 address is %x, data count is %u",PIOReadBuffer.LowPart,PIOReadCount);
/*****************************************************************************
* Create a PRB and Fill it with specific parameters
******************************************************************************/
pdx->PRBStruct = (pPRB) ExAllocatePool(NonPagedPool,sizeof(PRB));
DebugPrint("PRB system address is:0x%x",(ULONG)pdx->PRBStruct);
PAddress = MmGetPhysicalAddress(pdx->PRBStruct);
DebugPrint("PRBS Physical address is:0x%x",(ULONG)PAddress.LowPart);
//Make PRB 8 bytes aligned
while((PAddress.LowPart)% 8 != 0)
{
ExFreePool(pdx->PRBStruct);
pdx->PRBStruct = (pPRB) ExAllocatePool(NonPagedPool,sizeof(PRB));
PAddress = MmGetPhysicalAddress(pdx->PRBStruct);
DebugPrint(" After ReAllocate Physical Address is:0x%x\n" ,PAddress.LowPart );
DebugPrint(" After ReAllocate System Address is:0x%x\n" ,(ULONG)pdx->PRBStruct);
}
pdx->PRBStruct->Control = 0x0000;
pdx->PRBStruct->ProtocolOverride = 0x0000;
pdx->PRBStruct->ReceivedCount = 0x00000000;
pdx->PRBStruct->FISType = 0x27;
pdx->PRBStruct->PMP = 0x80;
pdx->PRBStruct->Command_Status = SATAREG48Buffer->cmnd;
pdx->PRBStruct->Features_Error = SATAREG48Buffer->feat_cur;
pdx->PRBStruct->SectorNumber = SATAREG48Buffer->secn_cur;
pdx->PRBStruct->CylLow = SATAREG48Buffer->cyll_cur;
pdx->PRBStruct->CylHigh = SATAREG48Buffer->cylh_cur;
pdx->PRBStruct->DevHead = SATAREG48Buffer->devh;
pdx->PRBStruct->SectorNumberExp = SATAREG48Buffer->secn_pre;
pdx->PRBStruct->CylLowExp = SATAREG48Buffer->cyll_pre;
pdx->PRBStruct->CylHighExp = SATAREG48Buffer->cylh_pre;
pdx->PRBStruct->FeaturesExp = SATAREG48Buffer->feat_pre;
pdx->PRBStruct->SectorCount = SATAREG48Buffer->secc_cur;
pdx->PRBStruct->SectorCountExp = SATAREG48Buffer->secc_pre;
pdx->PRBStruct->Reserved0 = 0x00;
pdx->PRBStruct->DeviceControl = SATAREG48Buffer->fxdc;
pdx->PRBStruct->Reserved1 = 0x00000000;
pdx->PRBStruct->Reserved2 = 0x00000000;
pdx->PRBStruct->SGE0Low = (ULONG)PIOReadBuffer.LowPart;
pdx->PRBStruct->SGE0High = 0x00000000;
pdx->PRBStruct->SGE0Count = PIOReadCount;
pdx->PRBStruct->SGE0Control = 0x80000000;
pdx->PRBStruct->SGE1Low = 0x00000000;
pdx->PRBStruct->SGE1High = 0x00000000;
pdx->PRBStruct->SGE1Count = 0;
pdx->PRBStruct->SGE1Control = 0x00000000;
DebugPrint("the port interrupt status 0x%x",READ_REGISTER_ULONG((PULONG)(PortBaseAddress+0x1008)));
WRITE_REGISTER_ULONG((PULONG)(PortBaseAddress+0x1C00),PAddress.LowPart);
WRITE_REGISTER_ULONG((PULONG)(PortBaseAddress+0x1C04),0x00000000);
pdx->NeedToHandle =TRUE;
return status;
}
/******************************************************************************************************************
*
* Function : IOCTL_3124REGISTER_PIO_WRITE_Handler
*
* Description: Handle IRP_MJ_DEVICE_CONTROL which Control_Code is IOCTL_3124REGISTER_PIO_WRITE
******************************************************************************************************************/
NTSTATUS IOCTL_3124REGISTER_PIO_WRITE_Handler(IN PDEVICE_OBJECT fdo,
IN PIRP pIrp)
{
NTSTATUS status;
PDEVICE_EXTENSION pdx;
PIO_STACK_LOCATION pIrpStack;
ULONG Slot = 0;
PVOID IoBuffer;
PULONG LongBuffer;
PUSHORT ShortBuffer;
PUCHAR CharBuffer;
ULONG PortBaseAddress;
PSATAREG48 SATAREG48Buffer;
PHYSICAL_ADDRESS PIOWriteBuffer,PAddress;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -