1 /**	$MirOS: src/sys/sys/tty.h,v 1.3 2005/07/07 14:39:27 tg Exp $ */
2 /*	$OpenBSD: tty.h,v 1.19 2004/09/19 21:34:43 mickey Exp $	*/
3 /*	$NetBSD: tty.h,v 1.30.4.1 1996/06/02 09:08:13 mrg Exp $	*/
4 
5 /*-
6  * Copyright (c) 1982, 1986, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. 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  */
40 
41 #include <sys/termios.h>
42 #include <sys/queue.h>
43 #include <sys/select.h>		/* For struct selinfo. */
44 #include <sys/timeout.h>
45 
46 #define KERN_TTY_TKNIN		1	/* quad: input chars */
47 #define KERN_TTY_TKNOUT		2	/* quad: output chars */
48 #define KERN_TTY_TKRAWCC	3	/* quad: input chars, raw mode */
49 #define KERN_TTY_TKCANCC	4	/* quad: input char, cooked mode */
50 #define KERN_TTY_INFO		5	/* struct: tty stats */
51 #define KERN_TTY_MAXPTYS	6	/* int: max ptys */
52 #define KERN_TTY_NPTYS		7	/* int: number of allocated ptys */
53 #define KERN_TTY_MAXID		8
54 
55 #define CTL_KERN_TTY_NAMES { \
56 	{ 0, 0 }, \
57 	{ "tk_nin", CTLTYPE_QUAD }, \
58 	{ "tk_nout", CTLTYPE_QUAD }, \
59 	{ "tk_rawcc", CTLTYPE_QUAD }, \
60 	{ "tk_cancc", CTLTYPE_QUAD }, \
61 	{ "ttyinfo", CTLTYPE_STRUCT }, \
62 	{ "maxptys", CTLTYPE_INT }, \
63 	{ "nptys", CTLTYPE_INT }, \
64 }
65 
66 /* ptmget, for /dev/ptm pty getting ioctl PTMGET */
67 
68 struct ptmget {
69 	int	cfd;
70 	int	sfd;
71 	char	cn[16];
72 	char	sn[16];
73 };
74 #define PTMGET _IOR('t', 1, struct ptmget) /* get ptys */
75 #define PATH_PTMDEV	"/dev/ptm"
76 #define TTY_GID		4	/* XXX evil hardcoding of tty gid */
77 
78 /*
79  * Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
80  * exactly the same behaviour as in true clists.
81  * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
82  * (but, saves memory and cpu time)
83  *
84  * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
85  */
86 struct clist {
87 	int	c_cc;		/* count of characters in queue */
88 	int	c_cn;		/* total ring buffer length */
89 	u_char	*c_cf;		/* points to first character */
90 	u_char	*c_cl;		/* points to next open character */
91 	u_char	*c_cs;		/* start of ring buffer */
92 	u_char	*c_ce;		/* c_ce + c_len */
93 	u_char	*c_cq;		/* N bits/bytes long, see tty_subr.c */
94 };
95 
96 /*
97  * Per-tty structure.
98  *
99  * Should be split in two, into device and tty drivers.
100  * Glue could be masks of what to echo and circular buffer
101  * (low, high, timeout).
102  */
103 struct tty {
104 	TAILQ_ENTRY(tty) tty_link;	/* Link in global tty list. */
105 	struct	clist t_rawq;		/* Device raw input queue. */
106 	long	t_rawcc;		/* Raw input queue statistics. */
107 	struct	clist t_canq;		/* Device canonical queue. */
108 	long	t_cancc;		/* Canonical queue statistics. */
109 	struct	clist t_outq;		/* Device output queue. */
110 	long	t_outcc;		/* Output queue statistics. */
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 };
135 
136 /*
137  * Small version of struct tty exported via sysctl KERN_TTY_INFO
138  */
139 struct itty {
140 	dev_t   t_dev;
141 	int t_rawq_c_cc;
142 	int t_canq_c_cc;
143 	int t_outq_c_cc;
144 	short t_hiwat;
145 	short t_lowat;
146 	short t_column;
147 	int t_state;
148 	struct session *t_session;
149 	pid_t t_pgrp_pg_id;
150 	u_char t_line;
151 };
152 
153 #define	t_cc		t_termios.c_cc
154 #define	t_cflag		t_termios.c_cflag
155 #define	t_iflag		t_termios.c_iflag
156 #define	t_ispeed	t_termios.c_ispeed
157 #define	t_lflag		t_termios.c_lflag
158 #define	t_min		t_termios.c_min
159 #define	t_oflag		t_termios.c_oflag
160 #define	t_ospeed	t_termios.c_ospeed
161 #define	t_time		t_termios.c_time
162 
163 #define	TTIPRI	25			/* Sleep priority for tty reads. */
164 #define	TTOPRI	26			/* Sleep priority for tty writes. */
165 
166 #define	TTMASK	15
167 #define	OBUFSIZ	100
168 #define	TTYHOG	1024
169 
170 #ifdef _KERNEL
171 #define	TTMAXHIWAT	roundup(2048, CBSIZE)
172 #define	TTMINHIWAT	roundup(100, CBSIZE)
173 #define	TTMAXLOWAT	256
174 #define	TTMINLOWAT	32
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 /* Character type information. */
199 #define	ORDINARY	0
200 #define	CONTROL		1
201 #define	BACKSPACE	2
202 #define	NEWLINE		3
203 #define	TAB		4
204 #define	VTAB		5
205 #define	RETURN		6
206 
207 struct speedtab {
208 	int sp_speed;			/* Speed. */
209 	int sp_code;			/* Code. */
210 };
211 
212 /* Modem control commands (driver). */
213 #define	DMSET		0
214 #define	DMBIS		1
215 #define	DMBIC		2
216 #define	DMGET		3
217 
218 /* Flags on a character passed to ttyinput. */
219 #define	TTY_CHARMASK	0x000000ff	/* Character mask */
220 #define	TTY_QUOTE	0x00000100	/* Character quoted */
221 #define	TTY_ERRORMASK	0xff000000	/* Error mask */
222 #define	TTY_FE		0x01000000	/* Framing error or BREAK condition */
223 #define	TTY_PE		0x02000000	/* Parity error */
224 
225 /* Is tp controlling terminal for p? */
226 #define	isctty(p, tp)							\
227 	((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
228 
229 /* Is p in background of tp? */
230 #define	isbackground(p, tp)						\
231 	(isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
232 
233 /*
234  * ttylist_head is defined here so that user-land has access to it.
235  */
236 TAILQ_HEAD(ttylist_head, tty);		/* the ttylist is a TAILQ */
237 
238 #ifdef _KERNEL
239 
240 extern	int tty_count;			/* number of ttys in global ttylist */
241 extern	struct ttychars ttydefaults;
242 
243 /* Symbolic sleep message strings. */
244 extern	 char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[], ttybuf[];
245 
246 int	sysctl_tty(int *, u_int, void *, size_t *, void *, size_t);
247 int	sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
248 
249 int	 b_to_q(u_char *cp, int cc, struct clist *q);
250 void	 catq(struct clist *from, struct clist *to);
251 void	 clist_init(void);
252 int	 getc(struct clist *q);
253 void	 ndflush(struct clist *q, int cc);
254 int	 ndqb(struct clist *q, int flag);
255 u_char	*nextc(struct clist *q, u_char *cp, int *c);
256 int	 putc(int c, struct clist *q);
257 int	 q_to_b(struct clist *q, u_char *cp, int cc);
258 int	 unputc(struct clist *q);
259 
260 int	 nullmodem(struct tty *tp, int flag);
261 int	 tputchar(int c, struct tty *tp);
262 int	 ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
263 	    struct proc *p);
264 int	 ttread(struct tty *tp, struct uio *uio, int flag);
265 void	 ttrstrt(void *tp);
266 int	 ttpoll(dev_t device, int events, struct proc *p);
267 int	 ttkqfilter(dev_t dev, struct knote *kn);
268 void	 ttsetwater(struct tty *tp);
269 int	 ttspeedtab(int speed, const struct speedtab *table);
270 int	 ttstart(struct tty *tp);
271 void	 ttwakeup(struct tty *tp);
272 int	 ttwrite(struct tty *tp, struct uio *uio, int flag);
273 void	 ttychars(struct tty *tp);
274 int	 ttycheckoutq(struct tty *tp, int wait);
275 int	 ttyclose(struct tty *tp);
276 void	 ttyflush(struct tty *tp, int rw);
277 void	 ttyinfo(struct tty *tp);
278 int	 ttyinput(int c, struct tty *tp);
279 int	 ttylclose(struct tty *tp, int flag);
280 int	 ttymodem(struct tty *tp, int flag);
281 int	 ttyopen(dev_t device, struct tty *tp);
282 int	 ttyoutput(int c, struct tty *tp);
283 void	 ttypend(struct tty *tp);
284 void	 ttyretype(struct tty *tp);
285 void	 ttyrub(int c, struct tty *tp);
286 int	 ttysleep(struct tty *tp,
287 	    void *chan, int pri, char *wmesg, int timeout);
288 int	 ttywait(struct tty *tp);
289 int	 ttywflush(struct tty *tp);
290 
291 void	tty_init(void);
292 struct tty *ttymalloc(void);
293 void	 ttyfree(struct tty *);
294 u_char	*firstc(struct clist *clp, int *c);
295 
296 int	cttyopen(dev_t, int, int, struct proc *);
297 int	cttyread(dev_t, struct uio *, int);
298 int	cttywrite(dev_t, struct uio *, int);
299 int	cttyioctl(dev_t, u_long, caddr_t, int, struct proc *);
300 int	cttypoll(dev_t, int, struct proc *);
301 
302 int	clalloc(struct clist *, int, int);
303 void	clfree(struct clist *);
304 
305 #if defined(COMPAT_43)
306 # define COMPAT_OLDTTY
307 int 	ttcompat(struct tty *, u_long, caddr_t, int, struct proc *);
308 #endif
309 
310 #endif
311