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