xref: /freebsd/sys/dev/ofw/ofw_console.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2001 Benno Rice.
5  * All rights reserved.
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  * 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.
15  *
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.
26  */
27 
28 #include <sys/cdefs.h>
29 #include "opt_ofw.h"
30 
31 #include <sys/param.h>
32 #include <sys/kdb.h>
33 #include <sys/kernel.h>
34 #include <sys/priv.h>
35 #include <sys/systm.h>
36 #include <sys/types.h>
37 #include <sys/conf.h>
38 #include <sys/cons.h>
39 #include <sys/consio.h>
40 #include <sys/tty.h>
41 
42 #include <dev/ofw/openfirm.h>
43 
44 #include <ddb/ddb.h>
45 
46 #ifndef	OFWCONS_POLL_HZ
47 #define	OFWCONS_POLL_HZ	4	/* 50-100 works best on Ultra2 */
48 #endif
49 #define OFBURSTLEN	128	/* max number of bytes to write in one chunk */
50 
51 static tsw_open_t ofwtty_open;
52 static tsw_close_t ofwtty_close;
53 static tsw_outwakeup_t ofwtty_outwakeup;
54 
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,
60 };
61 
62 static int			polltime;
63 static struct callout		ofw_timer;
64 
65 #if defined(KDB)
66 static int			alt_break_state;
67 #endif
68 
69 static void	ofw_timeout(void *);
70 
71 static cn_probe_t	ofw_cnprobe;
72 static cn_init_t	ofw_cninit;
73 static cn_term_t	ofw_cnterm;
74 static cn_getc_t	ofw_cngetc;
75 static cn_putc_t	ofw_cnputc;
76 static cn_grab_t	ofw_cngrab;
77 static cn_ungrab_t	ofw_cnungrab;
78 
79 CONSOLE_DRIVER(ofw);
80 
81 static void
82 cn_drvinit(void *unused)
83 {
84 	phandle_t options;
85 	char output[32];
86 	struct tty *tp;
87 
88 	if (ofw_consdev.cn_pri != CN_DEAD &&
89 	    ofw_consdev.cn_name[0] != '\0') {
90 		tp = tty_alloc(&ofw_ttydevsw, NULL);
91 		tty_makedev(tp, NULL, "%s", "ofwcons");
92 
93 		/*
94 		 * XXX: This is a hack and it may result in two /dev/ttya
95 		 * XXX: devices on platforms where the sab driver works.
96 		 */
97 		if ((options = OF_finddevice("/options")) == -1 ||
98 		    OF_getprop(options, "output-device", output,
99 		    sizeof(output)) == -1)
100 			return;
101 		if (strlen(output) > 0)
102 			tty_makealias(tp, "%s", output);
103 		callout_init_mtx(&ofw_timer, tty_getlock(tp), 0);
104 	}
105 }
106 
107 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
108 
109 static pcell_t	stdin;
110 static pcell_t	stdout;
111 
112 static int
113 ofwtty_open(struct tty *tp)
114 {
115 	polltime = hz / OFWCONS_POLL_HZ;
116 	if (polltime < 1)
117 		polltime = 1;
118 
119 	callout_reset(&ofw_timer, polltime, ofw_timeout, tp);
120 
121 	return (0);
122 }
123 
124 static void
125 ofwtty_close(struct tty *tp)
126 {
127 
128 	callout_stop(&ofw_timer);
129 }
130 
131 static void
132 ofwtty_outwakeup(struct tty *tp)
133 {
134 	int len;
135 	u_char buf[OFBURSTLEN];
136 
137 	for (;;) {
138 		len = ttydisc_getc(tp, buf, sizeof buf);
139 		if (len == 0)
140 			break;
141 		OF_write(stdout, buf, len);
142 	}
143 }
144 
145 static void
146 ofw_timeout(void *v)
147 {
148 	struct	tty *tp;
149 	int 	c;
150 
151 	tp = (struct tty *)v;
152 
153 	tty_assert_locked(tp);
154 	while ((c = ofw_cngetc(NULL)) != -1)
155 		ttydisc_rint(tp, c, 0);
156 	ttydisc_rint_done(tp);
157 
158 	callout_schedule(&ofw_timer, polltime);
159 }
160 
161 static void
162 ofw_cnprobe(struct consdev *cp)
163 {
164 	int chosen;
165 
166 	if ((chosen = OF_finddevice("/chosen")) == -1) {
167 		cp->cn_pri = CN_DEAD;
168 		return;
169 	}
170 
171 	if (OF_getencprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
172 		cp->cn_pri = CN_DEAD;
173 		return;
174 	}
175 
176 	if (OF_getencprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) {
177 		cp->cn_pri = CN_DEAD;
178 		return;
179 	}
180 
181 	cp->cn_pri = CN_LOW;
182 }
183 
184 static void
185 ofw_cninit(struct consdev *cp)
186 {
187 
188 	/* XXX: This is the alias, but that should be good enough */
189 	strcpy(cp->cn_name, "ofwcons");
190 }
191 
192 static void
193 ofw_cnterm(struct consdev *cp)
194 {
195 }
196 
197 static void
198 ofw_cngrab(struct consdev *cp)
199 {
200 }
201 
202 static void
203 ofw_cnungrab(struct consdev *cp)
204 {
205 }
206 
207 static int
208 ofw_cngetc(struct consdev *cp)
209 {
210 	unsigned char ch;
211 
212 	if (OF_read(stdin, &ch, 1) > 0) {
213 #if defined(KDB)
214 		kdb_alt_break(ch, &alt_break_state);
215 #endif
216 		return (ch);
217 	}
218 
219 	return (-1);
220 }
221 
222 static void
223 ofw_cnputc(struct consdev *cp, int c)
224 {
225 	char cbuf;
226 
227 	if (c == '\n') {
228 		cbuf = '\r';
229 		OF_write(stdout, &cbuf, 1);
230 	}
231 
232 	cbuf = c;
233 	OF_write(stdout, &cbuf, 1);
234 }
235