cons.c revision 1.40.2.1 1 1.40.2.1 lukem /* $NetBSD: cons.c,v 1.40.2.1 2001/07/10 14:01:58 lukem Exp $ */
2 1.18 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1988 University of Utah.
5 1.17 cgd * Copyright (c) 1990, 1993
6 1.17 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * This code is derived from software contributed to Berkeley by
9 1.1 cgd * the Systems Programming Group of the University of Utah Computer
10 1.1 cgd * Science Department.
11 1.1 cgd *
12 1.1 cgd * Redistribution and use in source and binary forms, with or without
13 1.1 cgd * modification, are permitted provided that the following conditions
14 1.1 cgd * are met:
15 1.1 cgd * 1. Redistributions of source code must retain the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer.
17 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 cgd * notice, this list of conditions and the following disclaimer in the
19 1.1 cgd * documentation and/or other materials provided with the distribution.
20 1.1 cgd * 3. All advertising materials mentioning features or use of this software
21 1.1 cgd * must display the following acknowledgement:
22 1.1 cgd * This product includes software developed by the University of
23 1.1 cgd * California, Berkeley and its contributors.
24 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.1 cgd * may be used to endorse or promote products derived from this software
26 1.1 cgd * without specific prior written permission.
27 1.1 cgd *
28 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 cgd * SUCH DAMAGE.
39 1.1 cgd *
40 1.17 cgd * from: Utah $Hdr: cons.c 1.7 92/01/21$
41 1.17 cgd *
42 1.17 cgd * @(#)cons.c 8.2 (Berkeley) 1/12/94
43 1.1 cgd */
44 1.1 cgd
45 1.10 cgd #include <sys/param.h>
46 1.10 cgd #include <sys/proc.h>
47 1.10 cgd #include <sys/user.h>
48 1.10 cgd #include <sys/systm.h>
49 1.10 cgd #include <sys/buf.h>
50 1.10 cgd #include <sys/ioctl.h>
51 1.10 cgd #include <sys/tty.h>
52 1.10 cgd #include <sys/file.h>
53 1.10 cgd #include <sys/conf.h>
54 1.10 cgd #include <sys/vnode.h>
55 1.1 cgd
56 1.11 cgd #include <dev/cons.h>
57 1.1 cgd
58 1.10 cgd struct tty *constty = NULL; /* virtual console output device */
59 1.1 cgd struct consdev *cn_tab; /* physical console device info */
60 1.13 cgd struct vnode *cn_devvp; /* vnode for underlying device. */
61 1.1 cgd
62 1.7 andrew int
63 1.1 cgd cnopen(dev, flag, mode, p)
64 1.1 cgd dev_t dev;
65 1.1 cgd int flag, mode;
66 1.1 cgd struct proc *p;
67 1.1 cgd {
68 1.37 mrg dev_t cndev;
69 1.21 mycroft
70 1.1 cgd if (cn_tab == NULL)
71 1.1 cgd return (0);
72 1.10 cgd
73 1.10 cgd /*
74 1.10 cgd * always open the 'real' console device, so we don't get nailed
75 1.10 cgd * later. This follows normal device semantics; they always get
76 1.10 cgd * open() calls.
77 1.10 cgd */
78 1.37 mrg cndev = cn_tab->cn_dev;
79 1.37 mrg if (cndev == NODEV) {
80 1.33 leo /*
81 1.33 leo * This is most likely an error in the console attach
82 1.33 leo * code. Panicing looks better than jumping into nowhere
83 1.33 leo * through cdevsw below....
84 1.33 leo */
85 1.33 leo panic("cnopen: cn_tab->cn_dev == NODEV\n");
86 1.33 leo }
87 1.37 mrg if (dev == cndev) {
88 1.37 mrg /*
89 1.37 mrg * This causes cnopen() to be called recursively, which
90 1.37 mrg * is generally a bad thing. It is often caused when
91 1.37 mrg * dev == 0 and cn_dev has not been set, but was probably
92 1.37 mrg * initialised to 0.
93 1.37 mrg */
94 1.37 mrg panic("cnopen: cn_tab->cn_dev == dev\n");
95 1.37 mrg }
96 1.33 leo
97 1.13 cgd if (cn_devvp == NULLVP) {
98 1.15 cgd /* try to get a reference on its vnode, but fail silently */
99 1.37 mrg cdevvp(cndev, &cn_devvp);
100 1.13 cgd }
101 1.37 mrg return ((*cdevsw[major(cndev)].d_open)(cndev, flag, mode, p));
102 1.1 cgd }
103 1.1 cgd
104 1.7 andrew int
105 1.1 cgd cnclose(dev, flag, mode, p)
106 1.1 cgd dev_t dev;
107 1.1 cgd int flag, mode;
108 1.1 cgd struct proc *p;
109 1.1 cgd {
110 1.10 cgd struct vnode *vp;
111 1.10 cgd
112 1.1 cgd if (cn_tab == NULL)
113 1.1 cgd return (0);
114 1.10 cgd
115 1.10 cgd /*
116 1.10 cgd * If the real console isn't otherwise open, close it.
117 1.10 cgd * If it's otherwise open, don't close it, because that'll
118 1.10 cgd * screw up others who have it open.
119 1.10 cgd */
120 1.1 cgd dev = cn_tab->cn_dev;
121 1.13 cgd if (cn_devvp != NULLVP) {
122 1.13 cgd /* release our reference to real dev's vnode */
123 1.13 cgd vrele(cn_devvp);
124 1.13 cgd cn_devvp = NULLVP;
125 1.13 cgd }
126 1.16 mycroft if (vfinddev(dev, VCHR, &vp) && vcount(vp))
127 1.10 cgd return (0);
128 1.1 cgd return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
129 1.1 cgd }
130 1.1 cgd
131 1.7 andrew int
132 1.1 cgd cnread(dev, uio, flag)
133 1.1 cgd dev_t dev;
134 1.1 cgd struct uio *uio;
135 1.24 cgd int flag;
136 1.1 cgd {
137 1.21 mycroft
138 1.10 cgd /*
139 1.10 cgd * If we would redirect input, punt. This will keep strange
140 1.10 cgd * things from happening to people who are using the real
141 1.10 cgd * console. Nothing should be using /dev/console for
142 1.10 cgd * input (except a shell in single-user mode, but then,
143 1.10 cgd * one wouldn't TIOCCONS then).
144 1.10 cgd */
145 1.10 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
146 1.10 cgd return 0;
147 1.10 cgd else if (cn_tab == NULL)
148 1.10 cgd return ENXIO;
149 1.10 cgd
150 1.1 cgd dev = cn_tab->cn_dev;
151 1.1 cgd return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
152 1.1 cgd }
153 1.1 cgd
154 1.7 andrew int
155 1.1 cgd cnwrite(dev, uio, flag)
156 1.1 cgd dev_t dev;
157 1.1 cgd struct uio *uio;
158 1.24 cgd int flag;
159 1.1 cgd {
160 1.21 mycroft
161 1.10 cgd /*
162 1.10 cgd * Redirect output, if that's appropriate.
163 1.10 cgd * If there's no real console, return ENXIO.
164 1.10 cgd */
165 1.10 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
166 1.2 cgd dev = constty->t_dev;
167 1.10 cgd else if (cn_tab == NULL)
168 1.10 cgd return ENXIO;
169 1.2 cgd else
170 1.2 cgd dev = cn_tab->cn_dev;
171 1.1 cgd return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
172 1.25 mycroft }
173 1.25 mycroft
174 1.31 mycroft void
175 1.25 mycroft cnstop(tp, flag)
176 1.25 mycroft struct tty *tp;
177 1.25 mycroft int flag;
178 1.25 mycroft {
179 1.31 mycroft
180 1.1 cgd }
181 1.1 cgd
182 1.7 andrew int
183 1.1 cgd cnioctl(dev, cmd, data, flag, p)
184 1.1 cgd dev_t dev;
185 1.20 cgd u_long cmd;
186 1.1 cgd caddr_t data;
187 1.20 cgd int flag;
188 1.1 cgd struct proc *p;
189 1.1 cgd {
190 1.1 cgd int error;
191 1.1 cgd
192 1.1 cgd /*
193 1.1 cgd * Superuser can always use this to wrest control of console
194 1.1 cgd * output from the "virtual" console.
195 1.1 cgd */
196 1.10 cgd if (cmd == TIOCCONS && constty != NULL) {
197 1.1 cgd error = suser(p->p_ucred, (u_short *) NULL);
198 1.1 cgd if (error)
199 1.1 cgd return (error);
200 1.1 cgd constty = NULL;
201 1.1 cgd return (0);
202 1.1 cgd }
203 1.10 cgd
204 1.10 cgd /*
205 1.10 cgd * Redirect the ioctl, if that's appropriate.
206 1.10 cgd * Note that strange things can happen, if a program does
207 1.10 cgd * ioctls on /dev/console, then the console is redirected
208 1.10 cgd * out from under it.
209 1.10 cgd */
210 1.12 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
211 1.10 cgd dev = constty->t_dev;
212 1.10 cgd else if (cn_tab == NULL)
213 1.10 cgd return ENXIO;
214 1.10 cgd else
215 1.10 cgd dev = cn_tab->cn_dev;
216 1.1 cgd return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
217 1.1 cgd }
218 1.1 cgd
219 1.1 cgd /*ARGSUSED*/
220 1.7 andrew int
221 1.32 mycroft cnpoll(dev, events, p)
222 1.1 cgd dev_t dev;
223 1.32 mycroft int events;
224 1.1 cgd struct proc *p;
225 1.1 cgd {
226 1.21 mycroft
227 1.10 cgd /*
228 1.38 lukem * Redirect the poll, if that's appropriate.
229 1.10 cgd * I don't want to think of the possible side effects
230 1.10 cgd * of console redirection here.
231 1.10 cgd */
232 1.12 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
233 1.10 cgd dev = constty->t_dev;
234 1.10 cgd else if (cn_tab == NULL)
235 1.10 cgd return ENXIO;
236 1.10 cgd else
237 1.10 cgd dev = cn_tab->cn_dev;
238 1.32 mycroft return ((*cdevsw[major(dev)].d_poll)(dev, events, p));
239 1.40.2.1 lukem }
240 1.40.2.1 lukem
241 1.40.2.1 lukem /*ARGSUSED*/
242 1.40.2.1 lukem int
243 1.40.2.1 lukem cnkqfilter(dev, kn)
244 1.40.2.1 lukem dev_t dev;
245 1.40.2.1 lukem struct knote *kn;
246 1.40.2.1 lukem {
247 1.40.2.1 lukem
248 1.40.2.1 lukem /*
249 1.40.2.1 lukem * Redirect the kqfilter, if that's appropriate.
250 1.40.2.1 lukem * I don't want to think of the possible side effects
251 1.40.2.1 lukem * of console redirection here.
252 1.40.2.1 lukem */
253 1.40.2.1 lukem if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
254 1.40.2.1 lukem dev = constty->t_dev;
255 1.40.2.1 lukem else if (cn_tab == NULL)
256 1.40.2.1 lukem return ENXIO;
257 1.40.2.1 lukem else
258 1.40.2.1 lukem dev = cn_tab->cn_dev;
259 1.40.2.1 lukem return ((*cdevsw[major(dev)].d_kqfilter)(dev, kn));
260 1.1 cgd }
261 1.1 cgd
262 1.7 andrew int
263 1.1 cgd cngetc()
264 1.1 cgd {
265 1.21 mycroft
266 1.1 cgd if (cn_tab == NULL)
267 1.1 cgd return (0);
268 1.1 cgd return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
269 1.36 itojun }
270 1.36 itojun
271 1.36 itojun int
272 1.36 itojun cngetsn(cp, size)
273 1.36 itojun char *cp;
274 1.36 itojun int size;
275 1.36 itojun {
276 1.36 itojun char *lp;
277 1.36 itojun int c, len;
278 1.36 itojun
279 1.36 itojun cnpollc(1);
280 1.36 itojun
281 1.36 itojun lp = cp;
282 1.36 itojun len = 0;
283 1.36 itojun for (;;) {
284 1.36 itojun c = cngetc();
285 1.36 itojun switch (c) {
286 1.36 itojun case '\n':
287 1.36 itojun case '\r':
288 1.36 itojun printf("\n");
289 1.36 itojun *lp++ = '\0';
290 1.36 itojun cnpollc(0);
291 1.36 itojun return (len);
292 1.36 itojun case '\b':
293 1.36 itojun case '\177':
294 1.36 itojun case '#':
295 1.36 itojun if (len) {
296 1.36 itojun --len;
297 1.36 itojun --lp;
298 1.36 itojun printf("\b \b");
299 1.36 itojun }
300 1.36 itojun continue;
301 1.36 itojun case '@':
302 1.40 jdolecek case 'u'&037: /* CTRL-u */
303 1.36 itojun len = 0;
304 1.36 itojun lp = cp;
305 1.36 itojun printf("\n");
306 1.36 itojun continue;
307 1.36 itojun default:
308 1.36 itojun if (len + 1 >= size || c < ' ') {
309 1.40 jdolecek printf("\007");
310 1.36 itojun continue;
311 1.36 itojun }
312 1.36 itojun printf("%c", c);
313 1.36 itojun ++len;
314 1.36 itojun *lp++ = c;
315 1.36 itojun }
316 1.36 itojun }
317 1.1 cgd }
318 1.1 cgd
319 1.29 christos void
320 1.1 cgd cnputc(c)
321 1.35 augustss int c;
322 1.1 cgd {
323 1.21 mycroft
324 1.1 cgd if (cn_tab == NULL)
325 1.29 christos return;
326 1.29 christos
327 1.1 cgd if (c) {
328 1.1 cgd (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
329 1.1 cgd if (c == '\n')
330 1.1 cgd (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
331 1.1 cgd }
332 1.19 mycroft }
333 1.19 mycroft
334 1.19 mycroft void
335 1.19 mycroft cnpollc(on)
336 1.19 mycroft int on;
337 1.19 mycroft {
338 1.19 mycroft static int refcount = 0;
339 1.19 mycroft
340 1.19 mycroft if (cn_tab == NULL)
341 1.19 mycroft return;
342 1.19 mycroft if (!on)
343 1.19 mycroft --refcount;
344 1.19 mycroft if (refcount == 0)
345 1.19 mycroft (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
346 1.19 mycroft if (on)
347 1.19 mycroft ++refcount;
348 1.21 mycroft }
349 1.21 mycroft
350 1.21 mycroft void
351 1.28 cgd nullcnpollc(dev, on)
352 1.28 cgd dev_t dev;
353 1.21 mycroft int on;
354 1.21 mycroft {
355 1.21 mycroft
356 1.34 thorpej }
357 1.34 thorpej
358 1.34 thorpej void
359 1.34 thorpej cnbell(pitch, period, volume)
360 1.34 thorpej u_int pitch, period, volume;
361 1.34 thorpej {
362 1.34 thorpej
363 1.34 thorpej if (cn_tab == NULL || cn_tab->cn_bell == NULL)
364 1.34 thorpej return;
365 1.34 thorpej (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
366 1.2 cgd }
367