cons.c revision 1.44 1 1.44 provos /* $NetBSD: cons.c,v 1.44 2002/09/27 15:37:08 provos 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.41 lukem
45 1.41 lukem #include <sys/cdefs.h>
46 1.44 provos __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.44 2002/09/27 15:37:08 provos Exp $");
47 1.1 cgd
48 1.10 cgd #include <sys/param.h>
49 1.10 cgd #include <sys/proc.h>
50 1.10 cgd #include <sys/user.h>
51 1.10 cgd #include <sys/systm.h>
52 1.10 cgd #include <sys/buf.h>
53 1.10 cgd #include <sys/ioctl.h>
54 1.10 cgd #include <sys/tty.h>
55 1.10 cgd #include <sys/file.h>
56 1.10 cgd #include <sys/conf.h>
57 1.10 cgd #include <sys/vnode.h>
58 1.1 cgd
59 1.11 cgd #include <dev/cons.h>
60 1.1 cgd
61 1.43 gehenna dev_type_open(cnopen);
62 1.43 gehenna dev_type_close(cnclose);
63 1.43 gehenna dev_type_read(cnread);
64 1.43 gehenna dev_type_write(cnwrite);
65 1.43 gehenna dev_type_ioctl(cnioctl);
66 1.43 gehenna dev_type_poll(cnpoll);
67 1.43 gehenna
68 1.43 gehenna const struct cdevsw cons_cdevsw = {
69 1.43 gehenna cnopen, cnclose, cnread, cnwrite, cnioctl,
70 1.43 gehenna nostop, notty, cnpoll, nommap, D_TTY
71 1.43 gehenna };
72 1.43 gehenna
73 1.10 cgd struct tty *constty = NULL; /* virtual console output device */
74 1.1 cgd struct consdev *cn_tab; /* physical console device info */
75 1.13 cgd struct vnode *cn_devvp; /* vnode for underlying device. */
76 1.1 cgd
77 1.7 andrew int
78 1.1 cgd cnopen(dev, flag, mode, p)
79 1.1 cgd dev_t dev;
80 1.1 cgd int flag, mode;
81 1.1 cgd struct proc *p;
82 1.1 cgd {
83 1.43 gehenna const struct cdevsw *cdev;
84 1.37 mrg dev_t cndev;
85 1.21 mycroft
86 1.1 cgd if (cn_tab == NULL)
87 1.1 cgd return (0);
88 1.10 cgd
89 1.10 cgd /*
90 1.10 cgd * always open the 'real' console device, so we don't get nailed
91 1.10 cgd * later. This follows normal device semantics; they always get
92 1.10 cgd * open() calls.
93 1.10 cgd */
94 1.37 mrg cndev = cn_tab->cn_dev;
95 1.37 mrg if (cndev == NODEV) {
96 1.33 leo /*
97 1.33 leo * This is most likely an error in the console attach
98 1.33 leo * code. Panicing looks better than jumping into nowhere
99 1.33 leo * through cdevsw below....
100 1.33 leo */
101 1.44 provos panic("cnopen: no console device");
102 1.33 leo }
103 1.37 mrg if (dev == cndev) {
104 1.37 mrg /*
105 1.37 mrg * This causes cnopen() to be called recursively, which
106 1.37 mrg * is generally a bad thing. It is often caused when
107 1.37 mrg * dev == 0 and cn_dev has not been set, but was probably
108 1.37 mrg * initialised to 0.
109 1.37 mrg */
110 1.44 provos panic("cnopen: cn_tab->cn_dev == dev");
111 1.37 mrg }
112 1.43 gehenna cdev = cdevsw_lookup(cndev);
113 1.43 gehenna if (cdev == NULL)
114 1.43 gehenna return (ENXIO);
115 1.33 leo
116 1.13 cgd if (cn_devvp == NULLVP) {
117 1.15 cgd /* try to get a reference on its vnode, but fail silently */
118 1.37 mrg cdevvp(cndev, &cn_devvp);
119 1.13 cgd }
120 1.43 gehenna return ((*cdev->d_open)(cndev, flag, mode, p));
121 1.1 cgd }
122 1.1 cgd
123 1.7 andrew int
124 1.1 cgd cnclose(dev, flag, mode, p)
125 1.1 cgd dev_t dev;
126 1.1 cgd int flag, mode;
127 1.1 cgd struct proc *p;
128 1.1 cgd {
129 1.43 gehenna const struct cdevsw *cdev;
130 1.10 cgd struct vnode *vp;
131 1.10 cgd
132 1.1 cgd if (cn_tab == NULL)
133 1.1 cgd return (0);
134 1.10 cgd
135 1.10 cgd /*
136 1.10 cgd * If the real console isn't otherwise open, close it.
137 1.10 cgd * If it's otherwise open, don't close it, because that'll
138 1.10 cgd * screw up others who have it open.
139 1.10 cgd */
140 1.1 cgd dev = cn_tab->cn_dev;
141 1.43 gehenna cdev = cdevsw_lookup(dev);
142 1.43 gehenna if (cdev == NULL)
143 1.43 gehenna return (ENXIO);
144 1.13 cgd if (cn_devvp != NULLVP) {
145 1.13 cgd /* release our reference to real dev's vnode */
146 1.13 cgd vrele(cn_devvp);
147 1.13 cgd cn_devvp = NULLVP;
148 1.13 cgd }
149 1.16 mycroft if (vfinddev(dev, VCHR, &vp) && vcount(vp))
150 1.10 cgd return (0);
151 1.43 gehenna return ((*cdev->d_close)(dev, flag, mode, p));
152 1.1 cgd }
153 1.1 cgd
154 1.7 andrew int
155 1.1 cgd cnread(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.43 gehenna const struct cdevsw *cdev;
161 1.21 mycroft
162 1.10 cgd /*
163 1.10 cgd * If we would redirect input, punt. This will keep strange
164 1.10 cgd * things from happening to people who are using the real
165 1.10 cgd * console. Nothing should be using /dev/console for
166 1.10 cgd * input (except a shell in single-user mode, but then,
167 1.10 cgd * one wouldn't TIOCCONS then).
168 1.10 cgd */
169 1.10 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
170 1.10 cgd return 0;
171 1.10 cgd else if (cn_tab == NULL)
172 1.10 cgd return ENXIO;
173 1.10 cgd
174 1.1 cgd dev = cn_tab->cn_dev;
175 1.43 gehenna cdev = cdevsw_lookup(dev);
176 1.43 gehenna if (cdev == NULL)
177 1.43 gehenna return (ENXIO);
178 1.43 gehenna return ((*cdev->d_read)(dev, uio, flag));
179 1.1 cgd }
180 1.1 cgd
181 1.7 andrew int
182 1.1 cgd cnwrite(dev, uio, flag)
183 1.1 cgd dev_t dev;
184 1.1 cgd struct uio *uio;
185 1.24 cgd 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 * Redirect output, if that's appropriate.
191 1.10 cgd * If there's no real console, return ENXIO.
192 1.10 cgd */
193 1.10 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
194 1.2 cgd dev = constty->t_dev;
195 1.10 cgd else if (cn_tab == NULL)
196 1.10 cgd return ENXIO;
197 1.2 cgd else
198 1.2 cgd dev = cn_tab->cn_dev;
199 1.43 gehenna cdev = cdevsw_lookup(dev);
200 1.43 gehenna if (cdev == NULL)
201 1.43 gehenna return (ENXIO);
202 1.43 gehenna return ((*cdev->d_write)(dev, uio, flag));
203 1.25 mycroft }
204 1.25 mycroft
205 1.7 andrew int
206 1.1 cgd cnioctl(dev, cmd, data, flag, p)
207 1.1 cgd dev_t dev;
208 1.20 cgd u_long cmd;
209 1.1 cgd caddr_t data;
210 1.20 cgd int flag;
211 1.1 cgd struct proc *p;
212 1.1 cgd {
213 1.43 gehenna const struct cdevsw *cdev;
214 1.1 cgd int error;
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Superuser can always use this to wrest control of console
218 1.1 cgd * output from the "virtual" console.
219 1.1 cgd */
220 1.10 cgd if (cmd == TIOCCONS && constty != NULL) {
221 1.1 cgd error = suser(p->p_ucred, (u_short *) NULL);
222 1.1 cgd if (error)
223 1.1 cgd return (error);
224 1.1 cgd constty = NULL;
225 1.1 cgd return (0);
226 1.1 cgd }
227 1.10 cgd
228 1.10 cgd /*
229 1.10 cgd * Redirect the ioctl, if that's appropriate.
230 1.10 cgd * Note that strange things can happen, if a program does
231 1.10 cgd * ioctls on /dev/console, then the console is redirected
232 1.10 cgd * out from under it.
233 1.10 cgd */
234 1.12 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
235 1.10 cgd dev = constty->t_dev;
236 1.10 cgd else if (cn_tab == NULL)
237 1.10 cgd return ENXIO;
238 1.10 cgd else
239 1.10 cgd dev = cn_tab->cn_dev;
240 1.43 gehenna cdev = cdevsw_lookup(dev);
241 1.43 gehenna if (cdev == NULL)
242 1.43 gehenna return (ENXIO);
243 1.43 gehenna return ((*cdev->d_ioctl)(dev, cmd, data, flag, p));
244 1.1 cgd }
245 1.1 cgd
246 1.1 cgd /*ARGSUSED*/
247 1.7 andrew int
248 1.32 mycroft cnpoll(dev, events, p)
249 1.1 cgd dev_t dev;
250 1.32 mycroft int events;
251 1.1 cgd struct proc *p;
252 1.1 cgd {
253 1.43 gehenna const struct cdevsw *cdev;
254 1.21 mycroft
255 1.10 cgd /*
256 1.38 lukem * Redirect the poll, if that's appropriate.
257 1.10 cgd * I don't want to think of the possible side effects
258 1.10 cgd * of console redirection here.
259 1.10 cgd */
260 1.12 cgd if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
261 1.10 cgd dev = constty->t_dev;
262 1.10 cgd else if (cn_tab == NULL)
263 1.10 cgd return ENXIO;
264 1.10 cgd else
265 1.10 cgd dev = cn_tab->cn_dev;
266 1.43 gehenna cdev = cdevsw_lookup(dev);
267 1.43 gehenna if (cdev == NULL)
268 1.43 gehenna return (ENXIO);
269 1.43 gehenna return ((*cdev->d_poll)(dev, events, p));
270 1.1 cgd }
271 1.1 cgd
272 1.7 andrew int
273 1.1 cgd cngetc()
274 1.1 cgd {
275 1.21 mycroft
276 1.1 cgd if (cn_tab == NULL)
277 1.1 cgd return (0);
278 1.1 cgd return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
279 1.36 itojun }
280 1.36 itojun
281 1.36 itojun int
282 1.36 itojun cngetsn(cp, size)
283 1.36 itojun char *cp;
284 1.36 itojun int size;
285 1.36 itojun {
286 1.36 itojun char *lp;
287 1.36 itojun int c, len;
288 1.36 itojun
289 1.36 itojun cnpollc(1);
290 1.36 itojun
291 1.36 itojun lp = cp;
292 1.36 itojun len = 0;
293 1.36 itojun for (;;) {
294 1.36 itojun c = cngetc();
295 1.36 itojun switch (c) {
296 1.36 itojun case '\n':
297 1.36 itojun case '\r':
298 1.36 itojun printf("\n");
299 1.36 itojun *lp++ = '\0';
300 1.36 itojun cnpollc(0);
301 1.36 itojun return (len);
302 1.36 itojun case '\b':
303 1.36 itojun case '\177':
304 1.36 itojun case '#':
305 1.36 itojun if (len) {
306 1.36 itojun --len;
307 1.36 itojun --lp;
308 1.36 itojun printf("\b \b");
309 1.36 itojun }
310 1.36 itojun continue;
311 1.36 itojun case '@':
312 1.40 jdolecek case 'u'&037: /* CTRL-u */
313 1.36 itojun len = 0;
314 1.36 itojun lp = cp;
315 1.36 itojun printf("\n");
316 1.36 itojun continue;
317 1.36 itojun default:
318 1.36 itojun if (len + 1 >= size || c < ' ') {
319 1.40 jdolecek printf("\007");
320 1.36 itojun continue;
321 1.36 itojun }
322 1.36 itojun printf("%c", c);
323 1.36 itojun ++len;
324 1.36 itojun *lp++ = c;
325 1.36 itojun }
326 1.36 itojun }
327 1.1 cgd }
328 1.1 cgd
329 1.29 christos void
330 1.1 cgd cnputc(c)
331 1.35 augustss int c;
332 1.1 cgd {
333 1.21 mycroft
334 1.1 cgd if (cn_tab == NULL)
335 1.29 christos return;
336 1.29 christos
337 1.1 cgd if (c) {
338 1.1 cgd (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
339 1.1 cgd if (c == '\n')
340 1.1 cgd (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
341 1.1 cgd }
342 1.19 mycroft }
343 1.19 mycroft
344 1.19 mycroft void
345 1.19 mycroft cnpollc(on)
346 1.19 mycroft int on;
347 1.19 mycroft {
348 1.19 mycroft static int refcount = 0;
349 1.19 mycroft
350 1.19 mycroft if (cn_tab == NULL)
351 1.19 mycroft return;
352 1.19 mycroft if (!on)
353 1.19 mycroft --refcount;
354 1.19 mycroft if (refcount == 0)
355 1.19 mycroft (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
356 1.19 mycroft if (on)
357 1.19 mycroft ++refcount;
358 1.21 mycroft }
359 1.21 mycroft
360 1.21 mycroft void
361 1.28 cgd nullcnpollc(dev, on)
362 1.28 cgd dev_t dev;
363 1.21 mycroft int on;
364 1.21 mycroft {
365 1.21 mycroft
366 1.34 thorpej }
367 1.34 thorpej
368 1.34 thorpej void
369 1.34 thorpej cnbell(pitch, period, volume)
370 1.34 thorpej u_int pitch, period, volume;
371 1.34 thorpej {
372 1.34 thorpej
373 1.34 thorpej if (cn_tab == NULL || cn_tab->cn_bell == NULL)
374 1.34 thorpej return;
375 1.34 thorpej (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
376 1.2 cgd }
377