tty.h revision 135377
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 135377 2004-09-17 11:39:56Z 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 68131387Smarcelstruct tty; 69135368Sphkstruct pps_state; 70135377Sphkstruct cdev; 71131387Smarcel 72135368Sphktypedef int t_open_t(struct tty *, struct cdev *); 73135368Sphktypedef void t_close_t(struct tty *); 74131373Sphktypedef void t_oproc_t(struct tty *); 75131373Sphktypedef void t_stop_t(struct tty *, int); 76131373Sphktypedef int t_param_t(struct tty *, struct termios *); 77131373Sphktypedef int t_modem_t(struct tty *, int, int); 78131373Sphktypedef void t_break_t(struct tty *, int); 79131373Sphktypedef int t_ioctl_t(struct tty *, u_long cmd, void * data, 80131373Sphk int fflag, struct thread *td); 81135368Sphk/* XXX: same as d_ioctl_t in sys/conf.h to avoid #include polution */ 82135368Sphktypedef int __d_ioctl_t(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td); 83131373Sphk 84135368Sphk 85135368Sphk 861541Srgrimes/* 871541Srgrimes * Per-tty structure. 881541Srgrimes * 891541Srgrimes * Should be split in two, into device and tty drivers. 901541Srgrimes * Glue could be masks of what to echo and circular buffer 911541Srgrimes * (low, high, timeout). 921541Srgrimes */ 931541Srgrimesstruct tty { 941541Srgrimes struct clist t_rawq; /* Device raw input queue. */ 951541Srgrimes long t_rawcc; /* Raw input queue statistics. */ 961541Srgrimes struct clist t_canq; /* Device canonical queue. */ 971541Srgrimes long t_cancc; /* Canonical queue statistics. */ 981541Srgrimes struct clist t_outq; /* Device output queue. */ 991541Srgrimes long t_outcc; /* Output queue statistics. */ 10022877Sbde int t_line; /* Interface to device drivers. */ 101130966Sphk struct cdev *t_dev; /* Device. */ 1021541Srgrimes int t_state; /* Device and driver (TS*) state. */ 1031541Srgrimes int t_flags; /* Tty flags. */ 1045396Sache int t_timeout; /* Timeout for ttywait() */ 1051541Srgrimes struct pgrp *t_pgrp; /* Foreground process group. */ 1061541Srgrimes struct session *t_session; /* Enclosing session. */ 10741087Struckman struct sigio *t_sigio; /* Information for async I/O. */ 1081541Srgrimes struct selinfo t_rsel; /* Tty read/oob select. */ 1091541Srgrimes struct selinfo t_wsel; /* Tty write select. */ 1101541Srgrimes struct termios t_termios; /* Termios state. */ 111135297Sphk struct termios t_init_in; /* ... init ingoing */ 112135297Sphk struct termios t_init_out; /* ... outgoing */ 113135297Sphk struct termios t_lock_in; /* ... lock ingoing */ 114135297Sphk struct termios t_lock_out; /* ... outgoing */ 1151541Srgrimes struct winsize t_winsize; /* Window size. */ 116131373Sphk void *t_sc; /* driver private softc pointer. */ 1179833Sbde int t_column; /* Tty output column. */ 1189833Sbde int t_rocount, t_rocol; /* Tty. */ 11934185Sbde int t_ififosize; /* Total size of upstream fifos. */ 12034185Sbde int t_ihiwat; /* High water mark for input. */ 12134185Sbde int t_ilowat; /* Low water mark for input. */ 12234185Sbde speed_t t_ispeedwat; /* t_ispeed override for watermarks. */ 12334185Sbde int t_ohiwat; /* High water mark for output. */ 12434185Sbde int t_olowat; /* Low water mark for output. */ 12534185Sbde speed_t t_ospeedwat; /* t_ospeed override for watermarks. */ 1269833Sbde int t_gen; /* Generation number. */ 127130261Sphk TAILQ_ENTRY(tty) t_list; /* Global chain of ttys for pstat(8) */ 128135368Sphk int t_actout; /* Outbound device open */ 129135368Sphk int t_wopeners; /* #threads waiting for DCD in open */ 130130261Sphk 131130261Sphk struct mtx t_mtx; 132130261Sphk int t_refcnt; 133131130Sphk int t_hotchar; /* linedisc preferred hot char */ 134131981Sphk int t_dtr_wait; /* Inter-session DTR holddown [hz] */ 135135368Sphk int t_do_timestamp; /* flag instead ? */ 136135368Sphk struct timeval t_timestamp; /* char timestamp */ 137135368Sphk struct pps_state *t_pps; /* PPS-API stuff */ 138131373Sphk 139131373Sphk /* Driver supplied methods */ 140131373Sphk t_oproc_t *t_oproc; /* Start output. */ 141131373Sphk t_stop_t *t_stop; /* Stop output. */ 142131373Sphk t_param_t *t_param; /* Set parameters. */ 143131373Sphk t_modem_t *t_modem; /* Set modem state (optional). */ 144131373Sphk t_break_t *t_break; /* Set break state (optional). */ 145131373Sphk t_ioctl_t *t_ioctl; /* Set ioctl handling (optional). */ 146135368Sphk t_open_t *t_open; /* First open */ 147135368Sphk t_close_t *t_close; /* Last close */ 148135368Sphk __d_ioctl_t *t_cioctl; /* Ioctl on control devices */ 1491541Srgrimes}; 1501541Srgrimes 1511541Srgrimes#define t_cc t_termios.c_cc 1521541Srgrimes#define t_cflag t_termios.c_cflag 1531541Srgrimes#define t_iflag t_termios.c_iflag 1541541Srgrimes#define t_ispeed t_termios.c_ispeed 1551541Srgrimes#define t_lflag t_termios.c_lflag 1561541Srgrimes#define t_min t_termios.c_min 1571541Srgrimes#define t_oflag t_termios.c_oflag 1581541Srgrimes#define t_ospeed t_termios.c_ospeed 1591541Srgrimes#define t_time t_termios.c_time 1601541Srgrimes 16172376Sjake#define TTIPRI (PSOCK + 1) /* Sleep priority for tty reads. */ 16272376Sjake#define TTOPRI (PSOCK + 2) /* Sleep priority for tty writes. */ 1631541Srgrimes 1647850Sbde/* 16597402Sbde * Userland version of struct tty, for sysctl. 16697366Sdes */ 16797366Sdesstruct xtty { 16897402Sbde size_t xt_size; /* Structure size. */ 16997366Sdes long xt_rawcc; /* Raw input queue statistics. */ 17097366Sdes long xt_cancc; /* Canonical queue statistics. */ 17197366Sdes long xt_outcc; /* Output queue statistics. */ 17297366Sdes int xt_line; /* Interface to device drivers. */ 173130640Sphk dev_t xt_dev; /* Userland (sysctl) instance. */ 17497366Sdes int xt_state; /* Device and driver (TS*) state. */ 17597366Sdes int xt_flags; /* Tty flags. */ 17697402Sbde int xt_timeout; /* Timeout for ttywait(). */ 17797402Sbde pid_t xt_pgid; /* Process group ID. */ 17897402Sbde pid_t xt_sid; /* Session ID. */ 17997366Sdes struct termios xt_termios; /* Termios state. */ 18097366Sdes struct winsize xt_winsize; /* Window size. */ 18197366Sdes int xt_column; /* Tty output column. */ 18297366Sdes int xt_rocount, xt_rocol; /* Tty. */ 18397366Sdes int xt_ififosize; /* Total size of upstream fifos. */ 18497366Sdes int xt_ihiwat; /* High water mark for input. */ 18597366Sdes int xt_ilowat; /* Low water mark for input. */ 18697366Sdes speed_t xt_ispeedwat; /* t_ispeed override for watermarks. */ 18797366Sdes int xt_ohiwat; /* High water mark for output. */ 18897366Sdes int xt_olowat; /* Low water mark for output. */ 18997366Sdes speed_t xt_ospeedwat; /* t_ospeed override for watermarks. */ 19097366Sdes}; 19197366Sdes 19297366Sdes/* 1937850Sbde * User data unfortunately has to be copied through buffers on the way to 1947850Sbde * and from clists. The buffers are on the stack so their sizes must be 1957850Sbde * fairly small. 1967850Sbde */ 1977850Sbde#define IBUFSIZ 384 /* Should be >= max value of MIN. */ 1981541Srgrimes#define OBUFSIZ 100 1997850Sbde 2009543Sjoerg#ifndef TTYHOG 201111900Sdas#define TTYHOG 8192 2029543Sjoerg#endif 2031541Srgrimes 20455205Speter#ifdef _KERNEL 2051541Srgrimes#define TTMAXHIWAT roundup(2048, CBSIZE) 2061541Srgrimes#define TTMINHIWAT roundup(100, CBSIZE) 2071541Srgrimes#define TTMAXLOWAT 256 2081541Srgrimes#define TTMINLOWAT 32 2091541Srgrimes#endif 2101541Srgrimes 2111541Srgrimes/* These flags are kept in t_state. */ 2129790Sbde#define TS_SO_OLOWAT 0x00001 /* Wake up when output <= low water. */ 2131541Srgrimes#define TS_ASYNC 0x00002 /* Tty in async I/O mode. */ 2141541Srgrimes#define TS_BUSY 0x00004 /* Draining output. */ 2151541Srgrimes#define TS_CARR_ON 0x00008 /* Carrier is present. */ 2161541Srgrimes#define TS_FLUSH 0x00010 /* Outq has been flushed during DMA. */ 2171541Srgrimes#define TS_ISOPEN 0x00020 /* Open has completed. */ 2181541Srgrimes#define TS_TBLOCK 0x00040 /* Further input blocked. */ 2191541Srgrimes#define TS_TIMEOUT 0x00080 /* Wait for output char processing. */ 2201541Srgrimes#define TS_TTSTOP 0x00100 /* Output paused. */ 2219823Sbde#ifdef notyet 2221541Srgrimes#define TS_WOPEN 0x00200 /* Open in progress. */ 2239623Sbde#endif 2241541Srgrimes#define TS_XCLUDE 0x00400 /* Tty requires exclusivity. */ 2251541Srgrimes 2261541Srgrimes/* State for intra-line fancy editing work. */ 2271541Srgrimes#define TS_BKSL 0x00800 /* State for lowercase \ work. */ 2281541Srgrimes#define TS_CNTTB 0x01000 /* Counting tab width, ignore FLUSHO. */ 2291541Srgrimes#define TS_ERASE 0x02000 /* Within a \.../ for PRTRUB. */ 2301541Srgrimes#define TS_LNCH 0x04000 /* Next character is literal. */ 2311541Srgrimes#define TS_TYPEN 0x08000 /* Retyping suspended input (PENDIN). */ 2321541Srgrimes#define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN) 2331541Srgrimes 2349790Sbde/* Extras. */ 2359823Sbde#define TS_CAN_BYPASS_L_RINT 0x010000 /* Device in "raw" mode. */ 2369824Sbde#define TS_CONNECTED 0x020000 /* Connection open. */ 2379823Sbde#define TS_SNOOP 0x040000 /* Device is being snooped on. */ 2389790Sbde#define TS_SO_OCOMPLETE 0x080000 /* Wake up when output completes. */ 2399824Sbde#define TS_ZOMBIE 0x100000 /* Connection lost. */ 2406391Sugen 2419832Sbde/* Hardware flow-control-invoked bits. */ 2429832Sbde#define TS_CAR_OFLOW 0x200000 /* For MDMBUF (XXX handle in driver). */ 2439832Sbde#ifdef notyet 2449832Sbde#define TS_CTS_OFLOW 0x400000 /* For CCTS_OFLOW. */ 2459832Sbde#define TS_DSR_OFLOW 0x800000 /* For CDSR_OFLOW. */ 2469832Sbde#endif 2479832Sbde 248131981Sphk#define TS_DTR_WAIT 0x1000000 /* DTR hold-down between sessions */ 249131981Sphk#define TS_GONE 0x2000000 /* Hardware detached */ 250131981Sphk 2511541Srgrimes/* Character type information. */ 2521541Srgrimes#define ORDINARY 0 2531541Srgrimes#define CONTROL 1 2541541Srgrimes#define BACKSPACE 2 2551541Srgrimes#define NEWLINE 3 2561541Srgrimes#define TAB 4 2571541Srgrimes#define VTAB 5 2581541Srgrimes#define RETURN 6 2591541Srgrimes 2601541Srgrimesstruct speedtab { 2611541Srgrimes int sp_speed; /* Speed. */ 2621541Srgrimes int sp_code; /* Code. */ 2631541Srgrimes}; 2641541Srgrimes 2651541Srgrimes/* Modem control commands (driver). */ 2661541Srgrimes#define DMSET 0 2671541Srgrimes#define DMBIS 1 2681541Srgrimes#define DMBIC 2 2691541Srgrimes#define DMGET 3 2701541Srgrimes 2711541Srgrimes/* Flags on a character passed to ttyinput. */ 2721541Srgrimes#define TTY_CHARMASK 0x000000ff /* Character mask */ 2731541Srgrimes#define TTY_QUOTE 0x00000100 /* Character quoted */ 2741541Srgrimes#define TTY_ERRORMASK 0xff000000 /* Error mask */ 2757467Sache#define TTY_FE 0x01000000 /* Framing error */ 2761541Srgrimes#define TTY_PE 0x02000000 /* Parity error */ 2777467Sache#define TTY_OE 0x04000000 /* Overrun error */ 2787467Sache#define TTY_BI 0x08000000 /* Break condition */ 2791541Srgrimes 2801541Srgrimes/* Is tp controlling terminal for p? */ 2811541Srgrimes#define isctty(p, tp) \ 2821541Srgrimes ((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT) 2831541Srgrimes 2841541Srgrimes/* Is p in background of tp? */ 2851541Srgrimes#define isbackground(p, tp) \ 2861541Srgrimes (isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp) 2871541Srgrimes 2889639Sbde/* Unique sleep addresses. */ 2899824Sbde#define TSA_CARR_ON(tp) ((void *)&(tp)->t_rawq) 2909824Sbde#define TSA_HUP_OR_INPUT(tp) ((void *)&(tp)->t_rawq.c_cf) 2919790Sbde#define TSA_OCOMPLETE(tp) ((void *)&(tp)->t_outq.c_cl) 2929790Sbde#define TSA_OLOWAT(tp) ((void *)&(tp)->t_outq) 2939639Sbde 29455205Speter#ifdef _KERNEL 29530354Sphk#ifdef MALLOC_DECLARE 29630354SphkMALLOC_DECLARE(M_TTYS); 29730354Sphk#endif 298135368Sphk 299135368Sphk/* Minor number flag bits */ 300135368Sphk#define MINOR_CALLOUT 0x80000000 301135368Sphk#define MINOR_INIT 0x40000000 302135368Sphk#define MINOR_LOCK 0x20000000 303135368Sphk 304135368Sphk#define ISCALLOUT(dev) (minor(dev) & MINOR_CALLOUT) 305135368Sphk#define ISINIT(dev) (minor(dev) & MINOR_INIT) 306135368Sphk#define ISLOCK(dev) (minor(dev) & MINOR_LOCK) 307135368Sphk 308116663Siedowseextern struct msgbuf consmsgbuf; /* Message buffer for constty. */ 3097090Sbdeextern struct tty *constty; /* Temporary virtual console. */ 310110997Sphkextern long tk_cancc; 311110997Sphkextern long tk_nin; 312110997Sphkextern long tk_nout; 313110997Sphkextern long tk_rawcc; 3141541Srgrimes 31592719Salfredint b_to_q(char *cp, int cc, struct clist *q); 31692719Salfredvoid catq(struct clist *from, struct clist *to); 31792719Salfredvoid clist_alloc_cblocks(struct clist *q, int ccmax, int ccres); 31892719Salfredvoid clist_free_cblocks(struct clist *q); 319116663Siedowsevoid constty_set(struct tty *tp); 320116663Siedowsevoid constty_clear(void); 32192719Salfredint getc(struct clist *q); 32292719Salfredvoid ndflush(struct clist *q, int cc); 32392719Salfredchar *nextc(struct clist *q, char *cp, int *c); 32492719Salfredvoid nottystop(struct tty *tp, int rw); 32592719Salfredint putc(int c, struct clist *q); 32692719Salfredint q_to_b(struct clist *q, char *cp, int cc); 32792719Salfredvoid termioschars(struct termios *t); 32892719Salfredint tputchar(int c, struct tty *tp); 32992719Salfredint ttcompat(struct tty *tp, u_long com, caddr_t data, int flag); 33092719Salfredint ttioctl(struct tty *tp, u_long com, void *data, int flag); 33192719Salfredint ttread(struct tty *tp, struct uio *uio, int flag); 33292719Salfredvoid ttrstrt(void *tp); 33392719Salfredint ttsetcompat(struct tty *tp, u_long *com, caddr_t data, 33492719Salfred struct termios *term); 33592719Salfredvoid ttsetwater(struct tty *tp); 33692719Salfredint ttspeedtab(int speed, struct speedtab *table); 33792719Salfredint ttstart(struct tty *tp); 33892719Salfredvoid ttwakeup(struct tty *tp); 33992719Salfredint ttwrite(struct tty *tp, struct uio *uio, int flag); 34092719Salfredvoid ttwwakeup(struct tty *tp); 341135363Sphkstruct tty *ttyalloc(void); 34292719Salfredvoid ttyblock(struct tty *tp); 34392719Salfredvoid ttychars(struct tty *tp); 34492719Salfredint ttycheckoutq(struct tty *tp, int wait); 345132226Sphkint tty_close(struct tty *tp); 346131981Sphkint ttydtrwaitsleep(struct tty *tp); 347131981Sphkvoid ttydtrwaitstart(struct tty *tp); 34892719Salfredvoid ttyflush(struct tty *tp, int rw); 349131981Sphkvoid ttygone(struct tty *tp); 35092719Salfredvoid ttyinfo(struct tty *tp); 35192719Salfredint ttyinput(int c, struct tty *tp); 35292719Salfredint ttylclose(struct tty *tp, int flag); 353131134Sphkvoid ttyldoptim(struct tty *tp); 35492719Salfredstruct tty *ttymalloc(struct tty *tp); 35592719Salfredint ttymodem(struct tty *tp, int flag); 356132226Sphkint tty_open(struct cdev *device, struct tty *tp); 357130261Sphkint ttyref(struct tty *tp); 358130261Sphkint ttyrel(struct tty *tp); 35993258Sbdeint ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo); 36092719Salfredint ttywait(struct tty *tp); 36192719Salfredint unputc(struct clist *q); 3627430Sbde 363130075Sphk/* 364130075Sphk * XXX: temporary 365130075Sphk */ 366130075Sphk#include <sys/linedisc.h> 367130075Sphk 36855205Speter#endif /* _KERNEL */ 3692165Spaul 3704825Sbde#endif /* !_SYS_TTY_H_ */ 371