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

📄 computer00usbdevice.cpp

📁 使用driverstudio开发的一个USB驱动程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	}

	I.PnpComplete(this, status);

	T.Trace(TraceInfo, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::DeviceControl
//		Dispatch routine for IRP_MJ_DEVICE_CONTROL requests.  
//
//	Arguments:
//		IN I 
//			the ioctl IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS Computer00USBDevice::DeviceControl(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	switch (I.IoctlCode())
	{

    case READ_ENDP1:
		status = READ_ENDP1_Handler(I);
		break;

    case WRITE_ENDP1:
		status = WRITE_ENDP1_Handler(I);
		break;

    case READ_ENDP2:
		status = READ_ENDP2_Handler(I);
		break;

    case WRITE_ENDP2:
		status = WRITE_ENDP2_Handler(I);
		break;

	default:
		status = STATUS_INVALID_DEVICE_REQUEST;
		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)
	{
		I.PnpComplete(this, status);
	}

	T.Trace(TraceInfo, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::READ_ENDP1_Handler
//		Handler for ioctl READ_ENDP1.  The DeviceControl
//		method will complete the IRP.
//
//	Arguments:
//		IN I 
//			the ioctl IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS Computer00USBDevice::READ_ENDP1_Handler(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	ULONG inputSize = I.IoctlInputBufferSize();
	ULONG outputSize = I.IoctlOutputBufferSize();

    // Direct ioctl
    PVOID inputBuffer = I.IoctlBuffer();
	PVOID outputBuffer = NULL;

/*    if (I.Mdl() != NULL)
    {
		KMemory Mem(I.Mdl());
		// Note that this routine is safe on all platforms. 
        outputBuffer = Mem.MapToSystemSpace();
    }
*/
	I.Information() = 0;
	// TODO: Validate the parameters of the IRP.  Replace "FALSE"
	//		 in the following line with error checking code that
	//		 evaulates to TRUE if the request is not valid.
	if (I.Mdl() == NULL)
	{
		status = STATUS_INVALID_PARAMETER;
	}
	else
	{
		KMemory Mem(I.Mdl());
		// Note that this routine is safe on all platforms. 
		outputBuffer = Mem.MapToSystemSpace();
		
		// TODO: copy data
		//		 To satisfy the ioctl now, transfer data using the
		//		 caller's buffers at "inputBuffer" and/or "outputBuffer".  
		//		 Then, indicate how much data was transferred:
		PURB pUrb=m_Endpoint1In.BuildInterruptTransfer(	Mem,	//direct方式时,使用KMemory
													outputSize,	//读数据的数据字节数
													TRUE,		//表示设备传输的字节数可以少于指定的字节数
													NULL,		//连接下一个传输的URB,这里没有,置为NULL
													NULL);		//指向一个已经存在的URB。置为NULL,分配一个新的URB

		if(pUrb==NULL)		//如果分配失败
		{
			status=STATUS_INSUFFICIENT_RESOURCES;		//返回资源不足
		}
		else
		{
			status=m_Endpoint1In.SubmitUrb(pUrb,NULL,this,1000);	//提交URB,并设置1s超时
			I.Information() = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;	//实际读到的数据字节数
			delete pUrb;	//删除刚刚分配的URB
		}
	}

	T.Trace(NT_SUCCESS(status)?TraceInfo:TraceWarning, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::WRITE_ENDP1_Handler
//		Handler for ioctl WRITE_ENDP1.  The DeviceControl
//		method will complete the IRP.
//
//	Arguments:
//		IN I 
//			the ioctl IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS Computer00USBDevice::WRITE_ENDP1_Handler(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	ULONG inputSize = I.IoctlInputBufferSize();
	ULONG outputSize = I.IoctlOutputBufferSize();

    // Direct ioctl
    PVOID inputBuffer = I.IoctlBuffer();
	PVOID outputBuffer = NULL;

/*    if (I.Mdl() != NULL)
    {
		KMemory Mem(I.Mdl());
		// Note that this routine is safe on all platforms. 
        outputBuffer = Mem.MapToSystemSpace();
    }
*/
	I.Information() = 0;
	// TODO: Validate the parameters of the IRP.  Replace "FALSE"
	//		 in the following line with error checking code that
	//		 evaulates to TRUE if the request is not valid.
	if (inputBuffer == NULL)
	{
		status = STATUS_INVALID_PARAMETER;
	}
	else
	{
		// TODO: copy data
		//		 To satisfy the ioctl now, transfer data using the
		//		 caller's buffers at "inputBuffer" and/or "outputBuffer".  
		//		 Then, indicate how much data was transferred:
		PURB pUrb=m_Endpoint1Out.BuildInterruptTransfer(	inputBuffer,//direct方式时,使用inputBuffer
															inputSize,	//读数据的数据字节数
															FALSE,		//表示设备传输的字节数不可以少于指定的字节数
															NULL,		//连接下一个传输的URB,这里没有,置为NULL
															NULL);		//指向一个已经存在的URB。置为NULL,分配一个新的URB

		if(pUrb==NULL)		//如果分配失败
		{
			status=STATUS_INSUFFICIENT_RESOURCES;		//返回资源不足
		}
		else
		{
			status=m_Endpoint1Out.SubmitUrb(pUrb,NULL,this,1000);	//提交URB,并设置1s超时
			I.Information() = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;	//实际读到的数据字节数
			delete pUrb;	//删除刚刚分配的URB
		}
	}

	T.Trace(NT_SUCCESS(status)?TraceInfo:TraceWarning, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::READ_ENDP2_Handler
//		Handler for ioctl READ_ENDP2.  The DeviceControl
//		method will complete the IRP.
//
//	Arguments:
//		IN I 
//			the ioctl IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS Computer00USBDevice::READ_ENDP2_Handler(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	ULONG inputSize = I.IoctlInputBufferSize();
	ULONG outputSize = I.IoctlOutputBufferSize();

    // Direct ioctl
    PVOID inputBuffer = I.IoctlBuffer();
	PVOID outputBuffer = NULL;

/*    if (I.Mdl() != NULL)
    {
		KMemory Mem(I.Mdl());
		// Note that this routine is safe on all platforms. 
        outputBuffer = Mem.MapToSystemSpace();
    }
*/
	// TODO: Validate the parameters of the IRP.  Replace "FALSE"
	//		 in the following line with error checking code that
	//		 evaulates to TRUE if the request is not valid.
	I.Information() = 0;

	if (I.Mdl() == NULL)
	{
		status = STATUS_INVALID_PARAMETER;
	}
	else
	{
		KMemory Mem(I.Mdl());
		// Note that this routine is safe on all platforms. 
		outputBuffer = Mem.MapToSystemSpace();
		
		// TODO: copy data
		//		 To satisfy the ioctl now, transfer data using the
		//		 caller's buffers at "inputBuffer" and/or "outputBuffer".  
		//		 Then, indicate how much data was transferred:
		PURB pUrb=m_Endpoint2In.BuildBulkTransfer(	Mem,		//direct方式时,使用KMemory
													outputSize,	//读数据的数据字节数
													TRUE,		//TURE表示读数据
													NULL,		//连接下一个传输的URB,这里没有,置为NULL
													TRUE,		//表示设备传输的字节数可以少于指定的字节数
													NULL);		//指向一个已经存在的URB。置为NULL,分配一个新的URB

		if(pUrb==NULL)		//如果分配失败
		{
			status=STATUS_INSUFFICIENT_RESOURCES;		//返回资源不足
		}
		else
		{
			status=m_Endpoint2In.SubmitUrb(pUrb,NULL,this,1000);	//提交URB,并设置1s超时
			I.Information() = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;	//实际读到的数据字节数
			delete pUrb;	//删除刚刚分配的URB
		}
	}

	T.Trace(NT_SUCCESS(status)?TraceInfo:TraceWarning, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::WRITE_ENDP2_Handler
//		Handler for ioctl WRITE_ENDP2.  The DeviceControl
//		method will complete the IRP.
//
//	Arguments:
//		IN I 
//			the ioctl IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS Computer00USBDevice::WRITE_ENDP2_Handler(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	ULONG inputSize = I.IoctlInputBufferSize();
	ULONG outputSize = I.IoctlOutputBufferSize();

    // Direct ioctl
    PVOID inputBuffer = I.IoctlBuffer();
	PVOID outputBuffer = NULL;

/*    if (I.Mdl() != NULL)
    {
		KMemory Mem(I.Mdl());
		// Note that this routine is safe on all platforms. 
        outputBuffer = Mem.MapToSystemSpace();
    }
*/
	I.Information() = 0;
	// TODO: Validate the parameters of the IRP.  Replace "FALSE"
	//		 in the following line with error checking code that
	//		 evaulates to TRUE if the request is not valid.
	if (inputBuffer == NULL)
	{
		status = STATUS_INVALID_PARAMETER;
	}
	else
	{		
		// TODO: copy data
		//		 To satisfy the ioctl now, transfer data using the
		//		 caller's buffers at "inputBuffer" and/or "outputBuffer".  
		//		 Then, indicate how much data was transferred:
		PURB pUrb=m_Endpoint2Out.BuildBulkTransfer(	inputBuffer,//direct方式时,使用inputBuffer
													inputSize,	//读数据的数据字节数
													FALSE,		//FALSE表示输出数据
													NULL,		//连接下一个传输的URB,这里没有,置为NULL
													FALSE,		//表示设备传输的字节数不可以少于指定的字节数
													NULL);		//指向一个已经存在的URB。置为NULL,分配一个新的URB

		if(pUrb==NULL)		//如果分配失败
		{
			status=STATUS_INSUFFICIENT_RESOURCES;		//返回资源不足
		}
		else
		{
			status=m_Endpoint2Out.SubmitUrb(pUrb,NULL,this,1000);	//提交URB,并设置1s超时
			I.Information() = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;	//实际读到的数据字节数
			delete pUrb;	//删除刚刚分配的URB
		}
	}

	T.Trace(NT_SUCCESS(status)?TraceInfo:TraceWarning, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::CleanUp
//		Dispatch routine for IRP_MJ_CLEANUP requests.  
//
//	Arguments:
//		IN I 
//			the cleanup IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS Computer00USBDevice::CleanUp(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	// TODO: At this point, perform custom processing for IRP_MJ_CLEANUP

	I.PnpComplete(this, status);

	T.Trace(TraceInfo, __FUNCTION__"--.  IRP %p, STATUS %x\n", I, status);

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00USBDevice::Invalidate
//		This method performs resource cleanup.
//		This function is called from OnStopDevice, OnRemoveDevice and
//		OnStartDevice (in error conditions).  It calls the Invalidate
//		member funcitons for each resource to free the underlying system
//		resource if allocated.  It is safe to call Invalidate more than
//		once for a resource, or for an uninitialized resource.
//
//	Arguments:
//		none
//
//	Return Value:
//		none
//
VOID Computer00USBDevice::Invalidate()
{
	NTSTATUS status = STATUS_SUCCESS;

	status = m_Lower.DeActivateConfiguration();
	if (!NT_SUCCESS(status))
	{
		T.Trace(TraceWarning, __FUNCTION__" DeActivateConfiguration failed, STATUS %x\n", status);
	}

	m_Lower.ReleaseResources();

    // Free our registry value buffer
    if (DeviceName != NULL)
    {
        delete DeviceName;
        DeviceName = NULL;
    }

}

⌨️ 快捷键说明

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