PipProcessDevNodeTree

NTSTATUS __stdcall PipProcessDevNodeTree(
        _DEVICE_NODE *SubtreeRootDeviceNode,
        _PNP_DEVICE_ACTION_ENTRY *Request,
        _ADD_CONTEXT *AddContext,
        _ENUM_TYPE EnumType,
        UINT64 ReallocateResources,
        UINT64 Synchronous,
        UINT64 ProcessOnlyIntermediateStates){
  char v7; 
  _ADD_CONTEXT *v8; 
  _ENUM_TYPE v9; 
  _DEVICE_NODE *v11; 
  char v12; 
  unsigned __int8 v13; 
  __int64 v14; 
  __int64 v15; 
  _DEVICE_NODE *v16; 
  int v17; 
  int Flags; 
  int v19; 
  NTSTATUS restarted; 
  NTSTATUS started; 
  LOGICAL v22; 
  int v23; 
  _DEVICE_NODE *Child; 
  _DEVICE_NODE *v25; 
  _DEVICE_NODE *Parent; 
  int v28; 
  _QWORD *v29; 
  UINT64 v30; 
  UINT64 v31; 
  UINT64 v32; 
  char v33; 
  char v34; 
  NTSTATUS v35; 
  _DEVICE_NODE *v36; 
  _UNICODE_STRING String; 
  __int128 Object; 
  __int64 v39; 
  char v40; 
  char v41; 
  _ADD_CONTEXT *Context; 
  _ENUM_TYPE v43; 

  v43 = EnumType;
  Context = AddContext;
  v7 = ProcessOnlyIntermediateStates;
  v8 = AddContext;
  v36 = SubtreeRootDeviceNode;
  v40 = 0;
  v9 = EnumType;
  v11 = SubtreeRootDeviceNode;
  String = 0i64;
  if( Request
    && !Request->ReorderingBarrier
    && EnumType != EnumTypeShallow
    && !(_BYTE)ProcessOnlyIntermediateStates
    && PiCollapseEnumRequests(&Request->ListEntry) )
  {
    v11 = (_DEVICE_NODE *)*(&PnpReplaceEvent + 831);
  }
  v12 = 0;
  v35 = 0;
  v34 = 0;
  v13 = 0;
  v33 = 0;
  while( 2 )
  {
    if( !v7 )
    {
      if( (byte_140C130BB & 8) != 0 )
        McTemplateK0z_EtwWriteTransfer(
          (__int64)SubtreeRootDeviceNode,
          (const _EVENT_DESCRIPTOR *)KMPnPEvt_AssignResources_Start,
          0i64,
          v11->InstancePath.Buffer);
      LOBYTE(ProcessOnlyIntermediateStates) = 0;
      v33 = PnpProcessAssignResources(v11, ReallocateResources, &ProcessOnlyIntermediateStates);
      if( (byte_140C130BB & 8) != 0 )
        McTemplateK0z_EtwWriteTransfer(
          v14,
          (const _EVENT_DESCRIPTOR *)KMPnPEvt_AssignResources_Stop,
          v15,
          v11->InstancePath.Buffer);
      if( (_BYTE)ProcessOnlyIntermediateStates )
      {
        LOBYTE(v32) = 1;
        LOBYTE(v31) = 0;
        LOBYTE(v30) = 0;
        if( PipProcessDevNodeTree(*(&PnpReplaceEvent + 831), Request, v8, v9, v30, v31, v32) == -1073741106 )
          v12 = 1;
        v34 = v12;
      }
    }
    LOBYTE(ProcessOnlyIntermediateStates) = 0;
    v16 = v11;
    v41 = 1;
    do
    {
      v17 = 1;
      if( v34 )
        goto LABEL_105;
      if( v16->State != DeviceNodeUninitialized
        && (v16->Flags & 0x6000) == 0
        && PnpCheckForActiveDependencies(v16, 2ui64) )
      {
        PipSetDevNodeProblem(v16, 0x33ui64, 0);
      }
      Flags = v16->Flags;
      if( (Flags & 0x6000) != 0 )
        goto LABEL_75;
      switch( v16->State )
      {
        case DeviceNodeUninitialized:
          if( v9 == EnumTypeIgnoreUninstalledDevices && (Flags & 0x2000000) != 0 || v7 )
            goto LABEL_68;
          if( PnpCheckForActiveDependencies(v16, 1ui64) )
          {
            PipSetDevNodeProblem(v16, 0x33ui64, 0);
            goto LABEL_68;
          }
          v19 = PiProcessNewDeviceNode(v16);
          if( v19 >= 0 )
            v17 = 0;
          goto LABEL_67;
        case DeviceNodeInitialized:
          if( v7 )
            goto LABEL_68;
          v19 = PipCallDriverAddDevice(v16, Context);
          if( v19 >= 0 )
          {
            PoFxPrepareDevice(v16, 0);
            v17 = 0;
            v33 = 1;
          }
          goto LABEL_67;
        case DeviceNodeResourcesAssigned:
          if( v7 )
            goto LABEL_68;
          PoFxActivateDevice(v16->PhysicalDeviceObject);
          v22 = (unsigned __int8)Synchronous;
          v16->StateFlags |= 0x200u;
          started = PipProcessStartPhase1(v16, v22, v13);
LABEL_40:
          v19 = started;
          if( started == 259 )
          {
            v13 = 1;
LABEL_68:
            v40 = 0;
            v19 = PnpDeviceCompletionProcessCompletedRequests(0, 0, &v40);
            if( v19 != -1073741106 )
            {
              if( !v40 && (_BYTE)ProcessOnlyIntermediateStates )
              {
                LOBYTE(ProcessOnlyIntermediateStates) = 0;
                v19 = PnpDeviceCompletionProcessCompletedRequests(0, 1, &v40);
                v13 &= -(v40 != 0);
              }
              if( v19 != -1073741106 )
                goto LABEL_75;
            }
            goto LABEL_73;
          }
          v17 = 0;
          goto LABEL_67;
        case DeviceNodeStartCompletion:
          restarted = PipProcessStartPhase2(v16);
          goto LABEL_37;
        case DeviceNodeStartPostWork:
          if( Flags < 0 )
          {
            v17 = 1;
            goto LABEL_68;
          }
          restarted = PipProcessStartPhase3(v16, v9 != EnumTypeShallow);
          goto LABEL_37;
      }
      if( v16->State != DeviceNodeStarted )
      {
        if( v16->State != DeviceNodeStopped )
        {
          if( v16->State != DeviceNodeRestartCompletion )
          {
            if( v16->State != DeviceNodeEnumerateCompletion )
              goto LABEL_68;
            if( (v16->StateFlags & 0x200) != 0 )
            {
              PoFxIdleDevice(v16->PhysicalDeviceObject);
              v16->StateFlags &= ~0x200u;
            }
            v19 = PipEnumerateCompleted(v16);
            v17 = 2;
            goto LABEL_67;
          }
          restarted = PipProcessRestartPhase2(v16);
LABEL_37:
          v19 = restarted;
          if( restarted < 0 )
          {
            v19 = -1073741106;
            goto LABEL_67;
          }
LABEL_49:
          v17 = 0;
          goto LABEL_67;
        }
        started = PipProcessRestartPhase1(v16, (unsigned __int8)Synchronous, v13);
        goto LABEL_40;
      }
      if( v9 == EnumTypeShallow && v16 != v11 )
        goto LABEL_68;
      v17 = 2;
      if( v7 || (Flags & 8) == 0 )
        goto LABEL_68;
      v19 = PipEnumerateDevice(v16, (unsigned __int8)Synchronous, v13);
      if( v19 == 259 )
      {
        v13 = 1;
        v17 = 1;
        goto LABEL_68;
      }
      if( v19 >= 0 )
        goto LABEL_49;
LABEL_67:
      if( v19 != -1073741106 )
        goto LABEL_68;
LABEL_73:
      if( v7 )
      {
        v35 = v19;
LABEL_75:
        SubtreeRootDeviceNode = (_DEVICE_NODE *)1;
        if( !v7 && (v16->StateFlags & 1) != 0 )
        {
          PpDevCfgTraceDeviceStart(v16);
          SubtreeRootDeviceNode = (_DEVICE_NODE *)1;
        }
        if( v13 && v17 && PnpAsyncOptions >= 0 )
          v17 = 1;
        v23 = v17 - 1;
        if( v23 )
        {
          if( v23 == 1 )
          {
            Child = v16->Child;
            if( !Child )
              goto LABEL_92;
LABEL_85:
            v16 = Child;
          }
        }
        else
        {
          while( 1 )
          {
LABEL_92:
            if( v16 == v11 )
            {
              LOBYTE(ProcessOnlyIntermediateStates) = 1;
              LOBYTE(SubtreeRootDeviceNode) = -(v13 != 0);
              v41 &= (unsigned __int8)SubtreeRootDeviceNode;
              goto LABEL_86;
            }
            Child = v16->Sibling;
            if( v16->Sibling )
              goto LABEL_85;
            if( v13 && PnpAsyncOptions && PnpAsyncOptions >= 0 )
              break;
            if( v16->Parent )
              v16 = v16->Parent;
          }
          Parent = v16->Parent;
          if( Parent )
            v16 = Parent->Child;
          LOBYTE(ProcessOnlyIntermediateStates) = 1;
        }
LABEL_86:
        v25 = v36;
        v12 = v34;
        goto LABEL_87;
      }
      v9 = v43;
LABEL_105:
      v12 = 0;
      v34 = 0;
      PnpDeviceCompletionProcessCompletedRequests(1, 1, &v40);
      v13 = 0;
      LOBYTE(v32) = 1;
      LOBYTE(v31) = Synchronous;
      LOBYTE(v30) = ReallocateResources;
      PipProcessDevNodeTree(*(&PnpReplaceEvent + 831), Request, Context, v9, v30, v31, v32);
      v28 = IopAllocateUnicodeString(&String, v11->InstancePath.Length);
      if( v28 < 0 )
      {
        HalPutDmaAdapter((PADAPTER_OBJECT)v36->PhysicalDeviceObject);
        return v28;
      }
      RtlCopyUnicodeString(&String, &v11->InstancePath);
      HalPutDmaAdapter((PADAPTER_OBJECT)v36->PhysicalDeviceObject);
      PpDevNodeUnlockTree(PPL_TREEOP_ALLOW_READS);
      LODWORD(v36) = 0;
      v39 = 0i64;
      Object = 0i64;
      KeInitializeEvent((INT64)&Object, 0, 0);
      if( (int)PnpInsertNoopEvent((__int64)&Object, (__int64)&v36) >= 0 )
        KeWaitForSingleObject((UINT64)&Object, 0, 0, 0, 0i64);
      PpDevNodeLockTree(PPL_TREEOP_ALLOW_READS);
      v29 = PnpDeviceObjectFromDeviceInstanceWithTag((__int64)&String, 0x746C6644u);
      RtlFreeAnsiString(&String);
      if( !v29 )
        return -1073741823;
      v11 = *(_DEVICE_NODE **)(v29[39] + 40i64);
      v25 = v11;
      v16 = v11;
      v36 = v11;
LABEL_87:
      v9 = v43;
    }
    while( v41 );
    v8 = Context;
    if( v33 )
      continue;
    break;
  }
  if( !v7 )
    HalPutDmaAdapter((PADAPTER_OBJECT)v25->PhysicalDeviceObject);
  return v35;
}

Referenced by:

PiProcessReenumeration
PiProcessResourceRequirementsChanged
PiProcessStartSystemDevices
PiRestartDevice
PipProcessDevNodeTree
PnpDeviceActionWorker
PnpProcessRebalance