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