kern_cons.c revision 2415
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.13 1994/08/13 03:49:34 wollman Exp $
40 */
41
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/proc.h>
46#include <sys/user.h>
47#include <sys/buf.h>
48#include <sys/ioctl.h>
49#include <sys/tty.h>
50#include <sys/file.h>
51#include <sys/conf.h>
52#include <sys/vnode.h>
53#include <machine/stdarg.h>
54
55#include <machine/cons.h>
56
57/* XXX - all this could be autoconfig()ed */
58int pccnprobe(), pccninit(), pccngetc(), pccnputc();
59
60#include "sio.h"
61#if NSIO > 0
62int siocnprobe(), siocninit(), siocngetc(), siocnputc();
63#endif
64
65struct	consdev constab[] = {
66	{ pccnprobe,	pccninit,	pccngetc,	pccnputc },
67#if NSIO > 0
68	{ siocnprobe,	siocninit,	siocngetc,	siocnputc },
69#endif
70	{ 0 },
71};
72/* end XXX */
73
74struct	tty *constty = 0;	/* virtual console output device */
75struct	consdev *cn_tab;	/* physical console device info */
76struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
77
78void
79cninit()
80{
81	register struct consdev *cp;
82
83	/*
84	 * Collect information about all possible consoles
85	 * and find the one with highest priority
86	 */
87	for (cp = constab; cp->cn_probe; cp++) {
88		(*cp->cn_probe)(cp);
89		if (cp->cn_pri > CN_DEAD &&
90		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
91			cn_tab = cp;
92	}
93	/*
94	 * No console, we can handle it
95	 */
96	if ((cp = cn_tab) == NULL)
97		return;
98	/*
99	 * Turn on console
100	 */
101	cn_tty = cp->cn_tp;
102	(*cp->cn_init)(cp);
103}
104
105int
106cnopen(dev, flag, mode, p)
107	dev_t dev;
108	int flag, mode;
109	struct proc *p;
110{
111	struct vnode *vp = 0;
112
113	if (cn_tab == NULL)
114		return (0);
115
116	dev = cn_tab->cn_dev;
117	if (vfinddev(dev, VCHR, &vp) && vcount(vp))
118		return (0);
119
120	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
121}
122
123int
124cnclose(dev, flag, mode, p)
125	dev_t dev;
126	int flag, mode;
127	struct proc *p;
128{
129	struct vnode *vp = 0;
130
131	if (cn_tab == NULL)
132		return (0);
133
134	dev = cn_tab->cn_dev;
135	if (vfinddev(dev, VCHR, &vp) && vcount(vp))
136		return (0);
137
138	return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
139}
140
141int
142cnread(dev, uio, flag)
143	dev_t dev;
144	struct uio *uio;
145	int flag;
146{
147	if (cn_tab == NULL)
148		return (0);
149	dev = cn_tab->cn_dev;
150	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
151}
152
153int
154cnwrite(dev, uio, flag)
155	dev_t dev;
156	struct uio *uio;
157	int flag;
158{
159	if (cn_tab == NULL)
160		return (0);
161	if (constty)
162		dev = constty->t_dev;
163	else
164		dev = cn_tab->cn_dev;
165	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
166}
167
168int
169cnioctl(dev, cmd, data, flag, p)
170	dev_t dev;
171	int cmd;
172	caddr_t data;
173	int flag;
174	struct proc *p;
175{
176	int error;
177
178	if (cn_tab == NULL)
179		return (0);
180	/*
181	 * Superuser can always use this to wrest control of console
182	 * output from the "virtual" console.
183	 */
184	if (cmd == TIOCCONS && constty) {
185		error = suser(p->p_ucred, (u_short *) NULL);
186		if (error)
187			return (error);
188		constty = NULL;
189		return (0);
190	}
191	dev = cn_tab->cn_dev;
192	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
193}
194
195/*ARGSUSED*/
196int
197cnselect(dev, rw, p)
198	dev_t dev;
199	int rw;
200	struct proc *p;
201{
202	if (cn_tab == NULL)
203		return (1);
204	return (ttselect(cn_tab->cn_dev, rw, p));
205}
206
207int
208cngetc()
209{
210	if (cn_tab == NULL)
211		return (0);
212	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
213}
214
215void
216cnputc(c)
217	register int c;
218{
219	if (cn_tab == NULL)
220		return;
221	if (c) {
222		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
223		if (c == '\n')
224			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
225	}
226}
227
228int
229pg(const char *p, ...) {
230  va_list args;
231  va_start(args, p);
232  printf("%r\n>", p, args);
233  return(cngetc());
234}
235
236
237