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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
/* $NetBSD: vidcvideo.c,v 1.45 2018/01/24 05:35:58 riastradh Exp $ */

/*
 * Copyright (c) 2001 Reinoud Zandijk
 * Copyright (c) 1998, 1999 Tohru Nishimura.  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.
 *
 * 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.
 *
 * Created vidcvideo.c from /dev/tc/cfb.c to fit the Acorn/ARM VIDC1 and VIDC20 chips
 *
 */

#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */

__KERNEL_RCSID(0, "$NetBSD: vidcvideo.c,v 1.45 2018/01/24 05:35:58 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/ioctl.h>

#include <arm/mainbus/mainbus.h>
#include <sys/bus.h>
#include <machine/intr.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>

#include <dev/rasops/rasops.h>
#include <dev/wsfont/wsfont.h>

#include <dev/wscons/wsdisplay_vconsvar.h>

#include <uvm/uvm_extern.h>
#include <arm/arm32/pmap.h>
#include <arm/cpufunc.h>

/* for vidc_mode ... needs to be MI indepenent one day */
#include <arm/iomd/vidc.h>
#include <arm/iomd/vidc20config.h>
#include <arm/iomd/vidcvideo.h>
#include <machine/bootconfig.h>

/* FOR DEBUG */
extern videomemory_t videomemory;

struct hwcmap256 {
#define	CMAP_SIZE	256	/* 256 R/G/B entries */
	uint8_t r[CMAP_SIZE];
	uint8_t g[CMAP_SIZE];
	uint8_t b[CMAP_SIZE];
};


/* XXX for CURSOR_MAX_WIDTH = 32 */
struct hwcursor32 {
	struct wsdisplay_curpos cc_pos;
	struct wsdisplay_curpos cc_hot;
	struct wsdisplay_curpos cc_size;
	uint8_t cc_color[6];		/* how many? */
	uint32_t cc_image[(CURSOR_MAX_WIDTH/4) * CURSOR_MAX_HEIGHT];
	uint32_t cc_mask[(CURSOR_MAX_WIDTH/4) * CURSOR_MAX_HEIGHT];
};


struct fb_devconfig {
	vaddr_t dc_vaddr;		/* memory space virtual base address */
	paddr_t dc_paddr;		/* memory space physical base address */
	vsize_t dc_size;		/* size of slot memory */
	int	dc_width;		/* width of frame buffer */
	int	dc_height;		/* height of frame buffer */
	int	dc_log2_depth;		/* log2 of bits per pixel */
	int	dc_depth;		/* depth, bits per pixel */
	int	dc_rowbytes;		/* bytes in a FB scan line */
	vaddr_t	dc_videobase;		/* base of flat frame buffer */
	int	dc_blanked;		/* currently has video disabled */
	void   *dc_hwscroll_cookie;	/* cookie for hardware scroll */
	void   *dc_ih;			/* interrupt handler for dc */

	int	dc_curenb;		/* is cursor sprite enabled ?	*/
	int	_internal_dc_changed;	/* need update of hardware	*/
	int	dc_writeback_delay;	/* Screenarea write back vsync counter */
#define	WSDISPLAY_CMAP_DOLUT	0x20
#define WSDISPLAY_VIDEO_ONOFF	0x40
#define WSDISPLAY_WB_COUNTER	0x80

	struct hwcmap256	dc_cmap;   /* software copy of colormap	*/
	struct hwcursor32	dc_cursor; /* software copy of cursor	*/

	struct vidc_mode	mode_info;

	struct wsdisplay_emulops orig_ri_ops;	/* Rasops functions for deligation */

	/* virtual console support */
	struct vcons_data dc_vd;
	struct vcons_screen dc_console;
};


struct vidcvideo_softc {
	device_t sc_dev;
	struct fb_devconfig *sc_dc;	/* device configuration		*/
};


/* Function prototypes for glue */
static int  vidcvideo_match(device_t , cfdata_t , void *);
static void vidcvideo_attach(device_t , device_t , void *);


/* config glue */
CFATTACH_DECL_NEW(vidcvideo, sizeof(struct vidcvideo_softc),
    vidcvideo_match, vidcvideo_attach, NULL, NULL);

static struct fb_devconfig vidcvideo_console_dc;
static bool vidcvideo_is_console = false; 


static struct wsscreen_descr vidcvideo_stdscreen = {
	"std", 0, 0,
	NULL, /* textops */
	8, 16,
	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
	NULL
};

static const struct wsscreen_descr *_vidcvideo_scrlist[] = {
	&vidcvideo_stdscreen,
};

static const struct wsscreen_list vidcvideo_screenlist = {
	sizeof(_vidcvideo_scrlist) / sizeof(struct wsscreen_descr *),
	_vidcvideo_scrlist
};

static int	vidcvideoioctl(void *, void *, u_long, void *, int,
    struct lwp *);
static paddr_t	vidcvideommap(void *, void *, off_t, int);
static void	vidcvideoinit_screen(void *, struct vcons_screen *, int, long *);

static void	vidcvideo_queue_dc_change(struct fb_devconfig*, int);
static int	flush_dc_changes_to_screen(struct fb_devconfig*);

static struct wsdisplay_accessops vidcvideo_accessops = {
	vidcvideoioctl,
	vidcvideommap,
	NULL,	/* alloc_screen */
	NULL,	/* free_screen */
	NULL,	/* show_screen */
	NULL,	/* load_font */
	NULL,	/* pollc */
	NULL	/* scroll */
};


/* Function prototypes */
int         vidcvideo_cnattach(vaddr_t);
static void vidcvideo_colourmap_and_cursor_init(struct fb_devconfig *);

static int  get_cmap(struct vidcvideo_softc *, struct wsdisplay_cmap *);
static int  set_cmap(struct vidcvideo_softc *, struct wsdisplay_cmap *);
static int  set_cursor(struct vidcvideo_softc *, struct wsdisplay_cursor *);
static int  get_cursor(struct vidcvideo_softc *, struct wsdisplay_cursor *);
static void set_curpos(struct vidcvideo_softc *, struct wsdisplay_curpos *);
static void vidcvideo_getdevconfig(vaddr_t, u_int, struct fb_devconfig *);

static int  vidcvideointr(void *);

/* Acceleration function prototypes */
static void vv_copyrows(void *, int, int, int);
static void vv_eraserows(void *, int, int, long);
static void vv_putchar(void *c, int row, int col, u_int uc, long attr);


static int
vidcvideo_match(device_t parent, cfdata_t match, void *aux)
{

	/* Can't probe AFAIK ; how ? */
	return 1;
}


static void
vidcvideo_getdevconfig(vaddr_t dense_addr, u_int mem_size,
		struct fb_devconfig *dc)
{

	dc->dc_vaddr = dense_addr;
	(void) pmap_extract(pmap_kernel(), dc->dc_vaddr, &(dc->dc_paddr));

	vidcvideo_getmode(&dc->mode_info);

	dc->dc_width = dc->mode_info.timings.hdisplay;
	dc->dc_height = dc->mode_info.timings.vdisplay;
	dc->dc_log2_depth = dc->mode_info.log2_bpp;
	dc->dc_depth = 1 << dc->dc_log2_depth;
	dc->dc_videobase = dc->dc_vaddr;
	dc->dc_blanked = 0;

	/* this should/could be done somewhat more elegant! */
	switch (dc->dc_depth) {
		case 1:
			dc->dc_rowbytes = dc->dc_width / 8;
			break;
		case 2:
			dc->dc_rowbytes = dc->dc_width / 4;
			break;
		case 4:
			dc->dc_rowbytes = dc->dc_width / 2;
			break;
		case 8:
			dc->dc_rowbytes = dc->dc_width;
			break;
		case 16:
			dc->dc_rowbytes = dc->dc_width * 2;
			break;
		case 32:
			dc->dc_rowbytes = dc->dc_width * 4;
			break;
		default:
			printf("Unknown colour depth %d ... what to do ?", dc->dc_depth);
			break;
	}

	/* setup the correct size */
	dc->dc_size = mem_size;

	/* initialize colormap and cursor resource */
	vidcvideo_colourmap_and_cursor_init(dc);

	/* blank the memory */
	memset((void*)dc->dc_vaddr, 0, dc->dc_size);

	/* intitialise miscelanious */
	dc->dc_writeback_delay = 0;
}

static void
vidcvideoinit_screen(void *cookie, struct vcons_screen *scr,
		    int existing, long *defattr)
{
	struct rasops_info *ri = &scr->scr_ri;
	struct fb_devconfig *dc = cookie;

	if ((scr == &dc->dc_console) && (dc->dc_vd.active != NULL))
		return;

	ri->ri_flg    = RI_NO_AUTO;	/* RI_CENTER | RI_FULLCLEAR; */
	ri->ri_depth  = dc->dc_depth;
	ri->ri_bits   = (void *) dc->dc_videobase;
	ri->ri_width  = dc->dc_width;
	ri->ri_height = dc->dc_height;
	ri->ri_stride = dc->dc_rowbytes;
	ri->ri_hw     = &dc->dc_console;	/* link back */

	rasops_init(ri,
	    ri->ri_height / 8,
	    ri->ri_width / 8);

	ri->ri_caps = WSSCREEN_WSCOLORS;

	rasops_reconfig(ri,
	    ri->ri_height / ri->ri_font->fontheight,
	    ri->ri_width / ri->ri_font->fontwidth);

	/*
	 * Provide a hook for the acceleration functions and make a copy of the
	 * original rasops functions for passing on calls
	 */
	memcpy(&(dc->orig_ri_ops), &(ri->ri_ops),
	    sizeof(struct wsdisplay_emulops));

	/* add our accelerated functions */
	ri->ri_ops.eraserows = vv_eraserows;
	ri->ri_ops.copyrows  = vv_copyrows;

	/* add the extra activity measuring functions; they just delegate on */
	ri->ri_ops.putchar   = vv_putchar;

	vidcvideo_stdscreen.nrows = ri->ri_rows;
	vidcvideo_stdscreen.ncols = ri->ri_cols;
	vidcvideo_stdscreen.textops = &ri->ri_ops;
	vidcvideo_stdscreen.capabilities = ri->ri_caps;
}

static void
vidcvideo_attach(device_t parent, device_t self, void *aux)
{
	struct vidcvideo_softc *sc = device_private(self);
	struct fb_devconfig *dc;
	struct wsemuldisplaydev_attach_args waa;
	long defattr;
	
	sc->sc_dev = self;

	dc = sc->sc_dc = &vidcvideo_console_dc;

	/*
	 * for reasons which are crazy we init vidcvideo twice,
	 * the second time sets up the cursor
	 */
	vidcvideo_init();
	if (!vidcvideo_is_console) {
		vidcvideo_getdevconfig(videomemory.vidm_vbase, 
				videomemory.vidm_size,
			       	sc->sc_dc);
	}

	vcons_init(&dc->dc_vd, dc, &vidcvideo_stdscreen, &vidcvideo_accessops);
	dc->dc_vd.init_screen = vidcvideoinit_screen;

	vcons_init_screen(&dc->dc_vd, &dc->dc_console, 1, &defattr);

	dc->dc_console.scr_flags |= VCONS_SCREEN_IS_STATIC;

	vidcvideo_printdetails();
	aprint_normal(": mode %s, %dbpp\n", dc->mode_info.timings.name,
	    dc->dc_depth);

	/* set up interrupt flags */
	vidcvideo_queue_dc_change(dc, WSDISPLAY_CMAP_DOLUT);

	/* Establish an interrupt handler, and clear any pending interrupts */
	dc->dc_ih = intr_claim(IRQ_FLYBACK, IPL_TTY, "vblank", vidcvideointr, dc);

	waa.console = (vidcvideo_is_console ? 1 : 0);
	waa.scrdata = &vidcvideo_screenlist;
	waa.accessops = &vidcvideo_accessops;
	waa.accesscookie = &dc->dc_vd;

	config_found(self, &waa, wsemuldisplaydevprint);
}


static int
vidcvideoioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    struct lwp *l)
{
	struct vcons_data *vd = v;
	struct vidcvideo_softc *sc = vd->cookie;
	struct fb_devconfig *dc = sc->sc_dc;
	struct vcons_screen *ms = vd->active;
	int state;

	switch (cmd) {
	case WSDISPLAYIO_GTYPE:
		*(u_int *)data = WSDISPLAY_TYPE_VIDC;
		return 0;

	case WSDISPLAYIO_GINFO:
		if (ms == NULL)
			return ENODEV;
#define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
		wsd_fbip->height = dc->dc_height;
		wsd_fbip->width  = dc->dc_width;
		wsd_fbip->depth  = dc->dc_depth;
		wsd_fbip->cmsize = CMAP_SIZE;
#undef fbt
		return 0;

	case WSDISPLAYIO_GETCMAP:
		return get_cmap(sc, (struct wsdisplay_cmap *)data);

	case WSDISPLAYIO_PUTCMAP:
		return set_cmap(sc, (struct wsdisplay_cmap *)data);
	
	case WSDISPLAYIO_LINEBYTES:
		*(u_int *)data = dc->dc_rowbytes;
		return 0;

	case WSDISPLAYIO_SVIDEO:
		state = *(int *)data;
		dc->dc_blanked = (state == WSDISPLAYIO_VIDEO_OFF);
		vidcvideo_queue_dc_change(dc, WSDISPLAY_VIDEO_ONOFF);
		/* done on video blank */
		return 0;

	case WSDISPLAYIO_GVIDEO:
		*(u_int *)data = dc->dc_blanked ?
		    WSDISPLAYIO_VIDEO_OFF : WSDISPLAYIO_VIDEO_ON;
		return 0;

	case WSDISPLAYIO_GCURPOS:
		*(struct wsdisplay_curpos *)data = dc->dc_cursor.cc_pos;
		return 0;

	case WSDISPLAYIO_SCURPOS:
		set_curpos(sc, (struct wsdisplay_curpos *)data);
		vidcvideo_queue_dc_change(dc, WSDISPLAY_CURSOR_DOPOS);
		return 0;

	case WSDISPLAYIO_GCURMAX:
		((struct wsdisplay_curpos *)data)->x = CURSOR_MAX_WIDTH;
		((struct wsdisplay_curpos *)data)->y = CURSOR_MAX_HEIGHT;
		return 0;

	case WSDISPLAYIO_GCURSOR:
		return get_cursor(sc, (struct wsdisplay_cursor *)data);

	case WSDISPLAYIO_SCURSOR:
		return set_cursor(sc, (struct wsdisplay_cursor *)data);

	case WSDISPLAYIO_SMODE:
		state = *(int *)data;
		if (state == WSDISPLAYIO_MODE_MAPPED)
			dc->dc_hwscroll_cookie = vidcvideo_hwscroll_reset();
		if (state == WSDISPLAYIO_MODE_EMUL)
			vidcvideo_hwscroll_back(dc->dc_hwscroll_cookie);
		vidcvideo_progr_scroll();

		return 0;
	}
	return EPASSTHROUGH;
}


