Home | History | Annotate | Line # | Download | only in wsfb
genfb.c revision 1.5
      1 /*	$NetBSD: genfb.c,v 1.5 2007/08/04 23:51:37 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2007 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  *    contributors may be used to endorse or promote products derived
     17  *    from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.5 2007/08/04 23:51:37 macallan Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/proc.h>
     40 #include <sys/mutex.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/kernel.h>
     43 #include <sys/systm.h>
     44 #include <sys/malloc.h>
     45 
     46 #include <dev/wscons/wsconsio.h>
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/rasops/rasops.h>
     49 #include <dev/wsfont/wsfont.h>
     50 
     51 #include <dev/wscons/wsdisplay_vconsvar.h>
     52 
     53 #include <dev/wsfb/genfbvar.h>
     54 
     55 #include "opt_genfb.h"
     56 #include "opt_wsfb.h"
     57 
     58 static int	genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     59 static paddr_t	genfb_mmap(void *, void *, off_t, int);
     60 static void	genfb_init_screen(void *, struct vcons_screen *, int, long *);
     61 
     62 static int	genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     63 static int 	genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
     64 static void	genfb_restore_palette(struct genfb_softc *);
     65 static int 	genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
     66 			    uint8_t, uint8_t);
     67 
     68 extern const u_char rasops_cmap[768];
     69 
     70 struct wsdisplay_accessops genfb_accessops = {
     71 	genfb_ioctl,
     72 	genfb_mmap,
     73 	NULL,	/* alloc_screen */
     74 	NULL,	/* free_screen */
     75 	NULL,	/* show_screen */
     76 	NULL, 	/* load_font */
     77 	NULL,	/* pollc */
     78 	NULL	/* scroll */
     79 };
     80 
     81 void
     82 genfb_init(struct genfb_softc *sc)
     83 {
     84 	prop_dictionary_t dict;
     85 	uint64_t cmap_cb;
     86 	uint32_t fboffset;
     87 
     88 	dict = device_properties(&sc->sc_dev);
     89 #ifdef GENFB_DEBUG
     90 	printf(prop_dictionary_externalize(dict));
     91 #endif
     92 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width))
     93 		panic("no width property");
     94 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height))
     95 		panic("no height property");
     96 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth))
     97 		panic("no depth property");
     98 
     99 	/* XXX should be a 64bit value */
    100 	if (!prop_dictionary_get_uint32(dict, "address", &fboffset))
    101 		panic("no address property");
    102 	sc->sc_fboffset = fboffset;
    103 
    104 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
    105 		sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
    106 	sc->sc_fbsize = sc->sc_width * sc->sc_stride;
    107 
    108 	/* optional colour map callback */
    109 	sc->sc_cmcb = NULL;
    110 	if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
    111 		if (cmap_cb != 0)
    112 			sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
    113 	}
    114 }
    115 
    116 int
    117 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
    118 {
    119 	struct wsemuldisplaydev_attach_args aa;
    120 	prop_dictionary_t dict;
    121 	struct rasops_info *ri;
    122 	long defattr;
    123 	int console, i, j;
    124 
    125 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    126 		"default",
    127 		0, 0,
    128 		NULL,
    129 		8, 16,
    130 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    131 		NULL
    132 	};
    133 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    134 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    135 	memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
    136 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    137 
    138 	sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_WAITOK);
    139 
    140 	dict = device_properties(&sc->sc_dev);
    141 
    142 	prop_dictionary_get_bool(dict, "is_console", &console);
    143 
    144 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    145 	    &genfb_accessops);
    146 	sc->vd.init_screen = genfb_init_screen;
    147 
    148 	ri = &sc->sc_console_screen.scr_ri;
    149 
    150 	if (console) {
    151 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    152 		    &defattr);
    153 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    154 
    155 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    156 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    157 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    158 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    159 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    160 		    defattr);
    161 	} else {
    162 		/*
    163 		 * since we're not the console we can postpone the rest
    164 		 * until someone actually allocates a screen for us
    165 		 */
    166 	}
    167 
    168 	j = 0;
    169 	for (i = 0; i < (1 << (sc->sc_depth - 1)); i++) {
    170 
    171 		sc->sc_cmap_red[i] = rasops_cmap[j];
    172 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    173 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    174 		genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    175 		    rasops_cmap[j + 2]);
    176 		j += 3;
    177 	}
    178 
    179 	aa.console = console;
    180 	aa.scrdata = &sc->sc_screenlist;
    181 	aa.accessops = &genfb_accessops;
    182 	aa.accesscookie = &sc->vd;
    183 
    184 	config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
    185 
    186 	return 0;
    187 }
    188 
    189 static int
    190 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    191 	struct lwp *l)
    192 {
    193 	struct vcons_data *vd = v;
    194 	struct genfb_softc *sc = vd->cookie;
    195 	struct wsdisplay_fbinfo *wdf;
    196 	struct vcons_screen *ms = vd->active;
    197 
    198 	switch (cmd) {
    199 
    200 		case WSDISPLAYIO_GINFO:
    201 			wdf = (void *)data;
    202 			wdf->height = ms->scr_ri.ri_height;
    203 			wdf->width = ms->scr_ri.ri_width;
    204 			wdf->depth = ms->scr_ri.ri_depth;
    205 			wdf->cmsize = 256;
    206 			return 0;
    207 
    208 		case WSDISPLAYIO_GETCMAP:
    209 			return genfb_getcmap(sc,
    210 			    (struct wsdisplay_cmap *)data);
    211 
    212 		case WSDISPLAYIO_PUTCMAP:
    213 			return genfb_putcmap(sc,
    214 			    (struct wsdisplay_cmap *)data);
    215 
    216 		case WSDISPLAYIO_LINEBYTES:
    217 			*(u_int *)data = sc->sc_stride;
    218 			return 0;
    219 
    220 		case WSDISPLAYIO_SMODE:
    221 			{
    222 				int new_mode = *(int*)data;
    223 				if (new_mode != sc->sc_mode) {
    224 					sc->sc_mode = new_mode;
    225 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    226 						genfb_restore_palette(sc);
    227 						vcons_redraw_screen(ms);
    228 					}
    229 				}
    230 			}
    231 			return 0;
    232 		default:
    233 			if (sc->sc_ops.genfb_ioctl)
    234 				return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
    235 				    data, flag, l);
    236 	}
    237 	return EPASSTHROUGH;
    238 }
    239 
    240 static paddr_t
    241 genfb_mmap(void *v, void *vs, off_t offset, int prot)
    242 {
    243 	struct vcons_data *vd = v;
    244 	struct genfb_softc *sc = vd->cookie;
    245 
    246 	if (sc->sc_ops.genfb_mmap)
    247 		return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
    248 
    249 	return -1;
    250 }
    251 
    252 static void
    253 genfb_init_screen(void *cookie, struct vcons_screen *scr,
    254     int existing, long *defattr)
    255 {
    256 	struct genfb_softc *sc = cookie;
    257 	struct rasops_info *ri = &scr->scr_ri;
    258 
    259 	ri->ri_depth = sc->sc_depth;
    260 	ri->ri_width = sc->sc_width;
    261 	ri->ri_height = sc->sc_height;
    262 	ri->ri_stride = sc->sc_stride;
    263 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    264 
    265 	if (sc->sc_shadowfb != NULL) {
    266 
    267 		ri->ri_hwbits = (char *)sc->sc_fbaddr;
    268 		ri->ri_bits = (char *)sc->sc_shadowfb;
    269 	} else
    270 		ri->ri_bits = (char *)sc->sc_fbaddr;
    271 
    272 	if (existing) {
    273 		ri->ri_flg |= RI_CLEAR;
    274 	}
    275 
    276 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    277 	ri->ri_caps = WSSCREEN_WSCOLORS;
    278 
    279 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    280 		    sc->sc_width / ri->ri_font->fontwidth);
    281 
    282 	ri->ri_hw = scr;
    283 }
    284 
    285 static int
    286 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    287 {
    288 	u_char *r, *g, *b;
    289 	u_int index = cm->index;
    290 	u_int count = cm->count;
    291 	int i, error;
    292 	u_char rbuf[256], gbuf[256], bbuf[256];
    293 
    294 #ifdef GENFB_DEBUG
    295 	aprint_debug("putcmap: %d %d\n",index, count);
    296 #endif
    297 	if (cm->index >= 256 || cm->count > 256 ||
    298 	    (cm->index + cm->count) > 256)
    299 		return EINVAL;
    300 	error = copyin(cm->red, &rbuf[index], count);
    301 	if (error)
    302 		return error;
    303 	error = copyin(cm->green, &gbuf[index], count);
    304 	if (error)
    305 		return error;
    306 	error = copyin(cm->blue, &bbuf[index], count);
    307 	if (error)
    308 		return error;
    309 
    310 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    311 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    312 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    313 
    314 	r = &sc->sc_cmap_red[index];
    315 	g = &sc->sc_cmap_green[index];
    316 	b = &sc->sc_cmap_blue[index];
    317 
    318 	for (i = 0; i < count; i++) {
    319 		genfb_putpalreg(sc, index, *r, *g, *b);
    320 		index++;
    321 		r++, g++, b++;
    322 	}
    323 	return 0;
    324 }
    325 
    326 static int
    327 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
    328 {
    329 	u_int index = cm->index;
    330 	u_int count = cm->count;
    331 	int error;
    332 
    333 	if (index >= 255 || count > 256 || index + count > 256)
    334 		return EINVAL;
    335 
    336 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    337 	if (error)
    338 		return error;
    339 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    340 	if (error)
    341 		return error;
    342 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    343 	if (error)
    344 		return error;
    345 
    346 	return 0;
    347 }
    348 
    349 static void
    350 genfb_restore_palette(struct genfb_softc *sc)
    351 {
    352 	int i;
    353 
    354 	for (i = 0; i < (1 << (sc->sc_depth - 1)); i++) {
    355 		genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
    356 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    357 	}
    358 }
    359 
    360 static int
    361 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    362     uint8_t b)
    363 {
    364 
    365 	if (sc->sc_cmcb) {
    366 
    367 		sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
    368 		    idx, r, g, b);
    369 	}
    370 	return 0;
    371 }
    372 
    373