/*
 * Common header that forms the first 3 words of each command/response.
 */
typedef struct {
	uint32	Command ;
	uint32	Interface ;
	uint32	SequenceNo ;
} ApiHeader_t ;

/*
 * Command specifies what this command/response is all about.
 *
 * Except for GET_CONFIG, Interface specifies which interface we wish
 * to operate on..
 *
 * SequenceNo is an opaque value that can be used to pair
 * command/responses by an application. The firmware simply
 * ensures that a response bears the same sequence number as
 * the command.
 *
 * API_NAK is the value sent hack to the host when a command
 * could not be completed due to it making no sense (not that 
 * the hardware didn't like it, but that (say) Interface is
 * out of range.) The failing command can be correllated with
 * the reply by means of the SequenceNo..
 */

/*
 * Possible values for Command.
 */
#define	GET_CONFIG		0x1001	/* Payload is GetConfig_t */
#define	GET_IFACE_CONFIG	0x1002	/* Payload is GetIfaceConfig_t */
#define	GET_IFACE_MODE		0x1003	/* Payload is IfaceMode_t */
#define	SET_IFACE_MODE		0x1004	/* Payload is IfaceMode_t */
#define	GET_IFACE_STATE		0x1005	/* Payload is IfaceState_t */
#define	SET_IFACE_STATE		0x1006	/* Payload is IfaceState_t */
#define	GET_DI_CONFIG		0x1007	/* Payload is DIConfig_t */
#define	SET_DI_CONFIG		0x1008	/* Payload is DIConfig_t */
#define	GET_RFC1406		0x1009	/* Payload is RFC1406_t */
#define	EVENT_RESPONSE		0x100A	/* Payload is EventResponse_t */
#define	READ_REGISTER		0x100B	/* Payload is RWRegister_t */
#define	WRITE_REGISTER		0x100C	/* Payload is RWRegister_t */
#define	HW_SPECIFIC		0x100D	/* Payload is HWSpecific_t */
#define	API_NAK			0x100E	/* No payload, nak of command.. */

/*
 * Config_t
 *
 * General configuration response. Returns basic information such as
 * hardware type, revision levels, API capabilities, and a map of
 * interfaces currently present.
 */
typedef struct {
	ApiHeader_t	Header ;
	uint32		HardwareType ;
	uint32		HardwareRevision ;
	uint32		FirmwareRevision ;
	uint32		APIRevision ;
	uint32		Interfaces ;
} Config_t ;

/*
 * Possible values for HardwareType.
 */
#define	HW_T1MUX	1
#define	HW_ISAPRI	2

/*
 * Hardware/Firmware Revision levels are hardware specific.
 */

/*
 * Possible values for APIRevision.
 */
#define	API_V1		1

/*
 * Macros for working with Interfaces.
 *
 *	IFACE_PRESENT(map, num)
 *		Returns true/false depending whether interface "num" is
 *		present in the bitmap "map".
 *
 *	IFACE_MAP(num)
 *		Returns the bitmap value representing interface "num".
 */
#define	IFACE_PRESENT(map, num)		((map) & IFACE_MAP(num))
#define	IFACE_MAP(num)			(1 << (num))


/*
 * IfaceConfig_t
 *
 * Per interface capabilities, line type, clocking options, drop and
 * insert.
 */
typedef struct {
	ApiHeader_t	Header ;
	uint32		Capabilities ;
	uint32		DIMap ;		/* Use IFACE_MAP/PRESENT macros */
} IfaceConfig_t ;

/*
 * Possible component values for Capabilities.
 */
#define	T1_OK		0x0001
#define E1_OK		0x0002
#define	LONG_HAUL_OK	0x0004
#define	SHORT_HAUL_OK	0x0008
#define	LOCAL_CLOCK_OK	0x0010


/*
 * IfaceMode_t
 *
 * Per interface modes of operation, frame type, line coding, LBO etc
 */
 
typedef struct {
	ApiHeader_t	Header ;
	uint32		Mode ;
	uint32		Options ;
} IfaceMode_t ;

/*
 * Macros and symbolic constants for Mode..
 */
