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