
/*
 Copyright 1999 by Digi International (www.digi.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the 
 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 PURPOSE.  See the GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * $Id: sdxb_prot.h,v 1.21 2000/11/29 18:02:48 robertw Exp $
 *
 * Definitions for the sdxb protocol.  This file is intended to contain
 * things that both the FEP and the driver need to know.
 *
 */

/*
 * S/DXB messages are data messages and command messages.  The data
 * messages are just DXB data messages, and need no further definition
 * here.  The command messages are DXB control-data messages, and the
 * first byte of the control portion indicates what the packet type
 * is.
 */

/*
 * Values for the command type byte, and the lengths of the command
 * and response packets
 */

#define	SDXB_SYNC			0x0
#define	SDXB_SYNC_CLEN		0x04
#define	SDXB_SYNC_RLEN		0x04

#define	SDXB_BUFSIZE			0x1
#define	SDXB_BUFSIZE_CLEN	0x04
#define	SDXB_BUFSIZE_RLEN	0x0C

#define	SDXB_MSTAT			0x2
#define	SDXB_MSTAT_CLEN		0x04
#define	SDXB_MSTAT_RLEN		0x04

#define	SDXB_EVENT			0x3
#define	SDXB_EVENT_CLEN		0x04
#define	SDXB_EVENT_RLEN		0x04

#define	SDXB_LERR			0x4
#define	SDXB_LERR_CLEN		0x04
#define	SDXB_LERR_RLEN		0x30

#define	SDXB_PCAP			0x5
#define	SDXB_PCAP_CLEN		0x04
#define	SDXB_PCAP_RLEN		0x08

#define	SDXB_PARAM			0x6
#define	SDXB_PARAM_CLEN		0x28
#define	SDXB_PARAM_RLEN		0x28

#define	SDXB_WMARK			0x7
#define	SDXB_WMARK_CLEN		0x0C
#define	SDXB_WMARK_RLEN		0x0

#define	SDXB_SEQ			0x8
#define	SDXB_SEQ_CLEN		0x18
#define	SDXB_SEQ_OCLEN		0x14
#define	SDXB_SEQ_RLEN		0x0C

#define	SDXB_SCIMMED			0x9
#define	SDXB_SCIMMED_CLEN	0x04
#define	SDXB_SCIMMED_RLEN	0x0

#define	SDXB_BREAK			0xA
#define	SDXB_BREAK_CLEN		0x04
#define	SDXB_BREAK_RLEN		0x0

#define	SDXB_FLUSH			0xB
#define	SDXB_FLUSH_CLEN		0x04
#define	SDXB_FLUSH_RLEN		0x0

#define	SDXB_PAUSE			0xC
#define	SDXB_PAUSE_CLEN		0x04
#define	SDXB_PAUSE_RLEN		0x0

#define	SDXB_SCRECV			0xD
#define	SDXB_SCRECV_CLEN	0x0
#define	SDXB_SCRECV_RLEN	0x04

#define	SDXB_FREXCP			0xE
#define	SDXB_FREXCP_CLEN	0x0
#define	SDXB_FREXCP_RLEN	0x04

#define SDXB_MARKIDLE			0xF
#define SDXB_MARKIDLE_CLEN	0x4
#define SDXB_MARKIDLE_RLEN	0x4

#define	SDXB_TXPRINT			0x10
#define	SDXB_TXPRINT_CLEN	0x50
#define	SDXB_TXPRINT_RLEN	0x50

#define SDXB_ISDN			0x80

#define	SDXB_PROTERR			0xFF
#define	SDXB_PROTERR_CLEN	0x0
#define	SDXB_PROTERR_RLEN	0x4 /* plus header from bad messages */

#define SDXB_MAX_CLEN		0x50
#define SDXB_MAX_RLEN		0x50

/*
 * Structures defining the format of the commands
 */

struct sdxb_sync_command {
    dxb_uint8	ctype ;		/* command type */
    dxb_uint8	flags ;
#				define	DRAIN	0x1
    dxb_uint16	sseq ;		/* opaque sequence number */
} ;

struct sdxb_sync_response {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;
    dxb_uint16	sseq ;		/* sseq from the command */
} ;

struct sdxb_bufsize_command {
    dxb_uint8	ctype ;
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint8	fill3 ;
} ;

struct sdxb_bufsize_response {
    dxb_uint8	ctype ;
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint8	fill3 ;
    dxb_uint32	insize ;	/* size of input buffer */
    dxb_uint32	outsize ;	/* size of output buffer */
} ;

struct sdxb_mstat_command {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED and/or RESPPRI */
    dxb_uint8	mint ;		/* signals that cause notification of change */
    dxb_uint8	mout ;		/* sets modem outputs */
} ;

