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