/* ex: se sw=4 sm: */
/****************************************************************************
 * Copyright (c) 1993-95 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 his software other than for whick it was
 * delevered.  This is an unpublished copyrighted work of Digi International
 * Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
 * prohibited.
 */
#ident "@(#) %W%	%E%"
#ident "@(#) $Header: /net/fs1/eng/cvs/sync/embed/dxb/nts.h,v 1.2 1997/10/21 20:48:01 gary Exp $"

#if !defined(lint)
static char nts_hid[] =
    "Copyright(c) 1994-95 Digi International Inc. All Rights Reserved";
#endif

#include <sys/types.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#if SVR4
#include <sys/termio.h>
#include <sys/termiox.h>
#include <sys/kmem.h>
#else
#include <sys/termios.h>
#endif
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/errno.h>
#include <sys/tihdr.h>
#include <sys/sysmacros.h>
#if !DITTY
#include <sys/cmn_err.h>
#endif
#if SCO
#include <sys/immu.h>
#include <sys/tty.h>
#include <sys/region.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/sioconf.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/tss.h>
#include <sys/sio.h>
#include <sys/v86.h>

#include "digitype.h"
#include "digi.h"
#include "vpix.h"
#else
#include <sys/param.h>
#include <sys/signal.h>
#include <sys/immu.h>
#include <sys/eucioctl.h>
#include <sys/strtty.h>
#include <sys/proc.h>
#include <sys/user.h>
#include "stdigi.h"
#endif
#include "ntsst.h"
#include "types.h"
#include "alloc.h"
#include "sxbshare.h"
#include "dxb.h"

#define xx_tty		ncxa_tty
#define xx_prn_tty	ncxa_prn_tty

#define xzdevflag	ntsdevflag

#define kalive		ntskalive
#define xzchan		ntschan
#define xz_tot_chan	nts_tot_chan
#define xznserv		ntsnserv
#define xzbrd		ntsbrd
#define xzchan		ntschan
#define xztxrx		ntstxrx
#define xznports	ntsnports
#define xzxtime		ntsxtime
#define xzxtra		ntsxtra
#define xz_info		nts_info
#define xztime		ntstime

#define xzuwinit	ntsuwinit
#define xzlwinit	ntslwinit
#define xzlrinit	ntslrinit
#define xzurinit	ntsurinit
#define xzinfo		ntsinfo

#define xzfail		ntsfail
#define xzinit		ntsinit
#define xzopen		ntsopen
#define xzclose		ntsclose
#define xzuwput		ntsuwput
#define xzuwsrv		ntsuwsrv
#define xzlwput		ntslwput
#define xzlwsrv		ntslwsrv
#define xzlrput		ntslrput
#define xzlrsrv		ntslrsrv
#define xzurput		ntsurput
#define xzursrv		ntsursrv
#define xzpoll		ntspoll
#define xziocdata	ntsiocdata

#define xzinit_debug	ntsinit_debug
#define xzopen_debug	ntsopen_debug
#define xzclose_debug	ntsclose_debug
#define xzuwput_debug	ntsuwput_debug
#define xzuwsrv_debug	ntsuwsrv_debug
#define xzlwput_debug	ntslwput_debug
#define xzlwsrv_debug	ntslwsrv_debug
#define xzlrput_debug	ntslrput_debug
#define xzlrsrv_debug	ntslrsrv_debug
#define xzurput_debug	ntsurput_debug
#define xzursrv_debug	ntsursrv_debug
#define xzpoll_debug	ntspoll_debug

/*
 * If INBUF is greater than 16 pages, be certain to redefine
 * bd_in and bd_out, they are currently unsigned short.
 */
#define INBUF		8 * NBPP		/* This MUST be 2 ^ n */

#define KME_OPEN	0x80

#define MTYPE(n0,n1)	(((n0) << 4) | (n1))

/*
 * SMASK is the mask for TOUT and RIN counters.
 */
#define SMASK		0x0ffff

#define DIST(from,to)	(((u_short)(to) - (u_short)(from)) & SMASK)

#define HTONS(ba,s)	*(char *)(ba) = (((s) >> 8) & 0x0ff); \
			*(char *) ((ba)+1) = ((s) & 0x0ff)

#define HTONL(ba,l)	*(char *)(ba) = (((l) >> 24) & 0x0ff); \
			*(char *)((ba)+1) = (((l) >> 16) & 0x0ff); \
			*(char *)((ba)+2) = (((l) >> 8) & 0x0ff); \
			*(char *)((ba)+3) = ((l) & 0x0ff)

#define NTOHS(s)	( (((*(char *) (s)) & 0x00ff) << 8) \
			| (((*(char *) ((s)+1)) & 0x0ff)) \
			)

