1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang *
4a9643ea8Slogwang * Copyright (c) 2008 Ed Schouten <[email protected]>
5a9643ea8Slogwang * All rights reserved.
6a9643ea8Slogwang *
7a9643ea8Slogwang * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang * modification, are permitted provided that the following conditions
9a9643ea8Slogwang * are met:
10a9643ea8Slogwang * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang * notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang * notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang * documentation and/or other materials provided with the distribution.
15a9643ea8Slogwang *
16a9643ea8Slogwang * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a9643ea8Slogwang * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a9643ea8Slogwang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a9643ea8Slogwang * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a9643ea8Slogwang * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a9643ea8Slogwang * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a9643ea8Slogwang * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9643ea8Slogwang * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a9643ea8Slogwang * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a9643ea8Slogwang * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a9643ea8Slogwang * SUCH DAMAGE.
27a9643ea8Slogwang *
28a9643ea8Slogwang * $FreeBSD$
29a9643ea8Slogwang */
30a9643ea8Slogwang
31a9643ea8Slogwang #ifndef _SYS_TTYHOOK_H_
32a9643ea8Slogwang #define _SYS_TTYHOOK_H_
33a9643ea8Slogwang
34a9643ea8Slogwang #ifndef _SYS_TTY_H_
35a9643ea8Slogwang #error "can only be included through <sys/tty.h>"
36a9643ea8Slogwang #endif /* !_SYS_TTY_H_ */
37a9643ea8Slogwang
38a9643ea8Slogwang struct tty;
39a9643ea8Slogwang
40a9643ea8Slogwang /*
41a9643ea8Slogwang * Hooks interface, which allows to capture and inject traffic into the
42a9643ea8Slogwang * input and output paths of a TTY.
43a9643ea8Slogwang */
44a9643ea8Slogwang
45a9643ea8Slogwang typedef int th_rint_t(struct tty *tp, char c, int flags);
46a9643ea8Slogwang typedef size_t th_rint_bypass_t(struct tty *tp, const void *buf, size_t len);
47a9643ea8Slogwang typedef void th_rint_done_t(struct tty *tp);
48a9643ea8Slogwang typedef size_t th_rint_poll_t(struct tty *tp);
49a9643ea8Slogwang
50a9643ea8Slogwang typedef size_t th_getc_inject_t(struct tty *tp, void *buf, size_t len);
51a9643ea8Slogwang typedef void th_getc_capture_t(struct tty *tp, const void *buf, size_t len);
52a9643ea8Slogwang typedef size_t th_getc_poll_t(struct tty *tp);
53a9643ea8Slogwang
54a9643ea8Slogwang typedef void th_close_t(struct tty *tp);
55a9643ea8Slogwang
56a9643ea8Slogwang struct ttyhook {
57a9643ea8Slogwang /* Character input. */
58a9643ea8Slogwang th_rint_t *th_rint;
59a9643ea8Slogwang th_rint_bypass_t *th_rint_bypass;
60a9643ea8Slogwang th_rint_done_t *th_rint_done;
61a9643ea8Slogwang th_rint_poll_t *th_rint_poll;
62a9643ea8Slogwang
63a9643ea8Slogwang /* Character output. */
64a9643ea8Slogwang th_getc_inject_t *th_getc_inject;
65a9643ea8Slogwang th_getc_capture_t *th_getc_capture;
66a9643ea8Slogwang th_getc_poll_t *th_getc_poll;
67a9643ea8Slogwang
68a9643ea8Slogwang th_close_t *th_close;
69a9643ea8Slogwang };
70a9643ea8Slogwang
71a9643ea8Slogwang int ttyhook_register(struct tty **, struct proc *, int,
72a9643ea8Slogwang struct ttyhook *, void *);
73a9643ea8Slogwang void ttyhook_unregister(struct tty *);
74a9643ea8Slogwang #define ttyhook_softc(tp) ((tp)->t_hooksoftc)
75a9643ea8Slogwang #define ttyhook_hashook(tp,hook) ((tp)->t_hook != NULL && \
76a9643ea8Slogwang (tp)->t_hook->th_ ## hook != NULL)
77a9643ea8Slogwang
78a9643ea8Slogwang static __inline int
ttyhook_rint(struct tty * tp,char c,int flags)79a9643ea8Slogwang ttyhook_rint(struct tty *tp, char c, int flags)
80a9643ea8Slogwang {
81*22ce4affSfengbojiang tty_assert_locked(tp);
82a9643ea8Slogwang MPASS(!tty_gone(tp));
83a9643ea8Slogwang
84a9643ea8Slogwang return tp->t_hook->th_rint(tp, c, flags);
85a9643ea8Slogwang }
86a9643ea8Slogwang
87a9643ea8Slogwang static __inline size_t
ttyhook_rint_bypass(struct tty * tp,const void * buf,size_t len)88a9643ea8Slogwang ttyhook_rint_bypass(struct tty *tp, const void *buf, size_t len)
89a9643ea8Slogwang {
90*22ce4affSfengbojiang tty_assert_locked(tp);
91a9643ea8Slogwang MPASS(!tty_gone(tp));
92a9643ea8Slogwang
93a9643ea8Slogwang return tp->t_hook->th_rint_bypass(tp, buf, len);
94a9643ea8Slogwang }
95a9643ea8Slogwang
96a9643ea8Slogwang static __inline void
ttyhook_rint_done(struct tty * tp)97a9643ea8Slogwang ttyhook_rint_done(struct tty *tp)
98a9643ea8Slogwang {
99*22ce4affSfengbojiang tty_assert_locked(tp);
100a9643ea8Slogwang MPASS(!tty_gone(tp));
101a9643ea8Slogwang
102a9643ea8Slogwang tp->t_hook->th_rint_done(tp);
103a9643ea8Slogwang }
104a9643ea8Slogwang
105a9643ea8Slogwang static __inline size_t
ttyhook_rint_poll(struct tty * tp)106a9643ea8Slogwang ttyhook_rint_poll(struct tty *tp)
107a9643ea8Slogwang {
108*22ce4affSfengbojiang tty_assert_locked(tp);
109a9643ea8Slogwang MPASS(!tty_gone(tp));
110a9643ea8Slogwang
111a9643ea8Slogwang return tp->t_hook->th_rint_poll(tp);
112a9643ea8Slogwang }
113a9643ea8Slogwang
114a9643ea8Slogwang static __inline size_t
ttyhook_getc_inject(struct tty * tp,void * buf,size_t len)115a9643ea8Slogwang ttyhook_getc_inject(struct tty *tp, void *buf, size_t len)
116a9643ea8Slogwang {
117*22ce4affSfengbojiang tty_assert_locked(tp);
118a9643ea8Slogwang MPASS(!tty_gone(tp));
119a9643ea8Slogwang
120a9643ea8Slogwang return tp->t_hook->th_getc_inject(tp, buf, len);
121a9643ea8Slogwang }
122a9643ea8Slogwang
123a9643ea8Slogwang static __inline void
ttyhook_getc_capture(struct tty * tp,const void * buf,size_t len)124a9643ea8Slogwang ttyhook_getc_capture(struct tty *tp, const void *buf, size_t len)
125a9643ea8Slogwang {
126*22ce4affSfengbojiang tty_assert_locked(tp);
127a9643ea8Slogwang MPASS(!tty_gone(tp));
128a9643ea8Slogwang
129a9643ea8Slogwang tp->t_hook->th_getc_capture(tp, buf, len);
130a9643ea8Slogwang }
131a9643ea8Slogwang
132a9643ea8Slogwang static __inline size_t
ttyhook_getc_poll(struct tty * tp)133a9643ea8Slogwang ttyhook_getc_poll(struct tty *tp)
134a9643ea8Slogwang {
135*22ce4affSfengbojiang tty_assert_locked(tp);
136a9643ea8Slogwang MPASS(!tty_gone(tp));
137a9643ea8Slogwang
138a9643ea8Slogwang return tp->t_hook->th_getc_poll(tp);
139a9643ea8Slogwang }
140a9643ea8Slogwang
141a9643ea8Slogwang static __inline void
ttyhook_close(struct tty * tp)142a9643ea8Slogwang ttyhook_close(struct tty *tp)
143a9643ea8Slogwang {
144*22ce4affSfengbojiang tty_assert_locked(tp);
145a9643ea8Slogwang
146a9643ea8Slogwang tp->t_hook->th_close(tp);
147a9643ea8Slogwang }
148a9643ea8Slogwang
149a9643ea8Slogwang #endif /* !_SYS_TTYHOOK_H_ */
150