#define	SPAN_MODE(x)	((x) & 0x7)
#define	SPAN_T1		0x0
#define	SPAN_E1		0x1

#define	DI_MODE(x)	((x) & 0x8)
#define DI_INACTIVE	0x0
#define	DI_ACTIVE	0x8

#define	CLOCK_SOURCE(x)	((x) & 0x30)
#define	CLOCK_LINE	0x00
#define	CLOCK_LOCAL	0x10
#define	CLOCK_THRU	0x20

#define	LINE_CODING(x)	((x) & 0x1c0)
#define	LINE_AMI	0x00
#define	LINE_B8ZS	0x40
#define	LINE_HDB3	0x80

#define	FRAME_FORMAT(x)	((x) & 0xe00)
#define	FRAME_D4	0x000
#define	FRAME_ESF	0x200
#define	FRAME_CEPT	0x400
/*
 * French Special
 */
#define FRAME_NOCRC     0x600

#define	SPAN_TYPE(x)	((x) & 0x1000)
#define	SPAN_LONG	0x0000
#define	SPAN_SHORT	0x1000

#define JITTER_MODE(x)	((x) & 0x6000)
#define	JITTER_AUTO	0x0000
#define	JITTER_TX	0x2000
#define	JITTER_RX	0x4000
#define	JITTER_NONE	0x6000

#define	SIGNAL_MODE(x)	((x) & 0x78000)
#define	SIGNAL_NONE	0x00000
#define	SIGNAL_CPE_GS	0x08000
#define	SIGNAL_EM	0x10000
#define	SIGNAL_MOS	0x18000

/*
 * Macros and symbolic constants for Options..
 */
#define	SPAN_LBO(x)	((x) & 0x3)		/* Long Haul Line Build Out */
#define	SPAN_LBO_0	0x0			/* 0dB */
#define	SPAN_LBO_075	0x1			/* -7.5dB */
#define	SPAN_LBO_150	0x2			/* -15dB */
#define	SPAN_LBO_225	0x3			/* -22.5dB */

#define	RX_GAIN(x)	((x) & 0x4)		/* Long Haul Receiver Gain */
#define	RX_GAIN_LO	0x0			/* Low Gain */
#define	RX_GAIN_HI	0x4			/* High Gain */


/*
 * IfaceState_t
 *
 * Per interface state information, alarm status, signalling, busy-outs and
 * the event reporting mask.
 */
typedef struct {
	ApiHeader_t	Header ;
	uint32		State ;
	uint32		Signalling ;
	uint32		BusyOut ;
	uint32		DelayedBusyOut ;
	uint32		EventMask ;
} IfaceState_t ;

/*
 * Macros and symbolic constants for State..
 */
#define	LOOPBACK_MASK		0x3
#define	LOOPBACK_STATE(x)	((x) & LOOPBACK_MASK)
#define	NO_LOOPBACK		0x0
#define	LINE_LOOPBACK		0x1
#define	PAYLOAD_LOOPBACK	0x2

#define	LOS_STATE(x)		((x) & 0x4)
#define	SIGNAL_PRESENT		0x0
#define	SIGNAL_LOST		0x4

#define	ALARM_CONDITION(x)	((x) & 0x18)
#define	NO_ALARM_CONDITION	0x00
#define	YELLOW_ALARM_CONDITION	0x08
#define	BLUE_ALARM_CONDITION	0x10
#define	RED_ALARM_CONDITION	0x18

#define TX_ALARM(x)		((x) & 0x60)
#define	TX_NO_ALARM		0x00
#define	TX_YELLOW_ALARM		0x20
#define	TX_BLUE_ALARM		0x40

#define	RECEIVE_ATTEN(x)	((x) & 0x180)
#define	RECEIVE_ATTEN_0		0x000
#define	RECEIVE_ATTEN_1		0x080
#define	RECEIVE_ATTEN_2		0x100
#define	RECEIVE_ATTEN_3		0x180

#define RX_CRC_ERROR(x)         ((x) & 0x600
#define RX_NO_CRC_ERROR         0x000
#define RX_CRC_ERROR_LOCAL      0x200
#define RX_CRC_ERROR_REMOTE     0x400


