xref: /openbsd/sys/sys/tty.h (revision 8a8d3c1c)
1 /*	$OpenBSD: tty.h,v 1.43 2024/05/12 08:21:56 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	TTMASK	15
168 #define	OBUFSIZ	512
169 #define	TTYHOG(tp)	(tp)->t_qlen
170 
171 #ifdef _KERNEL
172 #define	TTMAXLOWAT	256
173 #define	TTMINLOWAT	32
174 #define	TTMINHIWAT	100
175 #define	TTHIWATMINSPACE	200		/* Min space above hiwat */
176 #endif
177 
178 /* These flags are kept in t_state. */
179 #define	TS_ASLEEP	0x00001		/* Process waiting for tty. */
180 #define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
181 #define	TS_BUSY		0x00004		/* Draining output. */
182 #define	TS_CARR_ON	0x00008		/* Carrier is present. */
183 #define	TS_FLUSH	0x00010		/* Outq has been flushed during DMA. */
184 #define	TS_ISOPEN	0x00020		/* Open has completed. */
185 #define	TS_TBLOCK	0x00040		/* Further input blocked. */
186 #define	TS_TIMEOUT	0x00080		/* Wait for output char processing. */
187 #define	TS_TTSTOP	0x00100		/* Output paused. */
188 #define	TS_WOPEN	0x00200		/* Open in progress. */
189 #define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
190 
191 /* State for intra-line fancy editing work. */
192 #define	TS_BKSL		0x00800		/* State for lowercase \ work. */
193 #define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
194 #define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
195 #define	TS_LNCH		0x04000		/* Next character is literal. */
196 #define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
197 #define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
198 
199 #define TS_TSTAMPDCDSET	0x10000		/* update timestamp on DCD set */
200 #define TS_TSTAMPDCDCLR	0x20000		/* update timestamp on DCD clr */
201 #define TS_TSTAMPCTSSET	0x40000		/* update timestamp on CTS set */
202 #define TS_TSTAMPCTSCLR	0x80000		/* update timestamp on CTS clr */
203 
204 /* Character type information. */
205 #define	ORDINARY	0
206 #define	CONTROL		1
207 #define	BACKSPACE	2
208 #define	NEWLINE		3
209 #define	TAB		4
210 #define	VTAB		5
211 #define	RETURN		6
212 
213 struct speedtab {
214 	int sp_speed;			/* Speed. */
215 	int sp_code;			/* Code. */
216 };
217 
218 /* Modem control commands (driver). */
219 #define	DMSET		0
220 #define	DMBIS		1
221 #define	DMBIC		2
222 #define	DMGET		3
223 
224 /* Flags on a character passed to ttyinput. */
225 #define	TTY_CHARMASK	0x000000ff	/* Character mask */
226 #define	TTY_QUOTE	0x00000100	/* Character quoted */
227 #define	TTY_ERRORMASK	0xff000000	/* Error mask */
228 #define	TTY_FE		0x01000000	/* Framing error or BREAK condition */
229 #define	TTY_PE		0x02000000	/* Parity error */
230 
231 /* Is tp controlling terminal for pr? */
232 #define	isctty(pr, tp)							\
233 	((pr)->ps_session == (tp)->t_session && (pr)->ps_flags & PS_CONTROLT)
234 
235 /* Is pr in background of tp? */
236 #define	isbackground(pr, tp)						\
237 	(isctty((pr), (tp)) && (pr)->ps_pgrp != (tp)->t_pgrp)
238 
239 /*
240  * ttylist_head is defined here so that user-land has access to it.
241  */
242 TAILQ_HEAD(ttylist_head, tty);		/* the ttylist is a TAILQ */
243 
244 #ifdef _KERNEL
245 
246 extern	int tty_count;			/* number of ttys in global ttylist */
247 
248 /* Symbolic sleep message strings. */
249 extern	 char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[];
250 
251 extern int64_t tk_cancc, tk_nin, tk_nout, tk_rawcc;
252 
253 int	sysctl_tty(int *, u_int, void *, size_t *, void *, size_t);
254 int	sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
255 
256 int	 b_to_q(u_char *cp, int cc, struct clist *q);
257 void	 catq(struct clist *from, struct clist *to);
258 int	 getc(struct clist *q);
259 void	 ndflush(struct clist *q, int cc);
260 int	 ndqb(struct clist *q, int flag);
261 u_char	*firstc(struct clist *clp, int *c, int *cc);
262 u_char	*nextc(struct clist *q, u_char *cp, int *c, int *cc);
263 int	 putc(int c, struct clist *q);
264 int	 q_to_b(struct clist *q, u_char *cp, int cc);
265 int	 unputc(struct clist *q);
266 
267 int	 nullmodem(struct tty *tp, int flag);
268 int	 tputchar(int c, struct tty *tp);
269 int	 ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
270 	    struct proc *p);
271 int	 ttread(struct tty *tp, struct uio *uio, int flag);
272 void	 ttrstrt(void *tp);
273 int	 ttkqfilter(dev_t dev, struct knote *kn);
274 void	 ttsetwater(struct tty *tp);
275 int	 ttspeedtab(int speed, const struct speedtab *table);
276 int	 ttstart(struct tty *tp);
277 void	 ttwakeupwr(struct tty *tp);
278 void	 ttwakeup(struct tty *tp);
279 int	 ttwrite(struct tty *tp, struct uio *uio, int flag);
280 void	 ttychars(struct tty *tp);
281 int	 ttycheckoutq(struct tty *tp, int wait);
282 int	 ttyclose(struct tty *tp);
283 void	 ttyflush(struct tty *tp, int rw);
284 void	 ttyinfo(struct tty *tp);
285 int	 ttyinput(int c, struct tty *tp);
286 int	 ttylclose(struct tty *tp, int flag, struct proc *p);
287 int	 ttymodem(struct tty *tp, int flag);
288 int	 ttyopen(dev_t device, struct tty *tp, struct proc *p);
289 int	 ttyoutput(int c, struct tty *tp);
290 void	 ttypend(struct tty *tp);
291 int	 ttyretype(struct tty *tp);
292 int	 ttyrub(int c, struct tty *tp);
293 int	 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg);
294 int	 ttywait(struct tty *tp);
295 int	 ttywflush(struct tty *tp);
296 void	 ttytstamp(struct tty *tp, int octs, int ncts, int odcd, int ndcd);
297 
298 void	tty_init(void);
299 struct tty *ttymalloc(int);
300 void	 ttyfree(struct tty *);
301 
302 int	cttyopen(dev_t, int, int, struct proc *);
303 int	cttyread(dev_t, struct uio *, int);
304 int	cttywrite(dev_t, struct uio *, int);
305 int	cttyioctl(dev_t, u_long, caddr_t, int, struct proc *);
306 
307 void	clalloc(struct clist *, int, int);
308 void	clfree(struct clist *);
309 
310 int	nullioctl(struct tty *, u_long, caddr_t, int, struct proc *);
311 
312 int	pppopen(dev_t dev, struct tty *, struct proc *);
313 int	pppclose(struct tty *, int, struct proc *);
314 int	ppptioctl(struct tty *, u_long, caddr_t, int, struct proc *);
315 int	pppinput(int c, struct tty *);
316 int	pppstart(struct tty *);
317 int	pppread(struct tty *, struct uio *, int);
318 int	pppwrite(struct tty *, struct uio *, int);
319 
320 int	nmeaopen(dev_t, struct tty *, struct proc *);
321 int	nmeaclose(struct tty *, int, struct proc *);
322 int	nmeainput(int, struct tty *);
323 
324 int	mstsopen(dev_t, struct tty *, struct proc *);
325 int	mstsclose(struct tty *, int, struct proc *);
326 int	mstsinput(int, struct tty *);
327 
328 int	endrunopen(dev_t, struct tty *, struct proc *);
329 int	endrunclose(struct tty *, int, struct proc *);
330 int	endruninput(int, struct tty *);
331 
332 #endif /* _KERNEL */
333