cfe_console.c revision 186513
1/*-
2 * Copyright (c) 2007 Bruce M. Simpson.
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 THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/cfe/cfe_console.c 186513 2008-12-27 11:38:41Z bz $");
29
30#include "opt_comconsole.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/cfe/cfe_api.h>
44#include <dev/cfe/cfe_error.h>
45
46#include <ddb/ddb.h>
47
48#ifndef	CFECONS_POLL_HZ
49#define	CFECONS_POLL_HZ	4
50#endif
51#define CFEBURSTLEN	128	/* max number of bytes to write in one chunk */
52
53static tsw_open_t cfe_tty_open;
54static tsw_close_t cfe_tty_close;
55static tsw_outwakeup_t cfe_tty_outwakeup;
56
57static struct ttydevsw cfe_ttydevsw = {
58	.tsw_flags	= TF_NOPREFIX,
59	.tsw_open	= cfe_tty_open,
60	.tsw_close	= cfe_tty_close,
61	.tsw_outwakeup	= cfe_tty_outwakeup,
62};
63
64static int			conhandle = -1;
65/* XXX does cfe have to poll? */
66static int			polltime;
67static struct callout_handle	cfe_timeouthandle
68    = CALLOUT_HANDLE_INITIALIZER(&cfe_timeouthandle);
69
70#if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
71static int			alt_break_state;
72#endif
73
74static void	cfe_timeout(void *);
75
76static cn_probe_t	cfe_cnprobe;
77static cn_init_t	cfe_cninit;
78static cn_term_t	cfe_cnterm;
79static cn_getc_t	cfe_cngetc;
80static cn_putc_t	cfe_cnputc;
81
82CONSOLE_DRIVER(cfe);
83
84static void
85cn_drvinit(void *unused)
86{
87	char output[32];
88	struct tty *tp;
89
90	if (cfe_consdev.cn_pri != CN_DEAD &&
91	    cfe_consdev.cn_name[0] != '\0') {
92		tp = tty_alloc(&cfe_ttydevsw, NULL, NULL);
93		tty_makedev(tp, NULL, "%s", output);
94		tty_makealias(tp, "cfecons");
95	}
96}
97
98static int
99cfe_tty_open(struct tty *tp)
100{
101	polltime = hz / CFECONS_POLL_HZ;
102	if (polltime < 1)
103		polltime = 1;
104	cfe_timeouthandle = timeout(cfe_timeout, tp, polltime);
105
106	return (0);
107}
108
109static void
110cfe_tty_close(struct tty *tp)
111{
112
113	/* XXX Should be replaced with callout_stop(9) */
114	untimeout(cfe_timeout, tp, cfe_timeouthandle);
115}
116
117static void
118cfe_tty_outwakeup(struct tty *tp)
119{
120	int len;
121	u_char buf[CFEBURSTLEN];
122
123	for (;;) {
124		len = ttydisc_getc(tp, buf, sizeof buf);
125		if (len == 0)
126			break;
127		while (cfe_write(conhandle, buf, len) == 0)
128			continue;
129	}
130}
131
132static void
133cfe_timeout(void *v)
134{
135	struct	tty *tp;
136	int 	c;
137
138	tp = (struct tty *)v;
139
140	tty_lock(tp);
141	while ((c = cfe_cngetc(NULL)) != -1)
142		ttydisc_rint(tp, c, 0);
143	ttydisc_rint_done(tp);
144	tty_unlock(tp);
145
146	cfe_timeouthandle = timeout(cfe_timeout, tp, polltime);
147}
148
149static void
150cfe_cnprobe(struct consdev *cp)
151{
152
153	conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
154	if (conhandle < 0) {
155		cp->cn_pri = CN_DEAD;
156		return;
157	}
158
159	/* XXX */
160	if (bootverbose) {
161		char *bootmsg = "Using CFE firmware console.\n";
162		int i;
163
164		for (i = 0; i < strlen(bootmsg); i++)
165			cfe_cnputc(cp, bootmsg[i]);
166	}
167
168	cp->cn_pri = CN_LOW;
169}
170
171static void
172cfe_cninit(struct consdev *cp)
173{
174
175	strcpy(cp->cn_name, "cfecons");
176}
177
178static void
179cfe_cnterm(struct consdev *cp)
180{
181
182}
183
184static int
185cfe_cngetc(struct consdev *cp)
186{
187	int result;
188	unsigned char ch;
189
190	while ((result = cfe_read(conhandle, &ch, 1)) == 0)
191		continue;
192
193	if (result > 0) {
194#if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
195		int kdb_brk;
196
197		if ((kdb_brk = kdb_alt_break(ch, &alt_break_state)) != 0) {
198			switch (kdb_brk) {
199			case KDB_REQ_DEBUGGER:
200				kdb_enter(KDB_WHY_BREAK,
201				    "Break sequence on console");
202				break;
203			case KDB_REQ_PANIC:
204				kdb_panic("Panic sequence on console");
205				break;
206			case KDB_REQ_REBOOT:
207				kdb_reboot();
208				break;
209
210			}
211		}
212#endif
213		return (ch);
214	}
215
216	return (-1);
217}
218
219static void
220cfe_cnputc(struct consdev *cp, int c)
221{
222	char cbuf;
223
224	if (c == '\n')
225		cfe_cnputc(cp, '\r');
226
227	cbuf = c;
228	while (cfe_write(conhandle, &cbuf, 1) == 0)
229		continue;
230}
231
232SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
233