ofw_console.c revision 225214
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: head/sys/dev/ofw/ofw_console.c 225214 2011-08-27 14:24:27Z rwatson $");
28
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
51static tsw_open_t ofwtty_open;
52static tsw_close_t ofwtty_close;
53static tsw_outwakeup_t ofwtty_outwakeup;
54
55static 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
62static int			polltime;
63static struct callout_handle	ofw_timeouthandle
64    = CALLOUT_HANDLE_INITIALIZER(&ofw_timeouthandle);
65
66#if defined(KDB)
67static int			alt_break_state;
68#endif
69
70static void	ofw_timeout(void *);
71
72static cn_probe_t	ofw_cnprobe;
73static cn_init_t	ofw_cninit;
74static cn_term_t	ofw_cnterm;
75static cn_getc_t	ofw_cngetc;
76static cn_putc_t	ofw_cnputc;
77
78CONSOLE_DRIVER(ofw);
79
80static void
81cn_drvinit(void *unused)
82{
83	phandle_t options;
84	char output[32];
85	struct tty *tp;
86
87	if (ofw_consdev.cn_pri != CN_DEAD &&
88	    ofw_consdev.cn_name[0] != '\0') {
89		if ((options = OF_finddevice("/options")) == -1 ||
90		    OF_getprop(options, "output-device", output,
91		    sizeof(output)) == -1)
92			return;
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		tp = tty_alloc(&ofw_ttydevsw, NULL);
98		tty_makedev(tp, NULL, "%s", output);
99		tty_makealias(tp, "ofwcons");
100	}
101}
102
103SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
104
105static int	stdin;
106static int	stdout;
107
108static int
109ofwtty_open(struct tty *tp)
110{
111	polltime = hz / OFWCONS_POLL_HZ;
112	if (polltime < 1)
113		polltime = 1;
114
115	ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
116
117	return (0);
118}
119
120static void
121ofwtty_close(struct tty *tp)
122{
123
124	/* XXX Should be replaced with callout_stop(9) */
125	untimeout(ofw_timeout, tp, ofw_timeouthandle);
126}
127
128static void
129ofwtty_outwakeup(struct tty *tp)
130{
131	int len;
132	u_char buf[OFBURSTLEN];
133
134	for (;;) {
135		len = ttydisc_getc(tp, buf, sizeof buf);
136		if (len == 0)
137			break;
138		OF_write(stdout, buf, len);
139	}
140}
141
142static void
143ofw_timeout(void *v)
144{
145	struct	tty *tp;
146	int 	c;
147
148	tp = (struct tty *)v;
149
150	tty_lock(tp);
151	while ((c = ofw_cngetc(NULL)) != -1)
152		ttydisc_rint(tp, c, 0);
153	ttydisc_rint_done(tp);
154	tty_unlock(tp);
155
156	ofw_timeouthandle = timeout(ofw_timeout, tp, polltime);
157}
158
159static void
160ofw_cnprobe(struct consdev *cp)
161{
162	int chosen;
163
164	if ((chosen = OF_finddevice("/chosen")) == -1) {
165		cp->cn_pri = CN_DEAD;
166		return;
167	}
168
169	if (OF_getprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
170		cp->cn_pri = CN_DEAD;
171		return;
172	}
173
174	if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) {
175		cp->cn_pri = CN_DEAD;
176		return;
177	}
178
179	cp->cn_pri = CN_LOW;
180}
181
182static void
183ofw_cninit(struct consdev *cp)
184{
185
186	/* XXX: This is the alias, but that should be good enough */
187	strcpy(cp->cn_name, "ofwcons");
188}
189
190static void
191ofw_cnterm(struct consdev *cp)
192{
193}
194
195static int
196ofw_cngetc(struct consdev *cp)
197{
198	unsigned char ch;
199
200	if (OF_read(stdin, &ch, 1) > 0) {
201#if defined(KDB)
202		kdb_alt_break(ch, &alt_break_state);
203#endif
204		return (ch);
205	}
206
207	return (-1);
208}
209
210static void
211ofw_cnputc(struct consdev *cp, int c)
212{
213	char cbuf;
214
215	if (c == '\n') {
216		cbuf = '\r';
217		OF_write(stdout, &cbuf, 1);
218	}
219
220	cbuf = c;
221	OF_write(stdout, &cbuf, 1);
222}
223