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