xref: /csrg-svn/sys/i386/i386/cons.c (revision 47986)
1*47986Sdonn /*
2*47986Sdonn  * Copyright (c) 1988 University of Utah.
3*47986Sdonn  * Copyright (c) 1990 The Regents of the University of California.
4*47986Sdonn  * All rights reserved.
5*47986Sdonn  *
6*47986Sdonn  * This code is derived from software contributed to Berkeley by
7*47986Sdonn  * the Systems Programming Group of the University of Utah Computer
8*47986Sdonn  * Science Department.
9*47986Sdonn  *
10*47986Sdonn  * Redistribution and use in source and binary forms are permitted provided
11*47986Sdonn  * that: (1) source distributions retain this entire copyright notice and
12*47986Sdonn  * comment, and (2) distributions including binaries display the following
13*47986Sdonn  * acknowledgement:  ``This product includes software developed by the
14*47986Sdonn  * University of California, Berkeley and its contributors'' in the
15*47986Sdonn  * documentation or other materials provided with the distribution and in
16*47986Sdonn  * all advertising materials mentioning features or use of this software.
17*47986Sdonn  * Neither the name of the University nor the names of its contributors may
18*47986Sdonn  * be used to endorse or promote products derived from this software without
19*47986Sdonn  * specific prior written permission.
20*47986Sdonn  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
21*47986Sdonn  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
22*47986Sdonn  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23*47986Sdonn  *
24*47986Sdonn  * from: Utah $Hdr: cons.c 1.1 90/07/09$
25*47986Sdonn  *
26*47986Sdonn  *	@(#)cons.c	7.5 (Berkeley) 12/16/90
27*47986Sdonn  */
28*47986Sdonn 
29*47986Sdonn #include "sys/param.h"
30*47986Sdonn #include "sys/user.h"
31*47986Sdonn #include "sys/systm.h"
32*47986Sdonn #include "sys/buf.h"
33*47986Sdonn #include "sys/ioctl.h"
34*47986Sdonn #include "sys/tty.h"
35*47986Sdonn #include "sys/file.h"
36*47986Sdonn #include "sys/conf.h"
37*47986Sdonn 
38*47986Sdonn #include "cons.h"
39*47986Sdonn 
40*47986Sdonn /* XXX - all this could be autoconfig()ed */
41*47986Sdonn #include "ite.h"
42*47986Sdonn #if NITE > 0
43*47986Sdonn int itecnprobe(), itecninit(), itecngetc(), itecnputc();
44*47986Sdonn #endif
45*47986Sdonn #include "dca.h"
46*47986Sdonn #if NDCA > 0
47*47986Sdonn int dcacnprobe(), dcacninit(), dcacngetc(), dcacnputc();
48*47986Sdonn #endif
49*47986Sdonn #include "dcm.h"
50*47986Sdonn #if NDCM > 0
51*47986Sdonn int dcmcnprobe(), dcmcninit(), dcmcngetc(), dcmcnputc();
52*47986Sdonn #endif
53*47986Sdonn 
54*47986Sdonn struct	consdev constab[] = {
55*47986Sdonn #if NITE > 0
56*47986Sdonn 	{ itecnprobe,	itecninit,	itecngetc,	itecnputc },
57*47986Sdonn #endif
58*47986Sdonn #if NDCA > 0
59*47986Sdonn 	{ dcacnprobe,	dcacninit,	dcacngetc,	dcacnputc },
60*47986Sdonn #endif
61*47986Sdonn #if NDCM > 0
62*47986Sdonn 	{ dcmcnprobe,	dcmcninit,	dcmcngetc,	dcmcnputc },
63*47986Sdonn #endif
64*47986Sdonn 	{ 0 },
65*47986Sdonn };
66*47986Sdonn /* end XXX */
67*47986Sdonn 
68*47986Sdonn struct	tty *constty = 0;	/* virtual console output device */
69*47986Sdonn struct	consdev *cn_tab;	/* physical console device info */
70*47986Sdonn struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
71*47986Sdonn 
72*47986Sdonn cninit()
73*47986Sdonn {
74*47986Sdonn 	register struct consdev *cp;
75*47986Sdonn 
76*47986Sdonn 	/*
77*47986Sdonn 	 * Collect information about all possible consoles
78*47986Sdonn 	 * and find the one with highest priority
79*47986Sdonn 	 */
80*47986Sdonn 	for (cp = constab; cp->cn_probe; cp++) {
81*47986Sdonn 		(*cp->cn_probe)(cp);
82*47986Sdonn 		if (cp->cn_pri > CN_DEAD &&
83*47986Sdonn 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
84*47986Sdonn 			cn_tab = cp;
85*47986Sdonn 	}
86*47986Sdonn 	/*
87*47986Sdonn 	 * No console, we can handle it
88*47986Sdonn 	 */
89*47986Sdonn 	if ((cp = cn_tab) == NULL)
90*47986Sdonn 		return;
91*47986Sdonn 	/*
92*47986Sdonn 	 * Turn on console
93*47986Sdonn 	 */
94*47986Sdonn 	cn_tty = cp->cn_tp;
95*47986Sdonn 	(*cp->cn_init)(cp);
96*47986Sdonn }
97*47986Sdonn 
98*47986Sdonn cnopen(dev, flag)
99*47986Sdonn 	dev_t dev;
100*47986Sdonn {
101*47986Sdonn 	if (cn_tab == NULL)
102*47986Sdonn 		return(0);
103*47986Sdonn 	dev = cn_tab->cn_dev;
104*47986Sdonn 	return ((*cdevsw[major(dev)].d_open)(dev, flag));
105*47986Sdonn }
106*47986Sdonn 
107*47986Sdonn cnclose(dev, flag)
108*47986Sdonn 	dev_t dev;
109*47986Sdonn {
110*47986Sdonn 	if (cn_tab == NULL)
111*47986Sdonn 		return(0);
112*47986Sdonn 	dev = cn_tab->cn_dev;
113*47986Sdonn 	return ((*cdevsw[major(dev)].d_close)(dev, flag));
114*47986Sdonn }
115*47986Sdonn 
116*47986Sdonn cnread(dev, uio, flag)
117*47986Sdonn 	dev_t dev;
118*47986Sdonn 	struct uio *uio;
119*47986Sdonn {
120*47986Sdonn 	if (cn_tab == NULL)
121*47986Sdonn 		return(0);
122*47986Sdonn 	dev = cn_tab->cn_dev;
123*47986Sdonn 	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
124*47986Sdonn }
125*47986Sdonn 
126*47986Sdonn cnwrite(dev, uio, flag)
127*47986Sdonn 	dev_t dev;
128*47986Sdonn 	struct uio *uio;
129*47986Sdonn {
130*47986Sdonn 	if (cn_tab == NULL)
131*47986Sdonn 		return(0);
132*47986Sdonn 	dev = cn_tab->cn_dev;
133*47986Sdonn 	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
134*47986Sdonn }
135*47986Sdonn 
136*47986Sdonn cnioctl(dev, cmd, data, flag)
137*47986Sdonn 	dev_t dev;
138*47986Sdonn 	caddr_t data;
139*47986Sdonn {
140*47986Sdonn 	int error;
141*47986Sdonn 
142*47986Sdonn 	if (cn_tab == NULL)
143*47986Sdonn 		return(0);
144*47986Sdonn 	/*
145*47986Sdonn 	 * Superuser can always use this to wrest control of console
146*47986Sdonn 	 * output from the "virtual" console.
147*47986Sdonn 	 */
148*47986Sdonn 	if (cmd == TIOCCONS && constty) {
149*47986Sdonn 		error = suser(u.u_cred, &u.u_acflag);
150*47986Sdonn 		if (error)
151*47986Sdonn 			return (error);
152*47986Sdonn 		constty = NULL;
153*47986Sdonn 		return (0);
154*47986Sdonn 	}
155*47986Sdonn 	dev = cn_tab->cn_dev;
156*47986Sdonn 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag));
157*47986Sdonn }
158*47986Sdonn 
159*47986Sdonn /*ARGSUSED*/
160*47986Sdonn cnselect(dev, rw)
161*47986Sdonn 	dev_t dev;
162*47986Sdonn 	int rw;
163*47986Sdonn {
164*47986Sdonn 	if (cn_tab == NULL)
165*47986Sdonn 		return(1);
166*47986Sdonn 	return(ttselect(cn_tab->cn_dev, rw));
167*47986Sdonn }
168*47986Sdonn 
169*47986Sdonn cngetc()
170*47986Sdonn {
171*47986Sdonn 	if (cn_tab == NULL)
172*47986Sdonn 		return(0);
173*47986Sdonn 	return((*cn_tab->cn_getc)(cn_tab->cn_dev));
174*47986Sdonn }
175*47986Sdonn 
176*47986Sdonn cnputc(c)
177*47986Sdonn 	register int c;
178*47986Sdonn {
179*47986Sdonn 	if (cn_tab == NULL)
180*47986Sdonn 		return;
181*47986Sdonn 	if (c) {
182*47986Sdonn 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
183*47986Sdonn 		if (c == '\n')
184*47986Sdonn 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
185*47986Sdonn 	}
186*47986Sdonn }
187