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