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