kern_cons.c revision 10666
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.33 1995/09/10 18:57:25 bde Exp $
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/conf.h>
45#include <sys/proc.h>
46#include <sys/tty.h>
47
48#include <machine/cons.h>
49#include <machine/stdarg.h>
50
51/* XXX this should be config(8)ed. */
52#include "sc.h"
53#include "vt.h"
54#include "sio.h"
55static struct consdev constab[] = {
56#if NSC > 0
57	{ sccnprobe,	sccninit,	sccngetc,	sccncheckc,	sccnputc },
58#endif
59#if NVT > 0
60	{ pccnprobe,	pccninit,	pccngetc,	pccncheckc,	pccnputc },
61#endif
62#if NSIO > 0
63	{ siocnprobe,	siocninit,	siocngetc,	siocncheckc,	siocnputc },
64#endif
65	{ 0 },
66};
67
68struct	tty *constty = 0;	/* virtual console output device */
69struct	tty *cn_tty;		/* XXX: console tty struct for tprintf */
70int	cons_unavail = 0;	/* XXX:
71				 * physical console not available for
72				 * input (i.e., it is in graphics mode)
73				 */
74
75static u_char cn_is_open;	/* nonzero if logical console is open */
76static u_char cn_phys_is_open;	/* nonzero if physical console is open */
77static d_close_t *cn_phys_close;	/* physical device close function */
78static d_open_t *cn_phys_open;	/* physical device open function */
79static struct consdev *cn_tab;	/* physical console device info */
80static struct tty *cn_tp;	/* physical console tty struct */
81
82#ifdef DEVFS
83#include <sys/kernel.h>
84#include <sys/devfsext.h>
85
86static void cndev_init __P((void *));
87SYSINIT(cndev, SI_SUB_DEVFS, SI_ORDER_ANY, cndev_init, NULL)
88
89static void
90cndev_init(dummy)
91	void *dummy;
92{
93  void * x;
94/*            path	name		devsw   minor	type   uid gid perm*/
95   x=dev_add("/misc",	"console",	cnopen, 0,	DV_CHR, 0,  0, 0640);
96}
97#endif /* DEVFS */
98
99void
100cninit()
101{
102	struct consdev *best_cp, *cp;
103
104	/*
105	 * Find the first console with the highest priority.
106	 */
107	best_cp = NULL;
108	for (cp = constab; cp->cn_probe; cp++) {
109		(*cp->cn_probe)(cp);
110		if (cp->cn_pri > CN_DEAD &&
111		    (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
112			best_cp = cp;
113	}
114
115	/*
116	 * If no console, give up.
117	 */
118	if (best_cp == NULL) {
119		cn_tab = best_cp;
120		return;
121	}
122
123	/*
124	 * Initialize console, then attach to it.  This ordering allows
125	 * debugging using the previous console, if any.
126	 * XXX if there was a previous console, then its driver should
127	 * be informed when we forget about it.
128	 */
129	(*best_cp->cn_init)(best_cp);
130	cn_tab = best_cp;
131}
132
133void
134cninit_finish()
135{
136	struct cdevsw *cdp;
137
138	if (cn_tab == NULL)
139		return;
140
141	/*
142	 * Hook the open and close functions.
143	 */
144	cdp = &cdevsw[major(cn_tab->cn_dev)];
145	cn_phys_close = cdp->d_close;
146	cdp->d_close = cnclose;
147	cn_phys_open = cdp->d_open;
148	cdp->d_open = cnopen;
149	cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev);
150	/*
151	 * XXX there are too many tty pointers.  cn_tty is only used for
152	 * sysctl(CPU_CONSDEV) (not for tprintf like the above comment
153	 * says).  cn_tp in struct consdev hasn't been initialized
154	 * (except statically to NULL) or used (except to initialize
155	 * cn_tty to the wrong value) for a year or two.
156	 */
157	cn_tty = cn_tp;
158}
159
160int
161cnopen(dev, flag, mode, p)
162	dev_t dev;
163	int flag, mode;
164	struct proc *p;
165{
166	dev_t cndev, physdev;
167	int retval;
168
169	if (cn_tab == NULL)
170		return (0);
171	cndev = cn_tab->cn_dev;
172	physdev = (major(dev) == major(cndev) ? dev : cndev);
173	retval = (*cn_phys_open)(physdev, flag, mode, p);
174	if (retval == 0) {
175		if (dev == cndev)
176			cn_phys_is_open = 1;
177		else if (physdev == cndev)
178			cn_is_open = 1;
179	}
180	return (retval);
181}
182
183int
184cnclose(dev, flag, mode, p)
185	dev_t dev;
186	int flag, mode;
187	struct proc *p;
188{
189	dev_t cndev;
190
191	if (cn_tab == NULL)
192		return (0);
193	cndev = cn_tab->cn_dev;
194	if (dev == cndev) {
195		/* the physical device is about to be closed */
196		cn_phys_is_open = 0;
197		if (cn_is_open) {
198			if (cn_tp) {
199				/* perform a ttyhalfclose() */
200				/* reset session and proc group */
201				cn_tp->t_pgrp = NULL;
202				cn_tp->t_session = NULL;
203			}
204			return (0);
205		}
206	} else if (major(dev) != major(cndev)) {
207		/* the logical console is about to be closed */
208		cn_is_open = 0;
209		if (cn_phys_is_open)
210			return (0);
211		dev = cndev;
212	}
213	return ((*cn_phys_close)(dev, flag, mode, p));
214}
215
216int
217cnread(dev, uio, flag)
218	dev_t dev;
219	struct uio *uio;
220	int flag;
221{
222	if (cn_tab == NULL)
223		return (0);
224	dev = cn_tab->cn_dev;
225	return ((*cdevsw[major(dev)].d_read)(dev, uio, flag));
226}
227
228int
229cnwrite(dev, uio, flag)
230	dev_t dev;
231	struct uio *uio;
232	int flag;
233{
234	if (cn_tab == NULL)
235		return (0);
236	if (constty)
237		dev = constty->t_dev;
238	else
239		dev = cn_tab->cn_dev;
240	return ((*cdevsw[major(dev)].d_write)(dev, uio, flag));
241}
242
243int
244cnioctl(dev, cmd, data, flag, p)
245	dev_t dev;
246	int cmd;
247	caddr_t data;
248	int flag;
249	struct proc *p;
250{
251	int error;
252
253	if (cn_tab == NULL)
254		return (0);
255	/*
256	 * Superuser can always use this to wrest control of console
257	 * output from the "virtual" console.
258	 */
259	if (cmd == TIOCCONS && constty) {
260		error = suser(p->p_ucred, (u_short *) NULL);
261		if (error)
262			return (error);
263		constty = NULL;
264		return (0);
265	}
266	dev = cn_tab->cn_dev;
267	return ((*cdevsw[major(dev)].d_ioctl)(dev, cmd, data, flag, p));
268}
269
270int
271cnselect(dev, rw, p)
272	dev_t dev;
273	int rw;
274	struct proc *p;
275{
276	if (cn_tab == NULL)
277		return (1);
278
279	dev = cn_tab->cn_dev;
280
281	return ((*cdevsw[major(dev)].d_select)(dev, rw, p));
282}
283
284int
285cngetc()
286{
287	int c;
288	if (cn_tab == NULL)
289		return (0);
290	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
291	if (c == '\r') c = '\n'; /* console input is always ICRNL */
292	return (c);
293}
294
295int
296cncheckc()
297{
298	if (cn_tab == NULL)
299		return (0);
300	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
301}
302
303void
304cnputc(c)
305	register int c;
306{
307	if (cn_tab == NULL)
308		return;
309	if (c) {
310		if (c == '\n')
311			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
312		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
313	}
314}
315
316int
317pg(const char *p, ...) {
318  va_list args;
319  va_start(args, p);
320  printf("%r\n>", p, args);
321  return(cngetc());
322}
323
324
325