Merge branch 'master' into kq_devices
[dragonfly.git] / sys / platform / vkernel / platform / console.c
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  * 
34  * $DragonFly: src/sys/platform/vkernel/platform/console.c,v 1.17 2007/07/02 04:19:14 dillon Exp $
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>
44 #include <sys/signalvar.h>
45 #include <sys/eventhandler.h>
46 #include <sys/interrupt.h>
47 #include <machine/md_var.h>
48 #include <unistd.h>
49 #include <termios.h>
50 #include <stdlib.h>
51
52 static int console_stolen_by_kernel;
53 static struct kqueue_info *kqueue_console_info;
54
55 /*
56  * Global console locking functions
57  */
58 void
59 cons_lock(void)
60 {
61 }
62
63 void
64 cons_unlock(void)
65 {
66 }
67
68 /************************************************************************
69  *                          CONSOLE DEVICE                              *
70  ************************************************************************
71  *
72  */
73
74 #define CDEV_MAJOR      12      /* steal the same major as /dev/ttyv* */
75
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);
79
80 static d_open_t         vcons_open;
81 static d_close_t        vcons_close;
82 static d_ioctl_t        vcons_ioctl;
83
84 static struct dev_ops vcons_ops = {
85         { "vcons", CDEV_MAJOR, D_TTY | D_KQFILTER },
86         .d_open =       vcons_open,
87         .d_close =      vcons_close,
88         .d_read =       ttyread,
89         .d_write =      ttywrite,
90         .d_ioctl =      vcons_ioctl,
91         .d_poll =       ttypoll,
92         .d_kqfilter =   ttykqfilter,
93 };
94
95 static int
96 vcons_open(struct dev_open_args *ap)
97 {
98         cdev_t dev = ap->a_head.a_dev;
99         struct tty *tp;
100         int error;
101
102         tp = dev->si_tty = ttymalloc(dev->si_tty);
103
104 #define ISSET(t, f)     ((t) & (f))
105
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;
110                 tp->t_dev = dev;
111
112                 tp->t_state |= TS_CARR_ON | TS_CONNECTED;
113                 ttychars(tp);
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;
120                 ttsetwater(tp);
121         }
122         if (minor(dev) == 0) {
123                 error = (*linesw[tp->t_line].l_open)(dev, tp);
124                 ioctl(0, TIOCGWINSZ, &tp->t_winsize);
125
126                 if (kqueue_console_info == NULL)
127                         kqueue_console_info = kqueue_add(0, vcons_intr, tp);
128         } else {
129                 /* dummy up other minors so the installer will run */
130                 error = 0;
131         }
132         return(error);
133 }
134
135 static int
136 vcons_close(struct dev_close_args *ap)
137 {
138         cdev_t dev = ap->a_head.a_dev;
139         struct tty *tp;
140
141         tp = dev->si_tty;
142         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
143         ttyclose(tp);
144         return(0);
145 }
146
147 static int
148 vcons_ioctl(struct dev_ioctl_args *ap)
149 {
150         cdev_t dev = ap->a_head.a_dev;
151         struct tty *tp;
152         int error;
153
154         tp = dev->si_tty;
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)
158                 return (error);
159         error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
160         if (error != ENOIOCTL)
161                 return (error);
162         return (ENOTTY);
163 }
164
165 static int
166 vcons_tty_param(struct tty *tp, struct termios *tio)
167 {
168         tp->t_ispeed = tio->c_ispeed;
169         tp->t_ospeed = tio->c_ospeed;
170         tp->t_cflag = tio->c_cflag;
171         return(0);
172 }
173
174 static void
175 vcons_tty_start(struct tty *tp)
176 {
177         int n;
178         char buf[64];
179
180         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
181                 ttwwakeup(tp);
182                 return;
183         }
184         tp->t_state |= TS_BUSY;
185         while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0) {
186                 /*
187                  * Dummy up ttyv1, etc.
188                  */
189                 if (minor(tp->t_dev) == 0) {
190                         pwrite(1, buf, n, -1);
191                 }
192         }
193         tp->t_state &= ~TS_BUSY;
194         ttwwakeup(tp);
195 }
196
197 static
198 void
199 vcons_intr(void *tpx, struct intrframe *frame __unused)
200 {
201         struct tty *tp = tpx;
202         unsigned char buf[32];
203         int i;
204         int n;
205
206         /*
207          * If we aren't open we only have synchronous traffic via the
208          * debugger and do not need to poll.
209          */
210         if ((tp->t_state & TS_ISOPEN) == 0)
211                 return;
212
213         /*
214          * Only poll if we are open and haven't been stolen by the debugger.
215          */
216         if (console_stolen_by_kernel == 0 && (tp->t_state & TS_ISOPEN)) {
217                 do {
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);
221                 } while (n > 0);
222         }
223 }
224
225 /************************************************************************
226  *                      KERNEL CONSOLE INTERFACE                        *
227  ************************************************************************
228  *
229  * Kernel direct-call interface console driver
230  */
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;
238
239 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsinit_fini, vconsterm, vconsgetc, 
240                 vconscheckc, vconsputc, NULL);
241
242 static struct termios init_tio;
243 static struct consdev *vconsole;
244
245 static void
246 vconsprobe(struct consdev *cp)
247 {
248         cp->cn_pri = CN_NORMAL;
249         cp->cn_probegood = 1;
250 }
251
252 /*
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.
256  */
257 static void
258 vconssignal(int sig)
259 {
260         struct termios curtio;
261         struct sigaction sa, osa;
262         sigset_t ss, oss;
263
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);
270         sigemptyset(&ss);
271         sigaddset(&ss, sig);
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);
277 }
278
279 static void
280 vconswinchsig(int __unused sig)
281 {
282         signalintr(3);
283 }
284
285 static void
286 vconswinch_intr(void *arg __unused, void *frame __unused)
287 {
288         struct winsize newsize;
289
290         if (vconsole != NULL && vconsole->cn_dev->si_tty != NULL) {
291                 ioctl(0, TIOCGWINSZ, &newsize);
292                 /*
293                  * ttioctl(vconsole->cn_dev->si_tty, TIOCSWINSZ, &newsize, 0);
294                  * I wished.  Unfortunately this needs a curproc, so do it
295                  * manually.
296                  */
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);
301                 }
302         }
303 }
304
305 static void
306 vconscleanup(void)
307 {
308         /*
309          * We might catch stray SIGIOs, so try hard.
310          */
311         while (tcsetattr(0, TCSAFLUSH, &init_tio) != 0 && errno == EINTR)
312                 /* NOTHING */;
313 }
314
315 static void
316 vconsinit(struct consdev *cp)
317 {
318         struct sigaction sa;
319
320         vconsole = cp;
321
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);
330         vcons_set_mode(0);
331 }
332
333 static void
334 vconsinit_fini(struct consdev *cp)
335 {
336         struct sigaction sa;
337         cdev_t dev;
338         int i;
339
340         /*
341          * We have to do this here rather then in early boot to be able
342          * to use the interrupt subsystem.
343          */
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);
349
350         /*
351          * Implement ttyv0-ttyv7.  At the moment ttyv1-7 are sink nulls.
352          */
353         for (i = 0; i < 8; ++i) {
354                 dev = make_dev(&vcons_ops, i,
355                                UID_ROOT, GID_WHEEL, 0600, "ttyv%d", i);
356                 if (i == 0) {
357                         cp->cn_dev = dev;
358                 }
359         }
360         EVENTHANDLER_REGISTER(shutdown_final, vconscleanup, NULL, SHUTDOWN_PRI_LAST);
361 }
362
363 static void
364 vconsterm(struct consdev *vp)
365 {
366         vconsole = NULL;
367         vconscleanup();
368 }
369
370 static int
371 vconsgetc(void *private)
372 {
373         unsigned char c;
374         ssize_t n;
375
376         console_stolen_by_kernel = 1;
377         for (;;) {
378                 n = pread(0, &c, 1, -1);
379                 if (n == 1)
380                         break;
381                 if (n < 0 && errno == EINTR)
382                         continue;
383                 panic("vconsgetc: EOF on console %d %d", n ,errno);
384         }
385         console_stolen_by_kernel = 0;
386         return((int)c);
387 }
388
389 static int
390 vconscheckc(void *private)
391 {
392         unsigned char c;
393
394         if (extpread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
395                 return((int)c);
396         return(-1);
397 }
398
399 static void
400 vconsputc(void *private, int c)
401 {
402         char cc = c;
403
404         pwrite(1, &cc, 1, -1);
405 }
406
407 void
408 vcons_set_mode(int in_debugger)
409 {
410         struct termios tio;
411
412         if (tcgetattr(0, &tio) < 0)
413                 return;
414         cfmakeraw(&tio);
415         tio.c_oflag |= OPOST | ONLCR;
416         tio.c_lflag |= ISIG;
417         if (in_debugger) {
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];
421         } else {
422                 tio.c_cc[VINTR] = _POSIX_VDISABLE;
423                 tio.c_cc[VSUSP] = _POSIX_VDISABLE;
424                 tio.c_cc[VSTATUS] = _POSIX_VDISABLE;
425         }
426         tcsetattr(0, TCSAFLUSH, &tio);
427 }