Home | History | Annotate | Line # | Download | only in wsfb
genfb.c revision 1.32
      1 /*	$NetBSD: genfb.c,v 1.32 2010/10/07 07:53:53 macallan 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.32 2010/10/07 07:53:53 macallan 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_verbose
     65 #endif
     66 
     67 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     68 static paddr_t	genfb_mmap(void *, void *, off_t, int);
     69 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
     70 
     71 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     72 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     73 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
     74 			    uint8_t, uint8_t);
     75 
     76 static void	genfb_brightness_up(device_t);
     77 static void	genfb_brightness_down(device_t);
     78 /* set backlight level */
     79 static void	genfb_set_backlight(struct genfb_softc *, int);
     80 /* turn backlight on and off without messing with the level */
     81 static void	genfb_switch_backlight(struct genfb_softc *, int);
     82 
     83 extern const u_char rasops_cmap[768];
     84 
     85 static int genfb_cnattach_called = 0;
     86 static int genfb_enabled = 1;
     87 
     88 struct wsdisplay_accessops genfb_accessops = {
     89 	genfb_ioctl,
     90 	genfb_mmap,
     91 	NULL,	/* alloc_screen */
     92 	NULL,	/* free_screen */
     93 	NULL,	/* show_screen */
     94 	NULL, 	/* load_font */
     95 	NULL,	/* pollc */
     96 	NULL	/* scroll */
     97 };
     98 
     99 static struct genfb_softc *genfb_softc = NULL;
    100 
    101 void
    102 genfb_init(struct genfb_softc *sc)
    103 {
    104 	prop_dictionary_t dict;
    105 	uint64_t cmap_cb, pmf_cb, bl_cb;
    106 	uint32_t fboffset;
    107 	bool console;
    108 
    109 	dict = device_properties(sc->sc_dev);
    110 #ifdef GENFB_DEBUG
    111 	printf(prop_dictionary_externalize(dict));
    112 #endif
    113 	prop_dictionary_get_bool(dict, "is_console", &console);
    114 
    115 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    116 		GPRINTF("no width property\n");
    117 		return;
    118 	}
    119 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    120 		GPRINTF("no height property\n");
    121 		return;
    122 	}
    123 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    124 		GPRINTF("no depth property\n");
    125 		return;
    126 	}
    127 
    128 	/* XXX should be a 64bit value */
    129 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset)) {
    130 		GPRINTF("no address property\n");
    131 		return;
    132 	}
    133 
    134 	sc->sc_fboffset = fboffset;
    135 
    136 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
    137 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
    138 
    139 	/*
    140 	 * deal with a bug in the Raptor firmware which always sets
    141 	 * stride = width even when depth != 8
    142 	 */
    143 	if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
    144 		sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    145 
    146 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
    147 
    148 	/* optional colour map callback */
    149 	sc->sc_cmcb = NULL;
    150 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
    151 		if (cmap_cb != 0)
    152 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
    153 	}
    154 
    155 	/* optional pmf callback */
    156 	sc->sc_pmfcb = NULL;
    157 	if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
    158 		if (pmf_cb != 0)
    159 			sc->sc_pmfcb = (void *)(vaddr_t)pmf_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 			sc->sc_backlight_on = 1;
    168 			aprint_naive_dev(sc->sc_dev,
    169 			    "enabling backlight control\n");
    170 			sc->sc_backlight_level =
    171 			    sc->sc_backlight->gpc_get_parameter(
    172 			    sc->sc_backlight->gpc_cookie);
    173 			if (console) {
    174 				pmf_event_register(sc->sc_dev,
    175 				    PMFE_DISPLAY_BRIGHTNESS_UP,
    176 				    genfb_brightness_up, TRUE);
    177 				pmf_event_register(sc->sc_dev,
    178 				    PMFE_DISPLAY_BRIGHTNESS_DOWN,
    179 				    genfb_brightness_down, TRUE);
    180 			}
    181 		}
    182 	}
    183 }
    184 
    185 int
    186 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
    187 {
    188 	struct wsemuldisplaydev_attach_args aa;
    189 	prop_dictionary_t dict;
    190 	struct rasops_info *ri;
    191 	uint16_t crow;
    192 	long defattr;
    193 	int i, j;
    194 	bool console;
    195 
    196 	dict = device_properties(sc->sc_dev);
    197 	prop_dictionary_get_bool(dict, "is_console", &console);
    198 
    199 	if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
    200 		crow = 0;
    201 	if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
    202 	    == false)
    203 		sc->sc_want_clear = true;
    204 
    205 	/* do not attach when we're not console */
    206 	if (!console) {
    207 		aprint_normal_dev(sc->sc_dev, "no console, unable to continue\n");
    208 		return -1;
    209 	}
    210 
    211 	aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
    212 	    "stride %d\n",
    213 	    sc->sc_fboffset ? (void *)(intptr_t)sc->sc_fboffset : sc->sc_fbaddr,
    214 	    sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
    215 
    216 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    217 		"default",
    218 		0, 0,
    219 		NULL,
    220 		8, 16,
    221 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    222 		NULL
    223 	};
    224 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    225 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    226 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
    227 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    228 
    229 #ifdef GENFB_SHADOWFB
    230 	sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
    231 	if (sc->sc_want_clear == false && sc->sc_shadowfb != NULL)
    232 		memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
    233 #endif
    234 
    235 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    236 	    &genfb_accessops);
    237 	sc->vd.init_screen = genfb_init_screen;
    238 
    239 	/* Do not print anything between this point and the screen
    240 	 * clear operation below.  Otherwise it will be lost. */
    241 
    242 	ri = &sc->sc_console_screen.scr_ri;
    243 
    244 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    245 	    &defattr);
    246 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    247 
    248 #ifdef SPLASHSCREEN
    249 /*
    250  * If system isn't going to go multiuser, or user has requested to see
    251  * boot text, don't render splash screen immediately
    252  */
    253 	if (DISABLESPLASH)
    254 #endif
    255 		vcons_redraw_screen(&sc->sc_console_screen);
    256 
    257 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    258 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    259 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    260 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    261 	wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
    262 	    defattr);
    263 
    264 	/* Clear the whole screen to bring it to a known state. */
    265 	if (sc->sc_want_clear)
    266 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
    267 
    268 	j = 0;
    269 	for (i = 0; i < min(1 << sc->sc_depth, 256); i++) {
    270 #ifndef SPLASHSCREEN
    271 		sc->sc_cmap_red[i] = rasops_cmap[j];
    272 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    273 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    274 		genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    275 		    rasops_cmap[j + 2]);
    276 		j += 3;
    277 #else
    278 		if(i >= SPLASH_CMAP_OFFSET &&
    279 		    i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
    280 			sc->sc_cmap_red[i] = _splash_header_data_cmap[j][0];
    281 			sc->sc_cmap_green[i] = _splash_header_data_cmap[j][1];
    282 			sc->sc_cmap_blue[i] = _splash_header_data_cmap[j][2];
    283 		} else {
    284 			sc->sc_cmap_red[i] = rasops_cmap[j];
    285 			sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    286 			sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    287 			genfb_putpalreg(sc, i, rasops_cmap[j],
    288 				rasops_cmap[j + 1],
    289 				rasops_cmap[j + 2]);
    290 		}
    291 		j += 3;
    292 #endif
    293 	}
    294 
    295 #ifdef SPLASHSCREEN
    296 	sc->sc_splash.si_depth = sc->sc_depth;
    297 	sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_bits;
    298 	sc->sc_splash.si_hwbits = sc->sc_fbaddr;
    299 	sc->sc_splash.si_width = sc->sc_width;
    300 	sc->sc_splash.si_height = sc->sc_height;
    301 	sc->sc_splash.si_stride = sc->sc_stride;
    302 	sc->sc_splash.si_fillrect = NULL;
    303 	if (!DISABLESPLASH)
    304 		splash_render(&sc->sc_splash, SPLASH_F_CENTER|SPLASH_F_FILL);
    305 #ifdef SPLASHSCREEN_PROGRESS
    306 	sc->sc_progress.sp_top = (sc->sc_height / 8) * 7;
    307 	sc->sc_progress.sp_width = (sc->sc_width / 4) * 3;
    308 	sc->sc_progress.sp_left = (sc->sc_width / 8) * 7;
    309 	sc->sc_progress.sp_height = 20;
    310 	sc->sc_progress.sp_state = -1;
    311 	sc->sc_progress.sp_si = &sc->sc_splash;
    312 	splash_progress_init(&sc->sc_progress);
    313 #endif
    314 #else
    315 	vcons_replay_msgbuf(&sc->sc_console_screen);
    316 #endif
    317 
    318 	if (genfb_softc == NULL)
    319 		genfb_softc = sc;
    320 
    321 	aa.console = console;
    322 	aa.scrdata = &sc->sc_screenlist;
    323 	aa.accessops = &genfb_accessops;
    324 	aa.accesscookie = &sc->vd;
    325 
    326 #ifdef GENFB_DISABLE_TEXT
    327 	if (!DISABLESPLASH)
    328 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    329 #endif
    330 
    331 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    332 
    333 	return 0;
    334 }
    335 
    336 static int
    337 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    338 	struct lwp *l)
    339 {
    340 	struct vcons_data *vd = v;
    341 	struct genfb_softc *sc = vd->cookie;
    342 	struct wsdisplay_fbinfo *wdf;
    343 	struct vcons_screen *ms = vd->active;
    344 	struct wsdisplay_param *param;
    345 	int new_mode, error;
    346 
    347 	switch (cmd) {
    348 		case WSDISPLAYIO_GINFO:
    349 			if (ms == NULL)
    350 				return ENODEV;
    351 			wdf = (void *)data;
    352 			wdf->height = ms->scr_ri.ri_height;
    353 			wdf->width = ms->scr_ri.ri_width;
    354 			wdf->depth = ms->scr_ri.ri_depth;
    355 			wdf->cmsize = 256;
    356 			return 0;
    357 
    358 		case WSDISPLAYIO_GETCMAP:
    359 			return genfb_getcmap(sc,
    360 			    (struct wsdisplay_cmap *)data);
    361 
    362 		case WSDISPLAYIO_PUTCMAP:
    363 			return genfb_putcmap(sc,
    364 			    (struct wsdisplay_cmap *)data);
    365 
    366 		case WSDISPLAYIO_LINEBYTES:
    367 			*(u_int *)data = sc->sc_stride;
    368 			return 0;
    369 
    370 		case WSDISPLAYIO_SMODE:
    371 			new_mode = *(int *)data;
    372 
    373 			/* notify the bus backend */
    374 			error = 0;
    375 			if (sc->sc_ops.genfb_ioctl)
    376 				error = sc->sc_ops.genfb_ioctl(sc, vs,
    377 					    cmd, data, flag, l);
    378 			if (error)
    379 				return error;
    380 
    381 			if (new_mode != sc->sc_mode) {
    382 				sc->sc_mode = new_mode;
    383 				if (new_mode == WSDISPLAYIO_MODE_EMUL) {
    384 					genfb_restore_palette(sc);
    385 					vcons_redraw_screen(ms);
    386 				}
    387 			}
    388 			return 0;
    389 		case WSDISPLAYIO_SSPLASH:
    390 #if defined(SPLASHSCREEN)
    391 			if(*(int *)data == 1) {
    392 				SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    393 				splash_render(&sc->sc_splash,
    394 						SPLASH_F_CENTER|SPLASH_F_FILL);
    395 #if defined(SPLASHSCREEN_PROGRESS)
    396 				sc->sc_progress.sp_running = 1;
    397 #endif
    398 			} else {
    399 				SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
    400 #if defined(SPLASHSCREEN_PROGRESS)
    401 				sc->sc_progress.sp_running = 0;
    402 #endif
    403 			}
    404 			vcons_redraw_screen(ms);
    405 			return 0;
    406 #else
    407 			return ENODEV;
    408 #endif
    409 		case WSDISPLAYIO_SPROGRESS:
    410 #if defined(SPLASHSCREEN) && defined(SPLASHSCREEN_PROGRESS)
    411 			sc->sc_progress.sp_force = 1;
    412 			splash_progress_update(&sc->sc_progress);
    413 			sc->sc_progress.sp_force = 0;
    414 			vcons_redraw_screen(ms);
    415 			return 0;
    416 #else
    417 			return ENODEV;
    418 #endif
    419 		case WSDISPLAYIO_GETPARAM:
    420 			param = (struct wsdisplay_param *)data;
    421 			if (sc->sc_backlight == NULL)
    422 				return EPASSTHROUGH;
    423 			switch (param->param) {
    424 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    425 				param->min = 0;
    426 				param->max = 255;
    427 				param->curval = sc->sc_backlight_level;
    428 				return 0;
    429 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    430 				param->min = 0;
    431 				param->max = 1;
    432 				param->curval = sc->sc_backlight_on;
    433 				return 0;
    434 			}
    435 			return EPASSTHROUGH;
    436 
    437 		case WSDISPLAYIO_SETPARAM:
    438 			param = (struct wsdisplay_param *)data;
    439 			if (sc->sc_backlight == NULL)
    440 				return EPASSTHROUGH;
    441 			switch (param->param) {
    442 			case WSDISPLAYIO_PARAM_BRIGHTNESS:
    443 				genfb_set_backlight(sc, param->curval);
    444 				return 0;
    445 			case WSDISPLAYIO_PARAM_BACKLIGHT:
    446 				genfb_switch_backlight(sc,  param->curval);
    447 				return 0;
    448 			}
    449 			return EPASSTHROUGH;
    450 		default:
    451 			if (sc->sc_ops.genfb_ioctl)
    452 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
    453 				    data, flag, l);
    454 	}
    455 	return EPASSTHROUGH;
    456 }
    457 
    458 static paddr_t
    459 genfb_mmap(void *v, void *vs, off_t offset, int prot)
    460 {
    461 	struct vcons_data *vd = v;
    462 	struct genfb_softc *sc = vd->cookie;
    463 
    464 	if (sc->sc_ops.genfb_mmap)
    465 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
    466 
    467 	return -1;
    468 }
    469 
    470 static void
    471 genfb_init_screen(void *cookie, struct vcons_screen *scr,
    472     int existing, long *defattr)
    473 {
    474 	struct genfb_softc *sc = cookie;
    475 	struct rasops_info *ri = &scr->scr_ri;
    476 
    477 	ri->ri_depth = sc->sc_depth;
    478 	ri->ri_width = sc->sc_width;
    479 	ri->ri_height = sc->sc_height;
    480 	ri->ri_stride = sc->sc_stride;
    481 	ri->ri_flg = RI_CENTER;
    482 	if (sc->sc_want_clear)
    483 		ri->ri_flg |= RI_FULLCLEAR;
    484 
    485 #ifdef GENFB_SHADOWFB
    486 	if (sc->sc_shadowfb != NULL) {
    487 
    488 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
    489 		ri->ri_bits = (char *)sc->sc_shadowfb;
    490 	} else
    491 #endif
    492 	{
    493 		ri->ri_bits = (char *)sc->sc_fbaddr;
    494 		scr->scr_flags |= VCONS_DONT_READ;
    495 	}
    496 
    497 	if (existing && sc->sc_want_clear) {
    498 		ri->ri_flg |= RI_CLEAR;
    499 	}
    500 
    501 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    502 	ri->ri_caps = WSSCREEN_WSCOLORS;
    503 
    504 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    505 		    sc->sc_width / ri->ri_font->fontwidth);
    506 
    507 	/* TODO: actually center output */
    508 	ri->ri_hw = scr;
    509 
    510 #ifdef GENFB_DISABLE_TEXT
    511 	if (scr == &sc->sc_console_screen && !DISABLESPLASH)
    512 		SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
    513 #endif
    514 }
    515 
    516 static int
    517 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    518 {
    519 	u_char *r, *g, *b;
    520 	u_int index = cm->index;
    521 	u_int count = cm->count;
    522 	int i, error;
    523 	u_char rbuf[256], gbuf[256], bbuf[256];
    524 
    525 #ifdef GENFB_DEBUG
    526 	aprint_debug("putcmap: %d %d\n",index, count);
    527 #endif
    528 	if (cm->index >= 256 || cm->count > 256 ||
    529 	    (cm->index + cm->count) > 256)
    530 		return EINVAL;
    531 	error = copyin(cm->red, &rbuf[index], count);
    532 	if (error)
    533 		return error;
    534 	error = copyin(cm->green, &gbuf[index], count);
    535 	if (error)
    536 		return error;
    537 	error = copyin(cm->blue, &bbuf[index], count);
    538 	if (error)
    539 		return error;
    540 
    541 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    542 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    543 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    544 
    545 	r = &sc->sc_cmap_red[index];
    546 	g = &sc->sc_cmap_green[index];
    547 	b = &sc->sc_cmap_blue[index];
    548 
    549 	for (i = 0; i < count; i++) {
    550 		genfb_putpalreg(sc, index, *r, *g, *b);
    551 		index++;
    552 		r++, g++, b++;
    553 	}
    554 	return 0;
    555 }
    556 
    557 static int
    558 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    559 {
    560 	u_int index = cm->index;
    561 	u_int count = cm->count;
    562 	int error;
    563 
    564 	if (index >= 255 || count > 256 || index + count > 256)
    565 		return EINVAL;
    566 
    567 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    568 	if (error)
    569 		return error;
    570 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    571 	if (error)
    572 		return error;
    573 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    574 	if (error)
    575 		return error;
    576 
    577 	return 0;
    578 }
    579 
    580 void
    581 genfb_restore_palette(struct genfb_softc *sc)
    582 {
    583 	int i;
    584 
    585 	if (sc->sc_depth <= 8) {
    586 		for (i = 0; i < (1 << sc->sc_depth); i++) {
    587 			genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    588 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    589 		}
    590 	}
    591 }
    592 
    593 static int
    594 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    595     uint8_t b)
    596 {
    597 
    598 	if (sc->sc_cmcb) {
    599 
    600 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
    601 		    idx, r, g, b);
    602 	}
    603 	return 0;
    604 }
    605 
    606 void
    607 genfb_cnattach(void)
    608 {
    609 	genfb_cnattach_called = 1;
    610 }
    611 
    612 void
    613 genfb_disable(void)
    614 {
    615 	genfb_enabled = 0;
    616 }
    617 
    618 int
    619 genfb_is_console(void)
    620 {
    621 	return genfb_cnattach_called;
    622 }
    623 
    624 int
    625 genfb_is_enabled(void)
    626 {
    627 	return genfb_enabled;
    628 }
    629 
    630 int
    631 genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
    632 {
    633 	struct genfb_softc *sc = genfb_softc;
    634 
    635 	if (sc && sc->sc_ops.genfb_borrow)
    636 		return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
    637 	return 0;
    638 }
    639 
    640 static void
    641 genfb_set_backlight(struct genfb_softc *sc, int level)
    642 {
    643 
    644 	KASSERT(sc->sc_backlight != NULL);
    645 
    646 	/*
    647 	 * should we do nothing when backlight is off, should we just store the
    648 	 * level and use it when turning back on or should we just flip sc_bl_on
    649 	 * and turn the backlight on?
    650 	 * For now turn it on so a crashed screensaver can't get the user stuck
    651 	 * with a dark screen as long as hotkeys work
    652 	 */
    653 	if (level > 255) level = 255;
    654 	if (level < 0) level = 0;
    655 	if (level == sc->sc_backlight_level)
    656 		return;
    657 	sc->sc_backlight_level = level;
    658 	if (sc->sc_backlight_on == 0)
    659 		sc->sc_backlight_on = 1;
    660 	sc->sc_backlight->gpc_set_parameter(
    661 	    sc->sc_backlight->gpc_cookie, level);
    662 }
    663 
    664 static void
    665 genfb_switch_backlight(struct genfb_softc *sc, int on)
    666 {
    667 	int level;
    668 
    669 	KASSERT(sc->sc_backlight != NULL);
    670 
    671 	if (on == sc->sc_backlight_on)
    672 		return;
    673 	sc->sc_backlight_on = on;
    674 	level = on ? sc->sc_backlight_level : 0;
    675 	sc->sc_backlight->gpc_set_parameter(
    676 	    sc->sc_backlight->gpc_cookie, level);
    677 }
    678 
    679 
    680 static void
    681 genfb_brightness_up(device_t dev)
    682 {
    683 	struct genfb_softc *sc = device_private(dev);
    684 
    685 	genfb_set_backlight(sc, sc->sc_backlight_level + 8);
    686 }
    687 
    688 static void
    689 genfb_brightness_down(device_t dev)
    690 {
    691 	struct genfb_softc *sc = device_private(dev);
    692 
    693 	genfb_set_backlight(sc, sc->sc_backlight_level - 8);
    694 }
    695