Home | History | Annotate | Line # | Download | only in dev
vncfb.c revision 1.5
      1 /* $NetBSD: vncfb.c,v 1.5 2011/12/30 12:54:41 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *        This product includes software developed by Jared D. McNeill.
     18  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  *    contributors may be used to endorse or promote products derived
     20  *    from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include "opt_wsemul.h"
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: vncfb.c,v 1.5 2011/12/30 12:54:41 jmcneill Exp $");
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/kernel.h>
     43 #include <sys/device.h>
     44 #include <sys/kmem.h>
     45 
     46 #include <dev/wscons/wsconsio.h>
     47 
     48 #include <dev/wscons/wsdisplayvar.h>
     49 #include <dev/wsfont/wsfont.h>
     50 #include <dev/rasops/rasops.h>
     51 #include <dev/wscons/wsdisplay_vconsvar.h>
     52 
     53 #include <dev/wscons/wskbdvar.h>
     54 #include <dev/wscons/wsksymdef.h>
     55 #include <dev/wscons/wsksymvar.h>
     56 
     57 #include <machine/mainbus.h>
     58 #include <machine/thunk.h>
     59 
     60 struct vncfb_fbops {
     61 	void	(*copycols)(void *, int, int, int, int);
     62 	void	(*erasecols)(void *, int, int, int, long);
     63 	void	(*copyrows)(void *, int, int, int);
     64 	void	(*eraserows)(void *, int, int, long);
     65 	void	(*putchar)(void *, int, int, u_int, long);
     66 	void	(*cursor)(void *, int, int, int);
     67 };
     68 
     69 struct vncfb_softc {
     70 	device_t		sc_dev;
     71 	device_t		sc_wskbddev;
     72 	thunk_rfb_t		sc_rfb;
     73 	unsigned int		sc_width;
     74 	unsigned int		sc_height;
     75 	unsigned int		sc_depth;
     76 	int			sc_mode;
     77 	uint8_t *		sc_framebuf;
     78 	struct vcons_data	sc_vd;
     79 	struct vncfb_fbops	sc_ops;
     80 
     81 	int			sc_kbd_enable;
     82 
     83 	void			*sc_ih;
     84 	void			*sc_sih;
     85 };
     86 
     87 static int	vncfb_match(device_t, cfdata_t, void *);
     88 static void	vncfb_attach(device_t, device_t, void *);
     89 
     90 CFATTACH_DECL_NEW(vncfb, sizeof(struct vncfb_softc),
     91     vncfb_match, vncfb_attach, NULL, NULL);
     92 
     93 static void	vncfb_putchar(void *, int, int, u_int, long);
     94 static void	vncfb_copycols(void *, int, int, int, int);
     95 static void	vncfb_erasecols(void *, int, int, int, long);
     96 static void	vncfb_copyrows(void *, int, int, int);
     97 static void	vncfb_eraserows(void *, int, int, long);
     98 static void	vncfb_cursor(void *, int, int, int);
     99 
    100 static int	vncfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
    101 static paddr_t	vncfb_mmap(void *, void *, off_t, int);
    102 
    103 static void	vncfb_init_screen(void *, struct vcons_screen *, int, long *);
    104 
    105 static void	vncfb_update(struct vncfb_softc *, int, int, int, int);
    106 static int	vncfb_intr(void *);
    107 static void	vncfb_softintr(void *);
    108 
    109 static int	vncfb_kbd_enable(void *, int);
    110 static void	vncfb_kbd_set_leds(void *, int);
    111 static int	vncfb_kbd_ioctl(void *, u_long, void *, int, lwp_t *);
    112 
    113 static void	vncfb_kbd_cngetc(void *, u_int *, int *);
    114 static void	vncfb_kbd_cnpollc(void *, int);
    115 static void	vncfb_kbd_bell(void *, u_int, u_int, u_int);
    116 
    117 static struct vcons_screen vncfb_console_screen;
    118 
    119 static struct wsscreen_descr vncfb_defaultscreen = {
    120 	.name = "default",
    121 	.fontwidth = 8,
    122 	.fontheight = 16,
    123 	.capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    124 };
    125 
    126 static const struct wsscreen_descr *vncfb_screens[] = {
    127 	&vncfb_defaultscreen,
    128 };
    129 
    130 static struct wsscreen_list vncfb_screenlist = {
    131 	.screens = vncfb_screens,
    132 	.nscreens = __arraycount(vncfb_screens),
    133 };
    134 
    135 static struct wsdisplay_accessops vncfb_accessops = {
    136 	.ioctl = vncfb_ioctl,
    137 	.mmap = vncfb_mmap,
    138 };
    139 
    140 extern const struct wscons_keydesc vnckbd_keydesctab[];
    141 
    142 static const struct wskbd_mapdata vncfb_keymapdata = {
    143 	vnckbd_keydesctab,
    144 	KB_US,
    145 };
    146 
    147 static struct wskbd_accessops vncfb_kbd_accessops = {
    148 	vncfb_kbd_enable,
    149 	vncfb_kbd_set_leds,
    150 	vncfb_kbd_ioctl,
    151 };
    152 
    153 static const struct wskbd_consops vncfb_kbd_consops = {
    154 	vncfb_kbd_cngetc,
    155 	vncfb_kbd_cnpollc,
    156 	vncfb_kbd_bell,
    157 };
    158 
    159 static int
    160 vncfb_match(device_t parent, cfdata_t match, void *priv)
    161 {
    162 	struct thunkbus_attach_args *taa = priv;
    163 
    164 	return taa->taa_type == THUNKBUS_TYPE_VNCFB;
    165 }
    166 
    167 static void
    168 vncfb_attach(device_t parent, device_t self, void *priv)
    169 {
    170 	struct vncfb_softc *sc = device_private(self);
    171 	struct thunkbus_attach_args *taa = priv;
    172 	struct wsemuldisplaydev_attach_args waa;
    173 	struct wskbddev_attach_args kaa;
    174 	struct rasops_info *ri;
    175 	unsigned long defattr;
    176 
    177 	sc->sc_dev = self;
    178 	sc->sc_width = taa->u.vnc.width;
    179 	sc->sc_height = taa->u.vnc.height;
    180 	sc->sc_depth = 32;
    181 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    182 #if notyet
    183 	sc->sc_sockfd = thunk_vnc_open_socket(taa->u.vnc.port);
    184 	if (sc->sc_sockfd == -1)
    185 		panic("couldn't open VNC socket");
    186 #endif
    187 
    188 	sc->sc_framebuf = kmem_zalloc(sc->sc_width * sc->sc_height *
    189 	    (sc->sc_depth / 8), KM_SLEEP);
    190 	KASSERT(sc->sc_framebuf != NULL);
    191 
    192 	aprint_naive("\n");
    193 	aprint_normal(": %ux%u %ubpp (port %u)\n",
    194 	    sc->sc_width, sc->sc_height, sc->sc_depth, taa->u.vnc.port);
    195 
    196 	sc->sc_rfb.width = sc->sc_width;
    197 	sc->sc_rfb.height = sc->sc_height;
    198 	sc->sc_rfb.depth = sc->sc_depth;
    199 	sc->sc_rfb.framebuf = sc->sc_framebuf;
    200 	snprintf(sc->sc_rfb.name, sizeof(sc->sc_rfb.name),
    201 	    "NetBSD/usermode %d.%d.%d",
    202 	    __NetBSD_Version__ / 100000000,
    203 	    (__NetBSD_Version__ / 1000000) % 100,
    204 	    (__NetBSD_Version__ / 100) % 100);
    205 	if (thunk_rfb_open(&sc->sc_rfb, taa->u.vnc.port) != 0)
    206 		panic("couldn't open rfb server");
    207 
    208 	sc->sc_sih = softint_establish(SOFTINT_SERIAL, vncfb_softintr, sc);
    209 	sc->sc_ih = sigio_intr_establish(vncfb_intr, sc);
    210 
    211 	vcons_init(&sc->sc_vd, sc, &vncfb_defaultscreen, &vncfb_accessops);
    212 	sc->sc_vd.init_screen = vncfb_init_screen;
    213 
    214 	ri = &vncfb_console_screen.scr_ri;
    215 	vcons_init_screen(&sc->sc_vd, &vncfb_console_screen, 1, &defattr);
    216 	vncfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    217 	vncfb_defaultscreen.textops = &ri->ri_ops;
    218 	vncfb_defaultscreen.capabilities = ri->ri_caps;
    219 	vncfb_defaultscreen.nrows = ri->ri_rows;
    220 	vncfb_defaultscreen.ncols = ri->ri_cols;
    221 	wsdisplay_cnattach(&vncfb_defaultscreen, ri, 0, 0, defattr);
    222 
    223 	vcons_replay_msgbuf(&vncfb_console_screen);
    224 
    225 	waa.console = true;
    226 	waa.scrdata = &vncfb_screenlist;
    227 	waa.accessops = &vncfb_accessops;
    228 	waa.accesscookie = &sc->sc_vd;
    229 
    230 	config_found(self, &waa, wsemuldisplaydevprint);
    231 
    232 	wskbd_cnattach(&vncfb_kbd_consops, sc, &vncfb_keymapdata);
    233 
    234 	kaa.console = true;
    235 	kaa.keymap = &vncfb_keymapdata;
    236 	kaa.accessops = &vncfb_kbd_accessops;
    237 	kaa.accesscookie = sc;
    238 
    239 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &kaa,
    240 	    wskbddevprint);
    241 }
    242 
    243 static void
    244 vncfb_init_screen(void *priv, struct vcons_screen *scr, int existing,
    245     long *defattr)
    246 {
    247 	struct vncfb_softc *sc = priv;
    248 	struct vncfb_fbops *ops = &sc->sc_ops;
    249 	struct rasops_info *ri = &scr->scr_ri;
    250 
    251 	ri->ri_width = sc->sc_width;
    252 	ri->ri_height = sc->sc_height;
    253 	ri->ri_depth = sc->sc_depth;
    254 	ri->ri_stride = sc->sc_width * ri->ri_depth / 8;
    255 	ri->ri_bits = sc->sc_framebuf;
    256 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    257 	if (existing)
    258 		ri->ri_flg |= RI_CLEAR;
    259 
    260 	rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
    261 	ri->ri_caps = WSSCREEN_WSCOLORS;
    262 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    263 	    sc->sc_width / ri->ri_font->fontwidth);
    264 
    265 	ri->ri_hw = scr;
    266 
    267 	ops->putchar = ri->ri_ops.putchar;
    268 	ops->copyrows = ri->ri_ops.copyrows;
    269 	ops->eraserows = ri->ri_ops.eraserows;
    270 	ops->copycols = ri->ri_ops.copycols;
    271 	ops->erasecols = ri->ri_ops.erasecols;
    272 	ops->cursor = ri->ri_ops.cursor;
    273 
    274 	ri->ri_ops.copyrows = vncfb_copyrows;
    275 	ri->ri_ops.copycols = vncfb_copycols;
    276 	ri->ri_ops.eraserows = vncfb_eraserows;
    277 	ri->ri_ops.erasecols = vncfb_erasecols;
    278 	ri->ri_ops.putchar = vncfb_putchar;
    279 	ri->ri_ops.cursor = vncfb_cursor;
    280 }
    281 
    282 static void
    283 vncfb_putchar(void *priv, int row, int col, u_int c, long attr)
    284 {
    285 	struct rasops_info *ri = priv;
    286 	struct vcons_screen *scr = ri->ri_hw;
    287 	struct vncfb_softc *sc = scr->scr_cookie;
    288 	struct vncfb_fbops *ops = &sc->sc_ops;
    289 	int x, y, w, h;
    290 
    291 	ops->putchar(ri, row, col, c, attr);
    292 
    293 	x = ri->ri_xorigin + (col * ri->ri_font->fontwidth);
    294 	y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
    295 	w = ri->ri_font->fontwidth;
    296 	h = ri->ri_font->fontheight;
    297 
    298 	vncfb_update(sc, x, y, w, h);
    299 }
    300 
    301 static void
    302 vncfb_copycols(void *priv, int row, int srccol, int dstcol, int ncols)
    303 {
    304 	struct rasops_info *ri = priv;
    305 	struct vcons_screen *scr = ri->ri_hw;
    306 	struct vncfb_softc *sc = scr->scr_cookie;
    307 	struct vncfb_fbops *ops = &sc->sc_ops;
    308 	int x, y, w, h;
    309 
    310 	ops->copycols(ri, row, srccol, dstcol, ncols);
    311 
    312 	y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
    313 	h = ri->ri_font->fontheight;
    314 	if (srccol < dstcol) {
    315 		x = ri->ri_xorigin + (srccol * ri->ri_font->fontwidth);
    316 		w = (dstcol - srccol) * ri->ri_font->fontwidth;
    317 
    318 	} else {
    319 		x = ri->ri_xorigin + (dstcol * ri->ri_font->fontwidth);
    320 		w = (srccol - dstcol) * ri->ri_font->fontwidth;
    321 	}
    322 
    323 	vncfb_update(sc, x, y, w, h);
    324 }
    325 
    326 static void
    327 vncfb_erasecols(void *priv, int row, int startcol, int ncols, long fillattr)
    328 {
    329 	struct rasops_info *ri = priv;
    330 	struct vcons_screen *scr = ri->ri_hw;
    331 	struct vncfb_softc *sc = scr->scr_cookie;
    332 	struct vncfb_fbops *ops = &sc->sc_ops;
    333 	int x, y, w, h;
    334 
    335 	ops->erasecols(ri, row, startcol, ncols, fillattr);
    336 
    337 	y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
    338 	h = ri->ri_font->fontheight;
    339 	x = ri->ri_xorigin + (startcol * ri->ri_font->fontwidth);
    340 	w = ncols * ri->ri_font->fontwidth;
    341 
    342 	vncfb_update(sc, x, y, w, h);
    343 }
    344 
    345 static void
    346 vncfb_copyrows(void *priv, int srcrow, int dstrow, int nrows)
    347 {
    348 	struct rasops_info *ri = priv;
    349 	struct vcons_screen *scr = ri->ri_hw;
    350 	struct vncfb_softc *sc = scr->scr_cookie;
    351 	struct vncfb_fbops *ops = &sc->sc_ops;
    352 	int x, y, w, h;
    353 
    354 	ops->copyrows(ri, srcrow, dstrow, nrows);
    355 
    356 	x = ri->ri_xorigin;
    357 	w = ri->ri_width;
    358 	if (srcrow < dstrow) {
    359 		y = ri->ri_yorigin + (srcrow * ri->ri_font->fontheight);
    360 		h = (nrows + (dstrow - srcrow)) * ri->ri_font->fontheight;
    361 	} else {
    362 		y = ri->ri_yorigin + (dstrow * ri->ri_font->fontheight);
    363 		h = (nrows + (srcrow - dstrow)) * ri->ri_font->fontheight;
    364 	}
    365 
    366 	vncfb_update(sc, x, y, w, h);
    367 }
    368 
    369 static void
    370 vncfb_eraserows(void *priv, int row, int nrows, long fillattr)
    371 {
    372 	struct rasops_info *ri = priv;
    373 	struct vcons_screen *scr = ri->ri_hw;
    374 	struct vncfb_softc *sc = scr->scr_cookie;
    375 	struct vncfb_fbops *ops = &sc->sc_ops;
    376 	int x, y, w, h;
    377 
    378 	ops->eraserows(ri, row, nrows, fillattr);
    379 
    380 	y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
    381 	h = nrows * ri->ri_font->fontheight;
    382 	x = ri->ri_xorigin;
    383 	w = ri->ri_width;
    384 
    385 	vncfb_update(sc, x, y, w, h);
    386 }
    387 
    388 static void
    389 vncfb_cursor(void *priv, int on, int row, int col)
    390 {
    391 	struct rasops_info *ri = priv;
    392 	struct vcons_screen *scr = ri->ri_hw;
    393 	struct vncfb_softc *sc = scr->scr_cookie;
    394 	struct vncfb_fbops *ops = &sc->sc_ops;
    395 	int ox, oy, x, y, w, h;
    396 
    397 	w = ri->ri_font->fontwidth;
    398 	h = ri->ri_font->fontheight;
    399 
    400 	ox = ri->ri_ccol * w + ri->ri_xorigin;
    401 	oy = ri->ri_crow * h + ri->ri_yorigin;
    402 
    403 	ops->cursor(ri, on, row, col);
    404 
    405 	x = ri->ri_ccol * w + ri->ri_xorigin;
    406 	y = ri->ri_crow * h + ri->ri_yorigin;
    407 
    408 	vncfb_update(sc, ox, oy, w, h);
    409 	vncfb_update(sc, x, y, w, h);
    410 }
    411 
    412 static int
    413 vncfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
    414 {
    415 	struct vcons_data *vd = v;
    416 	struct vncfb_softc *sc = vd->cookie;
    417 	struct wsdisplay_fbinfo *wdf;
    418 	struct vcons_screen *ms = vd->active;
    419 	int new_mode;
    420 
    421 	switch (cmd) {
    422 	case WSDISPLAYIO_GTYPE:
    423 		*(u_int *)data = WSDISPLAY_TYPE_VNC;
    424 		return 0;
    425 	case WSDISPLAYIO_GINFO:
    426 		wdf = data;
    427 		wdf->height = ms->scr_ri.ri_height;
    428 		wdf->width = ms->scr_ri.ri_width;
    429 		wdf->depth = ms->scr_ri.ri_depth;
    430 		wdf->cmsize = 256;
    431 		return 0;
    432 	case WSDISPLAYIO_SMODE:
    433 		new_mode = *(int *)data;
    434 		if (sc->sc_mode != new_mode) {
    435 			sc->sc_mode = new_mode;
    436 			if (new_mode == WSDISPLAYIO_MODE_EMUL)
    437 				vcons_redraw_screen(ms);
    438 		}
    439 		return 0;
    440 	default:
    441 		return EPASSTHROUGH;
    442 	}
    443 }
    444 
    445 static paddr_t
    446 vncfb_mmap(void *v, void *vs, off_t offset, int prot)
    447 {
    448 	/* TODO */
    449 	return -1;
    450 }
    451 
    452 static void
    453 vncfb_update(struct vncfb_softc *sc, int x, int y, int w, int h)
    454 {
    455 	thunk_rfb_update(&sc->sc_rfb, x, y, w, h);
    456 	softint_schedule(sc->sc_sih);
    457 }
    458 
    459 static int
    460 vncfb_intr(void *priv)
    461 {
    462 	struct vncfb_softc *sc = priv;
    463 
    464 	softint_schedule(sc->sc_sih);
    465 
    466 	return 0;
    467 }
    468 
    469 static void
    470 vncfb_softintr(void *priv)
    471 {
    472 	struct vncfb_softc *sc = priv;
    473 	thunk_rfb_event_t event;
    474 	int s;
    475 
    476 	while (thunk_rfb_poll(&sc->sc_rfb, &event) > 0) {
    477 		switch (event.message_type) {
    478 		case THUNK_RFB_KEY_EVENT:
    479 			s = spltty();
    480 			wskbd_input(sc->sc_wskbddev,
    481 			    event.data.key_event.down_flag ?
    482 			     WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
    483 			    event.data.key_event.keysym & 0xfff);
    484 			splx(s);
    485 			break;
    486 		default:
    487 			break;
    488 		}
    489 	}
    490 }
    491 
    492 static int
    493 vncfb_kbd_enable(void *priv, int on)
    494 {
    495 	struct vncfb_softc *sc = priv;
    496 
    497 	sc->sc_kbd_enable = on;
    498 
    499 	return 0;
    500 }
    501 
    502 static void
    503 vncfb_kbd_set_leds(void *priv, int leds)
    504 {
    505 }
    506 
    507 static int
    508 vncfb_kbd_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
    509 {
    510 	struct wskbd_bell_data *bd;
    511 
    512 	switch (cmd) {
    513 	case WSKBDIO_GTYPE:
    514 		*(int *)data = WSKBD_TYPE_RFB;
    515 		return 0;
    516 	case WSKBDIO_COMPLEXBELL:
    517 		bd = data;
    518 		vncfb_kbd_bell(priv, bd->pitch, bd->period, bd->volume);
    519 		return 0;
    520 	default:
    521 		return EPASSTHROUGH;
    522 	}
    523 }
    524 
    525 static void
    526 vncfb_kbd_cngetc(void *priv, u_int *type, int *data)
    527 {
    528 }
    529 
    530 static void
    531 vncfb_kbd_cnpollc(void *priv, int on)
    532 {
    533 }
    534 
    535 static void
    536 vncfb_kbd_bell(void *priv, u_int pitch, u_int period, u_int volume)
    537 {
    538 	struct vncfb_softc *sc = priv;
    539 
    540 	thunk_rfb_bell(&sc->sc_rfb);
    541 	softint_schedule(sc->sc_sih);
    542 }
    543