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$");
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/mutex.h>
51 #include <sys/fcntl.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/msgbuf.h>
56 #include <sys/namei.h>
59 #include <sys/queue.h>
60 #include <sys/reboot.h>
61 #include <sys/sysctl.h>
65 #include <sys/vnode.h>
69 #include <machine/cpu.h>
70 #include <machine/clock.h>
72 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
75 STAILQ_ENTRY(cn_device) cnd_next;
76 struct consdev *cnd_cn;
79 #define CNDEVPATHMAX 32
80 #define CNDEVTAB_SIZE 4
81 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
82 static STAILQ_HEAD(, cn_device) cn_devlist =
83 STAILQ_HEAD_INITIALIZER(cn_devlist);
85 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
86 * which is currently unavailable for inpit
87 * (i.e., if it is in graphics mode) will have
91 static char *consbuf; /* buffer used by `consmsgbuf' */
92 static struct callout conscallout; /* callout for outputting to constty */
93 struct msgbuf consmsgbuf; /* message buffer for console tty */
94 static u_char console_pausing; /* pause after each line during probe */
95 static char *console_pausestr=
96 "<pause; press any key to proceed to next line or '.' to end pause mode>";
97 struct tty *constty; /* pointer to console "window" tty */
98 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
99 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
101 static void constty_timeout(void *arg);
103 static struct consdev cons_consdev;
104 DATA_SET(cons_set, cons_consdev);
105 SET_DECLARE(cons_set, struct consdev);
110 struct consdev *best_cn, *cn, **list;
113 * Check if we should mute the console (for security reasons perhaps)
114 * It can be changes dynamically using sysctl kern.consmute
115 * once we are up and going.
118 cn_mute = ((boothowto & (RB_MUTE
121 |RB_ASKNAME)) == RB_MUTE);
124 * Find the first console with the highest priority.
127 SET_FOREACH(list, cons_set) {
130 /* Skip cons_consdev. */
131 if (cn->cn_ops == NULL)
133 cn->cn_ops->cn_probe(cn);
134 if (cn->cn_pri == CN_DEAD)
136 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
138 if (boothowto & RB_MULTIPLE) {
140 * Initialize console, and attach to it.
142 cn->cn_ops->cn_init(cn);
148 if ((boothowto & RB_MULTIPLE) == 0) {
149 best_cn->cn_ops->cn_init(best_cn);
152 if (boothowto & RB_PAUSE)
155 * Make the best console the preferred console.
166 /* add a new physical console to back the virtual console */
168 cnadd(struct consdev *cn)
170 struct cn_device *cnd;
173 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
174 if (cnd->cnd_cn == cn)
176 for (i = 0; i < CNDEVTAB_SIZE; i++) {
178 if (cnd->cnd_cn == NULL)
181 if (cnd->cnd_cn != NULL)
184 if (cn->cn_name[0] == '\0') {
185 /* XXX: it is unclear if/where this print might output */
186 printf("WARNING: console at %p has no name\n", cn);
188 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
189 if (STAILQ_FIRST(&cn_devlist) == cnd)
190 ttyconsdev_select(cnd->cnd_cn->cn_name);
192 /* Add device to the active mask. */
193 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
199 cnremove(struct consdev *cn)
201 struct cn_device *cnd;
204 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
205 if (cnd->cnd_cn != cn)
207 if (STAILQ_FIRST(&cn_devlist) == cnd)
208 ttyconsdev_select(NULL);
209 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
212 /* Remove this device from available mask. */
213 for (i = 0; i < CNDEVTAB_SIZE; i++)
214 if (cnd == &cn_devtab[i]) {
215 cons_avail_mask &= ~(1 << i);
221 * syscons gets really confused if console resources are
222 * freed after the system has initialized.
224 if (cn->cn_term != NULL)
225 cn->cn_ops->cn_term(cn);
232 cnselect(struct consdev *cn)
234 struct cn_device *cnd;
236 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
237 if (cnd->cnd_cn != cn)
239 if (cnd == STAILQ_FIRST(&cn_devlist))
241 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
242 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
243 ttyconsdev_select(cnd->cnd_cn->cn_name);
249 cnavailable(struct consdev *cn, int available)
253 for (i = 0; i < CNDEVTAB_SIZE; i++) {
254 if (cn_devtab[i].cnd_cn == cn)
258 if (i < CNDEVTAB_SIZE)
259 cons_avail_mask |= (1 << i);
260 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
262 if (i < CNDEVTAB_SIZE)
263 cons_avail_mask &= ~(1 << i);
264 cn->cn_flags |= CN_FLAG_NOAVAIL;
272 return (cons_avail_mask == 0);
276 * sysctl_kern_console() provides output parseable in conscontrol(1).
279 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
281 struct cn_device *cnd;
282 struct consdev *cp, **list;
287 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND);
291 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
292 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
293 sbuf_printf(sb, "/");
294 SET_FOREACH(list, cons_set) {
296 if (cp->cn_name[0] != '\0')
297 sbuf_printf(sb, "%s,", cp->cn_name);
300 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
301 if (error == 0 && req->newptr != NULL) {
309 SET_FOREACH(list, cons_set) {
311 if (strcmp(p, cp->cn_name) != 0)
328 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
329 0, 0, sysctl_kern_console, "A", "Console device control");
332 * User has changed the state of the console muting.
333 * This may require us to open or close the device in question.
336 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
340 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
341 if (error != 0 || req->newptr == NULL)
346 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
347 0, sizeof(cn_mute), sysctl_kern_consmute, "I",
348 "State of the console muting");
353 struct cn_device *cnd;
356 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
358 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
359 cn->cn_ops->cn_grab(cn);
366 struct cn_device *cnd;
369 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
371 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG))
372 cn->cn_ops->cn_ungrab(cn);
377 * Low level console routines.
386 while ((c = cncheckc()) == -1)
389 c = '\n'; /* console input is always ICRNL */
396 struct cn_device *cnd;
402 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
404 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
405 c = cn->cn_ops->cn_getc(cn);
414 cngets(char *cp, size_t size, int visible)
465 struct cn_device *cnd;
469 if (cn_mute || c == '\0')
471 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
473 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
475 cn->cn_ops->cn_putc(cn, '\r');
476 cn->cn_ops->cn_putc(cn, c);
479 if (console_pausing && c == '\n' && !kdb_active) {
480 for (cp = console_pausestr; *cp != '\0'; cp++)
487 for (cp = console_pausestr; *cp != '\0'; cp++)
499 if (use_cnputs_mtx) {
501 * NOTE: Debug prints and/or witness printouts in
502 * console driver clients can cause the "cnputs_mtx"
503 * mutex to recurse. Simply return if that happens.
505 if (mtx_owned(&cnputs_mtx))
507 mtx_lock_spin(&cnputs_mtx);
511 while ((c = *p++) != '\0')
515 mtx_unlock_spin(&cnputs_mtx);
518 static int consmsgbuf_size = 8192;
519 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
520 "Console tty buffer size");
523 * Redirect console output to a tty.
526 constty_set(struct tty *tp)
530 KASSERT(tp != NULL, ("constty_set: NULL tp"));
531 if (consbuf == NULL) {
532 size = consmsgbuf_size;
533 consbuf = malloc(size, M_TTYCONS, M_WAITOK);
534 msgbuf_init(&consmsgbuf, consbuf, size);
535 callout_init(&conscallout, 0);
538 constty_timeout(NULL);
542 * Disable console redirection to a tty.
552 callout_stop(&conscallout);
553 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
555 free(consbuf, M_TTYCONS);
559 /* Times per second to check for pending console tty messages. */
560 static int constty_wakeups_per_second = 5;
561 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
562 &constty_wakeups_per_second, 0,
563 "Times per second to check for pending console tty messages");
566 constty_timeout(void *arg)
570 if (constty != NULL) {
572 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
573 if (tty_putchar(constty, c) < 0) {
583 if (constty != NULL) {
584 callout_reset(&conscallout, hz / constty_wakeups_per_second,
585 constty_timeout, NULL);
587 /* Deallocate the constty buffer memory. */
593 cn_drvinit(void *unused)
596 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
600 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
603 * Sysbeep(), if we have hardware for it
606 #ifdef HAS_TIMER_SPKR
611 sysbeepstop(void *chan)
614 timer_spkr_release();
619 sysbeep(int pitch, int period)
622 if (timer_spkr_acquire()) {
624 /* Something else owns it. */
628 timer_spkr_setfreq(pitch);
631 timeout(sysbeepstop, (void *)NULL, period);
639 * No hardware, no sound
643 sysbeep(int pitch __unused, int period __unused)