kern_cons.c revision 620
171579Sdeischen/*
271579Sdeischen * Copyright (c) 1988 University of Utah.
371579Sdeischen * Copyright (c) 1991 The Regents of the University of California.
471579Sdeischen * All rights reserved.
571579Sdeischen *
671579Sdeischen * This code is derived from software contributed to Berkeley by
771579Sdeischen * the Systems Programming Group of the University of Utah Computer
871579Sdeischen * Science Department.
971579Sdeischen *
1071579Sdeischen * Redistribution and use in source and binary forms, with or without
1171579Sdeischen * modification, are permitted provided that the following conditions
1271579Sdeischen * are met:
1371579Sdeischen * 1. Redistributions of source code must retain the above copyright
1471579Sdeischen *    notice, this list of conditions and the following disclaimer.
1571579Sdeischen * 2. Redistributions in binary form must reproduce the above copyright
1671579Sdeischen *    notice, this list of conditions and the following disclaimer in the
1771579Sdeischen *    documentation and/or other materials provided with the distribution.
1871579Sdeischen * 3. All advertising materials mentioning features or use of this software
1971579Sdeischen *    must display the following acknowledgement:
2071579Sdeischen *	This product includes software developed by the University of
2171579Sdeischen *	California, Berkeley and its contributors.
2271579Sdeischen * 4. Neither the name of the University nor the names of its contributors
2371579Sdeischen *    may be used to endorse or promote products derived from this software
2471579Sdeischen *    without specific prior written permission.
2571579Sdeischen *
2671579Sdeischen * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2790039Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2890039Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2990039Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3075368Sdeischen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3171579Sdeischen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32205997Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33205997Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3475368Sdeischen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35218824Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36209876Snwhitehorn * SUCH DAMAGE.
3775368Sdeischen *
3871579Sdeischen *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
3971579Sdeischen *	$Id$
4071579Sdeischen */
4171579Sdeischen
4271579Sdeischen
4371579Sdeischen#include "sys/param.h"
4471579Sdeischen#include "sys/proc.h"
45#include "sys/user.h"
46#include "sys/systm.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
54#include "cons.h"
55
56/* XXX - all this could be autoconfig()ed */
57int pccnprobe(), pccninit(), pccngetc(), pccnputc();
58#include "com.h"
59#if NCOM > 0
60int comcnprobe(), comcninit(), comcngetc(), comcnputc();
61#endif
62
63struct	consdev constab[] = {
64	{ pccnprobe,	pccninit,	pccngetc,	pccnputc },
65#if NCOM > 0
66	{ comcnprobe,	comcninit,	comcngetc,	comcnputc },
67#endif
68	{ 0 },
69};
70/* end XXX */
71
72struct	tty *constty = 0;	/* virtual console output device */
73struct	consdev *cn_tab;	/* physical console device info */
74struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
75
76cninit()
77{
78	register struct consdev *cp;
79
80	/*
81	 * Collect information about all possible consoles
82	 * and find the one with highest priority
83	 */
84	for (cp = constab; cp->cn_probe; cp++) {
85		(*cp->cn_probe)(cp);
86		if (cp->cn_pri > CN_DEAD &&
87		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
88			cn_tab = cp;
89	}
90	/*
91	 * No console, we can handle it
92	 */
93	if ((cp = cn_tab) == NULL)
94		return;
95	/*
96	 * Turn on console
97	 */
98	cn_tty = cp->cn_tp;
99	(*cp->cn_init)(cp);
100}
101
102static struct vnode	*cnopenvp = NULLVP;
103
104
105cnopen(dev, flag, mode, p)
106	dev_t dev;
107	int flag, mode;
108	struct proc *p;
109{
110	int		error;
111
112
113	if (cn_tab == NULL)
114		return (0);
115	dev = cn_tab->cn_dev;
116	if (cnopenvp == NULLVP)
117		if ((error = getdevvp(dev, &cnopenvp, VCHR))) {
118			printf("cnopen: getdevvp returned %d !\n", error);
119			return(error);
120		}
121	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
122}
123
124cnclose(dev, flag, mode, p)
125	dev_t dev;
126	int flag, mode;
127	struct proc *p;
128{
129	int		error;
130
131
132	if (cn_tab == NULL)
133		return (0);
134	dev = cn_tab->cn_dev;
135	if (vcount(cnopenvp) <= 1)
136		error = (*cdevsw[major(dev)].d_close)(dev, flag, mode, p);
137	else
138		error = 0;
139	if (error == 0) {
140		vrele(cnopenvp);
141		cnopenvp = NULLVP;
142	return(error);
143	}
144}
145
146cnread(dev, uio, flag)
147	dev_t dev;
148	struct uio *uio;
149{
150	if (cn_tab == NULL)
151		return (0);
152	dev = cn_tab->cn_dev;
153	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
154}
155
156cnwrite(dev, uio, flag)
157	dev_t dev;
158	struct uio *uio;
159{
160	if (cn_tab == NULL)
161		return (0);
162	if (constty)					/* 16 Aug 92*/
163		dev = constty->t_dev;
164	else
165		dev = cn_tab->cn_dev;
166	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
167}
168
169cnioctl(dev, cmd, data, flag, p)
170	dev_t dev;
171	caddr_t data;
172	struct proc *p;
173{
174	int error;
175
176	if (cn_tab == NULL)
177		return (0);
178	/*
179	 * Superuser can always use this to wrest control of console
180	 * output from the "virtual" console.
181	 */
182	if (cmd == TIOCCONS && constty) {
183		error = suser(p->p_ucred, (u_short *) NULL);
184		if (error)
185			return (error);
186		constty = NULL;
187		return (0);
188	}
189	dev = cn_tab->cn_dev;
190	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
191}
192
193/*ARGSUSED*/
194cnselect(dev, rw, p)
195	dev_t dev;
196	int rw;
197	struct proc *p;
198{
199	if (cn_tab == NULL)
200		return (1);
201	return (ttselect(cn_tab->cn_dev, rw, p));
202}
203
204cngetc()
205{
206	if (cn_tab == NULL)
207		return (0);
208	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
209}
210
211cnputc(c)
212	register int c;
213{
214	if (cn_tab == NULL)
215		return;
216	if (c) {
217		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
218		if (c == '\n')
219			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
220	}
221}
222
223pg(p,q,r,s,t,u,v,w,x,y,z) char *p; {
224	printf(p,q,r,s,t,u,v,w,x,y,z);
225	printf("\n>");
226	return(cngetc());
227}
228
229
230