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