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