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