Home | History | Annotate | Line # | Download | only in hpc
hpcfb.c revision 1.11
      1 /*	$NetBSD: hpcfb.c,v 1.11 2001/07/22 09:56:41 takemura Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999
      5  *         Shin Takemura and PocketBSD Project. All rights reserved.
      6  * Copyright (c) 2000,2001
      7  *         SATO Kazumi. All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the PocketBSD project
     20  *	and its contributors.
     21  * 4. Neither the name of the project nor the names of its contributors
     22  *    may be used to endorse or promote products derived from this software
     23  *    without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  * SUCH DAMAGE.
     36  *
     37  */
     38 
     39 /*
     40  * jump scroll, scroll thread, multiscreen, virtual text vram
     41  * and hpcfb_emulops functions
     42  * written by SATO Kazumi.
     43  */
     44 
     45 #define FBDEBUG
     46 static const char _copyright[] __attribute__ ((unused)) =
     47     "Copyright (c) 1999 Shin Takemura.  All rights reserved.";
     48 static const char _rcsid[] __attribute__ ((unused)) =
     49     "$NetBSD: hpcfb.c,v 1.11 2001/07/22 09:56:41 takemura Exp $";
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/kernel.h>
     54 #include <sys/signalvar.h>
     55 #include <sys/map.h>
     56 #include <sys/proc.h>
     57 #include <sys/kthread.h>
     58 #include <sys/lock.h>
     59 #include <sys/user.h>
     60 #include <sys/device.h>
     61 #include <sys/conf.h>
     62 #include <sys/malloc.h>
     63 #include <sys/buf.h>
     64 #include <sys/ioctl.h>
     65 
     66 #include <uvm/uvm_extern.h>
     67 
     68 #include <machine/bus.h>
     69 
     70 #include <dev/wscons/wsconsio.h>
     71 #include <dev/wscons/wsdisplayvar.h>
     72 #include <dev/wscons/wscons_callbacks.h>
     73 
     74 #include <dev/wsfont/wsfont.h>
     75 #include <dev/rasops/rasops.h>
     76 
     77 #include <dev/hpc/hpcfbvar.h>
     78 #include <dev/hpc/hpcfbio.h>
     79 
     80 #include "bivideo.h"
     81 #if NBIVIDEO > 0
     82 #include <dev/hpc/bivideovar.h>
     83 #endif
     84 
     85 #ifdef FBDEBUG
     86 int	hpcfb_debug = 0;
     87 #define	DPRINTF(arg) if (hpcfb_debug) printf arg;
     88 #else
     89 #define	DPRINTF(arg)
     90 #endif
     91 
     92 #ifndef HPCFB_MAX_COLUMN
     93 #define HPCFB_MAX_COLUMN 130
     94 #endif /* HPCFB_MAX_COLUMN */
     95 #ifndef HPCFB_MAX_ROW
     96 #define HPCFB_MAX_ROW 80
     97 #endif /* HPCFB_MAX_ROW */
     98 
     99 /*
    100  * currently experimental
    101 #define HPCFB_JUMP
    102 */
    103 
    104 struct hpcfb_vchar {
    105 	u_int c;
    106 	long attr;
    107 };
    108 
    109 struct hpcfb_tvrow {
    110 	int maxcol;
    111 	int spacecol;
    112 	struct hpcfb_vchar col[HPCFB_MAX_COLUMN];
    113 };
    114 
    115 struct hpcfb_devconfig {
    116 	struct rasops_info	dc_rinfo;	/* rasops information */
    117 
    118 	int		dc_blanked;	/* currently had video disabled */
    119 	struct hpcfb_softc *dc_sc;
    120 	int dc_rows;
    121 	int dc_cols;
    122 	struct hpcfb_tvrow *dc_tvram;
    123 	int dc_curx;
    124 	int dc_cury;
    125 #ifdef HPCFB_JUMP
    126 	int dc_min_row;
    127 	int dc_max_row;
    128 	int dc_scroll;
    129 	struct callout dc_scroll_ch;
    130 	int dc_scroll_src;
    131 	int dc_scroll_dst;
    132 	int dc_scroll_num;
    133 #endif /* HPCFB_JUMP */
    134 	volatile int dc_state;
    135 #define HPCFB_DC_CURRENT		0x80000000
    136 #define HPCFB_DC_DRAWING		0x01	/* drawing raster ops */
    137 #define HPCFB_DC_TDRAWING		0x02	/* drawing tvram */
    138 #define HPCFB_DC_SCROLLPENDING		0x04	/* scroll is pending */
    139 #define HPCFB_DC_UPDATE			0x08	/* tvram update */
    140 #define HPCFB_DC_SCRDELAY		0x10	/* scroll time but delay it */
    141 #define HPCFB_DC_SCRTHREAD		0x20	/* in scroll thread or callout */
    142 #define HPCFB_DC_UPDATEALL		0x40	/* need to redraw all */
    143 #define HPCFB_DC_ABORT			0x80	/* abort redrawing */
    144 	int	dc_memsize;
    145 	u_char *dc_fbaddr;
    146 };
    147 
    148 #define HPCFB_MAX_SCREEN 5
    149 #define HPCFB_MAX_JUMP 5
    150 
    151 struct hpcfb_softc {
    152 	struct	device sc_dev;
    153 	struct	hpcfb_devconfig *sc_dc;	/* device configuration */
    154 	const struct hpcfb_accessops	*sc_accessops;
    155 	void *sc_accessctx;
    156 	void *sc_powerhook;	/* power management hook */
    157 	struct device *sc_wsdisplay;
    158 	int sc_screen_resumed;
    159 	int sc_polling;
    160 	int sc_mapping;
    161 	struct proc *sc_thread;
    162 	struct lock sc_lock;
    163 	void *sc_wantedscreen;
    164 	void (*sc_switchcb)(void *, int, int);
    165 	void *sc_switchcbarg;
    166 	struct callout sc_switch_callout;
    167 	int sc_nfbconf;
    168 	struct hpcfb_fbconf *sc_fbconflist;
    169 };
    170 
    171 /*
    172  *  function prototypes
    173  */
    174 int	hpcfbmatch(struct device *, struct cfdata *, void *);
    175 void	hpcfbattach(struct device *, struct device *, void *);
    176 int	hpcfbprint(void *, const char *);
    177 
    178 int	hpcfb_ioctl(void *, u_long, caddr_t, int, struct proc *);
    179 paddr_t	hpcfb_mmap(void *, off_t, int);
    180 
    181 void	hpcfb_refresh_screen(struct hpcfb_softc *);
    182 void	hpcfb_doswitch(struct hpcfb_softc *);
    183 
    184 #ifdef HPCFB_JUMP
    185 static void	hpcfb_create_thread(void *);
    186 static void	hpcfb_thread(void *);
    187 #endif /* HPCFB_JUMP */
    188 
    189 static int	hpcfb_init(struct hpcfb_fbconf *, struct hpcfb_devconfig *);
    190 static int	hpcfb_alloc_screen(void *, const struct wsscreen_descr *,
    191 		    void **, int *, int *, long *);
    192 static void	hpcfb_free_screen(void *, void *);
    193 static int	hpcfb_show_screen(void *, void *, int,
    194 		    void (*) (void *, int, int), void *);
    195 static void     hpcfb_pollc(void *, int);
    196 static void	hpcfb_power(int, void *);
    197 static void	hpcfb_cmap_reorder(struct hpcfb_fbconf *,
    198 		    struct hpcfb_devconfig *);
    199 
    200 static int	pow(int, int);
    201 
    202 void    hpcfb_cursor(void *, int, int, int);
    203 int     hpcfb_mapchar(void *, int, unsigned int *);
    204 void    hpcfb_putchar(void *, int, int, u_int, long);
    205 void    hpcfb_copycols(void *, int, int, int, int);
    206 void    hpcfb_erasecols(void *, int, int, int, long);
    207 void    hpcfb_redraw(void *, int, int, int);
    208 void    hpcfb_copyrows(void *, int, int, int);
    209 void    hpcfb_eraserows(void *, int, int, long);
    210 int     hpcfb_alloc_attr(void *, int, int, int, long *);
    211 void    hpcfb_cursor_raw(void *, int, int, int);
    212 
    213 #ifdef HPCFB_JUMP
    214 void	hpcfb_update(void *);
    215 void	hpcfb_do_scroll(void *);
    216 void	hpcfb_check_update(void *);
    217 #endif /* HPCFB_JUMP */
    218 
    219 struct wsdisplay_emulops hpcfb_emulops = {
    220 	hpcfb_cursor,
    221 	hpcfb_mapchar,
    222 	hpcfb_putchar,
    223 	hpcfb_copycols,
    224 	hpcfb_erasecols,
    225 	hpcfb_copyrows,
    226 	hpcfb_eraserows,
    227 	hpcfb_alloc_attr
    228 };
    229 
    230 /*
    231  *  static variables
    232  */
    233 struct cfattach hpcfb_ca = {
    234 	sizeof(struct hpcfb_softc), hpcfbmatch, hpcfbattach,
    235 };
    236 
    237 struct wsscreen_descr hpcfb_stdscreen = {
    238 	"std",
    239 	0, 0,	/* will be filled in -- XXX shouldn't, it's global */
    240 	&hpcfb_emulops,	/* XXX */
    241 	0, 0,
    242 	WSSCREEN_REVERSE
    243 };
    244 
    245 const struct wsscreen_descr *_hpcfb_scrlist[] = {
    246 	&hpcfb_stdscreen,
    247 	/* XXX other formats, graphics screen? */
    248 };
    249 
    250 struct wsscreen_list hpcfb_screenlist = {
    251 	sizeof(_hpcfb_scrlist) / sizeof(struct wsscreen_descr *),
    252 	_hpcfb_scrlist
    253 };
    254 
    255 struct wsdisplay_accessops hpcfb_accessops = {
    256 	hpcfb_ioctl,
    257 	hpcfb_mmap,
    258 	hpcfb_alloc_screen,
    259 	hpcfb_free_screen,
    260 	hpcfb_show_screen,
    261 	0 /* load_font */,
    262 	hpcfb_pollc
    263 };
    264 
    265 void    hpcfb_tv_putchar(struct hpcfb_devconfig *, int, int, u_int, long);
    266 void    hpcfb_tv_copycols(struct hpcfb_devconfig *, int, int, int, int);
    267 void    hpcfb_tv_erasecols(struct hpcfb_devconfig *, int, int, int, long);
    268 void    hpcfb_tv_copyrows(struct hpcfb_devconfig *, int, int, int);
    269 void    hpcfb_tv_eraserows(struct hpcfb_devconfig *, int, int, long);
    270 
    271 struct wsdisplay_emulops rasops_emul;
    272 
    273 static int hpcfbconsole;
    274 struct hpcfb_devconfig hpcfb_console_dc;
    275 struct wsscreen_descr hpcfb_console_wsscreen;
    276 struct hpcfb_tvrow hpcfb_console_tvram[HPCFB_MAX_ROW];
    277 
    278 /*
    279  *  function bodies
    280  */
    281 static int
    282 pow(int x, int n)
    283 {
    284 	int res = 1;
    285 	while (0 < n--) {
    286 		res *= x;
    287 	}
    288 	return (res);
    289 }
    290 
    291 int
    292 hpcfbmatch(struct device *parent, struct cfdata *match, void *aux)
    293 {
    294 	return (1);
    295 }
    296 
    297 void
    298 hpcfbattach(struct device *parent, struct device *self, void *aux)
    299 {
    300 	struct hpcfb_softc *sc = (struct hpcfb_softc *)self;
    301 	struct hpcfb_attach_args *ha = aux;
    302 	struct wsemuldisplaydev_attach_args wa;
    303 
    304 	sc->sc_accessops = ha->ha_accessops;
    305 	sc->sc_accessctx = ha->ha_accessctx;
    306 	sc->sc_nfbconf = ha->ha_nfbconf;
    307 	sc->sc_fbconflist = ha->ha_fbconflist;
    308 
    309 	if (hpcfbconsole) {
    310 		sc->sc_dc = &hpcfb_console_dc;
    311 		hpcfb_console_dc.dc_sc = sc;
    312 		printf(": %dx%d pixels, %d colors, %dx%d chars",
    313 		    sc->sc_dc->dc_rinfo.ri_width,sc->sc_dc->dc_rinfo.ri_height,
    314 		    pow(2, sc->sc_dc->dc_rinfo.ri_depth),
    315 		    sc->sc_dc->dc_rinfo.ri_cols,sc->sc_dc->dc_rinfo.ri_rows);
    316 		/* Set video chip dependent CLUT if any. */
    317 		if (sc->sc_accessops->setclut)
    318 			sc->sc_accessops->setclut(sc->sc_accessctx,
    319 			    &hpcfb_console_dc.dc_rinfo);
    320 	}
    321 	printf("\n");
    322 
    323 	sc->sc_polling = 0; /* XXX */
    324 	sc->sc_mapping = 0; /* XXX */
    325 	callout_init(&sc->sc_switch_callout);
    326 
    327 	/* Add a power hook to power management */
    328 	sc->sc_powerhook = powerhook_establish(hpcfb_power, sc);
    329 	if (sc->sc_powerhook == NULL)
    330 		printf("%s: WARNING: unable to establish power hook\n",
    331 		    sc->sc_dev.dv_xname);
    332 
    333 	wa.console = hpcfbconsole;
    334 	wa.scrdata = &hpcfb_screenlist;
    335 	wa.accessops = &hpcfb_accessops;
    336 	wa.accesscookie = sc;
    337 
    338 	sc->sc_wsdisplay = config_found(self, &wa, wsemuldisplaydevprint);
    339 
    340 #ifdef HPCFB_JUMP
    341 	/*
    342 	 * Create a kernel thread to scroll,
    343 	 */
    344 	kthread_create(hpcfb_create_thread, sc);
    345 #endif /* HPCFB_JUMP */
    346 }
    347 
    348 #ifdef HPCFB_JUMP
    349 void
    350 hpcfb_create_thread(void *arg)
    351 {
    352 	struct hpcfb_softc *sc = arg;
    353 
    354 	if (kthread_create1(hpcfb_thread, sc, &sc->sc_thread,
    355 	    "%s", sc->sc_dev.dv_xname) == 0)
    356 		return;
    357 
    358 	/*
    359 	 * We were unable to create the HPCFB thread; bail out.
    360 	 */
    361 	sc->sc_thread = 0;
    362 	printf("%s: unable to create thread, kernel hpcfb scroll support disabled\n",
    363 	    sc->sc_dev.dv_xname);
    364 }
    365 
    366 void
    367 hpcfb_thread(void *arg)
    368 {
    369 	struct hpcfb_softc *sc = arg;
    370 
    371 	/*
    372 	 * Loop forever, doing a periodic check for update events.
    373 	 */
    374 	for (;;) {
    375 		/* HPCFB_LOCK(sc); */
    376 		sc->sc_dc->dc_state |= HPCFB_DC_SCRTHREAD;
    377 		if (!sc->sc_mapping) /* draw only EMUL mode */
    378 			hpcfb_update(sc->sc_dc);
    379 		sc->sc_dc->dc_state &= ~HPCFB_DC_SCRTHREAD;
    380 		/* APM_UNLOCK(sc); */
    381 		(void) tsleep(sc, PWAIT, "hpcfb",  (8 * hz) / 7 / 10);
    382 	}
    383 }
    384 #endif /* HPCFB_JUMP */
    385 
    386 /* Print function (for parent devices). */
    387 int
    388 hpcfbprint(void *aux, const char *pnp)
    389 {
    390 	if (pnp)
    391 		printf("hpcfb at %s", pnp);
    392 
    393 	return (UNCONF);
    394 }
    395 
    396 int
    397 hpcfb_cnattach(struct hpcfb_fbconf *fbconf)
    398 {
    399 	struct hpcfb_fbconf __fbconf __attribute__((__unused__));
    400 	long defattr;
    401 
    402 	DPRINTF(("%s(%d): hpcfb_cnattach()\n", __FILE__, __LINE__));
    403 #if NBIVIDEO > 0
    404 	if (fbconf == 0) {
    405 		memset(&__fbconf, 0, sizeof(struct hpcfb_fbconf));
    406 		if (bivideo_getcnfb(&__fbconf) != 0)
    407 			return (ENXIO);
    408 		fbconf = &__fbconf;
    409 	}
    410 #endif /* NBIVIDEO > 0 */
    411 	memset(&hpcfb_console_dc, 0, sizeof(struct hpcfb_devconfig));
    412 	if (hpcfb_init(fbconf, &hpcfb_console_dc) != 0)
    413 		return (ENXIO);
    414 	hpcfb_console_dc.dc_state |= HPCFB_DC_CURRENT;
    415 
    416 	hpcfb_console_dc.dc_tvram = hpcfb_console_tvram;
    417 	/* clear screen */
    418 	memset(hpcfb_console_tvram, 0, sizeof(hpcfb_console_tvram));
    419 	hpcfb_redraw(&hpcfb_console_dc, 0, hpcfb_console_dc.dc_rows, 1);
    420 
    421 	hpcfb_console_wsscreen = hpcfb_stdscreen;
    422 	hpcfb_console_wsscreen.nrows = hpcfb_console_dc.dc_rows;
    423 	hpcfb_console_wsscreen.ncols = hpcfb_console_dc.dc_cols;
    424 	hpcfb_console_wsscreen.capabilities = hpcfb_console_dc.dc_rinfo.ri_caps;
    425 	hpcfb_alloc_attr(&hpcfb_console_dc, 7, 0, 0, &defattr);
    426 	wsdisplay_cnattach(&hpcfb_console_wsscreen, &hpcfb_console_dc,
    427 	    0, 0, defattr);
    428 	hpcfbconsole = 1;
    429 
    430 	return (0);
    431 }
    432 
    433 int
    434 hpcfb_init(struct hpcfb_fbconf *fbconf,	struct hpcfb_devconfig *dc)
    435 {
    436 	struct rasops_info *ri;
    437 	vaddr_t fbaddr;
    438 
    439 	fbaddr = (vaddr_t)fbconf->hf_baseaddr;
    440 	dc->dc_fbaddr = (u_char *)fbaddr;
    441 
    442 	/* init rasops */
    443 	ri = &dc->dc_rinfo;
    444 	memset(ri, 0, sizeof(struct rasops_info));
    445 	ri->ri_depth = fbconf->hf_pixel_width;
    446 	ri->ri_bits = (caddr_t)fbaddr;
    447 	ri->ri_width = fbconf->hf_width;
    448 	ri->ri_height = fbconf->hf_height;
    449 	ri->ri_stride = fbconf->hf_bytes_per_line;
    450 #if 0
    451 	ri->ri_flg = RI_FORCEMONO | RI_CURSOR;
    452 #else
    453 	ri->ri_flg = RI_CURSOR;
    454 #endif
    455 	if (fbconf->hf_order_flags != 0) {
    456 		if (fbconf->hf_order_flags & HPCFB_REVORDER_BYTE) {
    457 #if BYTE_ORDER == BIG_ENDIAN
    458 			ri->ri_flg |= RI_BSWAP;
    459 #endif
    460 		} else {
    461 #if BYTE_ORDER == LITTLE_ENDIAN
    462 			ri->ri_flg |= RI_BSWAP;
    463 #endif
    464 		}
    465 	}
    466 
    467 	if (rasops_init(ri, HPCFB_MAX_ROW, HPCFB_MAX_COLUMN)) {
    468 		panic("%s(%d): rasops_init() failed!", __FILE__, __LINE__);
    469 	}
    470 
    471 	/* over write color map of rasops */
    472 	hpcfb_cmap_reorder (fbconf, dc);
    473 
    474 	dc->dc_curx = -1;
    475 	dc->dc_cury = -1;
    476 	dc->dc_rows = dc->dc_rinfo.ri_rows;
    477 	dc->dc_cols = dc->dc_rinfo.ri_cols;
    478 #ifdef HPCFB_JUMP
    479 	dc->dc_max_row = 0;
    480 	dc->dc_min_row = dc->dc_rows;
    481 	dc->dc_scroll = 0;
    482 	callout_init(&dc->dc_scroll_ch);
    483 #endif /* HPCFB_JUMP */
    484 	dc->dc_memsize = ri->ri_stride * ri->ri_height;
    485 	/* hook rasops in hpcfb_ops */
    486 	rasops_emul = ri->ri_ops; /* struct copy */
    487 	ri->ri_ops = hpcfb_emulops; /* struct copy */
    488 
    489 	return (0);
    490 }
    491 
    492 static void
    493 hpcfb_cmap_reorder(struct hpcfb_fbconf *fbconf, struct hpcfb_devconfig *dc)
    494 {
    495 	struct rasops_info *ri = &dc->dc_rinfo;
    496 	int reverse = fbconf->hf_access_flags & HPCFB_ACCESS_REVERSE;
    497 	int *cmap = ri->ri_devcmap;
    498 	int i, j, bg, fg, tmp;
    499 
    500 	/*
    501 	 * Set forground and background so that the screen
    502 	 * looks black on white.
    503 	 * Normally, black = 00 and white = ff.
    504 	 * HPCFB_ACCESS_REVERSE means black = ff and white = 00.
    505 	 */
    506 	switch (fbconf->hf_pixel_width) {
    507 	case 1:
    508 		/* FALLTHROUGH */
    509 	case 2:
    510 		/* FALLTHROUGH */
    511 	case 4:
    512 		if (reverse) {
    513 			bg = 0;
    514 			fg = ~0;
    515 		} else {
    516 			bg = ~0;
    517 			fg = 0;
    518 		}
    519 		/* for gray-scale LCD, hi-contrast color map */
    520 		cmap[0] = bg;
    521 		for (i = 1; i < 16; i++)
    522 			cmap[i] = fg;
    523 		break;
    524 	case 8:
    525 		/* FALLTHROUGH */
    526 	case 16:
    527 		if (reverse) {
    528 			for (i = 0, j = 15; i < 8; i++, j--) {
    529 				tmp = cmap[i];
    530 				cmap[i] = cmap[j];
    531 				cmap[j] = tmp;
    532 			}
    533 		}
    534 		break;
    535 	}
    536 }
    537 
    538 int
    539 hpcfb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    540 {
    541 	struct hpcfb_softc *sc = v;
    542 	struct hpcfb_devconfig *dc = sc->sc_dc;
    543 	struct wsdisplay_fbinfo *wdf;
    544 
    545 	DPRINTF(("hpcfb_ioctl(cmd=0x%lx)\n", cmd));
    546 	switch (cmd) {
    547 	case WSKBDIO_BELL:
    548 		return (0);
    549 		break;
    550 
    551 	case WSDISPLAYIO_GTYPE:
    552 		*(u_int *)data = WSDISPLAY_TYPE_HPCFB;
    553 		return (0);
    554 
    555 	case WSDISPLAYIO_GINFO:
    556 		wdf = (void *)data;
    557 		wdf->height = dc->dc_rinfo.ri_height;
    558 		wdf->width = dc->dc_rinfo.ri_width;
    559 		wdf->depth = dc->dc_rinfo.ri_depth;
    560 		wdf->cmsize = 256;	/* XXXX */
    561 		return (0);
    562 
    563 	case WSDISPLAYIO_SMODE:
    564 		if (*(int *)data == WSDISPLAYIO_MODE_EMUL){
    565 			if (sc->sc_mapping){
    566 				sc->sc_mapping = 0;
    567 				if (dc->dc_state&HPCFB_DC_DRAWING)
    568 					dc->dc_state &= ~HPCFB_DC_ABORT;
    569 #ifdef HPCFB_FORCE_REDRAW
    570 				hpcfb_refresh_screen(sc);
    571 #else
    572 				dc->dc_state |= HPCFB_DC_UPDATEALL;
    573 #endif
    574 			}
    575 		} else {
    576 			if (!sc->sc_mapping) {
    577 				sc->sc_mapping = 1;
    578 				dc->dc_state |= HPCFB_DC_ABORT;
    579 			}
    580 			sc->sc_mapping = 1;
    581 		}
    582 		if (sc && sc->sc_accessops->iodone)
    583 			(*sc->sc_accessops->iodone)(sc->sc_accessctx);
    584 		return (0);
    585 
    586 	case WSDISPLAYIO_GETCMAP:
    587 	case WSDISPLAYIO_PUTCMAP:
    588 	case WSDISPLAYIO_GETPARAM:
    589 	case WSDISPLAYIO_SETPARAM:
    590 	case HPCFBIO_GCONF:
    591 	case HPCFBIO_SCONF:
    592 	case HPCFBIO_GDSPCONF:
    593 	case HPCFBIO_SDSPCONF:
    594 	case HPCFBIO_GOP:
    595 	case HPCFBIO_SOP:
    596 		return ((*sc->sc_accessops->ioctl)(sc->sc_accessctx,
    597 		    cmd, data, flag, p));
    598 
    599 	default:
    600 		if (IOCGROUP(cmd) != 't')
    601 			DPRINTF(("%s(%d): hpcfb_ioctl(%lx, %lx) grp=%c num=%ld\n",
    602 			    __FILE__, __LINE__,
    603 			    cmd, (u_long)data, (char)IOCGROUP(cmd), cmd&0xff));
    604 		break;
    605 	}
    606 
    607 	return (ENOTTY); /* Inappropriate ioctl for device */
    608 }
    609 
    610 paddr_t
    611 hpcfb_mmap(void *v, off_t offset, int prot)
    612 {
    613 	struct hpcfb_softc *sc = v;
    614 
    615 	return ((*sc->sc_accessops->mmap)(sc->sc_accessctx, offset, prot));
    616 }
    617 
    618 static void
    619 hpcfb_power(int why, void *arg)
    620 {
    621 	struct hpcfb_softc *sc = arg;
    622 
    623 	switch (why) {
    624 	case PWR_STANDBY:
    625 		break;
    626 	case PWR_SOFTSUSPEND:
    627 		/* XXX, casting to 'struct wsdisplay_softc *' means
    628 		   that you should not call the method here... */
    629 		sc->sc_screen_resumed = wsdisplay_getactivescreen(
    630 			(struct wsdisplay_softc *)sc->sc_wsdisplay);
    631 		if (wsdisplay_switch(sc->sc_wsdisplay,
    632 		    WSDISPLAY_NULLSCREEN,
    633 		    1 /* waitok */) == 0) {
    634 			wsscreen_switchwait(
    635 				(struct wsdisplay_softc *)sc->sc_wsdisplay,
    636 				WSDISPLAY_NULLSCREEN);
    637 		} else {
    638 			sc->sc_screen_resumed = WSDISPLAY_NULLSCREEN;
    639 		}
    640 
    641 		sc->sc_dc->dc_state &= ~HPCFB_DC_CURRENT;
    642 		break;
    643 	case PWR_SOFTRESUME:
    644 		sc->sc_dc->dc_state |= HPCFB_DC_CURRENT;
    645 		if (sc->sc_screen_resumed != WSDISPLAY_NULLSCREEN)
    646 			wsdisplay_switch(sc->sc_wsdisplay,
    647 			    sc->sc_screen_resumed,
    648 			    1 /* waitok */);
    649 		break;
    650 	}
    651 }
    652 
    653 void
    654 hpcfb_refresh_screen(struct hpcfb_softc *sc)
    655 {
    656 	struct hpcfb_devconfig *dc = sc->sc_dc;
    657 	int x, y;
    658 
    659 	DPRINTF(("hpcfb_refres_screen()\n"));
    660 	if (dc == NULL)
    661 		return;
    662 
    663 #ifdef HPCFB_JUMP
    664 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
    665 		dc->dc_state &= ~HPCFB_DC_SCROLLPENDING;
    666 		dc->dc_state &= ~HPCFB_DC_UPDATE;
    667 		callout_stop(&dc->dc_scroll_ch);
    668 	}
    669 #endif /* HPCFB_JUMP */
    670 	/*
    671 	 * refresh screen
    672 	 */
    673 	dc->dc_state &= ~HPCFB_DC_UPDATEALL;
    674 	x = dc->dc_curx;
    675 	y = dc->dc_cury;
    676 	if (0 <= x && 0 <= y)
    677 		hpcfb_cursor_raw(dc, 0,  y, x); /* disable cursor */
    678 	/* redraw all text */
    679 	hpcfb_redraw(dc, 0, dc->dc_rows, 1);
    680 	if (0 <= x && 0 <= y)
    681 		hpcfb_cursor_raw(dc, 1,  y, x); /* enable cursor */
    682 }
    683 
    684 static int
    685 hpcfb_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
    686     int *curxp, int *curyp, long *attrp)
    687 {
    688 	struct hpcfb_softc *sc = v;
    689 	struct hpcfb_devconfig *dc;
    690 
    691 	DPRINTF(("%s(%d): hpcfb_alloc_screen()\n", __FILE__, __LINE__));
    692 
    693 	dc = malloc(sizeof(struct hpcfb_devconfig), M_DEVBUF, M_WAITOK);
    694 	if (dc == NULL)
    695 		return (ENOMEM);
    696 
    697 	memset(dc, 0, sizeof(struct hpcfb_devconfig));
    698 	dc->dc_sc = sc;
    699 	if (hpcfb_init(&sc->sc_fbconflist[0], dc) != 0)
    700 		return (EINVAL);
    701 	if (sc->sc_accessops->font) {
    702 		sc->sc_accessops->font(sc->sc_accessctx,
    703 		    dc->dc_rinfo.ri_font);
    704 	}
    705 	/* Set video chip dependent CLUT if any. */
    706 	if (sc->sc_accessops->setclut)
    707 		sc->sc_accessops->setclut(sc->sc_accessctx, &dc->dc_rinfo);
    708 	printf("hpcfb: %dx%d pixels, %d colors, %dx%d chars\n",
    709 	    dc->dc_rinfo.ri_width, dc->dc_rinfo.ri_height,
    710 	    pow(2, dc->dc_rinfo.ri_depth),
    711 	    dc->dc_rinfo.ri_cols, dc->dc_rinfo.ri_rows);
    712 
    713 	/*
    714 	 * XXX, wsdisplay won't reffer the information in wsscreen_descr
    715 	 * structure until alloc_screen will be called, at least, under
    716 	 * current implementation...
    717 	 */
    718 	hpcfb_stdscreen.nrows = dc->dc_rows;
    719         hpcfb_stdscreen.ncols = dc->dc_cols;
    720 	hpcfb_stdscreen.capabilities = dc->dc_rinfo.ri_caps;
    721 
    722 	dc->dc_fbaddr = dc->dc_rinfo.ri_bits;
    723 	dc->dc_rows = dc->dc_rinfo.ri_rows;
    724 	dc->dc_cols = dc->dc_rinfo.ri_cols;
    725 	dc->dc_memsize = dc->dc_rinfo.ri_stride * dc->dc_rinfo.ri_height;
    726 
    727 	dc->dc_curx = -1;
    728 	dc->dc_cury = -1;
    729 	dc->dc_tvram = malloc(sizeof(struct hpcfb_tvrow)*dc->dc_rows,
    730 	    M_DEVBUF, M_WAITOK);
    731 	if (dc->dc_tvram == NULL){
    732 		free(dc, M_DEVBUF);
    733 		return (ENOMEM);
    734 	}
    735 	memset(dc->dc_tvram, 0, sizeof(struct hpcfb_tvrow)*dc->dc_rows);
    736 
    737 	*curxp = 0;
    738 	*curyp = 0;
    739 	*cookiep = dc;
    740 	hpcfb_alloc_attr(*cookiep, 7, 0, 0, attrp);
    741 	DPRINTF(("%s(%d): hpcfb_alloc_screen(): 0x%p\n",
    742 	    __FILE__, __LINE__, dc));
    743 
    744 	return (0);
    745 }
    746 
    747 static void
    748 hpcfb_free_screen(void *v, void *cookie)
    749 {
    750 	struct hpcfb_devconfig *dc = cookie;
    751 
    752 	DPRINTF(("%s(%d): hpcfb_free_screen(0x%p)\n",
    753 	    __FILE__, __LINE__, cookie));
    754 #ifdef DIAGNOSTIC
    755 	if (dc == &hpcfb_console_dc)
    756 		panic("hpcfb_free_screen: console");
    757 #endif
    758 	free(dc->dc_tvram, M_DEVBUF);
    759 	free(dc, M_DEVBUF);
    760 }
    761 
    762 static int
    763 hpcfb_show_screen(void *v, void *cookie, int waitok,
    764     void (*cb)(void *, int, int), void *cbarg)
    765 {
    766 	struct hpcfb_softc *sc = v;
    767 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
    768 	struct hpcfb_devconfig *odc;
    769 
    770 	DPRINTF(("%s(%d): hpcfb_show_screen(0x%p)\n",
    771 	    __FILE__, __LINE__, dc));
    772 
    773 	odc = sc->sc_dc;
    774 
    775 	if (dc == NULL || odc == dc) {
    776 		hpcfb_refresh_screen(sc);
    777 		return (0);
    778 	}
    779 
    780 	sc->sc_wantedscreen = cookie;
    781 	sc->sc_switchcb = cb;
    782 	sc->sc_switchcbarg = cbarg;
    783 	if (cb) {
    784 		callout_reset(&sc->sc_switch_callout, 0,
    785 		    (void(*)(void *))hpcfb_doswitch, sc);
    786 		return (EAGAIN);
    787 	}
    788 
    789 	hpcfb_doswitch(sc);
    790 	return (0);
    791 }
    792 
    793 void
    794 hpcfb_doswitch(struct hpcfb_softc *sc)
    795 {
    796 	struct hpcfb_devconfig *dc;
    797 	struct hpcfb_devconfig *odc;
    798 
    799 	DPRINTF(("hpcfb_doswitch()\n"));
    800 	odc = sc->sc_dc;
    801 	dc = sc->sc_wantedscreen;
    802 
    803 	if (!dc) {
    804 		(*sc->sc_switchcb)(sc->sc_switchcbarg, EIO, 0);
    805 		return;
    806 	}
    807 
    808 	if (odc == dc)
    809 		return;
    810 
    811 	if (odc) {
    812 #ifdef HPCFB_JUMP
    813 		odc->dc_state |= HPCFB_DC_ABORT;
    814 #endif /* HPCFB_JUMP */
    815 
    816 		if (odc->dc_curx >= 0 && odc->dc_cury >= 0)
    817 			hpcfb_cursor_raw(odc, 0,  odc->dc_cury, odc->dc_curx);
    818 		/* disable cursor */
    819 		/* disable old screen */
    820 		odc->dc_state &= ~HPCFB_DC_CURRENT;
    821 		/* XXX, This is too dangerous.
    822 		odc->dc_rinfo.ri_bits = NULL;
    823 		*/
    824 	}
    825 	/* switch screen to new one */
    826 	dc->dc_state |= HPCFB_DC_CURRENT;
    827 	dc->dc_rinfo.ri_bits = dc->dc_fbaddr;
    828 	sc->sc_dc = dc;
    829 
    830 	/* redraw screen image */
    831 	hpcfb_refresh_screen(sc);
    832 
    833 	sc->sc_wantedscreen = NULL;
    834 	if (sc->sc_switchcb)
    835 		(*sc->sc_switchcb)(sc->sc_switchcbarg, 0, 0);
    836 
    837 	return;
    838 }
    839 
    840 static void
    841 hpcfb_pollc(void *v, int on)
    842 {
    843 	struct hpcfb_softc *sc = v;
    844 
    845 	if (sc == NULL)
    846 		return;
    847 	sc->sc_polling = on;
    848 	if (sc->sc_accessops->iodone)
    849 		(*sc->sc_accessops->iodone)(sc->sc_accessctx);
    850 	if (on) {
    851 		hpcfb_refresh_screen(sc);
    852 		if (sc->sc_accessops->iodone)
    853 			(*sc->sc_accessops->iodone)(sc->sc_accessctx);
    854 	}
    855 
    856 	return;
    857 }
    858 
    859 /*
    860  * cursor
    861  */
    862 void
    863 hpcfb_cursor(void *cookie, int on, int row, int col)
    864 {
    865 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
    866 
    867 	if (on) {
    868 		dc->dc_curx = col;
    869 		dc->dc_cury = row;
    870 	} else {
    871 		dc->dc_curx = -1;
    872 		dc->dc_cury = -1;
    873 	}
    874 
    875 	hpcfb_cursor_raw(cookie, on, row, col);
    876 }
    877 
    878 void
    879 hpcfb_cursor_raw(cookie, on, row, col)
    880 	void *cookie;
    881 	int on, row, col;
    882 {
    883 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
    884 	struct hpcfb_softc *sc = dc->dc_sc;
    885 	struct rasops_info *ri = &dc->dc_rinfo;
    886 	int curwidth, curheight;
    887 	int xoff, yoff;
    888 
    889 #ifdef HPCFB_JUMP
    890 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
    891 		dc->dc_state |= HPCFB_DC_UPDATE;
    892 		return;
    893 	}
    894 #endif /* HPCFB_JUMP */
    895 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
    896 		return;
    897 
    898 	if (ri->ri_bits == NULL)
    899 		return;
    900 
    901 	dc->dc_state |= HPCFB_DC_DRAWING;
    902 	if (sc && sc->sc_accessops->cursor) {
    903 		xoff = col * ri->ri_font->fontwidth;
    904 		yoff = row * ri->ri_font->fontheight;
    905 		curheight = ri->ri_font->fontheight;
    906 		curwidth = ri->ri_font->fontwidth;
    907 		(*sc->sc_accessops->cursor)(sc->sc_accessctx,
    908 		    on, xoff, yoff, curwidth, curheight);
    909 	} else
    910 		rasops_emul.cursor(ri, on, row, col);
    911 	dc->dc_state &= ~HPCFB_DC_DRAWING;
    912 }
    913 
    914 /*
    915  * mapchar
    916  */
    917 int
    918 hpcfb_mapchar(void *cookie, int c, unsigned int *cp)
    919 {
    920 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
    921 	struct rasops_info *ri = &dc->dc_rinfo;
    922 
    923 	return (rasops_emul.mapchar(ri, c, cp));
    924 }
    925 
    926 /*
    927  * putchar
    928  */
    929 void
    930 hpcfb_tv_putchar(struct hpcfb_devconfig *dc, int row, int col, u_int uc,
    931     long attr)
    932 {
    933 	struct hpcfb_tvrow *vscn = dc->dc_tvram;
    934 	struct hpcfb_vchar *vc = &vscn[row].col[col];
    935 	struct hpcfb_vchar *vcb;
    936 
    937 	if (vscn == 0)
    938 		return;
    939 
    940 	dc->dc_state |= HPCFB_DC_TDRAWING;
    941 #ifdef HPCFB_JUMP
    942 	if (row < dc->dc_min_row)
    943 		dc->dc_min_row = row;
    944 	if (row > dc->dc_max_row)
    945 		dc->dc_max_row = row;
    946 
    947 #endif /* HPCFB_JUMP */
    948 	if (vscn[row].maxcol +1 == col)
    949 		vscn[row].maxcol = col;
    950 	else if (vscn[row].maxcol < col) {
    951 		vcb =  &vscn[row].col[vscn[row].maxcol+1];
    952 		memset(vcb, 0,
    953 		    sizeof(struct hpcfb_vchar)*(col-vscn[row].maxcol-1));
    954 		vscn[row].maxcol = col;
    955 	}
    956 	vc->c = uc;
    957 	vc->attr = attr;
    958 	dc->dc_state &= ~HPCFB_DC_TDRAWING;
    959 #ifdef HPCFB_JUMP
    960 	hpcfb_check_update(dc);
    961 #endif /* HPCFB_JUMP */
    962 }
    963 
    964 void
    965 hpcfb_putchar(void *cookie, int row, int col, u_int uc, long attr)
    966 {
    967 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
    968 	struct hpcfb_softc *sc = dc->dc_sc;
    969 	struct rasops_info *ri = &dc->dc_rinfo;
    970 	int xoff;
    971 	int yoff;
    972 	int fclr, uclr;
    973 	struct wsdisplay_font *font;
    974 
    975 	hpcfb_tv_putchar(dc, row, col, uc, attr);
    976 #ifdef HPCFB_JUMP
    977 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
    978 		dc->dc_state |= HPCFB_DC_UPDATE;
    979 		return;
    980 	}
    981 #endif /* HPCFB_JUMP */
    982 
    983 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
    984 		return;
    985 	if (ri->ri_bits == NULL)
    986 		return;
    987 
    988 	dc->dc_state |= HPCFB_DC_DRAWING;
    989 	if (sc && sc->sc_accessops->putchar
    990 	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
    991 		font = ri->ri_font;
    992 		yoff = row * ri->ri_font->fontheight;
    993 		xoff =  col * ri->ri_font->fontwidth;
    994 		fclr = ri->ri_devcmap[((u_int)attr >> 24) & 15];
    995 		uclr = ri->ri_devcmap[((u_int)attr >> 16) & 15];
    996 
    997 		(*sc->sc_accessops->putchar)(sc->sc_accessctx,
    998 		    xoff, yoff, font, fclr, uclr, uc, attr);
    999 	} else
   1000 		rasops_emul.putchar(ri, row, col, uc, attr);
   1001 	dc->dc_state &= ~HPCFB_DC_DRAWING;
   1002 #ifdef HPCFB_JUMP
   1003 	hpcfb_check_update(dc);
   1004 #endif /* HPCFB_JUMP */
   1005 }
   1006 
   1007 /*
   1008  * copycols
   1009  */
   1010 void
   1011 hpcfb_tv_copycols(struct hpcfb_devconfig *dc, int row, int srccol, int dstcol,
   1012     int ncols)
   1013 {
   1014 	struct hpcfb_tvrow *vscn = dc->dc_tvram;
   1015 	struct hpcfb_vchar *svc = &vscn[row].col[srccol];
   1016 	struct hpcfb_vchar *dvc = &vscn[row].col[dstcol];
   1017 
   1018 	if (vscn == 0)
   1019 		return;
   1020 
   1021 	dc->dc_state |= HPCFB_DC_TDRAWING;
   1022 #ifdef HPCFB_JUMP
   1023 	if (row < dc->dc_min_row)
   1024 		dc->dc_min_row = row;
   1025 	if (row > dc->dc_max_row)
   1026 		dc->dc_max_row = row;
   1027 #endif /* HPCFB_JUMP */
   1028 
   1029 	memcpy(dvc, svc, ncols*sizeof(struct hpcfb_vchar));
   1030 	if (vscn[row].maxcol < srccol+ncols-1)
   1031 		vscn[row].maxcol = srccol+ncols-1;
   1032 	if (vscn[row].maxcol < dstcol+ncols-1)
   1033 		vscn[row].maxcol = dstcol+ncols-1;
   1034 	dc->dc_state &= ~HPCFB_DC_TDRAWING;
   1035 #ifdef HPCFB_JUMP
   1036 	hpcfb_check_update(dc);
   1037 #endif /* HPCFB_JUMP */
   1038 }
   1039 
   1040 void
   1041 hpcfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1042 {
   1043 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
   1044 	struct hpcfb_softc *sc = dc->dc_sc;
   1045 	struct rasops_info *ri = &dc->dc_rinfo;
   1046 	int srcxoff,dstxoff;
   1047 	int srcyoff,dstyoff;
   1048 	int height, width;
   1049 
   1050 	hpcfb_tv_copycols(dc, row, srccol, dstcol, ncols);
   1051 #ifdef HPCFB_JUMP
   1052 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
   1053 		dc->dc_state |= HPCFB_DC_UPDATE;
   1054 		return;
   1055 	}
   1056 #endif /* HPCFB_JUMP */
   1057 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
   1058 		return;
   1059 	if (ri->ri_bits == NULL)
   1060 		return;
   1061 
   1062 	dc->dc_state |= HPCFB_DC_DRAWING;
   1063 	if (sc && sc->sc_accessops->bitblit
   1064 	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
   1065 		srcxoff = srccol * ri->ri_font->fontwidth;
   1066 		srcyoff = row * ri->ri_font->fontheight;
   1067 		dstxoff = dstcol * ri->ri_font->fontwidth;
   1068 		dstyoff = row * ri->ri_font->fontheight;
   1069 		width = ncols * ri->ri_font->fontwidth;
   1070 		height = ri->ri_font->fontheight;
   1071 		(*sc->sc_accessops->bitblit)(sc->sc_accessctx,
   1072 		    srcxoff, srcyoff, dstxoff, dstyoff, height, width);
   1073 	} else
   1074 		rasops_emul.copycols(ri, row, srccol, dstcol, ncols);
   1075 	dc->dc_state &= ~HPCFB_DC_DRAWING;
   1076 #ifdef HPCFB_JUMP
   1077 	hpcfb_check_update(dc);
   1078 #endif /* HPCFB_JUMP */
   1079 }
   1080 
   1081 
   1082 /*
   1083  * erasecols
   1084  */
   1085 void
   1086 hpcfb_tv_erasecols(struct hpcfb_devconfig *dc, int row, int startcol,
   1087     int ncols, long attr)
   1088 {
   1089 	struct hpcfb_tvrow *vscn = dc->dc_tvram;
   1090 
   1091 	if (vscn == 0)
   1092 		return;
   1093 
   1094 	dc->dc_state |= HPCFB_DC_TDRAWING;
   1095 #ifdef HPCFB_JUMP
   1096 	if (row < dc->dc_min_row)
   1097 		dc->dc_min_row = row;
   1098 	if (row > dc->dc_max_row)
   1099 		dc->dc_max_row = row;
   1100 #endif /* HPCFB_JUMP */
   1101 
   1102 	vscn[row].maxcol = startcol-1;
   1103 	if (vscn[row].spacecol < startcol+ncols-1)
   1104 		vscn[row].spacecol = startcol+ncols-1;
   1105 	dc->dc_state &= ~HPCFB_DC_TDRAWING;
   1106 #ifdef HPCFB_JUMP
   1107 	hpcfb_check_update(dc);
   1108 #endif /* HPCFB_JUMP */
   1109 }
   1110 
   1111 void
   1112 hpcfb_erasecols(void *cookie, int row, int startcol, int ncols, long attr)
   1113 {
   1114 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
   1115 	struct hpcfb_softc *sc = dc->dc_sc;
   1116 	struct rasops_info *ri = &dc->dc_rinfo;
   1117 	int xoff, yoff;
   1118 	int width, height;
   1119 
   1120 	hpcfb_tv_erasecols(dc, row, startcol, ncols, attr);
   1121 #ifdef HPCFB_JUMP
   1122 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
   1123 		dc->dc_state |= HPCFB_DC_UPDATE;
   1124 		return;
   1125 	}
   1126 #endif /* HPCFB_JUMP */
   1127 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
   1128 		return;
   1129 	if (ri->ri_bits == NULL)
   1130 		return;
   1131 
   1132 	dc->dc_state |= HPCFB_DC_DRAWING;
   1133 	if (sc && sc->sc_accessops->erase
   1134 	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
   1135 		xoff = startcol * ri->ri_font->fontwidth;
   1136 		yoff = row * ri->ri_font->fontheight;
   1137 		width = ncols * ri->ri_font->fontwidth;
   1138 		height = ri->ri_font->fontheight;
   1139 		(*sc->sc_accessops->erase)(sc->sc_accessctx,
   1140 		    xoff, yoff, height, width, attr);
   1141 	} else
   1142 		rasops_emul.erasecols(ri, row, startcol, ncols, attr);
   1143 	dc->dc_state &= ~HPCFB_DC_DRAWING;
   1144 #ifdef HPCFB_JUMP
   1145 	hpcfb_check_update(dc);
   1146 #endif /* HPCFB_JUMP */
   1147 }
   1148 
   1149 /*
   1150  * Copy rows.
   1151  */
   1152 void
   1153 hpcfb_tv_copyrows(struct hpcfb_devconfig *dc, int src, int dst, int num)
   1154 {
   1155 	struct hpcfb_tvrow *vscn = dc->dc_tvram;
   1156 	struct hpcfb_tvrow *svc = &vscn[src];
   1157 	struct hpcfb_tvrow *dvc = &vscn[dst];
   1158 	int i;
   1159 	int d;
   1160 
   1161 	if (vscn == 0)
   1162 		return;
   1163 
   1164 	dc->dc_state |= HPCFB_DC_TDRAWING;
   1165 #ifdef HPCFB_JUMP
   1166 	if (dst < dc->dc_min_row)
   1167 		dc->dc_min_row = dst;
   1168 	if (dst + num > dc->dc_max_row)
   1169 		dc->dc_max_row = dst + num -1;
   1170 #endif /* HPCFB_JUMP */
   1171 
   1172 	if (svc > dvc)
   1173 		d = 1;
   1174 	else if (svc < dvc) {
   1175 		svc += num-1;
   1176 		dvc += num-1;
   1177 		d = -1;
   1178 	} else  {
   1179 		dc->dc_state &= ~HPCFB_DC_TDRAWING;
   1180 #ifdef HPCFB_JUMP
   1181 		hpcfb_check_update(dc);
   1182 #endif /* HPCFB_JUMP */
   1183 		return;
   1184 	}
   1185 
   1186 	for (i = 0; i < num; i++) {
   1187 		memcpy(&dvc->col[0], &svc->col[0], sizeof(struct hpcfb_vchar)*(svc->maxcol+1));
   1188 		if (svc->maxcol < dvc->maxcol && dvc->spacecol < dvc->maxcol)
   1189 			dvc->spacecol = dvc->maxcol;
   1190 		dvc->maxcol = svc->maxcol;
   1191 		svc+=d;
   1192 		dvc+=d;
   1193 	}
   1194 	dc->dc_state &= ~HPCFB_DC_TDRAWING;
   1195 #ifdef HPCFB_JUMP
   1196 	hpcfb_check_update(dc);
   1197 #endif /* HPCFB_JUMP */
   1198 }
   1199 
   1200 void
   1201 hpcfb_redraw(cookie, row, num, all)
   1202 	void *cookie;
   1203 	int row, num;
   1204 	int all;
   1205 {
   1206 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
   1207 	struct rasops_info *ri = &dc->dc_rinfo;
   1208 	int cols;
   1209 	struct hpcfb_tvrow *vscn = dc->dc_tvram;
   1210 	struct hpcfb_vchar *svc;
   1211 	int i, j;
   1212 
   1213 #ifdef HPCFB_JUMP
   1214 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
   1215 		dc->dc_state |= HPCFB_DC_UPDATE;
   1216 		return;
   1217 	}
   1218 #endif /* HPCFB_JUMP */
   1219 	if (dc->dc_sc != NULL
   1220 	    && !dc->dc_sc->sc_polling
   1221 	    && dc->dc_sc->sc_mapping)
   1222 		return;
   1223 
   1224 	dc->dc_state &= ~HPCFB_DC_ABORT;
   1225 
   1226 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
   1227 		return;
   1228 	if (vscn == 0)
   1229 		return;
   1230 
   1231 	if (ri->ri_bits == NULL)
   1232 		return;
   1233 
   1234 	dc->dc_state |= HPCFB_DC_DRAWING;
   1235 	dc->dc_state |= HPCFB_DC_TDRAWING;
   1236 	for (i = 0; i < num; i++) {
   1237 		if (dc->dc_state&HPCFB_DC_ABORT)
   1238 			break;
   1239 		cols = vscn[row+i].maxcol;
   1240 		for (j = 0; j <= cols; j++) {
   1241 			if (dc->dc_state&HPCFB_DC_ABORT)
   1242 				continue;
   1243 			svc = &vscn[row+i].col[j];
   1244 			rasops_emul.putchar(ri, row + i, j, svc->c, svc->attr);
   1245 		}
   1246 		if (all)
   1247 			cols = dc->dc_cols-1;
   1248 		else
   1249 			cols = vscn[row+i].spacecol;
   1250 		for (; j <= cols; j++) {
   1251 			if (dc->dc_state&HPCFB_DC_ABORT)
   1252 				continue;
   1253 			rasops_emul.putchar(ri, row + i, j, ' ', 0);
   1254 		}
   1255 		vscn[row+i].spacecol = 0;
   1256 	}
   1257 	if (dc->dc_state&HPCFB_DC_ABORT)
   1258 		dc->dc_state &= ~HPCFB_DC_ABORT;
   1259 	dc->dc_state &= ~HPCFB_DC_DRAWING;
   1260 	dc->dc_state &= ~HPCFB_DC_TDRAWING;
   1261 #ifdef HPCFB_JUMP
   1262 	hpcfb_check_update(dc);
   1263 #endif /* HPCFB_JUMP */
   1264 }
   1265 
   1266 #ifdef HPCFB_JUMP
   1267 void
   1268 hpcfb_update(void *v)
   1269 {
   1270 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)v;
   1271 
   1272 	/* callout_stop(&dc->dc_scroll_ch); */
   1273 	dc->dc_state &= ~HPCFB_DC_SCROLLPENDING;
   1274 	if (dc->dc_curx > 0 && dc->dc_cury > 0)
   1275 		hpcfb_cursor_raw(dc, 0,  dc->dc_cury, dc->dc_curx);
   1276 	if ((dc->dc_state&HPCFB_DC_UPDATEALL)) {
   1277 		hpcfb_redraw(dc, 0, dc->dc_rows, 1);
   1278 		dc->dc_state &= ~(HPCFB_DC_UPDATE|HPCFB_DC_UPDATEALL);
   1279 	} else if ((dc->dc_state&HPCFB_DC_UPDATE)) {
   1280 		hpcfb_redraw(dc, dc->dc_min_row,
   1281 		    dc->dc_max_row - dc->dc_min_row, 0);
   1282 		dc->dc_state &= ~HPCFB_DC_UPDATE;
   1283 	} else {
   1284 		hpcfb_redraw(dc, dc->dc_scroll_dst, dc->dc_scroll_num, 0);
   1285 	}
   1286 	if (dc->dc_curx > 0 && dc->dc_cury > 0)
   1287 		hpcfb_cursor_raw(dc, 1,  dc->dc_cury, dc->dc_curx);
   1288 }
   1289 
   1290 void
   1291 hpcfb_do_scroll(void *v)
   1292 {
   1293 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)v;
   1294 
   1295 	dc->dc_state |= HPCFB_DC_SCRTHREAD;
   1296 	if (dc->dc_state&(HPCFB_DC_DRAWING|HPCFB_DC_TDRAWING))
   1297 		dc->dc_state |= HPCFB_DC_SCRDELAY;
   1298 	else if (dc->dc_sc != NULL && dc->dc_sc->sc_thread)
   1299 		wakeup(dc->dc_sc);
   1300 	else if (dc->dc_sc != NULL && !dc->dc_sc->sc_mapping) {
   1301 		/* draw only EMUL mode */
   1302 		hpcfb_update(v);
   1303 	}
   1304 	dc->dc_state &= ~HPCFB_DC_SCRTHREAD;
   1305 }
   1306 
   1307 void
   1308 hpcfb_check_update(void *v)
   1309 {
   1310 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)v;
   1311 
   1312 	if (dc->dc_sc != NULL
   1313 	    && dc->dc_sc->sc_polling
   1314 	    && (dc->dc_state&HPCFB_DC_SCROLLPENDING)){
   1315 		callout_stop(&dc->dc_scroll_ch);
   1316 		dc->dc_state &= ~HPCFB_DC_SCRDELAY;
   1317 		hpcfb_update(v);
   1318 	}
   1319 	else if (dc->dc_state&HPCFB_DC_SCRDELAY){
   1320 		dc->dc_state &= ~HPCFB_DC_SCRDELAY;
   1321 		hpcfb_update(v);
   1322 	} else if (dc->dc_state&HPCFB_DC_UPDATEALL){
   1323 		dc->dc_state &= ~HPCFB_DC_UPDATEALL;
   1324 		hpcfb_update(v);
   1325 	}
   1326 }
   1327 #endif /* HPCFB_JUMP */
   1328 
   1329 void
   1330 hpcfb_copyrows(void *cookie, int src, int dst, int num)
   1331 {
   1332 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
   1333 	struct rasops_info *ri = &dc->dc_rinfo;
   1334 	struct hpcfb_softc *sc = dc->dc_sc;
   1335 	int srcyoff, dstyoff;
   1336 	int width, height;
   1337 
   1338 	hpcfb_tv_copyrows(cookie, src, dst, num);
   1339 
   1340 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
   1341 		return;
   1342 	if (ri->ri_bits == NULL)
   1343 		return;
   1344 
   1345 	if (sc && sc->sc_accessops->bitblit
   1346 	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
   1347 		dc->dc_state |= HPCFB_DC_DRAWING;
   1348 		srcyoff = src * ri->ri_font->fontheight;
   1349 		dstyoff = dst * ri->ri_font->fontheight;
   1350 		width = dc->dc_cols * ri->ri_font->fontwidth;
   1351 		height = num * ri->ri_font->fontheight;
   1352 		(*sc->sc_accessops->bitblit)(sc->sc_accessctx,
   1353 		    0, srcyoff, 0, dstyoff, height, width);
   1354 		dc->dc_state &= ~HPCFB_DC_DRAWING;
   1355 	}
   1356 	else {
   1357 #ifdef HPCFB_JUMP
   1358 		if (sc && sc->sc_polling) {
   1359 			hpcfb_check_update(dc);
   1360 		} else if ((dc->dc_state&HPCFB_DC_SCROLLPENDING) == 0) {
   1361 			dc->dc_state |= HPCFB_DC_SCROLLPENDING;
   1362 			dc->dc_scroll = 1;
   1363 			dc->dc_scroll_src = src;
   1364 			dc->dc_scroll_dst = dst;
   1365 			dc->dc_scroll_num = num;
   1366 			callout_reset(&dc->dc_scroll_ch, hz/100, &hpcfb_do_scroll, dc);
   1367 			return;
   1368 		} else if (dc->dc_scroll++ < dc->dc_rows/HPCFB_MAX_JUMP) {
   1369 			dc->dc_state |= HPCFB_DC_UPDATE;
   1370 			return;
   1371 		} else {
   1372 			dc->dc_state &= ~HPCFB_DC_SCROLLPENDING;
   1373 			callout_stop(&dc->dc_scroll_ch);
   1374 		}
   1375 		if (dc->dc_state&HPCFB_DC_UPDATE) {
   1376 			dc->dc_state &= ~HPCFB_DC_UPDATE;
   1377 			hpcfb_redraw(cookie, dc->dc_min_row,
   1378 			    dc->dc_max_row - dc->dc_min_row, 0);
   1379 			dc->dc_max_row = 0;
   1380 			dc->dc_min_row = dc->dc_rows;
   1381 			if (dc->dc_curx > 0 && dc->dc_cury > 0)
   1382 				hpcfb_cursor(dc, 1,  dc->dc_cury, dc->dc_curx);
   1383 			return;
   1384 		}
   1385 #endif /* HPCFB_JUMP */
   1386 		hpcfb_redraw(cookie, dst, num, 0);
   1387 	}
   1388 #ifdef HPCFB_JUMP
   1389 	hpcfb_check_update(dc);
   1390 #endif /* HPCFB_JUMP */
   1391 }
   1392 
   1393 /*
   1394  * eraserows
   1395  */
   1396 void
   1397 hpcfb_tv_eraserows(struct hpcfb_devconfig *dc, int row, int nrow, long attr)
   1398 {
   1399 	struct hpcfb_tvrow *vscn = dc->dc_tvram;
   1400 	int cols;
   1401 	int i;
   1402 
   1403 	if (vscn == 0)
   1404 		return;
   1405 
   1406 	dc->dc_state |= HPCFB_DC_TDRAWING;
   1407 	dc->dc_state &= ~HPCFB_DC_TDRAWING;
   1408 #ifdef HPCFB_JUMP
   1409 	if (row < dc->dc_min_row)
   1410 		dc->dc_min_row = row;
   1411 	if (row + nrow > dc->dc_max_row)
   1412 		dc->dc_max_row = row + nrow;
   1413 #endif /* HPCFB_JUMP */
   1414 
   1415 	for (i = 0; i < nrow; i++) {
   1416 		cols = vscn[row+i].maxcol;
   1417 		if (vscn[row+i].spacecol < cols)
   1418 			vscn[row+i].spacecol = cols;
   1419 		vscn[row+i].maxcol = -1;
   1420 	}
   1421 #ifdef HPCFB_JUMP
   1422 	hpcfb_check_update(dc);
   1423 #endif /* HPCFB_JUMP */
   1424 }
   1425 
   1426 void
   1427 hpcfb_eraserows(void *cookie, int row, int nrow, long attr)
   1428 {
   1429 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
   1430 	struct hpcfb_softc *sc = dc->dc_sc;
   1431 	struct rasops_info *ri = &dc->dc_rinfo;
   1432 	int yoff;
   1433 	int width;
   1434 	int height;
   1435 
   1436 	hpcfb_tv_eraserows(dc, row, nrow, attr);
   1437 #ifdef HPCFB_JUMP
   1438 	if (dc->dc_state&HPCFB_DC_SCROLLPENDING) {
   1439 		dc->dc_state |= HPCFB_DC_UPDATE;
   1440 		return;
   1441 	}
   1442 #endif /* HPCFB_JUMP */
   1443 	if ((dc->dc_state&HPCFB_DC_CURRENT) == 0)
   1444 		return;
   1445 	if (ri->ri_bits == NULL)
   1446 		return;
   1447 
   1448 	dc->dc_state |= HPCFB_DC_DRAWING;
   1449 	if (sc && sc->sc_accessops->erase
   1450 	    && (dc->dc_state&HPCFB_DC_CURRENT)) {
   1451 		yoff = row * ri->ri_font->fontheight;
   1452 		width = dc->dc_cols * ri->ri_font->fontwidth;
   1453 		height = nrow * ri->ri_font->fontheight;
   1454 		(*sc->sc_accessops->erase)(sc->sc_accessctx,
   1455 		    0, yoff, height, width, attr);
   1456 	} else
   1457 		rasops_emul.eraserows(ri, row, nrow, attr);
   1458 	dc->dc_state &= ~HPCFB_DC_DRAWING;
   1459 #ifdef HPCFB_JUMP
   1460 	hpcfb_check_update(dc);
   1461 #endif /* HPCFB_JUMP */
   1462 }
   1463 
   1464 /*
   1465  * alloc_attr
   1466  */
   1467 int
   1468 hpcfb_alloc_attr(void *cookie, int fg, int bg, int flags, long *attrp)
   1469 {
   1470 	struct hpcfb_devconfig *dc = (struct hpcfb_devconfig *)cookie;
   1471 	struct rasops_info *ri = &dc->dc_rinfo;
   1472 
   1473 	return (rasops_emul.alloc_attr(ri, fg, bg, flags, attrp));
   1474 }
   1475