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 $
38 #include <sys/param.h>
40 #if defined(__DragonFly__) || defined(_BOOT)
46 #include <dev/dcons/dcons.h>
50 dcons_ischar(struct dcons_softc *dc)
52 u_int32_t ptr, pos, gen, next_gen;
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)
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 !! */
78 dcons_checkc(struct dcons_softc *dc)
81 u_int32_t ptr, pos, gen, next_gen;
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)
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 !! */
103 c = ch->buf[ch->pos];
105 if (ch->pos >= ch->size) {
114 dcons_putc(struct dcons_softc *dc, int c)
120 ch->buf[ch->pos] = c;
122 if (ch->pos >= ch->size) {
123 ch->gen = DCONS_NEXT_GEN(ch->gen);
126 *ch->ptr = DCONS_MAKE_PTR(ch);
130 dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
131 struct dcons_softc *sc)
134 struct dcons_softc *dc;
138 osize = size * 3 / 4;
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);
160 dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
163 struct dcons_softc *dc;
165 if (buf->version != htonl(DCONS_VERSION))
168 s = DCONS_HEADER_SIZE;
169 for (port = 0; port < DCONS_NPORT; 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;
183 s += dc->o.size + dc->i.size;
190 buf->magic = ntohl(DCONS_MAGIC);
196 dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
198 int size0, size1, offset;
200 offset = DCONS_HEADER_SIZE;
201 size0 = (size - offset);
202 size1 = size0 * 3 / 4; /* console port buffer */
204 dcons_init_port(0, offset, size1, buf, sc);
206 dcons_init_port(1, offset, size0 - size1, buf, sc);
207 buf->version = htonl(DCONS_VERSION);
208 buf->magic = ntohl(DCONS_MAGIC);