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