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