📄 zini.c
字号:
// #define DRIVER
//
// Include files needed for WDM driver support
//
#include <wdm.h>
#include "stdarg.h"
#include "stdio.h"
//
// Include files needed for USB support
//
#include "usbdi.h"
#include "usbdlib.h"
//
// Include file for the Ezusb Device
//
#include "ezusbsys.h"
NTSTATUS
DriverEntry(
IN PDRIVER_OBJECT DriverObject,
IN PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
Installable driver initialization entry point.
This is where the driver is called when the driver is being loaded
by the I/O system. This entry point is called directly by the I/O system.
Arguments:
DriverObject - pointer to the driver object
RegistryPath - pointer to a unicode string representing the path
to driver-specific key in the registry
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS ntStatus = STATUS_SUCCESS;
Ezusb_KdPrint (("entering (Ezusb) DriverEntry (Build: %s/%s\n",__DATE__,__TIME__));
//
// Create dispatch points for the various events handled by this
// driver. For example, device I/O control calls (e.g., when a Win32
// application calls the DeviceIoControl function) will be dispatched to
// routine specified below in the IRP_MJ_DEVICE_CONTROL case.
//
DriverObject->DriverExtension->AddDevice = z_AddDevice;
DriverObject->DriverUnload = z_Unload;
DriverObject->MajorFunction[IRP_MJ_CREATE] = a_Create;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = b_Close;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = c_ProcessIOCTL;
DriverObject->MajorFunction[IRP_MJ_PNP] = d_DispatchPnp;
DriverObject->MajorFunction[IRP_MJ_POWER] = e_DispatchPower;
Ezusb_KdPrint (("exiting (Ezusb) DriverEntry (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS z_AddDevice(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT PhysicalDeviceObject
)
/*++
Routine Description:
This routine is called to create a new instance of the device
Arguments:
DriverObject - pointer to the driver object for this instance of Ezusb
PhysicalDeviceObject - pointer to a device object created by the bus
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS ntStatus = STATUS_SUCCESS;
PDEVICE_OBJECT deviceObject = NULL;
PDEVICE_EXTENSION pdx;
int instance;
Ezusb_KdPrint(("enter Ezusb_PnPAddDevice\n"));
#define MAX_EZUSB_DEVICES 8
//
// create our functional device object (FDO). This driver supports multiple ezusb devices.
// This loop will look for an available instance number. Keep incrementing the instance
// until a call to Ezusb_CreateDeviceObject succeeds.
//
instance = 0;
do
{
ntStatus = z_CreateDeviceObject(DriverObject, &deviceObject, instance);
instance++;
} while (!NT_SUCCESS(ntStatus) && (instance < MAX_EZUSB_DEVICES));
if (NT_SUCCESS(ntStatus))
{
pdx = deviceObject->DeviceExtension;
//
// Non plug and play drivers usually create the device object in
// driver entry, and the I/O manager autimatically clears this flag.
// Since we are creating the device object ourselves in response to
// a PnP START_DEVICE IRP, we need to clear this flag ourselves.
//
deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
//
// This driver uses direct I/O for read/write requests
//
deviceObject->Flags |= DO_DIRECT_IO;
deviceObject->Flags |= DO_POWER_PAGABLE;
//
//
// store away the Physical device Object
//
pdx->PhysicalDeviceObject=PhysicalDeviceObject;
//
// Attach to the StackDeviceObject. This is the device object that what we
// use to send Irps and Urbs down the USB software stack
//
pdx->StackDeviceObject =
IoAttachDeviceToDeviceStack(deviceObject, PhysicalDeviceObject);
ASSERT (pdx->StackDeviceObject != NULL);
pdx->LastFailedUrbStatus = 0;
pdx->usage = 1; // locked until RemoveDevice
KeInitializeEvent(&pdx->evRemove,
NotificationEvent,
FALSE); // set when use count drops to zero
}
Ezusb_KdPrint(("exit Ezusb_PnPAddDevice (%x)\n", ntStatus));
return ntStatus;
}
NTSTATUS z_CreateDeviceObject(
IN PDRIVER_OBJECT DriverObject,
IN PDEVICE_OBJECT *DeviceObject,
LONG Instance
)
/*++
Routine Description:
Creates a Functional DeviceObject
Arguments:
DriverObject - pointer to the driver object for device
DeviceObject - pointer to DeviceObject pointer to return
created device object.
Instance - instnace of the device create.
Return Value:
STATUS_SUCCESS if successful,
STATUS_UNSUCCESSFUL otherwise
--*/
{
NTSTATUS ntStatus;
WCHAR deviceLinkBuffer[] = L"\\DosDevices\\Ezusb-0";
UNICODE_STRING deviceLinkUnicodeString;
WCHAR deviceNameBuffer[] = L"\\Device\\Ezusb-0";
UNICODE_STRING deviceNameUnicodeString;
PDEVICE_EXTENSION pdx;
STRING deviceName;
Ezusb_KdPrint(("enter Ezusb_CreateDeviceObject instance = %d\n", Instance));
//
// fix up device names based on Instance
//
deviceLinkBuffer[18] = (USHORT) ('0' + Instance);
deviceNameBuffer[14] = (USHORT) ('0' + Instance);
Ezusb_KdPrint(("Create Device name (%ws)\n", deviceNameBuffer));
RtlInitUnicodeString (&deviceNameUnicodeString,
deviceNameBuffer);
//
//Print out the unicode string
//NOTE: We must first convert the string to Unicode due to a bug in the Debugger that does not allow
// Unicode Strings to be printed to the debug device.
//
deviceName.Buffer = NULL;
ntStatus = RtlUnicodeStringToAnsiString (&deviceName,
&deviceNameUnicodeString,
TRUE);
if (NT_SUCCESS(ntStatus))
{
Ezusb_KdPrint(("Create Device Name (%s)\n", deviceName.Buffer));
RtlFreeAnsiString (&deviceName);
}
else
{
Ezusb_KdPrint(("Unicode to Ansi str failed w/ ntStatus: 0x%x\n",ntStatus));
}
ntStatus = IoCreateDevice (DriverObject,
sizeof (DEVICE_EXTENSION),
&deviceNameUnicodeString,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
DeviceObject);
if (NT_SUCCESS(ntStatus))
{
// Initialize our device extension
pdx = (PDEVICE_EXTENSION) ((*DeviceObject)->DeviceExtension);
RtlCopyMemory(pdx->DeviceLinkNameBuffer,
deviceLinkBuffer,
sizeof(deviceLinkBuffer));
pdx->OpenHandles = 0;
pdx->ConfigurationHandle = NULL;
pdx->DeviceDescriptor = NULL;
pdx->NeedCleanup = FALSE;
pdx->DataRingBuffer = NULL;
pdx->DescriptorRingBuffer = NULL;
pdx->Started = FALSE;
// Initialize our interface
pdx->Interface = NULL;
RtlInitUnicodeString (&deviceLinkUnicodeString,
deviceLinkBuffer);
Ezusb_KdPrint(("Create DosDevice name (%ws)\n", deviceLinkBuffer));
ntStatus = IoCreateSymbolicLink (&deviceLinkUnicodeString,
&deviceNameUnicodeString);
}
Ezusb_KdPrint(("exit Ezusb_CreateDeviceObject (%x)\n", ntStatus));
return ntStatus;
}
VOID z_Unload(
IN PDRIVER_OBJECT DriverObject
)
/*++
Routine Description:
Free all the allocated resources, etc.
TODO: This is a placeholder for driver writer to add code on unload
Arguments:
DriverObject - pointer to a driver object
Return Value:
None
--*/
{
Ezusb_KdPrint (("enter Ezusb_Unload\n"));
/*
// TODO: Free any global resources allocated in DriverEntry
*/
Ezusb_KdPrint (("exit Ezusb_Unload\n"));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -