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

📄 test1device.cpp

📁 这是一个pc与avr通过D12进行USB通讯的简单实例
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//		This routine implements the D12_DRIVER_READ function.
//		This routine runs at passive level.
//

NTSTATUS Test1Device::D12_DRIVER_READ_Handler(KIrp I)
{
	PURB pUrb;
	NTSTATUS status;

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

// TODO:	Handle the the D12_DRIVER_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.
	// Always ok to read 0 elements.
	if (I.IoctlOutputBufferSize() == 0)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_SUCCESS);
	}

	// Declare a memory object

    ULONG dwTotalSize = I.IoctlInputBufferSize(CURRENT);
	ULONG dwMaxSize = m_Endpoint1In.MaximumTransferSize();

	// If the total requested read size is greater than the Maximum Transfer
	// Size for the Pipe, request to read only the Maximum Transfer Size since
	// the bus driver will fail an URB with a TransferBufferLength of greater
	// than the Maximum Transfer Size. 
	if (dwTotalSize > dwMaxSize)
	{
		ASSERT(dwMaxSize);
		dwTotalSize = dwMaxSize;
	}
    ULONG dwBytesRead = 0;

	pUrb = m_Endpoint1In.BuildInterruptTransfer(
				(unsigned char *)I.IoctlBuffer(),		// transfer buffer
				I.IoctlInputBufferSize(),								// transfer buffer size
				TRUE,							// Short Ok
				NULL,							// link urb
				NULL							// new urb
				);

	if ( pUrb != NULL)
	{
	    // Submit the URB to our USB device, synchronously - say less is OK
//		pUrb->UrbBulkOrInterruptTransfer.TransferFlags =
//				(USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK);

        status = m_Endpoint1In.SubmitUrb(pUrb, NULL, NULL,300);

        if ( NT_SUCCESS(status) ) 
        {
	            dwBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;

				if (dwBytesRead > 0) 
					t << "Read got " << dwTotalSize	<< " bytes from USB\n";
	    }

		delete pUrb;
	}
	else
	{
		status=STATUS_INSUFFICIENT_RESOURCES;
	}

	
    I.Information() = dwBytesRead;
    return status;
}

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

NTSTATUS Test1Device::D12_DRIVER_WRITE_Handler(KIrp I)
{
	PURB pUrb;
	NTSTATUS status;

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

// TODO:	Handle the the D12_DRIVER_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.
	if (I.IoctlOutputBufferSize() == 0)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_SUCCESS);
	}

	// Declare a memory object

    ULONG dwTotalSize = I.IoctlOutputBufferSize(CURRENT);
	ULONG dwMaxSize = m_Endpoint1Out.MaximumTransferSize();

	// If the total requested read size is greater than the Maximum Transfer
	// Size for the Pipe, request to read only the Maximum Transfer Size since
	// the bus driver will fail an URB with a TransferBufferLength of greater
	// than the Maximum Transfer Size. 
	if (dwTotalSize > dwMaxSize)
	{
		ASSERT(dwMaxSize);
		dwTotalSize = dwMaxSize;
	}
    ULONG dwBytesRead = 0;

	pUrb = m_Endpoint1Out.BuildInterruptTransfer(
				(unsigned char *)I.IoctlBuffer(),		// transfer buffer
				I.IoctlOutputBufferSize(),								// transfer buffer size
				TRUE,							// Short Ok
				NULL,							// link urb
				NULL							// new urb
				);

	if ( pUrb != NULL)
	{
	    // Submit the URB to our USB device, synchronously - say less is OK

        status = m_Endpoint1Out.SubmitUrb(pUrb, NULL, NULL);

        if ( NT_SUCCESS(status) ) 
        {
	            dwBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;

				if (dwBytesRead > 0) 
					t << "Write got " << dwTotalSize	<< " bytes from USB\n";
	    }

		delete pUrb;
	}
	else
	{
		status=STATUS_INSUFFICIENT_RESOURCES;
	}

	
    I.Information() = dwBytesRead;
    return status;
}

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

