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