xref: /csrg-svn/sys/hp/dev/cons.c (revision 41475)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1990 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  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: cons.c 1.4 88/12/03$
13  *
14  *	@(#)cons.c	7.1 (Berkeley) 05/08/90
15  */
16 
17 #include "param.h"
18 #include "user.h"
19 #include "systm.h"
20 #include "buf.h"
21 #include "ioctl.h"
22 #include "tty.h"
23 #include "file.h"
24 #include "conf.h"
25 
26 #include "cons.h"
27 
28 /* XXX - all this could be autoconfig()ed */
29 #include "ite.h"
30 #if NITE > 0
31 int itecnprobe(), itecninit(), itecngetc(), itecnputc();
32 #endif
33 #include "dca.h"
34 #if NDCA > 0
35 int dcacnprobe(), dcacninit(), dcacngetc(), dcacnputc();
36 #endif
37 
38 struct	consdev constab[] = {
39 #if NITE > 0
40 	{ itecnprobe,	itecninit,	itecngetc,	itecnputc },
41 #endif
42 #if NDCA > 0
43 	{ dcacnprobe,	dcacninit,	dcacngetc,	dcacnputc },
44 #endif
45 	{ 0 },
46 };
47 /* end XXX */
48 
49 extern	struct consdev constab[];
50 
51 struct	tty *constty = 0;	/* virtual console output device */
52 struct	consdev *cn_tab;	/* physical console device info */
53 struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
54 
55 cninit()
56 {
57 	register struct consdev *cp;
58 
59 	/*
60 	 * Collect information about all possible consoles
61 	 * and find the one with highest priority
62 	 */
63 	for (cp = constab; cp->cn_probe; cp++) {
64 		(*cp->cn_probe)(cp);
65 		if (cp->cn_pri > CN_DEAD &&
66 		    (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
67 			cn_tab = cp;
68 	}
69 	/*
70 	 * No console, we can handle it
71 	 */
72 	if ((cp = cn_tab) == NULL)
73 		return;
74 	/*
75 	 * Turn on console
76 	 */
77 	cn_tty = cp->cn_tp;
78 	(*cp->cn_init)(cp);
79 }
80 
81 cnopen(dev, flag)
82 	dev_t dev;
83 {
84 	if (cn_tab == NULL)
85 		return(0);
86 	dev = cn_tab->cn_dev;
87 	return ((*cdevsw[major(dev)].d_open)(dev, flag));
88 }
89 
90 cnclose(dev, flag)
91 	dev_t dev;
92 {
93 	if (cn_tab == NULL)
94 		return(0);
95 	dev = cn_tab->cn_dev;
96 	return ((*cdevsw[major(dev)].d_close)(dev, flag));
97 }
98 
99 cnread(dev, uio)
100 	dev_t dev;
101 	struct uio *uio;
102 {
103 	if (cn_tab == NULL)
104 		return(0);
105 	dev = cn_tab->cn_dev;
106 	return ((*cdevsw[major(dev)].d_read)(dev, uio));
107 }
108 
109 cnwrite(dev, uio)
110 	dev_t dev;
111 	struct uio *uio;
112 {
113 	if (cn_tab == NULL)
114 		return(0);
115 	dev = cn_tab->cn_dev;
116 	return ((*cdevsw[major(dev)].d_write)(dev, uio));
117 }
118 
119 cnioctl(dev, cmd, data, flag)
120 	dev_t dev;
121 	caddr_t data;
122 {
123 	int error;
124 
125 	if (cn_tab == NULL)
126 		return(0);
127 	/*
128 	 * Superuser can always use this to wrest control of console
129 	 * output from the "virtual" console.
130 	 */
131 	if (cmd == TIOCCONS && constty) {
132 		error = suser(u.u_cred, &u.u_acflag);
133 		if (error)
134 			return (error);
135 		constty = NULL;
136 		return (0);
137 	}
138 	dev = cn_tab->cn_dev;
139 	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag));
140 }
141 
142 /*ARGSUSED*/
143 cnselect(dev, rw)
144 	dev_t dev;
145 	int rw;
146 {
147 	if (cn_tab == NULL)
148 		return(1);
149 	return(ttselect(cn_tab->cn_dev, rw));
150 }
151 
152 cngetc()
153 {
154 	if (cn_tab == NULL)
155 		return(0);
156 	return((*cn_tab->cn_getc)(cn_tab->cn_dev));
157 }
158 
159 cnputc(c)
160 	register int c;
161 {
162 	if (cn_tab == NULL)
163 		return;
164 	if (c) {
165 		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
166 		if (c == '\n')
167 			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
168 	}
169 }
170