xref: /openbsd/sys/sys/tty.h (revision 31d6c6f2)
1 /*	$OpenBSD: tty.h,v 1.44 2024/10/26 00:56:35 jsg Exp $	*/
2 /*	$NetBSD: tty.h,v 1.30.4.1 1996/06/02 09:08:13 mrg Exp $	*/
3 
4 /*-
5  * Copyright (c) 1982, 1986, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  * (c) UNIX System Laboratories, Inc.
8  * All or some portions of this file are derived from material licensed
9  * to the University of California by American Telephone and Telegraph
10  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11  * the permission of UNIX System Laboratories, Inc.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)tty.h	8.6 (Berkeley) 1/21/94
38  */
39 
40 #include <sys/termios.h>
41 #include <sys/queue.h>
42 #include <sys/selinfo.h>		/* For struct selinfo. */
43 #include <sys/timeout.h>
44 
45 #define KERN_TTY_TKNIN		1	/* quad: input chars */
46 #define KERN_TTY_TKNOUT		2	/* quad: output chars */
47 #define KERN_TTY_TKRAWCC	3	/* quad: input chars, raw mode */
48 #define KERN_TTY_TKCANCC	4	/* quad: input char, cooked mode */
49 #define KERN_TTY_INFO		5	/* struct: tty stats */
50 /* was KERN_TTY_MAXPTYS		6 */
51 /* was KERN_TTY_NPTYS		7 */
52 #define KERN_TTY_MAXID		8
53 
54 #define CTL_KERN_TTY_NAMES { \
55 	{ 0, 0 }, \
56 	{ "tk_nin", CTLTYPE_QUAD }, \
57 	{ "tk_nout", CTLTYPE_QUAD }, \
58 	{ "tk_rawcc", CTLTYPE_QUAD }, \
59 	{ "tk_cancc", CTLTYPE_QUAD }, \
60 	{ "ttyinfo", CTLTYPE_STRUCT }, \
61 	{ "gap", 0 }, \
62 	{ "gap", 0 }, \
63 }
64 
65 /* ptmget, for /dev/ptm pty getting ioctl PTMGET */
66 
67 struct ptmget {
68 	int	cfd;
69 	int	sfd;
70 	char	cn[16];
71 	char	sn[16];
72 };
73 #define PTMGET _IOR('t', 1, struct ptmget) /* get ptys */
74 #define PATH_PTMDEV	"/dev/ptm"
75 #define TTY_GID		4	/* XXX evil hardcoding of tty gid */
76 
77 /*
78  * Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
79  * exactly the same behaviour as in true clists.
80  * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
81  * (but, saves memory and cpu time)
82  *
83  * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
84  */
85 struct clist {
86 	int	c_cc;		/* count of characters in queue */
87 	int	c_cn;		/* total ring buffer length */
88 	u_char	*c_cf;		/* points to first character */
89 	u_char	*c_cl;		/* points to next open character */
90 	u_char	*c_cs;		/* start of ring buffer */
91 	u_char	*c_ce;		/* c_ce + c_len */
92 	u_char	*c_cq;		/* N bits/bytes long, see tty_subr.c */
93 };
94 
95 /*
96  * Per-tty structure.
97  *
98  * Should be split in two, into device and tty drivers.
99  * Glue could be masks of what to echo and circular buffer
100  * (low, high, timeout).
101  */
102 struct tty {
103 	TAILQ_ENTRY(tty) tty_link;	/* Link in global tty list. */
104 	struct	clist t_rawq;		/* Device raw input queue. */
105 	long	t_rawcc;		/* Raw input queue statistics. */
106 	struct	clist t_canq;		/* Device canonical queue. */
107 	long	t_cancc;		/* Canonical queue statistics. */
108 	struct	clist t_outq;		/* Device output queue. */
109 	long	t_outcc;		/* Output queue statistics. */
110 	int	t_qlen;			/* Length of above queues */
111 	u_char	t_line;			/* Interface to device drivers. */
112 	dev_t	t_dev;			/* Device. */
113 	int	t_state;		/* Device and driver (TS*) state. */
114 	int	t_flags;		/* Tty flags. */
115 	struct	pgrp *t_pgrp;		/* Foreground process group. */
116 	struct	session *t_session;	/* Enclosing session. */
117 	struct	selinfo t_rsel;		/* Tty read/oob select. */
118 	struct	selinfo t_wsel;		/* Tty write select. */
119 	struct	termios t_termios;	/* Termios state. */
120 	struct	winsize t_winsize;	/* Window size. */
121 					/* Start output. */
122 	void	(*t_oproc)(struct tty *);
123 					/* Set hardware state. */
124 	int	(*t_param)(struct tty *, struct termios *);
125 					/* Set hardware flow control. */
126 	int	(*t_hwiflow)(struct tty *tp, int flag);
127 	void	*t_sc;			/* XXX: net/if_sl.c:sl_softc. */
128 	short	t_column;		/* Tty output column. */
129 	short	t_rocount, t_rocol;	/* Tty. */
130 	short	t_hiwat;		/* High water mark. */
131 	short	t_lowat;		/* Low water mark. */
132 	short	t_gen;			/* Generation number. */
133 	struct timeout t_rstrt_to;	/* restart timeout */
134 	struct timeval t_tv;		/* timestamp */
135 };
136 
137 /*
138  * Small version of struct tty exported via sysctl KERN_TTY_INFO
139  */
140 struct itty {
141 	dev_t   t_dev;
142 	int t_rawq_c_cc;
143 	int t_canq_c_cc;
144 	int t_outq_c_cc;
145 	short t_hiwat;
146 	short t_lowat;
147 	short t_column;
148 	int t_state;
149 	struct session *t_session;
150 	pid_t t_pgrp_pg_id;
151 	u_char t_line;
152 };
153 
154 #define	t_cc		t_termios.c_cc
155 #define	t_cflag		t_termios.c_cflag
156 #define	t_iflag		t_termios.c_iflag
157 #define	t_ispeed	t_termios.c_ispeed
158 #define	t_lflag		t_termios.c_lflag
159 #define	t_min		t_termios.c_min
160 #define	t_oflag		t_termios.c_oflag
161 #define	t_ospeed	t_termios.c_ospeed
162 #define	t_time		t_termios.c_time
163 
164 #define	TTIPRI	25			/* Sleep priority for tty reads. */
165 #define	TTOPRI	26			/* Sleep priority for tty writes. */
166 
167 #define	OBUFSIZ	512
168 #define	TTYHOG(tp)	(tp)->t_qlen
169 
170 #ifdef _KERNEL
171 #define	TTMAXLOWAT	256
172 #define	TTMINLOWAT	32
173 #define	TTMINHIWAT	100
174 #define	TTHIWATMINSPACE	200		/* Min space above hiwat */
175 #endif
176 
177 /* These flags are kept in t_state. */
178 #define	TS_ASLEEP	0x00001		/* Process waiting for tty. */
179 #define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
180 #define	TS_BUSY		0x00004		/* Draining output. */
181 #define	TS_CARR_ON	0x00008		/* Carrier is present. */
182 #define	TS_FLUSH	0x00010		/* Outq has been flushed during DMA. */
183 #define	TS_ISOPEN	0x00020		/* Open has completed. */
184 #define	TS_TBLOCK	0x00040		/* Further input blocked. */
185 #define	TS_TIMEOUT	0x00080		/* Wait for output char processing. */
186 #define	TS_TTSTOP	0x00100		/* Output paused. */
187 #define	TS_WOPEN	0x00200		/* Open in progress. */
188 #define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
189 
190 /* State for intra-line fancy editing work. */
191 #define	TS_BKSL		0x00800		/* State for lowercase \ work. */
192 #define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
193 #define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
194 #define	TS_LNCH		0x04000		/* Next character is literal. */
195 #define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
196 #define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
197 
198 #define TS_TSTAMPDCDSET	0x10000		/* update timestamp on DCD set */
199 #define TS_TSTAMPDCDCLR	0x20000		/* update timestamp on DCD clr */
200 #define TS_TSTAMPCTSSET	0x40000		/* update timestamp on CTS set */
201 #define TS_TSTAMPCTSCLR	0x80000		/* update timestamp on CTS clr */
202 
203 /* Character type information. */
204 #define	ORDINARY	0
205 #define	CONTROL		1
206 #define	BACKSPACE	2
207 #define	NEWLINE		3
208 #define	TAB		4
209 #define	VTAB		5
210 #define	RETURN		6
211 
212 struct speedtab {
213 	int sp_speed;			/* Speed. */
214 	int sp_code;			/* Code. */
215 };
216 
217 /* Modem control commands (driver). */
218 #define	DMSET		0
219 #define	DMBIS		1
220 #define	DMBIC		2
221 #define	DMGET		3
222 
223 /* Flags on a character passed to ttyinput. */
224 #define	TTY_CHARMASK	0x000000ff	/* Character mask */
225 #define	TTY_QUOTE	0x00000100	/* Character quoted */
226 #define	TTY_ERRORMASK	0xff000000	/* Error mask */
227 #define	TTY_FE		0x01000000	/* Framing error or BREAK condition */
228 #define	TTY_PE		0x02000000	/* Parity error */
229 
230 /* Is tp controlling terminal for pr? */
231 #define	isctty(pr, tp)							\
232 	((pr)->ps_session == (tp)->t_session && (pr)->ps_flags & PS_CONTROLT)
233 
234 /* Is pr in background of tp? */
235 #define	isbackground(pr, tp)						\
236 	(isctty((pr), (tp)) && (pr)->ps_pgrp != (tp)->t_pgrp)
237 
238 /*
239  * ttylist_head is defined here so that user-land has access to it.
240  */
241 TAILQ_HEAD(ttylist_head, tty);		/* the ttylist is a TAILQ */
242 
243 #ifdef _KERNEL
244 
245 extern	int tty_count;			/* number of ttys in global ttylist */
246 
247 /* Symbolic sleep message strings. */
248 extern	 char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[];
249 
250 extern int64_t tk_cancc, tk_nin, tk_nout, tk_rawcc;
251 
252 int	sysctl_tty(int *, u_int, void *, size_t *, void *, size_t);
253 int	sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
254 
255 int	 b_to_q(u_char *cp, int cc, struct clist *q);
256 void	 catq(struct clist *from, struct clist *to);
257 int	 getc(struct clist *q);
258 void	 ndflush(struct clist *q, int cc);
259 int	 ndqb(struct clist *q, int flag);
260 u_char	*firstc(struct clist *clp, int *c, int *cc);
261 u_char	*nextc(struct clist *q, u_char *cp, int *c, int *cc);
262 int	 putc(int c, struct clist *q);
263 int	 q_to_b(struct clist *q, u_char *cp, int cc);
264 int	 unputc(struct clist *q);
265 
266 int	 nullmodem(struct tty *tp, int flag);
267 int	 tputchar(int c, struct tty *tp);
268 int	 ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
269 	    struct proc *p);
270 int	 ttread(struct tty *tp, struct uio *uio, int flag);
271 void	 ttrstrt(void *tp);
272 int	 ttkqfilter(dev_t dev, struct knote *kn);
273 void	 ttsetwater(struct tty *tp);
274 int	 ttspeedtab(int speed, const struct speedtab *table);
275 int	 ttstart(struct tty *tp);
276 void	 ttwakeupwr(struct tty *tp);
277 void	 ttwakeup(struct tty *tp);
278 int	 ttwrite(struct tty *tp, struct uio *uio, int flag);
279 void	 ttychars(struct tty *tp);
280 int	 ttycheckoutq(struct tty *tp, int wait);
281 int	 ttyclose(struct tty *tp);
282 void	 ttyflush(struct tty *tp, int rw);
283 void	 ttyinfo(struct tty *tp);
284 int	 ttyinput(int c, struct tty *tp);
285 int	 ttylclose(struct tty *tp, int flag, struct proc *p);
286 int	 ttymodem(struct tty *tp, int flag);
287 int	 ttyopen(dev_t device, struct tty *tp, struct proc *p);
288 int	 ttyoutput(int c, struct tty *tp);
289 void	 ttypend(struct tty *tp);
290 int	 ttyretype(struct tty *tp);
291 int	 ttyrub(int c, struct tty *tp);
292 int	 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg);
293 int	 ttywait(struct tty *tp);
294 int	 ttywflush(struct tty *tp);
295 void	 ttytstamp(struct tty *tp, int octs, int ncts, int odcd, int ndcd);
296 
297 void	tty_init(void);
298 struct tty *ttymalloc(int);
299 void	 ttyfree(struct tty *);
300 
301 int	cttyopen(dev_t, int, int, struct proc *);
302 int	cttyread(dev_t, struct uio *, int);
303 int	cttywrite(dev_t, struct uio *, int);
304 int	cttyioctl(dev_t, u_long, caddr_t, int, struct proc *);
305 
306 void	clalloc(struct clist *, int, int);
307 void	clfree(struct clist *);
308 
309 int	nullioctl(struct tty *, u_long, caddr_t, int, struct proc *);
310 
311 int	pppopen(dev_t dev, struct tty *, struct proc *);
312 int	pppclose(struct tty *, int, struct proc *);
313 int	ppptioctl(struct tty *, u_long, caddr_t, int, struct proc *);
314 int	pppinput(int c, struct tty *);
315 int	pppstart(struct tty *);
316 int	pppread(struct tty *, struct uio *, int);
317 int	pppwrite(struct tty *, struct uio *, int);
318 
319 int	nmeaopen(dev_t, struct tty *, struct proc *);
320 int	nmeaclose(struct tty *, int, struct proc *);
321 int	nmeainput(int, struct tty *);
322 
323 int	mstsopen(dev_t, struct tty *, struct proc *);
324 int	mstsclose(struct tty *, int, struct proc *);
325 int	mstsinput(int, struct tty *);
326 
327 int	endrunopen(dev_t, struct tty *, struct proc *);
328 int	endrunclose(struct tty *, int, struct proc *);
329 int	endruninput(int, struct tty *);
330 
331 #endif /* _KERNEL */
332