xref: /freebsd/sys/dev/dcons/dcons.c (revision 401ab69cff8fa2320a9f8ea4baa114a6da6c952b)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (C) 2003,2004
5  * 	Hidetoshi Shimokawa. 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *
18  *	This product includes software developed by Hidetoshi Shimokawa.
19  *
20  * 4. Neither the name of the author nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $Id: dcons.c,v 1.65 2003/10/24 03:24:55 simokawa Exp $
37  */
38 
39 #include <sys/param.h>
40 
41 #if defined(_BOOT)
42 #include "dcons.h"
43 #include "stand.h"
44 #else
45 #include <dev/dcons/dcons.h>
46 #endif
47 
48 int
49 dcons_ischar(struct dcons_softc *dc)
50 {
51 	u_int32_t ptr, pos, gen, next_gen;
52 	struct dcons_ch *ch;
53 
54 	ch = &dc->i;
55 
56 	ptr = ntohl(*ch->ptr);
57 	gen = ptr >> DCONS_GEN_SHIFT;
58 	pos = ptr & DCONS_POS_MASK;
59 	if (gen == ch->gen && pos == ch->pos)
60 		return (0);
61 
62 	next_gen = DCONS_NEXT_GEN(ch->gen);
63 	/* XXX sanity check */
64 	if ((gen != ch->gen && gen != next_gen)
65 			|| (gen == ch->gen && pos < ch->pos)) {
66 		/* generation skipped !! */
67 		/* XXX discard */
68 		ch->gen = gen;
69 		ch->pos = pos;
70 		return (0);
71 	}
72 
73 	return (1);
74 }
75 
76 int
77 dcons_checkc(struct dcons_softc *dc)
78 {
79 	unsigned char c;
80 	u_int32_t ptr, pos, gen, next_gen;
81 	struct dcons_ch *ch;
82 
83 	ch = &dc->i;
84 
85 	ptr = ntohl(*ch->ptr);
86 	gen = ptr >> DCONS_GEN_SHIFT;
87 	pos = ptr & DCONS_POS_MASK;
88 	if (gen == ch->gen && pos == ch->pos)
89 		return (-1);
90 
91 	next_gen = DCONS_NEXT_GEN(ch->gen);
92 	/* XXX sanity check */
93 	if ((gen != ch->gen && gen != next_gen)
94 			|| (gen == ch->gen && pos < ch->pos)) {
95 		/* generation skipped !! */
96 		/* XXX discard */
97 		ch->gen = gen;
98 		ch->pos = pos;
99 		return (-1);
100 	}
101 
102 	c = ch->buf[ch->pos];
103 	ch->pos ++;
104 	if (ch->pos >= ch->size) {
105 		ch->gen = next_gen;
106 		ch->pos = 0;
107 	}
108 
109 	return (c);
110 }
111 
112 void
113 dcons_putc(struct dcons_softc *dc, int c)
114 {
115 	struct dcons_ch *ch;
116 
117 	ch = &dc->o;
118 
119 	ch->buf[ch->pos] = c;
120 	ch->pos ++;
121 	if (ch->pos >= ch->size) {
122 		ch->gen = DCONS_NEXT_GEN(ch->gen);
123 		ch->pos = 0;
124 	}
125 	*ch->ptr = DCONS_MAKE_PTR(ch);
126 }
127 
128 static int
129 dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
130     struct dcons_softc *sc)
131 {
132 	int osize;
133 	struct dcons_softc *dc;
134 
135 	dc = &sc[port];
136 
137 	osize = size * 3 / 4;
138 
139 	dc->o.size = osize;
140 	dc->i.size = size - osize;
141 	dc->o.buf = (char *)buf + offset;
142 	dc->i.buf = dc->o.buf + osize;
143 	dc->o.gen = dc->i.gen = 0;
144 	dc->o.pos = dc->i.pos = 0;
145 	dc->o.ptr = &buf->optr[port];
146 	dc->i.ptr = &buf->iptr[port];
147 	dc->brk_state = STATE0;
148 	buf->osize[port] = htonl(osize);
149 	buf->isize[port] = htonl(size - osize);
150 	buf->ooffset[port] = htonl(offset);
151 	buf->ioffset[port] = htonl(offset + osize);
152 	buf->optr[port] = DCONS_MAKE_PTR(&dc->o);
153 	buf->iptr[port] = DCONS_MAKE_PTR(&dc->i);
154 
155 	return(0);
156 }
157 
158 int
159 dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
160 {
161 	int port, s;
162 	struct dcons_softc *dc;
163 
164 	if (buf->version != htonl(DCONS_VERSION))
165 		return (-1);
166 
167 	s = DCONS_HEADER_SIZE;
168 	for (port = 0; port < DCONS_NPORT; port ++) {
169 		dc = &sc[port];
170 		dc->o.size = ntohl(buf->osize[port]);
171 		dc->i.size = ntohl(buf->isize[port]);
172 		dc->o.buf = (char *)buf + ntohl(buf->ooffset[port]);
173 		dc->i.buf = (char *)buf + ntohl(buf->ioffset[port]);
174 		dc->o.gen = ntohl(buf->optr[port]) >> DCONS_GEN_SHIFT;
175 		dc->i.gen = ntohl(buf->iptr[port]) >> DCONS_GEN_SHIFT;
176 		dc->o.pos = ntohl(buf->optr[port]) & DCONS_POS_MASK;
177 		dc->i.pos = ntohl(buf->iptr[port]) & DCONS_POS_MASK;
178 		dc->o.ptr = &buf->optr[port];
179 		dc->i.ptr = &buf->iptr[port];
180 		dc->brk_state = STATE0;
181 
182 		s += dc->o.size + dc->i.size;
183 	}
184 
185 	/* sanity check */
186 	if (s > size)
187 		return (-1);
188 
189 	buf->magic = ntohl(DCONS_MAGIC);
190 
191 	return (0);
192 }
193 
194 void
195 dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
196 {
197 	int size0, size1, offset;
198 
199 	offset = DCONS_HEADER_SIZE;
200 	size0 = (size - offset);
201 	size1 = size0 * 3 / 4;		/* console port buffer */
202 
203 	dcons_init_port(0, offset, size1, buf, sc);
204 	offset += size1;
205 	dcons_init_port(1, offset, size0 - size1, buf, sc);
206 	buf->version = htonl(DCONS_VERSION);
207 	buf->magic = ntohl(DCONS_MAGIC);
208 }
209