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