Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*	$NetBSD: view.c,v 1.39 2023/03/26 15:24:21 andvar Exp $	*/

/*
 * Copyright (c) 1994 Christian E. Hopps
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Christian E. Hopps.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/* The view major device is a placeholder device.  It serves
 * simply to map the semantics of a graphics display to
 * the semantics of a character block device.  In other
 * words the graphics system as currently built does not like to be
 * referred to by open/close/ioctl.  This device serves as
 * a interface to graphics. */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: view.c,v 1.39 2023/03/26 15:24:21 andvar Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/conf.h>
#include <machine/cpu.h>
#include <atari/dev/grfabs_reg.h>
#include <atari/dev/viewioctl.h>
#include <atari/dev/viewvar.h>

#include "view.h"
#include "ioconf.h"

static void view_display(struct view_softc *);
static void view_remove(struct view_softc *);
static int  view_setsize(struct view_softc *, struct view_size *);
static int  view_get_colormap(struct view_softc *, colormap_t *);
static int  view_set_colormap(struct view_softc *, colormap_t *);

struct view_softc views[NVIEW];
static int view_inited;

int view_default_x;
int view_default_y;
int view_default_width  = 640;
int view_default_height = 400;
int view_default_depth  = 1;

static dev_type_open(viewopen);
static dev_type_close(viewclose);
static dev_type_ioctl(viewioctl);
static dev_type_mmap(viewmmap);

