/*
 * $Header: /smsa/bsd:include/sys/tty.h:bsd.riscix1_2  1.19  $
 * $Source: /smsa/bsd:include/sys/tty.h: $
 *
 * Copyright (c) 1988 Acorn Computers Ltd., Cambridge, England
 *
 */
/* @(#)tty.h	1.2 87/05/15 3.2/4.3NFSSRC */
/*
 * Copyright (c) 1982, 1986 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 *
 *	@(#)tty.h	7.1 (Berkeley) 6/4/86
 */

#ifndef	__sys_tty_h
#define	__sys_tty_h

#ifndef	__sys_types_h
#include "types.h"
#endif
#ifndef	__sys_ioctl_h
#include "ioctl.h"
#endif
#ifndef	__sys_ttychars_h
#include "ttychars.h"
#endif
#ifndef	__sys_ttydev_h
#include "ttydev.h"
#endif
#ifndef __termios_h
#include "../termios.h"
#endif

/*
 * A clist structure is the head of a linked list queue
 * of characters.  The characters are stored in blocks
 * containing a link and CBSIZE (param.h) characters. 
 * The routines in tty_subr.c manipulate these structures.
 */
struct clist {
	int	c_cc;		/* character count */
	char	*c_cf;		/* pointer to first char */
	char	*c_cl;		/* pointer to last char */
};

/*
 * Per-tty structure.
 *
 * Should be split in two, into device and tty drivers.
 * Glue could be masks of what to echo and circular buffer
 * (low, high, timeout).
 */
struct tty {
	union {
		struct {
			struct	clist T_rawq;
			struct	clist T_canq;
		} t_t;
#define	t_rawq	t_nu.t_t.T_rawq		/* raw characters or partial line */
#define	t_canq	t_nu.t_t.T_canq		/* raw characters or partial line */
		struct {
			caddr_t	T_bufp;
			char	*T_cp;
			int	T_inbuf;
			int	T_rec;
		} t_n;
#define	t_bufp	t_nu.t_n.T_bufp		/* buffer allocated to protocol */
#define	t_cp	t_nu.t_n.T_cp		/* pointer into the ripped off buffer */
#define	t_inbuf	t_nu.t_n.T_inbuf	/* number chars in the buffer */
#define	t_rec	t_nu.t_n.T_rec		/* have a complete record */
	} t_nu;
	struct	clist t_outq;		/* device */
	int	(*t_oproc)();		/* device */
	struct	proc *t_rsel;		/* tty */
	struct	proc *t_wsel;
	int	t_flags;		/* some of both */
	int	t_state;		/* some of both */
	union
	{
	    caddr_t	u_linep;	/* used by some line disciplines */
	    int		u_canesc;	/* tty: count of escapes on canq */
	} t__lu;
#define t_canesc	t__lu.u_canesc
#define t_linep		t__lu.u_linep
	pid_t	t_pgrp;			/* tty */
	pid_t	t_sessldr;		/* pid of session leader (POSIX) */
	dev_t	t_dev;			/* device */
	u_char	t_col;			/* tty */
	u_char	t_rocount, t_rocol;	/* tty */
	u_char	t_rawesc;		/* tty: count of escapes on rawq */
	u_char	t_rpendc;		/* holds last of 2/3 byte PARMRK seq */
#define TTQSZ(tp,x)  ((tp)->t_/**/x/**/q.c_cc - (tp)->t_/**/x/**/esc)
	struct termios	t_termios;	/* POSIX compliance support */
#define t_ispeed	t_termios.c_ispeed
#define t_ospeed	t_termios.c_ospeed
#define t_iflag		t_termios.c_iflag
#define t_oflag		t_termios.c_oflag
#define t_cflag		t_termios.c_cflag
#define t_lflag		t_termios.c_lflag
#define t_line		t_termios.c_line
#define	t_erase		t_termios.c_cc[VERASE]
#define	t_kill		t_termios.c_cc[VKILL]
#define	t_intrc		t_termios.c_cc[VINTR]
#define	t_quitc		t_termios.c_cc[VQUIT]
#define	t_startc	t_termios.c_cc[VSTART]
#define	t_stopc		t_termios.c_cc[VSTOP]
#define	t_eofc		t_termios.c_cc[VEOF]
#define	t_brkc		t_termios.c_cc[VEOL]
#define	t_suspc		t_termios.c_cc[VSUSP]
#define	t_dsuspc	t_termios.c_cc[VDSUSP]
#define	t_rprntc	t_termios.c_cc[VREPRINT]
#define	t_flushc	t_termios.c_cc[VDISCARD]
#define	t_werasc	t_termios.c_cc[VWERASE]
#define	t_lnextc	t_termios.c_cc[VLNEXT]
#define t_rdmin		t_termios.c_cc[VMIN]
#define t_rdtime	t_termios.c_cc[VTIME]
	struct winsize t_winsize;	/* window size */
};

