Home | History | Annotate | Line # | Download | only in isa
ega.c revision 1.2
      1 /* $NetBSD: ega.c,v 1.2 2000/01/05 18:27:47 ad 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 
    377 		pcdisplay_cursor_init(&scr->pcs);
    378 	} else {
    379 		cpos = 0;
    380 		scr->pcs.dispoffset = scr->mindispoffset;
    381 	}
    382 
    383 	scr->pcs.vc_crow = cpos / type->ncols;
    384 	scr->pcs.vc_ccol = cpos % type->ncols;
    385 #ifdef PCDISPLAY_SOFTCURSOR
    386 	scr->pcs.cursoron = 0;
    387 #else
    388 	scr->pcs.cursoron = 1;
    389 #endif
    390 
    391 	res = ega_alloc_attr(scr, 0, 0, 0, attrp);
    392 #ifdef DIAGNOSTIC
    393 	if (res)
    394 		panic("ega_init_screen: attribute botch");
    395 #endif
    396 
    397 	scr->pcs.mem = NULL;
    398 
    399 	scr->fontset1 = scr->fontset2 = 0;
    400 	if (ega_selectfont(vc, scr, 0, 0)) {
    401 		if (scr == &ega_console_screen)
    402 			panic("ega_init_screen: no font");
    403 		else
    404 			printf("ega_init_screen: no font\n");
    405 	}
    406 
    407 	vc->nscreens++;
    408 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    409 }
    410 
    411 static void
    412 ega_init(vc, iot, memt, mono)
    413 	struct ega_config *vc;
    414 	bus_space_tag_t iot, memt;
    415 	int mono;
    416 {
    417 	struct vga_handle *vh = &vc->hdl;
    418 	int i;
    419 
    420         vh->vh_iot = iot;
    421         vh->vh_memt = memt;
    422 	vh->vh_mono = mono;
    423 
    424         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    425                 panic("ega_common_setup: couldn't map ega io");
    426 
    427 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    428 			  &vh->vh_ioh_6845))
    429                 panic("ega_common_setup: couldn't map 6845 io");
    430 
    431         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    432                 panic("ega_common_setup: couldn't map memory");
    433 
    434         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
    435 				(vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
    436 				&vh->vh_memh))
    437                 panic("ega_common_setup: mem subrange failed");
    438 
    439 	vc->nscreens = 0;
    440 	LIST_INIT(&vc->screens);
    441 	vc->active = NULL;
    442 	vc->currenttype = vh->vh_mono ? &ega_stdscreen_mono : &ega_stdscreen;
    443 
    444 	vc->vc_fonts[0] = &ega_builtinfont;
    445 	for (i = 1; i < 4; i++)
    446 		vc->vc_fonts[i] = 0;
    447 
    448 	vc->currentfontset1 = vc->currentfontset2 = 0;
    449 }
    450 
    451 int
    452 ega_match(parent, match, aux)
    453 	struct device *parent;
    454 	struct cfdata *match;
    455 	void *aux;
    456 {
    457 	struct isa_attach_args *ia = aux;
    458 	int mono;
    459 
    460 	/* If values are hardwired to something that they can't be, punt. */
    461 	if ((ia->ia_iobase != IOBASEUNK &&
    462 	     ia->ia_iobase != 0x3d0 &&
    463 	     ia->ia_iobase != 0x3b0) ||
    464 	    /* ia->ia_iosize != 0 || XXX isa.c */
    465 	    (ia->ia_maddr != MADDRUNK &&
    466 	     ia->ia_maddr != 0xb8000 &&
    467 	     ia->ia_maddr != 0xb0000) ||
    468 	    (ia->ia_msize != 0 && ia->ia_msize != 0x8000) ||
    469 	    ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
    470 		return (0);
    471 
    472 	if (ega_is_console(ia->ia_iot))
    473 		mono = ega_console_dc.hdl.vh_mono;
    474 	else if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
    475 		 ega_probe_col(ia->ia_iot, ia->ia_memt))
    476 		mono = 0;
    477 	else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
    478 		ega_probe_mono(ia->ia_iot, ia->ia_memt))
    479 		mono = 1;
    480 	else
    481 		return (0);
    482 
    483 	ia->ia_iobase = mono ? 0x3b0 : 0x3d0;
    484 	ia->ia_iosize = 0x10;
    485 	ia->ia_maddr = mono ? 0xb0000 : 0xb8000;
    486 	ia->ia_msize = 0x8000;
    487 	return (2); /* beat pcdisplay */
    488 }
    489 
    490 void
    491 ega_attach(parent, self, aux)
    492 	struct device *parent, *self;
    493 	void *aux;
    494 {
    495 	struct isa_attach_args *ia = aux;
    496 	struct ega_softc *sc = (struct ega_softc *)self;
    497 	int console;
    498 	struct ega_config *dc;
    499 	struct wsemuldisplaydev_attach_args aa;
    500 
    501 	printf("\n");
    502 
    503 	console = ega_is_console(ia->ia_iot);
    504 
    505 	if (console) {
    506 		dc = &ega_console_dc;
    507 		sc->nscreens = 1;
    508 		ega_console_attached = 1;
    509 	} else {
    510 		dc = malloc(sizeof(struct ega_config),
    511 			    M_DEVBUF, M_WAITOK);
    512 		if (ia->ia_iobase != 0x3b0 && ia->ia_maddr != 0xb0000 &&
    513 		    ega_probe_col(ia->ia_iot, ia->ia_memt))
    514 			ega_init(dc, ia->ia_iot, ia->ia_memt, 0);
    515 		else if (ia->ia_iobase != 0x3d0 && ia->ia_maddr != 0xb8000 &&
    516 			 ega_probe_mono(ia->ia_iot, ia->ia_memt))
    517 			ega_init(dc, ia->ia_iot, ia->ia_memt, 1);
    518 		else
    519 			panic("ega_attach: display disappeared");
    520 	}
    521 	sc->sc_dc = dc;
    522 
    523 	aa.console = console;
    524 	aa.scrdata = &ega_screenlist;
    525 	aa.accessops = &ega_accessops;
    526 	aa.accesscookie = dc;
    527 
    528         config_found(self, &aa, wsemuldisplaydevprint);
    529 }
    530 
    531 
    532 int
    533 ega_cnattach(iot, memt)
    534 	bus_space_tag_t iot, memt;
    535 {
    536 	int mono;
    537 	long defattr;
    538 	const struct wsscreen_descr *scr;
    539 
    540 	if (ega_probe_col(iot, memt))
    541 		mono = 0;
    542 	else if (ega_probe_mono(iot, memt))
    543 		mono = 1;
    544 	else
    545 		return (ENXIO);
    546 
    547 	ega_init(&ega_console_dc, iot, memt, mono);
    548 	scr = ega_console_dc.currenttype;
    549 	ega_init_screen(&ega_console_dc, &ega_console_screen, scr, 1, &defattr);
    550 
    551 	ega_console_screen.pcs.active = 1;
    552 	ega_console_dc.active = &ega_console_screen;
    553 
    554 	wsdisplay_cnattach(scr, &ega_console_screen,
    555 			   ega_console_screen.pcs.vc_ccol,
    556 			   ega_console_screen.pcs.vc_crow,
    557 			   defattr);
    558 
    559 	egaconsole = 1;
    560 	return (0);
    561 }
    562 
    563 static int
    564 ega_is_console(iot)
    565 	bus_space_tag_t iot;
    566 {
    567 	if (egaconsole &&
    568 	    !ega_console_attached &&
    569 	    iot == ega_console_dc.hdl.vh_iot)
    570 		return (1);
    571 	return (0);
    572 }
    573 
    574 static int
    575 ega_ioctl(v, cmd, data, flag, p)
    576 	void *v;
    577 	u_long cmd;
    578 	caddr_t data;
    579 	int flag;
    580 	struct proc *p;
    581 {
    582 	/*
    583 	 * XXX "do something!"
    584 	 */
    585 	return (-1);
    586 }
    587 
    588 static int
    589 ega_mmap(v, offset, prot)
    590 	void *v;
    591 	off_t offset;
    592 	int prot;
    593 {
    594 	return (-1);
    595 }
    596 
    597 static int
    598 ega_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
    599 	void *v;
    600 	const struct wsscreen_descr *type;
    601 	void **cookiep;
    602 	int *curxp, *curyp;
    603 	long *defattrp;
    604 {
    605 	struct ega_config *vc = v;
    606 	struct egascreen *scr;
    607 
    608 	if (vc->nscreens == 1) {
    609 		/*
    610 		 * When allocating the second screen, get backing store
    611 		 * for the first one too.
    612 		 * XXX We could be more clever and use video RAM.
    613 		 */
    614 		vc->screens.lh_first->pcs.mem =
    615 		  malloc(type->ncols * type->nrows * 2, M_DEVBUF, M_WAITOK);
    616 	}
    617 
    618 	scr = malloc(sizeof(struct egascreen), M_DEVBUF, M_WAITOK);
    619 	ega_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    620 
    621 	if (vc->nscreens == 1) {
    622 		scr->pcs.active = 1;
    623 		vc->active = scr;
    624 		vc->currenttype = type;
    625 	} else {
    626 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
    627 				      M_DEVBUF, M_WAITOK);
    628 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
    629 	}
    630 
    631 	*cookiep = scr;
    632 	*curxp = scr->pcs.vc_ccol;
    633 	*curyp = scr->pcs.vc_crow;
    634 	return (0);
    635 }
    636 
    637 static void
    638 ega_free_screen(v, cookie)
    639 	void *v;
    640 	void *cookie;
    641 {
    642 	struct egascreen *vs = cookie;
    643 	struct ega_config *vc = vs->cfg;
    644 
    645 	LIST_REMOVE(vs, next);
    646 	if (vs != &ega_console_screen)
    647 		free(vs, M_DEVBUF);
    648 	else
    649 		panic("ega_free_screen: console");
    650 
    651 	if (vc->active == vs)
    652 		vc->active = 0;
    653 }
    654 
    655 static void
    656 ega_setfont(vc, scr)
    657 	struct ega_config *vc;
    658 	struct egascreen *scr;
    659 {
    660 	int fontslot1, fontslot2;
    661 
    662 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
    663 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
    664 	if (vc->currentfontset1 != fontslot1 ||
    665 	    vc->currentfontset2 != fontslot2) {
    666 		vga_setfontset(&vc->hdl, 2 * fontslot1, 2 * fontslot2);
    667 		vc->currentfontset1 = fontslot1;
    668 		vc->currentfontset2 = fontslot2;
    669 	}
    670 }
    671 
    672 static int
    673 ega_show_screen(v, cookie, waitok, cb, cbarg)
    674 	void *v;
    675 	void *cookie;
    676 	int waitok;
    677 	void (*cb) __P((void *, int, int));
    678 	void *cbarg;
    679 {
    680 	struct egascreen *scr = cookie, *oldscr;
    681 	struct ega_config *vc = scr->cfg;
    682 
    683 	oldscr = vc->active; /* can be NULL! */
    684 	if (scr == oldscr) {
    685 		return (0);
    686 	}
    687 
    688 	vc->wantedscreen = cookie;
    689 	vc->switchcb = cb;
    690 	vc->switchcbarg = cbarg;
    691 	if (cb) {
    692 		timeout((void(*)(void *))ega_doswitch, vc, 0);
    693 		return (EAGAIN);
    694 	}
    695 
    696 	ega_doswitch(vc);
    697 	return (0);
    698 }
    699 
    700 void
    701 ega_doswitch(vc)
    702 	struct ega_config *vc;
    703 {
    704 	struct egascreen *scr, *oldscr;
    705 	struct vga_handle *vh = &vc->hdl;
    706 	const struct wsscreen_descr *type;
    707 
    708 	scr = vc->wantedscreen;
    709 	if (!scr) {
    710 		printf("ega_doswitch: disappeared\n");
    711 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    712 		return;
    713 	}
    714 	type = scr->pcs.type;
    715 	oldscr = vc->active; /* can be NULL! */
    716 #ifdef DIAGNOSTIC
    717 	if (oldscr) {
    718 		if (!oldscr->pcs.active)
    719 			panic("ega_show_screen: not active");
    720 		if (oldscr->pcs.type != vc->currenttype)
    721 			panic("ega_show_screen: bad type");
    722 	}
    723 #endif
    724 	if (scr == oldscr) {
    725 		return;
    726 	}
    727 #ifdef DIAGNOSTIC
    728 	if (scr->pcs.active)
    729 		panic("ega_show_screen: active");
    730 #endif
    731 
    732 	if (oldscr) {
    733 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
    734 
    735 		oldscr->pcs.active = 0;
    736 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
    737 					oldscr->pcs.dispoffset, oldscr->pcs.mem,
    738 					oldtype->ncols * oldtype->nrows);
    739 	}
    740 
    741 	if (vc->currenttype != type) {
    742 		vga_setscreentype(vh, type);
    743 		vc->currenttype = type;
    744 	}
    745 
    746 	ega_setfont(vc, scr);
    747 	/* XXX swich colours! */
    748 
    749 	scr->pcs.dispoffset = scr->mindispoffset;
    750 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
    751 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
    752 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
    753 	}
    754 
    755 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
    756 				scr->pcs.dispoffset, scr->pcs.mem,
    757 				type->ncols * type->nrows);
    758 	scr->pcs.active = 1;
    759 
    760 	vc->active = scr;
    761 
    762 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
    763 			 scr->pcs.vc_crow, scr->pcs.vc_ccol);
    764 
    765 	vc->wantedscreen = 0;
    766 	if (vc->switchcb)
    767 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
    768 }
    769 
    770 static int
    771 ega_load_font(v, cookie, data)
    772 	void *v;
    773 	void *cookie;
    774 	struct wsdisplay_font *data;
    775 {
    776 	struct ega_config *vc = v;
    777 	struct egascreen *scr = cookie;
    778 	char *name2;
    779 	int res, slot;
    780 	struct egafont *f;
    781 
    782 	if (scr) {
    783 		name2 = strchr(data->name, ',');
    784 		if (name2)
    785 			*name2++ = '\0';
    786 		res = ega_selectfont(vc, scr, data->name, name2);
    787 		if (!res)
    788 			ega_setfont(vc, scr);
    789 		return (res);
    790 	}
    791 
    792 	if (data->fontwidth != 8 || data->stride != 1)
    793 		return (EINVAL); /* XXX 1 byte per line */
    794 	if (data->firstchar != 0 || data->numchars != 256)
    795 		return (EINVAL);
    796 #ifndef WSCONS_SUPPORT_PCVTFONTS
    797 	if (data->encoding == WSDISPLAY_FONTENC_PCVT) {
    798 		printf("vga: pcvt font support not built in, see vga(4)\n");
    799 		return (EINVAL);
    800 	}
    801 #endif
    802 
    803 	for (slot = 0; slot < 4; slot++)
    804 		if (!vc->vc_fonts[slot])
    805 			break;
    806 	if (slot == 4)
    807 		return (ENOSPC);
    808 
    809 	f = malloc(sizeof(struct egafont), M_DEVBUF, M_WAITOK);
    810 	strncpy(f->name, data->name, sizeof(f->name));
    811 	f->height = data->fontheight;
    812 	f->encoding = data->encoding;
    813 #ifdef notyet
    814 	f->firstchar = data->firstchar;
    815 	f->numchars = data->numchars;
    816 #endif
    817 #ifdef EGAFONTDEBUG
    818 	printf("ega: load %s (8x%d, enc %d) font to slot %d\n", f->name,
    819 	       f->height, f->encoding, slot);
    820 #endif
    821 	vga_loadchars(&vc->hdl, 2 * slot, 0, 256, f->height, data->data);
    822 	f->slot = slot;
    823 	vc->vc_fonts[slot] = f;
    824 
    825 	return (0);
    826 }
    827 
    828 static int
    829 ega_alloc_attr(id, fg, bg, flags, attrp)
    830 	void *id;
    831 	int fg, bg;
    832 	int flags;
    833 	long *attrp;
    834 {
    835 	struct egascreen *scr = id;
    836 	struct ega_config *vc = scr->cfg;
    837 
    838 	if (vc->hdl.vh_mono) {
    839 		if (flags & WSATTR_WSCOLORS)
    840 			return (EINVAL);
    841 		if (flags & WSATTR_REVERSE)
    842 			*attrp = 0x70;
    843 		else
    844 			*attrp = 0x07;
    845 		if (flags & WSATTR_UNDERLINE)
    846 			*attrp |= FG_UNDERLINE;
    847 		if (flags & WSATTR_HILIT)
    848 			*attrp |= FG_INTENSE;
    849 	} else {
    850 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
    851 			return (EINVAL);
    852 		if (flags & WSATTR_WSCOLORS)
    853 			*attrp = fgansitopc[fg] | bgansitopc[bg];
    854 		else
    855 			*attrp = 7;
    856 		if (flags & WSATTR_HILIT)
    857 			*attrp += 8;
    858 	}
    859 	if (flags & WSATTR_BLINK)
    860 		*attrp |= FG_BLINK;
    861 	return (0);
    862 }
    863 
    864 void
    865 ega_copyrows(id, srcrow, dstrow, nrows)
    866 	void *id;
    867 	int srcrow, dstrow, nrows;
    868 {
    869 	struct egascreen *scr = id;
    870 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
    871 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
    872 	int ncols = scr->pcs.type->ncols;
    873 	bus_size_t srcoff, dstoff;
    874 
    875 	srcoff = srcrow * ncols + 0;
    876 	dstoff = dstrow * ncols + 0;
    877 
    878 	if (scr->pcs.active) {
    879 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
    880 #ifdef PCDISPLAY_SOFTCURSOR
    881 			int cursoron = scr->pcs.cursoron;
    882 
    883 			if (cursoron)
    884 				pcdisplay_cursor(&scr->pcs, 0,
    885 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
    886 #endif
    887 			/* scroll up whole screen */
    888 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
    889 			    <= scr->maxdispoffset) {
    890 				scr->pcs.dispoffset += srcrow * ncols * 2;
    891 			} else {
    892 				bus_space_copy_region_2(memt, memh,
    893 					scr->pcs.dispoffset + srcoff * 2,
    894 					memh, scr->mindispoffset,
    895 					nrows * ncols);
    896 				scr->pcs.dispoffset = scr->mindispoffset;
    897 			}
    898 			vga_6845_write(&scr->cfg->hdl, startadrh,
    899 				       scr->pcs.dispoffset >> 9);
    900 			vga_6845_write(&scr->cfg->hdl, startadrl,
    901 				       scr->pcs.dispoffset >> 1);
    902 #ifdef PCDISPLAY_SOFTCURSOR
    903 			if (cursoron)
    904 				pcdisplay_cursor(&scr->pcs, 1,
    905 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
    906 #endif
    907 		} else {
    908 			bus_space_copy_region_2(memt, memh,
    909 					scr->pcs.dispoffset + srcoff * 2,
    910 					memh, scr->pcs.dispoffset + dstoff * 2,
    911 					nrows * ncols);
    912 		}
    913 	} else
    914 		bcopy(&scr->pcs.mem[srcoff], &scr->pcs.mem[dstoff],
    915 		      nrows * ncols * 2);
    916 }
    917