kern_cons.c revision 5764
1/*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
39 *	$Id: cons.c,v 1.19 1994/12/18 19:42:41 joerg Exp $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/proc.h>
46#include <sys/tty.h>
47
48#include <machine/cons.h>
49#include <machine/stdarg.h>
50
51/* XXX - all this could be autoconfig()ed */
52#include "sc.h"
53#include "vt.h"
54#if NSC > 0 || NVT > 0
55int pccnprobe(), pccninit(), pccngetc(), pccncheckc(), pccnputc();
56#endif
57
58#include "sio.h"
59#if NSIO > 0
60int siocnprobe(), siocninit(), siocngetc(), siocncheckc(), siocnputc();
61#endif
62
63static struct consdev constab[] = {
64#if NSC > 0 || NVT > 0
65	{ pccnprobe,	pccninit,	pccngetc,	pccncheckc,	pccnputc },
66#endif
67#if NSIO > 0
68	{ siocnprobe,	siocninit,	siocngetc,	siocncheckc,	siocnputc },
69#endif
70	{ 0 },
71};
72/* end XXX */
73
74struct	tty *constty = 0;	/* virtual console output device */
75struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
76
77/* XXX */
78typedef	u_char bool_t;
79typedef	int d_close_t __P((dev_t dev, int fflag, int devtype, struct proc *p));
80typedef	int d_open_t __P((dev_t dev, int oflags, int devtype, struct proc *p));
81
82static bool_t cn_is_open;	/* nonzero if logical console is open */
83static bool_t cn_phys_is_open;	/* nonzero if physical console is open */
84static d_close_t *cn_phys_close;	/* physical device close function */
85static d_open_t *cn_phys_open;	/* physical device open function */
86static struct consdev *cn_tab;	/* physical console device info */
87
88void
89cninit()
90{
91	register struct consdev *cp;
92	struct cdevsw *cdp;
93
94	/*
95	 * Collect information about all possible consoles
96	 * and find the one with highest priority
97	 */
98	for (cp = constab; cp->cn_probe; cp++) {
99		(*cp->cn_probe)(cp);
100		if (cp->cn_pri > CN_DEAD &&
101		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
102			cn_tab = cp;
103	}
104	/*
105	 * No console, give up.
106	 */
107	if ((cp = cn_tab) == NULL)
108		return;
109	/*
110	 * Hook the open and close functions.
111	 */
112	cdp = &cdevsw[major(cn_tab->cn_dev)];
113	cn_phys_close = cdp->d_close;
114	cdp->d_close = cnclose;
115	cn_phys_open = cdp->d_open;
116	cdp->d_open = cnopen;
117	/*
118	 * Turn on console
119	 */
120	cn_tty = cp->cn_tp;
121	(*cp->cn_init)(cp);
122}
123
124int
125cnopen(dev, flag, mode, p)
126	dev_t dev;
127	int flag, mode;
128	struct proc *p;
129{
130	dev_t cndev, physdev;
131	int retval;
132
133	if (cn_tab == NULL)
134		return (0);
135	cndev = cn_tab->cn_dev;
136	physdev = (major(dev) == major(cndev) ? dev : cndev);
137	retval = (*cn_phys_open)(physdev, flag, mode, p);
138	if (retval == 0) {
139		if (dev == cndev)
140			cn_phys_is_open = 1;
141		else if (physdev == cndev)
142			cn_is_open = 1;
143	}
144	return (retval);
145}
146
147int
148cnclose(dev, flag, mode, p)
149	dev_t dev;
150	int flag, mode;
151	struct proc *p;
152{
153	dev_t cndev;
154
155	if (cn_tab == NULL)
156		return (0);
157	cndev = cn_tab->cn_dev;
158	if (dev == cndev) {
159		cn_phys_is_open = 0;
160		if (cn_is_open)
161			return (0);
162	} else if (major(dev) != major(cndev)) {
163		cn_is_open = 0;
164		if (cn_phys_is_open)
165			return (0);
166		dev = cndev;
167	}
168	return ((*cn_phys_close)(dev, flag, mode, p));
169}
170
171int
172cnread(dev, uio, flag)
173	dev_t dev;
174	struct uio *uio;
175	int flag;
176{
177	if (cn_tab == NULL)
178		return (0);
179	dev = cn_tab->cn_dev;
180	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
181}
182
183int
184cnwrite(dev, uio, flag)
185	dev_t dev;
186	struct uio *uio;
187	int flag;
188{
189	if (cn_tab == NULL)
190		return (0);
191	if (constty)
192		dev = constty->t_dev;
193	else
194		dev = cn_tab->cn_dev;
195	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
196}
197
198int
199cnioctl(dev, cmd, data, flag, p)
200	dev_t dev;
201	int cmd;
202	caddr_t data;
203	int flag;
204	struct proc *p;
205{
206	int error;
207
208	if (cn_tab == NULL)
209		return (0);
210	/*
211	 * Superuser can always use this to wrest control of console
212	 * output from the "virtual" console.
213	 */
214	if (cmd == TIOCCONS && constty) {
215		error = suser(p->p_ucred, (u_short *) NULL);
216		if (error)
217			return (error);
218		constty = NULL;
219		return (0);
220	}
221	dev = cn_tab->cn_dev;
222	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
223}
224
225/*ARGSUSED*/
226int
227cnselect(dev, rw, p)
228	dev_t dev;
229	int rw;
230	struct proc *p;
231{
232	if (cn_tab == NULL)
233		return (1);
234	return (ttselect(cn_tab->cn_dev, rw, p));
235}
236
237int
238cngetc()
239{
240	int c;
241	if (cn_tab == NULL)
242		return (0);
243	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
244	if (c == '\r') c = '\n'; /* console input is always ICRNL */
245	return (c);
246}
247
248int
249cncheckc()
250{
251	if (cn_tab == NULL)
252		return (0);
253	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
254}
255
256void
257cnputc(c)
258	register int c;
259{
260	if (cn_tab == NULL)
261		return;
262	if (c) {
263		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
264		if (c == '\n')
265			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
266	}
267}
268
269int
270pg(const char *p, ...) {
271  va_list args;
272  va_start(args, p);
273  printf("%r\n>", p, args);
274  return(cngetc());
275}
276
277
278