/*++
*******************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved                            *
*                                                                                                                                                                                                               *
* This software contains proprietary and confidential information of Digi               *
* International Inc.  By accepting transfer of this copy, Recipient agrees              *
* to retain this software in confidence, to prevent disclosure to others,               *
* and to make no use of this software other than that for which it was                  *
* delivered.  This is an unpublished copyrighted work of Digi International     *
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly     *
* prohibited.                                                                                                                                                                           *
*                                                                                                                                                                                                               *
* Restricted Rights Legend                                                                                                                                              *
*                                                                                                                                                                                                               *
* Use, duplication, or disclosure by the Government is subject to                                       *
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in                   *
* Technical Data and Computer Software clause at DFARS 252.227-7031 or                  *
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -                            *
* Restricted Rights at 48 CFR 52.227-19, as applicable.                                                         *
*                                                                                                                                                                                                               *
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343                            *
*                                                                                                                                                                                                               *
*******************************************************************************

Module Name:   digippp.c

                Parag - Changes for PPP

*/

#include "digippp.h"

// Global pointer to driver object.  Set by DriverEntry, used for event logging.
PDRIVER_OBJECT gpDriverObject = NULL;

// Set nonzero to force DriverEntry to return failure, causing driver to be unloaded.
STATIC ULONG DigiDontLoadDriver = FALSE;

// Set nonzero to enable event logging of calls.
STATIC ULONG gfCallLogging;

// NdisWan wrapper handle.  Received from Ndis by InitializeWrapper (DriverEntry), 
// used for RegisterMiniport (DriverEntry), and TerminateWrapper (MinportHalt).
NDIS_HANDLE ghWrapper;

extern WCHAR gPppverVersion[];

VOID
DriverUnload(
        IN PDRIVER_OBJECT pDriverObject )
/*++

Description:

        Last chance to cleanup before driver is unloaded.  Everything allocated or started 
        in DriverEntry or otherwise should be freed, destroyed or stopped before returning 
        from DriverUnload.

--*/
{
    UNREFERENCED_PARAMETER( pDriverObject );
    DigiDump( DIGIINIT, ( "PPP!DriverUnload\n" ) );

    MemPrintQuit();
    DigiDisplayMemoryLeaks();
}


// DriverEntry can be discarded after initialization
NTSTATUS DriverEntry( IN PDRIVER_OBJECT  pDriverObject,
                      IN PUNICODE_STRING pRegistryPath );
#pragma alloc_text( INIT, DriverEntry )


NTSTATUS
DriverEntry(
    IN PDRIVER_OBJECT pDriverObject, 
    IN PUNICODE_STRING pRegistryPath 
    )
/*++

Description:

        Driver main entrypoint.  NT calls this routine when first loading the driver.

Parameters:

        pDriverObject
                Driver object defines the driver entrypoints, etc.

        pRegistryPath
                Complete path to driver registry key.  Driver global config params are stored 
                here.  i.e. \Registry\System\ControlSet001\DigiPPP.  

Return Value:

        STATUS_SUCCESS or error.

--*/
{
    NTSTATUS Status;

    DigiInitMem( 'cldh' );
    MemPrintInitialize( "\\SystemRoot\\digppp.log" );
    DigiDebugInitEx( pRegistryPath, 
                     DIGIINIT|DIGISTATE|DIGITAPIEVENT|DIGIWANEVENT|DIGITAPIOID|DIGIWARN|DIGIERROR|DIGIBREAK, 
                     DEBUGMODE_CONSOLE,
                     "sradppp", gPppverVersion );

    DigiDump( DIGIINIT, ( "\nPPP!DriverEntry:\n"
                          "   gDebugMask         = 0x%x\n"
                          "   gDebugMode         = 0x%x\n"
                          "   DigiDontLoadDriver = 0x%x\n\n",
                          &gDebugMask, &gDebugMode, &DigiDontLoadDriver ) );

    gpDriverObject = pDriverObject;

    if ( DigiDbgBreakOnEntryEnabled() )
        DbgBreakPoint();
    if ( DigiDontLoadDriver )
    {
        Status = STATUS_CANCELLED;
        goto Exit;
    }

#if 0
    // Optional call logging
    {
        HANDLE hKey = NULL;
        Status = DigiRegOpenKey( &hKey, pRegistryPath->Buffer );
        if ( NT_SUCCESS( Status ) )
        {
            DigiRegQueryDword( hKey, L"EnableCallLogging", &gfCallLogging, DigiRegFlagOptional, FALSE );
            DigiRegCloseKey( hKey );
        }
    }
#endif

    NdisMInitializeWrapper( &ghWrapper, pDriverObject, pRegistryPath, NULL );

    {
        NDIS_WAN_MINIPORT_CHARACTERISTICS Wan = {0};
        Wan.Reserved = NDIS_USE_WAN_WRAPPER;
        Wan.MajorNdisVersion        = 3;
        Wan.MinorNdisVersion        = 0;
        Wan.InitializeHandler       = PPPInitialize;
        Wan.HaltHandler             = PPPHalt;
        Wan.QueryInformationHandler = PPPQueryInfo;
        Wan.SetInformationHandler   = PPPSetInfo;
        Wan.WanSendHandler          = PPPSend;
        Status = NdisMRegisterMiniport( ghWrapper, &Wan, sizeof Wan );
                
#if NT5_BUILD
        if ( NT_SUCCESS( Status ) )
        {
            NdisMRegisterUnloadHandler(ghWrapper,DriverUnload);
        }
#endif

    }

 Exit:

    DigiDump( DIGIINIT, ( "PPP!DriverEntry: Leave, status 0x%08x\n", Status ) );

    if ( NT_ERROR( Status ) )
    {
        MemPrintQuit();
        DigiDisplayMemoryLeaks();
    }

    return Status;
}