paddr_t
vidcvideommap(void *v, void *vs, off_t offset, int prot)
{
	struct vcons_data *vd = v;
	struct vidcvideo_softc *sc = vd->cookie;

	if (offset >= sc->sc_dc->dc_size || offset < 0)
		return -1;

	return arm_btop(sc->sc_dc->dc_paddr + offset);
}


/* EXPORT */ int
vidcvideo_cnattach(vaddr_t addr)
{
	struct fb_devconfig *dc = &vidcvideo_console_dc;
	struct rasops_info *ri;
	long defattr;

	vidcvideo_init();

	/* fetch current framebuffer config */
	vidcvideo_getdevconfig(videomemory.vidm_vbase,
			videomemory.vidm_size,
		       	dc);

	dc->dc_vd.active = NULL;
	vidcvideoinit_screen(dc, &dc->dc_console, 1, &defattr);

	ri = &(dc->dc_console.scr_ri);
	ri->ri_hw = &dc->dc_console;
	dc->dc_console.scr_cookie = dc;

	(*ri->ri_ops.allocattr)(ri,
				WS_DEFAULT_FG, /* fg */
				WS_DEFAULT_BG, /* bg */
				0,           /* wsattrs */
				&defattr);

	wsdisplay_cnattach(&vidcvideo_stdscreen,
			   ri,   /* emulcookie */
		       	   0, 0, /* cursor position */
			   defattr);
	
	vidcvideo_is_console = true;

	return 0;
}


