2 * Copyright (C) 2001 Benno Rice.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_comconsole.h"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
41 #include <sys/consio.h>
44 #include <dev/ofw/openfirm.h>
50 static d_open_t ofw_dev_open;
51 static d_close_t ofw_dev_close;
52 static d_ioctl_t ofw_dev_ioctl;
56 static struct cdevsw ofw_cdevsw = {
57 .d_open = ofw_dev_open,
58 .d_close = ofw_dev_close,
61 .d_ioctl = ofw_dev_ioctl,
67 static struct tty *ofw_tp = NULL;
69 static struct callout_handle ofw_timeouthandle
70 = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle);
72 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
73 static int alt_break_state;
76 static void ofw_tty_start(struct tty *);
77 static int ofw_tty_param(struct tty *, struct termios *);
78 static void ofw_tty_stop(struct tty *, int);
79 static void ofw_timeout(void *);
81 static cn_probe_t ofw_cons_probe;
82 static cn_init_t ofw_cons_init;
83 static cn_getc_t ofw_cons_getc;
84 static cn_checkc_t ofw_cons_checkc;
85 static cn_putc_t ofw_cons_putc;
87 CONS_DRIVER(ofw, ofw_cons_probe, ofw_cons_init, NULL, ofw_cons_getc,
88 ofw_cons_checkc, ofw_cons_putc, NULL);
91 cn_drvinit(void *unused)
96 if (ofw_consdev.cn_dev != NULL) {
97 if ((options = OF_finddevice("/options")) == -1 ||
98 OF_getprop(options, "output-device", output,
99 sizeof(output)) == -1)
101 make_dev(&ofw_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "%s",
103 make_dev_alias(ofw_consdev.cn_dev, "ofwcons");
107 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE + CDEV_MAJOR, cn_drvinit, NULL)
113 ofw_dev_open(dev_t dev, int flag, int mode, struct thread *td)
117 int error, setuptimeout;
123 tp = ofw_tp = dev->si_tty = ttymalloc(ofw_tp);
125 tp->t_oproc = ofw_tty_start;
126 tp->t_param = ofw_tty_param;
127 tp->t_stop = ofw_tty_stop;
130 if ((tp->t_state & TS_ISOPEN) == 0) {
131 tp->t_state |= TS_CARR_ON;
133 tp->t_iflag = TTYDEF_IFLAG;
134 tp->t_oflag = TTYDEF_OFLAG;
135 tp->t_cflag = TTYDEF_CFLAG|CLOCAL;
136 tp->t_lflag = TTYDEF_LFLAG;
137 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
141 } else if ((tp->t_state & TS_XCLUDE) && suser(td)) {
145 error = (*linesw[tp->t_line].l_open)(dev, tp);
147 if (error == 0 && setuptimeout) {
148 polltime = hz / OFW_POLL_HZ;
153 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
160 ofw_dev_close(dev_t dev, int flag, int mode, struct thread *td)
172 (*linesw[tp->t_line].l_close)(tp, flag);
179 ofw_dev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
192 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
193 if (error != ENOIOCTL) {
197 error = ttioctl(tp, cmd, data, flag);
198 if (error != ENOIOCTL) {
206 ofw_tty_param(struct tty *tp, struct termios *t)
213 ofw_tty_start(struct tty *tp)
216 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
221 tp->t_state |= TS_BUSY;
222 while (tp->t_outq.c_cc != 0) {
223 ofw_cons_putc(NULL, getc(&tp->t_outq));
225 tp->t_state &= ~TS_BUSY;
231 ofw_tty_stop(struct tty *tp, int flag)
234 if (tp->t_state & TS_BUSY) {
235 if ((tp->t_state & TS_TTSTOP) == 0) {
236 tp->t_state |= TS_FLUSH;
247 tp = (struct tty *)v;
249 while ((c = ofw_cons_checkc(NULL)) != -1) {
250 if (tp->t_state & TS_ISOPEN) {
251 (*linesw[tp->t_line].l_rint)(c, tp);
255 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
259 ofw_cons_probe(struct consdev *cp)
263 if ((chosen = OF_finddevice("/chosen")) == -1) {
264 cp->cn_pri = CN_DEAD;
268 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
269 cp->cn_pri = CN_DEAD;
273 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) {
274 cp->cn_pri = CN_DEAD;
279 cp->cn_pri = CN_INTERNAL;
283 ofw_cons_init(struct consdev *cp)
286 cp->cn_dev = makedev(CDEV_MAJOR, 0);
291 ofw_cons_getc(struct consdev *cp)
298 while ((l = OF_read(stdin, &ch, 1)) != 1) {
299 if (l != -2 && l != 0) {
304 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
305 if (db_alt_break(ch, &alt_break_state))
313 ofw_cons_checkc(struct consdev *cp)
317 if (OF_read(stdin, &ch, 1) > 0) {
318 #if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER)
319 if (db_alt_break(ch, &alt_break_state))
329 ofw_cons_putc(struct consdev *cp, int c)
335 OF_write(stdout, &cbuf, 1);
339 OF_write(stdout, &cbuf, 1);