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