]> git.saurik.com Git - apple/xnu.git/blob - bsd/sys/tty.h
5d8db38bcf57fca9bf48a59b83fcfdf5473f46e5
[apple/xnu.git] / bsd / sys / tty.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
31 /*-
32 * Copyright (c) 1982, 1986, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * SUCH DAMAGE.
67 *
68 * @(#)tty.h 8.6 (Berkeley) 1/21/94
69 */
70
71 #ifndef _SYS_TTY_H_
72 #define _SYS_TTY_H_
73
74 #include <sys/appleapiopts.h>
75 #include <sys/cdefs.h>
76 #include <sys/termios.h>
77 #include <sys/select.h> /* For struct selinfo. */
78
79
80 #ifdef KERNEL
81 /*
82 * NetBSD Clists are actually ring buffers. The c_cc, c_cf, c_cl fields have
83 * exactly the same behaviour as in true clists.
84 * if c_cq is NULL, the ring buffer has no TTY_QUOTE functionality
85 * (but, saves memory and cpu time)
86 *
87 * *DON'T* play with c_cs, c_ce, c_cq, or c_cl outside tty_subr.c!!!
88 */
89 struct clist {
90 int c_cc; /* count of characters in queue */
91 int c_cn; /* total ring buffer length */
92 u_char *c_cf; /* points to first character */
93 u_char *c_cl; /* points to next open character */
94 u_char *c_cs; /* start of ring buffer */
95 u_char *c_ce; /* c_ce + c_len */
96 u_char *c_cq; /* N bits/bytes long, see tty_subr.c */
97 };
98
99 #ifndef TTYCLSIZE
100 #define TTYCLSIZE 1024
101 #endif
102
103
104 /*
105 * Per-tty structure.
106 *
107 * Should be split in two, into device and tty drivers.
108 * Glue could be masks of what to echo and circular buffer
109 * (low, high, timeout).
110 */
111 struct tty {
112 struct clist t_rawq; /* Device raw input queue. */
113 long t_rawcc; /* Raw input queue statistics. */
114 struct clist t_canq; /* Device canonical queue. */
115 long t_cancc; /* Canonical queue statistics. */
116 struct clist t_outq; /* Device output queue. */
117 long t_outcc; /* Output queue statistics. */
118 int t_line; /* Interface to device drivers. */
119 dev_t t_dev; /* Device. */
120 int t_state; /* Device and driver (TS*) state. */
121 int t_flags; /* Tty flags. */
122 int t_timeout; /* Timeout for ttywait() */
123 struct pgrp *t_pgrp; /* Foreground process group. */
124 struct session *t_session; /* Enclosing session. */
125 struct selinfo t_rsel; /* Tty read/oob select. */
126 struct selinfo t_wsel; /* Tty write select. */
127 struct termios t_termios; /* Termios state. */
128 struct winsize t_winsize; /* Window size. */
129 /* Start output. */
130 void (*t_oproc)(struct tty *);
131 /* Stop output. */
132 void (*t_stop)(struct tty *, int);
133 /* Set hardware state. */
134 int (*t_param)(struct tty *, struct termios *);
135 void *t_sc; /* XXX: net/if_sl.c:sl_softc. */
136 int t_column; /* Tty output column. */
137 int t_rocount, t_rocol; /* Tty. */
138 int t_hiwat; /* High water mark. */
139 int t_lowat; /* Low water mark. */
140 int t_gen; /* Generation number. */
141 };
142
143 #define t_cc t_termios.c_cc
144 #define t_cflag t_termios.c_cflag
145 #define t_iflag t_termios.c_iflag
146 #define t_ispeed t_termios.c_ispeed
147 #define t_lflag t_termios.c_lflag
148 #define t_min t_termios.c_min
149 #define t_oflag t_termios.c_oflag
150 #define t_ospeed t_termios.c_ospeed
151 #define t_time t_termios.c_time
152
153
154 #define TTIPRI 25 /* Sleep priority for tty reads. */
155 #define TTOPRI 26 /* Sleep priority for tty writes. */
156
157 /*
158 * User data unfortunately has to be copied through buffers on the way to
159 * and from clists. The buffers are on the stack so their sizes must be
160 * fairly small.
161 */
162 #define IBUFSIZ 384 /* Should be >= max value of MIN. */
163 #define OBUFSIZ 100
164
165 #ifndef TTYHOG
166 #define TTYHOG 1024
167 #endif
168
169 #define TTMAXHIWAT roundup(2048, CBSIZE)
170 #define TTMINHIWAT roundup(100, CBSIZE)
171 #define TTMAXLOWAT 256
172 #define TTMINLOWAT 32
173 #else
174 struct tty;
175 struct clist;
176 #endif /* KERNEL */
177
178 /* These flags are kept in t_state. */
179 #define TS_SO_OLOWAT 0x00001 /* Wake up when output <= low water. */
180 #define TS_ASYNC 0x00002 /* Tty in async I/O mode. */
181 #define TS_BUSY 0x00004 /* Draining output. */
182 #define TS_CARR_ON 0x00008 /* Carrier is present. */
183 #define TS_FLUSH 0x00010 /* Outq has been flushed during DMA. */
184 #define TS_ISOPEN 0x00020 /* Open has completed. */
185 #define TS_TBLOCK 0x00040 /* Further input blocked. */
186 #define TS_TIMEOUT 0x00080 /* Wait for output char processing. */
187 #define TS_TTSTOP 0x00100 /* Output paused. */
188 #ifdef notyet
189 #define TS_WOPEN 0x00200 /* Open in progress. */
190 #endif
191 #define TS_XCLUDE 0x00400 /* Tty requires exclusivity. */
192
193 /* State for intra-line fancy editing work. */
194 #define TS_BKSL 0x00800 /* State for lowercase \ work. */
195 #define TS_CNTTB 0x01000 /* Counting tab width, ignore FLUSHO. */
196 #define TS_ERASE 0x02000 /* Within a \.../ for PRTRUB. */
197 #define TS_LNCH 0x04000 /* Next character is literal. */
198 #define TS_TYPEN 0x08000 /* Retyping suspended input (PENDIN). */
199 #define TS_LOCAL (TS_BKSL | TS_CNTTB | TS_ERASE | TS_LNCH | TS_TYPEN)
200
201 /* Extras. */
202 #define TS_CAN_BYPASS_L_RINT 0x010000 /* Device in "raw" mode. */
203 #define TS_CONNECTED 0x020000 /* Connection open. */
204 #define TS_SNOOP 0x040000 /* Device is being snooped on. */
205 #define TS_SO_OCOMPLETE 0x080000 /* Wake up when output completes. */
206 #define TS_ZOMBIE 0x100000 /* Connection lost. */
207
208 /* Hardware flow-control-invoked bits. */
209 #define TS_CAR_OFLOW 0x200000 /* For MDMBUF (XXX handle in driver). */
210 #ifdef notyet
211 #define TS_CTS_OFLOW 0x400000 /* For CCTS_OFLOW. */
212 #define TS_DSR_OFLOW 0x800000 /* For CDSR_OFLOW. */
213 #endif
214
215
216 /* Character type information. */
217 #define ORDINARY 0
218 #define CONTROL 1
219 #define BACKSPACE 2
220 #define NEWLINE 3
221 #define TAB 4
222 #define VTAB 5
223 #define RETURN 6
224
225 struct speedtab {
226 int sp_speed; /* Speed. */
227 int sp_code; /* Code. */
228 };
229
230 /* Modem control commands (driver). */
231 #define DMSET 0
232 #define DMBIS 1
233 #define DMBIC 2
234 #define DMGET 3
235
236 /* Flags on a character passed to ttyinput. */
237 #define TTY_CHARMASK 0x000000ff /* Character mask */
238 #define TTY_QUOTE 0x00000100 /* Character quoted */
239 #define TTY_ERRORMASK 0xff000000 /* Error mask */
240 #define TTY_FE 0x01000000 /* Framing error */
241 #define TTY_PE 0x02000000 /* Parity error */
242 #define TTY_OE 0x04000000 /* Overrun error */
243 #define TTY_BI 0x08000000 /* Break condition */
244
245 #ifdef KERNEL
246 /* Is tp controlling terminal for p? */
247 #define isctty(p, tp) \
248 ((p)->p_session == (tp)->t_session && (p)->p_flag & P_CONTROLT)
249
250 /* Is p in background of tp? */
251 #define isbackground(p, tp) \
252 (isctty((p), (tp)) && (p)->p_pgrp != (tp)->t_pgrp)
253
254 /* Unique sleep addresses. */
255 #define TSA_CARR_ON(tp) ((void *)&(tp)->t_rawq)
256 #define TSA_HUP_OR_INPUT(tp) ((void *)&(tp)->t_rawq.c_cf)
257 #define TSA_OCOMPLETE(tp) ((void *)&(tp)->t_outq.c_cl)
258 #define TSA_OLOWAT(tp) ((void *)&(tp)->t_outq)
259 #define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
260 #define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
261 #define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
262
263
264 __BEGIN_DECLS
265
266 int b_to_q(const u_char *cp, int cc, struct clist *q);
267 void catq(struct clist *from, struct clist *to);
268 void clist_init(void);
269 int getc(struct clist *q);
270 void ndflush(struct clist *q, int cc);
271 int ndqb(struct clist *q, int flag);
272 u_char *firstc (struct clist *clp, int *c);
273 u_char *nextc(struct clist *q, u_char *cp, int *c);
274 int putc(int c, struct clist *q);
275 int q_to_b(struct clist *q, u_char *cp, int cc);
276 int unputc(struct clist *q);
277 int clalloc(struct clist *clp, int size, int quot);
278 void clfree(struct clist *clp);
279 void cinit(void);
280 void clrbits(u_char *cp, int off, int len);
281
282 #ifdef KERNEL_PRIVATE
283 int ttcompat(struct tty *tp, u_long com, caddr_t data, int flag,
284 struct proc *p);
285 int ttsetcompat(struct tty *tp, u_long *com, caddr_t data, struct termios *term);
286 #endif /* KERNEL_PRIVATE */
287
288 void termioschars(struct termios *t);
289 int tputchar(int c, struct tty *tp);
290 int ttioctl(struct tty *tp, u_long com, caddr_t data, int flag,
291 struct proc *p);
292 int ttread(struct tty *tp, struct uio *uio, int flag);
293 void ttrstrt(void *tp);
294 int ttyselect(struct tty *tp, int rw, void * wql, struct proc *p);
295 int ttselect(dev_t dev, int rw, void * wql, struct proc *p);
296 void ttsetwater(struct tty *tp);
297 int ttspeedtab(int speed, struct speedtab *table);
298 int ttstart(struct tty *tp);
299 void ttwakeup(struct tty *tp);
300 int ttwrite(struct tty *tp, struct uio *uio, int flag);
301 void ttwwakeup(struct tty *tp);
302 void ttyblock(struct tty *tp);
303 void ttychars(struct tty *tp);
304 int ttycheckoutq(struct tty *tp, int wait);
305 int ttyclose(struct tty *tp);
306 void ttyflush(struct tty *tp, int rw);
307 void ttyinfo(struct tty *tp);
308 int ttyinput(int c, struct tty *tp);
309 int ttylclose(struct tty *tp, int flag);
310 int ttymodem(struct tty *tp, int flag);
311 int ttyopen(dev_t device, struct tty *tp);
312 int ttysleep(struct tty *tp,
313 void *chan, int pri, const char *wmesg, int timeout);
314 int ttywait(struct tty *tp);
315 struct tty *ttymalloc(void);
316 void ttyfree(struct tty *);
317
318 __END_DECLS
319
320 #endif /* KERNEL */
321
322
323 #endif /* !_SYS_TTY_H_ */