2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * Copyright (c) 1999 Michael Smith
5 * Copyright (c) 2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 #include "opt_syscons.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/mutex.h>
52 #include <sys/fcntl.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/msgbuf.h>
57 #include <sys/namei.h>
60 #include <sys/queue.h>
61 #include <sys/reboot.h>
62 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
70 #include <machine/cpu.h>
71 #include <machine/clock.h>
73 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
76 STAILQ_ENTRY(cn_device) cnd_next;
77 struct consdev *cnd_cn;
80 #define CNDEVPATHMAX 32
81 #define CNDEVTAB_SIZE 4
82 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
83 static STAILQ_HEAD(, cn_device) cn_devlist =
84 STAILQ_HEAD_INITIALIZER(cn_devlist);
86 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
87 * which is currently unavailable for inpit
88 * (i.e., if it is in graphics mode) will have
92 static char *consbuf; /* buffer used by `consmsgbuf' */
93 static struct callout conscallout; /* callout for outputting to constty */
94 struct msgbuf consmsgbuf; /* message buffer for console tty */
95 static u_char console_pausing; /* pause after each line during probe */
96 static char *console_pausestr=
97 "<pause; press any key to proceed to next line or '.' to end pause mode>";
98 struct tty *constty; /* pointer to console "window" tty */
99 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
100 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
102 static void constty_timeout(void *arg);
104 static struct consdev cons_consdev;
105 DATA_SET(cons_set, cons_consdev);
106 SET_DECLARE(cons_set, struct consdev);
111 struct consdev *best_cn, *cn, **list;
114 * Check if we should mute the console (for security reasons perhaps)
115 * It can be changes dynamically using sysctl kern.consmute
116 * once we are up and going.
119 cn_mute = ((boothowto & (RB_MUTE
122 |RB_ASKNAME)) == RB_MUTE);
125 * Find the first console with the highest priority.
128 SET_FOREACH(list, cons_set) {
131 /* Skip cons_consdev. */
132 if (cn->cn_ops == NULL)
134 cn->cn_ops->cn_probe(cn);
135 if (cn->cn_pri == CN_DEAD)
137 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
139 if (boothowto & RB_MULTIPLE) {
141 * Initialize console, and attach to it.
143 cn->cn_ops->cn_init(cn);
149 if ((boothowto & RB_MULTIPLE) == 0) {
150 best_cn->cn_ops->cn_init(best_cn);
153 if (boothowto & RB_PAUSE)
156 * Make the best console the preferred console.
162 * Release early console.
174 /* add a new physical console to back the virtual console */
176 cnadd(struct consdev *cn)
178 struct cn_device *cnd;
181 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
182 if (cnd->cnd_cn == cn)
184 for (i = 0; i < CNDEVTAB_SIZE; i++) {
186 if (cnd->cnd_cn == NULL)
189 if (cnd->cnd_cn != NULL)
192 if (cn->cn_name[0] == '\0') {
193 /* XXX: it is unclear if/where this print might output */
194 printf("WARNING: console at %p has no name\n", cn);
196 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
197 if (STAILQ_FIRST(&cn_devlist) == cnd)
198 ttyconsdev_select(cnd->cnd_cn->cn_name);
200 /* Add device to the active mask. */
201 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
207 cnremove(struct consdev *cn)
209 struct cn_device *cnd;
212 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
213 if (cnd->cnd_cn != cn)
215 if (STAILQ_FIRST(&cn_devlist) == cnd)
216 ttyconsdev_select(NULL);
217 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
220 /* Remove this device from available mask. */
221 for (i = 0; i < CNDEVTAB_SIZE; i++)
222 if (cnd == &cn_devtab[i]) {
223 cons_avail_mask &= ~(1 << i);
229 * syscons gets really confused if console resources are
230 * freed after the system has initialized.
232 if (cn->cn_term != NULL)
233 cn->cn_ops->cn_term(cn);
240 cnselect(struct consdev *cn)
242 struct cn_device *cnd;
244 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
245 if (cnd->cnd_cn != cn)
247 if (cnd == STAILQ_FIRST(&cn_devlist))
249 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
250 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
251 ttyconsdev_select(cnd->cnd_cn->cn_name);
257 cnavailable(struct consdev *cn, int available)
261 for (i = 0; i < CNDEVTAB_SIZE; i++) {
262 if (cn_devtab[i].cnd_cn == cn)
266 if (i < CNDEVTAB_SIZE)
267 cons_avail_mask |= (1 << i);
268 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
270 if (i < CNDEVTAB_SIZE)
271 cons_avail_mask &= ~(1 << i);
272 cn->cn_flags |= CN_FLAG_NOAVAIL;
280 return (cons_avail_mask == 0);
284 * sysctl_kern_console() provides output parseable in conscontrol(1).
287 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
289 struct cn_device *cnd;
290 struct consdev *cp, **list;
295 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND |
300 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
301 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
302 sbuf_printf(sb, "/");
303 SET_FOREACH(list, cons_set) {
305 if (cp->cn_name[0] != '\0')
306 sbuf_printf(sb, "%s,", cp->cn_name);
309 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
310 if (error == 0 && req->newptr != NULL) {
318 SET_FOREACH(list, cons_set) {
320 if (strcmp(p, cp->cn_name) != 0)
337 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
338 0, 0, sysctl_kern_console, "A", "Console device control");
341 * User has changed the state of the console muting.
342 * This may require us to open or close the device in question.
345 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
349 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
350 if (error != 0 || req->newptr == NULL)
355 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
356 0, sizeof(cn_mute), sysctl_kern_consmute, "I",
357 "State of the console muting");
362 struct cn_device *cnd;
365 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
367 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
368 cn->cn_ops->cn_grab(cn);
375 struct cn_device *cnd;
378 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
380 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
381 cn->cn_ops->cn_ungrab(cn);
386 * Low level console routines.
395 while ((c = cncheckc()) == -1)
398 c = '\n'; /* console input is always ICRNL */
405 struct cn_device *cnd;
411 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
413 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
414 c = cn->cn_ops->cn_getc(cn);
423 cngets(char *cp, size_t size, int visible)
472 struct cn_device *cnd;
477 if (early_putc != NULL) {
485 if (cn_mute || c == '\0')
487 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
489 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
491 cn->cn_ops->cn_putc(cn, '\r');
492 cn->cn_ops->cn_putc(cn, c);
495 if (console_pausing && c == '\n' && !kdb_active) {
496 for (cp = console_pausestr; *cp != '\0'; cp++)
503 for (cp = console_pausestr; *cp != '\0'; cp++)
515 if (use_cnputs_mtx) {
517 * NOTE: Debug prints and/or witness printouts in
518 * console driver clients can cause the "cnputs_mtx"
519 * mutex to recurse. Simply return if that happens.
521 if (mtx_owned(&cnputs_mtx))
523 mtx_lock_spin(&cnputs_mtx);
527 while ((c = *p++) != '\0')
531 mtx_unlock_spin(&cnputs_mtx);
534 static int consmsgbuf_size = 8192;
535 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
536 "Console tty buffer size");
539 * Redirect console output to a tty.
542 constty_set(struct tty *tp)
546 KASSERT(tp != NULL, ("constty_set: NULL tp"));
547 if (consbuf == NULL) {
548 size = consmsgbuf_size;
549 consbuf = malloc(size, M_TTYCONS, M_WAITOK);
550 msgbuf_init(&consmsgbuf, consbuf, size);
551 callout_init(&conscallout, 0);
554 constty_timeout(NULL);
558 * Disable console redirection to a tty.
568 callout_stop(&conscallout);
569 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
571 free(consbuf, M_TTYCONS);
575 /* Times per second to check for pending console tty messages. */
576 static int constty_wakeups_per_second = 5;
577 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
578 &constty_wakeups_per_second, 0,
579 "Times per second to check for pending console tty messages");
582 constty_timeout(void *arg)
586 if (constty != NULL) {
588 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
589 if (tty_putchar(constty, c) < 0) {
599 if (constty != NULL) {
600 callout_reset(&conscallout, hz / constty_wakeups_per_second,
601 constty_timeout, NULL);
603 /* Deallocate the constty buffer memory. */
609 cn_drvinit(void *unused)
612 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
616 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
619 * Sysbeep(), if we have hardware for it
622 #ifdef HAS_TIMER_SPKR
625 static struct callout beeping_timer;
628 sysbeepstop(void *chan)
631 timer_spkr_release();
636 sysbeep(int pitch, int period)
639 if (timer_spkr_acquire()) {
641 /* Something else owns it. */
645 timer_spkr_setfreq(pitch);
648 callout_reset(&beeping_timer, period, sysbeepstop, NULL);
654 sysbeep_init(void *unused)
657 callout_init(&beeping_timer, 1);
659 SYSINIT(sysbeep, SI_SUB_SOFTINTR, SI_ORDER_ANY, sysbeep_init, NULL);
663 * No hardware, no sound
667 sysbeep(int pitch __unused, int period __unused)
676 * Temporary support for sc(4) to vt(4) transition.
678 static unsigned vty_prefer;
679 static char vty_name[16];
680 SYSCTL_STRING(_kern, OID_AUTO, vty, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, vty_name,
681 0, "Console vty driver");
684 vty_enabled(unsigned vty)
686 static unsigned vty_selected = 0;
688 if (vty_selected == 0) {
689 TUNABLE_STR_FETCH("kern.vty", vty_name, sizeof(vty_name));
692 if (strcmp(vty_name, "sc") == 0) {
693 vty_selected = VTY_SC;
698 if (strcmp(vty_name, "vt") == 0) {
699 vty_selected = VTY_VT;
703 if (vty_prefer != 0) {
704 vty_selected = vty_prefer;
708 vty_selected = VTY_VT;
709 #elif defined(DEV_SC)
710 vty_selected = VTY_SC;
714 if (vty_selected == VTY_VT)
715 strcpy(vty_name, "vt");
716 else if (vty_selected == VTY_SC)
717 strcpy(vty_name, "sc");
719 return ((vty_selected & vty) != 0);
723 vty_set_preferred(unsigned vty)
728 vty_prefer &= ~VTY_SC;
731 vty_prefer &= ~VTY_VT;