Home | History | Annotate | Line # | Download | only in dev
view.c revision 1.17.2.1
      1 /*	$NetBSD: view.c,v 1.17.2.1 2001/10/10 11:55:51 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Christian E. Hopps
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Christian E. Hopps.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /* The view major device is a placeholder device.  It serves
     34  * simply to map the semantics of a graphics dipslay to
     35  * the semantics of a character block device.  In other
     36  * words the graphics system as currently built does not like to be
     37  * refered to by open/close/ioctl.  This device serves as
     38  * a interface to graphics. */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/proc.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/file.h>
     45 #include <sys/device.h>
     46 #include <sys/malloc.h>
     47 #include <sys/queue.h>
     48 #include <sys/poll.h>
     49 #include <sys/vnode.h>
     50 #include <machine/cpu.h>
     51 #include <amiga/dev/grfabs_reg.h>
     52 #include <amiga/dev/viewioctl.h>
     53 #include <amiga/dev/viewvar.h>
     54 
     55 #include <sys/conf.h>
     56 #include <machine/conf.h>
     57 
     58 #include "view.h"
     59 
     60 static void view_display __P((struct view_softc *));
     61 static void view_remove __P((struct view_softc *));
     62 static int view_setsize __P((struct view_softc *, struct view_size *));
     63 
     64 int view_get_colormap __P((struct view_softc *, colormap_t *));
     65 int view_set_colormap __P((struct view_softc *, colormap_t *));
     66 
     67 void viewattach __P((int));
     68 
     69 struct view_softc views[NVIEW];
     70 int view_inited;			/* also checked in ite_cc.c */
     71 
     72 int view_default_x;
     73 int view_default_y;
     74 int view_default_width = 640;
     75 int view_default_height = 400;
     76 int view_default_depth = 2;
     77 
     78 /*
     79  *  functions for probeing.
     80  */
     81 
     82 void
     83 viewattach(cnt)
     84 	int cnt;
     85 {
     86 	viewprobe();
     87 	printf("%d view%s configured\n", NVIEW, NVIEW > 1 ? "s" : "");
     88 }
     89 
     90 /* this function is called early to set up a display. */
     91 void
     92 viewprobe()
     93 {
     94     	int i;
     95 
     96 	if (view_inited)
     97 		return;
     98 
     99     	view_inited = 1;
    100 
    101     	for (i=0; i<NVIEW; i++) {
    102 		views[i].view = NULL;
    103 		views[i].flags = 0;
    104     	}
    105 	return;
    106 }
    107 
    108 
    109 /*
    110  *  Internal functions.
    111  */
    112 
    113 static void
    114 view_display (vu)
    115 	struct view_softc *vu;
    116 {
    117 	int s, i;
    118 
    119 	if (vu == NULL)
    120 		return;
    121 
    122 	s = spltty ();
    123 
    124 	/*
    125 	 * mark views that share this monitor as not displaying
    126 	 */
    127 	for (i=0; i<NVIEW; i++) {
    128 		if ((views[i].flags & VUF_DISPLAY) &&
    129 		    views[i].monitor == vu->monitor)
    130 			views[i].flags &= ~VUF_DISPLAY;
    131 	}
    132 
    133 	vu->flags |= VUF_ADDED;
    134 	if (vu->view) {
    135 		vu->view->display.x = vu->size.x;
    136 		vu->view->display.y = vu->size.y;
    137 
    138 		grf_display_view(vu->view);
    139 
    140 		vu->size.x = vu->view->display.x;
    141 		vu->size.y = vu->view->display.y;
    142 		vu->flags |= VUF_DISPLAY;
    143 	}
    144 	splx(s);
    145 }
    146 
    147 /*
    148  * remove a view from our added list if it is marked as displaying
    149  * switch to a new display.
    150  */
    151 static void
    152 view_remove(vu)
    153 	struct view_softc *vu;
    154 {
    155 	int i;
    156 
    157 	if ((vu->flags & VUF_ADDED) == 0)
    158 		return;
    159 
    160 	vu->flags &= ~VUF_ADDED;
    161 	if (vu->flags & VUF_DISPLAY) {
    162 		for (i = 0; i < NVIEW; i++) {
    163 			if ((views[i].flags & VUF_ADDED) && &views[i] != vu &&
    164 			    views[i].monitor == vu->monitor) {
    165 				view_display(&views[i]);
    166 				break;
    167 			}
    168 		}
    169 	}
    170 	vu->flags &= ~VUF_DISPLAY;
    171 	grf_remove_view(vu->view);
    172 }
    173 
    174 static int
    175 view_setsize(vu, vs)
    176 	struct view_softc *vu;
    177 	struct view_size *vs;
    178 {
    179 	view_t *new, *old;
    180 	dimen_t ns;
    181 	int co, cs;
    182 
    183 	co = 0;
    184 	cs = 0;
    185 	if (vs->x != vu->size.x || vs->y != vu->size.y)
    186 		co = 1;
    187 
    188 	if (vs->width != vu->size.width || vs->height != vu->size.height ||
    189 	    vs->depth != vu->size.depth)
    190 		cs = 1;
    191 
    192 	if (cs == 0 && co == 0)
    193 		return(0);
    194 
    195 	ns.width = vs->width;
    196 	ns.height = vs->height;
    197 
    198 	new = grf_alloc_view(NULL, &ns, vs->depth);
    199 	if (new == NULL)
    200 		return(ENOMEM);
    201 
    202 	old = vu->view;
    203 	vu->view = new;
    204 	vu->size.x = new->display.x;
    205 	vu->size.y = new->display.y;
    206 	vu->size.width = new->display.width;
    207 	vu->size.height = new->display.height;
    208 	vu->size.depth = new->bitmap->depth;
    209 	vu->mode = grf_get_display_mode(vu->view);
    210 	vu->monitor = grf_get_monitor(vu->mode);
    211 	vu->size.x = vs->x;
    212 	vu->size.y = vs->y;
    213 
    214 	/*
    215 	 * we need a custom remove here to avoid letting
    216 	 * another view display mark as not added or displayed
    217 	 */
    218 	if (vu->flags & VUF_DISPLAY) {
    219 		vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
    220 		view_display(vu);
    221 	}
    222 	grf_free_view(old);
    223 	return(0);
    224 }
    225 
    226 /*
    227  *  functions made available by conf.c
    228  */
    229 
    230 /*ARGSUSED*/
    231 int
    232 viewopen(devvp, flags, mode, p)
    233 	struct vnode *devvp;
    234 	int flags, mode;
    235 	struct proc *p;
    236 {
    237 	dimen_t size;
    238 	struct view_softc *vu;
    239 	dev_t dev;
    240 
    241 	dev = vdev_rdev(devvp);
    242 	vu = &views[minor(dev)];
    243 
    244 	if (minor(dev) >= NVIEW)
    245 		return(EXDEV);
    246 
    247 	if (vu->flags & VUF_OPEN)
    248 		return(EBUSY);
    249 
    250 	vdev_setprivdata(devvp, vu);
    251 
    252 	vu->size.x = view_default_x;
    253 	vu->size.y = view_default_y;
    254 	size.width = vu->size.width = view_default_width;
    255 	size.height = vu->size.height = view_default_height;
    256 	vu->size.depth = view_default_depth;
    257 
    258 	vu->view = grf_alloc_view(NULL, &size, vu->size.depth);
    259 	if (vu->view == NULL)
    260 		return(ENOMEM);
    261 
    262 	vu->size.x = vu->view->display.x;
    263 	vu->size.y = vu->view->display.y;
    264 	vu->size.width = vu->view->display.width;
    265 	vu->size.height = vu->view->display.height;
    266 	vu->size.depth = vu->view->bitmap->depth;
    267        	vu->flags |= VUF_OPEN;
    268     	vu->mode = grf_get_display_mode(vu->view);
    269     	vu->monitor = grf_get_monitor(vu->mode);
    270        	return(0);
    271 }
    272 
    273 /*ARGSUSED*/
    274 int
    275 viewclose(devvp, flags, mode, p)
    276 	struct vnode *devvp;
    277 	int flags, mode;
    278 	struct proc *p;
    279 {
    280 	struct view_softc *vu;
    281 
    282 	vu = vdev_privdata(devvp);
    283 
    284 	if ((vu->flags & VUF_OPEN) == 0)
    285 		return(0);
    286 	view_remove (vu);
    287 	grf_free_view (vu->view);
    288 	vu->flags = 0;
    289 	vu->view = NULL;
    290 	vu->mode = NULL;
    291 	vu->monitor = NULL;
    292        	return(0);
    293 }
    294 
    295 
    296 /*ARGSUSED*/
    297 int
    298 viewioctl(devvp, cmd, data, flag, p)
    299 	struct vnode *devvp;
    300 	u_long cmd;
    301 	caddr_t data;
    302 	int flag;
    303 	struct proc *p;
    304 {
    305 	struct view_softc *vu;
    306 	bmap_t *bm;
    307 	int error;
    308 
    309 	vu = vdev_privdata(devvp);
    310 	error = 0;
    311 
    312 	switch (cmd) {
    313 	case VIOCDISPLAY:
    314 		view_display(vu);
    315 		break;
    316 	case VIOCREMOVE:
    317 		view_remove(vu);
    318 		break;
    319 	case VIOCGSIZE:
    320 		bcopy(&vu->size, data, sizeof (struct view_size));
    321 		break;
    322 	case VIOCSSIZE:
    323 		error = view_setsize(vu, (struct view_size *)data);
    324 		break;
    325 	case VIOCGBMAP:
    326 		bm = (bmap_t *)data;
    327 		bcopy(vu->view->bitmap, bm, sizeof(bmap_t));
    328 		if (flag != -1) {
    329 			bm->plane = 0;
    330 			bm->blit_temp = 0;
    331 			bm->hardware_address = 0;
    332 		}
    333 		break;
    334 	case VIOCGCMAP:
    335 		error = view_get_colormap(vu, (colormap_t *)data);
    336 		break;
    337 	case VIOCSCMAP:
    338 		error = view_set_colormap(vu, (colormap_t *)data);
    339 		break;
    340 	default:
    341 		error = EINVAL;
    342 		break;
    343 	}
    344 	return(error);
    345 }
    346 
    347 int
    348 view_get_colormap(vu, ucm)
    349 	struct view_softc *vu;
    350 	colormap_t *ucm;
    351 {
    352 	int error;
    353 	u_long *cme;
    354 	u_long *uep;
    355 
    356 	/* add one incase of zero, ick. */
    357 	cme = malloc(sizeof (u_long)*(ucm->size + 1), M_IOCTLOPS, M_WAITOK);
    358 	if (cme == NULL)
    359 		return(ENOMEM);
    360 
    361 	uep = ucm->entry;
    362 	error = 0;
    363 	ucm->entry = cme;	  /* set entry to out alloc. */
    364 	if (vu->view == NULL || grf_get_colormap(vu->view, ucm))
    365 		error = EINVAL;
    366 	else
    367 		error = copyout(cme, uep, sizeof(u_long) * ucm->size);
    368 	ucm->entry = uep;	  /* set entry back to users. */
    369 	free(cme, M_IOCTLOPS);
    370 	return(error);
    371 }
    372 
    373 int
    374 view_set_colormap(vu, ucm)
    375 	struct view_softc *vu;
    376 	colormap_t *ucm;
    377 {
    378 	colormap_t *cm;
    379 	int error;
    380 
    381 	error = 0;
    382 	cm = malloc(sizeof(u_long) * ucm->size + sizeof (*cm), M_IOCTLOPS,
    383 	    M_WAITOK);
    384 	if (cm == NULL)
    385 		return(ENOMEM);
    386 
    387 	bcopy (ucm, cm, sizeof(colormap_t));
    388 	cm->entry = (u_long *)&cm[1];		 /* table directly after. */
    389 	if (((error =
    390 	    copyin(ucm->entry, cm->entry, sizeof (u_long) * ucm->size)) == 0)
    391 	    && (vu->view == NULL || grf_use_colormap(vu->view, cm)))
    392 		error = EINVAL;
    393 	free(cm, M_IOCTLOPS);
    394 	return(error);
    395 }
    396 
    397 /*ARGSUSED*/
    398 paddr_t
    399 viewmmap(devvp, off, prot)
    400 	struct vnode *devvp;
    401 	off_t off;
    402 	int prot;
    403 {
    404 	struct view_softc *vu;
    405 	bmap_t *bm;
    406 	u_char *bmd_start;
    407 	u_long bmd_size;
    408 
    409 	vu = vdev_privdata(devvp);
    410 	bm = vu->view->bitmap;
    411 	bmd_start = bm->hardware_address;
    412 	bmd_size = bm->bytes_per_row*bm->rows*bm->depth;
    413 
    414 	if (off >= 0 && off < bmd_size)
    415 		return(((paddr_t)bmd_start + off) >> PGSHIFT);
    416 
    417 	return(-1);
    418 }
    419 
    420 /*ARGSUSED*/
    421 int
    422 viewpoll(devvp, events, p)
    423 	struct vnode *devvp;
    424 	int events;
    425 	struct proc *p;
    426 {
    427 	return(events & (POLLOUT | POLLWRNORM));
    428 }
    429