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