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