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