Home | History | Annotate | Line # | Download | only in ic
vga.c revision 1.58
      1  1.58  drochner /* $NetBSD: vga.c,v 1.58 2002/07/01 16:56:09 drochner Exp $ */
      2   1.1  drochner 
      3   1.1  drochner /*
      4   1.1  drochner  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5   1.1  drochner  * All rights reserved.
      6   1.1  drochner  *
      7   1.1  drochner  * Author: Chris G. Demetriou
      8   1.1  drochner  *
      9   1.1  drochner  * Permission to use, copy, modify and distribute this software and
     10   1.1  drochner  * its documentation is hereby granted, provided that both the copyright
     11   1.1  drochner  * notice and this permission notice appear in all copies of the
     12   1.1  drochner  * software, derivative works or modified versions, and any portions
     13   1.1  drochner  * thereof, and that both notices appear in supporting documentation.
     14   1.1  drochner  *
     15   1.1  drochner  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16   1.1  drochner  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1  drochner  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18   1.1  drochner  *
     19   1.1  drochner  * Carnegie Mellon requests users of this software to return to
     20   1.1  drochner  *
     21   1.1  drochner  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1  drochner  *  School of Computer Science
     23   1.1  drochner  *  Carnegie Mellon University
     24   1.1  drochner  *  Pittsburgh PA 15213-3890
     25   1.1  drochner  *
     26   1.1  drochner  * any improvements or extensions that they make and grant Carnegie the
     27   1.1  drochner  * rights to redistribute these changes.
     28   1.1  drochner  */
     29  1.43     lukem 
     30  1.43     lukem #include <sys/cdefs.h>
     31  1.58  drochner __KERNEL_RCSID(0, "$NetBSD: vga.c,v 1.58 2002/07/01 16:56:09 drochner Exp $");
     32   1.1  drochner 
     33   1.1  drochner #include <sys/param.h>
     34   1.1  drochner #include <sys/systm.h>
     35  1.26   thorpej #include <sys/callout.h>
     36   1.1  drochner #include <sys/kernel.h>
     37   1.1  drochner #include <sys/device.h>
     38   1.1  drochner #include <sys/malloc.h>
     39   1.1  drochner #include <sys/queue.h>
     40   1.1  drochner #include <machine/bus.h>
     41   1.1  drochner 
     42   1.4  drochner #include <dev/ic/mc6845reg.h>
     43   1.4  drochner #include <dev/ic/pcdisplayvar.h>
     44   1.4  drochner #include <dev/ic/vgareg.h>
     45   1.4  drochner #include <dev/ic/vgavar.h>
     46   1.4  drochner 
     47   1.1  drochner #include <dev/wscons/wsdisplayvar.h>
     48   1.1  drochner #include <dev/wscons/wsconsio.h>
     49  1.14  drochner #include <dev/wscons/unicode.h>
     50  1.37  drochner #include <dev/wsfont/wsfont.h>
     51   1.1  drochner 
     52   1.3  drochner #include <dev/ic/pcdisplay.h>
     53   1.1  drochner 
     54  1.57  christos /* for WSCONS_SUPPORT_PCVTFONTS and WSDISPLAY_CHARFUNCS */
     55  1.57  christos #include "opt_wsdisplay_compat.h"
     56  1.56  drochner #include "opt_vga.h"
     57  1.16  drochner 
     58  1.37  drochner static struct wsdisplay_font _vga_builtinfont = {
     59  1.12  drochner 	"builtin",
     60  1.37  drochner 	0, 256,
     61  1.12  drochner 	WSDISPLAY_FONTENC_IBM,
     62  1.37  drochner 	8, 16, 1,
     63  1.37  drochner 	WSDISPLAY_FONTORDER_L2R, 0,
     64  1.12  drochner 	0
     65  1.12  drochner };
     66  1.12  drochner 
     67  1.37  drochner struct egavga_font {
     68  1.37  drochner 	struct wsdisplay_font *wsfont;
     69  1.58  drochner 	int cookie; /* wsfont handle, -1 invalid */
     70  1.37  drochner 	int slot; /* in adapter RAM */
     71  1.37  drochner 	int usecount;
     72  1.37  drochner 	TAILQ_ENTRY(egavga_font) next; /* LRU queue */
     73  1.37  drochner };
     74  1.37  drochner 
     75  1.37  drochner static struct egavga_font vga_builtinfont = {
     76  1.37  drochner 	&_vga_builtinfont,
     77  1.58  drochner 	-1, 0
     78  1.37  drochner };
     79  1.37  drochner 
     80  1.38  drochner #ifdef VGA_CONSOLE_SCREENTYPE
     81  1.38  drochner static struct egavga_font vga_consolefont;
     82  1.38  drochner #endif
     83  1.38  drochner 
     84   1.1  drochner struct vgascreen {
     85   1.4  drochner 	struct pcdisplayscreen pcs;
     86   1.4  drochner 
     87   1.1  drochner 	LIST_ENTRY(vgascreen) next;
     88   1.1  drochner 
     89   1.1  drochner 	struct vga_config *cfg;
     90   1.1  drochner 
     91   1.1  drochner 	/* videostate */
     92  1.37  drochner 	struct egavga_font *fontset1, *fontset2;
     93   1.1  drochner 	/* font data */
     94   1.1  drochner 	/* palette */
     95   1.8  drochner 
     96   1.8  drochner 	int mindispoffset, maxdispoffset;
     97   1.1  drochner };
     98   1.1  drochner 
     99   1.1  drochner static int vgaconsole, vga_console_type, vga_console_attached;
    100   1.1  drochner static struct vgascreen vga_console_screen;
    101   1.1  drochner static struct vga_config vga_console_vc;
    102   1.1  drochner 
    103  1.37  drochner struct egavga_font *egavga_getfont(struct vga_config *, struct vgascreen *,
    104  1.37  drochner 				   char *, int);
    105  1.37  drochner void egavga_unreffont(struct vga_config *, struct egavga_font *);
    106  1.37  drochner 
    107  1.45  junyoung int vga_selectfont(struct vga_config *, struct vgascreen *, char *, char *);
    108  1.45  junyoung void vga_init_screen(struct vga_config *, struct vgascreen *,
    109  1.45  junyoung 		     const struct wsscreen_descr *, int, long *);
    110  1.45  junyoung void vga_init(struct vga_config *, bus_space_tag_t, bus_space_tag_t);
    111  1.45  junyoung static void vga_setfont(struct vga_config *, struct vgascreen *);
    112  1.45  junyoung 
    113  1.45  junyoung static int vga_mapchar(void *, int, unsigned int *);
    114  1.45  junyoung static int vga_alloc_attr(void *, int, int, int, long *);
    115  1.45  junyoung static void vga_copyrows(void *, int, int, int);
    116   1.1  drochner 
    117   1.1  drochner const struct wsdisplay_emulops vga_emulops = {
    118   1.4  drochner 	pcdisplay_cursor,
    119  1.12  drochner 	vga_mapchar,
    120   1.6  drochner 	pcdisplay_putchar,
    121   1.4  drochner 	pcdisplay_copycols,
    122   1.4  drochner 	pcdisplay_erasecols,
    123   1.8  drochner 	vga_copyrows,
    124   1.4  drochner 	pcdisplay_eraserows,
    125   1.3  drochner 	vga_alloc_attr
    126   1.3  drochner };
    127   1.3  drochner 
    128   1.3  drochner /*
    129   1.3  drochner  * translate WS(=ANSI) color codes to standard pc ones
    130   1.3  drochner  */
    131  1.35  jdolecek static const unsigned char fgansitopc[] = {
    132   1.3  drochner #ifdef __alpha__
    133   1.3  drochner 	/*
    134   1.3  drochner 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
    135   1.3  drochner 	 * XXX We should probably not bother with this
    136   1.3  drochner 	 * XXX (reinitialize the palette registers).
    137   1.3  drochner 	 */
    138   1.3  drochner 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
    139   1.3  drochner 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
    140   1.3  drochner #else
    141   1.3  drochner 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
    142   1.3  drochner 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
    143   1.3  drochner #endif
    144   1.3  drochner }, bgansitopc[] = {
    145   1.3  drochner #ifdef __alpha__
    146   1.3  drochner 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
    147   1.3  drochner 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
    148   1.3  drochner #else
    149   1.3  drochner 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
    150   1.3  drochner 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
    151   1.3  drochner #endif
    152   1.1  drochner };
    153   1.1  drochner 
    154  1.33     lukem const struct wsscreen_descr vga_25lscreen = {
    155   1.1  drochner 	"80x25", 80, 25,
    156   1.1  drochner 	&vga_emulops,
    157   1.3  drochner 	8, 16,
    158   1.3  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    159  1.33     lukem }, vga_25lscreen_mono = {
    160   1.3  drochner 	"80x25", 80, 25,
    161   1.3  drochner 	&vga_emulops,
    162   1.3  drochner 	8, 16,
    163   1.3  drochner 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    164  1.33     lukem }, vga_25lscreen_bf = {
    165  1.12  drochner 	"80x25bf", 80, 25,
    166  1.12  drochner 	&vga_emulops,
    167  1.12  drochner 	8, 16,
    168  1.12  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    169  1.17  drochner }, vga_40lscreen = {
    170  1.17  drochner 	"80x40", 80, 40,
    171  1.17  drochner 	&vga_emulops,
    172  1.17  drochner 	8, 10,
    173  1.17  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    174  1.17  drochner }, vga_40lscreen_mono = {
    175  1.17  drochner 	"80x40", 80, 40,
    176  1.17  drochner 	&vga_emulops,
    177  1.17  drochner 	8, 10,
    178  1.17  drochner 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    179  1.17  drochner }, vga_40lscreen_bf = {
    180  1.17  drochner 	"80x40bf", 80, 40,
    181  1.17  drochner 	&vga_emulops,
    182  1.17  drochner 	8, 10,
    183  1.17  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    184  1.12  drochner }, vga_50lscreen = {
    185   1.1  drochner 	"80x50", 80, 50,
    186   1.1  drochner 	&vga_emulops,
    187   1.3  drochner 	8, 8,
    188   1.3  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    189   1.3  drochner }, vga_50lscreen_mono = {
    190   1.3  drochner 	"80x50", 80, 50,
    191   1.3  drochner 	&vga_emulops,
    192   1.3  drochner 	8, 8,
    193   1.3  drochner 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    194  1.12  drochner }, vga_50lscreen_bf = {
    195  1.12  drochner 	"80x50bf", 80, 50,
    196  1.12  drochner 	&vga_emulops,
    197  1.12  drochner 	8, 8,
    198  1.12  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    199  1.30  drochner }, vga_24lscreen = {
    200  1.30  drochner 	"80x24", 80, 24,
    201  1.30  drochner 	&vga_emulops,
    202  1.30  drochner 	8, 16,
    203  1.30  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK
    204  1.30  drochner }, vga_24lscreen_mono = {
    205  1.30  drochner 	"80x24", 80, 24,
    206  1.30  drochner 	&vga_emulops,
    207  1.30  drochner 	8, 16,
    208  1.30  drochner 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE
    209  1.30  drochner }, vga_24lscreen_bf = {
    210  1.30  drochner 	"80x24bf", 80, 24,
    211  1.30  drochner 	&vga_emulops,
    212  1.30  drochner 	8, 16,
    213  1.30  drochner 	WSSCREEN_WSCOLORS | WSSCREEN_BLINK
    214   1.1  drochner };
    215   1.1  drochner 
    216  1.12  drochner #define VGA_SCREEN_CANTWOFONTS(type) (!((type)->capabilities & WSSCREEN_HILIT))
    217  1.12  drochner 
    218   1.2  drochner const struct wsscreen_descr *_vga_scrlist[] = {
    219  1.33     lukem 	&vga_25lscreen,
    220  1.33     lukem 	&vga_25lscreen_bf,
    221  1.17  drochner 	&vga_40lscreen,
    222  1.17  drochner 	&vga_40lscreen_bf,
    223   1.1  drochner 	&vga_50lscreen,
    224  1.12  drochner 	&vga_50lscreen_bf,
    225  1.30  drochner 	&vga_24lscreen,
    226  1.30  drochner 	&vga_24lscreen_bf,
    227   1.1  drochner 	/* XXX other formats, graphics screen? */
    228   1.3  drochner }, *_vga_scrlist_mono[] = {
    229  1.33     lukem 	&vga_25lscreen_mono,
    230  1.17  drochner 	&vga_40lscreen_mono,
    231   1.3  drochner 	&vga_50lscreen_mono,
    232  1.30  drochner 	&vga_24lscreen_mono,
    233   1.3  drochner 	/* XXX other formats, graphics screen? */
    234   1.1  drochner };
    235   1.1  drochner 
    236   1.3  drochner const struct wsscreen_list vga_screenlist = {
    237   1.3  drochner 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
    238   1.3  drochner 	_vga_scrlist
    239   1.3  drochner }, vga_screenlist_mono = {
    240   1.3  drochner 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
    241   1.3  drochner 	_vga_scrlist_mono
    242   1.1  drochner };
    243   1.1  drochner 
    244  1.45  junyoung static int	vga_ioctl(void *, u_long, caddr_t, int, struct proc *);
    245  1.45  junyoung static paddr_t	vga_mmap(void *, off_t, int);
    246  1.45  junyoung static int	vga_alloc_screen(void *, const struct wsscreen_descr *,
    247  1.45  junyoung 				 void **, int *, int *, long *);
    248  1.45  junyoung static void	vga_free_screen(void *, void *);
    249  1.45  junyoung static int	vga_show_screen(void *, void *, int,
    250  1.45  junyoung 				void (*)(void *, int, int), void *);
    251  1.45  junyoung static int	vga_load_font(void *, void *, struct wsdisplay_font *);
    252  1.57  christos #ifdef WSDISPLAY_CHARFUNCS
    253  1.53  christos static int	vga_getwschar(void *, struct wsdisplay_char *);
    254  1.53  christos static int	vga_putwschar(void *, struct wsdisplay_char *);
    255  1.57  christos #endif /* WSDISPLAY_CHARFUNCS */
    256   1.1  drochner 
    257  1.45  junyoung void vga_doswitch(struct vga_config *);
    258  1.22  drochner 
    259   1.1  drochner const struct wsdisplay_accessops vga_accessops = {
    260   1.1  drochner 	vga_ioctl,
    261   1.1  drochner 	vga_mmap,
    262   1.1  drochner 	vga_alloc_screen,
    263   1.1  drochner 	vga_free_screen,
    264   1.1  drochner 	vga_show_screen,
    265  1.53  christos 	vga_load_font,
    266  1.53  christos 	NULL,
    267  1.57  christos #ifdef WSDISPLAY_CHARFUNCS
    268  1.53  christos 	vga_getwschar,
    269  1.53  christos 	vga_putwschar
    270  1.57  christos #else /* WSDISPLAY_CHARFUNCS */
    271  1.57  christos 	NULL,
    272  1.57  christos 	NULL
    273  1.57  christos #endif /* WSDISPLAY_CHARFUNCS */
    274   1.1  drochner };
    275   1.1  drochner 
    276   1.1  drochner /*
    277   1.1  drochner  * The following functions implement back-end configuration grabbing
    278   1.1  drochner  * and attachment.
    279   1.1  drochner  */
    280   1.1  drochner int
    281  1.45  junyoung vga_common_probe(bus_space_tag_t iot, bus_space_tag_t memt)
    282   1.1  drochner {
    283   1.1  drochner 	bus_space_handle_t ioh_vga, ioh_6845, memh;
    284   1.4  drochner 	u_int8_t regval;
    285   1.1  drochner 	u_int16_t vgadata;
    286   1.1  drochner 	int gotio_vga, gotio_6845, gotmem, mono, rv;
    287   1.1  drochner 	int dispoffset;
    288   1.1  drochner 
    289   1.1  drochner 	gotio_vga = gotio_6845 = gotmem = rv = 0;
    290   1.1  drochner 
    291   1.1  drochner 	if (bus_space_map(iot, 0x3c0, 0x10, 0, &ioh_vga))
    292   1.1  drochner 		goto bad;
    293   1.1  drochner 	gotio_vga = 1;
    294   1.1  drochner 
    295   1.1  drochner 	/* read "misc output register" */
    296   1.4  drochner 	regval = bus_space_read_1(iot, ioh_vga, 0xc);
    297   1.4  drochner 	mono = !(regval & 1);
    298   1.1  drochner 
    299   1.1  drochner 	if (bus_space_map(iot, (mono ? 0x3b0 : 0x3d0), 0x10, 0, &ioh_6845))
    300   1.1  drochner 		goto bad;
    301   1.1  drochner 	gotio_6845 = 1;
    302   1.1  drochner 
    303   1.1  drochner 	if (bus_space_map(memt, 0xa0000, 0x20000, 0, &memh))
    304   1.1  drochner 		goto bad;
    305   1.1  drochner 	gotmem = 1;
    306   1.1  drochner 
    307   1.1  drochner 	dispoffset = (mono ? 0x10000 : 0x18000);
    308   1.1  drochner 
    309   1.1  drochner 	vgadata = bus_space_read_2(memt, memh, dispoffset);
    310   1.1  drochner 	bus_space_write_2(memt, memh, dispoffset, 0xa55a);
    311   1.4  drochner 	if (bus_space_read_2(memt, memh, dispoffset) != 0xa55a)
    312   1.4  drochner 		goto bad;
    313   1.1  drochner 	bus_space_write_2(memt, memh, dispoffset, vgadata);
    314   1.1  drochner 
    315   1.4  drochner 	/*
    316   1.4  drochner 	 * check if this is really a VGA
    317   1.4  drochner 	 * (try to write "Color Select" register as XFree86 does)
    318   1.4  drochner 	 * XXX check before if at least EGA?
    319   1.4  drochner 	 */
    320   1.4  drochner 	/* reset state */
    321   1.4  drochner 	(void) bus_space_read_1(iot, ioh_6845, 10);
    322   1.4  drochner 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
    323   1.4  drochner 			  20 | 0x20); /* colselect | enable */
    324   1.4  drochner 	regval = bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR);
    325   1.4  drochner 	/* toggle the implemented bits */
    326   1.4  drochner 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval ^ 0x0f);
    327   1.4  drochner 	bus_space_write_1(iot, ioh_vga, VGA_ATC_INDEX,
    328   1.4  drochner 			  20 | 0x20);
    329   1.4  drochner 	/* read back */
    330   1.4  drochner 	if (bus_space_read_1(iot, ioh_vga, VGA_ATC_DATAR) != (regval ^ 0x0f))
    331   1.4  drochner 		goto bad;
    332   1.4  drochner 	/* restore contents */
    333   1.4  drochner 	bus_space_write_1(iot, ioh_vga, VGA_ATC_DATAW, regval);
    334   1.4  drochner 
    335   1.4  drochner 	rv = 1;
    336   1.1  drochner bad:
    337   1.1  drochner 	if (gotio_vga)
    338   1.1  drochner 		bus_space_unmap(iot, ioh_vga, 0x10);
    339   1.1  drochner 	if (gotio_6845)
    340   1.1  drochner 		bus_space_unmap(iot, ioh_6845, 0x10);
    341   1.1  drochner 	if (gotmem)
    342   1.1  drochner 		bus_space_unmap(memt, memh, 0x20000);
    343   1.1  drochner 
    344   1.1  drochner 	return (rv);
    345   1.1  drochner }
    346   1.1  drochner 
    347  1.12  drochner /*
    348  1.15  drochner  * We want at least ASCII 32..127 be present in the
    349  1.15  drochner  * first font slot.
    350  1.12  drochner  */
    351  1.12  drochner #define vga_valid_primary_font(f) \
    352  1.37  drochner 	(f->wsfont->encoding == WSDISPLAY_FONTENC_IBM || \
    353  1.37  drochner 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO || \
    354  1.37  drochner 	f->wsfont->encoding == WSDISPLAY_FONTENC_ISO7)
    355  1.37  drochner 
    356  1.37  drochner struct egavga_font *
    357  1.45  junyoung egavga_getfont(struct vga_config *vc, struct vgascreen *scr, char *name,
    358  1.45  junyoung 	       int primary)
    359  1.37  drochner {
    360  1.37  drochner 	struct egavga_font *f;
    361  1.37  drochner 	int cookie;
    362  1.37  drochner 	struct wsdisplay_font *wf;
    363  1.37  drochner 
    364  1.37  drochner 	TAILQ_FOREACH(f, &vc->vc_fontlist, next) {
    365  1.37  drochner 		if (wsfont_matches(f->wsfont, name,
    366  1.48        ad 				   8, scr->pcs.type->fontheight, 0) &&
    367  1.37  drochner 		    (!primary || vga_valid_primary_font(f))) {
    368  1.37  drochner #ifdef VGAFONTDEBUG
    369  1.37  drochner 			if (scr != &vga_console_screen || vga_console_attached)
    370  1.37  drochner 				printf("vga_getfont: %s already present\n",
    371  1.38  drochner 				       name ? name : "<default>");
    372  1.37  drochner #endif
    373  1.37  drochner 			goto found;
    374  1.37  drochner 		}
    375  1.37  drochner 	}
    376  1.37  drochner 
    377  1.48        ad 	cookie = wsfont_find(name, 8, scr->pcs.type->fontheight, 0,
    378  1.48        ad 	                     WSDISPLAY_FONTORDER_L2R, 0);
    379  1.37  drochner 	/* XXX obey "primary" */
    380  1.37  drochner 	if (cookie == -1) {
    381  1.37  drochner #ifdef VGAFONTDEBUG
    382  1.37  drochner 		if (scr != &vga_console_screen || vga_console_attached)
    383  1.51  drochner 			printf("vga_getfont: %s not found\n",
    384  1.51  drochner 			       name ? name : "<default>");
    385  1.37  drochner #endif
    386  1.37  drochner 		return (0);
    387  1.37  drochner 	}
    388  1.37  drochner 
    389  1.47        ad 	if (wsfont_lock(cookie, &wf))
    390  1.37  drochner 		return (0);
    391  1.37  drochner 
    392  1.38  drochner #ifdef VGA_CONSOLE_SCREENTYPE
    393  1.38  drochner 	if (scr == &vga_console_screen)
    394  1.38  drochner 		f = &vga_consolefont;
    395  1.38  drochner 	else
    396  1.38  drochner #endif
    397  1.37  drochner 	f = malloc(sizeof(struct egavga_font), M_DEVBUF, M_NOWAIT);
    398  1.37  drochner 	if (!f) {
    399  1.37  drochner 		wsfont_unlock(cookie);
    400  1.37  drochner 		return (0);
    401  1.37  drochner 	}
    402  1.37  drochner 	f->wsfont = wf;
    403  1.37  drochner 	f->cookie = cookie;
    404  1.37  drochner 	f->slot = -1; /* not yet loaded */
    405  1.37  drochner 	f->usecount = 0; /* incremented below */
    406  1.37  drochner 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
    407  1.37  drochner 
    408  1.37  drochner found:
    409  1.37  drochner 	f->usecount++;
    410  1.37  drochner #ifdef VGAFONTDEBUG
    411  1.37  drochner 	if (scr != &vga_console_screen || vga_console_attached)
    412  1.37  drochner 		printf("vga_getfont: usecount=%d\n", f->usecount);
    413  1.37  drochner #endif
    414  1.37  drochner 	return (f);
    415  1.37  drochner }
    416  1.37  drochner 
    417  1.37  drochner void
    418  1.45  junyoung egavga_unreffont(struct vga_config *vc, struct egavga_font *f)
    419  1.37  drochner {
    420  1.37  drochner 
    421  1.37  drochner 	f->usecount--;
    422  1.37  drochner #ifdef VGAFONTDEBUG
    423  1.37  drochner 	printf("vga_unreffont: usecount=%d\n", f->usecount);
    424  1.37  drochner #endif
    425  1.58  drochner 	if (f->usecount == 0 && f->cookie != -1) {
    426  1.37  drochner 		TAILQ_REMOVE(&vc->vc_fontlist, f, next);
    427  1.41  drochner 		if (f->slot != -1) {
    428  1.41  drochner 			KASSERT(vc->vc_fonts[f->slot] == f);
    429  1.41  drochner 			vc->vc_fonts[f->slot] = 0;
    430  1.41  drochner 		}
    431  1.37  drochner 		wsfont_unlock(f->cookie);
    432  1.38  drochner #ifdef VGA_CONSOLE_SCREENTYPE
    433  1.38  drochner 		if (f != &vga_consolefont)
    434  1.38  drochner #endif
    435  1.37  drochner 		free(f, M_DEVBUF);
    436  1.37  drochner 	}
    437  1.37  drochner }
    438  1.12  drochner 
    439  1.12  drochner int
    440  1.45  junyoung vga_selectfont(struct vga_config *vc, struct vgascreen *scr, char *name1,
    441  1.45  junyoung 	       char *name2)
    442  1.12  drochner {
    443  1.12  drochner 	const struct wsscreen_descr *type = scr->pcs.type;
    444  1.37  drochner 	struct egavga_font *f1, *f2;
    445  1.12  drochner 
    446  1.37  drochner 	f1 = egavga_getfont(vc, scr, name1, 1);
    447  1.37  drochner 	if (!f1)
    448  1.37  drochner 		return (ENXIO);
    449  1.12  drochner 
    450  1.37  drochner 	if (VGA_SCREEN_CANTWOFONTS(type) && name2) {
    451  1.37  drochner 		f2 = egavga_getfont(vc, scr, name2, 0);
    452  1.37  drochner 		if (!f2) {
    453  1.37  drochner 			egavga_unreffont(vc, f1);
    454  1.37  drochner 			return (ENXIO);
    455  1.12  drochner 		}
    456  1.37  drochner 	} else
    457  1.37  drochner 		f2 = 0;
    458  1.12  drochner 
    459  1.12  drochner #ifdef VGAFONTDEBUG
    460  1.37  drochner 	if (scr != &vga_console_screen || vga_console_attached) {
    461  1.37  drochner 		printf("vga (%s): font1=%s (slot %d)", type->name,
    462  1.37  drochner 		       f1->wsfont->name, f1->slot);
    463  1.37  drochner 		if (f2)
    464  1.37  drochner 			printf(", font2=%s (slot %d)",
    465  1.37  drochner 			       f2->wsfont->name, f2->slot);
    466  1.37  drochner 		printf("\n");
    467  1.37  drochner 	}
    468  1.12  drochner #endif
    469  1.37  drochner 	if (scr->fontset1)
    470  1.37  drochner 		egavga_unreffont(vc, scr->fontset1);
    471  1.37  drochner 	scr->fontset1 = f1;
    472  1.37  drochner 	if (scr->fontset2)
    473  1.37  drochner 		egavga_unreffont(vc, scr->fontset2);
    474  1.37  drochner 	scr->fontset2 = f2;
    475  1.37  drochner 	return (0);
    476  1.12  drochner }
    477  1.12  drochner 
    478   1.1  drochner void
    479  1.45  junyoung vga_init_screen(struct vga_config *vc, struct vgascreen *scr,
    480  1.45  junyoung 		const struct wsscreen_descr *type, int existing, long *attrp)
    481   1.1  drochner {
    482   1.8  drochner 	int cpos;
    483   1.3  drochner 	int res;
    484   1.1  drochner 
    485   1.4  drochner 	scr->cfg = vc;
    486   1.4  drochner 	scr->pcs.hdl = (struct pcdisplay_handle *)&vc->hdl;
    487   1.4  drochner 	scr->pcs.type = type;
    488  1.39  drochner 	scr->pcs.active = existing;
    489   1.8  drochner 	scr->mindispoffset = 0;
    490   1.8  drochner 	scr->maxdispoffset = 0x8000 - type->nrows * type->ncols * 2;
    491   1.1  drochner 
    492   1.1  drochner 	if (existing) {
    493  1.39  drochner 		vc->active = scr;
    494  1.39  drochner 
    495   1.1  drochner 		cpos = vga_6845_read(&vc->hdl, cursorh) << 8;
    496   1.1  drochner 		cpos |= vga_6845_read(&vc->hdl, cursorl);
    497   1.1  drochner 
    498   1.1  drochner 		/* make sure we have a valid cursor position */
    499   1.1  drochner 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
    500   1.1  drochner 			cpos = 0;
    501   1.8  drochner 
    502   1.8  drochner 		scr->pcs.dispoffset = vga_6845_read(&vc->hdl, startadrh) << 9;
    503   1.8  drochner 		scr->pcs.dispoffset |= vga_6845_read(&vc->hdl, startadrl) << 1;
    504   1.8  drochner 
    505   1.8  drochner 		/* make sure we have a valid memory offset */
    506   1.8  drochner 		if (scr->pcs.dispoffset < scr->mindispoffset ||
    507   1.8  drochner 		    scr->pcs.dispoffset > scr->maxdispoffset)
    508   1.8  drochner 			scr->pcs.dispoffset = scr->mindispoffset;
    509  1.40  drochner 
    510  1.40  drochner 		if (type != vc->currenttype) {
    511  1.40  drochner 			vga_setscreentype(&vc->hdl, type);
    512  1.40  drochner 			vc->currenttype = type;
    513  1.40  drochner 		}
    514   1.8  drochner 	} else {
    515   1.8  drochner 		cpos = 0;
    516   1.8  drochner 		scr->pcs.dispoffset = scr->mindispoffset;
    517   1.1  drochner 	}
    518   1.1  drochner 
    519   1.4  drochner 	scr->pcs.vc_crow = cpos / type->ncols;
    520   1.4  drochner 	scr->pcs.vc_ccol = cpos % type->ncols;
    521  1.25        ad 	pcdisplay_cursor_init(&scr->pcs, existing);
    522   1.1  drochner 
    523   1.1  drochner #ifdef __alpha__
    524   1.4  drochner 	if (!vc->hdl.vh_mono)
    525   1.3  drochner 		/*
    526   1.3  drochner 		 * DEC firmware uses a blue background.
    527   1.3  drochner 		 */
    528   1.3  drochner 		res = vga_alloc_attr(scr, WSCOL_WHITE, WSCOL_BLUE,
    529   1.3  drochner 				     WSATTR_WSCOLORS, attrp);
    530   1.3  drochner 	else
    531   1.3  drochner #endif
    532   1.3  drochner 	res = vga_alloc_attr(scr, 0, 0, 0, attrp);
    533   1.3  drochner #ifdef DIAGNOSTIC
    534   1.3  drochner 	if (res)
    535   1.3  drochner 		panic("vga_init_screen: attribute botch");
    536   1.1  drochner #endif
    537   1.1  drochner 
    538   1.4  drochner 	scr->pcs.mem = NULL;
    539  1.15  drochner 
    540  1.37  drochner 	wsfont_init();
    541  1.15  drochner 	scr->fontset1 = scr->fontset2 = 0;
    542  1.12  drochner 	if (vga_selectfont(vc, scr, 0, 0)) {
    543  1.12  drochner 		if (scr == &vga_console_screen)
    544  1.12  drochner 			panic("vga_init_screen: no font");
    545  1.12  drochner 		else
    546  1.12  drochner 			printf("vga_init_screen: no font\n");
    547  1.12  drochner 	}
    548  1.40  drochner 	if (existing)
    549  1.40  drochner 		vga_setfont(vc, scr);
    550  1.15  drochner 
    551   1.1  drochner 	vc->nscreens++;
    552   1.1  drochner 	LIST_INSERT_HEAD(&vc->screens, scr, next);
    553   1.1  drochner }
    554   1.1  drochner 
    555   1.1  drochner void
    556  1.45  junyoung vga_init(struct vga_config *vc, bus_space_tag_t iot, bus_space_tag_t memt)
    557   1.1  drochner {
    558   1.1  drochner 	struct vga_handle *vh = &vc->hdl;
    559   1.1  drochner 	u_int8_t mor;
    560  1.12  drochner 	int i;
    561   1.1  drochner 
    562   1.1  drochner         vh->vh_iot = iot;
    563   1.1  drochner         vh->vh_memt = memt;
    564   1.1  drochner 
    565   1.1  drochner         if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
    566   1.1  drochner                 panic("vga_common_setup: couldn't map vga io");
    567   1.1  drochner 
    568   1.1  drochner 	/* read "misc output register" */
    569   1.1  drochner 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, 0xc);
    570   1.4  drochner 	vh->vh_mono = !(mor & 1);
    571   1.1  drochner 
    572   1.4  drochner 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
    573   1.1  drochner 			  &vh->vh_ioh_6845))
    574   1.1  drochner                 panic("vga_common_setup: couldn't map 6845 io");
    575   1.1  drochner 
    576   1.1  drochner         if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
    577   1.1  drochner                 panic("vga_common_setup: couldn't map memory");
    578   1.1  drochner 
    579   1.1  drochner         if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
    580   1.4  drochner 				(vh->vh_mono ? 0x10000 : 0x18000), 0x8000,
    581   1.1  drochner 				&vh->vh_memh))
    582   1.1  drochner                 panic("vga_common_setup: mem subrange failed");
    583   1.5  drochner 
    584   1.5  drochner 	/* should only reserve the space (no need to map - save KVM) */
    585   1.5  drochner 	vc->vc_biostag = memt;
    586   1.5  drochner 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0,
    587   1.5  drochner 			  &vc->vc_bioshdl))
    588   1.5  drochner 		vc->vc_biosmapped = 0;
    589   1.5  drochner 	else
    590   1.5  drochner 		vc->vc_biosmapped = 1;
    591   1.1  drochner 
    592   1.1  drochner 	vc->nscreens = 0;
    593   1.1  drochner 	LIST_INIT(&vc->screens);
    594   1.1  drochner 	vc->active = NULL;
    595  1.34  drochner 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
    596  1.26   thorpej 	callout_init(&vc->vc_switch_callout);
    597  1.12  drochner 
    598  1.12  drochner 	vc->vc_fonts[0] = &vga_builtinfont;
    599  1.23  drochner 	for (i = 1; i < 8; i++)
    600  1.12  drochner 		vc->vc_fonts[i] = 0;
    601  1.37  drochner 	TAILQ_INIT(&vc->vc_fontlist);
    602  1.37  drochner 	TAILQ_INSERT_HEAD(&vc->vc_fontlist, &vga_builtinfont, next);
    603  1.12  drochner 
    604  1.12  drochner 	vc->currentfontset1 = vc->currentfontset2 = 0;
    605   1.1  drochner }
    606   1.1  drochner 
    607   1.1  drochner void
    608  1.45  junyoung vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
    609  1.56  drochner 		  bus_space_tag_t memt, int type, int quirks,
    610  1.56  drochner 		  const struct vga_funcs *vf)
    611  1.28      soda {
    612   1.1  drochner 	int console;
    613   1.1  drochner 	struct vga_config *vc;
    614   1.1  drochner 	struct wsemuldisplaydev_attach_args aa;
    615   1.1  drochner 
    616   1.1  drochner 	console = vga_is_console(iot, type);
    617   1.1  drochner 
    618   1.1  drochner 	if (console) {
    619   1.1  drochner 		vc = &vga_console_vc;
    620   1.1  drochner 		vga_console_attached = 1;
    621   1.1  drochner 	} else {
    622   1.1  drochner 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
    623   1.1  drochner 		vga_init(vc, iot, memt);
    624   1.1  drochner 	}
    625   1.1  drochner 
    626  1.58  drochner 	if (quirks & VGA_QUIRK_ONEFONT) {
    627  1.58  drochner 		vc->vc_nfontslots = 1;
    628  1.58  drochner #ifndef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    629  1.58  drochner 		/*
    630  1.58  drochner 		 * XXX maybe invalidate font in slot > 0, but this can
    631  1.58  drochner 		 * only be happen with VGA_CONSOLE_SCREENTYPE, and then
    632  1.58  drochner 		 * we require VGA_CONSOLE_ATI_BROKEN_FONTSEL anyway.
    633  1.58  drochner 		 */
    634  1.58  drochner #endif
    635  1.58  drochner 	} else {
    636  1.58  drochner 		vc->vc_nfontslots = 8;
    637  1.58  drochner #ifndef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    638  1.58  drochner 		/*
    639  1.58  drochner 		 * XXX maybe validate builtin font shifted to slot 1 if
    640  1.58  drochner 		 * slot 0 got overwritten because of VGA_CONSOLE_SCREENTYPE,
    641  1.58  drochner 		 * but it will be reloaded anyway if needed.
    642  1.58  drochner 		 */
    643  1.58  drochner #endif
    644  1.58  drochner 	}
    645  1.58  drochner 
    646  1.58  drochner 	/*
    647  1.58  drochner 	 * Save the builtin font to memory. In case it got overwritten
    648  1.58  drochner 	 * in console initialization, use the copy in slot 1.
    649  1.58  drochner 	 */
    650  1.58  drochner #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    651  1.58  drochner #define BUILTINFONTLOC (vga_builtinfont.slot == -1 ? 1 : 0)
    652  1.58  drochner #else
    653  1.58  drochner 	KASSERT(vga_builtinfont.slot == 0);
    654  1.58  drochner #define BUILTINFONTLOC (0)
    655  1.58  drochner #endif
    656  1.58  drochner 	vga_builtinfont.wsfont->data =
    657  1.58  drochner 		malloc(256 * vga_builtinfont.wsfont->fontheight,
    658  1.58  drochner 		       M_DEVBUF, M_WAITOK);
    659  1.58  drochner 	vga_readoutchars(&vc->hdl, BUILTINFONTLOC, 0, 256,
    660  1.56  drochner 			 vga_builtinfont.wsfont->fontheight,
    661  1.56  drochner 			 vga_builtinfont.wsfont->data);
    662  1.56  drochner 
    663  1.42   thorpej 	vc->vc_type = type;
    664  1.42   thorpej 	vc->vc_funcs = vf;
    665  1.42   thorpej 
    666  1.42   thorpej 	sc->sc_vc = vc;
    667  1.42   thorpej 	vc->softc = sc;
    668  1.28      soda 
    669   1.1  drochner 	aa.console = console;
    670   1.4  drochner 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
    671   1.1  drochner 	aa.accessops = &vga_accessops;
    672   1.1  drochner 	aa.accesscookie = vc;
    673   1.1  drochner 
    674  1.42   thorpej         config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    675   1.1  drochner }
    676   1.1  drochner 
    677   1.1  drochner int
    678  1.45  junyoung vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
    679   1.1  drochner {
    680   1.3  drochner 	long defattr;
    681   1.3  drochner 	const struct wsscreen_descr *scr;
    682   1.3  drochner 
    683   1.1  drochner 	if (check && !vga_common_probe(iot, memt))
    684   1.1  drochner 		return (ENXIO);
    685   1.1  drochner 
    686   1.1  drochner 	/* set up bus-independent VGA configuration */
    687   1.1  drochner 	vga_init(&vga_console_vc, iot, memt);
    688  1.34  drochner #ifdef VGA_CONSOLE_SCREENTYPE
    689  1.34  drochner 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
    690  1.34  drochner 	       &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
    691  1.34  drochner 	if (!scr)
    692  1.34  drochner 		panic("vga_cnattach: invalid screen type");
    693  1.34  drochner #else
    694  1.11  drochner 	scr = vga_console_vc.currenttype;
    695  1.34  drochner #endif
    696  1.56  drochner #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
    697  1.56  drochner 	/*
    698  1.56  drochner 	 * On some (most/all?) ATI cards, only font slot 0 is usable.
    699  1.56  drochner 	 * vga_init_screen() might need font slot 0 for a non-default
    700  1.58  drochner 	 * console font, so save the builtin VGA font to another font slot.
    701  1.58  drochner 	 * The attach() code will take care later.
    702  1.56  drochner 	 */
    703  1.56  drochner 	vga_copyfont01(&vga_console_vc.hdl);
    704  1.56  drochner 	vga_console_vc.vc_nfontslots = 1;
    705  1.56  drochner #else
    706  1.56  drochner 	vga_console_vc.vc_nfontslots = 8;
    707  1.56  drochner #endif
    708   1.4  drochner 	vga_init_screen(&vga_console_vc, &vga_console_screen, scr, 1, &defattr);
    709   1.1  drochner 
    710   1.3  drochner 	wsdisplay_cnattach(scr, &vga_console_screen,
    711   1.4  drochner 			   vga_console_screen.pcs.vc_ccol,
    712   1.4  drochner 			   vga_console_screen.pcs.vc_crow,
    713   1.3  drochner 			   defattr);
    714   1.1  drochner 
    715   1.1  drochner 	vgaconsole = 1;
    716   1.1  drochner 	vga_console_type = type;
    717   1.1  drochner 	return (0);
    718   1.1  drochner }
    719   1.1  drochner 
    720   1.1  drochner int
    721  1.45  junyoung vga_is_console(bus_space_tag_t iot, int type)
    722   1.1  drochner {
    723   1.1  drochner 	if (vgaconsole &&
    724   1.1  drochner 	    !vga_console_attached &&
    725   1.1  drochner 	    iot == vga_console_vc.hdl.vh_iot &&
    726   1.1  drochner 	    (vga_console_type == -1 || (type == vga_console_type)))
    727   1.1  drochner 		return (1);
    728   1.1  drochner 	return (0);
    729   1.1  drochner }
    730   1.1  drochner 
    731  1.46     lukem #define	VGA_TS_BLANK	0x20
    732  1.46     lukem 
    733  1.46     lukem static int
    734  1.46     lukem vga_get_video(struct vga_config *vc)
    735  1.46     lukem {
    736  1.46     lukem 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_BLANK) == 0;
    737  1.46     lukem }
    738  1.46     lukem 
    739  1.46     lukem static void
    740  1.46     lukem vga_set_video(struct vga_config *vc, int state)
    741  1.46     lukem {
    742  1.46     lukem 	int val;
    743  1.46     lukem 
    744  1.46     lukem 	vga_ts_write(&vc->hdl, syncreset, 0x01);
    745  1.46     lukem 	if (state) {					/* unblank screen */
    746  1.46     lukem 		val = vga_ts_read(&vc->hdl, mode);
    747  1.46     lukem 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_BLANK);
    748  1.46     lukem #ifndef VGA_NO_VBLANK
    749  1.46     lukem 		val = vga_6845_read(&vc->hdl, mode);
    750  1.46     lukem 		vga_6845_write(&vc->hdl, mode, val | 0x80);
    751  1.46     lukem #endif
    752  1.46     lukem 	} else {					/* blank screen */
    753  1.46     lukem 		val = vga_ts_read(&vc->hdl, mode);
    754  1.46     lukem 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_BLANK);
    755  1.46     lukem #ifndef VGA_NO_VBLANK
    756  1.46     lukem 		val = vga_6845_read(&vc->hdl, mode);
    757  1.46     lukem 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
    758  1.46     lukem #endif
    759  1.46     lukem 	}
    760  1.46     lukem 	vga_ts_write(&vc->hdl, syncreset, 0x03);
    761  1.46     lukem }
    762  1.46     lukem 
    763   1.1  drochner int
    764  1.45  junyoung vga_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    765   1.1  drochner {
    766   1.1  drochner 	struct vga_config *vc = v;
    767  1.42   thorpej 	const struct vga_funcs *vf = vc->vc_funcs;
    768   1.1  drochner 
    769   1.1  drochner 	switch (cmd) {
    770   1.1  drochner 	case WSDISPLAYIO_GTYPE:
    771   1.1  drochner 		*(int *)data = vc->vc_type;
    772  1.10  augustss 		return 0;
    773  1.12  drochner 
    774   1.1  drochner 	case WSDISPLAYIO_GINFO:
    775  1.42   thorpej 		/* XXX should get detailed hardware information here */
    776  1.49    atatat 		return EPASSTHROUGH;
    777  1.42   thorpej 
    778  1.46     lukem 	case WSDISPLAYIO_GVIDEO:
    779  1.46     lukem 		*(int *)data = (vga_get_video(vc) ? WSDISPLAYIO_VIDEO_ON :
    780  1.46     lukem 				WSDISPLAYIO_VIDEO_OFF);
    781  1.46     lukem 		return 0;
    782  1.46     lukem 
    783  1.46     lukem 	case WSDISPLAYIO_SVIDEO:
    784  1.46     lukem 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
    785  1.46     lukem 		return 0;
    786  1.46     lukem 
    787   1.1  drochner 	case WSDISPLAYIO_GETCMAP:
    788   1.1  drochner 	case WSDISPLAYIO_PUTCMAP:
    789   1.1  drochner 	case WSDISPLAYIO_GCURPOS:
    790   1.1  drochner 	case WSDISPLAYIO_SCURPOS:
    791   1.1  drochner 	case WSDISPLAYIO_GCURMAX:
    792   1.1  drochner 	case WSDISPLAYIO_GCURSOR:
    793   1.1  drochner 	case WSDISPLAYIO_SCURSOR:
    794   1.1  drochner 		/* NONE of these operations are by the generic VGA driver. */
    795  1.49    atatat 		return EPASSTHROUGH;
    796   1.1  drochner 	}
    797  1.12  drochner 
    798  1.42   thorpej 	if (vc->vc_funcs == NULL)
    799  1.49    atatat 		return (EPASSTHROUGH);
    800  1.42   thorpej 
    801  1.42   thorpej 	if (vf->vf_ioctl == NULL)
    802  1.49    atatat 		return (EPASSTHROUGH);
    803  1.42   thorpej 
    804  1.42   thorpej 	return ((*vf->vf_ioctl)(v, cmd, data, flag, p));
    805   1.1  drochner }
    806   1.1  drochner 
    807  1.29    simonb static paddr_t
    808  1.45  junyoung vga_mmap(void *v, off_t offset, int prot)
    809   1.1  drochner {
    810  1.42   thorpej 	struct vga_config *vc = v;
    811  1.42   thorpej 	const struct vga_funcs *vf = vc->vc_funcs;
    812   1.1  drochner 
    813  1.42   thorpej 	if (vc->vc_funcs == NULL)
    814  1.42   thorpej 		return (-1);
    815  1.28      soda 
    816  1.42   thorpej 	if (vf->vf_mmap == NULL)
    817  1.42   thorpej 		return (-1);
    818  1.32   thorpej 
    819  1.42   thorpej 	return ((*vf->vf_mmap)(v, offset, prot));
    820   1.1  drochner }
    821   1.1  drochner 
    822   1.1  drochner int
    823  1.45  junyoung vga_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    824  1.45  junyoung 		 int *curxp, int *curyp, long *defattrp)
    825   1.1  drochner {
    826   1.1  drochner 	struct vga_config *vc = v;
    827   1.1  drochner 	struct vgascreen *scr;
    828   1.1  drochner 
    829   1.1  drochner 	if (vc->nscreens == 1) {
    830  1.38  drochner 		struct vgascreen *scr1 = vc->screens.lh_first;
    831   1.1  drochner 		/*
    832   1.1  drochner 		 * When allocating the second screen, get backing store
    833   1.1  drochner 		 * for the first one too.
    834   1.1  drochner 		 * XXX We could be more clever and use video RAM.
    835   1.1  drochner 		 */
    836  1.38  drochner 		scr1->pcs.mem =
    837  1.38  drochner 		  malloc(scr1->pcs.type->ncols * scr1->pcs.type->nrows * 2,
    838  1.38  drochner 			 M_DEVBUF, M_WAITOK);
    839   1.1  drochner 	}
    840   1.1  drochner 
    841   1.1  drochner 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
    842   1.4  drochner 	vga_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
    843   1.1  drochner 
    844  1.39  drochner 	if (vc->nscreens > 1) {
    845   1.4  drochner 		scr->pcs.mem = malloc(type->ncols * type->nrows * 2,
    846   1.4  drochner 				      M_DEVBUF, M_WAITOK);
    847   1.4  drochner 		pcdisplay_eraserows(&scr->pcs, 0, type->nrows, *defattrp);
    848   1.1  drochner 	}
    849   1.1  drochner 
    850   1.1  drochner 	*cookiep = scr;
    851   1.4  drochner 	*curxp = scr->pcs.vc_ccol;
    852   1.4  drochner 	*curyp = scr->pcs.vc_crow;
    853   1.1  drochner 
    854   1.1  drochner 	return (0);
    855   1.1  drochner }
    856   1.1  drochner 
    857   1.1  drochner void
    858  1.45  junyoung vga_free_screen(void *v, void *cookie)
    859   1.1  drochner {
    860   1.1  drochner 	struct vgascreen *vs = cookie;
    861  1.11  drochner 	struct vga_config *vc = vs->cfg;
    862   1.1  drochner 
    863   1.1  drochner 	LIST_REMOVE(vs, next);
    864  1.38  drochner 	if (vs->fontset1)
    865  1.38  drochner 		egavga_unreffont(vc, vs->fontset1);
    866  1.38  drochner 	if (vs->fontset2)
    867  1.38  drochner 		egavga_unreffont(vc, vs->fontset2);
    868  1.38  drochner 
    869   1.1  drochner 	if (vs != &vga_console_screen)
    870   1.1  drochner 		free(vs, M_DEVBUF);
    871   1.1  drochner 	else
    872   1.1  drochner 		panic("vga_free_screen: console");
    873  1.11  drochner 
    874  1.11  drochner 	if (vc->active == vs)
    875  1.11  drochner 		vc->active = 0;
    876   1.1  drochner }
    877   1.1  drochner 
    878  1.40  drochner static void vga_usefont(struct vga_config *, struct egavga_font *);
    879  1.37  drochner 
    880  1.40  drochner static void
    881  1.45  junyoung vga_usefont(struct vga_config *vc, struct egavga_font *f)
    882  1.37  drochner {
    883  1.37  drochner 	int slot;
    884  1.37  drochner 	struct egavga_font *of;
    885  1.37  drochner 
    886  1.37  drochner 	if (f->slot != -1)
    887  1.37  drochner 		goto toend;
    888  1.37  drochner 
    889  1.51  drochner 	for (slot = 0; slot < vc->vc_nfontslots; slot++) {
    890  1.37  drochner 		if (!vc->vc_fonts[slot])
    891  1.37  drochner 			goto loadit;
    892  1.37  drochner 	}
    893  1.37  drochner 
    894  1.37  drochner 	/* have to kick out another one */
    895  1.37  drochner 	TAILQ_FOREACH(of, &vc->vc_fontlist, next) {
    896  1.37  drochner 		if (of->slot != -1) {
    897  1.37  drochner 			KASSERT(vc->vc_fonts[of->slot] == of);
    898  1.37  drochner 			slot = of->slot;
    899  1.37  drochner 			of->slot = -1;
    900  1.37  drochner 			goto loadit;
    901  1.37  drochner 		}
    902  1.37  drochner 	}
    903  1.54  junyoung 	panic("vga_usefont");
    904  1.37  drochner 
    905  1.37  drochner loadit:
    906  1.52  drochner 	vga_loadchars(&vc->hdl, slot, f->wsfont->firstchar,
    907  1.52  drochner 		      f->wsfont->numchars, f->wsfont->fontheight,
    908  1.52  drochner 		      f->wsfont->data);
    909  1.37  drochner 	f->slot = slot;
    910  1.37  drochner 	vc->vc_fonts[slot] = f;
    911  1.37  drochner 
    912  1.37  drochner toend:
    913  1.37  drochner 	TAILQ_REMOVE(&vc->vc_fontlist, f, next);
    914  1.37  drochner 	TAILQ_INSERT_TAIL(&vc->vc_fontlist, f, next);
    915  1.37  drochner }
    916  1.37  drochner 
    917  1.12  drochner static void
    918  1.45  junyoung vga_setfont(struct vga_config *vc, struct vgascreen *scr)
    919  1.12  drochner {
    920  1.12  drochner 	int fontslot1, fontslot2;
    921  1.12  drochner 
    922  1.37  drochner 	if (scr->fontset1)
    923  1.37  drochner 		vga_usefont(vc, scr->fontset1);
    924  1.37  drochner 	if (scr->fontset2)
    925  1.37  drochner 		vga_usefont(vc, scr->fontset2);
    926  1.37  drochner 
    927  1.12  drochner 	fontslot1 = (scr->fontset1 ? scr->fontset1->slot : 0);
    928  1.12  drochner 	fontslot2 = (scr->fontset2 ? scr->fontset2->slot : fontslot1);
    929  1.12  drochner 	if (vc->currentfontset1 != fontslot1 ||
    930  1.12  drochner 	    vc->currentfontset2 != fontslot2) {
    931  1.12  drochner 		vga_setfontset(&vc->hdl, fontslot1, fontslot2);
    932  1.12  drochner 		vc->currentfontset1 = fontslot1;
    933  1.12  drochner 		vc->currentfontset2 = fontslot2;
    934  1.12  drochner 	}
    935  1.12  drochner }
    936  1.12  drochner 
    937  1.22  drochner int
    938  1.45  junyoung vga_show_screen(void *v, void *cookie, int waitok,
    939  1.45  junyoung 		void (*cb)(void *, int, int), void *cbarg)
    940   1.1  drochner {
    941   1.4  drochner 	struct vgascreen *scr = cookie, *oldscr;
    942   1.1  drochner 	struct vga_config *vc = scr->cfg;
    943  1.22  drochner 
    944  1.22  drochner 	oldscr = vc->active; /* can be NULL! */
    945  1.22  drochner 	if (scr == oldscr) {
    946  1.22  drochner 		return (0);
    947  1.22  drochner 	}
    948  1.22  drochner 
    949  1.22  drochner 	vc->wantedscreen = cookie;
    950  1.22  drochner 	vc->switchcb = cb;
    951  1.22  drochner 	vc->switchcbarg = cbarg;
    952  1.22  drochner 	if (cb) {
    953  1.26   thorpej 		callout_reset(&vc->vc_switch_callout, 0,
    954  1.26   thorpej 		    (void(*)(void *))vga_doswitch, vc);
    955  1.22  drochner 		return (EAGAIN);
    956  1.22  drochner 	}
    957  1.22  drochner 
    958  1.22  drochner 	vga_doswitch(vc);
    959  1.22  drochner 	return (0);
    960  1.22  drochner }
    961  1.22  drochner 
    962  1.22  drochner void
    963  1.45  junyoung vga_doswitch(struct vga_config *vc)
    964  1.22  drochner {
    965  1.22  drochner 	struct vgascreen *scr, *oldscr;
    966   1.1  drochner 	struct vga_handle *vh = &vc->hdl;
    967  1.22  drochner 	const struct wsscreen_descr *type;
    968   1.1  drochner 
    969  1.22  drochner 	scr = vc->wantedscreen;
    970  1.22  drochner 	if (!scr) {
    971  1.22  drochner 		printf("vga_doswitch: disappeared\n");
    972  1.22  drochner 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
    973  1.22  drochner 		return;
    974  1.22  drochner 	}
    975  1.22  drochner 	type = scr->pcs.type;
    976  1.11  drochner 	oldscr = vc->active; /* can be NULL! */
    977   1.4  drochner #ifdef DIAGNOSTIC
    978  1.11  drochner 	if (oldscr) {
    979  1.11  drochner 		if (!oldscr->pcs.active)
    980  1.11  drochner 			panic("vga_show_screen: not active");
    981  1.11  drochner 		if (oldscr->pcs.type != vc->currenttype)
    982  1.11  drochner 			panic("vga_show_screen: bad type");
    983  1.11  drochner 	}
    984   1.4  drochner #endif
    985   1.4  drochner 	if (scr == oldscr) {
    986   1.1  drochner 		return;
    987   1.4  drochner 	}
    988   1.4  drochner #ifdef DIAGNOSTIC
    989   1.4  drochner 	if (scr->pcs.active)
    990   1.4  drochner 		panic("vga_show_screen: active");
    991   1.4  drochner #endif
    992   1.4  drochner 
    993  1.11  drochner 	if (oldscr) {
    994  1.11  drochner 		const struct wsscreen_descr *oldtype = oldscr->pcs.type;
    995   1.1  drochner 
    996  1.11  drochner 		oldscr->pcs.active = 0;
    997  1.11  drochner 		bus_space_read_region_2(vh->vh_memt, vh->vh_memh,
    998  1.11  drochner 					oldscr->pcs.dispoffset, oldscr->pcs.mem,
    999  1.11  drochner 					oldtype->ncols * oldtype->nrows);
   1000  1.11  drochner 	}
   1001   1.1  drochner 
   1002  1.11  drochner 	if (vc->currenttype != type) {
   1003   1.4  drochner 		vga_setscreentype(vh, type);
   1004  1.11  drochner 		vc->currenttype = type;
   1005  1.11  drochner 	}
   1006  1.12  drochner 
   1007  1.12  drochner 	vga_setfont(vc, scr);
   1008  1.11  drochner 	/* XXX swich colours! */
   1009   1.1  drochner 
   1010   1.8  drochner 	scr->pcs.dispoffset = scr->mindispoffset;
   1011  1.11  drochner 	if (!oldscr || (scr->pcs.dispoffset != oldscr->pcs.dispoffset)) {
   1012   1.8  drochner 		vga_6845_write(vh, startadrh, scr->pcs.dispoffset >> 9);
   1013   1.8  drochner 		vga_6845_write(vh, startadrl, scr->pcs.dispoffset >> 1);
   1014   1.8  drochner 	}
   1015   1.8  drochner 
   1016  1.11  drochner 	bus_space_write_region_2(vh->vh_memt, vh->vh_memh,
   1017  1.11  drochner 				scr->pcs.dispoffset, scr->pcs.mem,
   1018  1.11  drochner 				type->ncols * type->nrows);
   1019  1.11  drochner 	scr->pcs.active = 1;
   1020   1.1  drochner 
   1021   1.4  drochner 	vc->active = scr;
   1022   1.1  drochner 
   1023   1.4  drochner 	pcdisplay_cursor(&scr->pcs, scr->pcs.cursoron,
   1024   1.4  drochner 			 scr->pcs.vc_crow, scr->pcs.vc_ccol);
   1025  1.22  drochner 
   1026  1.22  drochner 	vc->wantedscreen = 0;
   1027  1.22  drochner 	if (vc->switchcb)
   1028  1.22  drochner 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
   1029   1.1  drochner }
   1030   1.1  drochner 
   1031   1.1  drochner static int
   1032  1.45  junyoung vga_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1033   1.1  drochner {
   1034  1.12  drochner 	struct vga_config *vc = v;
   1035   1.1  drochner 	struct vgascreen *scr = cookie;
   1036  1.12  drochner 	char *name2;
   1037  1.37  drochner 	int res;
   1038  1.12  drochner 
   1039  1.12  drochner 	if (scr) {
   1040  1.50   hannken 		name2 = NULL;
   1041  1.50   hannken 		if (data->name) {
   1042  1.50   hannken 			name2 = strchr(data->name, ',');
   1043  1.50   hannken 			if (name2)
   1044  1.50   hannken 				*name2++ = '\0';
   1045  1.50   hannken 		}
   1046  1.12  drochner 		res = vga_selectfont(vc, scr, data->name, name2);
   1047  1.52  drochner 		if (!res && scr->pcs.active)
   1048  1.12  drochner 			vga_setfont(vc, scr);
   1049  1.12  drochner 		return (res);
   1050  1.12  drochner 	}
   1051   1.1  drochner 
   1052   1.1  drochner 	return (0);
   1053   1.1  drochner }
   1054   1.1  drochner 
   1055   1.3  drochner static int
   1056  1.45  junyoung vga_alloc_attr(void *id, int fg, int bg, int flags, long *attrp)
   1057   1.3  drochner {
   1058   1.3  drochner 	struct vgascreen *scr = id;
   1059   1.3  drochner 	struct vga_config *vc = scr->cfg;
   1060   1.3  drochner 
   1061   1.4  drochner 	if (vc->hdl.vh_mono) {
   1062   1.3  drochner 		if (flags & WSATTR_WSCOLORS)
   1063   1.3  drochner 			return (EINVAL);
   1064   1.3  drochner 		if (flags & WSATTR_REVERSE)
   1065   1.3  drochner 			*attrp = 0x70;
   1066   1.3  drochner 		else
   1067   1.3  drochner 			*attrp = 0x07;
   1068   1.3  drochner 		if (flags & WSATTR_UNDERLINE)
   1069   1.3  drochner 			*attrp |= FG_UNDERLINE;
   1070   1.3  drochner 		if (flags & WSATTR_HILIT)
   1071   1.3  drochner 			*attrp |= FG_INTENSE;
   1072   1.3  drochner 	} else {
   1073   1.3  drochner 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
   1074   1.3  drochner 			return (EINVAL);
   1075   1.3  drochner 		if (flags & WSATTR_WSCOLORS)
   1076   1.3  drochner 			*attrp = fgansitopc[fg] | bgansitopc[bg];
   1077   1.3  drochner 		else
   1078   1.3  drochner 			*attrp = 7;
   1079   1.3  drochner 		if (flags & WSATTR_HILIT)
   1080   1.3  drochner 			*attrp += 8;
   1081   1.3  drochner 	}
   1082   1.3  drochner 	if (flags & WSATTR_BLINK)
   1083   1.3  drochner 		*attrp |= FG_BLINK;
   1084   1.3  drochner 	return (0);
   1085   1.8  drochner }
   1086   1.8  drochner 
   1087  1.45  junyoung static void
   1088  1.45  junyoung vga_copyrows(void *id, int srcrow, int dstrow, int nrows)
   1089   1.8  drochner {
   1090   1.8  drochner 	struct vgascreen *scr = id;
   1091   1.8  drochner 	bus_space_tag_t memt = scr->pcs.hdl->ph_memt;
   1092   1.8  drochner 	bus_space_handle_t memh = scr->pcs.hdl->ph_memh;
   1093   1.8  drochner 	int ncols = scr->pcs.type->ncols;
   1094   1.8  drochner 	bus_size_t srcoff, dstoff;
   1095   1.8  drochner 
   1096   1.8  drochner 	srcoff = srcrow * ncols + 0;
   1097   1.8  drochner 	dstoff = dstrow * ncols + 0;
   1098   1.8  drochner 
   1099   1.8  drochner 	if (scr->pcs.active) {
   1100   1.8  drochner 		if (dstrow == 0 && (srcrow + nrows == scr->pcs.type->nrows)) {
   1101  1.18        ad #ifdef PCDISPLAY_SOFTCURSOR
   1102  1.20        ad 			int cursoron = scr->pcs.cursoron;
   1103  1.20        ad 
   1104  1.21   mycroft 			if (cursoron)
   1105  1.21   mycroft 				pcdisplay_cursor(&scr->pcs, 0,
   1106  1.21   mycroft 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
   1107  1.18        ad #endif
   1108   1.8  drochner 			/* scroll up whole screen */
   1109   1.8  drochner 			if ((scr->pcs.dispoffset + srcrow * ncols * 2)
   1110   1.8  drochner 			    <= scr->maxdispoffset) {
   1111   1.8  drochner 				scr->pcs.dispoffset += srcrow * ncols * 2;
   1112   1.8  drochner 			} else {
   1113   1.8  drochner 				bus_space_copy_region_2(memt, memh,
   1114   1.8  drochner 					scr->pcs.dispoffset + srcoff * 2,
   1115   1.8  drochner 					memh, scr->mindispoffset,
   1116   1.8  drochner 					nrows * ncols);
   1117   1.8  drochner 				scr->pcs.dispoffset = scr->mindispoffset;
   1118   1.8  drochner 			}
   1119   1.8  drochner 			vga_6845_write(&scr->cfg->hdl, startadrh,
   1120   1.8  drochner 				       scr->pcs.dispoffset >> 9);
   1121   1.8  drochner 			vga_6845_write(&scr->cfg->hdl, startadrl,
   1122   1.8  drochner 				       scr->pcs.dispoffset >> 1);
   1123  1.18        ad #ifdef PCDISPLAY_SOFTCURSOR
   1124  1.21   mycroft 			if (cursoron)
   1125  1.21   mycroft 				pcdisplay_cursor(&scr->pcs, 1,
   1126  1.21   mycroft 				    scr->pcs.vc_crow, scr->pcs.vc_ccol);
   1127  1.18        ad #endif
   1128   1.8  drochner 		} else {
   1129   1.8  drochner 			bus_space_copy_region_2(memt, memh,
   1130   1.8  drochner 					scr->pcs.dispoffset + srcoff * 2,
   1131   1.8  drochner 					memh, scr->pcs.dispoffset + dstoff * 2,
   1132   1.8  drochner 					nrows * ncols);
   1133   1.8  drochner 		}
   1134   1.8  drochner 	} else
   1135  1.36   thorpej 		memcpy(&scr->pcs.mem[dstoff], &scr->pcs.mem[srcoff],
   1136   1.8  drochner 		      nrows * ncols * 2);
   1137  1.12  drochner }
   1138  1.12  drochner 
   1139  1.12  drochner #ifdef WSCONS_SUPPORT_PCVTFONTS
   1140  1.12  drochner 
   1141  1.12  drochner #define NOTYET 0xffff
   1142  1.35  jdolecek static const u_int16_t pcvt_unichars[0xa0] = {
   1143  1.44     bjh21 /* 0 */	_e006U, /* N/L control */
   1144  1.14  drochner 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1145  1.12  drochner 	NOTYET,
   1146  1.12  drochner 	0x2409, /* SYMBOL FOR HORIZONTAL TABULATION */
   1147  1.12  drochner 	0x240a, /* SYMBOL FOR LINE FEED */
   1148  1.12  drochner 	0x240b, /* SYMBOL FOR VERTICAL TABULATION */
   1149  1.12  drochner 	0x240c, /* SYMBOL FOR FORM FEED */
   1150  1.12  drochner 	0x240d, /* SYMBOL FOR CARRIAGE RETURN */
   1151  1.12  drochner 	NOTYET, NOTYET,
   1152  1.14  drochner /* 1 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1153  1.12  drochner 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1154  1.12  drochner /* 2 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1155  1.12  drochner 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1156  1.14  drochner /* 3 */	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1157  1.12  drochner 	NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET, NOTYET,
   1158  1.12  drochner /* 4 */	0x03c1, /* GREEK SMALL LETTER RHO */
   1159  1.12  drochner 	0x03c8, /* GREEK SMALL LETTER PSI */
   1160  1.12  drochner 	0x2202, /* PARTIAL DIFFERENTIAL */
   1161  1.12  drochner 	0x03bb, /* GREEK SMALL LETTER LAMDA */
   1162  1.12  drochner 	0x03b9, /* GREEK SMALL LETTER IOTA */
   1163  1.12  drochner 	0x03b7, /* GREEK SMALL LETTER ETA */
   1164  1.12  drochner 	0x03b5, /* GREEK SMALL LETTER EPSILON */
   1165  1.12  drochner 	0x03c7, /* GREEK SMALL LETTER CHI */
   1166  1.12  drochner 	0x2228, /* LOGICAL OR */
   1167  1.12  drochner 	0x2227, /* LOGICAL AND */
   1168  1.12  drochner 	0x222a, /* UNION */
   1169  1.12  drochner 	0x2283, /* SUPERSET OF */
   1170  1.12  drochner 	0x2282, /* SUBSET OF */
   1171  1.12  drochner 	0x03a5, /* GREEK CAPITAL LETTER UPSILON */
   1172  1.12  drochner 	0x039e, /* GREEK CAPITAL LETTER XI */
   1173  1.12  drochner 	0x03a8, /* GREEK CAPITAL LETTER PSI */
   1174  1.14  drochner /* 5 */	0x03a0, /* GREEK CAPITAL LETTER PI */
   1175  1.12  drochner 	0x21d2, /* RIGHTWARDS DOUBLE ARROW */
   1176  1.12  drochner 	0x21d4, /* LEFT RIGHT DOUBLE ARROW */
   1177  1.12  drochner 	0x039b, /* GREEK CAPITAL LETTER LAMDA */
   1178  1.12  drochner 	0x0398, /* GREEK CAPITAL LETTER THETA */
   1179  1.12  drochner 	0x2243, /* ASYMPTOTICALLY EQUAL TO */
   1180  1.12  drochner 	0x2207, /* NABLA */
   1181  1.12  drochner 	0x2206, /* INCREMENT */
   1182  1.12  drochner 	0x221d, /* PROPORTIONAL TO */
   1183  1.12  drochner 	0x2234, /* THEREFORE */
   1184  1.12  drochner 	0x222b, /* INTEGRAL */
   1185  1.12  drochner 	0x2215, /* DIVISION SLASH */
   1186  1.12  drochner 	0x2216, /* SET MINUS */
   1187  1.44     bjh21 	_e00eU, /* angle? */
   1188  1.44     bjh21 	_e00dU, /* inverted angle? */
   1189  1.44     bjh21 	_e00bU, /* braceleftmid */
   1190  1.44     bjh21 /* 6 */	_e00cU, /* bracerightmid */
   1191  1.44     bjh21 	_e007U, /* bracelefttp */
   1192  1.44     bjh21 	_e008U, /* braceleftbt */
   1193  1.44     bjh21 	_e009U, /* bracerighttp */
   1194  1.44     bjh21 	_e00aU, /* bracerightbt */
   1195  1.12  drochner 	0x221a, /* SQUARE ROOT */
   1196  1.12  drochner 	0x03c9, /* GREEK SMALL LETTER OMEGA */
   1197  1.12  drochner 	0x00a5, /* YEN SIGN */
   1198  1.12  drochner 	0x03be, /* GREEK SMALL LETTER XI */
   1199  1.12  drochner 	0x00fd, /* LATIN SMALL LETTER Y WITH ACUTE */
   1200  1.12  drochner 	0x00fe, /* LATIN SMALL LETTER THORN */
   1201  1.12  drochner 	0x00f0, /* LATIN SMALL LETTER ETH */
   1202  1.12  drochner 	0x00de, /* LATIN CAPITAL LETTER THORN */
   1203  1.12  drochner 	0x00dd, /* LATIN CAPITAL LETTER Y WITH ACUTE */
   1204  1.12  drochner 	0x00d7, /* MULTIPLICATION SIGN */
   1205  1.12  drochner 	0x00d0, /* LATIN CAPITAL LETTER ETH */
   1206  1.14  drochner /* 7 */	0x00be, /* VULGAR FRACTION THREE QUARTERS */
   1207  1.12  drochner 	0x00b8, /* CEDILLA */
   1208  1.12  drochner 	0x00b4, /* ACUTE ACCENT */
   1209  1.12  drochner 	0x00af, /* MACRON */
   1210  1.12  drochner 	0x00ae, /* REGISTERED SIGN */
   1211  1.12  drochner 	0x00ad, /* SOFT HYPHEN */
   1212  1.12  drochner 	0x00ac, /* NOT SIGN */
   1213  1.12  drochner 	0x00a8, /* DIAERESIS */
   1214  1.12  drochner 	0x2260, /* NOT EQUAL TO */
   1215  1.44     bjh21 	_e005U, /* scan 9 */
   1216  1.44     bjh21 	_e004U, /* scan 7 */
   1217  1.44     bjh21 	_e003U, /* scan 5 */
   1218  1.44     bjh21 	_e002U, /* scan 3 */
   1219  1.44     bjh21 	_e001U, /* scan 1 */
   1220  1.12  drochner 	0x03c5, /* GREEK SMALL LETTER UPSILON */
   1221  1.12  drochner 	0x00f8, /* LATIN SMALL LETTER O WITH STROKE */
   1222  1.12  drochner /* 8 */	0x0153, /* LATIN SMALL LIGATURE OE */
   1223  1.12  drochner 	0x00f5, /* LATIN SMALL LETTER O WITH TILDE !!!doc bug */
   1224  1.12  drochner 	0x00e3, /* LATIN SMALL LETTER A WITH TILDE */
   1225  1.12  drochner 	0x0178, /* LATIN CAPITAL LETTER Y WITH DIAERESIS */
   1226  1.12  drochner 	0x00db, /* LATIN CAPITAL LETTER U WITH CIRCUMFLEX */
   1227  1.12  drochner 	0x00da, /* LATIN CAPITAL LETTER U WITH ACUTE */
   1228  1.12  drochner 	0x00d9, /* LATIN CAPITAL LETTER U WITH GRAVE */
   1229  1.12  drochner 	0x00d8, /* LATIN CAPITAL LETTER O WITH STROKE */
   1230  1.12  drochner 	0x0152, /* LATIN CAPITAL LIGATURE OE */
   1231  1.12  drochner 	0x00d5, /* LATIN CAPITAL LETTER O WITH TILDE */
   1232  1.12  drochner 	0x00d4, /* LATIN CAPITAL LETTER O WITH CIRCUMFLEX */
   1233  1.12  drochner 	0x00d3, /* LATIN CAPITAL LETTER O WITH ACUTE */
   1234  1.12  drochner 	0x00d2, /* LATIN CAPITAL LETTER O WITH GRAVE */
   1235  1.12  drochner 	0x00cf, /* LATIN CAPITAL LETTER I WITH DIAERESIS */
   1236  1.12  drochner 	0x00ce, /* LATIN CAPITAL LETTER I WITH CIRCUMFLEX */
   1237  1.12  drochner 	0x00cd, /* LATIN CAPITAL LETTER I WITH ACUTE */
   1238  1.14  drochner /* 9 */	0x00cc, /* LATIN CAPITAL LETTER I WITH GRAVE */
   1239  1.12  drochner 	0x00cb, /* LATIN CAPITAL LETTER E WITH DIAERESIS */
   1240  1.12  drochner 	0x00ca, /* LATIN CAPITAL LETTER E WITH CIRCUMFLEX */
   1241  1.12  drochner 	0x00c8, /* LATIN CAPITAL LETTER E WITH GRAVE */
   1242  1.12  drochner 	0x00c3, /* LATIN CAPITAL LETTER A WITH TILDE */
   1243  1.12  drochner 	0x00c2, /* LATIN CAPITAL LETTER A WITH CIRCUMFLEX */
   1244  1.12  drochner 	0x00c1, /* LATIN CAPITAL LETTER A WITH ACUTE */
   1245  1.12  drochner 	0x00c0, /* LATIN CAPITAL LETTER A WITH GRAVE */
   1246  1.12  drochner 	0x00b9, /* SUPERSCRIPT ONE */
   1247  1.12  drochner 	0x00b7, /* MIDDLE DOT */
   1248  1.12  drochner 	0x03b6, /* GREEK SMALL LETTER ZETA */
   1249  1.12  drochner 	0x00b3, /* SUPERSCRIPT THREE */
   1250  1.12  drochner 	0x00a9, /* COPYRIGHT SIGN */
   1251  1.12  drochner 	0x00a4, /* CURRENCY SIGN */
   1252  1.12  drochner 	0x03ba, /* GREEK SMALL LETTER KAPPA */
   1253  1.44     bjh21 	_e000U  /* mirrored question mark? */
   1254  1.12  drochner };
   1255  1.12  drochner 
   1256  1.45  junyoung static int vga_pcvt_mapchar(int, u_int *);
   1257  1.12  drochner 
   1258  1.12  drochner static int
   1259  1.45  junyoung vga_pcvt_mapchar(int uni, u_int *index)
   1260  1.12  drochner {
   1261  1.12  drochner 	int i;
   1262  1.12  drochner 
   1263  1.12  drochner 	for (i = 0; i < 0xa0; i++) /* 0xa0..0xff are reserved */
   1264  1.13  drochner 		if (uni == pcvt_unichars[i]) {
   1265  1.13  drochner 			*index = i;
   1266  1.13  drochner 			return (5);
   1267  1.13  drochner 		}
   1268  1.13  drochner 	*index = 0x99; /* middle dot */
   1269  1.13  drochner 	return (0);
   1270  1.12  drochner }
   1271  1.12  drochner 
   1272  1.12  drochner #endif /* WSCONS_SUPPORT_PCVTFONTS */
   1273  1.12  drochner 
   1274  1.34  drochner #ifdef WSCONS_SUPPORT_ISO7FONTS
   1275  1.34  drochner 
   1276  1.34  drochner static int
   1277  1.45  junyoung vga_iso7_mapchar(int uni, u_int *index)
   1278  1.34  drochner {
   1279  1.34  drochner 
   1280  1.34  drochner 	/*
   1281  1.34  drochner 	 * U+0384 (GREEK TONOS) to
   1282  1.34  drochner 	 * U+03ce (GREEK SMALL LETTER OMEGA WITH TONOS)
   1283  1.34  drochner 	 * map directly to the iso-9 font
   1284  1.34  drochner 	 */
   1285  1.34  drochner 	if (uni >= 0x0384 && uni <= 0x03ce) {
   1286  1.34  drochner 		/* U+0384 is at offset 0xb4 in the font */
   1287  1.34  drochner 		*index = uni - 0x0384 + 0xb4;
   1288  1.34  drochner 		return (5);
   1289  1.34  drochner 	}
   1290  1.34  drochner 
   1291  1.34  drochner 	/* XXX more chars in the iso-9 font */
   1292  1.34  drochner 
   1293  1.34  drochner 	*index = 0xa4; /* shaded rectangle */
   1294  1.34  drochner 	return (0);
   1295  1.34  drochner }
   1296  1.34  drochner 
   1297  1.34  drochner #endif /* WSCONS_SUPPORT_ISO7FONTS */
   1298  1.34  drochner 
   1299  1.45  junyoung static int _vga_mapchar(void *, const struct egavga_font *, int, u_int *);
   1300  1.12  drochner 
   1301  1.12  drochner static int
   1302  1.45  junyoung _vga_mapchar(void *id, const struct egavga_font *font, int uni, u_int *index)
   1303  1.12  drochner {
   1304  1.12  drochner 
   1305  1.37  drochner 	switch (font->wsfont->encoding) {
   1306  1.12  drochner 	case WSDISPLAY_FONTENC_ISO:
   1307  1.13  drochner 		if (uni < 256) {
   1308  1.13  drochner 			*index = uni;
   1309  1.13  drochner 			return (5);
   1310  1.13  drochner 		} else {
   1311  1.13  drochner 			*index = ' ';
   1312  1.13  drochner 			return (0);
   1313  1.13  drochner 		}
   1314  1.12  drochner 		break;
   1315  1.12  drochner 	case WSDISPLAY_FONTENC_IBM:
   1316  1.13  drochner 		return (pcdisplay_mapchar(id, uni, index));
   1317  1.12  drochner #ifdef WSCONS_SUPPORT_PCVTFONTS
   1318  1.12  drochner 	case WSDISPLAY_FONTENC_PCVT:
   1319  1.13  drochner 		return (vga_pcvt_mapchar(uni, index));
   1320  1.34  drochner #endif
   1321  1.34  drochner #ifdef WSCONS_SUPPORT_ISO7FONTS
   1322  1.34  drochner 	case WSDISPLAY_FONTENC_ISO7:
   1323  1.34  drochner 		return (vga_iso7_mapchar(uni, index));
   1324  1.12  drochner #endif
   1325  1.13  drochner 	default:
   1326  1.16  drochner #ifdef VGAFONTDEBUG
   1327  1.37  drochner 		printf("_vga_mapchar: encoding=%d\n", font->wsfont->encoding);
   1328  1.16  drochner #endif
   1329  1.13  drochner 		*index = ' ';
   1330  1.13  drochner 		return (0);
   1331  1.12  drochner 	}
   1332  1.12  drochner }
   1333  1.12  drochner 
   1334  1.13  drochner static int
   1335  1.45  junyoung vga_mapchar(void *id, int uni, u_int *index)
   1336  1.12  drochner {
   1337  1.12  drochner 	struct vgascreen *scr = id;
   1338  1.45  junyoung 	u_int idx1, idx2;
   1339  1.13  drochner 	int res1, res2;
   1340  1.12  drochner 
   1341  1.13  drochner 	res1 = 0;
   1342  1.13  drochner 	idx1 = ' '; /* space */
   1343  1.13  drochner 	if (scr->fontset1)
   1344  1.13  drochner 		res1 = _vga_mapchar(id, scr->fontset1, uni, &idx1);
   1345  1.13  drochner 	res2 = -1;
   1346  1.12  drochner 	if (scr->fontset2) {
   1347  1.12  drochner 		KASSERT(VGA_SCREEN_CANTWOFONTS(scr->pcs.type));
   1348  1.13  drochner 		res2 = _vga_mapchar(id, scr->fontset2, uni, &idx2);
   1349  1.12  drochner 	}
   1350  1.13  drochner 	if (res2 > res1) {
   1351  1.14  drochner 		*index = idx2 | 0x0800; /* attribute bit 3 */
   1352  1.14  drochner 		return (res2);
   1353  1.13  drochner 	}
   1354  1.13  drochner 	*index = idx1;
   1355  1.13  drochner 	return (res1);
   1356  1.53  christos }
   1357  1.53  christos 
   1358  1.57  christos #ifdef WSDISPLAY_CHARFUNCS
   1359  1.53  christos int
   1360  1.53  christos vga_getwschar(void *cookie, struct wsdisplay_char *wschar)
   1361  1.53  christos {
   1362  1.53  christos 	struct vgascreen *scr = cookie;
   1363  1.53  christos 
   1364  1.53  christos 	if (scr == NULL) return 0;
   1365  1.53  christos 	return (pcdisplay_getwschar(&scr->pcs, wschar));
   1366  1.53  christos }
   1367  1.53  christos 
   1368  1.53  christos int
   1369  1.53  christos vga_putwschar(void *cookie, struct wsdisplay_char *wschar)
   1370  1.53  christos {
   1371  1.53  christos 	struct vgascreen *scr = cookie;
   1372  1.53  christos 
   1373  1.53  christos 	if (scr == NULL) return 0;
   1374  1.53  christos 	return (pcdisplay_putwschar(&scr->pcs, wschar));
   1375   1.1  drochner }
   1376  1.57  christos #endif /* WSDISPLAY_CHARFUNCS */
   1377