Home | History | Annotate | Line # | Download | only in wsfb
genfb.c revision 1.50
      1 /*	$NetBSD: genfb.c,v 1.50 2013/01/10 22:06:59 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.50 2013/01/10 22:06:59 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/kernel.h>
     35 #include <sys/device.h>
     36 #include <sys/proc.h>
     37 #include <sys/mutex.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/kernel.h>
     40 #include <sys/systm.h>
     41 #include <sys/kmem.h>
     42 
     43 #include <dev/wscons/wsconsio.h>
     44 #include <dev/wscons/wsdisplayvar.h>
     45 #include <dev/rasops/rasops.h>
     46 #include <dev/wsfont/wsfont.h>
     47 
     48 #include <dev/wscons/wsdisplay_vconsvar.h>
     49 
     50 #include <dev/wsfb/genfbvar.h>
     51 
     52 #ifdef GENFB_DISABLE_TEXT
     53 #include <sys/reboot.h>
     54 #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
     55 		AB_VERBOSE | AB_DEBUG) )
     56 #endif
     57 
     58 #include "opt_genfb.h"
     59 #include "opt_wsfb.h"
     60 
     61 #ifdef GENFB_DEBUG
     62 #define GPRINTF panic
     63 #else
     64 #define GPRINTF aprint_debug
     65 #endif
     66 
     67 #define GENFB_BRIGHTNESS_STEP 15
     68 
     69 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     70 static paddr_t	genfb_mmap(void *, void *, off_t, int);
     71 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
     72 
     73 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     74 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     75 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
     76 			    uint8_t, uint8_t);
     77 static void	genfb_init_palette(struct genfb_softc *);
     78 
     79 static void	genfb_brightness_up(device_t);
     80 static void	genfb_brightness_down(device_t);
     81 
     82 extern const u_char rasops_cmap[768];
     83 
     84 static int genfb_cnattach_called = 0;
     85 static int genfb_enabled = 1;
     86 
     87 static struct genfb_softc *genfb_softc = NULL;
     88 
     89 void
     90 genfb_init(struct genfb_softc *sc)
     91 {
     92 	prop_dictionary_t dict;
     93 	uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
     94 	uint32_t fboffset;
     95 	bool console;
     96 
     97 	dict = device_properties(sc->sc_dev);
     98 #ifdef GENFB_DEBUG
     99 	printf(prop_dictionary_externalize(dict));
    100 #endif
    101 	prop_dictionary_get_bool(dict, "is_console", &console);
    102 
    103 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    104 		GPRINTF("no width property\n");
    105 		return;
    106 	}
    107 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    108 		GPRINTF("no height property\n");
    109 		return;
    110 	}
    111 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    112 		GPRINTF("no depth property\n");
    113 		return;
    114 	}
    115 
    116 	/* XXX should be a 64bit value */
    117 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
    118 		GPRINTF("no address property\n");
    119 		return;
    120 	}
    121 
    122 	sc->sc_fboffset = fboffset;
    123 
    124 	sc->sc_fbaddr = NULL;
    125 	if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
    126 		sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
    127 	}
    128 
    129 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
    130 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
    131 
    132 	/*
    133 	 * deal with a bug in the Raptor firmware which always sets
    134 	 * stride = width even when depth != 8
    135 	 */
    136 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
    137 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    138 
    139 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
    140 
    141 	/* optional colour map callback */
    142 	sc->sc_cmcb = NULL;
    143 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
    144 		if (cmap_cb != 0)
    145 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
    146 	}
    147 
    148 	/* optional pmf callback */
    149 	sc->sc_pmfcb = NULL;
    150 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
    151 		if (pmf_cb != 0)
    152 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
    153 	}
    154 
    155 	/* optional mode callback */
    156 	sc->sc_modecb = NULL;
    157 	if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
    158 		if (mode_cb != 0)
    159 			sc->sc_modecb = (void *)(vaddr_t)mode_cb;
    160 	}
    161 
    162 	/* optional backlight control callback */
    163 	sc->sc_backlight = NULL;
    164 	if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
    165 		if (bl_cb != 0) {
    166 			sc->sc_backlight = (void *)(vaddr_t)bl_cb;
    167 			aprint_naive_dev(sc->sc_dev,
    168 			    "enabling backlight control\n");
    169 		}
    170 	}
    171 
    172 	/* optional brightness control callback */
    173 	sc->sc_brightness = NULL;
    174 	if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
    175 		if (br_cb != 0) {
    176 			sc->sc_brightness = (void *)(vaddr_t)br_cb;
    177 			aprint_naive_dev(sc->sc_dev,
    178 			    "enabling brightness control\n");
    179 			if (console &&
    180 			    sc->sc_brightness->gpc_upd_parameter != NULL) {
    181 				pmf_event_register(sc->sc_dev,
    182 				    PMFE_DISPLAY_BRIGHTNESS_UP,
    183 				    genfb_brightness_up, TRUE);
    184 				pmf_event_register(sc->sc_dev,
    185 				    PMFE_DISPLAY_BRIGHTNESS_DOWN,
    186 				    genfb_brightness_down, TRUE);
    187 			}
    188 		}
    189 	}
    190 }
    191 
    192 int
    193 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
    194 {
    195 	struct wsemuldisplaydev_attach_args aa;
    196 	prop_dictionary_t dict;
    197 	struct rasops_info *ri;
    198 	uint16_t crow;
    199 	long defattr;
    200 	bool console;
    201 #ifdef SPLASHSCREEN
    202 	int i, j;
    203 	int error = ENXIO;
    204 #endif
    205 
    206 	dict = device_properties(sc->sc_dev);
    207 	prop_dictionary_get_bool(dict, "is_console", &console);
    208 
    209 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
    210 		crow = 0;
    211 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
    212 	    == false)
    213 		sc->sc_want_clear = true;
    214 
    215 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
    216 	    "stride %d\n",
    217 	    sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
    218 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    219 
    220 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    221 		"default",
    222 		0, 0,
    223 		NULL,
    224 		8, 16,
    225 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    226 		NULL
    227 	};
    228 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    229 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    230 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
    231 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    232 	if (sc->sc_modecb != NULL)
    233 		sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
    234 
    235 	sc->sc_accessops.ioctl = genfb_ioctl;
    236 	sc->sc_accessops.mmap = genfb_mmap;
    237 
    238 #ifdef GENFB_SHADOWFB
    239 	sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
    240 	if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
    241 		memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
    242 #endif
    243 
    244 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    245 	    &sc->sc_accessops);
    246 	sc->vd.init_screen = genfb_init_screen;
    247 
    248 	/* Do not print anything between this point and the screen
    249 	 * clear operation below.  Otherwise it will be lost. */
    250 
    251 	ri = &sc->sc_console_screen.scr_ri;
    252 
    253 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    254 	    &defattr);
    255 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    256 
    257 #ifdef SPLASHSCREEN
    258 /*
    259  * If system isn't going to go multiuser, or user has requested to see
    260  * boot text, don't render splash screen immediately
    261  */
    262 	if (DISABLESPLASH)
    263 #endif
    264 		vcons_redraw_screen(&sc->sc_console_screen);
    265 
    266 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    267 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    268 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    269 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    270 
    271 	if (crow >= ri->ri_rows) {
    272 		crow = 0;
    273 		sc->sc_want_clear = 1;
    274 	}
    275 
    276 	if (console)
    277 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
    278 		    defattr);
    279 
    280 	/* Clear the whole screen to bring it to a known state. */
    281 	if (sc->sc_want_clear)
    282 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
    283 
    284 #ifdef SPLASHSCREEN
    285 	j = 0;
    286 	for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
    287 		if (i >= SPLASH_CMAP_OFFSET &&
    288 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
    289 			splash_get_cmap(i,
    290 			    &sc->sc_cmap_red[i],
    291 			    &sc->sc_cmap_green[i],
    292 			    &sc->sc_cmap_blue[i]);
    293 		} else {
    294 			sc->sc_cmap_red[i] = rasops_cmap[j];
    295 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    296 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    297 		}
    298 		j += 3;
    299 	}
    300 	genfb_restore_palette(sc);
    301 
    302 	sc->sc_splash.si_depth = sc->sc_depth;
    303 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
    304 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
    305 	sc->sc_splash.si_width = sc->sc_width;
    306 	sc->sc_splash.si_height = sc->sc_height;
    307 	sc->sc_splash.si_stride = sc->sc_stride;
    308 	sc->sc_splash.si_fillrect = NULL;
    309 	if (!DISABLESPLASH) {
    310 		error = splash_render(&sc->sc_splash,
    311 		    SPLASH_F_CENTER|SPLASH_F_FILL);
    312 		if (error) {
    313 			SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    314 			genfb_init_palette(sc);
    315 			vcons_replay_msgbuf(&sc->sc_console_screen);
    316 		}
    317 	}
    318 #else
    319 	genfb_init_palette(sc);
    320 	vcons_replay_msgbuf(&sc->sc_console_screen);
    321 #endif
    322 
    323 	if (genfb_softc == NULL)
    324 		genfb_softc = sc;
    325 
    326 	aa.console = console;
    327 	aa.scrdata = &sc->sc_screenlist;
    328 	aa.accessops = &sc->sc_accessops;
    329 	aa.accesscookie = &sc->vd;
    330 
    331 #ifdef GENFB_DISABLE_TEXT
    332 	if (!DISABLESPLASH && error == 0)
    333 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    334 #endif
    335 
    336 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    337 
    338 	return 0;
    339 }
    340 
    341 static int
    342 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    343 	struct lwp *l)
    344 {
    345 	struct vcons_data *vd = v;
    346 	struct genfb_softc *sc = vd->cookie;
    347 	struct wsdisplay_fbinfo *wdf;
    348 	struct vcons_screen *ms = vd->active;
    349 	struct wsdisplay_param *param;
    350 	int new_mode, error, val;
    351 
    352 	switch (cmd) {
    353 		case WSDISPLAYIO_GINFO:
    354 			if (ms == NULL)
    355 				return ENODEV;
    356 			wdf = (void *)data;
    357 			wdf->height = ms->scr_ri.ri_height;
    358 			wdf->width = ms->scr_ri.ri_width;
    359 			wdf->depth = ms->scr_ri.ri_depth;
    360 			wdf->cmsize = 256;
    361 			return 0;
    362 
    363 		case WSDISPLAYIO_GETCMAP:
    364 			return genfb_getcmap(sc,
    365 			    (struct wsdisplay_cmap *)data);
    366 
    367 		case WSDISPLAYIO_PUTCMAP:
    368 			return genfb_putcmap(sc,
    369 			    (struct wsdisplay_cmap *)data);
    370 
    371 		case WSDISPLAYIO_LINEBYTES:
    372 			*(u_int *)data = sc->sc_stride;
    373 			return 0;
    374 
    375 		case WSDISPLAYIO_SMODE:
    376 			new_mode = *(int *)data;
    377 
    378 			/* notify the bus backend */
    379 			error = 0;
    380 			if (sc->sc_ops.genfb_ioctl)
    381 				error = sc->sc_ops.genfb_ioctl(sc, vs,
    382 					    cmd, data, flag, l);
    383 			if (error && error != EPASSTHROUGH)
    384 				return error;
    385 
    386 			if (new_mode != sc->sc_mode) {
    387 				sc->sc_mode = new_mode;
    388 				if (sc->sc_modecb != NULL)
    389 					sc->sc_modecb->gmc_setmode(sc,
    390 					    sc->sc_mode);
    391 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    392 					genfb_restore_palette(sc);
    393 					vcons_redraw_screen(ms);
    394 				}
    395 			}
    396 			return 0;
    397 		case WSDISPLAYIO_SSPLASH:
    398 #if defined(SPLASHSCREEN)
    399 			if(*(int *)data == 1) {
    400 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    401 				splash_render(&sc->sc_splash,
    402 						SPLASH_F_CENTER|SPLASH_F_FILL);
    403 			} else {
    404 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    405 				genfb_init_palette(sc);
    406 			}
    407 			vcons_redraw_screen(ms);
    408 			return 0;
    409 #else
    410 			return ENODEV;
    411 #endif
    412 		case WSDISPLAYIO_GETPARAM:
    413 			param = (struct wsdisplay_param *)data;
    414 			switch (param->param) {
    415 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    416 				if (sc->sc_brightness == NULL)
    417 					return EPASSTHROUGH;
    418 				param->min = 0;
    419 				param->max = 255;
    420 				return sc->sc_brightness->gpc_get_parameter(
    421 				    sc->sc_brightness->gpc_cookie,
    422 				    &param->curval);
    423 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    424 				if (sc->sc_backlight == NULL)
    425 					return EPASSTHROUGH;
    426 				param->min = 0;
    427 				param->max = 1;
    428 				return sc->sc_backlight->gpc_get_parameter(
    429 				    sc->sc_backlight->gpc_cookie,
    430 				    &param->curval);
    431 			}
    432 			return EPASSTHROUGH;
    433 
    434 		case WSDISPLAYIO_SETPARAM:
    435 			param = (struct wsdisplay_param *)data;
    436 			switch (param->param) {
    437 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    438 				if (sc->sc_brightness == NULL)
    439 					return EPASSTHROUGH;
    440 				val = param->curval;
    441 				if (val < 0) val = 0;
    442 				if (val > 255) val = 255;
    443 				return sc->sc_brightness->gpc_set_parameter(
    444 				    sc->sc_brightness->gpc_cookie, val);
    445 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    446 				if (sc->sc_backlight == NULL)
    447 					return EPASSTHROUGH;
    448 				val = param->curval;
    449 				if (val < 0) val = 0;
    450 				if (val > 1) val = 1;
    451 				return sc->sc_backlight->gpc_set_parameter(
    452 				    sc->sc_backlight->gpc_cookie, val);
    453 			}
    454 			return EPASSTHROUGH;
    455 		case WSDISPLAYIO_GET_EDID: {
    456 			struct wsdisplayio_edid_info *d = data;
    457 			return wsdisplayio_get_edid(sc->sc_dev, d);
    458 		}
    459 		default:
    460 			if (sc->sc_ops.genfb_ioctl)
    461 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
    462 				    data, flag, l);
    463 	}
    464 	return EPASSTHROUGH;
    465 }
    466 
    467 static paddr_t
    468 genfb_mmap(void *v, void *vs, off_t offset, int prot)
    469 {
    470 	struct vcons_data *vd = v;
    471 	struct genfb_softc *sc = vd->cookie;
    472 
    473 	if (sc->sc_ops.genfb_mmap)
    474 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
    475 
    476 	return -1;
    477 }
    478 
    479 static void
    480 genfb_init_screen(void *cookie, struct vcons_screen *scr,
    481     int existing, long *defattr)
    482 {
    483 	struct genfb_softc *sc = cookie;
    484 	struct rasops_info *ri = &scr->scr_ri;
    485 
    486 	ri->ri_depth = sc->sc_depth;
    487 	ri->ri_width = sc->sc_width;
    488 	ri->ri_height = sc->sc_height;
    489 	ri->ri_stride = sc->sc_stride;
    490 	ri->ri_flg = RI_CENTER;
    491 	if (sc->sc_want_clear)
    492 		ri->ri_flg |= RI_FULLCLEAR;
    493 
    494 #ifdef GENFB_SHADOWFB
    495 	if (sc->sc_shadowfb != NULL) {
    496 
    497 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
    498 		ri->ri_bits = (char *)sc->sc_shadowfb;
    499 	} else
    500 #endif
    501 	{
    502 		ri->ri_bits = (char *)sc->sc_fbaddr;
    503 		scr->scr_flags |= VCONS_DONT_READ;
    504 	}
    505 
    506 	if (existing && sc->sc_want_clear) {
    507 		ri->ri_flg |= RI_CLEAR;
    508 	}
    509 
    510 	if (ri->ri_depth == 32) {
    511 		bool is_bgr = false;
    512 
    513 		ri->ri_flg |= RI_ENABLE_ALPHA;
    514 		prop_dictionary_get_bool(device_properties(sc->sc_dev),
    515 		    "is_bgr", &is_bgr);
    516 		if (is_bgr) {
    517 			/* someone requested BGR */
    518 			ri->ri_rnum = 8;
    519 			ri->ri_gnum = 8;
    520 			ri->ri_bnum = 8;
    521 			ri->ri_rpos = 0;
    522 			ri->ri_gpos = 8;
    523 			ri->ri_bpos = 16;
    524 		} else {
    525 			/* assume RGB */
    526 			ri->ri_rnum = 8;
    527 			ri->ri_gnum = 8;
    528 			ri->ri_bnum = 8;
    529 			ri->ri_rpos = 16;
    530 			ri->ri_gpos = 8;
    531 			ri->ri_bpos = 0;
    532 		}
    533 	}
    534 
    535 	if (ri->ri_depth == 8 && sc->sc_cmcb != NULL)
    536 		ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
    537 
    538 
    539 	rasops_init(ri, 0, 0);
    540 	ri->ri_caps = WSSCREEN_WSCOLORS;
    541 
    542 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    543 		    sc->sc_width / ri->ri_font->fontwidth);
    544 
    545 	/* TODO: actually center output */
    546 	ri->ri_hw = scr;
    547 
    548 #ifdef GENFB_DISABLE_TEXT
    549 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
    550 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    551 #endif
    552 }
    553 
    554 static int
    555 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    556 {
    557 	u_char *r, *g, *b;
    558 	u_int index = cm->index;
    559 	u_int count = cm->count;
    560 	int i, error;
    561 	u_char rbuf[256], gbuf[256], bbuf[256];
    562 
    563 #ifdef GENFB_DEBUG
    564 	aprint_debug("putcmap: %d %d\n",index, count);
    565 #endif
    566 	if (cm->index >= 256 || cm->count > 256 ||
    567 	    (cm->index + cm->count) > 256)
    568 		return EINVAL;
    569 	error = copyin(cm->red, &rbuf[index], count);
    570 	if (error)
    571 		return error;
    572 	error = copyin(cm->green, &gbuf[index], count);
    573 	if (error)
    574 		return error;
    575 	error = copyin(cm->blue, &bbuf[index], count);
    576 	if (error)
    577 		return error;
    578 
    579 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    580 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    581 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    582 
    583 	r = &sc->sc_cmap_red[index];
    584 	g = &sc->sc_cmap_green[index];
    585 	b = &sc->sc_cmap_blue[index];
    586 
    587 	for (i = 0; i < count; i++) {
    588 		genfb_putpalreg(sc, index, *r, *g, *b);
    589 		index++;
    590 		r++, g++, b++;
    591 	}
    592 	return 0;
    593 }
    594 
    595 static int
    596 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    597 {
    598 	u_int index = cm->index;
    599 	u_int count = cm->count;
    600 	int error;
    601 
    602 	if (index >= 255 || count > 256 || index + count > 256)
    603 		return EINVAL;
    604 
    605 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    606 	if (error)
    607 		return error;
    608 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    609 	if (error)
    610 		return error;
    611 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    612 	if (error)
    613 		return error;
    614 
    615 	return 0;
    616 }
    617 
    618 void
    619 genfb_restore_palette(struct genfb_softc *sc)
    620 {
    621 	int i;
    622 
    623 	if (sc->sc_depth <= 8) {
    624 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    625 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    626 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    627 		}
    628 	}
    629 }
    630 
    631 static void
    632 genfb_init_palette(struct genfb_softc *sc)
    633 {
    634 	int i, j, tmp;
    635 
    636 	if (sc->sc_depth == 8) {
    637 		/* generate an r3g3b2 colour map */
    638 		for (i = 0; i < 256; i++) {
    639 			tmp = i & 0xe0;
    640 			/*
    641 			 * replicate bits so 0xe0 maps to a red value of 0xff
    642 			 * in order to make white look actually white
    643 			 */
    644 			tmp |= (tmp >> 3) | (tmp >> 6);
    645 			sc->sc_cmap_red[i] = tmp;
    646 
    647 			tmp = (i & 0x1c) << 3;
    648 			tmp |= (tmp >> 3) | (tmp >> 6);
    649 			sc->sc_cmap_green[i] = tmp;
    650 
    651 			tmp = (i & 0x03) << 6;
    652 			tmp |= tmp >> 2;
    653 			tmp |= tmp >> 4;
    654 			sc->sc_cmap_blue[i] = tmp;
    655 
    656 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    657 				       sc->sc_cmap_green[i],
    658 				       sc->sc_cmap_blue[i]);
    659 		}
    660 	} else {
    661 		/* steal rasops' ANSI cmap */
    662 		j = 0;
    663 		for (i = 0; i < 256; i++) {
    664 			sc->sc_cmap_red[i] = rasops_cmap[j];
    665 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    666 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    667 			j += 3;
    668 		}
    669 	}
    670 }
    671 
    672 static int
    673 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    674     uint8_t b)
    675 {
    676 
    677 	if (sc->sc_cmcb) {
    678 
    679 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
    680 		    idx, r, g, b);
    681 	}
    682 	return 0;
    683 }
    684 
    685 void
    686 genfb_cnattach(void)
    687 {
    688 	genfb_cnattach_called = 1;
    689 }
    690 
    691 void
    692 genfb_disable(void)
    693 {
    694 	genfb_enabled = 0;
    695 }
    696 
    697 int
    698 genfb_is_console(void)
    699 {
    700 	return genfb_cnattach_called;
    701 }
    702 
    703 int
    704 genfb_is_enabled(void)
    705 {
    706 	return genfb_enabled;
    707 }
    708 
    709 int
    710 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
    711 {
    712 	struct genfb_softc *sc = genfb_softc;
    713 
    714 	if (sc && sc->sc_ops.genfb_borrow)
    715 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
    716 	return 0;
    717 }
    718 
    719 static void
    720 genfb_brightness_up(device_t dev)
    721 {
    722 	struct genfb_softc *sc = device_private(dev);
    723 
    724 	KASSERT(sc->sc_brightness != NULL &&
    725 		sc->sc_brightness->gpc_upd_parameter != NULL);
    726 
    727 	(void)sc->sc_brightness->gpc_upd_parameter(
    728 	    sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
    729 }
    730 
    731 static void
    732 genfb_brightness_down(device_t dev)
    733 {
    734 	struct genfb_softc *sc = device_private(dev);
    735 
    736 	KASSERT(sc->sc_brightness != NULL &&
    737 		sc->sc_brightness->gpc_upd_parameter != NULL);
    738 
    739 	(void)sc->sc_brightness->gpc_upd_parameter(
    740 	    sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
    741 }
    742 
    743 void
    744 genfb_enable_polling(device_t dev)
    745 {
    746 	struct genfb_softc *sc = device_private(dev);
    747 
    748 	if (sc->sc_console_screen.scr_vd) {
    749 		SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    750 		vcons_hard_switch(&sc->sc_console_screen);
    751 		vcons_enable_polling(&sc->vd);
    752 	}
    753 }
    754 
    755 void
    756 genfb_disable_polling(device_t dev)
    757 {
    758 	struct genfb_softc *sc = device_private(dev);
    759 
    760 	if (sc->sc_console_screen.scr_vd) {
    761 		vcons_disable_polling(&sc->vd);
    762 	}
    763 }
    764