tmx320c6412device.cpp

来自「网友写的一个PCI卡,基于PCI总线的驱动」· C++ 代码 · 共 1,276 行 · 第 1/3 页

CPP
1,276
字号
//
//		The base class passes the irp to the lower device.
//

NTSTATUS TMX320C6412Device::OnStopDevice(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering TMX320C6412Device::OnStopDevice\n";

	// Device stopped, release the system resources.
	Invalidate();

// TODO:	Add device-specific code to stop your device   

	return status;
	
	// The following macro simply allows compilation at Warning Level 4
	// If you reference this parameter in the function simply remove the macro.
	UNREFERENCED_PARAMETER(I);
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::OnRemoveDevice
//
//	Routine Description:
//		Handler for IRP_MJ_PNP subfcn IRP_MN_REMOVE_DEVICE
//
//	Parameters:
//		I - Current IRP
//
//	Return Value:
//		NTSTATUS - Result code
//
//	Comments:
//		The system calls this when the device is removed.
//		Our PnP policy will take care of 
//			(1) giving the IRP to the lower device
//			(2) detaching the PDO
//			(3) deleting the device object
//

NTSTATUS TMX320C6412Device::OnRemoveDevice(KIrp I)
{
	t << "Entering TMX320C6412Device::OnRemoveDevice\n";

	// Device removed, release the system resources.
	Invalidate();



// TODO:	Add device-specific code to remove your device   

	return STATUS_SUCCESS;

	// The following macro simply allows compilation at Warning Level 4
	// If you reference this parameter in the function simply remove the macro.
	UNREFERENCED_PARAMETER(I);
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::OnDevicePowerUp
//
//	Routine Description:
//		Handler for IRP_MJ_POWER with minor function IRP_MN_SET_POWER
//		for a request to go to power on state from low power state
//
//	Parameters:
//		I - IRP containing POWER request
//
//	Return Value:
//		NTSTATUS - Status code indicating success or failure
//
//	Comments:
//		This routine implements the OnDevicePowerUp function.
//		This function was called by the framework from the completion
//		routine of the IRP_MJ_POWER dispatch handler in KPnpDevice.
//		The bus driver has completed the IRP and this driver can now
//		access the hardware device.  
//		This routine runs at dispatch level.
//	

NTSTATUS TMX320C6412Device::OnDevicePowerUp(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering TMX320C6412Device::OnDevicePowerUp\n";

// TODO:	Service the device.
//			Restore any context to the hardware device that
//			was saved during the handling of a power down request.
//			See the OnDeviceSleep function.
//			Do NOT complete this IRP.
//
	return status;

	// The following macro simply allows compilation at Warning Level 4
	// If you reference this parameter in the function simply remove the macro.
	UNREFERENCED_PARAMETER(I);
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::OnDeviceSleep
//
//	Routine Description:
//		Handler for IRP_MJ_POWER with minor function IRP_MN_SET_POWER
//		for a request to go to a low power state from a high power state
//
//	Parameters:
//		I - IRP containing POWER request
//
//	Return Value:
//		NTSTATUS - Status code indicating success or failure
//
//	Comments:
//		This routine implements the OnDeviceSleep function.
//		This function was called by the framework from the IRP_MJ_POWER 
//		dispatch handler in KPnpDevice prior to forwarding to the PDO.
//		The hardware has yet to be powered down and this driver can now
//		access the hardware device.  
//		This routine runs at passive level.
//	

NTSTATUS TMX320C6412Device::OnDeviceSleep(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering TMX320C6412Device::OnDeviceSleep\n";

// TODO:	Service the device.
//			Save any context to the hardware device that will be required 
//			during a power up request. See the OnDevicePowerUp function.
//			Do NOT complete this IRP.  The base class handles forwarding
//			this IRP to the PDO.
//
	return status;

	// The following macro simply allows compilation at Warning Level 4
	// If you reference this parameter in the function simply remove the macro.
	UNREFERENCED_PARAMETER(I);
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::Create
//
//	Routine Description:
//		Handler for IRP_MJ_CREATE
//
//	Parameters:
//		I - Current IRP
//
//	Return Value:
//		NTSTATUS - Result code
//
//	Comments:
//

NTSTATUS TMX320C6412Device::Create(KIrp I)
{
	NTSTATUS status;

	t << "Entering TMX320C6412Device::Create, " << I << EOL;

// TODO: Add driver specific create handling code here

	// Generally a create IRP is targeted at our FDO, so we don't need
	// to pass it down to the PDO.  We have found for some devices, the
	// PDO is not expecting this Irp and returns an error code.
	// The default wizard code, therefore completes the Irp here using
	// PnpComplete().  The following commented code could be used instead
	// of PnpComplete() to pass the Irp to the PDO, which would complete it.
	//
//	I.ForceReuseOfCurrentStackLocationInCalldown();
//	status = m_Lower.PnpCall(this, I);

	status = I.PnpComplete(this, STATUS_SUCCESS, IO_NO_INCREMENT);

	t << "TMX320C6412Device::Create Status " << (ULONG)status << EOL;

	return status;
}


////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::Close
//
//	Routine Description:
//		Handler for IRP_MJ_CLOSE
//
//	Parameters:
//		I - Current IRP
//
//	Return Value:
//		NTSTATUS - Result code
//
//	Comments:
//

NTSTATUS TMX320C6412Device::Close(KIrp I)
{
	NTSTATUS status;

	t << "Entering TMX320C6412Device::Close, " << I << EOL;

// TODO: Add driver specific close handling code here

	// Generally a close IRP is targeted at our FDO, so we don't need
	// to pass it down to the PDO.  We have found for some devices, the
	// PDO is not expecting this Irp and returns an error code.
	// The default wizard code, therefore completes the Irp here using
	// PnpComplete().  The following commented code could be used instead
	// of PnpComplete() to pass the Irp to the PDO, which would complete it.
	//
//	I.ForceReuseOfCurrentStackLocationInCalldown();
//	status = m_Lower.PnpCall(this, I);

	status = I.PnpComplete(this, STATUS_SUCCESS, IO_NO_INCREMENT);

	t << "TMX320C6412Device::Close Status " << (ULONG)status << EOL;

    return status;
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::Cleanup
//
//	Routine Description:
//		Handler for IRP_MJ_CLEANUP	
//
//	Parameters:
//		I - Current IRP
//
//	Return Value:
//		NTSTATUS - Result code
//
//	Comments:
//

NTSTATUS TMX320C6412Device::CleanUp(KIrp I)
{
	t << "Entering CleanUp, " << I << EOL;

// TODO:	Insert your code to respond to the CLEANUP message.
//			This code cleans up the single Wizard created queue.  If you
//			have created additional queues,	or have any outstanding Irps
//			stored in some other fashion in your driver, you should clean
//			these up as well for the file object specified in the cleanup Irp.

	m_DriverManagedQueue.PnpCleanUp(this, I.FileObject());
	return I.PnpComplete(this, STATUS_SUCCESS);
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::DeviceControl
//
//	Routine Description:
//		Handler for IRP_MJ_DEVICE_CONTROL
//
//	Parameters:
//		I - Current IRP
// 
//	Return Value:
//		None
//
//	Comments:
//		This routine is the first handler for Device Control requests.
//		Some function codes may be handled immediately, 
//		while others may be serialized through the StartIo routine.
//		
//		The KPnpDevice class handles restricting IRP flow
//		if the device is stopping or being removed.
//

NTSTATUS TMX320C6412Device::DeviceControl(KIrp I) 
{
	NTSTATUS status;

	t << "Entering TMX320C6412Device::Device Control, " << I << EOL;
	switch (I.IoctlCode())
	{
		case TMX320C6412_IOCTL_CFG_READ:
			status = TMX320C6412_IOCTL_CFG_READ_Handler(I);
			break;

		case TMX320C6412_IOCTL_CFG_WRITE:
			status = TMX320C6412_IOCTL_CFG_WRITE_Handler(I);
			break;

		case TMX320C6412_IOCTL_BASE0_READ:
			status = TMX320C6412_IOCTL_BASE0_READ_Handler(I);
			break;

		case TMX320C6412_IOCTL_BASE0_WRITE:
			status = TMX320C6412_IOCTL_BASE0_WRITE_Handler(I);
			break;

		case TMX320C6412_IOCTL_BASE1_READ:
			status = TMX320C6412_IOCTL_BASE1_READ_Handler(I);
			break;

		case TMX320C6412_IOCTL_BASE1_WRITE:
			status = TMX320C6412_IOCTL_BASE1_WRITE_Handler(I);
			break;

		case TMX320C6412_IOCTL_BASE2_READ:
			status = TMX320C6412_IOCTL_BASE2_READ_Handler(I);
			break;

		case TMX320C6412_IOCTL_BASE2_WRITE:
			status = TMX320C6412_IOCTL_BASE2_WRITE_Handler(I);
			break;

		default:
			// Unrecognized IOCTL request
			status = STATUS_INVALID_PARAMETER;
			break;
	}

	// If the IRP was queued, or its IOCTL handler deferred processing using some
	// driver specific scheme, the status variable is set to STATUS_PENDING.
	// In this case we simply return that status, and the IRP will be completed
	// later.  Otherwise, complete the IRP using the status returned by the
	// IOCTL handler.
	if (status == STATUS_PENDING)
	{
		return status;
	}
	else
	{
		return I.PnpComplete(this, status);
	}
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::TMX320C6412_IOCTL_CFG_READ_Handler
//
//	Routine Description:
//		Handler for IO Control Code TMX320C6412_IOCTL_CFG_READ
//
//	Parameters:
//		I - IRP containing IOCTL request
//
//	Return Value:
//		NTSTATUS - Status code indicating success or failure
//
//	Comments:
//		This routine implements the TMX320C6412_IOCTL_CFG_READ function.
//		This routine runs at passive level.
//

NTSTATUS TMX320C6412Device::TMX320C6412_IOCTL_CFG_READ_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering TMX320C6412Device::TMX320C6412_IOCTL_CFG_READ_Handler, " << I << EOL;
// TODO:	Verify that the input parameters are correct
//			If not, return STATUS_INVALID_PARAMETER

// TODO:	Handle the the TMX320C6412_IOCTL_CFG_READ request, or 
//			defer the processing of the IRP (i.e. by queuing) and set
//			status to STATUS_PENDING.

// TODO:	Assuming that the request was handled here. Set I.Information
//			to indicate how much data to copy back to the user.

	KMemory Mem(I.Mdl());
	PUCHAR pOutBuffer = (PUCHAR) Mem.MapToSystemSpace();  //输出缓冲区

	m_PciCfg.ReadHeader (pOutBuffer, 0, sizeof(PCI_CONFIG_HEADER_0));

	I.Information() = sizeof(PCI_CONFIG_HEADER_0);

	return status;
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::TMX320C6412_IOCTL_CFG_WRITE_Handler
//
//	Routine Description:
//		Handler for IO Control Code TMX320C6412_IOCTL_CFG_WRITE
//
//	Parameters:
//		I - IRP containing IOCTL request
//
//	Return Value:
//		NTSTATUS - Status code indicating success or failure
//
//	Comments:
//		This routine implements the TMX320C6412_IOCTL_CFG_WRITE function.
//		This routine runs at passive level.
//

NTSTATUS TMX320C6412Device::TMX320C6412_IOCTL_CFG_WRITE_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering TMX320C6412Device::TMX320C6412_IOCTL_CFG_WRITE_Handler, " << I << EOL;
// TODO:	Verify that the input parameters are correct
//			If not, return STATUS_INVALID_PARAMETER

// TODO:	Handle the the TMX320C6412_IOCTL_CFG_WRITE request, or 
//			defer the processing of the IRP (i.e. by queuing) and set
//			status to STATUS_PENDING.

// TODO:	Assuming that the request was handled here. Set I.Information
//			to indicate how much data to copy back to the user.
	PUCHAR pInBuffer = (PUCHAR) I.IoctlBuffer();     //输入缓冲区
	ULONG StartOffset = 0, Count = 0;

	memcpy (&StartOffset, pInBuffer, sizeof (ULONG));
	memcpy (&Count, pInBuffer+4, sizeof (ULONG));

	m_PciCfg.WriteHeader (pInBuffer+8, StartOffset, Count);

	I.Information() = Count;

	return status;
}

////////////////////////////////////////////////////////////////////////
//  TMX320C6412Device::TMX320C6412_IOCTL_BASE0_READ_Handler
//
//	Routine Description:
//		Handler for IO Control Code TMX320C6412_IOCTL_BASE0_READ
//
//	Parameters:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?