xref: /illumos-gate/usr/src/uts/i86pc/io/gfx_private/gfxp_vgatext.c (revision 856f710c9dc323b39da5935194d7928ffb99b67f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
29 /*		All Rights Reserved	*/
30 
31 #include <sys/errno.h>
32 #include <sys/types.h>
33 #include <sys/conf.h>
34 #include <sys/kmem.h>
35 #include <sys/visual_io.h>
36 #include <sys/font.h>
37 #include <sys/fbio.h>
38 #include <sys/ddi.h>
39 #include <sys/stat.h>
40 #include <sys/sunddi.h>
41 #include <sys/open.h>
42 #include <sys/modctl.h>
43 #include <sys/pci.h>
44 #include <sys/kd.h>
45 #include <sys/ddi_impldefs.h>
46 #include <sys/gfx_private.h>
47 #include <sys/vgareg.h>
48 #include "gfxp_fb.h"
49 
50 #define	MYNAME	"gfxp_vgatext"
51 
52 static ddi_device_acc_attr_t dev_attr = {
53 	DDI_DEVICE_ATTR_V0,
54 	DDI_NEVERSWAP_ACC,
55 	DDI_STRICTORDER_ACC,
56 };
57 
58 /* default structure for FBIOGATTR ioctl */
59 static struct fbgattr vgatext_attr =  {
60 /*	real_type	owner */
61 	FBTYPE_SUNFAST_COLOR, 0,
62 /* fbtype: type		h  w  depth cms  size */
63 	{ FBTYPE_SUNFAST_COLOR, VGA_TEXT_ROWS, VGA_TEXT_COLS, 1,    256,  0 },
64 /* fbsattr: flags emu_type	dev_specific */
65 	{ 0, FBTYPE_SUN4COLOR, { 0 } },
66 /*	emu_types */
67 	{ -1 }
68 };
69 
70 static struct vis_identifier gfxp_vgatext_ident = { "illumos_text" };
71 
72 static int vgatext_devinit(struct gfxp_fb_softc *, struct vis_devinit *data);
73 static void	vgatext_cons_copy(struct gfxp_fb_softc *,
74 			struct vis_conscopy *);
75 static void	vgatext_cons_display(struct gfxp_fb_softc *,
76 			struct vis_consdisplay *);
77 static int	vgatext_cons_clear(struct gfxp_fb_softc *,
78 			struct vis_consclear *);
79 static void	vgatext_cons_cursor(struct gfxp_fb_softc *,
80 			struct vis_conscursor *);
81 static void	vgatext_polled_copy(struct vis_polledio_arg *,
82 			struct vis_conscopy *);
83 static void	vgatext_polled_display(struct vis_polledio_arg *,
84 			struct vis_consdisplay *);
85 static void	vgatext_polled_cursor(struct vis_polledio_arg *,
86 			struct vis_conscursor *);
87 static void	vgatext_init(struct gfxp_fb_softc *);
88 static void	vgatext_set_text(struct gfxp_fb_softc *);
89 
90 static void	vgatext_get_text(struct gfxp_fb_softc *softc);
91 static void	vgatext_save_text(struct gfxp_fb_softc *softc);
92 static void	vgatext_kdsettext(struct gfxp_fb_softc *softc);
93 static int	vgatext_suspend(struct gfxp_fb_softc *softc);
94 static void	vgatext_resume(struct gfxp_fb_softc *softc);
95 static int	vgatext_devmap(dev_t, devmap_cookie_t, offset_t, size_t,
96     size_t *, uint_t, void *);
97 
98 #if	defined(USE_BORDERS)
99 static void	vgatext_init_graphics(struct gfxp_fb_softc *);
100 #endif
101 
102 static int vgatext_kdsetmode(struct gfxp_fb_softc *softc, int mode);
103 static void vgatext_setfont(struct gfxp_fb_softc *softc);
104 static void vgatext_get_cursor(struct gfxp_fb_softc *softc,
105     screen_pos_t *row, screen_pos_t *col);
106 static void vgatext_set_cursor(struct gfxp_fb_softc *softc, int row, int col);
107 static void vgatext_hide_cursor(struct gfxp_fb_softc *softc);
108 static void vgatext_save_colormap(struct gfxp_fb_softc *softc);
109 static void vgatext_restore_colormap(struct gfxp_fb_softc *softc);
110 static int vgatext_get_pci_reg_index(dev_info_t *const devi,
111     unsigned long himask, unsigned long hival, unsigned long addr,
112 		off_t *offset);
113 static int vgatext_get_isa_reg_index(dev_info_t *const devi,
114 		unsigned long hival, unsigned long addr, off_t *offset);
115 
116 static struct gfxp_ops gfxp_vgatext_ops = {
117 	.ident = &gfxp_vgatext_ident,
118 	.kdsetmode = vgatext_kdsetmode,
119 	.devinit = vgatext_devinit,
120 	.cons_copy = vgatext_cons_copy,
121 	.cons_display = vgatext_cons_display,
122 	.cons_cursor = vgatext_cons_cursor,
123 	.cons_clear = vgatext_cons_clear,
124 	.suspend = vgatext_suspend,
125 	.resume = vgatext_resume,
126 	.devmap = vgatext_devmap
127 };
128 
129 #define	STREQ(a, b)	(strcmp((a), (b)) == 0)
130 
131 int
132 gfxp_vga_attach(dev_info_t *devi, struct gfxp_fb_softc *softc)
133 {
134 	int	unit = ddi_get_instance(devi);
135 	int	error;
136 	char	*parent_type = NULL;
137 	int	reg_rnumber;
138 	off_t	reg_offset;
139 	off_t	mem_offset;
140 	char	*cons;
141 	struct gfx_vga *vga;
142 
143 
144 	softc->polledio.display = vgatext_polled_display;
145 	softc->polledio.copy = vgatext_polled_copy;
146 	softc->polledio.cursor = vgatext_polled_cursor;
147 	softc->gfxp_ops = &gfxp_vgatext_ops;
148 	softc->fbgattr = &vgatext_attr;
149 	vga = kmem_zalloc(sizeof (*vga), KM_SLEEP);
150 	softc->console = (union gfx_console *)vga;
151 
152 	error = ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_get_parent(devi),
153 	    DDI_PROP_DONTPASS, "device_type", &parent_type);
154 	if (error != DDI_SUCCESS) {
155 		cmn_err(CE_WARN, MYNAME ": can't determine parent type.");
156 		goto fail;
157 	}
158 
159 	/* Not enable AGP and DRM by default */
160 	if (STREQ(parent_type, "isa") || STREQ(parent_type, "eisa")) {
161 		reg_rnumber = vgatext_get_isa_reg_index(devi, 1, VGA_REG_ADDR,
162 		    &reg_offset);
163 		if (reg_rnumber < 0) {
164 			cmn_err(CE_WARN,
165 			    MYNAME
166 			    ": can't find reg entry for registers");
167 			error = DDI_FAILURE;
168 			goto fail;
169 		}
170 		vga->fb_regno = vgatext_get_isa_reg_index(devi, 0,
171 		    VGA_MEM_ADDR, &mem_offset);
172 		if (vga->fb_regno < 0) {
173 			cmn_err(CE_WARN,
174 			    MYNAME ": can't find reg entry for memory");
175 			error = DDI_FAILURE;
176 			goto fail;
177 		}
178 	} else if (STREQ(parent_type, "pci") || STREQ(parent_type, "pciex")) {
179 		reg_rnumber = vgatext_get_pci_reg_index(devi,
180 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
181 		    PCI_ADDR_IO|PCI_RELOCAT_B, VGA_REG_ADDR,
182 		    &reg_offset);
183 		if (reg_rnumber < 0) {
184 			cmn_err(CE_WARN,
185 			    MYNAME ": can't find reg entry for registers");
186 			error = DDI_FAILURE;
187 			goto fail;
188 		}
189 		vga->fb_regno = vgatext_get_pci_reg_index(devi,
190 		    PCI_REG_ADDR_M|PCI_REG_REL_M,
191 		    PCI_ADDR_MEM32|PCI_RELOCAT_B, VGA_MEM_ADDR,
192 		    &mem_offset);
193 		if (vga->fb_regno < 0) {
194 			cmn_err(CE_WARN,
195 			    MYNAME ": can't find reg entry for memory");
196 			error = DDI_FAILURE;
197 			goto fail;
198 		}
199 	} else {
200 		cmn_err(CE_WARN, MYNAME ": unknown parent type \"%s\".",
201 		    parent_type);
202 		error = DDI_FAILURE;
203 		goto fail;
204 	}
205 	ddi_prop_free(parent_type);
206 	parent_type = NULL;
207 
208 	error = ddi_regs_map_setup(devi, reg_rnumber,
209 	    (caddr_t *)&vga->regs.addr, reg_offset, VGA_REG_SIZE,
210 	    &dev_attr, &vga->regs.handle);
211 	if (error != DDI_SUCCESS)
212 		goto fail;
213 	vga->regs.mapped = B_TRUE;
214 
215 	vga->fb_size = VGA_MEM_SIZE;
216 
217 	error = ddi_regs_map_setup(devi, vga->fb_regno,
218 	    (caddr_t *)&vga->fb.addr, mem_offset, vga->fb_size, &dev_attr,
219 	    &vga->fb.handle);
220 	if (error != DDI_SUCCESS)
221 		goto fail;
222 	vga->fb.mapped = B_TRUE;
223 
224 	if (ddi_get8(vga->regs.handle,
225 	    vga->regs.addr + VGA_MISC_R) & VGA_MISC_IOA_SEL)
226 		vga->text_base = (caddr_t)vga->fb.addr + VGA_COLOR_BASE;
227 	else
228 		vga->text_base = (caddr_t)vga->fb.addr + VGA_MONO_BASE;
229 
230 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(),
231 	    DDI_PROP_DONTPASS, "console", &cons) == DDI_SUCCESS) {
232 		if (strcmp(cons, "graphics") == 0) {
233 			softc->happyface_boot = 1;
234 			softc->silent = 1;
235 			vga->current_base = vga->shadow;
236 		} else {
237 			vga->current_base = vga->text_base;
238 		}
239 		ddi_prop_free(cons);
240 	} else {
241 		vga->current_base = vga->text_base;
242 	}
243 
244 	/* Set cursor info. */
245 	vga->cursor.visible = fb_info.cursor.visible;
246 	vga->cursor.row = fb_info.cursor.pos.y;
247 	vga->cursor.col = fb_info.cursor.pos.x;
248 
249 	error = ddi_prop_create(makedevice(DDI_MAJOR_T_UNKNOWN, unit),
250 	    devi, DDI_PROP_CANSLEEP, DDI_KERNEL_IOCTL, NULL, 0);
251 	if (error != DDI_SUCCESS)
252 		goto fail;
253 
254 	/* only do this if not in graphics mode */
255 	if ((softc->silent == 0) && (GFXP_IS_CONSOLE(softc))) {
256 		vgatext_init(softc);
257 		vgatext_save_colormap(softc);
258 	}
259 
260 	return (DDI_SUCCESS);
261 
262 fail:
263 	kmem_free(vga, sizeof (*vga));
264 	if (parent_type != NULL)
265 		ddi_prop_free(parent_type);
266 	return (error);
267 }
268 
269 int
270 gfxp_vga_detach(dev_info_t *devi __unused, struct gfxp_fb_softc *softc)
271 {
272 	if (softc->console->vga.fb.mapped)
273 		ddi_regs_map_free(&softc->console->vga.fb.handle);
274 	if (softc->console->vga.regs.mapped)
275 		ddi_regs_map_free(&softc->console->vga.regs.handle);
276 	kmem_free(softc->console, sizeof (struct gfx_vga));
277 	return (DDI_SUCCESS);
278 }
279 
280 /*
281  * vgatext_save_text
282  * vgatext_suspend
283  * vgatext_resume
284  *
285  *	Routines to save and restore contents of the VGA text area
286  * Mostly, this is to support Suspend/Resume operation for graphics
287  * device drivers.  Here in the VGAtext common code, we simply squirrel
288  * away the contents of the hardware's text area during Suspend and then
289  * put it back during Resume
290  */
291 static void
292 vgatext_save_text(struct gfxp_fb_softc *softc)
293 {
294 	union gfx_console *console = softc->console;
295 	unsigned i;
296 
297 	for (i = 0; i < sizeof (console->vga.shadow); i++)
298 		console->vga.shadow[i] = console->vga.current_base[i];
299 }
300 
301 static int
302 vgatext_suspend(struct gfxp_fb_softc *softc)
303 {
304 	switch (softc->mode) {
305 	case KD_TEXT:
306 		vgatext_save_text(softc);
307 		break;
308 
309 	case KD_GRAPHICS:
310 		break;
311 
312 	default:
313 		cmn_err(CE_WARN, MYNAME ": unknown mode in vgatext_suspend.");
314 		return (DDI_FAILURE);
315 	}
316 	return (DDI_SUCCESS);
317 }
318 
319 static void
320 vgatext_resume(struct gfxp_fb_softc *softc)
321 {
322 
323 	switch (softc->mode) {
324 	case KD_TEXT:
325 		vgatext_kdsettext(softc);
326 		break;
327 
328 	case KD_GRAPHICS:
329 
330 		/*
331 		 * Upon RESUME, the graphics device will always actually
332 		 * be in TEXT mode even though the Xorg server did not
333 		 * make that mode change itself (the suspend code did).
334 		 * We want first, therefore, to restore textmode
335 		 * operation fully, and then the Xorg server will
336 		 * do the rest to restore the device to its
337 		 * (hi resolution) graphics mode
338 		 */
339 		vgatext_kdsettext(softc);
340 #if	defined(USE_BORDERS)
341 		vgatext_init_graphics(softc);
342 #endif
343 		break;
344 	default:
345 		cmn_err(CE_WARN, MYNAME ": unknown mode in vgatext_resume.");
346 		break;
347 	}
348 }
349 
350 static void
351 vgatext_progressbar_stop(struct gfxp_fb_softc *softc)
352 {
353 	extern void progressbar_stop(void);
354 
355 	if (softc->silent == 1) {
356 		softc->silent = 0;
357 		progressbar_stop();
358 	}
359 }
360 
361 static void
362 vgatext_kdsettext(struct gfxp_fb_softc *softc)
363 {
364 	union gfx_console *console = softc->console;
365 	int i;
366 
367 	vgatext_init(softc);
368 	for (i = 0; i < sizeof (console->vga.shadow); i++) {
369 		console->vga.text_base[i] = console->vga.shadow[i];
370 	}
371 	console->vga.current_base = console->vga.text_base;
372 	if (console->vga.cursor.visible) {
373 		vgatext_set_cursor(softc,
374 		    console->vga.cursor.row,
375 		    console->vga.cursor.col);
376 	}
377 	vgatext_restore_colormap(softc);
378 }
379 
380 static void
381 vgatext_kdsetgraphics(struct gfxp_fb_softc *softc)
382 {
383 	vgatext_progressbar_stop(softc);
384 	vgatext_save_text(softc);
385 	softc->console->vga.current_base = softc->console->vga.shadow;
386 	vgatext_get_text(softc);
387 #if	defined(USE_BORDERS)
388 	vgatext_init_graphics(softc);
389 #endif
390 }
391 
392 static int
393 vgatext_kdsetmode(struct gfxp_fb_softc *softc, int mode)
394 {
395 	switch (mode) {
396 	case KD_TEXT:
397 		if (softc->blt_ops.setmode != NULL)
398 			softc->blt_ops.setmode(KD_TEXT);
399 		vgatext_kdsettext(softc);
400 		break;
401 
402 	case KD_GRAPHICS:
403 		vgatext_kdsetgraphics(softc);
404 		if (softc->blt_ops.setmode != NULL)
405 			softc->blt_ops.setmode(KD_GRAPHICS);
406 		break;
407 
408 	case KD_RESETTEXT:
409 		/*
410 		 * In order to avoid racing with a starting X server,
411 		 * this needs to be a test and set that is performed in
412 		 * a single (softc->lock protected) ioctl into this driver.
413 		 */
414 		if (softc->mode == KD_TEXT && softc->silent == 1) {
415 			vgatext_progressbar_stop(softc);
416 			vgatext_kdsettext(softc);
417 		}
418 		mode = KD_TEXT;
419 		break;
420 
421 	default:
422 		return (EINVAL);
423 	}
424 
425 	softc->mode = mode;
426 	return (0);
427 }
428 
429 /*ARGSUSED*/
430 static int
431 vgatext_devmap(dev_t dev, devmap_cookie_t dhp, offset_t off, size_t len,
432     size_t *maplen, uint_t model, void *ptr)
433 {
434 	struct gfxp_fb_softc *softc = (struct gfxp_fb_softc *)ptr;
435 	union gfx_console *console;
436 	int err;
437 	size_t length;
438 
439 
440 	if (softc == NULL) {
441 		cmn_err(CE_WARN, "vgatext: Can't find softstate");
442 		return (-1);
443 	}
444 	console = softc->console;
445 
446 	if (!(off >= VGA_MEM_ADDR &&
447 	    off < VGA_MEM_ADDR + console->vga.fb_size)) {
448 		cmn_err(CE_WARN, "vgatext: Can't map offset 0x%llx", off);
449 		return (-1);
450 	}
451 
452 	if (off + len > VGA_MEM_ADDR + console->vga.fb_size)
453 		length = VGA_MEM_ADDR + console->vga.fb_size - off;
454 	else
455 		length = len;
456 
457 	if ((err = devmap_devmem_setup(dhp, softc->devi,
458 	    NULL, console->vga.fb_regno, off - VGA_MEM_ADDR,
459 	    length, PROT_ALL, 0, &dev_attr)) < 0) {
460 		return (err);
461 	}
462 
463 
464 	*maplen = length;
465 	return (0);
466 }
467 
468 
469 static int
470 vgatext_devinit(struct gfxp_fb_softc *softc, struct vis_devinit *data)
471 {
472 	/* initialize console instance */
473 	data->version = VIS_CONS_REV;
474 	data->width = VGA_TEXT_COLS;
475 	data->height = VGA_TEXT_ROWS;
476 	data->linebytes = VGA_TEXT_COLS;
477 	data->color_map = NULL;
478 	data->depth = 4;
479 	data->mode = VIS_TEXT;
480 	data->polledio = &softc->polledio;
481 
482 	vgatext_save_text(softc);	/* save current console */
483 	vgatext_hide_cursor(softc);
484 	return (0);
485 }
486 
487 /*
488  * Binary searchable table for Unicode to CP437 conversion.
489  */
490 struct unicp437 {
491 	uint16_t	unicode_base;
492 	uint8_t		cp437_base;
493 	uint8_t		length;
494 };
495 
496 static const struct unicp437 cp437table[] = {
497 	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 }, { 0x00a1, 0xad, 0x00 },
498 	{ 0x00a2, 0x9b, 0x00 }, { 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
499 	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 }, { 0x00ab, 0xae, 0x00 },
500 	{ 0x00ac, 0xaa, 0x00 }, { 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
501 	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 }, { 0x00b6, 0x14, 0x00 },
502 	{ 0x00b7, 0xfa, 0x00 }, { 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
503 	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 }, { 0x00bf, 0xa8, 0x00 },
504 	{ 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
505 	{ 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 }, { 0x00d6, 0x99, 0x00 },
506 	{ 0x00dc, 0x9a, 0x00 }, { 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
507 	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 }, { 0x00e4, 0x84, 0x00 },
508 	{ 0x00e5, 0x86, 0x00 }, { 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
509 	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 }, { 0x00ea, 0x88, 0x01 },
510 	{ 0x00ec, 0x8d, 0x00 }, { 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
511 	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 }, { 0x00f1, 0xa4, 0x00 },
512 	{ 0x00f2, 0x95, 0x00 }, { 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
513 	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 }, { 0x00f8, 0xed, 0x00 },
514 	{ 0x00f9, 0x97, 0x00 }, { 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
515 	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 }, { 0x0192, 0x9f, 0x00 },
516 	{ 0x0393, 0xe2, 0x00 }, { 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
517 	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 }, { 0x03b1, 0xe0, 0x01 },
518 	{ 0x03b4, 0xeb, 0x00 }, { 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
519 	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 }, { 0x03c4, 0xe7, 0x00 },
520 	{ 0x03c6, 0xed, 0x00 }, { 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
521 	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 }, { 0x2019, 0x27, 0x00 },
522 	{ 0x201c, 0x22, 0x00 }, { 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
523 	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 }, { 0x20a7, 0x9e, 0x00 },
524 	{ 0x20ac, 0xee, 0x00 }, { 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
525 	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 }, { 0x2193, 0x19, 0x00 },
526 	{ 0x2194, 0x1d, 0x00 }, { 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
527 	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 }, { 0x2211, 0xe4, 0x00 },
528 	{ 0x2212, 0x2d, 0x00 }, { 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
529 	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 }, { 0x2229, 0xef, 0x00 },
530 	{ 0x2248, 0xf7, 0x00 }, { 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
531 	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 }, { 0x2310, 0xa9, 0x00 },
532 	{ 0x2320, 0xf4, 0x00 }, { 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
533 	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 }, { 0x2510, 0xbf, 0x00 },
534 	{ 0x2514, 0xc0, 0x00 }, { 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
535 	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 }, { 0x2534, 0xc1, 0x00 },
536 	{ 0x253c, 0xc5, 0x00 }, { 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
537 	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 }, { 0x2554, 0xc9, 0x00 },
538 	{ 0x2555, 0xb8, 0x00 }, { 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
539 	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 }, { 0x255a, 0xc8, 0x00 },
540 	{ 0x255b, 0xbe, 0x00 }, { 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
541 	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 }, { 0x2561, 0xb5, 0x00 },
542 	{ 0x2562, 0xb6, 0x00 }, { 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
543 	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 }, { 0x2568, 0xd0, 0x00 },
544 	{ 0x2569, 0xca, 0x00 }, { 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
545 	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 }, { 0x2584, 0xdc, 0x00 },
546 	{ 0x2588, 0xdb, 0x00 }, { 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
547 	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 }, { 0x25ac, 0x16, 0x00 },
548 	{ 0x25b2, 0x1e, 0x00 }, { 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
549 	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 }, { 0x25d8, 0x08, 0x00 },
550 	{ 0x25d9, 0x0a, 0x00 }, { 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
551 	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 }, { 0x2660, 0x06, 0x00 },
552 	{ 0x2663, 0x05, 0x00 }, { 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x00 },
553 	{ 0x266c, 0x0e, 0x00 }
554 };
555 
556 static uint8_t
557 vga_get_cp437(uint32_t c)
558 {
559 	int min, mid, max;
560 
561 	min = 0;
562 	max = (sizeof (cp437table) / sizeof (struct unicp437)) - 1;
563 
564 	if (c < cp437table[0].unicode_base ||
565 	    c > cp437table[max].unicode_base + cp437table[max].length)
566 		return ('?');
567 
568 	while (max >= min) {
569 		mid = (min + max) / 2;
570 		if (c < cp437table[mid].unicode_base)
571 			max = mid - 1;
572 		else if (c > cp437table[mid].unicode_base +
573 		    cp437table[mid].length)
574 			min = mid + 1;
575 		else
576 			return (c - cp437table[mid].unicode_base +
577 			    cp437table[mid].cp437_base);
578 	}
579 
580 	return ('?');
581 }
582 
583 /*
584  * display a string on the screen at (row, col)
585  *	 assume it has been cropped to fit.
586  */
587 static void
588 vgatext_cons_display(struct gfxp_fb_softc *softc, struct vis_consdisplay *da)
589 {
590 	uint32_t *string;
591 	int	i;
592 	unsigned char	attr;
593 	struct cgatext {
594 		unsigned char ch;
595 		unsigned char attr;
596 	};
597 	struct cgatext *addr;
598 
599 	/*
600 	 * Sanity checks.  This is a last-ditch effort to avoid damage
601 	 * from brokenness or maliciousness above.
602 	 */
603 	if (da->row < 0 || da->row >= VGA_TEXT_ROWS ||
604 	    da->col < 0 || da->col >= VGA_TEXT_COLS ||
605 	    da->col + da->width > VGA_TEXT_COLS)
606 		return;
607 
608 	/*
609 	 * To be fully general, we should copyin the data.  This is not
610 	 * really relevant for this text-only driver, but a graphical driver
611 	 * should support these ioctls from userland to enable simple
612 	 * system startup graphics.
613 	 */
614 	attr = (solaris_color_to_pc_color[da->bg_color & 0xf] << 4)
615 	    | solaris_color_to_pc_color[da->fg_color & 0xf];
616 	string = (uint32_t *)da->data;
617 	addr = (struct cgatext *)softc->console->vga.current_base
618 	    +  (da->row * VGA_TEXT_COLS + da->col);
619 	for (i = 0; i < da->width; i++) {
620 		addr[i].ch = vga_get_cp437(string[i]);
621 		addr[i].attr = attr;
622 	}
623 }
624 
625 static void
626 vgatext_polled_display(
627 	struct vis_polledio_arg *arg,
628 	struct vis_consdisplay *da)
629 {
630 	vgatext_cons_display((struct gfxp_fb_softc *)arg, da);
631 }
632 
633 /*
634  * screen-to-screen copy
635  */
636 
637 static void
638 vgatext_cons_copy(struct gfxp_fb_softc *softc, struct vis_conscopy *ma)
639 {
640 	unsigned short	*from;
641 	unsigned short	*to;
642 	int		cnt;
643 	screen_size_t chars_per_row;
644 	unsigned short	*to_row_start;
645 	unsigned short	*from_row_start;
646 	screen_size_t	rows_to_move;
647 	unsigned short	*base;
648 
649 	/*
650 	 * Sanity checks.  Note that this is a last-ditch effort to avoid
651 	 * damage caused by broken-ness or maliciousness above.
652 	 */
653 	if (ma->s_col < 0 || ma->s_col >= VGA_TEXT_COLS ||
654 	    ma->s_row < 0 || ma->s_row >= VGA_TEXT_ROWS ||
655 	    ma->e_col < 0 || ma->e_col >= VGA_TEXT_COLS ||
656 	    ma->e_row < 0 || ma->e_row >= VGA_TEXT_ROWS ||
657 	    ma->t_col < 0 || ma->t_col >= VGA_TEXT_COLS ||
658 	    ma->t_row < 0 || ma->t_row >= VGA_TEXT_ROWS ||
659 	    ma->s_col > ma->e_col ||
660 	    ma->s_row > ma->e_row)
661 		return;
662 
663 	/*
664 	 * Remember we're going to copy shorts because each
665 	 * character/attribute pair is 16 bits.
666 	 */
667 	chars_per_row = ma->e_col - ma->s_col + 1;
668 	rows_to_move = ma->e_row - ma->s_row + 1;
669 
670 	/* More sanity checks. */
671 	if (ma->t_row + rows_to_move > VGA_TEXT_ROWS ||
672 	    ma->t_col + chars_per_row > VGA_TEXT_COLS)
673 		return;
674 
675 	base = (unsigned short *)softc->console->vga.current_base;
676 
677 	to_row_start = base + ((ma->t_row * VGA_TEXT_COLS) + ma->t_col);
678 	from_row_start = base + ((ma->s_row * VGA_TEXT_COLS) + ma->s_col);
679 
680 	if (to_row_start < from_row_start) {
681 		while (rows_to_move-- > 0) {
682 			to = to_row_start;
683 			from = from_row_start;
684 			to_row_start += VGA_TEXT_COLS;
685 			from_row_start += VGA_TEXT_COLS;
686 			for (cnt = chars_per_row; cnt-- > 0; )
687 				*to++ = *from++;
688 		}
689 	} else {
690 		/*
691 		 * Offset to the end of the region and copy backwards.
692 		 */
693 		cnt = rows_to_move * VGA_TEXT_COLS + chars_per_row;
694 		to_row_start += cnt;
695 		from_row_start += cnt;
696 
697 		while (rows_to_move-- > 0) {
698 			to_row_start -= VGA_TEXT_COLS;
699 			from_row_start -= VGA_TEXT_COLS;
700 			to = to_row_start;
701 			from = from_row_start;
702 			for (cnt = chars_per_row; cnt-- > 0; )
703 				*--to = *--from;
704 		}
705 	}
706 }
707 
708 static void
709 vgatext_polled_copy(
710 	struct vis_polledio_arg *arg,
711 	struct vis_conscopy *ca)
712 {
713 	vgatext_cons_copy((struct gfxp_fb_softc *)arg, ca);
714 }
715 
716 /*ARGSUSED*/
717 static int
718 vgatext_cons_clear(struct gfxp_fb_softc *softc, struct vis_consclear *ca)
719 {
720 	uint16_t val, fg, *base;
721 	int i;
722 
723 	if (ca->bg_color == 0)		/* bright white */
724 		fg = 1;			/* black */
725 	else
726 		fg = 8;
727 
728 	val = (solaris_color_to_pc_color[ca->bg_color & 0xf] << 4) |
729 	    solaris_color_to_pc_color[fg];
730 	val = (val << 8) | ' ';
731 
732 	base = (uint16_t *)softc->console->vga.current_base;
733 	for (i = 0; i < VGA_TEXT_ROWS * VGA_TEXT_COLS; i++)
734 		base[i] = val;
735 
736 	return (0);
737 }
738 
739 static void
740 vgatext_cons_cursor(struct gfxp_fb_softc *softc, struct vis_conscursor *ca)
741 {
742 	if (softc->silent)
743 		return;
744 
745 	switch (ca->action) {
746 	case VIS_HIDE_CURSOR:
747 		softc->console->vga.cursor.visible = B_FALSE;
748 		if (softc->console->vga.current_base ==
749 		    softc->console->vga.text_base)
750 			vgatext_hide_cursor(softc);
751 		break;
752 	case VIS_DISPLAY_CURSOR:
753 		/*
754 		 * Sanity check.  This is a last-ditch effort to avoid
755 		 * damage from brokenness or maliciousness above.
756 		 */
757 		if (ca->col < 0 || ca->col >= VGA_TEXT_COLS ||
758 		    ca->row < 0 || ca->row >= VGA_TEXT_ROWS)
759 			return;
760 
761 		softc->console->vga.cursor.visible = B_TRUE;
762 		softc->console->vga.cursor.col = ca->col;
763 		softc->console->vga.cursor.row = ca->row;
764 		if (softc->console->vga.current_base ==
765 		    softc->console->vga.text_base)
766 			vgatext_set_cursor(softc, ca->row, ca->col);
767 		break;
768 	case VIS_GET_CURSOR:
769 		if (softc->console->vga.current_base ==
770 		    softc->console->vga.text_base) {
771 			vgatext_get_cursor(softc, &ca->row, &ca->col);
772 		}
773 		break;
774 	}
775 }
776 
777 static void
778 vgatext_polled_cursor(
779 	struct vis_polledio_arg *arg,
780 	struct vis_conscursor *ca)
781 {
782 	vgatext_cons_cursor((struct gfxp_fb_softc *)arg, ca);
783 }
784 
785 static void
786 vgatext_hide_cursor(struct gfxp_fb_softc *softc)
787 {
788 	union gfx_console *console = softc->console;
789 	uint8_t msl, s;
790 
791 	if (softc->silent)
792 		return;
793 
794 	msl = vga_get_crtc(&console->vga.regs, VGA_CRTC_MAX_S_LN) & 0x1f;
795 	s = vga_get_crtc(&console->vga.regs, VGA_CRTC_CSSL) & 0xc0;
796 	s |= (1 << 5);
797 
798 	/* disable cursor */
799 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CSSL, s);
800 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CESL, msl);
801 }
802 
803 static void
804 vgatext_set_cursor(struct gfxp_fb_softc *softc, int row, int col)
805 {
806 	union gfx_console *console = softc->console;
807 	short	addr;
808 	uint8_t msl, s;
809 
810 	if (softc->silent)
811 		return;
812 
813 	msl = vga_get_crtc(&console->vga.regs, VGA_CRTC_MAX_S_LN) & 0x1f;
814 	s = vga_get_crtc(&console->vga.regs, VGA_CRTC_CSSL) & 0xc0;
815 
816 	addr = row * VGA_TEXT_COLS + col;
817 
818 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CLAH, addr >> 8);
819 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CLAL, addr & 0xff);
820 
821 	/* enable cursor */
822 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CSSL, s);
823 	vga_set_crtc(&console->vga.regs, VGA_CRTC_CESL, msl);
824 }
825 
826 static void
827 vgatext_get_cursor(struct gfxp_fb_softc *softc,
828     screen_pos_t *row, screen_pos_t *col)
829 {
830 	union gfx_console *console = softc->console;
831 	short   addr;
832 
833 	addr = (vga_get_crtc(&console->vga.regs, VGA_CRTC_CLAH) << 8) +
834 	    vga_get_crtc(&console->vga.regs, VGA_CRTC_CLAL);
835 
836 	*row = addr / VGA_TEXT_COLS;
837 	*col = addr % VGA_TEXT_COLS;
838 }
839 
840 static void
841 vgatext_get_text(struct gfxp_fb_softc *softc)
842 {
843 	union gfx_console *console = softc->console;
844 	struct vgareg *vga_reg;
845 	struct vgaregmap *regs;
846 	int i;
847 
848 	regs = &console->vga.regs;
849 	vga_reg = &console->vga.vga_reg;
850 
851 	vga_reg->vga_misc = vga_get_reg(regs, VGA_MISC_R);
852 
853 	/* get crt controller registers */
854 	for (i = 0; i < NUM_CRTC_REG; i++) {
855 		vga_reg->vga_crtc[i] = vga_get_crtc(regs, i);
856 	}
857 
858 	/* get attribute registers */
859 	for (i = 0; i < NUM_ATR_REG; i++) {
860 		vga_reg->vga_atr[i] = vga_get_atr(regs, i);
861 	}
862 
863 	/* get graphics controller registers */
864 	for (i = 0; i < NUM_GRC_REG; i++) {
865 		vga_reg->vga_grc[i] = vga_get_grc(regs, i);
866 	}
867 
868 	/* get sequencer registers */
869 	for (i = 1; i < NUM_SEQ_REG; i++) {
870 		vga_reg->vga_seq[i] = vga_get_seq(regs, i);
871 	}
872 }
873 
874 /*
875  * This code is experimental. It's only enabled if console is
876  * set to graphics, a preliminary implementation of happyface boot.
877  */
878 static void
879 vgatext_set_text(struct gfxp_fb_softc *softc)
880 {
881 	union gfx_console *console = softc->console;
882 	struct vgareg *vga_reg;
883 	struct vgaregmap *regs;
884 	int i;
885 
886 	regs = &console->vga.regs;
887 	vga_reg = &console->vga.vga_reg;
888 
889 	vgatext_get_text(softc);
890 
891 	/*
892 	 * Set output register bits for text mode.
893 	 * Make sure the VGA adapter is not in monochrome emulation mode.
894 	 */
895 	vga_set_reg(regs, VGA_MISC_W, VGA_MISC_HSP | VGA_MISC_PGSL |
896 	    VGA_MISC_VCLK1 | VGA_MISC_ENB_RAM | VGA_MISC_IOA_SEL);
897 
898 	/* set sequencer registers */
899 	vga_set_seq(regs, VGA_SEQ_RST_SYN,
900 	    (vga_get_seq(regs, VGA_SEQ_RST_SYN) &
901 	    ~VGA_SEQ_RST_SYN_NO_SYNC_RESET));
902 	for (i = 1; i < NUM_SEQ_REG; i++) {
903 		vga_set_seq(regs, i, VGA_SEQ_TEXT[i]);
904 	}
905 	vga_set_seq(regs, VGA_SEQ_RST_SYN,
906 	    (vga_get_seq(regs, VGA_SEQ_RST_SYN) |
907 	    VGA_SEQ_RST_SYN_NO_ASYNC_RESET |
908 	    VGA_SEQ_RST_SYN_NO_SYNC_RESET));
909 
910 	/* set crt controller registers */
911 	vga_set_crtc(regs, VGA_CRTC_VRE,
912 	    (vga_reg->vga_crtc[VGA_CRTC_VRE] & ~VGA_CRTC_VRE_LOCK));
913 	for (i = 0; i < NUM_CRTC_REG; i++) {
914 		vga_set_crtc(regs, i, VGA_CRTC_TEXT[i]);
915 	}
916 
917 	/* set graphics controller registers */
918 	for (i = 0; i < NUM_GRC_REG; i++) {
919 		vga_set_grc(regs, i, VGA_GRC_TEXT[i]);
920 	}
921 
922 	/* set attribute registers */
923 	for (i = 0; i < NUM_ATR_REG; i++) {
924 		vga_set_atr(regs, i, VGA_ATR_TEXT[i]);
925 	}
926 
927 	/* set palette */
928 	for (i = 0; i < VGA_TEXT_CMAP_ENTRIES; i++) {
929 		vga_put_cmap(regs, i,
930 		    VGA_TEXT_PALETTES[i][0] << 2,
931 		    VGA_TEXT_PALETTES[i][1] << 2,
932 		    VGA_TEXT_PALETTES[i][2] << 2);
933 	}
934 	for (i = VGA_TEXT_CMAP_ENTRIES; i < VGA8_CMAP_ENTRIES; i++) {
935 		vga_put_cmap(regs, i, 0, 0, 0);
936 	}
937 }
938 
939 static void
940 vgatext_init(struct gfxp_fb_softc *softc)
941 {
942 	union gfx_console *console = softc->console;
943 	unsigned char atr_mode;
944 
945 	atr_mode = vga_get_atr(&console->vga.regs, VGA_ATR_MODE);
946 	if (atr_mode & VGA_ATR_MODE_GRAPH)
947 		vgatext_set_text(softc);
948 	atr_mode = vga_get_atr(&console->vga.regs, VGA_ATR_MODE);
949 	atr_mode &= ~VGA_ATR_MODE_BLINK;
950 	atr_mode &= ~VGA_ATR_MODE_9WIDE;
951 	vga_set_atr(&console->vga.regs, VGA_ATR_MODE, atr_mode);
952 #if	defined(USE_BORDERS)
953 	vga_set_atr(&console->vga.regs, VGA_ATR_BDR_CLR,
954 	    vga_get_atr(&console->vga.regs, pc_brt_white));
955 #else
956 	vga_set_atr(&console->vga.regs, VGA_ATR_BDR_CLR,
957 	    vga_get_atr(&console->vga.regs, pc_black));
958 #endif
959 	vgatext_setfont(softc);	/* need selectable font? */
960 }
961 
962 #if	defined(USE_BORDERS)
963 static void
964 vgatext_init_graphics(struct gfxp_fb_softc *softc)
965 {
966 	vga_set_atr(&softc->console->vga.regs, VGA_ATR_BDR_CLR,
967 	    vga_get_atr(&softc->console->vga.regs, pc_black));
968 }
969 #endif
970 
971 /*
972  * Binary searchable table for CP437 to Unicode conversion.
973  */
974 struct cp437uni {
975 	uint8_t		cp437_base;
976 	uint16_t	unicode_base;
977 	uint8_t		length;
978 };
979 
980 static const struct cp437uni cp437unitable[] = {
981 	{   0, 0x0000, 0 }, {   1, 0x263A, 1 }, {   3, 0x2665, 1 },
982 	{   5, 0x2663, 0 }, {   6, 0x2660, 0 }, {   7, 0x2022, 0 },
983 	{   8, 0x25D8, 0 }, {   9, 0x25CB, 0 }, {  10, 0x25D9, 0 },
984 	{  11, 0x2642, 0 }, {  12, 0x2640, 0 }, {  13, 0x266A, 1 },
985 	{  15, 0x263C, 0 }, {  16, 0x25BA, 0 }, {  17, 0x25C4, 0 },
986 	{  18, 0x2195, 0 }, {  19, 0x203C, 0 }, {  20, 0x00B6, 0 },
987 	{  21, 0x00A7, 0 }, {  22, 0x25AC, 0 }, {  23, 0x21A8, 0 },
988 	{  24, 0x2191, 0 }, {  25, 0x2193, 0 }, {  26, 0x2192, 0 },
989 	{  27, 0x2190, 0 }, {  28, 0x221F, 0 }, {  29, 0x2194, 0 },
990 	{  30, 0x25B2, 0 }, {  31, 0x25BC, 0 }, {  32, 0x0020, 0x5e },
991 	{ 127, 0x2302, 0 }, { 128, 0x00C7, 0 }, { 129, 0x00FC, 0 },
992 	{ 130, 0x00E9, 0 }, { 131, 0x00E2, 0 }, { 132, 0x00E4, 0 },
993 	{ 133, 0x00E0, 0 }, { 134, 0x00E5, 0 }, { 135, 0x00E7, 0 },
994 	{ 136, 0x00EA, 1 }, { 138, 0x00E8, 0 }, { 139, 0x00EF, 0 },
995 	{ 140, 0x00EE, 0 }, { 141, 0x00EC, 0 }, { 142, 0x00C4, 1 },
996 	{ 144, 0x00C9, 0 }, { 145, 0x00E6, 0 }, { 146, 0x00C6, 0 },
997 	{ 147, 0x00F4, 0 }, { 148, 0x00F6, 0 }, { 149, 0x00F2, 0 },
998 	{ 150, 0x00FB, 0 }, { 151, 0x00F9, 0 }, { 152, 0x00FF, 0 },
999 	{ 153, 0x00D6, 0 }, { 154, 0x00DC, 0 }, { 155, 0x00A2, 1 },
1000 	{ 157, 0x00A5, 0 }, { 158, 0x20A7, 0 }, { 159, 0x0192, 0 },
1001 	{ 160, 0x00E1, 0 }, { 161, 0x00ED, 0 }, { 162, 0x00F3, 0 },
1002 	{ 163, 0x00FA, 0 }, { 164, 0x00F1, 0 }, { 165, 0x00D1, 0 },
1003 	{ 166, 0x00AA, 0 }, { 167, 0x00BA, 0 }, { 168, 0x00BF, 0 },
1004 	{ 169, 0x2310, 0 }, { 170, 0x00AC, 0 }, { 171, 0x00BD, 0 },
1005 	{ 172, 0x00BC, 0 }, { 173, 0x00A1, 0 }, { 174, 0x00AB, 0 },
1006 	{ 175, 0x00BB, 0 }, { 176, 0x2591, 2 }, { 179, 0x2502, 0 },
1007 	{ 180, 0x2524, 0 }, { 181, 0x2561, 1 }, { 183, 0x2556, 0 },
1008 	{ 184, 0x2555, 0 }, { 185, 0x2563, 0 }, { 186, 0x2551, 0 },
1009 	{ 187, 0x2557, 0 }, { 188, 0x255D, 0 }, { 189, 0x255C, 0 },
1010 	{ 190, 0x255B, 0 }, { 191, 0x2510, 0 }, { 192, 0x2514, 0 },
1011 	{ 193, 0x2534, 0 }, { 194, 0x252C, 0 }, { 195, 0x251C, 0 },
1012 	{ 196, 0x2500, 0 }, { 197, 0x253C, 0 }, { 198, 0x255E, 1 },
1013 	{ 200, 0x255A, 0 }, { 201, 0x2554, 0 }, { 202, 0x2569, 0 },
1014 	{ 203, 0x2566, 0 }, { 204, 0x2560, 0 }, { 205, 0x2550, 0 },
1015 	{ 206, 0x256C, 0 }, { 207, 0x2567, 1 }, { 209, 0x2564, 1 },
1016 	{ 211, 0x2559, 0 }, { 212, 0x2558, 0 }, { 213, 0x2552, 1 },
1017 	{ 215, 0x256B, 0 }, { 216, 0x256A, 0 }, { 217, 0x2518, 0 },
1018 	{ 218, 0x250C, 0 }, { 219, 0x2588, 0 }, { 220, 0x2584, 0 },
1019 	{ 221, 0x258C, 0 }, { 222, 0x2590, 0 }, { 223, 0x2580, 0 },
1020 	{ 224, 0x03B1, 0 }, { 225, 0x00DF, 0 }, { 226, 0x0393, 0 },
1021 	{ 227, 0x03C0, 0 }, { 228, 0x03A3, 0 }, { 229, 0x03C3, 0 },
1022 	{ 230, 0x00B5, 0 }, { 231, 0x03C4, 0 }, { 232, 0x03A6, 0 },
1023 	{ 233, 0x0398, 0 }, { 234, 0x03A9, 0 }, { 235, 0x03B4, 0 },
1024 	{ 236, 0x221E, 0 }, { 237, 0x03C6, 0 }, { 238, 0x03B5, 0 },
1025 	{ 239, 0x2229, 0 }, { 240, 0x2261, 0 }, { 241, 0x00B1, 0 },
1026 	{ 242, 0x2265, 0 }, { 243, 0x2264, 0 }, { 244, 0x2320, 1 },
1027 	{ 246, 0x00F7, 0 }, { 247, 0x2248, 0 }, { 248, 0x00B0, 0 },
1028 	{ 249, 0x2219, 0 }, { 250, 0x00B7, 0 }, { 251, 0x221A, 0 },
1029 	{ 252, 0x207F, 0 }, { 253, 0x00B2, 0 }, { 254, 0x25A0, 0 },
1030 	{ 255, 0x00A0, 0 }
1031 };
1032 
1033 static uint16_t
1034 vga_cp437_to_uni(uint8_t c)
1035 {
1036 	int min, mid, max;
1037 
1038 	min = 0;
1039 	max = (sizeof (cp437unitable) / sizeof (struct cp437uni)) - 1;
1040 
1041 	while (max >= min) {
1042 		mid = (min + max) / 2;
1043 		if (c < cp437unitable[mid].cp437_base)
1044 			max = mid - 1;
1045 		else if (c > cp437unitable[mid].cp437_base +
1046 		    cp437unitable[mid].length)
1047 			min = mid + 1;
1048 		else
1049 			return (c - cp437unitable[mid].cp437_base +
1050 			    cp437unitable[mid].unicode_base);
1051 	}
1052 
1053 	return ('?');
1054 }
1055 
1056 static void
1057 vgatext_setfont(struct gfxp_fb_softc *softc)
1058 {
1059 	union gfx_console *console = softc->console;
1060 	static uchar_t fsreg[8] = {0x0, 0x30, 0x5, 0x35, 0xa, 0x3a, 0xf, 0x3f};
1061 
1062 	const uchar_t *from;
1063 	uchar_t volatile *to;
1064 	uint16_t c;
1065 	int	i, j, s;
1066 	int	bpc, f_offset;
1067 
1068 	/* Sync-reset the sequencer registers */
1069 	vga_set_seq(&console->vga.regs, 0x00, 0x01);
1070 	/*
1071 	 *  enable write to plane2, since fonts
1072 	 * could only be loaded into plane2
1073 	 */
1074 	vga_set_seq(&console->vga.regs, 0x02, 0x04);
1075 	/*
1076 	 *  sequentially access data in the bit map being
1077 	 * selected by MapMask register (index 0x02)
1078 	 */
1079 	vga_set_seq(&console->vga.regs, 0x04, 0x07);
1080 	/* Sync-reset ended, and allow the sequencer to operate */
1081 	vga_set_seq(&console->vga.regs, 0x00, 0x03);
1082 
1083 	/*
1084 	 *  select plane 2 on Read Mode 0
1085 	 */
1086 	vga_set_grc(&console->vga.regs, 0x04, 0x02);
1087 	/*
1088 	 *  system addresses sequentially access data, follow
1089 	 * Memory Mode register bit 2 in the sequencer
1090 	 */
1091 	vga_set_grc(&console->vga.regs, 0x05, 0x00);
1092 	/*
1093 	 * set range of host memory addresses decoded by VGA
1094 	 * hardware -- A0000h-BFFFFh (128K region)
1095 	 */
1096 	vga_set_grc(&console->vga.regs, 0x06, 0x00);
1097 
1098 	/*
1099 	 * This assumes 8x16 characters, which yield the traditional 80x25
1100 	 * screen.  It really should support other character heights.
1101 	 */
1102 	bpc = 16;
1103 	s = console->vga.vga_fontslot;
1104 	f_offset = s * 8 * 1024;
1105 	for (i = 0; i < 256; i++) {
1106 		c = vga_cp437_to_uni(i);
1107 		from = font_lookup(font_data_8x16.font, c);
1108 		to = (unsigned char *)console->vga.fb.addr + f_offset +
1109 		    i * 0x20;
1110 		for (j = 0; j < bpc; j++)
1111 			*to++ = *from++;
1112 	}
1113 
1114 	/* Sync-reset the sequencer registers */
1115 	vga_set_seq(&console->vga.regs, 0x00, 0x01);
1116 	/* enable write to plane 0 and 1 */
1117 	vga_set_seq(&console->vga.regs, 0x02, 0x03);
1118 	/*
1119 	 * enable character map selection
1120 	 * and odd/even addressing
1121 	 */
1122 	vga_set_seq(&console->vga.regs, 0x04, 0x03);
1123 	/*
1124 	 * select font map
1125 	 */
1126 	vga_set_seq(&console->vga.regs, 0x03, fsreg[s]);
1127 	/* Sync-reset ended, and allow the sequencer to operate */
1128 	vga_set_seq(&console->vga.regs, 0x00, 0x03);
1129 
1130 	/* restore graphic registers */
1131 
1132 	/* select plane 0 */
1133 	vga_set_grc(&console->vga.regs, 0x04, 0x00);
1134 	/* enable odd/even addressing mode */
1135 	vga_set_grc(&console->vga.regs, 0x05, 0x10);
1136 	/*
1137 	 * range of host memory addresses decoded by VGA
1138 	 * hardware -- B8000h-BFFFFh (32K region)
1139 	 */
1140 	vga_set_grc(&console->vga.regs, 0x06, 0x0e);
1141 	/* enable all color plane */
1142 	vga_set_atr(&console->vga.regs, 0x12, 0x0f);
1143 
1144 }
1145 
1146 static void
1147 vgatext_save_colormap(struct gfxp_fb_softc *softc)
1148 {
1149 	union gfx_console *console = softc->console;
1150 	int i;
1151 
1152 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
1153 		console->vga.attrib_palette[i] =
1154 		    vga_get_atr(&console->vga.regs, i);
1155 	}
1156 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
1157 		vga_get_cmap(&console->vga.regs, i,
1158 		    &console->vga.colormap[i].red,
1159 		    &console->vga.colormap[i].green,
1160 		    &console->vga.colormap[i].blue);
1161 	}
1162 }
1163 
1164 static void
1165 vgatext_restore_colormap(struct gfxp_fb_softc *softc)
1166 {
1167 	union gfx_console *console = softc->console;
1168 	int i;
1169 
1170 	for (i = 0; i < VGA_ATR_NUM_PLT; i++) {
1171 		vga_set_atr(&console->vga.regs, i,
1172 		    console->vga.attrib_palette[i]);
1173 	}
1174 	for (i = 0; i < VGA8_CMAP_ENTRIES; i++) {
1175 		vga_put_cmap(&console->vga.regs, i,
1176 		    console->vga.colormap[i].red,
1177 		    console->vga.colormap[i].green,
1178 		    console->vga.colormap[i].blue);
1179 	}
1180 }
1181 
1182 /*
1183  * search the entries of the "reg" property for one which has the desired
1184  * combination of phys_hi bits and contains the desired address.
1185  *
1186  * This version searches a PCI-style "reg" property.  It was prompted by
1187  * issues surrounding the presence or absence of an entry for the ROM:
1188  * (a) a transition problem with PowerPC Virtual Open Firmware
1189  * (b) uncertainty as to whether an entry will be included on a device
1190  *     with ROM support (and so an "active" ROM base address register),
1191  *     but no ROM actually installed.
1192  *
1193  * See the note below on vgatext_get_isa_reg_index for the reasons for
1194  * returning the offset.
1195  *
1196  * Note that this routine may not be fully general; it is intended for the
1197  * specific purpose of finding a couple of particular VGA reg entries and
1198  * may not be suitable for all reg-searching purposes.
1199  */
1200 static int
1201 vgatext_get_pci_reg_index(
1202 	dev_info_t *const devi,
1203 	unsigned long himask,
1204 	unsigned long hival,
1205 	unsigned long addr,
1206 	off_t *offset)
1207 {
1208 
1209 	int			length, index;
1210 	pci_regspec_t	*reg;
1211 
1212 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1213 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1214 		return (-1);
1215 	}
1216 
1217 	for (index = 0; index < length / sizeof (pci_regspec_t); index++) {
1218 		if ((reg[index].pci_phys_hi & himask) != hival)
1219 			continue;
1220 		if (reg[index].pci_size_hi != 0)
1221 			continue;
1222 		if (reg[index].pci_phys_mid != 0)
1223 			continue;
1224 		if (reg[index].pci_phys_low > addr)
1225 			continue;
1226 		if (reg[index].pci_phys_low + reg[index].pci_size_low <= addr)
1227 			continue;
1228 
1229 		*offset = addr - reg[index].pci_phys_low;
1230 		kmem_free(reg, (size_t)length);
1231 		return (index);
1232 	}
1233 	kmem_free(reg, (size_t)length);
1234 
1235 	return (-1);
1236 }
1237 
1238 /*
1239  * search the entries of the "reg" property for one which has the desired
1240  * combination of phys_hi bits and contains the desired address.
1241  *
1242  * This version searches a ISA-style "reg" property.  It was prompted by
1243  * issues surrounding 8514/A support.  By IEEE 1275 compatibility conventions,
1244  * 8514/A registers should have been added after all standard VGA registers.
1245  * Unfortunately, the Solaris/Intel device configuration framework
1246  * (a) lists the 8514/A registers before the video memory, and then
1247  * (b) also sorts the entries so that I/O entries come before memory
1248  *     entries.
1249  *
1250  * It returns the "reg" index and offset into that register set.
1251  * The offset is needed because there exist (broken?) BIOSes that
1252  * report larger ranges enclosing the standard ranges.  One reports
1253  * 0x3bf for 0x21 instead of 0x3c0 for 0x20, for instance.  Using the
1254  * offset adjusts for this difference in the base of the register set.
1255  *
1256  * Note that this routine may not be fully general; it is intended for the
1257  * specific purpose of finding a couple of particular VGA reg entries and
1258  * may not be suitable for all reg-searching purposes.
1259  */
1260 static int
1261 vgatext_get_isa_reg_index(
1262 	dev_info_t *const devi,
1263 	unsigned long hival,
1264 	unsigned long addr,
1265 	off_t *offset)
1266 {
1267 
1268 	int		length, index;
1269 	struct regspec	*reg;
1270 
1271 	if (ddi_getlongprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
1272 	    "reg", (caddr_t)&reg, &length) != DDI_PROP_SUCCESS) {
1273 		return (-1);
1274 	}
1275 
1276 	for (index = 0; index < length / sizeof (struct regspec); index++) {
1277 		if (reg[index].regspec_bustype != hival)
1278 			continue;
1279 		if (reg[index].regspec_addr > addr)
1280 			continue;
1281 		if (reg[index].regspec_addr + reg[index].regspec_size <= addr)
1282 			continue;
1283 
1284 		*offset = addr - reg[index].regspec_addr;
1285 		kmem_free(reg, (size_t)length);
1286 		return (index);
1287 	}
1288 	kmem_free(reg, (size_t)length);
1289 
1290 	return (-1);
1291 }
1292 
1293 /*
1294  * This vgatext function is used to return the fb, and reg pointers
1295  * and handles for peer graphics drivers.
1296  */
1297 
1298 void
1299 vgatext_return_pointers(struct gfxp_fb_softc *softc, struct vgaregmap *fbs,
1300     struct vgaregmap *regss)
1301 {
1302 
1303 	fbs->addr	= softc->console->vga.fb.addr;
1304 	fbs->handle	= softc->console->vga.fb.handle;
1305 	fbs->mapped	= softc->console->vga.fb.mapped;
1306 	regss->addr	= softc->console->vga.regs.addr;
1307 	regss->handle	= softc->console->vga.regs.handle;
1308 	regss->mapped	= softc->console->vga.regs.mapped;
1309 }
1310