xref: /freebsd/sys/dev/dcons/dcons.c (revision 9a0c3479e22feda1bdb2db4b97f9deb1b5fa6269)
1 /*-
2  * Copyright (C) 2003,2004
3  * 	Hidetoshi Shimokawa. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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:
15  *
16  *	This product includes software developed by Hidetoshi Shimokawa.
17  *
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.
21  *
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
32  * SUCH DAMAGE.
33  *
34  * $Id: dcons.c,v 1.65 2003/10/24 03:24:55 simokawa Exp $
35  * $FreeBSD$
36  */
37 
38 #include <sys/param.h>
39 
40 #if defined(__DragonFly__) || defined(_BOOT)
41 #include "dcons.h"
42 #if defined(_BOOT)
43 #include "stand.h"
44 #endif
45 #else
46 #include <dev/dcons/dcons.h>
47 #endif
48 
49 int
50 dcons_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 
77 int
78 dcons_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 
113 void
114 dcons_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 
129 static int
130 dcons_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 
159 int
160 dcons_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 
195 void
196 dcons_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