const struct cdevsw view_cdevsw = {
	.d_open = viewopen,
	.d_close = viewclose,
	.d_read = nullread,
	.d_write = nullwrite,
	.d_ioctl = viewioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = nopoll,
	.d_mmap = viewmmap,
	.d_kqfilter = nokqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

/*
 *  functions for probeing.
 */
void
viewattach(int cnt)
{
	viewprobe();
	printf("%d view%s configured\n", NVIEW, NVIEW > 1 ? "s" : "");
}

/* this function is called early to set up a display. */
int
viewprobe(void)
{
	int i;

	if (view_inited)
		return 1;

	view_inited = 1;

	for (i = 0; i < NVIEW; i++) {
		views[i].view = NULL;
		views[i].flags = 0;
	}
	return 1;
}


/*
 *  Internal functions.
 */

static void
view_display (struct view_softc *vu)
{
	int s, i;

	if (vu == NULL)
		return;

	s = spltty();

	/*
	 * mark views that share this monitor as not displaying
	 */
	for (i = 0; i < NVIEW; i++) {
		if (views[i].flags & VUF_DISPLAY) {
			if (vu->view && (vu->view == views[i].view)) {
				splx(s);
				return;
			}
			if (views[i].view) {
				grf_save_view(views[i].view);
				views[i].view->flags &= ~VF_DISPLAY;
			}
			views[i].flags &= ~VUF_DISPLAY;
		}
	}

	vu->flags |= VUF_ADDED;
	if (vu->view) {
		vu->view->display.x = vu->size.x;
		vu->view->display.y = vu->size.y;

		grf_display_view(vu->view);
		vu->view->flags |= VF_DISPLAY;

		vu->size.x = vu->view->display.x;
		vu->size.y = vu->view->display.y;
		vu->flags |= VUF_DISPLAY;
	}
	splx(s);
}

/*
 * remove a view from our added list if it is marked as displaying
 * switch to a new display.
 */
static void
view_remove(struct view_softc *vu)
{
	int i;

	if ((vu->flags & VUF_ADDED) == 0)
		return;

	vu->flags &= ~VUF_ADDED;
	if (vu->flags & VUF_DISPLAY) {
		for (i = 0; i < NVIEW; i++) {
			if ((views[i].flags & VUF_ADDED) && &views[i] != vu) {
				view_display(&views[i]);
				break;
			}
		}
	}
	vu->flags &= ~VUF_DISPLAY;
	grf_remove_view(vu->view);
}

static int
view_setsize(struct view_softc *vu, struct view_size *vs)
{
	view_t	*new, *old;
	dmode_t	*dmode;
	dimen_t ns;
	int	co, cs;

	co = 0;
	cs = 0;
	if (vs->x != vu->size.x || vs->y != vu->size.y)
		co = 1;

	if (vs->width != vu->size.width || vs->height != vu->size.height ||
	    vs->depth != vu->size.depth)
		cs = 1;

	if (cs == 0 && co == 0)
		return 0;

	ns.width  = vs->width;
	ns.height = vs->height;

	if ((dmode = grf_get_best_mode(&ns, vs->depth)) != NULL) {
		/*
		 * If we can't do better, leave it
		 */
		if (dmode == vu->view->mode)
			return 0;
	}
	new = grf_alloc_view(dmode, &ns, vs->depth);
	if (new == NULL)
		return ENOMEM;

	old = vu->view;
	vu->view = new;
	vu->size.x = new->display.x;
	vu->size.y = new->display.y;
	vu->size.width = new->display.width;
	vu->size.height = new->display.height;
	vu->size.depth = new->bitmap->depth;

	/*
	 * we need a custom remove here to avoid letting
	 * another view display mark as not added or displayed
	 */
	if (vu->flags & VUF_DISPLAY) {
		vu->flags &= ~(VUF_ADDED|VUF_DISPLAY);
		view_display(vu);
	}
	grf_free_view(old);
	return 0;
}

static int
view_get_colormap (struct view_softc *vu, colormap_t *ucm)
{
	int	error;
	long	*cme;
	long	*uep;

	if (ucm->size > MAX_CENTRIES)
		return EINVAL;

	/* add one incase of zero, ick. */
	cme = malloc(sizeof(ucm->entry[0])*(ucm->size+1), M_TEMP,M_WAITOK);
	if (cme == NULL)
		return ENOMEM;

	error      = 0;
	uep        = ucm->entry;
	ucm->entry = cme;	  /* set entry to out alloc. */
	if (vu->view == NULL || grf_get_colormap(vu->view, ucm))
		error = EINVAL;
	else
		error = copyout(cme, uep, sizeof(ucm->entry[0]) * ucm->size);
	ucm->entry = uep;	  /* set entry back to users. */
	free(cme, M_TEMP);
	return error;
}

static int
view_set_colormap(struct view_softc *vu, colormap_t *ucm)
{
	colormap_t	*cm;
	int		error = 0;

	if (ucm->size > MAX_CENTRIES)
		return EINVAL;

	cm = malloc(sizeof(ucm->entry[0])*ucm->size + sizeof(*cm),
	    M_TEMP, M_WAITOK);
	if (cm == NULL)
		return ENOMEM;

	memcpy(cm, ucm, sizeof(colormap_t));
	cm->entry = (long *)&cm[1];		 /* table directly after. */
	if (((error =
	    copyin(ucm->entry,cm->entry,sizeof(ucm->entry[0])*ucm->size)) == 0)
	    && (vu->view == NULL || grf_use_colormap(vu->view, cm)))
		error = EINVAL;
	free(cm, M_TEMP);
	return error;
}

/*
 *  functions made available by conf.c
 */

/*ARGSUSED*/
static int
viewopen(dev_t dev, int flags, int mode, struct lwp *l)
{
	dimen_t			size;
	struct view_softc	*vu;

	vu = &views[minor(dev)];

	if (minor(dev) >= NVIEW)
		return EXDEV;
	if (vu->flags & VUF_OPEN)
		return EBUSY;

	vu->size.x = view_default_x;
	vu->size.y = view_default_y;
	size.width = vu->size.width = view_default_width;
	size.height = vu->size.height = view_default_height;
	vu->size.depth = view_default_depth;
	vu->view = grf_alloc_view(NULL, &size, vu->size.depth);
	if (vu->view == NULL)
		return ENOMEM;

	vu->size.x = vu->view->display.x;
	vu->size.y = vu->view->display.y;
	vu->size.width = vu->view->display.width;
	vu->size.height = vu->view->display.height;
	vu->size.depth = vu->view->bitmap->depth;
	vu->flags |= VUF_OPEN;
	return 0;
}

/*ARGSUSED*/
static int
viewclose (dev_t dev, int flags, int mode, struct lwp *l)
{
	struct view_softc *vu;

	vu = &views[minor(dev)];

	if ((vu->flags & VUF_OPEN) == 0)
		return 0; /* XXX not open? */
	view_remove (vu);
	grf_free_view (vu->view);
	vu->flags = 0;
	vu->view = NULL;
	return 0;
}


/*ARGSUSED*/
static int
viewioctl (dev_t dev, u_long cmd, void * data, int flag, struct lwp *l)
{
	struct view_softc	*vu;
	bmap_t			*bm;
	int			error;

	vu = &views[minor(dev)];
	error = 0;

	switch (cmd) {
	case VIOCDISPLAY:
		view_display(vu);
		break;
	case VIOCREMOVE:
		view_remove(vu);
		break;
	case VIOCGSIZE:
		memcpy(data, &vu->size, sizeof (struct view_size));
		break;
	case VIOCSSIZE:
		error = view_setsize(vu, (struct view_size *)data);
		break;
	case VIOCGBMAP:
		bm = (bmap_t *)data;
		memcpy(bm, vu->view->bitmap, sizeof(bmap_t));
		if (l != NOLWP) {
			bm->plane      = NULL;
			bm->hw_address = NULL;
			bm->regs       = NULL;
			bm->hw_regs    = NULL;
		}
		break;
	case VIOCGCMAP:
		error = view_get_colormap(vu, (colormap_t *)data);
		break;
	case VIOCSCMAP:
		error = view_set_colormap(vu, (colormap_t *)data);
		break;
	default:
		error = EPASSTHROUGH;
		break;
	}
	return error;
}

/*ARGSUSED*/
static paddr_t
viewmmap(dev_t dev, off_t off, int prot)
{
	struct view_softc	*vu;
	bmap_t			*bm;
	u_char			*bmd_start;
	u_long			bmd_lin, bmd_vga;

	vu = &views[minor(dev)];
	bm = vu->view->bitmap;
	bmd_start = bm->hw_address;
	bmd_lin = bm->lin_base;
	bmd_vga = bm->vga_base;

	/*
	 * control registers
	 */
	if (off >= 0 && off < bm->reg_size)
		return ((paddr_t)bm->hw_regs + off) >> PGSHIFT;

	/*
	 * VGA memory
	 */
	if (off >= bmd_vga && off < (bmd_vga + bm->vga_mappable))
		return ((paddr_t)bm->vga_address - bmd_vga + off) >> PGSHIFT;

	/*
	 * frame buffer
	 */
	if (off >= bmd_lin && off < (bmd_lin + bm->phys_mappable))
		return ((paddr_t)bmd_start - bmd_lin + off) >> PGSHIFT;

	return -1;
}

view_t	*
viewview(dev_t dev)
{

	return views[minor(dev)].view;
}