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