Home | History | Annotate | Line # | Download | only in wscons
wsdisplay_vcons.c revision 1.38.10.2
      1  1.38.10.2    martin /*	$NetBSD: wsdisplay_vcons.c,v 1.38.10.2 2020/04/13 08:04:52 martin Exp $ */
      2        1.1  macallan 
      3        1.1  macallan /*-
      4        1.1  macallan  * Copyright (c) 2005, 2006 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.38.10.2    martin __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.38.10.2 2020/04/13 08:04:52 martin 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/buf.h>
     36        1.1  macallan #include <sys/device.h>
     37        1.1  macallan #include <sys/ioctl.h>
     38        1.1  macallan #include <sys/malloc.h>
     39        1.1  macallan #include <sys/mman.h>
     40        1.1  macallan #include <sys/tty.h>
     41        1.1  macallan #include <sys/conf.h>
     42        1.1  macallan #include <sys/proc.h>
     43        1.1  macallan #include <sys/kthread.h>
     44        1.1  macallan #include <sys/tprintf.h>
     45       1.19  macallan #include <sys/atomic.h>
     46        1.1  macallan 
     47        1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     48        1.1  macallan #include <dev/wscons/wsconsio.h>
     49        1.1  macallan #include <dev/wsfont/wsfont.h>
     50        1.1  macallan #include <dev/rasops/rasops.h>
     51        1.1  macallan 
     52        1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     53        1.1  macallan 
     54       1.32  riastrad #ifdef _KERNEL_OPT
     55       1.14  macallan #include "opt_wsemul.h"
     56       1.14  macallan #include "opt_wsdisplay_compat.h"
     57       1.14  macallan #include "opt_vcons.h"
     58       1.32  riastrad #endif
     59       1.14  macallan 
     60       1.37  macallan #ifdef VCONS_DEBUG
     61       1.37  macallan #define DPRINTF printf
     62       1.37  macallan #else
     63       1.37  macallan #define DPRINTF if (0) printf
     64       1.37  macallan #endif
     65       1.37  macallan 
     66        1.1  macallan static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
     67        1.1  macallan 	    long *);
     68        1.1  macallan 
     69       1.10  christos static int  vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     70        1.1  macallan static int  vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
     71        1.1  macallan 	    int *, int *, long *);
     72        1.1  macallan static void vcons_free_screen(void *, void *);
     73        1.1  macallan static int  vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
     74        1.1  macallan 	    void *);
     75       1.37  macallan static int  vcons_load_font(void *, void *, struct wsdisplay_font *);
     76        1.1  macallan 
     77       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
     78       1.14  macallan static void vcons_scroll(void *, void *, int);
     79       1.14  macallan static void vcons_do_scroll(struct vcons_screen *);
     80       1.14  macallan #endif
     81       1.14  macallan 
     82       1.15     joerg static void vcons_do_switch(void *);
     83        1.1  macallan 
     84        1.1  macallan /* methods that work only on text buffers */
     85        1.1  macallan static void vcons_copycols_buffer(void *, int, int, int, int);
     86        1.1  macallan static void vcons_erasecols_buffer(void *, int, int, int, long);
     87        1.1  macallan static void vcons_copyrows_buffer(void *, int, int, int);
     88        1.1  macallan static void vcons_eraserows_buffer(void *, int, int, long);
     89        1.1  macallan static void vcons_putchar_buffer(void *, int, int, u_int, long);
     90        1.1  macallan 
     91        1.1  macallan /*
     92        1.1  macallan  * actual wrapper methods which call both the _buffer ones above and the
     93        1.1  macallan  * driver supplied ones to do the drawing
     94        1.1  macallan  */
     95        1.1  macallan static void vcons_copycols(void *, int, int, int, int);
     96        1.1  macallan static void vcons_erasecols(void *, int, int, int, long);
     97        1.1  macallan static void vcons_copyrows(void *, int, int, int);
     98        1.1  macallan static void vcons_eraserows(void *, int, int, long);
     99        1.1  macallan static void vcons_putchar(void *, int, int, u_int, long);
    100       1.25  macallan #ifdef VCONS_DRAW_INTR
    101       1.30   mlelstv static void vcons_erasecols_cached(void *, int, int, int, long);
    102       1.30   mlelstv static void vcons_eraserows_cached(void *, int, int, long);
    103       1.25  macallan static void vcons_putchar_cached(void *, int, int, u_int, long);
    104       1.25  macallan #endif
    105        1.1  macallan static void vcons_cursor(void *, int, int, int);
    106        1.1  macallan 
    107       1.17  macallan /*
    108       1.17  macallan  * methods that avoid framebuffer reads
    109       1.17  macallan  */
    110       1.17  macallan static void vcons_copycols_noread(void *, int, int, int, int);
    111       1.17  macallan static void vcons_copyrows_noread(void *, int, int, int);
    112       1.17  macallan 
    113       1.17  macallan 
    114       1.14  macallan /* support for reading/writing text buffers. For wsmoused */
    115        1.6      jmmv static int  vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
    116        1.6      jmmv static int  vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
    117        1.1  macallan 
    118        1.1  macallan static void vcons_lock(struct vcons_screen *);
    119        1.1  macallan static void vcons_unlock(struct vcons_screen *);
    120        1.1  macallan 
    121       1.20  jmcneill #ifdef VCONS_DRAW_INTR
    122       1.20  jmcneill static void vcons_intr(void *);
    123       1.22  jmcneill static void vcons_softintr(void *);
    124  1.38.10.1  christos static void vcons_init_thread(void *);
    125       1.20  jmcneill #endif
    126        1.1  macallan 
    127        1.1  macallan int
    128        1.1  macallan vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
    129        1.1  macallan     struct wsdisplay_accessops *ao)
    130        1.1  macallan {
    131        1.2  macallan 
    132        1.2  macallan 	/* zero out everything so we can rely on untouched fields being 0 */
    133        1.3  macallan 	memset(vd, 0, sizeof(struct vcons_data));
    134        1.2  macallan 
    135        1.1  macallan 	vd->cookie = cookie;
    136        1.1  macallan 
    137        1.1  macallan 	vd->init_screen = vcons_dummy_init_screen;
    138        1.1  macallan 	vd->show_screen_cb = NULL;
    139        1.1  macallan 
    140        1.6      jmmv 	/* keep a copy of the accessops that we replace below with our
    141        1.6      jmmv 	 * own wrappers */
    142        1.6      jmmv 	vd->ioctl = ao->ioctl;
    143        1.6      jmmv 
    144        1.6      jmmv 	/* configure the accessops */
    145        1.6      jmmv 	ao->ioctl = vcons_ioctl;
    146        1.1  macallan 	ao->alloc_screen = vcons_alloc_screen;
    147        1.1  macallan 	ao->free_screen = vcons_free_screen;
    148        1.1  macallan 	ao->show_screen = vcons_show_screen;
    149       1.37  macallan 	ao->load_font = vcons_load_font;
    150       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    151       1.14  macallan 	ao->scroll = vcons_scroll;
    152       1.14  macallan #endif
    153        1.1  macallan 
    154        1.1  macallan 	LIST_INIT(&vd->screens);
    155        1.1  macallan 	vd->active = NULL;
    156        1.1  macallan 	vd->wanted = NULL;
    157        1.1  macallan 	vd->currenttype = def;
    158       1.37  macallan 	vd->defaulttype = def;
    159       1.11        ad 	callout_init(&vd->switch_callout, 0);
    160       1.15     joerg 	callout_setfunc(&vd->switch_callout, vcons_do_switch, vd);
    161       1.25  macallan #ifdef VCONS_DRAW_INTR
    162       1.25  macallan 	vd->cells = 0;
    163       1.25  macallan 	vd->attrs = NULL;
    164       1.25  macallan 	vd->chars = NULL;
    165       1.25  macallan 	vd->cursor_offset = -1;
    166       1.25  macallan #endif
    167        1.1  macallan 
    168        1.1  macallan 	/*
    169        1.1  macallan 	 * a lock to serialize access to the framebuffer.
    170        1.1  macallan 	 * when switching screens we need to make sure there's no rasops
    171        1.1  macallan 	 * operation in progress
    172        1.1  macallan 	 */
    173        1.1  macallan #ifdef DIAGNOSTIC
    174        1.1  macallan 	vd->switch_poll_count = 0;
    175        1.1  macallan #endif
    176       1.20  jmcneill #ifdef VCONS_DRAW_INTR
    177       1.22  jmcneill 	vd->intr_softint = softint_establish(SOFTINT_SERIAL,
    178       1.22  jmcneill 	    vcons_softintr, vd);
    179       1.20  jmcneill 	callout_init(&vd->intr, 0);
    180       1.20  jmcneill 	callout_setfunc(&vd->intr, vcons_intr, vd);
    181       1.23  jmcneill 	vd->intr_valid = 1;
    182       1.21  jmcneill 
    183  1.38.10.1  christos 	if (kthread_create(PRI_NONE, 0, NULL, vcons_init_thread, vd, NULL,
    184  1.38.10.1  christos 	    "vcons_init") != 0) {
    185  1.38.10.1  christos 		printf("%s: unable to create thread.\n", __func__);
    186  1.38.10.1  christos 		return -1;
    187  1.38.10.1  christos 	}
    188       1.20  jmcneill #endif
    189        1.1  macallan 	return 0;
    190        1.1  macallan }
    191        1.1  macallan 
    192        1.1  macallan static void
    193        1.1  macallan vcons_lock(struct vcons_screen *scr)
    194        1.1  macallan {
    195        1.1  macallan #ifdef VCONS_PARANOIA
    196        1.1  macallan 	int s;
    197        1.1  macallan 
    198        1.1  macallan 	s = splhigh();
    199        1.1  macallan #endif
    200        1.1  macallan 	SCREEN_BUSY(scr);
    201        1.1  macallan #ifdef VCONS_PARANOIA
    202        1.1  macallan 	splx(s);
    203        1.1  macallan #endif
    204        1.1  macallan }
    205        1.1  macallan 
    206        1.1  macallan static void
    207        1.1  macallan vcons_unlock(struct vcons_screen *scr)
    208        1.1  macallan {
    209        1.1  macallan #ifdef VCONS_PARANOIA
    210        1.1  macallan 	int s;
    211        1.1  macallan 
    212        1.1  macallan 	s = splhigh();
    213        1.1  macallan #endif
    214        1.1  macallan 	SCREEN_IDLE(scr);
    215        1.1  macallan #ifdef VCONS_PARANOIA
    216        1.1  macallan 	splx(s);
    217        1.1  macallan #endif
    218        1.1  macallan }
    219        1.1  macallan 
    220        1.1  macallan static void
    221        1.9  christos vcons_dummy_init_screen(void *cookie,
    222        1.9  christos     struct vcons_screen *scr, int exists,
    223        1.9  christos     long *defattr)
    224        1.1  macallan {
    225        1.1  macallan 
    226        1.1  macallan 	/*
    227        1.1  macallan 	 * default init_screen() method.
    228        1.1  macallan 	 * Needs to be overwritten so we bitch and whine in case anyone ends
    229        1.1  macallan 	 * up in here.
    230        1.1  macallan 	 */
    231        1.1  macallan 	printf("vcons_init_screen: dummy function called. Your driver is "
    232        1.1  macallan 	       "supposed to supply a replacement for proper operation\n");
    233        1.1  macallan }
    234        1.1  macallan 
    235       1.37  macallan static int
    236       1.37  macallan vcons_alloc_buffers(struct vcons_data *vd, struct vcons_screen *scr)
    237        1.1  macallan {
    238        1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    239        1.1  macallan 	int cnt, i;
    240       1.25  macallan #ifdef VCONS_DRAW_INTR
    241       1.25  macallan 	int size;
    242       1.25  macallan #endif
    243        1.1  macallan 
    244       1.37  macallan 	/*
    245       1.37  macallan 	 * we allocate both chars and attributes in one chunk, attributes first
    246       1.37  macallan 	 * because they have the (potentially) bigger alignment
    247       1.37  macallan 	 */
    248       1.37  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    249       1.37  macallan 	cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
    250       1.37  macallan 	scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
    251       1.37  macallan 	scr->scr_current_line = 0;
    252       1.37  macallan 	scr->scr_line_wanted = 0;
    253       1.37  macallan 	scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
    254       1.37  macallan 	scr->scr_current_offset = scr->scr_offset_to_zero;
    255       1.37  macallan #else
    256       1.37  macallan 	cnt = ri->ri_rows * ri->ri_cols;
    257       1.37  macallan #endif
    258       1.37  macallan 	scr->scr_attrs = malloc(cnt * (sizeof(long) +
    259       1.37  macallan 	    sizeof(uint32_t)), M_DEVBUF, M_WAITOK);
    260       1.37  macallan 	if (scr->scr_attrs == NULL)
    261       1.37  macallan 		return ENOMEM;
    262       1.37  macallan 
    263       1.37  macallan 	scr->scr_chars = (uint32_t *)&scr->scr_attrs[cnt];
    264       1.37  macallan 
    265       1.37  macallan 	/*
    266       1.37  macallan 	 * fill the attribute buffer with *defattr, chars with 0x20
    267       1.37  macallan 	 * since we don't know if the driver tries to mimic firmware output or
    268       1.37  macallan 	 * reset everything we do nothing to VRAM here, any driver that feels
    269       1.37  macallan 	 * the need to clear screen or something will have to do it on its own
    270       1.37  macallan 	 * Additional screens will start out in the background anyway so
    271       1.37  macallan 	 * cleaning or not only really affects the initial console screen
    272       1.37  macallan 	 */
    273       1.37  macallan 	for (i = 0; i < cnt; i++) {
    274       1.37  macallan 		scr->scr_attrs[i] = scr->scr_defattr;
    275       1.37  macallan 		scr->scr_chars[i] = 0x20;
    276       1.37  macallan 	}
    277       1.37  macallan 
    278       1.37  macallan #ifdef VCONS_DRAW_INTR
    279       1.37  macallan 	size = ri->ri_cols * ri->ri_rows;
    280       1.37  macallan 	if (size > vd->cells) {
    281       1.37  macallan 		if (vd->chars != NULL) free(vd->chars, M_DEVBUF);
    282       1.37  macallan 		if (vd->attrs != NULL) free(vd->attrs, M_DEVBUF);
    283       1.37  macallan 		vd->cells = size;
    284       1.37  macallan 		vd->chars = malloc(size * sizeof(uint32_t), M_DEVBUF,
    285       1.37  macallan 		    M_WAITOK|M_ZERO);
    286       1.37  macallan 		vd->attrs = malloc(size * sizeof(long), M_DEVBUF,
    287       1.37  macallan 		    M_WAITOK|M_ZERO);
    288       1.37  macallan 		vcons_invalidate_cache(vd);
    289       1.37  macallan 	} else if (SCREEN_IS_VISIBLE(scr))
    290       1.37  macallan 		vcons_invalidate_cache(vd);
    291       1.37  macallan #endif
    292       1.37  macallan 	return 0;
    293       1.37  macallan }
    294       1.37  macallan 
    295       1.37  macallan int
    296       1.37  macallan vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
    297       1.37  macallan     int existing, long *defattr)
    298       1.37  macallan {
    299       1.37  macallan 	struct rasops_info *ri = &scr->scr_ri;
    300       1.37  macallan 	int i;
    301       1.37  macallan 
    302        1.1  macallan 	scr->scr_cookie = vd->cookie;
    303        1.4  jmcneill 	scr->scr_vd = scr->scr_origvd = vd;
    304       1.14  macallan 	scr->scr_busy = 0;
    305       1.37  macallan 	if (scr->scr_type == NULL)
    306       1.37  macallan 		scr->scr_type = vd->defaulttype;
    307        1.2  macallan 
    308        1.1  macallan 	/*
    309        1.1  macallan 	 * call the driver-supplied init_screen function which is expected
    310        1.1  macallan 	 * to set up rasops_info, override cursor() and probably others
    311        1.1  macallan 	 */
    312        1.1  macallan 	vd->init_screen(vd->cookie, scr, existing, defattr);
    313        1.1  macallan 
    314        1.1  macallan 	/*
    315        1.1  macallan 	 * save the non virtual console aware rasops and replace them with
    316        1.1  macallan 	 * our wrappers
    317        1.1  macallan 	 */
    318        1.1  macallan 	vd->eraserows = ri->ri_ops.eraserows;
    319        1.1  macallan 	vd->erasecols = ri->ri_ops.erasecols;
    320       1.37  macallan 	scr->putchar   = ri->ri_ops.putchar;
    321        1.1  macallan 	vd->cursor    = ri->ri_ops.cursor;
    322        1.1  macallan 
    323       1.18  macallan 	if (scr->scr_flags & VCONS_NO_COPYCOLS) {
    324       1.19  macallan 		vd->copycols  = vcons_copycols_noread;
    325       1.18  macallan 	} else {
    326       1.19  macallan 		vd->copycols = ri->ri_ops.copycols;
    327       1.18  macallan 	}
    328       1.18  macallan 
    329       1.18  macallan 	if (scr->scr_flags & VCONS_NO_COPYROWS) {
    330       1.19  macallan 		vd->copyrows  = vcons_copyrows_noread;
    331       1.17  macallan 	} else {
    332       1.19  macallan 		vd->copyrows = ri->ri_ops.copyrows;
    333       1.17  macallan 	}
    334       1.17  macallan 
    335       1.19  macallan 	ri->ri_ops.eraserows = vcons_eraserows;
    336       1.19  macallan 	ri->ri_ops.erasecols = vcons_erasecols;
    337       1.19  macallan 	ri->ri_ops.putchar   = vcons_putchar;
    338       1.19  macallan 	ri->ri_ops.cursor    = vcons_cursor;
    339       1.19  macallan 	ri->ri_ops.copycols  = vcons_copycols;
    340       1.19  macallan 	ri->ri_ops.copyrows  = vcons_copyrows;
    341       1.19  macallan 
    342       1.19  macallan 
    343        1.1  macallan 	ri->ri_hw = scr;
    344        1.1  macallan 
    345       1.35  christos 	i = ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
    346       1.35  christos 	if (i != 0) {
    347       1.35  christos #ifdef DIAGNOSTIC
    348       1.35  christos 		printf("vcons: error allocating attribute %d\n", i);
    349       1.35  christos #endif
    350       1.35  christos 		scr->scr_defattr = 0;
    351       1.35  christos 	} else
    352       1.35  christos 		scr->scr_defattr = *defattr;
    353        1.1  macallan 
    354       1.37  macallan 	vcons_alloc_buffers(vd, scr);
    355       1.25  macallan 
    356        1.2  macallan 	if(vd->active == NULL) {
    357        1.2  macallan 		vd->active = scr;
    358        1.2  macallan 		SCREEN_VISIBLE(scr);
    359        1.2  macallan 	}
    360        1.2  macallan 
    361        1.1  macallan 	if (existing) {
    362        1.2  macallan 		SCREEN_VISIBLE(scr);
    363        1.2  macallan 		vd->active = scr;
    364        1.1  macallan 	} else {
    365        1.2  macallan 		SCREEN_INVISIBLE(scr);
    366        1.1  macallan 	}
    367        1.1  macallan 
    368        1.1  macallan 	LIST_INSERT_HEAD(&vd->screens, scr, next);
    369        1.1  macallan 	return 0;
    370        1.1  macallan }
    371        1.1  macallan 
    372       1.37  macallan static int
    373       1.37  macallan vcons_load_font(void *v, void *cookie, struct wsdisplay_font *f)
    374       1.37  macallan {
    375       1.37  macallan 	struct vcons_data *vd = v;
    376       1.37  macallan 	struct vcons_screen *scr = cookie;
    377       1.37  macallan 	struct rasops_info *ri;
    378       1.37  macallan 	struct wsdisplay_font *font;
    379       1.37  macallan 	int flags = WSFONT_FIND_BITMAP, fcookie;
    380       1.37  macallan 
    381       1.37  macallan 	/* see if we're asked to add a font or use it */
    382       1.37  macallan 	if (scr == NULL)
    383       1.37  macallan 		return 0;
    384       1.37  macallan 
    385       1.37  macallan 	ri = &scr->scr_ri;
    386       1.37  macallan 
    387       1.37  macallan 	/* see if the driver knows how to handle multiple fonts */
    388       1.37  macallan 	if ((scr->scr_flags & VCONS_LOADFONT) == 0) {
    389       1.37  macallan 		return EOPNOTSUPP;
    390       1.37  macallan 	}
    391       1.37  macallan 
    392       1.37  macallan 	/* now see what fonts we can use */
    393       1.37  macallan 	if (ri->ri_flg & RI_ENABLE_ALPHA) {
    394       1.37  macallan 		flags |= WSFONT_FIND_ALPHA;
    395       1.37  macallan 	}
    396       1.37  macallan 
    397       1.37  macallan 	fcookie = wsfont_find(f->name, 0, 0, 0, 0, 0, flags);
    398       1.37  macallan 	if (fcookie == -1)
    399       1.37  macallan 		return EINVAL;
    400       1.37  macallan 
    401       1.37  macallan 	wsfont_lock(fcookie, &font);
    402       1.37  macallan 	if (font == NULL)
    403       1.37  macallan 		return EINVAL;
    404       1.37  macallan 
    405       1.37  macallan 	/* ok, we got a font. Now clear the screen with the old parameters */
    406       1.37  macallan 	if (SCREEN_IS_VISIBLE(scr))
    407       1.37  macallan 		vd->eraserows(ri, 0, ri->ri_rows, scr->scr_defattr);
    408       1.37  macallan 
    409       1.37  macallan 	vcons_lock(vd->active);
    410       1.37  macallan #ifdef VCONS_DRAW_INTR
    411       1.37  macallan 	callout_halt(&vd->intr, NULL);
    412       1.37  macallan #endif
    413       1.37  macallan 	/* set the new font and re-initialize things */
    414       1.37  macallan 	ri->ri_font = font;
    415       1.37  macallan 	wsfont_unlock(ri->ri_wsfcookie);
    416       1.37  macallan 	ri->ri_wsfcookie = fcookie;
    417       1.37  macallan 
    418       1.37  macallan 	vd->init_screen(vd->cookie, scr, 1, &scr->scr_defattr);
    419       1.37  macallan 	DPRINTF("caps %x %x\n", scr->scr_type->capabilities, ri->ri_caps);
    420       1.37  macallan 	if (scr->scr_type->capabilities & WSSCREEN_RESIZE) {
    421       1.37  macallan 		scr->scr_type->nrows = ri->ri_rows;
    422       1.37  macallan 		scr->scr_type->ncols = ri->ri_cols;
    423       1.37  macallan 		DPRINTF("new size %d %d\n", ri->ri_rows, ri->ri_cols);
    424       1.37  macallan 	}
    425       1.37  macallan 
    426       1.37  macallan 
    427       1.37  macallan 	/* now, throw the old buffers away */
    428       1.37  macallan 	if (scr->scr_attrs)
    429       1.37  macallan 		free(scr->scr_attrs, M_DEVBUF);
    430       1.37  macallan 	/* allocate new buffers */
    431       1.37  macallan 	vcons_alloc_buffers(vd, scr);
    432       1.37  macallan 
    433  1.38.10.2    martin 	/* save the potentially changed ri_ops */
    434  1.38.10.2    martin 	vd->eraserows = ri->ri_ops.eraserows;
    435  1.38.10.2    martin 	vd->erasecols = ri->ri_ops.erasecols;
    436       1.37  macallan 	scr->putchar   = ri->ri_ops.putchar;
    437  1.38.10.2    martin 	vd->cursor    = ri->ri_ops.cursor;
    438  1.38.10.2    martin 
    439  1.38.10.2    martin 	if (scr->scr_flags & VCONS_NO_COPYCOLS) {
    440  1.38.10.2    martin 		vd->copycols  = vcons_copycols_noread;
    441  1.38.10.2    martin 	} else {
    442  1.38.10.2    martin 		vd->copycols = ri->ri_ops.copycols;
    443  1.38.10.2    martin 	}
    444  1.38.10.2    martin 
    445  1.38.10.2    martin 	if (scr->scr_flags & VCONS_NO_COPYROWS) {
    446  1.38.10.2    martin 		vd->copyrows  = vcons_copyrows_noread;
    447  1.38.10.2    martin 	} else {
    448  1.38.10.2    martin 		vd->copyrows = ri->ri_ops.copyrows;
    449  1.38.10.2    martin 	}
    450       1.37  macallan 
    451       1.37  macallan 	/* and put our wrappers back */
    452       1.37  macallan 	ri->ri_ops.eraserows = vcons_eraserows;
    453       1.37  macallan 	ri->ri_ops.erasecols = vcons_erasecols;
    454       1.37  macallan 	ri->ri_ops.putchar   = vcons_putchar;
    455       1.37  macallan 	ri->ri_ops.cursor    = vcons_cursor;
    456       1.37  macallan 	ri->ri_ops.copycols  = vcons_copycols;
    457       1.37  macallan 	ri->ri_ops.copyrows  = vcons_copyrows;
    458       1.37  macallan 	vcons_unlock(vd->active);
    459       1.38  macallan 
    460       1.38  macallan 	/* notify things that we're about to redraw */
    461       1.38  macallan 	if (vd->show_screen_cb != NULL)
    462       1.38  macallan 		vd->show_screen_cb(scr, vd->show_screen_cookie);
    463       1.38  macallan 
    464       1.37  macallan #ifdef VCONS_DRAW_INTR
    465       1.37  macallan 	/*
    466       1.37  macallan 	 * XXX
    467       1.37  macallan 	 * Something(tm) craps all over VRAM somewhere up there if we're
    468       1.37  macallan 	 * using VCONS_DRAW_INTR. Until I figure out what causes it, just
    469       1.37  macallan 	 * redraw the screen for now.
    470       1.37  macallan 	 */
    471       1.37  macallan 	vcons_redraw_screen(vd->active);
    472       1.37  macallan 	callout_schedule(&vd->intr, mstohz(33));
    473       1.37  macallan #endif
    474       1.37  macallan 	/* no need to draw anything, wsdisplay should reset the terminal */
    475       1.37  macallan 
    476       1.37  macallan 	return 0;
    477       1.37  macallan }
    478       1.37  macallan 
    479        1.1  macallan static void
    480       1.15     joerg vcons_do_switch(void *arg)
    481        1.1  macallan {
    482       1.15     joerg 	struct vcons_data *vd = arg;
    483        1.1  macallan 	struct vcons_screen *scr, *oldscr;
    484        1.1  macallan 
    485        1.1  macallan 	scr = vd->wanted;
    486        1.1  macallan 	if (!scr) {
    487        1.1  macallan 		printf("vcons_switch_screen: disappeared\n");
    488        1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, EIO, 0);
    489        1.1  macallan 		return;
    490        1.1  macallan 	}
    491        1.1  macallan 	oldscr = vd->active; /* can be NULL! */
    492        1.1  macallan 
    493        1.1  macallan 	/*
    494        1.1  macallan 	 * if there's an old, visible screen we mark it invisible and wait
    495        1.1  macallan 	 * until it's not busy so we can safely switch
    496        1.1  macallan 	 */
    497        1.1  macallan 	if (oldscr != NULL) {
    498        1.1  macallan 		SCREEN_INVISIBLE(oldscr);
    499        1.1  macallan 		if (SCREEN_IS_BUSY(oldscr)) {
    500       1.15     joerg 			callout_schedule(&vd->switch_callout, 1);
    501        1.1  macallan #ifdef DIAGNOSTIC
    502        1.1  macallan 			/* bitch if we wait too long */
    503        1.1  macallan 			vd->switch_poll_count++;
    504        1.1  macallan 			if (vd->switch_poll_count > 100) {
    505        1.1  macallan 				panic("vcons: screen still busy");
    506        1.1  macallan 			}
    507        1.1  macallan #endif
    508        1.1  macallan 			return;
    509        1.1  macallan 		}
    510        1.1  macallan 		/* invisible screen -> no visible cursor image */
    511        1.1  macallan 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
    512        1.1  macallan #ifdef DIAGNOSTIC
    513        1.1  macallan 		vd->switch_poll_count = 0;
    514        1.1  macallan #endif
    515        1.1  macallan 	}
    516        1.1  macallan 
    517        1.1  macallan 	if (scr == oldscr)
    518        1.1  macallan 		return;
    519        1.1  macallan 
    520        1.1  macallan #ifdef DIAGNOSTIC
    521        1.1  macallan 	if (SCREEN_IS_VISIBLE(scr))
    522       1.14  macallan 		printf("vcons_switch_screen: already active");
    523        1.1  macallan #endif
    524        1.1  macallan 
    525        1.1  macallan #ifdef notyet
    526        1.1  macallan 	if (vd->currenttype != type) {
    527        1.1  macallan 		vcons_set_screentype(vd, type);
    528        1.1  macallan 		vd->currenttype = type;
    529        1.1  macallan 	}
    530        1.1  macallan #endif
    531        1.1  macallan 
    532        1.1  macallan 	SCREEN_VISIBLE(scr);
    533        1.1  macallan 	vd->active = scr;
    534        1.1  macallan 	vd->wanted = NULL;
    535        1.1  macallan 
    536       1.37  macallan #ifdef VCONS_DRAW_INTR
    537       1.37  macallan 	vcons_invalidate_cache(vd);
    538       1.37  macallan #endif
    539       1.37  macallan 
    540        1.1  macallan 	if (vd->show_screen_cb != NULL)
    541       1.38  macallan 		vd->show_screen_cb(scr, vd->show_screen_cookie);
    542        1.1  macallan 
    543        1.1  macallan 	if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
    544        1.1  macallan 		vcons_redraw_screen(scr);
    545        1.1  macallan 
    546        1.1  macallan 	if (vd->switch_cb)
    547        1.1  macallan 		vd->switch_cb(vd->switch_cb_arg, 0, 0);
    548        1.1  macallan }
    549        1.1  macallan 
    550        1.1  macallan void
    551        1.1  macallan vcons_redraw_screen(struct vcons_screen *scr)
    552        1.1  macallan {
    553       1.28  macallan 	uint32_t *charptr = scr->scr_chars, c;
    554       1.28  macallan 	long *attrptr = scr->scr_attrs, a, last_a = 0, mask, cmp, acmp;
    555        1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    556       1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    557       1.28  macallan 	int i, j, offset, boffset = 0, start = -1;
    558        1.1  macallan 
    559       1.28  macallan 	mask = 0x00ff00ff;	/* background and flags */
    560       1.36  macallan 	cmp = 0xffffffff;	/* never match anything */
    561        1.1  macallan 	vcons_lock(scr);
    562        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    563        1.1  macallan 
    564        1.1  macallan 		/*
    565        1.1  macallan 		 * only clear the screen when RI_FULLCLEAR is set since we're
    566        1.1  macallan 		 * going to overwrite every single character cell anyway
    567        1.1  macallan 		 */
    568        1.1  macallan 		if (ri->ri_flg & RI_FULLCLEAR) {
    569       1.25  macallan 			vd->eraserows(ri, 0, ri->ri_rows,
    570        1.1  macallan 			    scr->scr_defattr);
    571       1.28  macallan 			cmp = scr->scr_defattr & mask;
    572        1.1  macallan 		}
    573        1.1  macallan 
    574        1.1  macallan 		/* redraw the screen */
    575       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    576       1.14  macallan 		offset = scr->scr_current_offset;
    577       1.14  macallan #else
    578        1.1  macallan 		offset = 0;
    579       1.14  macallan #endif
    580        1.1  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    581       1.28  macallan 			start = -1;
    582        1.1  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    583        1.1  macallan 				/*
    584        1.1  macallan 				 * no need to use the wrapper function - we
    585        1.1  macallan 				 * don't change any characters or attributes
    586        1.1  macallan 				 * and we already made sure the screen we're
    587        1.1  macallan 				 * working on is visible
    588        1.1  macallan 				 */
    589       1.28  macallan 				c = charptr[offset];
    590       1.28  macallan 				a = attrptr[offset];
    591       1.28  macallan 				acmp = a & mask;
    592       1.28  macallan 				if (c == ' ') {
    593       1.28  macallan 					/*
    594       1.28  macallan 					 * if we already erased the background
    595       1.28  macallan 					 * and this blank uses the same colour
    596       1.28  macallan 					 * and flags we don't need to do
    597       1.28  macallan 					 * anything here
    598       1.28  macallan 					 */
    599       1.28  macallan 					if (acmp == cmp)
    600       1.28  macallan 						goto next;
    601       1.28  macallan 					/*
    602       1.28  macallan 					 * see if we can optimize things a
    603       1.28  macallan 					 * little bit by drawing stretches of
    604       1.28  macallan 					 * blanks using erasecols
    605       1.28  macallan 					 */
    606       1.28  macallan 
    607       1.28  macallan 					if (start == -1) {
    608       1.28  macallan 						start = j;
    609       1.28  macallan 						last_a = acmp;
    610       1.28  macallan 					} else if (acmp != last_a) {
    611       1.28  macallan 						/*
    612       1.28  macallan 						 * different attr, need to
    613       1.36  macallan 						 * flush & restart
    614       1.28  macallan 						 */
    615       1.28  macallan 						vd->erasecols(ri, i, start,
    616       1.28  macallan 						    j - start, last_a);
    617       1.36  macallan 						start = j;
    618       1.36  macallan 						last_a = acmp;
    619       1.28  macallan 					}
    620       1.28  macallan 				} else {
    621       1.28  macallan 					if (start != -1) {
    622       1.28  macallan 						vd->erasecols(ri, i, start,
    623       1.28  macallan 						    j - start, last_a);
    624       1.28  macallan 						start = -1;
    625       1.28  macallan 					}
    626       1.28  macallan 
    627       1.37  macallan 					scr->putchar(ri, i, j, c, a);
    628       1.28  macallan 				}
    629       1.28  macallan next:
    630       1.25  macallan #ifdef VCONS_DRAW_INTR
    631       1.25  macallan 				vd->chars[boffset] = charptr[offset];
    632       1.25  macallan 				vd->attrs[boffset] = attrptr[offset];
    633       1.25  macallan #endif
    634       1.25  macallan 				offset++;
    635       1.25  macallan 				boffset++;
    636       1.25  macallan 			}
    637       1.28  macallan 			/* end of the line - draw all defered blanks, if any */
    638       1.28  macallan 			if (start != -1) {
    639       1.28  macallan 				vd->erasecols(ri, i, start, j - start, last_a);
    640       1.28  macallan 			}
    641       1.25  macallan 		}
    642       1.25  macallan 		ri->ri_flg &= ~RI_CURSOR;
    643       1.25  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    644       1.25  macallan #ifdef VCONS_DRAW_INTR
    645       1.25  macallan 		vd->cursor_offset = ri->ri_crow * ri->ri_cols + ri->ri_ccol;
    646       1.25  macallan #endif
    647       1.25  macallan 	}
    648       1.25  macallan 	vcons_unlock(scr);
    649       1.25  macallan }
    650       1.25  macallan 
    651       1.25  macallan #ifdef VCONS_DRAW_INTR
    652       1.25  macallan void
    653       1.25  macallan vcons_update_screen(struct vcons_screen *scr)
    654       1.25  macallan {
    655       1.27  macallan 	uint32_t *charptr = scr->scr_chars;
    656       1.25  macallan 	long *attrptr = scr->scr_attrs;
    657       1.25  macallan 	struct rasops_info *ri = &scr->scr_ri;
    658       1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    659       1.25  macallan 	int i, j, offset, boffset = 0;
    660       1.25  macallan 
    661       1.25  macallan 	vcons_lock(scr);
    662       1.25  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    663       1.25  macallan 
    664       1.25  macallan 		/* redraw the screen */
    665       1.25  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    666       1.25  macallan 		offset = scr->scr_current_offset;
    667       1.25  macallan #else
    668       1.25  macallan 		offset = 0;
    669       1.25  macallan #endif
    670       1.25  macallan 		/*
    671       1.25  macallan 		 * we mark the character cell occupied by the cursor as dirty
    672       1.25  macallan 		 * so we don't have to deal with it
    673       1.25  macallan 		 * notice that this isn't necessarily the position where rasops
    674       1.25  macallan 		 * thinks it is, just where we drew it the last time
    675       1.25  macallan 		 */
    676       1.25  macallan 		if (vd->cursor_offset >= 0)
    677       1.25  macallan 			vd->attrs[vd->cursor_offset] = 0xffffffff;
    678       1.25  macallan 
    679       1.25  macallan 		for (i = 0; i < ri->ri_rows; i++) {
    680       1.25  macallan 			for (j = 0; j < ri->ri_cols; j++) {
    681       1.25  macallan 				/*
    682       1.25  macallan 				 * no need to use the wrapper function - we
    683       1.25  macallan 				 * don't change any characters or attributes
    684       1.25  macallan 				 * and we already made sure the screen we're
    685       1.25  macallan 				 * working on is visible
    686       1.25  macallan 				 */
    687       1.25  macallan 				if ((vd->chars[boffset] != charptr[offset]) ||
    688       1.25  macallan 				    (vd->attrs[boffset] != attrptr[offset])) {
    689       1.37  macallan 					scr->putchar(ri, i, j,
    690       1.25  macallan 				 	   charptr[offset], attrptr[offset]);
    691       1.25  macallan 					vd->chars[boffset] = charptr[offset];
    692       1.25  macallan 					vd->attrs[boffset] = attrptr[offset];
    693       1.25  macallan 				}
    694        1.1  macallan 				offset++;
    695       1.25  macallan 				boffset++;
    696        1.1  macallan 			}
    697        1.1  macallan 		}
    698        1.1  macallan 		ri->ri_flg &= ~RI_CURSOR;
    699        1.1  macallan 		scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
    700       1.25  macallan 		vd->cursor_offset = ri->ri_crow * ri->ri_cols + ri->ri_ccol;
    701        1.1  macallan 	}
    702        1.1  macallan 	vcons_unlock(scr);
    703        1.1  macallan }
    704       1.25  macallan #endif
    705        1.1  macallan 
    706        1.1  macallan static int
    707       1.10  christos vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    708        1.6      jmmv 	struct lwp *l)
    709        1.6      jmmv {
    710        1.6      jmmv 	struct vcons_data *vd = v;
    711       1.27  macallan 	int error = 0;
    712       1.27  macallan 
    713        1.6      jmmv 
    714        1.6      jmmv 	switch (cmd) {
    715        1.6      jmmv 	case WSDISPLAYIO_GETWSCHAR:
    716        1.6      jmmv 		error = vcons_getwschar((struct vcons_screen *)vs,
    717        1.6      jmmv 			(struct wsdisplay_char *)data);
    718        1.6      jmmv 		break;
    719        1.6      jmmv 
    720        1.6      jmmv 	case WSDISPLAYIO_PUTWSCHAR:
    721        1.6      jmmv 		error = vcons_putwschar((struct vcons_screen *)vs,
    722        1.6      jmmv 			(struct wsdisplay_char *)data);
    723        1.6      jmmv 		break;
    724        1.6      jmmv 
    725       1.27  macallan 	case WSDISPLAYIO_SET_POLLING: {
    726       1.27  macallan 		int poll = *(int *)data;
    727       1.27  macallan 
    728       1.27  macallan 		/* first call the driver's ioctl handler */
    729       1.27  macallan 		if (vd->ioctl != NULL)
    730       1.27  macallan 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    731       1.27  macallan 		if (poll) {
    732       1.27  macallan 			vcons_enable_polling(vd);
    733       1.27  macallan 			vcons_hard_switch(LIST_FIRST(&vd->screens));
    734       1.27  macallan 		} else
    735       1.27  macallan 			vcons_disable_polling(vd);
    736       1.27  macallan 		}
    737       1.27  macallan 		break;
    738       1.27  macallan 
    739        1.6      jmmv 	default:
    740        1.6      jmmv 		if (vd->ioctl != NULL)
    741        1.6      jmmv 			error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
    742        1.6      jmmv 		else
    743        1.6      jmmv 			error = EPASSTHROUGH;
    744        1.6      jmmv 	}
    745        1.6      jmmv 
    746        1.6      jmmv 	return error;
    747        1.6      jmmv }
    748        1.6      jmmv 
    749        1.6      jmmv static int
    750        1.1  macallan vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    751        1.1  macallan     int *curxp, int *curyp, long *defattrp)
    752        1.1  macallan {
    753        1.1  macallan 	struct vcons_data *vd = v;
    754        1.1  macallan 	struct vcons_screen *scr;
    755       1.37  macallan 	struct wsscreen_descr *t = __UNCONST(type);
    756        1.1  macallan 	int ret;
    757        1.1  macallan 
    758        1.1  macallan 	scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
    759        1.1  macallan 	if (scr == NULL)
    760        1.1  macallan 		return ENOMEM;
    761        1.1  macallan 
    762        1.1  macallan 	scr->scr_flags = 0;
    763        1.2  macallan 	scr->scr_status = 0;
    764        1.2  macallan 	scr->scr_busy = 0;
    765       1.37  macallan 	scr->scr_type = __UNCONST(type);
    766        1.1  macallan 
    767        1.1  macallan 	ret = vcons_init_screen(vd, scr, 0, defattrp);
    768        1.1  macallan 	if (ret != 0) {
    769        1.1  macallan 		free(scr, M_DEVBUF);
    770        1.1  macallan 		return ret;
    771        1.1  macallan 	}
    772       1.37  macallan 	if (t->capabilities & WSSCREEN_RESIZE) {
    773       1.37  macallan 		t->nrows = scr->scr_ri.ri_rows;
    774       1.37  macallan 		t->ncols = scr->scr_ri.ri_cols;
    775       1.37  macallan 	}
    776        1.1  macallan 
    777        1.1  macallan 	if (vd->active == NULL) {
    778        1.1  macallan 		SCREEN_VISIBLE(scr);
    779        1.1  macallan 		vd->active = scr;
    780        1.1  macallan 		vd->currenttype = type;
    781        1.1  macallan 	}
    782        1.1  macallan 
    783        1.1  macallan 	*cookiep = scr;
    784        1.1  macallan 	*curxp = scr->scr_ri.ri_ccol;
    785        1.1  macallan 	*curyp = scr->scr_ri.ri_crow;
    786        1.1  macallan 	return 0;
    787        1.1  macallan }
    788        1.1  macallan 
    789        1.1  macallan static void
    790        1.1  macallan vcons_free_screen(void *v, void *cookie)
    791        1.1  macallan {
    792        1.1  macallan 	struct vcons_data *vd = v;
    793        1.1  macallan 	struct vcons_screen *scr = cookie;
    794        1.1  macallan 
    795        1.1  macallan 	vcons_lock(scr);
    796        1.1  macallan 	/* there should be no rasops activity here */
    797        1.1  macallan 
    798        1.1  macallan 	LIST_REMOVE(scr, next);
    799        1.1  macallan 
    800        1.1  macallan 	if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
    801        1.1  macallan 		free(scr->scr_attrs, M_DEVBUF);
    802        1.1  macallan 		free(scr, M_DEVBUF);
    803        1.1  macallan 	} else {
    804        1.1  macallan 		/*
    805        1.1  macallan 		 * maybe we should just restore the old rasops_info methods
    806        1.1  macallan 		 * and free the character/attribute buffer here?
    807        1.1  macallan 		 */
    808        1.1  macallan #ifdef VCONS_DEBUG
    809        1.1  macallan 		panic("vcons_free_screen: console");
    810        1.1  macallan #else
    811        1.1  macallan 		printf("vcons_free_screen: console\n");
    812        1.1  macallan #endif
    813        1.1  macallan 	}
    814        1.1  macallan 
    815        1.1  macallan 	if (vd->active == scr)
    816        1.1  macallan 		vd->active = NULL;
    817        1.1  macallan }
    818        1.1  macallan 
    819        1.1  macallan static int
    820        1.9  christos vcons_show_screen(void *v, void *cookie, int waitok,
    821        1.1  macallan     void (*cb)(void *, int, int), void *cb_arg)
    822        1.1  macallan {
    823        1.1  macallan 	struct vcons_data *vd = v;
    824        1.1  macallan 	struct vcons_screen *scr;
    825        1.1  macallan 
    826        1.1  macallan 	scr = cookie;
    827        1.1  macallan 	if (scr == vd->active)
    828        1.1  macallan 		return 0;
    829        1.1  macallan 
    830        1.1  macallan 	vd->wanted = scr;
    831        1.1  macallan 	vd->switch_cb = cb;
    832        1.1  macallan 	vd->switch_cb_arg = cb_arg;
    833        1.1  macallan 	if (cb) {
    834       1.15     joerg 		callout_schedule(&vd->switch_callout, 0);
    835        1.1  macallan 		return EAGAIN;
    836        1.1  macallan 	}
    837        1.1  macallan 
    838        1.1  macallan 	vcons_do_switch(vd);
    839        1.1  macallan 	return 0;
    840        1.1  macallan }
    841        1.1  macallan 
    842        1.1  macallan /* wrappers for rasops_info methods */
    843        1.1  macallan 
    844        1.1  macallan static void
    845        1.1  macallan vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
    846        1.1  macallan {
    847        1.1  macallan 	struct rasops_info *ri = cookie;
    848        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    849        1.1  macallan 	int from = srccol + row * ri->ri_cols;
    850        1.1  macallan 	int to = dstcol + row * ri->ri_cols;
    851        1.1  macallan 
    852       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    853       1.14  macallan 	int offset;
    854       1.14  macallan 	offset = scr->scr_offset_to_zero;
    855       1.14  macallan 
    856       1.14  macallan 	memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from],
    857       1.14  macallan 	    ncols * sizeof(long));
    858       1.14  macallan 	memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from],
    859       1.27  macallan 	    ncols * sizeof(uint32_t));
    860       1.14  macallan #else
    861        1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
    862        1.1  macallan 	    ncols * sizeof(long));
    863        1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
    864       1.27  macallan 	    ncols * sizeof(uint32_t));
    865       1.14  macallan #endif
    866       1.20  jmcneill 
    867       1.20  jmcneill #ifdef VCONS_DRAW_INTR
    868       1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
    869       1.20  jmcneill #endif
    870        1.1  macallan }
    871        1.1  macallan 
    872        1.1  macallan static void
    873        1.1  macallan vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    874        1.1  macallan {
    875        1.1  macallan 	struct rasops_info *ri = cookie;
    876        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    877        1.1  macallan 
    878        1.1  macallan 	vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
    879        1.1  macallan 
    880       1.21  jmcneill #if defined(VCONS_DRAW_INTR)
    881       1.21  jmcneill 	if (scr->scr_vd->use_intr)
    882       1.21  jmcneill 		return;
    883       1.21  jmcneill #endif
    884       1.21  jmcneill 
    885        1.1  macallan 	vcons_lock(scr);
    886        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    887       1.34   mlelstv #if defined(VCONS_DRAW_INTR)
    888       1.34   mlelstv 		vcons_update_screen(scr);
    889       1.34   mlelstv #else
    890       1.26  macallan 		scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
    891       1.30   mlelstv #endif
    892        1.1  macallan 	}
    893        1.1  macallan 	vcons_unlock(scr);
    894        1.1  macallan }
    895        1.1  macallan 
    896        1.1  macallan static void
    897       1.17  macallan vcons_copycols_noread(void *cookie, int row, int srccol, int dstcol, int ncols)
    898       1.17  macallan {
    899       1.17  macallan 	struct rasops_info *ri = cookie;
    900       1.17  macallan 	struct vcons_screen *scr = ri->ri_hw;
    901       1.37  macallan #ifdef VCONS_DRAW_INTR
    902       1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    903       1.37  macallan #endif
    904       1.17  macallan 
    905       1.30   mlelstv 	vcons_lock(scr);
    906       1.17  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    907       1.25  macallan 		int pos, c, offset, ppos;
    908       1.17  macallan 
    909       1.17  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    910       1.17  macallan 		offset = scr->scr_current_offset;
    911       1.17  macallan #else
    912       1.17  macallan 		offset = 0;
    913       1.17  macallan #endif
    914       1.25  macallan 		ppos = ri->ri_cols * row + dstcol;
    915       1.25  macallan 		pos = ppos + offset;
    916       1.17  macallan 		for (c = dstcol; c < (dstcol + ncols); c++) {
    917       1.25  macallan #ifdef VCONS_DRAW_INTR
    918       1.25  macallan 			if ((scr->scr_chars[pos] != vd->chars[ppos]) ||
    919       1.25  macallan 			    (scr->scr_attrs[pos] != vd->attrs[ppos])) {
    920       1.37  macallan 				scr->putchar(cookie, row, c,
    921       1.25  macallan 				   scr->scr_chars[pos], scr->scr_attrs[pos]);
    922       1.25  macallan 				vd->chars[ppos] = scr->scr_chars[pos];
    923       1.25  macallan 				vd->attrs[ppos] = scr->scr_attrs[pos];
    924       1.25  macallan 			}
    925       1.25  macallan #else
    926       1.37  macallan 			scr->putchar(cookie, row, c, scr->scr_chars[pos],
    927       1.25  macallan 			    scr->scr_attrs[pos]);
    928       1.25  macallan #endif
    929       1.17  macallan 			pos++;
    930       1.25  macallan 			ppos++;
    931       1.17  macallan 		}
    932       1.17  macallan 	}
    933       1.30   mlelstv 	vcons_unlock(scr);
    934       1.17  macallan }
    935       1.17  macallan 
    936       1.17  macallan static void
    937        1.1  macallan vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
    938        1.1  macallan {
    939        1.1  macallan 	struct rasops_info *ri = cookie;
    940        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    941        1.1  macallan 	int start = startcol + row * ri->ri_cols;
    942        1.1  macallan 	int end = start + ncols, i;
    943        1.1  macallan 
    944       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
    945       1.14  macallan 	int offset;
    946       1.14  macallan 	offset = scr->scr_offset_to_zero;
    947       1.14  macallan 
    948       1.14  macallan 	for (i = start; i < end; i++) {
    949       1.14  macallan 		scr->scr_attrs[offset + i] = fillattr;
    950       1.14  macallan 		scr->scr_chars[offset + i] = 0x20;
    951       1.14  macallan 	}
    952       1.14  macallan #else
    953        1.1  macallan 	for (i = start; i < end; i++) {
    954        1.1  macallan 		scr->scr_attrs[i] = fillattr;
    955        1.1  macallan 		scr->scr_chars[i] = 0x20;
    956        1.1  macallan 	}
    957       1.14  macallan #endif
    958       1.20  jmcneill 
    959       1.20  jmcneill #ifdef VCONS_DRAW_INTR
    960       1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
    961       1.20  jmcneill #endif
    962        1.1  macallan }
    963        1.1  macallan 
    964       1.25  macallan #ifdef VCONS_DRAW_INTR
    965       1.25  macallan static void
    966       1.25  macallan vcons_erasecols_cached(void *cookie, int row, int startcol, int ncols, long fillattr)
    967       1.25  macallan {
    968       1.25  macallan 	struct rasops_info *ri = cookie;
    969       1.25  macallan 	struct vcons_screen *scr = ri->ri_hw;
    970       1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
    971       1.25  macallan 	int i, pos = row * ri->ri_cols + startcol;
    972       1.25  macallan 
    973       1.25  macallan 	for (i = pos; i < ncols; i++) {
    974       1.25  macallan 		vd->chars[i] = 0x20;
    975       1.25  macallan 		vd->attrs[i] = fillattr;
    976       1.25  macallan 	}
    977       1.25  macallan 	vd->erasecols(cookie, row, startcol, ncols, fillattr);
    978       1.25  macallan }
    979       1.25  macallan #endif
    980       1.25  macallan 
    981        1.1  macallan static void
    982        1.1  macallan vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    983        1.1  macallan {
    984        1.1  macallan 	struct rasops_info *ri = cookie;
    985        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    986        1.1  macallan 
    987        1.1  macallan 	vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
    988        1.1  macallan 
    989       1.21  jmcneill #if defined(VCONS_DRAW_INTR)
    990       1.21  jmcneill 	if (scr->scr_vd->use_intr)
    991       1.21  jmcneill 		return;
    992       1.21  jmcneill #endif
    993       1.21  jmcneill 
    994        1.1  macallan 	vcons_lock(scr);
    995        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
    996       1.25  macallan #ifdef VCONS_DRAW_INTR
    997       1.30   mlelstv 		vcons_erasecols_cached(cookie, row, startcol, ncols,
    998       1.30   mlelstv 		    fillattr);
    999       1.25  macallan #else
   1000       1.25  macallan 		scr->scr_vd->erasecols(cookie, row, startcol, ncols, fillattr);
   1001       1.25  macallan #endif
   1002        1.1  macallan 	}
   1003        1.1  macallan 	vcons_unlock(scr);
   1004        1.1  macallan }
   1005        1.1  macallan 
   1006        1.1  macallan static void
   1007        1.1  macallan vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
   1008        1.1  macallan {
   1009        1.1  macallan 	struct rasops_info *ri = cookie;
   1010        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1011        1.1  macallan 	int from, to, len;
   1012        1.1  macallan 
   1013       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1014       1.14  macallan 	int offset;
   1015       1.14  macallan 	offset = scr->scr_offset_to_zero;
   1016       1.14  macallan 
   1017       1.14  macallan 	/* do we need to scroll the back buffer? */
   1018       1.14  macallan 	if (dstrow == 0) {
   1019       1.14  macallan 		from = ri->ri_cols * srcrow;
   1020       1.14  macallan 		to = ri->ri_cols * dstrow;
   1021       1.14  macallan 
   1022       1.14  macallan 		memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
   1023       1.14  macallan 		    scr->scr_offset_to_zero * sizeof(long));
   1024       1.14  macallan 		memmove(&scr->scr_chars[to], &scr->scr_chars[from],
   1025       1.27  macallan 		    scr->scr_offset_to_zero * sizeof(uint32_t));
   1026       1.14  macallan 	}
   1027       1.14  macallan 	from = ri->ri_cols * srcrow + offset;
   1028       1.14  macallan 	to = ri->ri_cols * dstrow + offset;
   1029       1.14  macallan 	len = ri->ri_cols * nrows;
   1030       1.14  macallan 
   1031       1.14  macallan #else
   1032        1.1  macallan 	from = ri->ri_cols * srcrow;
   1033        1.1  macallan 	to = ri->ri_cols * dstrow;
   1034        1.1  macallan 	len = ri->ri_cols * nrows;
   1035       1.14  macallan #endif
   1036        1.1  macallan 	memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
   1037        1.1  macallan 	    len * sizeof(long));
   1038        1.1  macallan 	memmove(&scr->scr_chars[to], &scr->scr_chars[from],
   1039       1.27  macallan 	    len * sizeof(uint32_t));
   1040       1.20  jmcneill 
   1041       1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1042       1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
   1043       1.20  jmcneill #endif
   1044        1.1  macallan }
   1045        1.1  macallan 
   1046        1.1  macallan static void
   1047        1.1  macallan vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1048        1.1  macallan {
   1049        1.1  macallan 	struct rasops_info *ri = cookie;
   1050        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1051        1.1  macallan 
   1052        1.1  macallan 	vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
   1053        1.1  macallan 
   1054       1.21  jmcneill #if defined(VCONS_DRAW_INTR)
   1055       1.21  jmcneill 	if (scr->scr_vd->use_intr)
   1056       1.21  jmcneill 		return;
   1057       1.21  jmcneill #endif
   1058       1.21  jmcneill 
   1059        1.1  macallan 	vcons_lock(scr);
   1060        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1061       1.34   mlelstv #if defined(VCONS_DRAW_INTR)
   1062       1.34   mlelstv 		vcons_update_screen(scr);
   1063       1.34   mlelstv #else
   1064       1.26  macallan 		scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
   1065       1.30   mlelstv #endif
   1066        1.1  macallan 	}
   1067        1.1  macallan 	vcons_unlock(scr);
   1068        1.1  macallan }
   1069        1.1  macallan 
   1070        1.1  macallan static void
   1071       1.17  macallan vcons_copyrows_noread(void *cookie, int srcrow, int dstrow, int nrows)
   1072       1.17  macallan {
   1073       1.17  macallan 	struct rasops_info *ri = cookie;
   1074       1.17  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1075       1.37  macallan #ifdef VCONS_DRAW_INTR
   1076       1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
   1077       1.37  macallan #endif
   1078       1.30   mlelstv 	vcons_lock(scr);
   1079       1.17  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1080       1.25  macallan 		int pos, l, c, offset, ppos;
   1081       1.17  macallan 
   1082       1.17  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1083       1.17  macallan 		offset = scr->scr_current_offset;
   1084       1.17  macallan #else
   1085       1.17  macallan 		offset = 0;
   1086       1.17  macallan #endif
   1087       1.25  macallan 		ppos = ri->ri_cols * dstrow;
   1088       1.25  macallan 		pos = ppos + offset;
   1089       1.17  macallan 		for (l = dstrow; l < (dstrow + nrows); l++) {
   1090       1.17  macallan 			for (c = 0; c < ri->ri_cols; c++) {
   1091       1.25  macallan #ifdef VCONS_DRAW_INTR
   1092       1.25  macallan 				if ((scr->scr_chars[pos] != vd->chars[ppos]) ||
   1093       1.25  macallan 				    (scr->scr_attrs[pos] != vd->attrs[ppos])) {
   1094       1.37  macallan 					scr->putchar(cookie, l, c,
   1095       1.25  macallan 					   scr->scr_chars[pos], scr->scr_attrs[pos]);
   1096       1.25  macallan 					vd->chars[ppos] = scr->scr_chars[pos];
   1097       1.25  macallan 					vd->attrs[ppos] = scr->scr_attrs[pos];
   1098       1.25  macallan 				}
   1099       1.25  macallan #else
   1100       1.37  macallan 				scr->putchar(cookie, l, c, scr->scr_chars[pos],
   1101       1.25  macallan 				    scr->scr_attrs[pos]);
   1102       1.25  macallan #endif
   1103       1.17  macallan 				pos++;
   1104       1.25  macallan 				ppos++;
   1105       1.17  macallan 			}
   1106       1.17  macallan 		}
   1107       1.17  macallan 	}
   1108       1.30   mlelstv 	vcons_unlock(scr);
   1109       1.17  macallan }
   1110       1.17  macallan 
   1111       1.17  macallan static void
   1112        1.1  macallan vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
   1113        1.1  macallan {
   1114        1.1  macallan 	struct rasops_info *ri = cookie;
   1115        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1116        1.1  macallan 	int start, end, i;
   1117        1.1  macallan 
   1118       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1119       1.14  macallan 	int offset;
   1120       1.14  macallan 	offset = scr->scr_offset_to_zero;
   1121       1.14  macallan 
   1122       1.14  macallan 	start = ri->ri_cols * row + offset;
   1123       1.14  macallan 	end = ri->ri_cols * (row + nrows) + offset;
   1124       1.14  macallan #else
   1125        1.1  macallan 	start = ri->ri_cols * row;
   1126        1.1  macallan 	end = ri->ri_cols * (row + nrows);
   1127       1.14  macallan #endif
   1128        1.1  macallan 
   1129        1.1  macallan 	for (i = start; i < end; i++) {
   1130        1.1  macallan 		scr->scr_attrs[i] = fillattr;
   1131        1.1  macallan 		scr->scr_chars[i] = 0x20;
   1132        1.1  macallan 	}
   1133       1.20  jmcneill 
   1134       1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1135       1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
   1136       1.20  jmcneill #endif
   1137        1.1  macallan }
   1138        1.1  macallan 
   1139       1.30   mlelstv #ifdef VCONS_DRAW_INTR
   1140       1.30   mlelstv static void
   1141       1.30   mlelstv vcons_eraserows_cached(void *cookie, int row, int nrows, long fillattr)
   1142       1.30   mlelstv {
   1143       1.30   mlelstv 	struct rasops_info *ri = cookie;
   1144       1.30   mlelstv 	struct vcons_screen *scr = ri->ri_hw;
   1145       1.30   mlelstv 	struct vcons_data *vd = scr->scr_vd;
   1146       1.30   mlelstv 	int i, pos = row * ri->ri_cols, end = (row+nrows) * ri->ri_cols;
   1147       1.30   mlelstv 
   1148       1.30   mlelstv 	for (i = pos; i < end; i++) {
   1149       1.30   mlelstv 		vd->chars[i] = 0x20;
   1150       1.30   mlelstv 		vd->attrs[i] = fillattr;
   1151       1.30   mlelstv 	}
   1152       1.30   mlelstv 	vd->eraserows(cookie, row, nrows, fillattr);
   1153       1.30   mlelstv }
   1154       1.30   mlelstv #endif
   1155       1.30   mlelstv 
   1156        1.1  macallan static void
   1157        1.1  macallan vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
   1158        1.1  macallan {
   1159        1.1  macallan 	struct rasops_info *ri = cookie;
   1160        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1161        1.1  macallan 
   1162        1.1  macallan 	vcons_eraserows_buffer(cookie, row, nrows, fillattr);
   1163        1.1  macallan 
   1164       1.21  jmcneill #if defined(VCONS_DRAW_INTR)
   1165       1.21  jmcneill 	if (scr->scr_vd->use_intr)
   1166       1.21  jmcneill 		return;
   1167       1.21  jmcneill #endif
   1168       1.21  jmcneill 
   1169        1.1  macallan 	vcons_lock(scr);
   1170        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1171       1.30   mlelstv #ifdef VCONS_DRAW_INTR
   1172       1.30   mlelstv 		vcons_eraserows_cached(cookie, row, nrows, fillattr);
   1173       1.30   mlelstv #else
   1174       1.25  macallan 		scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
   1175       1.30   mlelstv #endif
   1176        1.1  macallan 	}
   1177        1.1  macallan 	vcons_unlock(scr);
   1178        1.1  macallan }
   1179        1.1  macallan 
   1180        1.1  macallan static void
   1181        1.1  macallan vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
   1182        1.1  macallan {
   1183        1.1  macallan 	struct rasops_info *ri = cookie;
   1184        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1185        1.1  macallan 	int pos;
   1186        1.1  macallan 
   1187       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1188       1.14  macallan 	int offset;
   1189       1.14  macallan 	offset = scr->scr_offset_to_zero;
   1190       1.14  macallan 
   1191       1.14  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
   1192       1.14  macallan 	     (col < ri->ri_cols)) {
   1193       1.14  macallan 		pos = col + row * ri->ri_cols;
   1194       1.14  macallan 		scr->scr_attrs[pos + offset] = attr;
   1195       1.14  macallan 		scr->scr_chars[pos + offset] = c;
   1196       1.14  macallan 	}
   1197       1.14  macallan #else
   1198        1.1  macallan 	if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
   1199        1.1  macallan 	     (col < ri->ri_cols)) {
   1200        1.1  macallan 		pos = col + row * ri->ri_cols;
   1201        1.1  macallan 		scr->scr_attrs[pos] = attr;
   1202        1.1  macallan 		scr->scr_chars[pos] = c;
   1203        1.1  macallan 	}
   1204       1.14  macallan #endif
   1205       1.20  jmcneill 
   1206       1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1207       1.22  jmcneill 	atomic_inc_uint(&scr->scr_dirty);
   1208       1.20  jmcneill #endif
   1209        1.1  macallan }
   1210        1.1  macallan 
   1211       1.25  macallan #ifdef VCONS_DRAW_INTR
   1212       1.25  macallan static void
   1213       1.25  macallan vcons_putchar_cached(void *cookie, int row, int col, u_int c, long attr)
   1214       1.25  macallan {
   1215       1.25  macallan 	struct rasops_info *ri = cookie;
   1216       1.25  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1217       1.25  macallan 	struct vcons_data *vd = scr->scr_vd;
   1218       1.25  macallan 	int pos = row * ri->ri_cols + col;
   1219       1.25  macallan 
   1220       1.25  macallan 	if ((vd->chars == NULL) || (vd->attrs == NULL)) {
   1221       1.37  macallan 		scr->putchar(cookie, row, col, c, attr);
   1222       1.25  macallan 		return;
   1223       1.25  macallan 	}
   1224       1.25  macallan 	if ((vd->chars[pos] != c) || (vd->attrs[pos] != attr)) {
   1225       1.25  macallan 		vd->attrs[pos] = attr;
   1226       1.25  macallan 		vd->chars[pos] = c;
   1227       1.37  macallan 		scr->putchar(cookie, row, col, c, attr);
   1228       1.25  macallan 	}
   1229       1.25  macallan }
   1230       1.25  macallan #endif
   1231       1.25  macallan 
   1232        1.1  macallan static void
   1233        1.1  macallan vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
   1234        1.1  macallan {
   1235        1.1  macallan 	struct rasops_info *ri = cookie;
   1236        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1237        1.1  macallan 
   1238        1.1  macallan 	vcons_putchar_buffer(cookie, row, col, c, attr);
   1239       1.21  jmcneill 
   1240       1.21  jmcneill #if defined(VCONS_DRAW_INTR)
   1241       1.21  jmcneill 	if (scr->scr_vd->use_intr)
   1242       1.21  jmcneill 		return;
   1243       1.21  jmcneill #endif
   1244       1.21  jmcneill 
   1245        1.1  macallan 	vcons_lock(scr);
   1246        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1247       1.25  macallan #ifdef VCONS_DRAW_INTR
   1248       1.25  macallan 		vcons_putchar_cached(cookie, row, col, c, attr);
   1249       1.25  macallan #else
   1250       1.37  macallan 		scr->putchar(cookie, row, col, c, attr);
   1251       1.25  macallan #endif
   1252        1.1  macallan 	}
   1253        1.1  macallan 	vcons_unlock(scr);
   1254        1.1  macallan }
   1255        1.1  macallan 
   1256        1.1  macallan static void
   1257        1.1  macallan vcons_cursor(void *cookie, int on, int row, int col)
   1258        1.1  macallan {
   1259        1.1  macallan 	struct rasops_info *ri = cookie;
   1260        1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1261        1.1  macallan 
   1262       1.20  jmcneill 
   1263       1.20  jmcneill #if defined(VCONS_DRAW_INTR)
   1264       1.21  jmcneill 	if (scr->scr_vd->use_intr) {
   1265       1.21  jmcneill 		vcons_lock(scr);
   1266       1.21  jmcneill 		if (scr->scr_ri.ri_crow != row || scr->scr_ri.ri_ccol != col) {
   1267       1.21  jmcneill 			scr->scr_ri.ri_crow = row;
   1268       1.21  jmcneill 			scr->scr_ri.ri_ccol = col;
   1269       1.22  jmcneill 			atomic_inc_uint(&scr->scr_dirty);
   1270       1.21  jmcneill 		}
   1271       1.21  jmcneill 		vcons_unlock(scr);
   1272       1.21  jmcneill 		return;
   1273       1.20  jmcneill 	}
   1274       1.21  jmcneill #endif
   1275       1.21  jmcneill 
   1276       1.21  jmcneill 	vcons_lock(scr);
   1277       1.21  jmcneill 
   1278        1.5  macallan 	if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
   1279       1.26  macallan 		scr->scr_vd->cursor(cookie, on, row, col);
   1280        1.1  macallan 	} else {
   1281        1.1  macallan 		scr->scr_ri.ri_crow = row;
   1282        1.1  macallan 		scr->scr_ri.ri_ccol = col;
   1283        1.1  macallan 	}
   1284        1.1  macallan 	vcons_unlock(scr);
   1285        1.1  macallan }
   1286        1.1  macallan 
   1287        1.1  macallan /* methods to read/write characters via ioctl() */
   1288        1.1  macallan 
   1289        1.1  macallan static int
   1290        1.6      jmmv vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
   1291        1.1  macallan {
   1292        1.1  macallan 	long attr;
   1293        1.6      jmmv 	struct rasops_info *ri;
   1294       1.35  christos 	int error;
   1295        1.6      jmmv 
   1296        1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
   1297        1.6      jmmv 
   1298       1.13    rumble 	ri = &scr->scr_ri;
   1299       1.13    rumble 
   1300       1.12       mjf 	if (__predict_false((unsigned int)wsc->col > ri->ri_cols ||
   1301       1.12       mjf 	    (unsigned int)wsc->row > ri->ri_rows))
   1302       1.12       mjf 			return (EINVAL);
   1303        1.1  macallan 
   1304       1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
   1305       1.14  macallan 	     (wsc->col < ri->ri_cols)) {
   1306       1.14  macallan 
   1307       1.35  christos 		error = ri->ri_ops.allocattr(ri, wsc->foreground,
   1308       1.35  christos 		    wsc->background, wsc->flags, &attr);
   1309       1.35  christos 		if (error)
   1310       1.35  christos 			return error;
   1311       1.14  macallan 		vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
   1312       1.14  macallan #ifdef VCONS_DEBUG
   1313       1.14  macallan 		printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col,
   1314       1.14  macallan 		    wsc->letter, attr);
   1315       1.14  macallan #endif
   1316       1.14  macallan 		return 0;
   1317       1.14  macallan 	} else
   1318       1.14  macallan 		return EINVAL;
   1319        1.1  macallan }
   1320        1.1  macallan 
   1321        1.1  macallan static int
   1322        1.6      jmmv vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
   1323        1.1  macallan {
   1324        1.6      jmmv 	int offset;
   1325        1.1  macallan 	long attr;
   1326        1.6      jmmv 	struct rasops_info *ri;
   1327        1.6      jmmv 
   1328        1.6      jmmv 	KASSERT(scr != NULL && wsc != NULL);
   1329        1.6      jmmv 
   1330        1.6      jmmv 	ri = &scr->scr_ri;
   1331        1.1  macallan 
   1332       1.14  macallan 	if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
   1333       1.14  macallan 	     (wsc->col < ri->ri_cols)) {
   1334       1.14  macallan 
   1335       1.14  macallan 		offset = ri->ri_cols * wsc->row + wsc->col;
   1336       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1337       1.14  macallan 		offset += scr->scr_offset_to_zero;
   1338       1.14  macallan #endif
   1339       1.14  macallan 		wsc->letter = scr->scr_chars[offset];
   1340       1.14  macallan 		attr = scr->scr_attrs[offset];
   1341       1.14  macallan 
   1342       1.14  macallan 		/*
   1343       1.14  macallan 		 * this is ugly. We need to break up an attribute into colours and
   1344       1.14  macallan 		 * flags but there's no rasops method to do that so we must rely on
   1345       1.14  macallan 		 * the 'canonical' encoding.
   1346       1.14  macallan 		 */
   1347       1.14  macallan #ifdef VCONS_DEBUG
   1348       1.14  macallan 		printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row,
   1349       1.14  macallan 		    wsc->col, wsc->letter, attr);
   1350       1.14  macallan #endif
   1351       1.14  macallan 		wsc->foreground = (attr >> 24) & 0xff;
   1352       1.14  macallan 		wsc->background = (attr >> 16) & 0xff;
   1353       1.14  macallan 		wsc->flags      = attr & 0xff;
   1354       1.14  macallan 		return 0;
   1355       1.14  macallan 	} else
   1356       1.14  macallan 		return EINVAL;
   1357       1.14  macallan }
   1358       1.14  macallan 
   1359       1.14  macallan #ifdef WSDISPLAY_SCROLLSUPPORT
   1360       1.14  macallan 
   1361       1.14  macallan static void
   1362       1.14  macallan vcons_scroll(void *cookie, void *vs, int where)
   1363       1.14  macallan {
   1364       1.14  macallan 	struct vcons_screen *scr = vs;
   1365       1.14  macallan 
   1366       1.14  macallan 	if (where == 0) {
   1367       1.14  macallan 		scr->scr_line_wanted = 0;
   1368       1.14  macallan 	} else {
   1369       1.14  macallan 		scr->scr_line_wanted = scr->scr_line_wanted - where;
   1370       1.14  macallan 		if (scr->scr_line_wanted < 0)
   1371       1.14  macallan 			scr->scr_line_wanted = 0;
   1372       1.14  macallan 		if (scr->scr_line_wanted > scr->scr_lines_in_buffer)
   1373       1.14  macallan 			scr->scr_line_wanted = scr->scr_lines_in_buffer;
   1374       1.14  macallan 	}
   1375       1.14  macallan 
   1376       1.14  macallan 	if (scr->scr_line_wanted != scr->scr_current_line) {
   1377       1.14  macallan 
   1378       1.14  macallan 		vcons_do_scroll(scr);
   1379       1.14  macallan 	}
   1380       1.14  macallan }
   1381       1.14  macallan 
   1382       1.14  macallan static void
   1383       1.14  macallan vcons_do_scroll(struct vcons_screen *scr)
   1384       1.14  macallan {
   1385       1.14  macallan 	int dist, from, to, num;
   1386       1.14  macallan 	int r_offset, r_start;
   1387       1.14  macallan 	int i, j;
   1388       1.14  macallan 
   1389       1.14  macallan 	if (scr->scr_line_wanted == scr->scr_current_line)
   1390       1.14  macallan 		return;
   1391       1.14  macallan 	dist = scr->scr_line_wanted - scr->scr_current_line;
   1392       1.14  macallan 	scr->scr_current_line = scr->scr_line_wanted;
   1393       1.14  macallan 	scr->scr_current_offset = scr->scr_ri.ri_cols *
   1394       1.14  macallan 	    (scr->scr_lines_in_buffer - scr->scr_current_line);
   1395       1.14  macallan 	if (abs(dist) >= scr->scr_ri.ri_rows) {
   1396       1.14  macallan 		vcons_redraw_screen(scr);
   1397       1.14  macallan 		return;
   1398       1.14  macallan 	}
   1399       1.14  macallan 	/* scroll and redraw only what we really have to */
   1400       1.14  macallan 	if (dist > 0) {
   1401       1.14  macallan 		/* we scroll down */
   1402       1.14  macallan 		from = 0;
   1403       1.14  macallan 		to = dist;
   1404       1.14  macallan 		num = scr->scr_ri.ri_rows - dist;
   1405       1.14  macallan 		/* now the redraw parameters */
   1406       1.14  macallan 		r_offset = scr->scr_current_offset;
   1407       1.14  macallan 		r_start = 0;
   1408       1.14  macallan 	} else {
   1409       1.14  macallan 		/* scrolling up */
   1410       1.14  macallan 		to = 0;
   1411       1.14  macallan 		from = -dist;
   1412       1.14  macallan 		num = scr->scr_ri.ri_rows + dist;
   1413       1.14  macallan 		r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
   1414       1.14  macallan 		r_start = num;
   1415       1.14  macallan 	}
   1416       1.14  macallan 	scr->scr_vd->copyrows(scr, from, to, num);
   1417       1.14  macallan 	for (i = 0; i < abs(dist); i++) {
   1418       1.14  macallan 		for (j = 0; j < scr->scr_ri.ri_cols; j++) {
   1419       1.25  macallan #ifdef VCONS_DRAW_INTR
   1420       1.25  macallan 			vcons_putchar_cached(scr, i + r_start, j,
   1421       1.25  macallan 			    scr->scr_chars[r_offset],
   1422       1.25  macallan 			    scr->scr_attrs[r_offset]);
   1423       1.25  macallan #else
   1424       1.37  macallan 			scr->putchar(scr, i + r_start, j,
   1425       1.14  macallan 			    scr->scr_chars[r_offset],
   1426       1.14  macallan 			    scr->scr_attrs[r_offset]);
   1427       1.25  macallan #endif
   1428       1.14  macallan 			r_offset++;
   1429       1.14  macallan 		}
   1430       1.14  macallan 	}
   1431       1.14  macallan 
   1432       1.14  macallan 	if (scr->scr_line_wanted == 0) {
   1433       1.14  macallan 		/* this was a reset - need to draw the cursor */
   1434       1.14  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
   1435       1.14  macallan 		scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow,
   1436       1.14  macallan 		    scr->scr_ri.ri_ccol);
   1437       1.14  macallan 	}
   1438       1.14  macallan }
   1439       1.14  macallan 
   1440       1.14  macallan #endif /* WSDISPLAY_SCROLLSUPPORT */
   1441       1.14  macallan 
   1442       1.20  jmcneill #ifdef VCONS_DRAW_INTR
   1443       1.20  jmcneill static void
   1444       1.20  jmcneill vcons_intr(void *cookie)
   1445       1.20  jmcneill {
   1446       1.20  jmcneill 	struct vcons_data *vd = cookie;
   1447       1.20  jmcneill 
   1448       1.22  jmcneill 	softint_schedule(vd->intr_softint);
   1449       1.20  jmcneill }
   1450       1.20  jmcneill 
   1451       1.20  jmcneill static void
   1452       1.22  jmcneill vcons_softintr(void *cookie)
   1453       1.20  jmcneill {
   1454       1.20  jmcneill 	struct vcons_data *vd = cookie;
   1455       1.20  jmcneill 	struct vcons_screen *scr = vd->active;
   1456       1.22  jmcneill 	unsigned int dirty;
   1457       1.20  jmcneill 
   1458       1.30   mlelstv 	if (scr && vd->use_intr) {
   1459       1.22  jmcneill 		if (!SCREEN_IS_BUSY(scr)) {
   1460       1.22  jmcneill 			dirty = atomic_swap_uint(&scr->scr_dirty, 0);
   1461       1.30   mlelstv 			if (vd->use_intr == 2) {
   1462       1.30   mlelstv 				if ((scr->scr_flags & VCONS_NO_REDRAW) == 0) {
   1463       1.30   mlelstv 					vd->use_intr = 1;
   1464       1.30   mlelstv 					vcons_redraw_screen(scr);
   1465       1.30   mlelstv 				}
   1466       1.30   mlelstv 			} else if (dirty > 0) {
   1467       1.22  jmcneill 				if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
   1468       1.25  macallan 					vcons_update_screen(scr);
   1469       1.22  jmcneill 			}
   1470       1.20  jmcneill 		}
   1471       1.20  jmcneill 	}
   1472       1.20  jmcneill 
   1473       1.20  jmcneill 	callout_schedule(&vd->intr, mstohz(33));
   1474       1.20  jmcneill }
   1475       1.21  jmcneill 
   1476       1.21  jmcneill static void
   1477  1.38.10.1  christos vcons_init_thread(void *cookie)
   1478       1.21  jmcneill {
   1479  1.38.10.1  christos 	struct vcons_data *vd = (struct vcons_data *)cookie;
   1480  1.38.10.1  christos 
   1481       1.30   mlelstv 	vd->use_intr = 2;
   1482       1.21  jmcneill 	callout_schedule(&vd->intr, mstohz(33));
   1483  1.38.10.1  christos 	kthread_exit(0);
   1484       1.21  jmcneill }
   1485       1.20  jmcneill #endif /* VCONS_DRAW_INTR */
   1486       1.23  jmcneill 
   1487       1.23  jmcneill void
   1488       1.23  jmcneill vcons_enable_polling(struct vcons_data *vd)
   1489       1.23  jmcneill {
   1490       1.23  jmcneill 	struct vcons_screen *scr = vd->active;
   1491       1.23  jmcneill 
   1492       1.23  jmcneill #ifdef VCONS_DRAW_INTR
   1493       1.23  jmcneill 	vd->use_intr = 0;
   1494       1.23  jmcneill #endif
   1495       1.23  jmcneill 
   1496       1.23  jmcneill 	if (scr && !SCREEN_IS_BUSY(scr)) {
   1497       1.23  jmcneill 		if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
   1498       1.23  jmcneill 			vcons_redraw_screen(scr);
   1499       1.23  jmcneill 	}
   1500       1.23  jmcneill }
   1501       1.23  jmcneill 
   1502       1.23  jmcneill void
   1503       1.23  jmcneill vcons_disable_polling(struct vcons_data *vd)
   1504       1.23  jmcneill {
   1505       1.23  jmcneill #ifdef VCONS_DRAW_INTR
   1506       1.23  jmcneill 	struct vcons_screen *scr = vd->active;
   1507       1.23  jmcneill 
   1508       1.23  jmcneill 	if (!vd->intr_valid)
   1509       1.23  jmcneill 		return;
   1510       1.23  jmcneill 
   1511       1.30   mlelstv 	vd->use_intr = 2;
   1512       1.23  jmcneill 	if (scr)
   1513       1.23  jmcneill 		atomic_inc_uint(&scr->scr_dirty);
   1514       1.23  jmcneill #endif
   1515       1.23  jmcneill }
   1516       1.24  jmcneill 
   1517       1.24  jmcneill void
   1518       1.24  jmcneill vcons_hard_switch(struct vcons_screen *scr)
   1519       1.24  jmcneill {
   1520       1.24  jmcneill 	struct vcons_data *vd = scr->scr_vd;
   1521       1.24  jmcneill 	struct vcons_screen *oldscr = vd->active;
   1522       1.24  jmcneill 
   1523       1.24  jmcneill 	if (oldscr) {
   1524       1.24  jmcneill 		SCREEN_INVISIBLE(oldscr);
   1525       1.24  jmcneill 		oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
   1526       1.24  jmcneill 	}
   1527       1.24  jmcneill 	SCREEN_VISIBLE(scr);
   1528       1.24  jmcneill 	vd->active = scr;
   1529       1.24  jmcneill 	vd->wanted = NULL;
   1530       1.24  jmcneill 
   1531       1.24  jmcneill 	if (vd->show_screen_cb != NULL)
   1532       1.38  macallan 		vd->show_screen_cb(scr, vd->show_screen_cookie);
   1533       1.24  jmcneill }
   1534       1.25  macallan 
   1535       1.25  macallan #ifdef VCONS_DRAW_INTR
   1536       1.25  macallan void
   1537       1.25  macallan vcons_invalidate_cache(struct vcons_data *vd)
   1538       1.25  macallan {
   1539       1.25  macallan 	int i;
   1540       1.25  macallan 
   1541       1.25  macallan 	if (vd->cells == 0)
   1542       1.25  macallan 		return;
   1543       1.25  macallan 
   1544       1.25  macallan 	for (i = 0; i > vd->cells; i++) {
   1545       1.25  macallan 		vd->chars[i] = -1;
   1546       1.25  macallan 		vd->attrs[i] = -1;
   1547       1.25  macallan 	}
   1548       1.25  macallan }
   1549       1.25  macallan #endif
   1550