static int
vidcvideointr(void *arg)
{
	struct fb_devconfig *dc = arg;

	return flush_dc_changes_to_screen(dc);
}

static int
flush_dc_changes_to_screen(struct fb_devconfig *dc)
{
	int v, cleared = 0;

	v = dc->_internal_dc_changed;

	if (v == 0) {
		disable_irq(IRQ_FLYBACK);
		return 1;
	}

	if (v & WSDISPLAY_WB_COUNTER) {
	    	dc->dc_writeback_delay--;
		if (dc->dc_writeback_delay == 0) {
		    	cpu_dcache_wb_range(dc->dc_vaddr, dc->dc_size);
			cleared |= WSDISPLAY_WB_COUNTER;
		}
	}

	if (v & WSDISPLAY_CMAP_DOLUT) {
		struct hwcmap256 *cm = &dc->dc_cmap;
		int index;

		if (dc->dc_depth == 4) {
			/* palette for 4 bpp is different from 8bpp */
			vidcvideo_write(VIDC_PALREG, 0x00000000);
			for (index=0; index < (1 << dc->dc_depth); index++)
				vidcvideo_write(VIDC_PALETTE,
					VIDC_COL(cm->r[index],
						 cm->g[index],
						 cm->b[index]));
		}

		if (dc->dc_depth == 8) {
			/*
			 * dunno what to do in more than 8bpp 
			 * palettes only make sense in 8bpp and less modes
			 * on VIDC
			 */
			vidcvideo_write(VIDC_PALREG, 0x00000000);
			for (index = 0; index < CMAP_SIZE; index++) {
				vidcvideo_write(VIDC_PALETTE,
					VIDC_COL(cm->r[index], cm->g[index],
					    cm->b[index]));
			}
		}
		cleared |= WSDISPLAY_CMAP_DOLUT;
	}

	if (v & WSDISPLAY_VIDEO_ONOFF) {
		vidcvideo_blank(dc->dc_blanked);
		cleared |= WSDISPLAY_VIDEO_ONOFF;
	}

	if (v & (WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT)) {
		int x, y;
		x = dc->dc_cursor.cc_pos.x - dc->dc_cursor.cc_hot.x;
		y = dc->dc_cursor.cc_pos.y - dc->dc_cursor.cc_hot.y;

		vidcvideo_updatecursor(x, y);
		cleared |= WSDISPLAY_CURSOR_DOPOS | WSDISPLAY_CURSOR_DOHOT;
	}

	if (v & WSDISPLAY_CURSOR_DOCUR) {
		vidcvideo_enablecursor(dc->dc_curenb);
		cleared |= WSDISPLAY_CURSOR_DOCUR;
	}

	dc->_internal_dc_changed ^= cleared;

	if (dc->_internal_dc_changed == 0) {
		disable_irq(IRQ_FLYBACK);
	}

	return 1;
}


