Home | History | Annotate | Line # | Download | only in ic
vga.c revision 1.61
      1 /* $NetBSD: vga.c,v 1.61 2002/07/07 07:34:54 junyoung Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: vga.c,v 1.61 2002/07/07 07:34:54 junyoung Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/callout.h>
     36 #include <sys/kernel.h>
     37 #include <sys/device.h>
     38 #include <sys/malloc.h>
     39 #include <sys/queue.h>
     40 #include <machine/bus.h>
     41 
     42 #include <dev/ic/mc6845reg.h>
     43 #include <dev/ic/pcdisplayvar.h>
     44 #include <dev/ic/vgareg.h>
     45 #include <dev/ic/vgavar.h>
     46 
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/unicode.h>
     50 #include <dev/wsfont/wsfont.h>
     51 
     52 #include <dev/ic/pcdisplay.h>
     53 
     54 /* for WSCONS_SUPPORT_PCVTFONTS and WSDISPLAY_CHARFUNCS */
     55 #include "opt_wsdisplay_compat.h"
     56 #include "opt_vga.h"
     57 
     58 static struct wsdisplay_font _vga_builtinfont = {
     59 	"builtin",
     60 	0, 256,
     61 	WSDISPLAY_FONTENC_IBM,
     62 	8, 16, 1,
     63 	WSDISPLAY_FONTORDER_L2R, 0,
     64 	0
     65 };
     66 
     67 struct egavga_font {
     68 	struct wsdisplay_font *wsfont;
     69 	int cookie; /* wsfont handle, -1 invalid */
     70 	int slot; /* in adapter RAM */
     71 	int usecount;
     72 	TAILQ_ENTRY(egavga_font) next; /* LRU queue */
     73 };
     74 
     75 static struct egavga_font vga_builtinfont = {
     76 	&_vga_builtinfont,
     77 	-1, 0
     78 };
     79 
     80 #ifdef VGA_CONSOLE_SCREENTYPE
     81 static struct egavga_font vga_consolefont;
     82 #endif
     83 
     84 struct vgascreen {
     85 	struct pcdisplayscreen pcs;
     86 
     87 	LIST_ENTRY(vgascreen) next;
     88 
     89 	struct vga_config *cfg;
     90 
     91 	/* videostate */
     92 	struct egavga_font *fontset1, *fontset2;
     93 	/* font data */
     94 	/* palette */
     95 
     96 	int mindispoffset, maxdispoffset;
     97 };
     98 
     99 static int vgaconsole, vga_console_type, vga_console_attached;
    100 static struct vgascreen vga_console_screen;
    101 static struct vga_config vga_console_vc;
    102 
    103 struct egavga_font *egavga_getfont(struct vga_config *, struct vgascreen *,
    104 				   char *, int);
    105 void egavga_unreffont(struct vga_config *, struct egavga_font *);
    106 
    107 int vga_selectfont(struct vga_config *, struct vgascreen *, char *, char *);
    108 void vga_init_screen(struct vga_config *, struct vgascreen *,
    109 		     const struct wsscreen_descr *, int, long *);
    110 void vga_init(struct vga_config *, bus_space_tag_t, bus_space_tag_t);
    111 static void vga_setfont(struct vga_config *, struct vgascreen *);
    112 
    113 static int vga_mapchar(void *, int, unsigned int *);
    114 static int vga_allocattr(void *, int, int, int, long *);
    115 static void vga_copyrows(void *, int, int, int);
    116 
    117 const struct wsdisplay_emulops vga_emulops = {
    118 	pcdisplay_cursor,
    119 	vga_mapchar,
    120 	pcdisplay_putchar,
    121 	pcdisplay_copycols,
    122 	pcdisplay_erasecols,
    123 	vga_copyrows,
    124 	pcdisplay_eraserows,
    125 	vga_allocattr
    126 };
    127 
    128 /*
    129  * translate WS(=ANSI) color codes to standard pc ones
    130  */
    131 static const unsigned char fgansitopc[] = {
    132 #ifdef __alpha__
    133 	/*
    134 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
    135 	 * XXX We should probably not bother with this
    136 	 * XXX (reinitialize the palette registers).
    137 	 */
    138 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
    139 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
    140 #else
    141 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
    142 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
    143 #endif
    144 }, bgansitopc[] = {
    145 #ifdef __alpha__
    146 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
    147 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
    148 #else
    149 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
    150 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
    151 #endif
    152 };
    153 
    154 const struct wsscreen_descr vga_25lscreen = {
    155 	"80x25", 80, 25,
    156 	&vga_emulops,
    157 	8, 16,
    158 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    159 }, vga_25lscreen_mono = {
    160 	"80x25", 80, 25,
    161 	&vga_emulops,
    162 	8, 16,
    163 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    164 }, vga_25lscreen_bf = {
    165 	"80x25bf", 80, 25,
    166 	&vga_emulops,
    167 	8, 16,
    168 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    169 }, vga_40lscreen = {
    170 	"80x40", 80, 40,
    171 	&vga_emulops,
    172 	8, 10,
    173 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    174 }, vga_40lscreen_mono = {
    175 	"80x40", 80, 40,
    176 	&vga_emulops,
    177 	8, 10,
    178 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    179 }, vga_40lscreen_bf = {
    180 	"80x40bf", 80, 40,
    181 	&vga_emulops,
    182 	8, 10,
    183 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    184 }, vga_50lscreen = {
    185 	"80x50", 80, 50,
    186 	&vga_emulops,
    187 	8, 8,
    188 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    189 }, vga_50lscreen_mono = {
    190 	"80x50", 80, 50,
    191 	&vga_emulops,
    192 	8, 8,
    193 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    194 }, vga_50lscreen_bf = {
    195 	"80x50bf", 80, 50,
    196 	&vga_emulops,
    197 	8, 8,
    198 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    199 }, vga_24lscreen = {
    200 	"80x24", 80, 24,
    201 	&vga_emulops,
    202 	8, 16,
    203 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    204 }, vga_24lscreen_mono = {
    205 	"80x24", 80, 24,
    206 	&vga_emulops,
    207 	8, 16,
    208 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    209 }, vga_24lscreen_bf = {
    210 	"80x24bf", 80, 24,
    211 	&vga_emulops,
    212 	8, 16,
    213 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    214 };
    215 
    216 #define VGA_SCREEN_CANTWOFONTS(type) (!((type)->capabilities & WSSCREEN_HILIT))
    217 
    218 const struct wsscreen_descr *_vga_scrlist[] = {
    219 	&vga_25lscreen,
    220 	&vga_25lscreen_bf,
    221 	&vga_40lscreen,
    222 	&vga_40lscreen_bf,
    223 	&vga_50lscreen,
    224 	&vga_50lscreen_bf,
    225 	&vga_24lscreen,
    226 	&vga_24lscreen_bf,
    227 	/* XXX other formats, graphics screen? */
    228 }, *_vga_scrlist_mono[] = {
    229 	&vga_25lscreen_mono,
    230 	&vga_40lscreen_mono,
    231 	&vga_50lscreen_mono,
    232 	&vga_24lscreen_mono,
    233 	/* XXX other formats, graphics screen? */
    234 };
    235 
    236 const struct wsscreen_list vga_screenlist = {
    237 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
    238 	_vga_scrlist
    239 }, vga_screenlist_mono = {
    240 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
    241 	_vga_scrlist_mono
    242 };
    243 
    244 static int	vga_ioctl(void *, u_long, caddr_t, int, struct proc *);
    245 static paddr_t	vga_mmap(void *, off_t, int);
    246 static int	vga_alloc_screen(void *, const struct wsscreen_descr *,
    247 				 void **, int *, int *, long *);
    248 static void	vga_free_screen(void *, void *);
    249 static int	vga_show_screen(void *, void *, int,
    250 				void (*)(void *, int, int), void *);
    251 static int	vga_load_font(void *, void *, struct wsdisplay_font *);
    252 #ifdef WSDISPLAY_CHARFUNCS
    253 static int	vga_getwschar(void *, struct wsdisplay_char *);
    254 static int	vga_putwschar(void *, struct wsdisplay_char *);
    255 #endif /* WSDISPLAY_CHARFUNCS */
    256 
    257 void vga_doswitch(struct vga_config *);
    258 
    259 const struct wsdisplay_accessops vga_accessops = {
    260 	vga_ioctl,
    261 	vga_mmap,
    262 	vga_alloc_screen,
    263 	vga_free_screen,
    264 	vga_show_screen,
    265 	vga_load_font,
    266 	NULL,
    267 #ifdef WSDISPLAY_CHARFUNCS
    268 	vga_getwschar,
    269 	vga_putwschar
    270 #else /* WSDISPLAY_CHARFUNCS */
    271 	NULL,
    272 	NULL
    273 #endif /* WSDISPLAY_CHARFUNCS */
    274 };
    275 
    276 /*
    277  * The following functions implement back-end configuration grabbing
    278  * and attachment.
    279  */
    280 int
    281 vga_common_probe(bus_space_tag_t iot, bus_space_tag_t memt)
    282 {
    283 	bus_space_handle_t ioh_vga, ioh_6845, memh;
    284 	u_int8_t regval;
    285 	u_int16_t vgadata;
    286 	int gotio_vga, gotio_6845, gotmem, mono, rv;
    287 	int dispoffset;
    288 
    289 	gotio_vga = gotio_6845 = gotmem = rv = 0;
    290 
    291 	if (bus_space_map(iot, 0x3c0, 0x10, 0, &ioh_vga))
    292 		goto bad;
    293 	gotio_vga = 1;
    294 
    295 	/* read "misc output register" */
    296 	regval = bus_space_read_1(iot, ioh_vga, 0xc);
    297 	mono = !(regval & 1);
    298 
    299 	if (bus_space_map(iot, (mono ? 0x3b0 : 0x3d0), 0x10, 0, &ioh_6845))
    300 		goto bad;
    301 	gotio_6845 = 1;
    302 
    303 	if (bus_space_map(memt, 0xa0000, 0x20000, 0, &memh))
    304 		goto bad;
    305 	gotmem = 1;
    306 
    307 	dispoffset = (mono ? 0x10000 : 0x18000);
    308 
    309 	vgadata = bus_space_read_2(memt, memh, dispoffset);
    310 	bus_space_write_2(memt, memh, dispoffset, 0xa55a);
    311 	if (bus_space_read_2(memt, memh, dispoffset) != 0xa55a)
    312 		goto bad;
    313 	bus_space_write_2(memt, memh, dispoffset, vgadata);
    314 
    315 	/*
    316 	 * check if this is really a VGA
    317 	 * (try to write "Color Select" register as XFree86 does)
    318 	 * XXX check before if at least EGA?
    319 	 */
    320 	/* reset state */
    321 	(void) bus_space_read_1(iot, ioh_6845, 10);
    322 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
    323 			  20 | 0x20); /* colselect | enable */
    324 	regval = bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR);
    325 	/* toggle the implemented bits */
    326 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval ^ 0x0f);
    327 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
    328 			  20 | 0x20);
    329 	/* read back */
    330 	if (bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR) != (regval ^ 0x0f))
    331 		goto bad;
    332 	/* restore contents */
    333 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval);
    334 
    335 	rv = 1;
    336 bad:
    337 	if (gotio_vga)
    338 		bus_space_unmap(iot, ioh_vga, 0x10);
    339 	if (gotio_6845)
    340 		bus_space_unmap(iot, ioh_6845, 0x10);
    341 	if (gotmem)
    342 		bus_space_unmap(memt, memh, 0x20000);
    343 
    344 	return (rv);
    345 }
    346 
    347 /*
    348  * We want at least ASCII 32..127 be present in the
    349  * first font slot.
    350  */
    351 #define vga_valid_primary_font(f) \
    352 	(f->wsfont->encoding == WSDISPLAY_FONTENC_IBM || \
    353 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO || \
    354 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO7)
    355 
    356 struct egavga_font *
    357 egavga_getfont(struct vga_config *vc, struct vgascreen *scr, char *name,
    358 	       int primary)
    359 {
    360 	struct egavga_font *f;
    361 	int cookie;
    362 	struct wsdisplay_font *wf;
    363 
    364 	TAILQ_FOREACH(f, &vc->vc_fontlist, next) {
    365 		if (wsfont_matches(f->wsfont, name,
    366 				   8, scr->pcs.type->fontheight, 0) &&
    367 		    (!primary || vga_valid_primary_font(f))) {
    368 #ifdef VGAFONTDEBUG
    369 			if (scr != &vga_console_screen || vga_console_attached)
    370 				printf("vga_getfont: %s already present\n",
    371 				       name ? name : "<default>");
    372 #endif
    373 			goto found;
    374 		}
    375 	}
    376 
    377 	cookie = wsfont_find(name, 8, scr->pcs.type->fontheight, 0,
    378 	                     WSDISPLAY_FONTORDER_L2R, 0);
    379 	/* XXX obey "primary" */
    380 	if (cookie == -1) {
    381 #ifdef VGAFONTDEBUG
    382 		if (scr != &vga_console_screen || vga_console_attached)
    383 			printf("vga_getfont: %s not found\n",
    384 			       name ? name : "<default>");
    385 #endif
    386 		return (0);
    387 	}
    388 
    389 	if (wsfont_lock(cookie, &wf))
    390 		return (0);
    391 
    392 #ifdef VGA_CONSOLE_SCREENTYPE
    393 	if (scr == &vga_console_screen)
    394 		f = &vga_consolefont;
    395 	else
    396 #endif
    397 	f = malloc(sizeof(struct egavga_font), M_DEVBUF, M_NOWAIT);
    398 	if (!f) {
    399 		wsfont_unlock(cookie);
    400 		return (0);
    401 	}
    402 	f->wsfont = wf;
    403 	f->cookie = cookie;
    404 	f->slot = -1; /* not yet loaded */
    405 	f->usecount = 0; /* incremented below */
    406 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
    407 
    408 found:
    409 	f->usecount++;
    410 #ifdef VGAFONTDEBUG
    411 	if (scr != &vga_console_screen || vga_console_attached)
    412 		printf("vga_getfont: usecount=%d\n", f->usecount);
    413 #endif
    414 	return (f);
    415 }
    416 
    417 void
    418 egavga_unreffont(struct vga_config *vc, struct egavga_font *f)
    419 {
    420 
    421 	f->usecount--;
    422 #ifdef VGAFONTDEBUG
    423 	printf("vga_unreffont: usecount=%d\n", f->usecount);
    424 #endif
    425 	if (f->usecount == 0 && f->cookie != -1) {
    426 		TAILQ_REMOVE(&vc->vc_fontlist, f, next);
    427 		if (f->slot != -1) {
    428 			KASSERT(vc->vc_fonts[f->slot] == f);
    429 			vc->vc_fonts[f->slot] = 0;
    430 		}
    431 		wsfont_unlock(f->cookie);
    432 #ifdef VGA_CONSOLE_SCREENTYPE
    433 		if (f != &vga_consolefont)
    434 #endif
    435 		free(f, M_DEVBUF);
    436 	}
    437 }
    438 
    439 int
    440 vga_selectfont(struct vga_config *vc, struct vgascreen *scr, char *name1,
    441 	       char *name2)
    442 {
    443 	const struct wsscreen_descr *type = scr->pcs.type;
    444 	struct egavga_font *f1, *f2;
    445 
    446 	f1 = egavga_getfont(vc, scr, name1, 1);
    447 	if (!f1)
    448 		return (ENXIO);
    449 
    450 	if (VGA_SCREEN_CANTWOFONTS(type) && name2) {
    451 		f2 = egavga_getfont(vc, scr, name2, 0);
    452 		if (!f2) {
    453 			egavga_unreffont(vc, f1);
    454 			return (ENXIO);
    455 		}
    456 	} else
    457 		f2 = 0;
    458 
    459 #ifdef VGAFONTDEBUG
    460 	if (scr != &vga_console_screen || vga_console_attached) {
    461 		printf("vga (%s): font1=%s (slot %d)", type->name,
    462 		       f1->wsfont->name, f1->slot);
    463 		if (f2)
    464 			printf(", font2=%s (slot %d)",
    465 			       f2->wsfont->name, f2->slot);
    466 		printf("\n");
    467 	}
    468 #endif
    469 	if (scr->fontset1)
    470 		egavga_unreffont(vc, scr->fontset1);
    471 	scr->fontset1 = f1;
    472 	if (scr->fontset2)
    473 		egavga_unreffont(vc, scr->fontset2);
    474 	scr->fontset2 = f2;
    475 	return (0);
    476 }
    477 
    478 void
    479 vga_init_screen(struct vga_config *vc, struct vgascreen *scr,
    480 		const struct wsscreen_descr *type, int existing, long *attrp)
    481 {
    482 	int cpos;
    483 	int res;
    484 
    485 	scr->cfg = vc;
    486 	scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
    487 	scr->pcs.type = type;
    488 	scr->pcs.active = existing;
    489 	scr->mindispoffset = 0;
    490 	scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
    491 
    492 	if (existing) {
    493 		vc->active = scr;
    494 
    495 		cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
    496 		cpos |= vga_6845_read(&vc->hdl, cursorl);
    497 
    498 		/* make sure we have a valid cursor position */
    499 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    500 			cpos = 0;
    501 
    502 		scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
    503 		scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
    504 
    505 		/* make sure we have a valid memory offset */
    506 		if (scr->pcs.dispoffset < scr->mindispoffset ||
    507 		    scr->pcs.dispoffset > scr->maxdispoffset)
    508 			scr->pcs.dispoffset = scr->mindispoffset;
    509 
    510 		if (type != vc->currenttype) {
    511 			vga_setscreentype(&vc->hdl, type);
    512 			vc->currenttype = type;
    513 		}
    514 	} else {
    515 		cpos = 0;
    516 		scr->pcs.dispoffset = scr->mindispoffset;
    517 	}
    518 
    519 	scr->pcs.cursorrow = cpos / type->ncols;
    520 	scr->pcs.cursorcol = cpos % type->ncols;
    521 	pcdisplay_cursor_init(&scr->pcs, existing);
    522 
    523 #ifdef __alpha__
    524 	if (!vc->hdl.vh_mono)
    525 		/*
    526 		 * DEC firmware uses a blue background.
    527 		 */
    528 		res = vga_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
    529 				     WSATTR_WSCOLORS, attrp);
    530 	else
    531 #endif
    532 	res = vga_allocattr(scr, 0, 0, 0, attrp);
    533 #ifdef DIAGNOSTIC
    534 	if (res)
    535 		panic("vga_init_screen: attribute botch");
    536 #endif
    537 
    538 	scr->pcs.mem = NULL;
    539 
    540 	wsfont_init();
    541 	scr->fontset1 = scr->fontset2 = 0;
    542 	if (vga_selectfont(vc, scr, 0, 0)) {
    543 		if (scr == &vga_console_screen)
    544 			panic("vga_init_screen: no font");
    545 		else
    546 			printf("vga_init_screen: no font\n");
    547 	}
    548 	if (existing)
    549 		vga_setfont(vc, scr);
    550 
    551 	vc->nscreens++;
    552 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    553 }
    554 
    555 void
    556 vga_init(struct vga_config *vc, bus_space_tag_t iot, bus_space_tag_t memt)
    557 {
    558 	struct vga_handle *vh = &vc->hdl;
    559 	u_int8_t mor;
    560 	int i;
    561 
    562         vh->vh_iot = iot;
    563         vh->vh_memt = memt;
    564 
    565         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    566                 panic("vga_init: couldn't map vga io");
    567 
    568 	/* read "misc output register" */
    569 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, 0xc);
    570 	vh->vh_mono = !(mor & 1);
    571 
    572 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    573 			  &vh->vh_ioh_6845))
    574                 panic("vga_init: couldn't map 6845 io");
    575 
    576         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    577                 panic("vga_init: couldn't map memory");
    578 
    579         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
    580 				(vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
    581 				&vh->vh_memh))
    582                 panic("vga_init: mem subrange failed");
    583 
    584 	/* should only reserve the space (no need to map - save KVM) */
    585 	vc->vc_biostag = memt;
    586 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0,
    587 			  &vc->vc_bioshdl))
    588 		vc->vc_biosmapped = 0;
    589 	else
    590 		vc->vc_biosmapped = 1;
    591 
    592 	vc->nscreens = 0;
    593 	LIST_INIT(&vc->screens);
    594 	vc->active = NULL;
    595 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
    596 	callout_init(&vc->vc_switch_callout);
    597 
    598 	vc->vc_fonts[0] = &vga_builtinfont;
    599 	for (i = 1; i < 8; i++)
    600 		vc->vc_fonts[i] = 0;
    601 	TAILQ_INIT(&vc->vc_fontlist);
    602 	TAILQ_INSERT_HEAD(&vc->vc_fontlist, &vga_builtinfont, next);
    603 
    604 	vc->currentfontset1 = vc->currentfontset2 = 0;
    605 }
    606 
    607 void
    608 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
    609 		  bus_space_tag_t memt, int type, int quirks,
    610 		  const struct vga_funcs *vf)
    611 {
    612 	int console;
    613 	struct vga_config *vc;
    614 	struct wsemuldisplaydev_attach_args aa;
    615 
    616 	console = vga_is_console(iot, type);
    617 
    618 	if (console) {
    619 		vc = &vga_console_vc;
    620 		vga_console_attached = 1;
    621 	} else {
    622 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
    623 		vga_init(vc, iot, memt);
    624 	}
    625 
    626 	if (quirks & VGA_QUIRK_ONEFONT) {
    627 		vc->vc_nfontslots = 1;
    628 #ifndef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    629 		/*
    630 		 * XXX maybe invalidate font in slot > 0, but this can
    631 		 * only be happen with VGA_CONSOLE_SCREENTYPE, and then
    632 		 * we require VGA_CONSOLE_ATI_BROKEN_FONTSEL anyway.
    633 		 */
    634 #endif
    635 	} else {
    636 		vc->vc_nfontslots = 8;
    637 #ifndef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    638 		/*
    639 		 * XXX maybe validate builtin font shifted to slot 1 if
    640 		 * slot 0 got overwritten because of VGA_CONSOLE_SCREENTYPE,
    641 		 * but it will be reloaded anyway if needed.
    642 		 */
    643 #endif
    644 	}
    645 
    646 	/*
    647 	 * Save the builtin font to memory. In case it got overwritten
    648 	 * in console initialization, use the copy in slot 1.
    649 	 */
    650 #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    651 #define BUILTINFONTLOC (vga_builtinfont.slot == -1 ? 1 : 0)
    652 #else
    653 	KASSERT(vga_builtinfont.slot == 0);
    654 #define BUILTINFONTLOC (0)
    655 #endif
    656 	vga_builtinfont.wsfont->data =
    657 		malloc(256 * vga_builtinfont.wsfont->fontheight,
    658 		       M_DEVBUF, M_WAITOK);
    659 	vga_readoutchars(&vc->hdl, BUILTINFONTLOC, 0, 256,
    660 			 vga_builtinfont.wsfont->fontheight,
    661 			 vga_builtinfont.wsfont->data);
    662 
    663 	vc->vc_type = type;
    664 	vc->vc_funcs = vf;
    665 
    666 	sc->sc_vc = vc;
    667 	vc->softc = sc;
    668 
    669 	aa.console = console;
    670 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    671 	aa.accessops = &vga_accessops;
    672 	aa.accesscookie = vc;
    673 
    674         config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    675 }
    676 
    677 int
    678 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
    679 {
    680 	long defattr;
    681 	const struct wsscreen_descr *scr;
    682 
    683 	if (check && !vga_common_probe(iot, memt))
    684 		return (ENXIO);
    685 
    686 	/* set up bus-independent VGA configuration */
    687 	vga_init(&vga_console_vc, iot, memt);
    688 #ifdef VGA_CONSOLE_SCREENTYPE
    689 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    690 	       &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
    691 	if (!scr)
    692 		panic("vga_cnattach: invalid screen type");
    693 #else
    694 	scr = vga_console_vc.currenttype;
    695 #endif
    696 #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    697 	/*
    698 	 * On some (most/all?) ATI cards, only font slot 0 is usable.
    699 	 * vga_init_screen() might need font slot 0 for a non-default
    700 	 * console font, so save the builtin VGA font to another font slot.
    701 	 * The attach() code will take care later.
    702 	 */
    703 	vga_copyfont01(&vga_console_vc.hdl);
    704 	vga_console_vc.vc_nfontslots = 1;
    705 #else
    706 	vga_console_vc.vc_nfontslots = 8;
    707 #endif
    708 	vga_init_screen(&vga_console_vc, &vga_console_screen, scr, 1, &defattr);
    709 
    710 	wsdisplay_cnattach(scr, &vga_console_screen,
    711 			   vga_console_screen.pcs.cursorcol,
    712 			   vga_console_screen.pcs.cursorrow,
    713 			   defattr);
    714 
    715 	vgaconsole = 1;
    716 	vga_console_type = type;
    717 	return (0);
    718 }
    719 
    720 int
    721 vga_is_console(bus_space_tag_t iot, int type)
    722 {
    723 	if (vgaconsole &&
    724 	    !vga_console_attached &&
    725 	    iot == vga_console_vc.hdl.vh_iot &&
    726 	    (vga_console_type == -1 || (type == vga_console_type)))
    727 		return (1);
    728 	return (0);
    729 }
    730 
    731 #define	VGA_TS_BLANK	0x20
    732 
    733 static int
    734 vga_get_video(struct vga_config *vc)
    735 {
    736 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_BLANK) == 0;
    737 }
    738 
    739 static void
    740 vga_set_video(struct vga_config *vc, int state)
    741 {
    742 	int val;
    743 
    744 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    745 	if (state) {					/* unblank screen */
    746 		val = vga_ts_read(&vc->hdl, mode);
    747 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_BLANK);
    748 #ifndef VGA_NO_VBLANK
    749 		val = vga_6845_read(&vc->hdl, mode);
    750 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    751 #endif
    752 	} else {					/* blank screen */
    753 		val = vga_ts_read(&vc->hdl, mode);
    754 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_BLANK);
    755 #ifndef VGA_NO_VBLANK
    756 		val = vga_6845_read(&vc->hdl, mode);
    757 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    758 #endif
    759 	}
    760 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    761 }
    762 
    763 int
    764 vga_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    765 {
    766 	struct vga_config *vc = v;
    767 	const struct vga_funcs *vf = vc->vc_funcs;
    768 
    769 	switch (cmd) {
    770 	case WSDISPLAYIO_GTYPE:
    771 		*(int *)data = vc->vc_type;
    772 		return 0;
    773 
    774 	case WSDISPLAYIO_GINFO:
    775 		/* XXX should get detailed hardware information here */
    776 		return EPASSTHROUGH;
    777 
    778 	case WSDISPLAYIO_GVIDEO:
    779 		*(int *)data = (vga_get_video(vc) ? WSDISPLAYIO_VIDEO_ON :
    780 				WSDISPLAYIO_VIDEO_OFF);
    781 		return 0;
    782 
    783 	case WSDISPLAYIO_SVIDEO:
    784 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    785 		return 0;
    786 
    787 	case WSDISPLAYIO_GETCMAP:
    788 	case WSDISPLAYIO_PUTCMAP:
    789 	case WSDISPLAYIO_GCURPOS:
    790 	case WSDISPLAYIO_SCURPOS:
    791 	case WSDISPLAYIO_GCURMAX:
    792 	case WSDISPLAYIO_GCURSOR:
    793 	case WSDISPLAYIO_SCURSOR:
    794 		/* NONE of these operations are by the generic VGA driver. */
    795 		return EPASSTHROUGH;
    796 	}
    797 
    798 	if (vc->vc_funcs == NULL)
    799 		return (EPASSTHROUGH);
    800 
    801 	if (vf->vf_ioctl == NULL)
    802 		return (EPASSTHROUGH);
    803 
    804 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
    805 }
    806 
    807 static paddr_t
    808 vga_mmap(void *v, off_t offset, int prot)
    809 {
    810 	struct vga_config *vc = v;
    811 	const struct vga_funcs *vf = vc->vc_funcs;
    812 
    813 	if (vc->vc_funcs == NULL)
    814 		return (-1);
    815 
    816 	if (vf->vf_mmap == NULL)
    817 		return (-1);
    818 
    819 	return ((*vf->vf_mmap)(v, offset, prot));
    820 }
    821 
    822 int
    823 vga_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    824 		 int *curxp, int *curyp, long *defattrp)
    825 {
    826 	struct vga_config *vc = v;
    827 	struct vgascreen *scr;
    828 
    829 	if (vc->nscreens == 1) {
    830 		struct vgascreen *scr1 = vc->screens.lh_first;
    831 		/*
    832 		 * When allocating the second screen, get backing store
    833 		 * for the first one too.
    834 		 * XXX We could be more clever and use video RAM.
    835 		 */
    836 		scr1->pcs.mem =
    837 		  malloc(scr1->pcs.type->ncols * scr1->pcs.type->nrows * 2,
    838 			 M_DEVBUF, M_WAITOK);
    839 	}
    840 
    841 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    842 	vga_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    843 
    844 	if (vc->nscreens > 1) {
    845 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
    846 				      M_DEVBUF, M_WAITOK);
    847 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
    848 	}
    849 
    850 	*cookiep = scr;
    851 	*curxp = scr->pcs.cursorcol;
    852 	*curyp = scr->pcs.cursorrow;
    853 
    854 	return (0);
    855 }
    856 
    857 void
    858 vga_free_screen(void *v, void *cookie)
    859 {
    860 	struct vgascreen *vs = cookie;
    861 	struct vga_config *vc = vs->cfg;
    862 
    863 	LIST_REMOVE(vs, next);
    864 	if (vs->fontset1)
    865 		egavga_unreffont(vc, vs->fontset1);
    866 	if (vs->fontset2)
    867 		egavga_unreffont(vc, vs->fontset2);
    868 
    869 	if (vs != &vga_console_screen)
    870 		free(vs, M_DEVBUF);
    871 	else
    872 		panic("vga_free_screen: console");
    873 
    874 	if (vc->active == vs)
    875 		vc->active = 0;
    876 }
    877 
    878 static void vga_usefont(struct vga_config *, struct egavga_font *);
    879 
    880 static void
    881 vga_usefont(struct vga_config *vc, struct egavga_font *f)
    882 {
    883 	int slot;
    884 	struct egavga_font *of;
    885 
    886 	if (f->slot != -1)
    887 		goto toend;
    888 
    889 	for (slot = 0; slot < vc->vc_nfontslots; slot++) {
    890 		if (!vc->vc_fonts[slot])
    891 			goto loadit;
    892 	}
    893 
    894 	/* have to kick out another one */
    895 	TAILQ_FOREACH(of, &vc->vc_fontlist, next) {
    896 		if (of->slot != -1) {
    897 			KASSERT(vc->vc_fonts[of->slot] == of);
    898 			slot = of->slot;
    899 			of->slot = -1;
    900 			goto loadit;
    901 		}
    902 	}
    903 	panic("vga_usefont");
    904 
    905 loadit:
    906 	vga_loadchars(&vc->hdl, slot, f->wsfont->firstchar,
    907 		      f->wsfont->numchars, f->wsfont->fontheight,
    908 		      f->wsfont->data);
    909 	f->slot = slot;
    910 	vc->vc_fonts[slot] = f;
    911 
    912 toend:
    913 	TAILQ_REMOVE(&vc->vc_fontlist, f, next);
    914 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
    915 }
    916 
    917 static void
    918 vga_setfont(struct vga_config *vc, struct vgascreen *scr)
    919 {
    920 	int fontslot1, fontslot2;
    921 
    922 	if (scr->fontset1)
    923 		vga_usefont(vc, scr->fontset1);
    924 	if (scr->fontset2)
    925 		vga_usefont(vc, scr->fontset2);
    926 
    927 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
    928 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
    929 	if (vc->currentfontset1 != fontslot1 ||
    930 	    vc->currentfontset2 != fontslot2) {
    931 		vga_setfontset(&vc->hdl, fontslot1, fontslot2);
    932 		vc->currentfontset1 = fontslot1;
    933 		vc->currentfontset2 = fontslot2;
    934 	}
    935 }
    936 
    937 int
    938 vga_show_screen(void *v, void *cookie, int waitok,
    939 		void (*cb)(void *, int, int), void *cbarg)
    940 {
    941 	struct vgascreen *scr = cookie, *oldscr;
    942 	struct vga_config *vc = scr->cfg;
    943 
    944 	oldscr = vc->active; /* can be NULL! */
    945 	if (scr == oldscr) {
    946 		return (0);
    947 	}
    948 
    949 	vc->wantedscreen = cookie;
    950 	vc->switchcb = cb;
    951 	vc->switchcbarg = cbarg;
    952 	if (cb) {
    953 		callout_reset(&vc->vc_switch_callout, 0,
    954 		    (void(*)(void *))vga_doswitch, vc);
    955 		return (EAGAIN);
    956 	}
    957 
    958 	vga_doswitch(vc);
    959 	return (0);
    960 }
    961 
    962 void
    963 vga_doswitch(struct vga_config *vc)
    964 {
    965 	struct vgascreen *scr, *oldscr;
    966 	struct vga_handle *vh = &vc->hdl;
    967 	const struct wsscreen_descr *type;
    968 
    969 	scr = vc->wantedscreen;
    970 	if (!scr) {
    971 		printf("vga_doswitch: disappeared\n");
    972 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    973 		return;
    974 	}
    975 	type = scr->pcs.type;
    976 	oldscr = vc->active; /* can be NULL! */
    977 #ifdef DIAGNOSTIC
    978 	if (oldscr) {
    979 		if (!oldscr->pcs.active)
    980 			panic("vga_show_screen: not active");
    981 		if (oldscr->pcs.type != vc->currenttype)
    982 			panic("vga_show_screen: bad type");
    983 	}
    984 #endif
    985 	if (scr == oldscr) {
    986 		return;
    987 	}
    988 #ifdef DIAGNOSTIC
    989 	if (scr->pcs.active)
    990 		panic("vga_show_screen: active");
    991 #endif
    992 
    993 	if (oldscr) {
    994 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
    995 
    996 		oldscr->pcs.active = 0;
    997 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
    998 					oldscr->pcs.dispoffset, oldscr->pcs.mem,
    999 					oldtype->ncols * oldtype->nrows);
   1000 	}
   1001 
   1002 	if (vc->currenttype != type) {
   1003 		vga_setscreentype(vh, type);
   1004 		vc->currenttype = type;
   1005 	}
   1006 
   1007 	vga_setfont(vc, scr);
   1008 	/* XXX swich colours! */
   1009 
   1010 	scr->pcs.dispoffset = scr->mindispoffset;
   1011 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
   1012 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
   1013 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
   1014 	}
   1015 
   1016 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
   1017 				scr->pcs.dispoffset, scr->pcs.mem,
   1018 				type->ncols * type->nrows);
   1019 	scr->pcs.active = 1;
   1020 
   1021 	vc->active = scr;
   1022 
   1023 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
   1024 			 scr->pcs.cursorrow, scr->pcs.cursorcol);
   1025 
   1026 	vc->wantedscreen = 0;
   1027 	if (vc->switchcb)
   1028 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
   1029 }
   1030 
   1031 static int
   1032 vga_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1033 {
   1034 	struct vga_config *vc = v;
   1035 	struct vgascreen *scr = cookie;
   1036 	char *name2;
   1037 	int res;
   1038 
   1039 	if (scr) {
   1040 		name2 = NULL;
   1041 		if (data->name) {
   1042 			name2 = strchr(data->name, ',');
   1043 			if (name2)
   1044 				*name2++ = '\0';
   1045 		}
   1046 		res = vga_selectfont(vc, scr, data->name, name2);
   1047 		if (!res && scr->pcs.active)
   1048 			vga_setfont(vc, scr);
   1049 		return (res);
   1050 	}
   1051 
   1052 	return (0);
   1053 }
   1054 
   1055 static int
   1056 vga_allocattr(void *id, int fg, int bg, int flags, long *attrp)
   1057 {
   1058 	struct vgascreen *scr = id;
   1059 	struct vga_config *vc = scr->cfg;
   1060 
   1061 	if (vc->hdl.vh_mono) {
   1062 		if (flags & WSATTR_WSCOLORS)
   1063 			return (EINVAL);
   1064 		if (flags & WSATTR_REVERSE)
   1065 			*attrp = 0x70;
   1066 		else
   1067 			*attrp = 0x07;
   1068 		if (flags & WSATTR_UNDERLINE)
   1069 			*attrp |= FG_UNDERLINE;
   1070 		if (flags & WSATTR_HILIT)
   1071 			*attrp |= FG_INTENSE;
   1072 	} else {
   1073 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1074 			return (EINVAL);
   1075 		if (flags & WSATTR_WSCOLORS)
   1076 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1077 		else
   1078 			*attrp = 7;
   1079 		if (flags & WSATTR_HILIT)
   1080 			*attrp += 8;
   1081 	}
   1082 	if (flags & WSATTR_BLINK)
   1083 		*attrp |= FG_BLINK;
   1084 	return (0);
   1085 }
   1086 
   1087 static void
   1088 vga_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1089 {
   1090 	struct vgascreen *scr = id;
   1091 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
   1092 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
   1093 	int ncols = scr->pcs.type->ncols;
   1094 	bus_size_t srcoff, dstoff;
   1095 
   1096 	srcoff = srcrow * ncols + 0;
   1097 	dstoff = dstrow * ncols + 0;
   1098 
   1099 	if (scr->pcs.active) {
   1100 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
   1101 #ifdef PCDISPLAY_SOFTCURSOR
   1102 			int cursoron = scr->pcs.cursoron;
   1103 
   1104 			if (cursoron)
   1105 				pcdisplay_cursor(&scr->pcs, 0,
   1106 				    scr->pcs.cursorrow, scr->pcs.cursorcol);
   1107 #endif
   1108 			/* scroll up whole screen */
   1109 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
   1110 			    <= scr->maxdispoffset) {
   1111 				scr->pcs.dispoffset += srcrow * ncols * 2;
   1112 			} else {
   1113 				bus_space_copy_region_2(memt, memh,
   1114 					scr->pcs.dispoffset + srcoff * 2,
   1115 					memh, scr->mindispoffset,
   1116 					nrows * ncols);
   1117 				scr->pcs.dispoffset = scr->mindispoffset;
   1118 			}
   1119 			vga_6845_write(&scr->cfg->hdl, startadrh,
   1120 				       scr->pcs.dispoffset >> 9);
   1121 			vga_6845_write(&scr->cfg->hdl, startadrl,
   1122 				       scr->pcs.dispoffset >> 1);
   1123 #ifdef PCDISPLAY_SOFTCURSOR
   1124 			if (cursoron)
   1125 				pcdisplay_cursor(&scr->pcs, 1,
   1126 				    scr->pcs.cursorrow, scr->pcs.cursorcol);
   1127 #endif
   1128 		} else {
   1129 			bus_space_copy_region_2(memt, memh,
   1130 					scr->pcs.dispoffset + srcoff * 2,
   1131 					memh, scr->pcs.dispoffset + dstoff * 2,
   1132 					nrows * ncols);
   1133 		}
   1134 	} else
   1135 		memcpy(&scr->pcs.mem[dstoff], &scr->pcs.mem[srcoff],
   1136 		      nrows * ncols * 2);
   1137 }
   1138 
   1139 #ifdef WSCONS_SUPPORT_PCVTFONTS
   1140 
   1141 #define NOTYET 0xffff
   1142 static const u_int16_t pcvt_unichars[0xa0] = {
   1143 /* 0 */	_e006U, /* N/L control */
   1144 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1145 	NOTYET,
   1146 	0x2409, /* SYMBOL FOR HORIZONTAL TABULATION */
   1147 	0x240a, /* SYMBOL FOR LINE FEED */
   1148 	0x240b, /* SYMBOL FOR VERTICAL TABULATION */
   1149 	0x240c, /* SYMBOL FOR FORM FEED */
   1150 	0x240d, /* SYMBOL FOR CARRIAGE RETURN */
   1151 	NOTYET, NOTYET,
   1152 /* 1 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1153 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1154 /* 2 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1155 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1156 /* 3 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1157 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1158 /* 4 */	0x03c1, /* GREEK SMALL LETTER RHO */
   1159 	0x03c8, /* GREEK SMALL LETTER PSI */
   1160 	0x2202, /* PARTIAL DIFFERENTIAL */
   1161 	0x03bb, /* GREEK SMALL LETTER LAMDA */
   1162 	0x03b9, /* GREEK SMALL LETTER IOTA */
   1163 	0x03b7, /* GREEK SMALL LETTER ETA */
   1164 	0x03b5, /* GREEK SMALL LETTER EPSILON */
   1165 	0x03c7, /* GREEK SMALL LETTER CHI */
   1166 	0x2228, /* LOGICAL OR */
   1167 	0x2227, /* LOGICAL AND */
   1168 	0x222a, /* UNION */
   1169 	0x2283, /* SUPERSET OF */
   1170 	0x2282, /* SUBSET OF */
   1171 	0x03a5, /* GREEK CAPITAL LETTER UPSILON */
   1172 	0x039e, /* GREEK CAPITAL LETTER XI */
   1173 	0x03a8, /* GREEK CAPITAL LETTER PSI */
   1174 /* 5 */	0x03a0, /* GREEK CAPITAL LETTER PI */
   1175 	0x21d2, /* RIGHTWARDS DOUBLE ARROW */
   1176 	0x21d4, /* LEFT RIGHT DOUBLE ARROW */
   1177 	0x039b, /* GREEK CAPITAL LETTER LAMDA */
   1178 	0x0398, /* GREEK CAPITAL LETTER THETA */
   1179 	0x2243, /* ASYMPTOTICALLY EQUAL TO */
   1180 	0x2207, /* NABLA */
   1181 	0x2206, /* INCREMENT */
   1182 	0x221d, /* PROPORTIONAL TO */
   1183 	0x2234, /* THEREFORE */
   1184 	0x222b, /* INTEGRAL */
   1185 	0x2215, /* DIVISION SLASH */
   1186 	0x2216, /* SET MINUS */
   1187 	_e00eU, /* angle? */
   1188 	_e00dU, /* inverted angle? */
   1189 	_e00bU, /* braceleftmid */
   1190 /* 6 */	_e00cU, /* bracerightmid */
   1191 	_e007U, /* bracelefttp */
   1192 	_e008U, /* braceleftbt */
   1193 	_e009U, /* bracerighttp */
   1194 	_e00aU, /* bracerightbt */
   1195 	0x221a, /* SQUARE ROOT */
   1196 	0x03c9, /* GREEK SMALL LETTER OMEGA */
   1197 	0x00a5, /* YEN SIGN */
   1198 	0x03be, /* GREEK SMALL LETTER XI */
   1199 	0x00fd, /* LATIN SMALL LETTER Y WITH ACUTE */
   1200 	0x00fe, /* LATIN SMALL LETTER THORN */
   1201 	0x00f0, /* LATIN SMALL LETTER ETH */
   1202 	0x00de, /* LATIN CAPITAL LETTER THORN */
   1203 	0x00dd, /* LATIN CAPITAL LETTER Y WITH ACUTE */
   1204 	0x00d7, /* MULTIPLICATION SIGN */
   1205 	0x00d0, /* LATIN CAPITAL LETTER ETH */
   1206 /* 7 */	0x00be, /* VULGAR FRACTION THREE QUARTERS */
   1207 	0x00b8, /* CEDILLA */
   1208 	0x00b4, /* ACUTE ACCENT */
   1209 	0x00af, /* MACRON */
   1210 	0x00ae, /* REGISTERED SIGN */
   1211 	0x00ad, /* SOFT HYPHEN */
   1212 	0x00ac, /* NOT SIGN */
   1213 	0x00a8, /* DIAERESIS */
   1214 	0x2260, /* NOT EQUAL TO */
   1215 	_e005U, /* scan 9 */
   1216 	_e004U, /* scan 7 */
   1217 	_e003U, /* scan 5 */
   1218 	_e002U, /* scan 3 */
   1219 	_e001U, /* scan 1 */
   1220 	0x03c5, /* GREEK SMALL LETTER UPSILON */
   1221 	0x00f8, /* LATIN SMALL LETTER O WITH STROKE */
   1222 /* 8 */	0x0153, /* LATIN SMALL LIGATURE OE */
   1223 	0x00f5, /* LATIN SMALL LETTER O WITH TILDE !!!doc bug */
   1224 	0x00e3, /* LATIN SMALL LETTER A WITH TILDE */
   1225 	0x0178, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
   1226 	0x00db, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
   1227 	0x00da, /* LATIN CAPITAL LETTER U WITH ACUTE */
   1228 	0x00d9, /* LATIN CAPITAL LETTER U WITH GRAVE */
   1229 	0x00d8, /* LATIN CAPITAL LETTER O WITH STROKE */
   1230 	0x0152, /* LATIN CAPITAL LIGATURE OE */
   1231 	0x00d5, /* LATIN CAPITAL LETTER O WITH TILDE */
   1232 	0x00d4, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
   1233 	0x00d3, /* LATIN CAPITAL LETTER O WITH ACUTE */
   1234 	0x00d2, /* LATIN CAPITAL LETTER O WITH GRAVE */
   1235 	0x00cf, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
   1236 	0x00ce, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
   1237 	0x00cd, /* LATIN CAPITAL LETTER I WITH ACUTE */
   1238 /* 9 */	0x00cc, /* LATIN CAPITAL LETTER I WITH GRAVE */
   1239 	0x00cb, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
   1240 	0x00ca, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
   1241 	0x00c8, /* LATIN CAPITAL LETTER E WITH GRAVE */
   1242 	0x00c3, /* LATIN CAPITAL LETTER A WITH TILDE */
   1243 	0x00c2, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
   1244 	0x00c1, /* LATIN CAPITAL LETTER A WITH ACUTE */
   1245 	0x00c0, /* LATIN CAPITAL LETTER A WITH GRAVE */
   1246 	0x00b9, /* SUPERSCRIPT ONE */
   1247 	0x00b7, /* MIDDLE DOT */
   1248 	0x03b6, /* GREEK SMALL LETTER ZETA */
   1249 	0x00b3, /* SUPERSCRIPT THREE */
   1250 	0x00a9, /* COPYRIGHT SIGN */
   1251 	0x00a4, /* CURRENCY SIGN */
   1252 	0x03ba, /* GREEK SMALL LETTER KAPPA */
   1253 	_e000U  /* mirrored question mark? */
   1254 };
   1255 
   1256 static int vga_pcvt_mapchar(int, u_int *);
   1257 
   1258 static int
   1259 vga_pcvt_mapchar(int uni, u_int *index)
   1260 {
   1261 	int i;
   1262 
   1263 	for (i = 0; i < 0xa0; i++) /* 0xa0..0xff are reserved */
   1264 		if (uni == pcvt_unichars[i]) {
   1265 			*index = i;
   1266 			return (5);
   1267 		}
   1268 	*index = 0x99; /* middle dot */
   1269 	return (0);
   1270 }
   1271 
   1272 #endif /* WSCONS_SUPPORT_PCVTFONTS */
   1273 
   1274 #ifdef WSCONS_SUPPORT_ISO7FONTS
   1275 
   1276 static int
   1277 vga_iso7_mapchar(int uni, u_int *index)
   1278 {
   1279 
   1280 	/*
   1281 	 * U+0384 (GREEK TONOS) to
   1282 	 * U+03ce (GREEK SMALL LETTER OMEGA WITH TONOS)
   1283 	 * map directly to the iso-9 font
   1284 	 */
   1285 	if (uni >= 0x0384 && uni <= 0x03ce) {
   1286 		/* U+0384 is at offset 0xb4 in the font */
   1287 		*index = uni - 0x0384 + 0xb4;
   1288 		return (5);
   1289 	}
   1290 
   1291 	/* XXX more chars in the iso-9 font */
   1292 
   1293 	*index = 0xa4; /* shaded rectangle */
   1294 	return (0);
   1295 }
   1296 
   1297 #endif /* WSCONS_SUPPORT_ISO7FONTS */
   1298 
   1299 static int _vga_mapchar(void *, const struct egavga_font *, int, u_int *);
   1300 
   1301 static int
   1302 _vga_mapchar(void *id, const struct egavga_font *font, int uni, u_int *index)
   1303 {
   1304 
   1305 	switch (font->wsfont->encoding) {
   1306 	case WSDISPLAY_FONTENC_ISO:
   1307 		if (uni < 256) {
   1308 			*index = uni;
   1309 			return (5);
   1310 		} else {
   1311 			*index = ' ';
   1312 			return (0);
   1313 		}
   1314 		break;
   1315 	case WSDISPLAY_FONTENC_IBM:
   1316 		return (pcdisplay_mapchar(id, uni, index));
   1317 #ifdef WSCONS_SUPPORT_PCVTFONTS
   1318 	case WSDISPLAY_FONTENC_PCVT:
   1319 		return (vga_pcvt_mapchar(uni, index));
   1320 #endif
   1321 #ifdef WSCONS_SUPPORT_ISO7FONTS
   1322 	case WSDISPLAY_FONTENC_ISO7:
   1323 		return (vga_iso7_mapchar(uni, index));
   1324 #endif
   1325 	default:
   1326 #ifdef VGAFONTDEBUG
   1327 		printf("_vga_mapchar: encoding=%d\n", font->wsfont->encoding);
   1328 #endif
   1329 		*index = ' ';
   1330 		return (0);
   1331 	}
   1332 }
   1333 
   1334 static int
   1335 vga_mapchar(void *id, int uni, u_int *index)
   1336 {
   1337 	struct vgascreen *scr = id;
   1338 	u_int idx1, idx2;
   1339 	int res1, res2;
   1340 
   1341 	res1 = 0;
   1342 	idx1 = ' '; /* space */
   1343 	if (scr->fontset1)
   1344 		res1 = _vga_mapchar(id, scr->fontset1, uni, &idx1);
   1345 	res2 = -1;
   1346 	if (scr->fontset2) {
   1347 		KASSERT(VGA_SCREEN_CANTWOFONTS(scr->pcs.type));
   1348 		res2 = _vga_mapchar(id, scr->fontset2, uni, &idx2);
   1349 	}
   1350 	if (res2 > res1) {
   1351 		*index = idx2 | 0x0800; /* attribute bit 3 */
   1352 		return (res2);
   1353 	}
   1354 	*index = idx1;
   1355 	return (res1);
   1356 }
   1357 
   1358 #ifdef WSDISPLAY_CHARFUNCS
   1359 int
   1360 vga_getwschar(void *cookie, struct wsdisplay_char *wschar)
   1361 {
   1362 	struct vgascreen *scr = cookie;
   1363 
   1364 	if (scr == NULL) return 0;
   1365 	return (pcdisplay_getwschar(&scr->pcs, wschar));
   1366 }
   1367 
   1368 int
   1369 vga_putwschar(void *cookie, struct wsdisplay_char *wschar)
   1370 {
   1371 	struct vgascreen *scr = cookie;
   1372 
   1373 	if (scr == NULL) return 0;
   1374 	return (pcdisplay_putwschar(&scr->pcs, wschar));
   1375 }
   1376 #endif /* WSDISPLAY_CHARFUNCS */
   1377