4 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * 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
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $DragonFly: src/sys/platform/vkernel/platform/console.c,v 1.17 2007/07/02 04:19:14 dillon Exp $
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
44 #include <sys/termios.h>
45 #include <sys/fcntl.h>
46 #include <sys/signalvar.h>
47 #include <sys/eventhandler.h>
48 #include <sys/interrupt.h>
49 #include <machine/md_var.h>
54 static int console_stolen_by_kernel;
55 static struct kqueue_info *kqueue_console_info;
57 /************************************************************************
59 ************************************************************************
63 #define CDEV_MAJOR 12 /* steal the same major as /dev/ttyv* */
65 static int vcons_tty_param(struct tty *tp, struct termios *tio);
66 static void vcons_tty_start(struct tty *tp);
67 static void vcons_intr(void *tpx, struct intrframe *frame __unused);
69 static d_open_t vcons_open;
70 static d_close_t vcons_close;
71 static d_ioctl_t vcons_ioctl;
73 static struct dev_ops vcons_ops = {
74 { "vcons", CDEV_MAJOR, D_TTY },
76 .d_close = vcons_close,
79 .d_ioctl = vcons_ioctl,
80 .d_kqfilter = ttykqfilter
84 vcons_open(struct dev_open_args *ap)
86 cdev_t dev = ap->a_head.a_dev;
90 lwkt_gettoken(&tty_token);
91 tp = dev->si_tty = ttymalloc(dev->si_tty);
93 #define ISSET(t, f) ((t) & (f))
95 if ((tp->t_state & TS_ISOPEN) == 0) {
96 tp->t_oproc = vcons_tty_start;
97 tp->t_param = vcons_tty_param;
98 tp->t_stop = nottystop;
101 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
103 tp->t_iflag = TTYDEF_IFLAG;
104 tp->t_oflag = TTYDEF_OFLAG;
105 tp->t_cflag = TTYDEF_CFLAG;
106 tp->t_lflag = TTYDEF_LFLAG;
107 tp->t_ispeed = TTYDEF_SPEED;
108 tp->t_ospeed = TTYDEF_SPEED;
111 if (minor(dev) == 0) {
112 error = (*linesw[tp->t_line].l_open)(dev, tp);
113 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
115 if (kqueue_console_info == NULL)
116 kqueue_console_info = kqueue_add(0, vcons_intr, tp);
118 /* dummy up other minors so the installer will run */
121 lwkt_reltoken(&tty_token);
126 vcons_close(struct dev_close_args *ap)
128 cdev_t dev = ap->a_head.a_dev;
131 lwkt_gettoken(&tty_token);
133 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
135 lwkt_reltoken(&tty_token);
140 vcons_ioctl(struct dev_ioctl_args *ap)
142 cdev_t dev = ap->a_head.a_dev;
146 lwkt_gettoken(&tty_token);
148 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
149 ap->a_fflag, ap->a_cred);
150 if (error != ENOIOCTL) {
151 lwkt_reltoken(&tty_token);
154 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
155 if (error != ENOIOCTL) {
156 lwkt_reltoken(&tty_token);
159 lwkt_reltoken(&tty_token);
164 vcons_tty_param(struct tty *tp, struct termios *tio)
166 lwkt_gettoken(&tty_token);
167 tp->t_ispeed = tio->c_ispeed;
168 tp->t_ospeed = tio->c_ospeed;
169 tp->t_cflag = tio->c_cflag;
170 lwkt_reltoken(&tty_token);
175 vcons_tty_start(struct tty *tp)
180 lwkt_gettoken(&tty_token);
181 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
183 lwkt_reltoken(&tty_token);
186 tp->t_state |= TS_BUSY;
187 while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
189 * Dummy up ttyv1, etc.
191 if (minor(tp->t_dev) == 0) {
192 pwrite(1, buf, n, -1);
195 tp->t_state &= ~TS_BUSY;
196 lwkt_reltoken(&tty_token);
202 vcons_intr(void *tpx, struct intrframe *frame __unused)
204 struct tty *tp = tpx;
205 unsigned char buf[32];
209 lwkt_gettoken(&tty_token);
211 * If we aren't open we only have synchronous traffic via the
212 * debugger and do not need to poll.
214 if ((tp->t_state & TS_ISOPEN) == 0) {
215 lwkt_reltoken(&tty_token);
220 * Only poll if we are open and haven't been stolen by the debugger.
222 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
224 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
225 for (i = 0; i < n; ++i)
226 (*linesw[tp->t_line].l_rint)(buf[i], tp);
229 lwkt_reltoken(&tty_token);
232 /************************************************************************
233 * KERNEL CONSOLE INTERFACE *
234 ************************************************************************
236 * Kernel direct-call interface console driver
238 static cn_probe_t vconsprobe;
239 static cn_init_t vconsinit;
240 static cn_init_fini_t vconsinit_fini;
241 static cn_term_t vconsterm;
242 static cn_getc_t vconsgetc;
243 static cn_checkc_t vconscheckc;
244 static cn_putc_t vconsputc;
246 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
247 vconscheckc, vconsputc, NULL);
249 static struct termios init_tio;
250 static struct consdev *vconsole;
253 vconsprobe(struct consdev *cp)
255 cp->cn_pri = CN_NORMAL;
256 cp->cn_probegood = 1;
260 * This is a little bulky handler to set proper terminal
261 * settings in the case of a signal which might lead to
262 * termination or suspension.
267 struct termios curtio;
268 struct sigaction sa, osa;
271 tcgetattr(0, &curtio);
272 tcsetattr(0, TCSAFLUSH, &init_tio);
273 bzero(&sa, sizeof(sa));
274 sigemptyset(&sa.sa_mask);
275 sa.sa_handler = SIG_DFL;
276 sigaction(sig, &sa, &osa);
279 sigprocmask(SIG_UNBLOCK, &ss, &oss);
280 raise(sig); /* now hand down the sig */
281 sigprocmask(SIG_SETMASK, &oss, NULL);
282 sigaction(sig, &osa, NULL);
283 tcsetattr(0, TCSAFLUSH, &curtio);
287 vconswinchsig(int __unused sig)
293 vconswinch_intr(void *arg __unused, void *frame __unused)
295 struct winsize newsize;
297 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
298 ioctl(0, TIOCGWINSZ, &newsize);
300 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
301 * I wished. Unfortunately this needs a curproc, so do it
304 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
305 sizeof(newsize)) != 0) {
306 vconsole->cn_dev->si_tty->t_winsize = newsize;
307 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
316 * We might catch stray SIGIOs, so try hard.
318 while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
323 vconsinit(struct consdev *cp)
329 tcgetattr(0, &init_tio);
330 bzero(&sa, sizeof(sa));
331 sigemptyset(&sa.sa_mask);
332 sa.sa_handler = vconssignal;
333 sigaction(SIGTSTP, &sa, NULL);
334 sigaction(SIGINT, &sa, NULL);
335 sigaction(SIGTERM, &sa, NULL);
336 atexit(vconscleanup);
341 vconsinit_fini(struct consdev *cp)
348 * We have to do this here rather then in early boot to be able
349 * to use the interrupt subsystem.
351 register_int(3, vconswinch_intr, NULL, "swinch", NULL, 0);
352 bzero(&sa, sizeof(sa));
353 sigemptyset(&sa.sa_mask);
354 sa.sa_handler = vconswinchsig;
355 sigaction(SIGWINCH, &sa, NULL);
358 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
360 for (i = 0; i < 8; ++i) {
361 dev = make_dev(&vcons_ops, i,
362 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
367 EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
371 vconsterm(struct consdev *vp)
378 vconsgetc(void *private)
383 console_stolen_by_kernel = 1;
385 n = pread(0, &c, 1, -1);
388 if (n < 0 && errno == EINTR)
390 panic("vconsgetc: EOF on console %jd %d", (intmax_t)n, errno);
392 console_stolen_by_kernel = 0;
397 vconscheckc(void *private)
401 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
407 vconsputc(void *private, int c)
411 pwrite(1, &cc, 1, -1);
415 vcons_set_mode(int in_debugger)
419 if (tcgetattr(0, &tio) < 0) {
423 tio.c_oflag |= OPOST | ONLCR;
426 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
427 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
428 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
430 tio.c_cc[VINTR] = _POSIX_VDISABLE;
431 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
432 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
434 tcsetattr(0, TCSAFLUSH, &tio);