ttydevsw.h revision 183274
1/*-
2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/sys/ttydevsw.h 183274 2008-09-22 18:44:09Z ed $
30 */
31
32#ifndef _SYS_TTYDEVSW_H_
33#define	_SYS_TTYDEVSW_H_
34
35#ifndef _SYS_TTY_H_
36#error "can only be included through <sys/tty.h>"
37#endif /* !_SYS_TTY_H_ */
38
39/*
40 * Driver routines that are called from the line discipline to adjust
41 * hardware parameters and such.
42 */
43typedef int tsw_open_t(struct tty *tp);
44typedef void tsw_close_t(struct tty *tp);
45typedef void tsw_outwakeup_t(struct tty *tp);
46typedef void tsw_inwakeup_t(struct tty *tp);
47typedef int tsw_ioctl_t(struct tty *tp, u_long cmd, caddr_t data,
48    struct thread *td);
49typedef int tsw_param_t(struct tty *tp, struct termios *t);
50typedef int tsw_modem_t(struct tty *tp, int sigon, int sigoff);
51typedef int tsw_mmap_t(struct tty *tp, vm_offset_t offset,
52    vm_paddr_t * paddr, int nprot);
53typedef void tsw_pktnotify_t(struct tty *tp, char event);
54typedef void tsw_free_t(void *softc);
55
56struct ttydevsw {
57	unsigned int	tsw_flags;	/* Default TTY flags. */
58
59	tsw_open_t	*tsw_open;	/* Device opening. */
60	tsw_close_t	*tsw_close;	/* Device closure. */
61
62	tsw_outwakeup_t	*tsw_outwakeup;	/* Output available. */
63	tsw_inwakeup_t	*tsw_inwakeup;	/* Input can be stored again. */
64
65	tsw_ioctl_t	*tsw_ioctl;	/* ioctl() hooks. */
66	tsw_param_t	*tsw_param;	/* TIOCSETA device parameter setting. */
67	tsw_modem_t	*tsw_modem;	/* Modem sigon/sigoff. */
68
69	tsw_mmap_t	*tsw_mmap;	/* mmap() hooks. */
70	tsw_pktnotify_t	*tsw_pktnotify;	/* TIOCPKT events. */
71
72	tsw_free_t	*tsw_free;	/* Destructor. */
73};
74
75static __inline int
76ttydevsw_open(struct tty *tp)
77{
78	tty_lock_assert(tp, MA_OWNED);
79	MPASS(!tty_gone(tp));
80
81	return tp->t_devsw->tsw_open(tp);
82}
83
84static __inline void
85ttydevsw_close(struct tty *tp)
86{
87	tty_lock_assert(tp, MA_OWNED);
88	MPASS(!tty_gone(tp));
89
90	tp->t_devsw->tsw_close(tp);
91}
92
93static __inline void
94ttydevsw_outwakeup(struct tty *tp)
95{
96	tty_lock_assert(tp, MA_OWNED);
97	MPASS(!tty_gone(tp));
98
99	/* Prevent spurious wakeups. */
100	if (tp->t_flags & TF_STOPPED)
101		return;
102	if (ttyoutq_bytesused(&tp->t_outq) == 0)
103		return;
104
105	tp->t_devsw->tsw_outwakeup(tp);
106}
107
108static __inline void
109ttydevsw_inwakeup(struct tty *tp)
110{
111	tty_lock_assert(tp, MA_OWNED);
112	MPASS(!tty_gone(tp));
113
114	/* Prevent spurious wakeups. */
115	if (tp->t_flags & TF_HIWAT_IN)
116		return;
117
118	tp->t_devsw->tsw_inwakeup(tp);
119}
120
121static __inline int
122ttydevsw_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
123{
124	tty_lock_assert(tp, MA_OWNED);
125	MPASS(!tty_gone(tp));
126
127	return tp->t_devsw->tsw_ioctl(tp, cmd, data, td);
128}
129
130static __inline int
131ttydevsw_param(struct tty *tp, struct termios *t)
132{
133	MPASS(!tty_gone(tp));
134
135	return tp->t_devsw->tsw_param(tp, t);
136}
137
138static __inline int
139ttydevsw_modem(struct tty *tp, int sigon, int sigoff)
140{
141	MPASS(!tty_gone(tp));
142
143	return tp->t_devsw->tsw_modem(tp, sigon, sigoff);
144}
145
146static __inline int
147ttydevsw_mmap(struct tty *tp, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
148{
149	MPASS(!tty_gone(tp));
150
151	return tp->t_devsw->tsw_mmap(tp, offset, paddr, nprot);
152}
153
154static __inline void
155ttydevsw_pktnotify(struct tty *tp, char event)
156{
157	tty_lock_assert(tp, MA_OWNED);
158	MPASS(!tty_gone(tp));
159
160	tp->t_devsw->tsw_pktnotify(tp, event);
161}
162
163static __inline void
164ttydevsw_free(struct tty *tp)
165{
166	MPASS(tty_gone(tp));
167
168	tp->t_devsw->tsw_free(tty_softc(tp));
169}
170
171#endif /* !_SYS_TTYDEVSW_H_ */
172