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

📄 drvpnp.c

📁 usb。rar 驱动 windowsxp下的 还不错 源码 快下吧
💻 C
📖 第 1 页 / 共 2 页
字号:
///////////////////////////////////////////////////////////////////
//即插即用例程的实现:
//1. 添加设备例程DrvAddDevice()。
//2. 即插即用管理例程DrvPnpIrp()。
///////////////////////////////////////////////////////////////////   
#include <driver.h>
#include "usbdriver.h"

NTSTATUS PnpHandleStartDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS ForwardAndWait(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS ForwardIrpComplete(IN PDEVICE_OBJECT fdo,
						    IN PIRP Irp,
                            IN PKEVENT pev);
NTSTATUS UsbStartDevice(IN PDEVICE_OBJECT fdo);
NTSTATUS UsbConfigureDevice(IN  PDEVICE_OBJECT fdo);
NTSTATUS UsbSelectInterfaces(IN PDEVICE_OBJECT fdo,
            IN PUSB_CONFIGURATION_DESCRIPTOR ConfigurationDescriptor,
            IN PUSBD_INTERFACE_INFORMATION Interface);
NTSTATUS PnpHandleDefault(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS PnpHandleStopDevice(IN  PDEVICE_OBJECT fdo);
NTSTATUS PnpHandleRemoveDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp);
NTSTATUS UsbRemoveDevice(IN PDEVICE_OBJECT fdo);
//
//DrvAddDevice()例程。
//
NTSTATUS DrvAddDevice(IN PDRIVER_OBJECT DriverObject,
                       IN PDEVICE_OBJECT PhysicalDeviceObject)
{
	NTSTATUS ntStatus = STATUS_SUCCESS;
    //TODO:   
    WCHAR KernelDeviceNameBuffer[] = L"\\Device\\Cyusb-0";  
    UNICODE_STRING KernelDeviceNameUnicode;					
    WCHAR UserDeviceLinkBuffer[] = L"\\DosDevices\\Cyusb-0";
    UNICODE_STRING UserDeviceLinkUnicode;
    PDEVICE_OBJECT fdo = NULL; 
    PDEVICE_EXTENSION pdx;  
    
    RtlInitUnicodeString (&KernelDeviceNameUnicode,
                          KernelDeviceNameBuffer);
 
    ntStatus = IoCreateDevice (DriverObject,
                               sizeof (DEVICE_EXTENSION),
                               &KernelDeviceNameUnicode,
                               FILE_DEVICE_UNKNOWN,
                               0,
                               FALSE,
                               &fdo);
  
    if( !NT_SUCCESS(ntStatus))
		return ntStatus;

   	RtlInitUnicodeString (&UserDeviceLinkUnicode,
                          UserDeviceLinkBuffer);

    ntStatus = IoCreateSymbolicLink (&UserDeviceLinkUnicode,
                                     &KernelDeviceNameUnicode); 

    pdx = (PDEVICE_EXTENSION) (fdo->DeviceExtension);

    RtlCopyMemory(pdx->DeviceLinkName,
                  UserDeviceLinkBuffer,
                  sizeof(UserDeviceLinkBuffer));

    pdx->OpenHandles = 0;	  
    pdx->ConfigurationHandle = NULL;
    pdx->DeviceDescriptor = NULL;
    pdx->Interface = NULL;
       
    fdo->Flags &= ~DO_DEVICE_INITIALIZING;

    fdo->Flags |= DO_DIRECT_IO;

    pdx->PhysicalDeviceObject=PhysicalDeviceObject;
	  
    pdx->LowerDeviceObject =
         IoAttachDeviceToDeviceStack(fdo, PhysicalDeviceObject);

    pdx->Usages = 1; 
    KeInitializeEvent(&pdx->evRemove,
                      NotificationEvent,
                      FALSE);  
    
	return ntStatus;
}
//
//DrvPnpIrp()例程。
//
NTSTATUS DrvPnpIrp(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{
	NTSTATUS ntStatus = STATUS_SUCCESS;
    //TODO:
	//NTSTATUS ntStatus=STATUS_SUCCESS;
	PIO_STACK_LOCATION IrpStack;
    PDEVICE_EXTENSION pdx = fdo->DeviceExtension;
    ULONG MinorFunction;

    if (!LockDevice(fdo))
		return CompleteRequest(Irp, STATUS_DELETE_PENDING, 0);
    IrpStack = IoGetCurrentIrpStackLocation (Irp);
    MinorFunction = IrpStack->MinorFunction;

    switch (MinorFunction)
		{
		case IRP_MN_START_DEVICE:
			ntStatus = PnpHandleStartDevice(fdo,Irp);
			break; 

        case IRP_MN_STOP_DEVICE:
         PnpHandleDefault(fdo,Irp);
         ntStatus = PnpHandleStopDevice(fdo);
         break; 

         case IRP_MN_REMOVE_DEVICE:
		  ntStatus = PnpHandleRemoveDevice(fdo,Irp);
          break;

        case IRP_MN_QUERY_CAPABILITIES:
		  {
			  PDEVICE_CAPABILITIES pdc = IrpStack->Parameters.DeviceCapabilities.Capabilities;
		      if (pdc->Version < 1) {
				  ntStatus = PnpHandleDefault(fdo, Irp);
                  break;
			  }
              ntStatus = ForwardAndWait(fdo, Irp);
			  if (NT_SUCCESS(ntStatus)) { 
				  pdc = IrpStack->Parameters.DeviceCapabilities.Capabilities;
           	      pdc->SurpriseRemovalOK = TRUE;
			  }
   		      ntStatus = CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
		  }
          break; 
       default:
		 ntStatus = PnpHandleDefault(fdo, Irp);
   } 
   if (MinorFunction != IRP_MN_REMOVE_DEVICE)
	   UnlockDevice(fdo);
	return ntStatus;
}


BOOLEAN LockDevice(IN PDEVICE_OBJECT fdo)
{
   PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

   LONG usage = InterlockedIncrement(&pdx->Usages);

   if (pdx->Removing) {
	   if (InterlockedDecrement(&pdx->Usages) == 0)
		   KeSetEvent(&pdx->evRemove, 0, FALSE);
	   return FALSE;
   }
   return TRUE;
}
//
void UnlockDevice(PDEVICE_OBJECT fdo)
{
	PDEVICE_EXTENSION pdx;
	LONG usage;

	pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
    usage = InterlockedDecrement(&pdx->Usages);

    if (usage == 0) {	
		KeSetEvent(&pdx->evRemove, 0, FALSE);
   }					
}
//
NTSTATUS CompleteRequest(IN PIRP Irp,
						 IN NTSTATUS status,
						 IN ULONG info)
{
	Irp->IoStatus.Status = status;
	Irp->IoStatus.Information = info;
	IoCompleteRequest(Irp, IO_NO_INCREMENT);
	
	return status;
}
//
NTSTATUS PnpHandleStartDevice(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
	NTSTATUS ntStatus;

    ntStatus = ForwardAndWait(fdo, Irp);
	if (!NT_SUCCESS(ntStatus))
		return CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
    ntStatus = UsbStartDevice(fdo);
    
	return CompleteRequest(Irp, ntStatus, 0);
}

NTSTATUS ForwardAndWait(IN PDEVICE_OBJECT fdo,IN PIRP Irp)
{
	NTSTATUS ntStatus;
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	KEVENT event;

	KeInitializeEvent(&event, NotificationEvent, FALSE);

	IoCopyCurrentIrpStackLocationToNext(Irp);
	IoSetCompletionRoutine(Irp, (PIO_COMPLETION_ROUTINE)ForwardIrpComplete,
		                  (PVOID) &event, TRUE, TRUE, TRUE);

	ntStatus = IoCallDriver(pdx->LowerDeviceObject, Irp);

	if (ntStatus == STATUS_PENDING)	{
		KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
        ntStatus = Irp->IoStatus.Status;
    }
	return ntStatus;
}
//
NTSTATUS ForwardIrpComplete(IN PDEVICE_OBJECT fdo,
						    IN PIRP Irp,
                            IN PKEVENT pev)
{
   KeSetEvent(pev, 0, FALSE);
   return STATUS_MORE_PROCESSING_REQUIRED;
}
//
NTSTATUS UsbStartDevice(IN PDEVICE_OBJECT fdo)
{
	NTSTATUS ntStatus;
    PDEVICE_EXTENSION pdx;
    PUSB_DEVICE_DESCRIPTOR deviceDescriptor = NULL;
    PURB urb;
    USHORT SizeUrb;
	ULONG SizeDescriptor;

	pdx = fdo->DeviceExtension;
    SizeUrb=sizeof(struct _URB_CONTROL_DESCRIPTOR_REQUEST);
	urb = ExAllocatePool( NonPagedPool,SizeUrb);
    if (urb==NULL) 	return STATUS_NO_MEMORY;

	SizeDescriptor = sizeof(USB_DEVICE_DESCRIPTOR);
    deviceDescriptor = ExAllocatePool(NonPagedPool,SizeDescriptor);
    if (deviceDescriptor==NULL) {
		ExFreePool(urb);
        return STATUS_NO_MEMORY;
	}

   UsbBuildGetDescriptorRequest(urb,
                                SizeUrb,
                                USB_DEVICE_DESCRIPTOR_TYPE,
                                0,
                                0,
                                deviceDescriptor,

⌨️ 快捷键说明

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