nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / misc / 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: 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 $
37  */
38
39 #include <sys/param.h>
40
41 #if defined(__DragonFly__) || defined(_BOOT)
42 #include "dcons.h"
43 #else
44 #include <dev/dcons/dcons.h>
45 #endif
46
47 int
48 dcons_ischar(struct dcons_softc *dc)
49 {
50         u_int32_t ptr, pos, gen, next_gen;
51         struct dcons_ch *ch;
52
53         ch = &dc->i;
54
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)
59                 return (0);
60
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 !! */
66                 /* XXX discard */
67                 ch->gen = gen;
68                 ch->pos = pos;
69                 return (0);
70         }
71
72         return (1);
73 }
74
75 int
76 dcons_checkc(struct dcons_softc *dc)
77 {
78         unsigned char c;
79         u_int32_t ptr, pos, gen, next_gen;
80         struct dcons_ch *ch;
81
82         ch = &dc->i;
83
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)
88                 return (-1);
89
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 !! */
95                 /* XXX discard */
96                 ch->gen = gen;
97                 ch->pos = pos;
98                 return (-1);
99         }
100
101         c = ch->buf[ch->pos];
102         ch->pos ++;
103         if (ch->pos >= ch->size) {
104                 ch->gen = next_gen;
105                 ch->pos = 0;
106         }
107
108         return (c);
109 }
110
111 void
112 dcons_putc(struct dcons_softc *dc, int c)
113 {
114         struct dcons_ch *ch;
115
116         ch = &dc->o;
117
118         ch->buf[ch->pos] = c;
119         ch->pos ++;
120         if (ch->pos >= ch->size) {
121                 ch->gen = DCONS_NEXT_GEN(ch->gen);
122                 ch->pos = 0;
123         }
124         *ch->ptr = DCONS_MAKE_PTR(ch);
125 }
126
127 static int
128 dcons_init_port(int port, int offset, int size, struct dcons_buf *buf,
129     struct dcons_softc *sc)
130 {
131         int osize;
132         struct dcons_softc *dc;
133
134         dc = &sc[port];
135
136         osize = size * 3 / 4;
137
138         dc->o.size = osize;
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);
153
154         return(0);
155 }
156
157 int
158 dcons_load_buffer(struct dcons_buf *buf, int size, struct dcons_softc *sc)
159 {
160         int port, s;
161         struct dcons_softc *dc;
162
163         if (buf->version != htonl(DCONS_VERSION))
164                 return (-1);
165
166         s = DCONS_HEADER_SIZE;
167         for (port = 0; port < DCONS_NPORT; port ++) {
168                 dc = &sc[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;
180
181                 s += dc->o.size + dc->i.size;
182         }
183
184         /* sanity check */
185         if (s > size)
186                 return (-1);
187
188         buf->magic = ntohl(DCONS_MAGIC);
189
190         return (0);
191 }
192
193 void
194 dcons_init(struct dcons_buf *buf, int size, struct dcons_softc *sc)
195 {
196         int size0, size1, offset;
197
198         offset = DCONS_HEADER_SIZE;
199         size0 = (size - offset);
200         size1 = size0 * 3 / 4;          /* console port buffer */
201
202         dcons_init_port(0, offset, size1, buf, sc);
203         offset += size1;
204         dcons_init_port(1, offset, size0 - size1, buf, sc);
205         buf->version = htonl(DCONS_VERSION);
206         buf->magic = ntohl(DCONS_MAGIC);
207 }