/*****************************************************************************
* 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:

   sohinitmp.c

Abstract:

   This module contains functions to enumurate the registry and 
   register the Minports with XWAN.

Author:

   Jeff Romatoski

Revision History:


--*/


////////////////////////////////////////////////////////////////////////////////


#include "digisoh.h"
#include "sohutil.h"
#include "sohinitmp.h"
#include "sohmsg.h"
#include "xwan.h"

#if !NT5_BUILD
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT,DigiSohInitializeMiniports)
#pragma alloc_text(INIT,DigiSohInitializeMiniportInstance)
#endif


ULONG
DigiSohInitializeMiniports(
	PSOH_DEV_EXT				pDevExt,
	IN PUNICODE_STRING			pucParametersPath
	)

/*++

   Routine Description:

   This interaces with the XWAN clonable device module to build
   a list of available Miniport.

   Arguments:

   pRegistryPath	- Pointer to the registry path (DigiSoh).

   Return Value:	- The Number of Ports


   -- */
{

	NTSTATUS		Status;
	ULONG			ulCount = 0;

	DigiDump( DIGIFLOW, ( "Entering DigiSohInitializeMiniports\n" ) );

	// Enumerate the the Miniport Adapters Key
    Status = DigiSohUtilEnumSubkeys(pucParametersPath,
							 DIGISOH_REGSUBKEY_MPA,
							 DigiSohInitializeMiniportInstance,
							 (PVOID) &ulCount,
							 (PVOID) pDevExt,
							 NULL);

	DigiDump( DIGIINIT, ( "DigiSohInitializeMiniports: Count = %d\n", ulCount ) );
	DigiDump( DIGIFLOW, ( "Exiting DigiSohInitializeMiniports\n" ) );

	return  ulCount ;
}

NTSTATUS
DigiSohInitializeMiniportInstance(
    IN PWSTR					pwRegistryPath,
    IN PWSTR					pwDeviceName,
    IN PULONG					pulCount,
	IN PSOH_DEV_EXT				pDevExt,
	IN PVOID					Nothing
    )

/*++

   Routine Description:

   This routine processes one instance of a miniport adapter by enumerating the
   ports for a Miniport adapter port instance.

   Arguments:

   pRegistryPath	- Pointer to the Miniport adapter registry path (Dg570mp1).

   Return Value:	- The Number of Ports


   -- */
{

	NTSTATUS		Status;
	UNICODE_STRING	MiniportPath;

	DigiDump( DIGIFLOW, ( "Entering DigiSohInitializeMiniportInstance\n" ) );
	DigiDump( DIGIINIT, ( "Registry Path = %ls\n",pwRegistryPath ) );
	DigiDump( DIGIINIT, ( "Device Name   = %ls\n",pwDeviceName ) );

	// Enumerate the Ports from the Miniports Miniport Key

	// Obtain a UNICODE STRING long enough to add the Paramters Key to 
	// the input registry path.
	Status = DigiStrAllocUnicodeString ( &MiniportPath,
										 DIGISOH_REGSUBKEY_SERVICES,
										 pwDeviceName,
										 NULL);
	
	if ( !NT_SUCCESS(Status) )
	{
		DigiDump(
			DIGIINIT,
			("DigiSohInitializeMiniportInstance: Couldn't Allocate the Configurations String \n")
			);
		return Status;
	}

	// Enumerate the the Miniport Devices Key
    Status = DigiSohUtilEnumSubkeys(&MiniportPath,
							 DIGISOH_REGSUBKEY_PARAMETERS,
							 DigiSohProcessMiniportInstance,
							 (PVOID) pulCount,
							 (PVOID) pDevExt,
							 pwDeviceName);

	DigiStrFreeUnicodeString( &MiniportPath);

	DigiDump( DIGIINIT, ( "DigiSohInitializeMiniportInstance: Status = %08x\n", Status ) );
	DigiDump( DIGIFLOW, ( "Exiting DigiSohInitializeMiniportInstance\n" ) );

	return ( Status );
}

NTSTATUS
DigiSohProcessMiniportInstance(
    IN PWSTR					pwRegistryPath,
    IN PWSTR					pwPortNumber,
    IN PULONG					pulCount,
	IN PSOH_DEV_EXT				pDevExt,
	IN PWSTR					pwDeviceName
    )

/*++

   Routine Description:

   This routine processes Miniport adapter port instance.  Here we register
   the Miniport with the Streams based XWAN module.

   Arguments:

   pRegistryPath	- Pointer to the registry path (DigiSoh).

   Return Value:	- Status


   -- */
{

	NTSTATUS		Status			= STATUS_SUCCESS;
	HANDLE			hKeyMiniPort	= NULL;
	ULONG			ulSnid;

	DigiDump( DIGIFLOW, ( "Entering DigiSohInitializeMiniportInstance\n" ) );
	DigiDump( DIGIINIT, ( "Registry Path = %ls\n",pwRegistryPath ) );
	DigiDump( DIGIINIT, ( "Adapter Registry Path = %ls\n",pwPortNumber ) );
	DigiDump( DIGIINIT, ( "Device Name = %ls\n",pwDeviceName ) );

	// Get a Key to the Registry Path
	Status = DigiRegOpenKey( &hKeyMiniPort, pwRegistryPath );
	if ( NT_ERROR( Status ) ) {
		goto EXIT;
	}

	// Get the Snid from the Registry
	if (!NT_SUCCESS(DigiRegQueryDword( hKeyMiniPort,
							 DXB_REGVAL_LINE_SNID,
							 &ulSnid,
							 DigiRegFlagRequired,
							 0 )))
	{

		// No Snid Key, so this Registry does not define a Port
		DigiDump( DIGIERRORS,
			      ( "SOH: Snid Registry Key Not Found\n") );
		goto EXIT;
	}

	// Register the device with the XWAN Minport module
	Status = XwanRegisterMiniport(pwDeviceName,pwPortNumber,ulSnid);

	if ( !NT_SUCCESS( Status ) )
	{

		// error processing
		DigiDump( DIGIERRORS,
			      ( "SOH: Duplicate Port for %d.\n", ulSnid ) );

		DigiLogError( pDevExt->pDeviceObject,
					  SOH_MINIPORT_REGISTRATION_ERROR,
					  19,
					  NULL,0,
					  pwDeviceName, (wcslen(pwDeviceName) * sizeof(WCHAR)) + sizeof(WCHAR),
					  pwPortNumber, (wcslen(pwPortNumber) * sizeof(WCHAR)) + sizeof(WCHAR)
					);

	}
	else
	{
		*pulCount+=1;
	}

EXIT:

	// Free the Key Resource
	if ( hKeyMiniPort ) {
		DigiRegCloseKey( hKeyMiniPort );
	}

	DigiDump( DIGIFLOW, ( "Exiting DigiSohInitializeMiniportInstance\n" ) );

	return ( Status );
}

#endif	// NT5_BUILD

