xref: /freebsd/sys/dev/ofw/ofw_console.c (revision 6356dba0b403daa023dec24559ab1f8e602e4f14)
1 /*-
2  * Copyright (C) 2001 Benno Rice.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 #include "opt_comconsole.h"
30 #include "opt_ofw.h"
31 
32 #include <sys/param.h>
33 #include <sys/kdb.h>
34 #include <sys/kernel.h>
35 #include <sys/priv.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/conf.h>
39 #include <sys/cons.h>
40 #include <sys/consio.h>
41 #include <sys/tty.h>
42 
43 #include <dev/ofw/openfirm.h>
44 
45 #include <ddb/ddb.h>
46 
47 #ifndef	OFWCONS_POLL_HZ
48 #define	OFWCONS_POLL_HZ	4	/* 50-100 works best on Ultra2 */
49 #endif
50 #define OFBURSTLEN	128	/* max number of bytes to write in one chunk */
51 
52 static tsw_open_t ofwtty_open;
53 static tsw_close_t ofwtty_close;
54 static tsw_outwakeup_t ofwtty_outwakeup;
55 
56 static struct ttydevsw ofw_ttydevsw = {
57 	.tsw_flags	= TF_NOPREFIX,
58 	.tsw_open	= ofwtty_open,
59 	.tsw_close	= ofwtty_close,
60 	.tsw_outwakeup	= ofwtty_outwakeup,
61 };
62 
63 static struct tty		*ofw_tp = NULL;
64 static int			polltime;
65 static struct callout_handle	ofw_timeouthandle
66     = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle);
67 
68 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
69 static int			alt_break_state;
70 #endif
71 
72 static void	ofw_timeout(void *);
73 
74 static cn_probe_t	ofw_cnprobe;
75 static cn_init_t	ofw_cninit;
76 static cn_term_t	ofw_cnterm;
77 static cn_getc_t	ofw_cngetc;
78 static cn_putc_t	ofw_cnputc;
79 
80 CONSOLE_DRIVER(ofw);
81 
82 static void
83 cn_drvinit(void *unused)
84 {
85 	phandle_t options;
86 	char output[32];
87 	struct tty *tp;
88 
89 	if (ofw_consdev.cn_pri != CN_DEAD &&
90 	    ofw_consdev.cn_name[0] != '\0') {
91 		if ((options = OF_finddevice("/options")) == -1 ||
92 		    OF_getprop(options, "output-device", output,
93 		    sizeof(output)) == -1)
94 			return;
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 		tp = tty_alloc(&ofw_ttydevsw, NULL, NULL);
100 		tty_makedev(tp, NULL, "%s", output);
101 		tty_makealias(tp, "ofwcons");
102 	}
103 }
104 
105 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
106 
107 static int	stdin;
108 static int	stdout;
109 
110 static int
111 ofwtty_open(struct tty *tp)
112 {
113 	polltime = hz / OFWCONS_POLL_HZ;
114 	if (polltime < 1)
115 		polltime = 1;
116 
117 	ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
118 
119 	return (0);
120 }
121 
122 static void
123 ofwtty_close(struct tty *tp)
124 {
125 
126 	/* XXX Should be replaced with callout_stop(9) */
127 	untimeout(ofw_timeout, tp, ofw_timeouthandle);
128 }
129 
130 static void
131 ofwtty_outwakeup(struct tty *tp)
132 {
133 	int len;
134 	u_char buf[OFBURSTLEN];
135 
136 	for (;;) {
137 		len = ttydisc_getc(tp, buf, sizeof buf);
138 		if (len == 0)
139 			break;
140 		OF_write(stdout, buf, len);
141 	}
142 }
143 
144 static void
145 ofw_timeout(void *v)
146 {
147 	struct	tty *tp;
148 	int 	c;
149 
150 	tp = (struct tty *)v;
151 
152 	tty_lock(tp);
153 	while ((c = ofw_cngetc(NULL)) != -1)
154 		ttydisc_rint(tp, c, 0);
155 	ttydisc_rint_done(tp);
156 	tty_unlock(tp);
157 
158 	ofw_timeouthandle = timeout(ofw_timeout, tp, 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_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
172 		cp->cn_pri = CN_DEAD;
173 		return;
174 	}
175 
176 	if (OF_getprop(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 	sprintf(cp->cn_name, "ofwcons");
190 	cp->cn_tp = ofw_tp;
191 }
192 
193 static void
194 ofw_cnterm(struct consdev *cp)
195 {
196 }
197 
198 static int
199 ofw_cngetc(struct consdev *cp)
200 {
201 	unsigned char ch;
202 
203 	if (OF_read(stdin, &ch, 1) > 0) {
204 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
205 		int kdb_brk;
206 
207 		if ((kdb_brk = kdb_alt_break(ch, &alt_break_state)) != 0) {
208 			switch (kdb_brk) {
209 			case KDB_REQ_DEBUGGER:
210 				kdb_enter(KDB_WHY_BREAK,
211 				    "Break sequence on console");
212 				break;
213 			case KDB_REQ_PANIC:
214 				kdb_panic("Panic sequence on console");
215 				break;
216 			case KDB_REQ_REBOOT:
217 				kdb_reboot();
218 				break;
219 
220 			}
221 		}
222 #endif
223 		return (ch);
224 	}
225 
226 	return (-1);
227 }
228 
229 static void
230 ofw_cnputc(struct consdev *cp, int c)
231 {
232 	char cbuf;
233 
234 	if (c == '\n') {
235 		cbuf = '\r';
236 		OF_write(stdout, &cbuf, 1);
237 	}
238 
239 	cbuf = c;
240 	OF_write(stdout, &cbuf, 1);
241 }
242