FreeBSD/Linux Kernel Cross Reference
sys/dev/dcons/dcons.c
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: releng/6.1/sys/dev/dcons/dcons.c 139749 2005-01-06 01:43:34Z imp $
36 */
37
38 #include <sys/param.h>
39
40 #if defined(__DragonFly__) || defined(_BOOT)
41 #include "dcons.h"
42 #else
43 #include <dev/dcons/dcons.h>
44 #endif
45
46 int
47 dcons_ischar(struct dcons_softc *dc)
48 {
49 u_int32_t ptr, pos, gen, next_gen;
50 struct dcons_ch *ch;
51
52 ch = &dc->i;
53
54 ptr = ntohl(*ch->ptr);
55 gen = ptr >> DCONS_GEN_SHIFT;
56 pos = ptr & DCONS_POS_MASK;
57 if (gen == ch->gen && pos == ch->pos)
58 return (0);
59
60 next_gen = DCONS_NEXT_GEN(ch->gen);
61 /* XXX sanity check */
62 if ((gen != ch->gen && gen != next_gen)
63 || (gen == ch->gen && pos < ch->pos)) {
64 /* generation skipped !! */
65 /* XXX discard */
66 ch->gen = gen;
67 ch->pos = pos;
68 return (0);
69 }
70
71 return (1);
72 }
73
74 int
75 dcons_checkc(struct dcons_softc *dc)
76 {
77 unsigned char c;
78 u_int32_t ptr, pos, gen, next_gen;
79 struct dcons_ch *ch;
80
81 ch = &dc->i;
82
83 ptr = ntohl(*ch->ptr);
84 gen = ptr >> DCONS_GEN_SHIFT;
85 pos = ptr & DCONS_POS_MASK;
86 if (gen == ch->gen && pos == ch->pos)
87 return (-1);
88
89 next_gen = DCONS_NEXT_GEN(ch->gen);
90 /* XXX sanity check */
91 if ((gen != ch->gen && gen != next_gen)
92 || (gen == ch->gen && pos < ch->pos)) {
93 /* generation skipped !! */
94 /* XXX discard */
95 ch->gen = gen;
96 ch->pos = pos;
97 return (-1);
98 }
99
100 c = ch->buf[ch->pos];
101 ch->pos ++;
102 if (ch->pos >= ch->size) {
103 ch->gen = next_gen;
104 ch->pos = 0;
105 }
106
107 return (c);
108 }
109
110 void
111 dcons_putc(struct dcons_softc *dc, int c)
112 {
113 struct dcons_ch *ch;
114
115 ch = &dc->o;
116
117 ch->buf[ch->pos] = c;
118 ch->pos ++;
119 if (ch->pos >= ch->size) {
120 ch->gen = DCONS_NEXT_GEN(ch->gen);
121 ch->pos = 0;
122 }
123 *ch->ptr = DCONS_MAKE_PTR(ch);
124 }
125
126 static int
127 dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
128 struct dcons_softc *sc)
129 {
130 int osize;
131 struct dcons_softc *dc;
132
133 dc = &sc[port];
134
135 osize = size * 3 / 4;
136
137 dc->o.size = osize;
138 dc->i.size = size - osize;
139 dc->o.buf = (char *)buf + offset;
140 dc->i.buf = dc->o.buf + osize;
141 dc->o.gen = dc->i.gen = 0;
142 dc->o.pos = dc->i.pos = 0;
143 dc->o.ptr = &buf->optr[port];
144 dc->i.ptr = &buf->iptr[port];
145 dc->brk_state = STATE0;
146 buf->osize[port] = htonl(osize);
147 buf->isize[port] = htonl(size - osize);
148 buf->ooffset[port] = htonl(offset);
149 buf->ioffset[port] = htonl(offset + osize);
150 buf->optr[port] = DCONS_MAKE_PTR(&dc->o);
151 buf->iptr[port] = DCONS_MAKE_PTR(&dc->i);
152
153 return(0);
154 }
155
156 int
157 dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
158 {
159 int port, s;
160 struct dcons_softc *dc;
161
162 if (buf->version != htonl(DCONS_VERSION))
163 return (-1);
164
165 s = DCONS_HEADER_SIZE;
166 for (port = 0; port < DCONS_NPORT; port ++) {
167 dc = &sc[port];
168 dc->o.size = ntohl(buf->osize[port]);
169 dc->i.size = ntohl(buf->isize[port]);
170 dc->o.buf = (char *)buf + ntohl(buf->ooffset[port]);
171 dc->i.buf = (char *)buf + ntohl(buf->ioffset[port]);
172 dc->o.gen = ntohl(buf->optr[port]) >> DCONS_GEN_SHIFT;
173 dc->i.gen = ntohl(buf->iptr[port]) >> DCONS_GEN_SHIFT;
174 dc->o.pos = ntohl(buf->optr[port]) & DCONS_POS_MASK;
175 dc->i.pos = ntohl(buf->iptr[port]) & DCONS_POS_MASK;
176 dc->o.ptr = &buf->optr[port];
177 dc->i.ptr = &buf->iptr[port];
178 dc->brk_state = STATE0;
179
180 s += dc->o.size + dc->i.size;
181 }
182
183 /* sanity check */
184 if (s > size)
185 return (-1);
186
187 buf->magic = ntohl(DCONS_MAGIC);
188
189 return (0);
190 }
191
192 void
193 dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
194 {
195 int size0, size1, offset;
196
197 offset = DCONS_HEADER_SIZE;
198 size0 = (size - offset);
199 size1 = size0 * 3 / 4; /* console port buffer */
200
201 dcons_init_port(0, offset, size1, buf, sc);
202 offset += size1;
203 dcons_init_port(1, offset, size0 - size1, buf, sc);
204 buf->version = htonl(DCONS_VERSION);
205 buf->magic = ntohl(DCONS_MAGIC);
206 }
Cache object: ab9f1415b46c64fdac99bc59a9f263c2
|