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

📄 ezusbsys.c

📁 usb 驱动程序源码
💻 C
📖 第 1 页 / 共 5 页
字号:
Ezusb_DefaultPnpHandler(
   IN PDEVICE_OBJECT fdo,
   IN PIRP Irp
   )
{
   PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;

   IoSkipCurrentIrpStackLocation(Irp);
   return IoCallDriver(pdx->StackDeviceObject, Irp);
}

///////////////////////////////////////////////////////////////////////////////
// @func Handle completion of a request by a lower-level driver
// @parm Functional device object
// @parm I/O request which has completed
// @parm Context argument supplied to IoSetCompletionRoutine, namely address of
// KEVENT object on which ForwardAndWait is waiting
// @comm This is the completion routine used for requests forwarded by ForwardAndWait. It
// sets the event object and thereby awakens ForwardAndWait.
// @comm Note that it's *not* necessary for this particular completion routine to test
// the PendingReturned flag in the IRP and then call IoMarkIrpPending. You do that in many
// completion routines because the dispatch routine can't know soon enough that the
// lower layer has returned STATUS_PENDING. In our case, we're never going to pass a
// STATUS_PENDING back up the driver chain, so we don't need to worry about this.

NTSTATUS 
OnRequestComplete(
   IN PDEVICE_OBJECT fdo,
   IN PIRP Irp,
   IN PKEVENT pev
   )
/*++

Routine Description:
   Handle completion of a request by a lower-level driver

Arguments:
   DriverObject -  Functional device object
   Irp - I/O request which has completed
   pev - Context argument supplied to IoSetCompletionRoutine, namely address of
         KEVENT object on which ForwardAndWait is waiting

Return Value:
   STATUS_MORE_PROCESSING_REQUIRED
--*/
{
   KeSetEvent(pev, 0, FALSE);
   return STATUS_MORE_PROCESSING_REQUIRED;
}

NTSTATUS
ForwardAndWait(
   IN PDEVICE_OBJECT fdo,
   IN PIRP Irp
   )
/*++
Routine Description:
   Forward request to lower level and await completion

   The only purpose of this routine in this particular driver is to pass down
   IRP_MN_START_DEVICE requests and wait for the PDO to handle them.
   
   The processor must be at PASSIVE IRQL because this function initializes
   and waits for non-zero time on a kernel event object.

Arguments:
   fdo - pointer to a device object
   Irp          - pointer to an I/O Request Packet

Return Value:
   STATUS_SUCCESS if successful,
   STATUS_UNSUCCESSFUL otherwise
--*/
{
	KEVENT event;
	PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fdo->DeviceExtension;
	NTSTATUS ntStatus;

   ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
	
	//
   // Initialize a kernel event object to use in waiting for the lower-level
	// driver to finish processing the object. 
   //
	KeInitializeEvent(&event, NotificationEvent, FALSE);

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

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

	if (ntStatus == STATUS_PENDING)
	{
      KeWaitForSingleObject(&event, Executive, KernelMode, FALSE, NULL);
      ntStatus = Irp->IoStatus.Status;
   }

	return ntStatus;
}

NTSTATUS
CompleteRequest(
   IN PIRP Irp,
   IN NTSTATUS status,
   IN ULONG info
   )
/*++
Routine Description:
   Mark I/O request complete

Arguments:
   Irp - I/O request in question
   status - Standard status code
   info Additional information related to status code

Return Value:
   STATUS_SUCCESS if successful,
   STATUS_UNSUCCESSFUL otherwise
--*/
{
	Irp->IoStatus.Status = status;
	Irp->IoStatus.Information = info;
	IoCompleteRequest(Irp, IO_NO_INCREMENT);

   return status;
}

NTSTATUS
Ezusb_DispatchPnp(
   IN PDEVICE_OBJECT fdo,
   IN PIRP           Irp
   )
