Home | History | Annotate | Line # | Download | only in dev
view.c revision 1.17.4.2
      1 /*	$NetBSD: view.c,v 1.17.4.2 2002/06/17 18:48:26 jdolecek 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/conf.h>
     49 #include <sys/poll.h>
     50 #include <machine/cpu.h>
     51 #include <atari/dev/grfabs_reg.h>
     52 #include <atari/dev/viewioctl.h>
     53 #include <atari/dev/viewvar.h>
     54 #include "view.h"
     55 
     56 cdev_decl(view);
     57 
     58 static void view_display __P((struct view_softc *));
     59 static void view_remove __P((struct view_softc *));
     60 static int  view_setsize __P((struct view_softc *, struct view_size *));
     61 static int  view_get_colormap __P((struct view_softc *, colormap_t *));
     62 static int  view_set_colormap __P((struct view_softc *, colormap_t *));
     63 
     64 struct view_softc views[NVIEW];
     65 static int view_inited;
     66 
     67 int view_default_x;
     68 int view_default_y;
     69 int view_default_width  = 640;
     70 int view_default_height = 400;
     71 int view_default_depth  = 1;
     72 
     73 /*
     74  *  functions for probeing.
     75  */
     76 void	viewattach __P((int));
     77 
     78 void
     79 viewattach(cnt)
     80 	int cnt;
     81 {
     82 	viewprobe();
     83 	printf("%d view%s configured\n", NVIEW, NVIEW > 1 ? "s" : "");
     84 }
     85 
     86 /* this function is called early to set up a display. */
     87 int
     88 viewprobe()
     89 {
     90     	int i;
     91 
     92 	if(view_inited)
     93 		return(1);
     94 
     95 	view_inited = 1;
     96 
     97 	for(i=0; i<NVIEW; i++) {
     98 		views[i].view = NULL;
     99 		views[i].flags = 0;
    100 	}
    101 	return(1);
    102 }
    103 
    104 
    105 /*
    106  *  Internal functions.
    107  */
    108 
    109 static void
    110 view_display (vu)
    111 	struct view_softc *vu;
    112 {
    113 	int s, i;
    114 
    115 	if (vu == NULL)
    116 		return;
    117 
    118 	s = spltty();
    119 
    120 	/*
    121 	 * mark views that share this monitor as not displaying
    122 	 */
    123 	for (i = 0; i < NVIEW; i++) {
    124 		if(views[i].flags & VUF_DISPLAY) {
    125 			if (vu->view && (vu->view == views[i].view)) {
    126 				splx(s);
    127 				return;
    128 			}
    129 			if (views[i].view) {
    130 				grf_save_view(views[i].view);
    131 				views[i].view->flags &= ~VF_DISPLAY;
    132 			}
    133 			views[i].flags &= ~VUF_DISPLAY;
    134 		}
    135 	}
    136 
    137 	vu->flags |= VUF_ADDED;
    138 	if (vu->view) {
    139 		vu->view->display.x = vu->size.x;
    140 		vu->view->display.y = vu->size.y;
    141 
    142 		grf_display_view(vu->view);
    143 		vu->view->flags |= VF_DISPLAY;
    144 
    145 		vu->size.x = vu->view->display.x;
    146 		vu->size.y = vu->view->display.y;
    147 		vu->flags |= VUF_DISPLAY;
    148 	}
    149 	splx(s);
    150 }
    151 
    152 /*
    153  * remove a view from our added list if it is marked as displaying
    154  * switch to a new display.
    155  */
    156 static void
    157 view_remove(vu)
    158 	struct view_softc *vu;
    159 {
    160 	int i;
    161 
    162 	if ((vu->flags & VUF_ADDED) == 0)
    163 		return;
    164 
    165 	vu->flags &= ~VUF_ADDED;
    166 	if (vu->flags & VUF_DISPLAY) {
    167 		for (i = 0; i < NVIEW; i++) {
    168 			if((views[i].flags & VUF_ADDED) && &views[i] != vu) {
    169 				view_display(&views[i]);
    170 				break;
    171 			}
    172 		}
    173 	}
    174 	vu->flags &= ~VUF_DISPLAY;
    175 	grf_remove_view(vu->view);
    176 }
    177 
    178 static int
    179 view_setsize(vu, vs)
    180 	struct view_softc *vu;
    181 	struct view_size *vs;
    182 {
    183 	view_t	*new, *old;
    184 	dmode_t	*dmode;
    185 	dimen_t ns;
    186 	int	co, cs;
    187 
    188 	co = 0;
    189 	cs = 0;
    190 	if (vs->x != vu->size.x || vs->y != vu->size.y)
    191 		co = 1;
    192 
    193 	if (vs->width != vu->size.width || vs->height != vu->size.height ||
    194 	    vs->depth != vu->size.depth)
    195 		cs = 1;
    196 
    197 	if (cs == 0 && co == 0)
    198 		return(0);
    199 
    200 	ns.width  = vs->width;
    201 	ns.height = vs->height;
    202 
    203 	if((dmode = grf_get_best_mode(&ns, vs->depth)) != NULL) {
    204 		/*
    205 		 * If we can't do better, leave it
    206 		 */
    207 		if(dmode == vu->view->mode)
    208 			return(0);
    209 	}
    210 	new = grf_alloc_view(dmode, &ns, vs->depth);
    211 	if (new == NULL)
    212 		return(ENOMEM);
    213 
    214 	old = vu->view;
    215 	vu->view = new;
    216 	vu->size.x = new->display.x;
    217 	vu->size.y = new->display.y;
    218 	vu->size.width = new->display.width;
    219 	vu->size.height = new->display.height;
    220 	vu->size.depth = new->bitmap->depth;
    221 
    222 	/*
    223 	 * we need a custom remove here to avoid letting
    224 	 * another view display mark as not added or displayed
    225 	 */
    226 	if (vu->flags & VUF_DISPLAY) {
    227 		vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
    228 		view_display(vu);
    229 	}
    230 	grf_free_view(old);
    231 	return(0);
    232 }
    233 
    234 static int
    235 view_get_colormap (vu, ucm)
    236 struct view_softc	*vu;
    237 colormap_t		*ucm;
    238 {
    239 	int	error;
    240 	long	*cme;
    241 	long	*uep;
    242 
    243 	if(ucm->size > MAX_CENTRIES)
    244 		return(EINVAL);
    245 
    246 	/* add one incase of zero, ick. */
    247 	cme = malloc(sizeof(ucm->entry[0])*(ucm->size+1), M_IOCTLOPS,M_WAITOK);
    248 	if (cme == NULL)
    249 		return(ENOMEM);
    250 
    251 	error      = 0;
    252 	uep        = ucm->entry;
    253 	ucm->entry = cme;	  /* set entry to out alloc. */
    254 	if(vu->view == NULL || grf_get_colormap(vu->view, ucm))
    255 		error = EINVAL;
    256 	else error = copyout(cme, uep, sizeof(ucm->entry[0]) * ucm->size);
    257 	ucm->entry = uep;	  /* set entry back to users. */
    258 	free(cme, M_IOCTLOPS);
    259 	return(error);
    260 }
    261 
    262 static int
    263 view_set_colormap(vu, ucm)
    264 struct view_softc	*vu;
    265 colormap_t		*ucm;
    266 {
    267 	colormap_t	*cm;
    268 	int		error = 0;
    269 
    270 	if(ucm->size > MAX_CENTRIES)
    271 		return(EINVAL);
    272 
    273 	cm = malloc(sizeof(ucm->entry[0])*ucm->size + sizeof(*cm), M_IOCTLOPS,
    274 								M_WAITOK);
    275 	if(cm == NULL)
    276 		return(ENOMEM);
    277 
    278 	bcopy(ucm, cm, sizeof(colormap_t));
    279 	cm->entry = (long *)&cm[1];		 /* table directly after. */
    280 	if (((error =
    281 	    copyin(ucm->entry,cm->entry,sizeof(ucm->entry[0])*ucm->size)) == 0)
    282 	    && (vu->view == NULL || grf_use_colormap(vu->view, cm)))
    283 		error = EINVAL;
    284 	free(cm, M_IOCTLOPS);
    285 	return(error);
    286 }
    287 
    288 /*
    289  *  functions made available by conf.c
    290  */
    291 
    292 /*ARGSUSED*/
    293 int
    294 viewopen(dev, flags, mode, p)
    295 dev_t		dev;
    296 int		flags;
    297 int		mode;
    298 struct proc	*p;
    299 {
    300 	dimen_t			size;
    301 	struct view_softc	*vu;
    302 
    303 	vu = &views[minor(dev)];
    304 
    305 	if(minor(dev) >= NVIEW)
    306 		return(EXDEV);
    307 	if(vu->flags & VUF_OPEN)
    308 		return(EBUSY);
    309 
    310 	vu->size.x = view_default_x;
    311 	vu->size.y = view_default_y;
    312 	size.width = vu->size.width = view_default_width;
    313 	size.height = vu->size.height = view_default_height;
    314 	vu->size.depth = view_default_depth;
    315 	vu->view = grf_alloc_view(NULL, &size, vu->size.depth);
    316 	if (vu->view == NULL)
    317 		return(ENOMEM);
    318 
    319 	vu->size.x = vu->view->display.x;
    320 	vu->size.y = vu->view->display.y;
    321 	vu->size.width = vu->view->display.width;
    322 	vu->size.height = vu->view->display.height;
    323 	vu->size.depth = vu->view->bitmap->depth;
    324        	vu->flags |= VUF_OPEN;
    325        	return(0);
    326 }
    327 
    328 /*ARGSUSED*/
    329 int
    330 viewclose (dev, flags, mode, p)
    331 	dev_t		dev;
    332 	int 		flags;
    333 	int		mode;
    334 	struct proc	*p;
    335 {
    336 	struct view_softc *vu;
    337 
    338 	vu = &views[minor(dev)];
    339 
    340 	if ((vu->flags & VUF_OPEN) == 0)
    341 		return (0); /* XXX not open? */
    342 	view_remove (vu);
    343 	grf_free_view (vu->view);
    344 	vu->flags = 0;
    345 	vu->view = NULL;
    346 	return (0);
    347 }
    348 
    349 
    350 /*ARGSUSED*/
    351 int
    352 viewioctl (dev, cmd, data, flag, p)
    353 dev_t		dev;
    354 u_long		cmd;
    355 caddr_t		data;
    356 int		flag;
    357 struct proc	*p;
    358 {
    359 	struct view_softc	*vu;
    360 	bmap_t			*bm;
    361 	int			error;
    362 
    363 	vu = &views[minor(dev)];
    364 	error = 0;
    365 
    366 	switch (cmd) {
    367 	case VIOCDISPLAY:
    368 		view_display(vu);
    369 		break;
    370 	case VIOCREMOVE:
    371 		view_remove(vu);
    372 		break;
    373 	case VIOCGSIZE:
    374 		bcopy(&vu->size, data, sizeof (struct view_size));
    375 		break;
    376 	case VIOCSSIZE:
    377 		error = view_setsize(vu, (struct view_size *)data);
    378 		break;
    379 	case VIOCGBMAP:
    380 		bm = (bmap_t *)data;
    381 		bcopy(vu->view->bitmap, bm, sizeof(bmap_t));
    382 		if (p != NOPROC) {
    383 			bm->plane      = NULL;
    384 			bm->hw_address = NULL;
    385 			bm->regs       = NULL;
    386 			bm->hw_regs    = NULL;
    387 		}
    388 		break;
    389 	case VIOCGCMAP:
    390 		error = view_get_colormap(vu, (colormap_t *)data);
    391 		break;
    392 	case VIOCSCMAP:
    393 		error = view_set_colormap(vu, (colormap_t *)data);
    394 		break;
    395 	default:
    396 		error = EINVAL;
    397 		break;
    398 	}
    399 	return(error);
    400 }
    401 
    402 /*ARGSUSED*/
    403 paddr_t
    404 viewmmap(dev, off, prot)
    405 	dev_t	dev;
    406 	off_t	off;
    407 	int	prot;
    408 {
    409 	struct view_softc	*vu;
    410 	bmap_t			*bm;
    411 	u_char			*bmd_start;
    412 	u_long			bmd_lin, bmd_vga;
    413 
    414 	vu = &views[minor(dev)];
    415 	bm = vu->view->bitmap;
    416 	bmd_start = bm->hw_address;
    417 	bmd_lin = bm->lin_base;
    418 	bmd_vga = bm->vga_base;
    419 
    420 	/*
    421 	 * control registers
    422 	 */
    423 	if (off >= 0 && off < bm->reg_size)
    424 		return(((paddr_t)bm->hw_regs + off) >> PGSHIFT);
    425 
    426 	/*
    427 	 * VGA memory
    428 	 */
    429 	if (off >= bmd_vga && off < (bmd_vga + bm->vga_mappable))
    430 		return(((paddr_t)bm->vga_address - bmd_vga + off) >> PGSHIFT);
    431 
    432 	/*
    433 	 * frame buffer
    434 	 */
    435 	if (off >= bmd_lin && off < (bmd_lin + bm->phys_mappable))
    436 		return(((paddr_t)bmd_start - bmd_lin + off) >> PGSHIFT);
    437 
    438 	return(-1);
    439 }
    440 
    441 /*ARGSUSED*/
    442 int
    443 viewpoll(dev, events, p)
    444 dev_t		dev;
    445 int		events;
    446 struct proc	*p;
    447 {
    448 	int revents = 0;
    449 
    450 	if (events & (POLLOUT | POLLWRNORM))
    451 		revents |= events & (POLLOUT | POLLWRNORM);
    452 	return (revents);
    453 }
    454 
    455 int
    456 viewkqfilter(dev_t dev, struct knote *kn)
    457 {
    458 
    459 	/* XXXLUKEM (thorpej): not supported -- why is poll? */
    460 	return (1);
    461 }
    462 
    463 view_t	*
    464 viewview(dev)
    465 dev_t	dev;
    466 {
    467 	return(views[minor(dev)].view);
    468 }
    469