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.2 2003/06/17 04:28:32 dillon Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/malloc.h>
36 #include <machine/console.h>
37 #include <machine/md_var.h>
39 #include <dev/fb/fbreg.h>
40 #include <dev/syscons/syscons.h>
42 #define vtb_wrap(vtb, at, offset) \
43 (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
46 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
52 vtb->vtb_size = cols*rows;
53 vtb->vtb_buffer = NULL;
59 if ((buf == NULL) && (cols*rows != 0)) {
61 (vm_offset_t)malloc(cols*rows*sizeof(u_int16_t),
63 (wait) ? M_WAITOK : M_NOWAIT);
64 if (vtb->vtb_buffer != NULL) {
65 bzero((void *)sc_vtb_pointer(vtb, 0),
66 cols*rows*sizeof(u_int16_t));
67 vtb->vtb_flags |= VTB_ALLOCED;
70 vtb->vtb_buffer = (vm_offset_t)buf;
72 vtb->vtb_flags |= VTB_VALID;
75 vtb->vtb_buffer = (vm_offset_t)buf;
76 vtb->vtb_flags |= VTB_VALID;
84 sc_vtb_destroy(sc_vtb_t *vtb)
94 vtb->vtb_buffer = NULL;
95 switch (vtb->vtb_type) {
98 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != NULL))
99 free((void *)p, M_DEVBUF);
105 vtb->vtb_type = VTB_INVALID;
109 sc_vtb_size(int cols, int rows)
111 return (size_t)(cols*rows*sizeof(u_int16_t));
115 sc_vtb_getc(sc_vtb_t *vtb, int at)
117 if (vtb->vtb_type == VTB_FRAMEBUFFER)
118 return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff);
120 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff);
124 sc_vtb_geta(sc_vtb_t *vtb, int at)
126 if (vtb->vtb_type == VTB_FRAMEBUFFER)
127 return (readw(sc_vtb_pointer(vtb, at)) & 0xff00);
129 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00);
133 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
135 if (vtb->vtb_type == VTB_FRAMEBUFFER)
136 writew(sc_vtb_pointer(vtb, at), a | c);
138 *(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c;
142 sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a)
144 if (vtb->vtb_type == VTB_FRAMEBUFFER)
147 *(u_int16_t *)p = a | c;
148 return (p + sizeof(u_int16_t));
152 sc_vtb_pointer(sc_vtb_t *vtb, int at)
154 return (vtb->vtb_buffer + sizeof(u_int16_t)*(at));
158 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
160 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
164 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
166 if (vtb->vtb_type == VTB_FRAMEBUFFER)
167 fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size);
169 fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size);
173 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
175 /* XXX if both are VTB_VRAMEBUFFER... */
176 if (vtb2->vtb_type == VTB_FRAMEBUFFER) {
177 bcopy_toio(sc_vtb_pointer(vtb1, from),
178 sc_vtb_pointer(vtb2, to),
179 count*sizeof(u_int16_t));
180 } else if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
181 bcopy_fromio(sc_vtb_pointer(vtb1, from),
182 sc_vtb_pointer(vtb2, to),
183 count*sizeof(u_int16_t));
185 bcopy((void *)sc_vtb_pointer(vtb1, from),
186 (void *)sc_vtb_pointer(vtb2, to),
187 count*sizeof(u_int16_t));
192 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
196 if (vtb2->vtb_type != VTB_RINGBUFFER)
200 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
201 if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
202 bcopy_fromio(sc_vtb_pointer(vtb1, from),
203 sc_vtb_pointer(vtb2, vtb2->vtb_tail),
204 len*sizeof(u_int16_t));
206 bcopy((void *)sc_vtb_pointer(vtb1, from),
207 (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail),
208 len*sizeof(u_int16_t));
212 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
217 sc_vtb_seek(sc_vtb_t *vtb, int pos)
219 vtb->vtb_tail = pos%vtb->vtb_size;
223 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
225 if (at + count > vtb->vtb_size)
226 count = vtb->vtb_size - at;
227 if (vtb->vtb_type == VTB_FRAMEBUFFER)
228 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
230 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
234 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
236 if (from + count > vtb->vtb_size)
237 count = vtb->vtb_size - from;
238 if (to + count > vtb->vtb_size)
239 count = vtb->vtb_size - to;
242 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
243 bcopy_io(sc_vtb_pointer(vtb, from),
244 sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
246 bcopy((void *)sc_vtb_pointer(vtb, from),
247 (void *)sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
252 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
256 if (at + count > vtb->vtb_size)
257 count = vtb->vtb_size - at;
258 len = vtb->vtb_size - at - count;
260 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
261 bcopy_io(sc_vtb_pointer(vtb, at + count),
262 sc_vtb_pointer(vtb, at),
263 len*sizeof(u_int16_t));
265 bcopy((void *)sc_vtb_pointer(vtb, at + count),
266 (void *)sc_vtb_pointer(vtb, at),
267 len*sizeof(u_int16_t));
270 if (vtb->vtb_type == VTB_FRAMEBUFFER)
271 fillw_io(attr | c, sc_vtb_pointer(vtb, at + len),
272 vtb->vtb_size - at - len);
274 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at + len),
275 vtb->vtb_size - at - len);
279 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
281 if (at + count > vtb->vtb_size) {
282 count = vtb->vtb_size - at;
284 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
285 bcopy_io(sc_vtb_pointer(vtb, at),
286 sc_vtb_pointer(vtb, at + count),
287 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
289 bcopy((void *)sc_vtb_pointer(vtb, at),
290 (void *)sc_vtb_pointer(vtb, at + count),
291 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
294 if (vtb->vtb_type == VTB_FRAMEBUFFER)
295 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
297 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);