#define NTOHL(l)	( (((*(char *) (l)) & 0x0ff) << 24) \
			| (((*(char *) ((l)+1)) & 0x0ff) << 16) \
			| (((*(char *) ((l)+2)) & 0x0ff) << 8) \
			| (((*(char *) ((l)+3)) & 0x0ff)) \
			)


#define FEP_CFLAGS	(CSIZE|CSTOPB|CREAD|PARENB|PARODD)
#define	FEP_IFLAGS	(IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK \
			|ISTRIP|IXON|IXANY|IXOFF)
#if SVR4
#   define FEP_OFLAGS	(OLCUC|ONLCR|OCRNL|ONOCR|ONLRET|TABDLY)
#   define FEP_LFLAGS	0
#else
#   define FEP_OFLAGS	(OPOST|OLCUC|ONLCR|OCRNL|ONOCR|ONLRET|TABDLY)
#endif
#define FEP_XFLAGS	(X_PAR|X_MODEM|X_CASE|X_TOSS|X_IXON)

/****************************************************************************
 * Request / Response flags.
 */
#define OPEN_TYPE	0x00000007
#define IMM_OPEN	0x00000001	/* Immediate open request */
#define P_OPEN		0x00000002	/* Persistent open */
#define IN_OPEN		0x00000004	/* Incoming open */
#define CL_TYPE		0x00000018
#define C_IMM_OPEN	0x00000008	/* Cancel Immediate open request */
#define C_IN_OPEN	0x00000010	/* Cancel Incoming/Persistent open */
#define SSYNC		0x00000020	/* Send a sync packet */
#define SMODEM		0x00000040	/* set modem outputs and HHNDSHK */
#define SEVENTS		0x00000080	/* Set EINT and MINT */
#define GSTATUS		0x00000100	/* Get ESTAT and MSTAT */
#define SFLOWC		0x00000200	/* software flow control characters */
#define SUFLAGS		0x00000400
#define SCHIMM		0x00000800	/* send character immediate */
#define SBREAK		0x00001000	/* send break immediate */
#define FLUSHIN		0x00002000
#define FLUSHOUT	0x00004000
#define PORT_CAPABILITY 0x00008000	/* get port capability */
#define BUFSZ		0x00010000	/* server tx and rx buffer sizes */
#define RESET_TS	0x00020000
#define SRXW		0x00040000	/* set rx high and low water levels */
#define STREQ		0x00080000	/* set window trigger */
#define SRWIN		0x00100000	/* Set RWIN */
#define SRPARMS		0x00200000	/* Set RMAX and RTIME */
#define STPARMS		0x00400000	/* Set TMAX and TTIME */
#define SLERR		0x00800000	/* Line error request packet */
#define STOP_I		0x01000000
#define START_I		0x02000000
#define STOP_O		0x04000000
#define START_O		0x08000000
#define GSEQUENCE	0x10000000	/* Get current sequence numbers */

/****************************************************************************
 * rts internal flags.
 */
#define CX_OPEN		0x00000001
#define CX_LINKED	0x00000002
#define CX_CAPABILITY	0x00000004
#define	CX_READY	0x00000008
#define CX_CLOSE	0x00000010
#define CX_KMEOPEN	0x00000020
#define CX_KALIVE	0x00000040
#define CX_FAILED	0x00000080
#define CX_RQENB	0x00000100

#define IFDATA		0x00001000
#define IFMODEM		0x00002000
#define IFBREAK		0x00004000
#define IFLOW		0x00008000

#define CX_IMOPEN	0x00010000
#define CX_INOPEN	0x00020000
#define CX_IMFAIL	0x00040000
#define CX_INFAIL	0x00080000


/****************************************************************************
 * Packet types
 */
#define DATA0		0x00
#define DATA1		0x01
#define DATA2		0x02
#define DATA3		0x03
#define DATA4		0x04
#define DATA5		0x05
#define DATA6		0x06
#define DATA7		0x07
#define DATA8		0x08
#define DATA9		0x09
#define WIN_UPD		0x0a
#define CMDPKT		0x0b
#define EVPKT		0x0c
#define MODPKT		0x0f	/* (module << 4) | MODPKT */
#define MODS8		0x0f8	/* Select a module # >= 8 */
#define ID_REQ		0x0fb	/* ID Request packet */
#define ID_RESP		0x0fc	/* ID Response packet */
#define KME_PKT		0x0fd	/* KME Packet */
#define DIAG		0x0fe	/* diagnostic packet */
#define RESET		0x0ff	/* Reset packet */

/****************************************************************************
 * Packet type 11 (Commands).
 */
