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