kern_cons.c revision 14845
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.41 1995/12/22 13:09:30 phk 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/sysctl.h>
50#include <sys/proc.h>
51#include <sys/tty.h>
52
53#include <machine/cpu.h>
54#include <machine/cons.h>
55#include <machine/stdarg.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", "");
92
93int	cons_unavail = 0;	/* XXX:
94				 * physical console not available for
95				 * input (i.e., it is in graphics mode)
96				 */
97
98static u_char cn_is_open;	/* nonzero if logical console is open */
99static u_char cn_phys_is_open;	/* nonzero if physical console is open */
100static d_close_t *cn_phys_close;	/* physical device close function */
101static d_open_t *cn_phys_open;	/* physical device open function */
102static struct consdev *cn_tab;	/* physical console device info */
103static struct tty *cn_tp;	/* physical console tty struct */
104#ifdef DEVFS
105void *cn_devfs_token;		/* represents the devfs entry */
106#endif /* DEVFS */
107
108void
109cninit()
110{
111	struct consdev *best_cp, *cp;
112
113	/*
114	 * Find the first console with the highest priority.
115	 */
116	best_cp = NULL;
117	for (cp = constab; cp->cn_probe; cp++) {
118		(*cp->cn_probe)(cp);
119		if (cp->cn_pri > CN_DEAD &&
120		    (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
121			best_cp = cp;
122	}
123
124	/*
125	 * If no console, give up.
126	 */
127	if (best_cp == NULL) {
128		cn_tab = best_cp;
129		return;
130	}
131
132	/*
133	 * Initialize console, then attach to it.  This ordering allows
134	 * debugging using the previous console, if any.
135	 * XXX if there was a previous console, then its driver should
136	 * be informed when we forget about it.
137	 */
138	(*best_cp->cn_init)(best_cp);
139	cn_tab = best_cp;
140}
141
142void
143cninit_finish()
144{
145	struct cdevsw *cdp;
146
147	if (cn_tab == NULL)
148		return;
149
150	/*
151	 * Hook the open and close functions.
152	 */
153	cdp = cdevsw[major(cn_tab->cn_dev)];
154	cn_phys_close = cdp->d_close;
155	cdp->d_close = cnclose;
156	cn_phys_open = cdp->d_open;
157	cdp->d_open = cnopen;
158	cn_tp = (*cdp->d_devtotty)(cn_tab->cn_dev);
159	cn_dev_t = cn_tp->t_dev;
160}
161
162static int
163cnopen(dev, flag, mode, p)
164	dev_t dev;
165	int flag, mode;
166	struct proc *p;
167{
168	dev_t cndev, physdev;
169	int retval;
170
171	if (cn_tab == NULL)
172		return (0);
173	cndev = cn_tab->cn_dev;
174	physdev = (major(dev) == major(cndev) ? dev : cndev);
175	retval = (*cn_phys_open)(physdev, flag, mode, p);
176	if (retval == 0) {
177		if (dev == cndev)
178			cn_phys_is_open = 1;
179		else if (physdev == cndev)
180			cn_is_open = 1;
181	}
182	return (retval);
183}
184
185static int
186cnclose(dev, flag, mode, p)
187	dev_t dev;
188	int flag, mode;
189	struct proc *p;
190{
191	dev_t cndev;
192
193	if (cn_tab == NULL)
194		return (0);
195	cndev = cn_tab->cn_dev;
196	if (dev == cndev) {
197		/* the physical device is about to be closed */
198		cn_phys_is_open = 0;
199		if (cn_is_open) {
200			if (cn_tp) {
201				/* perform a ttyhalfclose() */
202				/* reset session and proc group */
203				cn_tp->t_pgrp = NULL;
204				cn_tp->t_session = NULL;
205			}
206			return (0);
207		}
208	} else if (major(dev) != major(cndev)) {
209		/* the logical console is about to be closed */
210		cn_is_open = 0;
211		if (cn_phys_is_open)
212			return (0);
213		dev = cndev;
214	}
215	return ((*cn_phys_close)(dev, flag, mode, p));
216}
217
218static int
219cnread(dev, uio, flag)
220	dev_t dev;
221	struct uio *uio;
222	int flag;
223{
224	if (cn_tab == NULL)
225		return (0);
226	dev = cn_tab->cn_dev;
227	return ((*cdevsw[major(dev)]->d_read)(dev, uio, flag));
228}
229
230static int
231cnwrite(dev, uio, flag)
232	dev_t dev;
233	struct uio *uio;
234	int flag;
235{
236	if (cn_tab == NULL)
237		return (0);
238	if (constty)
239		dev = constty->t_dev;
240	else
241		dev = cn_tab->cn_dev;
242	return ((*cdevsw[major(dev)]->d_write)(dev, uio, flag));
243}
244
245static int
246cnioctl(dev, cmd, data, flag, p)
247	dev_t dev;
248	int cmd;
249	caddr_t data;
250	int flag;
251	struct proc *p;
252{
253	int error;
254
255	if (cn_tab == NULL)
256		return (0);
257	/*
258	 * Superuser can always use this to wrest control of console
259	 * output from the "virtual" console.
260	 */
261	if (cmd == TIOCCONS && constty) {
262		error = suser(p->p_ucred, (u_short *) NULL);
263		if (error)
264			return (error);
265		constty = NULL;
266		return (0);
267	}
268	dev = cn_tab->cn_dev;
269	return ((*cdevsw[major(dev)]->d_ioctl)(dev, cmd, data, flag, p));
270}
271
272static int
273cnselect(dev, rw, p)
274	dev_t dev;
275	int rw;
276	struct proc *p;
277{
278	if (cn_tab == NULL)
279		return (1);
280
281	dev = cn_tab->cn_dev;
282
283	return ((*cdevsw[major(dev)]->d_select)(dev, rw, p));
284}
285
286int
287cngetc()
288{
289	int c;
290	if (cn_tab == NULL)
291		return (0);
292	c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
293	if (c == '\r') c = '\n'; /* console input is always ICRNL */
294	return (c);
295}
296
297int
298cncheckc()
299{
300	if (cn_tab == NULL)
301		return (0);
302	return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
303}
304
305void
306cnputc(c)
307	register int c;
308{
309	if (cn_tab == NULL)
310		return;
311	if (c) {
312		if (c == '\n')
313			(*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
314		(*cn_tab->cn_putc)(cn_tab->cn_dev, c);
315	}
316}
317
318static cn_devsw_installed = 0;
319
320static void
321cn_drvinit(void *unused)
322{
323	dev_t dev;
324
325	if( ! cn_devsw_installed ) {
326		dev = makedev(CDEV_MAJOR,0);
327		cdevsw_add(&dev,&cn_cdevsw,NULL);
328		cn_devsw_installed = 1;
329#ifdef DEVFS
330		cn_devfs_token = devfs_add_devswf(&cn_cdevsw, 0, DV_CHR,
331						  UID_ROOT, GID_WHEEL, 0600,
332						  "console");
333#endif
334	}
335}
336
337SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)
338
339
340