cons.c revision 1.49 1 /* $NetBSD: cons.c,v 1.49 2003/08/07 16:30:51 agc Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * from: Utah $Hdr: cons.c 1.7 92/01/21$
36 *
37 * @(#)cons.c 8.2 (Berkeley) 1/12/94
38 */
39
40 /*
41 * Copyright (c) 1988 University of Utah.
42 *
43 * This code is derived from software contributed to Berkeley by
44 * the Systems Programming Group of the University of Utah Computer
45 * Science Department.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * from: Utah $Hdr: cons.c 1.7 92/01/21$
76 *
77 * @(#)cons.c 8.2 (Berkeley) 1/12/94
78 */
79
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: cons.c,v 1.49 2003/08/07 16:30:51 agc Exp $");
82
83 #include <sys/param.h>
84 #include <sys/proc.h>
85 #include <sys/user.h>
86 #include <sys/systm.h>
87 #include <sys/buf.h>
88 #include <sys/ioctl.h>
89 #include <sys/tty.h>
90 #include <sys/file.h>
91 #include <sys/conf.h>
92 #include <sys/vnode.h>
93
94 #include <dev/cons.h>
95
96 dev_type_open(cnopen);
97 dev_type_close(cnclose);
98 dev_type_read(cnread);
99 dev_type_write(cnwrite);
100 dev_type_ioctl(cnioctl);
101 dev_type_poll(cnpoll);
102 dev_type_kqfilter(cnkqfilter);
103
104 const struct cdevsw cons_cdevsw = {
105 cnopen, cnclose, cnread, cnwrite, cnioctl,
106 nostop, notty, cnpoll, nommap, cnkqfilter, D_TTY
107 };
108
109 struct tty *constty = NULL; /* virtual console output device */
110 struct consdev *cn_tab; /* physical console device info */
111 struct vnode *cn_devvp; /* vnode for underlying device. */
112
113 int
114 cnopen(dev_t dev, int flag, int mode, struct proc *p)
115 {
116 const struct cdevsw *cdev;
117 dev_t cndev;
118
119 if (cn_tab == NULL)
120 return (0);
121
122 /*
123 * always open the 'real' console device, so we don't get nailed
124 * later. This follows normal device semantics; they always get
125 * open() calls.
126 */
127 cndev = cn_tab->cn_dev;
128 if (cndev == NODEV) {
129 /*
130 * This is most likely an error in the console attach
131 * code. Panicing looks better than jumping into nowhere
132 * through cdevsw below....
133 */
134 panic("cnopen: no console device");
135 }
136 if (dev == cndev) {
137 /*
138 * This causes cnopen() to be called recursively, which
139 * is generally a bad thing. It is often caused when
140 * dev == 0 and cn_dev has not been set, but was probably
141 * initialised to 0.
142 */
143 panic("cnopen: cn_tab->cn_dev == dev");
144 }
145 cdev = cdevsw_lookup(cndev);
146 if (cdev == NULL)
147 return (ENXIO);
148
149 if (cn_devvp == NULLVP) {
150 /* try to get a reference on its vnode, but fail silently */
151 cdevvp(cndev, &cn_devvp);
152 }
153 return ((*cdev->d_open)(cndev, flag, mode, p));
154 }
155
156 int
157 cnclose(dev_t dev, int flag, int mode, struct proc *p)
158 {
159 const struct cdevsw *cdev;
160 struct vnode *vp;
161
162 if (cn_tab == NULL)
163 return (0);
164
165 /*
166 * If the real console isn't otherwise open, close it.
167 * If it's otherwise open, don't close it, because that'll
168 * screw up others who have it open.
169 */
170 dev = cn_tab->cn_dev;
171 cdev = cdevsw_lookup(dev);
172 if (cdev == NULL)
173 return (ENXIO);
174 if (cn_devvp != NULLVP) {
175 /* release our reference to real dev's vnode */
176 vrele(cn_devvp);
177 cn_devvp = NULLVP;
178 }
179 if (vfinddev(dev, VCHR, &vp) && vcount(vp))
180 return (0);
181 return ((*cdev->d_close)(dev, flag, mode, p));
182 }
183
184 int
185 cnread(dev_t dev, struct uio *uio, int flag)
186 {
187 const struct cdevsw *cdev;
188
189 /*
190 * If we would redirect input, punt. This will keep strange
191 * things from happening to people who are using the real
192 * console. Nothing should be using /dev/console for
193 * input (except a shell in single-user mode, but then,
194 * one wouldn't TIOCCONS then).
195 */
196 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
197 return 0;
198 else if (cn_tab == NULL)
199 return ENXIO;
200
201 dev = cn_tab->cn_dev;
202 cdev = cdevsw_lookup(dev);
203 if (cdev == NULL)
204 return (ENXIO);
205 return ((*cdev->d_read)(dev, uio, flag));
206 }
207
208 int
209 cnwrite(dev_t dev, struct uio *uio, int flag)
210 {
211 const struct cdevsw *cdev;
212
213 /*
214 * Redirect output, if that's appropriate.
215 * If there's no real console, return ENXIO.
216 */
217 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
218 dev = constty->t_dev;
219 else if (cn_tab == NULL)
220 return ENXIO;
221 else
222 dev = cn_tab->cn_dev;
223 cdev = cdevsw_lookup(dev);
224 if (cdev == NULL)
225 return (ENXIO);
226 return ((*cdev->d_write)(dev, uio, flag));
227 }
228
229 int
230 cnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
231 {
232 const struct cdevsw *cdev;
233 int error;
234
235 /*
236 * Superuser can always use this to wrest control of console
237 * output from the "virtual" console.
238 */
239 if (cmd == TIOCCONS && constty != NULL) {
240 error = suser(p->p_ucred, (u_short *) NULL);
241 if (error)
242 return (error);
243 constty = NULL;
244 return (0);
245 }
246
247 /*
248 * Redirect the ioctl, if that's appropriate.
249 * Note that strange things can happen, if a program does
250 * ioctls on /dev/console, then the console is redirected
251 * out from under it.
252 */
253 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
254 dev = constty->t_dev;
255 else if (cn_tab == NULL)
256 return ENXIO;
257 else
258 dev = cn_tab->cn_dev;
259 cdev = cdevsw_lookup(dev);
260 if (cdev == NULL)
261 return (ENXIO);
262 return ((*cdev->d_ioctl)(dev, cmd, data, flag, p));
263 }
264
265 /*ARGSUSED*/
266 int
267 cnpoll(dev_t dev, int events, struct proc *p)
268 {
269 const struct cdevsw *cdev;
270
271 /*
272 * Redirect the poll, if that's appropriate.
273 * I don't want to think of the possible side effects
274 * of console redirection here.
275 */
276 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
277 dev = constty->t_dev;
278 else if (cn_tab == NULL)
279 return ENXIO;
280 else
281 dev = cn_tab->cn_dev;
282 cdev = cdevsw_lookup(dev);
283 if (cdev == NULL)
284 return (ENXIO);
285 return ((*cdev->d_poll)(dev, events, p));
286 }
287
288 /*ARGSUSED*/
289 int
290 cnkqfilter(dev_t dev, struct knote *kn)
291 {
292 const struct cdevsw *cdev;
293
294 /*
295 * Redirect the kqfilter, if that's appropriate.
296 * I don't want to think of the possible side effects
297 * of console redirection here.
298 */
299 if (constty != NULL && (cn_tab == NULL || cn_tab->cn_pri != CN_REMOTE))
300 dev = constty->t_dev;
301 else if (cn_tab == NULL)
302 return ENXIO;
303 else
304 dev = cn_tab->cn_dev;
305 cdev = cdevsw_lookup(dev);
306 if (cdev == NULL)
307 return (ENXIO);
308 return ((*cdev->d_kqfilter)(dev, kn));
309 }
310
311 int
312 cngetc(void)
313 {
314 if (cn_tab == NULL)
315 return (0);
316 return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
317 }
318
319 int
320 cngetsn(char *cp, int size)
321 {
322 char *lp;
323 int c, len;
324
325 cnpollc(1);
326
327 lp = cp;
328 len = 0;
329 for (;;) {
330 c = cngetc();
331 switch (c) {
332 case '\n':
333 case '\r':
334 printf("\n");
335 *lp++ = '\0';
336 cnpollc(0);
337 return (len);
338 case '\b':
339 case '\177':
340 case '#':
341 if (len) {
342 --len;
343 --lp;
344 printf("\b \b");
345 }
346 continue;
347 case '@':
348 case 'u'&037: /* CTRL-u */
349 len = 0;
350 lp = cp;
351 printf("\n");
352 continue;
353 default:
354 if (len + 1 >= size || c < ' ') {
355 printf("\007");
356 continue;
357 }
358 printf("%c", c);
359 ++len;
360 *lp++ = c;
361 }
362 }
363 }
364
365 void
366 cnputc(int c)
367 {
368
369 if (cn_tab == NULL)
370 return;
371
372 if (c) {
373 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
374 if (c == '\n')
375 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
376 }
377 }
378
379 void
380 cnpollc(int on)
381 {
382 static int refcount = 0;
383
384 if (cn_tab == NULL)
385 return;
386 if (!on)
387 --refcount;
388 if (refcount == 0)
389 (*cn_tab->cn_pollc)(cn_tab->cn_dev, on);
390 if (on)
391 ++refcount;
392 }
393
394 void
395 nullcnpollc(dev_t dev, int on)
396 {
397
398 }
399
400 void
401 cnbell(u_int pitch, u_int period, u_int volume)
402 {
403
404 if (cn_tab == NULL || cn_tab->cn_bell == NULL)
405 return;
406 (*cn_tab->cn_bell)(cn_tab->cn_dev, pitch, period, volume);
407 }
408
409 void
410 cnflush(void)
411 {
412 if (cn_tab == NULL || cn_tab->cn_flush == NULL)
413 return;
414 (*cn_tab->cn_flush)(cn_tab->cn_dev);
415 }
416
417 void
418 cnhalt(void)
419 {
420 if (cn_tab == NULL || cn_tab->cn_halt == NULL)
421 return;
422 (*cn_tab->cn_halt)(cn_tab->cn_dev);
423 }
424