1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2008 by Nathan Whitehorn. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/param.h>
28#include <sys/kdb.h>
29#include <sys/kernel.h>
30#include <sys/priv.h>
31#include <sys/systm.h>
32#include <sys/types.h>
33#include <sys/conf.h>
34#include <sys/cons.h>
35#include <sys/consio.h>
36#include <sys/tty.h>
37
38#include <dev/ofw/openfirm.h>
39
40#include <ddb/ddb.h>
41
42#include "mambocall.h"
43
44#define MAMBOBURSTLEN	128	/* max number of bytes to write in one chunk */
45
46#define MAMBO_CONSOLE_WRITE	0
47#define MAMBO_CONSOLE_READ	60
48
49static tsw_outwakeup_t mambotty_outwakeup;
50
51static struct ttydevsw mambo_ttydevsw = {
52	.tsw_flags	= TF_NOPREFIX,
53	.tsw_outwakeup	= mambotty_outwakeup,
54};
55
56static int			polltime;
57static struct callout		mambo_callout;
58static struct tty 		*tp = NULL;
59
60#if defined(KDB)
61static int			alt_break_state;
62#endif
63
64static void	mambo_timeout(void *);
65
66static cn_probe_t	mambo_cnprobe;
67static cn_init_t	mambo_cninit;
68static cn_term_t	mambo_cnterm;
69static cn_getc_t	mambo_cngetc;
70static cn_putc_t	mambo_cnputc;
71static cn_grab_t	mambo_cngrab;
72static cn_ungrab_t	mambo_cnungrab;
73
74CONSOLE_DRIVER(mambo);
75
76static void
77cn_drvinit(void *unused)
78{
79
80	if (mambo_consdev.cn_pri != CN_DEAD &&
81	    mambo_consdev.cn_name[0] != '\0') {
82		if (OF_finddevice("/mambo") == -1)
83			return;
84
85		tp = tty_alloc(&mambo_ttydevsw, NULL);
86		tty_init_console(tp, 0);
87		tty_makedev(tp, NULL, "%s", "mambocons");
88
89		polltime = 1;
90
91		callout_init(&mambo_callout, 1);
92		callout_reset(&mambo_callout, polltime, mambo_timeout, NULL);
93	}
94}
95
96SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
97
98static void
99mambotty_outwakeup(struct tty *tp)
100{
101	int len;
102	u_char buf[MAMBOBURSTLEN];
103
104	for (;;) {
105		len = ttydisc_getc(tp, buf, sizeof buf);
106		if (len == 0)
107			break;
108		mambocall(MAMBO_CONSOLE_WRITE, buf, (register_t)len, 1UL);
109	}
110}
111
112static void
113mambo_timeout(void *v)
114{
115	int 	c;
116
117	tty_lock(tp);
118	while ((c = mambo_cngetc(NULL)) != -1)
119		ttydisc_rint(tp, c, 0);
120	ttydisc_rint_done(tp);
121	tty_unlock(tp);
122
123	callout_reset(&mambo_callout, polltime, mambo_timeout, NULL);
124}
125
126static void
127mambo_cnprobe(struct consdev *cp)
128{
129	if (OF_finddevice("/mambo") == -1) {
130		cp->cn_pri = CN_DEAD;
131		return;
132	}
133
134	cp->cn_pri = CN_NORMAL;
135}
136
137static void
138mambo_cninit(struct consdev *cp)
139{
140
141	/* XXX: This is the alias, but that should be good enough */
142	strcpy(cp->cn_name, "mambocons");
143}
144
145static void
146mambo_cnterm(struct consdev *cp)
147{
148}
149
150static void
151mambo_cngrab(struct consdev *cp)
152{
153}
154
155static void
156mambo_cnungrab(struct consdev *cp)
157{
158}
159
160static int
161mambo_cngetc(struct consdev *cp)
162{
163	int ch;
164
165	ch = mambocall(MAMBO_CONSOLE_READ);
166
167	if (ch > 0 && ch < 0xff) {
168#if defined(KDB)
169		kdb_alt_break(ch, &alt_break_state);
170#endif
171		return (ch);
172	}
173
174	return (-1);
175}
176
177static void
178mambo_cnputc(struct consdev *cp, int c)
179{
180	char cbuf;
181
182	cbuf = c;
183	mambocall(MAMBO_CONSOLE_WRITE, &cbuf, 1UL, 1UL);
184}
185