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