#define OPENREQ		0x0a	/* Open request */
#define OPENRESP	0x0b	/* Open response */
#define SYNCREQ		0x0c	/* Sync request packet */
#define SYNCRESP	0x0d	/* Sync response packet */
#define SEQREQ		0x0e	/* Sequence request packet */
#define SEQRESP		0x0f	/* Sequence response packet */
#define STATUSREQ	0x10	/* Status request packet */
#define STATUSRESP	0x11	/* ESTAT and MSTAT response packet */
#define LERRREQ		0x12	/* Line error request packet */
#define LERRRESP	0x13	/* Line error response packet */
#define BUFREQ		0x14	/* Get size of server buffers */
#define BUFRESP		0x15	/* Size of server buffers */
#define CAPABILITYREQ	0x16	/* Capability request packet */
#define CAPABILITYRESP	0x17	/* Capability response packet */
#define SETUFLAGS	0x28	/* Set Baud and unix flag values */
#define ILLEGAL		0x29	/* Set iflags, oflags, and xflags */
#define SETEVENTS	0x2a	/* Set ESTAT and MSTAT event conditions */
#define SETTREQ		0x2b	/* Set TREQ to trigger a win seq packet */
#define SETHFLOW	0x2c	/* Set Modem output and Hrdwre flow cntrl */
#define SETRXW		0x2d	/* Set rx low and high water levels */
#define SETSFLOW	0x2e	/* Set software flow control characters */
#define SETRPARM	0x2f	/* Set RTIME and RMAX */
#define SETTPARM	0x30	/* Set TTIME and TMAX */
#define SENDCHAR	0x3c	/* Send character immediate */
#define SENDBRK		0x3d	/* Send break immediate */
#define FLUSHBUF	0x3e	/* Flush buffers 0,1,2,3 */
#define PAUSE		0x3f	/* Pause input/output */
#define UNPAUSE		0x40	/* Unpause input/output */

/****************************************************************************
 * Event flags.
 */
#define OPU		0x0001	/* output paused unconditionally by client */
#define OPS		0x0002	/* output paused by softwar flow control */
#define OPX		0x0004	/* output paused by xtra software flow */
#define OPH		0x0008	/* output paused by hardware flow control */
#define IPU		0x0010
#define IPS		0x0020	/* input paused by lo/hi water marks */
#define TXB		0x0040	/* Tx break pending */
#define TXI		0x0080	/* Tx immediate pending */
#define TXF		0x0100	/* Tx flow control char pending */
#define RXB		0x0200	/* Break recieved */


/****************************************************************************
 * Modem bit.
 */
#define N_DTR		0x0001
#define N_RTS		0x0002
#define N_CTS		0x0010
#define N_DSR		0x0020
#define N_RI		0x0040
#define N_DCD		0x0080

/****************************************************************************
 * OFLAGS
 */
#define N_OPOST		0x0001
#define N_OLCUC		0x0002
#define N_ONLCR		0x0004
#define N_OCRNL		0x0008
#define N_ONOCR		0x0010
#define N_ONLRET	0x0020
#define N_TABDLY	0x1800

/****************************************************************************
 * IFLAGS
 */
#define N_IGNBRK	0x0001
#define N_BRKINT	0x0002
#define N_IGNPAR	0x0004
#define N_PARMRK	0x0008
#define N_INPCK		0x0010
#define N_ISTRIP	0x0020
#define N_IXON		0x0400
#define N_IXOFF		0x0800
#define N_IXANY		0x1000
#define N_DOSMODE	0x8000

/****************************************************************************
 * CFLAGS
 */
#define N_CBAUD		0x000f	/* not supported */
#define N_CSIZE		0x0030
#define N_CSTOPB	0x0040
#define N_CREAD		0x0080
#define N_PARENB	0x0100
#define N_PARODD	0x0200
#define N_HUPCL		0x0400

#define N_CS5		0x0000
#define N_CS6		0x0010
#define N_CS7		0x0020
#define N_CS8		0x0030

/****************************************************************************
 * XFLAGS
 */
#define X_PAR		0x0001	/* enable Mark/Space parity */
#define X_MODEM		0x0002	/* enable in-band modem signalling */
#define X_CASE		0x0004	/* XCASE processing on output */
#define X_TOSS		0x0040	/* Toss IXANY character */
#define X_IXON		0x2000	/* enable aux output sfc characters */

#if !(SVR4)
typedef struct v86_t	V86_t;
#endif


/****************************************************************************
 * Data structures for ncxa and nts drivers.
 */
typedef struct {
    u_int	xt_flag;
    u_char	xt_cc[NCCS];
    u_char	xt_res;
} xtra_t;

