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