1 /*- 2 * Copyright (c) 1982, 1986, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)tty.h 8.6 (Berkeley) 1/21/94 35 * $FreeBSD: src/sys/sys/tty.h,v 1.53.2.1 2001/02/26 04:23:21 jlemon Exp $ 36 * $DragonFly: src/sys/sys/tty.h,v 1.12 2006/09/10 01:26:40 dillon Exp $ 37 */ 38 39 #ifndef _SYS_TTY_H_ 40 #define _SYS_TTY_H_ 41 42 #ifndef _SYS_TERMIOS_H_ 43 #include <sys/termios.h> 44 #endif 45 #ifndef _SYS_EVENT_H_ 46 #include <sys/event.h> 47 #endif 48 #ifdef _KERNEL 49 #include <sys/device.h> 50 #endif 51 52 /* 53 * Clists are character lists, which is a variable length linked list 54 * of cblocks, with a count of the number of characters in the list. 55 */ 56 struct clist { 57 int c_cc; /* Number of characters in the clist. */ 58 int c_ccmax; /* Max # chars for this clist. */ 59 int c_cchead; /* First char */ 60 int c_unused01; 61 short *c_data; /* linear data buffer (no longer chained) */ 62 }; 63 64 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES) 65 66 #ifndef _SYS_QUEUE_H_ 67 #include <sys/queue.h> 68 #endif 69 #ifndef _SYS_THREAD_H_ 70 #include <sys/thread.h> 71 #endif 72 73 /* 74 * Per-tty structure. 75 * 76 * Should be split in two, into device and tty drivers. 77 * Glue could be masks of what to echo and circular buffer 78 * (low, high, timeout). 79 */ 80 struct tty { 81 struct lwkt_token t_token; 82 struct clist t_rawq; /* Device raw input queue. */ 83 long t_rawcc; /* Raw input queue statistics. */ 84 struct clist t_canq; /* Device canonical queue. */ 85 long t_cancc; /* Canonical queue statistics. */ 86 struct clist t_outq; /* Device output queue. */ 87 long t_outcc; /* Output queue statistics. */ 88 int t_line; /* Interface to device drivers. */ 89 struct cdev *t_dev; /* Device. */ 90 int t_state; /* Device and driver (TS*) state. */ 91 int t_flags; /* Tty flags. */ 92 int t_timeout; /* Timeout for ttywait() */ 93 struct pgrp *t_pgrp; /* Foreground process group. */ 94 struct session *t_session; /* Enclosing session. */ 95 struct sigio *t_sigio; /* Information for async I/O. */ 96 struct kqinfo t_rkq; /* Tty read/oob kq. */ 97 struct kqinfo t_wkq; /* Tty write kq. */ 98 struct termios t_termios; /* Termios state. */ 99 struct winsize t_winsize; /* Window size. */ 100 /* Start output. */ 101 void (*t_oproc) (struct tty *); 102 /* Stop output. */ 103 void (*t_stop) (struct tty *, int); 104 /* Set hardware state. */ 105 int (*t_param) (struct tty *, struct termios *); 106 void (*t_unhold) (struct tty *); /* callback to pty after unhold */ 107 void *t_sc; /* generic driver (u4b com) */ 108 void *t_slsc; /* if_sl.c, ppp_tty.c (disc) */ 109 int t_column; /* Tty output column. */ 110 int t_rocount, t_rocol; /* Tty. */ 111 int t_ififosize; /* Total size of upstream fifos. */ 112 int t_ihiwat; /* High water mark for input. */ 113 int t_ilowat; /* Low water mark for input. */ 114 speed_t t_ispeedwat; /* t_ispeed override for watermarks. */ 115 int t_ohiwat; /* High water mark for output. */ 116 int t_olowat; /* Low water mark for output. */ 117 speed_t t_ospeedwat; /* t_ospeed override for watermarks. */ 118 int t_gen; /* Generation number. */ 119 TAILQ_ENTRY(tty) t_list; /* Global chain of ttys for pstat(8) */ 120 int t_refs; /* Reference count. */ 121 }; 122 123 #define t_cc t_termios.c_cc 124 #define t_cflag t_termios.c_cflag 125 #define t_iflag t_termios.c_iflag 126 #define t_ispeed t_termios.c_ispeed 127 #define t_lflag t_termios.c_lflag 128 #define t_min t_termios.c_min 129 #define t_oflag t_termios.c_oflag 130 #define t_ospeed t_termios.c_ospeed 131 #define t_time t_termios.c_time 132 133 #endif 134 135 /* 136 * User data unfortunately has to be copied through buffers on the way to 137 * and from clists. The buffers are on the stack so their sizes must be 138 * fairly small. 139 */ 140 #define IBUFSIZ 384 /* Should be >= max value of MIN. */ 141 #define OBUFSIZ 100 142 143 #ifndef TTYHOG 144 #define TTYHOG 1024 145 #endif 146 147 #ifdef _KERNEL 148 #define TTMAXHIWAT 2048 149 #define TTMINHIWAT 100 150 #define TTMAXLOWAT 256 151 #define TTMINLOWAT 32 152 #endif 153 154 /* These flags are kept in t_state. */ 155 #define TS_SO_OLOWAT 0x00001 /* Wake up when output <= low water. */ 156 #define TS_ASYNC 0x00002 /* Tty in async I/O mode. */ 157 #define TS_BUSY 0x00004 /* Draining output. */ 158 #define TS_CARR_ON 0x00008 /* Carrier is present. */ 159 #define TS_FLUSH 0x00010 /* Outq has been flushed during DMA. */ 160 #define TS_ISOPEN 0x00020 /* Open has completed. */ 161 #define TS_TBLOCK 0x00040 /* Further input blocked. */ 162 #define TS_TIMEOUT 0x00080 /* Wait for output char processing. */ 163 #define TS_TTSTOP 0x00100 /* Output paused. */ 164 #ifdef notyet 165 #define TS_WOPEN 0x00200 /* Open in progress. */ 166 #endif 167 #define TS_XCLUDE 0x00400 /* Tty requires exclusivity. */ 168 169 /* State for intra-line fancy editing work. */ 170 #define TS_BKSL 0x00800 /* State for lowercase \ work. */ 171 #define TS_CNTTB 0x01000 /* Counting tab width, ignore FLUSHO. */ 172 #define TS_ERASE 0x02000 /* Within a \.../ for PRTRUB. */ 173 #define TS_LNCH 0x04000 /* Next character is literal. */ 174 #define TS_TYPEN 0x08000 /* Retyping suspended input (PENDIN). */ 175 #define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN) 176 177 /* Extras. */ 178 #define TS_CAN_BYPASS_L_RINT 0x010000 /* Device in "raw" mode. */ 179 #define TS_CONNECTED 0x020000 /* Connection open. */ 180 #define TS_SNOOP 0x040000 /* Device is being snooped on. */ 181 #define TS_SO_OCOMPLETE 0x080000 /* Wake up when output completes. */ 182 #define TS_ZOMBIE 0x100000 /* Connection lost. */ 183 184 /* Hardware flow-control-invoked bits. */ 185 #define TS_CAR_OFLOW 0x200000 /* For MDMBUF (XXX handle in driver). */ 186 #ifdef notyet 187 #define TS_CTS_OFLOW 0x400000 /* For CCTS_OFLOW. */ 188 #define TS_DSR_OFLOW 0x800000 /* For CDSR_OFLOW. */ 189 #endif 190 #define TS_REGISTERED 0x1000000 /* ttyregister sanity check */ 191 #define TS_MARKER 0x2000000 /* sysctl iteration marker */ 192 193 /* Character type information. */ 194 #define ORDINARY 0 195 #define CONTROL 1 196 #define BACKSPACE 2 197 #define NEWLINE 3 198 #define TAB 4 199 #define VTAB 5 200 #define RETURN 6 201 202 struct speedtab { 203 int sp_speed; /* Speed. */ 204 int sp_code; /* Code. */ 205 }; 206 207 /* Modem control commands (driver). */ 208 #define DMSET 0 209 #define DMBIS 1 210 #define DMBIC 2 211 #define DMGET 3 212 213 /* Flags on a character passed to ttyinput. */ 214 #define TTY_CHARMASK 0x00ff /* Character mask */ 215 #define TTY_QUOTE 0x0100 /* Character quoted */ 216 #define TTY_ERRORMASK 0xff000000 /* Error mask */ 217 #define TTY_FE 0x01000000 /* Framing error */ 218 #define TTY_PE 0x02000000 /* Parity error */ 219 #define TTY_OE 0x04000000 /* Overrun error */ 220 #define TTY_BI 0x08000000 /* Break condition */ 221 222 /* Is tp controlling terminal for p? */ 223 #define isctty(p, tp) \ 224 ((p)->p_session == (tp)->t_session && ((p)->p_flags & P_CONTROLT)) 225 226 /* Is p in background of tp? */ 227 #define isbackground(p, tp) \ 228 (isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp) 229 230 /* Unique sleep addresses. */ 231 #define TSA_CARR_ON(tp) ((void *)&(tp)->t_rawq) 232 #define TSA_HUP_OR_INPUT(tp) ((void *)&(tp)->t_rawq.c_cchead) 233 #define TSA_OCOMPLETE(tp) ((void *)&(tp)->t_outq.c_ccmax) 234 #define TSA_OLOWAT(tp) ((void *)&(tp)->t_outq) 235 #define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cchead) 236 #define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_ccmax) 237 #define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq) 238 239 #ifdef _KERNEL 240 241 #ifndef _SYS_MALLOC_H_ 242 #include <sys/malloc.h> 243 #endif 244 245 MALLOC_DECLARE(M_TTYS); 246 247 extern struct tty *constty; /* Temporary virtual console. */ 248 249 int clist_btoq (char *cp, int cc, struct clist *q); 250 void clist_catq (struct clist *from, struct clist *to); 251 void clist_alloc_cblocks (struct clist *q, int ccmax); 252 void clist_free_cblocks (struct clist *q); 253 int clist_getc (struct clist *q); 254 int clist_unputc (struct clist *q); 255 void ndflush (struct clist *q, int cc); 256 void *clist_nextc (struct clist *q, void *cp, int *c); 257 void nottystop (struct tty *tp, int rw); 258 int clist_putc (int c, struct clist *q); 259 int clist_qtob (struct clist *q, char *cp, int cc); 260 void termioschars (struct termios *t); 261 int tputchar (int c, struct tty *tp); 262 int ttcompat (struct tty *tp, u_long com, caddr_t data, int flag); 263 int ttioctl (struct tty *tp, u_long com, void *data, int flag); 264 int ttread (struct tty *tp, struct uio *uio, int flag); 265 int ttsetcompat (struct tty *tp, u_long *com, caddr_t data, 266 struct termios *term); 267 void ttsetwater (struct tty *tp); 268 int ttspeedtab (int speed, struct speedtab *table); 269 int ttstart (struct tty *tp); 270 void ttwakeup (struct tty *tp); 271 int ttwrite (struct tty *tp, struct uio *uio, int flag); 272 void ttwwakeup (struct tty *tp); 273 void ttyblock (struct tty *tp); 274 void ttychars (struct tty *tp); 275 int ttycheckoutq (struct tty *tp, int wait); 276 int ttyclose (struct tty *tp); 277 void ttyclearsession (struct tty *tp); 278 void ttyclosesession (struct session *, int); 279 void ttyflush (struct tty *tp, int rw); 280 void ttyhold (struct tty *tp); 281 void ttyunhold (struct tty *tp); 282 void ttyinfo (struct tty *tp); 283 int ttyinput (int c, struct tty *tp); 284 int ttylclose (struct tty *tp, int flag); 285 struct tty *ttymalloc (struct tty **tpp); 286 int ttymodem (struct tty *tp, int flag); 287 int ttyopen (cdev_t device, struct tty *tp); 288 int ttykqfilter (struct dev_kqfilter_args *); 289 int ttyread (struct dev_read_args *); 290 void ttyregister (struct tty *tp); 291 void ttyinit (struct tty *tp); 292 void ttyunregister (struct tty *tp); 293 int ttysleep (struct tty *tp, void *chan, int slpflags, char *wmesg, 294 int timeout); 295 int ttyrevoke (struct dev_revoke_args *); 296 int ttywait (struct tty *tp); 297 int ttywrite (struct dev_write_args *); 298 299 #endif /* _KERNEL */ 300 301 #endif /* !_SYS_TTY_H_ */ 302