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

   digisoh.h

Abstract:

   This module is the header file for the DigiSOH driver. It contains
   common definitions and typedefs for DigiSOH components.

Author:

   Ron Gredvig

--*/


#ifndef _DIGISOH_H
#define _DIGISOH_H

#define UNICODE

#define DIGIINIT					DEBUG_00000001
#define DIGIFLOW					DEBUG_00000002
#define DIGIERRORS					DEBUG_00000004
#define DIGIBUGCHECK				DEBUG_00000008
#define DIGINOTIMPLEMENTED			DEBUG_00000010
#define DIGIVERYLOUD				DEBUG_00000020

#include "types.h"

// Digi Driver Libraries
#include "digilib.h"
#include "dsioctl.h"
#include "dsapi.h"
#include "digikme.h"
#include "xwan.h"


#if NT5_BUILD
#include "DsPnp.h"
#endif

#define DEVICE_NAME  L"DigiSOH"

// Defaults for Registry items
#define DEFAULT_PORT_COUNT	4

#include "ll_control.h"
#include "wan_control.h"


typedef enum SOH_OBJECT_TYPE
{
	SOH_OBJECT_TYPE_DEV_EXT,
	SOH_OBJECT_TYPE_STREAM,
	SOH_OBJECT_TYPE_RUNTIME_ENV,
} SOH_OBJECT_TYPE, *PSOH_OBJECT_TYPE;

typedef enum SOH_STREAM_STATE
{
	SOH_STREAM_STATE_INACTIVE,
	SOH_STREAM_STATE_OPENING,
	SOH_STREAM_STATE_OPEN,
	SOH_STREAM_STATE_OPENFAILED,
	SOH_STREAM_STATE_ACTIVE,
	SOH_STREAM_STATE_CLOSING,
	SOH_STREAM_STATE_CLOSED,
	SOH_STREAM_STATE_CLOSEFAILED,
	IoctlQueuedState,
	IoctlPendingState,
	IoctlCompleteState,
	PushQueuedState,
	PushPendingState,
	PushCompleteState,
	LinkQueuedState,
	LinkPendingState,
	LinkCompleteState
} SOH_STREAM_STATE, *PSOH_STREAM_STATE;

typedef enum SOH_PROCESS_STATE
{
	SOH_PROCESS_STATE_IDLE,
	SOH_PROCESS_STATE_DSTRM_RUNNING,
	SOH_PROCESS_STATE_CANCEL_PENDING
} SOH_PROCESS_STATE, *PSOH_PROCESS_STATE;

//
// This structure is used to place/store information about generic requests
// to/from a Stream.
//

typedef struct SOH_DEV_EXT SOH_DEV_EXT, *PSOH_DEV_EXT;


typedef struct _SOH_STREAM_OBJECT
{

	// Debugging aids.
	SOH_OBJECT_TYPE Type;

	// The Device Extension for this Stream.
	PSOH_DEV_EXT pDevExt;

	// The list entry for this Streams Object.
	// The device extension holds the list head.
	LIST_ENTRY ListEntry;

	// Lock for this Stream Object.
	KSPIN_LOCK SpinLock;

	// This is the handle returned on DS_OPEN. It is passed in subsequent
	// requests and used as an index to the StreamObjectPointerArray to
	// retrieve a Stream Object pointer.
	HANDLE hObjectId;

	// This is the handle to the actual Stream in the DSTREAM subsystem
	LONG iStream;

	// A pointer to the original file object associated with this Stream. Used
	// by cleanup and close.
	PFILE_OBJECT pOriginalFileObject;

	// A pointer to a Stream Object this Stream Object is linked under
	struct _SOH_STREAM_OBJECT *pStreamObjLinkedUnder;

	// Pointers to IRP requests that complete synchronously
	PIRP pOpenIrp;
	PIRP pIoctlIrp;
	PIRP pPushIrp;
	PIRP pLinkIrp;
	PIRP pCloseIrp;

	// Stream State
	SOH_STREAM_STATE State;
	SOH_STREAM_STATE StateBeforeTheClose;

	// Ioctl State (PENDING, COMPLETE, FAILED)
	SOH_STREAM_STATE IoctlState;
	SOH_STREAM_STATE PushState;
	SOH_STREAM_STATE LinkState;

	// Time ordered lists of pending and completed putmsg/getmsg IRP requests.
	LIST_ENTRY PendingPutmsgListHead;	// includes M_DATA, M_PROTO & M_PCPROTO
	LIST_ENTRY CompletedPutmsgListHead;
	LIST_ENTRY PendingGetmsgListHead;	// includes M_DATA, M_PROTO & M_PCPROTO
	LIST_ENTRY CompletedGetmsgListHead;

	// Special lists for handling canceled or purged putmsg/getmsg IRP requests.
	LIST_ENTRY PurgeListHead;

	// Putmsg flow control
	BOOLEAN PutmsgBlocked;

	// Processing States, Used by Cleanup
	SOH_STREAM_STATE	ProcessState;
	SOH_STREAM_STATE	CancelState;

	// Event object to signal Cleanup to continue
	KEVENT evCleanup;

} SOH_STREAM_OBJECT, *PSOH_STREAM_OBJECT;


