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