view.c revision 1.31.2.1 1 /* $NetBSD: view.c,v 1.31.2.1 2010/04/30 14:39:11 uebayasi 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.31.2.1 2010/04/30 14:39:11 uebayasi 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/conf.h>
52 #include <machine/cpu.h>
53 #include <atari/dev/grfabs_reg.h>
54 #include <atari/dev/viewioctl.h>
55 #include <atari/dev/viewvar.h>
56 #include "view.h"
57
58 static void view_display(struct view_softc *);
59 static void view_remove(struct view_softc *);
60 static int view_setsize(struct view_softc *, struct view_size *);
61 static int view_get_colormap(struct view_softc *, colormap_t *);
62 static int view_set_colormap(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 dev_type_open(viewopen);
74 dev_type_close(viewclose);
75 dev_type_ioctl(viewioctl);
76 dev_type_mmap(viewmmap);
77
78 const struct cdevsw view_cdevsw = {
79 viewopen, viewclose, nullread, nullwrite, viewioctl,
80 nostop, notty, nopoll, viewmmap, nokqfilter,
81 };
82
83 /*
84 * functions for probeing.
85 */
86 void viewattach(int);
87
88 void
89 viewattach(int cnt)
90 {
91 viewprobe();
92 printf("%d view%s configured\n", NVIEW, NVIEW > 1 ? "s" : "");
93 }
94
95 /* this function is called early to set up a display. */
96 int
97 viewprobe(void)
98 {
99 int i;
100
101 if (view_inited)
102 return 1;
103
104 view_inited = 1;
105
106 for (i = 0; i < NVIEW; i++) {
107 views[i].view = NULL;
108 views[i].flags = 0;
109 }
110 return 1;
111 }
112
113
114 /*
115 * Internal functions.
116 */
117
118 static void
119 view_display (struct view_softc *vu)
120 {
121 int s, i;
122
123 if (vu == NULL)
124 return;
125
126 s = spltty();
127
128 /*
129 * mark views that share this monitor as not displaying
130 */
131 for (i = 0; i < NVIEW; i++) {
132 if (views[i].flags & VUF_DISPLAY) {
133 if (vu->view && (vu->view == views[i].view)) {
134 splx(s);
135 return;
136 }
137 if (views[i].view) {
138 grf_save_view(views[i].view);
139 views[i].view->flags &= ~VF_DISPLAY;
140 }
141 views[i].flags &= ~VUF_DISPLAY;
142 }
143 }
144
145 vu->flags |= VUF_ADDED;
146 if (vu->view) {
147 vu->view->display.x = vu->size.x;
148 vu->view->display.y = vu->size.y;
149
150 grf_display_view(vu->view);
151 vu->view->flags |= VF_DISPLAY;
152
153 vu->size.x = vu->view->display.x;
154 vu->size.y = vu->view->display.y;
155 vu->flags |= VUF_DISPLAY;
156 }
157 splx(s);
158 }
159
160 /*
161 * remove a view from our added list if it is marked as displaying
162 * switch to a new display.
163 */
164 static void
165 view_remove(struct view_softc *vu)
166 {
167 int i;
168
169 if ((vu->flags & VUF_ADDED) == 0)
170 return;
171
172 vu->flags &= ~VUF_ADDED;
173 if (vu->flags & VUF_DISPLAY) {
174 for (i = 0; i < NVIEW; i++) {
175 if ((views[i].flags & VUF_ADDED) && &views[i] != vu) {
176 view_display(&views[i]);
177 break;
178 }
179 }
180 }
181 vu->flags &= ~VUF_DISPLAY;
182 grf_remove_view(vu->view);
183 }
184
185 static int
186 view_setsize(struct view_softc *vu, struct view_size *vs)
187 {
188 view_t *new, *old;
189 dmode_t *dmode;
190 dimen_t ns;
191 int co, cs;
192
193 co = 0;
194 cs = 0;
195 if (vs->x != vu->size.x || vs->y != vu->size.y)
196 co = 1;
197
198 if (vs->width != vu->size.width || vs->height != vu->size.height ||
199 vs->depth != vu->size.depth)
200 cs = 1;
201
202 if (cs == 0 && co == 0)
203 return 0;
204
205 ns.width = vs->width;
206 ns.height = vs->height;
207
208 if ((dmode = grf_get_best_mode(&ns, vs->depth)) != NULL) {
209 /*
210 * If we can't do better, leave it
211 */
212 if (dmode == vu->view->mode)
213 return 0;
214 }
215 new = grf_alloc_view(dmode, &ns, vs->depth);
216 if (new == NULL)
217 return ENOMEM;
218
219 old = vu->view;
220 vu->view = new;
221 vu->size.x = new->display.x;
222 vu->size.y = new->display.y;
223 vu->size.width = new->display.width;
224 vu->size.height = new->display.height;
225 vu->size.depth = new->bitmap->depth;
226
227 /*
228 * we need a custom remove here to avoid letting
229 * another view display mark as not added or displayed
230 */
231 if (vu->flags & VUF_DISPLAY) {
232 vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
233 view_display(vu);
234 }
235 grf_free_view(old);
236 return 0;
237 }
238
239 static int
240 view_get_colormap (struct view_softc *vu, colormap_t *ucm)
241 {
242 int error;
243 long *cme;
244 long *uep;
245
246 if (ucm->size > MAX_CENTRIES)
247 return EINVAL;
248
249 /* add one incase of zero, ick. */
250 cme = malloc(sizeof(ucm->entry[0])*(ucm->size+1), M_TEMP,M_WAITOK);
251 if (cme == NULL)
252 return ENOMEM;
253
254 error = 0;
255 uep = ucm->entry;
256 ucm->entry = cme; /* set entry to out alloc. */
257 if (vu->view == NULL || grf_get_colormap(vu->view, ucm))
258 error = EINVAL;
259 else
260 error = copyout(cme, uep, sizeof(ucm->entry[0]) * ucm->size);
261 ucm->entry = uep; /* set entry back to users. */
262 free(cme, M_TEMP);
263 return error;
264 }
265
266 static int
267 view_set_colormap(struct view_softc *vu, colormap_t *ucm)
268 {
269 colormap_t *cm;
270 int error = 0;
271
272 if (ucm->size > MAX_CENTRIES)
273 return EINVAL;
274
275 cm = malloc(sizeof(ucm->entry[0])*ucm->size + sizeof(*cm),
276 M_TEMP, M_WAITOK);
277 if (cm == NULL)
278 return ENOMEM;
279
280 memcpy(cm, ucm, sizeof(colormap_t));
281 cm->entry = (long *)&cm[1]; /* table directly after. */
282 if (((error =
283 copyin(ucm->entry,cm->entry,sizeof(ucm->entry[0])*ucm->size)) == 0)
284 && (vu->view == NULL || grf_use_colormap(vu->view, cm)))
285 error = EINVAL;
286 free(cm, M_TEMP);
287 return error;
288 }
289
290 /*
291 * functions made available by conf.c
292 */
293
294 /*ARGSUSED*/
295 int
296 viewopen(dev_t dev, int flags, int mode, struct lwp *l)
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_t dev, int flags, int mode, struct lwp *l)
329 {
330 struct view_softc *vu;
331
332 vu = &views[minor(dev)];
333
334 if ((vu->flags & VUF_OPEN) == 0)
335 return 0; /* XXX not open? */
336 view_remove (vu);
337 grf_free_view (vu->view);
338 vu->flags = 0;
339 vu->view = NULL;
340 return 0;
341 }
342
343
344 /*ARGSUSED*/
345 int
346 viewioctl (dev_t dev, u_long cmd, void * data, int flag, struct lwp *l)
347 {
348 struct view_softc *vu;
349 bmap_t *bm;
350 int error;
351
352 vu = &views[minor(dev)];
353 error = 0;
354
355 switch (cmd) {
356 case VIOCDISPLAY:
357 view_display(vu);
358 break;
359 case VIOCREMOVE:
360 view_remove(vu);
361 break;
362 case VIOCGSIZE:
363 memcpy(data, &vu->size, sizeof (struct view_size));
364 break;
365 case VIOCSSIZE:
366 error = view_setsize(vu, (struct view_size *)data);
367 break;
368 case VIOCGBMAP:
369 bm = (bmap_t *)data;
370 memcpy(bm, vu->view->bitmap, sizeof(bmap_t));
371 if (l != NOLWP) {
372 bm->plane = NULL;
373 bm->hw_address = NULL;
374 bm->regs = NULL;
375 bm->hw_regs = NULL;
376 }
377 break;
378 case VIOCGCMAP:
379 error = view_get_colormap(vu, (colormap_t *)data);
380 break;
381 case VIOCSCMAP:
382 error = view_set_colormap(vu, (colormap_t *)data);
383 break;
384 default:
385 error = EPASSTHROUGH;
386 break;
387 }
388 return error;
389 }
390
391 /*ARGSUSED*/
392 paddr_t
393 viewmmap(dev_t dev, off_t off, int prot)
394 {
395 struct view_softc *vu;
396 bmap_t *bm;
397 u_char *bmd_start;
398 u_long bmd_lin, bmd_vga;
399
400 vu = &views[minor(dev)];
401 bm = vu->view->bitmap;
402 bmd_start = bm->hw_address;
403 bmd_lin = bm->lin_base;
404 bmd_vga = bm->vga_base;
405
406 /*
407 * control registers
408 */
409 if (off >= 0 && off < bm->reg_size)
410 return ((paddr_t)bm->hw_regs + off) >> PGSHIFT;
411
412 /*
413 * VGA memory
414 */
415 if (off >= bmd_vga && off < (bmd_vga + bm->vga_mappable))
416 return ((paddr_t)bm->vga_address - bmd_vga + off) >> PGSHIFT;
417
418 /*
419 * frame buffer
420 */
421 if (off >= bmd_lin && off < (bmd_lin + bm->phys_mappable))
422 return ((paddr_t)bmd_start - bmd_lin + off) >> PGSHIFT;
423
424 return -1;
425 }
426
427 view_t *
428 viewview(dev_t dev)
429 {
430
431 return views[minor(dev)].view;
432 }
433