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