static void vidcvideo_queue_dc_change(struct fb_devconfig *dc, int dc_change)
{
	dc->_internal_dc_changed |= dc_change;

	if (curcpl() == IPL_HIGH) {
		/* running in ddb or without interrupts */
	    	dc->dc_writeback_delay = 1;
		flush_dc_changes_to_screen(dc);
	} else {
		/*
		 * running with interrupts so handle this in the next
		 * vsync
		 */ 
		if (dc->dc_ih) {
			enable_irq(IRQ_FLYBACK);
		}
	}
}


static const u_char ri_col_data[6][6] = {
	{ 0,  0,  0,   0,  0,  0},	/*  1 bpp */
	{ 0,  0,  0,   0,  0,  0},	/*  2 bpp */
	{ 0,  0,  0,   0,  0,  0},	/*  4 bpp */
	{ 0,  0,  0,   0,  0,  0},	/*  8 bpp */
	{ 6,  5,  5,   0,  6, 11},	/* 16 bpp */
	{ 8,  8,  8,   0,  8, 16},	/* 32 bpp */
};

static void
vidcvideo_colourmap_and_cursor_init(struct fb_devconfig *dc)
{
	struct rasops_info *ri = &dc->dc_console.scr_ri;
	const u_char *rgbdat;
	struct hwcmap256 *cm;
	const uint8_t *p;
	int index;

	/* Whatever we do later... just make sure we have a
	 * sane palette to start with
	 */
	vidcvideo_stdpalette();

	/* set up rgb bit pattern values for rasops_init */
	rgbdat = ri_col_data[dc->dc_log2_depth];
	ri->ri_rnum = rgbdat[0];
	ri->ri_gnum = rgbdat[1];
	ri->ri_bnum = rgbdat[2];
	ri->ri_rpos = rgbdat[3];
	ri->ri_gpos = rgbdat[4];
	ri->ri_bpos = rgbdat[5];

	/* initialise color map */
	cm = &dc->dc_cmap;
	p = rasops_cmap;
	for (index = 0; index < CMAP_SIZE; index++, p += 3) {
		cm->r[index] = p[0];
		cm->g[index] = p[1];
		cm->b[index] = p[2];
	}
	/* flush to hardware */
	vidcvideo_queue_dc_change(dc, WSDISPLAY_CMAP_DOLUT);
}


