xref: /dragonfly/sys/sys/tty.h (revision 6e285212)
1 /*-
2  * Copyright (c) 1982, 1986, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)tty.h	8.6 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/sys/tty.h,v 1.53.2.1 2001/02/26 04:23:21 jlemon Exp $
40  * $DragonFly: src/sys/sys/tty.h,v 1.2 2003/06/17 04:28:59 dillon Exp $
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 
50 /*
51  * Clists are character lists, which is a variable length linked list
52  * of cblocks, with a count of the number of characters in the list.
53  */
54 struct clist {
55 	int	c_cc;		/* Number of characters in the clist. */
56 	int	c_cbcount;	/* Number of cblocks. */
57 	int	c_cbmax;	/* Max # cblocks allowed for this clist. */
58 	int	c_cbreserved;	/* # cblocks reserved for this clist. */
59 	char	*c_cf;		/* Pointer to the first cblock. */
60 	char	*c_cl;		/* Pointer to the last cblock. */
61 };
62 
63 /*
64  * Per-tty structure.
65  *
66  * Should be split in two, into device and tty drivers.
67  * Glue could be masks of what to echo and circular buffer
68  * (low, high, timeout).
69  */
70 struct tty {
71 	struct	clist t_rawq;		/* Device raw input queue. */
72 	long	t_rawcc;		/* Raw input queue statistics. */
73 	struct	clist t_canq;		/* Device canonical queue. */
74 	long	t_cancc;		/* Canonical queue statistics. */
75 	struct	clist t_outq;		/* Device output queue. */
76 	long	t_outcc;		/* Output queue statistics. */
77 	int	t_line;			/* Interface to device drivers. */
78 	dev_t	t_dev;			/* Device. */
79 	int	t_state;		/* Device and driver (TS*) state. */
80 	int	t_flags;		/* Tty flags. */
81 	int     t_timeout;              /* Timeout for ttywait() */
82 	struct	pgrp *t_pgrp;		/* Foreground process group. */
83 	struct	session *t_session;	/* Enclosing session. */
84 	struct  sigio *t_sigio;		/* Information for async I/O. */
85 	struct	selinfo t_rsel;		/* Tty read/oob select. */
86 	struct	selinfo t_wsel;		/* Tty write select. */
87 	struct	termios t_termios;	/* Termios state. */
88 	struct	winsize t_winsize;	/* Window size. */
89 					/* Start output. */
90 	void	(*t_oproc) __P((struct tty *));
91 					/* Stop output. */
92 	void	(*t_stop) __P((struct tty *, int));
93 					/* Set hardware state. */
94 	int	(*t_param) __P((struct tty *, struct termios *));
95 	void	*t_sc;			/* XXX: net/if_sl.c:sl_softc. */
96 	int	t_column;		/* Tty output column. */
97 	int	t_rocount, t_rocol;	/* Tty. */
98 	int	t_ififosize;		/* Total size of upstream fifos. */
99 	int	t_ihiwat;		/* High water mark for input. */
100 	int	t_ilowat;		/* Low water mark for input. */
101 	speed_t	t_ispeedwat;		/* t_ispeed override for watermarks. */
102 	int	t_ohiwat;		/* High water mark for output. */
103 	int	t_olowat;		/* Low water mark for output. */
104 	speed_t	t_ospeedwat;		/* t_ospeed override for watermarks. */
105 	int	t_gen;			/* Generation number. */
106 	SLIST_ENTRY(tty) t_list;	/* Global chain of ttys for pstat(8) */
107 };
108 
109 #define	t_cc		t_termios.c_cc
110 #define	t_cflag		t_termios.c_cflag
111 #define	t_iflag		t_termios.c_iflag
112 #define	t_ispeed	t_termios.c_ispeed
113 #define	t_lflag		t_termios.c_lflag
114 #define	t_min		t_termios.c_min
115 #define	t_oflag		t_termios.c_oflag
116 #define	t_ospeed	t_termios.c_ospeed
117 #define	t_time		t_termios.c_time
118 
119 #define	TTIPRI	25			/* Sleep priority for tty reads. */
120 #define	TTOPRI	26			/* Sleep priority for tty writes. */
121 
122 /*
123  * User data unfortunately has to be copied through buffers on the way to
124  * and from clists.  The buffers are on the stack so their sizes must be
125  * fairly small.
126  */
127 #define	IBUFSIZ	384			/* Should be >= max value of MIN. */
128 #define	OBUFSIZ	100
129 
130 #ifndef TTYHOG
131 #define	TTYHOG	1024
132 #endif
133 
134 #ifdef _KERNEL
135 #define	TTMAXHIWAT	roundup(2048, CBSIZE)
136 #define	TTMINHIWAT	roundup(100, CBSIZE)
137 #define	TTMAXLOWAT	256
138 #define	TTMINLOWAT	32
139 #endif
140 
141 /* These flags are kept in t_state. */
142 #define	TS_SO_OLOWAT	0x00001		/* Wake up when output <= low water. */
143 #define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
144 #define	TS_BUSY		0x00004		/* Draining output. */
145 #define	TS_CARR_ON	0x00008		/* Carrier is present. */
146 #define	TS_FLUSH	0x00010		/* Outq has been flushed during DMA. */
147 #define	TS_ISOPEN	0x00020		/* Open has completed. */
148 #define	TS_TBLOCK	0x00040		/* Further input blocked. */
149 #define	TS_TIMEOUT	0x00080		/* Wait for output char processing. */
150 #define	TS_TTSTOP	0x00100		/* Output paused. */
151 #ifdef notyet
152 #define	TS_WOPEN	0x00200		/* Open in progress. */
153 #endif
154 #define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
155 
156 /* State for intra-line fancy editing work. */
157 #define	TS_BKSL		0x00800		/* State for lowercase \ work. */
158 #define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
159 #define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
160 #define	TS_LNCH		0x04000		/* Next character is literal. */
161 #define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
162 #define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
163 
164 /* Extras. */
165 #define	TS_CAN_BYPASS_L_RINT 0x010000	/* Device in "raw" mode. */
166 #define	TS_CONNECTED	0x020000	/* Connection open. */
167 #define	TS_SNOOP	0x040000	/* Device is being snooped on. */
168 #define	TS_SO_OCOMPLETE	0x080000	/* Wake up when output completes. */
169 #define	TS_ZOMBIE	0x100000	/* Connection lost. */
170 
171 /* Hardware flow-control-invoked bits. */
172 #define	TS_CAR_OFLOW	0x200000	/* For MDMBUF (XXX handle in driver). */
173 #ifdef notyet
174 #define	TS_CTS_OFLOW	0x400000	/* For CCTS_OFLOW. */
175 #define	TS_DSR_OFLOW	0x800000	/* For CDSR_OFLOW. */
176 #endif
177 
178 /* Character type information. */
179 #define	ORDINARY	0
180 #define	CONTROL		1
181 #define	BACKSPACE	2
182 #define	NEWLINE		3
183 #define	TAB		4
184 #define	VTAB		5
185 #define	RETURN		6
186 
187 struct speedtab {
188 	int sp_speed;			/* Speed. */
189 	int sp_code;			/* Code. */
190 };
191 
192 /* Modem control commands (driver). */
193 #define	DMSET		0
194 #define	DMBIS		1
195 #define	DMBIC		2
196 #define	DMGET		3
197 
198 /* Flags on a character passed to ttyinput. */
199 #define	TTY_CHARMASK	0x000000ff	/* Character mask */
200 #define	TTY_QUOTE	0x00000100	/* Character quoted */
201 #define	TTY_ERRORMASK	0xff000000	/* Error mask */
202 #define	TTY_FE		0x01000000	/* Framing error */
203 #define	TTY_PE		0x02000000	/* Parity error */
204 #define	TTY_OE		0x04000000	/* Overrun error */
205 #define	TTY_BI		0x08000000	/* Break condition */
206 
207 /* Is tp controlling terminal for p? */
208 #define	isctty(p, tp)							\
209 	((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
210 
211 /* Is p in background of tp? */
212 #define	isbackground(p, tp)						\
213 	(isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
214 
215 /* Unique sleep addresses. */
216 #define	TSA_CARR_ON(tp)		((void *)&(tp)->t_rawq)
217 #define	TSA_HUP_OR_INPUT(tp)	((void *)&(tp)->t_rawq.c_cf)
218 #define	TSA_OCOMPLETE(tp)	((void *)&(tp)->t_outq.c_cl)
219 #define	TSA_OLOWAT(tp)		((void *)&(tp)->t_outq)
220 #define	TSA_PTC_READ(tp)	((void *)&(tp)->t_outq.c_cf)
221 #define	TSA_PTC_WRITE(tp)	((void *)&(tp)->t_rawq.c_cl)
222 #define	TSA_PTS_READ(tp)	((void *)&(tp)->t_canq)
223 
224 #ifdef _KERNEL
225 #ifdef MALLOC_DECLARE
226 MALLOC_DECLARE(M_TTYS);
227 #endif
228 extern	struct tty *constty;	/* Temporary virtual console. */
229 
230 int	 b_to_q __P((char *cp, int cc, struct clist *q));
231 void	 catq __P((struct clist *from, struct clist *to));
232 void	 clist_alloc_cblocks __P((struct clist *q, int ccmax, int ccres));
233 void	 clist_free_cblocks __P((struct clist *q));
234 int	 getc __P((struct clist *q));
235 void	 ndflush __P((struct clist *q, int cc));
236 char	*nextc __P((struct clist *q, char *cp, int *c));
237 void	 nottystop __P((struct tty *tp, int rw));
238 int	 putc __P((int c, struct clist *q));
239 int	 q_to_b __P((struct clist *q, char *cp, int cc));
240 void	 termioschars __P((struct termios *t));
241 int	 tputchar __P((int c, struct tty *tp));
242 int	 ttcompat __P((struct tty *tp, u_long com, caddr_t data, int flag));
243 int	 ttioctl __P((struct tty *tp, u_long com, void *data, int flag));
244 int	 ttread __P((struct tty *tp, struct uio *uio, int flag));
245 void	 ttrstrt __P((void *tp));
246 int	 ttsetcompat __P((struct tty *tp, u_long *com, caddr_t data,
247 	    struct termios *term));
248 void	 ttsetwater __P((struct tty *tp));
249 int	 ttspeedtab __P((int speed, struct speedtab *table));
250 int	 ttstart __P((struct tty *tp));
251 void	 ttwakeup __P((struct tty *tp));
252 int	 ttwrite __P((struct tty *tp, struct uio *uio, int flag));
253 void	 ttwwakeup __P((struct tty *tp));
254 void	 ttyblock __P((struct tty *tp));
255 void	 ttychars __P((struct tty *tp));
256 int	 ttycheckoutq __P((struct tty *tp, int wait));
257 int	 ttyclose __P((struct tty *tp));
258 void	 ttyflush __P((struct tty *tp, int rw));
259 void	 ttyfree __P((struct tty *tp));
260 void	 ttyinfo __P((struct tty *tp));
261 int	 ttyinput __P((int c, struct tty *tp));
262 int	 ttylclose __P((struct tty *tp, int flag));
263 struct tty *ttymalloc __P((struct tty *tp));
264 int	 ttymodem __P((struct tty *tp, int flag));
265 int	 ttyopen __P((dev_t device, struct tty *tp));
266 int	 ttypoll __P((dev_t dev, int events, struct proc *p));
267 int	 ttykqfilter __P((dev_t dev, struct knote *kn));
268 int	 ttyread __P((dev_t dev, struct uio *uio, int flag));
269 void	 ttyregister __P((struct tty *tp));
270 int	 ttysleep __P((struct tty *tp, void *chan, int pri, char *wmesg,
271 	    int timeout));
272 int	 ttywait __P((struct tty *tp));
273 int	 ttywrite __P((dev_t dev, struct uio *uio, int flag));
274 int	 unputc __P((struct clist *q));
275 
276 #endif /* _KERNEL */
277 
278 #endif /* !_SYS_TTY_H_ */
279