1183276Sed/*-
2183276Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3183276Sed * All rights reserved.
4183276Sed *
5183276Sed * Redistribution and use in source and binary forms, with or without
6183276Sed * modification, are permitted provided that the following conditions
7183276Sed * are met:
8183276Sed * 1. Redistributions of source code must retain the above copyright
9183276Sed *    notice, this list of conditions and the following disclaimer.
10183276Sed * 2. Redistributions in binary form must reproduce the above copyright
11183276Sed *    notice, this list of conditions and the following disclaimer in the
12183276Sed *    documentation and/or other materials provided with the distribution.
13183276Sed *
14183276Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15183276Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16183276Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17183276Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18183276Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19183276Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20183276Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21183276Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22183276Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23183276Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24183276Sed * SUCH DAMAGE.
25183276Sed *
26183276Sed * $FreeBSD$
27183276Sed */
28183276Sed
29183276Sed#ifndef _SYS_TTYHOOK_H_
30183276Sed#define	_SYS_TTYHOOK_H_
31183276Sed
32183276Sed#ifndef _SYS_TTY_H_
33183276Sed#error "can only be included through <sys/tty.h>"
34183276Sed#endif /* !_SYS_TTY_H_ */
35183276Sed
36183276Sedstruct tty;
37183276Sed
38183276Sed/*
39183276Sed * Hooks interface, which allows to capture and inject traffic into the
40183276Sed * input and output paths of a TTY.
41183276Sed */
42183276Sed
43183276Sedtypedef int th_rint_t(struct tty *tp, char c, int flags);
44183276Sedtypedef size_t th_rint_bypass_t(struct tty *tp, const void *buf, size_t len);
45183276Sedtypedef void th_rint_done_t(struct tty *tp);
46183276Sedtypedef size_t th_rint_poll_t(struct tty *tp);
47183276Sed
48183276Sedtypedef size_t th_getc_inject_t(struct tty *tp, void *buf, size_t len);
49183276Sedtypedef void th_getc_capture_t(struct tty *tp, const void *buf, size_t len);
50183276Sedtypedef size_t th_getc_poll_t(struct tty *tp);
51183276Sed
52183276Sedtypedef void th_close_t(struct tty *tp);
53183276Sed
54183276Sedstruct ttyhook {
55183276Sed	/* Character input. */
56183276Sed	th_rint_t		*th_rint;
57183276Sed	th_rint_bypass_t	*th_rint_bypass;
58183276Sed	th_rint_done_t		*th_rint_done;
59183276Sed	th_rint_poll_t		*th_rint_poll;
60183276Sed
61183276Sed	/* Character output. */
62183276Sed	th_getc_inject_t	*th_getc_inject;
63183276Sed	th_getc_capture_t	*th_getc_capture;
64183276Sed	th_getc_poll_t		*th_getc_poll;
65183276Sed
66183276Sed	th_close_t		*th_close;
67183276Sed};
68183276Sed
69186056Smavint	ttyhook_register(struct tty **, struct proc *, int,
70183276Sed    struct ttyhook *, void *);
71183276Sedvoid	ttyhook_unregister(struct tty *);
72183276Sed#define	ttyhook_softc(tp)		((tp)->t_hooksoftc)
73183276Sed#define	ttyhook_hashook(tp,hook)	((tp)->t_hook != NULL && \
74183276Sed					(tp)->t_hook->th_ ## hook != NULL)
75183276Sed
76183276Sedstatic __inline int
77183276Sedttyhook_rint(struct tty *tp, char c, int flags)
78183276Sed{
79183276Sed	tty_lock_assert(tp, MA_OWNED);
80183276Sed	MPASS(!tty_gone(tp));
81183276Sed
82183276Sed	return tp->t_hook->th_rint(tp, c, flags);
83183276Sed}
84183276Sed
85183276Sedstatic __inline size_t
86183276Sedttyhook_rint_bypass(struct tty *tp, const void *buf, size_t len)
87183276Sed{
88183276Sed	tty_lock_assert(tp, MA_OWNED);
89183276Sed	MPASS(!tty_gone(tp));
90183276Sed
91183276Sed	return tp->t_hook->th_rint_bypass(tp, buf, len);
92183276Sed}
93183276Sed
94183276Sedstatic __inline void
95183276Sedttyhook_rint_done(struct tty *tp)
96183276Sed{
97183276Sed	tty_lock_assert(tp, MA_OWNED);
98183276Sed	MPASS(!tty_gone(tp));
99183276Sed
100183276Sed	tp->t_hook->th_rint_done(tp);
101183276Sed}
102183276Sed
103183276Sedstatic __inline size_t
104183276Sedttyhook_rint_poll(struct tty *tp)
105183276Sed{
106183276Sed	tty_lock_assert(tp, MA_OWNED);
107183276Sed	MPASS(!tty_gone(tp));
108183276Sed
109183276Sed	return tp->t_hook->th_rint_poll(tp);
110183276Sed}
111183276Sed
112183276Sedstatic __inline size_t
113183276Sedttyhook_getc_inject(struct tty *tp, void *buf, size_t len)
114183276Sed{
115183276Sed	tty_lock_assert(tp, MA_OWNED);
116183276Sed	MPASS(!tty_gone(tp));
117183276Sed
118183276Sed	return tp->t_hook->th_getc_inject(tp, buf, len);
119183276Sed}
120183276Sed
121183276Sedstatic __inline void
122183276Sedttyhook_getc_capture(struct tty *tp, const void *buf, size_t len)
123183276Sed{
124183276Sed	tty_lock_assert(tp, MA_OWNED);
125183276Sed	MPASS(!tty_gone(tp));
126183276Sed
127183276Sed	tp->t_hook->th_getc_capture(tp, buf, len);
128183276Sed}
129183276Sed
130183276Sedstatic __inline size_t
131183276Sedttyhook_getc_poll(struct tty *tp)
132183276Sed{
133183276Sed	tty_lock_assert(tp, MA_OWNED);
134183276Sed	MPASS(!tty_gone(tp));
135183276Sed
136183276Sed	return tp->t_hook->th_getc_poll(tp);
137183276Sed}
138183276Sed
139183276Sedstatic __inline void
140183276Sedttyhook_close(struct tty *tp)
141183276Sed{
142183276Sed	tty_lock_assert(tp, MA_OWNED);
143183276Sed
144183276Sed	tp->t_hook->th_close(tp);
145183276Sed}
146183276Sed
147183276Sed#endif /* !_SYS_TTYHOOK_H_ */
148