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