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