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