xref: /freebsd/sys/dev/cfe/cfe_console.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/kdb.h>
34 #include <sys/kernel.h>
35 #include <sys/priv.h>
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/conf.h>
39 #include <sys/cons.h>
40 #include <sys/consio.h>
41 #include <sys/tty.h>
42 
43 #include <dev/cfe/cfe_api.h>
44 #include <dev/cfe/cfe_error.h>
45 
46 #include <ddb/ddb.h>
47 
48 #ifndef	CFECONS_POLL_HZ
49 #define	CFECONS_POLL_HZ	4
50 #endif
51 #define CFEBURSTLEN	128	/* max number of bytes to write in one chunk */
52 
53 static tsw_open_t cfe_tty_open;
54 static tsw_close_t cfe_tty_close;
55 static tsw_outwakeup_t cfe_tty_outwakeup;
56 
57 static struct ttydevsw cfe_ttydevsw = {
58 	.tsw_flags	= TF_NOPREFIX,
59 	.tsw_open	= cfe_tty_open,
60 	.tsw_close	= cfe_tty_close,
61 	.tsw_outwakeup	= cfe_tty_outwakeup,
62 };
63 
64 static int			conhandle = -1;
65 /* XXX does cfe have to poll? */
66 static int			polltime;
67 static struct callout		cfe_timer;
68 
69 #if defined(KDB)
70 static int			alt_break_state;
71 #endif
72 
73 static void	cfe_timeout(void *);
74 
75 static cn_probe_t	cfe_cnprobe;
76 static cn_init_t	cfe_cninit;
77 static cn_term_t	cfe_cnterm;
78 static cn_getc_t	cfe_cngetc;
79 static cn_putc_t	cfe_cnputc;
80 static cn_grab_t	cfe_cngrab;
81 static cn_ungrab_t	cfe_cnungrab;
82 
83 CONSOLE_DRIVER(cfe);
84 
85 static void
86 cn_drvinit(void *unused)
87 {
88 	struct tty *tp;
89 
90 	if (cfe_consdev.cn_pri != CN_DEAD &&
91 	    cfe_consdev.cn_name[0] != '\0') {
92 		tp = tty_alloc(&cfe_ttydevsw, NULL);
93 		callout_init_mtx(&cfe_timer, tty_getlock(tp), 0);
94 		tty_makedev(tp, NULL, "cfecons");
95 	}
96 }
97 
98 static int
99 cfe_tty_open(struct tty *tp)
100 {
101 	polltime = hz / CFECONS_POLL_HZ;
102 	if (polltime < 1)
103 		polltime = 1;
104 	callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
105 
106 	return (0);
107 }
108 
109 static void
110 cfe_tty_close(struct tty *tp)
111 {
112 
113 	callout_stop(&cfe_timer);
114 }
115 
116 static void
117 cfe_tty_outwakeup(struct tty *tp)
118 {
119 	int len, written, rc;
120 	u_char buf[CFEBURSTLEN];
121 
122 	for (;;) {
123 		len = ttydisc_getc(tp, buf, sizeof buf);
124 		if (len == 0)
125 			break;
126 
127 		written = 0;
128 		while (written < len) {
129 			rc = cfe_write(conhandle, &buf[written], len - written);
130 			if (rc < 0)
131 				break;
132 			written += rc;
133 		}
134 	}
135 }
136 
137 static void
138 cfe_timeout(void *v)
139 {
140 	struct	tty *tp;
141 	int 	c;
142 
143 	tp = (struct tty *)v;
144 
145 	tty_lock_assert(tp, MA_OWNED);
146 	while ((c = cfe_cngetc(NULL)) != -1)
147 		ttydisc_rint(tp, c, 0);
148 	ttydisc_rint_done(tp);
149 
150 	callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
151 }
152 
153 static void
154 cfe_cnprobe(struct consdev *cp)
155 {
156 
157 	conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
158 	if (conhandle < 0) {
159 		cp->cn_pri = CN_DEAD;
160 		return;
161 	}
162 
163 	/* XXX */
164 	if (bootverbose) {
165 		char *bootmsg = "Using CFE firmware console.\n";
166 		int i;
167 
168 		for (i = 0; i < strlen(bootmsg); i++)
169 			cfe_cnputc(cp, bootmsg[i]);
170 	}
171 
172 	cp->cn_pri = CN_LOW;
173 }
174 
175 static void
176 cfe_cninit(struct consdev *cp)
177 {
178 
179 	strcpy(cp->cn_name, "cfecons");
180 }
181 
182 static void
183 cfe_cnterm(struct consdev *cp)
184 {
185 
186 }
187 
188 static void
189 cfe_cngrab(struct consdev *cp)
190 {
191 
192 }
193 
194 static void
195 cfe_cnungrab(struct consdev *cp)
196 {
197 
198 }
199 
200 static int
201 cfe_cngetc(struct consdev *cp)
202 {
203 	unsigned char ch;
204 
205 	if (cfe_read(conhandle, &ch, 1) == 1) {
206 #if defined(KDB)
207 		kdb_alt_break(ch, &alt_break_state);
208 #endif
209 		return (ch);
210 	}
211 
212 	return (-1);
213 }
214 
215 static void
216 cfe_cnputc(struct consdev *cp, int c)
217 {
218 	char cbuf;
219 
220 	if (c == '\n')
221 		cfe_cnputc(cp, '\r');
222 
223 	cbuf = c;
224 	while (cfe_write(conhandle, &cbuf, 1) == 0)
225 		continue;
226 }
227 
228 SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
229