cons.c revision 1.33 1 1.33 leo /* $NetBSD: cons.c,v 1.33 1999/08/04 14:40:54 leo 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.21 mycroft
69 1.1 cgd if (cn_tab == NULL)
70 1.1 cgd return (0);
71 1.10 cgd
72 1.10 cgd /*
73 1.10 cgd * always open the 'real' console device, so we don't get nailed
74 1.10 cgd * later. This follows normal device semantics; they always get
75 1.10 cgd * open() calls.
76 1.10 cgd */
77 1.1 cgd dev = cn_tab->cn_dev;
78 1.33 leo if (dev == NODEV) {
79 1.33 leo /*
80 1.33 leo * This is most likely an error in the console attach
81 1.33 leo * code. Panicing looks better than jumping into nowhere
82 1.33 leo * through cdevsw below....
83 1.33 leo */
84 1.33 leo panic("cnopen: cn_tab->cn_dev == NODEV\n");
85 1.33 leo }
86 1.33 leo
87 1.13 cgd if (cn_devvp == NULLVP) {
88 1.15 cgd /* try to get a reference on its vnode, but fail silently */
89 1.15 cgd cdevvp(dev, &cn_devvp);
90 1.13 cgd }
91 1.23 mycroft return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
92 1.1 cgd }
93 1.1 cgd
94 1.7 andrew int
95 1.1 cgd cnclose(dev, flag, mode, p)
96 1.1 cgd dev_t dev;
97 1.1 cgd int flag, mode;
98 1.1 cgd struct proc *p;
99 1.1 cgd {
100 1.10 cgd struct vnode *vp;
101 1.10 cgd
102 1.1 cgd if (cn_tab == NULL)
103 1.1 cgd return (0);
104 1.10 cgd
105 1.10 cgd /*
106 1.10 cgd * If the real console isn't otherwise open, close it.
107 1.10 cgd * If it's otherwise open, don't close it, because that'll
108 1.10 cgd * screw up others who have it open.
109 1.10 cgd */
110 1.1 cgd dev = cn_tab->cn_dev;
111 1.13 cgd if (cn_devvp != NULLVP) {
112 1.13 cgd /* release our reference to real dev's vnode */
113 1.13 cgd vrele(cn_devvp);
114 1.13 cgd cn_devvp = NULLVP;
115 1.13 cgd }
116 1.16 mycroft if (vfinddev(dev, VCHR, &vp) && vcount(vp))
117 1.10 cgd return (0);
118 1.1 cgd return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
119 1.1 cgd }
120 1.1 cgd
121 1.7 andrew int
122 1.1 cgd cnread(dev, uio, flag)
123 1.1 cgd dev_t dev;
124 1.1 cgd struct uio *uio;
125 1.24 cgd int flag;
126 1.1 cgd {
127 1.21 mycroft
128 1.10 cgd /*
129 1.10 cgd * If we would redirect input, punt. This will keep strange
130 1.10 cgd * things from happening to people who are using the real
131 1.10 cgd * console. Nothing should be using /dev/console for
132 1.10 cgd * input (except a shell in single-user mode, but then,
133 1.10 cgd * one wouldn't TIOCCONS then).
134 1.10 cgd */
135 1.10 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
136 1.10 cgd return 0;
137 1.10 cgd else if (cn_tab == NULL)
138 1.10 cgd return ENXIO;
139 1.10 cgd
140 1.1 cgd dev = cn_tab->cn_dev;
141 1.1 cgd return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
142 1.1 cgd }
143 1.1 cgd
144 1.7 andrew int
145 1.1 cgd cnwrite(dev, uio, flag)
146 1.1 cgd dev_t dev;
147 1.1 cgd struct uio *uio;
148 1.24 cgd int flag;
149 1.1 cgd {
150 1.21 mycroft
151 1.10 cgd /*
152 1.10 cgd * Redirect output, if that's appropriate.
153 1.10 cgd * If there's no real console, return ENXIO.
154 1.10 cgd */
155 1.10 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
156 1.2 cgd dev = constty->t_dev;
157 1.10 cgd else if (cn_tab == NULL)
158 1.10 cgd return ENXIO;
159 1.2 cgd else
160 1.2 cgd dev = cn_tab->cn_dev;
161 1.1 cgd return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
162 1.25 mycroft }
163 1.25 mycroft
164 1.31 mycroft void
165 1.25 mycroft cnstop(tp, flag)
166 1.25 mycroft struct tty *tp;
167 1.25 mycroft int flag;
168 1.25 mycroft {
169 1.31 mycroft
170 1.1 cgd }
171 1.1 cgd
172 1.7 andrew int
173 1.1 cgd cnioctl(dev, cmd, data, flag, p)
174 1.1 cgd dev_t dev;
175 1.20 cgd u_long cmd;
176 1.1 cgd caddr_t data;
177 1.20 cgd int flag;
178 1.1 cgd struct proc *p;
179 1.1 cgd {
180 1.1 cgd int error;
181 1.1 cgd
182 1.1 cgd /*
183 1.1 cgd * Superuser can always use this to wrest control of console
184 1.1 cgd * output from the "virtual" console.
185 1.1 cgd */
186 1.10 cgd if (cmd == TIOCCONS && constty != NULL) {
187 1.1 cgd error = suser(p->p_ucred, (u_short *) NULL);
188 1.1 cgd if (error)
189 1.1 cgd return (error);
190 1.1 cgd constty = NULL;
191 1.1 cgd return (0);
192 1.1 cgd }
193 1.10 cgd
194 1.10 cgd /*
195 1.10 cgd * Redirect the ioctl, if that's appropriate.
196 1.10 cgd * Note that strange things can happen, if a program does
197 1.10 cgd * ioctls on /dev/console, then the console is redirected
198 1.10 cgd * out from under it.
199 1.10 cgd */
200 1.12 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
201 1.10 cgd dev = constty->t_dev;
202 1.10 cgd else if (cn_tab == NULL)
203 1.10 cgd return ENXIO;
204 1.10 cgd else
205 1.10 cgd dev = cn_tab->cn_dev;
206 1.1 cgd return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /*ARGSUSED*/
210 1.7 andrew int
211 1.32 mycroft cnpoll(dev, events, p)
212 1.1 cgd dev_t dev;
213 1.32 mycroft int events;
214 1.1 cgd struct proc *p;
215 1.1 cgd {
216 1.21 mycroft
217 1.10 cgd /*
218 1.10 cgd * Redirect the ioctl, if that's appropriate.
219 1.10 cgd * I don't want to think of the possible side effects
220 1.10 cgd * of console redirection here.
221 1.10 cgd */
222 1.12 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
223 1.10 cgd dev = constty->t_dev;
224 1.10 cgd else if (cn_tab == NULL)
225 1.10 cgd return ENXIO;
226 1.10 cgd else
227 1.10 cgd dev = cn_tab->cn_dev;
228 1.32 mycroft return ((*cdevsw[major(dev)].d_poll)(dev, events, p));
229 1.1 cgd }
230 1.1 cgd
231 1.7 andrew int
232 1.1 cgd cngetc()
233 1.1 cgd {
234 1.21 mycroft
235 1.1 cgd if (cn_tab == NULL)
236 1.1 cgd return (0);
237 1.1 cgd return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
238 1.1 cgd }
239 1.1 cgd
240 1.29 christos void
241 1.1 cgd cnputc(c)
242 1.1 cgd register int c;
243 1.1 cgd {
244 1.21 mycroft
245 1.1 cgd if (cn_tab == NULL)
246 1.29 christos return;
247 1.29 christos
248 1.1 cgd if (c) {
249 1.1 cgd (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
250 1.1 cgd if (c == '\n')
251 1.1 cgd (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
252 1.1 cgd }
253 1.19 mycroft }
254 1.19 mycroft
255 1.19 mycroft void
256 1.19 mycroft cnpollc(on)
257 1.19 mycroft int on;
258 1.19 mycroft {
259 1.19 mycroft static int refcount = 0;
260 1.19 mycroft
261 1.19 mycroft if (cn_tab == NULL)
262 1.19 mycroft return;
263 1.19 mycroft if (!on)
264 1.19 mycroft --refcount;
265 1.19 mycroft if (refcount == 0)
266 1.19 mycroft (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
267 1.19 mycroft if (on)
268 1.19 mycroft ++refcount;
269 1.21 mycroft }
270 1.21 mycroft
271 1.21 mycroft void
272 1.28 cgd nullcnpollc(dev, on)
273 1.28 cgd dev_t dev;
274 1.21 mycroft int on;
275 1.21 mycroft {
276 1.21 mycroft
277 1.2 cgd }
278