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