kern_cons.c revision 1005
1314125Sdelphij/*
296593Smarkm * Copyright (c) 1988 University of Utah.
396593Smarkm * Copyright (c) 1991 The Regents of the University of California.
4142429Snectar * All rights reserved.
596593Smarkm *
696593Smarkm * This code is derived from software contributed to Berkeley by
796593Smarkm * the Systems Programming Group of the University of Utah Computer
896593Smarkm * Science Department.
996593Smarkm *
1096593Smarkm * Redistribution and use in source and binary forms, with or without
1196593Smarkm * modification, are permitted provided that the following conditions
1296593Smarkm * are met:
1396593Smarkm * 1. Redistributions of source code must retain the above copyright
1496593Smarkm *    notice, this list of conditions and the following disclaimer.
1596593Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1696593Smarkm *    notice, this list of conditions and the following disclaimer in the
1796593Smarkm *    documentation and/or other materials provided with the distribution.
1896593Smarkm * 3. All advertising materials mentioning features or use of this software
1996593Smarkm *    must display the following acknowledgement:
20215698Ssimon *	This product includes software developed by the University of
21215698Ssimon *	California, Berkeley and its contributors.
22215698Ssimon * 4. Neither the name of the University nor the names of its contributors
23215698Ssimon *    may be used to endorse or promote products derived from this software
24215698Ssimon *    without specific prior written permission.
2596593Smarkm *
2696593Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2796593Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2896593Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2996593Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3096593Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3196593Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3296593Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3396593Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3496593Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3596593Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3696593Smarkm * SUCH DAMAGE.
3796593Smarkm *
3896593Smarkm *	from: @(#)cons.c	7.2 (Berkeley) 5/9/91
3996593Smarkm *	$Id: cons.c,v 1.8 1994/01/22 22:48:21 davidg Exp $
4096593Smarkm */
41276861Sjkim
42276861Sjkim
4396593Smarkm#include "sys/param.h"
4496593Smarkm#include "sys/proc.h"
45215698Ssimon#include "sys/user.h"
46215698Ssimon#include "sys/systm.h"
47215698Ssimon#include "sys/buf.h"
48215698Ssimon#include "sys/ioctl.h"
49314125Sdelphij#include "sys/tty.h"
50215698Ssimon#include "sys/file.h"
51142429Snectar#include "sys/conf.h"
52142429Snectar#include "machine/stdarg.h"
53276861Sjkim
54276861Sjkim#include "machine/cons.h"
55276861Sjkim
5696593Smarkm/* XXX - all this could be autoconfig()ed */
57314125Sdelphijint pccnprobe(), pccninit(), pccngetc(), pccnputc();
58314125Sdelphij
59314125Sdelphij#include "sio.h"
60314125Sdelphij#if NSIO > 0
61215698Ssimonint siocnprobe(), siocninit(), siocngetc(), siocnputc();
62314125Sdelphij#endif
63314125Sdelphij
64314125Sdelphij#include "com.h"
65276861Sjkim#if NCOM > 0
66215698Ssimonint comcnprobe(), comcninit(), comcngetc(), comcnputc();
6796593Smarkm#endif
6896593Smarkm
6996593Smarkmstruct	consdev constab[] = {
7096593Smarkm	{ pccnprobe,	pccninit,	pccngetc,	pccnputc },
7196593Smarkm#if NSIO > 0
7296593Smarkm	{ siocnprobe,	siocninit,	siocngetc,	siocnputc },
7396593Smarkm#endif
7496593Smarkm#if NCOM > 0
7596593Smarkm	{ comcnprobe,	comcninit,	comcngetc,	comcnputc },
7696593Smarkm#endif
7796593Smarkm	{ 0 },
7896593Smarkm};
7996593Smarkm/* end XXX */
8096593Smarkm
8196593Smarkmstruct	tty *constty = 0;	/* virtual console output device */
8296593Smarkmstruct	consdev *cn_tab;	/* physical console device info */
8396593Smarkmstruct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
8496593Smarkm
8596593Smarkmvoid
8696593Smarkmcninit()
8796593Smarkm{
8896593Smarkm	register struct consdev *cp;
8996593Smarkm
9096593Smarkm	/*
9196593Smarkm	 * Collect information about all possible consoles
9296593Smarkm	 * and find the one with highest priority
9396593Smarkm	 */
9496593Smarkm	for (cp = constab; cp->cn_probe; cp++) {
9596593Smarkm		(*cp->cn_probe)(cp);
9696593Smarkm		if (cp->cn_pri > CN_DEAD &&
9796593Smarkm		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
9896593Smarkm			cn_tab = cp;
9996593Smarkm	}
10096593Smarkm	/*
10196593Smarkm	 * No console, we can handle it
10296593Smarkm	 */
10396593Smarkm	if ((cp = cn_tab) == NULL)
10496593Smarkm		return;
10596593Smarkm	/*
10696593Smarkm	 * Turn on console
10796593Smarkm	 */
10896593Smarkm	cn_tty = cp->cn_tp;
10996593Smarkm	(*cp->cn_init)(cp);
11096593Smarkm}
11196593Smarkm
11296593Smarkmint
11396593Smarkmcnopen(dev, flag, mode, p)
11496593Smarkm	dev_t dev;
11596593Smarkm	int flag, mode;
11696593Smarkm	struct proc *p;
11796593Smarkm{
11896593Smarkm	if (cn_tab == NULL)
11996593Smarkm		return (0);
12096593Smarkm	dev = cn_tab->cn_dev;
12196593Smarkm	return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
12296593Smarkm}
12396593Smarkm
12496593Smarkmint
12596593Smarkmcnclose(dev, flag, mode, p)
12696593Smarkm	dev_t dev;
12796593Smarkm	int flag, mode;
12896593Smarkm	struct proc *p;
129142429Snectar{
13096593Smarkm	if (cn_tab == NULL)
131100946Snectar		return (0);
132314125Sdelphij	dev = cn_tab->cn_dev;
133215698Ssimon	return ((*cdevsw[major(dev)].d_close)(dev, flag, mode, p));
134215698Ssimon}
135215698Ssimon
136215698Ssimonint
13796593Smarkmcnread(dev, uio, flag)
138142429Snectar	dev_t dev;
13996593Smarkm	struct uio *uio;
14096593Smarkm	int flag;
14196593Smarkm{
14296593Smarkm	if (cn_tab == NULL)
143215698Ssimon		return (0);
144110010Smarkm	dev = cn_tab->cn_dev;
14596593Smarkm	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
14696593Smarkm}
14796593Smarkm
148276861Sjkimint
14996593Smarkmcnwrite(dev, uio, flag)
15096593Smarkm	dev_t dev;
15196593Smarkm	struct uio *uio;
152142429Snectar	int flag;
15396593Smarkm{
15496593Smarkm	if (cn_tab == NULL)
15596593Smarkm		return (0);
15696593Smarkm	if (constty)					/* 16 Aug 92*/
15796593Smarkm		dev = constty->t_dev;
158142429Snectar	else
15996593Smarkm		dev = cn_tab->cn_dev;
16096593Smarkm	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
16196593Smarkm}
162
163int
164cnioctl(dev, cmd, data, flag, p)
165	dev_t dev;
166	int cmd;
167	caddr_t data;
168	int flag;
169	struct proc *p;
170{
171	int error;
172
173	if (cn_tab == NULL)
174		return (0);
175	/*
176	 * Superuser can always use this to wrest control of console
177	 * output from the "virtual" console.
178	 */
179	if (cmd == TIOCCONS && constty) {
180		error = suser(p->p_ucred, (u_short *) NULL);
181		if (error)
182			return (error);
183		constty = NULL;
184		return (0);
185	}
186	dev = cn_tab->cn_dev;
187	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
188}
189
190/*ARGSUSED*/
191int
192cnselect(dev, rw, p)
193	dev_t dev;
194	int rw;
195	struct proc *p;
196{
197	if (cn_tab == NULL)
198		return (1);
199	return (ttselect(cn_tab->cn_dev, rw, p));
200}
201
202int
203cngetc()
204{
205	if (cn_tab == NULL)
206		return (0);
207	return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
208}
209
210void
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
223int
224pg(const char *p, ...) {
225  va_list args;
226  va_start(args, p);
227  printf("%r\n>", p, args);
228  return(cngetc());
229}
230
231
232