1 /* $OpenBSD: tty.h,v 1.22 2006/08/17 06:27:05 miod Exp $ */
2 /* $NetBSD: tty.h,v 1.30.4.1 1996/06/02 09:08:13 mrg Exp $ */
3
4 /*-
5 * Copyright (c) 1982, 1986, 1993
6 * The Regents of the University of California. All rights reserved.
7 * (c) UNIX System Laboratories, Inc.
8 * All or some portions of this file are derived from material licensed
9 * to the University of California by American Telephone and Telegraph
10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
11 * the permission of UNIX System Laboratories, Inc.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)tty.h 8.6 (Berkeley) 1/21/94
38 */
39
40 #include <sys/termios.h>
41 #include <sys/queue.h>
42 #include <sys/selinfo.h> /* For struct selinfo. */
43 #include <sys/timeout.h>
44
45 #define KERN_TTY_TKNIN 1 /* quad: input chars */
46 #define KERN_TTY_TKNOUT 2 /* quad: output chars */
47 #define KERN_TTY_TKRAWCC 3 /* quad: input chars, raw mode */
48 #define KERN_TTY_TKCANCC 4 /* quad: input char, cooked mode */
49 #define KERN_TTY_INFO 5 /* struct: tty stats */
50 #define KERN_TTY_MAXPTYS 6 /* int: max ptys */
51 #define KERN_TTY_NPTYS 7 /* int: number of allocated ptys */
52 #define KERN_TTY_MAXID 8
53
54 #define CTL_KERN_TTY_NAMES { \
55 { 0, 0 }, \
56 { "tk_nin", CTLTYPE_QUAD }, \
57 { "tk_nout", CTLTYPE_QUAD }, \
58 { "tk_rawcc", CTLTYPE_QUAD }, \
59 { "tk_cancc", CTLTYPE_QUAD }, \
60 { "ttyinfo", CTLTYPE_STRUCT }, \
61 { "maxptys", CTLTYPE_INT }, \
62 { "nptys", CTLTYPE_INT }, \
63 }
64
65 /* ptmget, for /dev/ptm pty getting ioctl PTMGET */
66
67 struct ptmget {
68 int cfd;
69 int sfd;
70 char cn[16];
71 char sn[16];
72 };
73 #define PTMGET _IOR('t', 1, struct ptmget) /* get ptys */
74 #define PATH_PTMDEV "/dev/ptm"
75 #define TTY_GID 4 /* XXX evil hardcoding of tty gid */
76
77 /*
78 * Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
79 * exactly the same behaviour as in true clists.
80 * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
81 * (but, saves memory and cpu time)
82 *
83 * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
84 */
85 struct clist {
86 int c_cc; /* count of characters in queue */
87 int c_cn; /* total ring buffer length */
88 u_char *c_cf; /* points to first character */
89 u_char *c_cl; /* points to next open character */
90 u_char *c_cs; /* start of ring buffer */
91 u_char *c_ce; /* c_ce + c_len */
92 u_char *c_cq; /* N bits/bytes long, see tty_subr.c */
93 };
94
95 /*
96 * Per-tty structure.
97 *
98 * Should be split in two, into device and tty drivers.
99 * Glue could be masks of what to echo and circular buffer
100 * (low, high, timeout).
101 */
102 struct tty {
103 TAILQ_ENTRY(tty) tty_link; /* Link in global tty list. */
104 struct clist t_rawq; /* Device raw input queue. */
105 long t_rawcc; /* Raw input queue statistics. */
106 struct clist t_canq; /* Device canonical queue. */
107 long t_cancc; /* Canonical queue statistics. */
108 struct clist t_outq; /* Device output queue. */
109 long t_outcc; /* Output queue statistics. */
110 u_char t_line; /* Interface to device drivers. */
111 dev_t t_dev; /* Device. */
112 int t_state; /* Device and driver (TS*) state. */
113 int t_flags; /* Tty flags. */
114 struct pgrp *t_pgrp; /* Foreground process group. */
115 struct session *t_session; /* Enclosing session. */
116 struct selinfo t_rsel; /* Tty read/oob select. */
117 struct selinfo t_wsel; /* Tty write select. */
118 struct termios t_termios; /* Termios state. */
119 struct winsize t_winsize; /* Window size. */
120 /* Start output. */
121 void (*t_oproc)(struct tty *);
122 /* Set hardware state. */
123 int (*t_param)(struct tty *, struct termios *);
124 /* Set hardware flow control. */
125 int (*t_hwiflow)(struct tty *tp, int flag);
126 void *t_sc; /* XXX: net/if_sl.c:sl_softc. */
127 short t_column; /* Tty output column. */
128 short t_rocount, t_rocol; /* Tty. */
129 short t_hiwat; /* High water mark. */
130 short t_lowat; /* Low water mark. */
131 short t_gen; /* Generation number. */
132 struct timeout t_rstrt_to; /* restart timeout */
133 struct timeval t_tv; /* timestamp */
134 };
135
136 /*
137 * Small version of struct tty exported via sysctl KERN_TTY_INFO
138 */
139 struct itty {
140 dev_t t_dev;
141 int t_rawq_c_cc;
142 int t_canq_c_cc;
143 int t_outq_c_cc;
144 short t_hiwat;
145 short t_lowat;
146 short t_column;
147 int t_state;
148 struct session *t_session;
149 pid_t t_pgrp_pg_id;
150 u_char t_line;
151 };
152
153 #define t_cc t_termios.c_cc
154 #define t_cflag t_termios.c_cflag
155 #define t_iflag t_termios.c_iflag
156 #define t_ispeed t_termios.c_ispeed
157 #define t_lflag t_termios.c_lflag
158 #define t_min t_termios.c_min
159 #define t_oflag t_termios.c_oflag
160 #define t_ospeed t_termios.c_ospeed
161 #define t_time t_termios.c_time
162
163 #define TTIPRI 25 /* Sleep priority for tty reads. */
164 #define TTOPRI 26 /* Sleep priority for tty writes. */
165
166 #define TTMASK 15
167 #define OBUFSIZ 512
168 #define TTYHOG 1024
169
170 #ifdef _KERNEL
171 #define TTMAXHIWAT roundup(2048, CBSIZE)
172 #define TTMINHIWAT roundup(100, CBSIZE)
173 #define TTMAXLOWAT 256
174 #define TTMINLOWAT 32
175 #endif
176
177 /* These flags are kept in t_state. */
178 #define TS_ASLEEP 0x00001 /* Process waiting for tty. */
179 #define TS_ASYNC 0x00002 /* Tty in async I/O mode. */
180 #define TS_BUSY 0x00004 /* Draining output. */
181 #define TS_CARR_ON 0x00008 /* Carrier is present. */
182 #define TS_FLUSH 0x00010 /* Outq has been flushed during DMA. */
183 #define TS_ISOPEN 0x00020 /* Open has completed. */
184 #define TS_TBLOCK 0x00040 /* Further input blocked. */
185 #define TS_TIMEOUT 0x00080 /* Wait for output char processing. */
186 #define TS_TTSTOP 0x00100 /* Output paused. */
187 #define TS_WOPEN 0x00200 /* Open in progress. */
188 #define TS_XCLUDE 0x00400 /* Tty requires exclusivity. */
189
190 /* State for intra-line fancy editing work. */
191 #define TS_BKSL 0x00800 /* State for lowercase \ work. */
192 #define TS_CNTTB 0x01000 /* Counting tab width, ignore FLUSHO. */
193 #define TS_ERASE 0x02000 /* Within a \.../ for PRTRUB. */
194 #define TS_LNCH 0x04000 /* Next character is literal. */
195 #define TS_TYPEN 0x08000 /* Retyping suspended input (PENDIN). */
196 #define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
197
198 #define TS_TSTAMPDCDSET 0x10000 /* update timestamp on DCD set */
199 #define TS_TSTAMPDCDCLR 0x20000 /* update timestamp on DCD clr */
200 #define TS_TSTAMPCTSSET 0x40000 /* update timestamp on CTS set */
201 #define TS_TSTAMPCTSCLR 0x80000 /* update timestamp on CTS clr */
202
203 /* Character type information. */
204 #define ORDINARY 0
205 #define CONTROL 1
206 #define BACKSPACE 2
207 #define NEWLINE 3
208 #define TAB 4
209 #define VTAB 5
210 #define RETURN 6
211
212 struct speedtab {
213 int sp_speed; /* Speed. */
214 int sp_code; /* Code. */
215 };
216
217 /* Modem control commands (driver). */
218 #define DMSET 0
219 #define DMBIS 1
220 #define DMBIC 2
221 #define DMGET 3
222
223 /* Flags on a character passed to ttyinput. */
224 #define TTY_CHARMASK 0x000000ff /* Character mask */
225 #define TTY_QUOTE 0x00000100 /* Character quoted */
226 #define TTY_ERRORMASK 0xff000000 /* Error mask */
227 #define TTY_FE 0x01000000 /* Framing error or BREAK condition */
228 #define TTY_PE 0x02000000 /* Parity error */
229
230 /* Is tp controlling terminal for p? */
231 #define isctty(p, tp) \
232 ((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
233
234 /* Is p in background of tp? */
235 #define isbackground(p, tp) \
236 (isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
237
238 /*
239 * ttylist_head is defined here so that user-land has access to it.
240 */
241 TAILQ_HEAD(ttylist_head, tty); /* the ttylist is a TAILQ */
242
243 #ifdef _KERNEL
244
245 extern int tty_count; /* number of ttys in global ttylist */
246 extern struct ttychars ttydefaults;
247
248 /* Symbolic sleep message strings. */
249 extern char ttyin[], ttyout[], ttopen[], ttclos[], ttybg[], ttybuf[];
250
251 int sysctl_tty(int *, u_int, void *, size_t *, void *, size_t);
252 int sysctl_pty(int *, u_int, void *, size_t *, void *, size_t);
253
254 int b_to_q(u_char *cp, int cc, struct clist *q);
255 void catq(struct clist *from, struct clist *to);
256 void clist_init(void);
257 int getc(struct clist *q);
258 void ndflush(struct clist *q, int cc);
259 int ndqb(struct clist *q, int flag);
260 u_char *nextc(struct clist *q, u_char *cp, int *c);
261 int putc(int c, struct clist *q);
262 int q_to_b(struct clist *q, u_char *cp, int cc);
263 int unputc(struct clist *q);
264
265 int nullmodem(struct tty *tp, int flag);
266 int tputchar(int c, struct tty *tp);
267 int ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
268 struct proc *p);
269 int ttread(struct tty *tp, struct uio *uio, int flag);
270 void ttrstrt(void *tp);
271 int ttpoll(dev_t device, int events, struct proc *p);
272 int ttkqfilter(dev_t dev, struct knote *kn);
273 void ttsetwater(struct tty *tp);
274 int ttspeedtab(int speed, const struct speedtab *table);
275 int ttstart(struct tty *tp);
276 void ttwakeup(struct tty *tp);
277 int ttwrite(struct tty *tp, struct uio *uio, int flag);
278 void ttychars(struct tty *tp);
279 int ttycheckoutq(struct tty *tp, int wait);
280 int ttyclose(struct tty *tp);
281 void ttyflush(struct tty *tp, int rw);
282 void ttyinfo(struct tty *tp);
283 int ttyinput(int c, struct tty *tp);
284 int ttylclose(struct tty *tp, int flag);
285 int ttymodem(struct tty *tp, int flag);
286 int ttyopen(dev_t device, struct tty *tp);
287 int ttyoutput(int c, struct tty *tp);
288 void ttypend(struct tty *tp);
289 void ttyretype(struct tty *tp);
290 void ttyrub(int c, struct tty *tp);
291 int ttysleep(struct tty *tp,
292 void *chan, int pri, char *wmesg, int timeout);
293 int ttywait(struct tty *tp);
294 int ttywflush(struct tty *tp);
295 void ttytstamp(struct tty *tp, int octs, int ncts, int odcd, int ndcd);
296
297 void tty_init(void);
298 struct tty *ttymalloc(void);
299 void ttyfree(struct tty *);
300 u_char *firstc(struct clist *clp, int *c);
301
302 int cttyopen(dev_t, int, int, struct proc *);
303 int cttyread(dev_t, struct uio *, int);
304 int cttywrite(dev_t, struct uio *, int);
305 int cttyioctl(dev_t, u_long, caddr_t, int, struct proc *);
306 int cttypoll(dev_t, int, struct proc *);
307
308 int clalloc(struct clist *, int, int);
309 void clfree(struct clist *);
310
311 #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_SVR4) || \
312 defined(COMPAT_FREEBSD) || defined(COMPAT_OSF1)
313 # define COMPAT_OLDTTY
314 int ttcompat(struct tty *, u_long, caddr_t, int, struct proc *);
315 #endif
316
317 #endif