Rename malloc->kmalloc, free->kfree, and realloc->krealloc. Pass 1
[dragonfly.git] / sys / dev / misc / syscons / scvtb.c
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3  * 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 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.
14  *
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.
25  *
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.8 2006/09/05 00:55:38 dillon Exp $
28  */
29
30 #include "opt_syscons.h"
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34
35 #include <machine/console.h>
36 #include <machine/md_var.h>
37
38 #include <dev/video/fb/fbreg.h>
39 #include "syscons.h"
40
41 #define vtb_wrap(vtb, at, offset)                               \
42     (((at) + (offset) + (vtb)->vtb_size)%(vtb)->vtb_size)
43
44 void
45 sc_vtb_init(sc_vtb_t *vtb, int type, int cols, int rows, void *buf, int wait)
46 {
47         vtb->vtb_flags = 0;
48         vtb->vtb_type = type;
49         vtb->vtb_cols = cols;
50         vtb->vtb_rows = rows;
51         vtb->vtb_size = cols*rows;
52         vtb->vtb_buffer = NULL;
53         vtb->vtb_tail = 0;
54
55         switch (type) {
56         case VTB_MEMORY:
57         case VTB_RINGBUFFER:
58                 if ((buf == NULL) && (cols*rows != 0)) {
59                         vtb->vtb_buffer = malloc(cols*rows*sizeof(uint16_t),
60                                     M_SYSCONS,
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;
65                         }
66                 } else {
67                         vtb->vtb_buffer = buf;
68                         vtb->vtb_flags |= VTB_VALID;
69                 }
70                 break;
71         case VTB_FRAMEBUFFER:
72                 vtb->vtb_buffer = buf;
73                 vtb->vtb_flags |= VTB_VALID;
74                 break;
75         default:
76                 break;
77         }
78 }
79
80 void
81 sc_vtb_destroy(sc_vtb_t *vtb)
82 {
83         uint16_t *p;
84
85         vtb->vtb_cols = 0;
86         vtb->vtb_rows = 0;
87         vtb->vtb_size = 0;
88         vtb->vtb_tail = 0;
89
90         p = vtb->vtb_buffer;
91         vtb->vtb_buffer = NULL;
92         switch (vtb->vtb_type) {
93         case VTB_MEMORY:
94         case VTB_RINGBUFFER:
95                 if ((vtb->vtb_flags & VTB_ALLOCED) && (p != NULL))
96                         kfree(p, M_SYSCONS);
97                 break;
98         default:
99                 break;
100         }
101         vtb->vtb_flags = 0;
102         vtb->vtb_type = VTB_INVALID;
103 }
104
105 size_t
106 sc_vtb_size(int cols, int rows)
107 {
108         return (size_t)(cols*rows*sizeof(uint16_t));
109 }
110
111 int
112 sc_vtb_getc(sc_vtb_t *vtb, int at)
113 {
114         if (vtb->vtb_type == VTB_FRAMEBUFFER)
115                 return (readw(vtb->vtb_buffer + at) & 0x00ff);
116         else
117                 return (*(vtb->vtb_buffer + at) & 0x00ff);
118 }
119
120 int
121 sc_vtb_geta(sc_vtb_t *vtb, int at)
122 {
123         if (vtb->vtb_type == VTB_FRAMEBUFFER)
124                 return (readw(vtb->vtb_buffer + at) & 0xff00);
125         else
126                 return (*(vtb->vtb_buffer + at) & 0xff00);
127 }
128
129 void
130 sc_vtb_putc(sc_vtb_t *vtb, int at, int c, int a)
131 {
132         if (vtb->vtb_type == VTB_FRAMEBUFFER)
133                 writew(vtb->vtb_buffer + at, a | c);
134         else
135                 *(vtb->vtb_buffer + at) = a | c;
136 }
137
138 uint16_t *
139 sc_vtb_putchar(sc_vtb_t *vtb, uint16_t *p, int c, int a)
140 {
141         if (vtb->vtb_type == VTB_FRAMEBUFFER)
142                 writew(p, a | c);
143         else
144                 *p = a | c;
145         return (p + 1);
146 }
147
148 int
149 sc_vtb_pos(sc_vtb_t *vtb, int pos, int offset)
150 {
151         return ((pos + offset + vtb->vtb_size)%vtb->vtb_size);
152 }
153
154 void
155 sc_vtb_clear(sc_vtb_t *vtb, int c, int attr)
156 {
157         if (vtb->vtb_type == VTB_FRAMEBUFFER)
158                 fillw_io(attr | c, vtb->vtb_buffer, vtb->vtb_size);
159         else
160                 fillw(attr | c, vtb->vtb_buffer, vtb->vtb_size);
161 }
162
163 void
164 sc_vtb_copy(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int to, int count)
165 {
166         /* XXX if both are VTB_VRAMEBUFFER... */
167         if (vtb2->vtb_type == VTB_FRAMEBUFFER) {
168                 bcopy_toio(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
169                            count*sizeof(uint16_t));
170         } else if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
171                 bcopy_fromio(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
172                              count*sizeof(uint16_t));
173         } else {
174                 bcopy(vtb1->vtb_buffer + from, vtb2->vtb_buffer + to,
175                       count*sizeof(uint16_t));
176         }
177 }
178
179 void
180 sc_vtb_append(sc_vtb_t *vtb1, int from, sc_vtb_t *vtb2, int count)
181 {
182         int len;
183
184         if (vtb2->vtb_type != VTB_RINGBUFFER)
185                 return;
186
187         while (count > 0) {
188                 len = imin(count, vtb2->vtb_size - vtb2->vtb_tail);
189                 if (vtb1->vtb_type == VTB_FRAMEBUFFER) {
190                         bcopy_fromio(vtb1->vtb_buffer + from,
191                                      vtb2->vtb_buffer + vtb2->vtb_tail,
192                                      len*sizeof(uint16_t));
193                 } else {
194                         bcopy(vtb1->vtb_buffer + from,
195                               vtb2->vtb_buffer + vtb2->vtb_tail,
196                               len*sizeof(uint16_t));
197                 }
198                 from += len;
199                 count -= len;
200                 vtb2->vtb_tail = vtb_wrap(vtb2, vtb2->vtb_tail, len);
201         }
202 }
203
204 void
205 sc_vtb_seek(sc_vtb_t *vtb, int pos)
206 {
207         vtb->vtb_tail = pos%vtb->vtb_size;
208 }
209
210 void
211 sc_vtb_erase(sc_vtb_t *vtb, int at, int count, int c, int attr)
212 {
213         if (at + count > vtb->vtb_size)
214                 count = vtb->vtb_size - at;
215         if (vtb->vtb_type == VTB_FRAMEBUFFER)
216                 fillw_io(attr | c, vtb->vtb_buffer + at, count);
217         else
218                 fillw(attr | c, vtb->vtb_buffer + at, count);
219 }
220
221 void
222 sc_vtb_move(sc_vtb_t *vtb, int from, int to, int count)
223 {
224         if (from + count > vtb->vtb_size)
225                 count = vtb->vtb_size - from;
226         if (to + count > vtb->vtb_size)
227                 count = vtb->vtb_size - to;
228         if (count <= 0)
229                 return;
230         if (vtb->vtb_type == VTB_FRAMEBUFFER) {
231                 bcopy_io(vtb->vtb_buffer + from, vtb->vtb_buffer + to,
232                          count*sizeof(uint16_t)); 
233         } else {
234                 bcopy(vtb->vtb_buffer + from, vtb->vtb_buffer + to,
235                       count*sizeof(uint16_t));
236         }
237 }
238
239 void
240 sc_vtb_delete(sc_vtb_t *vtb, int at, int count, int c, int attr)
241 {
242         int len;
243
244         if (at + count > vtb->vtb_size)
245                 count = vtb->vtb_size - at;
246         len = vtb->vtb_size - at - count;
247         if (len > 0) {
248                 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
249                         bcopy_io(vtb->vtb_buffer + at + count,
250                                  vtb->vtb_buffer + at,
251                                  len*sizeof(uint16_t)); 
252                 } else {
253                         bcopy(vtb->vtb_buffer + at + count,
254                               vtb->vtb_buffer + at,
255                               len*sizeof(uint16_t)); 
256                 }
257         }
258         if (vtb->vtb_type == VTB_FRAMEBUFFER)
259                 fillw_io(attr | c, vtb->vtb_buffer + at + len,
260                          vtb->vtb_size - at - len);
261         else
262                 fillw(attr | c, vtb->vtb_buffer + at + len,
263                       vtb->vtb_size - at - len);
264 }
265
266 void
267 sc_vtb_ins(sc_vtb_t *vtb, int at, int count, int c, int attr)
268 {
269         if (at + count > vtb->vtb_size) {
270                 count = vtb->vtb_size - at;
271         } else {
272                 if (vtb->vtb_type == VTB_FRAMEBUFFER) {
273                         bcopy_io(vtb->vtb_buffer + at,
274                                  vtb->vtb_buffer + at + count,
275                                  (vtb->vtb_size - at - count)*sizeof(uint16_t));
276                 } else {
277                         bcopy(vtb->vtb_buffer + at,
278                               vtb->vtb_buffer + at + count,
279                               (vtb->vtb_size - at - count)*sizeof(uint16_t)); 
280                 }
281         }
282         if (vtb->vtb_type == VTB_FRAMEBUFFER)
283                 fillw_io(attr | c, vtb->vtb_buffer + at, count);
284         else
285                 fillw(attr | c, vtb->vtb_buffer + at, count);
286 }