IopRemoveDevice

NTSTATUS __stdcall IopRemoveDevice(_DEVICE_OBJECT *TargetDevice, UINT64 IrpMinorCode){
  int v2; 
  int v3; 
  _DEVICE_NODE *DeviceNode; 
  INT64 v6; 
  INT64 v7; 
  INT64 v8; 
  INT64 v9; 
  _DEVICE_OBJECT *v10; 
  INT64 v11; 
  INT64 v12; 
  WCHAR *v13; 
  INT64 v14; 
  INT64 v15; 
  int v16; 
  INT64 v17; 
  INT64 v18; 
  _ADAPTER_OBJECT *v19; 
  _DEVICE_NODE *PreviousParent; 
  unsigned __int64 v21; 
  __int128 v23; 
  _IO_STACK_LOCATION TopStackLocation; 
  HPNPOPCTX *hPnpOpCtx; 

  v2 = 0;
  v3 = IrpMinorCode;
  v23 = 0i64;
  if( TargetDevice )
    DeviceNode = (_DEVICE_NODE *)TargetDevice->DeviceObjectExtension->DeviceNode;
  else
    DeviceNode = 0i64;
  hPnpOpCtx = 0i64;
  PiPnpRtlBeginOperation(&hPnpOpCtx);
  if( v3 == 2 )
    IopUncacheInterfaceInformation(TargetDevice, 1u);
  memset(&TopStackLocation, 0i64, sizeof(TopStackLocation));
  TopStackLocation.MajorFunction = 27;
  TopStackLocation.MinorFunction = v3;
  if( PnpFindMountableDevice((__int64)TargetDevice, v6, v7) )
  {
    v2 = 1;
    PnpLockMountableDevice(TargetDevice, v8, v9);
    v10 = PnpMarkDeviceForRemove(TargetDevice, 1i64, (_DEVICE_OBJECT **)&v23);
    PnpUnlockMountableDevice((__int64)TargetDevice, v11, v12);
  }
  else
  {
    v10 = TargetDevice;
  }
  if( v3 == 2 || v3 == 23 )
  {
    if( (DeviceNode->UserFlags & 8) != 0 )
    {
      PipClearDevNodeUserFlags(DeviceNode, 8ui64);
      IopDecDisableableDepends(DeviceNode);
    }
    if( v3 == 2 )
    {
      PiSwProcessParentRemoveIrp(TargetDevice);
      PiSwStopDestroy(v13);
    }
  }
  v16 = IopSynchronousCall(v10, &TopStackLocation, 3221225659i64, 0i64, 0i64);
  if( v2 )
  {
    PnpLockMountableDevice(TargetDevice, v14, v15);
    if( (unsigned int)(v3 - 2) <= 1 )
      PnpMarkDeviceForRemove(TargetDevice, 0i64, (_DEVICE_OBJECT **)&v23);
    v19 = (_ADAPTER_OBJECT *)*((_QWORD *)&v23 + 1);
    if( *((_QWORD *)&v23 + 1) )
    {
      IopDecrementDeviceObjectHandleCount(*((_DEVICE_OBJECT **)&v23 + 1));
      HalPutDmaAdapter(v19);
    }
    PnpUnlockMountableDevice((__int64)TargetDevice, v17, v18);
    if( (v3 != 1 || v16 < 0) && v3 != 23 )
    {
LABEL_21:
      if( v3 != 2 )
        goto LABEL_26;
      goto LABEL_22;
    }
    v16 = IopInvalidateVolumesForDevice(TargetDevice);
  }
  if( v3 != 23 )
    goto LABEL_21;
LABEL_22:
  PoFxAbandonDevice(DeviceNode);
  DeviceNode->CurrentPowerState.SystemState = PowerSystemUnspecified;
  if( v3 == 2 )
  {
    PipClearDevNodeFlags(DeviceNode, 0x1008ui64);
    PreviousParent = DeviceNode->PreviousParent;
    if( PreviousParent )
    {
      if( ((unsigned __int8)PreviousParent & 1) != 0 )
      {
        v21 = (unsigned __int64)PreviousParent & 0xFFFFFFFFFFFFFFFEui64;
        DeviceNode->PreviousParent = (_DEVICE_NODE *)v21;
        --*(_DWORD *)(v21 + 656);
      }
    }
  }
LABEL_26:
  if( hPnpOpCtx )
    PiPnpRtlEndOperation(hPnpOpCtx);
  return v16;
}

Referenced by:

PnpDeleteLockedDeviceNode
PnpDisableDevice
PnpQueryRemoveLockedDeviceNode
PnpRemoveLockedDeviceNode
PnpSurpriseRemoveLockedDeviceNode