tty.h revision 262861
1/*-
2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: stable/10/sys/sys/tty.h 262861 2014-03-06 18:30:56Z jhb $
30 */
31
32#ifndef _SYS_TTY_H_
33#define	_SYS_TTY_H_
34
35#include <sys/param.h>
36#include <sys/queue.h>
37#include <sys/lock.h>
38#include <sys/mutex.h>
39#include <sys/condvar.h>
40#include <sys/selinfo.h>
41#include <sys/_termios.h>
42#include <sys/ttycom.h>
43#include <sys/ttyqueue.h>
44
45struct cdev;
46struct file;
47struct pgrp;
48struct session;
49struct ucred;
50
51struct ttydevsw;
52
53/*
54 * Per-TTY structure, containing buffers, etc.
55 *
56 * List of locks
57 * (t)	locked by t_mtx
58 * (l)	locked by tty_list_sx
59 * (c)	const until freeing
60 */
61struct tty {
62	struct mtx	*t_mtx;		/* TTY lock. */
63	struct mtx	t_mtxobj;	/* Per-TTY lock (when not borrowing). */
64	TAILQ_ENTRY(tty) t_list;	/* (l) TTY list entry. */
65	unsigned int	t_flags;	/* (t) Terminal option flags. */
66/* Keep flags in sync with db_show_tty and pstat(8). */
67#define	TF_NOPREFIX	0x00001	/* Don't prepend "tty" to device name. */
68#define	TF_INITLOCK	0x00002	/* Create init/lock state devices. */
69#define	TF_CALLOUT	0x00004	/* Create "cua" devices. */
70#define	TF_OPENED_IN	0x00008	/* "tty" node is in use. */
71#define	TF_OPENED_OUT	0x00010	/* "cua" node is in use. */
72#define	TF_OPENED_CONS	0x00020 /* Device in use as console. */
73#define	TF_OPENED	(TF_OPENED_IN|TF_OPENED_OUT|TF_OPENED_CONS)
74#define	TF_GONE		0x00040	/* Device node is gone. */
75#define	TF_OPENCLOSE	0x00080	/* Device is in open()/close(). */
76#define	TF_ASYNC	0x00100	/* Asynchronous I/O enabled. */
77#define	TF_LITERAL	0x00200	/* Accept the next character literally. */
78#define	TF_HIWAT_IN	0x00400	/* We've reached the input watermark. */
79#define	TF_HIWAT_OUT	0x00800	/* We've reached the output watermark. */
80#define	TF_HIWAT	(TF_HIWAT_IN|TF_HIWAT_OUT)
81#define	TF_STOPPED	0x01000	/* Output flow control - stopped. */
82#define	TF_EXCLUDE	0x02000	/* Exclusive access. */
83#define	TF_BYPASS	0x04000	/* Optimized input path. */
84#define	TF_ZOMBIE	0x08000	/* Modem disconnect received. */
85#define	TF_HOOK		0x10000	/* TTY has hook attached. */
86#define	TF_BUSY_IN	0x20000	/* Process busy in read() -- not supported. */
87#define	TF_BUSY_OUT	0x40000	/* Process busy in write(). */
88#define	TF_BUSY		(TF_BUSY_IN|TF_BUSY_OUT)
89	unsigned int	t_revokecnt;	/* (t) revoke() count. */
90
91	/* Buffering mechanisms. */
92	struct ttyinq	t_inq;		/* (t) Input queue. */
93	size_t		t_inlow;	/* (t) Input low watermark. */
94	struct ttyoutq	t_outq;		/* (t) Output queue. */
95	size_t		t_outlow;	/* (t) Output low watermark. */
96
97	/* Sleeping mechanisms. */
98	struct cv	t_inwait;	/* (t) Input wait queue. */
99	struct cv	t_outwait;	/* (t) Output wait queue. */
100	struct cv	t_outserwait;	/* (t) Serial output wait queue. */
101	struct cv	t_bgwait;	/* (t) Background wait queue. */
102	struct cv	t_dcdwait;	/* (t) Carrier Detect wait queue. */
103
104	/* Polling mechanisms. */
105	struct selinfo	t_inpoll;	/* (t) Input poll queue. */
106	struct selinfo	t_outpoll;	/* (t) Output poll queue. */
107	struct sigio	*t_sigio;	/* (t) Asynchronous I/O. */
108
109	struct termios	t_termios;	/* (t) I/O processing flags. */
110	struct winsize	t_winsize;	/* (t) Window size. */
111	unsigned int	t_column;	/* (t) Current cursor position. */
112	unsigned int	t_writepos;	/* (t) Where input was interrupted. */
113	int		t_compatflags;	/* (t) COMPAT_43TTY flags. */
114
115	/* Init/lock-state devices. */
116	struct termios	t_termios_init_in;	/* tty%s.init. */
117	struct termios	t_termios_lock_in;	/* tty%s.lock. */
118	struct termios	t_termios_init_out;	/* cua%s.init. */
119	struct termios	t_termios_lock_out;	/* cua%s.lock. */
120
121	struct ttydevsw	*t_devsw;	/* (c) Driver hooks. */
122	struct ttyhook	*t_hook;	/* (t) Capture/inject hook. */
123
124	/* Process signal delivery. */
125	struct pgrp	*t_pgrp;	/* (t) Foreground process group. */
126	struct session	*t_session;	/* (t) Associated session. */
127	unsigned int	t_sessioncnt;	/* (t) Backpointing sessions. */
128
129	void		*t_devswsoftc;	/* (c) Soft config, for drivers. */
130	void		*t_hooksoftc;	/* (t) Soft config, for hooks. */
131	struct cdev	*t_dev;		/* (c) Primary character device. */
132};
133
134/*
135 * Userland version of struct tty, for sysctl kern.ttys
136 */
137struct xtty {
138	size_t	xt_size;	/* Structure size. */
139	size_t	xt_insize;	/* Input queue size. */
140	size_t	xt_incc;	/* Canonicalized characters. */
141	size_t	xt_inlc;	/* Input line charaters. */
142	size_t	xt_inlow;	/* Input low watermark. */
143	size_t	xt_outsize;	/* Output queue size. */
144	size_t	xt_outcc;	/* Output queue usage. */
145	size_t	xt_outlow;	/* Output low watermark. */
146	unsigned int xt_column;	/* Current column position. */
147	pid_t	xt_pgid;	/* Foreground process group. */
148	pid_t	xt_sid;		/* Session. */
149	unsigned int xt_flags;	/* Terminal option flags. */
150	dev_t	xt_dev;		/* Userland device. */
151};
152
153#ifdef _KERNEL
154
155/* Used to distinguish between normal, callout, lock and init devices. */
156#define	TTYUNIT_INIT		0x1
157#define	TTYUNIT_LOCK		0x2
158#define	TTYUNIT_CALLOUT		0x4
159
160/* Allocation and deallocation. */
161struct tty *tty_alloc(struct ttydevsw *tsw, void *softc);
162struct tty *tty_alloc_mutex(struct ttydevsw *tsw, void *softc, struct mtx *mtx);
163void	tty_rel_pgrp(struct tty *tp, struct pgrp *pgrp);
164void	tty_rel_sess(struct tty *tp, struct session *sess);
165void	tty_rel_gone(struct tty *tp);
166
167#define	tty_lock(tp)		mtx_lock((tp)->t_mtx)
168#define	tty_unlock(tp)		mtx_unlock((tp)->t_mtx)
169#define	tty_lock_owned(tp)	mtx_owned((tp)->t_mtx)
170#define	tty_lock_assert(tp,ma)	mtx_assert((tp)->t_mtx, (ma))
171#define	tty_getlock(tp)		((tp)->t_mtx)
172
173/* Device node creation. */
174void	tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
175    __printflike(3, 4);
176#define	tty_makealias(tp,fmt,...) \
177	make_dev_alias((tp)->t_dev, fmt, ## __VA_ARGS__)
178
179/* Signalling processes. */
180void	tty_signal_sessleader(struct tty *tp, int signal);
181void	tty_signal_pgrp(struct tty *tp, int signal);
182/* Waking up readers/writers. */
183int	tty_wait(struct tty *tp, struct cv *cv);
184int	tty_wait_background(struct tty *tp, struct thread *td, int sig);
185int	tty_timedwait(struct tty *tp, struct cv *cv, int timo);
186void	tty_wakeup(struct tty *tp, int flags);
187
188/* System messages. */
189int	tty_checkoutq(struct tty *tp);
190int	tty_putchar(struct tty *tp, char c);
191
192int	tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
193    struct thread *td);
194int	tty_ioctl_compat(struct tty *tp, u_long cmd, caddr_t data,
195    int fflag, struct thread *td);
196void	tty_set_winsize(struct tty *tp, const struct winsize *wsz);
197void	tty_init_console(struct tty *tp, speed_t speed);
198void	tty_flush(struct tty *tp, int flags);
199void	tty_hiwat_in_block(struct tty *tp);
200void	tty_hiwat_in_unblock(struct tty *tp);
201dev_t	tty_udev(struct tty *tp);
202#define	tty_opened(tp)		((tp)->t_flags & TF_OPENED)
203#define	tty_gone(tp)		((tp)->t_flags & TF_GONE)
204#define	tty_softc(tp)		((tp)->t_devswsoftc)
205#define	tty_devname(tp)		devtoname((tp)->t_dev)
206
207/* Status line printing. */
208void	tty_info(struct tty *tp);
209
210/* /dev/console selection. */
211void	ttyconsdev_select(const char *name);
212
213/* Pseudo-terminal hooks. */
214int	pts_alloc(int fflags, struct thread *td, struct file *fp);
215int	pts_alloc_external(int fd, struct thread *td, struct file *fp,
216    struct cdev *dev, const char *name);
217
218/* Drivers and line disciplines also need to call these. */
219#include <sys/ttydisc.h>
220#include <sys/ttydevsw.h>
221#include <sys/ttyhook.h>
222#endif /* _KERNEL */
223
224#endif /* !_SYS_TTY_H_ */
225