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