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

📄 computer00usbdevice.cpp

📁 usb自定义设备编程
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// Computer00UsbDevice.cpp
//
// Generated by DriverWizard 3.2.0 (Build 2485)
// Requires DDK and DriverWorks
// File created on 8/22/2008
//
// 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 "Computer00UsbDriver.h"
#include "Computer00UsbDevice.h"
#include "..\\intrface.h"

#pragma hdrstop("Computer00Usb.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;

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::Computer00UsbDevice
//		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
//
Computer00UsbDevice::Computer00UsbDevice(PDEVICE_OBJECT Pdo, ULONG Unit) :
	DeviceName(NULL),
	KPnpDevice(Pdo, &GUID_DEVINTERFACE_COMPUTER00USB)
{
	if (!NT_SUCCESS(m_ConstructorStatus))
	{
		T.Trace(TraceError, __FUNCTION__": Failed to create device Computer00UsbDevice"
			" 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
	//Ep1In.Initialize(m_Lower, 81, 8);	
	//Ep1Out.Initialize(m_Lower, 1, 8);	
	//Ep2In.Initialize(m_Lower, 82, 64);	
	//Ep2Out.Initialize(m_Lower, 2, 64);	

//原来代码有误,修改如下
	Ep1In.Initialize(m_Lower, 0x81, 8);	
	Ep1Out.Initialize(m_Lower, 0x01, 8);	
	Ep2In.Initialize(m_Lower, 0x82, 64);	
	Ep2Out.Initialize(m_Lower, 0x02, 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.


	LoadRegistryParameters();
}

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

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::LoadRegistryParameters
//		Read the registry to initialize driver data members.
//
//	Arguments:
//		none
//
//	Return Value:
//		none
//
void Computer00UsbDevice::LoadRegistryParameters()
{
    // Read the driver's registry values from the registry
	NTSTATUS status = STATUS_SUCCESS;
	UNICODE_STRING	regPath;
	KRegistryKey	RegKey;
    ULONG			length;


	status = RegKey.Initialize(
				m_Lower.DeviceObject(),
                PLUGPLAY_REGKEY_DRIVER,
                KEY_ALL_ACCESS
				);

	if (!NT_SUCCESS(status))
	{
		T.Trace(TraceWarning, __FUNCTION__":: Failed to open root key for DeviceName %x", status);
	}
	else
	{
		length = 0;

		// Attempt to read the value key
		status = RegKey.QueryValue(
                L"DeviceName", 
				DeviceName,
				length,
				PagedPool
				);

        if (NT_SUCCESS(status) && DeviceName != NULL)
        {
            T.Trace(TraceInfo, __FUNCTION__ ": RegKey.QueryValue returned %d bytes, DeviceName = %S\n", length, DeviceName);
        }
		else
		{
            T.Trace(TraceWarning, __FUNCTION__ ": RegKey.QueryValue failed to get DeviceName, STATUS %x\n", status);
		}
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
//  Computer00UsbDevice::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 Computer00UsbDevice::OnQueryCapabilities(KIrp I)
{

⌨️ 快捷键说明

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