1181905Sed/*-
2181905Sed * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3181905Sed * All rights reserved.
4181905Sed *
5181905Sed * Portions of this software were developed under sponsorship from Snow
6181905Sed * B.V., the Netherlands.
7181905Sed *
8181905Sed * Redistribution and use in source and binary forms, with or without
9181905Sed * modification, are permitted provided that the following conditions
10181905Sed * are met:
11181905Sed * 1. Redistributions of source code must retain the above copyright
12181905Sed *    notice, this list of conditions and the following disclaimer.
13181905Sed * 2. Redistributions in binary form must reproduce the above copyright
14181905Sed *    notice, this list of conditions and the following disclaimer in the
15181905Sed *    documentation and/or other materials provided with the distribution.
16181905Sed *
17181905Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18181905Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19181905Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20181905Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21181905Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22181905Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23181905Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24181905Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25181905Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26181905Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27181905Sed * SUCH DAMAGE.
28181905Sed *
29181905Sed * $FreeBSD$
30181905Sed */
31181905Sed
32181905Sed#ifndef _SYS_TTYDEVSW_H_
33181905Sed#define	_SYS_TTYDEVSW_H_
34181905Sed
35181905Sed#ifndef _SYS_TTY_H_
36181905Sed#error "can only be included through <sys/tty.h>"
37181905Sed#endif /* !_SYS_TTY_H_ */
38181905Sed
39181905Sed/*
40181905Sed * Driver routines that are called from the line discipline to adjust
41181905Sed * hardware parameters and such.
42181905Sed */
43183274Sedtypedef int tsw_open_t(struct tty *tp);
44183274Sedtypedef void tsw_close_t(struct tty *tp);
45183274Sedtypedef void tsw_outwakeup_t(struct tty *tp);
46183274Sedtypedef void tsw_inwakeup_t(struct tty *tp);
47183274Sedtypedef int tsw_ioctl_t(struct tty *tp, u_long cmd, caddr_t data,
48183274Sed    struct thread *td);
49223722Sedtypedef int tsw_cioctl_t(struct tty *tp, int unit, u_long cmd, caddr_t data,
50223722Sed    struct thread *td);
51183274Sedtypedef int tsw_param_t(struct tty *tp, struct termios *t);
52183274Sedtypedef int tsw_modem_t(struct tty *tp, int sigon, int sigoff);
53201223Srnolandtypedef int tsw_mmap_t(struct tty *tp, vm_ooffset_t offset,
54201223Srnoland    vm_paddr_t * paddr, int nprot, vm_memattr_t *memattr);
55183274Sedtypedef void tsw_pktnotify_t(struct tty *tp, char event);
56183274Sedtypedef void tsw_free_t(void *softc);
57294362Smariustypedef bool tsw_busy_t(struct tty *tp);
58181905Sed
59181905Sedstruct ttydevsw {
60181905Sed	unsigned int	tsw_flags;	/* Default TTY flags. */
61181905Sed
62181905Sed	tsw_open_t	*tsw_open;	/* Device opening. */
63181905Sed	tsw_close_t	*tsw_close;	/* Device closure. */
64181905Sed
65181905Sed	tsw_outwakeup_t	*tsw_outwakeup;	/* Output available. */
66181905Sed	tsw_inwakeup_t	*tsw_inwakeup;	/* Input can be stored again. */
67181905Sed
68181905Sed	tsw_ioctl_t	*tsw_ioctl;	/* ioctl() hooks. */
69223722Sed	tsw_cioctl_t	*tsw_cioctl;	/* ioctl() on control devices. */
70181905Sed	tsw_param_t	*tsw_param;	/* TIOCSETA device parameter setting. */
71181905Sed	tsw_modem_t	*tsw_modem;	/* Modem sigon/sigoff. */
72181905Sed
73181905Sed	tsw_mmap_t	*tsw_mmap;	/* mmap() hooks. */
74182764Sed	tsw_pktnotify_t	*tsw_pktnotify;	/* TIOCPKT events. */
75181905Sed
76181905Sed	tsw_free_t	*tsw_free;	/* Destructor. */
77223722Sed
78294362Smarius	tsw_busy_t	*tsw_busy;	/* Draining output. */
79294362Smarius
80294362Smarius	void		*tsw_spare[3];	/* For future use. */
81181905Sed};
82181905Sed
83181905Sedstatic __inline int
84181905Sedttydevsw_open(struct tty *tp)
85181905Sed{
86294753Smarius
87181905Sed	tty_lock_assert(tp, MA_OWNED);
88181905Sed	MPASS(!tty_gone(tp));
89181905Sed
90294753Smarius	return (tp->t_devsw->tsw_open(tp));
91181905Sed}
92181905Sed
93181905Sedstatic __inline void
94181905Sedttydevsw_close(struct tty *tp)
95181905Sed{
96294753Smarius
97181905Sed	tty_lock_assert(tp, MA_OWNED);
98181905Sed	MPASS(!tty_gone(tp));
99181905Sed
100181905Sed	tp->t_devsw->tsw_close(tp);
101181905Sed}
102181905Sed
103181905Sedstatic __inline void
104181905Sedttydevsw_outwakeup(struct tty *tp)
105181905Sed{
106294753Smarius
107181905Sed	tty_lock_assert(tp, MA_OWNED);
108181905Sed	MPASS(!tty_gone(tp));
109181905Sed
110181905Sed	/* Prevent spurious wakeups. */
111183276Sed	if (ttydisc_getc_poll(tp) == 0)
112181905Sed		return;
113181905Sed
114181905Sed	tp->t_devsw->tsw_outwakeup(tp);
115181905Sed}
116181905Sed
117181905Sedstatic __inline void
118181905Sedttydevsw_inwakeup(struct tty *tp)
119181905Sed{
120294753Smarius
121181905Sed	tty_lock_assert(tp, MA_OWNED);
122181905Sed	MPASS(!tty_gone(tp));
123181905Sed
124181905Sed	/* Prevent spurious wakeups. */
125181905Sed	if (tp->t_flags & TF_HIWAT_IN)
126181905Sed		return;
127181905Sed
128181905Sed	tp->t_devsw->tsw_inwakeup(tp);
129181905Sed}
130181905Sed
131181905Sedstatic __inline int
132181905Sedttydevsw_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
133181905Sed{
134294753Smarius
135181905Sed	tty_lock_assert(tp, MA_OWNED);
136181905Sed	MPASS(!tty_gone(tp));
137181905Sed
138294753Smarius	return (tp->t_devsw->tsw_ioctl(tp, cmd, data, td));
139181905Sed}
140181905Sed
141181905Sedstatic __inline int
142294753Smariusttydevsw_cioctl(struct tty *tp, int unit, u_long cmd, caddr_t data,
143294753Smarius    struct thread *td)
144223722Sed{
145294753Smarius
146223722Sed	tty_lock_assert(tp, MA_OWNED);
147223722Sed	MPASS(!tty_gone(tp));
148223722Sed
149294753Smarius	return (tp->t_devsw->tsw_cioctl(tp, unit, cmd, data, td));
150223722Sed}
151223722Sed
152223722Sedstatic __inline int
153181905Sedttydevsw_param(struct tty *tp, struct termios *t)
154181905Sed{
155294753Smarius
156181905Sed	MPASS(!tty_gone(tp));
157181905Sed
158294753Smarius	return (tp->t_devsw->tsw_param(tp, t));
159181905Sed}
160181905Sed
161181905Sedstatic __inline int
162181905Sedttydevsw_modem(struct tty *tp, int sigon, int sigoff)
163181905Sed{
164294753Smarius
165181905Sed	MPASS(!tty_gone(tp));
166181905Sed
167294753Smarius	return (tp->t_devsw->tsw_modem(tp, sigon, sigoff));
168181905Sed}
169181905Sed
170181905Sedstatic __inline int
171201223Srnolandttydevsw_mmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
172201223Srnoland    int nprot, vm_memattr_t *memattr)
173181905Sed{
174294753Smarius
175181905Sed	MPASS(!tty_gone(tp));
176181905Sed
177294753Smarius	return (tp->t_devsw->tsw_mmap(tp, offset, paddr, nprot, memattr));
178181905Sed}
179181905Sed
180181905Sedstatic __inline void
181182764Sedttydevsw_pktnotify(struct tty *tp, char event)
182182764Sed{
183294753Smarius
184182764Sed	tty_lock_assert(tp, MA_OWNED);
185182764Sed	MPASS(!tty_gone(tp));
186182764Sed
187182764Sed	tp->t_devsw->tsw_pktnotify(tp, event);
188182764Sed}
189182764Sed
190182764Sedstatic __inline void
191181905Sedttydevsw_free(struct tty *tp)
192181905Sed{
193294753Smarius
194181905Sed	MPASS(tty_gone(tp));
195181905Sed
196181905Sed	tp->t_devsw->tsw_free(tty_softc(tp));
197181905Sed}
198181905Sed
199294362Smariusstatic __inline bool
200294362Smariusttydevsw_busy(struct tty *tp)
201294362Smarius{
202294362Smarius
203294414Smarius	tty_lock_assert(tp, MA_OWNED);
204294414Smarius	MPASS(!tty_gone(tp));
205294362Smarius
206294362Smarius	return (tp->t_devsw->tsw_busy(tp));
207294362Smarius}
208294362Smarius
209181905Sed#endif /* !_SYS_TTYDEVSW_H_ */
210