tty.h revision 188147
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: head/sys/sys/tty.h 188147 2009-02-05 14:21:09Z ed $
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>
41181907Sed#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. */
65181907Sed	unsigned int	t_flags;	/* (t) Terminal option flags. */
66183922Sed/* Keep flags in sync with db_show_tty and pstat(8). */
67188147Sed#define	TF_NOPREFIX	0x00001	/* Don't prepend "tty" to device name. */
68188147Sed#define	TF_INITLOCK	0x00002	/* Create init/lock state devices. */
69188147Sed#define	TF_CALLOUT	0x00004	/* Create "cua" devices. */
70188147Sed#define	TF_OPENED_IN	0x00008	/* "tty" node is in use. */
71188147Sed#define	TF_OPENED_OUT	0x00010	/* "cua" node is in use. */
72188147Sed#define	TF_OPENED_CONS	0x00020 /* Device in use as console. */
73188147Sed#define	TF_OPENED	(TF_OPENED_IN|TF_OPENED_OUT|TF_OPENED_CONS)
74188147Sed#define	TF_GONE		0x00040	/* Device node is gone. */
75188147Sed#define	TF_OPENCLOSE	0x00080	/* Device is in open()/close(). */
76188147Sed#define	TF_ASYNC	0x00100	/* Asynchronous I/O enabled. */
77188147Sed#define	TF_LITERAL	0x00200	/* Accept the next character literally. */
78188147Sed#define	TF_HIWAT_IN	0x00400	/* We've reached the input watermark. */
79188147Sed#define	TF_HIWAT_OUT	0x00800	/* We've reached the output watermark. */
80181907Sed#define	TF_HIWAT	(TF_HIWAT_IN|TF_HIWAT_OUT)
81188147Sed#define	TF_STOPPED	0x01000	/* Output flow control - stopped. */
82188147Sed#define	TF_EXCLUDE	0x02000	/* Exclusive access. */
83188147Sed#define	TF_BYPASS	0x04000	/* Optimized input path. */
84188147Sed#define	TF_ZOMBIE	0x08000	/* Modem disconnect received. */
85188147Sed#define	TF_HOOK		0x10000	/* TTY has hook attached. */
86181907Sed	unsigned int	t_revokecnt;	/* (t) revoke() count. */
87130261Sphk
88181907Sed	/* Buffering mechanisms. */
89181907Sed	struct ttyinq	t_inq;		/* (t) Input queue. */
90181907Sed	size_t		t_inlow;	/* (t) Input low watermark. */
91181907Sed	struct ttyoutq	t_outq;		/* (t) Output queue. */
92181907Sed	size_t		t_outlow;	/* (t) Output low watermark. */
93131373Sphk
94181907Sed	/* Sleeping mechanisms. */
95181907Sed	struct cv	t_inwait;	/* (t) Input wait queue. */
96181907Sed	struct cv	t_outwait;	/* (t) Output wait queue. */
97181907Sed	struct cv	t_bgwait;	/* (t) Background wait queue. */
98181907Sed	struct cv	t_dcdwait;	/* (t) Carrier Detect wait queue. */
991541Srgrimes
100181907Sed	/* Polling mechanisms. */
101181907Sed	struct selinfo	t_inpoll;	/* (t) Input poll queue. */
102181907Sed	struct selinfo	t_outpoll;	/* (t) Output poll queue. */
103181907Sed	struct sigio	*t_sigio;	/* (t) Asynchronous I/O. */
1041541Srgrimes
105181907Sed	struct termios	t_termios;	/* (t) I/O processing flags. */
106181907Sed	struct winsize	t_winsize;	/* (t) Window size. */
107181907Sed	unsigned int	t_column;	/* (t) Current cursor position. */
108181907Sed	unsigned int	t_writepos;	/* (t) Where input was interrupted. */
109182763Sed	int		t_compatflags;	/* (t) COMPAT_43TTY flags. */
1101541Srgrimes
111181907Sed	/* Init/lock-state devices. */
112181907Sed	struct termios	t_termios_init_in;	/* tty%s.init. */
113181907Sed	struct termios	t_termios_lock_in;	/* tty%s.lock. */
114181907Sed	struct termios	t_termios_init_out;	/* cua%s.init. */
115181907Sed	struct termios	t_termios_lock_out;	/* cua%s.lock. */
116181907Sed
117181907Sed	struct ttydevsw	*t_devsw;	/* (c) Driver hooks. */
118183276Sed	struct ttyhook	*t_hook;	/* (t) Capture/inject hook. */
119181907Sed
120181907Sed	/* Process signal delivery. */
121181907Sed	struct pgrp	*t_pgrp;	/* (t) Foreground process group. */
122181907Sed	struct session	*t_session;	/* (t) Associated session. */
123181907Sed	unsigned int	t_sessioncnt;	/* (t) Backpointing sessions. */
124181907Sed
125183276Sed	void		*t_devswsoftc;	/* (c) Soft config, for drivers. */
126183276Sed	void		*t_hooksoftc;	/* (t) Soft config, for hooks. */
127181907Sed	struct cdev	*t_dev;		/* (c) Primary character device. */
12897366Sdes};
12997366Sdes
13097366Sdes/*
131181907Sed * Userland version of struct tty, for sysctl kern.ttys
1327850Sbde */
133181907Sedstruct xtty {
134181907Sed	size_t	xt_size;	/* Structure size. */
135181907Sed	size_t	xt_insize;	/* Input queue size. */
136181907Sed	size_t	xt_incc;	/* Canonicalized characters. */
137181907Sed	size_t	xt_inlc;	/* Input line charaters. */
138181907Sed	size_t	xt_inlow;	/* Input low watermark. */
139181907Sed	size_t	xt_outsize;	/* Output queue size. */
140181907Sed	size_t	xt_outcc;	/* Output queue usage. */
141181907Sed	size_t	xt_outlow;	/* Output low watermark. */
142181907Sed	unsigned int xt_column;	/* Current column position. */
143181907Sed	pid_t	xt_pgid;	/* Foreground process group. */
144181907Sed	pid_t	xt_sid;		/* Session. */
145181907Sed	unsigned int xt_flags;	/* Terminal option flags. */
146181907Sed	dev_t	xt_dev;		/* Userland device. */
1471541Srgrimes};
1481541Srgrimes
14955205Speter#ifdef _KERNEL
150135368Sphk
151181907Sed/* Allocation and deallocation. */
152183274Sedstruct tty *tty_alloc(struct ttydevsw *tsw, void *softc, struct mtx *mtx);
153183274Sedvoid	tty_rel_pgrp(struct tty *tp, struct pgrp *pgrp);
154183274Sedvoid	tty_rel_sess(struct tty *tp, struct session *sess);
155183274Sedvoid	tty_rel_gone(struct tty *tp);
156135368Sphk
157181907Sed#define	tty_lock(tp)		mtx_lock((tp)->t_mtx)
158181907Sed#define	tty_unlock(tp)		mtx_unlock((tp)->t_mtx)
159181907Sed#define	tty_lock_assert(tp,ma)	mtx_assert((tp)->t_mtx, (ma))
160183332Sed#define	tty_getlock(tp)		((tp)->t_mtx)
161135368Sphk
162181907Sed/* Device node creation. */
163183274Sedvoid	tty_makedev(struct tty *tp, struct ucred *cred, const char *fmt, ...)
164181907Sed    __printflike(3, 4);
165181907Sed#define	tty_makealias(tp,fmt,...) \
166181907Sed	make_dev_alias((tp)->t_dev, fmt, ## __VA_ARGS__)
1671541Srgrimes
168181907Sed/* Signalling processes. */
169183274Sedvoid	tty_signal_sessleader(struct tty *tp, int signal);
170183274Sedvoid	tty_signal_pgrp(struct tty *tp, int signal);
171181907Sed/* Waking up readers/writers. */
172183274Sedint	tty_wait(struct tty *tp, struct cv *cv);
173183274Sedint	tty_timedwait(struct tty *tp, struct cv *cv, int timo);
174183274Sedvoid	tty_wakeup(struct tty *tp, int flags);
1757430Sbde
176181907Sed/* System messages. */
177183274Sedint	tty_checkoutq(struct tty *tp);
178183274Sedint	tty_putchar(struct tty *tp, char c);
179151385Sphk
180183274Sedint	tty_ioctl(struct tty *tp, u_long cmd, void *data, struct thread *td);
181183274Sedint	tty_ioctl_compat(struct tty *tp, u_long cmd, caddr_t data,
182183274Sed    struct thread *td);
183183274Sedvoid	tty_init_console(struct tty *tp, speed_t speed);
184183274Sedvoid	tty_flush(struct tty *tp, int flags);
185183274Sedvoid	tty_hiwat_in_block(struct tty *tp);
186183274Sedvoid	tty_hiwat_in_unblock(struct tty *tp);
187183274Seddev_t	tty_udev(struct tty *tp);
188181907Sed#define	tty_opened(tp)		((tp)->t_flags & TF_OPENED)
189181907Sed#define	tty_gone(tp)		((tp)->t_flags & TF_GONE)
190183276Sed#define	tty_softc(tp)		((tp)->t_devswsoftc)
191181907Sed#define	tty_devname(tp)		devtoname((tp)->t_dev)
192151385Sphk
193181907Sed/* Status line printing. */
194183274Sedvoid	tty_info(struct tty *tp);
195151385Sphk
196184521Sed/* /dev/console selection. */
197184521Sedvoid	ttyconsdev_select(const char *name);
198184521Sed
199181907Sed/* Pseudo-terminal hooks. */
200183274Sedint	pts_alloc_external(int fd, struct thread *td, struct file *fp,
201183274Sed    struct cdev *dev, const char *name);
202151385Sphk
203181907Sed/* Drivers and line disciplines also need to call these. */
204183276Sed#include <sys/ttydisc.h>
205181907Sed#include <sys/ttydevsw.h>
206183276Sed#include <sys/ttyhook.h>
20755205Speter#endif /* _KERNEL */
2082165Spaul
2094825Sbde#endif /* !_SYS_TTY_H_ */
210