bt_subr.c revision 1.6 1 /* $NetBSD: bt_subr.c,v 1.6 2003/08/24 17:32:05 uwe Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)bt_subr.c 8.2 (Berkeley) 1/21/94
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: bt_subr.c,v 1.6 2003/08/24 17:32:05 uwe Exp $");
45
46 #include "opt_rcons.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/buf.h>
51 #include <sys/errno.h>
52 #include <sys/malloc.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <dev/sun/fbio.h>
57
58 #include <dev/sun/btreg.h>
59 #include <dev/sun/btvar.h>
60
61 /*
62 * Common code for dealing with Brooktree video DACs.
63 * (Contains some software-only code as well, since the colormap
64 * ioctls are shared between the cgthree and cgsix drivers.)
65 */
66
67 /*
68 * Implement an FBIOGETCMAP-like ioctl.
69 */
70 int
71 bt_getcmap(p, cm, cmsize, uspace)
72 struct fbcmap *p;
73 union bt_cmap *cm;
74 int cmsize;
75 int uspace;
76 {
77 u_int i, start, count;
78 int error = 0;
79 u_char *cp, *r, *g, *b;
80 u_char *cbuf = NULL;
81
82 start = p->index;
83 count = p->count;
84 if (start >= cmsize || count > cmsize - start)
85 return (EINVAL);
86
87 if (uspace) {
88 /* Check user buffers for appropriate access */
89 if (!uvm_useracc(p->red, count, B_WRITE) ||
90 !uvm_useracc(p->green, count, B_WRITE) ||
91 !uvm_useracc(p->blue, count, B_WRITE))
92 return (EFAULT);
93
94 /* Allocate temporary buffer for color values */
95 cbuf = malloc(3*count*sizeof(char), M_TEMP, M_WAITOK);
96 r = cbuf;
97 g = r + count;
98 b = g + count;
99 } else {
100 /* Direct access in kernel space */
101 r = p->red;
102 g = p->green;
103 b = p->blue;
104 }
105
106 /* Copy colors from BT map to fbcmap */
107 for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 3, i++) {
108 r[i] = cp[0];
109 g[i] = cp[1];
110 b[i] = cp[2];
111 }
112
113 if (uspace) {
114 error = copyout(r, p->red, count);
115 if (error)
116 goto out;
117 error = copyout(g, p->green, count);
118 if (error)
119 goto out;
120 error = copyout(b, p->blue, count);
121 if (error)
122 goto out;
123 }
124
125 out:
126 if (cbuf != NULL)
127 free(cbuf, M_TEMP);
128
129 return (error);
130 }
131
132 /*
133 * Implement the software portion of an FBIOPUTCMAP-like ioctl.
134 */
135 int
136 bt_putcmap(p, cm, cmsize, uspace)
137 struct fbcmap *p;
138 union bt_cmap *cm;
139 int cmsize;
140 int uspace;
141 {
142 u_int i, start, count;
143 int error = 0;
144 u_char *cp, *r, *g, *b;
145 u_char *cbuf = NULL;
146
147 start = p->index;
148 count = p->count;
149 if (start >= cmsize || count > cmsize - start)
150 return (EINVAL);
151
152 if (uspace) {
153 /* Check user buffers for appropriate access */
154 if (!uvm_useracc(p->red, count, B_READ) ||
155 !uvm_useracc(p->green, count, B_READ) ||
156 !uvm_useracc(p->blue, count, B_READ))
157 return (EFAULT);
158
159 /* Allocate temporary buffer for color values */
160 cbuf = malloc(3*count*sizeof(char), M_TEMP, M_WAITOK);
161 r = cbuf;
162 g = r + count;
163 b = g + count;
164 error = copyin(p->red, r, count);
165 if (error)
166 goto out;
167 error = copyin(p->green, g, count);
168 if (error)
169 goto out;
170 error = copyin(p->blue, b, count);
171 if (error)
172 goto out;
173 } else {
174 /* Direct access in kernel space */
175 r = p->red;
176 g = p->green;
177 b = p->blue;
178 }
179
180 /* Copy colors from fbcmap to BT map */
181 for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 3, i++) {
182 cp[0] = r[i];
183 cp[1] = g[i];
184 cp[2] = b[i];
185 }
186
187 out:
188 if (cbuf != NULL)
189 free(cbuf, M_TEMP);
190
191 return (error);
192 }
193
194 /*
195 * Initialize the color map to the default state:
196 *
197 * - 0 is white (PROM uses entry 0 for background)
198 * - all other entries are black (PROM uses entry 255 for foreground)
199 */
200 void
201 bt_initcmap(cm, cmsize)
202 union bt_cmap *cm;
203 int cmsize;
204 {
205 int i;
206 u_char *cp;
207
208 cp = &cm->cm_map[0][0];
209 cp[0] = cp[1] = cp[2] = 0xff;
210
211 for (i = 1, cp = &cm->cm_map[i][0]; i < cmsize; cp += 3, i++)
212 cp[0] = cp[1] = cp[2] = 0;
213
214 #ifdef RASTERCONSOLE
215 if (cmsize > 16) {
216 /*
217 * Setup an ANSI map at offset 1, for rasops;
218 * see dev/fb.c for usage (XXX - this should
219 * be replaced by more general colormap handling)
220 */
221 extern u_char rasops_cmap[];
222 bcopy(rasops_cmap, &cm->cm_map[1][0], 3*16);
223 }
224 #endif
225 }
226
227 #if notyet
228 static void
229 bt_loadcmap_packed256(fb, bt, start, ncolors)
230 struct fbdevice *fb;
231 volatile struct bt_regs *bt;
232 int start, ncolors;
233 {
234 u_int v;
235 int count, i;
236 u_char *c[3], **p;
237 struct cmap *cm = &fb->fb_cmap;
238
239 count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
240 bt = &sc->sc_fbc->fbc_dac;
241 bt->bt_addr = BT_D4M4(start);
242
243 /*
244 * Figure out where to start in the RGB arrays
245 * See btreg.h for the way RGB triplets are packed into 4-byte words.
246 */
247 c[0] = &cm->red[(4 * count) / 3)];
248 c[1] = &cm->green[(4 * count) / 3];
249 c[2] = &cm->blue[(4 * count) / 3];
250 p = &c[0];
251 i = (4 * count) % 3; /* This much of the last triplet is already in
252 the last packed word */
253 while (i--) {
254 c[1-i]++;
255 p++;
256 }
257
258
259 while (--count >= 0) {
260 u_int v = 0;
261
262 /*
263 * Retrieve four colormap entries, pack them into
264 * a 32-bit word and write to the hardware register.
265 */
266 for (i = 0; i < 4; i++) {
267 u_char *cp = *p;
268 v |= *cp++ << (8 * i);
269 *p = cp;
270 if (p++ == &c[2])
271 /* Wrap around */
272 p = &c[0];
273 }
274
275 bt->bt_cmap = v;
276 }
277 }
278 #endif
279