/*++
Routine Description:
   Process Plug and Play IRPs sent to this device.

Arguments:
   fdo - pointer to a device object
   Irp          - pointer to an I/O Request Packet

Return Value:
   NTSTATUS
--*/
{
   PIO_STACK_LOCATION irpStack;
   PDEVICE_EXTENSION pdx = fdo->DeviceExtension;
   ULONG fcn;
   NTSTATUS ntStatus;

   Ezusb_KdPrint (("Enter Ezusb_DispatchPnp\n"));

   if (!LockDevice(fdo))
		return CompleteRequest(Irp, STATUS_DELETE_PENDING, 0);

   //
   // Get a pointer to the current location in the Irp. This is where
   //     the function codes and parameters are located.
   //
   irpStack = IoGetCurrentIrpStackLocation (Irp);

   ASSERT(irpStack->MajorFunction == IRP_MJ_PNP);

   fcn = irpStack->MinorFunction;

   switch (fcn)
   {
      case IRP_MN_START_DEVICE:

         Ezusb_KdPrint (("IRP_MN_START_DEVICE\n"));

         ntStatus = Ezusb_HandleStartDevice(fdo,Irp);

         if (ntStatus == STATUS_SUCCESS)
         {
            pdx->Started = TRUE;
         }

         break; //IRP_MN_START_DEVICE

      case IRP_MN_STOP_DEVICE:

         Ezusb_KdPrint (("IRP_MN_STOP_DEVICE\n"));

         //
         // first pass the request down the stack
         //
         Ezusb_DefaultPnpHandler(fdo,Irp);

         ntStatus = Ezusb_StopDevice(fdo);

         break; //IRP_MN_STOP_DEVICE

      case IRP_MN_REMOVE_DEVICE:

         Ezusb_KdPrint (("IRP_MN_REMOVE_DEVICE\n"))

         ntStatus = Ezusb_HandleRemoveDevice(fdo,Irp);

         break; //IRP_MN_REMOVE_DEVICE

      case IRP_MN_QUERY_CAPABILITIES:
      {
         //
         // This code swiped from Walter Oney.  Please buy his book!!
         //

      	PDEVICE_CAPABILITIES pdc = irpStack->Parameters.DeviceCapabilities.Capabilities;

         Ezusb_KdPrint (("IRP_MN_QUERY_CAPABILITIES\n"))

         // Check to be sure we know how to handle this version of the capabilities structure

	      if (pdc->Version < 1)
         {
		      ntStatus = Ezusb_DefaultPnpHandler(fdo, Irp);
            break;
         }

         ntStatus = ForwardAndWait(fdo, Irp);
	      if (NT_SUCCESS(ntStatus))
   		{						// IRP succeeded
      		pdc = irpStack->Parameters.DeviceCapabilities.Capabilities;
            // setting this field prevents NT5 from notifying the user when the
            // device is removed.
		      pdc->SurpriseRemovalOK = TRUE;
   		}						// IRP succeeded

	      ntStatus = CompleteRequest(Irp, ntStatus, Irp->IoStatus.Information);
      }
         break; //IRP_MN_QUERY_CAPABILITIES


      //
      // All other PNP IRP's are just passed down the stack by the default handler
      //
      default:
        Ezusb_KdPrint (("Passing down unhandled PnP IOCTL 0x%x\n", fcn));
        ntStatus = Ezusb_DefaultPnpHandler(fdo, Irp);

   } // switch MinorFunction

	if (fcn != IRP_MN_REMOVE_DEVICE)
      UnlockDevice(fdo);

   Ezusb_KdPrint (("Exit Ezusb_DispatchPnp %x\n", ntStatus));
   return ntStatus;

}//Ezusb_Dispatch


NTSTATUS
Ezusb_DispatchPower(
    IN PDEVICE_OBJECT fdo,
    IN PIRP           Irp
    )
