Home | History | Annotate | Line # | Download | only in ic
vga_raster.c revision 1.48
      1 /*	$NetBSD: vga_raster.c,v 1.48 2020/04/24 22:31:36 ad 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.48 2020/04/24 22:31:36 ad 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,
    399 	    BUS_SPACE_MAP_CACHEABLE, &vh->vh_allmemh))
    400 		panic("vga_init: couldn't map memory");
    401 
    402 	if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
    403 	    &vh->vh_memh))
    404 		panic("vga_raster_init: mem subrange failed");
    405 
    406 	vc->nscreens = 0;
    407 	LIST_INIT(&vc->screens);
    408 	vc->active = NULL;
    409 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
    410 	callout_init(&vc->vc_switch_callout, 0);
    411 
    412 	wsfont_init();
    413 	vc->nfonts = 1;
    414 	LIST_INIT(&vc->vc_fontlist);
    415 	vf = &vga_console_fontset_ascii;
    416 	if (vga_no_builtinfont) {
    417 		struct wsdisplay_font *wf;
    418 		int cookie;
    419 
    420 		/* prefer 8x16 pixel font */
    421 		cookie = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
    422 		    0, WSFONT_FIND_BITMAP);
    423 		if (cookie == -1)
    424 			cookie = wsfont_find(NULL, 0, 0, 0,
    425 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R,
    426 			    WSFONT_FIND_BITMAP);
    427 		if (cookie == -1 || wsfont_lock(cookie, &wf))
    428 			panic("vga_raster_init: can't load console font");
    429 		vf->font = wf;
    430 	} else {
    431 		vga_load_builtinfont(vh, builtinfont_data, 0, 256);
    432 		vf->font = &builtinfont;
    433 	}
    434 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
    435 }
    436 
    437 static void
    438 vga_raster_init_screen(struct vga_config *vc, struct vgascreen *scr,
    439     const struct wsscreen_descr *type, int existing, long *attrp)
    440 {
    441 	int cpos;
    442 	int res __diagused;
    443 	struct vga_handle *vh;
    444 
    445 	scr->cfg = vc;
    446 	scr->hdl = &vc->hdl;
    447 	scr->type = type;
    448 	scr->mindispoffset = 0;
    449 	scr->maxdispoffset = scr->dispoffset +
    450 	    type->nrows * type->ncols * type->fontheight;
    451 	vh = &vc->hdl;
    452 
    453 	LIST_INIT(&scr->fontset);
    454 	vga_raster_setup_font(vc, scr);
    455 
    456 	if (existing) {
    457 		int i;
    458 
    459 		cpos = vga_6845_read(vh, cursorh) << 8;
    460 		cpos |= vga_6845_read(vh, cursorl);
    461 
    462 		/* make sure we have a valid cursor position */
    463 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    464 			cpos = 0;
    465 
    466 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
    467 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
    468 
    469 		/* make sure we have a valid memory offset */
    470 		if (scr->dispoffset < scr->mindispoffset ||
    471 		    scr->dispoffset > scr->maxdispoffset)
    472 			scr->dispoffset = scr->mindispoffset;
    473 
    474 		scr->mem = boot_scrmem;
    475 		scr->active = 1;
    476 
    477 		/* Save the current screen to memory. XXXBJY assume 80x25 */
    478 		for (i = 0; i < 80 * 25; i++) {
    479 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
    480 			    vh->vh_allmemh, 0x18000 + i * 2);
    481 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
    482 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
    483 			scr->mem[i].enc = scr->encoding;
    484 		}
    485 
    486 		vga_raster_setscreentype(vc, type);
    487 
    488 		/* Clear the entire screen. */
    489 		vga_gdc_write(vh, mode, 0x02);
    490 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
    491 		    0x4000);
    492 
    493 		vga_restore_screen(scr, type, scr->mem);
    494 	} else {
    495 		cpos = 0;
    496 		scr->dispoffset = scr->mindispoffset;
    497 		scr->mem = NULL;
    498 		scr->active = 0;
    499 	}
    500 
    501 	scr->cursorrow = cpos / type->ncols;
    502 	scr->cursorcol = cpos % type->ncols;
    503 	vga_raster_cursor_init(scr, existing);
    504 
    505 #ifdef __alpha__
    506 	if (!vc->hdl.vh_mono)
    507 		/*
    508 		 * DEC firmware uses a blue background.
    509 		 */
    510 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
    511 		    WSATTR_WSCOLORS, attrp);
    512 	else
    513 #endif
    514 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
    515 	KASSERTMSG(res == 0, "attribute botch");
    516 
    517 	vc->nscreens++;
    518 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    519 }
    520 
    521 void
    522 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
    523     bus_space_tag_t memt, int type, int quirks,
    524     const struct vga_funcs *vf)
    525 {
    526 	int console;
    527 	struct vga_config *vc;
    528 	struct wsemuldisplaydev_attach_args aa;
    529 
    530 	console = vga_is_console(iot, type);
    531 
    532 	if (console) {
    533 		vc = &vga_console_vc;
    534 		vga_console_attached = 1;
    535 	} else {
    536 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
    537 		vga_raster_init(vc, iot, memt);
    538 	}
    539 
    540 	vc->vc_type = type;
    541 	vc->vc_funcs = vf;
    542 
    543 	sc->sc_vc = vc;
    544 	vc->softc = sc;
    545 
    546 	aa.console = console;
    547 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    548 	aa.accessops = &vga_raster_accessops;
    549 	aa.accesscookie = vc;
    550 
    551 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    552 }
    553 
    554 int
    555 vga_cndetach(void)
    556 {
    557 	struct vga_config *vc;
    558 	struct vga_handle *vh;
    559 
    560 	vc = &vga_console_vc;
    561 	vh = &vc->hdl;
    562 
    563 	if (vgaconsole) {
    564 		wsdisplay_cndetach();
    565 
    566 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_vga, 0x10);
    567 		bus_space_unmap(vh->vh_iot, vh->vh_ioh_6845, 0x10);
    568 		bus_space_unmap(vh->vh_memt, vh->vh_allmemh, 0x20000);
    569 
    570 		vga_console_attached = 0;
    571 		vgaconsole = 0;
    572 
    573 		return 1;
    574 	}
    575 
    576 	return 0;
    577 }
    578 
    579 int
    580 vga_is_console(bus_space_tag_t iot, int type)
    581 {
    582 	if (vgaconsole &&
    583 	    !vga_console_attached &&
    584 	    iot == vga_console_vc.hdl.vh_iot &&
    585 	    (vga_console_type == -1 || (type == vga_console_type)))
    586 		return (1);
    587 	return (0);
    588 }
    589 
    590 static int
    591 vga_get_video(struct vga_config *vc)
    592 {
    593 
    594 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
    595 }
    596 
    597 static void
    598 vga_set_video(struct vga_config *vc, int state)
    599 {
    600 	int val;
    601 
    602 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    603 	if (state) {					/* unblank screen */
    604 		val = vga_ts_read(&vc->hdl, mode);
    605 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
    606 #ifndef VGA_NO_VBLANK
    607 		val = vga_6845_read(&vc->hdl, mode);
    608 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    609 #endif
    610 	} else {					/* blank screen */
    611 		val = vga_ts_read(&vc->hdl, mode);
    612 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
    613 #ifndef VGA_NO_VBLANK
    614 		val = vga_6845_read(&vc->hdl, mode);
    615 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    616 #endif
    617 	}
    618 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    619 }
    620 
    621 int
    622 vga_raster_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    623 	struct lwp *l)
    624 {
    625 	struct vga_config *vc = v;
    626 	const struct vga_funcs *vf = vc->vc_funcs;
    627 
    628 	switch (cmd) {
    629 	case WSDISPLAYIO_GTYPE:
    630 		*(int *)data = vc->vc_type;
    631 		return 0;
    632 
    633 	case WSDISPLAYIO_GINFO: {
    634 		struct wsdisplay_fbinfo *fbi = data;
    635 		const struct wsscreen_descr *wd = vc->currenttype;
    636 		const struct videomode *vm = wd->modecookie;
    637 		fbi->width = vm->hdisplay;
    638 		fbi->height = vm->vdisplay;
    639 		fbi->depth = 24;	/* xxx: ? */
    640 		fbi->cmsize = 256;	/* xxx: from palette */
    641 		return 0;
    642 	}
    643 
    644 	case WSDISPLAYIO_GVIDEO:
    645 		*(int *)data = (vga_get_video(vc) ?
    646 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
    647 		return 0;
    648 
    649 	case WSDISPLAYIO_SVIDEO:
    650 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    651 		return 0;
    652 
    653 	case WSDISPLAYIO_GETCMAP:
    654 	case WSDISPLAYIO_PUTCMAP:
    655 	case WSDISPLAYIO_GCURPOS:
    656 	case WSDISPLAYIO_SCURPOS:
    657 	case WSDISPLAYIO_GCURMAX:
    658 	case WSDISPLAYIO_GCURSOR:
    659 	case WSDISPLAYIO_SCURSOR:
    660 #ifdef DIAGNOSTIC
    661 		printf("%s: 0x%lx unsupported\n", __func__, cmd);
    662 #endif
    663 		/* NONE of these operations are by the generic VGA driver. */
    664 		return EPASSTHROUGH;
    665 	}
    666 
    667 	if (vc->vc_funcs == NULL) {
    668 #ifdef DIAGNOSTIC
    669 		printf("%s: no vc_funcs\n", __func__);
    670 #endif
    671 		return EPASSTHROUGH;
    672 	}
    673 
    674 	if (vf->vf_ioctl == NULL) {
    675 #ifdef DIAGNOSTIC
    676 		printf("%s: no vf_ioctl\n", __func__);
    677 #endif
    678 		return EPASSTHROUGH;
    679 	}
    680 
    681 	return ((*vf->vf_ioctl)(v, cmd, data, flag, l));
    682 }
    683 
    684 static paddr_t
    685 vga_raster_mmap(void *v, void *vs, off_t offset, int prot)
    686 {
    687 	struct vga_config *vc = v;
    688 	const struct vga_funcs *vf = vc->vc_funcs;
    689 
    690 	if (vc->vc_funcs == NULL)
    691 		return (-1);
    692 
    693 	if (vf->vf_mmap == NULL)
    694 		return (-1);
    695 
    696 	return ((*vf->vf_mmap)(v, offset, prot));
    697 }
    698 
    699 static int
    700 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
    701     void **cookiep, int *curxp, int *curyp, long *defattrp)
    702 {
    703 	struct vga_config *vc = v;
    704 	struct vgascreen *scr;
    705 
    706 	if (vc->nscreens == 1) {
    707 		vc->screens.lh_first->mem = boot_scrmem;
    708 	}
    709 
    710 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    711 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    712 
    713 	if (vc->nscreens == 1) {
    714 		scr->active = 1;
    715 		vc->active = scr;
    716 		vc->currenttype = type;
    717 	} else {
    718 		scr->mem = malloc(sizeof(struct vga_scrmem) *
    719 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
    720 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
    721 	}
    722 
    723 	*cookiep = scr;
    724 	*curxp = scr->cursorcol;
    725 	*curyp = scr->cursorrow;
    726 
    727 	return (0);
    728 }
    729 
    730 static void
    731 vga_raster_free_screen(void *v, void *cookie)
    732 {
    733 	struct vgascreen *vs = cookie;
    734 	struct vga_config *vc = vs->cfg;
    735 
    736 	LIST_REMOVE(vs, next);
    737 	vc->nscreens--;
    738 	if (vs != &vga_console_screen)
    739 		free(vs, M_DEVBUF);
    740 	else
    741 		panic("vga_raster_free_screen: console");
    742 
    743 	if (vc->active == vs)
    744 		vc->active = 0;
    745 }
    746 
    747 static int
    748 vga_raster_show_screen(void *v, void *cookie, int waitok,
    749     void (*cb)(void *, int, int), void *cbarg)
    750 {
    751 	struct vgascreen *scr = cookie, *oldscr;
    752 	struct vga_config *vc = scr->cfg;
    753 
    754 	oldscr = vc->active; /* can be NULL! */
    755 	if (scr == oldscr) {
    756 		return (0);
    757 	}
    758 
    759 	vc->wantedscreen = cookie;
    760 	vc->switchcb = cb;
    761 	vc->switchcbarg = cbarg;
    762 	if (cb) {
    763 		callout_reset(&vc->vc_switch_callout, 0,
    764 		    (void(*)(void *))vga_switch_screen, vc);
    765 		return (EAGAIN);
    766 	}
    767 
    768 	vga_switch_screen(vc);
    769 	return (0);
    770 }
    771 
    772 static void
    773 vga_switch_screen(struct vga_config *vc)
    774 {
    775 	struct vgascreen *scr, *oldscr;
    776 	struct vga_handle *vh = &vc->hdl;
    777 	const struct wsscreen_descr *type;
    778 
    779 	scr = vc->wantedscreen;
    780 	if (!scr) {
    781 		printf("vga_switch_screen: disappeared\n");
    782 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    783 		return;
    784 	}
    785 	type = scr->type;
    786 	oldscr = vc->active; /* can be NULL! */
    787 #ifdef DIAGNOSTIC
    788 	if (oldscr) {
    789 		if (!oldscr->active)
    790 			panic("vga_raster_show_screen: not active");
    791 		if (oldscr->type != vc->currenttype)
    792 			panic("vga_raster_show_screen: bad type");
    793 	}
    794 #endif
    795 	if (scr == oldscr) {
    796 		return;
    797 	}
    798 #ifdef DIAGNOSTIC
    799 	if (scr->active)
    800 		panic("vga_raster_show_screen: active");
    801 #endif
    802 
    803 	if (oldscr)
    804 		oldscr->active = 0;
    805 
    806 	if (vc->currenttype != type) {
    807 		vga_raster_setscreentype(vc, type);
    808 		vc->currenttype = type;
    809 	}
    810 
    811 	scr->dispoffset = scr->mindispoffset;
    812 
    813 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
    814 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
    815 		vga_6845_write(vh, startadrl, scr->dispoffset);
    816 	}
    817 
    818 	/* Clear the entire screen. */
    819 	vga_gdc_write(vh, mode, 0x02);
    820 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
    821 
    822 	scr->active = 1;
    823 	vga_restore_screen(scr, type, scr->mem);
    824 
    825 	vc->active = scr;
    826 
    827 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
    828 
    829 	vc->wantedscreen = 0;
    830 	if (vc->switchcb)
    831 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    832 }
    833 
    834 static int
    835 vga_raster_load_font(void *v, void *id,
    836     struct wsdisplay_font *data)
    837 {
    838 	/* XXX */
    839 	printf("vga_raster_load_font: called\n");
    840 
    841 	return (0);
    842 }
    843 
    844 static void
    845 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
    846 {
    847 	struct vga_raster_font *vf;
    848 	struct wsdisplay_font *wf;
    849 	int cookie;
    850 
    851 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
    852 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0,
    853 		    WSFONT_FIND_BITMAP)) {
    854 			scr->encoding = vf->font->encoding;
    855 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
    856 			return;
    857 		}
    858 	}
    859 
    860 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
    861 	    WSDISPLAY_FONTORDER_L2R, 0, WSFONT_FIND_BITMAP);
    862 	if (cookie == -1)
    863 		return;
    864 
    865 	if (wsfont_lock(cookie, &wf))
    866 		return;
    867 
    868 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_WAITOK);
    869 	vf->font = wf;
    870 	scr->encoding = vf->font->encoding;
    871 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
    872 }
    873 
    874 static void
    875 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
    876 {
    877 	int i;
    878 	int depth = 4;			/* XXXBJY hardcoded for now */
    879 	const u_int8_t palette[] = {
    880 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
    881 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
    882 	};
    883 
    884 	/*
    885 	 * Compute hsync and vsync polarity.
    886 	 */
    887 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
    888 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
    889 	    	regs->miscout = 0x23;
    890 		if (mode->flags & VID_NHSYNC)
    891 			regs->miscout |= 0x40;
    892 		if (mode->flags & VID_NVSYNC)
    893 			regs->miscout |= 0x80;
    894 	} else {
    895 		if (mode->flags & VID_DBLSCAN)
    896 			mode->vdisplay *= 2;
    897 		if (mode->vdisplay < 400)
    898 			regs->miscout = 0xa3;
    899 		else if (mode->vdisplay < 480)
    900 			regs->miscout = 0x63;
    901 		else if (mode->vdisplay < 768)
    902 			regs->miscout = 0xe3;
    903 		else
    904 			regs->miscout = 0x23;
    905 	}
    906 
    907 	/*
    908 	 * Time sequencer
    909 	 */
    910 	if (depth == 4)
    911 		regs->ts[0] = 0x02;
    912 	else
    913 		regs->ts[0] = 0x00;
    914 	if (mode->flags & VID_CLKDIV2)
    915 		regs->ts[1] = 0x09;
    916 	else
    917 		regs->ts[1] = 0x01;
    918 	regs->ts[2] = 0x0f;
    919 	regs->ts[3] = 0x00;
    920 	if (depth < 8)
    921 		regs->ts[4] = 0x06;
    922 	else
    923 		regs->ts[4] = 0x0e;
    924 
    925 	/*
    926 	 * CRTC controller
    927 	 */
    928 	regs->crtc[0] = (mode->htotal >> 3) - 5;
    929 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
    930 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
    931 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
    932 	regs->crtc[4] = mode->hsync_start >> 3;
    933 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
    934 	    | (((mode->hsync_end >> 3)) & 0x1f);
    935 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
    936 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
    937 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
    938 	    | ((mode->vsync_start & 0x100) >> 6)
    939 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
    940 	    | 0x10
    941 	    | (((mode->vtotal - 2) & 0x200) >> 4)
    942 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
    943 	    | ((mode->vsync_start & 0x200) >> 2);
    944 	regs->crtc[8] = 0x00;
    945 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
    946 	if (mode->flags & VID_DBLSCAN)
    947 		regs->crtc[9] |= 0x80;
    948 	regs->crtc[10] = 0x00;
    949 	regs->crtc[11] = 0x00;
    950 	regs->crtc[12] = 0x00;
    951 	regs->crtc[13] = 0x00;
    952 	regs->crtc[14] = 0x00;
    953 	regs->crtc[15] = 0x00;
    954 	regs->crtc[16] = mode->vsync_start & 0xff;
    955 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
    956 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
    957 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
    958 	regs->crtc[20] = 0x00;
    959 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
    960 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
    961 	if (depth < 8)
    962 		regs->crtc[23] = 0xe3;
    963 	else
    964 		regs->crtc[23] = 0xc3;
    965 	regs->crtc[24] = 0xff;
    966 
    967 	/*
    968 	 * Graphics display controller
    969 	 */
    970 	regs->gdc[0] = 0x00;
    971 	regs->gdc[1] = 0x00;
    972 	regs->gdc[2] = 0x00;
    973 	regs->gdc[3] = 0x00;
    974 	regs->gdc[4] = 0x00;
    975 	if (depth == 4)
    976 		regs->gdc[5] = 0x02;
    977 	else
    978 		regs->gdc[5] = 0x40;
    979 	regs->gdc[6] = 0x01;
    980 	regs->gdc[7] = 0x0f;
    981 	regs->gdc[8] = 0xff;
    982 
    983 	/*
    984 	 * Attribute controller
    985 	 */
    986 	/* Set palette registers. */
    987 	for (i = 0; i < 16; i++)
    988 		regs->atc[i] = palette[i];
    989 	if (depth == 4)
    990 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
    991 	else
    992 		regs->atc[16] = 0x41;
    993 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
    994 	regs->atc[18] = 0x0f;
    995 	regs->atc[19] = 0x00;
    996 	regs->atc[20] = 0x00;
    997 }
    998 
    999 static void
   1000 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
   1001 {
   1002 	int i;
   1003 
   1004 	/* Disable display. */
   1005 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
   1006 
   1007 	/* Write misc output register. */
   1008 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
   1009 	    regs->miscout);
   1010 
   1011 	/* Set synchronous reset. */
   1012 	vga_ts_write(vh, syncreset, 0x01);
   1013 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
   1014 	for (i = 2; i < VGA_TS_NREGS; i++)
   1015 		_vga_ts_write(vh, i, regs->ts[i]);
   1016 	/* Clear synchronous reset. */
   1017 	vga_ts_write(vh, syncreset, 0x03);
   1018 
   1019 	/* Unprotect CRTC registers 0-7. */
   1020 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
   1021 	/* Write CRTC registers. */
   1022 	for (i = 0; i < MC6845_NREGS; i++)
   1023 		_vga_6845_write(vh, i, regs->crtc[i]);
   1024 
   1025 	/* Write graphics display registers. */
   1026 	for (i = 0; i < VGA_GDC_NREGS; i++)
   1027 		_vga_gdc_write(vh, i, regs->gdc[i]);
   1028 
   1029 	/* Write attribute controller registers. */
   1030 	for (i = 0; i < VGA_ATC_NREGS; i++)
   1031 		_vga_attr_write(vh, i, regs->atc[i]);
   1032 
   1033 	/* Enable display. */
   1034 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
   1035 }
   1036 
   1037 static void
   1038 vga_raster_cursor_init(struct vgascreen *scr, int existing)
   1039 {
   1040 	int off;
   1041 
   1042 	if (existing) {
   1043 		/*
   1044 		 * This is the first screen. At this point, scr->active is
   1045 		 * false, so we can't use vga_raster_cursor() to do this.
   1046 		 */
   1047 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
   1048 		    scr->dispoffset / 8;
   1049 
   1050 		scr->cursortmp = scr->mem[off];
   1051 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
   1052 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
   1053 	} else {
   1054 		scr->cursortmp.ch = 0;
   1055 		scr->cursortmp.attr = 0;
   1056 		scr->cursortmp.second = 0;
   1057 		scr->cursortmp.enc = scr->encoding;
   1058 	}
   1059 
   1060 	scr->cursoron = 1;
   1061 }
   1062 
   1063 static void
   1064 vga_raster_cursor(void *id, int on, int row, int col)
   1065 {
   1066 	struct vgascreen *scr = id;
   1067 	int off, tmp;
   1068 
   1069 	/* Remove old cursor image */
   1070 	if (scr->cursoron) {
   1071 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1072 		if (scr->active) {
   1073 			tmp = scr->encoding;
   1074 			scr->encoding = scr->cursortmp.enc;
   1075 			if (scr->cursortmp.second)
   1076 				vga_raster_putchar(id, scr->cursorrow,
   1077 				    scr->cursorcol - 1, scr->cursortmp.ch,
   1078 				    scr->cursortmp.attr);
   1079 			else
   1080 				vga_raster_putchar(id, scr->cursorrow,
   1081 				    scr->cursorcol, scr->cursortmp.ch,
   1082 				    scr->cursortmp.attr);
   1083 			scr->encoding = tmp;
   1084 		}
   1085 	}
   1086 
   1087 	scr->cursorrow = row;
   1088 	scr->cursorcol = col;
   1089 
   1090 	if ((scr->cursoron = on) == 0)
   1091 		return;
   1092 
   1093 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
   1094 	scr->cursortmp = scr->mem[off];
   1095 	if (scr->active) {
   1096 		tmp = scr->encoding;
   1097 		scr->encoding = scr->cursortmp.enc;
   1098 		if (scr->cursortmp.second)
   1099 			vga_raster_putchar(id, scr->cursorrow,
   1100 			    scr->cursorcol - 1, scr->cursortmp.ch,
   1101 			    scr->cursortmp.attr ^ 0x77);
   1102 		else
   1103 			vga_raster_putchar(id, scr->cursorrow,
   1104 			    scr->cursorcol, scr->cursortmp.ch,
   1105 			    scr->cursortmp.attr ^ 0x77);
   1106 		scr->encoding = tmp;
   1107 	}
   1108 }
   1109 
   1110 static int
   1111 vga_raster_mapchar(void *id, int uni, u_int *index)
   1112 {
   1113 	struct vgascreen *scr = id;
   1114 
   1115 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
   1116 		return pcdisplay_mapchar(id, uni, index);
   1117 	else {
   1118 		*index = uni;
   1119 		return 5;
   1120 	}
   1121 }
   1122 
   1123 static void
   1124 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
   1125 {
   1126 	struct vgascreen *scr = id;
   1127 	size_t off;
   1128 	struct vga_raster_font *fs;
   1129 	u_int tmp_ch;
   1130 
   1131 	off = row * scr->type->ncols + col;
   1132 
   1133 	if (__predict_false(off >= (scr->type->ncols * scr->type->nrows)))
   1134 		return;
   1135 
   1136 	LIST_FOREACH(fs, &scr->fontset, next) {
   1137 		if ((scr->encoding == fs->font->encoding) &&
   1138 		    (c >= fs->font->firstchar) &&
   1139 		    (c < fs->font->firstchar + fs->font->numchars) &&
   1140 		    (scr->type->fontheight == fs->font->fontheight)) {
   1141 			if (scr->active) {
   1142 				tmp_ch = c - fs->font->firstchar;
   1143 				_vga_raster_putchar(scr, row, col, tmp_ch,
   1144 				    attr, fs);
   1145 			}
   1146 
   1147 			scr->mem[off].ch = c;
   1148 			scr->mem[off].attr = attr;
   1149 			scr->mem[off].second = 0;
   1150 			scr->mem[off].enc = fs->font->encoding;
   1151 
   1152 			if (fs->font->stride == 2) {
   1153 				scr->mem[off + 1].ch = c;
   1154 				scr->mem[off + 1].attr = attr;
   1155 				scr->mem[off + 1].second = 1;
   1156 				scr->mem[off + 1].enc = fs->font->encoding;
   1157 			}
   1158 
   1159 			return;
   1160 		}
   1161 	}
   1162 
   1163 	/*
   1164 	 * No match found.
   1165 	 */
   1166 	if (scr->active)
   1167 		/*
   1168 		 * Put a single width space character no matter what the
   1169 		 * actual width of the character is.
   1170 		 */
   1171 		_vga_raster_putchar(scr, row, col, ' ', attr,
   1172 		    &vga_console_fontset_ascii);
   1173 	scr->mem[off].ch = c;
   1174 	scr->mem[off].attr = attr;
   1175 	scr->mem[off].second = 0;
   1176 	scr->mem[off].enc = scr->encoding;
   1177 }
   1178 
   1179 static void
   1180 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
   1181     struct vga_raster_font *fs)
   1182 {
   1183 	struct vgascreen *scr = id;
   1184 	struct vga_handle *vh = scr->hdl;
   1185 	bus_space_tag_t memt = vh->vh_memt;
   1186 	bus_space_handle_t memh = vh->vh_memh;
   1187 	int i;
   1188 	int rasoff, rasoff2;
   1189 	int fheight = scr->type->fontheight;
   1190 	volatile u_int8_t pattern;
   1191 	u_int8_t fgcolor, bgcolor;
   1192 
   1193 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
   1194 	rasoff2 = rasoff;
   1195 
   1196 #if 0
   1197 	bgcolor = bgansitopc[attr >> 4];
   1198 	fgcolor = fgansitopc[attr & 0x0f];
   1199 #else
   1200 	bgcolor = ((attr >> 4) & 0x0f);
   1201 	fgcolor = attr & 0x0f;
   1202 #endif
   1203 
   1204 	if (fs->font->stride == 1) {
   1205 		/* Paint background. */
   1206 		vga_gdc_write(vh, mode, 0x02);
   1207 		for (i = 0; i < fheight; i++) {
   1208 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1209 			rasoff += scr->type->ncols;
   1210 		}
   1211 
   1212 		/* Draw a single width character. */
   1213 		vga_gdc_write(vh, mode, 0x03);
   1214 		vga_gdc_write(vh, setres, fgcolor);
   1215 		for (i = 0; i < fheight; i++) {
   1216 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
   1217 			/* When pattern is 0, skip output for speed-up. */
   1218 			if (pattern != 0) {
   1219 				bus_space_read_1(memt, memh, rasoff2);
   1220 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1221 			}
   1222 			rasoff2 += scr->type->ncols;
   1223 		}
   1224 	} else if (fs->font->stride == 2) {
   1225 		/* Paint background. */
   1226 		vga_gdc_write(vh, mode, 0x02);
   1227 		for (i = 0; i < fheight; i++) {
   1228 			bus_space_write_1(memt, memh, rasoff, bgcolor);
   1229 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
   1230 			rasoff += scr->type->ncols;
   1231 		}
   1232 
   1233 		/* Draw a double width character. */
   1234 		vga_gdc_write(vh, mode, 0x03);
   1235 		vga_gdc_write(vh, setres, fgcolor);
   1236 		for (i = 0; i < fheight; i++) {
   1237 			pattern = ((u_int8_t *)fs->font->data)
   1238 			    [(c * fheight + i) * 2];
   1239 			if (pattern != 0) {
   1240 				bus_space_read_1(memt, memh, rasoff2);
   1241 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1242 			}
   1243 			pattern = ((u_int8_t *)fs->font->data)
   1244 			    [(c * fheight + i) * 2 + 1];
   1245 			if (pattern != 0) {
   1246 				rasoff2++;
   1247 				bus_space_read_1(memt, memh, rasoff2);
   1248 				bus_space_write_1(memt, memh, rasoff2, pattern);
   1249 				rasoff2--;
   1250 			}
   1251 			rasoff2 += scr->type->ncols;
   1252 		}
   1253 	}
   1254 }
   1255 
   1256 static void
   1257 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
   1258 {
   1259 	struct vgascreen *scr = id;
   1260 	struct vga_handle *vh = scr->hdl;
   1261 	bus_space_tag_t memt = vh->vh_memt;
   1262 	bus_space_handle_t memh = vh->vh_memh;
   1263 	bus_size_t srcoff, dstoff;
   1264 	bus_size_t rassrcoff, rasdstoff;
   1265 	int i;
   1266 	int fheight = scr->type->fontheight;
   1267 
   1268 	srcoff = row * scr->type->ncols + srccol;
   1269 	dstoff = row * scr->type->ncols + dstcol;
   1270 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
   1271 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
   1272 
   1273 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1274 	    ncols * sizeof(struct vga_scrmem));
   1275 
   1276 	vga_gdc_write(vh, mode, 0x01);
   1277 	if (scr->active) {
   1278 		for (i = 0; i < fheight; i++) {
   1279 			bus_space_copy_region_1(memt, memh,
   1280 			    rassrcoff + i * scr->type->ncols, memh,
   1281 			    rasdstoff + i * scr->type->ncols, ncols);
   1282 		}
   1283 	}
   1284 }
   1285 
   1286 static void
   1287 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
   1288 {
   1289 	struct vgascreen *scr = id;
   1290 	int i;
   1291 
   1292 	if (scr->active == 0)
   1293 		return;
   1294 
   1295 	for (i = startcol; i < startcol + ncols; i++)
   1296 		vga_raster_putchar(id, row, i, ' ', fillattr);
   1297 }
   1298 
   1299 static void
   1300 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1301 {
   1302 	struct vgascreen *scr = id;
   1303 	struct vga_handle *vh = scr->hdl;
   1304 	bus_space_tag_t memt = vh->vh_memt;
   1305 	bus_space_handle_t memh = vh->vh_memh;
   1306 	int ncols;
   1307 	bus_size_t srcoff, dstoff;
   1308 	bus_size_t rassrcoff, rasdstoff;
   1309 	int fheight;
   1310 
   1311 	ncols = scr->type->ncols;
   1312 	fheight = scr->type->fontheight;
   1313 
   1314 	srcoff = srcrow * ncols;
   1315 	dstoff = dstrow * ncols;
   1316 	rassrcoff = srcoff * fheight;
   1317 	rasdstoff = dstoff * fheight;
   1318 
   1319 	if (scr->active) {
   1320 		vga_gdc_write(vh, mode, 0x01);
   1321 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
   1322 			int cursoron = scr->cursoron;
   1323 
   1324 			if (cursoron)
   1325 				/* Disable cursor. */
   1326 				vga_raster_cursor(scr, 0,
   1327 				    scr->cursorrow, scr->cursorcol);
   1328 
   1329 			/* scroll up whole screen */
   1330 			if ((scr->dispoffset + srcrow * ncols * fheight)
   1331 			    <= scr->maxdispoffset)
   1332 				scr->dispoffset += srcrow * ncols * fheight;
   1333 			else {
   1334 				bus_space_copy_region_1(memt, memh,
   1335 				    scr->dispoffset + rassrcoff,
   1336 				    memh, scr->mindispoffset,
   1337 				    nrows * ncols * fheight);
   1338 				scr->dispoffset = scr->mindispoffset;
   1339 			}
   1340 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
   1341 			vga_6845_write(vh, startadrl, scr->dispoffset);
   1342 
   1343 			if (cursoron)
   1344 				/* Enable cursor. */
   1345 				vga_raster_cursor(scr, 1, scr->cursorrow,
   1346 				    scr->cursorcol);
   1347 		} else
   1348 			bus_space_copy_region_1(memt, memh,
   1349 			    scr->dispoffset + rassrcoff, memh,
   1350 			    scr->dispoffset + rasdstoff,
   1351 			    nrows * ncols * fheight);
   1352 	}
   1353 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
   1354 	    nrows * ncols * sizeof(struct vga_scrmem));
   1355 }
   1356 
   1357 static void
   1358 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
   1359 {
   1360 	struct vgascreen *scr = id;
   1361 	struct vga_handle *vh = scr->hdl;
   1362 	bus_space_tag_t memt = vh->vh_memt;
   1363 	bus_space_handle_t memh = vh->vh_memh;
   1364 	bus_size_t off, count;
   1365 	bus_size_t rasoff, rascount;
   1366 	int i;
   1367 
   1368 	off = startrow * scr->type->ncols;
   1369 	count = nrows * scr->type->ncols;
   1370 	rasoff = off * scr->type->fontheight;
   1371 	rascount = count * scr->type->fontheight;
   1372 
   1373 	if (scr->active) {
   1374 		u_int8_t bgcolor = (fillattr >> 4) & 0x0F;
   1375 
   1376 		/* Paint background. */
   1377 		vga_gdc_write(vh, mode, 0x02);
   1378 		if (scr->type->ncols % 4 == 0) {
   1379 			u_int32_t fill = bgcolor | (bgcolor << 8) |
   1380 			    (bgcolor << 16) | (bgcolor << 24);
   1381 			/* We can speed up I/O */
   1382 			for (i = rasoff; i < rasoff + rascount; i += 4)
   1383 				bus_space_write_4(memt, memh,
   1384 				    scr->dispoffset + i, fill);
   1385 		} else {
   1386 			u_int16_t fill = bgcolor | (bgcolor << 8);
   1387 			for (i = rasoff; i < rasoff + rascount; i += 2)
   1388 				bus_space_write_2(memt, memh,
   1389 				    scr->dispoffset + i, fill);
   1390 		}
   1391 	}
   1392 	for (i = 0; i < count; i++) {
   1393 		scr->mem[off + i].ch = ' ';
   1394 		scr->mem[off + i].attr = fillattr;
   1395 		scr->mem[off + i].second = 0;
   1396 		scr->mem[off + i].enc = scr->encoding;
   1397 	}
   1398 }
   1399 
   1400 static int
   1401 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
   1402 {
   1403 	struct vgascreen *scr = id;
   1404 	struct vga_config *vc = scr->cfg;
   1405 
   1406 	if (__predict_false((unsigned int)fg >= sizeof(fgansitopc) ||
   1407 	    (unsigned int)bg >= sizeof(bgansitopc)))
   1408 	    	return (EINVAL);
   1409 
   1410 	if (vc->hdl.vh_mono) {
   1411 		if (flags & WSATTR_WSCOLORS)
   1412 			return (EINVAL);
   1413 		if (flags & WSATTR_REVERSE)
   1414 			*attrp = 0x70;
   1415 		else
   1416 			*attrp = 0x07;
   1417 		if (flags & WSATTR_UNDERLINE)
   1418 			*attrp |= FG_UNDERLINE;
   1419 		if (flags & WSATTR_HILIT)
   1420 			*attrp |= FG_INTENSE;
   1421 	} else {
   1422 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1423 			return (EINVAL);
   1424 		if (flags & WSATTR_WSCOLORS)
   1425 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1426 		else
   1427 			*attrp = 7;
   1428 		if (flags & WSATTR_HILIT)
   1429 			*attrp += 8;
   1430 	}
   1431 	if (flags & WSATTR_BLINK)
   1432 		*attrp |= FG_BLINK;
   1433 	return (0);
   1434 }
   1435 
   1436 static void
   1437 vga_restore_screen(struct vgascreen *scr,
   1438     const struct wsscreen_descr *type, struct vga_scrmem *mem)
   1439 {
   1440 	int i, j, off, tmp;
   1441 
   1442 	tmp = scr->encoding;
   1443 	for (i = 0; i < type->nrows; i++) {
   1444 		for (j = 0; j < type->ncols; j++) {
   1445 			off = i * type->ncols + j;
   1446 			if (mem[off].second != 1) {
   1447 				scr->encoding = mem[off].enc;
   1448 				vga_raster_putchar(scr, i, j, mem[off].ch,
   1449 				    mem[off].attr);
   1450 			}
   1451 		}
   1452 	}
   1453 	scr->encoding = tmp;
   1454 }
   1455 
   1456 static void
   1457 vga_raster_setscreentype(struct vga_config *vc,
   1458     const struct wsscreen_descr *type)
   1459 {
   1460 	struct vga_handle *vh = &vc->hdl;
   1461 	struct vga_moderegs moderegs;
   1462 
   1463 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
   1464 	vga_set_mode(vh, &moderegs);
   1465 }
   1466 
   1467 #ifdef WSDISPLAY_CUSTOM_OUTPUT
   1468 static void
   1469 vga_raster_replaceattr(void *id, long oldattr, long newattr)
   1470 {
   1471 	struct vgascreen *scr = id;
   1472 	const struct wsscreen_descr *type = scr->type;
   1473 	int off;
   1474 
   1475 	for (off = 0; off < type->nrows * type->ncols; off++) {
   1476 		if (scr->mem[off].attr == oldattr)
   1477 			scr->mem[off].attr = newattr;
   1478 	}
   1479 
   1480 	/* Repaint the whole screen, if needed */
   1481 	if (scr->active)
   1482 		vga_restore_screen(scr, type, scr->mem);
   1483 }
   1484 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
   1485 
   1486 void
   1487 vga_resume(struct vga_softc *sc)
   1488 {
   1489 #ifdef VGA_RESET_ON_RESUME
   1490 	vga_initregs(&sc->sc_vc->hdl);
   1491 #endif
   1492 }
   1493