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