tty.h revision 181905
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 * 291541Srgrimes * @(#)tty.h 8.6 (Berkeley) 1/21/94 3050477Speter * $FreeBSD: head/sys/sys/tty.h 181905 2008-08-20 08:31:58Z ed $ 311541Srgrimes */ 321541Srgrimes 332165Spaul#ifndef _SYS_TTY_H_ 344825Sbde#define _SYS_TTY_H_ 352165Spaul 36179246Sed#include <sys/clist.h> 371541Srgrimes#include <sys/termios.h> 3859288Sjlemon#include <sys/queue.h> 3970834Swollman#include <sys/selinfo.h> 40130261Sphk#include <sys/_lock.h> 41130261Sphk#include <sys/_mutex.h> 421541Srgrimes 43131387Smarcelstruct tty; 44135368Sphkstruct pps_state; 45135377Sphkstruct cdev; 46135905Sphkstruct cdevsw; 47174646Sjeffstruct thread; 48131387Smarcel 49135368Sphktypedef int t_open_t(struct tty *, struct cdev *); 50135368Sphktypedef void t_close_t(struct tty *); 51131373Sphktypedef void t_oproc_t(struct tty *); 52135905Sphktypedef void t_purge_t(struct tty *); 53131373Sphktypedef void t_stop_t(struct tty *, int); 54131373Sphktypedef int t_param_t(struct tty *, struct termios *); 55131373Sphktypedef int t_modem_t(struct tty *, int, int); 56131373Sphktypedef void t_break_t(struct tty *, int); 57131373Sphktypedef int t_ioctl_t(struct tty *, u_long cmd, void * data, 58131373Sphk int fflag, struct thread *td); 59135368Sphk/* XXX: same as d_ioctl_t in sys/conf.h to avoid #include polution */ 60135368Sphktypedef int __d_ioctl_t(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td); 61131373Sphk 62135368Sphk 63135368Sphk 641541Srgrimes/* 651541Srgrimes * Per-tty structure. 661541Srgrimes * 671541Srgrimes * Should be split in two, into device and tty drivers. 681541Srgrimes * Glue could be masks of what to echo and circular buffer 691541Srgrimes * (low, high, timeout). 701541Srgrimes */ 711541Srgrimesstruct tty { 721541Srgrimes struct clist t_rawq; /* Device raw input queue. */ 731541Srgrimes long t_rawcc; /* Raw input queue statistics. */ 741541Srgrimes struct clist t_canq; /* Device canonical queue. */ 751541Srgrimes long t_cancc; /* Canonical queue statistics. */ 761541Srgrimes struct clist t_outq; /* Device output queue. */ 771541Srgrimes long t_outcc; /* Output queue statistics. */ 7822877Sbde int t_line; /* Interface to device drivers. */ 79130966Sphk struct cdev *t_dev; /* Device. */ 80135905Sphk struct cdev *t_mdev; /* Device. */ 81135964Sphk u_int t_devunit; /* Cdev unit number */ 821541Srgrimes int t_state; /* Device and driver (TS*) state. */ 831541Srgrimes int t_flags; /* Tty flags. */ 845396Sache int t_timeout; /* Timeout for ttywait() */ 851541Srgrimes struct pgrp *t_pgrp; /* Foreground process group. */ 861541Srgrimes struct session *t_session; /* Enclosing session. */ 8741087Struckman struct sigio *t_sigio; /* Information for async I/O. */ 881541Srgrimes struct selinfo t_rsel; /* Tty read/oob select. */ 891541Srgrimes struct selinfo t_wsel; /* Tty write select. */ 901541Srgrimes struct termios t_termios; /* Termios state. */ 91135297Sphk struct termios t_init_in; /* ... init ingoing */ 92135297Sphk struct termios t_init_out; /* ... outgoing */ 93135297Sphk struct termios t_lock_in; /* ... lock ingoing */ 94135297Sphk struct termios t_lock_out; /* ... outgoing */ 951541Srgrimes struct winsize t_winsize; /* Window size. */ 96131373Sphk void *t_sc; /* driver private softc pointer. */ 97135406Sphk void *t_lsc; /* linedisc private softc pointer. */ 989833Sbde int t_column; /* Tty output column. */ 999833Sbde int t_rocount, t_rocol; /* Tty. */ 10034185Sbde int t_ififosize; /* Total size of upstream fifos. */ 10134185Sbde int t_ihiwat; /* High water mark for input. */ 10234185Sbde int t_ilowat; /* Low water mark for input. */ 10334185Sbde speed_t t_ispeedwat; /* t_ispeed override for watermarks. */ 10434185Sbde int t_ohiwat; /* High water mark for output. */ 10534185Sbde int t_olowat; /* Low water mark for output. */ 10634185Sbde speed_t t_ospeedwat; /* t_ospeed override for watermarks. */ 1079833Sbde int t_gen; /* Generation number. */ 108130261Sphk TAILQ_ENTRY(tty) t_list; /* Global chain of ttys for pstat(8) */ 109135368Sphk int t_actout; /* Outbound device open */ 110135368Sphk int t_wopeners; /* #threads waiting for DCD in open */ 111130261Sphk 112130261Sphk struct mtx t_mtx; 113130261Sphk int t_refcnt; 114131130Sphk int t_hotchar; /* linedisc preferred hot char */ 115131981Sphk int t_dtr_wait; /* Inter-session DTR holddown [hz] */ 116135368Sphk int t_do_timestamp; /* flag instead ? */ 117135368Sphk struct timeval t_timestamp; /* char timestamp */ 118135368Sphk struct pps_state *t_pps; /* PPS-API stuff */ 119131373Sphk 120131373Sphk /* Driver supplied methods */ 121131373Sphk t_oproc_t *t_oproc; /* Start output. */ 122131373Sphk t_stop_t *t_stop; /* Stop output. */ 123131373Sphk t_param_t *t_param; /* Set parameters. */ 124131373Sphk t_modem_t *t_modem; /* Set modem state (optional). */ 125131373Sphk t_break_t *t_break; /* Set break state (optional). */ 126131373Sphk t_ioctl_t *t_ioctl; /* Set ioctl handling (optional). */ 127135368Sphk t_open_t *t_open; /* First open */ 128135905Sphk t_purge_t *t_purge; /* Purge threads */ 129135368Sphk t_close_t *t_close; /* Last close */ 130135368Sphk __d_ioctl_t *t_cioctl; /* Ioctl on control devices */ 1311541Srgrimes}; 1321541Srgrimes 1331541Srgrimes#define t_cc t_termios.c_cc 1341541Srgrimes#define t_cflag t_termios.c_cflag 1351541Srgrimes#define t_iflag t_termios.c_iflag 1361541Srgrimes#define t_ispeed t_termios.c_ispeed 1371541Srgrimes#define t_lflag t_termios.c_lflag 1381541Srgrimes#define t_min t_termios.c_min 1391541Srgrimes#define t_oflag t_termios.c_oflag 1401541Srgrimes#define t_ospeed t_termios.c_ospeed 1411541Srgrimes#define t_time t_termios.c_time 1421541Srgrimes 14372376Sjake#define TTIPRI (PSOCK + 1) /* Sleep priority for tty reads. */ 14472376Sjake#define TTOPRI (PSOCK + 2) /* Sleep priority for tty writes. */ 1451541Srgrimes 1467850Sbde/* 14797402Sbde * Userland version of struct tty, for sysctl. 14897366Sdes */ 14997366Sdesstruct xtty { 15097402Sbde size_t xt_size; /* Structure size. */ 15197366Sdes long xt_rawcc; /* Raw input queue statistics. */ 15297366Sdes long xt_cancc; /* Canonical queue statistics. */ 15397366Sdes long xt_outcc; /* Output queue statistics. */ 15497366Sdes int xt_line; /* Interface to device drivers. */ 155130640Sphk dev_t xt_dev; /* Userland (sysctl) instance. */ 15697366Sdes int xt_state; /* Device and driver (TS*) state. */ 15797366Sdes int xt_flags; /* Tty flags. */ 15897402Sbde int xt_timeout; /* Timeout for ttywait(). */ 15997402Sbde pid_t xt_pgid; /* Process group ID. */ 16097402Sbde pid_t xt_sid; /* Session ID. */ 16197366Sdes struct termios xt_termios; /* Termios state. */ 16297366Sdes struct winsize xt_winsize; /* Window size. */ 16397366Sdes int xt_column; /* Tty output column. */ 16497366Sdes int xt_rocount, xt_rocol; /* Tty. */ 16597366Sdes int xt_ififosize; /* Total size of upstream fifos. */ 16697366Sdes int xt_ihiwat; /* High water mark for input. */ 16797366Sdes int xt_ilowat; /* Low water mark for input. */ 16897366Sdes speed_t xt_ispeedwat; /* t_ispeed override for watermarks. */ 16997366Sdes int xt_ohiwat; /* High water mark for output. */ 17097366Sdes int xt_olowat; /* Low water mark for output. */ 17197366Sdes speed_t xt_ospeedwat; /* t_ospeed override for watermarks. */ 17297366Sdes}; 17397366Sdes 17497366Sdes/* 1757850Sbde * User data unfortunately has to be copied through buffers on the way to 1767850Sbde * and from clists. The buffers are on the stack so their sizes must be 1777850Sbde * fairly small. 1787850Sbde */ 1797850Sbde#define IBUFSIZ 384 /* Should be >= max value of MIN. */ 1801541Srgrimes#define OBUFSIZ 100 1817850Sbde 1829543Sjoerg#ifndef TTYHOG 183111900Sdas#define TTYHOG 8192 1849543Sjoerg#endif 1851541Srgrimes 18655205Speter#ifdef _KERNEL 1871541Srgrimes#define TTMAXHIWAT roundup(2048, CBSIZE) 1881541Srgrimes#define TTMINHIWAT roundup(100, CBSIZE) 1891541Srgrimes#define TTMAXLOWAT 256 1901541Srgrimes#define TTMINLOWAT 32 1911541Srgrimes#endif 1921541Srgrimes 1931541Srgrimes/* These flags are kept in t_state. */ 1949790Sbde#define TS_SO_OLOWAT 0x00001 /* Wake up when output <= low water. */ 1951541Srgrimes#define TS_ASYNC 0x00002 /* Tty in async I/O mode. */ 1961541Srgrimes#define TS_BUSY 0x00004 /* Draining output. */ 1971541Srgrimes#define TS_CARR_ON 0x00008 /* Carrier is present. */ 1981541Srgrimes#define TS_FLUSH 0x00010 /* Outq has been flushed during DMA. */ 1991541Srgrimes#define TS_ISOPEN 0x00020 /* Open has completed. */ 2001541Srgrimes#define TS_TBLOCK 0x00040 /* Further input blocked. */ 2011541Srgrimes#define TS_TIMEOUT 0x00080 /* Wait for output char processing. */ 2021541Srgrimes#define TS_TTSTOP 0x00100 /* Output paused. */ 2039823Sbde#ifdef notyet 2041541Srgrimes#define TS_WOPEN 0x00200 /* Open in progress. */ 2059623Sbde#endif 2061541Srgrimes#define TS_XCLUDE 0x00400 /* Tty requires exclusivity. */ 2071541Srgrimes 2081541Srgrimes/* State for intra-line fancy editing work. */ 2091541Srgrimes#define TS_BKSL 0x00800 /* State for lowercase \ work. */ 2101541Srgrimes#define TS_CNTTB 0x01000 /* Counting tab width, ignore FLUSHO. */ 2111541Srgrimes#define TS_ERASE 0x02000 /* Within a \.../ for PRTRUB. */ 2121541Srgrimes#define TS_LNCH 0x04000 /* Next character is literal. */ 2131541Srgrimes#define TS_TYPEN 0x08000 /* Retyping suspended input (PENDIN). */ 2141541Srgrimes#define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN) 2151541Srgrimes 2169790Sbde/* Extras. */ 2179823Sbde#define TS_CAN_BYPASS_L_RINT 0x010000 /* Device in "raw" mode. */ 2189824Sbde#define TS_CONNECTED 0x020000 /* Connection open. */ 2199823Sbde#define TS_SNOOP 0x040000 /* Device is being snooped on. */ 2209790Sbde#define TS_SO_OCOMPLETE 0x080000 /* Wake up when output completes. */ 2219824Sbde#define TS_ZOMBIE 0x100000 /* Connection lost. */ 2226391Sugen 2239832Sbde/* Hardware flow-control-invoked bits. */ 2249832Sbde#define TS_CAR_OFLOW 0x200000 /* For MDMBUF (XXX handle in driver). */ 2259832Sbde#ifdef notyet 2269832Sbde#define TS_CTS_OFLOW 0x400000 /* For CCTS_OFLOW. */ 2279832Sbde#define TS_DSR_OFLOW 0x800000 /* For CDSR_OFLOW. */ 2289832Sbde#endif 2299832Sbde 230131981Sphk#define TS_DTR_WAIT 0x1000000 /* DTR hold-down between sessions */ 231131981Sphk#define TS_GONE 0x2000000 /* Hardware detached */ 232151383Sphk#define TS_CALLOUT 0x4000000 /* Callout devices */ 233131981Sphk 2341541Srgrimes/* Character type information. */ 2351541Srgrimes#define ORDINARY 0 2361541Srgrimes#define CONTROL 1 2371541Srgrimes#define BACKSPACE 2 2381541Srgrimes#define NEWLINE 3 2391541Srgrimes#define TAB 4 2401541Srgrimes#define VTAB 5 2411541Srgrimes#define RETURN 6 2421541Srgrimes 2431541Srgrimesstruct speedtab { 2441541Srgrimes int sp_speed; /* Speed. */ 2451541Srgrimes int sp_code; /* Code. */ 2461541Srgrimes}; 2471541Srgrimes 2481541Srgrimes/* Modem control commands (driver). */ 2491541Srgrimes#define DMSET 0 2501541Srgrimes#define DMBIS 1 2511541Srgrimes#define DMBIC 2 2521541Srgrimes#define DMGET 3 2531541Srgrimes 2541541Srgrimes/* Flags on a character passed to ttyinput. */ 2551541Srgrimes#define TTY_CHARMASK 0x000000ff /* Character mask */ 2561541Srgrimes#define TTY_QUOTE 0x00000100 /* Character quoted */ 2571541Srgrimes#define TTY_ERRORMASK 0xff000000 /* Error mask */ 2587467Sache#define TTY_FE 0x01000000 /* Framing error */ 2591541Srgrimes#define TTY_PE 0x02000000 /* Parity error */ 2607467Sache#define TTY_OE 0x04000000 /* Overrun error */ 2617467Sache#define TTY_BI 0x08000000 /* Break condition */ 2621541Srgrimes 2631541Srgrimes/* Is tp controlling terminal for p? */ 2641541Srgrimes#define isctty(p, tp) \ 2651541Srgrimes ((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT) 2661541Srgrimes 2671541Srgrimes/* Is p in background of tp? */ 2681541Srgrimes#define isbackground(p, tp) \ 2691541Srgrimes (isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp) 2701541Srgrimes 2719639Sbde/* Unique sleep addresses. */ 2729824Sbde#define TSA_CARR_ON(tp) ((void *)&(tp)->t_rawq) 2739824Sbde#define TSA_HUP_OR_INPUT(tp) ((void *)&(tp)->t_rawq.c_cf) 2749790Sbde#define TSA_OCOMPLETE(tp) ((void *)&(tp)->t_outq.c_cl) 2759790Sbde#define TSA_OLOWAT(tp) ((void *)&(tp)->t_outq) 2769639Sbde 27755205Speter#ifdef _KERNEL 27830354Sphk#ifdef MALLOC_DECLARE 27930354SphkMALLOC_DECLARE(M_TTYS); 28030354Sphk#endif 281135368Sphk 282135368Sphk/* Minor number flag bits */ 283135368Sphk#define MINOR_CALLOUT 0x80000000 284135368Sphk#define MINOR_INIT 0x40000000 285135368Sphk#define MINOR_LOCK 0x20000000 286135368Sphk 287135368Sphk#define ISCALLOUT(dev) (minor(dev) & MINOR_CALLOUT) 288135368Sphk#define ISINIT(dev) (minor(dev) & MINOR_INIT) 289135368Sphk#define ISLOCK(dev) (minor(dev) & MINOR_LOCK) 290135368Sphk 291110997Sphkextern long tk_cancc; 292110997Sphkextern long tk_nin; 293110997Sphkextern long tk_nout; 294110997Sphkextern long tk_rawcc; 2951541Srgrimes 29692719Salfredvoid nottystop(struct tty *tp, int rw); 29792719Salfredvoid termioschars(struct termios *t); 29892719Salfredint tputchar(int c, struct tty *tp); 29992719Salfredint ttcompat(struct tty *tp, u_long com, caddr_t data, int flag); 30092719Salfredint ttioctl(struct tty *tp, u_long com, void *data, int flag); 30192719Salfredint ttread(struct tty *tp, struct uio *uio, int flag); 30292719Salfredvoid ttrstrt(void *tp); 30392719Salfredvoid ttsetwater(struct tty *tp); 30492719Salfredint ttspeedtab(int speed, struct speedtab *table); 30592719Salfredint ttstart(struct tty *tp); 30692719Salfredvoid ttwakeup(struct tty *tp); 30792719Salfredint ttwrite(struct tty *tp, struct uio *uio, int flag); 30892719Salfredvoid ttwwakeup(struct tty *tp); 309135363Sphkstruct tty *ttyalloc(void); 31092719Salfredvoid ttyblock(struct tty *tp); 31192719Salfredvoid ttychars(struct tty *tp); 31292719Salfredint ttycheckoutq(struct tty *tp, int wait); 313135905Sphkvoid ttyconsolemode(struct tty *tp, int speed); 314132226Sphkint tty_close(struct tty *tp); 315151383Sphkint ttycreate(struct tty *tp, int flags, const char *fmt, ...) __printflike(3, 4); 316131981Sphkint ttydtrwaitsleep(struct tty *tp); 317131981Sphkvoid ttydtrwaitstart(struct tty *tp); 31892719Salfredvoid ttyflush(struct tty *tp, int rw); 319135905Sphkvoid ttyfree(struct tty *tp); 320131981Sphkvoid ttygone(struct tty *tp); 32192719Salfredvoid ttyinfo(struct tty *tp); 322136680Sphkvoid ttyinitmode(struct tty *tp, int echo, int speed); 32392719Salfredint ttyinput(int c, struct tty *tp); 32492719Salfredint ttylclose(struct tty *tp, int flag); 325131134Sphkvoid ttyldoptim(struct tty *tp); 32692719Salfredint ttymodem(struct tty *tp, int flag); 327132226Sphkint tty_open(struct cdev *device, struct tty *tp); 328130261Sphkint ttyref(struct tty *tp); 329130261Sphkint ttyrel(struct tty *tp); 33093258Sbdeint ttysleep(struct tty *tp, void *chan, int pri, char *wmesg, int timo); 33192719Salfredint ttywait(struct tty *tp); 3327430Sbde 333151385Sphkstatic __inline int 334151385Sphktt_open(struct tty *t, struct cdev *c) 335151385Sphk{ 336151385Sphk 337151385Sphk if (t->t_open == NULL) 338151385Sphk return (0); 339151385Sphk return (t->t_open(t, c)); 340151385Sphk} 341151385Sphk 342151385Sphkstatic __inline void 343151385Sphktt_close(struct tty *t) 344151385Sphk{ 345151385Sphk 346151385Sphk if (t->t_close != NULL) 347151385Sphk return (t->t_close(t)); 348151385Sphk} 349151385Sphk 350151385Sphkstatic __inline void 351151385Sphktt_oproc(struct tty *t) 352151385Sphk{ 353151385Sphk 354151385Sphk if (t->t_oproc != NULL) /* XXX: Kludge for pty. */ 355151385Sphk t->t_oproc(t); 356151385Sphk} 357151385Sphk 358151385Sphkstatic __inline void 359151385Sphktt_purge(struct tty *t) 360151385Sphk{ 361151385Sphk 362151385Sphk if (t->t_purge != NULL) 363151385Sphk t->t_purge(t); 364151385Sphk} 365151385Sphk 366151385Sphkstatic __inline void 367151385Sphktt_stop(struct tty *t, int i) 368151385Sphk{ 369151385Sphk 370151385Sphk t->t_stop(t, i); 371151385Sphk} 372151385Sphk 373151385Sphkstatic __inline int 374151385Sphktt_param(struct tty *t, struct termios *s) 375151385Sphk{ 376151385Sphk 377151385Sphk if (t->t_param == NULL) 378151385Sphk return (0); 379151385Sphk return (t->t_param(t, s)); 380151385Sphk} 381151385Sphk 382151385Sphkstatic __inline int 383151385Sphktt_modem(struct tty *t, int i, int j) 384151385Sphk{ 385151385Sphk 386151385Sphk if (t->t_modem == NULL) 387151385Sphk return (0); 388151385Sphk return (t->t_modem(t, i, j)); 389151385Sphk} 390151385Sphk 391151385Sphkstatic __inline int 392151385Sphktt_break(struct tty *t, int i) 393151385Sphk{ 394151385Sphk 395151385Sphk if (t->t_break == NULL) 396151385Sphk return (ENOIOCTL); 397151385Sphk t->t_break(t, i); 398151385Sphk return (0); 399151385Sphk} 400151385Sphk 401151385Sphkstatic __inline int 402151385Sphktt_ioctl(struct tty *t, u_long cmd, void *data, 403151385Sphk int fflag, struct thread *td) 404151385Sphk{ 405151385Sphk 406151385Sphk return (t->t_ioctl(t, cmd, data, fflag, td)); 407151385Sphk} 408151385Sphk 409130075Sphk/* 410130075Sphk * XXX: temporary 411130075Sphk */ 412130075Sphk#include <sys/linedisc.h> 413130075Sphk 41455205Speter#endif /* _KERNEL */ 4152165Spaul 4164825Sbde#endif /* !_SYS_TTY_H_ */ 417