Home | History | Annotate | Line # | Download | only in ic
vga_raster.c revision 1.45
      1 /*	$NetBSD: vga_raster.c,v 1.45 2019/11/10 21:16:35 chs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001, 2002 Bang Jun-Young
      5  * Copyright (c) 2004 Julio M. Merino Vidal
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     33  * All rights reserved.
     34  *
     35  * Author: Chris G. Demetriou
     36  *
     37  * Permission to use, copy, modify and distribute this software and
     38  * its documentation is hereby granted, provided that both the copyright
     39  * notice and this permission notice appear in all copies of the
     40  * software, derivative works or modified versions, and any portions
     41  * thereof, and that both notices appear in supporting documentation.
     42  *
     43  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     44  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     45  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     46  *
     47  * Carnegie Mellon requests users of this software to return to
     48  *
     49  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     50  *  School of Computer Science
     51  *  Carnegie Mellon University
     52  *  Pittsburgh PA 15213-3890
     53  *
     54  * any improvements or extensions that they make and grant Carnegie the
     55  * rights to redistribute these changes.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __KERNEL_RCSID(0, "$NetBSD: vga_raster.c,v 1.45 2019/11/10 21:16:35 chs Exp $");
     60 
     61 #include "opt_vga.h"
     62 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */
     63 
     64 #include <sys/param.h>
     65 #include <sys/systm.h>
     66 #include <sys/callout.h>
     67 #include <sys/kernel.h>
     68 #include <sys/device.h>
     69 #include <sys/malloc.h>
     70 #include <sys/queue.h>
     71 #include <sys/bus.h>
     72 
     73 #include <dev/ic/mc6845reg.h>
     74 #include <dev/ic/pcdisplayvar.h>
     75 #include <dev/ic/vgareg.h>
     76 #include <dev/ic/vgavar.h>
     77 #include <dev/videomode/videomode.h>
     78 
     79 #include <dev/wscons/wsdisplayvar.h>
     80 #include <dev/wscons/wsconsio.h>
     81 #include <dev/wsfont/wsfont.h>
     82 
     83 #include <dev/ic/pcdisplay.h>
     84 
     85 int vga_no_builtinfont = 0;
     86 
     87 u_int8_t builtinfont_data[256 * 16];
     88 
     89 struct wsdisplay_font builtinfont = {
     90 	"builtin",			/* typeface name */
     91 	0,				/* firstchar */
     92 	256,				/* numchars */
     93 	WSDISPLAY_FONTENC_IBM,		/* encoding */
     94 	8,				/* width */
     95 	16,				/* height */
     96 	1,				/* stride */
     97 	WSDISPLAY_FONTORDER_L2R,	/* bit order */
     98 	WSDISPLAY_FONTORDER_L2R,	/* byte order */
     99 	builtinfont_data		/* data */
    100 };
    101 
    102 struct vga_scrmem {
    103 	u_int16_t ch;
    104 	u_int8_t attr;
    105 	u_int8_t second;	/* XXXBJY should be u_int8_t len; */
    106 	u_int8_t enc;
    107 };
    108 
    109 #ifdef VGA_CONSOLE_SCREENTYPE
    110 #define VGA_SCRMEM_SIZE		(80 * 30)
    111 #else
    112 #define VGA_SCRMEM_SIZE		(80 * 25)
    113 #endif
    114 
    115 struct vga_scrmem boot_scrmem[VGA_SCRMEM_SIZE];
    116 
    117 struct vga_raster_font {
    118 	LIST_ENTRY(vga_raster_font) next;
    119 	struct wsdisplay_font *font;
    120 };
    121 
    122 struct vgascreen {
    123 	LIST_ENTRY(vgascreen) next;
    124 	struct vga_config *cfg;
    125 	struct vga_handle *hdl;
    126 	const struct wsscreen_descr *type;
    127 
    128 	int active;
    129 	struct vga_scrmem *mem;
    130 	int encoding;
    131 
    132 	int dispoffset;
    133 	int mindispoffset;
    134 	int maxdispoffset;
    135 
    136 	int cursoron;			/* Is cursor displayed? */
    137 	int cursorcol;			/* Current cursor column */
    138 	int cursorrow;			/* Current cursor row */
    139 	struct vga_scrmem cursortmp;
    140 	int cursorstride;
    141 
    142 	LIST_HEAD(, vga_raster_font) fontset;
    143 };
    144 
    145 struct vga_moderegs {
    146 	u_int8_t miscout;		/* Misc. output */
    147 	u_int8_t crtc[MC6845_NREGS];	/* CRTC controller */
    148 	u_int8_t atc[VGA_ATC_NREGS];	/* Attribute controller */
    149 	u_int8_t ts[VGA_TS_NREGS];	/* Time sequencer */
    150 	u_int8_t gdc[VGA_GDC_NREGS];	/* Graphics display controller */
    151 };
    152 
    153 static int vgaconsole, vga_console_type, vga_console_attached;
    154 static struct vgascreen vga_console_screen;
    155 static struct vga_config vga_console_vc;
    156 static struct vga_raster_font vga_console_fontset_ascii;
    157 static struct videomode vga_console_modes[2] = {
    158 	/* 640x400 for 80x25, 80x40 and 80x50 modes */
    159 	{
    160 		25175, 640, 664, 760, 800, 400, 409, 411, 450, 0, NULL,
    161 	},
    162 	/* 640x480 for 80x30 mode */
    163 	{
    164 		25175, 640, 664, 760, 800, 480, 491, 493, 525, 0, NULL,
    165 	}
    166 };
    167 
    168 static void vga_raster_init(struct vga_config *, bus_space_tag_t,
    169 		bus_space_tag_t);
    170 static void vga_raster_init_screen(struct vga_config *, struct vgascreen *,
    171 		const struct wsscreen_descr *, int, long *);
    172 static void vga_raster_setup_font(struct vga_config *, struct vgascreen *);
    173 static void vga_setup_regs(struct videomode *, struct vga_moderegs *);
    174 static void vga_set_mode(struct vga_handle *, struct vga_moderegs *);
    175 static void vga_restore_screen(struct vgascreen *,
    176 		const struct wsscreen_descr *, struct vga_scrmem *);
    177 static void vga_raster_cursor_init(struct vgascreen *, int);
    178 static void _vga_raster_putchar(void *, int, int, u_int, long,
    179 		struct vga_raster_font *);
    180 
    181 static void vga_raster_cursor(void *, int, int, int);
    182 static int  vga_raster_mapchar(void *, int, u_int *);
    183 static void vga_raster_putchar(void *, int, int, u_int, long);
    184 static void vga_raster_copycols(void *, int, int, int, int);
    185 static void vga_raster_erasecols(void *, int, int, int, long);
    186 static void vga_raster_copyrows(void *, int, int, int);
    187 static void vga_raster_eraserows(void *, int, int, long);
    188 static int  vga_raster_allocattr(void *, int, int, int, long *);
    189 #ifdef WSDISPLAY_CUSTOM_OUTPUT
    190 static void vga_raster_replaceattr(void *, long, long);
    191 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
    192 
    193 const struct wsdisplay_emulops vga_raster_emulops = {
    194 	vga_raster_cursor,
    195 	vga_raster_mapchar,
    196 	vga_raster_putchar,
    197 	vga_raster_copycols,
    198 	vga_raster_erasecols,
    199 	vga_raster_copyrows,
    200 	vga_raster_eraserows,
    201 	vga_raster_allocattr,
    202 #ifdef WSDISPLAY_CUSTOM_OUTPUT
    203 	vga_raster_replaceattr,
    204 #else /* WSDISPLAY_CUSTOM_OUTPUT */
    205 	NULL,
    206 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
    207 };
    208 
    209 /*
    210  * translate WS(=ANSI) color codes to standard pc ones
    211  */
    212 static const unsigned char fgansitopc[] = {
    213 #ifdef __alpha__
    214 	/*
    215 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
    216 	 * XXX We should probably not bother with this
    217 	 * XXX (reinitialize the palette registers).
    218 	 */
    219 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
    220 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
    221 #else
    222 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
    223 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
    224 #endif
    225 }, bgansitopc[] = {
    226 #ifdef __alpha__
    227 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
    228 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
    229 #else
    230 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
    231 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
    232 #endif
    233 };
    234 
    235 const struct wsscreen_descr vga_25lscreen = {
    236 	"80x25", 80, 25,
    237 	&vga_raster_emulops,
    238 	8, 16,
    239 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    240 	&vga_console_modes[0]
    241 }, vga_25lscreen_mono = {
    242 	"80x25", 80, 25,
    243 	&vga_raster_emulops,
    244 	8, 16,
    245 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    246 	&vga_console_modes[0]
    247 }, vga_30lscreen = {
    248 	"80x30", 80, 30,
    249 	&vga_raster_emulops,
    250 	8, 16,
    251 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    252 	&vga_console_modes[1]
    253 }, vga_30lscreen_mono = {
    254 	"80x30", 80, 30,
    255 	&vga_raster_emulops,
    256 	8, 16,
    257 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    258 	&vga_console_modes[1]
    259 }, vga_40lscreen = {
    260 	"80x40", 80, 40,
    261 	&vga_raster_emulops,
    262 	8, 10,
    263 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    264 	&vga_console_modes[0]
    265 }, vga_40lscreen_mono = {
    266 	"80x40", 80, 40,
    267 	&vga_raster_emulops,
    268 	8, 10,
    269 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    270 	&vga_console_modes[0]
    271 }, vga_50lscreen = {
    272 	"80x50", 80, 50,
    273 	&vga_raster_emulops,
    274 	8, 8,
    275 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
    276 	&vga_console_modes[0]
    277 }, vga_50lscreen_mono = {
    278 	"80x50", 80, 50,
    279 	&vga_raster_emulops,
    280 	8, 8,
    281 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
    282 	&vga_console_modes[0]
    283 };
    284 
    285 const struct wsscreen_descr *_vga_scrlist[] = {
    286 	&vga_25lscreen,
    287 	&vga_30lscreen,
    288 	&vga_40lscreen,
    289 	&vga_50lscreen,
    290 }, *_vga_scrlist_mono[] = {
    291 	&vga_25lscreen_mono,
    292 	&vga_30lscreen_mono,
    293 	&vga_40lscreen_mono,
    294 	&vga_50lscreen_mono,
    295 };
    296 
    297 const struct wsscreen_list vga_screenlist = {
    298 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
    299 	_vga_scrlist
    300 }, vga_screenlist_mono = {
    301 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
    302 	_vga_scrlist_mono
    303 };
    304 
    305 static int	vga_raster_ioctl(void *, void *, u_long, void *, int,
    306 		    struct lwp *);
    307 static paddr_t	vga_raster_mmap(void *, void *, off_t, int);
    308 static int	vga_raster_alloc_screen(void *, const struct wsscreen_descr *,
    309 		    void **, int *, int *, long *);
    310 static void	vga_raster_free_screen(void *, void *);
    311 static int	vga_raster_show_screen(void *, void *, int,
    312 		    void (*)(void *, int, int), void *);
    313 static int	vga_raster_load_font(void *, void *, struct wsdisplay_font *);
    314 
    315 static void 	vga_switch_screen(struct vga_config *);
    316 static void 	vga_raster_setscreentype(struct vga_config *,
    317 		    const struct wsscreen_descr *);
    318 
    319 const struct wsdisplay_accessops vga_raster_accessops = {
    320 	vga_raster_ioctl,
    321 	vga_raster_mmap,
    322 	vga_raster_alloc_screen,
    323 	vga_raster_free_screen,
    324 	vga_raster_show_screen,
    325 	vga_raster_load_font,
    326 	NULL,	/* pollc */
    327 	NULL,	/* scroll */
    328 };
    329 
    330 int
    331 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
    332 {
    333 	long defattr;
    334 	const struct wsscreen_descr *scr;
    335 #ifdef VGA_CONSOLE_SCREENTYPE
    336 	const char *typestr = NULL;
    337 #endif
    338 
    339 	if (check && !vga_common_probe(iot, memt))
    340 		return (ENXIO);
    341 
    342 	/* set up bus-independent VGA configuration */
    343 	vga_raster_init(&vga_console_vc, iot, memt);
    344 #ifdef VGA_CONSOLE_SCREENTYPE
    345 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    346 	    &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
    347 	if (!scr)
    348 		/* Invalid screen type, continue with the default mode. */
    349 		typestr = "80x25";
    350 	else if (scr->nrows > 30)
    351 		/* Unsupported screen type, try 80x30. */
    352 		typestr = "80x30";
    353 	if (typestr)
    354 		scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    355 		    &vga_screenlist_mono : &vga_screenlist, typestr);
    356 	if (scr != vga_console_vc.currenttype)
    357 		vga_console_vc.currenttype = scr;
    358 #else
    359 	scr = vga_console_vc.currenttype;
    360 #endif
    361 	vga_raster_init_screen(&vga_console_vc, &vga_console_screen, scr, 1,
    362 	    &defattr);
    363 
    364 	vga_console_screen.active = 1;
    365 	vga_console_vc.active = &vga_console_screen;
    366 
    367 	wsdisplay_cnattach(scr, &vga_console_screen,
    368 	    vga_console_screen.cursorcol, vga_console_screen.cursorrow,
    369 	    defattr);
    370 
    371 	vgaconsole = 1;
    372 	vga_console_type = type;
    373 	return (0);
    374 }
    375 
    376 static void
    377 vga_raster_init(struct vga_config *vc, bus_space_tag_t iot,
    378     bus_space_tag_t memt)
    379 {
    380 	struct vga_handle *vh = &vc->hdl;
    381 	u_int8_t mor;
    382 	struct vga_raster_font *vf;
    383 
    384 	vh->vh_iot = iot;
    385 	vh->vh_memt = memt;
    386 
    387 	if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    388 		panic("vga_raster_init: couldn't map vga io");
    389 
    390 	/* read "misc output register" */
    391 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAR);
    392 	vh->vh_mono = !(mor & 1);
    393 
    394 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    395 	    &vh->vh_ioh_6845))
    396 		panic("vga_raster_init: couldn't map 6845 io");
    397 
    398 	if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    399 		panic("vga_raster_init: couldn't map memory");
    400 
    401 	if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
    402 	    &vh->vh_memh))
    403 		panic("vga_raster_init: mem subrange failed");
    404 
    405 	vc->nscreens = 0;
    406 	LIST_INIT(&vc->screens);
    407 	vc->active = NULL;
    408 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
    409 	callout_init(&vc->vc_switch_callout, 0);
    410 
    411 	wsfont_init();
    412 	vc->nfonts = 1;
    413 	LIST_INIT(&vc->vc_fontlist);
    414 	vf = &vga_console_fontset_ascii;
    415 	if (vga_no_builtinfont) {
    416 		struct wsdisplay_font *wf;
    417 		int cookie;
    418 
    419 		/* prefer 8x16 pixel font */
    420 		cookie = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
    421 		    0, WSFONT_FIND_BITMAP);
    422 		if (cookie == -1)
    423 			cookie = wsfont_find(NULL, 0, 0, 0,
    424 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
    425 			    WSFONT_FIND_BITMAP);
    426 		if (cookie == -1 || wsfont_lock(cookie, &wf))
    427 			panic("vga_raster_init: can't load console font");
    428 		vf->font = wf;
    429 	} else {
    430 		vga_load_builtinfont(vh, builtinfont_data, 0, 256);
    431 		vf->font = &builtinfont;
    432 	}
    433 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
    434 }
    435 
    436 static void
    437 vga_raster_init_screen(struct vga_config *vc, struct vgascreen *scr,
    438     const struct wsscreen_descr *type, int existing, long *attrp)
    439 {
    440 	int cpos;
    441 	int res;
    442 	struct vga_handle *vh;
    443 
    444 	scr->cfg = vc;
    445 	scr->hdl = &vc->hdl;
    446 	scr->type = type;
    447 	scr->mindispoffset = 0;
    448 	scr->maxdispoffset = scr->dispoffset +
    449 	    type->nrows * type->ncols * type->fontheight;
    450 	vh = &vc->hdl;
    451 
    452 	LIST_INIT(&scr->fontset);
    453 	vga_raster_setup_font(vc, scr);
    454 
    455 	if (existing) {
    456 		int i;
    457 
    458 		cpos = vga_6845_read(vh, cursorh) << 8;
    459 		cpos |= vga_6845_read(vh, cursorl);
    460 
    461 		/* make sure we have a valid cursor position */
    462 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    463 			cpos = 0;
    464 
    465 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
    466 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
    467 
    468 		/* make sure we have a valid memory offset */
    469 		if (scr->dispoffset < scr->mindispoffset ||
    470 		    scr->dispoffset > scr->maxdispoffset)
    471 			scr->dispoffset = scr->mindispoffset;
    472 
    473 		scr->mem = boot_scrmem;
    474 		scr->active = 1;
    475 
    476 		/* Save the current screen to memory. XXXBJY assume 80x25 */
    477 		for (i = 0; i < 80 * 25; i++) {
    478 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
    479 			    vh->vh_allmemh, 0x18000 + i * 2);
    480 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
    481 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
    482 			scr->mem[i].enc = scr->encoding;
    483 		}
    484 
    485 		vga_raster_setscreentype(vc, type);
    486 
    487 		/* Clear the entire screen. */
    488 		vga_gdc_write(vh, mode, 0x02);
    489 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
    490 		    0x4000);
    491 
    492 		vga_restore_screen(scr, type, scr->mem);
    493 	} else {
    494 		cpos = 0;
    495 		scr->dispoffset = scr->mindispoffset;
    496 		scr->mem = NULL;
    497 		scr->active = 0;
    498 	}
    499 
    500 	scr->cursorrow = cpos / type->ncols;
    501 	scr->cursorcol = cpos % type->ncols;
    502 	vga_raster_cursor_init(scr, existing);
    503 
    504 #ifdef __alpha__
    505 	if (!vc->hdl.vh_mono)
    506 		/*
    507 		 * DEC firmware uses a blue background.
    508 		 */
    509 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
    510 		    WSATTR_WSCOLORS, attrp);
    511 	else
    512 #endif
    513 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
    514 #ifdef DIAGNOSTIC
    515 	if (res)
    516 		panic("vga_raster_init_screen: attribute botch");
    517 #endif
    518 
    519 	vc->nscreens++;
    520 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    521 }
    522 
    523 void
    524 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
    525     bus_space_tag_t memt, int type, int quirks,
    526     const struct vga_funcs *vf)
    527 {
    528 	int console;
    529 	struct vga_config *vc;
    530 	struct wsemuldisplaydev_attach_args aa;
    531 
    532 	console = vga_is_console(iot, type);
    533 
    534 	if (console) {
    535 		vc = &vga_console_vc;
    536 		vga_console_attached = 1;
    537 	} else {
    538 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
    539 		vga_raster_init(vc, iot, memt);
    540 	}
    541 
    542 	vc->vc_type = type;
    543 	vc->vc_funcs = vf;
    544 
    545 	sc->sc_vc = vc;
    546 	vc->softc = sc;
    547 
    548 	aa.console = console;
    549 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    550 	aa.accessops = &vga_raster_accessops;
    551 	aa.accesscookie = vc;
    552 
    553 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    554 }
    555 
    556 int
    557 vga_cndetach(void)
    558 {
    559 	struct vga_config *vc;
    560 	struct vga_handle *vh;
    561 
    562 	vc = &vga_console_vc;
    563 	vh = &vc->hdl;
    564 
    565 	if (vgaconsole) {
    566 		wsdisplay_cndetach();
    567 
    568 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_vga, 0x10);
    569 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_6845, 0x10);
    570 		bus_space_unmap(vh->vh_memt, vh->vh_allmemh, 0x20000);
    571 
    572 		vga_console_attached = 0;
    573 		vgaconsole = 0;
    574 
    575 		return 1;
    576 	}
    577 
    578 	return 0;
    579 }
    580 
    581 int
    582 vga_is_console(bus_space_tag_t iot, int type)
    583 {
    584 	if (vgaconsole &&
    585 	    !vga_console_attached &&
    586 	    iot == vga_console_vc.hdl.vh_iot &&
    587 	    (vga_console_type == -1 || (type == vga_console_type)))
    588 		return (1);
    589 	return (0);
    590 }
    591 
    592 static int
    593 vga_get_video(struct vga_config *vc)
    594 {
    595 
    596 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
    597 }
    598 
    599 static void
    600 vga_set_video(struct vga_config *vc, int state)
    601 {
    602 	int val;
    603 
    604 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    605 	if (state) {					/* unblank screen */
    606 		val = vga_ts_read(&vc->hdl, mode);
    607 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
    608 #ifndef VGA_NO_VBLANK
    609 		val = vga_6845_read(&vc->hdl, mode);
    610 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    611 #endif
    612 	} else {					/* blank screen */
    613 		val = vga_ts_read(&vc->hdl, mode);
    614 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
    615 #ifndef VGA_NO_VBLANK
    616 		val = vga_6845_read(&vc->hdl, mode);
    617 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    618 #endif
    619 	}
    620 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    621 }
    622 
    623 int
    624 vga_raster_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    625 	struct lwp *l)
    626 {
    627 	struct vga_config *vc = v;
    628 	const struct vga_funcs *vf = vc->vc_funcs;
    629 
    630 	switch (cmd) {
    631 	case WSDISPLAYIO_GTYPE:
    632 		*(int *)data = vc->vc_type;
    633 		return 0;
    634 
    635 	case WSDISPLAYIO_GINFO: {
    636 		struct wsdisplay_fbinfo *fbi = data;
    637 		const struct wsscreen_descr *wd = vc->currenttype;
    638 		const struct videomode *vm = wd->modecookie;
    639 		fbi->width = vm->hdisplay;
    640 		fbi->height = vm->vdisplay;
    641 		fbi->depth = 24;	/* xxx: ? */
    642 		fbi->cmsize = 256;	/* xxx: from palette */
    643 		return 0;
    644 	}
    645 
    646 	case WSDISPLAYIO_GVIDEO:
    647 		*(int *)data = (vga_get_video(vc) ?
    648 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
    649 		return 0;
    650 
    651 	case WSDISPLAYIO_SVIDEO:
    652 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    653 		return 0;
    654 
    655 	case WSDISPLAYIO_GETCMAP:
    656 	case WSDISPLAYIO_PUTCMAP:
    657 	case WSDISPLAYIO_GCURPOS:
    658 	case WSDISPLAYIO_SCURPOS:
    659 	case WSDISPLAYIO_GCURMAX:
    660 	case WSDISPLAYIO_GCURSOR:
    661 	case WSDISPLAYIO_SCURSOR:
    662 #ifdef DIAGNOSTIC
    663 		printf("%s: 0x%lx unsupported\n", __func__, cmd);
    664 #endif
    665 		/* NONE of these operations are by the generic VGA driver. */
    666 		return EPASSTHROUGH;
    667 	}
    668 
    669 	if (vc->vc_funcs == NULL) {
    670 #ifdef DIAGNOSTIC
    671 		printf("%s: no vc_funcs\n", __func__);
    672 #endif
    673 		return EPASSTHROUGH;
    674 	}
    675 
    676 	if (vf->vf_ioctl == NULL) {
    677 #ifdef DIAGNOSTIC
    678 		printf("%s: no vf_ioctl\n", __func__);
    679 #endif
    680 		return EPASSTHROUGH;
    681 	}
    682 
    683 	return ((*vf->vf_ioctl)(v, cmd, data, flag, l));
    684 }
    685 
    686 static paddr_t
    687 vga_raster_mmap(void *v, void *vs, off_t offset, int prot)
    688 {
    689 	struct vga_config *vc = v;
    690 	const struct vga_funcs *vf = vc->vc_funcs;
    691 
    692 	if (vc->vc_funcs == NULL)
    693 		return (-1);
    694 
    695 	if (vf->vf_mmap == NULL)
    696 		return (-1);
    697 
    698 	return ((*vf->vf_mmap)(v, offset, prot));
    699 }
    700 
    701 static int
    702 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
    703     void **cookiep, int *curxp, int *curyp, long *defattrp)
    704 {
    705 	struct vga_config *vc = v;
    706 	struct vgascreen *scr;
    707 
    708 	if (vc->nscreens == 1) {
    709 		vc->screens.lh_first->mem = boot_scrmem;
    710 	}
    711 
    712 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    713 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    714 
    715 	if (vc->nscreens == 1) {
    716 		scr->active = 1;
    717 		vc->active = scr;
    718 		vc->currenttype = type;
    719 	} else {
    720 		scr->mem = malloc(sizeof(struct vga_scrmem) *
    721 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
    722 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
    723 	}
    724 
    725 	*cookiep = scr;
    726 	*curxp = scr->cursorcol;
    727 	*curyp = scr->cursorrow;
    728 
    729 	return (0);
    730 }
    731 
    732 static void
    733 vga_raster_free_screen(void *v, void *cookie)
    734 {
    735 	struct vgascreen *vs = cookie;
    736 	struct vga_config *vc = vs->cfg;
    737 
    738 	LIST_REMOVE(vs, next);
    739 	vc->nscreens--;
    740 	if (vs != &vga_console_screen)
    741 		free(vs, M_DEVBUF);
    742 	else
    743 		panic("vga_raster_free_screen: console");
    744 
    745 	if (vc->active == vs)
    746 		vc->active = 0;
    747 }
    748 
    749 static int
    750 vga_raster_show_screen(void *v, void *cookie, int waitok,
    751     void (*cb)(void *, int, int), void *cbarg)
    752 {
    753 	struct vgascreen *scr = cookie, *oldscr;
    754 	struct vga_config *vc = scr->cfg;
    755 
    756 	oldscr = vc->active; /* can be NULL! */
    757 	if (scr == oldscr) {
    758 		return (0);
    759 	}
    760 
    761 	vc->wantedscreen = cookie;
    762 	vc->switchcb = cb;
    763 	vc->switchcbarg = cbarg;
    764 	if (cb) {
    765 		callout_reset(&vc->vc_switch_callout, 0,
    766 		    (void(*)(void *))vga_switch_screen, vc);
    767 		return (EAGAIN);
    768 	}
    769 
    770 	vga_switch_screen(vc);
    771 	return (0);
    772 }
    773 
    774 static void
    775 vga_switch_screen(struct vga_config *vc)
    776 {
    777 	struct vgascreen *scr, *oldscr;
    778 	struct vga_handle *vh = &vc->hdl;
    779 	const struct wsscreen_descr *type;
    780 
    781 	scr = vc->wantedscreen;
    782 	if (!scr) {
    783 		printf("vga_switch_screen: disappeared\n");
    784 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    785 		return;
    786 	}
    787 	type = scr->type;
    788 	oldscr = vc->active; /* can be NULL! */
    789 #ifdef DIAGNOSTIC
    790 	if (oldscr) {
    791 		if (!oldscr->active)
    792 			panic("vga_raster_show_screen: not active");
    793 		if (oldscr->type != vc->currenttype)
    794 			panic("vga_raster_show_screen: bad type");
    795 	}
    796 #endif
    797 	if (scr == oldscr) {
    798 		return;
    799 	}
    800 #ifdef DIAGNOSTIC
    801 	if (scr->active)
    802 		panic("vga_raster_show_screen: active");
    803 #endif
    804 
    805 	if (oldscr)
    806 		oldscr->active = 0;
    807 
    808 	if (vc->currenttype != type) {
    809 		vga_raster_setscreentype(vc, type);
    810 		vc->currenttype = type;
    811 	}
    812 
    813 	scr->dispoffset = scr->mindispoffset;
    814 
    815 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
    816 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
    817 		vga_6845_write(vh, startadrl, scr->dispoffset);
    818 	}
    819 
    820 	/* Clear the entire screen. */
    821 	vga_gdc_write(vh, mode, 0x02);
    822 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
    823 
    824 	scr->active = 1;
    825 	vga_restore_screen(scr, type, scr->mem);
    826 
    827 	vc->active = scr;
    828 
    829 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
    830 
    831 	vc->wantedscreen = 0;
    832 	if (vc->switchcb)
    833 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    834 }
    835 
    836 static int
    837 vga_raster_load_font(void *v, void *id,
    838     struct wsdisplay_font *data)
    839 {
    840 	/* XXX */
    841 	printf("vga_raster_load_font: called\n");
    842 
    843 	return (0);
    844 }
    845 
    846 static void
    847 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
    848 {
    849 	struct vga_raster_font *vf;
    850 	struct wsdisplay_font *wf;
    851 	int cookie;
    852 
    853 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
    854 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0,
    855 		    WSFONT_FIND_BITMAP)) {
    856 			scr->encoding = vf->font->encoding;
    857 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
    858 			return;
    859 		}
    860 	}
    861 
    862 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
    863 	    WSDISPLAY_FONTORDER_L2R, 0, WSFONT_FIND_BITMAP);
    864 	if (cookie == -1)
    865 		return;
    866 
    867 	if (wsfont_lock(cookie, &wf))
    868 		return;
    869 
    870 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_WAITOK);
    871 	vf->font = wf;
    872 	scr->encoding = vf->font->encoding;
    873 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
    874 }
    875 
    876 static void
    877 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
    878 {
    879 	int i;
    880 	int depth = 4;			/* XXXBJY hardcoded for now */
    881 	const u_int8_t palette[] = {
    882 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
    883 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
    884 	};
    885 
    886 	/*
    887 	 * Compute hsync and vsync polarity.
    888 	 */
    889 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
    890 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
    891 	    	regs->miscout = 0x23;
    892 		if (mode->flags & VID_NHSYNC)
    893 			regs->miscout |= 0x40;
    894 		if (mode->flags & VID_NVSYNC)
    895 			regs->miscout |= 0x80;
    896 	} else {
    897 		if (mode->flags & VID_DBLSCAN)
    898 			mode->vdisplay *= 2;
    899 		if (mode->vdisplay < 400)
    900 			regs->miscout = 0xa3;
    901 		else if (mode->vdisplay < 480)
    902 			regs->miscout = 0x63;
    903 		else if (mode->vdisplay < 768)
    904 			regs->miscout = 0xe3;
    905 		else
    906 			regs->miscout = 0x23;
    907 	}
    908 
    909 	/*
    910 	 * Time sequencer
    911 	 */
    912 	if (depth == 4)
    913 		regs->ts[0] = 0x02;
    914 	else
    915 		regs->ts[0] = 0x00;
    916 	if (mode->flags & VID_CLKDIV2)
    917 		regs->ts[1] = 0x09;
    918 	else
    919 		regs->ts[1] = 0x01;
    920 	regs->ts[2] = 0x0f;
    921 	regs->ts[3] = 0x00;
    922 	if (depth < 8)
    923 		regs->ts[4] = 0x06;
    924 	else
    925 		regs->ts[4] = 0x0e;
    926 
    927 	/*
    928 	 * CRTC controller
    929 	 */
    930 	regs->crtc[0] = (mode->htotal >> 3) - 5;
    931 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
    932 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
    933 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
    934 	regs->crtc[4] = mode->hsync_start >> 3;
    935 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
    936 	    | (((mode->hsync_end >> 3)) & 0x1f);
    937 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
    938 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
    939 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
    940 	    | ((mode->vsync_start & 0x100) >> 6)
    941 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
    942 	    | 0x10
    943 	    | (((mode->vtotal - 2) & 0x200) >> 4)
    944 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
    945 	    | ((mode->vsync_start & 0x200) >> 2);
    946 	regs->crtc[8] = 0x00;
    947 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
    948 	if (mode->flags & VID_DBLSCAN)
    949 		regs->crtc[9] |= 0x80;
    950 	regs->crtc[10] = 0x00;
    951 	regs->crtc[11] = 0x00;
    952 	regs->crtc[12] = 0x00;
    953 	regs->crtc[13] = 0x00;
    954 	regs->crtc[14] = 0x00;
    955 	regs->crtc[15] = 0x00;
    956 	regs->crtc[16] = mode->vsync_start & 0xff;
    957 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
    958 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
    959 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
    960 	regs->crtc[20] = 0x00;
    961 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
    962 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
    963 	if (depth < 8)
    964 		regs->crtc[23] = 0xe3;
    965 	else
    966 		regs->crtc[23] = 0xc3;
    967 	regs->crtc[24] = 0xff;
    968 
    969 	/*
    970 	 * Graphics display controller
    971 	 */
    972 	regs->gdc[0] = 0x00;
    973 	regs->gdc[1] = 0x00;
    974 	regs->gdc[2] = 0x00;
    975 	regs->gdc[3] = 0x00;
    976 	regs->gdc[4] = 0x00;
    977 	if (depth == 4)
    978 		regs->gdc[5] = 0x02;
    979 	else
    980 		regs->gdc[5] = 0x40;
    981 	regs->gdc[6] = 0x01;
    982 	regs->gdc[7] = 0x0f;
    983 	regs->gdc[8] = 0xff;
    984 
    985 	/*
    986 	 * Attribute controller
    987 	 */
    988 	/* Set palette registers. */
    989 	for (i = 0; i < 16; i++)
    990 		regs->atc[i] = palette[i];
    991 	if (depth == 4)
    992 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
    993 	else
    994 		regs->atc[16] = 0x41;
    995 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
    996 	regs->atc[18] = 0x0f;
    997 	regs->atc[19] = 0x00;
    998 	regs->atc[20] = 0x00;
    999 }
   1000 
   1001 static void
   1002 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
   1003 {
   1004 	int i;
   1005 
   1006 	/* Disable display. */
   1007 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
   1008 
   1009 	/* Write misc output register. */
   1010 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
   1011 	    regs->miscout);
   1012 
   1013 	/* Set synchronous reset. */
   1014 	vga_ts_write(vh, syncreset, 0x01);
   1015 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
   1016 	for (i = 2; i < VGA_TS_NREGS; i++)
   1017 		_vga_ts_write(vh, i, regs->ts[i]);
   1018 	/* Clear synchronous reset. */
   1019 	vga_ts_write(vh, syncreset, 0x03);
   1020 
   1021 	/* Unprotect CRTC registers 0-7. */
   1022 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
   1023 	/* Write CRTC registers. */
   1024 	for (i = 0; i < MC6845_NREGS; i++)
   1025 		_vga_6845_write(vh, i, regs->crtc[i]);
   1026 
   1027 	/* Write graphics display registers. */
   1028 	for (i = 0; i < VGA_GDC_NREGS; i++)
   1029 		_vga_gdc_write(vh, i, regs->gdc[i]);
   1030 
   1031 	/* Write attribute controller registers. */
   1032 	for (i = 0; i < VGA_ATC_NREGS; i++)
   1033 		_vga_attr_write(vh, i, regs->atc[i]);
   1034 
   1035 	/* Enable display. */
   1036 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
   1037 }
   1038 
   1039 static void
   1040 vga_raster_cursor_init(struct vgascreen *scr, int existing)
   1041 {
   1042 	int off;
   1043 
   1044 	if (existing) {
   1045 		/*
   1046 		 * This is the first screen. At this point, scr->active is
   1047 		 * false, so we can't use vga_raster_cursor() to do this.
   1048 		 */
   1049 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
   1050 		    scr->dispoffset / 8;
   1051 
   1052 		scr->cursortmp = scr->mem[off];
   1053 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
   1054 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
   1055 	} else {
   1056 		scr->cursortmp.ch = 0;
   1057 		scr->cursortmp.attr = 0;
   1058 		scr->cursortmp.second = 0;
   1059 		scr->cursortmp.enc = scr->encoding;
   1060 	}
   1061 
   1062 	scr->cursoron = 1;
   1063 }
   1064 
   1065 static void
   1066 vga_raster_cursor(void *id, int on, int row, int col)
   1067 {
   1068 	struct vgascreen *scr = id;
   1069 	int off, tmp;
   1070 
   1071 	/* Remove old cursor image */
   1072 	if (scr->cursoron) {
   1073 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1074 		if (scr->active) {
   1075 			tmp = scr->encoding;
   1076 			scr->encoding = scr->cursortmp.enc;
   1077 			if (scr->cursortmp.second)
   1078 				vga_raster_putchar(id, scr->cursorrow,
   1079 				    scr->cursorcol - 1, scr->cursortmp.ch,
   1080 				    scr->cursortmp.attr);
   1081 			else
   1082 				vga_raster_putchar(id, scr->cursorrow,
   1083 				    scr->cursorcol, scr->cursortmp.ch,
   1084 				    scr->cursortmp.attr);
   1085 			scr->encoding = tmp;
   1086 		}
   1087 	}
   1088 
   1089 	scr->cursorrow = row;
   1090 	scr->cursorcol = col;
   1091 
   1092 	if ((scr->cursoron = on) == 0)
   1093 		return;
   1094 
   1095 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1096 	scr->cursortmp = scr->mem[off];
   1097 	if (scr->active) {
   1098 		tmp = scr->encoding;
   1099 		scr->encoding = scr->cursortmp.enc;
   1100 		if (scr->cursortmp.second)
   1101 			vga_raster_putchar(id, scr->cursorrow,
   1102 			    scr->cursorcol - 1, scr->cursortmp.ch,
   1103 			    scr->cursortmp.attr ^ 0x77);
   1104 		else
   1105 			vga_raster_putchar(id, scr->cursorrow,
   1106 			    scr->cursorcol, scr->cursortmp.ch,
   1107 			    scr->cursortmp.attr ^ 0x77);
   1108 		scr->encoding = tmp;
   1109 	}
   1110 }
   1111 
   1112 static int
   1113 vga_raster_mapchar(void *id, int uni, u_int *index)
   1114 {
   1115 	struct vgascreen *scr = id;
   1116 
   1117 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
   1118 		return pcdisplay_mapchar(id, uni, index);
   1119 	else {
   1120 		*index = uni;
   1121 		return 5;
   1122 	}
   1123 }
   1124 
   1125 static void
   1126 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
   1127 {
   1128 	struct vgascreen *scr = id;
   1129 	size_t off;
   1130 	struct vga_raster_font *fs;
   1131 	u_int tmp_ch;
   1132 
   1133 	off = row * scr->type->ncols + col;
   1134 
   1135 	if (__predict_false(off >= (scr->type->ncols * scr->type->nrows)))
   1136 		return;
   1137 
   1138 	LIST_FOREACH(fs, &scr->fontset, next) {
   1139 		if ((scr->encoding == fs->font->encoding) &&
   1140 		    (c >= fs->font->firstchar) &&
   1141 		    (c < fs->font->firstchar + fs->font->numchars) &&
   1142 		    (scr->type->fontheight == fs->font->fontheight)) {
   1143 			if (scr->active) {
   1144 				tmp_ch = c - fs->font->firstchar;
   1145 				_vga_raster_putchar(scr, row, col, tmp_ch,
   1146 				    attr, fs);
   1147 			}
   1148 
   1149 			scr->mem[off].ch = c;
   1150 			scr->mem[off].attr = attr;
   1151 			scr->mem[off].second = 0;
   1152 			scr->mem[off].enc = fs->font->encoding;
   1153 
   1154 			if (fs->font->stride == 2) {
   1155 				scr->mem[off + 1].ch = c;
   1156 				scr->mem[off + 1].attr = attr;
   1157 				scr->mem[off + 1].second = 1;
   1158 				scr->mem[off + 1].enc = fs->font->encoding;
   1159 			}
   1160 
   1161 			return;
   1162 		}
   1163 	}
   1164 
   1165 	/*
   1166 	 * No match found.
   1167 	 */
   1168 	if (scr->active)
   1169 		/*
   1170 		 * Put a single width space character no matter what the
   1171 		 * actual width of the character is.
   1172 		 */
   1173 		_vga_raster_putchar(scr, row, col, ' ', attr,
   1174 		    &vga_console_fontset_ascii);
   1175 	scr->mem[off].ch = c;
   1176 	scr->mem[off].attr = attr;
   1177 	scr->mem[off].second = 0;
   1178 	scr->mem[off].enc = scr->encoding;
   1179 }
   1180 
   1181 static void
   1182 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
   1183     struct vga_raster_font *fs)
   1184 {
   1185 	struct vgascreen *scr = id;
   1186 	struct vga_handle *vh = scr->hdl;
   1187 	bus_space_tag_t memt = vh->vh_memt;
   1188 	bus_space_handle_t memh = vh->vh_memh;
   1189 	int i;
   1190 	int rasoff, rasoff2;
   1191 	int fheight = scr->type->fontheight;
   1192 	volatile u_int8_t pattern;
   1193 	u_int8_t fgcolor, bgcolor;
   1194 
   1195 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
   1196 	rasoff2 = rasoff;
   1197 
   1198 #if 0
   1199 	bgcolor = bgansitopc[attr >> 4];
   1200 	fgcolor = fgansitopc[attr & 0x0f];
   1201 #else
   1202 	bgcolor = ((attr >> 4) & 0x0f);
   1203 	fgcolor = attr & 0x0f;
   1204 #endif
   1205 
   1206 	if (fs->font->stride == 1) {
   1207 		/* Paint background. */
   1208 		vga_gdc_write(vh, mode, 0x02);
   1209 		for (i = 0; i < fheight; i++) {
   1210 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1211 			rasoff += scr->type->ncols;
   1212 		}
   1213 
   1214 		/* Draw a single width character. */
   1215 		vga_gdc_write(vh, mode, 0x03);
   1216 		vga_gdc_write(vh, setres, fgcolor);
   1217 		for (i = 0; i < fheight; i++) {
   1218 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
   1219 			/* When pattern is 0, skip output for speed-up. */
   1220 			if (pattern != 0) {
   1221 				bus_space_read_1(memt, memh, rasoff2);
   1222 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1223 			}
   1224 			rasoff2 += scr->type->ncols;
   1225 		}
   1226 	} else if (fs->font->stride == 2) {
   1227 		/* Paint background. */
   1228 		vga_gdc_write(vh, mode, 0x02);
   1229 		for (i = 0; i < fheight; i++) {
   1230 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1231 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
   1232 			rasoff += scr->type->ncols;
   1233 		}
   1234 
   1235 		/* Draw a double width character. */
   1236 		vga_gdc_write(vh, mode, 0x03);
   1237 		vga_gdc_write(vh, setres, fgcolor);
   1238 		for (i = 0; i < fheight; i++) {
   1239 			pattern = ((u_int8_t *)fs->font->data)
   1240 			    [(c * fheight + i) * 2];
   1241 			if (pattern != 0) {
   1242 				bus_space_read_1(memt, memh, rasoff2);
   1243 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1244 			}
   1245 			pattern = ((u_int8_t *)fs->font->data)
   1246 			    [(c * fheight + i) * 2 + 1];
   1247 			if (pattern != 0) {
   1248 				rasoff2++;
   1249 				bus_space_read_1(memt, memh, rasoff2);
   1250 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1251 				rasoff2--;
   1252 			}
   1253 			rasoff2 += scr->type->ncols;
   1254 		}
   1255 	}
   1256 }
   1257 
   1258 static void
   1259 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
   1260 {
   1261 	struct vgascreen *scr = id;
   1262 	struct vga_handle *vh = scr->hdl;
   1263 	bus_space_tag_t memt = vh->vh_memt;
   1264 	bus_space_handle_t memh = vh->vh_memh;
   1265 	bus_size_t srcoff, dstoff;
   1266 	bus_size_t rassrcoff, rasdstoff;
   1267 	int i;
   1268 	int fheight = scr->type->fontheight;
   1269 
   1270 	srcoff = row * scr->type->ncols + srccol;
   1271 	dstoff = row * scr->type->ncols + dstcol;
   1272 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
   1273 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
   1274 
   1275 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1276 	    ncols * sizeof(struct vga_scrmem));
   1277 
   1278 	vga_gdc_write(vh, mode, 0x01);
   1279 	if (scr->active) {
   1280 		for (i = 0; i < fheight; i++) {
   1281 			bus_space_copy_region_1(memt, memh,
   1282 			    rassrcoff + i * scr->type->ncols, memh,
   1283 			    rasdstoff + i * scr->type->ncols, ncols);
   1284 		}
   1285 	}
   1286 }
   1287 
   1288 static void
   1289 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
   1290 {
   1291 	struct vgascreen *scr = id;
   1292 	int i;
   1293 
   1294 	if (scr->active == 0)
   1295 		return;
   1296 
   1297 	for (i = startcol; i < startcol + ncols; i++)
   1298 		vga_raster_putchar(id, row, i, ' ', fillattr);
   1299 }
   1300 
   1301 static void
   1302 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1303 {
   1304 	struct vgascreen *scr = id;
   1305 	struct vga_handle *vh = scr->hdl;
   1306 	bus_space_tag_t memt = vh->vh_memt;
   1307 	bus_space_handle_t memh = vh->vh_memh;
   1308 	int ncols;
   1309 	bus_size_t srcoff, dstoff;
   1310 	bus_size_t rassrcoff, rasdstoff;
   1311 	int fheight;
   1312 
   1313 	ncols = scr->type->ncols;
   1314 	fheight = scr->type->fontheight;
   1315 
   1316 	srcoff = srcrow * ncols;
   1317 	dstoff = dstrow * ncols;
   1318 	rassrcoff = srcoff * fheight;
   1319 	rasdstoff = dstoff * fheight;
   1320 
   1321 	if (scr->active) {
   1322 		vga_gdc_write(vh, mode, 0x01);
   1323 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
   1324 			int cursoron = scr->cursoron;
   1325 
   1326 			if (cursoron)
   1327 				/* Disable cursor. */
   1328 				vga_raster_cursor(scr, 0,
   1329 				    scr->cursorrow, scr->cursorcol);
   1330 
   1331 			/* scroll up whole screen */
   1332 			if ((scr->dispoffset + srcrow * ncols * fheight)
   1333 			    <= scr->maxdispoffset)
   1334 				scr->dispoffset += srcrow * ncols * fheight;
   1335 			else {
   1336 				bus_space_copy_region_1(memt, memh,
   1337 				    scr->dispoffset + rassrcoff,
   1338 				    memh, scr->mindispoffset,
   1339 				    nrows * ncols * fheight);
   1340 				scr->dispoffset = scr->mindispoffset;
   1341 			}
   1342 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
   1343 			vga_6845_write(vh, startadrl, scr->dispoffset);
   1344 
   1345 			if (cursoron)
   1346 				/* Enable cursor. */
   1347 				vga_raster_cursor(scr, 1, scr->cursorrow,
   1348 				    scr->cursorcol);
   1349 		} else
   1350 			bus_space_copy_region_1(memt, memh,
   1351 			    scr->dispoffset + rassrcoff, memh,
   1352 			    scr->dispoffset + rasdstoff,
   1353 			    nrows * ncols * fheight);
   1354 	}
   1355 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1356 	    nrows * ncols * sizeof(struct vga_scrmem));
   1357 }
   1358 
   1359 static void
   1360 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
   1361 {
   1362 	struct vgascreen *scr = id;
   1363 	struct vga_handle *vh = scr->hdl;
   1364 	bus_space_tag_t memt = vh->vh_memt;
   1365 	bus_space_handle_t memh = vh->vh_memh;
   1366 	bus_size_t off, count;
   1367 	bus_size_t rasoff, rascount;
   1368 	int i;
   1369 
   1370 	off = startrow * scr->type->ncols;
   1371 	count = nrows * scr->type->ncols;
   1372 	rasoff = off * scr->type->fontheight;
   1373 	rascount = count * scr->type->fontheight;
   1374 
   1375 	if (scr->active) {
   1376 		u_int8_t bgcolor = (fillattr >> 4) & 0x0F;
   1377 
   1378 		/* Paint background. */
   1379 		vga_gdc_write(vh, mode, 0x02);
   1380 		if (scr->type->ncols % 4 == 0) {
   1381 			u_int32_t fill = bgcolor | (bgcolor << 8) |
   1382 			    (bgcolor << 16) | (bgcolor << 24);
   1383 			/* We can speed up I/O */
   1384 			for (i = rasoff; i < rasoff + rascount; i += 4)
   1385 				bus_space_write_4(memt, memh,
   1386 				    scr->dispoffset + i, fill);
   1387 		} else {
   1388 			u_int16_t fill = bgcolor | (bgcolor << 8);
   1389 			for (i = rasoff; i < rasoff + rascount; i += 2)
   1390 				bus_space_write_2(memt, memh,
   1391 				    scr->dispoffset + i, fill);
   1392 		}
   1393 	}
   1394 	for (i = 0; i < count; i++) {
   1395 		scr->mem[off + i].ch = ' ';
   1396 		scr->mem[off + i].attr = fillattr;
   1397 		scr->mem[off + i].second = 0;
   1398 		scr->mem[off + i].enc = scr->encoding;
   1399 	}
   1400 }
   1401 
   1402 static int
   1403 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
   1404 {
   1405 	struct vgascreen *scr = id;
   1406 	struct vga_config *vc = scr->cfg;
   1407 
   1408 	if (__predict_false((unsigned int)fg >= sizeof(fgansitopc) ||
   1409 	    (unsigned int)bg >= sizeof(bgansitopc)))
   1410 	    	return (EINVAL);
   1411 
   1412 	if (vc->hdl.vh_mono) {
   1413 		if (flags & WSATTR_WSCOLORS)
   1414 			return (EINVAL);
   1415 		if (flags & WSATTR_REVERSE)
   1416 			*attrp = 0x70;
   1417 		else
   1418 			*attrp = 0x07;
   1419 		if (flags & WSATTR_UNDERLINE)
   1420 			*attrp |= FG_UNDERLINE;
   1421 		if (flags & WSATTR_HILIT)
   1422 			*attrp |= FG_INTENSE;
   1423 	} else {
   1424 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1425 			return (EINVAL);
   1426 		if (flags & WSATTR_WSCOLORS)
   1427 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1428 		else
   1429 			*attrp = 7;
   1430 		if (flags & WSATTR_HILIT)
   1431 			*attrp += 8;
   1432 	}
   1433 	if (flags & WSATTR_BLINK)
   1434 		*attrp |= FG_BLINK;
   1435 	return (0);
   1436 }
   1437 
   1438 static void
   1439 vga_restore_screen(struct vgascreen *scr,
   1440     const struct wsscreen_descr *type, struct vga_scrmem *mem)
   1441 {
   1442 	int i, j, off, tmp;
   1443 
   1444 	tmp = scr->encoding;
   1445 	for (i = 0; i < type->nrows; i++) {
   1446 		for (j = 0; j < type->ncols; j++) {
   1447 			off = i * type->ncols + j;
   1448 			if (mem[off].second != 1) {
   1449 				scr->encoding = mem[off].enc;
   1450 				vga_raster_putchar(scr, i, j, mem[off].ch,
   1451 				    mem[off].attr);
   1452 			}
   1453 		}
   1454 	}
   1455 	scr->encoding = tmp;
   1456 }
   1457 
   1458 static void
   1459 vga_raster_setscreentype(struct vga_config *vc,
   1460     const struct wsscreen_descr *type)
   1461 {
   1462 	struct vga_handle *vh = &vc->hdl;
   1463 	struct vga_moderegs moderegs;
   1464 
   1465 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
   1466 	vga_set_mode(vh, &moderegs);
   1467 }
   1468 
   1469 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1470 static void
   1471 vga_raster_replaceattr(void *id, long oldattr, long newattr)
   1472 {
   1473 	struct vgascreen *scr = id;
   1474 	const struct wsscreen_descr *type = scr->type;
   1475 	int off;
   1476 
   1477 	for (off = 0; off < type->nrows * type->ncols; off++) {
   1478 		if (scr->mem[off].attr == oldattr)
   1479 			scr->mem[off].attr = newattr;
   1480 	}
   1481 
   1482 	/* Repaint the whole screen, if needed */
   1483 	if (scr->active)
   1484 		vga_restore_screen(scr, type, scr->mem);
   1485 }
   1486 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
   1487 
   1488 void
   1489 vga_resume(struct vga_softc *sc)
   1490 {
   1491 #ifdef VGA_RESET_ON_RESUME
   1492 	vga_initregs(&sc->sc_vc->hdl);
   1493 #endif
   1494 }
   1495