/*
 * Macros and symbolic constants for EventMask..
 */
#define	ERROR_EVENT		0x01
#define	ALARM_EVENT		0x02
#define	LOOPBACK_EVENT		0x04
#define	SIGNAL_EVENT		0x08
#define	FDL_RX_EVENT		0x10
#define	FDL_TX_EVENT		0x20

/*
 * DIConfig_t
 *
 * Per interface drop and insert configuration.
 */
typedef struct {
	ApiHeader_t	Header ;
	uint32	Mapping[32] ;
} DIConfig_t ;


/*
 * SNMP RFC 1406 statistics reporting.
 *
 * We currently only support the DS1 Near End Group.
 */
typedef struct {
	uint32		ES ;			/* dsx1......ESs */
	uint32		SES ;			/* dsx1......SESs */
	uint32		SEFS ;			/* dsx1......SEFS */
	uint32		UAS ;			/* dsx1......UASs */
	uint32		CSS ;			/* dsx1......CSSs */
	uint32		PCV ;			/* dsx1......PCVs */
	uint32		LES ;			/* dsx1......LESs */
	uint32		BES ;			/* dsx1......BESs */
	uint32		DM ;			/* dsx1......DMs */
	uint32		LCV ;			/* dsx1......LCVs */
} RfcInterval_t ;

typedef	struct {
	ApiHeader_t	Header ;
	uint32		TimeElapsed ;		/* dsx1TimeElapsed */
	uint32		ValidIntervals ;	/* dsx1ValidIntervals */
	uint32		LineType ;		/* dsx1LineType */
	uint32		LineCoding ;		/* dsx1LineCoding */
	uint32		SendCode ;		/* dsx1SendCode */
	uint32		LoopBackConfig ;	/* dsx1LoopbackConfig */
	uint32		LineStatus ;		/* dsx1LineStatus */
	uint32		SignalMode ;		/* dsx1SignalMode */
	uint32		TxClockSource ;		/* dsx1TransmitClockSource */
	uint32		Fdl ;			/* dsx1Fdl */
	
	RfcInterval_t	Current ;		/* Dsx1CurrentEntry */
	RfcInterval_t	Interval[96] ;		/* Dsx1IntervalEntry */
	RfcInterval_t	Total ;			/* Dsx1TotalEntry */
} RFC1406_t ;


#define	FDL_MSG_MAX_SIZE	256		/* Max size of fdl msg trace */

typedef	struct {
	uint32		Type ;
	uint32		Length ;
	uint8		Payload[FDL_MSG_MAX_SIZE] ;
} FDLMessage_t ;

/*
 * Possible values for MessageType.
 */
#define	FDL_HDLC_MSG	0x01
#define	FDL_BIT_MSG	0x02

typedef struct {
	ApiHeader_t	Header ;
	uint32		Event ;
	union {
		uint32		State ;
		uint32		Signalling ;
		uint32		Errors ;
		FDLMessage_t	Message ;
	} Syndrome ;
} EventResponse_t ;

/*
 * Event uses the same symbolic constants as IfaceState.EventMask...
 */

typedef struct {
	ApiHeader_t	Header ;
	uint32		Register ;
	uint32		Value ;
	uint32		Mode ;
} RWRegister_t ;

/*
 * Macros and symbolic constants for Mode..
 */
#define	ACCESS_SIZE(x)	((x) & 0x3)
#define	ACCESS_8BIT	0x0
#define	ACCESS_16BIT	0x1
#define	ACCESS_32BIT	0x2

typedef	struct {
	ApiHeader_t	Header ;
	uint32		Data[1]	;
} HWSpecific_t ;


typedef union {
	Config_t		Config ;
	IfaceConfig_t		IfaceConfig ;
	IfaceMode_t		IfaceMode ;
	IfaceState_t		IfaceState ;
	DIConfig_t		DIConfig ;
	RFC1406_t		RFC1406 ;
	EventResponse_t		EventResponse ;
	RWRegister_t		RWRegister ;
	HWSpecific_t		HWSpecific ;
} T1Command_t ;

