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