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
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
43 #include <sys/termios.h>
44 #include <sys/fcntl.h>
45 #include <sys/signalvar.h>
46 #include <sys/eventhandler.h>
47 #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 static int vcons_tty_param(struct tty *tp, struct termios *tio);
64 static void vcons_tty_start(struct tty *tp);
65 static void vcons_intr(void *tpx, struct intrframe *frame __unused);
67 static d_open_t vcons_open;
68 static d_close_t vcons_close;
69 static d_ioctl_t vcons_ioctl;
71 static struct dev_ops vcons_ops = {
72 { "vcons", 0, D_TTY },
74 .d_close = vcons_close,
77 .d_ioctl = vcons_ioctl,
78 .d_kqfilter = ttykqfilter,
82 vcons_open(struct dev_open_args *ap)
84 cdev_t dev = ap->a_head.a_dev;
88 lwkt_gettoken(&tty_token);
89 tp = dev->si_tty = ttymalloc(dev->si_tty);
91 #define ISSET(t, f) ((t) & (f))
93 if ((tp->t_state & TS_ISOPEN) == 0) {
94 tp->t_oproc = vcons_tty_start;
95 tp->t_param = vcons_tty_param;
96 tp->t_stop = nottystop;
99 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
101 tp->t_iflag = TTYDEF_IFLAG;
102 tp->t_oflag = TTYDEF_OFLAG;
103 tp->t_cflag = TTYDEF_CFLAG;
104 tp->t_lflag = TTYDEF_LFLAG;
105 tp->t_ispeed = TTYDEF_SPEED;
106 tp->t_ospeed = TTYDEF_SPEED;
109 if (minor(dev) == 0) {
110 error = (*linesw[tp->t_line].l_open)(dev, tp);
111 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
113 if (kqueue_console_info == NULL)
114 kqueue_console_info = kqueue_add(0, vcons_intr, tp);
116 /* dummy up other minors so the installer will run */
119 lwkt_reltoken(&tty_token);
124 vcons_close(struct dev_close_args *ap)
126 cdev_t dev = ap->a_head.a_dev;
129 lwkt_gettoken(&tty_token);
131 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
133 lwkt_reltoken(&tty_token);
138 vcons_ioctl(struct dev_ioctl_args *ap)
140 cdev_t dev = ap->a_head.a_dev;
144 lwkt_gettoken(&tty_token);
146 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
147 ap->a_fflag, ap->a_cred);
148 if (error != ENOIOCTL) {
149 lwkt_reltoken(&tty_token);
152 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
153 if (error != ENOIOCTL) {
154 lwkt_reltoken(&tty_token);
157 lwkt_reltoken(&tty_token);
162 vcons_tty_param(struct tty *tp, struct termios *tio)
164 lwkt_gettoken(&tty_token);
165 tp->t_ispeed = tio->c_ispeed;
166 tp->t_ospeed = tio->c_ospeed;
167 tp->t_cflag = tio->c_cflag;
168 lwkt_reltoken(&tty_token);
173 vcons_tty_start(struct tty *tp)
178 lwkt_gettoken(&tty_token);
179 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
181 lwkt_reltoken(&tty_token);
184 tp->t_state |= TS_BUSY;
185 while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
187 * Dummy up ttyv1, etc.
189 if (minor(tp->t_dev) == 0) {
190 pwrite(1, buf, n, -1);
193 tp->t_state &= ~TS_BUSY;
195 lwkt_reltoken(&tty_token);
200 vcons_intr(void *tpx, struct intrframe *frame __unused)
202 struct tty *tp = tpx;
203 unsigned char buf[32];
207 lwkt_gettoken(&tty_token);
209 * If we aren't open we only have synchronous traffic via the
210 * debugger and do not need to poll.
212 if ((tp->t_state & TS_ISOPEN) == 0) {
213 lwkt_reltoken(&tty_token);
218 * Only poll if we are open and haven't been stolen by the debugger.
220 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
222 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
223 for (i = 0; i < n; ++i)
224 (*linesw[tp->t_line].l_rint)(buf[i], tp);
227 lwkt_reltoken(&tty_token);
230 /************************************************************************
231 * KERNEL CONSOLE INTERFACE *
232 ************************************************************************
234 * Kernel direct-call interface console driver
236 static cn_probe_t vconsprobe;
237 static cn_init_t vconsinit;
238 static cn_init_fini_t vconsinit_fini;
239 static cn_term_t vconsterm;
240 static cn_getc_t vconsgetc;
241 static cn_checkc_t vconscheckc;
242 static cn_putc_t vconsputc;
244 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
245 vconscheckc, vconsputc, NULL);
247 static struct termios init_tio;
248 static struct consdev *vconsole;
251 vconsprobe(struct consdev *cp)
253 cp->cn_pri = CN_NORMAL;
254 cp->cn_probegood = 1;
258 * This is a little bulky handler to set proper terminal
259 * settings in the case of a signal which might lead to
260 * termination or suspension.
265 struct termios curtio;
266 struct sigaction sa, osa;
269 tcgetattr(0, &curtio);
270 tcsetattr(0, TCSAFLUSH, &init_tio);
271 bzero(&sa, sizeof(sa));
272 sigemptyset(&sa.sa_mask);
273 sa.sa_handler = SIG_DFL;
274 sigaction(sig, &sa, &osa);
277 sigprocmask(SIG_UNBLOCK, &ss, &oss);
278 raise(sig); /* now hand down the sig */
279 sigprocmask(SIG_SETMASK, &oss, NULL);
280 sigaction(sig, &osa, NULL);
281 tcsetattr(0, TCSAFLUSH, &curtio);
285 vconswinchsig(int __unused sig)
291 vconswinch_intr(void *arg __unused, void *frame __unused)
293 struct winsize newsize;
295 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
296 ioctl(0, TIOCGWINSZ, &newsize);
298 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
299 * I wished. Unfortunately this needs a curproc, so do it
302 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
303 sizeof(newsize)) != 0) {
304 vconsole->cn_dev->si_tty->t_winsize = newsize;
305 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
314 * We might catch stray SIGIOs, so try hard.
316 while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
321 vconsinit(struct consdev *cp)
327 tcgetattr(0, &init_tio);
328 bzero(&sa, sizeof(sa));
329 sigemptyset(&sa.sa_mask);
330 sa.sa_handler = vconssignal;
331 sigaction(SIGTSTP, &sa, NULL);
332 sigaction(SIGINT, &sa, NULL);
333 sigaction(SIGTERM, &sa, NULL);
334 atexit(vconscleanup);
339 vconsinit_fini(struct consdev *cp)
346 * We have to do this here rather then in early boot to be able
347 * to use the interrupt subsystem.
349 register_int_virtual(3, vconswinch_intr, NULL, "swinch", NULL,
351 bzero(&sa, sizeof(sa));
352 sigemptyset(&sa.sa_mask);
353 sa.sa_handler = vconswinchsig;
354 sigaction(SIGWINCH, &sa, NULL);
357 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
359 for (i = 0; i < 8; ++i) {
360 dev = make_dev(&vcons_ops, i,
361 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
366 EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
370 vconsterm(struct consdev *vp)
377 vconsgetc(void *private)
382 console_stolen_by_kernel = 1;
384 n = pread(0, &c, 1, -1);
387 if (n < 0 && errno == EINTR)
389 panic("vconsgetc: EOF on console %d %d", n ,errno);
391 console_stolen_by_kernel = 0;
396 vconscheckc(void *private)
400 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
406 vconsputc(void *private, int c)
410 pwrite(1, &cc, 1, -1);
414 vcons_set_mode(int in_debugger)
418 if (tcgetattr(0, &tio) < 0) {
422 tio.c_oflag |= OPOST | ONLCR;
425 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
426 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
427 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
429 tio.c_cc[VINTR] = _POSIX_VDISABLE;
430 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
431 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
433 tcsetattr(0, TCSAFLUSH, &tio);