2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
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 as
10 * the first lines of this file unmodified.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/syscons/scvtb.c,v 1.5.2.1 2001/07/16 05:21:23 yokota Exp $
27 * $DragonFly: src/sys/dev/misc/syscons/scvtb.c,v 1.6 2005/05/26 16:24:33 swildner Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <machine/console.h>
36 #include <machine/md_var.h>
38 #include <dev/video/fb/fbreg.h>
41 #define vtb_wrap(vtb, at, offset) \
42 (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
45 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
51 vtb->vtb_size = cols*rows;
52 vtb->vtb_buffer = NULL;
58 if ((buf == NULL) && (cols*rows != 0)) {
59 vtb->vtb_buffer = malloc(cols*rows*sizeof(uint16_t),
61 M_ZERO | ((wait) ? M_WAITOK : M_NOWAIT));
62 if (vtb->vtb_buffer != NULL) {
63 vtb->vtb_flags |= VTB_VALID;
64 vtb->vtb_flags |= VTB_ALLOCED;
67 vtb->vtb_buffer = buf;
68 vtb->vtb_flags |= VTB_VALID;
72 vtb->vtb_buffer = buf;
73 vtb->vtb_flags |= VTB_VALID;
81 sc_vtb_destroy(sc_vtb_t *vtb)
87 vtb->vtb_buffer = NULL;
88 switch (vtb->vtb_type) {
91 if ((vtb->vtb_flags & VTB_ALLOCED) && (vtb->vtb_buffer != NULL))
92 free(vtb->vtb_buffer, M_SYSCONS);
98 vtb->vtb_type = VTB_INVALID;
102 sc_vtb_size(int cols, int rows)
104 return (size_t)(cols*rows*sizeof(uint16_t));
108 sc_vtb_getc(sc_vtb_t *vtb, int at)
110 if (vtb->vtb_type == VTB_FRAMEBUFFER)
111 return (readw(vtb->vtb_buffer + at) & 0x00ff);
113 return (*(vtb->vtb_buffer + at) & 0x00ff);
117 sc_vtb_geta(sc_vtb_t *vtb, int at)
119 if (vtb->vtb_type == VTB_FRAMEBUFFER)
120 return (readw(vtb->vtb_buffer + at) & 0xff00);
122 return (*(vtb->vtb_buffer + at) & 0xff00);
126 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
128 if (vtb->vtb_type == VTB_FRAMEBUFFER)
129 writew(vtb->vtb_buffer + at, a | c);
131 *(vtb->vtb_buffer + at) = a | c;
135 sc_vtb_putchar(sc_vtb_t *vtb, uint16_t *p, int c, int a)
137 if (vtb->vtb_type == VTB_FRAMEBUFFER)
145 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
147 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
151 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
153 if (vtb->vtb_type == VTB_FRAMEBUFFER)
154 fillw_io(attr | c, vtb->vtb_buffer, vtb->vtb_size);
156 fillw(attr | c, vtb->vtb_buffer, vtb->vtb_size);
160 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
162 /* XXX if both are VTB_VRAMEBUFFER... */
163 if (vtb2->vtb_type == VTB_FRAMEBUFFER) {
164 bcopy_toio(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
165 count*sizeof(uint16_t));
166 } else if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
167 bcopy_fromio(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
168 count*sizeof(uint16_t));
170 bcopy(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
171 count*sizeof(uint16_t));
176 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
180 if (vtb2->vtb_type != VTB_RINGBUFFER)
184 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
185 if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
186 bcopy_fromio(vtb1->vtb_buffer + from,
187 vtb2->vtb_buffer + vtb2->vtb_tail,
188 len*sizeof(uint16_t));
190 bcopy(vtb1->vtb_buffer + from,
191 vtb2->vtb_buffer + vtb2->vtb_tail,
192 len*sizeof(uint16_t));
196 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
201 sc_vtb_seek(sc_vtb_t *vtb, int pos)
203 vtb->vtb_tail = pos%vtb->vtb_size;
207 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
209 if (at + count > vtb->vtb_size)
210 count = vtb->vtb_size - at;
211 if (vtb->vtb_type == VTB_FRAMEBUFFER)
212 fillw_io(attr | c, vtb->vtb_buffer + at, count);
214 fillw(attr | c, vtb->vtb_buffer + at, count);
218 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
220 if (from + count > vtb->vtb_size)
221 count = vtb->vtb_size - from;
222 if (to + count > vtb->vtb_size)
223 count = vtb->vtb_size - to;
226 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
227 bcopy_io(vtb->vtb_buffer + from, vtb->vtb_buffer + to,
228 count*sizeof(uint16_t));
230 bcopy(vtb->vtb_buffer + from, vtb->vtb_buffer + to,
231 count*sizeof(uint16_t));
236 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
240 if (at + count > vtb->vtb_size)
241 count = vtb->vtb_size - at;
242 len = vtb->vtb_size - at - count;
244 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
245 bcopy_io(vtb->vtb_buffer + at + count,
246 vtb->vtb_buffer + at,
247 len*sizeof(uint16_t));
249 bcopy(vtb->vtb_buffer + at + count,
250 vtb->vtb_buffer + at,
251 len*sizeof(uint16_t));
254 if (vtb->vtb_type == VTB_FRAMEBUFFER)
255 fillw_io(attr | c, vtb->vtb_buffer + at + len,
256 vtb->vtb_size - at - len);
258 fillw(attr | c, vtb->vtb_buffer + at + len,
259 vtb->vtb_size - at - len);
263 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
265 if (at + count > vtb->vtb_size) {
266 count = vtb->vtb_size - at;
268 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
269 bcopy_io(vtb->vtb_buffer + at,
270 vtb->vtb_buffer + at + count,
271 (vtb->vtb_size - at - count)*sizeof(uint16_t));
273 bcopy(vtb->vtb_buffer + at,
274 vtb->vtb_buffer + at + count,
275 (vtb->vtb_size - at - count)*sizeof(uint16_t));
278 if (vtb->vtb_type == VTB_FRAMEBUFFER)
279 fillw_io(attr | c, vtb->vtb_buffer + at, count);
281 fillw(attr | c, vtb->vtb_buffer + at, count);