xref: /netbsd-src/sys/dev/ic/vga_raster.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: vga_raster.c,v 1.22 2006/04/26 16:09:22 rpaulo Exp $	*/
2 
3 /*
4  * Copyright (c) 2001, 2002 Bang Jun-Young
5  * Copyright (c) 2004 Julio M. Merino Vidal
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
33  * All rights reserved.
34  *
35  * Author: Chris G. Demetriou
36  *
37  * Permission to use, copy, modify and distribute this software and
38  * its documentation is hereby granted, provided that both the copyright
39  * notice and this permission notice appear in all copies of the
40  * software, derivative works or modified versions, and any portions
41  * thereof, and that both notices appear in supporting documentation.
42  *
43  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
44  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
45  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46  *
47  * Carnegie Mellon requests users of this software to return to
48  *
49  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
50  *  School of Computer Science
51  *  Carnegie Mellon University
52  *  Pittsburgh PA 15213-3890
53  *
54  * any improvements or extensions that they make and grant Carnegie the
55  * rights to redistribute these changes.
56  */
57 
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: vga_raster.c,v 1.22 2006/04/26 16:09:22 rpaulo Exp $");
60 
61 #include "opt_wsmsgattrs.h" /* for WSDISPLAY_CUSTOM_OUTPUT */
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/callout.h>
66 #include <sys/kernel.h>
67 #include <sys/device.h>
68 #include <sys/malloc.h>
69 #include <sys/queue.h>
70 #include <machine/bus.h>
71 
72 #include <dev/ic/mc6845reg.h>
73 #include <dev/ic/pcdisplayvar.h>
74 #include <dev/ic/vgareg.h>
75 #include <dev/ic/vgavar.h>
76 #include <dev/ic/videomode.h>
77 
78 #include <dev/wscons/wsdisplayvar.h>
79 #include <dev/wscons/wsconsio.h>
80 #include <dev/wsfont/wsfont.h>
81 
82 #include <dev/ic/pcdisplay.h>
83 
84 int vga_no_builtinfont = 0;
85 
86 u_int8_t builtinfont_data[256 * 16];
87 
88 struct wsdisplay_font builtinfont = {
89 	"builtin",			/* typeface name */
90 	0,				/* firstchar */
91 	256,				/* numchars */
92 	WSDISPLAY_FONTENC_IBM,		/* encoding */
93 	8,				/* width */
94 	16,				/* height */
95 	1,				/* stride */
96 	WSDISPLAY_FONTORDER_L2R,	/* bit order */
97 	WSDISPLAY_FONTORDER_L2R,	/* byte order */
98 	builtinfont_data		/* data */
99 };
100 
101 struct vga_scrmem {
102 	u_int16_t ch;
103 	u_int8_t attr;
104 	u_int8_t second;	/* XXXBJY should be u_int8_t len; */
105 	u_int8_t enc;
106 };
107 
108 #ifdef VGA_CONSOLE_SCREENTYPE
109 #define VGA_SCRMEM_SIZE		(80 * 30)
110 #else
111 #define VGA_SCRMEM_SIZE		(80 * 25)
112 #endif
113 
114 struct vga_scrmem boot_scrmem[VGA_SCRMEM_SIZE];
115 
116 struct vga_raster_font {
117 	LIST_ENTRY(vga_raster_font) next;
118 	struct wsdisplay_font *font;
119 };
120 
121 struct vgascreen {
122 	LIST_ENTRY(vgascreen) next;
123 	struct vga_config *cfg;
124 	struct vga_handle *hdl;
125 	const struct wsscreen_descr *type;
126 
127 	int active;
128 	struct vga_scrmem *mem;
129 	int encoding;
130 
131 	int dispoffset;
132 	int mindispoffset;
133 	int maxdispoffset;
134 
135 	int cursoron;			/* Is cursor displayed? */
136 	int cursorcol;			/* Current cursor column */
137 	int cursorrow;			/* Current cursor row */
138 	struct vga_scrmem cursortmp;
139 	int cursorstride;
140 
141 	LIST_HEAD(, vga_raster_font) fontset;
142 };
143 
144 struct vga_moderegs {
145 	u_int8_t miscout;		/* Misc. output */
146 	u_int8_t crtc[MC6845_NREGS];	/* CRTC controller */
147 	u_int8_t atc[VGA_ATC_NREGS];	/* Attribute controller */
148 	u_int8_t ts[VGA_TS_NREGS];	/* Time sequencer */
149 	u_int8_t gdc[VGA_GDC_NREGS];	/* Graphics display controller */
150 };
151 
152 static int vgaconsole, vga_console_type, vga_console_attached;
153 static struct vgascreen vga_console_screen;
154 static struct vga_config vga_console_vc;
155 static struct vga_raster_font vga_console_fontset_ascii;
156 static struct videomode vga_console_modes[2] = {
157 	/* 640x400 for 80x25, 80x40 and 80x50 modes */
158 	{
159 		25175, 640, 664, 760, 800, 400, 409, 411, 450, 0
160 	},
161 	/* 640x480 for 80x30 mode */
162 	{
163 		25175, 640, 664, 760, 800, 480, 491, 493, 525, 0
164 	}
165 };
166 
167 static void vga_raster_init(struct vga_config *, bus_space_tag_t,
168 		bus_space_tag_t);
169 static void vga_raster_init_screen(struct vga_config *, struct vgascreen *,
170 		const struct wsscreen_descr *, int, long *);
171 static void vga_raster_setup_font(struct vga_config *, struct vgascreen *);
172 static void vga_setup_regs(struct videomode *, struct vga_moderegs *);
173 static void vga_set_mode(struct vga_handle *, struct vga_moderegs *);
174 static void vga_restore_screen(struct vgascreen *,
175 		const struct wsscreen_descr *, struct vga_scrmem *);
176 static void vga_raster_cursor_init(struct vgascreen *, int);
177 static void _vga_raster_putchar(void *, int, int, u_int, long,
178 		struct vga_raster_font *);
179 
180 static void vga_raster_cursor(void *, int, int, int);
181 static int  vga_raster_mapchar(void *, int, u_int *);
182 static void vga_raster_putchar(void *, int, int, u_int, long);
183 static void vga_raster_copycols(void *, int, int, int, int);
184 static void vga_raster_erasecols(void *, int, int, int, long);
185 static void vga_raster_copyrows(void *, int, int, int);
186 static void vga_raster_eraserows(void *, int, int, long);
187 static int  vga_raster_allocattr(void *, int, int, int, long *);
188 #ifdef WSDISPLAY_CUSTOM_OUTPUT
189 static void vga_raster_replaceattr(void *, long, long);
190 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
191 
192 const struct wsdisplay_emulops vga_raster_emulops = {
193 	vga_raster_cursor,
194 	vga_raster_mapchar,
195 	vga_raster_putchar,
196 	vga_raster_copycols,
197 	vga_raster_erasecols,
198 	vga_raster_copyrows,
199 	vga_raster_eraserows,
200 	vga_raster_allocattr,
201 #ifdef WSDISPLAY_CUSTOM_OUTPUT
202 	vga_raster_replaceattr,
203 #else /* WSDISPLAY_CUSTOM_OUTPUT */
204 	NULL,
205 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
206 };
207 
208 /*
209  * translate WS(=ANSI) color codes to standard pc ones
210  */
211 static const unsigned char fgansitopc[] = {
212 #ifdef __alpha__
213 	/*
214 	 * XXX DEC HAS SWITCHED THE CODES FOR BLUE AND RED!!!
215 	 * XXX We should probably not bother with this
216 	 * XXX (reinitialize the palette registers).
217 	 */
218 	FG_BLACK, FG_BLUE, FG_GREEN, FG_CYAN, FG_RED,
219 	FG_MAGENTA, FG_BROWN, FG_LIGHTGREY
220 #else
221 	FG_BLACK, FG_RED, FG_GREEN, FG_BROWN, FG_BLUE,
222 	FG_MAGENTA, FG_CYAN, FG_LIGHTGREY
223 #endif
224 }, bgansitopc[] = {
225 #ifdef __alpha__
226 	BG_BLACK, BG_BLUE, BG_GREEN, BG_CYAN, BG_RED,
227 	BG_MAGENTA, BG_BROWN, BG_LIGHTGREY
228 #else
229 	BG_BLACK, BG_RED, BG_GREEN, BG_BROWN, BG_BLUE,
230 	BG_MAGENTA, BG_CYAN, BG_LIGHTGREY
231 #endif
232 };
233 
234 const struct wsscreen_descr vga_25lscreen = {
235 	"80x25", 80, 25,
236 	&vga_raster_emulops,
237 	8, 16,
238 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
239 	&vga_console_modes[0]
240 }, vga_25lscreen_mono = {
241 	"80x25", 80, 25,
242 	&vga_raster_emulops,
243 	8, 16,
244 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
245 	&vga_console_modes[0]
246 }, vga_30lscreen = {
247 	"80x30", 80, 30,
248 	&vga_raster_emulops,
249 	8, 16,
250 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
251 	&vga_console_modes[1]
252 }, vga_30lscreen_mono = {
253 	"80x30", 80, 30,
254 	&vga_raster_emulops,
255 	8, 16,
256 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
257 	&vga_console_modes[1]
258 }, vga_40lscreen = {
259 	"80x40", 80, 40,
260 	&vga_raster_emulops,
261 	8, 10,
262 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
263 	&vga_console_modes[0]
264 }, vga_40lscreen_mono = {
265 	"80x40", 80, 40,
266 	&vga_raster_emulops,
267 	8, 10,
268 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
269 	&vga_console_modes[0]
270 }, vga_50lscreen = {
271 	"80x50", 80, 50,
272 	&vga_raster_emulops,
273 	8, 8,
274 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_BLINK,
275 	&vga_console_modes[0]
276 }, vga_50lscreen_mono = {
277 	"80x50", 80, 50,
278 	&vga_raster_emulops,
279 	8, 8,
280 	WSSCREEN_HILIT | WSSCREEN_UNDERLINE | WSSCREEN_BLINK | WSSCREEN_REVERSE,
281 	&vga_console_modes[0]
282 };
283 
284 const struct wsscreen_descr *_vga_scrlist[] = {
285 	&vga_25lscreen,
286 	&vga_30lscreen,
287 	&vga_40lscreen,
288 	&vga_50lscreen,
289 }, *_vga_scrlist_mono[] = {
290 	&vga_25lscreen_mono,
291 	&vga_30lscreen_mono,
292 	&vga_40lscreen_mono,
293 	&vga_50lscreen_mono,
294 };
295 
296 const struct wsscreen_list vga_screenlist = {
297 	sizeof(_vga_scrlist) / sizeof(struct wsscreen_descr *),
298 	_vga_scrlist
299 }, vga_screenlist_mono = {
300 	sizeof(_vga_scrlist_mono) / sizeof(struct wsscreen_descr *),
301 	_vga_scrlist_mono
302 };
303 
304 static int	vga_raster_ioctl(void *, void *, u_long, caddr_t, int,
305 		    struct lwp *);
306 static paddr_t	vga_raster_mmap(void *, void *, off_t, int);
307 static int	vga_raster_alloc_screen(void *, const struct wsscreen_descr *,
308 		    void **, int *, int *, long *);
309 static void	vga_raster_free_screen(void *, void *);
310 static int	vga_raster_show_screen(void *, void *, int,
311 		    void (*)(void *, int, int), void *);
312 static int	vga_raster_load_font(void *, void *, struct wsdisplay_font *);
313 
314 static void 	vga_switch_screen(struct vga_config *);
315 static void 	vga_raster_setscreentype(struct vga_config *,
316 		    const struct wsscreen_descr *);
317 
318 const struct wsdisplay_accessops vga_raster_accessops = {
319 	vga_raster_ioctl,
320 	vga_raster_mmap,
321 	vga_raster_alloc_screen,
322 	vga_raster_free_screen,
323 	vga_raster_show_screen,
324 	vga_raster_load_font,
325 };
326 
327 int
328 vga_cnattach(bus_space_tag_t iot, bus_space_tag_t memt, int type, int check)
329 {
330 	long defattr;
331 	const struct wsscreen_descr *scr;
332 #ifdef VGA_CONSOLE_SCREENTYPE
333 	const char *typestr = NULL;
334 #endif
335 
336 	if (check && !vga_common_probe(iot, memt))
337 		return (ENXIO);
338 
339 	/* set up bus-independent VGA configuration */
340 	vga_raster_init(&vga_console_vc, iot, memt);
341 #ifdef VGA_CONSOLE_SCREENTYPE
342 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
343 	    &vga_screenlist_mono : &vga_screenlist, VGA_CONSOLE_SCREENTYPE);
344 	if (!scr)
345 		/* Invalid screen type, continue with the default mode. */
346 		typestr = "80x25";
347 	else if (scr->nrows > 30)
348 		/* Unsupported screen type, try 80x30. */
349 		typestr = "80x30";
350 	scr = wsdisplay_screentype_pick(vga_console_vc.hdl.vh_mono ?
351 	    &vga_screenlist_mono : &vga_screenlist, typestr);
352 	if (scr != vga_console_vc.currenttype)
353 		vga_console_vc.currenttype = scr;
354 #else
355 	scr = vga_console_vc.currenttype;
356 #endif
357 	vga_raster_init_screen(&vga_console_vc, &vga_console_screen, scr, 1,
358 	    &defattr);
359 
360 	vga_console_screen.active = 1;
361 	vga_console_vc.active = &vga_console_screen;
362 
363 	wsdisplay_cnattach(scr, &vga_console_screen,
364 	    vga_console_screen.cursorcol, vga_console_screen.cursorrow,
365 	    defattr);
366 
367 	vgaconsole = 1;
368 	vga_console_type = type;
369 	return (0);
370 }
371 
372 void
373 vga_raster_init(struct vga_config *vc, bus_space_tag_t iot,
374     bus_space_tag_t memt)
375 {
376 	struct vga_handle *vh = &vc->hdl;
377 	u_int8_t mor;
378 	struct vga_raster_font *vf;
379 
380 	vh->vh_iot = iot;
381 	vh->vh_memt = memt;
382 
383 	if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
384 		panic("vga_raster_init: couldn't map vga io");
385 
386 	/* read "misc output register" */
387 	mor = bus_space_read_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAR);
388 	vh->vh_mono = !(mor & 1);
389 
390 	if (bus_space_map(vh->vh_iot, (vh->vh_mono ? 0x3b0 : 0x3d0), 0x10, 0,
391 	    &vh->vh_ioh_6845))
392 		panic("vga_raster_init: couldn't map 6845 io");
393 
394 	if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
395 		panic("vga_raster_init: couldn't map memory");
396 
397 	if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh, 0, 0x10000,
398 	    &vh->vh_memh))
399 		panic("vga_raster_init: mem subrange failed");
400 
401 	/* should only reserve the space (no need to map - save KVM) */
402 	vc->vc_biostag = memt;
403 	if (bus_space_map(vc->vc_biostag, 0xc0000, 0x8000, 0, &vc->vc_bioshdl))
404 		vc->vc_biosmapped = 0;
405 	else
406 		vc->vc_biosmapped = 1;
407 
408 	vc->nscreens = 0;
409 	LIST_INIT(&vc->screens);
410 	vc->active = NULL;
411 	vc->currenttype = vh->vh_mono ? &vga_25lscreen_mono : &vga_25lscreen;
412 	callout_init(&vc->vc_switch_callout);
413 
414 	wsfont_init();
415 	vc->nfonts = 1;
416 	LIST_INIT(&vc->vc_fontlist);
417 	vf = &vga_console_fontset_ascii;
418 	if (vga_no_builtinfont) {
419 		struct wsdisplay_font *wf;
420 		int cookie;
421 
422 		/* prefer 8x16 pixel font */
423 		cookie = wsfont_find(NULL, 8, 16, 0, WSDISPLAY_FONTORDER_L2R,
424 		    0);
425 		if (cookie == -1)
426 			cookie = wsfont_find(NULL, 0, 0, 0,
427 			    WSDISPLAY_FONTORDER_L2R, WSDISPLAY_FONTORDER_L2R);
428 		if (cookie == -1 || wsfont_lock(cookie, &wf))
429 			panic("vga_raster_init: can't load console font");
430 		vf->font = wf;
431 	} else {
432 		vga_load_builtinfont(vh, builtinfont_data, 0, 256);
433 		vf->font = &builtinfont;
434 	}
435 	LIST_INSERT_HEAD(&vc->vc_fontlist, vf, next);
436 }
437 
438 void
439 vga_raster_init_screen(struct vga_config *vc, struct vgascreen *scr,
440     const struct wsscreen_descr *type, int existing, long *attrp)
441 {
442 	int cpos;
443 	int res;
444 	struct vga_handle *vh;
445 
446 	scr->cfg = vc;
447 	scr->hdl = &vc->hdl;
448 	scr->type = type;
449 	scr->mindispoffset = 0;
450 	scr->maxdispoffset = 0x10000;
451 	vh = &vc->hdl;
452 
453 	LIST_INIT(&scr->fontset);
454 	vga_raster_setup_font(vc, scr);
455 
456 	if (existing) {
457 		int i;
458 
459 		cpos = vga_6845_read(vh, cursorh) << 8;
460 		cpos |= vga_6845_read(vh, cursorl);
461 
462 		/* make sure we have a valid cursor position */
463 		if (cpos < 0 || cpos >= type->nrows * type->ncols)
464 			cpos = 0;
465 
466 		scr->dispoffset = vga_6845_read(vh, startadrh) << 9;
467 		scr->dispoffset |= vga_6845_read(vh, startadrl) << 1;
468 
469 		/* make sure we have a valid memory offset */
470 		if (scr->dispoffset < scr->mindispoffset ||
471 		    scr->dispoffset > scr->maxdispoffset)
472 			scr->dispoffset = scr->mindispoffset;
473 
474 		scr->mem = boot_scrmem;
475 		scr->active = 1;
476 
477 		/* Save the current screen to memory. XXXBJY assume 80x25 */
478 		for (i = 0; i < 80 * 25; i++) {
479 			scr->mem[i].ch = bus_space_read_1(vh->vh_memt,
480 			    vh->vh_allmemh, 0x18000 + i * 2);
481 			scr->mem[i].attr = bus_space_read_1(vh->vh_memt,
482 			    vh->vh_allmemh, 0x18000 + i * 2 + 1);
483 			scr->mem[i].enc = scr->encoding;
484 		}
485 
486 		vga_raster_setscreentype(vc, type);
487 
488 		/* Clear the entire screen. */
489 		vga_gdc_write(vh, mode, 0x02);
490 		bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0,
491 		    0x4000);
492 
493 		vga_restore_screen(scr, type, scr->mem);
494 	} else {
495 		cpos = 0;
496 		scr->dispoffset = scr->mindispoffset;
497 		scr->mem = NULL;
498 		scr->active = 0;
499 	}
500 
501 	scr->cursorrow = cpos / type->ncols;
502 	scr->cursorcol = cpos % type->ncols;
503 	vga_raster_cursor_init(scr, existing);
504 
505 #ifdef __alpha__
506 	if (!vc->hdl.vh_mono)
507 		/*
508 		 * DEC firmware uses a blue background.
509 		 */
510 		res = vga_raster_allocattr(scr, WSCOL_WHITE, WSCOL_BLUE,
511 		    WSATTR_WSCOLORS, attrp);
512 	else
513 #endif
514 	res = vga_raster_allocattr(scr, 0, 0, 0, attrp);
515 #ifdef DIAGNOSTIC
516 	if (res)
517 		panic("vga_raster_init_screen: attribute botch");
518 #endif
519 
520 	vc->nscreens++;
521 	LIST_INSERT_HEAD(&vc->screens, scr, next);
522 }
523 
524 void
525 vga_common_attach(struct vga_softc *sc, bus_space_tag_t iot,
526     bus_space_tag_t memt, int type, int quirks, const struct vga_funcs *vf)
527 {
528 	int console;
529 	struct vga_config *vc;
530 	struct wsemuldisplaydev_attach_args aa;
531 
532 	console = vga_is_console(iot, type);
533 
534 	if (console) {
535 		vc = &vga_console_vc;
536 		vga_console_attached = 1;
537 	} else {
538 		vc = malloc(sizeof(struct vga_config), M_DEVBUF, M_WAITOK);
539 		vga_raster_init(vc, iot, memt);
540 	}
541 
542 	vc->vc_type = type;
543 	vc->vc_funcs = vf;
544 
545 	sc->sc_vc = vc;
546 	vc->softc = sc;
547 
548 	aa.console = console;
549 	aa.scrdata = (vc->hdl.vh_mono ? &vga_screenlist_mono : &vga_screenlist);
550 	aa.accessops = &vga_raster_accessops;
551 	aa.accesscookie = vc;
552 
553 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
554 }
555 
556 int
557 vga_is_console(bus_space_tag_t iot, int type)
558 {
559 	if (vgaconsole &&
560 	    !vga_console_attached &&
561 	    iot == vga_console_vc.hdl.vh_iot &&
562 	    (vga_console_type == -1 || (type == vga_console_type)))
563 		return (1);
564 	return (0);
565 }
566 
567 static int
568 vga_get_video(struct vga_config *vc)
569 {
570 
571 	return (vga_ts_read(&vc->hdl, mode) & VGA_TS_MODE_BLANK) == 0;
572 }
573 
574 static void
575 vga_set_video(struct vga_config *vc, int state)
576 {
577 	int val;
578 
579 	vga_ts_write(&vc->hdl, syncreset, 0x01);
580 	if (state) {					/* unblank screen */
581 		val = vga_ts_read(&vc->hdl, mode);
582 		vga_ts_write(&vc->hdl, mode, val & ~VGA_TS_MODE_BLANK);
583 #ifndef VGA_NO_VBLANK
584 		val = vga_6845_read(&vc->hdl, mode);
585 		vga_6845_write(&vc->hdl, mode, val | 0x80);
586 #endif
587 	} else {					/* blank screen */
588 		val = vga_ts_read(&vc->hdl, mode);
589 		vga_ts_write(&vc->hdl, mode, val | VGA_TS_MODE_BLANK);
590 #ifndef VGA_NO_VBLANK
591 		val = vga_6845_read(&vc->hdl, mode);
592 		vga_6845_write(&vc->hdl, mode, val & ~0x80);
593 #endif
594 	}
595 	vga_ts_write(&vc->hdl, syncreset, 0x03);
596 }
597 
598 int
599 vga_raster_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
600 	struct lwp *l)
601 {
602 	struct vga_config *vc = v;
603 	const struct vga_funcs *vf = vc->vc_funcs;
604 
605 	switch (cmd) {
606 	case WSDISPLAYIO_GTYPE:
607 		*(int *)data = vc->vc_type;
608 		return 0;
609 
610 	case WSDISPLAYIO_GINFO:
611 		/* XXX should get detailed hardware information here */
612 		return EPASSTHROUGH;
613 
614 	case WSDISPLAYIO_GVIDEO:
615 #if 1
616 		*(int *)data = (vga_get_video(vc) ?
617 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF);
618 		return 0;
619 #endif
620 
621 	case WSDISPLAYIO_SVIDEO:
622 #if 1
623 		vga_set_video(vc, *(int *)data == WSDISPLAYIO_VIDEO_ON);
624 		return 0;
625 #endif
626 
627 	case WSDISPLAYIO_GETCMAP:
628 	case WSDISPLAYIO_PUTCMAP:
629 	case WSDISPLAYIO_GCURPOS:
630 	case WSDISPLAYIO_SCURPOS:
631 	case WSDISPLAYIO_GCURMAX:
632 	case WSDISPLAYIO_GCURSOR:
633 	case WSDISPLAYIO_SCURSOR:
634 		/* NONE of these operations are by the generic VGA driver. */
635 		return EPASSTHROUGH;
636 	}
637 
638 	if (vc->vc_funcs == NULL)
639 		return (EPASSTHROUGH);
640 
641 	if (vf->vf_ioctl == NULL)
642 		return (EPASSTHROUGH);
643 
644 	return ((*vf->vf_ioctl)(v, cmd, data, flag, l));
645 }
646 
647 static paddr_t
648 vga_raster_mmap(void *v, void *vs, off_t offset, int prot)
649 {
650 	struct vga_config *vc = v;
651 	const struct vga_funcs *vf = vc->vc_funcs;
652 
653 	if (vc->vc_funcs == NULL)
654 		return (-1);
655 
656 	if (vf->vf_mmap == NULL)
657 		return (-1);
658 
659 	return ((*vf->vf_mmap)(v, offset, prot));
660 }
661 
662 int
663 vga_raster_alloc_screen(void *v, const struct wsscreen_descr *type,
664     void **cookiep, int *curxp, int *curyp, long *defattrp)
665 {
666 	struct vga_config *vc = v;
667 	struct vgascreen *scr;
668 
669 	if (vc->nscreens == 1) {
670 		vc->screens.lh_first->mem = boot_scrmem;
671 	}
672 
673 	scr = malloc(sizeof(struct vgascreen), M_DEVBUF, M_WAITOK);
674 	vga_raster_init_screen(vc, scr, type, vc->nscreens == 0, defattrp);
675 
676 	if (vc->nscreens == 1) {
677 		scr->active = 1;
678 		vc->active = scr;
679 		vc->currenttype = type;
680 	} else {
681 		scr->mem = malloc(sizeof(struct vga_scrmem) *
682 		    type->ncols * type->nrows, M_DEVBUF, M_WAITOK);
683 		vga_raster_eraserows(scr, 0, type->nrows, *defattrp);
684 	}
685 
686 	*cookiep = scr;
687 	*curxp = scr->cursorcol;
688 	*curyp = scr->cursorrow;
689 
690 	return (0);
691 }
692 
693 void
694 vga_raster_free_screen(void *v, void *cookie)
695 {
696 	struct vgascreen *vs = cookie;
697 	struct vga_config *vc = vs->cfg;
698 
699 	LIST_REMOVE(vs, next);
700 	vc->nscreens--;
701 	if (vs != &vga_console_screen)
702 		free(vs, M_DEVBUF);
703 	else
704 		panic("vga_raster_free_screen: console");
705 
706 	if (vc->active == vs)
707 		vc->active = 0;
708 }
709 
710 int
711 vga_raster_show_screen(void *v, void *cookie, int waitok,
712     void (*cb)(void *, int, int), void *cbarg)
713 {
714 	struct vgascreen *scr = cookie, *oldscr;
715 	struct vga_config *vc = scr->cfg;
716 
717 	oldscr = vc->active; /* can be NULL! */
718 	if (scr == oldscr) {
719 		return (0);
720 	}
721 
722 	vc->wantedscreen = cookie;
723 	vc->switchcb = cb;
724 	vc->switchcbarg = cbarg;
725 	if (cb) {
726 		callout_reset(&vc->vc_switch_callout, 0,
727 		    (void(*)(void *))vga_switch_screen, vc);
728 		return (EAGAIN);
729 	}
730 
731 	vga_switch_screen(vc);
732 	return (0);
733 }
734 
735 void
736 vga_switch_screen(struct vga_config *vc)
737 {
738 	struct vgascreen *scr, *oldscr;
739 	struct vga_handle *vh = &vc->hdl;
740 	const struct wsscreen_descr *type;
741 
742 	scr = vc->wantedscreen;
743 	if (!scr) {
744 		printf("vga_switch_screen: disappeared\n");
745 		(*vc->switchcb)(vc->switchcbarg, EIO, 0);
746 		return;
747 	}
748 	type = scr->type;
749 	oldscr = vc->active; /* can be NULL! */
750 #ifdef DIAGNOSTIC
751 	if (oldscr) {
752 		if (!oldscr->active)
753 			panic("vga_raster_show_screen: not active");
754 		if (oldscr->type != vc->currenttype)
755 			panic("vga_raster_show_screen: bad type");
756 	}
757 #endif
758 	if (scr == oldscr) {
759 		return;
760 	}
761 #ifdef DIAGNOSTIC
762 	if (scr->active)
763 		panic("vga_raster_show_screen: active");
764 #endif
765 
766 	if (oldscr)
767 		oldscr->active = 0;
768 
769 	if (vc->currenttype != type) {
770 		vga_raster_setscreentype(vc, type);
771 		vc->currenttype = type;
772 	}
773 
774 	scr->dispoffset = scr->mindispoffset;
775 
776 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
777 		vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
778 		vga_6845_write(vh, startadrl, scr->dispoffset);
779 	}
780 
781 	/* Clear the entire screen. */
782 	vga_gdc_write(vh, mode, 0x02);
783 	bus_space_set_region_4(vh->vh_memt, vh->vh_allmemh, 0, 0, 0x2000);
784 
785 	scr->active = 1;
786 	vga_restore_screen(scr, type, scr->mem);
787 
788 	vc->active = scr;
789 
790 	vga_raster_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
791 
792 	vc->wantedscreen = 0;
793 	if (vc->switchcb)
794 		(*vc->switchcb)(vc->switchcbarg, 0, 0);
795 }
796 
797 static int
798 vga_raster_load_font(void *v, void *id, struct wsdisplay_font *data)
799 {
800 	/* XXX */
801 	printf("vga_raster_load_font: called\n");
802 
803 	return (0);
804 }
805 
806 void
807 vga_raster_setup_font(struct vga_config *vc, struct vgascreen *scr)
808 {
809 	struct vga_raster_font *vf;
810 	struct wsdisplay_font *wf;
811 	int cookie;
812 
813 	LIST_FOREACH(vf, &vc->vc_fontlist, next) {
814 		if (wsfont_matches(vf->font, 0, 0, scr->type->fontheight, 0)) {
815 			scr->encoding = vf->font->encoding;
816 			LIST_INSERT_HEAD(&scr->fontset, vf, next);
817 			return;
818 		}
819 	}
820 
821 	cookie = wsfont_find(NULL, 0, scr->type->fontheight, 0,
822 	    WSDISPLAY_FONTORDER_L2R, 0);
823 	if (cookie == -1)
824 		return;
825 
826 	if (wsfont_lock(cookie, &wf))
827 		return;
828 
829 	vf = malloc(sizeof(struct vga_raster_font), M_DEVBUF, M_NOWAIT);
830 	if (!vf) {
831 		wsfont_unlock(cookie);
832 		return;
833 	}
834 
835 	vf->font = wf;
836 	scr->encoding = vf->font->encoding;
837 	LIST_INSERT_HEAD(&scr->fontset, vf, next);
838 }
839 
840 void
841 vga_setup_regs(struct videomode *mode, struct vga_moderegs *regs)
842 {
843 	int i;
844 	int depth = 4;			/* XXXBJY hardcoded for now */
845 	const u_int8_t palette[] = {
846 		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
847 		0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f
848 	};
849 
850 	/*
851 	 * Compute hsync and vsync polarity.
852 	 */
853 	if ((mode->flags & (VID_PHSYNC | VID_NHSYNC))
854 	    && (mode->flags & (VID_PVSYNC | VID_NVSYNC))) {
855 	    	regs->miscout = 0x23;
856 		if (mode->flags & VID_NHSYNC)
857 			regs->miscout |= 0x40;
858 		if (mode->flags & VID_NVSYNC)
859 			regs->miscout |= 0x80;
860 	} else {
861 		if (mode->flags & VID_DBLSCAN)
862 			mode->vdisplay *= 2;
863 		if (mode->vdisplay < 400)
864 			regs->miscout = 0xa3;
865 		else if (mode->vdisplay < 480)
866 			regs->miscout = 0x63;
867 		else if (mode->vdisplay < 768)
868 			regs->miscout = 0xe3;
869 		else
870 			regs->miscout = 0x23;
871 	}
872 
873 	/*
874 	 * Time sequencer
875 	 */
876 	if (depth == 4)
877 		regs->ts[0] = 0x02;
878 	else
879 		regs->ts[0] = 0x00;
880 	if (mode->flags & VID_CLKDIV2)
881 		regs->ts[1] = 0x09;
882 	else
883 		regs->ts[1] = 0x01;
884 	regs->ts[2] = 0x0f;
885 	regs->ts[3] = 0x00;
886 	if (depth < 8)
887 		regs->ts[4] = 0x06;
888 	else
889 		regs->ts[4] = 0x0e;
890 
891 	/*
892 	 * CRTC controller
893 	 */
894 	regs->crtc[0] = (mode->htotal >> 3) - 5;
895 	regs->crtc[1] = (mode->hdisplay >> 3) - 1;
896 	regs->crtc[2] = (mode->hsync_start >> 3) - 1;
897 	regs->crtc[3] = (((mode->hsync_end >> 3) - 1) & 0x1f) | 0x80;
898 	regs->crtc[4] = mode->hsync_start >> 3;
899 	regs->crtc[5] = ((((mode->hsync_end >> 3) - 1) & 0x20) << 2)
900 	    | (((mode->hsync_end >> 3)) & 0x1f);
901 	regs->crtc[6] = (mode->vtotal - 2) & 0xff;
902 	regs->crtc[7] = (((mode->vtotal - 2) & 0x100) >> 8)
903 	    | (((mode->vdisplay - 1) & 0x100) >> 7)
904 	    | ((mode->vsync_start & 0x100) >> 6)
905 	    | (((mode->vsync_start - 1) & 0x100) >> 5)
906 	    | 0x10
907 	    | (((mode->vtotal - 2) & 0x200) >> 4)
908 	    | (((mode->vdisplay - 1) & 0x200) >> 3)
909 	    | ((mode->vsync_start & 0x200) >> 2);
910 	regs->crtc[8] = 0x00;
911 	regs->crtc[9] = (((mode->vsync_start - 1) & 0x200) >> 4) | 0x40;
912 	if (mode->flags & VID_DBLSCAN)
913 		regs->crtc[9] |= 0x80;
914 	regs->crtc[10] = 0x00;
915 	regs->crtc[11] = 0x00;
916 	regs->crtc[12] = 0x00;
917 	regs->crtc[13] = 0x00;
918 	regs->crtc[14] = 0x00;
919 	regs->crtc[15] = 0x00;
920 	regs->crtc[16] = mode->vsync_start & 0xff;
921 	regs->crtc[17] = (mode->vsync_end & 0x0f) | 0x20;
922 	regs->crtc[18] = (mode->vdisplay - 1) & 0xff;
923 	regs->crtc[19] = mode->hdisplay >> 4;	/* XXXBJY */
924 	regs->crtc[20] = 0x00;
925 	regs->crtc[21] = (mode->vsync_start - 1) & 0xff;
926 	regs->crtc[22] = (mode->vsync_end - 1) & 0xff;
927 	if (depth < 8)
928 		regs->crtc[23] = 0xe3;
929 	else
930 		regs->crtc[23] = 0xc3;
931 	regs->crtc[24] = 0xff;
932 
933 	/*
934 	 * Graphics display controller
935 	 */
936 	regs->gdc[0] = 0x00;
937 	regs->gdc[1] = 0x00;
938 	regs->gdc[2] = 0x00;
939 	regs->gdc[3] = 0x00;
940 	regs->gdc[4] = 0x00;
941 	if (depth == 4)
942 		regs->gdc[5] = 0x02;
943 	else
944 		regs->gdc[5] = 0x40;
945 	regs->gdc[6] = 0x01;
946 	regs->gdc[7] = 0x0f;
947 	regs->gdc[8] = 0xff;
948 
949 	/*
950 	 * Attribute controller
951 	 */
952 	/* Set palette registers. */
953 	for (i = 0; i < 16; i++)
954 		regs->atc[i] = palette[i];
955 	if (depth == 4)
956 		regs->atc[16] = 0x01;	/* XXXBJY was 0x81 in XFree86 */
957 	else
958 		regs->atc[16] = 0x41;
959 	regs->atc[17] = 0x00;		/* XXXBJY just a guess */
960 	regs->atc[18] = 0x0f;
961 	regs->atc[19] = 0x00;
962 	regs->atc[20] = 0x00;
963 }
964 
965 void
966 vga_set_mode(struct vga_handle *vh, struct vga_moderegs *regs)
967 {
968 	int i;
969 
970 	/* Disable display. */
971 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) | VGA_TS_MODE_BLANK);
972 
973 	/* Write misc output register. */
974 	bus_space_write_1(vh->vh_iot, vh->vh_ioh_vga, VGA_MISC_DATAW,
975 	    regs->miscout);
976 
977 	/* Set synchronous reset. */
978 	vga_ts_write(vh, syncreset, 0x01);
979 	vga_ts_write(vh, mode, regs->ts[1] | VGA_TS_MODE_BLANK);
980 	for (i = 2; i < VGA_TS_NREGS; i++)
981 		_vga_ts_write(vh, i, regs->ts[i]);
982 	/* Clear synchronous reset. */
983 	vga_ts_write(vh, syncreset, 0x03);
984 
985 	/* Unprotect CRTC registers 0-7. */
986 	vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
987 	/* Write CRTC registers. */
988 	for (i = 0; i < MC6845_NREGS; i++)
989 		_vga_6845_write(vh, i, regs->crtc[i]);
990 
991 	/* Write graphics display registers. */
992 	for (i = 0; i < VGA_GDC_NREGS; i++)
993 		_vga_gdc_write(vh, i, regs->gdc[i]);
994 
995 	/* Write attribute controller registers. */
996 	for (i = 0; i < VGA_ATC_NREGS; i++)
997 		_vga_attr_write(vh, i, regs->atc[i]);
998 
999 	/* Enable display. */
1000 	vga_ts_write(vh, mode, vga_ts_read(vh, mode) & ~VGA_TS_MODE_BLANK);
1001 }
1002 
1003 void
1004 vga_raster_cursor_init(struct vgascreen *scr, int existing)
1005 {
1006 	struct vga_handle *vh = scr->hdl;
1007 	bus_space_tag_t memt;
1008 	bus_space_handle_t memh;
1009 	int off;
1010 
1011 	if (existing) {
1012 		/*
1013 		 * This is the first screen. At this point, scr->active is
1014 		 * false, so we can't use vga_raster_cursor() to do this.
1015 		 */
1016 		memt = vh->vh_memt;
1017 		memh = vh->vh_memh;
1018 		off = (scr->cursorrow * scr->type->ncols + scr->cursorcol) +
1019 		    scr->dispoffset / 8;
1020 
1021 		scr->cursortmp = scr->mem[off];
1022 		vga_raster_putchar(scr, scr->cursorrow, scr->cursorcol,
1023 		    scr->cursortmp.ch, scr->cursortmp.attr ^ 0x77);
1024 	} else {
1025 		scr->cursortmp.ch = 0;
1026 		scr->cursortmp.attr = 0;
1027 		scr->cursortmp.second = 0;
1028 		scr->cursortmp.enc = scr->encoding;
1029 	}
1030 
1031 	scr->cursoron = 1;
1032 }
1033 
1034 void
1035 vga_raster_cursor(void *id, int on, int row, int col)
1036 {
1037 	struct vgascreen *scr = id;
1038 	int off, tmp;
1039 
1040 	/* Remove old cursor image */
1041 	if (scr->cursoron) {
1042 		off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1043 		if (scr->active) {
1044 			tmp = scr->encoding;
1045 			scr->encoding = scr->cursortmp.enc;
1046 			if (scr->cursortmp.second)
1047 				vga_raster_putchar(id, scr->cursorrow,
1048 				    scr->cursorcol - 1, scr->cursortmp.ch,
1049 				    scr->cursortmp.attr);
1050 			else
1051 				vga_raster_putchar(id, scr->cursorrow,
1052 				    scr->cursorcol, scr->cursortmp.ch,
1053 				    scr->cursortmp.attr);
1054 			scr->encoding = tmp;
1055 		}
1056 	}
1057 
1058 	scr->cursorrow = row;
1059 	scr->cursorcol = col;
1060 
1061 	if ((scr->cursoron = on) == 0)
1062 		return;
1063 
1064 	off = scr->cursorrow * scr->type->ncols + scr->cursorcol;
1065 	scr->cursortmp = scr->mem[off];
1066 	if (scr->active) {
1067 		tmp = scr->encoding;
1068 		scr->encoding = scr->cursortmp.enc;
1069 		if (scr->cursortmp.second)
1070 			vga_raster_putchar(id, scr->cursorrow,
1071 			    scr->cursorcol - 1, scr->cursortmp.ch,
1072 			    scr->cursortmp.attr ^ 0x77);
1073 		else
1074 			vga_raster_putchar(id, scr->cursorrow,
1075 			    scr->cursorcol, scr->cursortmp.ch,
1076 			    scr->cursortmp.attr ^ 0x77);
1077 		scr->encoding = tmp;
1078 	}
1079 }
1080 
1081 static int
1082 vga_raster_mapchar(void *id, int uni, u_int *index)
1083 {
1084 	struct vgascreen *scr = id;
1085 
1086 	if (scr->encoding == WSDISPLAY_FONTENC_IBM)
1087 		return pcdisplay_mapchar(id, uni, index);
1088 	else {
1089 		*index = uni;
1090 		return 5;
1091 	}
1092 }
1093 
1094 void
1095 vga_raster_putchar(void *id, int row, int col, u_int c, long attr)
1096 {
1097 	struct vgascreen *scr = id;
1098 	int off;
1099 	struct vga_raster_font *fs;
1100 	u_int tmp_ch;
1101 
1102 	off = row * scr->type->ncols + col;
1103 
1104 	LIST_FOREACH(fs, &scr->fontset, next) {
1105 		if ((scr->encoding == fs->font->encoding) &&
1106 		    (c >= fs->font->firstchar) &&
1107 		    (c < fs->font->firstchar + fs->font->numchars) &&
1108 		    (scr->type->fontheight == fs->font->fontheight)) {
1109 			if (scr->active) {
1110 				tmp_ch = c - fs->font->firstchar;
1111 				_vga_raster_putchar(scr, row, col, tmp_ch,
1112 				    attr, fs);
1113 			}
1114 
1115 			scr->mem[off].ch = c;
1116 			scr->mem[off].attr = attr;
1117 			scr->mem[off].second = 0;
1118 			scr->mem[off].enc = fs->font->encoding;
1119 
1120 			if (fs->font->stride == 2) {
1121 				scr->mem[off + 1].ch = c;
1122 				scr->mem[off + 1].attr = attr;
1123 				scr->mem[off + 1].second = 1;
1124 				scr->mem[off + 1].enc = fs->font->encoding;
1125 			}
1126 
1127 			return;
1128 		}
1129 	}
1130 
1131 	/*
1132 	 * No match found.
1133 	 */
1134 	if (scr->active)
1135 		/*
1136 		 * Put a single width space character no matter what the
1137 		 * actual width of the character is.
1138 		 */
1139 		_vga_raster_putchar(scr, row, col, ' ', attr,
1140 		    &vga_console_fontset_ascii);
1141 	scr->mem[off].ch = c;
1142 	scr->mem[off].attr = attr;
1143 	scr->mem[off].second = 0;
1144 	scr->mem[off].enc = scr->encoding;
1145 }
1146 
1147 static void
1148 _vga_raster_putchar(void *id, int row, int col, u_int c, long attr,
1149     struct vga_raster_font *fs)
1150 {
1151 	struct vgascreen *scr = id;
1152 	struct vga_handle *vh = scr->hdl;
1153 	bus_space_tag_t memt = vh->vh_memt;
1154 	bus_space_handle_t memh = vh->vh_memh;
1155 	int i;
1156 	int rasoff, rasoff2;
1157 	int fheight = scr->type->fontheight;
1158 	volatile u_int8_t dummy, pattern;
1159 	u_int8_t fgcolor, bgcolor;
1160 
1161 	rasoff = scr->dispoffset + row * scr->type->ncols * fheight + col;
1162 	rasoff2 = rasoff;
1163 
1164 #if 0
1165 	bgcolor = bgansitopc[attr >> 4];
1166 	fgcolor = fgansitopc[attr & 0x0f];
1167 #else
1168 	bgcolor = ((attr >> 4) & 0x0f);
1169 	fgcolor = attr & 0x0f;
1170 #endif
1171 
1172 	if (fs->font->stride == 1) {
1173 		/* Paint background. */
1174 		vga_gdc_write(vh, mode, 0x02);
1175 		for (i = 0; i < fheight; i++) {
1176 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1177 			rasoff += scr->type->ncols;
1178 		}
1179 
1180 		/* Draw a single width character. */
1181 		vga_gdc_write(vh, mode, 0x03);
1182 		vga_gdc_write(vh, setres, fgcolor);
1183 		for (i = 0; i < fheight; i++) {
1184 			pattern = ((u_int8_t *)fs->font->data)[c * fheight + i];
1185 			/* When pattern is 0, skip output for speed-up. */
1186 			if (pattern != 0) {
1187 				dummy = bus_space_read_1(memt, memh, rasoff2);
1188 				bus_space_write_1(memt, memh, rasoff2, pattern);
1189 			}
1190 			rasoff2 += scr->type->ncols;
1191 		}
1192 	} else if (fs->font->stride == 2) {
1193 		/* Paint background. */
1194 		vga_gdc_write(vh, mode, 0x02);
1195 		for (i = 0; i < fheight; i++) {
1196 			bus_space_write_1(memt, memh, rasoff, bgcolor);
1197 			bus_space_write_1(memt, memh, rasoff + 1, bgcolor);
1198 			rasoff += scr->type->ncols;
1199 		}
1200 
1201 		/* Draw a double width character. */
1202 		vga_gdc_write(vh, mode, 0x03);
1203 		vga_gdc_write(vh, setres, fgcolor);
1204 		for (i = 0; i < fheight; i++) {
1205 			pattern = ((u_int8_t *)fs->font->data)
1206 			    [(c * fheight + i) * 2];
1207 			if (pattern != 0) {
1208 				dummy = bus_space_read_1(memt, memh, rasoff2);
1209 				bus_space_write_1(memt, memh, rasoff2, pattern);
1210 			}
1211 			pattern = ((u_int8_t *)fs->font->data)
1212 			    [(c * fheight + i) * 2 + 1];
1213 			if (pattern != 0) {
1214 				rasoff2++;
1215 				dummy = bus_space_read_1(memt, memh, rasoff2);
1216 				bus_space_write_1(memt, memh, rasoff2, pattern);
1217 				rasoff2--;
1218 			}
1219 			rasoff2 += scr->type->ncols;
1220 		}
1221 	}
1222 }
1223 
1224 void
1225 vga_raster_copycols(void *id, int row, int srccol, int dstcol, int ncols)
1226 {
1227 	struct vgascreen *scr = id;
1228 	struct vga_handle *vh = scr->hdl;
1229 	bus_space_tag_t memt = vh->vh_memt;
1230 	bus_space_handle_t memh = vh->vh_memh;
1231 	bus_size_t srcoff, dstoff;
1232 	bus_size_t rassrcoff, rasdstoff;
1233 	int i;
1234 	int fheight = scr->type->fontheight;
1235 
1236 	srcoff = row * scr->type->ncols + srccol;
1237 	dstoff = row * scr->type->ncols + dstcol;
1238 	rassrcoff = scr->dispoffset + row * scr->type->ncols * fheight + srccol;
1239 	rasdstoff = scr->dispoffset + row * scr->type->ncols * fheight + dstcol;
1240 
1241 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1242 	    ncols * sizeof(struct vga_scrmem));
1243 
1244 	vga_gdc_write(vh, mode, 0x01);
1245 	if (scr->active) {
1246 		for (i = 0; i < fheight; i++) {
1247 			bus_space_copy_region_1(memt, memh,
1248 			    rassrcoff + i * scr->type->ncols, memh,
1249 			    rasdstoff + i * scr->type->ncols, ncols);
1250 		}
1251 	}
1252 }
1253 
1254 void
1255 vga_raster_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
1256 {
1257 	struct vgascreen *scr = id;
1258 	int i;
1259 
1260 	if (scr->active == 0)
1261 		return;
1262 
1263 	for (i = startcol; i < startcol + ncols; i++)
1264 		vga_raster_putchar(id, row, i, ' ', fillattr);
1265 }
1266 
1267 void
1268 vga_raster_copyrows(void *id, int srcrow, int dstrow, int nrows)
1269 {
1270 	struct vgascreen *scr = id;
1271 	struct vga_handle *vh = scr->hdl;
1272 	bus_space_tag_t memt = vh->vh_memt;
1273 	bus_space_handle_t memh = vh->vh_memh;
1274 	int ncols;
1275 	bus_size_t srcoff, dstoff;
1276 	bus_size_t rassrcoff, rasdstoff;
1277 	int fheight;
1278 
1279 	ncols = scr->type->ncols;
1280 	fheight = scr->type->fontheight;
1281 
1282 	srcoff = srcrow * ncols;
1283 	dstoff = dstrow * ncols;
1284 	rassrcoff = srcoff * fheight;
1285 	rasdstoff = dstoff * fheight;
1286 
1287 	if (scr->active) {
1288 		vga_gdc_write(vh, mode, 0x01);
1289 		if (dstrow == 0 && (srcrow + nrows == scr->type->nrows)) {
1290 			int cursoron = scr->cursoron;
1291 
1292 			if (cursoron)
1293 				/* Disable cursor. */
1294 				vga_raster_cursor(scr, 0,
1295 				    scr->cursorrow, scr->cursorcol);
1296 
1297 			/* scroll up whole screen */
1298 			if ((scr->dispoffset + srcrow * ncols * fheight)
1299 			    <= scr->maxdispoffset)
1300 				scr->dispoffset += srcrow * ncols * fheight;
1301 			else {
1302 				bus_space_copy_region_1(memt, memh,
1303 				    scr->dispoffset + rassrcoff,
1304 				    memh, scr->mindispoffset,
1305 				    nrows * ncols * fheight);
1306 				scr->dispoffset = scr->mindispoffset;
1307 			}
1308 			vga_6845_write(vh, startadrh, scr->dispoffset >> 8);
1309 			vga_6845_write(vh, startadrl, scr->dispoffset);
1310 
1311 			if (cursoron)
1312 				/* Enable cursor. */
1313 				vga_raster_cursor(scr, 1, scr->cursorrow,
1314 				    scr->cursorcol);
1315 		} else
1316 			bus_space_copy_region_1(memt, memh,
1317 			    scr->dispoffset + rassrcoff, memh,
1318 			    scr->dispoffset + rasdstoff,
1319 			    nrows * ncols * fheight);
1320 	}
1321 	memcpy(&scr->mem[dstoff], &scr->mem[srcoff],
1322 	    nrows * ncols * sizeof(struct vga_scrmem));
1323 }
1324 
1325 void
1326 vga_raster_eraserows(void *id, int startrow, int nrows, long fillattr)
1327 {
1328 	struct vgascreen *scr = id;
1329 	struct vga_handle *vh = scr->hdl;
1330 	bus_space_tag_t memt = vh->vh_memt;
1331 	bus_space_handle_t memh = vh->vh_memh;
1332 	bus_size_t off, count;
1333 	bus_size_t rasoff, rascount;
1334 	int i;
1335 
1336 	off = startrow * scr->type->ncols;
1337 	count = nrows * scr->type->ncols;
1338 	rasoff = off * scr->type->fontheight;
1339 	rascount = count * scr->type->fontheight;
1340 
1341 	if (scr->active) {
1342 		u_int8_t bgcolor = (fillattr >> 4) & 0x0F;
1343 
1344 		/* Paint background. */
1345 		vga_gdc_write(vh, mode, 0x02);
1346 		if (scr->type->ncols % 4 == 0) {
1347 			u_int32_t fill = bgcolor | (bgcolor << 8) |
1348 			    (bgcolor << 16) | (bgcolor << 24);
1349 			/* We can speed up I/O */
1350 			for (i = rasoff; i < rasoff + rascount; i += 4)
1351 				bus_space_write_4(memt, memh,
1352 				    scr->dispoffset + i, fill);
1353 		} else {
1354 			u_int16_t fill = bgcolor | (bgcolor << 8);
1355 			for (i = rasoff; i < rasoff + rascount; i += 2)
1356 				bus_space_write_2(memt, memh,
1357 				    scr->dispoffset + i, fill);
1358 		}
1359 	}
1360 	for (i = 0; i < count; i++) {
1361 		scr->mem[off + i].ch = ' ';
1362 		scr->mem[off + i].attr = fillattr;
1363 		scr->mem[off + i].second = 0;
1364 		scr->mem[off + i].enc = scr->encoding;
1365 	}
1366 }
1367 
1368 static int
1369 vga_raster_allocattr(void *id, int fg, int bg, int flags, long *attrp)
1370 {
1371 	struct vgascreen *scr = id;
1372 	struct vga_config *vc = scr->cfg;
1373 
1374 	if (vc->hdl.vh_mono) {
1375 		if (flags & WSATTR_WSCOLORS)
1376 			return (EINVAL);
1377 		if (flags & WSATTR_REVERSE)
1378 			*attrp = 0x70;
1379 		else
1380 			*attrp = 0x07;
1381 		if (flags & WSATTR_UNDERLINE)
1382 			*attrp |= FG_UNDERLINE;
1383 		if (flags & WSATTR_HILIT)
1384 			*attrp |= FG_INTENSE;
1385 	} else {
1386 		if (flags & (WSATTR_UNDERLINE | WSATTR_REVERSE))
1387 			return (EINVAL);
1388 		if (flags & WSATTR_WSCOLORS)
1389 			*attrp = fgansitopc[fg] | bgansitopc[bg];
1390 		else
1391 			*attrp = 7;
1392 		if (flags & WSATTR_HILIT)
1393 			*attrp += 8;
1394 	}
1395 	if (flags & WSATTR_BLINK)
1396 		*attrp |= FG_BLINK;
1397 	return (0);
1398 }
1399 
1400 void
1401 vga_restore_screen(struct vgascreen *scr,
1402     const struct wsscreen_descr *type, struct vga_scrmem *mem)
1403 {
1404 	int i, j, off, tmp;
1405 
1406 	tmp = scr->encoding;
1407 	for (i = 0; i < type->nrows; i++) {
1408 		for (j = 0; j < type->ncols; j++) {
1409 			off = i * type->ncols + j;
1410 			if (mem[off].second != 1) {
1411 				scr->encoding = mem[off].enc;
1412 				vga_raster_putchar(scr, i, j, mem[off].ch,
1413 				    mem[off].attr);
1414 			}
1415 		}
1416 	}
1417 	scr->encoding = tmp;
1418 }
1419 
1420 void
1421 vga_raster_setscreentype(struct vga_config *vc,
1422     const struct wsscreen_descr *type)
1423 {
1424 	struct vga_handle *vh = &vc->hdl;
1425 	struct vga_moderegs moderegs;
1426 
1427 	vga_setup_regs((struct videomode *)type->modecookie, &moderegs);
1428 	vga_set_mode(vh, &moderegs);
1429 }
1430 
1431 #ifdef WSDISPLAY_CUSTOM_OUTPUT
1432 void
1433 vga_raster_replaceattr(void *id, long oldattr, long newattr)
1434 {
1435 	struct vgascreen *scr = id;
1436 	const struct wsscreen_descr *type = scr->type;
1437 	int off;
1438 
1439 	for (off = 0; off < type->nrows * type->ncols; off++) {
1440 		if (scr->mem[off].attr == oldattr)
1441 			scr->mem[off].attr = newattr;
1442 	}
1443 
1444 	/* Repaint the whole screen, if needed */
1445 	if (scr->active)
1446 		vga_restore_screen(scr, type, scr->mem);
1447 }
1448 #endif /* WSDISPLAY_CUSTOM_OUTPUT */
1449