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