/*++
*******************************************************************************
* Copyright (c) 1990-99 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:

   digix25.c

Abstract:

   
   This module contains required NT kernelmode driver entry and unload routines.  
   Also, any driver global driver.

Author:

   Michael Zarns
   Parag Agarwal - Ported to X.25
--*/

#include "digiX25.h"

#if NT5_BUILD
// Don't use MEMPRINT in 5.0 yet, due to MS bugs in registering Shutdown handler to 
// stop our memprint thread!
#else
#define X25_USE_MEMPRINT
#endif

// 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;


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, ( "DigiX25!DriverUnload\n" ) );

#ifdef X25_USE_MEMPRINT
	MemPrintQuit();
#endif

	DigiDisplayMemoryLeaks();
}


NTSTATUS DriverEntry( IN PDRIVER_OBJECT  pDriverObject,
                      IN PUNICODE_STRING pRegistryPath );


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\DigiX25.  

Return Value:

	STATUS_SUCCESS or error.

--*/
{
	NTSTATUS Status;

	DigiInitMem( '52X' );

#ifdef X25_USE_MEMPRINT
	MemPrintInitialize( "\\SystemRoot\\digiX25.log" );
#endif

//	DigiDebugInit( pRegistryPath, DIGIERROR|DIGIWARN|DIGITAPIOID|DIGIWANOID|DIGILANOID|DIGISTATE|DIGICALL|DIGIMP|DIGIINIT, DEBUGMODE_CONSOLE );
	DigiDebugInit( pRegistryPath, DIGIERROR|DIGIWARN, DEBUGMODE_CONSOLE );
#if DBG
	(void)DbgPrint( "\nDigiX25!DriverEntry:\n"
	                      "   gDebugMask         = 0x%x\n"
	                      "   gDebugMode         = 0x%x\n"
	                      "   DigiDontLoadDriver = 0x%x\n\n",
	                      &gDebugMask, &gDebugMode, &DigiDontLoadDriver );
#endif
   gpDriverObject = pDriverObject;

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

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

	{

		NDIS_MINIPORT_CHARACTERISTICS Characteristics = {0};
		Characteristics.InitializeHandler       = X25Initialize;
		Characteristics.HaltHandler             = X25Halt;
		Characteristics.QueryInformationHandler = X25QueryInfo;
		Characteristics.SetInformationHandler   = X25SetInfo;
  		Characteristics.Reserved            = NDIS_USE_WAN_WRAPPER;
		Characteristics.MajorNdisVersion    = 3;	// NT4: we're an NDIS 3.0 wan miniport driver
		Characteristics.WanSendHandler      = X25Send;
		Characteristics.CheckForHangHandler = X25CheckForHang;
		Characteristics.ResetHandler        = X25Reset;


		Status = NdisMRegisterMiniport( ghWrapper, &Characteristics, sizeof Characteristics );
	}

Exit:

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

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

	return Status;
}
