/*---------------------------------------------------------------*/
/* FILE NAME	  :  hdlc.h
**
** VERSION	  :  1.00
**
** DESCRIPTION	  :  This file contains all the define, structures
**		     and functional prototypes required for HDLC
**		     related communications.
**
** Modified for the Linux driver by Gary Jennejohn, Copyright (c) 2002, 2004
**/
/*---------------------------------------------------------------*/
#ifndef HDLC_H
#define HDLC_H

#define DEFT_ADDR	0x00	/* Default Destination Address - also broadcast */
#define MIN_HADDR	0x01	/* HADDR starts at 0x01 */
#define MAX_HADDR	0x1f	/* and ends at 0x1f */
#define HDLC_HDR	3	/* the no. of bytes in the HDLC header */
#define HDLC_CRC	2	/* The no. of CRC bytes */
#define RXBUFLEN	1500	/* Max RX Buffer Length		*/
#define TXBUFLEN	1500	/* Max TX Buffer Length		*/
#define RX_BUF_COUNT	10	/* Number of receive buffers to use */

/*
 * protocol definitions used by IDS. The driver ignores all packets
 * which are not one of these types
 */
#define IDS_IP		0x70
#define IDS_ARP		0x71
/* other definitions from IDS */
#define IDS_ARPTYPE	0x0b /* replaces ar_hrd */
#define IDS_ARPHWALEN	0x01 /* replaces ar_hln */
#define IDS_ARPPROTO	0x7070 /* replaces ar_pro */
/* some useful macros */
#define IDS_IP_1		(IDS_IP << 1)
#define IDS_IP_2		(0x01 + (IDS_IP << 1))
#define IDS_ARP_1		(IDS_ARP << 1)
#define IDS_ARP_2		(0x01 + (IDS_ARP << 1))
#define SETTARGADDR(ptr, hadr)	(ptr)->target = MKIDSADDR(hadr)
#define SETSRCADDR(ptr, hadr)	(ptr)->source = MKIDSADDR(hadr)
#define SETEND(ptr)		(ptr)->end = 0x03;
#define MKIDSADDR(hadr)		(0x80 + (hadr << 2))
/* reverse of the above */
#define UNMKIDSADDR(hadr)	((hadr - 0x80) >> 2)
/* the driver may get packets which aren't intended for it (how?) */
#define CHKFORME(ptr, myhaddr)	((MKIDSADDR(myhaddr) == (ptr)->target) || \
				 (0x80 == (ptr)->target))

#ifdef __KERNEL__
/*
 * header used by IDS on the HDLC bus - this replaces the ethernet frame
 * header (MAC1, MAC2, type - 14 bytes), which means that the driver must
 * reconstruct those bytes before passing the packet up the stack!
 * this is no big deal since it usually just means putting zeroes in.
 */
struct ids_frame_hdr {
	/*
	 * NOTE target corresponds to HADDR in the HDLC header, which is
	 * alwyas 16 bits.
	 */
	unsigned char target;	/* 0x80 + target << 2	*/
	unsigned char source;	/* 0x80 + source << 2	*/
	unsigned char proto_1;	/* 0x00 + protocol << 1 */
	unsigned char proto_2;	/* 0x01 + protocol << 1 */
	unsigned char end;	/* always 0x03		*/
} __attribute__((packed));
#define IDSHDR_OFF	9	/* IDS header starts at byte 10 in ethhdr */

/* for reference only
struct arphdr
{
	unsigned short	ar_hrd;
	unsigned short	ar_pro;
	unsigned char	ar_hln;
	unsigned char	ar_pln;
	unsigned short	ar_op;
};
*/

struct ids_arp_hdr {
	struct arphdr oah;		/* the original arphdr		*/
	/* this stuff is not defined in arphdr */
	unsigned char ar_sha;		/* sender hardware address	*/
	unsigned char ar_sip[4];	/* sender IP address		*/
	unsigned char ar_tha;		/* target hardware address	*/
	unsigned char ar_tip[4];	/* target IP address		*/
} __attribute__((packed));

/* helper */
struct eth_arp_hdr {
	struct arphdr oah;		/* the original arphdr */
	/*
	 *	 Ethernet looks like this, which is what we assume
	 */
	unsigned char ar_sha[ETH_ALEN]; /* sender hardware address	*/
	unsigned char ar_sip[4];	/* sender IP address		*/
	unsigned char ar_tha[ETH_ALEN]; /* target hardware address	*/
	unsigned char ar_tip[4];	/* target IP address		*/
} __attribute__((packed));

/* for reference only
typedef struct scc_param {
	ushort	scc_rbase;
	ushort	scc_tbase;
	u_char	scc_rfcr;
	u_char	scc_tfcr;
	ushort	scc_mrblr;
	uint	scc_rstate;
	uint	scc_idp;
	ushort	scc_rbptr;
	ushort	scc_ibc;
	uint	scc_rxtmp;
	uint	scc_tstate;
	uint	scc_tdp;
	ushort	scc_tbptr;
	ushort	scc_tbc;
	uint	scc_txtmp;
	uint	scc_rcrc;
	uint	scc_tcrc;
} sccp_t;
*/

/* SCC as HDLC controller */
typedef struct scc_hdlc {
	sccp_t shd_genscc;
	/* not used, just filler */
	uint res1;
	uint scc_c_mask;
	uint scc_c_pres;
	ushort scc_disfc;
	ushort scc_crcec;
	ushort scc_abtsc;
	ushort scc_nmarc;
	ushort scc_retrc;
	ushort scc_mflr;
	/* not used, just filler */
	ushort scc_maxcnt;
	ushort scc_rfthr;
	/* not used, just filler */
	ushort scc_rfcnt;
	ushort scc_hmask;
	ushort scc_haddr1;
	ushort scc_haddr2;
	ushort scc_haddr3;
	ushort scc_haddr4;
	ushort scc_tmp;
	ushort scc_tmp_mb;
}  __attribute__((packed)) scc_hdlc_t;
#endif /* __KERNEL__ */

#endif /* HDLC_H */
