xref: /netbsd-src/sys/dev/hpc/biconsdev.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: biconsdev.c,v 1.22 2014/07/25 08:10:37 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999-2001
5  *         Shin Takemura and PocketBSD Project. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the PocketBSD project
18  *	and its contributors.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 /*
37  * Copyright (c) 1995
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Ted Lemon.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  */
68 
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: biconsdev.c,v 1.22 2014/07/25 08:10:37 dholland Exp $");
71 
72 #include "biconsdev.h"
73 #include <sys/param.h>
74 #include <sys/proc.h>
75 #include <sys/systm.h>
76 #include <sys/buf.h>
77 #include <sys/ioctl.h>
78 #include <sys/tty.h>
79 #include <sys/conf.h>
80 #include <sys/kauth.h>
81 
82 #include <dev/cons.h>
83 #include <dev/hpc/bicons.h>
84 #include <dev/hpc/biconsvar.h>
85 
86 struct tty biconsdev_tty[NBICONSDEV];
87 void	biconsdevattach(int);
88 static	void biconsdev_output(struct tty *);
89 
90 dev_type_open(biconsdevopen);
91 dev_type_close(biconsdevclose);
92 dev_type_read(biconsdevread);
93 dev_type_write(biconsdevwrite);
94 dev_type_ioctl(biconsdevioctl);
95 dev_type_tty(biconsdevtty);
96 dev_type_poll(biconsdevpoll);
97 
98 const struct cdevsw biconsdev_cdevsw = {
99 	.d_open = biconsdevopen,
100 	.d_close = biconsdevclose,
101 	.d_read = biconsdevread,
102 	.d_write = biconsdevwrite,
103 	.d_ioctl = biconsdevioctl,
104 	.d_stop = nostop,
105 	.d_tty = biconsdevtty,
106 	.d_poll = biconsdevpoll,
107 	.d_mmap = nommap,
108 	.d_kqfilter = ttykqfilter,
109 	.d_discard = nodiscard,
110 	.d_flag = D_TTY
111 };
112 
113 void
114 biconsdevattach(int n)
115 {
116 	struct tty *tp = &biconsdev_tty[0];
117 	int maj;
118 
119 	/* locate the major number */
120 	maj = cdevsw_lookup_major(&biconsdev_cdevsw);
121 
122 	/* Set up the tty queues now... */
123 	clalloc(&tp->t_rawq, 1024, 1);
124 	clalloc(&tp->t_canq, 1024, 1);
125 	/* output queue doesn't need quoting */
126 	clalloc(&tp->t_outq, 1024, 0);
127 	/* Set default line discipline. */
128 	tp->t_linesw = ttyldisc_default();
129 
130 
131 	tp->t_dev = makedev(maj, 0);
132 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
133 	tp->t_param = (int (*)(struct tty *, struct termios *))nullop;
134 	tp->t_winsize.ws_row = bicons_height;
135 	tp->t_winsize.ws_col = bicons_width;
136 	tp->t_winsize.ws_xpixel = bicons_xpixel;
137 	tp->t_winsize.ws_ypixel = bicons_ypixel;
138 	tp->t_oproc = biconsdev_output;
139 }
140 
141 
142 static void
143 biconsdev_output(struct tty *tp)
144 {
145 	int s, n;
146 	char buf[OBUFSIZ];
147 
148 	s = spltty();
149 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
150 		splx(s);
151 		return;
152 	}
153 	tp->t_state |= TS_BUSY;
154 	splx(s);
155 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
156 	bicons_putn(buf, n);
157 
158 	s = spltty();
159 	tp->t_state &= ~TS_BUSY;
160 	/* Come back if there's more to do */
161 	if (ttypull(tp)) {
162 		tp->t_state |= TS_TIMEOUT;
163 		callout_schedule(&tp->t_rstrt_ch, 1);
164 	}
165 	splx(s);
166 }
167 
168 
169 int
170 biconsdevopen(dev_t dev, int flag, int mode, struct lwp *l)
171 {
172 	struct tty *tp = &biconsdev_tty[0];
173 	int status;
174 
175 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
176 		return (EBUSY);
177 
178 	if ((tp->t_state & TS_ISOPEN) == 0) {
179 		/*
180 		 * Leave baud rate alone!
181 		 */
182 		ttychars(tp);
183 		tp->t_iflag = TTYDEF_IFLAG;
184 		tp->t_oflag = TTYDEF_OFLAG;
185 		tp->t_lflag = TTYDEF_LFLAG;
186 		tp->t_cflag = TTYDEF_CFLAG;
187 		tp->t_state = TS_ISOPEN | TS_CARR_ON;
188 		(void)(*tp->t_param)(tp, &tp->t_termios);
189 		ttsetwater(tp);
190 	}
191 
192 	status = (*tp->t_linesw->l_open)(dev, tp);
193 	return status;
194 }
195 
196 
197 int
198 biconsdevclose(dev_t dev, int flag, int mode, struct lwp *l)
199 {
200 	struct tty *tp = &biconsdev_tty[0];
201 
202 	(*tp->t_linesw->l_close)(tp, flag);
203 	ttyclose(tp);
204 
205 	return (0);
206 }
207 
208 
209 int
210 biconsdevread(dev_t dev, struct uio *uio, int flag)
211 {
212 	struct tty *tp = &biconsdev_tty[0];
213 
214 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
215 }
216 
217 
218 int
219 biconsdevwrite(dev_t dev, struct uio *uio, int flag)
220 {
221 	struct tty *tp = &biconsdev_tty[0];
222 
223 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
224 }
225 
226 
227 int
228 biconsdevpoll(dev_t dev, int events, struct lwp *l)
229 {
230 	struct tty *tp = &biconsdev_tty[0];
231 
232 	return ((*tp->t_linesw->l_poll)(tp, events, l));
233 }
234 
235 
236 struct tty *
237 biconsdevtty(dev_t dev)
238 {
239 	struct tty *tp = &biconsdev_tty[0];
240 
241         return (tp);
242 }
243 
244 int
245 biconsdevioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
246 {
247 	struct tty *tp = &biconsdev_tty[0];
248 	int error;
249 
250 	if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l)) !=
251 	    EPASSTHROUGH)
252 		return (error);
253 	return (ttioctl(tp, cmd, data, flag, l));
254 }
255