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