struct sdxb_mstat_response {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED or not */
    dxb_uint8	mstat ;		/* Current signal status */
    dxb_uint8	fill ;
} ;

struct sdxb_event_command {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED and/or RESPPRI */
    dxb_uint16	eint ;		/* events  that cause notification of change */
} ;

struct sdxb_event_response {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED or not */
    dxb_uint16	estat ;		/* Current event status */
} ;

struct sdxb_lerr_command {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED and/or RESPPRI and/or CLEAR */
    dxb_uint16	ltime ;		/* time between periodic events in ms */
} ;

struct sdxb_lerr_response {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED or not */
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint32	norun ;		/* UART overrun errors */
    dxb_uint32	noflow ;	/* buffer overflow errors */
    dxb_uint32	nframe ;	/* framing errors */
    dxb_uint32	nparity ;	/* parity errors */
    dxb_uint32	nbreak ;	/* breaks rec'd */
    dxb_uint32  nsframe ;	/* count of short frames */
    dxb_uint32  nlframe ;	/* count of long frames */
    dxb_uint32  nrxabrt ;	/* count of received aborts */
    dxb_uint32  nbadcrc ;	/* count of bad crc pkts */
    dxb_uint32  ntxurun ;	/* number of tx underruns */
    dxb_uint32  ntxabrt ;	/* number of tx aborts */
} ;

struct sdxb_pcap_command {
    dxb_uint8	ctype ;
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint8	fill3 ;
} ;

struct sdxb_pcap_response {
    dxb_uint8	ctype ;
    dxb_uint8	ptype ;		/* port type */
#				define	PTYPE_RS232	0
#				define	PTYPE_RS422	1
#				define	PTYPE_PAR	2
#				define	PTYPE_MODEM	3
#				define	PTYPE_OTHER	5

    dxb_uint8	msig ;		/* signals supported by hardware */
    dxb_uint8	mhard ;		/* sigs that do flow control in hw */
    dxb_uint32	cvec ;		/* capability vector */
#				define CVEC_SPACE_PARITY	0x01
#				define CVEC_MARK_PARITY		0x02
#				define CVEC_TX_PRINT		0x04
} ;

struct sdxb_param_command {
    dxb_uint8	ctype ;
    dxb_uint8	fill1 ;
    dxb_uint16	cflags ;	/* control flags */
    dxb_uint16	iflags ;	/* input processing flags */
    dxb_uint16	oflags ;	/* output processing flags */
    dxb_uint16	xflags ;	/* miscellaneous flags */
    dxb_uint16	fflags ;	/* frame mode flags */
    dxb_uint32	txaccm ;	/* tx async control character map */
    dxb_uint32	rxaccm ;	/* rx async control character map */
    dxb_uint32	speed ;		/* raw speed in bps */
    dxb_uint8	mflow ;		/* modem control sigs used for hw flow ctrl */
    dxb_uint8	mctrl ;		/* flow control for XON/XOFF chars */
    dxb_uint8	xon ;		/* xon/xoff chars */
    dxb_uint8	xoff ;
    dxb_uint8	xxon ;		/* auxiliary set of xon/xoff chars */
    dxb_uint8	xxoff ;
    dxb_uint8	lnext ;		/* flow control escape char */
    dxb_uint8	spcr ;		/* special character recognition char */
    dxb_uint8	mspesc ;	/* esc. char for MS parity reporting */
    dxb_uint8	idel ;		/* char stripped from rx stream */
    dxb_uint8	msbr ;		/* MS break replacement char */
    dxb_uint8	mser ;		/* MS error replacement char */
    dxb_uint32	maxlen ;	/* maximum packet length */
} ;

#define sdxb_param_response sdxb_param_command

struct sdxb_wmark_command {
    dxb_uint8	ctype ;
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint8	fill3 ;
    dxb_uint32	rlow ;		/* rx flow ctrl low water mark */
    dxb_uint32	rhigh ;		/* rx flow ctrl high water mark */
} ;

struct sdxb_seq_command {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED */
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint32	rmax ;		/* see S/DXB spec for rmax, rtime, etc. */
    dxb_uint32	rtime ;
    dxb_uint32	tmax ;
    dxb_uint32	ttime ;
    dxb_uint32	icdtime ;	/* inter-character delay time (max) */
} ;

struct sdxb_seq_response {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* IMMED or not */
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint32	rin ;		/* current RIN value */
    dxb_uint32	tout ;		/* current ROUT value */
} ;

struct sdxb_scimmed_command {
    dxb_uint8	ctype ;
    dxb_uint8	sic ;		/* char to send immediately */
    dxb_uint8	fill ;
    dxb_uint8	fill2 ;
} ;