//
// _DIGI_STREAMS_DEVICE_EXTENSION holds Digi device specific information
//

typedef struct SOH_RUNTIME_ENV
{
	// Debugging aids.
	SOH_OBJECT_TYPE Type;

	// This list head contains this device’s Stream Objects list. Each
	// Stream Object contains a list entry. StreamObjectCount tracks the 
	// number of entries in the list.
	LIST_ENTRY StreamObjectListHead;
	ULONG StreamObjectCount;

	// A pointer to an array containing pointers to Stream Objects. The
	// hObjectId assigned to each Stream Object is an index into this array.
	PSOH_STREAM_OBJECT *apStreamObj;

	// Control access to this device extension.
	KSPIN_LOCK SpinLock;

	// Event object to signal Streams thread to run
	KEVENT evStreams;

	// Event object to signal Irp completer thread to run
	KEVENT evCompleter;

	// Storage for helper thread object pointers
	PVOID pDstreamThreadObject;
	PVOID pIrpCompThreadObject;

	// Flag to indicate the driver is unloading
	BOOLEAN Unloading;

	// Use for Streams Stack Initialization and Controlling Streams

	// Only One Controlling Stream per firmware image is allowed.
	// DigiSOH has one firmware image.  This presents a problem
	// for NT5 PnP.  We cannot bring down resources for a particular
	// adapter, therefore, if multiple adapters are in the system
	// a reboot is required for any configuration changes to take
	// affect.
	//
	//  If the firmware is every changed to allow multiple control streams,
	//  the controlling streams handles should be moved to the Device Object.
	HANDLE	hDevice;
	HANDLE	hFr;
	HANDLE	hX25;

	// Control access the controlling stream handles.
	KSPIN_LOCK HandleSpinLock;

	BOOLEAN bProcessingControlStreams;
	BOOLEAN bWaitProcessingControlStreams;

	// Event object to signal the control handles are now available
	KEVENT evHandles;

	// Count of Adapters using the streams engine
	ULONG	ulAdapterCount;

	// Device that controls the controlling stream
	PDEVICE_OBJECT	pDeviceObject;

} SOH_RUNTIME_ENV, *PSOH_RUNTIME_ENV;


//
// _DIGI_STREAMS_DEVICE_EXTENSION holds Digi device specific information
//

typedef struct SOH_DEV_EXT
{
	
	
#if NT5_BUILD
	PNP_DEVICE_TYPE	PnpType;		// This field must be the first word in the device extension.	
	PVOID pfdoExtension;
	PXWAN_ADAPTER	pXwanAdapter;
#endif

	// Debugging aids.
	SOH_OBJECT_TYPE Type;
	WCHAR aName[64];
	UNICODE_STRING ucName;

	SOH_RUNTIME_ENV	*pSohRuntimeEnv;

	// The parent Device Object of this device extension.
	PDEVICE_OBJECT pDeviceObject;
	PDRIVER_OBJECT pDriverObject;

	// Storage for user mode symbolic link to device name
	UNICODE_STRING ucSymbolicLinkName;

} SOH_DEV_EXT, *PSOH_DEV_EXT;


extern PSOH_STREAM_OBJECT	*gapStreamObj; 
extern ULONG				gulMaxStreams;
extern SOH_RUNTIME_ENV		gSohRuntimeEnv; 


#if NT5_BUILD
#include "sohpnp.h"
#endif


#endif // _DIGISOH_H

