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