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