view.c revision 1.17.4.5 1 1.17.4.5 jdolecek /* $NetBSD: view.c,v 1.17.4.5 2002/10/10 18:31:33 jdolecek Exp $ */
2 1.10 cgd
3 1.1 mw /*
4 1.2 chopps * Copyright (c) 1994 Christian E. Hopps
5 1.1 mw * All rights reserved.
6 1.1 mw *
7 1.1 mw * Redistribution and use in source and binary forms, with or without
8 1.1 mw * modification, are permitted provided that the following conditions
9 1.1 mw * are met:
10 1.1 mw * 1. Redistributions of source code must retain the above copyright
11 1.1 mw * notice, this list of conditions and the following disclaimer.
12 1.1 mw * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mw * notice, this list of conditions and the following disclaimer in the
14 1.1 mw * documentation and/or other materials provided with the distribution.
15 1.1 mw * 3. All advertising materials mentioning features or use of this software
16 1.1 mw * must display the following acknowledgement:
17 1.2 chopps * This product includes software developed by Christian E. Hopps.
18 1.2 chopps * 4. The name of the author may not be used to endorse or promote products
19 1.2 chopps * derived from this software without specific prior written permission
20 1.1 mw *
21 1.2 chopps * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.2 chopps * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.2 chopps * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.2 chopps * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.2 chopps * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.2 chopps * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.2 chopps * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.2 chopps * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.2 chopps * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.2 chopps * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 mw */
32 1.1 mw
33 1.1 mw /* The view major device is a placeholder device. It serves
34 1.17.4.2 jdolecek * simply to map the semantics of a graphics dipslay to
35 1.1 mw * the semantics of a character block device. In other
36 1.1 mw * words the graphics system as currently built does not like to be
37 1.1 mw * refered to by open/close/ioctl. This device serves as
38 1.1 mw * a interface to graphics. */
39 1.1 mw
40 1.17.4.2 jdolecek #include <sys/cdefs.h>
41 1.17.4.5 jdolecek __KERNEL_RCSID(0, "$NetBSD: view.c,v 1.17.4.5 2002/10/10 18:31:33 jdolecek Exp $");
42 1.17.4.2 jdolecek
43 1.4 chopps #include <sys/param.h>
44 1.13 veego #include <sys/systm.h>
45 1.4 chopps #include <sys/proc.h>
46 1.4 chopps #include <sys/ioctl.h>
47 1.4 chopps #include <sys/file.h>
48 1.8 chopps #include <sys/device.h>
49 1.4 chopps #include <sys/malloc.h>
50 1.6 chopps #include <sys/queue.h>
51 1.15 mhitch #include <sys/poll.h>
52 1.17.4.5 jdolecek #include <sys/conf.h>
53 1.4 chopps #include <machine/cpu.h>
54 1.4 chopps #include <amiga/dev/grfabs_reg.h>
55 1.4 chopps #include <amiga/dev/viewioctl.h>
56 1.4 chopps #include <amiga/dev/viewvar.h>
57 1.13 veego
58 1.1 mw #include "view.h"
59 1.1 mw
60 1.17.4.2 jdolecek static void view_display(struct view_softc *);
61 1.17.4.2 jdolecek static void view_remove(struct view_softc *);
62 1.17.4.2 jdolecek static int view_setsize(struct view_softc *, struct view_size *);
63 1.7 chopps
64 1.17.4.2 jdolecek int view_get_colormap(struct view_softc *, colormap_t *);
65 1.17.4.2 jdolecek int view_set_colormap(struct view_softc *, colormap_t *);
66 1.7 chopps
67 1.17.4.2 jdolecek void viewattach(int);
68 1.1 mw
69 1.1 mw struct view_softc views[NVIEW];
70 1.5 chopps int view_inited; /* also checked in ite_cc.c */
71 1.1 mw
72 1.1 mw int view_default_x;
73 1.1 mw int view_default_y;
74 1.1 mw int view_default_width = 640;
75 1.1 mw int view_default_height = 400;
76 1.1 mw int view_default_depth = 2;
77 1.17.4.5 jdolecek
78 1.17.4.5 jdolecek dev_type_open(viewopen);
79 1.17.4.5 jdolecek dev_type_close(viewclose);
80 1.17.4.5 jdolecek dev_type_ioctl(viewioctl);
81 1.17.4.5 jdolecek dev_type_poll(viewpoll);
82 1.17.4.5 jdolecek dev_type_mmap(viewmmap);
83 1.17.4.5 jdolecek dev_type_kqfilter(viewkqfilter);
84 1.17.4.5 jdolecek
85 1.17.4.5 jdolecek const struct cdevsw view_cdevsw = {
86 1.17.4.5 jdolecek viewopen, viewclose, nullread, nullwrite, viewioctl,
87 1.17.4.5 jdolecek nostop, notty, viewpoll, viewmmap, viewkqfilter,
88 1.17.4.5 jdolecek };
89 1.1 mw
90 1.17.4.2 jdolecek /*
91 1.1 mw * functions for probeing.
92 1.1 mw */
93 1.1 mw
94 1.13 veego void
95 1.17.4.2 jdolecek viewattach(int cnt)
96 1.1 mw {
97 1.8 chopps viewprobe();
98 1.16 christos printf("%d view%s configured\n", NVIEW, NVIEW > 1 ? "s" : "");
99 1.1 mw }
100 1.1 mw
101 1.8 chopps /* this function is called early to set up a display. */
102 1.13 veego void
103 1.17.4.2 jdolecek viewprobe(void)
104 1.1 mw {
105 1.1 mw int i;
106 1.17.4.2 jdolecek
107 1.7 chopps if (view_inited)
108 1.13 veego return;
109 1.1 mw
110 1.5 chopps view_inited = 1;
111 1.7 chopps
112 1.1 mw for (i=0; i<NVIEW; i++) {
113 1.7 chopps views[i].view = NULL;
114 1.7 chopps views[i].flags = 0;
115 1.1 mw }
116 1.13 veego return;
117 1.1 mw }
118 1.1 mw
119 1.1 mw
120 1.1 mw /*
121 1.1 mw * Internal functions.
122 1.1 mw */
123 1.1 mw
124 1.1 mw static void
125 1.17.4.2 jdolecek view_display(struct view_softc *vu)
126 1.1 mw {
127 1.7 chopps int s, i;
128 1.7 chopps
129 1.7 chopps if (vu == NULL)
130 1.7 chopps return;
131 1.17.4.2 jdolecek
132 1.7 chopps s = spltty ();
133 1.1 mw
134 1.7 chopps /*
135 1.17.4.2 jdolecek * mark views that share this monitor as not displaying
136 1.7 chopps */
137 1.7 chopps for (i=0; i<NVIEW; i++) {
138 1.17.4.2 jdolecek if ((views[i].flags & VUF_DISPLAY) &&
139 1.7 chopps views[i].monitor == vu->monitor)
140 1.7 chopps views[i].flags &= ~VUF_DISPLAY;
141 1.7 chopps }
142 1.1 mw
143 1.7 chopps vu->flags |= VUF_ADDED;
144 1.7 chopps if (vu->view) {
145 1.7 chopps vu->view->display.x = vu->size.x;
146 1.7 chopps vu->view->display.y = vu->size.y;
147 1.7 chopps
148 1.7 chopps grf_display_view(vu->view);
149 1.7 chopps
150 1.7 chopps vu->size.x = vu->view->display.x;
151 1.7 chopps vu->size.y = vu->view->display.y;
152 1.7 chopps vu->flags |= VUF_DISPLAY;
153 1.7 chopps }
154 1.7 chopps splx(s);
155 1.1 mw }
156 1.1 mw
157 1.17.4.2 jdolecek /*
158 1.7 chopps * remove a view from our added list if it is marked as displaying
159 1.7 chopps * switch to a new display.
160 1.7 chopps */
161 1.1 mw static void
162 1.17.4.2 jdolecek view_remove(struct view_softc *vu)
163 1.1 mw {
164 1.7 chopps int i;
165 1.1 mw
166 1.7 chopps if ((vu->flags & VUF_ADDED) == 0)
167 1.7 chopps return;
168 1.1 mw
169 1.7 chopps vu->flags &= ~VUF_ADDED;
170 1.7 chopps if (vu->flags & VUF_DISPLAY) {
171 1.7 chopps for (i = 0; i < NVIEW; i++) {
172 1.17.4.2 jdolecek if ((views[i].flags & VUF_ADDED) && &views[i] != vu &&
173 1.7 chopps views[i].monitor == vu->monitor) {
174 1.7 chopps view_display(&views[i]);
175 1.7 chopps break;
176 1.7 chopps }
177 1.7 chopps }
178 1.7 chopps }
179 1.7 chopps vu->flags &= ~VUF_DISPLAY;
180 1.7 chopps grf_remove_view(vu->view);
181 1.1 mw }
182 1.1 mw
183 1.1 mw static int
184 1.17.4.2 jdolecek view_setsize(struct view_softc *vu, struct view_size *vs)
185 1.7 chopps {
186 1.7 chopps view_t *new, *old;
187 1.7 chopps dimen_t ns;
188 1.7 chopps int co, cs;
189 1.17.4.2 jdolecek
190 1.7 chopps co = 0;
191 1.7 chopps cs = 0;
192 1.7 chopps if (vs->x != vu->size.x || vs->y != vu->size.y)
193 1.7 chopps co = 1;
194 1.7 chopps
195 1.7 chopps if (vs->width != vu->size.width || vs->height != vu->size.height ||
196 1.7 chopps vs->depth != vu->size.depth)
197 1.7 chopps cs = 1;
198 1.7 chopps
199 1.7 chopps if (cs == 0 && co == 0)
200 1.7 chopps return(0);
201 1.17.4.2 jdolecek
202 1.7 chopps ns.width = vs->width;
203 1.7 chopps ns.height = vs->height;
204 1.1 mw
205 1.7 chopps new = grf_alloc_view(NULL, &ns, vs->depth);
206 1.7 chopps if (new == NULL)
207 1.7 chopps return(ENOMEM);
208 1.17.4.2 jdolecek
209 1.7 chopps old = vu->view;
210 1.7 chopps vu->view = new;
211 1.7 chopps vu->size.x = new->display.x;
212 1.7 chopps vu->size.y = new->display.y;
213 1.7 chopps vu->size.width = new->display.width;
214 1.7 chopps vu->size.height = new->display.height;
215 1.7 chopps vu->size.depth = new->bitmap->depth;
216 1.7 chopps vu->mode = grf_get_display_mode(vu->view);
217 1.7 chopps vu->monitor = grf_get_monitor(vu->mode);
218 1.7 chopps vu->size.x = vs->x;
219 1.7 chopps vu->size.y = vs->y;
220 1.7 chopps
221 1.17.4.2 jdolecek /*
222 1.17.4.2 jdolecek * we need a custom remove here to avoid letting
223 1.17.4.2 jdolecek * another view display mark as not added or displayed
224 1.7 chopps */
225 1.7 chopps if (vu->flags & VUF_DISPLAY) {
226 1.7 chopps vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
227 1.7 chopps view_display(vu);
228 1.7 chopps }
229 1.7 chopps grf_free_view(old);
230 1.7 chopps return(0);
231 1.1 mw }
232 1.1 mw
233 1.1 mw /*
234 1.1 mw * functions made available by conf.c
235 1.1 mw */
236 1.1 mw
237 1.1 mw /*ARGSUSED*/
238 1.7 chopps int
239 1.17.4.2 jdolecek viewopen(dev_t dev, int flags, int mode, struct proc *p)
240 1.7 chopps {
241 1.7 chopps dimen_t size;
242 1.7 chopps struct view_softc *vu;
243 1.7 chopps
244 1.7 chopps vu = &views[minor(dev)];
245 1.7 chopps
246 1.7 chopps if (minor(dev) >= NVIEW)
247 1.7 chopps return(EXDEV);
248 1.7 chopps
249 1.7 chopps if (vu->flags & VUF_OPEN)
250 1.7 chopps return(EBUSY);
251 1.7 chopps
252 1.7 chopps vu->size.x = view_default_x;
253 1.7 chopps vu->size.y = view_default_y;
254 1.7 chopps size.width = vu->size.width = view_default_width;
255 1.7 chopps size.height = vu->size.height = view_default_height;
256 1.7 chopps vu->size.depth = view_default_depth;
257 1.7 chopps
258 1.7 chopps vu->view = grf_alloc_view(NULL, &size, vu->size.depth);
259 1.7 chopps if (vu->view == NULL)
260 1.7 chopps return(ENOMEM);
261 1.1 mw
262 1.1 mw vu->size.x = vu->view->display.x;
263 1.1 mw vu->size.y = vu->view->display.y;
264 1.1 mw vu->size.width = vu->view->display.width;
265 1.1 mw vu->size.height = vu->view->display.height;
266 1.1 mw vu->size.depth = vu->view->bitmap->depth;
267 1.1 mw vu->flags |= VUF_OPEN;
268 1.7 chopps vu->mode = grf_get_display_mode(vu->view);
269 1.7 chopps vu->monitor = grf_get_monitor(vu->mode);
270 1.7 chopps return(0);
271 1.1 mw }
272 1.1 mw
273 1.1 mw /*ARGSUSED*/
274 1.13 veego int
275 1.17.4.2 jdolecek viewclose(dev_t dev, int flags, int mode, struct proc *p)
276 1.1 mw {
277 1.7 chopps struct view_softc *vu;
278 1.7 chopps
279 1.7 chopps vu = &views[minor(dev)];
280 1.1 mw
281 1.7 chopps if ((vu->flags & VUF_OPEN) == 0)
282 1.13 veego return(0);
283 1.1 mw view_remove (vu);
284 1.1 mw grf_free_view (vu->view);
285 1.1 mw vu->flags = 0;
286 1.1 mw vu->view = NULL;
287 1.1 mw vu->mode = NULL;
288 1.17.4.2 jdolecek vu->monitor = NULL;
289 1.13 veego return(0);
290 1.1 mw }
291 1.1 mw
292 1.1 mw
293 1.1 mw /*ARGSUSED*/
294 1.7 chopps int
295 1.17.4.2 jdolecek viewioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
296 1.7 chopps {
297 1.7 chopps struct view_softc *vu;
298 1.7 chopps bmap_t *bm;
299 1.7 chopps int error;
300 1.7 chopps
301 1.7 chopps vu = &views[minor(dev)];
302 1.7 chopps error = 0;
303 1.7 chopps
304 1.7 chopps switch (cmd) {
305 1.7 chopps case VIOCDISPLAY:
306 1.7 chopps view_display(vu);
307 1.7 chopps break;
308 1.7 chopps case VIOCREMOVE:
309 1.7 chopps view_remove(vu);
310 1.7 chopps break;
311 1.7 chopps case VIOCGSIZE:
312 1.17.4.2 jdolecek bcopy(&vu->size, data, sizeof (struct view_size));
313 1.7 chopps break;
314 1.7 chopps case VIOCSSIZE:
315 1.7 chopps error = view_setsize(vu, (struct view_size *)data);
316 1.7 chopps break;
317 1.7 chopps case VIOCGBMAP:
318 1.7 chopps bm = (bmap_t *)data;
319 1.7 chopps bcopy(vu->view->bitmap, bm, sizeof(bmap_t));
320 1.13 veego if (flag != -1) {
321 1.7 chopps bm->plane = 0;
322 1.7 chopps bm->blit_temp = 0;
323 1.7 chopps bm->hardware_address = 0;
324 1.7 chopps }
325 1.7 chopps break;
326 1.7 chopps case VIOCGCMAP:
327 1.7 chopps error = view_get_colormap(vu, (colormap_t *)data);
328 1.7 chopps break;
329 1.7 chopps case VIOCSCMAP:
330 1.7 chopps error = view_set_colormap(vu, (colormap_t *)data);
331 1.7 chopps break;
332 1.7 chopps default:
333 1.17.4.3 jdolecek error = EPASSTHROUGH;
334 1.7 chopps break;
335 1.1 mw }
336 1.7 chopps return(error);
337 1.1 mw }
338 1.1 mw
339 1.7 chopps int
340 1.17.4.2 jdolecek view_get_colormap(struct view_softc *vu, colormap_t *ucm)
341 1.1 mw {
342 1.7 chopps int error;
343 1.7 chopps u_long *cme;
344 1.7 chopps u_long *uep;
345 1.7 chopps
346 1.7 chopps /* add one incase of zero, ick. */
347 1.17.4.4 jdolecek if (ucm->size + 1 > SIZE_T_MAX / sizeof(u_long))
348 1.17.4.4 jdolecek return EINVAL;
349 1.7 chopps cme = malloc(sizeof (u_long)*(ucm->size + 1), M_IOCTLOPS, M_WAITOK);
350 1.7 chopps if (cme == NULL)
351 1.7 chopps return(ENOMEM);
352 1.7 chopps
353 1.7 chopps uep = ucm->entry;
354 1.17.4.2 jdolecek error = 0;
355 1.7 chopps ucm->entry = cme; /* set entry to out alloc. */
356 1.7 chopps if (vu->view == NULL || grf_get_colormap(vu->view, ucm))
357 1.7 chopps error = EINVAL;
358 1.17.4.2 jdolecek else
359 1.7 chopps error = copyout(cme, uep, sizeof(u_long) * ucm->size);
360 1.7 chopps ucm->entry = uep; /* set entry back to users. */
361 1.7 chopps free(cme, M_IOCTLOPS);
362 1.7 chopps return(error);
363 1.7 chopps }
364 1.7 chopps
365 1.7 chopps int
366 1.17.4.2 jdolecek view_set_colormap(struct view_softc *vu, colormap_t *ucm)
367 1.7 chopps {
368 1.7 chopps colormap_t *cm;
369 1.7 chopps int error;
370 1.7 chopps
371 1.7 chopps error = 0;
372 1.7 chopps cm = malloc(sizeof(u_long) * ucm->size + sizeof (*cm), M_IOCTLOPS,
373 1.7 chopps M_WAITOK);
374 1.7 chopps if (cm == NULL)
375 1.7 chopps return(ENOMEM);
376 1.7 chopps
377 1.7 chopps bcopy (ucm, cm, sizeof(colormap_t));
378 1.7 chopps cm->entry = (u_long *)&cm[1]; /* table directly after. */
379 1.17.4.2 jdolecek if (((error =
380 1.7 chopps copyin(ucm->entry, cm->entry, sizeof (u_long) * ucm->size)) == 0)
381 1.7 chopps && (vu->view == NULL || grf_use_colormap(vu->view, cm)))
382 1.7 chopps error = EINVAL;
383 1.7 chopps free(cm, M_IOCTLOPS);
384 1.7 chopps return(error);
385 1.1 mw }
386 1.1 mw
387 1.1 mw /*ARGSUSED*/
388 1.17 simonb paddr_t
389 1.17.4.2 jdolecek viewmmap(dev_t dev, off_t off, int prot)
390 1.1 mw {
391 1.7 chopps struct view_softc *vu;
392 1.7 chopps bmap_t *bm;
393 1.7 chopps u_char *bmd_start;
394 1.17.4.2 jdolecek u_long bmd_size;
395 1.7 chopps
396 1.7 chopps vu = &views[minor(dev)];
397 1.7 chopps bm = vu->view->bitmap;
398 1.17.4.2 jdolecek bmd_start = bm->hardware_address;
399 1.7 chopps bmd_size = bm->bytes_per_row*bm->rows*bm->depth;
400 1.7 chopps
401 1.7 chopps if (off >= 0 && off < bmd_size)
402 1.17 simonb return(((paddr_t)bmd_start + off) >> PGSHIFT);
403 1.7 chopps
404 1.7 chopps return(-1);
405 1.1 mw }
406 1.1 mw
407 1.1 mw /*ARGSUSED*/
408 1.7 chopps int
409 1.17.4.2 jdolecek viewpoll(dev_t dev, int events, struct proc *p)
410 1.1 mw {
411 1.15 mhitch return(events & (POLLOUT | POLLWRNORM));
412 1.17.4.1 thorpej }
413 1.17.4.1 thorpej
414 1.17.4.1 thorpej int
415 1.17.4.1 thorpej viewkqfilter(dev_t dev, struct knote *kn)
416 1.17.4.1 thorpej {
417 1.17.4.1 thorpej
418 1.17.4.1 thorpej /* XXXLUKEM (thorpej): not supported -- why is poll? */
419 1.17.4.1 thorpej return (1);
420 1.1 mw }
421