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