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