xref: /netbsd/sys/dev/hpc/biconsdev.c (revision c4a72b64)
1 /*	$NetBSD: biconsdev.c,v 1.7 2002/10/23 09:13:11 jdolecek 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.7 2002/10/23 09:13:11 jdolecek 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,
104 	ttykqfilter, D_TTY
105 };
106 
107 void
108 biconsdevattach(int n)
109 {
110 	struct tty *tp = &biconsdev_tty[0];
111 	int maj;
112 
113 	/* locate the major number */
114 	maj = cdevsw_lookup_major(&biconsdev_cdevsw);
115 
116 	/* Set up the tty queues now... */
117 	clalloc(&tp->t_rawq, 1024, 1);
118 	clalloc(&tp->t_canq, 1024, 1);
119 	/* output queue doesn't need quoting */
120 	clalloc(&tp->t_outq, 1024, 0);
121 	/* Set default line discipline. */
122 	tp->t_linesw = linesw[0];
123 
124 
125 	tp->t_dev = makedev(maj, 0);
126 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
127 	tp->t_param = (int (*)(struct tty *, struct termios *))nullop;
128 	tp->t_winsize.ws_row = bicons_height;
129 	tp->t_winsize.ws_col = bicons_width;
130 	tp->t_winsize.ws_xpixel = bicons_xpixel;
131 	tp->t_winsize.ws_ypixel = bicons_ypixel;
132 	tp->t_oproc = biconsdev_output;
133 }
134 
135 
136 static void
137 biconsdev_output(struct tty *tp)
138 {
139 	int s, n;
140 	char buf[OBUFSIZ];
141 
142 	s = spltty();
143 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
144 		splx(s);
145 		return;
146 	}
147 	tp->t_state |= TS_BUSY;
148 	splx(s);
149 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
150 	bicons_putn(buf, n);
151 
152 	s = spltty();
153 	tp->t_state &= ~TS_BUSY;
154 	/* Come back if there's more to do */
155 	if (tp->t_outq.c_cc) {
156 		tp->t_state |= TS_TIMEOUT;
157 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
158 	}
159 	if (tp->t_outq.c_cc <= tp->t_lowat) {
160 		if (tp->t_state&TS_ASLEEP) {
161 			tp->t_state &= ~TS_ASLEEP;
162 			wakeup((caddr_t)&tp->t_outq);
163 		}
164 		selwakeup(&tp->t_wsel);
165 	}
166 	splx(s);
167 }
168 
169 
170 int
171 biconsdevopen(dev_t dev, int flag, int mode, struct proc *p)
172 {
173 	struct tty *tp = &biconsdev_tty[0];
174 	int status;
175 
176 	if ((tp->t_state & TS_ISOPEN) == 0) {
177 		/*
178 		 * Leave baud rate alone!
179 		 */
180 		ttychars(tp);
181 		tp->t_iflag = TTYDEF_IFLAG;
182 		tp->t_oflag = TTYDEF_OFLAG;
183 		tp->t_lflag = TTYDEF_LFLAG;
184 		tp->t_cflag = TTYDEF_CFLAG;
185 		tp->t_state = TS_ISOPEN | TS_CARR_ON;
186 		(void)(*tp->t_param)(tp, &tp->t_termios);
187 		ttsetwater(tp);
188 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
189 		return (EBUSY);
190 
191 	status = (*tp->t_linesw->l_open)(dev, tp);
192 	return status;
193 }
194 
195 
196 int
197 biconsdevclose(dev_t dev, int flag, int mode, struct proc *p)
198 {
199 	struct tty *tp = &biconsdev_tty[0];
200 
201 	(*tp->t_linesw->l_close)(tp, flag);
202 	ttyclose(tp);
203 
204 	return (0);
205 }
206 
207 
208 int
209 biconsdevread(dev_t dev, struct uio *uio, int flag)
210 {
211 	struct tty *tp = &biconsdev_tty[0];
212 
213 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
214 }
215 
216 
217 int
218 biconsdevwrite(dev_t dev, struct uio *uio, int flag)
219 {
220 	struct tty *tp = &biconsdev_tty[0];
221 
222 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
223 }
224 
225 
226 int
227 biconsdevpoll(dev_t dev, int events, struct proc *p)
228 {
229 	struct tty *tp = &biconsdev_tty[0];
230 
231 	return ((*tp->t_linesw->l_poll)(tp, events, p));
232 }
233 
234 
235 struct tty *
236 biconsdevtty(dev_t dev)
237 {
238 	struct tty *tp = &biconsdev_tty[0];
239 
240         return (tp);
241 }
242 
243 int
244 biconsdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
245 {
246 	struct tty *tp = &biconsdev_tty[0];
247 	int error;
248 
249 	if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, p)) !=
250 	    EPASSTHROUGH)
251 		return (error);
252 	return (ttioctl(tp, cmd, data, flag, p));
253 }
254