xref: /freebsd/sys/dev/cfe/cfe_console.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2007 Bruce M. Simpson.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/kdb.h>
31 #include <sys/kernel.h>
32 #include <sys/priv.h>
33 #include <sys/systm.h>
34 #include <sys/types.h>
35 #include <sys/conf.h>
36 #include <sys/cons.h>
37 #include <sys/consio.h>
38 #include <sys/tty.h>
39 
40 #include <dev/cfe/cfe_api.h>
41 #include <dev/cfe/cfe_error.h>
42 
43 #include <ddb/ddb.h>
44 
45 #ifndef	CFECONS_POLL_HZ
46 #define	CFECONS_POLL_HZ	4
47 #endif
48 #define CFEBURSTLEN	128	/* max number of bytes to write in one chunk */
49 
50 static tsw_open_t cfe_tty_open;
51 static tsw_close_t cfe_tty_close;
52 static tsw_outwakeup_t cfe_tty_outwakeup;
53 
54 static struct ttydevsw cfe_ttydevsw = {
55 	.tsw_flags	= TF_NOPREFIX,
56 	.tsw_open	= cfe_tty_open,
57 	.tsw_close	= cfe_tty_close,
58 	.tsw_outwakeup	= cfe_tty_outwakeup,
59 };
60 
61 static int			conhandle = -1;
62 /* XXX does cfe have to poll? */
63 static int			polltime;
64 static struct callout		cfe_timer;
65 
66 #if defined(KDB)
67 static int			alt_break_state;
68 #endif
69 
70 static void	cfe_timeout(void *);
71 
72 static cn_probe_t	cfe_cnprobe;
73 static cn_init_t	cfe_cninit;
74 static cn_term_t	cfe_cnterm;
75 static cn_getc_t	cfe_cngetc;
76 static cn_putc_t	cfe_cnputc;
77 static cn_grab_t	cfe_cngrab;
78 static cn_ungrab_t	cfe_cnungrab;
79 
80 CONSOLE_DRIVER(cfe);
81 
82 static void
83 cn_drvinit(void *unused)
84 {
85 	struct tty *tp;
86 
87 	if (cfe_consdev.cn_pri != CN_DEAD &&
88 	    cfe_consdev.cn_name[0] != '\0') {
89 		tp = tty_alloc(&cfe_ttydevsw, NULL);
90 		callout_init_mtx(&cfe_timer, tty_getlock(tp), 0);
91 		tty_makedev(tp, NULL, "cfecons");
92 	}
93 }
94 
95 static int
96 cfe_tty_open(struct tty *tp)
97 {
98 	polltime = hz / CFECONS_POLL_HZ;
99 	if (polltime < 1)
100 		polltime = 1;
101 	callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
102 
103 	return (0);
104 }
105 
106 static void
107 cfe_tty_close(struct tty *tp)
108 {
109 
110 	callout_stop(&cfe_timer);
111 }
112 
113 static void
114 cfe_tty_outwakeup(struct tty *tp)
115 {
116 	int len, written, rc;
117 	u_char buf[CFEBURSTLEN];
118 
119 	for (;;) {
120 		len = ttydisc_getc(tp, buf, sizeof buf);
121 		if (len == 0)
122 			break;
123 
124 		written = 0;
125 		while (written < len) {
126 			rc = cfe_write(conhandle, &buf[written], len - written);
127 			if (rc < 0)
128 				break;
129 			written += rc;
130 		}
131 	}
132 }
133 
134 static void
135 cfe_timeout(void *v)
136 {
137 	struct	tty *tp;
138 	int 	c;
139 
140 	tp = (struct tty *)v;
141 
142 	tty_assert_locked(tp);
143 	while ((c = cfe_cngetc(NULL)) != -1)
144 		ttydisc_rint(tp, c, 0);
145 	ttydisc_rint_done(tp);
146 
147 	callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
148 }
149 
150 static void
151 cfe_cnprobe(struct consdev *cp)
152 {
153 
154 	conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
155 	if (conhandle < 0) {
156 		cp->cn_pri = CN_DEAD;
157 		return;
158 	}
159 
160 	/* XXX */
161 	if (bootverbose) {
162 		char *bootmsg = "Using CFE firmware console.\n";
163 		int i;
164 
165 		for (i = 0; i < strlen(bootmsg); i++)
166 			cfe_cnputc(cp, bootmsg[i]);
167 	}
168 
169 	cp->cn_pri = CN_LOW;
170 }
171 
172 static void
173 cfe_cninit(struct consdev *cp)
174 {
175 
176 	strcpy(cp->cn_name, "cfecons");
177 }
178 
179 static void
180 cfe_cnterm(struct consdev *cp)
181 {
182 
183 }
184 
185 static void
186 cfe_cngrab(struct consdev *cp)
187 {
188 
189 }
190 
191 static void
192 cfe_cnungrab(struct consdev *cp)
193 {
194 
195 }
196 
197 static int
198 cfe_cngetc(struct consdev *cp)
199 {
200 	unsigned char ch;
201 
202 	if (cfe_read(conhandle, &ch, 1) == 1) {
203 #if defined(KDB)
204 		kdb_alt_break(ch, &alt_break_state);
205 #endif
206 		return (ch);
207 	}
208 
209 	return (-1);
210 }
211 
212 static void
213 cfe_cnputc(struct consdev *cp, int c)
214 {
215 	char cbuf;
216 
217 	if (c == '\n')
218 		cfe_cnputc(cp, '\r');
219 
220 	cbuf = c;
221 	while (cfe_write(conhandle, &cbuf, 1) == 0)
222 		continue;
223 }
224 
225 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
226