typedef struct c_r_ts {
    u_int	bd_lflags;	/* Local flags */
    queue_t	*bd_q;
    queue_t	*bd_upq;
    queue_t	*bd_downq;

    int		bd_dev;
    mblk_t	*bd_mp;
    DXB_CMD	*bd_dxb_cmd_p ;
    DXB_EVENT	*bd_dxb_event_p ;
    u_char	*bd_dxb_cmd_q ;
    u_char	*bd_dxb_event_q ;

    int		bd_iocid;

    int		bd_fail;

    int		bd_kme;
    queue_t	*bd_kmeq;
    int		bd_stport;	/* Last port serviced before canput failed */
    u_short	bd_in;
    u_short	bd_out;
    u_char	*bd_inbuf;
} brd_t;

/****************************************************************************
* Structures necessary to support KME under streams.
*/
typedef struct {
    int		tr_cmd;
    caddr_t	tr_ptr;
} trans_t;

#define DIGI_GCAP	(('d' << 8) | 160)

/*****************************************************************************
 * Get a new 'mp' and assorted housekeeping
 */
#define GET_NEW_MP(mpp,mp,size) \
{ \
    if(((mp) = allocb(size, BPRI_LO)) != NULL) { \
	*(mpp) = mp; \
	mpp = &((mp)->b_cont); \
    } \
}

/****************************************************************************
* Debug macros to selectively enable printf's.
*/
#if DBUG
#define Static
#define DEBUG0(a,b,c) \
    if (a) (void) cmn_err((b), (c))

#define DEBUG1(a,b,c,d) \
    if (a) (void) cmn_err((b), (c), (d))

#define DEBUG2(a,b,c,d,e) \
    if (a) (void) cmn_err((b), (c), (d), (e))

#define DEBUG3(a,b,c,d,e,f) \
    if (a) (void) cmn_err((b), (c), (d), (e), (f))

#define DEBUG4(a,b,c,d,e,f,g) \
    if (a) (void) cmn_err((b), (c), (d), (e), (f), (g))

#define DEBUG5(a,b,c,d,e,f,g,h) \
    if (a) (void) cmn_err((b), (c), (d), (e), (f), (g), (h))

#define DEBUG6(a,b,c,d,e,f,g,h,i) \
    if (a) (void) cmn_err((b), (c), (d), (e), (f), (g), (h), (i))
#else
#define Static	static
#define DEBUG0(a,b,c)
#define DEBUG1(a,b,c,d)
#define DEBUG2(a,b,c,d,e)
#define DEBUG3(a,b,c,d,e,f)
#define DEBUG4(a,b,c,d,e,f,g)
#define DEBUG5(a,b,c,d,e,f,g,h)
#define DEBUG6(a,b,c,d,e,f,g,h,i)
#endif

#if (SVR42MP && SVR4)
#define PUTQ(q,mp)		putq((q), (mp))
#define PUTBQ(q,mp)		putbq((q), (mp))
#define PUTNEXT(q, mp)		putnext((q), (mp))
#define CANPUT(q)		canput(q)
#define CANPUTNEXT(q)		canputnext(q)
#define PUTCTL(q,mp)		putctl((q), (mp))
#define PUTCTL1(q,mp,s)		putctl1((q), (mp), (s))
#define PUTNEXTCTL(q,mp)	putnextctl((q), (mp))
#define PUTNEXTCTL1(q,mp,s)	putnextctl1((q), (mp), (s))
#else
#define PUTQ(q,mp)		putq((q), (mp))
#define PUTBQ(q,mp)		putbq((q), (mp))
#define PUTNEXT(q,mp)		Putnext((q), (mp))
#define CANPUT(q)		canput(q)
#define CANPUTNEXT(q)		canput((q)->q_next)
#define PUTCTL(q,mp)		putctl((q), (mp))
#define PUTCTL1(q,mp,s)		putctl1((q), (mp), (s))
#define PUTNEXTCTL(q,mp)	putctl((q)->q_next, (mp))
#define PUTNEXTCTL1(q,mp,s)	putctl1((q)->q_next, (mp), (s))
#endif

#if 0
#define assert(x, msg)	if (!(x)) panic(msg)
#else
#	if SVR4
#	define assert(x, msg)	if(!(x))	xxyhowl(__LINE__, (msg))
#	else
#	define assert(x, msg)	if(!(x))	xx_howl(__LINE__, (msg))
#	endif
#endif

/*
 * The new SCO 3.2v5 OS supports some ioctl MACROS that we used 
 * to support in our own digi.h.  We will need to keep the old 
 * values around and support them as long as we keep using the
 * same object code for the 3.2v5 OS and for previous OSes.
 */

#define	DIGI_TIOCMSET	('d'<<8) | 252		/* set modem ctrl state	*/
#define	DIGI_TIOCMGET	('d'<<8) | 253		/* set modem ctrl state	*/
#define	DIGI_TIOCMBIC	('d'<<8) | 254		/* set modem ctrl state */
#define	DIGI_TIOCMBIS	('d'<<8) | 255		/* set modem ctrl state */