struct	sdxb_break_command {
    dxb_uint8	ctype ;
    dxb_uint8	fill ;
    dxb_uint16	btime ;		/* break durration in ms */
} ;

struct sdxb_flush_command {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* What to flush */
#				define SDXB_FLUSH_INPUT		0x1
#				define SDXB_FLUSH_OUTPUT	0x2
#				define SDXB_ABORT_PACKET	0x4
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
} ;

struct sdxb_pause_command {
    dxb_uint8	ctype ;
    dxb_uint8	pset ;		/* bits to set */
    dxb_uint8	preset ;	/* bits to clear */
    dxb_uint8 	fill ;		/* pset and preset match low 6 bits of ESTAT */
} ;

struct sdxb_screcv_response {
    dxb_uint8	ctype ;		/* special char will next in data stream */
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
    dxb_uint8	fill3 ;
} ;

struct sdxb_frexcp_response {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;		/* what sort of exception */
#				define FREXCP_TOOBIG	0x01
#				define FREXCP_FCSERR	0x02
#				define FREXCP_FRERR	0x04
#				define FREXCP_ABRT	0x08
#				define FREXCP_OFLW	0x10
    dxb_uint8	fill1 ;
    dxb_uint8	fill2 ;
} ;

#define SDXB_TXP_PLEN 28
#define SDXB_TXP_TLEN 10

struct sdxb_txprint_command {
    dxb_uint8	ctype ;
    dxb_uint8	flags ;
#				define TXPRINT_REPLY_ONLY 0x01
    dxb_uint16	xpflags ;		/* Historical, saved for driver */
    dxb_uint16	maxcps ;		/* Max printer CPS */
    dxb_uint16	maxchar ;		/* Max chars in print queue */
    dxb_uint16	bufsize ;		/* Printer Buffer size */
    dxb_uint8	onlen ;			/* length of onstr */
    dxb_uint8	offlen ;		/* length of offstr */
    dxb_uint8	onstr[SDXB_TXP_PLEN] ;	/* Printer on string */
    dxb_uint8	offstr[SDXB_TXP_PLEN] ;	/* Printer off string */
    dxb_uint8	term[SDXB_TXP_TLEN] ;	/* terminal name */
    dxb_uint8	fill[2] ;		/* padding */
} ;

#define sdxb_txprint_response sdxb_txprint_command

struct sdxb_proterr_response {
    dxb_uint8	ctype ;
    dxb_uint8	fill ;
    dxb_uint16	offset ;	/* offset where err was detected */
    dxb_uint8	badreq[32] ;	/* min 4, up to 32 bytes */
} ;

struct sdxb_markidle_response {
    dxb_uint8	ctype ;
    dxb_uint8	fill[3] ;
} ;


/*
 * Flag, etc. definitions common to different packet types
 */

#define	SDXB_IMMED		0x01 /* send an immediate reply */
#define	SDXB_RESPPRI		0x02 /* send replies high-priority */
#define SDXB_HIPRI		SDXB_RESPPRI /* for backwards compatability */
#define	SDXB_CLEAR		0x04 /* clear counters */

/*
 * Flag values for the parameters command
 */

#define	SDXB_CBAUD		0x000F /* not supported */
#define	SDXB_CSIZE		0x0030 /* character size */
#define	  SDXB_CS5		0x0000
#define	  SDXB_CS6		0x0010
#define	  SDXB_CS7		0x0020
#define	  SDXB_CS8		0x0030
#define	SDXB_CSTOPB		0x0040 /* two stop bits if set */
#define	SDXB_CREAD		0x0080 /* read enable */
#define	SDXB_PARENB		0x0100 /* parity enable */
#define	SDXB_PARODD		0x0200 /* parity odd */
#define	SDXB_HUPCL		0x0400 /* not supported */
#define	SDXB_CLOCAL		0x0800 /* not supported */

#define	SDXB_XPAR		0x0001 /* mark/space parity */
#define	SDXB_XMODEM		0x0002 /* in-band modem signaling */
#define	SDXB_XCASE		0x0004 /* not supported */
#define	SDXB_XEDATA		0x0008 /* err chars used as data regardless */
#define SDXB_XMSBRC		0x0010 /* MS break replacement char */
#define SDXB_XMSERC		0x0020 /* MS error replacement char */
#define	SDXB_XTOSS		0x0040 /* toss ixany chars */
#define	SDXB_XSCRAFTER		0x0080 /* sp chr rpt comes *after* character */
#define	SDXB_XMSPAR		0x0100 /* MS parity reporting */
#define	SDXB_XSCR		0x0200 /* special char recognition */
#define	SDXB_XSCRHP		0x0400 /* SCR is sent high priority */
#define	SDXB_XSTRIP		0x0800 /* unconditional char striping */
#define SDXB_XRBLK		0x1000 /* alternate receive blocking */
#define	SDXB_XIXON		0x2000 /* aux xon/xoff chars enabled  */
#define	SDXB_XLNXT		0x4000 /* enable lnext character */
#define	SDXB_XALTPIN		0x8000 /* Swap DCD and DSR signals */

