xref: /netbsd-src/sys/dev/hpc/biconsdev.c (revision 28c37e673e4d9b6cbdc7483062b915cc61d1ccf5)
1 /*	$NetBSD: biconsdev.c,v 1.6 2002/09/06 13:18:43 gehenna 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. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  */
72 
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: biconsdev.c,v 1.6 2002/09/06 13:18:43 gehenna Exp $");
75 
76 #include "biconsdev.h"
77 #include <sys/param.h>
78 #include <sys/proc.h>
79 #include <sys/systm.h>
80 #include <sys/buf.h>
81 #include <sys/ioctl.h>
82 #include <sys/tty.h>
83 #include <sys/conf.h>
84 
85 #include <dev/cons.h>
86 #include <dev/hpc/bicons.h>
87 #include <dev/hpc/biconsvar.h>
88 
89 struct tty biconsdev_tty[NBICONSDEV];
90 void	biconsdevattach(int);
91 static	void biconsdev_output(struct tty *);
92 
93 dev_type_open(biconsdevopen);
94 dev_type_close(biconsdevclose);
95 dev_type_read(biconsdevread);
96 dev_type_write(biconsdevwrite);
97 dev_type_ioctl(biconsdevioctl);
98 dev_type_tty(biconsdevtty);
99 dev_type_poll(biconsdevpoll);
100 
101 const struct cdevsw biconsdev_cdevsw = {
102 	biconsdevopen, biconsdevclose, biconsdevread, biconsdevwrite,
103 	biconsdevioctl, nostop, biconsdevtty, biconsdevpoll, nommap, D_TTY
104 };
105 
106 void
107 biconsdevattach(int n)
108 {
109 	struct tty *tp = &biconsdev_tty[0];
110 	int maj;
111 
112 	/* locate the major number */
113 	maj = cdevsw_lookup_major(&biconsdev_cdevsw);
114 
115 	/* Set up the tty queues now... */
116 	clalloc(&tp->t_rawq, 1024, 1);
117 	clalloc(&tp->t_canq, 1024, 1);
118 	/* output queue doesn't need quoting */
119 	clalloc(&tp->t_outq, 1024, 0);
120 	/* Set default line discipline. */
121 	tp->t_linesw = linesw[0];
122 
123 
124 	tp->t_dev = makedev(maj, 0);
125 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
126 	tp->t_param = (int (*)(struct tty *, struct termios *))nullop;
127 	tp->t_winsize.ws_row = bicons_height;
128 	tp->t_winsize.ws_col = bicons_width;
129 	tp->t_winsize.ws_xpixel = bicons_xpixel;
130 	tp->t_winsize.ws_ypixel = bicons_ypixel;
131 	tp->t_oproc = biconsdev_output;
132 }
133 
134 
135 static void
136 biconsdev_output(struct tty *tp)
137 {
138 	int s, n;
139 	char buf[OBUFSIZ];
140 
141 	s = spltty();
142 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
143 		splx(s);
144 		return;
145 	}
146 	tp->t_state |= TS_BUSY;
147 	splx(s);
148 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
149 	bicons_putn(buf, n);
150 
151 	s = spltty();
152 	tp->t_state &= ~TS_BUSY;
153 	/* Come back if there's more to do */
154 	if (tp->t_outq.c_cc) {
155 		tp->t_state |= TS_TIMEOUT;
156 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
157 	}
158 	if (tp->t_outq.c_cc <= tp->t_lowat) {
159 		if (tp->t_state&TS_ASLEEP) {
160 			tp->t_state &= ~TS_ASLEEP;
161 			wakeup((caddr_t)&tp->t_outq);
162 		}
163 		selwakeup(&tp->t_wsel);
164 	}
165 	splx(s);
166 }
167 
168 
169 int
170 biconsdevopen(dev_t dev, int flag, int mode, struct proc *p)
171 {
172 	struct tty *tp = &biconsdev_tty[0];
173 	int status;
174 
175 	if ((tp->t_state & TS_ISOPEN) == 0) {
176 		/*
177 		 * Leave baud rate alone!
178 		 */
179 		ttychars(tp);
180 		tp->t_iflag = TTYDEF_IFLAG;
181 		tp->t_oflag = TTYDEF_OFLAG;
182 		tp->t_lflag = TTYDEF_LFLAG;
183 		tp->t_cflag = TTYDEF_CFLAG;
184 		tp->t_state = TS_ISOPEN | TS_CARR_ON;
185 		(void)(*tp->t_param)(tp, &tp->t_termios);
186 		ttsetwater(tp);
187 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
188 		return (EBUSY);
189 
190 	status = (*tp->t_linesw->l_open)(dev, tp);
191 	return status;
192 }
193 
194 
195 int
196 biconsdevclose(dev_t dev, int flag, int mode, struct proc *p)
197 {
198 	struct tty *tp = &biconsdev_tty[0];
199 
200 	(*tp->t_linesw->l_close)(tp, flag);
201 	ttyclose(tp);
202 
203 	return (0);
204 }
205 
206 
207 int
208 biconsdevread(dev_t dev, struct uio *uio, int flag)
209 {
210 	struct tty *tp = &biconsdev_tty[0];
211 
212 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
213 }
214 
215 
216 int
217 biconsdevwrite(dev_t dev, struct uio *uio, int flag)
218 {
219 	struct tty *tp = &biconsdev_tty[0];
220 
221 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
222 }
223 
224 
225 int
226 biconsdevpoll(dev_t dev, int events, struct proc *p)
227 {
228 	struct tty *tp = &biconsdev_tty[0];
229 
230 	return ((*tp->t_linesw->l_poll)(tp, events, p));
231 }
232 
233 
234 struct tty *
235 biconsdevtty(dev_t dev)
236 {
237 	struct tty *tp = &biconsdev_tty[0];
238 
239         return (tp);
240 }
241 
242 int
243 biconsdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
244 {
245 	struct tty *tp = &biconsdev_tty[0];
246 	int error;
247 
248 	if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p)) !=
249 	    EPASSTHROUGH)
250 		return (error);
251 	return (ttioctl(tp, cmd, data, flag, p));
252 }
253