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