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