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