static int
get_cmap(struct vidcvideo_softc *sc, struct wsdisplay_cmap *p)
{
	u_int index = p->index, count = p->count;
	int error;

	if (index >= CMAP_SIZE || count > CMAP_SIZE - index)
		return EINVAL;

	error = copyout(&sc->sc_dc->dc_cmap.r[index], p->red, count);
	if (error)
		return error;
	error = copyout(&sc->sc_dc->dc_cmap.g[index], p->green, count);
	if (error)
		return error;
	error = copyout(&sc->sc_dc->dc_cmap.b[index], p->blue, count);
	return error;
}


static int
set_cmap(struct vidcvideo_softc *sc, struct wsdisplay_cmap *p)
{
    	struct fb_devconfig *dc = sc->sc_dc;
	struct hwcmap256 cmap;
	u_int index = p->index, count = p->count;
	int error;

	if (index >= CMAP_SIZE || count > CMAP_SIZE - index)
		return EINVAL;

	error = copyin(p->red, &cmap.r[index], count);
	if (error)
		return error;
	error = copyin(p->green, &cmap.g[index], count);
	if (error)
		return error;
	error = copyin(p->blue, &cmap.b[index], count);
	if (error)
		return error;
	memcpy(&dc->dc_cmap.r[index], &cmap.r[index], count);
	memcpy(&dc->dc_cmap.g[index], &cmap.g[index], count);
	memcpy(&dc->dc_cmap.b[index], &cmap.b[index], count);
	vidcvideo_queue_dc_change(dc, WSDISPLAY_CMAP_DOLUT);
	return 0;
}


