tty.h revision 131373
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1982, 1986, 1993
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes * (c) UNIX System Laboratories, Inc.
51541Srgrimes * All or some portions of this file are derived from material licensed
61541Srgrimes * to the University of California by American Telephone and Telegraph
71541Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with
81541Srgrimes * the permission of UNIX System Laboratories, Inc.
91541Srgrimes *
1097379Sdes * Copyright (c) 2002 Networks Associates Technologies, Inc.
1197379Sdes * All rights reserved.
1297379Sdes *
1397379Sdes * Portions of this software were developed for the FreeBSD Project by
1497379Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
1597379Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1697379Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1797379Sdes *
181541Srgrimes * Redistribution and use in source and binary forms, with or without
191541Srgrimes * modification, are permitted provided that the following conditions
201541Srgrimes * are met:
211541Srgrimes * 1. Redistributions of source code must retain the above copyright
221541Srgrimes *    notice, this list of conditions and the following disclaimer.
231541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
241541Srgrimes *    notice, this list of conditions and the following disclaimer in the
251541Srgrimes *    documentation and/or other materials provided with the distribution.
261541Srgrimes * 4. Neither the name of the University nor the names of its contributors
271541Srgrimes *    may be used to endorse or promote products derived from this software
281541Srgrimes *    without specific prior written permission.
291541Srgrimes *
301541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
311541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
321541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
331541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
341541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
351541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
361541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
371541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
381541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
391541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
401541Srgrimes * SUCH DAMAGE.
411541Srgrimes *
421541Srgrimes *	@(#)tty.h	8.6 (Berkeley) 1/21/94
4350477Speter * $FreeBSD: head/sys/sys/tty.h 131373 2004-06-30 21:38:08Z phk $
441541Srgrimes */
451541Srgrimes
462165Spaul#ifndef _SYS_TTY_H_
474825Sbde#define	_SYS_TTY_H_
482165Spaul
491541Srgrimes#include <sys/termios.h>
5059288Sjlemon#include <sys/queue.h>
5170834Swollman#include <sys/selinfo.h>
52130261Sphk#include <sys/_lock.h>
53130261Sphk#include <sys/_mutex.h>
541541Srgrimes
551541Srgrimes/*
561541Srgrimes * Clists are character lists, which is a variable length linked list
571541Srgrimes * of cblocks, with a count of the number of characters in the list.
581541Srgrimes */
591541Srgrimesstruct clist {
601541Srgrimes	int	c_cc;		/* Number of characters in the clist. */
614825Sbde	int	c_cbcount;	/* Number of cblocks. */
624825Sbde	int	c_cbmax;	/* Max # cblocks allowed for this clist. */
634825Sbde	int	c_cbreserved;	/* # cblocks reserved for this clist. */
641541Srgrimes	char	*c_cf;		/* Pointer to the first cblock. */
651541Srgrimes	char	*c_cl;		/* Pointer to the last cblock. */
661541Srgrimes};
671541Srgrimes
68131373Sphktypedef void t_oproc_t(struct tty *);
69131373Sphktypedef void t_stop_t(struct tty *, int);
70131373Sphktypedef int t_param_t(struct tty *, struct termios *);
71131373Sphktypedef int t_modem_t(struct tty *, int, int);
72131373Sphktypedef void t_break_t(struct tty *, int);
73131373Sphktypedef int t_ioctl_t(struct tty *, u_long cmd, void * data,
74131373Sphk		      int fflag, struct thread *td);
75131373Sphk
761541Srgrimes/*
771541Srgrimes * Per-tty structure.
781541Srgrimes *
791541Srgrimes * Should be split in two, into device and tty drivers.
801541Srgrimes * Glue could be masks of what to echo and circular buffer
811541Srgrimes * (low, high, timeout).
821541Srgrimes */
831541Srgrimesstruct tty {
841541Srgrimes	struct	clist t_rawq;		/* Device raw input queue. */
851541Srgrimes	long	t_rawcc;		/* Raw input queue statistics. */
861541Srgrimes	struct	clist t_canq;		/* Device canonical queue. */
871541Srgrimes	long	t_cancc;		/* Canonical queue statistics. */
881541Srgrimes	struct	clist t_outq;		/* Device output queue. */
891541Srgrimes	long	t_outcc;		/* Output queue statistics. */
9022877Sbde	int	t_line;			/* Interface to device drivers. */
91130966Sphk	struct cdev *t_dev;		/* Device. */
921541Srgrimes	int	t_state;		/* Device and driver (TS*) state. */
931541Srgrimes	int	t_flags;		/* Tty flags. */
945396Sache	int     t_timeout;              /* Timeout for ttywait() */
951541Srgrimes	struct	pgrp *t_pgrp;		/* Foreground process group. */
961541Srgrimes	struct	session *t_session;	/* Enclosing session. */
9741087Struckman	struct  sigio *t_sigio;		/* Information for async I/O. */
981541Srgrimes	struct	selinfo t_rsel;		/* Tty read/oob select. */
991541Srgrimes	struct	selinfo t_wsel;		/* Tty write select. */
1001541Srgrimes	struct	termios t_termios;	/* Termios state. */
1011541Srgrimes	struct	winsize t_winsize;	/* Window size. */
102131373Sphk	void	*t_sc;			/* driver private softc pointer. */
1039833Sbde	int	t_column;		/* Tty output column. */
1049833Sbde	int	t_rocount, t_rocol;	/* Tty. */
10534185Sbde	int	t_ififosize;		/* Total size of upstream fifos. */
10634185Sbde	int	t_ihiwat;		/* High water mark for input. */
10734185Sbde	int	t_ilowat;		/* Low water mark for input. */
10834185Sbde	speed_t	t_ispeedwat;		/* t_ispeed override for watermarks. */
10934185Sbde	int	t_ohiwat;		/* High water mark for output. */
11034185Sbde	int	t_olowat;		/* Low water mark for output. */
11134185Sbde	speed_t	t_ospeedwat;		/* t_ospeed override for watermarks. */
1129833Sbde	int	t_gen;			/* Generation number. */
113130261Sphk	TAILQ_ENTRY(tty) t_list;	/* Global chain of ttys for pstat(8) */
114130261Sphk
115130261Sphk	struct mtx t_mtx;
116130261Sphk	int	t_refcnt;
117131130Sphk	int	t_hotchar;		/* linedisc preferred hot char */
118131373Sphk
119131373Sphk	/* Driver supplied methods */
120131373Sphk	t_oproc_t *t_oproc;		/* Start output. */
121131373Sphk	t_stop_t *t_stop;		/* Stop output. */
122131373Sphk	t_param_t *t_param;		/* Set parameters. */
123131373Sphk	t_modem_t *t_modem;		/* Set modem state (optional). */
124131373Sphk	t_break_t *t_break;		/* Set break state (optional). */
125131373Sphk	t_ioctl_t *t_ioctl;		/* Set ioctl handling (optional). */
1261541Srgrimes};
1271541Srgrimes
1281541Srgrimes#define	t_cc		t_termios.c_cc
1291541Srgrimes#define	t_cflag		t_termios.c_cflag
1301541Srgrimes#define	t_iflag		t_termios.c_iflag
1311541Srgrimes#define	t_ispeed	t_termios.c_ispeed
1321541Srgrimes#define	t_lflag		t_termios.c_lflag
1331541Srgrimes#define	t_min		t_termios.c_min
1341541Srgrimes#define	t_oflag		t_termios.c_oflag
1351541Srgrimes#define	t_ospeed	t_termios.c_ospeed
1361541Srgrimes#define	t_time		t_termios.c_time
1371541Srgrimes
13872376Sjake#define	TTIPRI		(PSOCK + 1)	/* Sleep priority for tty reads. */
13972376Sjake#define	TTOPRI		(PSOCK + 2)	/* Sleep priority for tty writes. */
1401541Srgrimes
1417850Sbde/*
14297402Sbde * Userland version of struct tty, for sysctl.
14397366Sdes */
14497366Sdesstruct xtty {
14597402Sbde	size_t	xt_size;		/* Structure size. */
14697366Sdes	long	xt_rawcc;		/* Raw input queue statistics. */
14797366Sdes	long	xt_cancc;		/* Canonical queue statistics. */
14897366Sdes	long	xt_outcc;		/* Output queue statistics. */
14997366Sdes	int	xt_line;		/* Interface to device drivers. */
150130640Sphk	dev_t	xt_dev;			/* Userland (sysctl) instance. */
15197366Sdes	int	xt_state;		/* Device and driver (TS*) state. */
15297366Sdes	int	xt_flags;		/* Tty flags. */
15397402Sbde	int     xt_timeout;		/* Timeout for ttywait(). */
15497402Sbde	pid_t	xt_pgid;		/* Process group ID. */
15597402Sbde	pid_t	xt_sid;			/* Session ID. */
15697366Sdes	struct	termios xt_termios;	/* Termios state. */
15797366Sdes	struct	winsize xt_winsize;	/* Window size. */
15897366Sdes	int	xt_column;		/* Tty output column. */
15997366Sdes	int	xt_rocount, xt_rocol;	/* Tty. */
16097366Sdes	int	xt_ififosize;		/* Total size of upstream fifos. */
16197366Sdes	int	xt_ihiwat;		/* High water mark for input. */
16297366Sdes	int	xt_ilowat;		/* Low water mark for input. */
16397366Sdes	speed_t	xt_ispeedwat;		/* t_ispeed override for watermarks. */
16497366Sdes	int	xt_ohiwat;		/* High water mark for output. */
16597366Sdes	int	xt_olowat;		/* Low water mark for output. */
16697366Sdes	speed_t	xt_ospeedwat;		/* t_ospeed override for watermarks. */
16797366Sdes};
16897366Sdes
16997366Sdes/*
1707850Sbde * User data unfortunately has to be copied through buffers on the way to
1717850Sbde * and from clists.  The buffers are on the stack so their sizes must be
1727850Sbde * fairly small.
1737850Sbde */
1747850Sbde#define	IBUFSIZ	384			/* Should be >= max value of MIN. */
1751541Srgrimes#define	OBUFSIZ	100
1767850Sbde
1779543Sjoerg#ifndef TTYHOG
178111900Sdas#define	TTYHOG	8192
1799543Sjoerg#endif
1801541Srgrimes
18155205Speter#ifdef _KERNEL
1821541Srgrimes#define	TTMAXHIWAT	roundup(2048, CBSIZE)
1831541Srgrimes#define	TTMINHIWAT	roundup(100, CBSIZE)
1841541Srgrimes#define	TTMAXLOWAT	256
1851541Srgrimes#define	TTMINLOWAT	32
1861541Srgrimes#endif
1871541Srgrimes
1881541Srgrimes/* These flags are kept in t_state. */
1899790Sbde#define	TS_SO_OLOWAT	0x00001		/* Wake up when output <= low water. */
1901541Srgrimes#define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
1911541Srgrimes#define	TS_BUSY		0x00004		/* Draining output. */
1921541Srgrimes#define	TS_CARR_ON	0x00008		/* Carrier is present. */
1931541Srgrimes#define	TS_FLUSH	0x00010		/* Outq has been flushed during DMA. */
1941541Srgrimes#define	TS_ISOPEN	0x00020		/* Open has completed. */
1951541Srgrimes#define	TS_TBLOCK	0x00040		/* Further input blocked. */
1961541Srgrimes#define	TS_TIMEOUT	0x00080		/* Wait for output char processing. */
1971541Srgrimes#define	TS_TTSTOP	0x00100		/* Output paused. */
1989823Sbde#ifdef notyet
1991541Srgrimes#define	TS_WOPEN	0x00200		/* Open in progress. */
2009623Sbde#endif
2011541Srgrimes#define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
2021541Srgrimes
2031541Srgrimes/* State for intra-line fancy editing work. */
2041541Srgrimes#define	TS_BKSL		0x00800		/* State for lowercase \ work. */
2051541Srgrimes#define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
2061541Srgrimes#define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
2071541Srgrimes#define	TS_LNCH		0x04000		/* Next character is literal. */
2081541Srgrimes#define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
2091541Srgrimes#define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
2101541Srgrimes
2119790Sbde/* Extras. */
2129823Sbde#define	TS_CAN_BYPASS_L_RINT 0x010000	/* Device in "raw" mode. */
2139824Sbde#define	TS_CONNECTED	0x020000	/* Connection open. */
2149823Sbde#define	TS_SNOOP	0x040000	/* Device is being snooped on. */
2159790Sbde#define	TS_SO_OCOMPLETE	0x080000	/* Wake up when output completes. */
2169824Sbde#define	TS_ZOMBIE	0x100000	/* Connection lost. */
2176391Sugen
2189832Sbde/* Hardware flow-control-invoked bits. */
2199832Sbde#define	TS_CAR_OFLOW	0x200000	/* For MDMBUF (XXX handle in driver). */
2209832Sbde#ifdef notyet
2219832Sbde#define	TS_CTS_OFLOW	0x400000	/* For CCTS_OFLOW. */
2229832Sbde#define	TS_DSR_OFLOW	0x800000	/* For CDSR_OFLOW. */
2239832Sbde#endif
2249832Sbde
2251541Srgrimes/* Character type information. */
2261541Srgrimes#define	ORDINARY	0
2271541Srgrimes#define	CONTROL		1
2281541Srgrimes#define	BACKSPACE	2
2291541Srgrimes#define	NEWLINE		3
2301541Srgrimes#define	TAB		4
2311541Srgrimes#define	VTAB		5
2321541Srgrimes#define	RETURN		6
2331541Srgrimes
2341541Srgrimesstruct speedtab {
2351541Srgrimes	int sp_speed;			/* Speed. */
2361541Srgrimes	int sp_code;			/* Code. */
2371541Srgrimes};
2381541Srgrimes
2391541Srgrimes/* Modem control commands (driver). */
2401541Srgrimes#define	DMSET		0
2411541Srgrimes#define	DMBIS		1
2421541Srgrimes#define	DMBIC		2
2431541Srgrimes#define	DMGET		3
2441541Srgrimes
2451541Srgrimes/* Flags on a character passed to ttyinput. */
2461541Srgrimes#define	TTY_CHARMASK	0x000000ff	/* Character mask */
2471541Srgrimes#define	TTY_QUOTE	0x00000100	/* Character quoted */
2481541Srgrimes#define	TTY_ERRORMASK	0xff000000	/* Error mask */
2497467Sache#define	TTY_FE		0x01000000	/* Framing error */
2501541Srgrimes#define	TTY_PE		0x02000000	/* Parity error */
2517467Sache#define	TTY_OE		0x04000000	/* Overrun error */
2527467Sache#define	TTY_BI		0x08000000	/* Break condition */
2531541Srgrimes
2541541Srgrimes/* Is tp controlling terminal for p? */
2551541Srgrimes#define	isctty(p, tp)							\
2561541Srgrimes	((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
2571541Srgrimes
2581541Srgrimes/* Is p in background of tp? */
2591541Srgrimes#define	isbackground(p, tp)						\
2601541Srgrimes	(isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
2611541Srgrimes
2629639Sbde/* Unique sleep addresses. */
2639824Sbde#define	TSA_CARR_ON(tp)		((void *)&(tp)->t_rawq)
2649824Sbde#define	TSA_HUP_OR_INPUT(tp)	((void *)&(tp)->t_rawq.c_cf)
2659790Sbde#define	TSA_OCOMPLETE(tp)	((void *)&(tp)->t_outq.c_cl)
2669790Sbde#define	TSA_OLOWAT(tp)		((void *)&(tp)->t_outq)
2679639Sbde
26855205Speter#ifdef _KERNEL
26930354Sphk#ifdef MALLOC_DECLARE
27030354SphkMALLOC_DECLARE(M_TTYS);
27130354Sphk#endif
272116663Siedowseextern	struct msgbuf consmsgbuf; /* Message buffer for constty. */
2737090Sbdeextern	struct tty *constty;	/* Temporary virtual console. */
274110997Sphkextern long tk_cancc;
275110997Sphkextern long tk_nin;
276110997Sphkextern long tk_nout;
277110997Sphkextern long tk_rawcc;
2781541Srgrimes
27992719Salfredint	 b_to_q(char *cp, int cc, struct clist *q);
28092719Salfredvoid	 catq(struct clist *from, struct clist *to);
28192719Salfredvoid	 clist_alloc_cblocks(struct clist *q, int ccmax, int ccres);
28292719Salfredvoid	 clist_free_cblocks(struct clist *q);
283116663Siedowsevoid	 constty_set(struct tty *tp);
284116663Siedowsevoid	 constty_clear(void);
28592719Salfredint	 getc(struct clist *q);
28692719Salfredvoid	 ndflush(struct clist *q, int cc);
28792719Salfredchar	*nextc(struct clist *q, char *cp, int *c);
28892719Salfredvoid	 nottystop(struct tty *tp, int rw);
28992719Salfredint	 putc(int c, struct clist *q);
29092719Salfredint	 q_to_b(struct clist *q, char *cp, int cc);
29192719Salfredvoid	 termioschars(struct termios *t);
29292719Salfredint	 tputchar(int c, struct tty *tp);
29392719Salfredint	 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag);
29492719Salfredint	 ttioctl(struct tty *tp, u_long com, void *data, int flag);
29592719Salfredint	 ttread(struct tty *tp, struct uio *uio, int flag);
29692719Salfredvoid	 ttrstrt(void *tp);
29792719Salfredint	 ttsetcompat(struct tty *tp, u_long *com, caddr_t data,
29892719Salfred	    struct termios *term);
29992719Salfredvoid	 ttsetwater(struct tty *tp);
30092719Salfredint	 ttspeedtab(int speed, struct speedtab *table);
30192719Salfredint	 ttstart(struct tty *tp);
30292719Salfredvoid	 ttwakeup(struct tty *tp);
30392719Salfredint	 ttwrite(struct tty *tp, struct uio *uio, int flag);
30492719Salfredvoid	 ttwwakeup(struct tty *tp);
30592719Salfredvoid	 ttyblock(struct tty *tp);
30692719Salfredvoid	 ttychars(struct tty *tp);
30792719Salfredint	 ttycheckoutq(struct tty *tp, int wait);
30892719Salfredint	 ttyclose(struct tty *tp);
30992719Salfredvoid	 ttyflush(struct tty *tp, int rw);
31092719Salfredvoid	 ttyinfo(struct tty *tp);
31192719Salfredint	 ttyinput(int c, struct tty *tp);
31292719Salfredint	 ttylclose(struct tty *tp, int flag);
313131134Sphkvoid	 ttyldoptim(struct tty *tp);
31492719Salfredstruct tty *ttymalloc(struct tty *tp);
31592719Salfredint	 ttymodem(struct tty *tp, int flag);
316130585Sphkint	 ttyopen(struct cdev *device, struct tty *tp);
317130261Sphkint	 ttyref(struct tty *tp);
318130261Sphkint	 ttyrel(struct tty *tp);
31993258Sbdeint	 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo);
32092719Salfredint	 ttywait(struct tty *tp);
32192719Salfredint	 unputc(struct clist *q);
3227430Sbde
323130075Sphk/*
324130075Sphk * XXX: temporary
325130075Sphk */
326130075Sphk#include <sys/linedisc.h>
327130075Sphk
32855205Speter#endif /* _KERNEL */
3292165Spaul
3304825Sbde#endif /* !_SYS_TTY_H_ */
331