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