1 /*        $NetBSD: tty.h,v 1.104 2023/04/12 06:35:26 riastradh Exp $  */
2 
3 /*-
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 1982, 1986, 1993
31  *        The Regents of the University of California.  All rights reserved.
32  * (c) UNIX System Laboratories, Inc.
33  * All or some portions of this file are derived from material licensed
34  * to the University of California by American Telephone and Telegraph
35  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36  * the permission of UNIX System Laboratories, Inc.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *        @(#)tty.h 8.7 (Berkeley) 1/9/95
63  */
64 
65 #ifndef _SYS_TTY_H_
66 #define _SYS_TTY_H_
67 
68 #include <sys/termios.h>
69 #include <sys/select.h>
70 #include <sys/selinfo.h>      /* For struct selinfo. */
71 #include <sys/mutex.h>
72 #include <sys/condvar.h>
73 #include <sys/queue.h>
74 #include <sys/callout.h>
75 
76 /*
77  * Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
78  * exactly the same behaviour as in true clists.
79  * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
80  * (but, saves memory and CPU time)
81  *
82  * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
83  */
84 struct clist {
85           u_char    *c_cf;              /* points to first character */
86           u_char    *c_cl;              /* points to next open character */
87           u_char    *c_cs;              /* start of ring buffer */
88           u_char    *c_ce;              /* c_ce + c_len */
89           u_char    *c_cq;              /* N bits/bytes long, see tty_subr.c */
90           int       c_cc;               /* count of characters in queue */
91           int       c_cn;               /* total ring buffer length */
92 };
93 
94 /* tty signal types */
95 enum ttysigtype {
96           TTYSIG_PG1,
97           TTYSIG_PG2,
98           TTYSIG_LEADER,
99           TTYSIG_COUNT
100 };
101 
102 /*
103  * Per-tty structure.
104  *
105  * Should be split in two, into device and tty drivers.
106  * Glue could be masks of what to echo and circular buffer
107  * (low, high, timeout).
108  */
109 struct tty {
110           TAILQ_ENTRY(tty) tty_link;    /* Link in global tty list. */
111           struct    clist t_rawq;                 /* Device raw input queue. */
112           long      t_rawcc;            /* Raw input queue statistics. */
113           kcondvar_t t_rawcv;           /* notifier */
114           kcondvar_t t_rawcvf;                    /* notifier */
115           struct    clist t_canq;                 /* Device canonical queue. */
116           long      t_cancc;            /* Canonical queue statistics. */
117           kcondvar_t t_cancv;           /* notifier */
118           kcondvar_t t_cancvf;                    /* notifier */
119           struct    clist t_outq;                 /* Device output queue. */
120           long      t_outcc;            /* Output queue statistics. */
121           kcondvar_t t_outcv;           /* notifier */
122           kcondvar_t t_outcvf;                    /* notifier */
123           callout_t t_rstrt_ch;                   /* for delayed output start */
124           struct    linesw *t_linesw;   /* Interface to device drivers. */
125           dev_t     t_dev;                        /* Device. */
126           int       t_state;            /* Device and driver (TS*) state. */
127           int       t_wopen;            /* Processes waiting for open. */
128           int       t_flags;            /* Tty flags. */
129           int       t_qsize;            /* Tty character queue size */
130           struct    pgrp *t_pgrp;                 /* Foreground process group. */
131           struct    session *t_session; /* Enclosing session. */
132           struct    selinfo t_rsel;               /* Tty read/oob select. */
133           struct    selinfo t_wsel;               /* Tty write select. */
134           struct    termios t_termios;  /* Termios state. */
135           struct    winsize t_winsize;  /* Window size. */
136                                                   /* Start output. */
137           void      (*t_oproc)(struct tty *);
138                                                   /* Set hardware state. */
139           int       (*t_param)(struct tty *, struct termios *);
140                                                   /* Set hardware flow control. */
141           int       (*t_hwiflow)(struct tty *, int);
142           void      *t_sc;                        /* XXX: net/if_sl.c:sl_softc. */
143           short     t_column;           /* Tty output column. */
144           short     t_rocount, t_rocol; /* Tty. */
145           int       t_hiwat;            /* High water mark. */
146           int       t_lowat;            /* Low water mark. */
147           short     t_gen;                        /* Generation number. */
148           sigset_t t_sigs[TTYSIG_COUNT];          /* Pending signals */
149           int       t_sigcount;                   /* # pending signals */
150           TAILQ_ENTRY(tty) t_sigqueue;  /* entry on pending signal list */
151           void      *t_softc;           /* pointer to driver's softc. */
152           volatile unsigned t_refcnt;   /* reference count for constty */
153 };
154 
155 #ifdef TTY_ALLOW_PRIVATE
156 #define   t_cc                t_termios.c_cc
157 #endif
158 #define   t_cflag             t_termios.c_cflag
159 #define   t_iflag             t_termios.c_iflag
160 #define   t_ispeed  t_termios.c_ispeed
161 #define   t_lflag             t_termios.c_lflag
162 #define   t_oflag             t_termios.c_oflag
163 #define   t_ospeed  t_termios.c_ospeed
164 
165 #define   TTIPRI    25                            /* Sleep priority for tty reads. */
166 #define   TTOPRI    26                            /* Sleep priority for tty writes. */
167 
168 #define   TTMASK    15
169 #define   OBUFSIZ   100
170 #define   TTYHOG    tp->t_qsize
171 
172 #ifdef _KERNEL
173 #define   TTMAXHIWAT          roundup(tp->t_qsize << 1, 64)
174 #define   TTMINHIWAT          roundup(tp->t_qsize >> 3, 64)
175 #define   TTMAXLOWAT          (tp->t_qsize >> 2)
176 #define   TTMINLOWAT          (tp->t_qsize >> 5)
177 #define   TTROUND             64
178 #define   TTDIALOUT_MASK      0x80000             /* dialout=524288 in MAKEDEV.tmpl */
179 #define   TTCALLUNIT_MASK     0x40000             /* XXX: compat */
180 #define   TTUNIT_MASK         0x3ffff
181 #define   TTDIALOUT(d)        (minor(d) & TTDIALOUT_MASK)
182 #define   TTCALLUNIT(d)       (minor(d) & TTCALLUNIT_MASK)
183 #define   TTUNIT(d) (minor(d) & TTUNIT_MASK)
184 #endif /* _KERNEL */
185 
186 /* These flags are kept in t_state. */
187 #define   TS_SIGINFO          0x00001             /* Ignore mask on dispatch SIGINFO */
188 #define   TS_ASYNC  0x00002             /* Tty in async I/O mode. */
189 #define   TS_BUSY             0x00004             /* Draining output. */
190 #define   TS_CARR_ON          0x00008             /* Carrier is present. */
191 #define   TS_DIALOUT          0x00010             /* Tty used for dialout. */
192 #define   TS_FLUSH  0x00020             /* Outq has been flushed during DMA. */
193 #define   TS_ISOPEN 0x00040             /* Open has completed. */
194 #define   TS_TBLOCK 0x00080             /* Further input blocked. */
195 #define   TS_TIMEOUT          0x00100             /* Wait for output char processing. */
196 #define   TS_TTSTOP 0x00200             /* Output paused. */
197 #define   TS_XCLUDE 0x00400             /* Tty requires exclusivity. */
198 
199 /* State for intra-line fancy editing work. */
200 #define   TS_BKSL             0x00800             /* State for lowercase \ work. */
201 #define   TS_CNTTB  0x01000             /* Counting tab width, ignore FLUSHO. */
202 #define   TS_ERASE  0x02000             /* Within a \.../ for PRTRUB. */
203 #define   TS_LNCH             0x04000             /* Next character is literal. */
204 #define   TS_TYPEN  0x08000             /* Retyping suspended input (PENDIN). */
205 #define   TS_LOCAL  (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
206 
207 /* for special line disciplines, like dev/sun/sunkbd.c */
208 #define   TS_KERN_ONLY        0x10000             /* Device is accessible by kernel
209                                                    * only, deny all userland access */
210 
211 #define   TS_CANCEL 0x20000             /* I/O cancelled pending close. */
212 
213 /* Character type information. */
214 #define   ORDINARY  0
215 #define   CONTROL             1
216 #define   BACKSPACE 2
217 #define   NEWLINE             3
218 #define   TAB                 4
219 #define   VTAB                5
220 #define   RETURN              6
221 
222 struct speedtab {
223           int sp_speed;                           /* Speed. */
224           int sp_code;                            /* Code. */
225 };
226 
227 /* Modem control commands (driver). */
228 #define   DMSET               0
229 #define   DMBIS               1
230 #define   DMBIC               2
231 #define   DMGET               3
232 
233 /* Flags on a character passed to ttyinput. */
234 #define   TTY_CHARMASK        0x000000ff          /* Character mask */
235 #define   TTY_QUOTE 0x00000100          /* Character quoted */
236 #define   TTY_ERRORMASK       0xff000000          /* Error mask */
237 #define   TTY_FE              0x01000000          /* Framing error or BREAK condition */
238 #define   TTY_PE              0x02000000          /* Parity error */
239 
240 /* Is tp controlling terminal for p? */
241 #define   isctty(p, tp)                                                                   \
242           ((p)->p_session == (tp)->t_session && (p)->p_lflag & PL_CONTROLT)
243 
244 /* Is p in background of tp? */
245 #define   isbackground(p, tp)                                                   \
246           (isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
247 
248 /*
249  * ttylist_head is defined here so that user-land has access to it.
250  */
251 TAILQ_HEAD(ttylist_head, tty);                    /* the ttylist is a TAILQ */
252 
253 #ifdef _KERNEL
254 
255 extern kmutex_t     tty_lock;
256 extern kmutex_t     constty_lock;
257 extern struct tty *volatile constty;
258 
259 extern    int tty_count;                          /* number of ttys in global ttylist */
260 extern    struct ttychars ttydefaults;
261 
262 /* Symbolic sleep message strings. */
263 extern     const char ttclos[];
264 
265 int        b_to_q(const u_char *, int, struct clist *);
266 void       catq(struct clist *, struct clist *);
267 void       clist_init(void);
268 int        getc(struct clist *);
269 void       ndflush(struct clist *, int);
270 int        ndqb(struct clist *, int);
271 u_char    *nextc(struct clist *, u_char *, int *);
272 int        putc(int, struct clist *);
273 int        q_to_b(struct clist *, u_char *, int);
274 int        unputc(struct clist *);
275 
276 int        nullmodem(struct tty *, int);
277 int        tputchar(int, int, struct tty *);
278 int        ttioctl(struct tty *, u_long, void *, int, struct lwp *);
279 int        ttread(struct tty *, struct uio *, int);
280 void       ttrstrt(void *);
281 int        ttpoll(struct tty *, int, struct lwp *);
282 void       ttsetwater(struct tty *);
283 int        ttspeedtab(int, const struct speedtab *);
284 int        ttstart(struct tty *);
285 void       ttwakeup(struct tty *);
286 int        ttwrite(struct tty *, struct uio *, int);
287 void       ttychars(struct tty *);
288 int        ttycheckoutq(struct tty *);
289 void       ttycancel(struct tty *);
290 int        ttyclose(struct tty *);
291 void       ttyflush(struct tty *, int);
292 void       ttygetinfo(struct tty *, int, char *, size_t);
293 void       ttyputinfo(struct tty *, char *);
294 int        ttyinput(int, struct tty *);
295 int        ttyinput_wlock(int, struct tty *); /* XXX see wsdisplay.c */
296 int        ttylclose(struct tty *, int);
297 int        ttylopen(dev_t, struct tty *);
298 int        ttykqfilter(dev_t, struct knote *);
299 int        ttymodem(struct tty *, int);
300 int        ttyopen(struct tty *, int, int);
301 int        ttyoutput(int, struct tty *);
302 void       ttypend(struct tty *);
303 void       ttyretype(struct tty *);
304 void       ttyrub(int, struct tty *);
305 int        ttysleep(struct tty *, kcondvar_t *, bool, int);
306 int        ttypause(struct tty *, int);
307 int        ttywait(struct tty *);
308 int        ttywflush(struct tty *);
309 void       ttysig(struct tty *, enum ttysigtype, int);
310 void       tty_attach(struct tty *);
311 void       tty_detach(struct tty *);
312 void       tty_init(void);
313 struct tty
314           *tty_alloc(void);
315 void       tty_free(struct tty *);
316 u_char    *firstc(struct clist *, int *);
317 bool       ttypull(struct tty *);
318 int        tty_unit(dev_t);
319 void       tty_acquire(struct tty *);
320 void       tty_release(struct tty *);
321 
322 void       ttylock(struct tty *);
323 void       ttyunlock(struct tty *);
324 bool       ttylocked(struct tty *);
325 
326 int       clalloc(struct clist *, int, int);
327 void      clfree(struct clist *);
328 
329 /* overwritten to be non-null if ptm(4) is present */
330 
331 struct ptm_pty;
332 extern struct ptm_pty *ptm;
333 
334 unsigned char tty_getctrlchar(struct tty *, unsigned /*which*/);
335 void tty_setctrlchar(struct tty *, unsigned /*which*/, unsigned char /*val*/);
336 int tty_try_xonxoff(struct tty *, unsigned char /*c*/);
337 
338 #endif /* _KERNEL */
339 
340 #endif /* !_SYS_TTY_H_ */
341