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