dcons.c revision 330897
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 * $FreeBSD: stable/11/sys/dev/dcons/dcons.c 330897 2018-03-14 03:19:51Z eadler $
38 */
39
40#include <sys/param.h>
41
42#if defined(_BOOT)
43#include "dcons.h"
44#include "stand.h"
45#else
46#include <dev/dcons/dcons.h>
47#endif
48
49int
50dcons_ischar(struct dcons_softc *dc)
51{
52	u_int32_t ptr, pos, gen, next_gen;
53	struct dcons_ch *ch;
54
55	ch = &dc->i;
56
57	ptr = ntohl(*ch->ptr);
58	gen = ptr >> DCONS_GEN_SHIFT;
59	pos = ptr & DCONS_POS_MASK;
60	if (gen == ch->gen && pos == ch->pos)
61		return (0);
62
63	next_gen = DCONS_NEXT_GEN(ch->gen);
64	/* XXX sanity check */
65	if ((gen != ch->gen && gen != next_gen)
66			|| (gen == ch->gen && pos < ch->pos)) {
67		/* generation skipped !! */
68		/* XXX discard */
69		ch->gen = gen;
70		ch->pos = pos;
71		return (0);
72	}
73
74	return (1);
75}
76
77int
78dcons_checkc(struct dcons_softc *dc)
79{
80	unsigned char c;
81	u_int32_t ptr, pos, gen, next_gen;
82	struct dcons_ch *ch;
83
84	ch = &dc->i;
85
86	ptr = ntohl(*ch->ptr);
87	gen = ptr >> DCONS_GEN_SHIFT;
88	pos = ptr & DCONS_POS_MASK;
89	if (gen == ch->gen && pos == ch->pos)
90		return (-1);
91
92	next_gen = DCONS_NEXT_GEN(ch->gen);
93	/* XXX sanity check */
94	if ((gen != ch->gen && gen != next_gen)
95			|| (gen == ch->gen && pos < ch->pos)) {
96		/* generation skipped !! */
97		/* XXX discard */
98		ch->gen = gen;
99		ch->pos = pos;
100		return (-1);
101	}
102
103	c = ch->buf[ch->pos];
104	ch->pos ++;
105	if (ch->pos >= ch->size) {
106		ch->gen = next_gen;
107		ch->pos = 0;
108	}
109
110	return (c);
111}
112
113void
114dcons_putc(struct dcons_softc *dc, int c)
115{
116	struct dcons_ch *ch;
117
118	ch = &dc->o;
119
120	ch->buf[ch->pos] = c;
121	ch->pos ++;
122	if (ch->pos >= ch->size) {
123		ch->gen = DCONS_NEXT_GEN(ch->gen);
124		ch->pos = 0;
125	}
126	*ch->ptr = DCONS_MAKE_PTR(ch);
127}
128
129static int
130dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
131    struct dcons_softc *sc)
132{
133	int osize;
134	struct dcons_softc *dc;
135
136	dc = &sc[port];
137
138	osize = size * 3 / 4;
139
140	dc->o.size = osize;
141	dc->i.size = size - osize;
142	dc->o.buf = (char *)buf + offset;
143	dc->i.buf = dc->o.buf + osize;
144	dc->o.gen = dc->i.gen = 0;
145	dc->o.pos = dc->i.pos = 0;
146	dc->o.ptr = &buf->optr[port];
147	dc->i.ptr = &buf->iptr[port];
148	dc->brk_state = STATE0;
149	buf->osize[port] = htonl(osize);
150	buf->isize[port] = htonl(size - osize);
151	buf->ooffset[port] = htonl(offset);
152	buf->ioffset[port] = htonl(offset + osize);
153	buf->optr[port] = DCONS_MAKE_PTR(&dc->o);
154	buf->iptr[port] = DCONS_MAKE_PTR(&dc->i);
155
156	return(0);
157}
158
159int
160dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
161{
162	int port, s;
163	struct dcons_softc *dc;
164
165	if (buf->version != htonl(DCONS_VERSION))
166		return (-1);
167
168	s = DCONS_HEADER_SIZE;
169	for (port = 0; port < DCONS_NPORT; port ++) {
170		dc = &sc[port];
171		dc->o.size = ntohl(buf->osize[port]);
172		dc->i.size = ntohl(buf->isize[port]);
173		dc->o.buf = (char *)buf + ntohl(buf->ooffset[port]);
174		dc->i.buf = (char *)buf + ntohl(buf->ioffset[port]);
175		dc->o.gen = ntohl(buf->optr[port]) >> DCONS_GEN_SHIFT;
176		dc->i.gen = ntohl(buf->iptr[port]) >> DCONS_GEN_SHIFT;
177		dc->o.pos = ntohl(buf->optr[port]) & DCONS_POS_MASK;
178		dc->i.pos = ntohl(buf->iptr[port]) & DCONS_POS_MASK;
179		dc->o.ptr = &buf->optr[port];
180		dc->i.ptr = &buf->iptr[port];
181		dc->brk_state = STATE0;
182
183		s += dc->o.size + dc->i.size;
184	}
185
186	/* sanity check */
187	if (s > size)
188		return (-1);
189
190	buf->magic = ntohl(DCONS_MAGIC);
191
192	return (0);
193}
194
195void
196dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
197{
198	int size0, size1, offset;
199
200	offset = DCONS_HEADER_SIZE;
201	size0 = (size - offset);
202	size1 = size0 * 3 / 4;		/* console port buffer */
203
204	dcons_init_port(0, offset, size1, buf, sc);
205	offset += size1;
206	dcons_init_port(1, offset, size0 - size1, buf, sc);
207	buf->version = htonl(DCONS_VERSION);
208	buf->magic = ntohl(DCONS_MAGIC);
209}
210