xref: /netbsd-src/sys/dev/sun/bt_subr.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: bt_subr.c,v 1.8 2003/11/13 03:09:29 chs 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.8 2003/11/13 03:09:29 chs Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/buf.h>
49 #include <sys/errno.h>
50 #include <sys/malloc.h>
51 
52 #include <uvm/uvm_extern.h>
53 
54 #include <dev/sun/fbio.h>
55 
56 #include <dev/sun/btreg.h>
57 #include <dev/sun/btvar.h>
58 
59 /*
60  * Common code for dealing with Brooktree video DACs.
61  * (Contains some software-only code as well, since the colormap
62  * ioctls are shared between the cgthree and cgsix drivers.)
63  */
64 
65 /*
66  * Implement an FBIOGETCMAP-like ioctl.
67  */
68 int
69 bt_getcmap(p, cm, cmsize, uspace)
70 	struct fbcmap *p;
71 	union bt_cmap *cm;
72 	int cmsize;
73 	int uspace;
74 {
75 	u_int i, start, count;
76 	int error = 0;
77 	u_char *cp, *r, *g, *b;
78 	u_char *cbuf = NULL;
79 
80 	start = p->index;
81 	count = p->count;
82 	if (start >= cmsize || count > cmsize - start)
83 		return (EINVAL);
84 
85 	if (uspace) {
86 		/* Allocate temporary buffer for color values */
87 		cbuf = malloc(3 * count * sizeof(char), M_TEMP, M_WAITOK);
88 		r = cbuf;
89 		g = r + count;
90 		b = g + count;
91 	} else {
92 		/* Direct access in kernel space */
93 		r = p->red;
94 		g = p->green;
95 		b = p->blue;
96 	}
97 
98 	/* Copy colors from BT map to fbcmap */
99 	for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 3, i++) {
100 		r[i] = cp[0];
101 		g[i] = cp[1];
102 		b[i] = cp[2];
103 	}
104 
105 	if (uspace) {
106 		error = copyout(r, p->red, count);
107 		if (error)
108 			goto out;
109 		error = copyout(g, p->green, count);
110 		if (error)
111 			goto out;
112 		error = copyout(b, p->blue, count);
113 		if (error)
114 			goto out;
115 	}
116 
117 out:
118 	if (cbuf != NULL)
119 		free(cbuf, M_TEMP);
120 
121 	return (error);
122 }
123 
124 /*
125  * Implement the software portion of an FBIOPUTCMAP-like ioctl.
126  */
127 int
128 bt_putcmap(p, cm, cmsize, uspace)
129 	struct fbcmap *p;
130 	union bt_cmap *cm;
131 	int cmsize;
132 	int uspace;
133 {
134 	u_int i, start, count;
135 	int error = 0;
136 	u_char *cp, *r, *g, *b;
137 	u_char *cbuf = NULL;
138 
139 	start = p->index;
140 	count = p->count;
141 	if (start >= cmsize || count > cmsize - start)
142 		return (EINVAL);
143 
144 	if (uspace) {
145 		/* Allocate temporary buffer for color values */
146 		cbuf = malloc(3 * count * sizeof(char), M_TEMP, M_WAITOK);
147 		r = cbuf;
148 		g = r + count;
149 		b = g + count;
150 		error = copyin(p->red, r, count);
151 		if (error)
152 			goto out;
153 		error = copyin(p->green, g, count);
154 		if (error)
155 			goto out;
156 		error = copyin(p->blue, b, count);
157 		if (error)
158 			goto out;
159 	} else {
160 		/* Direct access in kernel space */
161 		r = p->red;
162 		g = p->green;
163 		b = p->blue;
164 	}
165 
166 	/* Copy colors from fbcmap to BT map */
167 	for (cp = &cm->cm_map[start][0], i = 0; i < count; cp += 3, i++) {
168 		cp[0] = r[i];
169 		cp[1] = g[i];
170 		cp[2] = b[i];
171 	}
172 
173 out:
174 	if (cbuf != NULL)
175 		free(cbuf, M_TEMP);
176 
177 	return (error);
178 }
179 
180 /*
181  * Initialize the color map to the default state:
182  *
183  *	- 0 is white			(PROM uses entry 0 for background)
184  *	- all other entries are black	(PROM uses entry 255 for foreground)
185  */
186 void
187 bt_initcmap(cm, cmsize)
188 	union bt_cmap *cm;
189 	int cmsize;
190 {
191 	int i;
192 	u_char *cp;
193 
194 	cp = &cm->cm_map[0][0];
195 	cp[0] = cp[1] = cp[2] = 0xff;
196 
197 	for (i = 1, cp = &cm->cm_map[i][0]; i < cmsize; cp += 3, i++)
198 		cp[0] = cp[1] = cp[2] = 0;
199 
200 #ifdef RASTERCONSOLE
201 	if (cmsize > 16) {
202 		/*
203 		 * Setup an ANSI map at offset 1, for rasops;
204 		 * see dev/fb.c for usage (XXX - this should
205 		 * be replaced by more general colormap handling)
206 		 */
207 		extern u_char rasops_cmap[];
208 		bcopy(rasops_cmap, &cm->cm_map[1][0], 3*16);
209 	}
210 #endif
211 }
212 
213 #if notyet
214 static void
215 bt_loadcmap_packed256(fb, bt, start, ncolors)
216 	struct fbdevice	*fb;
217 	volatile struct bt_regs *bt;
218 	int start, ncolors;
219 {
220 	u_int v;
221 	int count, i;
222 	u_char *c[3], **p;
223 	struct cmap *cm = &fb->fb_cmap;
224 
225 	count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
226 	bt = &sc->sc_fbc->fbc_dac;
227 	bt->bt_addr = BT_D4M4(start);
228 
229 	/*
230 	 * Figure out where to start in the RGB arrays
231 	 * See btreg.h for the way RGB triplets are packed into 4-byte words.
232 	 */
233 	c[0] = &cm->red[(4 * count) / 3)];
234 	c[1] = &cm->green[(4 * count) / 3];
235 	c[2] = &cm->blue[(4 * count) / 3];
236 	p = &c[0];
237 	i = (4 * count) % 3;	/* This much of the last triplet is already in
238 				   the last packed word */
239 	while (i--) {
240 		c[1-i]++;
241 		p++;
242 	}
243 
244 
245 	while (--count >= 0) {
246 		u_int v = 0;
247 
248 		/*
249 		 * Retrieve four colormap entries, pack them into
250 		 * a 32-bit word and write to the hardware register.
251 		 */
252 		for (i = 0; i < 4; i++) {
253 			u_char *cp = *p;
254 			v |= *cp++ << (8 * i);
255 			*p = cp;
256 			if (p++ == &c[2])
257 				/* Wrap around */
258 				p = &c[0];
259 		}
260 
261 		bt->bt_cmap = v;
262 	}
263 }
264 #endif
265