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