static int
set_cursor(struct vidcvideo_softc *sc, struct wsdisplay_cursor *p)
{
#define	cc (&dc->dc_cursor)
    	struct fb_devconfig *dc = sc->sc_dc;
	u_int v, index = 0, count = 0, icount = 0;
	uint8_t r[2], g[2], b[2], image[512], mask[512];
	int error;

	/* XXX gcc does not detect identical conditions */
	index = count = icount = 0;

	v = p->which;
	if (v & WSDISPLAY_CURSOR_DOCMAP) {
		index = p->cmap.index;
		count = p->cmap.count;
		if (index >= CURSOR_MAX_COLOURS ||
		    (index + count) > CURSOR_MAX_COLOURS)
			return EINVAL;
		error = copyin(p->cmap.red, &r[index], count);
		if (error)
			return error;
		error = copyin(p->cmap.green, &g[index], count);
		if (error)
			return error;
		error = copyin(p->cmap.blue, &b[index], count);
		if (error)
			return error;
	}
	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
		if (p->size.x > CURSOR_MAX_WIDTH ||
		    p->size.y > CURSOR_MAX_HEIGHT)
			return EINVAL;
		icount = sizeof(uint32_t) * p->size.y;
		error = copyin(p->image, &image, icount);
		if (error)
			return error;
		error = copyin(p->mask, &mask, icount);
		if (error)
			return error;
	}

	if (v & WSDISPLAY_CURSOR_DOCUR) 
		dc->dc_curenb = p->enable;
	if (v & WSDISPLAY_CURSOR_DOPOS)
		set_curpos(sc, &p->pos);
	if (v & WSDISPLAY_CURSOR_DOHOT)
		cc->cc_hot = p->hot;
	if (v & WSDISPLAY_CURSOR_DOCMAP) {
		memcpy(&cc->cc_color[index], &r[index], count);
		memcpy(&cc->cc_color[index + 2], &g[index], count);
		memcpy(&cc->cc_color[index + 4], &b[index], count);
	}
	if (v & WSDISPLAY_CURSOR_DOSHAPE) {
		cc->cc_size = p->size;
		memset(cc->cc_image, 0, sizeof cc->cc_image);
		memcpy(cc->cc_image, image, icount);
		memset(cc->cc_mask, 0, sizeof cc->cc_mask);
		memcpy(cc->cc_mask, mask, icount);
	}
	vidcvideo_queue_dc_change(dc, v);

	return 0;
