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