cons.c revision 1.40.4.4 1 /* $NetBSD: cons.c,v 1.40.4.4 2001/09/26 15:28:09 fvdl Exp $ */
2
3 /*
4 * Copyright (c) 1988 University of Utah.
5 * Copyright (c) 1990, 1993
6 * The Regents of the University of California. All rights reserved.
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 *
40 * from: Utah $Hdr: cons.c 1.7 92/01/21$
41 *
42 * @(#)cons.c 8.2 (Berkeley) 1/12/94
43 */
44
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>
55
56 #include <miscfs/specfs/specdev.h>
57
58 #include <dev/cons.h>
59
60 struct tty *constty = NULL; /* virtual console output device */
61 struct consdev *cn_tab; /* physical console device info */
62
63 int
64 cnopen(devvp, mode, flag, p)
65 struct vnode *devvp;
66 int flag, mode;
67 struct proc *p;
68 {
69 int error;
70 dev_t cndev, rdev;
71 struct vnode *vp;
72
73 if (cn_tab == NULL)
74 return (0);
75
76
77 /*
78 * always open the 'real' console device, so we don't get nailed
79 * later. This follows normal device semantics; they always get
80 * open() calls.
81 */
82 cndev = cn_tab->cn_dev;
83 if (cndev == NODEV) {
84 /*
85 * This is most likely an error in the console attach
86 * code. Panicing looks better than jumping into nowhere
87 * through cdevsw below....
88 */
89 panic("cnopen: cn_tab->cn_dev == NODEV\n");
90 }
91 rdev = vdev_rdev(devvp);
92 if (rdev == cndev) {
93 /*
94 * This causes cnopen() to be called recursively, which
95 * is generally a bad thing. It is often caused when
96 * dev == 0 and cn_dev has not been set, but was probably
97 * initialised to 0.
98 */
99 panic("cnopen: cn_tab->cn_dev == dev\n");
100 }
101
102 if (vfinddev(cndev, VCHR, &vp) == 0) {
103 error = cdevvp(cndev, &vp);
104 if (error != 0)
105 return error;
106 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
107 } else
108 vget(vp, LK_EXCLUSIVE | LK_RETRY);
109
110 vdev_setprivdata(devvp, vp);
111
112 error = VOP_OPEN(vp, mode, p->p_ucred, p, NULL);
113 VOP_UNLOCK(vp, 0);
114 if (error != 0)
115 vrele(vp);
116 return error;
117 }
118
119 int
120 cnclose(devvp, flag, mode, p)
121 struct vnode *devvp;
122 int flag, mode;
123 struct proc *p;
124 {
125 struct vnode *vp;
126 int error;
127
128 if (cn_tab == NULL)
129 return (0);
130
131 vp = vdev_privdata(devvp);
132
133 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
134 error = VOP_CLOSE(vp, flag, p->p_ucred, p);
135 vput(vp);
136
137 return error;
138 }
139
140 int
141 cnread(devvp, uio, flag)
142 struct vnode *devvp;
143 struct uio *uio;
144 int flag;
145 {
146 int error;
147 struct vnode *vp;
148 struct proc *p = uio->uio_procp;
149
150 /*
151 * If we would redirect input, punt. This will keep strange
152 * things from happening to people who are using the real
153 * console. Nothing should be using /dev/console for
154 * input (except a shell in single-user mode, but then,
155 * one wouldn't TIOCCONS then).
156 */
157 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
158 return 0;
159 else if (cn_tab == NULL)
160 return ENXIO;
161
162 vp = vdev_privdata(devvp);
163
164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
165 error = VOP_READ(vp, uio, flag, p->p_ucred);
166 VOP_UNLOCK(vp, 0);
167
168 return error;
169 }
170
171 int
172 cnwrite(devvp, uio, flag)
173 struct vnode *devvp;
174 struct uio *uio;
175 int flag;
176 {
177 int error;
178 struct vnode *vp;
179 struct proc *p = uio->uio_procp;
180
181 /*
182 * Redirect output, if that's appropriate.
183 * If there's no real console, return ENXIO.
184 */
185 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
186 vp = constty->t_devvp;
187 else if (cn_tab == NULL)
188 return ENXIO;
189 else
190 vp = vdev_privdata(devvp);
191
192 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
193 error = VOP_WRITE(vp, uio, flag, p->p_ucred);
194 VOP_UNLOCK(vp, 0);
195 return error;
196 }
197
198 void
199 cnstop(tp, flag)
200 struct tty *tp;
201 int flag;
202 {
203
204 }
205
206 int
207 cnioctl(devvp, cmd, data, flag, p)
208 struct vnode *devvp;
209 u_long cmd;
210 caddr_t data;
211 int flag;
212 struct proc *p;
213 {
214 int error;
215 struct vnode *vp;
216
217 /*
218 * Superuser can always use this to wrest control of console
219 * output from the "virtual" console.
220 */
221 if (cmd == TIOCCONS && constty != NULL) {
222 error = suser(p->p_ucred, (u_short *) NULL);
223 if (error)
224 return (error);
225 constty = NULL;
226 return (0);
227 }
228
229 /*
230 * Redirect the ioctl, if that's appropriate.
231 * Note that strange things can happen, if a program does
232 * ioctls on /dev/console, then the console is redirected
233 * out from under it.
234 */
235 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
236 vp = constty->t_devvp;
237 else if (cn_tab == NULL)
238 return ENXIO;
239 else
240 vp = vdev_privdata(devvp);
241
242 return VOP_IOCTL(vp, cmd, data, flag, p->p_ucred, p);
243 }
244
245 /*ARGSUSED*/
246 int
247 cnpoll(devvp, events, p)
248 struct vnode *devvp;
249 int events;
250 struct proc *p;
251 {
252 struct vnode *vp;
253
254 /*
255 * Redirect the poll, if that's appropriate.
256 * I don't want to think of the possible side effects
257 * of console redirection here.
258 */
259 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
260 vp = constty->t_devvp;
261 else if (cn_tab == NULL)
262 return ENXIO;
263 else
264 vp = vdev_privdata(devvp);
265
266 return VOP_POLL(vp, events, p);
267 }
268
269 int
270 cngetc()
271 {
272
273 if (cn_tab == NULL)
274 return (0);
275 return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
276 }
277
278 int
279 cngetsn(cp, size)
280 char *cp;
281 int size;
282 {
283 char *lp;
284 int c, len;
285
286 cnpollc(1);
287
288 lp = cp;
289 len = 0;
290 for (;;) {
291 c = cngetc();
292 switch (c) {
293 case '\n':
294 case '\r':
295 printf("\n");
296 *lp++ = '\0';
297 cnpollc(0);
298 return (len);
299 case '\b':
300 case '\177':
301 case '#':
302 if (len) {
303 --len;
304 --lp;
305 printf("\b \b");
306 }
307 continue;
308 case '@':
309 case 'u'&037: /* CTRL-u */
310 len = 0;
311 lp = cp;
312 printf("\n");
313 continue;
314 default:
315 if (len + 1 >= size || c < ' ') {
316 printf("\007");
317 continue;
318 }
319 printf("%c", c);
320 ++len;
321 *lp++ = c;
322 }
323 }
324 }
325
326 void
327 cnputc(c)
328 int c;
329 {
330
331 if (cn_tab == NULL)
332 return;
333
334 if (c) {
335 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
336 if (c == '\n')
337 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
338 }
339 }
340
341 void
342 cnpollc(on)
343 int on;
344 {
345 static int refcount = 0;
346
347 if (cn_tab == NULL)
348 return;
349 if (!on)
350 --refcount;
351 if (refcount == 0)
352 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
353 if (on)
354 ++refcount;
355 }
356
357 void
358 nullcnpollc(dev, on)
359 dev_t dev;
360 int on;
361 {
362
363 }
364
365 void
366 cnbell(pitch, period, volume)
367 u_int pitch, period, volume;
368 {
369
370 if (cn_tab == NULL || cn_tab->cn_bell == NULL)
371 return;
372 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
373 }
374