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.15 2007/05/28 05:26:29 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 <machine/md_var.h>
50 static int console_stolen_by_kernel;
51 static struct kqueue_info *kqueue_console_info;
54 * Global console locking functions
66 /************************************************************************
68 ************************************************************************
72 #define CDEV_MAJOR 12 /* steal the same major as /dev/ttyv* */
74 static int vcons_tty_param(struct tty *tp, struct termios *tio);
75 static void vcons_tty_start(struct tty *tp);
76 static void vcons_intr(void *tpx, struct intrframe *frame __unused);
78 static d_open_t vcons_open;
79 static d_close_t vcons_close;
80 static d_ioctl_t vcons_ioctl;
82 static struct dev_ops vcons_ops = {
83 { "vcons", CDEV_MAJOR, D_TTY },
85 .d_close = vcons_close,
88 .d_ioctl = vcons_ioctl,
93 vcons_open(struct dev_open_args *ap)
95 cdev_t dev = ap->a_head.a_dev;
99 tp = dev->si_tty = ttymalloc(dev->si_tty);
100 if ((tp->t_state & TS_ISOPEN) == 0) {
101 tp->t_oproc = vcons_tty_start;
102 tp->t_param = vcons_tty_param;
103 tp->t_stop = nottystop;
106 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
108 tp->t_iflag = TTYDEF_IFLAG;
109 tp->t_oflag = TTYDEF_OFLAG;
110 tp->t_cflag = TTYDEF_CFLAG;
111 tp->t_lflag = TTYDEF_LFLAG;
112 tp->t_ispeed = TTYDEF_SPEED;
113 tp->t_ospeed = TTYDEF_SPEED;
116 if (minor(dev) == 0) {
117 error = (*linesw[tp->t_line].l_open)(dev, tp);
118 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
120 if (kqueue_console_info == NULL)
121 kqueue_console_info = kqueue_add(0, vcons_intr, tp);
123 /* dummy up other minors so the installer will run */
130 vcons_close(struct dev_close_args *ap)
132 cdev_t dev = ap->a_head.a_dev;
136 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
142 vcons_ioctl(struct dev_ioctl_args *ap)
144 cdev_t dev = ap->a_head.a_dev;
149 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
150 ap->a_fflag, ap->a_cred);
151 if (error != ENOIOCTL)
153 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
154 if (error != ENOIOCTL)
160 vcons_tty_param(struct tty *tp, struct termios *tio)
162 tp->t_ispeed = tio->c_ispeed;
163 tp->t_ospeed = tio->c_ospeed;
164 tp->t_cflag = tio->c_cflag;
169 vcons_tty_start(struct tty *tp)
174 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
178 tp->t_state |= TS_BUSY;
179 while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
181 * Dummy up ttyv1, etc.
183 if (minor(tp->t_dev) == 0)
186 tp->t_state &= ~TS_BUSY;
192 vcons_intr(void *tpx, struct intrframe *frame __unused)
194 struct tty *tp = tpx;
195 unsigned char buf[32];
200 * If we aren't open we only have synchronous traffic via the
201 * debugger and do not need to poll.
203 if ((tp->t_state & TS_ISOPEN) == 0)
207 * Only poll if we are open and haven't been stolen by the debugger.
209 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
211 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
212 for (i = 0; i < n; ++i)
213 (*linesw[tp->t_line].l_rint)(buf[i], tp);
218 /************************************************************************
219 * KERNEL CONSOLE INTERFACE *
220 ************************************************************************
222 * Kernel direct-call interface console driver
224 static cn_probe_t vconsprobe;
225 static cn_init_t vconsinit;
226 static cn_init_fini_t vconsinit_fini;
227 static cn_term_t vconsterm;
228 static cn_getc_t vconsgetc;
229 static cn_checkc_t vconscheckc;
230 static cn_putc_t vconsputc;
232 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
233 vconscheckc, vconsputc, NULL);
235 static struct termios init_tio;
236 static struct consdev *vconsole;
239 vconsprobe(struct consdev *cp)
241 cp->cn_pri = CN_NORMAL;
242 cp->cn_probegood = 1;
246 * This is a little bulky handler to set proper terminal
247 * settings in the case of a signal which might lead to
248 * termination or suspension.
253 struct termios curtio;
254 struct sigaction sa, osa;
257 tcgetattr(0, &curtio);
258 tcsetattr(0, TCSAFLUSH, &init_tio);
259 bzero(&sa, sizeof(sa));
260 sigemptyset(&sa.sa_mask);
261 sa.sa_handler = SIG_DFL;
262 sigaction(sig, &sa, &osa);
265 sigprocmask(SIG_UNBLOCK, &ss, &oss);
266 raise(sig); /* now hand down the sig */
267 sigprocmask(SIG_SETMASK, &oss, NULL);
268 sigaction(sig, &osa, NULL);
269 tcsetattr(0, TCSAFLUSH, &curtio);
273 vconswinch(int __unused sig)
275 struct winsize newsize;
277 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
278 ioctl(0, TIOCGWINSZ, &newsize);
280 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
281 * I wished. Unfortunately this needs a curproc, so do it
284 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
285 sizeof(newsize)) != 0) {
286 vconsole->cn_dev->si_tty->t_winsize = newsize;
287 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
295 tcsetattr(0, TCSAFLUSH, &init_tio);
299 vconsinit(struct consdev *cp)
305 tcgetattr(0, &init_tio);
306 bzero(&sa, sizeof(sa));
307 sigemptyset(&sa.sa_mask);
308 sa.sa_handler = vconssignal;
309 sigaction(SIGTSTP, &sa, NULL);
310 sigaction(SIGINT, &sa, NULL);
311 sigaction(SIGTERM, &sa, NULL);
312 atexit(vconscleanup);
314 sa.sa_handler = vconswinch;
315 sigaction(SIGWINCH, &sa, NULL);
321 vconsinit_fini(struct consdev *cp)
327 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
329 dev_ops_add(&vcons_ops, -1 & ~7, 0);
330 for (i = 0; i < 8; ++i) {
331 dev = make_dev(&vcons_ops, i,
332 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
339 vconsterm(struct consdev *vp)
346 vconsgetc(void *private)
351 console_stolen_by_kernel = 1;
353 if ((n = read(0, &c, 1)) == 1)
355 if (n < 0 && errno == EINTR)
357 panic("vconsgetc: EOF on console %d %d", n ,errno);
359 console_stolen_by_kernel = 0;
364 vconscheckc(void *private)
368 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
374 vconsputc(void *private, int c)
382 vcons_set_mode(int in_debugger)
386 if (tcgetattr(0, &tio) < 0)
389 tio.c_oflag |= OPOST | ONLCR;
392 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
393 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
394 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
396 tio.c_cc[VINTR] = _POSIX_VDISABLE;
397 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
398 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
400 tcsetattr(0, TCSAFLUSH, &tio);