11541Srgrimes/*-
2181905Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
397379Sdes * All rights reserved.
497379Sdes *
5181905Sed * Portions of this software were developed under sponsorship from Snow
6181905Sed * B.V., the Netherlands.
797379Sdes *
81541Srgrimes * Redistribution and use in source and binary forms, with or without
91541Srgrimes * modification, are permitted provided that the following conditions
101541Srgrimes * are met:
111541Srgrimes * 1. Redistributions of source code must retain the above copyright
121541Srgrimes *    notice, this list of conditions and the following disclaimer.
131541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer in the
151541Srgrimes *    documentation and/or other materials provided with the distribution.
161541Srgrimes *
17181905Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20181905Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
2950477Speter * $FreeBSD: stable/11/sys/sys/tty.h 314538 2017-03-02 04:23:53Z ian $
301541Srgrimes */
311541Srgrimes
322165Spaul#ifndef _SYS_TTY_H_
334825Sbde#define	_SYS_TTY_H_
342165Spaul
35181907Sed#include <sys/param.h>
3659288Sjlemon#include <sys/queue.h>
37181907Sed#include <sys/lock.h>
38181907Sed#include <sys/mutex.h>
39181907Sed#include <sys/condvar.h>
4070834Swollman#include <sys/selinfo.h>
41199898Sed#include <sys/_termios.h>
42181907Sed#include <sys/ttycom.h>
43181907Sed#include <sys/ttyqueue.h>
441541Srgrimes
45135377Sphkstruct cdev;
46181907Sedstruct file;
47181907Sedstruct pgrp;
48181907Sedstruct session;
49181907Sedstruct ucred;
50131387Smarcel
51181907Sedstruct ttydevsw;
52131373Sphk
531541Srgrimes/*
54181907Sed * Per-TTY structure, containing buffers, etc.
551541Srgrimes *
56181907Sed * List of locks
57181907Sed * (t)	locked by t_mtx
58181907Sed * (l)	locked by tty_list_sx
59181907Sed * (c)	const until freeing
601541Srgrimes */
611541Srgrimesstruct tty {
62181907Sed	struct mtx	*t_mtx;		/* TTY lock. */
63181907Sed	struct mtx	t_mtxobj;	/* Per-TTY lock (when not borrowing). */
64181907Sed	TAILQ_ENTRY(tty) t_list;	/* (l) TTY list entry. */
65314538Sian	int		t_drainwait;	/* (t) TIOCDRAIN timeout seconds. */
66181907Sed	unsigned int	t_flags;	/* (t) Terminal option flags. */
67183922Sed/* Keep flags in sync with db_show_tty and pstat(8). */
68188147Sed#define	TF_NOPREFIX	0x00001	/* Don't prepend "tty" to device name. */
69188147Sed#define	TF_INITLOCK	0x00002	/* Create init/lock state devices. */
70188147Sed#define	TF_CALLOUT	0x00004	/* Create "cua" devices. */
71188147Sed#define	TF_OPENED_IN	0x00008	/* "tty" node is in use. */
72188147Sed#define	TF_OPENED_OUT	0x00010	/* "cua" node is in use. */
73188147Sed#define	TF_OPENED_CONS	0x00020 /* Device in use as console. */
74188147Sed#define	TF_OPENED	(TF_OPENED_IN|TF_OPENED_OUT|TF_OPENED_CONS)
75188147Sed#define	TF_GONE		0x00040	/* Device node is gone. */
76188147Sed#define	TF_OPENCLOSE	0x00080	/* Device is in open()/close(). */
77188147Sed#define	TF_ASYNC	0x00100	/* Asynchronous I/O enabled. */
78188147Sed#define	TF_LITERAL	0x00200	/* Accept the next character literally. */
79188147Sed#define	TF_HIWAT_IN	0x00400	/* We've reached the input watermark. */
80188147Sed#define	TF_HIWAT_OUT	0x00800	/* We've reached the output watermark. */
81181907Sed#define	TF_HIWAT	(TF_HIWAT_IN|TF_HIWAT_OUT)
82188147Sed#define	TF_STOPPED	0x01000	/* Output flow control - stopped. */
83188147Sed#define	TF_EXCLUDE	0x02000	/* Exclusive access. */
84188147Sed#define	TF_BYPASS	0x04000	/* Optimized input path. */
85188147Sed#define	TF_ZOMBIE	0x08000	/* Modem disconnect received. */
86188147Sed#define	TF_HOOK		0x10000	/* TTY has hook attached. */
87188487Sed#define	TF_BUSY_IN	0x20000	/* Process busy in read() -- not supported. */
88188487Sed#define	TF_BUSY_OUT	0x40000	/* Process busy in write(). */
89188487Sed#define	TF_BUSY		(TF_BUSY_IN|TF_BUSY_OUT)
90181907Sed	unsigned int	t_revokecnt;	/* (t) revoke() count. */
91130261Sphk
92181907Sed	/* Buffering mechanisms. */
93181907Sed	struct ttyinq	t_inq;		/* (t) Input queue. */
94181907Sed	size_t		t_inlow;	/* (t) Input low watermark. */
95181907Sed	struct ttyoutq	t_outq;		/* (t) Output queue. */
96181907Sed	size_t		t_outlow;	/* (t) Output low watermark. */
97131373Sphk
98181907Sed	/* Sleeping mechanisms. */
99181907Sed	struct cv	t_inwait;	/* (t) Input wait queue. */
100181907Sed	struct cv	t_outwait;	/* (t) Output wait queue. */
101194771Sed	struct cv	t_outserwait;	/* (t) Serial output wait queue. */
102181907Sed	struct cv	t_bgwait;	/* (t) Background wait queue. */
103181907Sed	struct cv	t_dcdwait;	/* (t) Carrier Detect wait queue. */
1041541Srgrimes
105181907Sed	/* Polling mechanisms. */
106181907Sed	struct selinfo	t_inpoll;	/* (t) Input poll queue. */
107181907Sed	struct selinfo	t_outpoll;	/* (t) Output poll queue. */
108181907Sed	struct sigio	*t_sigio;	/* (t) Asynchronous I/O. */
1091541Srgrimes
110181907Sed	struct termios	t_termios;	/* (t) I/O processing flags. */
111181907Sed	struct winsize	t_winsize;	/* (t) Window size. */
112181907Sed	unsigned int	t_column;	/* (t) Current cursor position. */
113181907Sed	unsigned int	t_writepos;	/* (t) Where input was interrupted. */
114182763Sed	int		t_compatflags;	/* (t) COMPAT_43TTY flags. */
1151541Srgrimes
116181907Sed	/* Init/lock-state devices. */
117181907Sed	struct termios	t_termios_init_in;	/* tty%s.init. */
118181907Sed	struct termios	t_termios_lock_in;	/* tty%s.lock. */
119181907Sed	struct termios	t_termios_init_out;	/* cua%s.init. */
120181907Sed	struct termios	t_termios_lock_out;	/* cua%s.lock. */
121181907Sed
122181907Sed	struct ttydevsw	*t_devsw;	/* (c) Driver hooks. */
123183276Sed	struct ttyhook	*t_hook;	/* (t) Capture/inject hook. */
124181907Sed
125181907Sed	/* Process signal delivery. */
126181907Sed	struct pgrp	*t_pgrp;	/* (t) Foreground process group. */
127181907Sed	struct session	*t_session;	/* (t) Associated session. */
128181907Sed	unsigned int	t_sessioncnt;	/* (t) Backpointing sessions. */
129181907Sed
130183276Sed	void		*t_devswsoftc;	/* (c) Soft config, for drivers. */
131183276Sed	void		*t_hooksoftc;	/* (t) Soft config, for hooks. */
132181907Sed	struct cdev	*t_dev;		/* (c) Primary character device. */
13397366Sdes};
13497366Sdes
13597366Sdes/*
136181907Sed * Userland version of struct tty, for sysctl kern.ttys
1377850Sbde */
138181907Sedstruct xtty {
139181907Sed	size_t	xt_size;	/* Structure size. */
140181907Sed	size_t	xt_insize;	/* Input queue size. */
141181907Sed	size_t	xt_incc;	/* Canonicalized characters. */
142181907Sed	size_t	xt_inlc;	/* Input line charaters. */
143181907Sed	size_t	xt_inlow;	/* Input low watermark. */
144181907Sed	size_t	xt_outsize;	/* Output queue size. */
145181907Sed	size_t	xt_outcc;	/* Output queue usage. */
146181907Sed	size_t	xt_outlow;	/* Output low watermark. */
147181907Sed	unsigned int xt_column;	/* Current column position. */
148181907Sed	pid_t	xt_pgid;	/* Foreground process group. */
149181907Sed	pid_t	xt_sid;		/* Session. */
150181907Sed	unsigned int xt_flags;	/* Terminal option flags. */
151181907Sed	dev_t	xt_dev;		/* Userland device. */
1521541Srgrimes};
1531541Srgrimes
15455205Speter#ifdef _KERNEL
155135368Sphk
156223722Sed/* Used to distinguish between normal, callout, lock and init devices. */
157223722Sed#define	TTYUNIT_INIT		0x1
158223722Sed#define	TTYUNIT_LOCK		0x2
159223722Sed#define	TTYUNIT_CALLOUT		0x4
160223722Sed
161181907Sed/* Allocation and deallocation. */
162193018Sedstruct tty *tty_alloc(struct ttydevsw *tsw, void *softc);
163193018Sedstruct tty *tty_alloc_mutex(struct ttydevsw *tsw, void *softc, struct mtx *mtx);
164183274Sedvoid	tty_rel_pgrp(struct tty *tp, struct pgrp *pgrp);
165183274Sedvoid	tty_rel_sess(struct tty *tp, struct session *sess);
166183274Sedvoid	tty_rel_gone(struct tty *tp);
167135368Sphk
168181907Sed#define	tty_lock(tp)		mtx_lock((tp)->t_mtx)
169181907Sed#define	tty_unlock(tp)		mtx_unlock((tp)->t_mtx)
170259016Sray#define	tty_lock_owned(tp)	mtx_owned((tp)->t_mtx)
171181907Sed#define	tty_lock_assert(tp,ma)	mtx_assert((tp)->t_mtx, (ma))
172183332Sed#define	tty_getlock(tp)		((tp)->t_mtx)
173135368Sphk
174181907Sed/* Device node creation. */
175259549Sglebiusint	tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
176259549Sglebius    const char *fmt, ...) __printflike(4, 5);
177259549Sglebius#define	TTYMK_CLONING		0x1
178259549Sglebius#define	tty_makedev(tp, cred, fmt, ...) \
179259549Sglebius	(void )tty_makedevf((tp), (cred), 0, (fmt), ## __VA_ARGS__)
180181907Sed#define	tty_makealias(tp,fmt,...) \
181181907Sed	make_dev_alias((tp)->t_dev, fmt, ## __VA_ARGS__)
1821541Srgrimes
183181907Sed/* Signalling processes. */
184183274Sedvoid	tty_signal_sessleader(struct tty *tp, int signal);
185183274Sedvoid	tty_signal_pgrp(struct tty *tp, int signal);
186181907Sed/* Waking up readers/writers. */
187183274Sedint	tty_wait(struct tty *tp, struct cv *cv);
188242078Sedint	tty_wait_background(struct tty *tp, struct thread *td, int sig);
189183274Sedint	tty_timedwait(struct tty *tp, struct cv *cv, int timo);
190183274Sedvoid	tty_wakeup(struct tty *tp, int flags);
1917430Sbde
192181907Sed/* System messages. */
193183274Sedint	tty_checkoutq(struct tty *tp);
194183274Sedint	tty_putchar(struct tty *tp, char c);
195151385Sphk
196201532Sedint	tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
197201532Sed    struct thread *td);
198183274Sedint	tty_ioctl_compat(struct tty *tp, u_long cmd, caddr_t data,
199201532Sed    int fflag, struct thread *td);
200242529Sedvoid	tty_set_winsize(struct tty *tp, const struct winsize *wsz);
201183274Sedvoid	tty_init_console(struct tty *tp, speed_t speed);
202183274Sedvoid	tty_flush(struct tty *tp, int flags);
203183274Sedvoid	tty_hiwat_in_block(struct tty *tp);
204183274Sedvoid	tty_hiwat_in_unblock(struct tty *tp);
205183274Seddev_t	tty_udev(struct tty *tp);
206181907Sed#define	tty_opened(tp)		((tp)->t_flags & TF_OPENED)
207181907Sed#define	tty_gone(tp)		((tp)->t_flags & TF_GONE)
208183276Sed#define	tty_softc(tp)		((tp)->t_devswsoftc)
209181907Sed#define	tty_devname(tp)		devtoname((tp)->t_dev)
210151385Sphk
211181907Sed/* Status line printing. */
212183274Sedvoid	tty_info(struct tty *tp);
213151385Sphk
214184521Sed/* /dev/console selection. */
215184521Sedvoid	ttyconsdev_select(const char *name);
216184521Sed
217181907Sed/* Pseudo-terminal hooks. */
218196886Sedint	pts_alloc(int fflags, struct thread *td, struct file *fp);
219183274Sedint	pts_alloc_external(int fd, struct thread *td, struct file *fp,
220183274Sed    struct cdev *dev, const char *name);
221151385Sphk
222181907Sed/* Drivers and line disciplines also need to call these. */
223183276Sed#include <sys/ttydisc.h>
224181907Sed#include <sys/ttydevsw.h>
225183276Sed#include <sys/ttyhook.h>
22655205Speter#endif /* _KERNEL */
2272165Spaul
2284825Sbde#endif /* !_SYS_TTY_H_ */
229