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