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