📄 crpnp.c
字号:
//
// Copyright (c) 2004 Golden Bits Software, Inc.
// All rights reserved
// www.goldenbits.com
//
//
// PNP stuff
//
// Here's the Vendor ID I'm using. It's unofficial and just for
// testing purposes.
//
// Vendor ID 7055, ProductID 100
//
#include "ntddk.h"
#include "wmilib.h"
#include "usbdi.h"
#include "usbdlib.h"
#include "CrMain.h"
#include "CrPnp.h"
#include "CrConfig.h"
#include "..\Include\UsbCryptAppInc.h"
// =============== UsbCrypt_AddDevice() ==============
// Desc: Adds device object for this USB driver
//
// Returns: NTSTATUS
//
NTSTATUS UsbCrypt_AddDevice(IN PDRIVER_OBJECT pDriverObject,
IN PDEVICE_OBJECT pPhysicalDevObject)
{
NTSTATUS ntStatus;
PDEVICE_OBJECT pNewDeviceObj;
PUSB_CRYPT_EXT pCryptExt;
POWER_STATE state;
KIRQL oldIrql;
POWER_STATE PowerState;
UNICODE_STRING SymbolicName;
USB_TRACE("UsbCrypt_AddDevice - Start\n");
pNewDeviceObj = NULL;
ntStatus = IoCreateDevice(pDriverObject,
sizeof(USB_CRYPT_EXT ), // our device extension
NULL, // Don't specify a name
// we'll use IoRegisterDeviceInterface()
FILE_DEVICE_UNKNOWN,
FILE_AUTOGENERATED_DEVICE_NAME, // device characteristics
FALSE, // Not exclusive
&pNewDeviceObj); // Our device object
if(!NT_SUCCESS(ntStatus))
{
// Failed to create device object!!!
USB_TRACE1("Failed to create USB Crypt device object, NTSTATUS = 0x%x\n", ntStatus);
return ntStatus;
}
//
// Setup or device extension
//
pCryptExt = (PUSB_CRYPT_EXT)pNewDeviceObj->DeviceExtension;
pCryptExt->pLowerPhysDevObject = pPhysicalDevObject;
pCryptExt->pOurDeviceObject = pNewDeviceObj;
pNewDeviceObj->Flags |= DO_DIRECT_IO;
// Set device PNP State
INITIALIZE_PNP_STATE(pCryptExt);
//
// Init and register WMI
//
pCryptExt->CryptWminfo.GuidCount =
sizeof (USBCryptGuidList) / sizeof (WMIGUIDREGINFO);
pCryptExt->CryptWminfo.GuidList = USBCryptGuidList;
pCryptExt->CryptWminfo.QueryWmiRegInfo = UsbCrypt_QueryWmiRegInfo;
pCryptExt->CryptWminfo.QueryWmiDataBlock = UsbCrypt_QueryWmiDataBlock;
pCryptExt->CryptWminfo.SetWmiDataBlock = UsbCrypt_SetWmiDataBlock;
pCryptExt->CryptWminfo.SetWmiDataItem = UsbCrypt_SetWmiDataItem;
pCryptExt->CryptWminfo.ExecuteWmiMethod = NULL;
pCryptExt->CryptWminfo.WmiFunctionControl = NULL;
ntStatus = IoWMIRegistrationControl(pCryptExt->pOurDeviceObject,
WMIREG_ACTION_REGISTER);
if(!NT_SUCCESS(ntStatus))
{
USB_TRACE1("UsbCrypt_AddDevice - Failed to register WMI, ntStatus 0x%x\n", ntStatus);
IoDeleteDevice(pNewDeviceObj);
return ntStatus;
}
//
// Set pagable flag if our phys object is also
// pageable
//
if(pPhysicalDevObject->Flags & DO_POWER_PAGABLE)
{
pNewDeviceObj->Flags |= DO_POWER_PAGABLE;
}
pCryptExt->DevicePower = PowerDeviceD0;
pCryptExt->SytemPower = PowerSystemWorking;
PowerState.DeviceState = pCryptExt->DevicePower;
// let the system know our pwer state
PoSetPowerState(pNewDeviceObj, DevicePowerState, PowerState);
//
// attach our driver to device stack
// IoAttachDeviceToDeviceStack() returns the top of the driver
// stack.
//
pCryptExt->pTopStackDeviceObject =
IoAttachDeviceToDeviceStack(pNewDeviceObj,
pPhysicalDevObject);
if(pCryptExt->pTopStackDeviceObject == NULL)
{
USB_TRACE("UsbCrypt_AddDevice - Failed to attached to top of device stack\n");
IoDeleteDevice(pNewDeviceObj);
return STATUS_NO_SUCH_DEVICE;
}
//
// Register device interface, this enables apps to
// talk to us
//
ntStatus = IoRegisterDeviceInterface(pCryptExt->pLowerPhysDevObject,
&GUID_CLASS_USBCRYPT,
NULL,
&pCryptExt->OurInterfaceName);
if(!NT_SUCCESS(ntStatus))
{
USB_TRACE("Error, failed to register device interface\n");
IoDetachDevice(pCryptExt->pTopStackDeviceObject);
IoDeleteDevice(pNewDeviceObj);
return ntStatus;
}
// Initialize remove lock
IoInitializeRemoveLock(&pCryptExt->RemoveLock, CRYT_MEM_TAG, 0, 100);
// create a work item to get decription progress
// we'll constantly use a work item (which is run in a seperate
// system thread) to "hang" a read on the progress pipe.
pCryptExt->pProgressWkItem = IoAllocateWorkItem(pNewDeviceObj);
//
// Clear the DO_DEVICE_INITIALIZING flag.
//
pNewDeviceObj->Flags &= ~DO_DEVICE_INITIALIZING;
USB_TRACE("AddDevice - Successful\n");
return STATUS_SUCCESS;
}
// ================ UsbCrypt_PnPHandler() ===========
// Desc: Handles PNP messages
//
// Returns: NTSTATUS
//
NTSTATUS UsbCrypt_PnPHandler(IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp)
{
PIO_STACK_LOCATION pIrpStack;
PUSB_CRYPT_EXT pCryptExt;
KEVENT startDeviceEvent;
NTSTATUS ntStatus;
BOOLEAN bPassDown = FALSE; // pass down to lower driver
pIrpStack = IoGetCurrentIrpStackLocation(pIrp);
pCryptExt = (PUSB_CRYPT_EXT)pDeviceObject->DeviceExtension;
//
// Have we been removed?
//
if(GET_CURRENT_PNP_STATE(pCryptExt) == Removed)
{
pIrp->IoStatus.Status = STATUS_DELETE_PENDING;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_DELETE_PENDING;
}
// print out PNP function
USB_TRACE1("UsbCrypt_PnPHandler - %s\n", PnPMinorFunctionString(pIrpStack->MinorFunction));
switch(pIrpStack->MinorFunction)
{
case IRP_MN_START_DEVICE:
ntStatus = StartUSBCryptDevice(pDeviceObject, pIrp);
bPassDown = FALSE;
if(NT_SUCCESS(ntStatus))
{
SET_NEW_PNP_STATE(pCryptExt, Working);
}
break;
case IRP_MN_QUERY_STOP_DEVICE:
// always succesfull
ntStatus = STATUS_SUCCESS;
bPassDown = TRUE;
// change device state
SET_NEW_PNP_STATE(pCryptExt, PendingStop);
break;
case IRP_MN_CANCEL_STOP_DEVICE:
ntStatus = STATUS_SUCCESS;
bPassDown = TRUE;
// change device state
SET_NEW_PNP_STATE(pCryptExt, Working);
break;
case IRP_MN_STOP_DEVICE:
ntStatus = STATUS_SUCCESS;
bPassDown = TRUE;
StopUSBCryptDevice(pDeviceObject, pCryptExt);
break;
case IRP_MN_QUERY_REMOVE_DEVICE:
ntStatus = STATUS_SUCCESS;
bPassDown = TRUE;
// set new device status
SET_NEW_PNP_STATE(pCryptExt, PendingRemove);
break;
case IRP_MN_CANCEL_REMOVE_DEVICE:
bPassDown = FALSE;
ntStatus = CancelRemove(pCryptExt, pIrp);
break;
case IRP_MN_SURPRISE_REMOVAL:
ntStatus = STATUS_SUCCESS;
bPassDown = TRUE;
USBCryptSurpriseRemoval(pDeviceObject, pCryptExt);
break;
case IRP_MN_REMOVE_DEVICE:
ntStatus = STATUS_SUCCESS;
bPassDown = TRUE;
RemoveCryptDevice(pDeviceObject, pCryptExt, pIrp);
return STATUS_SUCCESS;
break;
case IRP_MN_QUERY_CAPABILITIES:
ntStatus = CryptCapabilities(pDeviceObject, pIrp);
bPassDown = FALSE;
break;
default:
USB_TRACE1("PNP IRP not handled: %s\n",
PnPMinorFunctionString(pIrpStack->MinorFunction));
bPassDown = TRUE;
// leave status unchanged
ntStatus = pIrp->IoStatus.Status;
break;
} // end switch()
//
// complete request or pass down
//
pIrp->IoStatus.Status = ntStatus;
pIrp->IoStatus.Information = 0;
if(bPassDown)
{
// pass this PNP call down to the lower driver
IoSkipCurrentIrpStackLocation(pIrp);
ntStatus = IoCallDriver(pCryptExt->pTopStackDeviceObject, pIrp);
}
else
{
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
return ntStatus;
}
// ================== HandleStartDevice() ===============
// Desc: Starts device driver for USB device.
//
// Returns: NTSTATUS
//
NTSTATUS StartUSBCryptDevice(IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp)
{
KIRQL oldIrql;
KEVENT StartWaitEvent;
NTSTATUS Status;
LARGE_INTEGER dueTime;
PUSB_CRYPT_EXT pUsbCryptExt;
USB_TRACE("StartUSBCryptDevice() - begin of function\n");
//
// get device extension
//
pUsbCryptExt = (PUSB_CRYPT_EXT)pDeviceObject->DeviceExtension;
//
// First we need to pass start IRP down to lower drivers
//
KeInitializeEvent(&StartWaitEvent, NotificationEvent, FALSE);
IoCopyCurrentIrpStackLocationToNext(pIrp);
IoSetCompletionRoutine(pIrp,
(PIO_COMPLETION_ROUTINE)IrpGenericCompletionRoutine,
(PVOID)&StartWaitEvent,
TRUE,
TRUE,
TRUE);
// remember IoCallDriver() can be asynchronous
Status = IoCallDriver(pUsbCryptExt->pTopStackDeviceObject, pIrp);
if(Status == STATUS_PENDING)
{
KeWaitForSingleObject(&StartWaitEvent,
Executive,
KernelMode,
FALSE,
NULL);
// set return status
Status = pIrp->IoStatus.Status;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -