cons.c revision 1.8.2.1 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.8.2.1 1993/08/20 08:35:59 cgd Exp $
40 */
41
42
43 #include "param.h"
44 #include "proc.h"
45 #include "user.h"
46 #include "systm.h"
47 #include "buf.h"
48 #include "ioctl.h"
49 #include "tty.h"
50 #include "file.h"
51 #include "conf.h"
52 #include "vnode.h"
53
54 #include "cons.h"
55
56 /* XXX - all this could be autoconfig()ed */
57 #include "pc.h"
58 #if NPC > 0
59 int pccnprobe(), pccninit(), pccngetc(), pccnputc();
60 #endif
61 #include "com.h"
62 #if NCOM > 0
63 int comcnprobe(), comcninit(), comcngetc(), comcnputc();
64 #endif
65
66 struct consdev constab[] = {
67 #if NPC > 0
68 { pccnprobe, pccninit, pccngetc, pccnputc },
69 #endif
70 #if NCOM > 0
71 { comcnprobe, comcninit, comcngetc, comcnputc },
72 #endif
73 { 0 },
74 };
75 /* end XXX */
76
77 struct tty *constty = 0; /* virtual console output device */
78 struct consdev *cn_tab; /* physical console device info */
79
80 void
81 consinit()
82 {
83 }
84
85 void
86 cninit()
87 {
88 register struct consdev *cp;
89
90 /*
91 * Collect information about all possible consoles
92 * and find the one with highest priority
93 */
94 for (cp = constab; cp->cn_probe; cp++) {
95 (*cp->cn_probe)(cp);
96 if (cp->cn_pri > CN_DEAD &&
97 (cn_tab == NULL || cp->cn_pri > cn_tab->cn_pri))
98 cn_tab = cp;
99 }
100 /*
101 * No console, we can handle it
102 */
103 if ((cp = cn_tab) == NULL)
104 return;
105 /*
106 * Turn on console
107 */
108 (*cp->cn_init)(cp);
109 }
110
111 static struct vnode *cnopenvp = NULLVP;
112
113 int
114 cnopen(dev, flag, mode, p)
115 dev_t dev;
116 int flag, mode;
117 struct proc *p;
118 {
119 int error;
120
121 if (cn_tab == NULL)
122 return (0);
123 dev = cn_tab->cn_dev;
124 if (cnopenvp == NULLVP) {
125 if ((error = cdevvp(dev, &cnopenvp))) {
126 printf("cnopen: getdevvp returned %d !\n", error);
127 return(error);
128 }
129 }
130 return ((*cdevsw[major(dev)].d_open)(dev, flag, mode, p));
131 }
132
133 int
134 cnclose(dev, flag, mode, p)
135 dev_t dev;
136 int flag, mode;
137 struct proc *p;
138 {
139 int error;
140
141 if (cn_tab == NULL)
142 return (0);
143 dev = cn_tab->cn_dev;
144 if (vcount(cnopenvp) <= 1)
145 error = (*cdevsw[major(dev)].d_close)(dev, flag, mode, p);
146 else
147 error = 0;
148 if (error == 0) {
149 vrele(cnopenvp);
150 cnopenvp = NULLVP;
151 return(error);
152 }
153 }
154
155 int
156 cnread(dev, uio, flag)
157 dev_t dev;
158 struct uio *uio;
159 {
160 if (cn_tab == NULL)
161 return (0);
162 dev = cn_tab->cn_dev;
163 return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
164 }
165
166 int
167 cnwrite(dev, uio, flag)
168 dev_t dev;
169 struct uio *uio;
170 {
171 if (cn_tab == NULL)
172 return (0);
173 if (constty) /* 16 Aug 92*/
174 dev = constty->t_dev;
175 else
176 dev = cn_tab->cn_dev;
177 return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
178 }
179
180 int
181 cnioctl(dev, cmd, data, flag, p)
182 dev_t dev;
183 caddr_t data;
184 struct proc *p;
185 {
186 int error;
187
188 if (cn_tab == NULL)
189 return (0);
190 /*
191 * Superuser can always use this to wrest control of console
192 * output from the "virtual" console.
193 */
194 if (cmd == TIOCCONS && constty) {
195 error = suser(p->p_ucred, (u_short *) NULL);
196 if (error)
197 return (error);
198 constty = NULL;
199 return (0);
200 }
201 dev = cn_tab->cn_dev;
202 return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
203 }
204
205 /*ARGSUSED*/
206 int
207 cnselect(dev, rw, p)
208 dev_t dev;
209 int rw;
210 struct proc *p;
211 {
212 if (cn_tab == NULL)
213 return (1);
214 return (ttselect(cn_tab->cn_dev, rw, p));
215 }
216
217 int
218 cngetc()
219 {
220 if (cn_tab == NULL)
221 return (0);
222 return ((*cn_tab->cn_getc)(cn_tab->cn_dev));
223 }
224
225 int
226 cnputc(c)
227 register int c;
228 {
229 if (cn_tab == NULL)
230 return;
231 if (c) {
232 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
233 if (c == '\n')
234 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
235 }
236 }
237
238 int pg_wait = 0;
239 pg(p,q,r,s,t,u,v,w,x,y,z) char *p; {
240 printf(p,q,r,s,t,u,v,w,x,y,z);
241 if (pg_wait) {
242 printf("\n>");
243 return(cngetc());
244 } else
245 return 0;
246 }
247