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