view.c revision 1.18 1 /* $NetBSD: view.c,v 1.18 2002/01/26 13:41:00 aymeric 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 <machine/cpu.h>
50 #include <amiga/dev/grfabs_reg.h>
51 #include <amiga/dev/viewioctl.h>
52 #include <amiga/dev/viewvar.h>
53
54 #include <sys/conf.h>
55 #include <machine/conf.h>
56
57 #include "view.h"
58
59 static void view_display(struct view_softc *);
60 static void view_remove(struct view_softc *);
61 static int view_setsize(struct view_softc *, struct view_size *);
62
63 int view_get_colormap(struct view_softc *, colormap_t *);
64 int view_set_colormap(struct view_softc *, colormap_t *);
65
66 void viewattach(int);
67
68 struct view_softc views[NVIEW];
69 int view_inited; /* also checked in ite_cc.c */
70
71 int view_default_x;
72 int view_default_y;
73 int view_default_width = 640;
74 int view_default_height = 400;
75 int view_default_depth = 2;
76
77 /*
78 * functions for probeing.
79 */
80
81 void
82 viewattach(int cnt)
83 {
84 viewprobe();
85 printf("%d view%s configured\n", NVIEW, NVIEW > 1 ? "s" : "");
86 }
87
88 /* this function is called early to set up a display. */
89 void
90 viewprobe(void)
91 {
92 int i;
93
94 if (view_inited)
95 return;
96
97 view_inited = 1;
98
99 for (i=0; i<NVIEW; i++) {
100 views[i].view = NULL;
101 views[i].flags = 0;
102 }
103 return;
104 }
105
106
107 /*
108 * Internal functions.
109 */
110
111 static void
112 view_display(struct view_softc *vu)
113 {
114 int s, i;
115
116 if (vu == NULL)
117 return;
118
119 s = spltty ();
120
121 /*
122 * mark views that share this monitor as not displaying
123 */
124 for (i=0; i<NVIEW; i++) {
125 if ((views[i].flags & VUF_DISPLAY) &&
126 views[i].monitor == vu->monitor)
127 views[i].flags &= ~VUF_DISPLAY;
128 }
129
130 vu->flags |= VUF_ADDED;
131 if (vu->view) {
132 vu->view->display.x = vu->size.x;
133 vu->view->display.y = vu->size.y;
134
135 grf_display_view(vu->view);
136
137 vu->size.x = vu->view->display.x;
138 vu->size.y = vu->view->display.y;
139 vu->flags |= VUF_DISPLAY;
140 }
141 splx(s);
142 }
143
144 /*
145 * remove a view from our added list if it is marked as displaying
146 * switch to a new display.
147 */
148 static void
149 view_remove(struct view_softc *vu)
150 {
151 int i;
152
153 if ((vu->flags & VUF_ADDED) == 0)
154 return;
155
156 vu->flags &= ~VUF_ADDED;
157 if (vu->flags & VUF_DISPLAY) {
158 for (i = 0; i < NVIEW; i++) {
159 if ((views[i].flags & VUF_ADDED) && &views[i] != vu &&
160 views[i].monitor == vu->monitor) {
161 view_display(&views[i]);
162 break;
163 }
164 }
165 }
166 vu->flags &= ~VUF_DISPLAY;
167 grf_remove_view(vu->view);
168 }
169
170 static int
171 view_setsize(struct view_softc *vu, struct view_size *vs)
172 {
173 view_t *new, *old;
174 dimen_t ns;
175 int co, cs;
176
177 co = 0;
178 cs = 0;
179 if (vs->x != vu->size.x || vs->y != vu->size.y)
180 co = 1;
181
182 if (vs->width != vu->size.width || vs->height != vu->size.height ||
183 vs->depth != vu->size.depth)
184 cs = 1;
185
186 if (cs == 0 && co == 0)
187 return(0);
188
189 ns.width = vs->width;
190 ns.height = vs->height;
191
192 new = grf_alloc_view(NULL, &ns, vs->depth);
193 if (new == NULL)
194 return(ENOMEM);
195
196 old = vu->view;
197 vu->view = new;
198 vu->size.x = new->display.x;
199 vu->size.y = new->display.y;
200 vu->size.width = new->display.width;
201 vu->size.height = new->display.height;
202 vu->size.depth = new->bitmap->depth;
203 vu->mode = grf_get_display_mode(vu->view);
204 vu->monitor = grf_get_monitor(vu->mode);
205 vu->size.x = vs->x;
206 vu->size.y = vs->y;
207
208 /*
209 * we need a custom remove here to avoid letting
210 * another view display mark as not added or displayed
211 */
212 if (vu->flags & VUF_DISPLAY) {
213 vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
214 view_display(vu);
215 }
216 grf_free_view(old);
217 return(0);
218 }
219
220 /*
221 * functions made available by conf.c
222 */
223
224 /*ARGSUSED*/
225 int
226 viewopen(dev_t dev, int flags, int mode, struct proc *p)
227 {
228 dimen_t size;
229 struct view_softc *vu;
230
231 vu = &views[minor(dev)];
232
233 if (minor(dev) >= NVIEW)
234 return(EXDEV);
235
236 if (vu->flags & VUF_OPEN)
237 return(EBUSY);
238
239 vu->size.x = view_default_x;
240 vu->size.y = view_default_y;
241 size.width = vu->size.width = view_default_width;
242 size.height = vu->size.height = view_default_height;
243 vu->size.depth = view_default_depth;
244
245 vu->view = grf_alloc_view(NULL, &size, vu->size.depth);
246 if (vu->view == NULL)
247 return(ENOMEM);
248
249 vu->size.x = vu->view->display.x;
250 vu->size.y = vu->view->display.y;
251 vu->size.width = vu->view->display.width;
252 vu->size.height = vu->view->display.height;
253 vu->size.depth = vu->view->bitmap->depth;
254 vu->flags |= VUF_OPEN;
255 vu->mode = grf_get_display_mode(vu->view);
256 vu->monitor = grf_get_monitor(vu->mode);
257 return(0);
258 }
259
260 /*ARGSUSED*/
261 int
262 viewclose(dev_t dev, int flags, int mode, struct proc *p)
263 {
264 struct view_softc *vu;
265
266 vu = &views[minor(dev)];
267
268 if ((vu->flags & VUF_OPEN) == 0)
269 return(0);
270 view_remove (vu);
271 grf_free_view (vu->view);
272 vu->flags = 0;
273 vu->view = NULL;
274 vu->mode = NULL;
275 vu->monitor = NULL;
276 return(0);
277 }
278
279
280 /*ARGSUSED*/
281 int
282 viewioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
283 {
284 struct view_softc *vu;
285 bmap_t *bm;
286 int error;
287
288 vu = &views[minor(dev)];
289 error = 0;
290
291 switch (cmd) {
292 case VIOCDISPLAY:
293 view_display(vu);
294 break;
295 case VIOCREMOVE:
296 view_remove(vu);
297 break;
298 case VIOCGSIZE:
299 bcopy(&vu->size, data, sizeof (struct view_size));
300 break;
301 case VIOCSSIZE:
302 error = view_setsize(vu, (struct view_size *)data);
303 break;
304 case VIOCGBMAP:
305 bm = (bmap_t *)data;
306 bcopy(vu->view->bitmap, bm, sizeof(bmap_t));
307 if (flag != -1) {
308 bm->plane = 0;
309 bm->blit_temp = 0;
310 bm->hardware_address = 0;
311 }
312 break;
313 case VIOCGCMAP:
314 error = view_get_colormap(vu, (colormap_t *)data);
315 break;
316 case VIOCSCMAP:
317 error = view_set_colormap(vu, (colormap_t *)data);
318 break;
319 default:
320 error = EINVAL;
321 break;
322 }
323 return(error);
324 }
325
326 int
327 view_get_colormap(struct view_softc *vu, colormap_t *ucm)
328 {
329 int error;
330 u_long *cme;
331 u_long *uep;
332
333 /* add one incase of zero, ick. */
334 cme = malloc(sizeof (u_long)*(ucm->size + 1), M_IOCTLOPS, M_WAITOK);
335 if (cme == NULL)
336 return(ENOMEM);
337
338 uep = ucm->entry;
339 error = 0;
340 ucm->entry = cme; /* set entry to out alloc. */
341 if (vu->view == NULL || grf_get_colormap(vu->view, ucm))
342 error = EINVAL;
343 else
344 error = copyout(cme, uep, sizeof(u_long) * ucm->size);
345 ucm->entry = uep; /* set entry back to users. */
346 free(cme, M_IOCTLOPS);
347 return(error);
348 }
349
350 int
351 view_set_colormap(struct view_softc *vu, colormap_t *ucm)
352 {
353 colormap_t *cm;
354 int error;
355
356 error = 0;
357 cm = malloc(sizeof(u_long) * ucm->size + sizeof (*cm), M_IOCTLOPS,
358 M_WAITOK);
359 if (cm == NULL)
360 return(ENOMEM);
361
362 bcopy (ucm, cm, sizeof(colormap_t));
363 cm->entry = (u_long *)&cm[1]; /* table directly after. */
364 if (((error =
365 copyin(ucm->entry, cm->entry, sizeof (u_long) * ucm->size)) == 0)
366 && (vu->view == NULL || grf_use_colormap(vu->view, cm)))
367 error = EINVAL;
368 free(cm, M_IOCTLOPS);
369 return(error);
370 }
371
372 /*ARGSUSED*/
373 paddr_t
374 viewmmap(dev_t dev, off_t off, int prot)
375 {
376 struct view_softc *vu;
377 bmap_t *bm;
378 u_char *bmd_start;
379 u_long bmd_size;
380
381 vu = &views[minor(dev)];
382 bm = vu->view->bitmap;
383 bmd_start = bm->hardware_address;
384 bmd_size = bm->bytes_per_row*bm->rows*bm->depth;
385
386 if (off >= 0 && off < bmd_size)
387 return(((paddr_t)bmd_start + off) >> PGSHIFT);
388
389 return(-1);
390 }
391
392 /*ARGSUSED*/
393 int
394 viewpoll(dev_t dev, int events, struct proc *p)
395 {
396 return(events & (POLLOUT | POLLWRNORM));
397 }
398