NTSTATUS Test1Device::D12_DRIVER_BULK_IN_Handler(KIrp I)
{
	PURB pUrb;
	NTSTATUS status = STATUS_SUCCESS;

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

// TODO:	Handle the the D12_DRIVER_BULK_IN 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.
	if (I.IoctlOutputBufferSize() == 0)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_SUCCESS);
	}

	// Declare a memory object

    ULONG dwTotalSize = I.IoctlInputBufferSize(CURRENT);
	ULONG dwMaxSize = m_Endpoint2In.MaximumTransferSize();

	// If the total requested read size is greater than the Maximum Transfer
	// Size for the Pipe, request to read only the Maximum Transfer Size since
	// the bus driver will fail an URB with a TransferBufferLength of greater
	// than the Maximum Transfer Size. 
	if (dwTotalSize > dwMaxSize)
	{
		ASSERT(dwMaxSize);
		dwTotalSize = dwMaxSize;
	}
    ULONG dwBytesRead = 0;
	// Create an URB to do actual Bulk read from the pipe
	pUrb = m_Endpoint2In.BuildBulkTransfer(
				(unsigned char *)I.IoctlBuffer(),		// transfer buffer
				dwTotalSize,								// transfer buffer size
				TRUE,							// Host To Device
				NULL,							// link urb
				FALSE,							// Short Ok
				NULL						// new urb
				);

	if ( pUrb != NULL)
	{
	    // Submit the URB to our USB device, synchronously - say less is OK
//		pUrb->UrbBulkOrInterruptTransfer.TransferFlags =
//				(USBD_TRANSFER_DIRECTION_IN | USBD_SHORT_TRANSFER_OK);

        status = m_Endpoint2In.SubmitUrb(pUrb, NULL, NULL,300);

        if ( NT_SUCCESS(status) ) 
        {
	            dwBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;

				if (dwBytesRead > 0) 
					t << "BULK_IN got " << dwTotalSize	<< " bytes from USB\n";
	    }

		delete pUrb;
	}
	else
	{
		status=STATUS_INSUFFICIENT_RESOURCES;
	}

	
    I.Information() = dwBytesRead;
    return status;
}

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

NTSTATUS Test1Device::D12_DRIVER_BULK_OUT_Handler(KIrp I)
{
	PURB pUrb;
	NTSTATUS status;

	t << "D12_DRIVER_BULK_OUT_Handler, " << I << EOL ;
	if (I.IoctlOutputBufferSize() == 0)
	{
		I.Information() = 0;
		return I.PnpComplete(this, STATUS_SUCCESS);
	}

	// Declare a memory object

    ULONG dwTotalSize = I.IoctlOutputBufferSize(CURRENT);
	ULONG dwMaxSize = m_Endpoint2Out.MaximumTransferSize();

	// If the total requested read size is greater than the Maximum Transfer
	// Size for the Pipe, request to read only the Maximum Transfer Size since
	// the bus driver will fail an URB with a TransferBufferLength of greater
	// than the Maximum Transfer Size. 
	if (dwTotalSize > dwMaxSize)
	{
		ASSERT(dwMaxSize);
		dwTotalSize = dwMaxSize;
	}
    ULONG dwBytesRead = 0;


	pUrb = m_Endpoint2Out.BuildBulkTransfer(
				(unsigned char *)I.IoctlBuffer(),		// transfer buffer
				I.IoctlOutputBufferSize(),		// transfer buffer size
				FALSE,							// Device To Host
				NULL,							// link urb
				FALSE,							// Short Ok
				NULL							// new urb
				);

	if ( pUrb != NULL)
	{
	    // Submit the URB to our USB device, synchronously - say less is OK

        status = m_Endpoint2Out.SubmitUrb(pUrb, NULL, NULL);

        if ( NT_SUCCESS(status) ) 
        {
	            dwBytesRead = pUrb->UrbBulkOrInterruptTransfer.TransferBufferLength;

				if (dwBytesRead > 0) 
					t << "BULK_OUT got " << dwTotalSize	<< " bytes from USB\n";
	    }

		delete pUrb;
	}
	else
	{
		status=STATUS_INSUFFICIENT_RESOURCES;
	}
	
    I.Information() = dwBytesRead;
    return status;
}

NTSTATUS Test1Device::OnQueryCapabilities(KIrp I)
{
	I.CopyParametersDown();
	I.SetCompletionRoutine(LinkTo(OnQueryCapabilitiesComplete), this);
	return m_Lower.PnpCall(this, I);
}

NTSTATUS Test1Device::OnQueryCapabilitiesComplete(KIrp I)
{
	t << "Capabilities Completion routine\n";
	if (I->PendingReturned)
		I.MarkPending();
  	I.DeviceCapabilities()->SurpriseRemovalOK = TRUE;
	return STATUS_SUCCESS;
}

VOID Test1Device::Cancel(KIrp I)
{
	t << "Cancel(KIrp I)\n";
	if ( (PIRP)I == CurrentIrp() )
	{
		CurrentIrp() = NULL;
		CancelSpinLock::Release(I.CancelIrql());
		I.Information() = 0;
		I.PnpComplete(this, STATUS_CANCELLED);
		IncrementOutstandingRequestCount();
		m_pItem.Queue(LinkTo(Workitem), this);
	}
	else
		CancelSpinLock::Release(I.CancelIrql());
}

VOID Test1Device::Workitem()
{
	m_Endpoint1In.Abort();
	m_Endpoint1Out.Abort();
	m_Endpoint2In.Abort();
	m_Endpoint2Out.Abort();
	DecrementOutstandingRequestCount();
}





⌨️ 快捷键说明

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