Home | History | Annotate | Line # | Download | only in isa
ega.c revision 1.1
      1 /* $NetBSD: ega.c,v 1.1 1999/12/13 16:28:57 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/malloc.h>
     40 #include <machine/bus.h>
     41 
     42 #include <dev/isa/isavar.h>
     43 
     44 #include <dev/ic/mc6845reg.h>
     45 #include <dev/ic/pcdisplayvar.h>
     46 #include <dev/ic/vgareg.h>
     47 #include <dev/ic/vgavar.h>
     48 #include <dev/isa/egavar.h>
     49 
     50 #include <dev/ic/pcdisplay.h>
     51 
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wscons/wsdisplayvar.h>
     54 
     55 static struct egafont {
     56 	char name[16];
     57 	int height;
     58 	int encoding;
     59 	int slot;
     60 } ega_builtinfont = {
     61 	"builtin",
     62 	14,
     63 	WSDISPLAY_FONTENC_IBM,
     64 	0
     65 };
     66 
     67 struct egascreen {
     68 	struct pcdisplayscreen pcs;
     69 	LIST_ENTRY(egascreen) next;
     70 	struct ega_config *cfg;
     71 	struct egafont *fontset1, *fontset2;
     72 
     73 	int mindispoffset, maxdispoffset;
     74 };
     75 
     76 struct ega_config {
     77 	struct vga_handle hdl;
     78 
     79 	int nscreens;
     80 	LIST_HEAD(, egascreen) screens;
     81 	struct egascreen *active; /* current display */
     82 	const struct wsscreen_descr *currenttype;
     83 	int currentfontset1, currentfontset2;
     84 
     85 	struct egafont *vc_fonts[4];
     86 
     87 	struct egascreen *wantedscreen;
     88 	void (*switchcb) __P((void *, int, int));
     89 	void *switchcbarg;
     90 };
     91 
     92 struct ega_softc {
     93 	struct device sc_dev;
     94 	struct ega_config *sc_dc;
     95 	int nscreens;
     96 };
     97 
     98 static int egaconsole, ega_console_attached;
     99 static struct egascreen ega_console_screen;
    100 static struct ega_config ega_console_dc;
    101 
    102 int	ega_match __P((struct device *, struct cfdata *, void *));
    103 void	ega_attach __P((struct device *, struct device *, void *));
    104 
    105 static int ega_is_console __P((bus_space_tag_t));
    106 static int ega_probe_col __P((bus_space_tag_t, bus_space_tag_t));
    107 static int ega_probe_mono __P((bus_space_tag_t, bus_space_tag_t));
    108 int ega_selectfont __P((struct ega_config *, struct egascreen *,
    109 			char *, char *));
    110 void ega_init_screen __P((struct ega_config *, struct egascreen *,
    111 			  const struct wsscreen_descr *,
    112 			  int, long *));
    113 static void ega_init __P((struct ega_config *,
    114 			  bus_space_tag_t, bus_space_tag_t, int));
    115 static void ega_setfont __P((struct ega_config *, struct egascreen *));
    116 static int ega_alloc_attr __P((void *, int, int, int, long *));
    117 void ega_copyrows __P((void *, int, int, int));
    118 
    119 struct cfattach ega_ca = {
    120 	sizeof(struct ega_softc), ega_match, ega_attach,
    121 };
    122 
    123 const struct wsdisplay_emulops ega_emulops = {
    124 	pcdisplay_cursor,
    125 	pcdisplay_mapchar,
    126 	pcdisplay_putchar,
    127 	pcdisplay_copycols,
    128 	pcdisplay_erasecols,
    129 	ega_copyrows,
    130 	pcdisplay_eraserows,
    131 	ega_alloc_attr
    132 };
    133 
    134 /*
    135  * translate WS(=ANSI) color codes to standard pc ones
    136  */
    137 static unsigned char fgansitopc[] = {
    138 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
    139 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
    140 }, bgansitopc[] = {
    141 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
    142 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
    143 };
    144 
    145 const struct wsscreen_descr ega_stdscreen = {
    146 	"80x25", 80, 25,
    147 	&ega_emulops,
    148 	8, 14,
    149 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    150 }, ega_stdscreen_mono = {
    151 	"80x25", 80, 25,
    152 	&ega_emulops,
    153 	8, 14,
    154 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    155 }, ega_stdscreen_bf = {
    156 	"80x25bf", 80, 25,
    157 	&ega_emulops,
    158 	8, 14,
    159 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    160 }, ega_35lscreen = {
    161 	"80x35", 80, 35,
    162 	&ega_emulops,
    163 	8, 10,
    164 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    165 }, ega_35lscreen_mono = {
    166 	"80x35", 80, 35,
    167 	&ega_emulops,
    168 	8, 10,
    169 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    170 }, ega_35lscreen_bf = {
    171 	"80x35bf", 80, 35,
    172 	&ega_emulops,
    173 	8, 10,
    174 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    175 }, ega_43lscreen = {
    176 	"80x43", 80, 43,
    177 	&ega_emulops,
    178 	8, 8,
    179 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    180 }, ega_43lscreen_mono = {
    181 	"80x43", 80, 43,
    182 	&ega_emulops,
    183 	8, 8,
    184 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    185 }, ega_43lscreen_bf = {
    186 	"80x43bf", 80, 43,
    187 	&ega_emulops,
    188 	8, 8,
    189 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    190 };
    191 
    192 #define VGA_SCREEN_CANTWOFONTS(type) (!((type)->capabilities & WSSCREEN_HILIT))
    193 
    194 const struct wsscreen_descr *_ega_scrlist[] = {
    195 	&ega_stdscreen,
    196 	&ega_stdscreen_bf,
    197 	&ega_35lscreen,
    198 	&ega_35lscreen_bf,
    199 	&ega_43lscreen,
    200 	&ega_43lscreen_bf,
    201 }, *_ega_scrlist_mono[] = {
    202 	&ega_stdscreen_mono,
    203 	&ega_35lscreen_mono,
    204 	&ega_43lscreen_mono,
    205 };
    206 
    207 
    208 const struct wsscreen_list ega_screenlist = {
    209 	sizeof(_ega_scrlist) / sizeof(struct wsscreen_descr *),
    210 	_ega_scrlist
    211 }, ega_screenlist_mono = {
    212 	sizeof(_ega_scrlist_mono) / sizeof(struct wsscreen_descr *),
    213 	_ega_scrlist_mono
    214 };
    215 
    216 static int ega_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    217 static int ega_mmap __P((void *, off_t, int));
    218 static int ega_alloc_screen __P((void *, const struct wsscreen_descr *,
    219 				       void **, int *, int *, long *));
    220 static void ega_free_screen __P((void *, void *));
    221 static int ega_show_screen __P((void *, void *, int,
    222 				      	void (*) (void *, int, int), void *));
    223 static int ega_load_font __P((void *, void *, struct wsdisplay_font *));
    224 
    225 void ega_doswitch __P((struct ega_config *));
    226 
    227 const struct wsdisplay_accessops ega_accessops = {
    228 	ega_ioctl,
    229 	ega_mmap,
    230 	ega_alloc_screen,
    231 	ega_free_screen,
    232 	ega_show_screen,
    233 	ega_load_font
    234 };
    235 
    236 static int
    237 ega_probe_col(iot, memt)
    238 	bus_space_tag_t iot, memt;
    239 {
    240 	bus_space_handle_t memh, ioh_6845;
    241 	u_int16_t oldval, val;
    242 
    243 	if (bus_space_map(memt, 0xb8000, 0x8000, 0, &memh))
    244 		return (0);
    245 	oldval = bus_space_read_2(memt, memh, 0);
    246 	bus_space_write_2(memt, memh, 0, 0xa55a);
    247 	val = bus_space_read_2(memt, memh, 0);
    248 	bus_space_write_2(memt, memh, 0, oldval);
    249 	bus_space_unmap(memt, memh, 0x8000);
    250 	if (val != 0xa55a)
    251 		return (0);
    252 
    253 	if (bus_space_map(iot, 0x3d0, 0x10, 0, &ioh_6845))
    254 		return (0);
    255 	bus_space_unmap(iot, ioh_6845, 0x10);
    256 
    257 	return (1);
    258 }
    259 
    260 static int
    261 ega_probe_mono(iot, memt)
    262 	bus_space_tag_t iot, memt;
    263 {
    264 	bus_space_handle_t memh, ioh_6845;
    265 	u_int16_t oldval, val;
    266 
    267 	if (bus_space_map(memt, 0xb0000, 0x8000, 0, &memh))
    268 		return (0);
    269 	oldval = bus_space_read_2(memt, memh, 0);
    270 	bus_space_write_2(memt, memh, 0, 0xa55a);
    271 	val = bus_space_read_2(memt, memh, 0);
    272 	bus_space_write_2(memt, memh, 0, oldval);
    273 	bus_space_unmap(memt, memh, 0x8000);
    274 	if (val != 0xa55a)
    275 		return (0);
    276 
    277 	if (bus_space_map(iot, 0x3b0, 0x10, 0, &ioh_6845))
    278 		return (0);
    279 	bus_space_unmap(iot, ioh_6845, 0x10);
    280 
    281 	return (1);
    282 }
    283 /*
    284  * We want at least ASCII 32..127 be present in the
    285  * first font slot.
    286  */
    287 #define vga_valid_primary_font(f) \
    288 	(f->encoding == WSDISPLAY_FONTENC_IBM || \
    289 	f->encoding == WSDISPLAY_FONTENC_ISO)
    290 
    291 int
    292 ega_selectfont(vc, scr, name1, name2)
    293 	struct ega_config *vc;
    294 	struct egascreen *scr;
    295 	char *name1, *name2; /* NULL: take first found */
    296 {
    297 	const struct wsscreen_descr *type = scr->pcs.type;
    298 	struct egafont *f1, *f2;
    299 	int i;
    300 
    301 	f1 = f2 = 0;
    302 
    303 	for (i = 0; i < 4; i++) {
    304 		struct egafont *f = vc->vc_fonts[i];
    305 		if (!f || f->height != type->fontheight)
    306 			continue;
    307 		if (!f1 &&
    308 		    vga_valid_primary_font(f) &&
    309 		    (!name1 || !strcmp(name1, f->name))) {
    310 			f1 = f;
    311 			continue;
    312 		}
    313 		if (!f2 &&
    314 		    VGA_SCREEN_CANTWOFONTS(type) &&
    315 		    (!name2 || !strcmp(name2, f->name))) {
    316 			f2 = f;
    317 			continue;
    318 		}
    319 	}
    320 
    321 	/*
    322 	 * The request fails if no primary font was found,
    323 	 * or if a second font was requested but not found.
    324 	 */
    325 	if (f1 && (!name2 || f2)) {
    326 #ifdef EGAFONTDEBUG
    327 		if (scr != &ega_console_screen || ega_console_attached) {
    328 			printf("ega (%s): font1=%s (slot %d)", type->name,
    329 			       f1->name, f1->slot);
    330 			if (f2)
    331 				printf(", font2=%s (slot %d)",
    332 				       f2->name, f2->slot);
    333 			printf("\n");
    334 		}
    335 #endif
    336 		scr->fontset1 = f1;
    337 		scr->fontset2 = f2;
    338 		return (0);
    339 	}
    340 	return (ENXIO);
    341 }
    342 
    343 void
    344 ega_init_screen(vc, scr, type, existing, attrp)
    345 	struct ega_config *vc;
    346 	struct egascreen *scr;
    347 	const struct wsscreen_descr *type;
    348 	int existing;
    349 	long *attrp;
    350 {
    351 	int cpos;
    352 	int res;
    353 
    354 	scr->cfg = vc;
    355 	scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
    356 	scr->pcs.type = type;
    357 	scr->pcs.active = 0;
    358 	scr->mindispoffset = 0;
    359 	scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
    360 
    361 	if (existing) {
    362 		cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
    363 		cpos |= vga_6845_read(&vc->hdl, cursorl);
    364 
    365 		/* make sure we have a valid cursor position */
    366 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    367 			cpos = 0;
    368 
    369 		scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
    370 		scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
    371 
    372 		/* make sure we have a valid memory offset */
    373 		if (scr->pcs.dispoffset < scr->mindispoffset ||
    374 		    scr->pcs.dispoffset > scr->maxdispoffset)
    375 			scr->pcs.dispoffset = scr->mindispoffset;
    376 #ifdef PCDISPLAY_SOFTCURSOR
    377 		/* disable hardware cursor */
    378 		vga_6845_write(&vc->hdl, curstart, 0x10);
    379 		vga_6845_write(&vc->hdl, curend, 0x10);
    380 #endif
    381 	} else {
    382 		cpos = 0;
    383 		scr->pcs.dispoffset = scr->mindispoffset;
    384 	}
    385 
    386 	scr->pcs.vc_crow = cpos / type->ncols;
    387 	scr->pcs.vc_ccol = cpos % type->ncols;
    388 #ifdef PCDISPLAY_SOFTCURSOR
    389 	scr->pcs.cursoron = 0;
    390 #else
    391 	scr->pcs.cursoron = 1;
    392 #endif
    393 
    394 	res = ega_alloc_attr(scr, 0, 0, 0, attrp);
    395 #ifdef DIAGNOSTIC
    396 	if (res)
    397 		panic("ega_init_screen: attribute botch");
    398 #endif
    399 
    400 	scr->pcs.mem = NULL;
    401 
    402 	scr->fontset1 = scr->fontset2 = 0;
    403 	if (ega_selectfont(vc, scr, 0, 0)) {
    404 		if (scr == &ega_console_screen)
    405 			panic("ega_init_screen: no font");
    406 		else
    407 			printf("ega_init_screen: no font\n");
    408 	}
    409 
    410 	vc->nscreens++;
    411 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    412 }
    413 
    414 static void
    415 ega_init(vc, iot, memt, mono)
    416 	struct ega_config *vc;
    417 	bus_space_tag_t iot, memt;
    418 	int mono;
    419 {
    420 	struct vga_handle *vh = &vc->hdl;
    421 	int i;
    422 
    423         vh->vh_iot = iot;
    424         vh->vh_memt = memt;
    425 	vh->vh_mono = mono;
    426 
    427         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    428                 panic("ega_common_setup: couldn't map ega io");
    429 
    430 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    431 			  &vh->vh_ioh_6845))
    432                 panic("ega_common_setup: couldn't map 6845 io");
    433 
    434         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    435                 panic("ega_common_setup: couldn't map memory");
    436 
    437         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
    438 				(vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
    439 				&vh->vh_memh))
    440                 panic("ega_common_setup: mem subrange failed");
    441 
    442 	vc->nscreens = 0;
    443 	LIST_INIT(&vc->screens);
    444 	vc->active = NULL;
    445 	vc->currenttype = vh->vh_mono ? &ega_stdscreen_mono : &ega_stdscreen;
    446 
    447 	vc->vc_fonts[0] = &ega_builtinfont;
    448 	for (i = 1; i < 4; i++)
    449 		vc->vc_fonts[i] = 0;
    450 
    451 	vc->currentfontset1 = vc->currentfontset2 = 0;
    452 }
    453 
    454 int
    455 ega_match(parent, match, aux)
    456 	struct device *parent;
    457 	struct cfdata *match;
    458 	void *aux;
    459 {
    460 	struct isa_attach_args *ia = aux;
    461 	int mono;
    462 
    463 	/* If values are hardwired to something that they can't be, punt. */
    464 	if ((ia->ia_iobase != IOBASEUNK &&
    465 	     ia->ia_iobase != 0x3d0 &&
    466 	     ia->ia_iobase != 0x3b0) ||
    467 	    /* ia->ia_iosize != 0 || XXX isa.c */
    468 	    (ia->ia_maddr != MADDRUNK &&
    469 	     ia->ia_maddr != 0xb8000 &&
    470 	     ia->ia_maddr != 0xb0000) ||
    471 	    (ia->ia_msize != 0 && ia->ia_msize != 0x8000) ||
    472 	    ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
    473 		return (0);
    474 
    475 	if (ega_is_console(ia->ia_iot))
    476 		mono = ega_console_dc.hdl.vh_mono;
    477 	else if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
    478 		 ega_probe_col(ia->ia_iot, ia->ia_memt))
    479 		mono = 0;
    480 	else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
    481 		ega_probe_mono(ia->ia_iot, ia->ia_memt))
    482 		mono = 1;
    483 	else
    484 		return (0);
    485 
    486 	ia->ia_iobase = mono ? 0x3b0 : 0x3d0;
    487 	ia->ia_iosize = 0x10;
    488 	ia->ia_maddr = mono ? 0xb0000 : 0xb8000;
    489 	ia->ia_msize = 0x8000;
    490 	return (2); /* beat pcdisplay */
    491 }
    492 
    493 void
    494 ega_attach(parent, self, aux)
    495 	struct device *parent, *self;
    496 	void *aux;
    497 {
    498 	struct isa_attach_args *ia = aux;
    499 	struct ega_softc *sc = (struct ega_softc *)self;
    500 	int console;
    501 	struct ega_config *dc;
    502 	struct wsemuldisplaydev_attach_args aa;
    503 
    504 	printf("\n");
    505 
    506 	console = ega_is_console(ia->ia_iot);
    507 
    508 	if (console) {
    509 		dc = &ega_console_dc;
    510 		sc->nscreens = 1;
    511 		ega_console_attached = 1;
    512 	} else {
    513 		dc = malloc(sizeof(struct ega_config),
    514 			    M_DEVBUF, M_WAITOK);
    515 		if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
    516 		    ega_probe_col(ia->ia_iot, ia->ia_memt))
    517 			ega_init(dc, ia->ia_iot, ia->ia_memt, 0);
    518 		else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
    519 			 ega_probe_mono(ia->ia_iot, ia->ia_memt))
    520 			ega_init(dc, ia->ia_iot, ia->ia_memt, 1);
    521 		else
    522 			panic("ega_attach: display disappeared");
    523 	}
    524 	sc->sc_dc = dc;
    525 
    526 	aa.console = console;
    527 	aa.scrdata = &ega_screenlist;
    528 	aa.accessops = &ega_accessops;
    529 	aa.accesscookie = dc;
    530 
    531         config_found(self, &aa, wsemuldisplaydevprint);
    532 }
    533 
    534 
    535 int
    536 ega_cnattach(iot, memt)
    537 	bus_space_tag_t iot, memt;
    538 {
    539 	int mono;
    540 	long defattr;
    541 	const struct wsscreen_descr *scr;
    542 
    543 	if (ega_probe_col(iot, memt))
    544 		mono = 0;
    545 	else if (ega_probe_mono(iot, memt))
    546 		mono = 1;
    547 	else
    548 		return (ENXIO);
    549 
    550 	ega_init(&ega_console_dc, iot, memt, mono);
    551 	scr = ega_console_dc.currenttype;
    552 	ega_init_screen(&ega_console_dc, &ega_console_screen, scr, 1, &defattr);
    553 
    554 	ega_console_screen.pcs.active = 1;
    555 	ega_console_dc.active = &ega_console_screen;
    556 
    557 	wsdisplay_cnattach(scr, &ega_console_screen,
    558 			   ega_console_screen.pcs.vc_ccol,
    559 			   ega_console_screen.pcs.vc_crow,
    560 			   defattr);
    561 
    562 	egaconsole = 1;
    563 	return (0);
    564 }
    565 
    566 static int
    567 ega_is_console(iot)
    568 	bus_space_tag_t iot;
    569 {
    570 	if (egaconsole &&
    571 	    !ega_console_attached &&
    572 	    iot == ega_console_dc.hdl.vh_iot)
    573 		return (1);
    574 	return (0);
    575 }
    576 
    577 static int
    578 ega_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 	/*
    586 	 * XXX "do something!"
    587 	 */
    588 	return (-1);
    589 }
    590 
    591 static int
    592 ega_mmap(v, offset, prot)
    593 	void *v;
    594 	off_t offset;
    595 	int prot;
    596 {
    597 	return (-1);
    598 }
    599 
    600 static int
    601 ega_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
    602 	void *v;
    603 	const struct wsscreen_descr *type;
    604 	void **cookiep;
    605 	int *curxp, *curyp;
    606 	long *defattrp;
    607 {
    608 	struct ega_config *vc = v;
    609 	struct egascreen *scr;
    610 
    611 	if (vc->nscreens == 1) {
    612 		/*
    613 		 * When allocating the second screen, get backing store
    614 		 * for the first one too.
    615 		 * XXX We could be more clever and use video RAM.
    616 		 */
    617 		vc->screens.lh_first->pcs.mem =
    618 		  malloc(type->ncols * type->nrows * 2, M_DEVBUF, M_WAITOK);
    619 	}
    620 
    621 	scr = malloc(sizeof(struct egascreen), M_DEVBUF, M_WAITOK);
    622 	ega_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    623 
    624 	if (vc->nscreens == 1) {
    625 		scr->pcs.active = 1;
    626 		vc->active = scr;
    627 		vc->currenttype = type;
    628 	} else {
    629 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
    630 				      M_DEVBUF, M_WAITOK);
    631 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
    632 	}
    633 
    634 	*cookiep = scr;
    635 	*curxp = scr->pcs.vc_ccol;
    636 	*curyp = scr->pcs.vc_crow;
    637 	return (0);
    638 }
    639 
    640 static void
    641 ega_free_screen(v, cookie)
    642 	void *v;
    643 	void *cookie;
    644 {
    645 	struct egascreen *vs = cookie;
    646 	struct ega_config *vc = vs->cfg;
    647 
    648 	LIST_REMOVE(vs, next);
    649 	if (vs != &ega_console_screen)
    650 		free(vs, M_DEVBUF);
    651 	else
    652 		panic("ega_free_screen: console");
    653 
    654 	if (vc->active == vs)
    655 		vc->active = 0;
    656 }
    657 
    658 static void
    659 ega_setfont(vc, scr)
    660 	struct ega_config *vc;
    661 	struct egascreen *scr;
    662 {
    663 	int fontslot1, fontslot2;
    664 
    665 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
    666 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
    667 	if (vc->currentfontset1 != fontslot1 ||
    668 	    vc->currentfontset2 != fontslot2) {
    669 		vga_setfontset(&vc->hdl, 2 * fontslot1, 2 * fontslot2);
    670 		vc->currentfontset1 = fontslot1;
    671 		vc->currentfontset2 = fontslot2;
    672 	}
    673 }
    674 
    675 static int
    676 ega_show_screen(v, cookie, waitok, cb, cbarg)
    677 	void *v;
    678 	void *cookie;
    679 	int waitok;
    680 	void (*cb) __P((void *, int, int));
    681 	void *cbarg;
    682 {
    683 	struct egascreen *scr = cookie, *oldscr;
    684 	struct ega_config *vc = scr->cfg;
    685 
    686 	oldscr = vc->active; /* can be NULL! */
    687 	if (scr == oldscr) {
    688 		return (0);
    689 	}
    690 
    691 	vc->wantedscreen = cookie;
    692 	vc->switchcb = cb;
    693 	vc->switchcbarg = cbarg;
    694 	if (cb) {
    695 		timeout((void(*)(void *))ega_doswitch, vc, 0);
    696 		return (EAGAIN);
    697 	}
    698 
    699 	ega_doswitch(vc);
    700 	return (0);
    701 }
    702 
    703 void
    704 ega_doswitch(vc)
    705 	struct ega_config *vc;
    706 {
    707 	struct egascreen *scr, *oldscr;
    708 	struct vga_handle *vh = &vc->hdl;
    709 	const struct wsscreen_descr *type;
    710 
    711 	scr = vc->wantedscreen;
    712 	if (!scr) {
    713 		printf("ega_doswitch: disappeared\n");
    714 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    715 		return;
    716 	}
    717 	type = scr->pcs.type;
    718 	oldscr = vc->active; /* can be NULL! */
    719 #ifdef DIAGNOSTIC
    720 	if (oldscr) {
    721 		if (!oldscr->pcs.active)
    722 			panic("ega_show_screen: not active");
    723 		if (oldscr->pcs.type != vc->currenttype)
    724 			panic("ega_show_screen: bad type");
    725 	}
    726 #endif
    727 	if (scr == oldscr) {
    728 		return;
    729 	}
    730 #ifdef DIAGNOSTIC
    731 	if (scr->pcs.active)
    732 		panic("ega_show_screen: active");
    733 #endif
    734 
    735 	if (oldscr) {
    736 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
    737 
    738 		oldscr->pcs.active = 0;
    739 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
    740 					oldscr->pcs.dispoffset, oldscr->pcs.mem,
    741 					oldtype->ncols * oldtype->nrows);
    742 	}
    743 
    744 	if (vc->currenttype != type) {
    745 		vga_setscreentype(vh, type);
    746 		vc->currenttype = type;
    747 	}
    748 
    749 	ega_setfont(vc, scr);
    750 	/* XXX swich colours! */
    751 
    752 	scr->pcs.dispoffset = scr->mindispoffset;
    753 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
    754 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
    755 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
    756 	}
    757 
    758 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
    759 				scr->pcs.dispoffset, scr->pcs.mem,
    760 				type->ncols * type->nrows);
    761 	scr->pcs.active = 1;
    762 
    763 	vc->active = scr;
    764 
    765 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
    766 			 scr->pcs.vc_crow, scr->pcs.vc_ccol);
    767 
    768 	vc->wantedscreen = 0;
    769 	if (vc->switchcb)
    770 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    771 }
    772 
    773 static int
    774 ega_load_font(v, cookie, data)
    775 	void *v;
    776 	void *cookie;
    777 	struct wsdisplay_font *data;
    778 {
    779 	struct ega_config *vc = v;
    780 	struct egascreen *scr = cookie;
    781 	char *name2;
    782 	int res, slot;
    783 	struct egafont *f;
    784 
    785 	if (scr) {
    786 		name2 = strchr(data->name, ',');
    787 		if (name2)
    788 			*name2++ = '\0';
    789 		res = ega_selectfont(vc, scr, data->name, name2);
    790 		if (!res)
    791 			ega_setfont(vc, scr);
    792 		return (res);
    793 	}
    794 
    795 	if (data->fontwidth != 8 || data->stride != 1)
    796 		return (EINVAL); /* XXX 1 byte per line */
    797 	if (data->firstchar != 0 || data->numchars != 256)
    798 		return (EINVAL);
    799 #ifndef WSCONS_SUPPORT_PCVTFONTS
    800 	if (data->encoding == WSDISPLAY_FONTENC_PCVT) {
    801 		printf("vga: pcvt font support not built in, see vga(4)\n");
    802 		return (EINVAL);
    803 	}
    804 #endif
    805 
    806 	for (slot = 0; slot < 4; slot++)
    807 		if (!vc->vc_fonts[slot])
    808 			break;
    809 	if (slot == 4)
    810 		return (ENOSPC);
    811 
    812 	f = malloc(sizeof(struct egafont), M_DEVBUF, M_WAITOK);
    813 	strncpy(f->name, data->name, sizeof(f->name));
    814 	f->height = data->fontheight;
    815 	f->encoding = data->encoding;
    816 #ifdef notyet
    817 	f->firstchar = data->firstchar;
    818 	f->numchars = data->numchars;
    819 #endif
    820 #ifdef EGAFONTDEBUG
    821 	printf("ega: load %s (8x%d, enc %d) font to slot %d\n", f->name,
    822 	       f->height, f->encoding, slot);
    823 #endif
    824 	vga_loadchars(&vc->hdl, 2 * slot, 0, 256, f->height, data->data);
    825 	f->slot = slot;
    826 	vc->vc_fonts[slot] = f;
    827 
    828 	return (0);
    829 }
    830 
    831 static int
    832 ega_alloc_attr(id, fg, bg, flags, attrp)
    833 	void *id;
    834 	int fg, bg;
    835 	int flags;
    836 	long *attrp;
    837 {
    838 	struct egascreen *scr = id;
    839 	struct ega_config *vc = scr->cfg;
    840 
    841 	if (vc->hdl.vh_mono) {
    842 		if (flags & WSATTR_WSCOLORS)
    843 			return (EINVAL);
    844 		if (flags & WSATTR_REVERSE)
    845 			*attrp = 0x70;
    846 		else
    847 			*attrp = 0x07;
    848 		if (flags & WSATTR_UNDERLINE)
    849 			*attrp |= FG_UNDERLINE;
    850 		if (flags & WSATTR_HILIT)
    851 			*attrp |= FG_INTENSE;
    852 	} else {
    853 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
    854 			return (EINVAL);
    855 		if (flags & WSATTR_WSCOLORS)
    856 			*attrp = fgansitopc[fg] | bgansitopc[bg];
    857 		else
    858 			*attrp = 7;
    859 		if (flags & WSATTR_HILIT)
    860 			*attrp += 8;
    861 	}
    862 	if (flags & WSATTR_BLINK)
    863 		*attrp |= FG_BLINK;
    864 	return (0);
    865 }
    866 
    867 void
    868 ega_copyrows(id, srcrow, dstrow, nrows)
    869 	void *id;
    870 	int srcrow, dstrow, nrows;
    871 {
    872 	struct egascreen *scr = id;
    873 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
    874 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
    875 	int ncols = scr->pcs.type->ncols;
    876 	bus_size_t srcoff, dstoff;
    877 
    878 	srcoff = srcrow * ncols + 0;
    879 	dstoff = dstrow * ncols + 0;
    880 
    881 	if (scr->pcs.active) {
    882 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
    883 #ifdef PCDISPLAY_SOFTCURSOR
    884 			int cursoron = scr->pcs.cursoron;
    885 
    886 			if (cursoron)
    887 				pcdisplay_cursor(&scr->pcs, 0,
    888 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
    889 #endif
    890 			/* scroll up whole screen */
    891 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
    892 			    <= scr->maxdispoffset) {
    893 				scr->pcs.dispoffset += srcrow * ncols * 2;
    894 			} else {
    895 				bus_space_copy_region_2(memt, memh,
    896 					scr->pcs.dispoffset + srcoff * 2,
    897 					memh, scr->mindispoffset,
    898 					nrows * ncols);
    899 				scr->pcs.dispoffset = scr->mindispoffset;
    900 			}
    901 			vga_6845_write(&scr->cfg->hdl, startadrh,
    902 				       scr->pcs.dispoffset >> 9);
    903 			vga_6845_write(&scr->cfg->hdl, startadrl,
    904 				       scr->pcs.dispoffset >> 1);
    905 #ifdef PCDISPLAY_SOFTCURSOR
    906 			if (cursoron)
    907 				pcdisplay_cursor(&scr->pcs, 1,
    908 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
    909 #endif
    910 		} else {
    911 			bus_space_copy_region_2(memt, memh,
    912 					scr->pcs.dispoffset + srcoff * 2,
    913 					memh, scr->pcs.dispoffset + dstoff * 2,
    914 					nrows * ncols);
    915 		}
    916 	} else
    917 		bcopy(&scr->pcs.mem[srcoff], &scr->pcs.mem[dstoff],
    918 		      nrows * ncols * 2);
    919 }
    920