xref: /netbsd-src/sys/dev/rcons/rcons_kern.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: rcons_kern.c,v 1.12 2000/03/23 07:01:43 thorpej Exp $ */
2 
3 /*
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * All advertising materials mentioning features or use of this software
12  * must display the following acknowledgement:
13  *	This product includes software developed by the University of
14  *	California, Lawrence Berkeley Laboratory.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. All advertising materials mentioning features or use of this software
25  *    must display the following acknowledgement:
26  *	This product includes software developed by the University of
27  *	California, Berkeley and its contributors.
28  * 4. Neither the name of the University nor the names of its contributors
29  *    may be used to endorse or promote products derived from this software
30  *    without specific prior written permission.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42  * SUCH DAMAGE.
43  *
44  *	@(#)rcons_kern.c	8.1 (Berkeley) 6/11/93
45  */
46 
47 #include <sys/param.h>
48 #include <sys/device.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/ioctl.h>
52 #include <sys/tty.h>
53 #include <sys/proc.h>
54 
55 #include <dev/rcons/raster.h>
56 #include <dev/rcons/rcons.h>
57 
58 static void rcons_belltmr(void *);
59 
60 static struct rconsole *mydevicep; /* XXX */
61 static void rcons_output __P((struct tty *));
62 
63 void
64 rcons_cnputc(c)
65 	int c;
66 {
67 	char buf[1];
68 	long attr;
69 
70 	/* Swap in kernel attribute */
71 	attr = mydevicep->rc_attr;
72 	mydevicep->rc_attr = mydevicep->rc_kern_attr;
73 
74 	if (c == '\n')
75 		rcons_puts(mydevicep, "\r\n", 2);
76 	else {
77 		buf[0] = c;
78 		rcons_puts(mydevicep, buf, 1);
79 	}
80 
81 	/* Swap out kernel attribute */
82 	mydevicep->rc_attr = attr;
83 }
84 
85 static void
86 rcons_output(tp)
87 	struct tty *tp;
88 {
89 	int s, n;
90 	char buf[OBUFSIZ];
91 
92 	s = spltty();
93 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
94 		splx(s);
95 		return;
96 	}
97 	tp->t_state |= TS_BUSY;
98 	splx(s);
99 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
100 	rcons_puts(mydevicep, buf, n);
101 
102 	s = spltty();
103 	tp->t_state &= ~TS_BUSY;
104 	/* Come back if there's more to do */
105 	if (tp->t_outq.c_cc) {
106 		tp->t_state |= TS_TIMEOUT;
107 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
108 	}
109 	if (tp->t_outq.c_cc <= tp->t_lowat) {
110 		if (tp->t_state&TS_ASLEEP) {
111 			tp->t_state &= ~TS_ASLEEP;
112 			wakeup((caddr_t)&tp->t_outq);
113 		}
114 		selwakeup(&tp->t_wsel);
115 	}
116 	splx(s);
117 }
118 
119 /* Ring the console bell */
120 void
121 rcons_bell(rc)
122 	struct rconsole *rc;
123 {
124 	int i, s;
125 
126 	if (rc->rc_bits & FB_VISBELL) {
127 		/* invert the screen twice */
128 		i = ((rc->rc_bits & FB_INVERT) == 0);
129 		rcons_invert(rc, i);
130 		rcons_invert(rc, i ^ 1);
131 	}
132 
133 	s = splhigh();
134 	if (rc->rc_belldepth++) {
135 		if (rc->rc_belldepth > 3)
136 			rc->rc_belldepth = 3;
137 		splx(s);
138 	} else {
139 		rc->rc_ringing = 1;
140 		splx(s);
141 		(*rc->rc_bell)(1);
142 		/* XXX Chris doesn't like the following divide */
143 		callout_reset(&rc->rc_belltmr_ch, hz / 10,
144 		    rcons_belltmr, rc);
145 	}
146 }
147 
148 /* Bell timer service routine */
149 static void
150 rcons_belltmr(p)
151 	void *p;
152 {
153 	struct rconsole *rc = p;
154 	int s = splhigh(), i;
155 
156 	if (rc->rc_ringing) {
157 		rc->rc_ringing = 0;
158 		i = --rc->rc_belldepth;
159 		splx(s);
160 		(*rc->rc_bell)(0);
161 		if (i != 0)
162 			/* XXX Chris doesn't like the following divide */
163 			callout_reset(&rc->rc_belltmr_ch, hz / 30,
164 			    rcons_belltmr, rc);
165 	} else {
166 		rc->rc_ringing = 1;
167 		splx(s);
168 		(*rc->rc_bell)(1);
169 		callout_reset(&rc->rc_belltmr_ch, hz / 10,
170 		    rcons_belltmr, rc);
171 	}
172 }
173 
174 void
175 rcons_init(rc, clear)
176 	struct rconsole *rc;
177 	int clear;
178 {
179 	mydevicep = rc;
180 
181 	callout_init(&rc->rc_belltmr_ch);
182 
183 	/* Initialize operations set, clear screen and turn cursor on */
184 	rcons_init_ops(rc);
185 	if (clear) {
186 		rc->rc_col = 0;
187 		rc->rc_row = 0;
188 		rcons_clear2eop(rc);
189 	}
190 	rcons_cursor(rc);
191 }
192 
193 void
194 rcons_ttyinit(tp)
195 	struct tty *tp;
196 {
197 	/* XXX this should go away */
198 	struct rconsole *rc = mydevicep;
199 	struct winsize *ws;
200 
201 	if (rc == NULL)
202 		return;
203 
204 	/* Let the system know how big the console is */
205 	ws = &tp->t_winsize;
206 	ws->ws_row = rc->rc_maxrow;
207 	ws->ws_col = rc->rc_maxcol;
208 	ws->ws_xpixel = rc->rc_width;
209 	ws->ws_ypixel = rc->rc_height;
210 
211 	/* Initialization done; hook us up */
212 	tp->t_oproc = rcons_output;
213 	/*tp->t_stop = (void (*)()) nullop;*/
214 }
215