⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xxgdrvdevice.cpp

📁 中科院许西刚博士编写的USB的WDM驱动程序。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		return I.PnpComplete(this, STATUS_INSUFFICIENT_RESOURCES);
	}

	// Initialize context structure
	pCompInfo->m_pClass = this;
	pCompInfo->m_pUrb = pUrb;

    // Submit the URB to our USB device
	NTSTATUS status;
	status = m_Endpoint2OUT.SubmitUrb(I, pUrb, LinkTo(WriteComplete), pCompInfo, 0);
	return status;
}

////////////////////////////////////////////////////////////////////////
//  XXGDrvDevice::WriteComplete
//
//	Routine Description:
//		Completion Handler for IRP_MJ_WRITE
//
//	Parameters:
//		I - IRP just completed by USB
//		pContext - Context structure containing pointer to Urb
//
//	Return Value:
//		NTSTATUS	STATUS_SUCCESS
//
//	Comments:
//		This routine is called when USBD completes the write request
//

NTSTATUS XXGDrvDevice::WriteComplete(KIrp I, USB_COMPLETION_INFO* pContext)
{
	// Normal completion routine code to propagate pending flag

	if (I->PendingReturned) 
	{
		I.MarkPending();
	}
	
	NTSTATUS status = I.Status();
	PURB pUrb = pContext->m_pUrb;
	ULONG nBytesWritten = 0;

	if ( NT_SUCCESS(status) ) 
	{
		nBytesWritten = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;
		if (nBytesWritten > 0) 
			t << "Wrote " << nBytesWritten	<< " bytes to USB\n";
    }

	// Deallocate Urb and context structure
	delete pUrb;
	delete pContext;

	// set returned count
	I.Information() = nBytesWritten;
	
	// Plug and Play accounting
	DecrementOutstandingRequestCount();

	// allow IRP completion processing
	return STATUS_SUCCESS;
}

////////////////////////////////////////////////////////////////////////
//  XXGDrvDevice::FlushBuffers
//
//	Routine Description:
//		Handler for IRP_MJ_FLUSHBUFFERS
//
//	Parameters:
//		I - Current IRP
//
//	Return Value:
//		NTSTATUS - Result code
//
//	Comments:
//		This routine handles Flush Buffer requests.
//
//		The KPnpDevice class handles restricting IRP flow
//		if the device is stopping or being removed.
//

NTSTATUS XXGDrvDevice::FlushBuffers(KIrp I) 
{
	//t << "Entering XXGDrvDevice::Flush Buffers, " << I << EOL;
	NTSTATUS status = STATUS_SUCCESS;
	// Cached read data can usually be flushed immediately.
	// Cached write data must be written to the device.
	
	// The IRP should not be completed until the data is written.

// TODO: handle flush buffer request.
	//	 The following code serves as a placeholder by assuming that
	//	 no buffers need to be flushed and completing the IRP immediately.
	I.Information() = 0;

	return I.PnpComplete(this, status);
}

////////////////////////////////////////////////////////////////////////
//  XXGDrvDevice::InternalDeviceControl
//
//	Routine Description:
//		Handler for IRP_MJ_INTERNAL_DEVICE_CONTROL
//
//	Parameters:
//		I - Current IRP
//
//	Return Value:
//		NTSTATUS - Result code
//
//	Comments:
//		This routine handles Internal Device Control requests.
//		The KPnpDevice class handles restricting IRP flow
//		if the device is stopping or being removed.
//

NTSTATUS XXGDrvDevice::InternalDeviceControl(KIrp I)
{
	NTSTATUS status;

	//t << "Entering XXGDrvDevice::Internal Device Control, " << I << EOL;
	switch (I.IoctlCode())
	{
// TODO:	Insert case statements to handle requests specific to this device
//		case IOCTL_CODE:
// Call handler for this request code
//			status = This_Code_Handler(I);
//			break;

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

	// If the IRP's 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);
	}
}

////////////////////////////////////////////////////////////////////////
//  XXGDrvDevice::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.
//		The KPnpDevice class handles restricting IRP flow
//		if the device is stopping or being removed.
//

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

//	t << "Entering XXGDrvDevice::Device Control, " << I << EOL;
	switch (I.IoctlCode())
	{
		case XXGDRV_IOCTL_DRV_VERSION:
			status = XXGDRV_IOCTL_DRV_VERSION_Handler(I);
			break;

		case XXGDRV_IOCTL_DEV_VERSION:
			status = XXGDRV_IOCTL_DEV_VERSION_Handler(I);
			break;

		case XXGDRV_IOCTL_SYS_INIT:
			status = XXGDRV_IOCTL_SYS_INIT_Handler(I);
			break;

		case XXGDRV_IOCTL_DEV_INIT:
			status = XXGDRV_IOCTL_DEV_INIT_Handler(I);
			break;

		case XXGDRV_IOCTL_READ_DATA:
			status = XXGDRV_IOCTL_READ_DATA_Handler(I);
			break;

		case XXGDRV_IOCTL_WRITE_DATA:
			status = XXGDRV_IOCTL_WRITE_DATA_Handler(I);
			break;

		case XXGDRV_IOCTL_SET_CMD:
			status = XXGDRV_IOCTL_SET_CMD_Handler(I);
			break;

		case XXGDRV_IOCTL_GET_CMD:
			status = XXGDRV_IOCTL_GET_CMD_Handler(I);
			break;

		case XXGDRV_IOCTL_CONTROL:
			status = XXGDRV_IOCTL_CONTROL_Handler(I);
			break;

		case XXGDRV_IOCTL_ENABLE_ISO_MODE:
			status = XXGDRV_IOCTL_ENABLE_ISO_MODE_Handler(I);
			break;

		case XXGDRV_IOCTL_READ_WRITE_REGISTER:
			status = XXGDRV_IOCTL_READ_WRITE_REGISTER_Handler(I);
			break;

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

	// If the IRP's 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);
	}
}

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

NTSTATUS XXGDrvDevice::XXGDRV_IOCTL_DRV_VERSION_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

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

// TODO:	Handle the the XXGDRV_IOCTL_DRV_VERSION 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.
	I.Information() = 0;

	return status;
}

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

NTSTATUS XXGDrvDevice::XXGDRV_IOCTL_DEV_VERSION_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

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

// TODO:	Handle the the XXGDRV_IOCTL_DEV_VERSION 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.
	I.Information() = 0;

	return status;
}

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

NTSTATUS XXGDrvDevice::XXGDRV_IOCTL_SYS_INIT_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

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

// TODO:	Handle the the XXGDRV_IOCTL_SYS_INIT 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.
	I.Information() = 0;

	return status;
}

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

NTSTATUS XXGDrvDevice::XXGDRV_IOCTL_DEV_INIT_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;

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

// TODO:	Handle the the XXGDRV_IOCTL_DEV_INIT 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.
	I.Information() = 0;

	return status;
}

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

NTSTATUS XXGDrvDevice::XXGDRV_IOCTL_READ_DATA_Handler(KIrp I)
{
	ULONG ulReturned = 0;
	NTSTATUS status = STATUS_SUCCESS;

	//t << "Entering XXGDrvDevice::XXGDRV_IOCTL_READ_DATA_Handler, " << I << EOL;
// TODO:	Verify that the input parameters are correct
//			If not, return STATUS_INVALID_PARAMETER
	if (I.ReadSize() == 0)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_SUCCESS);
	}

⌨️ 快捷键说明

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