2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2001 Benno Rice.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #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>
48 #ifndef OFWCONS_POLL_HZ
49 #define OFWCONS_POLL_HZ 4 /* 50-100 works best on Ultra2 */
51 #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
53 static tsw_open_t ofwtty_open;
54 static tsw_close_t ofwtty_close;
55 static tsw_outwakeup_t ofwtty_outwakeup;
57 static struct ttydevsw ofw_ttydevsw = {
58 .tsw_flags = TF_NOPREFIX,
59 .tsw_open = ofwtty_open,
60 .tsw_close = ofwtty_close,
61 .tsw_outwakeup = ofwtty_outwakeup,
65 static struct callout ofw_timer;
68 static int alt_break_state;
71 static void ofw_timeout(void *);
73 static cn_probe_t ofw_cnprobe;
74 static cn_init_t ofw_cninit;
75 static cn_term_t ofw_cnterm;
76 static cn_getc_t ofw_cngetc;
77 static cn_putc_t ofw_cnputc;
78 static cn_grab_t ofw_cngrab;
79 static cn_ungrab_t ofw_cnungrab;
84 cn_drvinit(void *unused)
90 if (ofw_consdev.cn_pri != CN_DEAD &&
91 ofw_consdev.cn_name[0] != '\0') {
92 tp = tty_alloc(&ofw_ttydevsw, NULL);
93 tty_makedev(tp, NULL, "%s", "ofwcons");
96 * XXX: This is a hack and it may result in two /dev/ttya
97 * XXX: devices on platforms where the sab driver works.
99 if ((options = OF_finddevice("/options")) == -1 ||
100 OF_getprop(options, "output-device", output,
101 sizeof(output)) == -1)
103 if (strlen(output) > 0)
104 tty_makealias(tp, "%s", output);
105 callout_init_mtx(&ofw_timer, tty_getlock(tp), 0);
109 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
111 static pcell_t stdin;
112 static pcell_t stdout;
115 ofwtty_open(struct tty *tp)
117 polltime = hz / OFWCONS_POLL_HZ;
121 callout_reset(&ofw_timer, polltime, ofw_timeout, tp);
127 ofwtty_close(struct tty *tp)
130 callout_stop(&ofw_timer);
134 ofwtty_outwakeup(struct tty *tp)
137 u_char buf[OFBURSTLEN];
140 len = ttydisc_getc(tp, buf, sizeof buf);
143 OF_write(stdout, buf, len);
153 tp = (struct tty *)v;
155 tty_assert_locked(tp);
156 while ((c = ofw_cngetc(NULL)) != -1)
157 ttydisc_rint(tp, c, 0);
158 ttydisc_rint_done(tp);
160 callout_schedule(&ofw_timer, polltime);
164 ofw_cnprobe(struct consdev *cp)
168 if ((chosen = OF_finddevice("/chosen")) == -1) {
169 cp->cn_pri = CN_DEAD;
173 if (OF_getencprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
174 cp->cn_pri = CN_DEAD;
178 if (OF_getencprop(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);