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