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

/*
 *----------------------------------------------------------------------
 * Include File:  dxbloop.h
 *
 * Data structures and defines used to simulate DStreams and lower protocols.
 *
 *
 * Revision History:
 *
 *      Date   Revision  Author          Modification Notes
 *
 *   12/19/97  1.0.0     P. Donovan      Initial release
 *
 *----------------------------------------------------------------------
 */

/*
 * ----------------------------------------------------------------------
 *  Event Log Definitions.
 *----------------------------------------------------------------------
 */
#include "asyn_msg.h"                /* Generated error message defines. */

/*
 *----------------------------------------------------------------------
 * Macro Definitions.
 *----------------------------------------------------------------------
 */

/*
 *----------------------------------------------------------------------
 *  Definitions.
 *----------------------------------------------------------------------
 */

#define DS_MAX_DATA_BUF_QUEUED      15
#define DS_DEF_DATA_COUNT           100
#define DS_DEF_PUT_COUNT            500
#define DS_DEF_GET_COUNT            140
#define DS_DEF_BUF_SIZE             1024


//
// Structures needed to simulate DXB and protocols beneath it.
//

typedef struct DS_STREAM *PDSTREAM;

//
// One of these per DsOpenDevice()
//
typedef struct _DS_DEV {
   KSPIN_LOCK  Lock;
   LIST_ENTRY  StreamsQ;               // Queue of open streams
   POOL_LIST   DataPool;
   POOL_LIST   PutPool;
   POOL_LIST   GetPool;
   ULONG       InUse;
   KTIMER      Poll;                   // Kernel Timer structure.
   KDPC        PollFunction;           // Kernel DPC structure.
   LARGE_INTEGER PollCycle;              // Count in 100ns ticks.
} DS_DEV, *PDS_DEV;

//
// Stream structure
// One of these per DsOpen()
//
typedef struct _DS_STREAM {
   LIST_ENTRY     ListEntry;              // Allow queueing of descriptors
   KSPIN_LOCK     Lock;
   PDS_DEV        Device;
   LIST_ENTRY     PutQ;
   LIST_ENTRY     GetQ;
   PPORT_EXT      PortExt;
   ULONG          Port;
   // Count of S/DXB put commands on the Put Queue which have already
   // called the Put Completion routine.
   ULONG          BufferCount;

   // Keep track of Speed for simulating Loopback response
   ULONG          Speed;

   // System time when data transfer is complete
   LARGE_INTEGER  DataEndTime;

   USHORT         Sseq;
} DS_STREAM, *PDS_STREAM;

typedef struct _PEND_GET {
   LIST_ENTRY  ListEntry;              // Allow queueing of descriptors
#ifdef DBG
   ENTRY_STATE EntryState;             // For debugging purposes
#endif
   HANDLE   Hstream;
   ULONG    ControlLength;
   PUCHAR   DataBuffer;
   ULONG    DataLength;
   HANDLE   Context;
   UCHAR    Priority;
   PUTGETMSG_COMPLETION_ROUTINE CompletionRoutine;
} PEND_GET, *PPEND_GET;

typedef enum
{
   SdxbType, DataType, PendingType
} PUT_TYPE;

typedef struct _PEND_PUT {
   LIST_ENTRY  ListEntry;              // Allow queueing of descriptors
#ifdef DBG
   ENTRY_STATE EntryState;             // For debugging purposes
#endif
   PUT_TYPE    Type;                   // Put message type
   // This structure is only used when the Completion routine is still pending
   struct {
      HANDLE   hStream;
      PUCHAR   pControlBuffer;
      ULONG    ControlLength;
      PUCHAR   pDataBuffer;
      ULONG    DataLength;
      HANDLE   Context;
      UCHAR    Priority;
      PUTGETMSG_COMPLETION_ROUTINE CompletionRoutine;
   } Pending;                    // Pending put (Optional)
   // S/DXB Response
   struct {
      // Allow space for the largest S/DXB command/response
      UCHAR    Buffer[sizeof(PORT_PARAM_RSP_SDXB)];
      ULONG    Length;           // Length of data
   } Sdxb;
   // Data buffer
   RX_BUF  Data;

} PEND_PUT, *PPEND_PUT;

//
// Function prototypes used locally to Stubs.c
//
VOID DsStreamsPoll( IN PKDPC pDpc, IN PDS_DEV pCard,
                    IN PVOID SystemArg1, IN PVOID SystemArg2);
BOOLEAN DsProcessPut( IN PDS_STREAM pPort, IN PPEND_PUT pPut);
VOID DsCalcTxEnd(IN ULONG Speed, IN ULONG Length, OUT PLARGE_INTEGER pStamp);

