kernel - Remove D_KQFILTER flag
[dragonfly.git] / sys / platform / vkernel / platform / console.c
CommitLineData
6f7b98e0
MD
1/*
2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 *
0df380ef 34 * $DragonFly: src/sys/platform/vkernel/platform/console.c,v 1.17 2007/07/02 04:19:14 dillon Exp $
6f7b98e0
MD
35 */
36
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/conf.h>
40#include <sys/cons.h>
41#include <sys/tty.h>
42#include <sys/termios.h>
43#include <sys/fcntl.h>
30809ad1 44#include <sys/signalvar.h>
db74ac99 45#include <sys/eventhandler.h>
0df380ef 46#include <sys/interrupt.h>
cb118784 47#include <machine/md_var.h>
6f7b98e0 48#include <unistd.h>
e7f2d7de 49#include <termios.h>
3c5a0ece 50#include <stdlib.h>
6f7b98e0 51
aaf8b91f 52static int console_stolen_by_kernel;
9aeb4297 53static struct kqueue_info *kqueue_console_info;
aaf8b91f 54
6f7b98e0
MD
55/*
56 * Global console locking functions
57 */
58void
59cons_lock(void)
60{
61}
62
63void
64cons_unlock(void)
65{
66}
67
68/************************************************************************
69 * CONSOLE DEVICE *
70 ************************************************************************
71 *
72 */
73
a72d8a9f 74#define CDEV_MAJOR 12 /* steal the same major as /dev/ttyv* */
6f7b98e0
MD
75
76static int vcons_tty_param(struct tty *tp, struct termios *tio);
77static void vcons_tty_start(struct tty *tp);
9aeb4297 78static void vcons_intr(void *tpx, struct intrframe *frame __unused);
6f7b98e0
MD
79
80static d_open_t vcons_open;
81static d_close_t vcons_close;
82static d_ioctl_t vcons_ioctl;
83
84static struct dev_ops vcons_ops = {
d4b8aec4 85 { "vcons", CDEV_MAJOR, D_TTY },
6f7b98e0
MD
86 .d_open = vcons_open,
87 .d_close = vcons_close,
88 .d_read = ttyread,
89 .d_write = ttywrite,
90 .d_ioctl = vcons_ioctl,
70e491c0 91 .d_kqfilter = ttykqfilter,
6f7b98e0
MD
92};
93
94static int
95vcons_open(struct dev_open_args *ap)
96{
97 cdev_t dev = ap->a_head.a_dev;
98 struct tty *tp;
99 int error;
100
6f7b98e0 101 tp = dev->si_tty = ttymalloc(dev->si_tty);
cd29885a
MD
102
103#define ISSET(t, f) ((t) & (f))
79dbe9ee 104
1fdc022f
MD
105 if ((tp->t_state & TS_ISOPEN) == 0) {
106 tp->t_oproc = vcons_tty_start;
107 tp->t_param = vcons_tty_param;
108 tp->t_stop = nottystop;
109 tp->t_dev = dev;
110
111 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
112 ttychars(tp);
113 tp->t_iflag = TTYDEF_IFLAG;
114 tp->t_oflag = TTYDEF_OFLAG;
115 tp->t_cflag = TTYDEF_CFLAG;
116 tp->t_lflag = TTYDEF_LFLAG;
117 tp->t_ispeed = TTYDEF_SPEED;
118 tp->t_ospeed = TTYDEF_SPEED;
119 ttsetwater(tp);
aaf8b91f 120 }
a72d8a9f
MD
121 if (minor(dev) == 0) {
122 error = (*linesw[tp->t_line].l_open)(dev, tp);
123 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
124
125 if (kqueue_console_info == NULL)
126 kqueue_console_info = kqueue_add(0, vcons_intr, tp);
127 } else {
128 /* dummy up other minors so the installer will run */
129 error = 0;
130 }
6f7b98e0
MD
131 return(error);
132}
133
134static int
135vcons_close(struct dev_close_args *ap)
136{
137 cdev_t dev = ap->a_head.a_dev;
138 struct tty *tp;
139
6f7b98e0 140 tp = dev->si_tty;
1fdc022f
MD
141 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
142 ttyclose(tp);
6f7b98e0
MD
143 return(0);
144}
145
146static int
147vcons_ioctl(struct dev_ioctl_args *ap)
148{
149 cdev_t dev = ap->a_head.a_dev;
150 struct tty *tp;
151 int error;
152
6f7b98e0
MD
153 tp = dev->si_tty;
154 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
155 ap->a_fflag, ap->a_cred);
156 if (error != ENOIOCTL)
157 return (error);
158 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
159 if (error != ENOIOCTL)
160 return (error);
161 return (ENOTTY);
162}
163
164static int
165vcons_tty_param(struct tty *tp, struct termios *tio)
166{
167 tp->t_ispeed = tio->c_ispeed;
168 tp->t_ospeed = tio->c_ospeed;
169 tp->t_cflag = tio->c_cflag;
170 return(0);
171}
172
173static void
174vcons_tty_start(struct tty *tp)
175{
176 int n;
177 char buf[64];
178
179 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
180 ttwwakeup(tp);
181 return;
182 }
183 tp->t_state |= TS_BUSY;
a72d8a9f
MD
184 while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
185 /*
186 * Dummy up ttyv1, etc.
187 */
03bcb4ab 188 if (minor(tp->t_dev) == 0) {
94131955 189 pwrite(1, buf, n, -1);
03bcb4ab 190 }
a72d8a9f 191 }
6f7b98e0
MD
192 tp->t_state &= ~TS_BUSY;
193 ttwwakeup(tp);
194}
195
aaf8b91f
MD
196static
197void
9aeb4297 198vcons_intr(void *tpx, struct intrframe *frame __unused)
aaf8b91f
MD
199{
200 struct tty *tp = tpx;
201 unsigned char buf[32];
202 int i;
203 int n;
204
1fdc022f
MD
205 /*
206 * If we aren't open we only have synchronous traffic via the
207 * debugger and do not need to poll.
208 */
209 if ((tp->t_state & TS_ISOPEN) == 0)
210 return;
211
212 /*
213 * Only poll if we are open and haven't been stolen by the debugger.
214 */
aaf8b91f
MD
215 if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
216 do {
b09fd398 217 n = extpread(0, buf, sizeof(buf), O_FNONBLOCKING, -1LL);
aaf8b91f
MD
218 for (i = 0; i < n; ++i)
219 (*linesw[tp->t_line].l_rint)(buf[i], tp);
220 } while (n > 0);
221 }
aaf8b91f
MD
222}
223
6f7b98e0
MD
224/************************************************************************
225 * KERNEL CONSOLE INTERFACE *
226 ************************************************************************
227 *
228 * Kernel direct-call interface console driver
229 */
230static cn_probe_t vconsprobe;
231static cn_init_t vconsinit;
ce81f184 232static cn_init_fini_t vconsinit_fini;
6f7b98e0
MD
233static cn_term_t vconsterm;
234static cn_getc_t vconsgetc;
235static cn_checkc_t vconscheckc;
236static cn_putc_t vconsputc;
237
ce81f184 238CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc,
6f7b98e0
MD
239 vconscheckc, vconsputc, NULL);
240
3c5a0ece 241static struct termios init_tio;
30809ad1 242static struct consdev *vconsole;
3c5a0ece 243
6f7b98e0
MD
244static void
245vconsprobe(struct consdev *cp)
246{
e7f2d7de 247 cp->cn_pri = CN_NORMAL;
ce81f184 248 cp->cn_probegood = 1;
3c5a0ece 249}
e7f2d7de 250
3c5a0ece
SS
251/*
252 * This is a little bulky handler to set proper terminal
253 * settings in the case of a signal which might lead to
254 * termination or suspension.
255 */
256static void
257vconssignal(int sig)
258{
259 struct termios curtio;
260 struct sigaction sa, osa;
261 sigset_t ss, oss;
262
263 tcgetattr(0, &curtio);
264 tcsetattr(0, TCSAFLUSH, &init_tio);
265 bzero(&sa, sizeof(sa));
266 sigemptyset(&sa.sa_mask);
267 sa.sa_handler = SIG_DFL;
268 sigaction(sig, &sa, &osa);
269 sigemptyset(&ss);
270 sigaddset(&ss, sig);
271 sigprocmask(SIG_UNBLOCK, &ss, &oss);
272 raise(sig); /* now hand down the sig */
273 sigprocmask(SIG_SETMASK, &oss, NULL);
274 sigaction(sig, &osa, NULL);
275 tcsetattr(0, TCSAFLUSH, &curtio);
3c5a0ece
SS
276}
277
30809ad1 278static void
0df380ef
MD
279vconswinchsig(int __unused sig)
280{
281 signalintr(3);
282}
283
284static void
285vconswinch_intr(void *arg __unused, void *frame __unused)
30809ad1
SS
286{
287 struct winsize newsize;
288
289 if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
290 ioctl(0, TIOCGWINSZ, &newsize);
291 /*
292 * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
293 * I wished. Unfortunately this needs a curproc, so do it
294 * manually.
295 */
296 if (bcmp(&newsize, &vconsole->cn_dev->si_tty->t_winsize,
297 sizeof(newsize)) != 0) {
298 vconsole->cn_dev->si_tty->t_winsize = newsize;
299 pgsignal(vconsole->cn_dev->si_tty->t_pgrp, SIGWINCH, 1);
300 }
301 }
302}
303
304static void
305vconscleanup(void)
3c5a0ece 306{
db74ac99
SS
307 /*
308 * We might catch stray SIGIOs, so try hard.
309 */
310 while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
311 /* NOTHING */;
6f7b98e0
MD
312}
313
314static void
315vconsinit(struct consdev *cp)
316{
3c5a0ece
SS
317 struct sigaction sa;
318
30809ad1
SS
319 vconsole = cp;
320
3c5a0ece
SS
321 tcgetattr(0, &init_tio);
322 bzero(&sa, sizeof(sa));
323 sigemptyset(&sa.sa_mask);
324 sa.sa_handler = vconssignal;
325 sigaction(SIGTSTP, &sa, NULL);
326 sigaction(SIGINT, &sa, NULL);
327 sigaction(SIGTERM, &sa, NULL);
328 atexit(vconscleanup);
3c5a0ece 329 vcons_set_mode(0);
6f7b98e0
MD
330}
331
ce81f184
MD
332static void
333vconsinit_fini(struct consdev *cp)
334{
0df380ef 335 struct sigaction sa;
a72d8a9f
MD
336 cdev_t dev;
337 int i;
338
0df380ef
MD
339 /*
340 * We have to do this here rather then in early boot to be able
341 * to use the interrupt subsystem.
342 */
343 register_int(3, vconswinch_intr, NULL, "swinch", NULL, 0);
344 bzero(&sa, sizeof(sa));
345 sigemptyset(&sa.sa_mask);
346 sa.sa_handler = vconswinchsig;
347 sigaction(SIGWINCH, &sa, NULL);
348
a72d8a9f
MD
349 /*
350 * Implement ttyv0-ttyv7. At the moment ttyv1-7 are sink nulls.
351 */
a72d8a9f
MD
352 for (i = 0; i < 8; ++i) {
353 dev = make_dev(&vcons_ops, i,
354 UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
cd29885a 355 if (i == 0) {
a72d8a9f 356 cp->cn_dev = dev;
cd29885a 357 }
a72d8a9f 358 }
db74ac99 359 EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
ce81f184
MD
360}
361
6f7b98e0
MD
362static void
363vconsterm(struct consdev *vp)
364{
30809ad1 365 vconsole = NULL;
3c5a0ece 366 vconscleanup();
6f7b98e0
MD
367}
368
369static int
ce81f184 370vconsgetc(void *private)
6f7b98e0
MD
371{
372 unsigned char c;
6092278a
MD
373 ssize_t n;
374
aaf8b91f 375 console_stolen_by_kernel = 1;
6092278a 376 for (;;) {
94131955 377 n = pread(0, &c, 1, -1);
03bcb4ab 378 if (n == 1)
aaf8b91f 379 break;
6092278a
MD
380 if (n < 0 && errno == EINTR)
381 continue;
382 panic("vconsgetc: EOF on console %d %d", n ,errno);
383 }
aaf8b91f
MD
384 console_stolen_by_kernel = 0;
385 return((int)c);
6f7b98e0
MD
386}
387
388static int
ce81f184 389vconscheckc(void *private)
6f7b98e0
MD
390{
391 unsigned char c;
392
b09fd398 393 if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
6f7b98e0
MD
394 return((int)c);
395 return(-1);
396}
397
398static void
ce81f184 399vconsputc(void *private, int c)
6f7b98e0
MD
400{
401 char cc = c;
402
94131955 403 pwrite(1, &cc, 1, -1);
6f7b98e0
MD
404}
405
cb118784
MD
406void
407vcons_set_mode(int in_debugger)
408{
409 struct termios tio;
410
411 if (tcgetattr(0, &tio) < 0)
412 return;
413 cfmakeraw(&tio);
1b40cca7 414 tio.c_oflag |= OPOST | ONLCR;
cb118784
MD
415 tio.c_lflag |= ISIG;
416 if (in_debugger) {
3c5a0ece
SS
417 tio.c_cc[VINTR] = init_tio.c_cc[VINTR];
418 tio.c_cc[VSUSP] = init_tio.c_cc[VSUSP];
419 tio.c_cc[VSTATUS] = init_tio.c_cc[VSTATUS];
cb118784 420 } else {
5f99c60c
YT
421 tio.c_cc[VINTR] = _POSIX_VDISABLE;
422 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
423 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
cb118784
MD
424 }
425 tcsetattr(0, TCSAFLUSH, &tio);
426}