kern_cons.c revision 6712
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.21 1995/01/23 18:46:13 davidg 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;
79
80static bool_t cn_is_open;	/* nonzero if logical console is open */
81static bool_t cn_phys_is_open;	/* nonzero if physical console is open */
82static d_close_t *cn_phys_close;	/* physical device close function */
83static d_open_t *cn_phys_open;	/* physical device open function */
84static struct consdev *cn_tab;	/* physical console device info */
85
86void
87cninit()
88{
89	register struct consdev *cp;
90	struct cdevsw *cdp;
91
92	/*
93	 * Collect information about all possible consoles
94	 * and find the one with highest priority
95	 */
96	for (cp = constab; cp->cn_probe; cp++) {
97		(*cp->cn_probe)(cp);
98		if (cp->cn_pri > CN_DEAD &&
99		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
100			cn_tab = cp;
101	}
102	/*
103	 * No console, give up.
104	 */
105	if ((cp = cn_tab) == NULL)
106		return;
107	/*
108	 * Hook the open and close functions.
109	 */
110	cdp = &cdevsw[major(cn_tab->cn_dev)];
111	cn_phys_close = cdp->d_close;
112	cdp->d_close = cnclose;
113	cn_phys_open = cdp->d_open;
114	cdp->d_open = cnopen;
115	/*
116	 * Turn on console
117	 */
118	cn_tty = cp->cn_tp;
119	(*cp->cn_init)(cp);
120}
121
122int
123cnopen(dev, flag, mode, p)
124	dev_t dev;
125	int flag, mode;
126	struct proc *p;
127{
128	dev_t cndev, physdev;
129	int retval;
130
131	if (cn_tab == NULL)
132		return (0);
133	cndev = cn_tab->cn_dev;
134	physdev = (major(dev) == major(cndev) ? dev : cndev);
135	retval = (*cn_phys_open)(physdev, flag, mode, p);
136	if (retval == 0) {
137		if (dev == cndev)
138			cn_phys_is_open = 1;
139		else if (physdev == cndev)
140			cn_is_open = 1;
141	}
142	return (retval);
143}
144
145int
146cnclose(dev, flag, mode, p)
147	dev_t dev;
148	int flag, mode;
149	struct proc *p;
150{
151	dev_t cndev;
152
153	if (cn_tab == NULL)
154		return (0);
155	cndev = cn_tab->cn_dev;
156	if (dev == cndev) {
157		cn_phys_is_open = 0;
158		if (cn_is_open)
159			return (0);
160	} else if (major(dev) != major(cndev)) {
161		cn_is_open = 0;
162		if (cn_phys_is_open)
163			return (0);
164		dev = cndev;
165	}
166	return ((*cn_phys_close)(dev, flag, mode, p));
167}
168
169int
170cnread(dev, uio, flag)
171	dev_t dev;
172	struct uio *uio;
173	int flag;
174{
175	if (cn_tab == NULL)
176		return (0);
177	dev = cn_tab->cn_dev;
178	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
179}
180
181int
182cnwrite(dev, uio, flag)
183	dev_t dev;
184	struct uio *uio;
185	int flag;
186{
187	if (cn_tab == NULL)
188		return (0);
189	if (constty)
190		dev = constty->t_dev;
191	else
192		dev = cn_tab->cn_dev;
193	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
194}
195
196int
197cnioctl(dev, cmd, data, flag, p)
198	dev_t dev;
199	int cmd;
200	caddr_t data;
201	int flag;
202	struct proc *p;
203{
204	int error;
205
206	if (cn_tab == NULL)
207		return (0);
208	/*
209	 * Superuser can always use this to wrest control of console
210	 * output from the "virtual" console.
211	 */
212	if (cmd == TIOCCONS && constty) {
213		error = suser(p->p_ucred, (u_short *) NULL);
214		if (error)
215			return (error);
216		constty = NULL;
217		return (0);
218	}
219	dev = cn_tab->cn_dev;
220	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
221}
222
223/*ARGSUSED*/
224int
225cnselect(dev, rw, p)
226	dev_t dev;
227	int rw;
228	struct proc *p;
229{
230	if (cn_tab == NULL)
231		return (1);
232
233	dev = cn_tab->cn_dev;
234
235	return ((*cdevsw[major(dev)].d_select)(dev, rw, p));
236}
237
238int
239cngetc()
240{
241	int c;
242	if (cn_tab == NULL)
243		return (0);
244	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
245	if (c == '\r') c = '\n'; /* console input is always ICRNL */
246	return (c);
247}
248
249int
250cncheckc()
251{
252	if (cn_tab == NULL)
253		return (0);
254	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
255}
256
257void
258cnputc(c)
259	register int c;
260{
261	if (cn_tab == NULL)
262		return;
263	if (c) {
264		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
265		if (c == '\n')
266			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
267	}
268}
269
270int
271pg(const char *p, ...) {
272  va_list args;
273  va_start(args, p);
274  printf("%r\n>", p, args);
275  return(cngetc());
276}
277
278
279