#define	SDXB_IGNBRK		0x0001 /* ignore all input break conditions */
#define	SDXB_BRKINT		0x0002 /* break sends break event (ESTAT) */
#define	SDXB_IGNPAR		0x0004 /* errored characters are ignored */
#define	SDXB_PARMRK		0x0008 /* err chars marked in input stream */
#define	SDXB_INPCK		0x0010 /* input parity errors are detected */
#define SDXB_ISTRIP		0x0020 /* input chars masked with 0x7f */
#define	SDXB_INLCR		0x0040 /* not supported */
#define	SDXB_IGNCR		0x0080 /* not supported */
#define	SDXB_ICRNL		0x0100 /* not supported */
#define	SDXB_IUCLC		0x0200 /* not supported */
#define	SDXB_IXON		0x0400 /* output software flow ctrl enable */
#define	SDXB_IXANY		0x0800 /* any char can restart paused output */
#define	SDXB_IXOFF		0x1000 /* input software flow ctrl enable */
#define	SDXB_DOSMODE		0x8000 /* 16450-compatible error indication */

#define	SDXB_OPOST		0x0001 /* not supported */
#define	SDXB_OLCUC		0x0002 /* not supported */
#define	SDXB_ONLCR		0x0004 /* map nl to cr-nl on output */
#define	SDXB_OCRNL		0x0008 /* map cr to nl on output */
#define	SDXB_ONOCR		0x0010 /* no cr if already at col 0 */
#define SDXB_ONLRET		0x0020 /* assume nl does a cr */
#define SDXB_TABDLY		0x1800 /* tabs expand to 8 spaces if ==1800 */

#define	SDXB_FMODE		0x0001 /* in frame mode */
#define	SDXB_SYNCMODE		0x0002 /* sync frame mode (not async) */
#define	SDXB_SLIP		0x0004 /* async framing is slip */
#define	SDXB_OFCS		0x0008 /* FCS added to output frames */
#define	SDXB_IFCS		0x0010 /* FCS is checked on input frames */
#define	SDXB_IFCSSTRIP		0x0020 /* FCS is removed from input frames */
#define	SDXB_FCSV41		0x0040 /* FCS is CRC V.41 (else CRC-16) */
#define	SDXB_IDLEM		0x0080 /* Mark in idle mode (else flags) */
#define	SDXB_SPAD		0x0100 /* Send pad chars before flag  */
#define	SDXB_PAD00		0x0200 /* Pad char is 00 (else AA) */
#define SDXB_SHFLAG		0x0400 /* flag can be shared between frames */
#define SDXB_EXDATA		0x0800 /* data included w/frame excp. rpts */
#define SDXB_IDLEMDET		0x1000 /* detect mark idle reception (sync) */
#define SDXB_DIGITAL    0x2000 /* data use a digital DSP image, rather than modem pump */


/*
 * Modem control bit assignments
 */

#define	SDXB_DTR		0x01
#define SDXB_RTS		0x02
#define	SDXB_CTS		0x10
#define	SDXB_DSR		0x20
#define	SDXB_RI			0x40
#define	SDXB_DCD		0x80
#define	SDXB_CD			SDXB_DCD /* alias for convenience */

/* differentiate modem in and modem out signals */
#define SDXB_MDMIN	(SDXB_CTS | SDXB_DSR | SDXB_RI | SDXB_DCD)
#define SDXB_MDMOUT	(SDXB_DTR | SDXB_RTS)

/*
 * Event flags
 */
#define	SDXB_OPU		0x0001 /* out paused unconditionally */
#define	SDXB_OPS		0x0002 /* out paused by reg. sw flow ctrl */
#define	SDXB_OPX		0x0004 /* out paused by extra sw f/c */
#define	SDXB_OPH		0x0008 /* out paused by hw f/c */
#define	SDXB_OP			0x000F /* out paused in any fashion */
#define	SDXB_IPU		0x0010 /* in paused uncontitionally */
#define	SDXB_IPS		0x0020 /* in paused by high/low water marks */
#define	SDXB_IP			0x0030 /* in paused either way */
#define	SDXB_TXB		0x0040 /* tx break pending */
#define	SDXB_TXI		0x0080 /* tx immediate pending */
#define	SDXB_TXF		0x0100 /* tx flow ctrl char pending */
#define	SDXB_RXB		0x0200 /* break received */

/* the fixed cost for dxb messages under S/DXB is 8 */
#define	SDXB_FIXED_COST		0x08

