PnpNotifyTargetDeviceChange

NTSTATUS __stdcall PnpNotifyTargetDeviceChange(
        GUID *EventGuid,
        DEVICE_OBJECT *DeviceObject,
        TARGET_DEVICE_CUSTOM_NOTIFICATION *NotificationStructure,
        DRIVER_OBJECT **VetoingDriver){
  NTSTATUS v6; 
  __int64 v8; 
  _QWORD *v9; 
  __int64 v10; 
  _EJOB *CurrentServerSilo; 
  _ETHREAD *CurrentThread; 
  TARGET_DEVICE_CUSTOM_NOTIFICATION *v14; 
  int v15; 
  int v16; 
  GUID v17; 
  void *v18; 
  int v19; 
  void *v20; 
  _EJOB *v21; 
  _ETHREAD *v22; 
  char v23; 
  int v24; 
  int SessionIdFromPDO; 
  _QWORD *v26; 
  PVOID P; 
  DRIVER_OBJECT **v28; 
  __int128 v29[2]; 
  v28 = VetoingDriver;
  v6 = 0;
  v24 = 0;
  SessionIdFromPDO = -1;
  memset(v29, 0, sizeof(v29));
  ObfReferenceObjectWithTag(DeviceObject, 0x4E706E50u);
  v8 = *(_QWORD *)(*((_QWORD *)DeviceObject + 39) + 40i64);
  if( NotificationStructure )
  {
    NotificationStructure->Version = 1;
  }
  else
  {
    v17 = *EventGuid;
    LODWORD(v29[0]) = 2097153;
    *(GUID *)((char *)v29 + 4) = v17;
  }
  KeAcquireGuardedMutex(&PnpTargetDeviceNotifyLock);
  if( EventGuid == (GUID *)GUID_TARGET_DEVICE_REMOVE_CANCELLED
    || RtlCompareMemory(EventGuid, GUID_TARGET_DEVICE_REMOVE_CANCELLED, 0x10ui64) == 16 )
  {
    v9 = *(_QWORD **)(v8 + 480);
    v23 = 1;
  }
  else
  {
    v9 = *(_QWORD **)(v8 + 472);
    v23 = 0;
  }
  v10 = v8 + 472;
  v26 = (_QWORD *)v10;
  while( v9 != (_QWORD *)v10 )
  {
    P = v9;
    CurrentServerSilo = PsGetCurrentServerSilo();
    if( *((_DWORD *)v9 + 5) != (unsigned int)PsGetServerSiloServiceSessionId(CurrentServerSilo) )
      SessionIdFromPDO = IopGetSessionIdFromPDO(DeviceObject);
    ++*((_WORD *)v9 + 28);
    KeReleaseGuardedMutex(&PnpTargetDeviceNotifyLock);
    CurrentThread = (_ETHREAD *)KeGetCurrentThread();
    --*((_WORD *)CurrentThread + 242);
    ExAcquireResourceExclusiveLite((ERESOURCE *)v9[9], 1u);
    if( (SessionIdFromPDO == -1 || *((_DWORD *)v9 + 5) == SessionIdFromPDO) && !*((_BYTE *)v9 + 58) )
    {
      if( NotificationStructure )
      {
        NotificationStructure->FileObject = (PFILE_OBJECT)v9[10];
        v14 = NotificationStructure;
      }
      else
      {
        *((_QWORD *)&v29[1] + 1) = v9[10];
        v14 = (TARGET_DEVICE_CUSTOM_NOTIFICATION *)v29;
      }
      v15 = PnpNotifyDriverCallback((__int64)v9, (__int64)v14, &v24);
      ExReleaseResourceLite((PERESOURCE)v9[9]);
      KeLeaveCriticalRegion();
      v16 = v24;
      if( v15 < 0 )
        v16 = 0;
      v24 = v16;
      if( v16 < 0
        && (EventGuid == &GUID_TARGET_DEVICE_QUERY_REMOVE
         || RtlCompareMemory(EventGuid, &GUID_TARGET_DEVICE_QUERY_REMOVE, 0x10ui64) == 16) )
      {
        if( v28 )
          *v28 = (DRIVER_OBJECT *)v9[6];
        v18 = v9;
        *(__int128 *)((char *)v29 + 4) = *(_OWORD *)GUID_TARGET_DEVICE_REMOVE_CANCELLED;
        KeAcquireGuardedMutex(&PnpTargetDeviceNotifyLock);
        do
        {
          v19 = -1;
          v20 = v9;
          v21 = PsGetCurrentServerSilo();
          if( *((_DWORD *)v9 + 5) != (unsigned int)PsGetServerSiloServiceSessionId(v21) )
            v19 = IopGetSessionIdFromPDO(DeviceObject);
          ++*((_WORD *)v9 + 28);
          KeReleaseGuardedMutex(&PnpTargetDeviceNotifyLock);
          v22 = (_ETHREAD *)KeGetCurrentThread();
          --*((_WORD *)v22 + 242);
          ExAcquireResourceExclusiveLite((ERESOURCE *)v9[9], 1u);
          if( (v19 == -1 || *((_DWORD *)v9 + 5) == v19) && !*((_BYTE *)v9 + 58) )
          {
            *((_QWORD *)&v29[1] + 1) = v9[10];
            PnpNotifyDriverCallback((__int64)v9, (__int64)v29, 0i64);
          }
          ExReleaseResourceLite((PERESOURCE)v9[9]);
          KeLeaveCriticalRegion();
          KeAcquireGuardedMutex(&PnpTargetDeviceNotifyLock);
          v9 = (_QWORD *)v9[1];
          PnpDereferenceNotify(v20);
          if( v20 == v18 )
            PnpDereferenceNotify(v18);
        }
        while( v9 != v26 );
        v6 = v24;
        break;
      }
      v10 = (__int64)v26;
    }
    else
    {
      ExReleaseResourceLite((PERESOURCE)v9[9]);
      KeLeaveCriticalRegion();
    }
    KeAcquireGuardedMutex(&PnpTargetDeviceNotifyLock);
    if( v23 )
      v9 = (_QWORD *)v9[1];
    else
      v9 = (_QWORD *)*v9;
    PnpDereferenceNotify(P);
  }
  KeReleaseGuardedMutex(&PnpTargetDeviceNotifyLock);
  ObfDereferenceObjectWithTag(DeviceObject, 0x4E706E50ui64);
  return v6;
}

Referenced by:

PiSendTargetDeviceRemoveCanceledNotification
PiSendTargetDeviceRemoveCompleteNotification
PipSendTargetDeviceQueryRemoveNotification
PnpCancelRemoveOnHungDevices
PnpProcessCustomDeviceEvent