tty.h revision 131387
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 131387 2004-07-01 03:08:15Z marcel $
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
68131387Smarcelstruct tty;
69131387Smarcel
70131373Sphktypedef void t_oproc_t(struct tty *);
71131373Sphktypedef void t_stop_t(struct tty *, int);
72131373Sphktypedef int t_param_t(struct tty *, struct termios *);
73131373Sphktypedef int t_modem_t(struct tty *, int, int);
74131373Sphktypedef void t_break_t(struct tty *, int);
75131373Sphktypedef int t_ioctl_t(struct tty *, u_long cmd, void * data,
76131373Sphk		      int fflag, struct thread *td);
77131373Sphk
781541Srgrimes/*
791541Srgrimes * Per-tty structure.
801541Srgrimes *
811541Srgrimes * Should be split in two, into device and tty drivers.
821541Srgrimes * Glue could be masks of what to echo and circular buffer
831541Srgrimes * (low, high, timeout).
841541Srgrimes */
851541Srgrimesstruct tty {
861541Srgrimes	struct	clist t_rawq;		/* Device raw input queue. */
871541Srgrimes	long	t_rawcc;		/* Raw input queue statistics. */
881541Srgrimes	struct	clist t_canq;		/* Device canonical queue. */
891541Srgrimes	long	t_cancc;		/* Canonical queue statistics. */
901541Srgrimes	struct	clist t_outq;		/* Device output queue. */
911541Srgrimes	long	t_outcc;		/* Output queue statistics. */
9222877Sbde	int	t_line;			/* Interface to device drivers. */
93130966Sphk	struct cdev *t_dev;		/* Device. */
941541Srgrimes	int	t_state;		/* Device and driver (TS*) state. */
951541Srgrimes	int	t_flags;		/* Tty flags. */
965396Sache	int     t_timeout;              /* Timeout for ttywait() */
971541Srgrimes	struct	pgrp *t_pgrp;		/* Foreground process group. */
981541Srgrimes	struct	session *t_session;	/* Enclosing session. */
9941087Struckman	struct  sigio *t_sigio;		/* Information for async I/O. */
1001541Srgrimes	struct	selinfo t_rsel;		/* Tty read/oob select. */
1011541Srgrimes	struct	selinfo t_wsel;		/* Tty write select. */
1021541Srgrimes	struct	termios t_termios;	/* Termios state. */
1031541Srgrimes	struct	winsize t_winsize;	/* Window size. */
104131373Sphk	void	*t_sc;			/* driver private softc pointer. */
1059833Sbde	int	t_column;		/* Tty output column. */
1069833Sbde	int	t_rocount, t_rocol;	/* Tty. */
10734185Sbde	int	t_ififosize;		/* Total size of upstream fifos. */
10834185Sbde	int	t_ihiwat;		/* High water mark for input. */
10934185Sbde	int	t_ilowat;		/* Low water mark for input. */
11034185Sbde	speed_t	t_ispeedwat;		/* t_ispeed override for watermarks. */
11134185Sbde	int	t_ohiwat;		/* High water mark for output. */
11234185Sbde	int	t_olowat;		/* Low water mark for output. */
11334185Sbde	speed_t	t_ospeedwat;		/* t_ospeed override for watermarks. */
1149833Sbde	int	t_gen;			/* Generation number. */
115130261Sphk	TAILQ_ENTRY(tty) t_list;	/* Global chain of ttys for pstat(8) */
116130261Sphk
117130261Sphk	struct mtx t_mtx;
118130261Sphk	int	t_refcnt;
119131130Sphk	int	t_hotchar;		/* linedisc preferred hot char */
120131373Sphk
121131373Sphk	/* Driver supplied methods */
122131373Sphk	t_oproc_t *t_oproc;		/* Start output. */
123131373Sphk	t_stop_t *t_stop;		/* Stop output. */
124131373Sphk	t_param_t *t_param;		/* Set parameters. */
125131373Sphk	t_modem_t *t_modem;		/* Set modem state (optional). */
126131373Sphk	t_break_t *t_break;		/* Set break state (optional). */
127131373Sphk	t_ioctl_t *t_ioctl;		/* Set ioctl handling (optional). */
1281541Srgrimes};
1291541Srgrimes
1301541Srgrimes#define	t_cc		t_termios.c_cc
1311541Srgrimes#define	t_cflag		t_termios.c_cflag
1321541Srgrimes#define	t_iflag		t_termios.c_iflag
1331541Srgrimes#define	t_ispeed	t_termios.c_ispeed
1341541Srgrimes#define	t_lflag		t_termios.c_lflag
1351541Srgrimes#define	t_min		t_termios.c_min
1361541Srgrimes#define	t_oflag		t_termios.c_oflag
1371541Srgrimes#define	t_ospeed	t_termios.c_ospeed
1381541Srgrimes#define	t_time		t_termios.c_time
1391541Srgrimes
14072376Sjake#define	TTIPRI		(PSOCK + 1)	/* Sleep priority for tty reads. */
14172376Sjake#define	TTOPRI		(PSOCK + 2)	/* Sleep priority for tty writes. */
1421541Srgrimes
1437850Sbde/*
14497402Sbde * Userland version of struct tty, for sysctl.
14597366Sdes */
14697366Sdesstruct xtty {
14797402Sbde	size_t	xt_size;		/* Structure size. */
14897366Sdes	long	xt_rawcc;		/* Raw input queue statistics. */
14997366Sdes	long	xt_cancc;		/* Canonical queue statistics. */
15097366Sdes	long	xt_outcc;		/* Output queue statistics. */
15197366Sdes	int	xt_line;		/* Interface to device drivers. */
152130640Sphk	dev_t	xt_dev;			/* Userland (sysctl) instance. */
15397366Sdes	int	xt_state;		/* Device and driver (TS*) state. */
15497366Sdes	int	xt_flags;		/* Tty flags. */
15597402Sbde	int     xt_timeout;		/* Timeout for ttywait(). */
15697402Sbde	pid_t	xt_pgid;		/* Process group ID. */
15797402Sbde	pid_t	xt_sid;			/* Session ID. */
15897366Sdes	struct	termios xt_termios;	/* Termios state. */
15997366Sdes	struct	winsize xt_winsize;	/* Window size. */
16097366Sdes	int	xt_column;		/* Tty output column. */
16197366Sdes	int	xt_rocount, xt_rocol;	/* Tty. */
16297366Sdes	int	xt_ififosize;		/* Total size of upstream fifos. */
16397366Sdes	int	xt_ihiwat;		/* High water mark for input. */
16497366Sdes	int	xt_ilowat;		/* Low water mark for input. */
16597366Sdes	speed_t	xt_ispeedwat;		/* t_ispeed override for watermarks. */
16697366Sdes	int	xt_ohiwat;		/* High water mark for output. */
16797366Sdes	int	xt_olowat;		/* Low water mark for output. */
16897366Sdes	speed_t	xt_ospeedwat;		/* t_ospeed override for watermarks. */
16997366Sdes};
17097366Sdes
17197366Sdes/*
1727850Sbde * User data unfortunately has to be copied through buffers on the way to
1737850Sbde * and from clists.  The buffers are on the stack so their sizes must be
1747850Sbde * fairly small.
1757850Sbde */
1767850Sbde#define	IBUFSIZ	384			/* Should be >= max value of MIN. */
1771541Srgrimes#define	OBUFSIZ	100
1787850Sbde
1799543Sjoerg#ifndef TTYHOG
180111900Sdas#define	TTYHOG	8192
1819543Sjoerg#endif
1821541Srgrimes
18355205Speter#ifdef _KERNEL
1841541Srgrimes#define	TTMAXHIWAT	roundup(2048, CBSIZE)
1851541Srgrimes#define	TTMINHIWAT	roundup(100, CBSIZE)
1861541Srgrimes#define	TTMAXLOWAT	256
1871541Srgrimes#define	TTMINLOWAT	32
1881541Srgrimes#endif
1891541Srgrimes
1901541Srgrimes/* These flags are kept in t_state. */
1919790Sbde#define	TS_SO_OLOWAT	0x00001		/* Wake up when output <= low water. */
1921541Srgrimes#define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
1931541Srgrimes#define	TS_BUSY		0x00004		/* Draining output. */
1941541Srgrimes#define	TS_CARR_ON	0x00008		/* Carrier is present. */
1951541Srgrimes#define	TS_FLUSH	0x00010		/* Outq has been flushed during DMA. */
1961541Srgrimes#define	TS_ISOPEN	0x00020		/* Open has completed. */
1971541Srgrimes#define	TS_TBLOCK	0x00040		/* Further input blocked. */
1981541Srgrimes#define	TS_TIMEOUT	0x00080		/* Wait for output char processing. */
1991541Srgrimes#define	TS_TTSTOP	0x00100		/* Output paused. */
2009823Sbde#ifdef notyet
2011541Srgrimes#define	TS_WOPEN	0x00200		/* Open in progress. */
2029623Sbde#endif
2031541Srgrimes#define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
2041541Srgrimes
2051541Srgrimes/* State for intra-line fancy editing work. */
2061541Srgrimes#define	TS_BKSL		0x00800		/* State for lowercase \ work. */
2071541Srgrimes#define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
2081541Srgrimes#define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
2091541Srgrimes#define	TS_LNCH		0x04000		/* Next character is literal. */
2101541Srgrimes#define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
2111541Srgrimes#define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
2121541Srgrimes
2139790Sbde/* Extras. */
2149823Sbde#define	TS_CAN_BYPASS_L_RINT 0x010000	/* Device in "raw" mode. */
2159824Sbde#define	TS_CONNECTED	0x020000	/* Connection open. */
2169823Sbde#define	TS_SNOOP	0x040000	/* Device is being snooped on. */
2179790Sbde#define	TS_SO_OCOMPLETE	0x080000	/* Wake up when output completes. */
2189824Sbde#define	TS_ZOMBIE	0x100000	/* Connection lost. */
2196391Sugen
2209832Sbde/* Hardware flow-control-invoked bits. */
2219832Sbde#define	TS_CAR_OFLOW	0x200000	/* For MDMBUF (XXX handle in driver). */
2229832Sbde#ifdef notyet
2239832Sbde#define	TS_CTS_OFLOW	0x400000	/* For CCTS_OFLOW. */
2249832Sbde#define	TS_DSR_OFLOW	0x800000	/* For CDSR_OFLOW. */
2259832Sbde#endif
2269832Sbde
2271541Srgrimes/* Character type information. */
2281541Srgrimes#define	ORDINARY	0
2291541Srgrimes#define	CONTROL		1
2301541Srgrimes#define	BACKSPACE	2
2311541Srgrimes#define	NEWLINE		3
2321541Srgrimes#define	TAB		4
2331541Srgrimes#define	VTAB		5
2341541Srgrimes#define	RETURN		6
2351541Srgrimes
2361541Srgrimesstruct speedtab {
2371541Srgrimes	int sp_speed;			/* Speed. */
2381541Srgrimes	int sp_code;			/* Code. */
2391541Srgrimes};
2401541Srgrimes
2411541Srgrimes/* Modem control commands (driver). */
2421541Srgrimes#define	DMSET		0
2431541Srgrimes#define	DMBIS		1
2441541Srgrimes#define	DMBIC		2
2451541Srgrimes#define	DMGET		3
2461541Srgrimes
2471541Srgrimes/* Flags on a character passed to ttyinput. */
2481541Srgrimes#define	TTY_CHARMASK	0x000000ff	/* Character mask */
2491541Srgrimes#define	TTY_QUOTE	0x00000100	/* Character quoted */
2501541Srgrimes#define	TTY_ERRORMASK	0xff000000	/* Error mask */
2517467Sache#define	TTY_FE		0x01000000	/* Framing error */
2521541Srgrimes#define	TTY_PE		0x02000000	/* Parity error */
2537467Sache#define	TTY_OE		0x04000000	/* Overrun error */
2547467Sache#define	TTY_BI		0x08000000	/* Break condition */
2551541Srgrimes
2561541Srgrimes/* Is tp controlling terminal for p? */
2571541Srgrimes#define	isctty(p, tp)							\
2581541Srgrimes	((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
2591541Srgrimes
2601541Srgrimes/* Is p in background of tp? */
2611541Srgrimes#define	isbackground(p, tp)						\
2621541Srgrimes	(isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
2631541Srgrimes
2649639Sbde/* Unique sleep addresses. */
2659824Sbde#define	TSA_CARR_ON(tp)		((void *)&(tp)->t_rawq)
2669824Sbde#define	TSA_HUP_OR_INPUT(tp)	((void *)&(tp)->t_rawq.c_cf)
2679790Sbde#define	TSA_OCOMPLETE(tp)	((void *)&(tp)->t_outq.c_cl)
2689790Sbde#define	TSA_OLOWAT(tp)		((void *)&(tp)->t_outq)
2699639Sbde
27055205Speter#ifdef _KERNEL
27130354Sphk#ifdef MALLOC_DECLARE
27230354SphkMALLOC_DECLARE(M_TTYS);
27330354Sphk#endif
274116663Siedowseextern	struct msgbuf consmsgbuf; /* Message buffer for constty. */
2757090Sbdeextern	struct tty *constty;	/* Temporary virtual console. */
276110997Sphkextern long tk_cancc;
277110997Sphkextern long tk_nin;
278110997Sphkextern long tk_nout;
279110997Sphkextern long tk_rawcc;
2801541Srgrimes
28192719Salfredint	 b_to_q(char *cp, int cc, struct clist *q);
28292719Salfredvoid	 catq(struct clist *from, struct clist *to);
28392719Salfredvoid	 clist_alloc_cblocks(struct clist *q, int ccmax, int ccres);
28492719Salfredvoid	 clist_free_cblocks(struct clist *q);
285116663Siedowsevoid	 constty_set(struct tty *tp);
286116663Siedowsevoid	 constty_clear(void);
28792719Salfredint	 getc(struct clist *q);
28892719Salfredvoid	 ndflush(struct clist *q, int cc);
28992719Salfredchar	*nextc(struct clist *q, char *cp, int *c);
29092719Salfredvoid	 nottystop(struct tty *tp, int rw);
29192719Salfredint	 putc(int c, struct clist *q);
29292719Salfredint	 q_to_b(struct clist *q, char *cp, int cc);
29392719Salfredvoid	 termioschars(struct termios *t);
29492719Salfredint	 tputchar(int c, struct tty *tp);
29592719Salfredint	 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag);
29692719Salfredint	 ttioctl(struct tty *tp, u_long com, void *data, int flag);
29792719Salfredint	 ttread(struct tty *tp, struct uio *uio, int flag);
29892719Salfredvoid	 ttrstrt(void *tp);
29992719Salfredint	 ttsetcompat(struct tty *tp, u_long *com, caddr_t data,
30092719Salfred	    struct termios *term);
30192719Salfredvoid	 ttsetwater(struct tty *tp);
30292719Salfredint	 ttspeedtab(int speed, struct speedtab *table);
30392719Salfredint	 ttstart(struct tty *tp);
30492719Salfredvoid	 ttwakeup(struct tty *tp);
30592719Salfredint	 ttwrite(struct tty *tp, struct uio *uio, int flag);
30692719Salfredvoid	 ttwwakeup(struct tty *tp);
30792719Salfredvoid	 ttyblock(struct tty *tp);
30892719Salfredvoid	 ttychars(struct tty *tp);
30992719Salfredint	 ttycheckoutq(struct tty *tp, int wait);
31092719Salfredint	 ttyclose(struct tty *tp);
31192719Salfredvoid	 ttyflush(struct tty *tp, int rw);
31292719Salfredvoid	 ttyinfo(struct tty *tp);
31392719Salfredint	 ttyinput(int c, struct tty *tp);
31492719Salfredint	 ttylclose(struct tty *tp, int flag);
315131134Sphkvoid	 ttyldoptim(struct tty *tp);
31692719Salfredstruct tty *ttymalloc(struct tty *tp);
31792719Salfredint	 ttymodem(struct tty *tp, int flag);
318130585Sphkint	 ttyopen(struct cdev *device, struct tty *tp);
319130261Sphkint	 ttyref(struct tty *tp);
320130261Sphkint	 ttyrel(struct tty *tp);
32193258Sbdeint	 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo);
32292719Salfredint	 ttywait(struct tty *tp);
32392719Salfredint	 unputc(struct clist *q);
3247430Sbde
325130075Sphk/*
326130075Sphk * XXX: temporary
327130075Sphk */
328130075Sphk#include <sys/linedisc.h>
329130075Sphk
33055205Speter#endif /* _KERNEL */
3312165Spaul
3324825Sbde#endif /* !_SYS_TTY_H_ */
333