1210677Snwhitehorn/*-
2210677Snwhitehorn * Copyright (C) 2008 by Nathan Whitehorn. All rights reserved.
3210677Snwhitehorn *
4210677Snwhitehorn * Redistribution and use in source and binary forms, with or without
5210677Snwhitehorn * modification, are permitted provided that the following conditions
6210677Snwhitehorn * are met:
7210677Snwhitehorn * 1. Redistributions of source code must retain the above copyright
8210677Snwhitehorn *    notice, this list of conditions and the following disclaimer.
9210677Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
10210677Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
11210677Snwhitehorn *    documentation and/or other materials provided with the distribution.
12210677Snwhitehorn *
13210677Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14210677Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15210677Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16210677Snwhitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17210677Snwhitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
18210677Snwhitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
19210677Snwhitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20210677Snwhitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
21210677Snwhitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22210677Snwhitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23210677Snwhitehorn */
24210677Snwhitehorn
25210677Snwhitehorn#include <sys/cdefs.h>
26210677Snwhitehorn__FBSDID("$FreeBSD$");
27210677Snwhitehorn
28210677Snwhitehorn#include <sys/param.h>
29210677Snwhitehorn#include <sys/kdb.h>
30210677Snwhitehorn#include <sys/kernel.h>
31210677Snwhitehorn#include <sys/priv.h>
32210677Snwhitehorn#include <sys/systm.h>
33210677Snwhitehorn#include <sys/types.h>
34210677Snwhitehorn#include <sys/conf.h>
35210677Snwhitehorn#include <sys/cons.h>
36210677Snwhitehorn#include <sys/consio.h>
37210677Snwhitehorn#include <sys/tty.h>
38210677Snwhitehorn
39210677Snwhitehorn#include <dev/ofw/openfirm.h>
40210677Snwhitehorn
41210677Snwhitehorn#include <ddb/ddb.h>
42210677Snwhitehorn
43210677Snwhitehorn#include "mambocall.h"
44210677Snwhitehorn
45210677Snwhitehorn#define MAMBOBURSTLEN	128	/* max number of bytes to write in one chunk */
46210677Snwhitehorn
47210677Snwhitehorn#define MAMBO_CONSOLE_WRITE	0
48210677Snwhitehorn#define MAMBO_CONSOLE_READ	60
49210677Snwhitehorn
50210677Snwhitehornstatic tsw_outwakeup_t mambotty_outwakeup;
51210677Snwhitehorn
52210677Snwhitehornstatic struct ttydevsw mambo_ttydevsw = {
53210677Snwhitehorn	.tsw_flags	= TF_NOPREFIX,
54210677Snwhitehorn	.tsw_outwakeup	= mambotty_outwakeup,
55210677Snwhitehorn};
56210677Snwhitehorn
57210677Snwhitehornstatic int			polltime;
58210677Snwhitehornstatic struct callout		mambo_callout;
59210677Snwhitehornstatic struct tty 		*tp = NULL;
60210677Snwhitehorn
61225203Srwatson#if defined(KDB)
62210677Snwhitehornstatic int			alt_break_state;
63210677Snwhitehorn#endif
64210677Snwhitehorn
65210677Snwhitehornstatic void	mambo_timeout(void *);
66210677Snwhitehorn
67210677Snwhitehornstatic cn_probe_t	mambo_cnprobe;
68210677Snwhitehornstatic cn_init_t	mambo_cninit;
69210677Snwhitehornstatic cn_term_t	mambo_cnterm;
70210677Snwhitehornstatic cn_getc_t	mambo_cngetc;
71210677Snwhitehornstatic cn_putc_t	mambo_cnputc;
72228631Savgstatic cn_grab_t	mambo_cngrab;
73228631Savgstatic cn_ungrab_t	mambo_cnungrab;
74210677Snwhitehorn
75210677SnwhitehornCONSOLE_DRIVER(mambo);
76210677Snwhitehorn
77210677Snwhitehornstatic void
78210677Snwhitehorncn_drvinit(void *unused)
79210677Snwhitehorn{
80210677Snwhitehorn
81210677Snwhitehorn	if (mambo_consdev.cn_pri != CN_DEAD &&
82210677Snwhitehorn	    mambo_consdev.cn_name[0] != '\0') {
83210677Snwhitehorn		if (OF_finddevice("/mambo") == -1)
84210677Snwhitehorn			return;
85210677Snwhitehorn
86210677Snwhitehorn		tp = tty_alloc(&mambo_ttydevsw, NULL);
87210677Snwhitehorn		tty_init_console(tp, 0);
88210677Snwhitehorn		tty_makedev(tp, NULL, "%s", "mambocons");
89210677Snwhitehorn
90210677Snwhitehorn		polltime = 1;
91210677Snwhitehorn
92283291Sjkim		callout_init(&mambo_callout, 1);
93210677Snwhitehorn		callout_reset(&mambo_callout, polltime, mambo_timeout, NULL);
94210677Snwhitehorn	}
95210677Snwhitehorn}
96210677Snwhitehorn
97210677SnwhitehornSYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
98210677Snwhitehorn
99210677Snwhitehornstatic void
100210677Snwhitehornmambotty_outwakeup(struct tty *tp)
101210677Snwhitehorn{
102210677Snwhitehorn	int len;
103210677Snwhitehorn	u_char buf[MAMBOBURSTLEN];
104210677Snwhitehorn
105210677Snwhitehorn	for (;;) {
106210677Snwhitehorn		len = ttydisc_getc(tp, buf, sizeof buf);
107210677Snwhitehorn		if (len == 0)
108210677Snwhitehorn			break;
109210677Snwhitehorn		mambocall(MAMBO_CONSOLE_WRITE, buf, (register_t)len, 1UL);
110210677Snwhitehorn	}
111210677Snwhitehorn}
112210677Snwhitehorn
113210677Snwhitehornstatic void
114210677Snwhitehornmambo_timeout(void *v)
115210677Snwhitehorn{
116210677Snwhitehorn	int 	c;
117210677Snwhitehorn
118210677Snwhitehorn	tty_lock(tp);
119210677Snwhitehorn	while ((c = mambo_cngetc(NULL)) != -1)
120210677Snwhitehorn		ttydisc_rint(tp, c, 0);
121210677Snwhitehorn	ttydisc_rint_done(tp);
122210677Snwhitehorn	tty_unlock(tp);
123210677Snwhitehorn
124210677Snwhitehorn	callout_reset(&mambo_callout, polltime, mambo_timeout, NULL);
125210677Snwhitehorn}
126210677Snwhitehorn
127210677Snwhitehornstatic void
128210677Snwhitehornmambo_cnprobe(struct consdev *cp)
129210677Snwhitehorn{
130210677Snwhitehorn	if (OF_finddevice("/mambo") == -1) {
131210677Snwhitehorn		cp->cn_pri = CN_DEAD;
132210677Snwhitehorn		return;
133210677Snwhitehorn	}
134210677Snwhitehorn
135210677Snwhitehorn	cp->cn_pri = CN_NORMAL;
136210677Snwhitehorn}
137210677Snwhitehorn
138210677Snwhitehornstatic void
139210677Snwhitehornmambo_cninit(struct consdev *cp)
140210677Snwhitehorn{
141210677Snwhitehorn
142210677Snwhitehorn	/* XXX: This is the alias, but that should be good enough */
143210677Snwhitehorn	strcpy(cp->cn_name, "mambocons");
144210677Snwhitehorn}
145210677Snwhitehorn
146210677Snwhitehornstatic void
147210677Snwhitehornmambo_cnterm(struct consdev *cp)
148210677Snwhitehorn{
149210677Snwhitehorn}
150210677Snwhitehorn
151228631Savgstatic void
152228631Savgmambo_cngrab(struct consdev *cp)
153228631Savg{
154228631Savg}
155228631Savg
156228631Savgstatic void
157228631Savgmambo_cnungrab(struct consdev *cp)
158228631Savg{
159228631Savg}
160228631Savg
161210677Snwhitehornstatic int
162210677Snwhitehornmambo_cngetc(struct consdev *cp)
163210677Snwhitehorn{
164210677Snwhitehorn	int ch;
165210677Snwhitehorn
166210677Snwhitehorn	ch = mambocall(MAMBO_CONSOLE_READ);
167210677Snwhitehorn
168210677Snwhitehorn	if (ch > 0 && ch < 0xff) {
169225203Srwatson#if defined(KDB)
170225203Srwatson		kdb_alt_break(ch, &alt_break_state);
171210677Snwhitehorn#endif
172210677Snwhitehorn		return (ch);
173210677Snwhitehorn	}
174210677Snwhitehorn
175210677Snwhitehorn	return (-1);
176210677Snwhitehorn}
177210677Snwhitehorn
178210677Snwhitehornstatic void
179210677Snwhitehornmambo_cnputc(struct consdev *cp, int c)
180210677Snwhitehorn{
181210677Snwhitehorn	char cbuf;
182210677Snwhitehorn
183210677Snwhitehorn	cbuf = c;
184210677Snwhitehorn	mambocall(MAMBO_CONSOLE_WRITE, &cbuf, 1UL, 1UL);
185210677Snwhitehorn}
186