Home | History | Annotate | Line # | Download | only in dev
amidisplaycc.c revision 1.11
      1 /*	$NetBSD: amidisplaycc.c,v 1.11 2003/02/10 20:09:43 jandberg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 Jukka Andberg.
      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 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: amidisplaycc.c,v 1.11 2003/02/10 20:09:43 jandberg Exp $");
     32 
     33 /*
     34  * wscons interface to amiga custom chips. Contains the necessary functions
     35  * to render text on bitmapped screens. Uses the functions defined in
     36  * grfabs_reg.h for display creation/destruction and low level setup.
     37  *
     38  * For each virtual terminal a new screen ('view') is allocated.
     39  * Also one more is allocated for the mapped screen on demand.
     40  */
     41 
     42 #include "amidisplaycc.h"
     43 #include "grfcc.h"
     44 #include "view.h"
     45 #include "opt_amigaccgrf.h"
     46 
     47 #if NAMIDISPLAYCC>0
     48 
     49 #include <sys/param.h>
     50 #include <sys/types.h>
     51 #include <sys/device.h>
     52 #include <sys/malloc.h>
     53 #include <sys/systm.h>
     54 
     55 #include <sys/conf.h>
     56 
     57 #include <amiga/dev/grfabs_reg.h>
     58 #include <amiga/dev/viewioctl.h>
     59 #include <amiga/amiga/device.h>
     60 #include <dev/wscons/wsconsio.h>
     61 #include <dev/rcons/raster.h>
     62 #include <dev/wscons/wscons_raster.h>
     63 #include <dev/wscons/wsdisplayvar.h>
     64 #include <dev/cons.h>
     65 #include <dev/wsfont/wsfont.h>
     66 
     67 #include <machine/stdarg.h>
     68 
     69 #define AMIDISPLAYCC_MAXFONTS 8
     70 
     71 /* These can be lowered if you are sure you dont need that much colors. */
     72 #define MAXDEPTH 8
     73 #define MAXROWS 128
     74 #define MAXCOLUMNS 80
     75 
     76 #define ADJUSTCOLORS
     77 
     78 #define MAXCOLORS (1<<MAXDEPTH)
     79 
     80 struct amidisplaycc_screen;
     81 struct amidisplaycc_softc
     82 {
     83 	struct device dev;
     84 
     85 	/* runtime-loaded fonts */
     86 	struct wsdisplay_font         fonts[AMIDISPLAYCC_MAXFONTS];
     87 
     88 	struct amidisplaycc_screen  * currentscreen;
     89 
     90 	/* display turned on? */
     91 	int       ison;
     92 
     93 	/* stuff relating to the mapped screen */
     94 	view_t  * gfxview;
     95 	int       gfxwidth;
     96 	int       gfxheight;
     97 	int       gfxdepth;
     98 	int       gfxon;
     99 };
    100 
    101 
    102 /*
    103  * Configuration stuff.
    104  */
    105 
    106 static int  amidisplaycc_match(struct device *, struct cfdata *, void *);
    107 static void amidisplaycc_attach(struct device *, struct device *, void *);
    108 
    109 CFATTACH_DECL(amidisplaycc, sizeof(struct amidisplaycc_softc),
    110     amidisplaycc_match, amidisplaycc_attach, NULL, NULL);
    111 
    112 cons_decl(amidisplaycc_);
    113 
    114 /* end of configuration stuff */
    115 
    116 /* private utility functions */
    117 
    118 static int amidisplaycc_setvideo(struct amidisplaycc_softc *, int);
    119 
    120 static int amidisplaycc_setemulcmap(struct amidisplaycc_screen *,
    121 				    struct wsdisplay_cmap *);
    122 
    123 static int amidisplaycc_cmapioctl(view_t *, u_long, struct wsdisplay_cmap *);
    124 static int amidisplaycc_setcmap(view_t *, struct wsdisplay_cmap *);
    125 static int amidisplaycc_getcmap(view_t *, struct wsdisplay_cmap *);
    126 static int amidisplaycc_gfxscreen(struct amidisplaycc_softc *, int);
    127 
    128 static int amidisplaycc_setnamedfont(struct amidisplaycc_screen *, char *);
    129 static void amidisplaycc_setfont(struct amidisplaycc_screen *,
    130 				 struct wsdisplay_font *, int);
    131 static struct wsdisplay_font *amidisplaycc_findfont(struct amidisplaycc_softc *,
    132 						    const char *, int, int);
    133 
    134 static void dprintf(const char *fmt, ...);
    135 
    136 /* end of private utility functions */
    137 
    138 /* emulops for wscons */
    139 void amidisplaycc_cursor(void *, int, int, int);
    140 int amidisplaycc_mapchar(void *, int, unsigned int *);
    141 void amidisplaycc_putchar(void *, int, int, u_int, long);
    142 void amidisplaycc_copycols(void *, int, int, int, int);
    143 void amidisplaycc_erasecols(void *, int, int, int, long);
    144 void amidisplaycc_copyrows(void *, int, int, int);
    145 void amidisplaycc_eraserows(void *, int, int, long);
    146 int  amidisplaycc_allocattr(void *, int, int, int, long *);
    147 /* end of emulops for wscons */
    148 
    149 
    150 /* accessops for wscons */
    151 int amidisplaycc_ioctl(void *, u_long, caddr_t, int, struct proc *);
    152 paddr_t amidisplaycc_mmap(void *, off_t, int);
    153 int amidisplaycc_alloc_screen(void *, const struct wsscreen_descr *, void **,
    154 			      int *, int *, long *);
    155 void amidisplaycc_free_screen( void *, void *);
    156 int amidisplaycc_show_screen(void *, void *, int, void (*)(void *, int, int),
    157 			     void *);
    158 int amidisplaycc_load_font(void *, void *, struct wsdisplay_font *);
    159 void amidisplaycc_pollc(void *, int);
    160 /* end of accessops for wscons */
    161 
    162 /*
    163  * These structures are passed to wscons, and they contain the
    164  * display-specific callback functions.
    165  */
    166 
    167 const struct wsdisplay_accessops amidisplaycc_accessops = {
    168 	amidisplaycc_ioctl,
    169 	amidisplaycc_mmap,
    170 	amidisplaycc_alloc_screen,
    171 	amidisplaycc_free_screen,
    172 	amidisplaycc_show_screen,
    173 	amidisplaycc_load_font,
    174 	amidisplaycc_pollc
    175 };
    176 
    177 const struct wsdisplay_emulops amidisplaycc_emulops = {
    178 	amidisplaycc_cursor,
    179 	amidisplaycc_mapchar,
    180 	amidisplaycc_putchar,
    181 	amidisplaycc_copycols,
    182 	amidisplaycc_erasecols,
    183 	amidisplaycc_copyrows,
    184 	amidisplaycc_eraserows,
    185 	amidisplaycc_allocattr
    186 };
    187 
    188 /* add some of our own data to the wsscreen_descr */
    189 struct amidisplaycc_screen_descr {
    190 	struct wsscreen_descr  wsdescr;
    191 	int                    depth;
    192 	char                   name[16];
    193 };
    194 
    195 /*
    196  * List of supported screenmodes. Almost anything can be given here.
    197  */
    198 
    199 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
    200     /* CONSTCOND */ \
    201     {{ \
    202     name, \
    203     width / fontwidth, \
    204     height / fontheight, \
    205     &amidisplaycc_emulops, fontwidth, fontheight, \
    206     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
    207     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
    208     depth }
    209 
    210 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
    211 	/* name, width, height, depth, fontwidth==8, fontheight */
    212 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
    213 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
    214 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
    215 	ADCC_SCREEN("80x24", 640, 384, 3, 8, 16),
    216 
    217 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
    218 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
    219 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
    220 
    221 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
    222 	ADCC_SCREEN("640x200x1", 640, 200, 2, 8, 8),
    223 	ADCC_SCREEN("640x200x1", 640, 200, 3, 8, 8),
    224 };
    225 
    226 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
    227 const struct wsscreen_descr *amidisplaycc_screens[] = {
    228 	ADCC_SCREENPTR(0),
    229 	ADCC_SCREENPTR(1),
    230 	ADCC_SCREENPTR(2),
    231 	ADCC_SCREENPTR(3),
    232 	ADCC_SCREENPTR(4),
    233 	ADCC_SCREENPTR(5),
    234 	ADCC_SCREENPTR(6),
    235 	ADCC_SCREENPTR(7),
    236 	ADCC_SCREENPTR(8),
    237 	ADCC_SCREENPTR(9),
    238 };
    239 
    240 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
    241 
    242 /*
    243  * This structure also is passed to wscons. It contains pointers
    244  * to the available display modes.
    245  */
    246 
    247 const struct wsscreen_list amidisplaycc_screenlist = {
    248 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
    249 	amidisplaycc_screens
    250 };
    251 
    252 /*
    253  * Our own screen structure. One will be created for each screen.
    254  */
    255 
    256 struct amidisplaycc_screen
    257 {
    258 	struct amidisplaycc_softc *device;
    259 
    260 	int       isconsole;
    261 	int       isvisible;
    262 	view_t  * view;
    263 
    264 	int       ncols;
    265 	int       nrows;
    266 
    267 	int       cursorrow;
    268 	int       cursorcol;
    269 
    270 	/* Active bitplanes for each character row. */
    271 	int       rowmasks[MAXROWS];
    272 
    273 	/* Mapping of colors to screen colors. */
    274 	int       colormap[MAXCOLORS];
    275 
    276 	/* Copies of display parameters for convenience */
    277 	int       width;
    278 	int       height;
    279 	int       depth;
    280 
    281 	int       widthbytes; /* bytes_per_row           */
    282 	int       linebytes;  /* widthbytes + row_mod    */
    283 	int       rowbytes;   /* linebytes * fontheight  */
    284 
    285 	u_char  * planes[MAXDEPTH];
    286 
    287 	u_char  * savedscreen;
    288 
    289 	/*
    290 	 * The font is either one we loaded ourselves, or
    291 	 * one gotten using the wsfont system.
    292 	 *
    293 	 * wsfontcookie differentiates between them:
    294 	 * For fonts loaded by ourselves it is -1.
    295 	 * For wsfonts it contains a cookie for that system.
    296 	 */
    297 	struct wsdisplay_font  * font;
    298 	int                      wsfontcookie;
    299 	int                      fontwidth;
    300 	int                      fontheight;
    301 };
    302 
    303 typedef struct amidisplaycc_screen adccscr_t;
    304 
    305 /*
    306  * Need one statically allocated screen for early init.
    307  * The rest are mallocated when needed.
    308  */
    309 adccscr_t amidisplaycc_consolescreen;
    310 
    311 
    312 /*
    313  * Bring in the one or two builtin fonts.
    314  */
    315 
    316 extern unsigned char kernel_font_8x8[];
    317 extern unsigned char kernel_font_lo_8x8;
    318 extern unsigned char kernel_font_hi_8x8;
    319 
    320 /*
    321  * Default palettes for 2, 4 and 8 color emulation displays.
    322  */
    323 
    324 /* black, grey */
    325 static u_char pal2red[] = { 0x00, 0xaa };
    326 static u_char pal2grn[] = { 0x00, 0xaa };
    327 static u_char pal2blu[] = { 0x00, 0xaa };
    328 
    329 /* black, red, green, grey */
    330 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
    331 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
    332 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
    333 
    334 /* black, red, green, brown, blue, magenta, cyan, grey */
    335 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
    336 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
    337 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
    338 
    339 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
    340 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
    341 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
    342 
    343 #ifdef GRF_AGA
    344 extern int aga_enable;
    345 #else
    346 static int aga_enable = 0;
    347 #endif
    348 
    349 /*
    350  * This gets called at console init to determine the priority of
    351  * this console device.
    352  *
    353  * Of course pointers to this and other functions must present
    354  * in constab[] in conf.c for this to work.
    355  */
    356 void
    357 amidisplaycc_cnprobe(struct consdev *cd)
    358 {
    359 	cd->cn_pri = CN_INTERNAL;
    360 
    361 	/*
    362 	 * Yeah, real nice. But if we win the console then the wscons system
    363 	 * does the proper initialization.
    364 	 */
    365 	cd->cn_dev = NODEV;
    366 }
    367 
    368 /*
    369  * This gets called if this device is used as the console.
    370  */
    371 void
    372 amidisplaycc_cninit(struct consdev  * cd)
    373 {
    374 	void  * cookie;
    375 	long    attr;
    376 	int     x;
    377 	int     y;
    378 
    379 	/* Yeah, we got the console! */
    380 
    381 	/*
    382 	 * This will do the basic stuff we also need.
    383 	 */
    384 	config_console();
    385 
    386 	grfcc_probe();
    387 
    388 #if NVIEW>0
    389 	viewprobe();
    390 #endif
    391 
    392 	/*
    393 	 * Set up wscons to handle the details.
    394 	 * It will then call us back when it needs something
    395 	 * display-specific. It will also set up cn_tab properly,
    396 	 * something which we failed to do at amidisplaycc_cnprobe().
    397 	 */
    398 
    399 	/*
    400 	 * The alloc_screen knows to allocate the first screen statically.
    401 	 */
    402 	amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
    403 				  &cookie, &x, &y, &attr);
    404 	wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
    405 			   cookie, x, y, attr);
    406 }
    407 
    408 static int
    409 amidisplaycc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
    410 {
    411 	char *name = auxp;
    412 
    413 	if (matchname("amidisplaycc", name) == 0)
    414 		return (0);
    415 
    416 	/* Allow only one of us now. Not sure about that. */
    417 	if (cfp->cf_unit != 0)
    418 		return (0);
    419 
    420 	return 1;
    421 }
    422 
    423 /* ARGSUSED */
    424 static void
    425 amidisplaycc_attach(struct device *pdp, struct device *dp, void *auxp)
    426 {
    427 	struct wsemuldisplaydev_attach_args    waa;
    428 	struct amidisplaycc_softc            * adp;
    429 
    430 	adp = (struct amidisplaycc_softc*)dp;
    431 
    432 	grfcc_probe();
    433 
    434 #if NVIEW>0
    435 	viewprobe();
    436 #endif
    437 
    438 	/*
    439 	 * Attach only at real configuration time. Console init is done at
    440 	 * the amidisplaycc_cninit function above.
    441 	 */
    442 	if (adp) {
    443 		printf(": Amiga custom chip graphics %s",
    444 		       aga_enable ? "(AGA)" : "");
    445 
    446 		if (amidisplaycc_consolescreen.isconsole) {
    447 			adp->currentscreen = &amidisplaycc_consolescreen;
    448 			printf(" (console)");
    449 		} else
    450 			adp->currentscreen = NULL;
    451 
    452 		printf("\n");
    453 
    454 		adp->ison = 1;
    455 
    456 		/*
    457 		 * Mapped screen properties.
    458 		 * Would need a way to configure.
    459 		 */
    460 		adp->gfxview = NULL;
    461 		adp->gfxon = 0;
    462 		adp->gfxwidth = 640;
    463 		adp->gfxheight = 480;
    464 
    465 		if (aga_enable)
    466 			adp->gfxdepth = 8;
    467 		else
    468 			adp->gfxdepth = 4;
    469 
    470 		if (NELEMS(amidisplaycc_screentab) !=
    471 		    NELEMS(amidisplaycc_screens))
    472 			panic("invalid screen definitions");
    473 
    474 		waa.scrdata = &amidisplaycc_screenlist;
    475 		waa.console = amidisplaycc_consolescreen.isconsole;
    476 		waa.accessops = &amidisplaycc_accessops;
    477 		waa.accesscookie = dp;
    478 		config_found(dp, &waa, wsemuldisplaydevprint);
    479 
    480 		bzero(adp->fonts, sizeof(adp->fonts));
    481 
    482 		/* Initialize an alternate system for finding fonts. */
    483 		wsfont_init();
    484 	}
    485 }
    486 
    487 
    488 /*
    489  * Color, bgcolor and style are packed into one long attribute.
    490  * These macros are used to create/split the attribute
    491  */
    492 
    493 #define MAKEATTR(fg, bg, mode) (((fg)<<16) | ((bg)<<8) | (mode))
    494 #define ATTRFG(attr) (((attr)>>16) & 255)
    495 #define ATTRBG(attr) (((attr)>>8) & 255)
    496 #define ATTRMO(attr) ((attr) & 255)
    497 
    498 /*
    499  * Called by wscons to draw/clear the cursor.
    500  * We do this by xorring the block to the screen.
    501  *
    502  * This simple implementation will break if the screen is modified
    503  * under the cursor before clearing it.
    504  */
    505 void
    506 amidisplaycc_cursor(void *screen, int on, int row, int col)
    507 {
    508 	adccscr_t  * scr;
    509 	u_char     * dst;
    510 	int  i;
    511 
    512 	scr = screen;
    513 
    514 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
    515 		return;
    516 
    517 	/* was off, turning off again? */
    518 	if (!on && scr->cursorrow == -1 && scr->cursorcol == -1)
    519 		return;
    520 
    521 	/* was on, and turning on again? */
    522 	if (on && scr->cursorrow >= 0 && scr->cursorcol >= 0)
    523 	{
    524 		/* clear from old location first */
    525 		amidisplaycc_cursor (screen, 0, scr->cursorrow, scr->cursorcol);
    526 	}
    527 
    528 	dst = scr->planes[0];
    529 	dst += row * scr->rowbytes;
    530 	dst += col;
    531 
    532 	if (on) {
    533 		scr->cursorrow = row;
    534 		scr->cursorcol = col;
    535 	} else {
    536 		scr->cursorrow = -1;
    537 		scr->cursorcol = -1;
    538 	}
    539 
    540 	for (i = scr->fontheight ; i > 0 ; i--) {
    541 		*dst ^= 255;
    542 		dst += scr->linebytes;
    543 	}
    544 }
    545 
    546 
    547 /*
    548  * This obviously does something important, don't ask me what.
    549  */
    550 int
    551 amidisplaycc_mapchar(void *screen, int ch, unsigned int *chp)
    552 {
    553 	if (ch > 0 && ch < 256) {
    554 		*chp = ch;
    555 		return (5);
    556 	}
    557 	*chp = ' ';
    558 	return (0);
    559 }
    560 
    561 /*
    562  * Write a character to screen with color / bgcolor / hilite(bold) /
    563  * underline / reverse.
    564  * Surely could be made faster but I'm not sure if its worth the
    565  * effort as scrolling is at least a magnitude slower.
    566  */
    567 void
    568 amidisplaycc_putchar(void *screen, int row, int col, u_int ch, long attr)
    569 {
    570 	adccscr_t  * scr;
    571 	u_char     * dst;
    572 	u_char     * font;
    573 
    574 	int         fontheight;
    575 	u_int8_t  * fontreal;
    576 	int         fontlow;
    577 	int         fonthigh;
    578 
    579 	int     bmapoffset;
    580 	int     linebytes;
    581 	int     underline;
    582 	int     fgcolor;
    583 	int     bgcolor;
    584 	int     plane;
    585 	int     depth;
    586 	int     mode;
    587 	int     bold;
    588 	u_char  f;
    589 	int     j;
    590 
    591 	scr = screen;
    592 
    593 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
    594 		return;
    595 
    596 	/* Extract the colors from the attribute */
    597 	fgcolor = ATTRFG(attr);
    598 	bgcolor = ATTRBG(attr);
    599 	mode    = ATTRMO(attr);
    600 
    601 	/* Translate to screen colors */
    602 	fgcolor = scr->colormap[fgcolor];
    603 	bgcolor = scr->colormap[bgcolor];
    604 
    605 	if (mode & WSATTR_REVERSE) {
    606 		j = fgcolor;
    607 		fgcolor = bgcolor;
    608 		bgcolor = j;
    609 	}
    610 
    611 	bold      = (mode & WSATTR_HILIT) > 0;
    612 	underline = (mode & WSATTR_UNDERLINE) > 0;
    613 
    614 	/* If we have loaded a font use it otherwise the builtin font */
    615 	if (scr->font) {
    616 		fontreal = scr->font->data;
    617 		fontlow  = scr->font->firstchar;
    618 		fonthigh = fontlow + scr->font->numchars - 1;
    619 	} else {
    620 		fontreal = kernel_font_8x8;
    621 		fontlow  = kernel_font_lo_8x8;
    622 		fonthigh = kernel_font_hi_8x8;
    623 	}
    624 
    625 	fontheight = scr->fontheight;
    626 	depth      = scr->depth;
    627 	linebytes  = scr->linebytes;
    628 
    629 	if (ch < fontlow || ch > fonthigh)
    630 		ch = fontlow;
    631 
    632 	/* Find the location where the wanted char is in the font data */
    633 	fontreal += scr->fontheight * (ch - fontlow);
    634 
    635 	bmapoffset = row * scr->rowbytes + col;
    636 
    637 	scr->rowmasks[row] |= fgcolor | bgcolor;
    638 
    639 	for (plane = 0 ; plane < depth ; plane++) {
    640 		dst = scr->planes[plane] + bmapoffset;
    641 
    642 		if (fgcolor & 1) {
    643 			if (bgcolor & 1) {
    644 				/* fg=on bg=on (fill) */
    645 
    646 				for (j = 0 ; j < fontheight ; j++) {
    647 					*dst = 255;
    648 					dst += linebytes;
    649 				}
    650 			} else {
    651 				/* fg=on bg=off (normal) */
    652 
    653 				font = fontreal;
    654 				for (j = 0 ; j < fontheight ; j++) {
    655 					f = *(font++);
    656 					f |= f >> bold;
    657 					*dst = f;
    658 					dst += linebytes;
    659 				}
    660 
    661 				if (underline)
    662 					*(dst - linebytes) = 255;
    663 			}
    664 		} else {
    665 			if (bgcolor & 1) {
    666 				/* fg=off bg=on (inverted) */
    667 
    668 				font = fontreal;
    669 				for (j = 0 ; j < fontheight ; j++) {
    670 					f = *(font++);
    671 					f |= f >> bold;
    672 					*dst = ~f;
    673 					dst += linebytes;
    674 				}
    675 
    676 				if (underline)
    677 					*(dst - linebytes) = 0;
    678 			} else {
    679 				/* fg=off bg=off (clear) */
    680 
    681 				for (j = 0 ; j < fontheight ; j++) {
    682 					*dst = 0;
    683 					dst += linebytes;
    684 				}
    685 			}
    686 		}
    687 		fgcolor >>= 1;
    688 		bgcolor >>= 1;
    689 	}
    690 }
    691 
    692 /*
    693  * Copy characters on a row to another position on the same row.
    694  */
    695 
    696 void
    697 amidisplaycc_copycols(void *screen, int row, int srccol, int dstcol, int ncols)
    698 {
    699 	adccscr_t  * scr;
    700 	u_char     * src;
    701 	u_char     * dst;
    702 
    703 	int  bmapoffset;
    704 	int  linebytes;
    705 	int  depth;
    706 	int  plane;
    707 	int  i;
    708 	int  j;
    709 
    710 	scr = screen;
    711 
    712 	if (srccol < 0 || srccol + ncols > scr->ncols ||
    713 	    dstcol < 0 || dstcol + ncols > scr->ncols ||
    714 	    row < 0 || row >= scr->nrows)
    715 		return;
    716 
    717 	depth = scr->depth;
    718 	linebytes = scr->linebytes;
    719 	bmapoffset = row * scr->rowbytes;
    720 
    721 	for (plane = 0 ; plane < depth ; plane++) {
    722 		src = scr->planes[plane] + bmapoffset;
    723 
    724 		for (j = 0 ; j < scr->fontheight ; j++) {
    725 			dst = src;
    726 
    727 			if (srccol < dstcol) {
    728 
    729 				for (i = ncols - 1 ; i >= 0 ; i--)
    730 					dst[dstcol + i] = src[srccol + i];
    731 
    732 			} else {
    733 
    734 				for (i = 0 ; i < ncols ; i++)
    735 					dst[dstcol + i] = src[srccol + i];
    736 
    737 			}
    738 			src += linebytes;
    739 		}
    740 	}
    741 }
    742 
    743 /*
    744  * Erase part of a row.
    745  */
    746 
    747 void
    748 amidisplaycc_erasecols(void *screen, int row, int startcol, int ncols,
    749 		       long attr)
    750 {
    751 	adccscr_t  * scr;
    752 	u_char     * dst;
    753 
    754 	int  bmapoffset;
    755 	int  linebytes;
    756 	int  bgcolor;
    757 	int  depth;
    758 	int  plane;
    759 	int  fill;
    760 	int  j;
    761 
    762 	scr = screen;
    763 
    764 	if (row < 0 || row >= scr->nrows ||
    765 	    startcol < 0 || startcol + ncols > scr->ncols)
    766 		return;
    767 
    768 	depth = scr->depth;
    769 	linebytes = scr->linebytes;
    770 	bmapoffset = row * scr->rowbytes + startcol;
    771 
    772 	/* Erase will be done using the set background color. */
    773 	bgcolor = ATTRBG(attr);
    774 	bgcolor = scr->colormap[bgcolor];
    775 
    776 	for(plane = 0 ; plane < depth ; plane++) {
    777 
    778 		fill = (bgcolor & 1) ? 255 : 0;
    779 
    780 		dst = scr->planes[plane] + bmapoffset;
    781 
    782 		for (j = 0 ; j < scr->fontheight ; j++) {
    783 			memset(dst, fill, ncols);
    784 			dst += linebytes;
    785 		}
    786 	}
    787 }
    788 
    789 /*
    790  * Copy a number of rows to another location on the screen.
    791  * Combined with eraserows it can be used to perform operation
    792  * also known as 'scrolling'.
    793  */
    794 
    795 void
    796 amidisplaycc_copyrows(void *screen, int srcrow, int dstrow, int nrows)
    797 {
    798 	adccscr_t  * scr;
    799 	u_char     * src;
    800 	u_char     * dst;
    801 
    802 	int  srcbmapoffset;
    803 	int  dstbmapoffset;
    804 	int  widthbytes;
    805 	int  fontheight;
    806 	int  linebytes;
    807 	u_int copysize;
    808 	int  rowdelta;
    809 	int  rowbytes;
    810 	int  srcmask;
    811 	int  dstmask;
    812 	int  bmdelta;
    813 	int  depth;
    814 	int  plane;
    815 	int  i;
    816 	int  j;
    817 
    818 	scr = screen;
    819 
    820 	if (srcrow < 0 || srcrow + nrows > scr->nrows ||
    821 	    dstrow < 0 || dstrow + nrows > scr->nrows)
    822 		return;
    823 
    824 	depth = scr->depth;
    825 
    826 	widthbytes = scr->widthbytes;
    827 	rowbytes   = scr->rowbytes;
    828 	linebytes  = scr->linebytes;
    829 	fontheight = scr->fontheight;
    830 
    831 	srcbmapoffset = rowbytes * srcrow;
    832 	dstbmapoffset = rowbytes * dstrow;
    833 
    834 	if (srcrow < dstrow) {
    835 		/* Move data downwards, need to copy from down to up */
    836 		bmdelta = -rowbytes;
    837 		rowdelta = -1;
    838 
    839 		srcbmapoffset += rowbytes * (nrows - 1);
    840 		srcrow += nrows - 1;
    841 
    842 		dstbmapoffset += rowbytes * (nrows - 1);
    843 		dstrow += nrows - 1;
    844 	} else {
    845 		/* Move data upwards, copy up to down */
    846 		bmdelta = rowbytes;
    847 		rowdelta = 1;
    848 	}
    849 
    850 	if (widthbytes == linebytes)
    851 		copysize = rowbytes;
    852 	else
    853 		copysize = 0;
    854 
    855 	for (j = 0 ; j < nrows ; j++) {
    856 		/* Need to copy only planes that have data on src or dst */
    857 		srcmask = scr->rowmasks[srcrow];
    858 		dstmask = scr->rowmasks[dstrow];
    859 		scr->rowmasks[dstrow] = srcmask;
    860 
    861 		for (plane = 0 ; plane < depth ; plane++) {
    862 
    863 			if (srcmask & 1) {
    864 				/*
    865 				 * Source row has data on this
    866 				 * plane, copy it.
    867 				 */
    868 
    869 				src = scr->planes[plane] + srcbmapoffset;
    870 				dst = scr->planes[plane] + dstbmapoffset;
    871 
    872 				if (copysize > 0) {
    873 
    874 					memcpy(dst, src, copysize);
    875 
    876 				} else {
    877 
    878 					/*
    879 					 * Data not continuous,
    880 					 * must do in pieces
    881 					 */
    882 					for (i=0 ; i < fontheight ; i++) {
    883 						memcpy(dst, src, widthbytes);
    884 
    885 						src += linebytes;
    886 						dst += linebytes;
    887 					}
    888 				}
    889 			} else if (dstmask & 1) {
    890 				/*
    891 				 * Source plane is empty, but dest is not.
    892 				 * so all we need to is clear it.
    893 				 */
    894 
    895 				dst = scr->planes[plane] + dstbmapoffset;
    896 
    897 				if (copysize > 0) {
    898 					/* Do it all */
    899 					bzero(dst, copysize);
    900 				} else {
    901 					for (i = 0 ; i < fontheight ; i++) {
    902 						bzero(dst, widthbytes);
    903 						dst += linebytes;
    904 					}
    905 				}
    906 			}
    907 
    908 			srcmask >>= 1;
    909 			dstmask >>= 1;
    910 		}
    911 		srcbmapoffset += bmdelta;
    912 		dstbmapoffset += bmdelta;
    913 
    914 		srcrow += rowdelta;
    915 		dstrow += rowdelta;
    916 	}
    917 }
    918 
    919 /*
    920  * Erase some rows.
    921  */
    922 
    923 void
    924 amidisplaycc_eraserows(void *screen, int row, int nrows, long attr)
    925 {
    926 	adccscr_t  * scr;
    927 	u_char     * dst;
    928 
    929 	int  bmapoffset;
    930 	int  fillsize;
    931 	int  bgcolor;
    932 	int  depth;
    933 	int  plane;
    934 	int  fill;
    935 	int  j;
    936 
    937 	int  widthbytes;
    938 	int  linebytes;
    939 	int  rowbytes;
    940 
    941 
    942 	scr = screen;
    943 
    944 	if (row < 0 || row + nrows > scr->nrows)
    945 		return;
    946 
    947 	depth      = scr->depth;
    948 	widthbytes = scr->widthbytes;
    949 	linebytes  = scr->linebytes;
    950 	rowbytes   = scr->rowbytes;
    951 
    952 	bmapoffset = row * rowbytes;
    953 
    954 	if (widthbytes == linebytes)
    955 		fillsize = rowbytes * nrows;
    956 	else
    957 		fillsize = 0;
    958 
    959 	bgcolor = ATTRBG(attr);
    960 	bgcolor = scr->colormap[bgcolor];
    961 
    962 	for (j = 0 ; j < nrows ; j++)
    963 		scr->rowmasks[row+j] = bgcolor;
    964 
    965 	for (plane = 0 ; plane < depth ; plane++) {
    966 		dst = scr->planes[plane] + bmapoffset;
    967 		fill = (bgcolor & 1) ? 255 : 0;
    968 
    969 		if (fillsize > 0) {
    970 			/* If the rows are continuous, write them all. */
    971 			memset(dst, fill, fillsize);
    972 		} else {
    973 			for (j = 0 ; j < scr->fontheight * nrows ; j++) {
    974 				memset(dst, fill, widthbytes);
    975 				dst += linebytes;
    976 			}
    977 		}
    978 		bgcolor >>= 1;
    979 	}
    980 }
    981 
    982 
    983 /*
    984  * Compose an attribute value from foreground color,
    985  * background color, and flags.
    986  */
    987 int
    988 amidisplaycc_allocattr(void *screen, int fg, int bg, int flags, long *attrp)
    989 {
    990 	adccscr_t  * scr;
    991 	int          maxcolor;
    992 	int          newfg;
    993 	int          newbg;
    994 
    995 	scr = screen;
    996 	maxcolor = (1 << scr->view->bitmap->depth) - 1;
    997 
    998 	/* Ensure the colors are displayable. */
    999 	newfg = fg & maxcolor;
   1000 	newbg = bg & maxcolor;
   1001 
   1002 #ifdef ADJUSTCOLORS
   1003 	/*
   1004 	 * Hack for low-color screens, if background color is nonzero
   1005 	 * but would be displayed as one, adjust it.
   1006 	 */
   1007 	if (bg > 0 && newbg == 0)
   1008 		newbg = maxcolor;
   1009 
   1010 	/*
   1011 	 * If foreground and background colors are different but would
   1012 	 * display the same fix them by modifying the foreground.
   1013 	 */
   1014 	if (fg != bg && newfg == newbg) {
   1015 		if (newbg > 0)
   1016 			newfg = 0;
   1017 		else
   1018 			newfg = maxcolor;
   1019 	}
   1020 #endif
   1021 	*attrp = MAKEATTR(newfg, newbg, flags);
   1022 
   1023 	return (0);
   1024 }
   1025 
   1026 int
   1027 amidisplaycc_ioctl(void *dp, u_long cmd, caddr_t data, int flag, struct proc *p)
   1028 {
   1029 	struct amidisplaycc_softc *adp;
   1030 
   1031 	adp = dp;
   1032 
   1033 	if (adp == NULL) {
   1034 		printf("amidisplaycc_ioctl: adp==NULL\n");
   1035 		return (EINVAL);
   1036 	}
   1037 
   1038 #define UINTDATA (*(u_int*)data)
   1039 #define INTDATA (*(int*)data)
   1040 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
   1041 
   1042 	switch (cmd)
   1043 	{
   1044 	case WSDISPLAYIO_GTYPE:
   1045 		UINTDATA = WSDISPLAY_TYPE_AMIGACC;
   1046 		return (0);
   1047 
   1048 	case WSDISPLAYIO_SVIDEO:
   1049 		dprintf("amidisplaycc: WSDISPLAYIO_SVIDEO %s\n",
   1050 			UINTDATA ? "On" : "Off");
   1051 
   1052 		return (amidisplaycc_setvideo(adp, UINTDATA));
   1053 
   1054 	case WSDISPLAYIO_GVIDEO:
   1055 		dprintf("amidisplaycc: WSDISPLAYIO_GVIDEO\n");
   1056 		UINTDATA = adp->ison ?
   1057 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
   1058 
   1059 		return (0);
   1060 
   1061 	case WSDISPLAYIO_SMODE:
   1062 		if (INTDATA == WSDISPLAYIO_MODE_EMUL)
   1063 			return amidisplaycc_gfxscreen(adp, 0);
   1064 		if (INTDATA == WSDISPLAYIO_MODE_MAPPED)
   1065 			return amidisplaycc_gfxscreen(adp, 1);
   1066 		return (EINVAL);
   1067 
   1068 	case WSDISPLAYIO_GINFO:
   1069 		FBINFO.width  = adp->gfxwidth;
   1070 		FBINFO.height = adp->gfxheight;
   1071 		FBINFO.depth  = adp->gfxdepth;
   1072 		FBINFO.cmsize = 1 << FBINFO.depth;
   1073 		return (0);
   1074 
   1075 	case WSDISPLAYIO_PUTCMAP:
   1076 	case WSDISPLAYIO_GETCMAP:
   1077 		return (amidisplaycc_cmapioctl(adp->gfxview,
   1078 					       cmd,
   1079 					       (struct wsdisplay_cmap*)data));
   1080 	}
   1081 
   1082 	dprintf("amidisplaycc: unknown ioctl %lx (grp:'%c' num:%d)\n",
   1083 		(long)cmd,
   1084 		(char)((cmd&0xff00)>>8),
   1085 		(int)(cmd&0xff));
   1086 
   1087 	return (EPASSTHROUGH);
   1088 
   1089 #undef UINTDATA
   1090 #undef INTDATA
   1091 #undef FBINFO
   1092 }
   1093 
   1094 
   1095 /*
   1096  * Switch to either emulation (text) or mapped (graphics) mode
   1097  * We keep an extra screen for mapped mode so it does not
   1098  * interfere with emulation screens.
   1099  *
   1100  * Once the extra screen is created, it never goes away.
   1101  */
   1102 
   1103 static int
   1104 amidisplaycc_gfxscreen(struct amidisplaycc_softc *adp, int on)
   1105 {
   1106 	dimen_t  dimension;
   1107 
   1108 	dprintf("amidisplaycc: switching to %s mode.\n",
   1109 		on ? "mapped" : "emul");
   1110 
   1111 	/* Current mode same as requested mode? */
   1112 	if ( (on > 0) == (adp->gfxon > 0) )
   1113 		return (0);
   1114 
   1115 	if (!on) {
   1116 		/*
   1117 		 * Switch away from mapped mode. If there is
   1118 		 * a emulation screen, switch to it, otherwise
   1119 		 * just try to hide the mapped screen.
   1120 		 */
   1121 		adp->gfxon = 0;
   1122 		if (adp->currentscreen)
   1123 			grf_display_view(adp->currentscreen->view);
   1124 		else if (adp->gfxview)
   1125 			grf_remove_view(adp->gfxview);
   1126 
   1127 		return (0);
   1128 	}
   1129 
   1130 	/* switch to mapped mode then */
   1131 
   1132 	if (adp->gfxview == NULL) {
   1133 		/* First time here, create the screen */
   1134 
   1135 		dimension.width = adp->gfxwidth;
   1136 		dimension.height = adp->gfxheight;
   1137 
   1138 		dprintf("amidisplaycc: preparing mapped screen %dx%dx%d\n",
   1139 			dimension.width,
   1140 			dimension.height,
   1141 			adp->gfxdepth);
   1142 
   1143 		adp->gfxview = grf_alloc_view(NULL,
   1144 					      &dimension,
   1145 					      adp->gfxdepth);
   1146 	}
   1147 
   1148 	if (adp->gfxview) {
   1149 		adp->gfxon = 1;
   1150 
   1151 		grf_display_view(adp->gfxview);
   1152 	} else {
   1153 		printf("amidisplaycc: failed to make mapped screen\n");
   1154 		return (ENOMEM);
   1155 	}
   1156 	return (0);
   1157 }
   1158 
   1159 /*
   1160  * Map the graphics screen. It must have been created before
   1161  * by switching to mapped mode by using an ioctl.
   1162  */
   1163 paddr_t
   1164 amidisplaycc_mmap(void *dp, off_t off, int prot)
   1165 {
   1166 	struct amidisplaycc_softc  * adp;
   1167 	bmap_t                     * bm;
   1168 	paddr_t                      rv;
   1169 
   1170 	adp = (struct amidisplaycc_softc*)dp;
   1171 
   1172 	/* Check we are in mapped mode */
   1173 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
   1174 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
   1175 		return (paddr_t)(-1);
   1176 	}
   1177 
   1178 	/*
   1179 	 * As we all know by now, we are mapping our special
   1180 	 * screen here so our pretty text consoles are left
   1181 	 * untouched.
   1182 	 */
   1183 
   1184 	bm = adp->gfxview->bitmap;
   1185 
   1186 	/* Check that the offset is valid */
   1187 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
   1188 		dprintf("amidisplaycc_mmap: Offset out of range\n");
   1189 		return (paddr_t)(-1);
   1190 	}
   1191 
   1192 	rv = (paddr_t)bm->hardware_address;
   1193 	rv += off;
   1194 
   1195 	return (rv >> PGSHIFT);
   1196 }
   1197 
   1198 
   1199 /*
   1200  * Create a new screen.
   1201  * NULL dp signifies console and then memory is allocated statically
   1202  * and the screen is automatically displayed.
   1203  *
   1204  * A font with suitable size is searched and if not found
   1205  * the builtin 8x8 font is used.
   1206  *
   1207  * There are separate default palettes for 2, 4 and 8+ color
   1208  * screens.
   1209  */
   1210 
   1211 int
   1212 amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
   1213 			  void  **cookiep, int *curxp, int *curyp,
   1214 			  long *defattrp)
   1215 {
   1216 	const struct amidisplaycc_screen_descr  * adccscreenp;
   1217 	struct amidisplaycc_screen              * scr;
   1218 	struct amidisplaycc_softc               * adp;
   1219 	view_t                                  * view;
   1220 
   1221 	dimen_t  dimension;
   1222 	int      fontheight;
   1223 	int      fontwidth;
   1224 	int      maxcolor;
   1225 	int      depth;
   1226 	int      i;
   1227 	int      j;
   1228 
   1229 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
   1230 	depth = adccscreenp->depth;
   1231 
   1232 	adp = dp;
   1233 
   1234 	maxcolor = (1 << depth) - 1;
   1235 
   1236 	/* Sanity checks because of fixed buffers */
   1237 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
   1238 		return (ENOMEM);
   1239 	if (screenp->nrows > MAXROWS)
   1240 		return (ENOMEM);
   1241 
   1242 	fontwidth = screenp->fontwidth;
   1243 	fontheight = screenp->fontheight;
   1244 
   1245 	if (fontwidth != 8) {
   1246 		dprintf("amidisplaycc_alloc_screen: fontwidth %d invalid.\n",
   1247 		       fontwidth);
   1248 		return (EINVAL);
   1249 	}
   1250 
   1251 	/*
   1252 	 * The screen size is defined in characters.
   1253 	 * Calculate the pixel size using the font size.
   1254 	 */
   1255 
   1256 	dimension.width = screenp->ncols * fontwidth;
   1257 	dimension.height = screenp->nrows * fontheight;
   1258 
   1259 	view = grf_alloc_view(NULL, &dimension, depth);
   1260 	if (view == NULL)
   1261 		return (ENOMEM);
   1262 
   1263 	/*
   1264 	 * First screen gets the statically allocated console screen.
   1265 	 * Others are allocated dynamically.
   1266 	 */
   1267 	if (adp == NULL) {
   1268 		scr = &amidisplaycc_consolescreen;
   1269 		if (scr->isconsole)
   1270 			panic("more than one console?");
   1271 
   1272 		scr->isconsole = 1;
   1273 	} else {
   1274 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK);
   1275 		bzero(scr, sizeof(adccscr_t));
   1276 	}
   1277 
   1278 	scr->view = view;
   1279 
   1280 	scr->ncols = screenp->ncols;
   1281 	scr->nrows = screenp->nrows;
   1282 
   1283 	/* Copies of most used values */
   1284 	scr->width  = dimension.width;
   1285 	scr->height = dimension.height;
   1286 	scr->depth  = depth;
   1287 	scr->widthbytes = view->bitmap->bytes_per_row;
   1288 	scr->linebytes  = scr->widthbytes + view->bitmap->row_mod;
   1289 	scr->rowbytes   = scr->linebytes * fontheight;
   1290 
   1291 	scr->device = adp;
   1292 
   1293 
   1294 	/* --- LOAD FONT --- */
   1295 
   1296 	/* these need to be initialized befory trying to set font */
   1297 	scr->font         = NULL;
   1298 	scr->wsfontcookie = -1;
   1299 	scr->fontwidth    = fontwidth;
   1300 	scr->fontheight   = fontheight;
   1301 
   1302 	/*
   1303 	 * Note that dont try to load font for the console (adp==NULL)
   1304 	 *
   1305 	 * Here we dont care which font we get as long as it is the
   1306 	 * right size so pass NULL.
   1307 	 */
   1308 	if (adp)
   1309 		amidisplaycc_setnamedfont(scr, NULL);
   1310 
   1311 	/*
   1312 	 * If no font found, use the builtin one.
   1313 	 * It will look stupid if the wanted size was different.
   1314 	 */
   1315 	if (scr->font == NULL) {
   1316 		scr->fontwidth = 8;
   1317 		scr->fontheight = min(8, fontheight);
   1318 	}
   1319 
   1320 	/* --- LOAD FONT END --- */
   1321 
   1322 
   1323 	for (i = 0 ; i < depth ; i++) {
   1324 		scr->planes[i] = view->bitmap->plane[i];
   1325 	}
   1326 
   1327 	for (i = 0 ; i < MAXROWS ; i++)
   1328 		scr->rowmasks[i] = 0;
   1329 
   1330 	/* Simple one-to-one mapping for most colors */
   1331 	for (i = 0 ; i < MAXCOLORS ; i++)
   1332 		scr->colormap[i] = i;
   1333 
   1334 	/*
   1335 	 * Arrange the most used pens to quickest colors.
   1336 	 * The default color for given depth is (1<<depth)-1.
   1337 	 * It is assumed it is used most and it is mapped to
   1338 	 * color that can be drawn by writing data to one bitplane
   1339 	 * only.
   1340 	 * So map colors 3->2, 7->4, 15->8 and so on.
   1341 	 */
   1342 	for (i = 2 ; i < MAXCOLORS ; i *= 2) {
   1343 		j = i * 2 - 1;
   1344 
   1345 		if (j < MAXCOLORS) {
   1346 			scr->colormap[i] = j;
   1347 			scr->colormap[j] = i;
   1348 		}
   1349 	}
   1350 
   1351 	/*
   1352 	 * Set the default colormap.
   1353 	 */
   1354 	if (depth == 1)
   1355 		amidisplaycc_setemulcmap(scr, &pal2);
   1356 	else if (depth == 2)
   1357 		amidisplaycc_setemulcmap(scr, &pal4);
   1358 	else
   1359 		amidisplaycc_setemulcmap(scr, &pal8);
   1360 
   1361 	*cookiep = scr;
   1362 
   1363 	/* cursor initially at top left */
   1364 	scr->cursorrow = -1;
   1365 	scr->cursorcol = -1;
   1366 	*curxp = 0;
   1367 	*curyp = 0;
   1368 	amidisplaycc_cursor(scr, 1, *curxp, *curyp);
   1369 
   1370 	*defattrp = MAKEATTR(maxcolor, 0, 0);
   1371 
   1372 	/* Show the console automatically */
   1373 	if (adp == NULL)
   1374 		grf_display_view(scr->view);
   1375 
   1376 	if (adp) {
   1377 		dprintf("amidisplaycc: allocated screen; %dx%dx%d\n",
   1378 			dimension.width,
   1379 			dimension.height,
   1380 			depth);
   1381 	}
   1382 
   1383 	return (0);
   1384 }
   1385 
   1386 
   1387 /*
   1388  * Destroy a screen.
   1389  */
   1390 
   1391 void
   1392 amidisplaycc_free_screen(void *dp, void *screen)
   1393 {
   1394 	struct amidisplaycc_screen  * scr;
   1395 	struct amidisplaycc_softc   * adp;
   1396 
   1397 	scr = screen;
   1398 	adp = (struct amidisplaycc_softc*)adp;
   1399 
   1400 	if (scr == NULL)
   1401 		return;
   1402 
   1403 	/* Free the used font */
   1404 	amidisplaycc_setfont(scr, NULL, -1);
   1405 
   1406 	if (adp->currentscreen == scr)
   1407 		adp->currentscreen = NULL;
   1408 
   1409 	if (scr->view)
   1410 		grf_free_view(scr->view);
   1411 	scr->view = NULL;
   1412 
   1413 	/* Take care not to free the statically allocated console screen */
   1414 	if (scr != &amidisplaycc_consolescreen) {
   1415 		free(scr, M_DEVBUF);
   1416 	}
   1417 }
   1418 
   1419 /*
   1420  * Switch to another vt. Switch is always made immediately.
   1421  */
   1422 
   1423 /* ARGSUSED2 */
   1424 int
   1425 amidisplaycc_show_screen(void *dp, void *screen, int waitok,
   1426 			 void (*cb) (void *, int, int), void *cbarg)
   1427 {
   1428 	adccscr_t *scr;
   1429 	struct amidisplaycc_softc *adp;
   1430 
   1431 	adp = (struct amidisplaycc_softc*)dp;
   1432 	scr = screen;
   1433 
   1434 	if (adp == NULL) {
   1435 		dprintf("amidisplaycc_show_screen: adp==NULL\n");
   1436 		return (EINVAL);
   1437 	}
   1438 	if (scr == NULL) {
   1439 		dprintf("amidisplaycc_show_screen: scr==NULL\n");
   1440 		return (EINVAL);
   1441 	}
   1442 
   1443 	if (adp->gfxon) {
   1444 		dprintf("amidisplaycc: Screen shift while in gfx mode?");
   1445 		adp->gfxon = 0;
   1446 	}
   1447 
   1448 	adp->currentscreen = scr;
   1449 	adp->ison = 1;
   1450 
   1451 	grf_display_view(scr->view);
   1452 
   1453 	return (0);
   1454 }
   1455 
   1456 /*
   1457  * Internal. Finds the font in our softc that has the desired attributes.
   1458  * Or, if name is NULL, finds a free location for a new font.
   1459  * Returns a pointer to font structure in softc or NULL for failure.
   1460  *
   1461  * Three possible forms:
   1462  * findfont(adp, NULL, 0, 0)  -- find first empty location
   1463  * findfont(adp, NULL, x, y)  -- find last font with given size
   1464  * findfont(adp, name, x, y)  -- find last font with given name and size
   1465  *
   1466  * Note that when finding an empty location first one found is returned,
   1467  * however when finding an existing font, the last one matching is
   1468  * returned. This is because fonts cannot be unloaded and the last
   1469  * font on the list is the one added latest and thus probably preferred.
   1470  *
   1471  * Note also that this is the only function which makes assumptions
   1472  * about the storage location for the fonts.
   1473  */
   1474 static struct wsdisplay_font *
   1475 amidisplaycc_findfont(struct amidisplaycc_softc *adp, const char *name,
   1476 		      int width, int height)
   1477 {
   1478 	struct wsdisplay_font  * font;
   1479 
   1480 	int  findempty;
   1481 	int  f;
   1482 
   1483 	if (adp == NULL) {
   1484 		dprintf("amidisplaycc_findfont: NULL adp\n");
   1485 		return NULL;
   1486 	}
   1487 
   1488 	findempty = (name == NULL) && (width == 0) && (height == 0);
   1489 
   1490 	font = NULL;
   1491 
   1492 	for (f = 0 ; f < AMIDISPLAYCC_MAXFONTS ; f++) {
   1493 
   1494 		if (findempty && adp->fonts[f].name == NULL)
   1495 			return &adp->fonts[f];
   1496 
   1497 		if (!findempty && name == NULL && adp->fonts[f].name &&
   1498 		    adp->fonts[f].fontwidth == width &&
   1499 		    adp->fonts[f].fontheight == height)
   1500 			font = &adp->fonts[f];
   1501 
   1502 		if (name && adp->fonts[f].name &&
   1503 		    strcmp(name, adp->fonts[f].name) == 0 &&
   1504 		    width == adp->fonts[f].fontwidth &&
   1505 		    height == adp->fonts[f].fontheight)
   1506 			font = &adp->fonts[f];
   1507 	}
   1508 
   1509 	return (font);
   1510 }
   1511 
   1512 
   1513 /*
   1514  * Set the font on a screen and free the old one.
   1515  * Can be called with font of NULL to just free the
   1516  * old one.
   1517  * NULL font cannot be accompanied by valid cookie (!= -1)
   1518  */
   1519 static void
   1520 amidisplaycc_setfont(struct amidisplaycc_screen *scr,
   1521 		     struct wsdisplay_font *font, int wsfontcookie)
   1522 {
   1523 	if (scr == NULL)
   1524 		panic("amidisplaycc_setfont: scr==NULL");
   1525 	if (font == NULL && wsfontcookie != -1)
   1526 		panic("amidisplaycc_setfont: no font but eat cookie");
   1527 	if (scr->font == NULL && scr->wsfontcookie != -1)
   1528 		panic("amidisplaycc_setfont: no font but eat old cookie");
   1529 
   1530 	scr->font = font;
   1531 
   1532 	if (scr->wsfontcookie != -1)
   1533 		wsfont_unlock(scr->wsfontcookie);
   1534 
   1535 	scr->wsfontcookie = wsfontcookie;
   1536 }
   1537 
   1538 /*
   1539  * Try to find the named font and set the screen to use it.
   1540  * Check both the fonts we have loaded with load_font and
   1541  * fonts from wsfont system.
   1542  *
   1543  * Returns 0 on success.
   1544  */
   1545 
   1546 static int
   1547 amidisplaycc_setnamedfont(struct amidisplaycc_screen *scr, char *fontname)
   1548 {
   1549 	struct wsdisplay_font  * font;
   1550 	int  wsfontcookie;
   1551 
   1552 	wsfontcookie = -1;
   1553 
   1554 	if (scr == NULL || scr->device == NULL) {
   1555 		dprintf("amidisplaycc_setnamedfont: invalid\n");
   1556 		return (EINVAL);
   1557 	}
   1558 
   1559 	/* Try first our dynamically loaded fonts. */
   1560 	font = amidisplaycc_findfont(scr->device,
   1561 				     fontname,
   1562 				     scr->fontwidth,
   1563 				     scr->fontheight);
   1564 
   1565 	if (font == NULL) {
   1566 		/*
   1567 		 * Ok, no dynamically loaded font found.
   1568 		 * Try the wsfont system then.
   1569 		 */
   1570 		wsfontcookie = wsfont_find(fontname,
   1571 					   scr->fontwidth,
   1572 					   scr->fontheight,
   1573 					   1,
   1574 					   WSDISPLAY_FONTORDER_L2R,
   1575 					   WSDISPLAY_FONTORDER_L2R);
   1576 
   1577 		if (wsfontcookie == -1)
   1578 			return (EINVAL);
   1579 
   1580 		/* So, found a suitable font. Now lock it. */
   1581 		if (wsfont_lock(wsfontcookie,
   1582 				&font))
   1583 			return (EINVAL);
   1584 
   1585 		/* Ok here we have the font successfully. */
   1586 	}
   1587 
   1588 	amidisplaycc_setfont(scr, font, wsfontcookie);
   1589 	return (0);
   1590 }
   1591 
   1592 /*
   1593  * Load a font. This is used both to load a font and set it to screen.
   1594  * The function depends on the parameters.
   1595  * If the font has no data we must set a previously loaded
   1596  * font with the same name. If it has data, then just load
   1597  * the font but don't use it.
   1598  */
   1599 int
   1600 amidisplaycc_load_font(void *dp, void *cookie, struct wsdisplay_font *font)
   1601 {
   1602 	struct amidisplaycc_softc   * adp;
   1603 	struct amidisplaycc_screen  * scr;
   1604 	struct wsdisplay_font       * myfont;
   1605 
   1606 	u_int8_t  * c;
   1607 	void      * olddata;
   1608 	char      * name;
   1609 
   1610 	u_int       size;
   1611 	u_int       i;
   1612 
   1613 
   1614 	adp = dp;
   1615 	scr = cookie;
   1616 
   1617 	/*
   1618 	 * If font has no data it means we have to find the
   1619 	 * named font and use it.
   1620 	 */
   1621 	if (scr && font && font->name && !font->data)
   1622 		return amidisplaycc_setnamedfont(scr, font->name);
   1623 
   1624 
   1625 	/* Pre-load the font it is */
   1626 
   1627 	if (font->stride != 1) {
   1628 		dprintf("amidisplaycc_load_font: stride %d != 1\n",
   1629 		       font->stride);
   1630 		return (EINVAL);
   1631 	}
   1632 
   1633 	if (font->fontwidth != 8) {
   1634 		dprintf("amidisplaycc_load_font: width %d not supported\n",
   1635 		       font->fontwidth);
   1636 		return (EINVAL);
   1637 	}
   1638 
   1639 	/* Size of the font in bytes... Assuming stride==1 */
   1640 	size = font->fontheight * font->numchars;
   1641 
   1642 	/* Check if the same font was loaded before */
   1643 	myfont = amidisplaycc_findfont(adp,
   1644 				       font->name,
   1645 				       font->fontwidth,
   1646 				       font->fontheight);
   1647 
   1648 	olddata = NULL;
   1649 	if (myfont) {
   1650 		/* Old font found, we will replace */
   1651 
   1652 		if (myfont->name == NULL || myfont->data == NULL)
   1653 			panic("replacing NULL font/data");
   1654 
   1655 		/*
   1656 		 * Store the old data pointer. We'll free it later
   1657 		 * when the new one is in place. Reallocation is needed
   1658 		 * because the new font may have a different number
   1659 		 * of characters in it than the last time it was loaded.
   1660 		 */
   1661 
   1662 		olddata = myfont->data;
   1663 
   1664 	} else {
   1665 		/* Totally brand new font */
   1666 
   1667 		/* Try to find empty slot for the font */
   1668 		myfont = amidisplaycc_findfont(adp, NULL, 0, 0);
   1669 
   1670 		if (myfont == NULL)
   1671 			return (ENOMEM);
   1672 
   1673 		bzero(myfont, sizeof(struct wsdisplay_font));
   1674 
   1675 		myfont->fontwidth = font->fontwidth;
   1676 		myfont->fontheight = font->fontheight;
   1677 		myfont->stride = font->stride;
   1678 
   1679 		name = malloc(strlen(font->name)+1,
   1680 			      M_DEVBUF,
   1681 			      M_WAITOK);
   1682 		strcpy(name, font->name);
   1683 		myfont->name = name;
   1684 	}
   1685 	myfont->firstchar = font->firstchar;
   1686 	myfont->numchars  = font->numchars;
   1687 
   1688 	myfont->data = malloc(size,
   1689 			      M_DEVBUF,
   1690 			      M_WAITOK);
   1691 
   1692 	if (olddata)
   1693 		free(olddata, M_DEVBUF);
   1694 
   1695 
   1696 	memcpy(myfont->data, font->data, size);
   1697 
   1698 	if (font->bitorder == WSDISPLAY_FONTORDER_R2L) {
   1699 		/* Reverse the characters. */
   1700 		c = myfont->data;
   1701 		for (i = 0 ; i < size ; i++) {
   1702 			*c = ((*c & 0x0f) << 4) | ((*c & 0xf0) >> 4);
   1703 			*c = ((*c & 0x33) << 2) | ((*c & 0xcc) >> 2);
   1704 			*c = ((*c & 0x55) << 1) | ((*c & 0xaa) >> 1);
   1705 
   1706 			c++;
   1707 		}
   1708 	}
   1709 
   1710 	/* Yeah, we made it */
   1711 	return (0);
   1712 }
   1713 
   1714 /*
   1715  * Set display on/off.
   1716  */
   1717 static int
   1718 amidisplaycc_setvideo(struct amidisplaycc_softc *adp, int mode)
   1719 {
   1720         view_t * view;
   1721 
   1722 	if (adp == NULL) {
   1723 		dprintf("amidisplaycc_setvideo: adp==NULL\n");
   1724 		return (EINVAL);
   1725 	}
   1726 	if (adp->currentscreen == NULL) {
   1727 		dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
   1728 		return (EINVAL);
   1729 	}
   1730 
   1731 	/* select graphics or emulation screen */
   1732 	if (adp->gfxon && adp->gfxview)
   1733 		view = adp->gfxview;
   1734 	else
   1735 		view = adp->currentscreen->view;
   1736 
   1737 	if (mode) {
   1738 		/* on */
   1739 
   1740 		grf_display_view(view);
   1741 		dprintf("amidisplaycc: video is now on\n");
   1742 		adp->ison = 1;
   1743 
   1744 	} else {
   1745 		/* off */
   1746 
   1747 		grf_remove_view(view);
   1748 		dprintf("amidisplaycc: video is now off\n");
   1749 		adp->ison = 0;
   1750 	}
   1751 
   1752 	return (0);
   1753 }
   1754 
   1755 /*
   1756  * Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
   1757  * Just handle the copying of data to/from userspace and
   1758  * let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
   1759  * do the real work.
   1760  */
   1761 
   1762 static int
   1763 amidisplaycc_cmapioctl(view_t *view, u_long cmd, struct wsdisplay_cmap *cmap)
   1764 {
   1765 	struct wsdisplay_cmap  tmpcmap;
   1766 	u_char                 cmred[MAXCOLORS];
   1767 	u_char                 cmgrn[MAXCOLORS];
   1768 	u_char                 cmblu[MAXCOLORS];
   1769 
   1770 	int                    err;
   1771 
   1772 	if (cmap->index >= MAXCOLORS ||
   1773 	    cmap->count > MAXCOLORS ||
   1774 	    cmap->index + cmap->count > MAXCOLORS)
   1775 		return (EINVAL);
   1776 
   1777 	if (cmap->count == 0)
   1778 		return (0);
   1779 
   1780 	tmpcmap.index = cmap->index;
   1781 	tmpcmap.count = cmap->count;
   1782 	tmpcmap.red   = cmred;
   1783 	tmpcmap.green = cmgrn;
   1784 	tmpcmap.blue  = cmblu;
   1785 
   1786 	if (cmd == WSDISPLAYIO_PUTCMAP) {
   1787 		/* copy the color data to kernel space */
   1788 
   1789 		err = copyin(cmap->red, cmred, cmap->count);
   1790 		if (err)
   1791 			return (err);
   1792 
   1793 		err = copyin(cmap->green, cmgrn, cmap->count);
   1794 		if (err)
   1795 			return (err);
   1796 
   1797 		err = copyin(cmap->blue, cmblu, cmap->count);
   1798 		if (err)
   1799 			return (err);
   1800 
   1801 		return amidisplaycc_setcmap(view, &tmpcmap);
   1802 
   1803 	} else if (cmd == WSDISPLAYIO_GETCMAP) {
   1804 
   1805 		err = amidisplaycc_getcmap(view, &tmpcmap);
   1806 		if (err)
   1807 			return (err);
   1808 
   1809 		/* copy data to user space */
   1810 
   1811 		err = copyout(cmred, cmap->red, cmap->count);
   1812 		if (err)
   1813 			return (err);
   1814 
   1815 		err = copyout(cmgrn, cmap->green, cmap->count);
   1816 		if (err)
   1817 			return (err);
   1818 
   1819 		err = copyout(cmblu, cmap->blue, cmap->count);
   1820 		if (err)
   1821 			return (err);
   1822 
   1823 		return (0);
   1824 
   1825 	} else
   1826 		return (EPASSTHROUGH);
   1827 }
   1828 
   1829 /*
   1830  * Set the palette of a emulation screen.
   1831  * Here we do only color remapping and then call
   1832  * amidisplaycc_setcmap to do the work.
   1833  */
   1834 
   1835 static int
   1836 amidisplaycc_setemulcmap(struct amidisplaycc_screen *scr,
   1837 			 struct wsdisplay_cmap *cmap)
   1838 {
   1839 	struct wsdisplay_cmap  tmpcmap;
   1840 
   1841 	u_char                 red [MAXCOLORS];
   1842 	u_char                 grn [MAXCOLORS];
   1843 	u_char                 blu [MAXCOLORS];
   1844 
   1845 	int                    rc;
   1846 	int                    i;
   1847 
   1848 	/*
   1849 	 * Get old palette first.
   1850 	 * Because of the color mapping going on in the emulation
   1851 	 * screen the color range may not be contiguous in the real
   1852 	 * palette.
   1853 	 * So get the whole palette, insert the new colors
   1854 	 * at the appropriate places and then set the whole
   1855 	 * palette back.
   1856 	 */
   1857 
   1858 	tmpcmap.index = 0;
   1859 	tmpcmap.count = 1 << scr->depth;
   1860 	tmpcmap.red   = red;
   1861 	tmpcmap.green = grn;
   1862 	tmpcmap.blue  = blu;
   1863 
   1864 	rc = amidisplaycc_getcmap(scr->view, &tmpcmap);
   1865 	if (rc)
   1866 		return (rc);
   1867 
   1868 	for (i = cmap->index ; i < cmap->index + cmap->count ; i++) {
   1869 
   1870 		tmpcmap.red   [ scr->colormap[ i ] ] = cmap->red   [ i ];
   1871 		tmpcmap.green [ scr->colormap[ i ] ] = cmap->green [ i ];
   1872 		tmpcmap.blue  [ scr->colormap[ i ] ] = cmap->blue  [ i ];
   1873 	}
   1874 
   1875 	rc = amidisplaycc_setcmap(scr->view, &tmpcmap);
   1876 	if (rc)
   1877 		return (rc);
   1878 
   1879 	return (0);
   1880 }
   1881 
   1882 
   1883 /*
   1884  * Set the colormap for the given screen.
   1885  */
   1886 
   1887 static int
   1888 amidisplaycc_setcmap(view_t *view, struct wsdisplay_cmap *cmap)
   1889 {
   1890 	u_long      cmentries [MAXCOLORS];
   1891 
   1892 	int         green_div;
   1893 	int         blue_div;
   1894 	int         grey_div;
   1895 	int         red_div;
   1896 	u_int       colors;
   1897 	int         index;
   1898 	int         count;
   1899 	int         err;
   1900 	colormap_t  cm;
   1901 	int         c;
   1902 
   1903 	if (view == NULL)
   1904 		return (EINVAL);
   1905 
   1906 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue) {
   1907 		dprintf("amidisplaycc_setcmap: other==NULL\n");
   1908 		return (EINVAL);
   1909 	}
   1910 
   1911 	index  = cmap->index;
   1912 	count  = cmap->count;
   1913 	colors = (1 << view->bitmap->depth);
   1914 
   1915 	if (count > colors || index >= colors || index + count > colors)
   1916 		return (EINVAL);
   1917 
   1918 	if (count == 0)
   1919 		return (0);
   1920 
   1921 	cm.entry = cmentries;
   1922 	cm.first = index;
   1923 	cm.size  = count;
   1924 
   1925 	/*
   1926 	 * Get the old colormap. We need to do this at least to know
   1927 	 * how many bits to use with the color values.
   1928 	 */
   1929 
   1930 	err = grf_get_colormap(view, &cm);
   1931 	if (err)
   1932 		return (err);
   1933 
   1934 	/*
   1935 	 * The palette entries from wscons contain 8 bits per gun.
   1936 	 * We need to convert them to the number of bits the view
   1937 	 * expects. That is typically 4 or 8. Here we calculate the
   1938 	 * conversion constants with which we divide the color values.
   1939 	 */
   1940 
   1941 	if (cm.type == CM_COLOR) {
   1942 		red_div = 256 / (cm.red_mask + 1);
   1943 		green_div = 256 / (cm.green_mask + 1);
   1944 		blue_div = 256 / (cm.blue_mask + 1);
   1945 	} else if (cm.type == CM_GREYSCALE)
   1946 		grey_div = 256 / (cm.grey_mask + 1);
   1947 	else
   1948 		return (EINVAL); /* Hmhh */
   1949 
   1950 	/* Copy our new values to the current colormap */
   1951 
   1952 	for (c = 0 ; c < count ; c++) {
   1953 
   1954 		if (cm.type == CM_COLOR) {
   1955 
   1956 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
   1957 				cmap->red[c] / red_div,
   1958 				cmap->green[c] / green_div,
   1959 				cmap->blue[c] / blue_div);
   1960 
   1961 		} else if (cm.type == CM_GREYSCALE) {
   1962 
   1963 			/* Generate grey from average of r-g-b (?) */
   1964 
   1965 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
   1966 				0,
   1967 				0,
   1968 				(cmap->red[c] +
   1969 				 cmap->green[c] +
   1970 				 cmap->blue[c]) / 3 / grey_div);
   1971 		}
   1972 	}
   1973 
   1974 
   1975 	/*
   1976 	 * Now we have a new colormap that contains all the entries. Set
   1977 	 * it to the view.
   1978 	 */
   1979 
   1980 	err = grf_use_colormap(view, &cm);
   1981 	if (err)
   1982 		return err;
   1983 
   1984 	return (0);
   1985 }
   1986 
   1987 /*
   1988  * Return the colormap of the given screen.
   1989  */
   1990 
   1991 static int
   1992 amidisplaycc_getcmap(view_t *view, struct wsdisplay_cmap *cmap)
   1993 {
   1994 	u_long      cmentries [MAXCOLORS];
   1995 
   1996 	int         green_mul;
   1997 	int         blue_mul;
   1998 	int         grey_mul;
   1999 	int         red_mul;
   2000 	u_int       colors;
   2001 	int         index;
   2002 	int         count;
   2003 	int         err;
   2004 	colormap_t  cm;
   2005 	int         c;
   2006 
   2007 	if (view == NULL)
   2008 		return (EINVAL);
   2009 
   2010 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue)
   2011 		return (EINVAL);
   2012 
   2013 	index  = cmap->index;
   2014 	count  = cmap->count;
   2015 	colors = (1 << view->bitmap->depth);
   2016 
   2017 	if (count > colors || index >= colors || index + count > colors)
   2018 		return (EINVAL);
   2019 
   2020 	if (count == 0)
   2021 		return (0);
   2022 
   2023 	cm.entry = cmentries;
   2024 	cm.first = index;
   2025 	cm.size  = count;
   2026 
   2027 
   2028 	err = grf_get_colormap(view, &cm);
   2029 	if (err)
   2030 		return (err);
   2031 
   2032 	if (cm.type == CM_COLOR) {
   2033 		red_mul   = 256 / (cm.red_mask + 1);
   2034 		green_mul = 256 / (cm.green_mask + 1);
   2035 		blue_mul  = 256 / (cm.blue_mask + 1);
   2036 	} else if (cm.type == CM_GREYSCALE) {
   2037 		grey_mul  = 256 / (cm.grey_mask + 1);
   2038 	} else
   2039 		return (EINVAL);
   2040 
   2041 	/*
   2042 	 * Copy color data to wscons-style structure. Translate to
   2043 	 * 8 bits/gun from whatever resolution the color natively is.
   2044 	 */
   2045 
   2046 	for (c = 0 ; c < count ; c++) {
   2047 
   2048 		if (cm.type == CM_COLOR) {
   2049 
   2050 			cmap->red[c]   = CM_GET_RED(cm.entry[index+c]);
   2051 			cmap->green[c] = CM_GET_GREEN(cm.entry[index+c]);
   2052 			cmap->blue[c]  = CM_GET_BLUE(cm.entry[index+c]);
   2053 
   2054 			cmap->red[c]   *= red_mul;
   2055 			cmap->green[c] *= green_mul;
   2056 			cmap->blue[c]  *= blue_mul;
   2057 
   2058 		} else if (cm.type == CM_GREYSCALE) {
   2059 			cmap->red[c]   = CM_GET_GREY(cm.entry[index+c]);
   2060 			cmap->red[c]  *= grey_mul;
   2061 
   2062 			cmap->green[c] = cmap->red[c];
   2063 			cmap->blue[c]  = cmap->red[c];
   2064 		}
   2065 	}
   2066 
   2067 	return (0);
   2068 }
   2069 
   2070 /* ARGSUSED */
   2071 void
   2072 amidisplaycc_pollc(void *cookie, int on)
   2073 {
   2074 }
   2075 
   2076 /*
   2077  * These dummy functions are here just so that we can compete of
   2078  * the console at init.
   2079  * If we win the console then the wscons system will provide the
   2080  * real ones which in turn will call the apropriate wskbd device.
   2081  * These should never be called.
   2082  */
   2083 
   2084 /* ARGSUSED */
   2085 void
   2086 amidisplaycc_cnputc(dev_t cd, int ch)
   2087 {
   2088 }
   2089 
   2090 /* ARGSUSED */
   2091 int
   2092 amidisplaycc_cngetc(dev_t cd)
   2093 {
   2094 	return (0);
   2095 }
   2096 
   2097 /* ARGSUSED */
   2098 void
   2099 amidisplaycc_cnpollc(dev_t cd, int on)
   2100 {
   2101 }
   2102 
   2103 
   2104 /*
   2105  * Prints stuff if DEBUG is turned on.
   2106  */
   2107 
   2108 /* ARGSUSED */
   2109 static void
   2110 dprintf(const char *fmt, ...)
   2111 {
   2112 #ifdef DEBUG
   2113 	va_list ap;
   2114 
   2115 	va_start(ap, fmt);
   2116 	vprintf(fmt, ap);
   2117 	va_end(ap);
   2118 #endif
   2119 }
   2120 
   2121 #endif /* AMIDISPLAYCC */
   2122