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