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

📄 bulkusbdevice.cpp

📁 usb块传输的驱动程序例子。 可以作为参考。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// BulkUsbDevice.cpp
//
// Generated by DriverWizard 3.2.0 (Build 2485)
// Requires DDK and DriverWorks
// File created on 5/21/2007
//
// This source file contains the implementation of a subclass of KDevice.
// WDM drivers implement a subclass of KPnpDevice and override member
// functions to handle requests (IRPs) from the system.
//

#include <vdw.h>
#include <kusb.h>
#include <kusbbusintf.h>
#include "BulkUsbDriver.h"
#include "BulkUsbDevice.h"
#include "..\\intrface.h"

#pragma hdrstop("BulkUsb.pch")

// Global driver trace object
// TODO:	Use KDebugOnlyTrace if you want trace messages
//			to appear only in checked builds.  Use KTrace if
//			you want trace messages to always appear.  Call
//			method SetOutputLevel to set the output threshold.
extern KDebugOnlyTrace T;

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::BulkUsbDevice
//		This is the constructor for the class representing the Functional
//		Device Object, or FDO.  It is derived from KPnpDevice, which builds
//		in automatic dispatching of subfunctions of IRP_MJ_POWER and IRP_MJ_PNP
//		to virtual member functions.
//		The object being constructed contains a data member (m_Lower) of type
//		KPnpLowerDevice. By initializing it, the driver binds the FDO to the
//		PDO and creates an interface to the upper edge of the system class driver.
//
//	Arguments:
//		IN	Pdo
//				Physical Device Object.  This is a pointer to a system
//				device object that represents the physical device.
//
//		IN	Unit
//				Unit number to append to the device's base device name 
//				to distinguish multiple units of this device type.
//
//	Return Value:
//		none
//
BulkUsbDevice::BulkUsbDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
	KPnpDevice(Pdo, &GUID_DEVINTERFACE_BULKUSB)
{
	if (!NT_SUCCESS(m_ConstructorStatus))
	{
		T.Trace(TraceError, __FUNCTION__": Failed to create device BulkUsbDevice"
			" unit number %d status %x\n", Unit, m_ConstructorStatus);
		ASSERT(FALSE);
		return;
	}

	// Initialize the lower device
	m_Lower.Initialize(this, Pdo);

	// Initialize the interface object.  The wizard generates code 
	// to support a single interface.  You may create and add additional 
	// interfaces.  By default, the wizard uses InterfaceNumber 0 (the 
	// first interface descriptor), ConfigurationValue 1 (the first
	// configuration descriptor), and initial interface alternate
	// setting of 0.  If your device has multiple interfaces or alternate
	// settings for an interface, you can add additional KUsbInterface
	// objects or adjust the parameters passed to this function.
	m_Interface.Initialize(
					m_Lower, //KUsbLowerDevice
					0,		 //InterfaceNumber
					1,		 //ConfigurationValue 
					0		 //Initial Interface Alternate Setting
					); 

	// Initialize each Pipe object
	Pipe1.Initialize(m_Lower, 81, 64);	
	Pipe2.Initialize(m_Lower, 1, 64);	
	Pipe3.Initialize(m_Lower, 82, 64);	
	Pipe4.Initialize(m_Lower, 2, 64);	

#if (_WDM_ && (WDM_MAJORVERSION > 1 ||((WDM_MAJORVERSION == 1) && (WDM_MINORVERSION >= 0x20))))
	// Initialize USB direct client access interface
	if (STATUS_SUCCESS == m_BusIntf.Initialize(m_Lower.TopOfStack()))
		m_fBusIntfAvailable = TRUE;
	else
		m_fBusIntfAvailable = FALSE;
#endif

	// Inform the base class of the lower edge device object
	SetLowerDevice(&m_Lower);

	// Initialize the PnP Policy settings to the "standard" policy
	SetPnpPolicy();

	// TODO: Customize the PnP Policy for this device by setting
	//		 flags in m_Policies.

	// Initialize the Power Policy settings to the "standard" policy
	SetPowerPolicy();

	// TODO: Customize the Power Policy for this device by setting
	//		 flags in m_PowerPolicies.

}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::~BulkUsbDevice
//		This is the destructor for the class. 
//
//	Arguments:
//		none
//
//	Return Value:
//		none
//
BulkUsbDevice::~BulkUsbDevice()
{
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::DefaultPnp
//		Default handler for IRP_MJ_PNP. 
//		This routine just passes the IRP through to the lower device. IRPs 
//		that correspond to any virtual members of KpnpDevice that handle 
//		minor functions of IRP_MJ_PNP and that are not overridden get 
//		passed to this routine.
//
//	Arguments:
//		IN	I
//			the plug and play IRP
//
//	Return Value:
//		NTSTATUS
//			Result returned from lower device
//
NTSTATUS BulkUsbDevice::DefaultPnp(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);
	T << I; 

	I.ForceReuseOfCurrentStackLocationInCalldown();
	NTSTATUS status = m_Lower.PnpCall(this, I);

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

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::DefaultPower
//		Default handler for IRP_MJ_POWER. 
//		This routine just passes the IRP through to the lower device. IRPs 
//		that correspond to any virtual members of KpnpDevice that handle 
//		minor functions of IRP_MJ_POWER and that are not overridden get 
//		passed to this routine.
//
//	Arguments:
//		IN	I
//			the power IRP
//
//	Return Value:
//		NTSTATUS
//			Result returned from lower device
//
NTSTATUS BulkUsbDevice::DefaultPower(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);
	T << I; 

	I.IndicatePowerIrpProcessed();
	I.CopyParametersDown();
	NTSTATUS status = m_Lower.PnpPowerCall(this, I);

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

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::SystemControl
//		Default handler for IRP_MJ_SYSTEM_CONTROL. 
//		This routine just passes the IRP through to the next device since 
//		this driver is not a WMI provider.
//
//	Arguments:
//		IN	I
//			the system control (WMI) IRP
//
//	Return Value:
//		NTSTATUS
//			Result returned from lower device
//
NTSTATUS BulkUsbDevice::SystemControl(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);
	T << I; 

	NTSTATUS status = STATUS_SUCCESS;

	I.ForceReuseOfCurrentStackLocationInCalldown();
	status = m_Lower.PnpCall(this, I);

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

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::OnStartDevice
//		Handler for IRP_MJ_PNP subfcn IRP_MN_START_DEVICE. 
//		Initialize the hardware device. Typically, the driver initializes
//		physical resources here.  Call I.AllocatedResources() for a list
//		of the raw resources that the system has assigned to the device,
//		or I.TranslatedResources() for the translated resource list.
//
//	Arguments:
//		IN	I
//			the start device IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS BulkUsbDevice::OnStartDevice(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	I.Information() = 0;
	status = STATUS_UNSUCCESSFUL;

	AC_STATUS acStatus = AC_SUCCESS;

	// By default, the wizard passes a ConfigurationValue of 1 to 
	// ActivateConfiguration().  This corresponds to the first configuration 
	// that the device reports in its configuration descriptor.  If the device 
	// supports multiple configurations, pass the appropriate
	// ConfigurationValue of the configuration to activate here.
	acStatus = m_Lower.ActivateConfiguration(
		1	// ConfigurationValue 1 (the first configuration)
		);

	switch (acStatus)
	{
		case AC_SUCCESS:
			T << "USB ActivateConfiguration OK\n";
			status = STATUS_SUCCESS;

			// TODO: Use the USB direct client access			
			TestBusInterface();
			break;

		case AC_COULD_NOT_LOCATE_INTERFACE:
			T << "ActivateConfiguration Error: Could not locate interface\n";
			break;

		case AC_COULD_NOT_PRECONFIGURE_INTERFACE:
			T << "ActivateConfiguration Error: Could not get configuration descriptor\n";
			break;

		case AC_CONFIGURATION_REQUEST_FAILED:
			T << "ActivateConfiguration Error: H/W did not accept configuration URB\n";
			break;

		case AC_FAILED_TO_INITIALIZE_INTERFACE_OBJECT:
			T << "ActivateConfiguration Error: Failed to initialize interface object\n";
			break;

		case AC_FAILED_TO_GET_DESCRIPTOR:
			T << "ActivateConfiguration Error: Failed to get device descriptor\n";
			break;

		case AC_FAILED_TO_OPEN_PIPE_OBJECT:
			// NOTE: this may not be an error.  It could mean that
			// the device has an endpoint for which a KUsbPipe object has
			// not been instanced.  If the intention is to not use this endpoint,
			// then it's likely not a problem.  
			status = STATUS_SUCCESS;
			T << "ActivateConfiguration Warning: Failed to open pipe object\n";
			break;

		default:
			T << "ActivateConfiguration Error: Unexpected error activating USB configuration\n";
			break;
	}

	// TODO:	Add USB device-specific code to start the hardware.

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

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::OnStopDevice
//		Handler for IRP_MJ_PNP subfcn IRP_MN_STOP_DEVICE. 
//		The system calls this when the device is stopped.  Release any 
//		hardware resources in this routine.
//
//	Arguments:
//		IN	I
//			the stop device IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS BulkUsbDevice::OnStopDevice(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	// TODO: Add device-specific code to stop your hardware device.
    // The base class will handle completion of the IRP

	// Release the system resources
	Invalidate();

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

	// Can't fail this IRP
	return STATUS_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::OnRemoveDevice
//		Handler for IRP_MJ_PNP subfcn IRP_MN_REMOVE_DEVICE. 
//		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
//
//	Arguments:
//		IN	I
//			the remove device IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS BulkUsbDevice::OnRemoveDevice(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	// TODO: Add device-specific code to remove your hardware device.
    // The base class will handle completion of the IRP

	// Release the system resources
	Invalidate();

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

	// Can't fail this IRP
	return STATUS_SUCCESS;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::OnQueryCapabilities
//		Handler for IRP_MJ_PNP subfcn IRP_MN_QUERY_CAPABILITIES. 
//		The system calls this to query the device capabilities.
//		The Bus driver fills in the device capabilities structure.
//		This method is usually only overridden to alter the 
//		device capabilities reported by the bus driver.
//
//	Arguments:
//		IN	I
//			the query capabilities IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS BulkUsbDevice::OnQueryCapabilities(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.  IRP %p\n", I);

	NTSTATUS status = STATUS_SUCCESS;

	I.CopyParametersDown();
	I.SetCompletionRoutine(LinkTo(OnQueryCapabilitiesComplete), this, TRUE, TRUE, TRUE);

	status = m_Lower.PnpCall(this, I);

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

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::OnQueryCapabilitiesComplete
//		Completion routine for IRP_MJ_PNP subfcn IRP_MN_QUERY_CAPABILITIES. 
//		The system calls OnQueryCapabilities to query the device capabilities.
//		The Bus driver fills in the device capabilities structure.
//		This method is called when the IRP is completed to alter the 
//		device capabilities reported by the bus driver.
//
//	Arguments:
//		IN	I
//			the query capabilities IRP
//
//	Return Value:
//		NTSTATUS
//
NTSTATUS BulkUsbDevice::OnQueryCapabilitiesComplete(KIrp I)
{
	T.Trace(TraceInfo, __FUNCTION__"++.");

	NTSTATUS status = I.Status();


	if (I->PendingReturned)
		I.MarkPending();

	if (NT_SUCCESS(status)) 
	{
		// TODO: Adjust device capabilities structure as required.
		I.DeviceCapabilities()->SurpriseRemovalOK = TRUE;
	}

	T.Trace(TraceInfo, __FUNCTION__"--.");

	return status;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  BulkUsbDevice::TestBusInterface
//		This routine uses the USB direct client interface to 
//		query information.
//
//	Arguments:
//		none
//
//	Return Value:
//		none
//
void BulkUsbDevice::TestBusInterface()
{
#if (_WDM_ && (WDM_MAJORVERSION > 1 ||((WDM_MAJORVERSION == 1) && (WDM_MINORVERSION >= 0x20))))

	if (m_fBusIntfAvailable) 
	{
		T.Trace(TraceInfo, "USB Bus Interface Version: %u\n", m_BusIntf.Version());
		T.Trace(TraceInfo, "High Speed Device: %S\n", (m_BusIntf.IsDeviceHighSpeed()?"TRUE":"FALSE"));

		USBD_VERSION_INFORMATION UsbVerInfo;
		RtlZeroMemory(&UsbVerInfo, sizeof(USBD_VERSION_INFORMATION));
		ULONG HcdCapabilities = 0;

		m_BusIntf.GetUSBDIVersion(&UsbVerInfo, &HcdCapabilities);

		T.Trace(TraceInfo, "USBDI_Version: %u\n", UsbVerInfo.USBDI_Version);
		T.Trace(TraceInfo, "Supported_USB_Version: %u\n", UsbVerInfo.Supported_USB_Version);

		ULONG TotalBW, ConsumedBW;
		NTSTATUS Status = m_BusIntf.GetBandwidth(&TotalBW,&ConsumedBW);

		if (STATUS_SUCCESS == Status) 
		{
			T.Trace(TraceInfo, "Total Bandwidth: %u\n", TotalBW);
			T.Trace(TraceInfo, "Consumed Bandwidth: %u\n", ConsumedBW);
		}

		PWSTR HcName = NULL;
		Status = m_BusIntf.GetControllerName(HcName);

		if (STATUS_SUCCESS == Status && HcName) 
		{

⌨️ 快捷键说明

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