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