2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (C) 2003,2004
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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:
18 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
36 * $Id: dcons.c,v 1.65 2003/10/24 03:24:55 simokawa Exp $
39 #include <sys/param.h>
45 #include <dev/dcons/dcons.h>
49 dcons_ischar(struct dcons_softc *dc)
51 u_int32_t ptr, pos, gen, next_gen;
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)
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 !! */
77 dcons_checkc(struct dcons_softc *dc)
80 u_int32_t ptr, pos, gen, next_gen;
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)
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 !! */
102 c = ch->buf[ch->pos];
104 if (ch->pos >= ch->size) {
113 dcons_putc(struct dcons_softc *dc, int c)
119 ch->buf[ch->pos] = c;
121 if (ch->pos >= ch->size) {
122 ch->gen = DCONS_NEXT_GEN(ch->gen);
125 *ch->ptr = DCONS_MAKE_PTR(ch);
129 dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
130 struct dcons_softc *sc)
133 struct dcons_softc *dc;
137 osize = size * 3 / 4;
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);
159 dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
162 struct dcons_softc *dc;
164 if (buf->version != htonl(DCONS_VERSION))
167 s = DCONS_HEADER_SIZE;
168 for (port = 0; port < DCONS_NPORT; 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;
182 s += dc->o.size + dc->i.size;
189 buf->magic = ntohl(DCONS_MAGIC);
195 dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
197 int size0, size1, offset;
199 offset = DCONS_HEADER_SIZE;
200 size0 = (size - offset);
201 size1 = size0 * 3 / 4; /* console port buffer */
203 dcons_init_port(0, offset, size1, buf, sc);
205 dcons_init_port(1, offset, size0 - size1, buf, sc);
206 buf->version = htonl(DCONS_VERSION);
207 buf->magic = ntohl(DCONS_MAGIC);