#undef cc
}


static int
get_cursor(struct vidcvideo_softc *sc, struct wsdisplay_cursor *p)
{

	return EPASSTHROUGH; /* XXX */
}


static void
set_curpos(struct vidcvideo_softc *sc, struct wsdisplay_curpos *curpos)
{
	struct fb_devconfig *dc = sc->sc_dc;
	int x = curpos->x, y = curpos->y;

	if (y < 0)
		y = 0;
	else if (y > dc->dc_height)
		y = dc->dc_height;
	if (x < 0)
		x = 0;
	else if (x > dc->dc_width)
		x = dc->dc_width;
	dc->dc_cursor.cc_pos.x = x;
	dc->dc_cursor.cc_pos.y = y;
}


static void vv_copyrows(void *id, int srcrow, int dstrow, int nrows)
{
	struct rasops_info *ri = id;
	int height, size;
	unsigned char *src, *dst;
	struct vcons_screen *scr = ri->ri_hw;
	struct fb_devconfig *dc = (struct fb_devconfig *) (scr->scr_cookie);

	/* All movements are done in multiples of character heigths */
	height = ri->ri_font->fontheight * nrows;
	size   = height * ri->ri_stride;

	/* check if we are full screen scrolling */

#if 0
	int scrollup   = (srcrow + nrows >= ri->ri_rows);
	int scrolldown = (dstrow + nrows >= ri->ri_rows);
	if ((scrollup || scrolldown) &&
	    (videomemory.vidm_type == VIDEOMEM_TYPE_VRAM)) {
		int offset = (srcrow - dstrow) * ri->ri_yscale;
		ri->ri_bits = vidcvideo_hwscroll(offset);
		vidcvideo_progr_scroll();	/* sadistic ; shouldnt this be on vsync? */

		/* wipe out remains of the screen if nessisary */
		if (ri->ri_emuheight != ri->ri_height)
			vv_eraserows(id, ri->ri_rows, 1, 0);
		return;
	}
#endif

	/*
	 * Else we just copy the area : we're braindead for now 
	 * Note: we can't use hardware scrolling when the softc isnt
	 * known yet...  if its not known we dont have interrupts and
	 * we can't change the display address reliable other than in
	 * a Vsync
	 */

	src = ri->ri_bits + srcrow * ri->ri_font->fontheight * ri->ri_stride;
	dst = ri->ri_bits + dstrow * ri->ri_font->fontheight * ri->ri_stride;

	memmove(dst, src, size);

	/* delay the write back operation of the screen area */
	dc->dc_writeback_delay = SCREEN_WRITE_BACK_DELAY;
	vidcvideo_queue_dc_change(dc, WSDISPLAY_WB_COUNTER);
}


static void vv_eraserows(void *id, int startrow, int nrows, long attr)
{
	struct rasops_info *ri = id;
	int height;
	unsigned char *src;
	struct vcons_screen *scr = ri->ri_hw;
	struct fb_devconfig *dc = (struct fb_devconfig *) (scr->scr_cookie);

	/* we're braindead for now */
	height = ri->ri_font->fontheight * nrows * ri->ri_stride;

	src = ri->ri_bits + startrow * ri->ri_font->fontheight * ri->ri_stride;

	memset(src, 0, height);

	/* delay the write back operation of the screen area */
	dc->dc_writeback_delay = SCREEN_WRITE_BACK_DELAY;
	vidcvideo_queue_dc_change(dc, WSDISPLAY_WB_COUNTER);
}


static void vv_putchar(void *id, int row, int col, u_int uc, long attr)
{
    	struct rasops_info *ri = id;
	struct vcons_screen *scr = ri->ri_hw;
	struct fb_devconfig *dc = (struct fb_devconfig *) (scr->scr_cookie);

	/* just delegate */
	dc->orig_ri_ops.putchar(id, row, col, uc, attr);

	/* delay the write back operation of the screen area */
	dc->dc_writeback_delay = SCREEN_WRITE_BACK_DELAY;
	vidcvideo_queue_dc_change(dc, WSDISPLAY_WB_COUNTER);
}