/*++
Routine Description:
   Process the IRPs sent to this device.

Arguments:
   fdo - pointer to a device object
   Irp          - pointer to an I/O Request Packet

Return Value:
   NTSTATUS
--*/
{
   PIO_STACK_LOCATION irpStack, nextStack;
   PDEVICE_EXTENSION pdx = fdo->DeviceExtension;
   NTSTATUS ntStatus;

   Ezusb_KdPrint (("Enter Ezusb_DispatchPower\n"));

   Irp->IoStatus.Status = STATUS_SUCCESS;
   Irp->IoStatus.Information = 0;

   //
   // Get a pointer to the current location in the Irp. This is where
   //     the function codes and parameters are located.
   //
   irpStack = IoGetCurrentIrpStackLocation (Irp);



   Ezusb_KdPrint (("IRP_MJ_POWER MIN=0x%x Type=0x%x State=0x%x\n",irpStack->MinorFunction,
      irpStack->Parameters.Power.Type,
      irpStack->Parameters.Power.State.DeviceState));

   switch (irpStack->MinorFunction)
   {
      case IRP_MN_SET_POWER:

         switch (irpStack->Parameters.Power.Type)
         {
            case SystemPowerState:

               break; //SystemPowerState

            case DevicePowerState:
               switch (irpStack->Parameters.Power.State.DeviceState)
               {
                  case PowerDeviceD3:
                     Ezusb_KdPrint (("IRP_MN_SET_D3\n"));
                     break;
                  case PowerDeviceD2:
                     Ezusb_KdPrint (("IRP_MN_SET_D2\n"));
                     break;
                  case PowerDeviceD1:
                     Ezusb_KdPrint (("IRP_MN_SET_D1\n"));
                     break;
                  case PowerDeviceD0:
                     Ezusb_KdPrint (("IRP_MN_SET_D0\n"));
                     break;
                } // switch on Power.State.DeviceState

                break; //DevicePowerState

         }// switch on Power.Type

         break;  //IRP_MN_SET_POWER

      case IRP_MN_QUERY_POWER:

         // Look at what type of power query this is
         switch (irpStack->Parameters.Power.Type)
         {
            case SystemPowerState:

               break; //SystemPowerState

            case DevicePowerState:
               switch (irpStack->Parameters.Power.State.DeviceState)
               {
                  case PowerDeviceD2:
                     Ezusb_KdPrint (("IRP_MN_QUERY_D2\n"));
                     break;
                  case PowerDeviceD1:
                     Ezusb_KdPrint (("IRP_MN_QUERY_D1\n"));
                     break;
                  case PowerDeviceD3:
                     Ezusb_KdPrint (("IRP_MN_QUERY_D3\n"));
                     break;
               } //switch on Power.State.DeviceState

               break; //DevicePowerState
                            
         }//switch on Power.Type

         break; //IRP_MN_QUERY_POWER

      default:
         // A PnP Minor Function was not handled
         Ezusb_KdPrint (("Power IOCTL not handled\n"));

   } /* switch MinorFunction*/


   nextStack = IoGetNextIrpStackLocation(Irp);
   ASSERT(nextStack != NULL);
   RtlCopyMemory(nextStack, irpStack, sizeof(IO_STACK_LOCATION));

   //
   // All PNP_POWER messages get passed to the StackDeviceObject that
   // we were given in PnPAddDevice.
   //
   // This stack device object is managed by the USB software subsystem,
   // and so this IRP must be propagated to the owning device driver for
   // that stack device object, so that driver in turn can perform any
   // device state management (e.g., remove its device object, etc.).
   //
   Ezusb_KdPrint (("Passing Power Irp down\n"));

   //
   // Notes on passing power IRPs down:  Using IoCallDriver() to pass
   // down power IRPs worked until Windows 2000.  Using this method
   // with Win2K causes a blue screen at system shutdown.  Because of this,
   // I am modifying the driver to use the more correct PoXXX() functions
   // to handle power IRPs. Unfortunately, the PoXXX() calls weren't
   // added until the kernel until after the release of Windows 95 OSR2.
   // So, a driver using these calls will not load on a Windows 95 system.
   // If you need to use this driver under Windows 95. then you must
   // #define WIN95.
   //

#ifdef WIN95
   ntStatus = IoCallDriver(pdx->StackDeviceObject, Irp);
#else
   PoStartNextPowerIrp(Irp);
   ntStatus = PoCallDriver(pdx->StackDeviceObject,Irp);
#endif
   #define Transsize 2048

   //
   // If lower layer driver marked the Irp as pending then reflect that by
   // calling IoMarkIrpPending.
   //
   if (ntStatus == STATUS_PENDING)
   {
      IoMarkIrpPending(Irp);
      Ezusb_KdPrint (("Power Irp came back with STATUS_PENDING (%x)\n", ntStatus));
   }
   else
   {

⌨️ 快捷键说明

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