tty.h revision 97366
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 *
101541Srgrimes * Redistribution and use in source and binary forms, with or without
111541Srgrimes * modification, are permitted provided that the following conditions
121541Srgrimes * are met:
131541Srgrimes * 1. Redistributions of source code must retain the above copyright
141541Srgrimes *    notice, this list of conditions and the following disclaimer.
151541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161541Srgrimes *    notice, this list of conditions and the following disclaimer in the
171541Srgrimes *    documentation and/or other materials provided with the distribution.
181541Srgrimes * 3. All advertising materials mentioning features or use of this software
191541Srgrimes *    must display the following acknowledgement:
201541Srgrimes *	This product includes software developed by the University of
211541Srgrimes *	California, Berkeley and its contributors.
221541Srgrimes * 4. Neither the name of the University nor the names of its contributors
231541Srgrimes *    may be used to endorse or promote products derived from this software
241541Srgrimes *    without specific prior written permission.
251541Srgrimes *
261541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
271541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
281541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
291541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
301541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
311541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
321541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
341541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
351541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
361541Srgrimes * SUCH DAMAGE.
371541Srgrimes *
381541Srgrimes *	@(#)tty.h	8.6 (Berkeley) 1/21/94
3950477Speter * $FreeBSD: head/sys/sys/tty.h 97366 2002-05-28 05:40:53Z des $
401541Srgrimes */
411541Srgrimes
422165Spaul#ifndef _SYS_TTY_H_
434825Sbde#define	_SYS_TTY_H_
442165Spaul
451541Srgrimes#include <sys/termios.h>
4659288Sjlemon#include <sys/queue.h>
4770834Swollman#include <sys/selinfo.h>
481541Srgrimes
491541Srgrimes/*
501541Srgrimes * Clists are character lists, which is a variable length linked list
511541Srgrimes * of cblocks, with a count of the number of characters in the list.
521541Srgrimes */
531541Srgrimesstruct clist {
541541Srgrimes	int	c_cc;		/* Number of characters in the clist. */
554825Sbde	int	c_cbcount;	/* Number of cblocks. */
564825Sbde	int	c_cbmax;	/* Max # cblocks allowed for this clist. */
574825Sbde	int	c_cbreserved;	/* # cblocks reserved for this clist. */
581541Srgrimes	char	*c_cf;		/* Pointer to the first cblock. */
591541Srgrimes	char	*c_cl;		/* Pointer to the last cblock. */
601541Srgrimes};
611541Srgrimes
621541Srgrimes/*
631541Srgrimes * Per-tty structure.
641541Srgrimes *
651541Srgrimes * Should be split in two, into device and tty drivers.
661541Srgrimes * Glue could be masks of what to echo and circular buffer
671541Srgrimes * (low, high, timeout).
681541Srgrimes */
691541Srgrimesstruct tty {
701541Srgrimes	struct	clist t_rawq;		/* Device raw input queue. */
711541Srgrimes	long	t_rawcc;		/* Raw input queue statistics. */
721541Srgrimes	struct	clist t_canq;		/* Device canonical queue. */
731541Srgrimes	long	t_cancc;		/* Canonical queue statistics. */
741541Srgrimes	struct	clist t_outq;		/* Device output queue. */
751541Srgrimes	long	t_outcc;		/* Output queue statistics. */
7622877Sbde	int	t_line;			/* Interface to device drivers. */
7797366Sdes	dev_t	t_dev;			/* Device. */
781541Srgrimes	int	t_state;		/* Device and driver (TS*) state. */
791541Srgrimes	int	t_flags;		/* Tty flags. */
805396Sache	int     t_timeout;              /* Timeout for ttywait() */
811541Srgrimes	struct	pgrp *t_pgrp;		/* Foreground process group. */
821541Srgrimes	struct	session *t_session;	/* Enclosing session. */
8341087Struckman	struct  sigio *t_sigio;		/* Information for async I/O. */
841541Srgrimes	struct	selinfo t_rsel;		/* Tty read/oob select. */
851541Srgrimes	struct	selinfo t_wsel;		/* Tty write select. */
861541Srgrimes	struct	termios t_termios;	/* Termios state. */
871541Srgrimes	struct	winsize t_winsize;	/* Window size. */
881541Srgrimes					/* Start output. */
8992719Salfred	void	(*t_oproc)(struct tty *);
901541Srgrimes					/* Stop output. */
9192719Salfred	void	(*t_stop)(struct tty *, int);
921541Srgrimes					/* Set hardware state. */
9392719Salfred	int	(*t_param)(struct tty *, struct termios *);
941541Srgrimes	void	*t_sc;			/* XXX: net/if_sl.c:sl_softc. */
959833Sbde	int	t_column;		/* Tty output column. */
969833Sbde	int	t_rocount, t_rocol;	/* Tty. */
9734185Sbde	int	t_ififosize;		/* Total size of upstream fifos. */
9834185Sbde	int	t_ihiwat;		/* High water mark for input. */
9934185Sbde	int	t_ilowat;		/* Low water mark for input. */
10034185Sbde	speed_t	t_ispeedwat;		/* t_ispeed override for watermarks. */
10134185Sbde	int	t_ohiwat;		/* High water mark for output. */
10234185Sbde	int	t_olowat;		/* Low water mark for output. */
10334185Sbde	speed_t	t_ospeedwat;		/* t_ospeed override for watermarks. */
1049833Sbde	int	t_gen;			/* Generation number. */
10560938Sjake	SLIST_ENTRY(tty) t_list;	/* Global chain of ttys for pstat(8) */
1061541Srgrimes};
1071541Srgrimes
1081541Srgrimes#define	t_cc		t_termios.c_cc
1091541Srgrimes#define	t_cflag		t_termios.c_cflag
1101541Srgrimes#define	t_iflag		t_termios.c_iflag
1111541Srgrimes#define	t_ispeed	t_termios.c_ispeed
1121541Srgrimes#define	t_lflag		t_termios.c_lflag
1131541Srgrimes#define	t_min		t_termios.c_min
1141541Srgrimes#define	t_oflag		t_termios.c_oflag
1151541Srgrimes#define	t_ospeed	t_termios.c_ospeed
1161541Srgrimes#define	t_time		t_termios.c_time
1171541Srgrimes
11872376Sjake#define	TTIPRI		(PSOCK + 1)	/* Sleep priority for tty reads. */
11972376Sjake#define	TTOPRI		(PSOCK + 2)	/* Sleep priority for tty writes. */
1201541Srgrimes
1217850Sbde/*
12297366Sdes * Userland version of struct tty, for sysctl
12397366Sdes */
12497366Sdesstruct xtty {
12597366Sdes	size_t	xt_size;		/* Structure size */
12697366Sdes	long	xt_rawcc;		/* Raw input queue statistics. */
12797366Sdes	long	xt_cancc;		/* Canonical queue statistics. */
12897366Sdes	long	xt_outcc;		/* Output queue statistics. */
12997366Sdes	int	xt_line;		/* Interface to device drivers. */
13097366Sdes	udev_t	xt_dev;			/* Userland (sysctl) instance. */
13197366Sdes	int	xt_state;		/* Device and driver (TS*) state. */
13297366Sdes	int	xt_flags;		/* Tty flags. */
13397366Sdes	int     xt_timeout;		/* Timeout for ttywait() */
13497366Sdes	pid_t	xt_pgid;		/* Process group ID */
13597366Sdes	pid_t	xt_sid;			/* Session ID */
13697366Sdes	struct	termios xt_termios;	/* Termios state. */
13797366Sdes	struct	winsize xt_winsize;	/* Window size. */
13897366Sdes	int	xt_column;		/* Tty output column. */
13997366Sdes	int	xt_rocount, xt_rocol;	/* Tty. */
14097366Sdes	int	xt_ififosize;		/* Total size of upstream fifos. */
14197366Sdes	int	xt_ihiwat;		/* High water mark for input. */
14297366Sdes	int	xt_ilowat;		/* Low water mark for input. */
14397366Sdes	speed_t	xt_ispeedwat;		/* t_ispeed override for watermarks. */
14497366Sdes	int	xt_ohiwat;		/* High water mark for output. */
14597366Sdes	int	xt_olowat;		/* Low water mark for output. */
14697366Sdes	speed_t	xt_ospeedwat;		/* t_ospeed override for watermarks. */
14797366Sdes};
14897366Sdes
14997366Sdes/*
1507850Sbde * User data unfortunately has to be copied through buffers on the way to
1517850Sbde * and from clists.  The buffers are on the stack so their sizes must be
1527850Sbde * fairly small.
1537850Sbde */
1547850Sbde#define	IBUFSIZ	384			/* Should be >= max value of MIN. */
1551541Srgrimes#define	OBUFSIZ	100
1567850Sbde
1579543Sjoerg#ifndef TTYHOG
1581541Srgrimes#define	TTYHOG	1024
1599543Sjoerg#endif
1601541Srgrimes
16155205Speter#ifdef _KERNEL
1621541Srgrimes#define	TTMAXHIWAT	roundup(2048, CBSIZE)
1631541Srgrimes#define	TTMINHIWAT	roundup(100, CBSIZE)
1641541Srgrimes#define	TTMAXLOWAT	256
1651541Srgrimes#define	TTMINLOWAT	32
1661541Srgrimes#endif
1671541Srgrimes
1681541Srgrimes/* These flags are kept in t_state. */
1699790Sbde#define	TS_SO_OLOWAT	0x00001		/* Wake up when output <= low water. */
1701541Srgrimes#define	TS_ASYNC	0x00002		/* Tty in async I/O mode. */
1711541Srgrimes#define	TS_BUSY		0x00004		/* Draining output. */
1721541Srgrimes#define	TS_CARR_ON	0x00008		/* Carrier is present. */
1731541Srgrimes#define	TS_FLUSH	0x00010		/* Outq has been flushed during DMA. */
1741541Srgrimes#define	TS_ISOPEN	0x00020		/* Open has completed. */
1751541Srgrimes#define	TS_TBLOCK	0x00040		/* Further input blocked. */
1761541Srgrimes#define	TS_TIMEOUT	0x00080		/* Wait for output char processing. */
1771541Srgrimes#define	TS_TTSTOP	0x00100		/* Output paused. */
1789823Sbde#ifdef notyet
1791541Srgrimes#define	TS_WOPEN	0x00200		/* Open in progress. */
1809623Sbde#endif
1811541Srgrimes#define	TS_XCLUDE	0x00400		/* Tty requires exclusivity. */
1821541Srgrimes
1831541Srgrimes/* State for intra-line fancy editing work. */
1841541Srgrimes#define	TS_BKSL		0x00800		/* State for lowercase \ work. */
1851541Srgrimes#define	TS_CNTTB	0x01000		/* Counting tab width, ignore FLUSHO. */
1861541Srgrimes#define	TS_ERASE	0x02000		/* Within a \.../ for PRTRUB. */
1871541Srgrimes#define	TS_LNCH		0x04000		/* Next character is literal. */
1881541Srgrimes#define	TS_TYPEN	0x08000		/* Retyping suspended input (PENDIN). */
1891541Srgrimes#define	TS_LOCAL	(TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
1901541Srgrimes
1919790Sbde/* Extras. */
1929823Sbde#define	TS_CAN_BYPASS_L_RINT 0x010000	/* Device in "raw" mode. */
1939824Sbde#define	TS_CONNECTED	0x020000	/* Connection open. */
1949823Sbde#define	TS_SNOOP	0x040000	/* Device is being snooped on. */
1959790Sbde#define	TS_SO_OCOMPLETE	0x080000	/* Wake up when output completes. */
1969824Sbde#define	TS_ZOMBIE	0x100000	/* Connection lost. */
1976391Sugen
1989832Sbde/* Hardware flow-control-invoked bits. */
1999832Sbde#define	TS_CAR_OFLOW	0x200000	/* For MDMBUF (XXX handle in driver). */
2009832Sbde#ifdef notyet
2019832Sbde#define	TS_CTS_OFLOW	0x400000	/* For CCTS_OFLOW. */
2029832Sbde#define	TS_DSR_OFLOW	0x800000	/* For CDSR_OFLOW. */
2039832Sbde#endif
2049832Sbde
2051541Srgrimes/* Character type information. */
2061541Srgrimes#define	ORDINARY	0
2071541Srgrimes#define	CONTROL		1
2081541Srgrimes#define	BACKSPACE	2
2091541Srgrimes#define	NEWLINE		3
2101541Srgrimes#define	TAB		4
2111541Srgrimes#define	VTAB		5
2121541Srgrimes#define	RETURN		6
2131541Srgrimes
2141541Srgrimesstruct speedtab {
2151541Srgrimes	int sp_speed;			/* Speed. */
2161541Srgrimes	int sp_code;			/* Code. */
2171541Srgrimes};
2181541Srgrimes
2191541Srgrimes/* Modem control commands (driver). */
2201541Srgrimes#define	DMSET		0
2211541Srgrimes#define	DMBIS		1
2221541Srgrimes#define	DMBIC		2
2231541Srgrimes#define	DMGET		3
2241541Srgrimes
2251541Srgrimes/* Flags on a character passed to ttyinput. */
2261541Srgrimes#define	TTY_CHARMASK	0x000000ff	/* Character mask */
2271541Srgrimes#define	TTY_QUOTE	0x00000100	/* Character quoted */
2281541Srgrimes#define	TTY_ERRORMASK	0xff000000	/* Error mask */
2297467Sache#define	TTY_FE		0x01000000	/* Framing error */
2301541Srgrimes#define	TTY_PE		0x02000000	/* Parity error */
2317467Sache#define	TTY_OE		0x04000000	/* Overrun error */
2327467Sache#define	TTY_BI		0x08000000	/* Break condition */
2331541Srgrimes
2341541Srgrimes/* Is tp controlling terminal for p? */
2351541Srgrimes#define	isctty(p, tp)							\
2361541Srgrimes	((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
2371541Srgrimes
2381541Srgrimes/* Is p in background of tp? */
2391541Srgrimes#define	isbackground(p, tp)						\
2401541Srgrimes	(isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
2411541Srgrimes
2429639Sbde/* Unique sleep addresses. */
2439824Sbde#define	TSA_CARR_ON(tp)		((void *)&(tp)->t_rawq)
2449824Sbde#define	TSA_HUP_OR_INPUT(tp)	((void *)&(tp)->t_rawq.c_cf)
2459790Sbde#define	TSA_OCOMPLETE(tp)	((void *)&(tp)->t_outq.c_cl)
2469790Sbde#define	TSA_OLOWAT(tp)		((void *)&(tp)->t_outq)
2479639Sbde#define	TSA_PTC_READ(tp)	((void *)&(tp)->t_outq.c_cf)
2489639Sbde#define	TSA_PTC_WRITE(tp)	((void *)&(tp)->t_rawq.c_cl)
2499639Sbde#define	TSA_PTS_READ(tp)	((void *)&(tp)->t_canq)
2509639Sbde
25155205Speter#ifdef _KERNEL
25230354Sphk#ifdef MALLOC_DECLARE
25330354SphkMALLOC_DECLARE(M_TTYS);
25430354Sphk#endif
2557090Sbdeextern	struct tty *constty;	/* Temporary virtual console. */
2561541Srgrimes
25792719Salfredint	 b_to_q(char *cp, int cc, struct clist *q);
25892719Salfredvoid	 catq(struct clist *from, struct clist *to);
25992719Salfredvoid	 clist_alloc_cblocks(struct clist *q, int ccmax, int ccres);
26092719Salfredvoid	 clist_free_cblocks(struct clist *q);
26192719Salfredint	 getc(struct clist *q);
26292719Salfredvoid	 ndflush(struct clist *q, int cc);
26392719Salfredchar	*nextc(struct clist *q, char *cp, int *c);
26492719Salfredvoid	 nottystop(struct tty *tp, int rw);
26592719Salfredint	 putc(int c, struct clist *q);
26692719Salfredint	 q_to_b(struct clist *q, char *cp, int cc);
26792719Salfredvoid	 termioschars(struct termios *t);
26892719Salfredint	 tputchar(int c, struct tty *tp);
26992719Salfredint	 ttcompat(struct tty *tp, u_long com, caddr_t data, int flag);
27092719Salfredint	 ttioctl(struct tty *tp, u_long com, void *data, int flag);
27192719Salfredint	 ttread(struct tty *tp, struct uio *uio, int flag);
27292719Salfredvoid	 ttrstrt(void *tp);
27392719Salfredint	 ttsetcompat(struct tty *tp, u_long *com, caddr_t data,
27492719Salfred	    struct termios *term);
27592719Salfredvoid	 ttsetwater(struct tty *tp);
27692719Salfredint	 ttspeedtab(int speed, struct speedtab *table);
27792719Salfredint	 ttstart(struct tty *tp);
27892719Salfredvoid	 ttwakeup(struct tty *tp);
27992719Salfredint	 ttwrite(struct tty *tp, struct uio *uio, int flag);
28092719Salfredvoid	 ttwwakeup(struct tty *tp);
28192719Salfredvoid	 ttyblock(struct tty *tp);
28292719Salfredvoid	 ttychars(struct tty *tp);
28392719Salfredint	 ttycheckoutq(struct tty *tp, int wait);
28492719Salfredint	 ttyclose(struct tty *tp);
28592719Salfredvoid	 ttyflush(struct tty *tp, int rw);
28692719Salfredvoid	 ttyfree(struct tty *tp);
28792719Salfredvoid	 ttyinfo(struct tty *tp);
28892719Salfredint	 ttyinput(int c, struct tty *tp);
28992719Salfredint	 ttykqfilter(dev_t dev, struct knote *kn);
29092719Salfredint	 ttylclose(struct tty *tp, int flag);
29192719Salfredstruct tty *ttymalloc(struct tty *tp);
29292719Salfredint	 ttymodem(struct tty *tp, int flag);
29392719Salfredint	 ttyopen(dev_t device, struct tty *tp);
29492719Salfredint	 ttypoll(dev_t dev, int events, struct thread *td);
29592719Salfredint	 ttyread(dev_t dev, struct uio *uio, int flag);
29692719Salfredvoid	 ttyregister(struct tty *tp);
29793258Sbdeint	 ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo);
29892719Salfredint	 ttywait(struct tty *tp);
29992719Salfredint	 ttywrite(dev_t dev, struct uio *uio, int flag);
30092719Salfredint	 unputc(struct clist *q);
3017430Sbde
30255205Speter#endif /* _KERNEL */
3032165Spaul
3044825Sbde#endif /* !_SYS_TTY_H_ */
305