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$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
39 #include <sys/consio.h>
42 #include <dev/ofw/openfirm.h>
46 #ifndef OFWCONS_POLL_HZ
47 #define OFWCONS_POLL_HZ 4 /* 50-100 works best on Ultra2 */
49 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
51 static tsw_open_t ofwtty_open;
52 static tsw_close_t ofwtty_close;
53 static tsw_outwakeup_t ofwtty_outwakeup;
55 static struct ttydevsw ofw_ttydevsw = {
56 .tsw_flags = TF_NOPREFIX,
57 .tsw_open = ofwtty_open,
58 .tsw_close = ofwtty_close,
59 .tsw_outwakeup = ofwtty_outwakeup,
63 static struct callout_handle ofw_timeouthandle
64 = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle);
67 static int alt_break_state;
70 static void ofw_timeout(void *);
72 static cn_probe_t ofw_cnprobe;
73 static cn_init_t ofw_cninit;
74 static cn_term_t ofw_cnterm;
75 static cn_getc_t ofw_cngetc;
76 static cn_putc_t ofw_cnputc;
77 static cn_grab_t ofw_cngrab;
78 static cn_ungrab_t ofw_cnungrab;
83 cn_drvinit(void *unused)
89 if (ofw_consdev.cn_pri != CN_DEAD &&
90 ofw_consdev.cn_name[0] != '\0') {
91 tp = tty_alloc(&ofw_ttydevsw, NULL);
92 tty_makedev(tp, NULL, "%s", "ofwcons");
95 * XXX: This is a hack and it may result in two /dev/ttya
96 * XXX: devices on platforms where the sab driver works.
98 if ((options = OF_finddevice("/options")) == -1 ||
99 OF_getprop(options, "output-device", output,
100 sizeof(output)) == -1)
102 if (strlen(output) > 0)
103 tty_makealias(tp, output);
107 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
113 ofwtty_open(struct tty *tp)
115 polltime = hz / OFWCONS_POLL_HZ;
119 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
125 ofwtty_close(struct tty *tp)
128 /* XXX Should be replaced with callout_stop(9) */
129 untimeout(ofw_timeout, tp, ofw_timeouthandle);
133 ofwtty_outwakeup(struct tty *tp)
136 u_char buf[OFBURSTLEN];
139 len = ttydisc_getc(tp, buf, sizeof buf);
142 OF_write(stdout, buf, len);
152 tp = (struct tty *)v;
155 while ((c = ofw_cngetc(NULL)) != -1)
156 ttydisc_rint(tp, c, 0);
157 ttydisc_rint_done(tp);
160 ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
164 ofw_cnprobe(struct consdev *cp)
168 if ((chosen = OF_finddevice("/chosen")) == -1) {
169 cp->cn_pri = CN_DEAD;
173 if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
174 cp->cn_pri = CN_DEAD;
178 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) {
179 cp->cn_pri = CN_DEAD;
187 ofw_cninit(struct consdev *cp)
190 /* XXX: This is the alias, but that should be good enough */
191 strcpy(cp->cn_name, "ofwcons");
195 ofw_cnterm(struct consdev *cp)
200 ofw_cngrab(struct consdev *cp)
205 ofw_cnungrab(struct consdev *cp)
210 ofw_cngetc(struct consdev *cp)
214 if (OF_read(stdin, &ch, 1) > 0) {
216 kdb_alt_break(ch, &alt_break_state);
225 ofw_cnputc(struct consdev *cp, int c)
231 OF_write(stdout, &cbuf, 1);
235 OF_write(stdout, &cbuf, 1);