2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/platform/vkernel/platform/console.c,v 1.17 2007/07/02 04:19:14 dillon Exp $
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
42 #include <sys/termios.h>
43 #include <sys/fcntl.h>
44 #include <sys/signalvar.h>
45 #include <sys/eventhandler.h>
46 #include <sys/interrupt.h>
47 #include <machine/md_var.h>
52 static int console_stolen_by_kernel;
53 static struct kqueue_info *kqueue_console_info;
56 * Global console locking functions
68 /************************************************************************
70 ************************************************************************
74 #define CDEV_MAJOR 12 /* steal the same major as /dev/ttyv* */
76 static int vcons_tty_param(struct tty *tp, struct termios *tio);
77 static void vcons_tty_start(struct tty *tp);
78 static void vcons_intr(void *tpx, struct intrframe *frame __unused);
80 static d_open_t vcons_open;
81 static d_close_t vcons_close;
82 static d_ioctl_t vcons_ioctl;
84 static struct dev_ops vcons_ops = {
85 { "vcons", CDEV_MAJOR, D_TTY | D_KQFILTER },
87 .d_close = vcons_close,
90 .d_ioctl = vcons_ioctl,
92 .d_kqfilter = ttykqfilter,
96 vcons_open(struct dev_open_args *ap)
98 cdev_t dev = ap->a_head.a_dev;
102 tp = dev->si_tty = ttymalloc(dev->si_tty);
104 #define ISSET(t, f) ((t) & (f))
106 if ((tp->t_state & TS_ISOPEN) == 0) {
107 tp->t_oproc = vcons_tty_start;
108 tp->t_param = vcons_tty_param;
109 tp->t_stop = nottystop;
112 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
114 tp->t_iflag = TTYDEF_IFLAG;
115 tp->t_oflag = TTYDEF_OFLAG;
116 tp->t_cflag = TTYDEF_CFLAG;
117 tp->t_lflag = TTYDEF_LFLAG;
118 tp->t_ispeed = TTYDEF_SPEED;
119 tp->t_ospeed = TTYDEF_SPEED;
122 if (minor(dev) == 0) {
123 error = (*linesw[tp->t_line].l_open)(dev, tp);
124 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
126 if (kqueue_console_info == NULL)
127 kqueue_console_info = kqueue_add(0, vcons_intr, tp);
129 /* dummy up other minors so the installer will run */
136 vcons_close(struct dev_close_args *ap)
138 cdev_t dev = ap->a_head.a_dev;
142 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
148 vcons_ioctl(struct dev_ioctl_args *ap)
150 cdev_t dev = ap->a_head.a_dev;
155 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
156 ap->a_fflag, ap->a_cred);
157 if (error != ENOIOCTL)
159 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
160 if (error != ENOIOCTL)
166 vcons_tty_param(struct tty *tp, struct termios *tio)
168 tp->t_ispeed = tio->c_ispeed;
169 tp->t_ospeed = tio->c_ospeed;
170 tp->t_cflag = tio->c_cflag;
175 vcons_tty_start(struct tty *tp)
180 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
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;
199 vcons_intr(void *tpx, struct intrframe *frame __unused)
201 struct tty *tp = tpx;
202 unsigned char buf[32];
207 * If we aren't open we only have synchronous traffic via the
208 * debugger and do not need to poll.
210 if ((tp->t_state & TS_ISOPEN) == 0)
214 * Only poll if we are open and haven't been stolen by the debugger.
216 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
218 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
219 for (i = 0; i < n; ++i)
220 (*linesw[tp->t_line].l_rint)(buf[i], tp);
225 /************************************************************************
226 * KERNEL CONSOLE INTERFACE *
227 ************************************************************************
229 * Kernel direct-call interface console driver
231 static cn_probe_t vconsprobe;
232 static cn_init_t vconsinit;
233 static cn_init_fini_t vconsinit_fini;
234 static cn_term_t vconsterm;
235 static cn_getc_t vconsgetc;
236 static cn_checkc_t vconscheckc;
237 static cn_putc_t vconsputc;
239 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
240 vconscheckc, vconsputc, NULL);
242 static struct termios init_tio;
243 static struct consdev *vconsole;
246 vconsprobe(struct consdev *cp)
248 cp->cn_pri = CN_NORMAL;
249 cp->cn_probegood = 1;
253 * This is a little bulky handler to set proper terminal
254 * settings in the case of a signal which might lead to
255 * termination or suspension.
260 struct termios curtio;
261 struct sigaction sa, osa;
264 tcgetattr(0, &curtio);
265 tcsetattr(0, TCSAFLUSH, &init_tio);
266 bzero(&sa, sizeof(sa));
267 sigemptyset(&sa.sa_mask);
268 sa.sa_handler = SIG_DFL;
269 sigaction(sig, &sa, &osa);
272 sigprocmask(SIG_UNBLOCK, &ss, &oss);
273 raise(sig); /* now hand down the sig */
274 sigprocmask(SIG_SETMASK, &oss, NULL);
275 sigaction(sig, &osa, NULL);
276 tcsetattr(0, TCSAFLUSH, &curtio);
280 vconswinchsig(int __unused sig)
286 vconswinch_intr(void *arg __unused, void *frame __unused)
288 struct winsize newsize;
290 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
291 ioctl(0, TIOCGWINSZ, &newsize);
293 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
294 * I wished. Unfortunately this needs a curproc, so do it
297 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
298 sizeof(newsize)) != 0) {
299 vconsole->cn_dev->si_tty->t_winsize = newsize;
300 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
309 * We might catch stray SIGIOs, so try hard.
311 while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
316 vconsinit(struct consdev *cp)
322 tcgetattr(0, &init_tio);
323 bzero(&sa, sizeof(sa));
324 sigemptyset(&sa.sa_mask);
325 sa.sa_handler = vconssignal;
326 sigaction(SIGTSTP, &sa, NULL);
327 sigaction(SIGINT, &sa, NULL);
328 sigaction(SIGTERM, &sa, NULL);
329 atexit(vconscleanup);
334 vconsinit_fini(struct consdev *cp)
341 * We have to do this here rather then in early boot to be able
342 * to use the interrupt subsystem.
344 register_int(3, vconswinch_intr, NULL, "swinch", NULL, 0);
345 bzero(&sa, sizeof(sa));
346 sigemptyset(&sa.sa_mask);
347 sa.sa_handler = vconswinchsig;
348 sigaction(SIGWINCH, &sa, NULL);
351 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
353 for (i = 0; i < 8; ++i) {
354 dev = make_dev(&vcons_ops, i,
355 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
360 EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
364 vconsterm(struct consdev *vp)
371 vconsgetc(void *private)
376 console_stolen_by_kernel = 1;
378 n = pread(0, &c, 1, -1);
381 if (n < 0 && errno == EINTR)
383 panic("vconsgetc: EOF on console %d %d", n ,errno);
385 console_stolen_by_kernel = 0;
390 vconscheckc(void *private)
394 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
400 vconsputc(void *private, int c)
404 pwrite(1, &cc, 1, -1);
408 vcons_set_mode(int in_debugger)
412 if (tcgetattr(0, &tio) < 0)
415 tio.c_oflag |= OPOST | ONLCR;
418 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
419 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
420 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
422 tio.c_cc[VINTR] = _POSIX_VDISABLE;
423 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
424 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
426 tcsetattr(0, TCSAFLUSH, &tio);