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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/mutex.h>
48 #include <sys/fcntl.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/msgbuf.h>
53 #include <sys/namei.h>
56 #include <sys/queue.h>
57 #include <sys/reboot.h>
58 #include <sys/sysctl.h>
62 #include <sys/vnode.h>
66 #include <machine/cpu.h>
67 #include <machine/clock.h>
69 static MALLOC_DEFINE(M_TTYCONS, "tty console", "tty console handling");
72 STAILQ_ENTRY(cn_device) cnd_next;
73 struct consdev *cnd_cn;
76 #define CNDEVPATHMAX 32
77 #define CNDEVTAB_SIZE 4
78 static struct cn_device cn_devtab[CNDEVTAB_SIZE];
79 static STAILQ_HEAD(, cn_device) cn_devlist =
80 STAILQ_HEAD_INITIALIZER(cn_devlist);
82 int cons_avail_mask = 0; /* Bit mask. Each registered low level console
83 * which is currently unavailable for inpit
84 * (i.e., if it is in graphics mode) will have
88 static char *consbuf; /* buffer used by `consmsgbuf' */
89 static struct callout conscallout; /* callout for outputting to constty */
90 struct msgbuf consmsgbuf; /* message buffer for console tty */
91 static u_char console_pausing; /* pause after each line during probe */
92 static char *console_pausestr=
93 "<pause; press any key to proceed to next line or '.' to end pause mode>";
94 struct tty *constty; /* pointer to console "window" tty */
95 static struct mtx cnputs_mtx; /* Mutex for cnputs(). */
96 static int use_cnputs_mtx = 0; /* != 0 if cnputs_mtx locking reqd. */
98 static void constty_timeout(void *arg);
100 static struct consdev cons_consdev;
101 DATA_SET(cons_set, cons_consdev);
102 SET_DECLARE(cons_set, struct consdev);
107 struct consdev *best_cn, *cn, **list;
110 * Check if we should mute the console (for security reasons perhaps)
111 * It can be changes dynamically using sysctl kern.consmute
112 * once we are up and going.
115 cn_mute = ((boothowto & (RB_MUTE
118 |RB_ASKNAME)) == RB_MUTE);
121 * Find the first console with the highest priority.
124 SET_FOREACH(list, cons_set) {
127 if (cn->cn_probe == NULL)
130 if (cn->cn_pri == CN_DEAD)
132 if (best_cn == NULL || cn->cn_pri > best_cn->cn_pri)
134 if (boothowto & RB_MULTIPLE) {
136 * Initialize console, and attach to it.
144 if ((boothowto & RB_MULTIPLE) == 0) {
145 best_cn->cn_init(best_cn);
148 if (boothowto & RB_PAUSE)
151 * Make the best console the preferred console.
162 /* add a new physical console to back the virtual console */
164 cnadd(struct consdev *cn)
166 struct cn_device *cnd;
169 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
170 if (cnd->cnd_cn == cn)
172 for (i = 0; i < CNDEVTAB_SIZE; i++) {
174 if (cnd->cnd_cn == NULL)
177 if (cnd->cnd_cn != NULL)
180 if (cn->cn_name[0] == '\0') {
181 /* XXX: it is unclear if/where this print might output */
182 printf("WARNING: console at %p has no name\n", cn);
184 STAILQ_INSERT_TAIL(&cn_devlist, cnd, cnd_next);
185 if (STAILQ_FIRST(&cn_devlist) == cnd)
186 ttyconsdev_select(cnd->cnd_cn->cn_name);
188 /* Add device to the active mask. */
189 cnavailable(cn, (cn->cn_flags & CN_FLAG_NOAVAIL) == 0);
195 cnremove(struct consdev *cn)
197 struct cn_device *cnd;
200 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
201 if (cnd->cnd_cn != cn)
203 if (STAILQ_FIRST(&cn_devlist) == cnd)
204 ttyconsdev_select(NULL);
205 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
208 /* Remove this device from available mask. */
209 for (i = 0; i < CNDEVTAB_SIZE; i++)
210 if (cnd == &cn_devtab[i]) {
211 cons_avail_mask &= ~(1 << i);
217 * syscons gets really confused if console resources are
218 * freed after the system has initialized.
220 if (cn->cn_term != NULL)
228 cnselect(struct consdev *cn)
230 struct cn_device *cnd;
232 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
233 if (cnd->cnd_cn != cn)
235 if (cnd == STAILQ_FIRST(&cn_devlist))
237 STAILQ_REMOVE(&cn_devlist, cnd, cn_device, cnd_next);
238 STAILQ_INSERT_HEAD(&cn_devlist, cnd, cnd_next);
239 ttyconsdev_select(cnd->cnd_cn->cn_name);
245 cnavailable(struct consdev *cn, int available)
249 for (i = 0; i < CNDEVTAB_SIZE; i++) {
250 if (cn_devtab[i].cnd_cn == cn)
254 if (i < CNDEVTAB_SIZE)
255 cons_avail_mask |= (1 << i);
256 cn->cn_flags &= ~CN_FLAG_NOAVAIL;
258 if (i < CNDEVTAB_SIZE)
259 cons_avail_mask &= ~(1 << i);
260 cn->cn_flags |= CN_FLAG_NOAVAIL;
268 return (cons_avail_mask == 0);
272 * sysctl_kern_console() provides output parseable in conscontrol(1).
275 sysctl_kern_console(SYSCTL_HANDLER_ARGS)
277 struct cn_device *cnd;
278 struct consdev *cp, **list;
283 sb = sbuf_new(NULL, NULL, CNDEVPATHMAX * 2, SBUF_AUTOEXTEND);
287 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next)
288 sbuf_printf(sb, "%s,", cnd->cnd_cn->cn_name);
289 sbuf_printf(sb, "/");
290 SET_FOREACH(list, cons_set) {
292 if (cp->cn_name[0] != '\0')
293 sbuf_printf(sb, "%s,", cp->cn_name);
296 error = sysctl_handle_string(oidp, sbuf_data(sb), sbuf_len(sb), req);
297 if (error == 0 && req->newptr != NULL) {
305 SET_FOREACH(list, cons_set) {
307 if (strcmp(p, cp->cn_name) != 0)
324 SYSCTL_PROC(_kern, OID_AUTO, console, CTLTYPE_STRING|CTLFLAG_RW,
325 0, 0, sysctl_kern_console, "A", "Console device control");
328 * User has changed the state of the console muting.
329 * This may require us to open or close the device in question.
332 sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
336 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
337 if (error != 0 || req->newptr == NULL)
342 SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
343 0, sizeof(cn_mute), sysctl_kern_consmute, "I", "");
346 * Low level console routines.
355 while ((c = cncheckc()) == -1)
358 c = '\n'; /* console input is always ICRNL */
365 struct cn_device *cnd;
371 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
373 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
374 if (cn->cn_checkc != NULL)
375 c = cn->cn_checkc(cn);
389 struct cn_device *cnd;
393 if (cn_mute || c == '\0')
395 STAILQ_FOREACH(cnd, &cn_devlist, cnd_next) {
397 if (!kdb_active || !(cn->cn_flags & CN_FLAG_NODEBUG)) {
399 cn->cn_putc(cn, '\r');
403 if (console_pausing && c == '\n' && !kdb_active) {
404 for (cp = console_pausestr; *cp != '\0'; cp++)
409 for (cp = console_pausestr; *cp != '\0'; cp++)
421 if (use_cnputs_mtx) {
423 * NOTE: Debug prints and/or witness printouts in
424 * console driver clients can cause the "cnputs_mtx"
425 * mutex to recurse. Simply return if that happens.
427 if (mtx_owned(&cnputs_mtx))
429 mtx_lock_spin(&cnputs_mtx);
433 while ((c = *p++) != '\0')
437 mtx_unlock_spin(&cnputs_mtx);
440 static int consmsgbuf_size = 8192;
441 SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
445 * Redirect console output to a tty.
448 constty_set(struct tty *tp)
452 KASSERT(tp != NULL, ("constty_set: NULL tp"));
453 if (consbuf == NULL) {
454 size = consmsgbuf_size;
455 consbuf = malloc(size, M_TTYCONS, M_WAITOK);
456 msgbuf_init(&consmsgbuf, consbuf, size);
457 callout_init(&conscallout, 0);
460 constty_timeout(NULL);
464 * Disable console redirection to a tty.
474 callout_stop(&conscallout);
475 while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
477 free(consbuf, M_TTYCONS);
481 /* Times per second to check for pending console tty messages. */
482 static int constty_wakeups_per_second = 5;
483 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
484 &constty_wakeups_per_second, 0, "");
487 constty_timeout(void *arg)
491 if (constty != NULL) {
493 while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
494 if (tty_putchar(constty, c) < 0) {
504 if (constty != NULL) {
505 callout_reset(&conscallout, hz / constty_wakeups_per_second,
506 constty_timeout, NULL);
508 /* Deallocate the constty buffer memory. */
514 cn_drvinit(void *unused)
517 mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
521 SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
524 * Sysbeep(), if we have hardware for it
527 #ifdef HAS_TIMER_SPKR
532 sysbeepstop(void *chan)
535 timer_spkr_release();
540 sysbeep(int pitch, int period)
543 if (timer_spkr_acquire()) {
545 /* Something else owns it. */
549 timer_spkr_setfreq(pitch);
552 timeout(sysbeepstop, (void *)NULL, period);
560 * No hardware, no sound
564 sysbeep(int pitch __unused, int period __unused)