bvm_console.c revision 221905
1/*-
2 * Copyright (c) 2011 NetApp, Inc.
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 NETAPP, INC ``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 NETAPP, INC 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 * $FreeBSD$
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include "opt_comconsole.h"
33
34#include <sys/param.h>
35#include <sys/kernel.h>
36#include <sys/systm.h>
37#include <sys/types.h>
38#include <sys/cons.h>
39#include <sys/tty.h>
40#include <sys/reboot.h>
41#include <sys/bus.h>
42
43#include <sys/kdb.h>
44#include <ddb/ddb.h>
45
46#ifndef	BVMCONS_POLL_HZ
47#define	BVMCONS_POLL_HZ	4
48#endif
49#define BVMBURSTLEN	16	/* max number of bytes to write in one chunk */
50
51static tsw_open_t bvm_tty_open;
52static tsw_close_t bvm_tty_close;
53static tsw_outwakeup_t bvm_tty_outwakeup;
54
55static struct ttydevsw bvm_ttydevsw = {
56	.tsw_flags	= TF_NOPREFIX,
57	.tsw_open	= bvm_tty_open,
58	.tsw_close	= bvm_tty_close,
59	.tsw_outwakeup	= bvm_tty_outwakeup,
60};
61
62static int			polltime;
63static struct callout_handle	bvm_timeouthandle
64    = CALLOUT_HANDLE_INITIALIZER(&bvm_timeouthandle);
65
66#if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
67static int			alt_break_state;
68#endif
69
70#define	BVM_CONS_PORT	0x220
71static int bvm_cons_port = BVM_CONS_PORT;
72
73static void	bvm_timeout(void *);
74
75static cn_probe_t	bvm_cnprobe;
76static cn_init_t	bvm_cninit;
77static cn_term_t	bvm_cnterm;
78static cn_getc_t	bvm_cngetc;
79static cn_putc_t	bvm_cnputc;
80
81CONSOLE_DRIVER(bvm);
82
83static int
84bvm_rcons(u_char *ch)
85{
86	int c;
87
88	c = inl(bvm_cons_port);
89	if (c != -1) {
90		*ch = (u_char)c;
91		return (0);
92	} else
93		return (-1);
94}
95
96static void
97bvm_wcons(u_char ch)
98{
99
100	outl(bvm_cons_port, ch);
101}
102
103static void
104cn_drvinit(void *unused)
105{
106	struct tty *tp;
107
108	if (bvm_consdev.cn_pri != CN_DEAD &&
109	    bvm_consdev.cn_name[0] != '\0') {
110		tp = tty_alloc(&bvm_ttydevsw, NULL);
111		tty_makedev(tp, NULL, "bvmcons");
112	}
113}
114
115static int
116bvm_tty_open(struct tty *tp)
117{
118	polltime = hz / BVMCONS_POLL_HZ;
119	if (polltime < 1)
120		polltime = 1;
121	bvm_timeouthandle = timeout(bvm_timeout, tp, polltime);
122
123	return (0);
124}
125
126static void
127bvm_tty_close(struct tty *tp)
128{
129
130	/* XXX Should be replaced with callout_stop(9) */
131	untimeout(bvm_timeout, tp, bvm_timeouthandle);
132}
133
134static void
135bvm_tty_outwakeup(struct tty *tp)
136{
137	int len, written;
138	u_char buf[BVMBURSTLEN];
139
140	for (;;) {
141		len = ttydisc_getc(tp, buf, sizeof(buf));
142		if (len == 0)
143			break;
144
145		written = 0;
146		while (written < len)
147			bvm_wcons(buf[written++]);
148	}
149}
150
151static void
152bvm_timeout(void *v)
153{
154	struct	tty *tp;
155	int 	c;
156
157	tp = (struct tty *)v;
158
159	tty_lock(tp);
160	while ((c = bvm_cngetc(NULL)) != -1)
161		ttydisc_rint(tp, c, 0);
162	ttydisc_rint_done(tp);
163	tty_unlock(tp);
164
165	bvm_timeouthandle = timeout(bvm_timeout, tp, polltime);
166}
167
168static void
169bvm_cnprobe(struct consdev *cp)
170{
171	int disabled, port;
172
173	disabled = 0;
174	resource_int_value("bvmconsole", 0, "disabled", &disabled);
175	if (disabled)
176		cp->cn_pri = CN_DEAD;
177	else
178		cp->cn_pri = CN_NORMAL;
179
180	if (resource_int_value("bvmconsole", 0, "port", &port) == 0)
181		bvm_cons_port = port;
182}
183
184static void
185bvm_cninit(struct consdev *cp)
186{
187	int i;
188	const char *bootmsg = "Using bvm console.\n";
189
190	if (boothowto & RB_VERBOSE) {
191		for (i = 0; i < strlen(bootmsg); i++)
192			bvm_cnputc(cp, bootmsg[i]);
193	}
194
195	strcpy(cp->cn_name, "bvmcons");
196}
197
198static void
199bvm_cnterm(struct consdev *cp)
200{
201
202}
203
204static int
205bvm_cngetc(struct consdev *cp)
206{
207	unsigned char ch;
208
209	if (bvm_rcons(&ch) == 0) {
210#if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
211		int kdb_brk;
212
213		if ((kdb_brk = kdb_alt_break(ch, &alt_break_state)) != 0) {
214			switch (kdb_brk) {
215			case KDB_REQ_DEBUGGER:
216				kdb_enter(KDB_WHY_BREAK,
217				    "Break sequence on console");
218				break;
219			case KDB_REQ_PANIC:
220				kdb_panic("Panic sequence on console");
221				break;
222			case KDB_REQ_REBOOT:
223				kdb_reboot();
224				break;
225
226			}
227		}
228#endif
229		return (ch);
230	}
231
232	return (-1);
233}
234
235static void
236bvm_cnputc(struct consdev *cp, int c)
237{
238
239	bvm_wcons(c);
240}
241
242SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
243