Ejemplo n.º 1
0
NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp)
{
    libusb_device_t *dev = device_object->DeviceExtension;
    IO_STACK_LOCATION *stack_location = IoGetCurrentIrpStackLocation(irp);

    switch (stack_location->MajorFunction)
    {
    case IRP_MJ_PNP:
        return dispatch_pnp(dev, irp);

    case IRP_MJ_POWER:
		// ID: 2960644 (farthen)
		// You can't set the power state if the device is not handled at all
		if(!dev->next_stack_device)
		{
			return complete_irp(irp, STATUS_INVALID_DEVICE_STATE, 0);
		}
        return dispatch_power(dev, irp);
    }

    /* since this driver may run as an upper filter we have to check whether */
    /* the IRP is sent to this device object or to the lower one */
    if (accept_irp(dev, irp))
    {
        switch (stack_location->MajorFunction)
        {
        case IRP_MJ_DEVICE_CONTROL:

            if (dev->is_started)
            {
                return dispatch_ioctl(dev, irp);
            }
            else /* not started yet */
            {
                return complete_irp(irp, STATUS_INVALID_DEVICE_STATE, 0);
            }

        case IRP_MJ_CREATE:

            if (dev->is_started)
            {
				// only one driver can act as power policy owner and 
				// power_set_device_state() can only be issued by the PPO.
				// disallow_power_control is set to true for drivers which 
				// we know cause a BSOD on any attempt to request power irps.
                if (dev->power_state.DeviceState != PowerDeviceD0 && !dev->disallow_power_control)
                {
                    /* power up the device, block until the call */
                    /* completes */
                    power_set_device_state(dev, PowerDeviceD0, TRUE);
                }
                return complete_irp(irp, STATUS_SUCCESS, 0);
            }
            else /* not started yet */
            {
                return complete_irp(irp, STATUS_INVALID_DEVICE_STATE, 0);
            }

        case IRP_MJ_CLOSE:

            /* release all interfaces bound to this file object */
            release_all_interfaces(dev, stack_location->FileObject);
            return complete_irp(irp, STATUS_SUCCESS, 0);

        case IRP_MJ_CLEANUP:

            return complete_irp(irp, STATUS_SUCCESS, 0);

        default:
            return complete_irp(irp, STATUS_NOT_SUPPORTED, 0);
        }
    }
    else /* the IRP is for the lower device object */
    {
        return pass_irp_down(dev, irp, NULL, NULL);
    }
}
Ejemplo n.º 2
0
NTSTATUS DDKAPI dispatch(DEVICE_OBJECT *device_object, IRP *irp)
{
  libusb_device_t *dev = device_object->DeviceExtension;

  switch(IoGetCurrentIrpStackLocation(irp)->MajorFunction) 
    {
    case IRP_MJ_PNP:
      return dispatch_pnp(dev, irp);
      
    case IRP_MJ_POWER:
      return dispatch_power(dev, irp);
    }

  /* since this driver may run as an upper filter we have to check whether */
  /* the IRP is sent to this device object or to the lower one */
  if(accept_irp(dev, irp))
    {
      switch(IoGetCurrentIrpStackLocation(irp)->MajorFunction) 
        {
        case IRP_MJ_DEVICE_CONTROL:
          
          if(dev->is_started)
            {
              return dispatch_ioctl(dev, irp);
            }
          else /* not started yet */
            {
              return complete_irp(irp, STATUS_INVALID_DEVICE_STATE, 0);
            }

        case IRP_MJ_CREATE:
          
          if(dev->is_started)
            {
              if(InterlockedIncrement(&dev->ref_count) == 1)
                {
                  if(dev->power_state.DeviceState != PowerDeviceD0)
                    {
                      /* power up the device, block until the call */
                      /* completes */
                      power_set_device_state(dev, PowerDeviceD0, TRUE);
                    }
                }

              return complete_irp(irp, STATUS_SUCCESS, 0);
            }
          else /* not started yet */
            {
              return complete_irp(irp, STATUS_INVALID_DEVICE_STATE, 0);
            }
          
        case IRP_MJ_CLOSE:
          
          if(!InterlockedDecrement(&dev->ref_count))
            {
              /* release all interfaces when the last handle is closed */
              release_all_interfaces(dev);
            }
          return complete_irp(irp, STATUS_SUCCESS, 0);
          
        case IRP_MJ_CLEANUP:

          return complete_irp(irp, STATUS_SUCCESS, 0);
          
        default:
          return complete_irp(irp, STATUS_NOT_SUPPORTED, 0);
        }
    }
  else /* the IRP is for the lower device object */
    {
      return pass_irp_down(dev, irp, NULL, NULL);
    }
}