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