7da2b98579441c73544ce6556eb4c48809bc520a
[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.2 2007/01/07 05:45:06 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 <unistd.h>
45
46 /*
47  * Global console locking functions
48  */
49 void
50 cons_lock(void)
51 {
52 }
53
54 void
55 cons_unlock(void)
56 {
57 }
58
59 /************************************************************************
60  *                          CONSOLE DEVICE                              *
61  ************************************************************************
62  *
63  */
64
65 #define CDEV_MAJOR      183
66
67 static int vcons_tty_param(struct tty *tp, struct termios *tio);
68 static void vcons_tty_start(struct tty *tp);
69
70 static d_open_t         vcons_open;
71 static d_close_t        vcons_close;
72 static d_ioctl_t        vcons_ioctl;
73
74 static struct dev_ops vcons_ops = {
75         { "vcons", CDEV_MAJOR, D_TTY },
76         .d_open =       vcons_open,
77         .d_close =      vcons_close,
78         .d_read =       ttyread,
79         .d_write =      ttywrite,
80         .d_ioctl =      vcons_ioctl,
81         .d_poll =       ttypoll,
82 };
83
84 static int
85 vcons_open(struct dev_open_args *ap)
86 {
87         cdev_t dev = ap->a_head.a_dev;
88         struct tty *tp;
89         int error;
90
91         if (minor(dev) != 0)
92                 return(ENXIO);
93
94         tp = dev->si_tty = ttymalloc(dev->si_tty);
95         tp->t_oproc = vcons_tty_start;
96         tp->t_param = vcons_tty_param;
97         tp->t_stop = nottystop;
98         tp->t_dev = dev;
99
100         if (tp->t_state & TS_ISOPEN)
101                 return (EBUSY);
102
103         tp->t_state |= TS_CARR_ON;
104         ttychars(tp);
105         tp->t_iflag = TTYDEF_IFLAG;
106         tp->t_oflag = TTYDEF_OFLAG;
107         tp->t_cflag = TTYDEF_CFLAG;
108         tp->t_lflag = TTYDEF_LFLAG;
109         tp->t_ispeed = TTYDEF_SPEED;
110         tp->t_ospeed = TTYDEF_SPEED;
111         ttsetwater(tp);
112
113         error = (*linesw[tp->t_line].l_open)(dev, tp);
114         return(error);
115 }
116
117 static int
118 vcons_close(struct dev_close_args *ap)
119 {
120         cdev_t dev = ap->a_head.a_dev;
121         struct tty *tp;
122
123         if (minor(dev) != 0)
124                 return(ENXIO);
125         tp = dev->si_tty;
126         if (tp->t_state & TS_ISOPEN) {
127                 (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
128                 ttyclose(tp);
129         }
130         return(0);
131 }
132
133 static int
134 vcons_ioctl(struct dev_ioctl_args *ap)
135 {
136         cdev_t dev = ap->a_head.a_dev;
137         struct tty *tp;
138         int error;
139
140         if (minor(dev) != 0)
141                 return(ENXIO);
142         tp = dev->si_tty;
143         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
144                                               ap->a_fflag, ap->a_cred);
145         if (error != ENOIOCTL)
146                 return (error);
147         error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
148         if (error != ENOIOCTL)
149                 return (error);
150         return (ENOTTY);
151 }
152
153 static int
154 vcons_tty_param(struct tty *tp, struct termios *tio)
155 {
156         tp->t_ispeed = tio->c_ispeed;
157         tp->t_ospeed = tio->c_ospeed;
158         tp->t_cflag = tio->c_cflag;
159         return(0);
160 }
161
162 static void
163 vcons_tty_start(struct tty *tp)
164 {
165         int n;
166         char buf[64];
167
168         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
169                 ttwwakeup(tp);
170                 return;
171         }
172         tp->t_state |= TS_BUSY;
173         while ((n = q_to_b(&tp->t_outq, buf, sizeof(buf))) > 0)
174                 write(1, buf, n);
175         tp->t_state &= ~TS_BUSY;
176         ttwwakeup(tp);
177 }
178
179 /************************************************************************
180  *                      KERNEL CONSOLE INTERFACE                        *
181  ************************************************************************
182  *
183  * Kernel direct-call interface console driver
184  */
185 static cn_probe_t       vconsprobe;
186 static cn_init_t        vconsinit;
187 static cn_term_t        vconsterm;
188 static cn_getc_t        vconsgetc;
189 static cn_checkc_t      vconscheckc;
190 static cn_putc_t        vconsputc;
191
192 CONS_DRIVER(vcons, vconsprobe, vconsinit, vconsterm, vconsgetc, 
193                 vconscheckc, vconsputc, NULL);
194
195 static void
196 vconsprobe(struct consdev *cp)
197 {
198     cp->cn_pri = CN_NORMAL;
199     cp->cn_dev = make_dev(&vcons_ops, 255,
200                           UID_ROOT, GID_WHEEL, 0600, "vconsolectl");
201 }
202
203 static void
204 vconsinit(struct consdev *cp)
205 {
206 }
207
208 static void
209 vconsterm(struct consdev *vp)
210 {
211 }
212
213 static int
214 vconsgetc(cdev_t dev)
215 {
216         unsigned char c;
217         ssize_t n;
218
219         for (;;) {
220                 if ((n = read(0, &c, 1)) == 1)
221                         return((int)c);
222                 if (n < 0 && errno == EINTR)
223                         continue;
224                 panic("vconsgetc: EOF on console %d %d", n ,errno);
225         }
226 }
227
228 static int
229 vconscheckc(cdev_t dev)
230 {
231         unsigned char c;
232
233         if (__pread(0, &c, 1, O_FNONBLOCKING, -1LL) == 1)
234                 return((int)c);
235         return(-1);
236 }
237
238 static void
239 vconsputc(cdev_t dev, int c)
240 {
241         char cc = c;
242
243         write(1, &cc, 1);
244 }
245
246