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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_syscons.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/consio.h>
39 #include <machine/md_var.h>
40 #include <machine/bus.h>
42 #include <dev/fb/fbreg.h>
43 #include <dev/syscons/syscons.h>
45 #define vtb_wrap(vtb, at, offset) \
46 (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
49 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
55 vtb->vtb_size = cols*rows;
62 if ((buf == NULL) && (cols*rows != 0)) {
64 (vm_offset_t)malloc(cols*rows*sizeof(u_int16_t),
66 (wait) ? M_WAITOK : M_NOWAIT);
67 if (vtb->vtb_buffer != 0) {
68 bzero((void *)sc_vtb_pointer(vtb, 0),
69 cols*rows*sizeof(u_int16_t));
70 vtb->vtb_flags |= VTB_ALLOCED;
73 vtb->vtb_buffer = (vm_offset_t)buf;
75 vtb->vtb_flags |= VTB_VALID;
79 vtb->vtb_buffer = (vm_offset_t)buf;
80 vtb->vtb_flags |= VTB_VALID;
89 sc_vtb_destroy(sc_vtb_t *vtb)
100 switch (vtb->vtb_type) {
103 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != 0))
104 free((void *)p, M_DEVBUF);
110 vtb->vtb_type = VTB_INVALID;
114 sc_vtb_size(int cols, int rows)
116 return (size_t)(cols*rows*sizeof(u_int16_t));
120 sc_vtb_getc(sc_vtb_t *vtb, int at)
123 if (vtb->vtb_type == VTB_FRAMEBUFFER)
124 return (readw(sc_vtb_pointer(vtb, at)) & 0x00ff);
127 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0x00ff);
131 sc_vtb_geta(sc_vtb_t *vtb, int at)
134 if (vtb->vtb_type == VTB_FRAMEBUFFER)
135 return (readw(sc_vtb_pointer(vtb, at)) & 0xff00);
138 return (*(u_int16_t *)sc_vtb_pointer(vtb, at) & 0xff00);
142 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
145 if (vtb->vtb_type == VTB_FRAMEBUFFER)
146 writew(sc_vtb_pointer(vtb, at), a | c);
149 *(u_int16_t *)sc_vtb_pointer(vtb, at) = a | c;
153 sc_vtb_putchar(sc_vtb_t *vtb, vm_offset_t p, int c, int a)
156 if (vtb->vtb_type == VTB_FRAMEBUFFER)
160 *(u_int16_t *)p = a | c;
161 return (p + sizeof(u_int16_t));
165 sc_vtb_pointer(sc_vtb_t *vtb, int at)
167 return (vtb->vtb_buffer + sizeof(u_int16_t)*(at));
171 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
173 return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
177 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
180 if (vtb->vtb_type == VTB_FRAMEBUFFER)
181 fillw_io(attr | c, sc_vtb_pointer(vtb, 0), vtb->vtb_size);
184 fillw(attr | c, (void *)sc_vtb_pointer(vtb, 0), vtb->vtb_size);
188 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
191 /* XXX if both are VTB_VRAMEBUFFER... */
192 if (vtb2->vtb_type == VTB_FRAMEBUFFER)
193 bcopy_toio(sc_vtb_pointer(vtb1, from),
194 sc_vtb_pointer(vtb2, to),
195 count*sizeof(u_int16_t));
196 else if (vtb1->vtb_type == VTB_FRAMEBUFFER)
197 bcopy_fromio(sc_vtb_pointer(vtb1, from),
198 sc_vtb_pointer(vtb2, to),
199 count*sizeof(u_int16_t));
202 bcopy((void *)sc_vtb_pointer(vtb1, from),
203 (void *)sc_vtb_pointer(vtb2, to),
204 count*sizeof(u_int16_t));
208 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
212 if (vtb2->vtb_type != VTB_RINGBUFFER)
216 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
218 if (vtb1->vtb_type == VTB_FRAMEBUFFER)
219 bcopy_fromio(sc_vtb_pointer(vtb1, from),
220 sc_vtb_pointer(vtb2, vtb2->vtb_tail),
221 len*sizeof(u_int16_t));
224 bcopy((void *)sc_vtb_pointer(vtb1, from),
225 (void *)sc_vtb_pointer(vtb2, vtb2->vtb_tail),
226 len*sizeof(u_int16_t));
229 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
234 sc_vtb_seek(sc_vtb_t *vtb, int pos)
236 vtb->vtb_tail = pos%vtb->vtb_size;
240 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
242 if (at + count > vtb->vtb_size)
243 count = vtb->vtb_size - at;
245 if (vtb->vtb_type == VTB_FRAMEBUFFER)
246 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
249 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);
253 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
255 if (from + count > vtb->vtb_size)
256 count = vtb->vtb_size - from;
257 if (to + count > vtb->vtb_size)
258 count = vtb->vtb_size - to;
262 if (vtb->vtb_type == VTB_FRAMEBUFFER)
263 bcopy_io(sc_vtb_pointer(vtb, from),
264 sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
267 bcopy((void *)sc_vtb_pointer(vtb, from),
268 (void *)sc_vtb_pointer(vtb, to), count*sizeof(u_int16_t));
272 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
276 if (at + count > vtb->vtb_size)
277 count = vtb->vtb_size - at;
278 len = vtb->vtb_size - at - count;
281 if (vtb->vtb_type == VTB_FRAMEBUFFER)
282 bcopy_io(sc_vtb_pointer(vtb, at + count),
283 sc_vtb_pointer(vtb, at),
284 len*sizeof(u_int16_t));
287 bcopy((void *)sc_vtb_pointer(vtb, at + count),
288 (void *)sc_vtb_pointer(vtb, at),
289 len*sizeof(u_int16_t));
292 if (vtb->vtb_type == VTB_FRAMEBUFFER)
293 fillw_io(attr | c, sc_vtb_pointer(vtb, at + len),
294 vtb->vtb_size - at - len);
297 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at + len),
298 vtb->vtb_size - at - len);
302 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
304 if (at + count > vtb->vtb_size)
305 count = vtb->vtb_size - at;
308 if (vtb->vtb_type == VTB_FRAMEBUFFER)
309 bcopy_io(sc_vtb_pointer(vtb, at),
310 sc_vtb_pointer(vtb, at + count),
311 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
314 bcopy((void *)sc_vtb_pointer(vtb, at),
315 (void *)sc_vtb_pointer(vtb, at + count),
316 (vtb->vtb_size - at - count)*sizeof(u_int16_t));
319 if (vtb->vtb_type == VTB_FRAMEBUFFER)
320 fillw_io(attr | c, sc_vtb_pointer(vtb, at), count);
323 fillw(attr | c, (void *)sc_vtb_pointer(vtb, at), count);