2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
39 * $FreeBSD: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $
44 #include <sys/param.h>
45 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/reboot.h>
51 #include <sys/sysctl.h>
57 #include <machine/cpu.h>
59 static d_open_t cnopen;
60 static d_close_t cnclose;
61 static d_read_t cnread;
62 static d_write_t cnwrite;
63 static d_ioctl_t cnioctl;
64 static d_poll_t cnpoll;
65 static d_kqfilter_t cnkqfilter;
68 static struct cdevsw cn_cdevsw = {
76 /* strategy */ nostrategy,
81 /* flags */ D_TTY | D_KQFILTER,
83 /* kqfilter */ cnkqfilter,
86 static dev_t cn_dev_t; /* seems to be never really used */
87 static udev_t cn_udev_t;
88 SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
89 &cn_udev_t, sizeof cn_udev_t, "T,dev_t", "");
93 int cons_unavail = 0; /* XXX:
94 * physical console not available for
95 * input (i.e., it is in graphics mode)
98 static u_char cn_is_open; /* nonzero if logical console is open */
99 static int openmode, openflag; /* how /dev/console was openned */
100 static dev_t cn_devfsdev; /* represents the device private info */
101 static u_char cn_phys_is_open; /* nonzero if physical device is open */
102 static d_close_t *cn_phys_close; /* physical device close function */
103 static d_open_t *cn_phys_open; /* physical device open function */
104 struct consdev *cn_tab; /* physical console device info */
105 static u_char console_pausing; /* pause after each line during probe */
106 static char *console_pausestr=
107 "<pause; press any key to proceed to next line or '.' to end pause mode>";
109 CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
114 struct consdev *best_cp, *cp, **list;
117 * Find the first console with the highest priority.
120 list = (struct consdev **)cons_set.ls_items;
121 while ((cp = *list++) != NULL) {
122 if (cp->cn_probe == NULL)
125 if (cp->cn_pri > CN_DEAD &&
126 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
131 * Check if we should mute the console (for security reasons perhaps)
132 * It can be changes dynamically using sysctl kern.consmute
133 * once we are up and going.
136 cn_mute = ((boothowto & (RB_MUTE
140 |RB_CONFIG)) == RB_MUTE);
143 * If no console, give up.
145 if (best_cp == NULL) {
146 if (cn_tab != NULL && cn_tab->cn_term != NULL)
147 (*cn_tab->cn_term)(cn_tab);
153 * Initialize console, then attach to it. This ordering allows
154 * debugging using the previous console, if any.
156 (*best_cp->cn_init)(best_cp);
157 if (cn_tab != NULL && cn_tab != best_cp) {
158 /* Turn off the previous console. */
159 if (cn_tab->cn_term != NULL)
160 (*cn_tab->cn_term)(cn_tab);
162 if (boothowto & RB_PAUSE)
172 if ((cn_tab == NULL) || cn_mute)
176 * Hook the open and close functions.
178 cdp = devsw(cn_tab->cn_dev);
180 cn_phys_close = cdp->d_close;
181 cdp->d_close = cnclose;
182 cn_phys_open = cdp->d_open;
183 cdp->d_open = cnopen;
185 cn_dev_t = cn_tab->cn_dev;
186 cn_udev_t = dev2udev(cn_dev_t);
199 * Unhook the open and close functions.
201 cdp = devsw(cn_tab->cn_dev);
203 cdp->d_close = cn_phys_close;
204 cdp->d_open = cn_phys_open;
206 cn_phys_close = NULL;
213 * User has changed the state of the console muting.
214 * This may require us to open or close the device in question.
217 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
223 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
224 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
225 if(ocn_mute && !cn_mute) {
227 * going from muted to unmuted.. open the physical dev
228 * if the console has been openned
232 /* XXX curproc is not what we want really */
233 error = cnopen(cn_dev_t, openflag,
235 /* if it failed, back it out */
236 if ( error != 0) cnuninit();
237 } else if (!ocn_mute && cn_mute) {
239 * going from unmuted to muted.. close the physical dev
240 * if it's only open via /dev/console
243 error = cnclose(cn_dev_t, openflag,
245 if ( error == 0) cnuninit();
249 * back out the change if there was an error
257 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
258 0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
261 cnopen(dev, flag, mode, p)
266 dev_t cndev, physdev;
269 if (cn_tab == NULL || cn_phys_open == NULL)
271 cndev = cn_tab->cn_dev;
272 physdev = (major(dev) == major(cndev) ? dev : cndev);
274 * If mute is active, then non console opens don't get here
275 * so we don't need to check for that. They
276 * bypass this and go straight to the device.
279 retval = (*cn_phys_open)(physdev, flag, mode, p);
282 * check if we openned it via /dev/console or
283 * via the physical entry (e.g. /dev/sio0).
287 else if (physdev == cndev) {
292 dev->si_tty = physdev->si_tty;
298 cnclose(dev, flag, mode, p)
306 if (cn_tab == NULL || cn_phys_open == NULL)
308 cndev = cn_tab->cn_dev;
309 cn_tp = cndev->si_tty;
311 * act appropriatly depending on whether it's /dev/console
312 * or the pysical device (e.g. /dev/sio) that's being closed.
313 * in either case, don't actually close the device unless
317 /* the physical device is about to be closed */
321 /* perform a ttyhalfclose() */
322 /* reset session and proc group */
323 cn_tp->t_pgrp = NULL;
324 cn_tp->t_session = NULL;
328 } else if (major(dev) != major(cndev)) {
329 /* the logical console is about to be closed */
336 return ((*cn_phys_close)(dev, flag, mode, p));
341 cnread(dev, uio, flag)
347 if (cn_tab == NULL || cn_phys_open == NULL)
349 dev = cn_tab->cn_dev;
350 return ((*devsw(dev)->d_read)(dev, uio, flag));
354 cnwrite(dev, uio, flag)
360 if (cn_tab == NULL || cn_phys_open == NULL) {
361 uio->uio_resid = 0; /* dump the data */
365 dev = constty->t_dev;
367 dev = cn_tab->cn_dev;
369 return ((*devsw(dev)->d_write)(dev, uio, flag));
373 cnioctl(dev, cmd, data, flag, p)
382 if (cn_tab == NULL || cn_phys_open == NULL)
385 * Superuser can always use this to wrest control of console
386 * output from the "virtual" console.
388 if (cmd == TIOCCONS && constty) {
395 dev = cn_tab->cn_dev;
396 return ((*devsw(dev)->d_ioctl)(dev, cmd, data, flag, p));
400 cnpoll(dev, events, p)
405 if ((cn_tab == NULL) || cn_mute)
408 dev = cn_tab->cn_dev;
410 return ((*devsw(dev)->d_poll)(dev, events, p));
418 if ((cn_tab == NULL) || cn_mute)
421 dev = cn_tab->cn_dev;
422 if (devsw(dev)->d_flags & D_KQFILTER)
423 return ((*devsw(dev)->d_kqfilter)(dev, kn));
431 if ((cn_tab == NULL) || cn_mute)
433 c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
434 if (c == '\r') c = '\n'; /* console input is always ICRNL */
441 if ((cn_tab == NULL) || cn_mute)
443 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
452 if ((cn_tab == NULL) || cn_mute)
456 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
457 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
459 if (console_pausing && !db_active && (c == '\n')) {
461 if (console_pausing && (c == '\n')) {
463 for(cp=console_pausestr; *cp != '\0'; cp++)
464 (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp);
467 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
468 for(cp=console_pausestr; *cp != '\0'; cp++)
469 (*cn_tab->cn_putc)(cn_tab->cn_dev, ' ');
470 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
485 if (refcount == 0 && cn_tab->cn_dbctl != NULL)
486 (*cn_tab->cn_dbctl)(cn_tab->cn_dev, on);
492 cn_drvinit(void *unused)
495 cn_devfsdev = make_dev(&cn_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
499 SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)