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