#define	TTIPRI	28
#define	TTOPRI	29

/* limits */
#define	NSPEEDS	16
#define	TTMASK	15
#define	OBUFSIZ	100
#define	TTYHOG	255

#ifdef _KERNEL
extern short	tthiwat[NSPEEDS], ttlowat[NSPEEDS];
#define	TTHIWAT(tp)	tthiwat[(tp)->t_ospeed&TTMASK]
#define	TTLOWAT(tp)	ttlowat[(tp)->t_ospeed&TTMASK]
#endif

/* internal state bits */
#define	TS_TIMEOUT	0x00000001	/* delay timeout in progress */
#define	TS_WOPEN	0x00000002	/* waiting for open to complete */
#define	TS_ISOPEN	0x00000004	/* device is open */
#define	TS_FLUSH	0x00000008	/* outq has been flushed during DMA */
#define	TS_CARR_ON	0x00000010	/* software copy of carrier-present */
#define	TS_BUSY		0x00000020	/* output in progress */
#define	TS_ASLEEP	0x00000040	/* wakeup when output done */
#define	TS_XCLUDE	0x00000080	/* exclusive-use flag against open */
#define	TS_TTSTOP	0x00000100	/* output stopped by ctl-s */
#define	TS_HUPCLS	0x00000200	/* hang up upon last close */
#define	TS_TBLOCK	0x00000400	/* tandem queue blocked */
#define	TS_RCOLL	0x00000800	/* collision in read select */
#define	TS_WCOLL	0x00001000	/* collision in write select */
#define	TS_NBIO		0x00002000	/* tty in non-blocking mode */
#define	TS_ASYNC	0x00004000	/* tty in async i/o mode */

/* state for intra-line fancy editing work */
#define	TS_BKSL		0x00010000	/* state for lowercase \ work */
#define	TS_QUOT		0x00020000	/* last character input was \ */
#define	TS_ERASE	0x00040000	/* within a \.../ for PRTRUB */
#define	TS_LNCH		0x00080000	/* next character is literal */
#define	TS_TYPEN	0x00100000	/* retyping suspended input (PENDIN) */
#define	TS_CNTTB	0x00200000	/* counting tab width; leave FLUSHO alone */

#define	TS_LOCAL	(TS_BKSL|TS_QUOT|TS_ERASE|TS_LNCH|TS_TYPEN|TS_CNTTB)

/*
 * State for implementation of POSIX non-canonical read timeout: names
 * straight from Sys V code.
 */
#define TS_RTO		0x00400000
#define TS_TACT		0x00800000
#define TS_RTODONE	0x01000000

/* Extension to cope with POSIX 2/3-byte sequence for PARMRK */
#define	TS_RPEND1	0x02000000		/* 1 byte (t_rpendc) pending */
#define TS_RPEND2	0x04000000		/* NUL then 1 byte pending., */

/*
 * Extension for POSIX input: driver passes character status (CS) code
 * orred in with character to input routine.
 */

#define CS_OK		(0 << 8)	/* character is OK */
#define CS_FRMERR	(1 << 8)	/* framing error on line */
#define CS_PARERR	(2 << 8)	/* hardware detected parity error */
#define CS_QUOTE	(3 << 8)	/* don't interpret char */
#define CS__MASK	(3 << 8)	/* mask for above values */

#ifdef _KERNEL
/* Functions in tty_subr.c */
extern int getc (struct clist *queue);
extern int f_getc (struct clist *queue);
extern char *nextc (struct clist *p, char *cp);
extern int putc (int ch, struct clist *queue);
extern int f_putc (int ch, struct clist *queue);
extern int unputc (struct clist *queue);
extern void ndflush (struct clist *q, int cc);
extern int ndqt (struct clist *q, int term);
extern int b_to_q (char *buff, int count, struct clist *queue);
extern int q_to_b (struct clist *queue, char *buff, int count);
extern int atomic_btoq (char *buf, int count, struct clist *queue);
extern void catq (struct clist *from, struct clist *to);
#endif

#endif/*__sys_tty_h*/

/* EOF tty.h */
