2 * Copyright (C) 2003,2004
3 * Hidetoshi Shimokawa. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
16 * This product includes software developed by Hidetoshi Shimokawa.
18 * 4. Neither the name of the author nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $Id: dcons.c,v 1.65 2003/10/24 03:24:55 simokawa Exp $
35 * $FreeBSD: src/sys/dev/dcons/dcons.c,v 1.19 2004/10/13 05:38:42 simokawa Exp $
36 * $DragonFly: src/sys/dev/misc/dcons/dcons.c,v 1.3 2004/10/25 13:53:26 simokawa Exp $
39 #include <sys/param.h>
41 #if defined(__DragonFly__) || defined(_BOOT)
44 #include <dev/dcons/dcons.h>
48 dcons_ischar(struct dcons_softc *dc)
50 u_int32_t ptr, pos, gen, next_gen;
55 ptr = ntohl(*ch->ptr);
56 gen = ptr >> DCONS_GEN_SHIFT;
57 pos = ptr & DCONS_POS_MASK;
58 if (gen == ch->gen && pos == ch->pos)
61 next_gen = DCONS_NEXT_GEN(ch->gen);
62 /* XXX sanity check */
63 if ((gen != ch->gen && gen != next_gen)
64 || (gen == ch->gen && pos < ch->pos)) {
65 /* generation skipped !! */
76 dcons_checkc(struct dcons_softc *dc)
79 u_int32_t ptr, pos, gen, next_gen;
84 ptr = ntohl(*ch->ptr);
85 gen = ptr >> DCONS_GEN_SHIFT;
86 pos = ptr & DCONS_POS_MASK;
87 if (gen == ch->gen && pos == ch->pos)
90 next_gen = DCONS_NEXT_GEN(ch->gen);
91 /* XXX sanity check */
92 if ((gen != ch->gen && gen != next_gen)
93 || (gen == ch->gen && pos < ch->pos)) {
94 /* generation skipped !! */
101 c = ch->buf[ch->pos];
103 if (ch->pos >= ch->size) {
112 dcons_putc(struct dcons_softc *dc, int c)
118 ch->buf[ch->pos] = c;
120 if (ch->pos >= ch->size) {
121 ch->gen = DCONS_NEXT_GEN(ch->gen);
124 *ch->ptr = DCONS_MAKE_PTR(ch);
128 dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
129 struct dcons_softc *sc)
132 struct dcons_softc *dc;
136 osize = size * 3 / 4;
139 dc->i.size = size - osize;
140 dc->o.buf = (char *)buf + offset;
141 dc->i.buf = dc->o.buf + osize;
142 dc->o.gen = dc->i.gen = 0;
143 dc->o.pos = dc->i.pos = 0;
144 dc->o.ptr = &buf->optr[port];
145 dc->i.ptr = &buf->iptr[port];
146 dc->brk_state = STATE0;
147 buf->osize[port] = htonl(osize);
148 buf->isize[port] = htonl(size - osize);
149 buf->ooffset[port] = htonl(offset);
150 buf->ioffset[port] = htonl(offset + osize);
151 buf->optr[port] = DCONS_MAKE_PTR(&dc->o);
152 buf->iptr[port] = DCONS_MAKE_PTR(&dc->i);
158 dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
161 struct dcons_softc *dc;
163 if (buf->version != htonl(DCONS_VERSION))
166 s = DCONS_HEADER_SIZE;
167 for (port = 0; port < DCONS_NPORT; port ++) {
169 dc->o.size = ntohl(buf->osize[port]);
170 dc->i.size = ntohl(buf->isize[port]);
171 dc->o.buf = (char *)buf + ntohl(buf->ooffset[port]);
172 dc->i.buf = (char *)buf + ntohl(buf->ioffset[port]);
173 dc->o.gen = ntohl(buf->optr[port]) >> DCONS_GEN_SHIFT;
174 dc->i.gen = ntohl(buf->iptr[port]) >> DCONS_GEN_SHIFT;
175 dc->o.pos = ntohl(buf->optr[port]) & DCONS_POS_MASK;
176 dc->i.pos = ntohl(buf->iptr[port]) & DCONS_POS_MASK;
177 dc->o.ptr = &buf->optr[port];
178 dc->i.ptr = &buf->iptr[port];
179 dc->brk_state = STATE0;
181 s += dc->o.size + dc->i.size;
188 buf->magic = ntohl(DCONS_MAGIC);
194 dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
196 int size0, size1, offset;
198 offset = DCONS_HEADER_SIZE;
199 size0 = (size - offset);
200 size1 = size0 * 3 / 4; /* console port buffer */
202 dcons_init_port(0, offset, size1, buf, sc);
204 dcons_init_port(1, offset, size0 - size1, buf, sc);
205 buf->version = htonl(DCONS_VERSION);
206 buf->magic = ntohl(DCONS_MAGIC);