xref: /netbsd/sys/sys/tty.h (revision bf9ec67e)
1 /*	$NetBSD: tty.h,v 1.52 2001/06/08 15:43:34 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the University of
23  *	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *	@(#)tty.h	8.7 (Berkeley) 1/9/95
41  */
42 
43 #ifndef _SYS_TTY_H_
44 #define _SYS_TTY_H_
45 
46 #include <sys/termios.h>
47 #include <sys/select.h>		/* For struct selinfo. */
48 #include <sys/queue.h>
49 #include <sys/callout.h>
50 
51 /*
52  * Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
53  * exactly the same behaviour as in true clists.
54  * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
55  * (but, saves memory and cpu time)
56  *
57  * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
58  */
59 struct clist {
60 	int	c_cc;		/* count of characters in queue */
61 	int	c_cn;		/* total ring buffer length */
62 	u_char	*c_cf;		/* points to first character */
63 	u_char	*c_cl;		/* points to next open character */
64 	u_char	*c_cs;		/* start of ring buffer */
65 	u_char	*c_ce;		/* c_ce + c_len */
66 	u_char	*c_cq;		/* N bits/bytes long, see tty_subr.c */
67 };
68 
69 /*
70  * Per-tty structure.
71  *
72  * Should be split in two, into device and tty drivers.
73  * Glue could be masks of what to echo and circular buffer
74  * (low, high, timeout).
75  */
76 struct tty {
77 	TAILQ_ENTRY(tty) tty_link;	/* Link in global tty list. */
78 	struct	clist t_rawq;		/* Device raw input queue. */
79 	long	t_rawcc;		/* Raw input queue statistics. */
80 	struct	clist t_canq;		/* Device canonical queue. */
81 	long	t_cancc;		/* Canonical queue statistics. */
82 	struct	clist t_outq;		/* Device output queue. */
83 	struct	callout t_outq_ch;	/* for ttycheckoutq() */
84 	struct	callout t_rstrt_ch;	/* for delayed output start */
85 	long	t_outcc;		/* Output queue statistics. */
86 	struct	linesw *t_linesw;	/* Interface to device drivers. */
87 	dev_t	t_dev;			/* Device. */
88 	int	t_state;		/* Device and driver (TS*) state. */
89 	int	t_wopen;		/* Processes waiting for open. */
90 	int	t_flags;		/* Tty flags. */
91 	struct	pgrp *t_pgrp;		/* Foreground process group. */
92 	struct	session *t_session;	/* Enclosing session. */
93 	struct	selinfo t_rsel;		/* Tty read/oob select. */
94 	struct	selinfo t_wsel;		/* Tty write select. */
95 	struct	termios t_termios;	/* Termios state. */
96 	struct	winsize t_winsize;	/* Window size. */
97 					/* Start output. */
98 	void	(*t_oproc) __P((struct tty *));
99 					/* Set hardware state. */
100 	int	(*t_param) __P((struct tty *, struct termios *));
101 					/* Set hardware flow control. */
102 	int	(*t_hwiflow) __P((struct tty *tp, int flag));
103 	void	*t_sc;			/* XXX: net/if_sl.c:sl_softc. */
104 	short	t_column;		/* Tty output column. */
105 	short	t_rocount, t_rocol;	/* Tty. */
106 	short	t_hiwat;		/* High water mark. */
107 	short	t_lowat;		/* Low water mark. */
108 	short	t_gen;			/* Generation number. */
109 };
110 
111 #define	t_cc		t_termios.c_cc
112 #define	t_cflag		t_termios.c_cflag
113 #define	t_iflag		t_termios.c_iflag
114 #define	t_ispeed	t_termios.c_ispeed
115 #define	t_lflag		t_termios.c_lflag
116 #define	t_oflag		t_termios.c_oflag
117 #define	t_ospeed	t_termios.c_ospeed
118 
119 #define	TTIPRI	25			/* Sleep priority for tty reads. */
120 #define	TTOPRI	26			/* Sleep priority for tty writes. */
121 
122 #define	TTMASK	15
123 #define	OBUFSIZ	100
124 #define	TTYHOG	1024
125 
126 #ifdef _KERNEL
127 #define	TTMAXHIWAT	roundup(2048, CBSIZE)
128 #define	TTMINHIWAT	roundup(100, CBSIZE)
129 #define	TTMAXLOWAT	256
130 #define	TTMINLOWAT	32
131 #endif /* _KERNEL */
132 
133 /* These flags are kept in t_state. */
134 #define	TS_ASLEEP	0x00001		/* Process waiting for tty. */
135 #define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
136 #define	TS_BUSY		0x00004		/* Draining output. */
137 #define	TS_CARR_ON	0x00008		/* Carrier is present. */
138 #define	TS_DIALOUT	0x00010		/* Tty used for dialout. */
139 #define	TS_FLUSH	0x00020		/* Outq has been flushed during DMA. */
140 #define	TS_ISOPEN	0x00040		/* Open has completed. */
141 #define	TS_TBLOCK	0x00080		/* Further input blocked. */
142 #define	TS_TIMEOUT	0x00100		/* Wait for output char processing. */
143 #define	TS_TTSTOP	0x00200		/* Output paused. */
144 #define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
145 
146 /* State for intra-line fancy editing work. */
147 #define	TS_BKSL		0x00800		/* State for lowercase \ work. */
148 #define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
149 #define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
150 #define	TS_LNCH		0x04000		/* Next character is literal. */
151 #define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
152 #define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
153 
154 /* Character type information. */
155 #define	ORDINARY	0
156 #define	CONTROL		1
157 #define	BACKSPACE	2
158 #define	NEWLINE		3
159 #define	TAB		4
160 #define	VTAB		5
161 #define	RETURN		6
162 
163 struct speedtab {
164 	int sp_speed;			/* Speed. */
165 	int sp_code;			/* Code. */
166 };
167 
168 /* Modem control commands (driver). */
169 #define	DMSET		0
170 #define	DMBIS		1
171 #define	DMBIC		2
172 #define	DMGET		3
173 
174 /* Flags on a character passed to ttyinput. */
175 #define	TTY_CHARMASK	0x000000ff	/* Character mask */
176 #define	TTY_QUOTE	0x00000100	/* Character quoted */
177 #define	TTY_ERRORMASK	0xff000000	/* Error mask */
178 #define	TTY_FE		0x01000000	/* Framing error or BREAK condition */
179 #define	TTY_PE		0x02000000	/* Parity error */
180 
181 /* Is tp controlling terminal for p? */
182 #define	isctty(p, tp)							\
183 	((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
184 
185 /* Is p in background of tp? */
186 #define	isbackground(p, tp)						\
187 	(isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
188 
189 /*
190  * ttylist_head is defined here so that user-land has access to it.
191  */
192 TAILQ_HEAD(ttylist_head, tty);		/* the ttylist is a TAILQ */
193 
194 #ifdef _KERNEL
195 
196 extern	int tty_count;			/* number of ttys in global ttylist */
197 extern	struct ttychars ttydefaults;
198 
199 /* Symbolic sleep message strings. */
200 extern	 const char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[], ttybuf[];
201 
202 int	 b_to_q __P((const u_char *cp, int cc, struct clist *q));
203 void	 catq __P((struct clist *from, struct clist *to));
204 void	 clist_init __P((void));
205 int	 getc __P((struct clist *q));
206 void	 ndflush __P((struct clist *q, int cc));
207 int	 ndqb __P((struct clist *q, int flag));
208 u_char	*nextc __P((struct clist *q, u_char *cp, int *c));
209 int	 putc __P((int c, struct clist *q));
210 int	 q_to_b __P((struct clist *q, u_char *cp, int cc));
211 int	 unputc __P((struct clist *q));
212 
213 int	 nullmodem __P((struct tty *tp, int flag));
214 int	 tputchar __P((int c, struct tty *tp));
215 int	 ttioctl __P((struct tty *tp, u_long com, caddr_t data, int flag,
216 	    struct proc *p));
217 int	 ttread __P((struct tty *tp, struct uio *uio, int flag));
218 void	 ttrstrt __P((void *tp));
219 int	 ttpoll __P((struct tty *tp, int events, struct proc *p));
220 void	 ttsetwater __P((struct tty *tp));
221 int	 ttspeedtab __P((int speed, struct speedtab *table));
222 int	 ttstart __P((struct tty *tp));
223 void	 ttwakeup __P((struct tty *tp));
224 int	 ttwrite __P((struct tty *tp, struct uio *uio, int flag));
225 void	 ttychars __P((struct tty *tp));
226 int	 ttycheckoutq __P((struct tty *tp, int wait));
227 int	 ttyclose __P((struct tty *tp));
228 void	 ttyflush __P((struct tty *tp, int rw));
229 void	 ttyinfo __P((struct tty *tp));
230 int	 ttyinput __P((int c, struct tty *tp));
231 int	 ttylclose __P((struct tty *tp, int flag));
232 int	 ttylopen __P((dev_t device, struct tty *tp));
233 int	 ttymodem __P((struct tty *tp, int flag));
234 int	 ttyopen __P((struct tty *tp, int dialout, int nonblock));
235 int	 ttyoutput __P((int c, struct tty *tp));
236 void	 ttypend __P((struct tty *tp));
237 void	 ttyretype __P((struct tty *tp));
238 void	 ttyrub __P((int c, struct tty *tp));
239 int	 ttysleep __P((struct tty *tp,
240 	    void *chan, int pri, const char *wmesg, int timeout));
241 int	 ttywait __P((struct tty *tp));
242 int	 ttywflush __P((struct tty *tp));
243 
244 void	 tty_init __P((void));
245 void	 tty_attach __P((struct tty *));
246 void	 tty_detach __P((struct tty *));
247 struct tty
248 	*ttymalloc __P((void));
249 void	 ttyfree __P((struct tty *));
250 u_char	*firstc __P((struct clist *clp, int *c));
251 
252 int	clalloc __P((struct clist *, int, int));
253 void	clfree __P((struct clist *));
254 
255 #if defined(_KERNEL_OPT)
256 #include "opt_compat_freebsd.h"
257 #include "opt_compat_sunos.h"
258 #include "opt_compat_svr4.h"
259 #include "opt_compat_43.h"
260 #include "opt_compat_osf1.h"
261 #endif
262 
263 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4) || \
264     defined(COMPAT_FREEBSD) || defined(COMPAT_OSF1) || defined(LKM)
265 # define COMPAT_OLDTTY
266 int 	ttcompat __P((struct tty *, u_long, caddr_t, int, struct proc *));
267 #endif
268 
269 #endif /* _KERNEL */
270 
271 #endif /* !_SYS_TTY_H_ */
272