xref: /netbsd-src/sys/arch/amiga/dev/amidisplaycc.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: amidisplaycc.c,v 1.8 2002/09/02 11:09:48 aymeric Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 Jukka Andberg.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: amidisplaycc.c,v 1.8 2002/09/02 11:09:48 aymeric Exp $");
32 
33 /*
34  * wscons interface to amiga custom chips. Contains the necessary functions
35  * to render text on bitmapped screens. Uses the functions defined in
36  * grfabs_reg.h for display creation/destruction and low level setup.
37  *
38  * For each virtual terminal a new screen ('view') is allocated.
39  * Also one more is allocated for the mapped screen on demand.
40  */
41 
42 #include "amidisplaycc.h"
43 #include "grfcc.h"
44 #include "view.h"
45 #include "opt_amigaccgrf.h"
46 
47 #if NAMIDISPLAYCC>0
48 
49 #include <sys/param.h>
50 #include <sys/types.h>
51 #include <sys/device.h>
52 #include <sys/malloc.h>
53 #include <sys/systm.h>
54 
55 #include <sys/conf.h>
56 
57 #include <amiga/dev/grfabs_reg.h>
58 #include <amiga/dev/viewioctl.h>
59 #include <amiga/amiga/device.h>
60 #include <dev/wscons/wsconsio.h>
61 #include <dev/rcons/raster.h>
62 #include <dev/wscons/wscons_raster.h>
63 #include <dev/wscons/wsdisplayvar.h>
64 #include <dev/cons.h>
65 #include <dev/wsfont/wsfont.h>
66 
67 #include <machine/stdarg.h>
68 
69 #define AMIDISPLAYCC_MAXFONTS 8
70 
71 /* These can be lowered if you are sure you dont need that much colors. */
72 #define MAXDEPTH 8
73 #define MAXROWS 128
74 #define MAXCOLUMNS 80
75 
76 #define ADJUSTCOLORS
77 
78 #define MAXCOLORS (1<<MAXDEPTH)
79 
80 struct amidisplaycc_screen;
81 struct amidisplaycc_softc
82 {
83 	struct device dev;
84 
85 	/* runtime-loaded fonts */
86 	struct wsdisplay_font         fonts[AMIDISPLAYCC_MAXFONTS];
87 
88 	struct amidisplaycc_screen  * currentscreen;
89 
90 	/* display turned on? */
91 	int       ison;
92 
93 	/* stuff relating to the mapped screen */
94 	view_t  * gfxview;
95 	int       gfxwidth;
96 	int       gfxheight;
97 	int       gfxdepth;
98 	int       gfxon;
99 };
100 
101 
102 /*
103  * Configuration stuff.
104  */
105 
106 static int  amidisplaycc_match(struct device *, struct cfdata *, void *);
107 static void amidisplaycc_attach(struct device *, struct device *, void *);
108 
109 struct cfattach amidisplaycc_ca = {
110 	sizeof(struct amidisplaycc_softc),
111 	amidisplaycc_match,
112 	amidisplaycc_attach
113 };
114 
115 cons_decl(amidisplaycc_);
116 
117 /* end of configuration stuff */
118 
119 /* private utility functions */
120 
121 static int amidisplaycc_setvideo(struct amidisplaycc_softc *, int);
122 
123 static int amidisplaycc_setemulcmap(struct amidisplaycc_screen *,
124 				    struct wsdisplay_cmap *);
125 
126 static int amidisplaycc_cmapioctl(view_t *, u_long, struct wsdisplay_cmap *);
127 static int amidisplaycc_setcmap(view_t *, struct wsdisplay_cmap *);
128 static int amidisplaycc_getcmap(view_t *, struct wsdisplay_cmap *);
129 static int amidisplaycc_gfxscreen(struct amidisplaycc_softc *, int);
130 
131 static int amidisplaycc_setnamedfont(struct amidisplaycc_screen *, char *);
132 static void amidisplaycc_setfont(struct amidisplaycc_screen *,
133 				 struct wsdisplay_font *, int);
134 static struct wsdisplay_font *amidisplaycc_findfont(struct amidisplaycc_softc *,
135 						    const char *, int, int);
136 
137 static void dprintf(const char *fmt, ...);
138 
139 /* end of private utility functions */
140 
141 /* emulops for wscons */
142 void amidisplaycc_cursor(void *, int, int, int);
143 int amidisplaycc_mapchar(void *, int, unsigned int *);
144 void amidisplaycc_putchar(void *, int, int, u_int, long);
145 void amidisplaycc_copycols(void *, int, int, int, int);
146 void amidisplaycc_erasecols(void *, int, int, int, long);
147 void amidisplaycc_copyrows(void *, int, int, int);
148 void amidisplaycc_eraserows(void *, int, int, long);
149 int  amidisplaycc_allocattr(void *, int, int, int, long *);
150 /* end of emulops for wscons */
151 
152 
153 /* accessops for wscons */
154 int amidisplaycc_ioctl(void *, u_long, caddr_t, int, struct proc *);
155 paddr_t amidisplaycc_mmap(void *, off_t, int);
156 int amidisplaycc_alloc_screen(void *, const struct wsscreen_descr *, void **,
157 			      int *, int *, long *);
158 void amidisplaycc_free_screen( void *, void *);
159 int amidisplaycc_show_screen(void *, void *, int, void (*)(void *, int, int),
160 			     void *);
161 int amidisplaycc_load_font(void *, void *, struct wsdisplay_font *);
162 void amidisplaycc_pollc(void *, int);
163 /* end of accessops for wscons */
164 
165 /*
166  * These structures are passed to wscons, and they contain the
167  * display-specific callback functions.
168  */
169 
170 const struct wsdisplay_accessops amidisplaycc_accessops = {
171 	amidisplaycc_ioctl,
172 	amidisplaycc_mmap,
173 	amidisplaycc_alloc_screen,
174 	amidisplaycc_free_screen,
175 	amidisplaycc_show_screen,
176 	amidisplaycc_load_font,
177 	amidisplaycc_pollc
178 };
179 
180 const struct wsdisplay_emulops amidisplaycc_emulops = {
181 	amidisplaycc_cursor,
182 	amidisplaycc_mapchar,
183 	amidisplaycc_putchar,
184 	amidisplaycc_copycols,
185 	amidisplaycc_erasecols,
186 	amidisplaycc_copyrows,
187 	amidisplaycc_eraserows,
188 	amidisplaycc_allocattr
189 };
190 
191 /* add some of our own data to the wsscreen_descr */
192 struct amidisplaycc_screen_descr {
193 	struct wsscreen_descr  wsdescr;
194 	int                    depth;
195 	char                   name[16];
196 };
197 
198 /*
199  * List of supported screenmodes. Almost anything can be given here.
200  */
201 
202 #define ADCC_SCREEN(name, width, height, depth, fontwidth, fontheight) \
203     /* CONSTCOND */ \
204     {{ \
205     name, \
206     width / fontwidth, \
207     height / fontheight, \
208     &amidisplaycc_emulops, fontwidth, fontheight, \
209     (depth > 1 ? WSSCREEN_WSCOLORS : 0) | WSSCREEN_REVERSE | \
210     WSSCREEN_HILIT | WSSCREEN_UNDERLINE }, \
211     depth }
212 
213 struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
214 	/* name, width, height, depth, fontwidth==8, fontheight */
215 	ADCC_SCREEN("80x50", 640, 400, 3, 8, 8),
216 	ADCC_SCREEN("80x40", 640, 400, 3, 8, 10),
217 	ADCC_SCREEN("80x25", 640, 400, 3, 8, 16),
218 	ADCC_SCREEN("80x24", 640, 384, 3, 8, 16),
219 
220 	ADCC_SCREEN("640x400x1", 640, 400, 1, 8, 8),
221 	ADCC_SCREEN("640x400x2", 640, 400, 2, 8, 8),
222 	ADCC_SCREEN("640x400x3", 640, 400, 3, 8, 8),
223 
224 	ADCC_SCREEN("640x200x1", 640, 200, 1, 8, 8),
225 	ADCC_SCREEN("640x200x1", 640, 200, 2, 8, 8),
226 	ADCC_SCREEN("640x200x1", 640, 200, 3, 8, 8),
227 };
228 
229 #define ADCC_SCREENPTR(index) &amidisplaycc_screentab[index].wsdescr
230 const struct wsscreen_descr *amidisplaycc_screens[] = {
231 	ADCC_SCREENPTR(0),
232 	ADCC_SCREENPTR(1),
233 	ADCC_SCREENPTR(2),
234 	ADCC_SCREENPTR(3),
235 	ADCC_SCREENPTR(4),
236 	ADCC_SCREENPTR(5),
237 	ADCC_SCREENPTR(6),
238 	ADCC_SCREENPTR(7),
239 	ADCC_SCREENPTR(8),
240 	ADCC_SCREENPTR(9),
241 };
242 
243 #define NELEMS(arr) (sizeof(arr)/sizeof((arr)[0]))
244 
245 /*
246  * This structure also is passed to wscons. It contains pointers
247  * to the available display modes.
248  */
249 
250 const struct wsscreen_list amidisplaycc_screenlist = {
251 	sizeof(amidisplaycc_screens)/sizeof(amidisplaycc_screens[0]),
252 	amidisplaycc_screens
253 };
254 
255 /*
256  * Our own screen structure. One will be created for each screen.
257  */
258 
259 struct amidisplaycc_screen
260 {
261 	struct amidisplaycc_softc *device;
262 
263 	int       isconsole;
264 	int       isvisible;
265 	view_t  * view;
266 
267 	int       ncols;
268 	int       nrows;
269 
270 	int       cursorrow;
271 	int       cursorcol;
272 
273 	/* Active bitplanes for each character row. */
274 	int       rowmasks[MAXROWS];
275 
276 	/* Mapping of colors to screen colors. */
277 	int       colormap[MAXCOLORS];
278 
279 	/* Copies of display parameters for convenience */
280 	int       width;
281 	int       height;
282 	int       depth;
283 
284 	int       widthbytes; /* bytes_per_row           */
285 	int       linebytes;  /* widthbytes + row_mod    */
286 	int       rowbytes;   /* linebytes * fontheight  */
287 
288 	u_char  * planes[MAXDEPTH];
289 
290 	u_char  * savedscreen;
291 
292 	/*
293 	 * The font is either one we loaded ourselves, or
294 	 * one gotten using the wsfont system.
295 	 *
296 	 * wsfontcookie differentiates between them:
297 	 * For fonts loaded by ourselves it is -1.
298 	 * For wsfonts it contains a cookie for that system.
299 	 */
300 	struct wsdisplay_font  * font;
301 	int                      wsfontcookie;
302 	int                      fontwidth;
303 	int                      fontheight;
304 };
305 
306 typedef struct amidisplaycc_screen adccscr_t;
307 
308 /*
309  * Need one statically allocated screen for early init.
310  * The rest are mallocated when needed.
311  */
312 adccscr_t amidisplaycc_consolescreen;
313 
314 
315 /*
316  * Bring in the one or two builtin fonts.
317  */
318 
319 extern unsigned char kernel_font_8x8[];
320 extern unsigned char kernel_font_lo_8x8;
321 extern unsigned char kernel_font_hi_8x8;
322 
323 /*
324  * Default palettes for 2, 4 and 8 color emulation displays.
325  */
326 
327 /* black, grey */
328 static u_char pal2red[] = { 0x00, 0xaa };
329 static u_char pal2grn[] = { 0x00, 0xaa };
330 static u_char pal2blu[] = { 0x00, 0xaa };
331 
332 /* black, red, green, grey */
333 static u_char pal4red[] = { 0x00, 0xaa, 0x00, 0xaa };
334 static u_char pal4grn[] = { 0x00, 0x00, 0xaa, 0xaa };
335 static u_char pal4blu[] = { 0x00, 0x00, 0x00, 0xaa };
336 
337 /* black, red, green, brown, blue, magenta, cyan, grey */
338 static u_char pal8red[] = { 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa};
339 static u_char pal8grn[] = { 0x00, 0x00, 0xaa, 0xaa, 0x00, 0x00, 0xaa, 0xaa};
340 static u_char pal8blu[] = { 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa};
341 
342 static struct wsdisplay_cmap pal2 = { 0, 2, pal2red, pal2grn, pal2blu };
343 static struct wsdisplay_cmap pal4 = { 0, 4, pal4red, pal4grn, pal4blu };
344 static struct wsdisplay_cmap pal8 = { 0, 8, pal8red, pal8grn, pal8blu };
345 
346 #ifdef GRF_AGA
347 extern int aga_enable;
348 #else
349 static int aga_enable = 0;
350 #endif
351 
352 /*
353  * This gets called at console init to determine the priority of
354  * this console device.
355  *
356  * Of course pointers to this and other functions must present
357  * in constab[] in conf.c for this to work.
358  */
359 void
360 amidisplaycc_cnprobe(struct consdev *cd)
361 {
362 	cd->cn_pri = CN_INTERNAL;
363 
364 	/*
365 	 * Yeah, real nice. But if we win the console then the wscons system
366 	 * does the proper initialization.
367 	 */
368 	cd->cn_dev = NODEV;
369 }
370 
371 /*
372  * This gets called if this device is used as the console.
373  */
374 void
375 amidisplaycc_cninit(struct consdev  * cd)
376 {
377 	void  * cookie;
378 	long    attr;
379 	int     x;
380 	int     y;
381 
382 	/* Yeah, we got the console! */
383 
384 	/*
385 	 * This will do the basic stuff we also need.
386 	 */
387 	config_console();
388 
389 	grfcc_probe();
390 
391 #if NVIEW>0
392 	viewprobe();
393 #endif
394 
395 	/*
396 	 * Set up wscons to handle the details.
397 	 * It will then call us back when it needs something
398 	 * display-specific. It will also set up cn_tab properly,
399 	 * something which we failed to do at amidisplaycc_cnprobe().
400 	 */
401 
402 	/*
403 	 * The alloc_screen knows to allocate the first screen statically.
404 	 */
405 	amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
406 				  &cookie, &x, &y, &attr);
407 	wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
408 			   cookie, x, y, attr);
409 }
410 
411 static int
412 amidisplaycc_match(struct device *pdp, struct cfdata *cfp, void *auxp)
413 {
414 	char *name = auxp;
415 
416 	if (matchname("amidisplaycc", name) == 0)
417 		return (0);
418 
419 	/* Allow only one of us now. Not sure about that. */
420 	if (cfp->cf_unit != 0)
421 		return (0);
422 
423 	return 1;
424 }
425 
426 /* ARGSUSED */
427 static void
428 amidisplaycc_attach(struct device *pdp, struct device *dp, void *auxp)
429 {
430 	struct wsemuldisplaydev_attach_args    waa;
431 	struct amidisplaycc_softc            * adp;
432 
433 	adp = (struct amidisplaycc_softc*)dp;
434 
435 	grfcc_probe();
436 
437 #if NVIEW>0
438 	viewprobe();
439 #endif
440 
441 	/*
442 	 * Attach only at real configuration time. Console init is done at
443 	 * the amidisplaycc_cninit function above.
444 	 */
445 	if (adp) {
446 		printf(": Amiga custom chip graphics %s",
447 		       aga_enable ? "(AGA)" : "");
448 
449 		if (amidisplaycc_consolescreen.isconsole) {
450 			adp->currentscreen = &amidisplaycc_consolescreen;
451 			printf(" (console)");
452 		} else
453 			adp->currentscreen = NULL;
454 
455 		printf("\n");
456 
457 		adp->ison = 1;
458 
459 		/*
460 		 * Mapped screen properties.
461 		 * Would need a way to configure.
462 		 */
463 		adp->gfxview = NULL;
464 		adp->gfxon = 0;
465 		adp->gfxwidth = 640;
466 		adp->gfxheight = 480;
467 
468 		if (aga_enable)
469 			adp->gfxdepth = 8;
470 		else
471 			adp->gfxdepth = 4;
472 
473 		if (NELEMS(amidisplaycc_screentab) !=
474 		    NELEMS(amidisplaycc_screens))
475 			panic("invalid screen definitions");
476 
477 		waa.scrdata = &amidisplaycc_screenlist;
478 		waa.console = amidisplaycc_consolescreen.isconsole;
479 		waa.accessops = &amidisplaycc_accessops;
480 		waa.accesscookie = dp;
481 		config_found(dp, &waa, wsemuldisplaydevprint);
482 
483 		bzero(adp->fonts, sizeof(adp->fonts));
484 
485 		/* Initialize an alternate system for finding fonts. */
486 		wsfont_init();
487 	}
488 }
489 
490 
491 /*
492  * Color, bgcolor and style are packed into one long attribute.
493  * These macros are used to create/split the attribute
494  */
495 
496 #define MAKEATTR(fg, bg, mode) (((fg)<<16) | ((bg)<<8) | (mode))
497 #define ATTRFG(attr) (((attr)>>16) & 255)
498 #define ATTRBG(attr) (((attr)>>8) & 255)
499 #define ATTRMO(attr) ((attr) & 255)
500 
501 /*
502  * Called by wscons to draw/clear the cursor.
503  * We do this by xorring the block to the screen.
504  *
505  * This simple implementation will break if the screen is modified
506  * under the cursor before clearing it.
507  */
508 void
509 amidisplaycc_cursor(void *screen, int on, int row, int col)
510 {
511 	adccscr_t  * scr;
512 	u_char     * dst;
513 	int  i;
514 
515 	scr = screen;
516 
517 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
518 		return;
519 
520 	if (!on && scr->cursorrow==-1 && scr->cursorcol==-1)
521 		return;
522 
523 	if (!on) {
524 		row = scr->cursorrow;
525 		col = scr->cursorcol;
526 	}
527 
528 	dst = scr->planes[0];
529 	dst += row * scr->rowbytes;
530 	dst += col;
531 
532 	if (on) {
533 		scr->cursorrow = row;
534 		scr->cursorcol = col;
535 	} else {
536 		scr->cursorrow = -1;
537 		scr->cursorcol = -1;
538 	}
539 
540 	for (i = scr->fontheight ; i > 0 ; i--) {
541 		*dst ^= 255;
542 		dst += scr->linebytes;
543 	}
544 }
545 
546 
547 /*
548  * This obviously does something important, don't ask me what.
549  */
550 int
551 amidisplaycc_mapchar(void *screen, int ch, unsigned int *chp)
552 {
553 	if (ch > 0 && ch < 256) {
554 		*chp = ch;
555 		return (5);
556 	}
557 	*chp = ' ';
558 	return (0);
559 }
560 
561 /*
562  * Write a character to screen with color / bgcolor / hilite(bold) /
563  * underline / reverse.
564  * Surely could be made faster but I'm not sure if its worth the
565  * effort as scrolling is at least a magnitude slower.
566  */
567 void
568 amidisplaycc_putchar(void *screen, int row, int col, u_int ch, long attr)
569 {
570 	adccscr_t  * scr;
571 	u_char     * dst;
572 	u_char     * font;
573 
574 	int         fontheight;
575 	u_int8_t  * fontreal;
576 	int         fontlow;
577 	int         fonthigh;
578 
579 	int     bmapoffset;
580 	int     linebytes;
581 	int     underline;
582 	int     fgcolor;
583 	int     bgcolor;
584 	int     plane;
585 	int     depth;
586 	int     mode;
587 	int     bold;
588 	u_char  f;
589 	int     j;
590 
591 	scr = screen;
592 
593 	if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
594 		return;
595 
596 	/* Extract the colors from the attribute */
597 	fgcolor = ATTRFG(attr);
598 	bgcolor = ATTRBG(attr);
599 	mode    = ATTRMO(attr);
600 
601 	/* Translate to screen colors */
602 	fgcolor = scr->colormap[fgcolor];
603 	bgcolor = scr->colormap[bgcolor];
604 
605 	if (mode & WSATTR_REVERSE) {
606 		j = fgcolor;
607 		fgcolor = bgcolor;
608 		bgcolor = j;
609 	}
610 
611 	bold      = (mode & WSATTR_HILIT) > 0;
612 	underline = (mode & WSATTR_UNDERLINE) > 0;
613 
614 	/* If we have loaded a font use it otherwise the builtin font */
615 	if (scr->font) {
616 		fontreal = scr->font->data;
617 		fontlow  = scr->font->firstchar;
618 		fonthigh = fontlow + scr->font->numchars - 1;
619 	} else {
620 		fontreal = kernel_font_8x8;
621 		fontlow  = kernel_font_lo_8x8;
622 		fonthigh = kernel_font_hi_8x8;
623 	}
624 
625 	fontheight = scr->fontheight;
626 	depth      = scr->depth;
627 	linebytes  = scr->linebytes;
628 
629 	if (ch < fontlow || ch > fonthigh)
630 		ch = fontlow;
631 
632 	/* Find the location where the wanted char is in the font data */
633 	fontreal += scr->fontheight * (ch - fontlow);
634 
635 	bmapoffset = row * scr->rowbytes + col;
636 
637 	scr->rowmasks[row] |= fgcolor | bgcolor;
638 
639 	for (plane = 0 ; plane < depth ; plane++) {
640 		dst = scr->planes[plane] + bmapoffset;
641 
642 		if (fgcolor & 1) {
643 			if (bgcolor & 1) {
644 				/* fg=on bg=on (fill) */
645 
646 				for (j = 0 ; j < fontheight ; j++) {
647 					*dst = 255;
648 					dst += linebytes;
649 				}
650 			} else {
651 				/* fg=on bg=off (normal) */
652 
653 				font = fontreal;
654 				for (j = 0 ; j < fontheight ; j++) {
655 					f = *(font++);
656 					f |= f >> bold;
657 					*dst = f;
658 					dst += linebytes;
659 				}
660 
661 				if (underline)
662 					*(dst - linebytes) = 255;
663 			}
664 		} else {
665 			if (bgcolor & 1) {
666 				/* fg=off bg=on (inverted) */
667 
668 				font = fontreal;
669 				for (j = 0 ; j < fontheight ; j++) {
670 					f = *(font++);
671 					f |= f >> bold;
672 					*dst = ~f;
673 					dst += linebytes;
674 				}
675 
676 				if (underline)
677 					*(dst - linebytes) = 0;
678 			} else {
679 				/* fg=off bg=off (clear) */
680 
681 				for (j = 0 ; j < fontheight ; j++) {
682 					*dst = 0;
683 					dst += linebytes;
684 				}
685 			}
686 		}
687 		fgcolor >>= 1;
688 		bgcolor >>= 1;
689 	}
690 }
691 
692 /*
693  * Copy characters on a row to another position on the same row.
694  */
695 
696 void
697 amidisplaycc_copycols(void *screen, int row, int srccol, int dstcol, int ncols)
698 {
699 	adccscr_t  * scr;
700 	u_char     * src;
701 	u_char     * dst;
702 
703 	int  bmapoffset;
704 	int  linebytes;
705 	int  depth;
706 	int  plane;
707 	int  i;
708 	int  j;
709 
710 	scr = screen;
711 
712 	if (srccol < 0 || srccol + ncols > scr->ncols ||
713 	    dstcol < 0 || dstcol + ncols > scr->ncols ||
714 	    row < 0 || row >= scr->nrows)
715 		return;
716 
717 	depth = scr->depth;
718 	linebytes = scr->linebytes;
719 	bmapoffset = row * scr->rowbytes;
720 
721 	for (plane = 0 ; plane < depth ; plane++) {
722 		src = scr->planes[plane] + bmapoffset;
723 
724 		for (j = 0 ; j < scr->fontheight ; j++) {
725 			dst = src;
726 
727 			if (srccol < dstcol) {
728 
729 				for (i = ncols - 1 ; i >= 0 ; i--)
730 					dst[dstcol + i] = src[srccol + i];
731 
732 			} else {
733 
734 				for (i = 0 ; i < ncols ; i++)
735 					dst[dstcol + i] = src[srccol + i];
736 
737 			}
738 			src += linebytes;
739 		}
740 	}
741 }
742 
743 /*
744  * Erase part of a row.
745  */
746 
747 void
748 amidisplaycc_erasecols(void *screen, int row, int startcol, int ncols,
749 		       long attr)
750 {
751 	adccscr_t  * scr;
752 	u_char     * dst;
753 
754 	int  bmapoffset;
755 	int  linebytes;
756 	int  bgcolor;
757 	int  depth;
758 	int  plane;
759 	int  fill;
760 	int  j;
761 
762 	scr = screen;
763 
764 	if (row < 0 || row >= scr->nrows ||
765 	    startcol < 0 || startcol + ncols > scr->ncols)
766 		return;
767 
768 	depth = scr->depth;
769 	linebytes = scr->linebytes;
770 	bmapoffset = row * scr->rowbytes + startcol;
771 
772 	/* Erase will be done using the set background color. */
773 	bgcolor = ATTRBG(attr);
774 	bgcolor = scr->colormap[bgcolor];
775 
776 	for(plane = 0 ; plane < depth ; plane++) {
777 
778 		fill = (bgcolor & 1) ? 255 : 0;
779 
780 		dst = scr->planes[plane] + bmapoffset;
781 
782 		for (j = 0 ; j < scr->fontheight ; j++) {
783 			memset(dst, fill, ncols);
784 			dst += linebytes;
785 		}
786 	}
787 }
788 
789 /*
790  * Copy a number of rows to another location on the screen.
791  * Combined with eraserows it can be used to perform operation
792  * also known as 'scrolling'.
793  */
794 
795 void
796 amidisplaycc_copyrows(void *screen, int srcrow, int dstrow, int nrows)
797 {
798 	adccscr_t  * scr;
799 	u_char     * src;
800 	u_char     * dst;
801 
802 	int  srcbmapoffset;
803 	int  dstbmapoffset;
804 	int  widthbytes;
805 	int  fontheight;
806 	int  linebytes;
807 	u_int copysize;
808 	int  rowdelta;
809 	int  rowbytes;
810 	int  srcmask;
811 	int  dstmask;
812 	int  bmdelta;
813 	int  depth;
814 	int  plane;
815 	int  i;
816 	int  j;
817 
818 	scr = screen;
819 
820 	if (srcrow < 0 || srcrow + nrows > scr->nrows ||
821 	    dstrow < 0 || dstrow + nrows > scr->nrows)
822 		return;
823 
824 	depth = scr->depth;
825 
826 	widthbytes = scr->widthbytes;
827 	rowbytes   = scr->rowbytes;
828 	linebytes  = scr->linebytes;
829 	fontheight = scr->fontheight;
830 
831 	srcbmapoffset = rowbytes * srcrow;
832 	dstbmapoffset = rowbytes * dstrow;
833 
834 	if (srcrow < dstrow) {
835 		/* Move data downwards, need to copy from down to up */
836 		bmdelta = -rowbytes;
837 		rowdelta = -1;
838 
839 		srcbmapoffset += rowbytes * (nrows - 1);
840 		srcrow += nrows - 1;
841 
842 		dstbmapoffset += rowbytes * (nrows - 1);
843 		dstrow += nrows - 1;
844 	} else {
845 		/* Move data upwards, copy up to down */
846 		bmdelta = rowbytes;
847 		rowdelta = 1;
848 	}
849 
850 	if (widthbytes == linebytes)
851 		copysize = rowbytes;
852 	else
853 		copysize = 0;
854 
855 	for (j = 0 ; j < nrows ; j++) {
856 		/* Need to copy only planes that have data on src or dst */
857 		srcmask = scr->rowmasks[srcrow];
858 		dstmask = scr->rowmasks[dstrow];
859 		scr->rowmasks[dstrow] = srcmask;
860 
861 		for (plane = 0 ; plane < depth ; plane++) {
862 
863 			if (srcmask & 1) {
864 				/*
865 				 * Source row has data on this
866 				 * plane, copy it.
867 				 */
868 
869 				src = scr->planes[plane] + srcbmapoffset;
870 				dst = scr->planes[plane] + dstbmapoffset;
871 
872 				if (copysize > 0) {
873 
874 					memcpy(dst, src, copysize);
875 
876 				} else {
877 
878 					/*
879 					 * Data not continuous,
880 					 * must do in pieces
881 					 */
882 					for (i=0 ; i < fontheight ; i++) {
883 						memcpy(dst, src, widthbytes);
884 
885 						src += linebytes;
886 						dst += linebytes;
887 					}
888 				}
889 			} else if (dstmask & 1) {
890 				/*
891 				 * Source plane is empty, but dest is not.
892 				 * so all we need to is clear it.
893 				 */
894 
895 				dst = scr->planes[plane] + dstbmapoffset;
896 
897 				if (copysize > 0) {
898 					/* Do it all */
899 					bzero(dst, copysize);
900 				} else {
901 					for (i = 0 ; i < fontheight ; i++) {
902 						bzero(dst, widthbytes);
903 						dst += linebytes;
904 					}
905 				}
906 			}
907 
908 			srcmask >>= 1;
909 			dstmask >>= 1;
910 		}
911 		srcbmapoffset += bmdelta;
912 		dstbmapoffset += bmdelta;
913 
914 		srcrow += rowdelta;
915 		dstrow += rowdelta;
916 	}
917 }
918 
919 /*
920  * Erase some rows.
921  */
922 
923 void
924 amidisplaycc_eraserows(void *screen, int row, int nrows, long attr)
925 {
926 	adccscr_t  * scr;
927 	u_char     * dst;
928 
929 	int  bmapoffset;
930 	int  fillsize;
931 	int  bgcolor;
932 	int  depth;
933 	int  plane;
934 	int  fill;
935 	int  j;
936 
937 	int  widthbytes;
938 	int  linebytes;
939 	int  rowbytes;
940 
941 
942 	scr = screen;
943 
944 	if (row < 0 || row + nrows > scr->nrows)
945 		return;
946 
947 	depth      = scr->depth;
948 	widthbytes = scr->widthbytes;
949 	linebytes  = scr->linebytes;
950 	rowbytes   = scr->rowbytes;
951 
952 	bmapoffset = row * rowbytes;
953 
954 	if (widthbytes == linebytes)
955 		fillsize = rowbytes * nrows;
956 	else
957 		fillsize = 0;
958 
959 	bgcolor = ATTRBG(attr);
960 	bgcolor = scr->colormap[bgcolor];
961 
962 	for (j = 0 ; j < nrows ; j++)
963 		scr->rowmasks[row+j] = bgcolor;
964 
965 	for (plane = 0 ; plane < depth ; plane++) {
966 		dst = scr->planes[plane] + bmapoffset;
967 		fill = (bgcolor & 1) ? 255 : 0;
968 
969 		if (fillsize > 0) {
970 			/* If the rows are continuous, write them all. */
971 			memset(dst, fill, fillsize);
972 		} else {
973 			for (j = 0 ; j < scr->fontheight * nrows ; j++) {
974 				memset(dst, fill, widthbytes);
975 				dst += linebytes;
976 			}
977 		}
978 		bgcolor >>= 1;
979 	}
980 }
981 
982 
983 /*
984  * Compose an attribute value from foreground color,
985  * background color, and flags.
986  */
987 int
988 amidisplaycc_allocattr(void *screen, int fg, int bg, int flags, long *attrp)
989 {
990 	adccscr_t  * scr;
991 	int          maxcolor;
992 	int          newfg;
993 	int          newbg;
994 
995 	scr = screen;
996 	maxcolor = (1 << scr->view->bitmap->depth) - 1;
997 
998 	/* Ensure the colors are displayable. */
999 	newfg = fg & maxcolor;
1000 	newbg = bg & maxcolor;
1001 
1002 #ifdef ADJUSTCOLORS
1003 	/*
1004 	 * Hack for low-color screens, if background color is nonzero
1005 	 * but would be displayed as one, adjust it.
1006 	 */
1007 	if (bg > 0 && newbg == 0)
1008 		newbg = maxcolor;
1009 
1010 	/*
1011 	 * If foreground and background colors are different but would
1012 	 * display the same fix them by modifying the foreground.
1013 	 */
1014 	if (fg != bg && newfg == newbg) {
1015 		if (newbg > 0)
1016 			newfg = 0;
1017 		else
1018 			newfg = maxcolor;
1019 	}
1020 #endif
1021 	*attrp = MAKEATTR(newfg, newbg, flags);
1022 
1023 	return (0);
1024 }
1025 
1026 int
1027 amidisplaycc_ioctl(void *dp, u_long cmd, caddr_t data, int flag, struct proc *p)
1028 {
1029 	struct amidisplaycc_softc *adp;
1030 
1031 	adp = dp;
1032 
1033 	if (adp == NULL) {
1034 		printf("amidisplaycc_ioctl: adp==NULL\n");
1035 		return (EINVAL);
1036 	}
1037 
1038 #define UINTDATA (*(u_int*)data)
1039 #define INTDATA (*(int*)data)
1040 #define FBINFO (*(struct wsdisplay_fbinfo*)data)
1041 
1042 	switch (cmd)
1043 	{
1044 	case WSDISPLAYIO_GTYPE:
1045 		UINTDATA = WSDISPLAY_TYPE_AMIGACC;
1046 		return (0);
1047 
1048 	case WSDISPLAYIO_SVIDEO:
1049 		dprintf("amidisplaycc: WSDISPLAYIO_SVIDEO %s\n",
1050 			UINTDATA ? "On" : "Off");
1051 
1052 		return (amidisplaycc_setvideo(adp, UINTDATA));
1053 
1054 	case WSDISPLAYIO_GVIDEO:
1055 		dprintf("amidisplaycc: WSDISPLAYIO_GVIDEO\n");
1056 		UINTDATA = adp->ison ?
1057 		    WSDISPLAYIO_VIDEO_ON : WSDISPLAYIO_VIDEO_OFF;
1058 
1059 		return (0);
1060 
1061 	case WSDISPLAYIO_SMODE:
1062 		if (INTDATA == WSDISPLAYIO_MODE_EMUL)
1063 			return amidisplaycc_gfxscreen(adp, 0);
1064 		if (INTDATA == WSDISPLAYIO_MODE_MAPPED)
1065 			return amidisplaycc_gfxscreen(adp, 1);
1066 		return (EINVAL);
1067 
1068 	case WSDISPLAYIO_GINFO:
1069 		FBINFO.width  = adp->gfxwidth;
1070 		FBINFO.height = adp->gfxheight;
1071 		FBINFO.depth  = adp->gfxdepth;
1072 		FBINFO.cmsize = 1 << FBINFO.depth;
1073 		return (0);
1074 
1075 	case WSDISPLAYIO_PUTCMAP:
1076 	case WSDISPLAYIO_GETCMAP:
1077 		return (amidisplaycc_cmapioctl(adp->gfxview,
1078 					       cmd,
1079 					       (struct wsdisplay_cmap*)data));
1080 	}
1081 
1082 	dprintf("amidisplaycc: unknown ioctl %lx (grp:'%c' num:%d)\n",
1083 		(long)cmd,
1084 		(char)((cmd&0xff00)>>8),
1085 		(int)(cmd&0xff));
1086 
1087 	return (EPASSTHROUGH);
1088 
1089 #undef UINTDATA
1090 #undef INTDATA
1091 #undef FBINFO
1092 }
1093 
1094 
1095 /*
1096  * Switch to either emulation (text) or mapped (graphics) mode
1097  * We keep an extra screen for mapped mode so it does not
1098  * interfere with emulation screens.
1099  *
1100  * Once the extra screen is created, it never goes away.
1101  */
1102 
1103 static int
1104 amidisplaycc_gfxscreen(struct amidisplaycc_softc *adp, int on)
1105 {
1106 	dimen_t  dimension;
1107 
1108 	dprintf("amidisplaycc: switching to %s mode.\n",
1109 		on ? "mapped" : "emul");
1110 
1111 	/* Current mode same as requested mode? */
1112 	if ( (on > 0) == (adp->gfxon > 0) )
1113 		return (0);
1114 
1115 	if (!on) {
1116 		/*
1117 		 * Switch away from mapped mode. If there is
1118 		 * a emulation screen, switch to it, otherwise
1119 		 * just try to hide the mapped screen.
1120 		 */
1121 		adp->gfxon = 0;
1122 		if (adp->currentscreen)
1123 			grf_display_view(adp->currentscreen->view);
1124 		else if (adp->gfxview)
1125 			grf_remove_view(adp->gfxview);
1126 
1127 		return (0);
1128 	}
1129 
1130 	/* switch to mapped mode then */
1131 
1132 	if (adp->gfxview == NULL) {
1133 		/* First time here, create the screen */
1134 
1135 		dimension.width = adp->gfxwidth;
1136 		dimension.height = adp->gfxheight;
1137 
1138 		dprintf("amidisplaycc: preparing mapped screen %dx%dx%d\n",
1139 			dimension.width,
1140 			dimension.height,
1141 			adp->gfxdepth);
1142 
1143 		adp->gfxview = grf_alloc_view(NULL,
1144 					      &dimension,
1145 					      adp->gfxdepth);
1146 	}
1147 
1148 	if (adp->gfxview) {
1149 		adp->gfxon = 1;
1150 
1151 		grf_display_view(adp->gfxview);
1152 	} else {
1153 		printf("amidisplaycc: failed to make mapped screen\n");
1154 		return (ENOMEM);
1155 	}
1156 	return (0);
1157 }
1158 
1159 /*
1160  * Map the graphics screen. It must have been created before
1161  * by switching to mapped mode by using an ioctl.
1162  */
1163 paddr_t
1164 amidisplaycc_mmap(void *dp, off_t off, int prot)
1165 {
1166 	struct amidisplaycc_softc  * adp;
1167 	bmap_t                     * bm;
1168 	paddr_t                      rv;
1169 
1170 	adp = (struct amidisplaycc_softc*)dp;
1171 
1172 	/* Check we are in mapped mode */
1173 	if (adp->gfxon == 0 || adp->gfxview == NULL) {
1174 		dprintf("amidisplaycc_mmap: Not in mapped mode\n");
1175 		return (paddr_t)(-1);
1176 	}
1177 
1178 	/*
1179 	 * As we all know by now, we are mapping our special
1180 	 * screen here so our pretty text consoles are left
1181 	 * untouched.
1182 	 */
1183 
1184 	bm = adp->gfxview->bitmap;
1185 
1186 	/* Check that the offset is valid */
1187 	if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
1188 		dprintf("amidisplaycc_mmap: Offset out of range\n");
1189 		return (paddr_t)(-1);
1190 	}
1191 
1192 	rv = (paddr_t)bm->hardware_address;
1193 	rv += off;
1194 
1195 	return (rv >> PGSHIFT);
1196 }
1197 
1198 
1199 /*
1200  * Create a new screen.
1201  * NULL dp signifies console and then memory is allocated statically
1202  * and the screen is automatically displayed.
1203  *
1204  * A font with suitable size is searched and if not found
1205  * the builtin 8x8 font is used.
1206  *
1207  * There are separate default palettes for 2, 4 and 8+ color
1208  * screens.
1209  */
1210 
1211 int
1212 amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
1213 			  void  **cookiep, int *curxp, int *curyp,
1214 			  long *defattrp)
1215 {
1216 	const struct amidisplaycc_screen_descr  * adccscreenp;
1217 	struct amidisplaycc_screen              * scr;
1218 	struct amidisplaycc_softc               * adp;
1219 	view_t                                  * view;
1220 
1221 	dimen_t  dimension;
1222 	int      fontheight;
1223 	int      fontwidth;
1224 	int      maxcolor;
1225 	int      depth;
1226 	int      i;
1227 	int      j;
1228 
1229 	adccscreenp = (const struct amidisplaycc_screen_descr *)screenp;
1230 	depth = adccscreenp->depth;
1231 
1232 	adp = dp;
1233 
1234 	maxcolor = (1 << depth) - 1;
1235 
1236 	/* Sanity checks because of fixed buffers */
1237 	if (depth > MAXDEPTH || maxcolor >= MAXCOLORS)
1238 		return (ENOMEM);
1239 	if (screenp->nrows > MAXROWS)
1240 		return (ENOMEM);
1241 
1242 	fontwidth = screenp->fontwidth;
1243 	fontheight = screenp->fontheight;
1244 
1245 	if (fontwidth != 8) {
1246 		dprintf("amidisplaycc_alloc_screen: fontwidth %d invalid.\n",
1247 		       fontwidth);
1248 		return (EINVAL);
1249 	}
1250 
1251 	/*
1252 	 * The screen size is defined in characters.
1253 	 * Calculate the pixel size using the font size.
1254 	 */
1255 
1256 	dimension.width = screenp->ncols * fontwidth;
1257 	dimension.height = screenp->nrows * fontheight;
1258 
1259 	view = grf_alloc_view(NULL, &dimension, depth);
1260 	if (view == NULL)
1261 		return (ENOMEM);
1262 
1263 	/*
1264 	 * First screen gets the statically allocated console screen.
1265 	 * Others are allocated dynamically.
1266 	 */
1267 	if (adp == NULL) {
1268 		scr = &amidisplaycc_consolescreen;
1269 		if (scr->isconsole)
1270 			panic("more than one console?");
1271 
1272 		scr->isconsole = 1;
1273 	} else {
1274 		scr = malloc(sizeof(adccscr_t), M_DEVBUF, M_WAITOK);
1275 		bzero(scr, sizeof(adccscr_t));
1276 	}
1277 
1278 	scr->view = view;
1279 
1280 	scr->ncols = screenp->ncols;
1281 	scr->nrows = screenp->nrows;
1282 
1283 	/* Copies of most used values */
1284 	scr->width  = dimension.width;
1285 	scr->height = dimension.height;
1286 	scr->depth  = depth;
1287 	scr->widthbytes = view->bitmap->bytes_per_row;
1288 	scr->linebytes  = scr->widthbytes + view->bitmap->row_mod;
1289 	scr->rowbytes   = scr->linebytes * fontheight;
1290 
1291 	scr->device = adp;
1292 
1293 
1294 	/* --- LOAD FONT --- */
1295 
1296 	/* these need to be initialized befory trying to set font */
1297 	scr->font         = NULL;
1298 	scr->wsfontcookie = -1;
1299 	scr->fontwidth    = fontwidth;
1300 	scr->fontheight   = fontheight;
1301 
1302 	/*
1303 	 * Note that dont try to load font for the console (adp==NULL)
1304 	 *
1305 	 * Here we dont care which font we get as long as it is the
1306 	 * right size so pass NULL.
1307 	 */
1308 	if (adp)
1309 		amidisplaycc_setnamedfont(scr, NULL);
1310 
1311 	/*
1312 	 * If no font found, use the builtin one.
1313 	 * It will look stupid if the wanted size was different.
1314 	 */
1315 	if (scr->font == NULL) {
1316 		scr->fontwidth = 8;
1317 		scr->fontheight = min(8, fontheight);
1318 	}
1319 
1320 	/* --- LOAD FONT END --- */
1321 
1322 
1323 	for (i = 0 ; i < depth ; i++) {
1324 		scr->planes[i] = view->bitmap->plane[i];
1325 	}
1326 
1327 	for (i = 0 ; i < MAXROWS ; i++)
1328 		scr->rowmasks[i] = 0;
1329 
1330 	/* Simple one-to-one mapping for most colors */
1331 	for (i = 0 ; i < MAXCOLORS ; i++)
1332 		scr->colormap[i] = i;
1333 
1334 	/*
1335 	 * Arrange the most used pens to quickest colors.
1336 	 * The default color for given depth is (1<<depth)-1.
1337 	 * It is assumed it is used most and it is mapped to
1338 	 * color that can be drawn by writing data to one bitplane
1339 	 * only.
1340 	 * So map colors 3->2, 7->4, 15->8 and so on.
1341 	 */
1342 	for (i = 2 ; i < MAXCOLORS ; i *= 2) {
1343 		j = i * 2 - 1;
1344 
1345 		if (j < MAXCOLORS) {
1346 			scr->colormap[i] = j;
1347 			scr->colormap[j] = i;
1348 		}
1349 	}
1350 
1351 	/*
1352 	 * Set the default colormap.
1353 	 */
1354 	if (depth == 1)
1355 		amidisplaycc_setemulcmap(scr, &pal2);
1356 	else if (depth == 2)
1357 		amidisplaycc_setemulcmap(scr, &pal4);
1358 	else
1359 		amidisplaycc_setemulcmap(scr, &pal8);
1360 
1361 	*cookiep = scr;
1362 
1363 	*curxp = 0;
1364 	*curyp = 0;
1365 	amidisplaycc_cursor(scr, 1, *curxp, *curyp);
1366 
1367 	*defattrp = MAKEATTR(maxcolor, 0, 0);
1368 
1369 	/* Show the console automatically */
1370 	if (adp == NULL)
1371 		grf_display_view(scr->view);
1372 
1373 	if (adp) {
1374 		dprintf("amidisplaycc: allocated screen; %dx%dx%d\n",
1375 			dimension.width,
1376 			dimension.height,
1377 			depth);
1378 	}
1379 
1380 	return (0);
1381 }
1382 
1383 
1384 /*
1385  * Destroy a screen.
1386  */
1387 
1388 void
1389 amidisplaycc_free_screen(void *dp, void *screen)
1390 {
1391 	struct amidisplaycc_screen  * scr;
1392 	struct amidisplaycc_softc   * adp;
1393 
1394 	scr = screen;
1395 	adp = (struct amidisplaycc_softc*)adp;
1396 
1397 	if (scr == NULL)
1398 		return;
1399 
1400 	/* Free the used font */
1401 	amidisplaycc_setfont(scr, NULL, -1);
1402 
1403 	if (adp->currentscreen == scr)
1404 		adp->currentscreen = NULL;
1405 
1406 	if (scr->view)
1407 		grf_free_view(scr->view);
1408 	scr->view = NULL;
1409 
1410 	/* Take care not to free the statically allocated console screen */
1411 	if (scr != &amidisplaycc_consolescreen) {
1412 		free(scr, M_DEVBUF);
1413 	}
1414 }
1415 
1416 /*
1417  * Switch to another vt. Switch is always made immediately.
1418  */
1419 
1420 /* ARGSUSED2 */
1421 int
1422 amidisplaycc_show_screen(void *dp, void *screen, int waitok,
1423 			 void (*cb) (void *, int, int), void *cbarg)
1424 {
1425 	adccscr_t *scr;
1426 	struct amidisplaycc_softc *adp;
1427 
1428 	adp = (struct amidisplaycc_softc*)dp;
1429 	scr = screen;
1430 
1431 	if (adp == NULL) {
1432 		dprintf("amidisplaycc_show_screen: adp==NULL\n");
1433 		return (EINVAL);
1434 	}
1435 	if (scr == NULL) {
1436 		dprintf("amidisplaycc_show_screen: scr==NULL\n");
1437 		return (EINVAL);
1438 	}
1439 
1440 	if (adp->gfxon) {
1441 		dprintf("amidisplaycc: Screen shift while in gfx mode?");
1442 		adp->gfxon = 0;
1443 	}
1444 
1445 	adp->currentscreen = scr;
1446 	adp->ison = 1;
1447 
1448 	grf_display_view(scr->view);
1449 
1450 	return (0);
1451 }
1452 
1453 /*
1454  * Internal. Finds the font in our softc that has the desired attributes.
1455  * Or, if name is NULL, finds a free location for a new font.
1456  * Returns a pointer to font structure in softc or NULL for failure.
1457  *
1458  * Three possible forms:
1459  * findfont(adp, NULL, 0, 0)  -- find first empty location
1460  * findfont(adp, NULL, x, y)  -- find last font with given size
1461  * findfont(adp, name, x, y)  -- find last font with given name and size
1462  *
1463  * Note that when finding an empty location first one found is returned,
1464  * however when finding an existing font, the last one matching is
1465  * returned. This is because fonts cannot be unloaded and the last
1466  * font on the list is the one added latest and thus probably preferred.
1467  *
1468  * Note also that this is the only function which makes assumptions
1469  * about the storage location for the fonts.
1470  */
1471 static struct wsdisplay_font *
1472 amidisplaycc_findfont(struct amidisplaycc_softc *adp, const char *name,
1473 		      int width, int height)
1474 {
1475 	struct wsdisplay_font  * font;
1476 
1477 	int  findempty;
1478 	int  f;
1479 
1480 	if (adp == NULL) {
1481 		dprintf("amidisplaycc_findfont: NULL adp\n");
1482 		return NULL;
1483 	}
1484 
1485 	findempty = (name == NULL) && (width == 0) && (height == 0);
1486 
1487 	font = NULL;
1488 
1489 	for (f = 0 ; f < AMIDISPLAYCC_MAXFONTS ; f++) {
1490 
1491 		if (findempty && adp->fonts[f].name == NULL)
1492 			return &adp->fonts[f];
1493 
1494 		if (!findempty && name == NULL && adp->fonts[f].name &&
1495 		    adp->fonts[f].fontwidth == width &&
1496 		    adp->fonts[f].fontheight == height)
1497 			font = &adp->fonts[f];
1498 
1499 		if (name && adp->fonts[f].name &&
1500 		    strcmp(name, adp->fonts[f].name) == 0 &&
1501 		    width == adp->fonts[f].fontwidth &&
1502 		    height == adp->fonts[f].fontheight)
1503 			font = &adp->fonts[f];
1504 	}
1505 
1506 	return (font);
1507 }
1508 
1509 
1510 /*
1511  * Set the font on a screen and free the old one.
1512  * Can be called with font of NULL to just free the
1513  * old one.
1514  * NULL font cannot be accompanied by valid cookie (!= -1)
1515  */
1516 static void
1517 amidisplaycc_setfont(struct amidisplaycc_screen *scr,
1518 		     struct wsdisplay_font *font, int wsfontcookie)
1519 {
1520 	if (scr == NULL)
1521 		panic("amidisplaycc_setfont: scr==NULL");
1522 	if (font == NULL && wsfontcookie != -1)
1523 		panic("amidisplaycc_setfont: no font but eat cookie");
1524 	if (scr->font == NULL && scr->wsfontcookie != -1)
1525 		panic("amidisplaycc_setfont: no font but eat old cookie");
1526 
1527 	scr->font = font;
1528 
1529 	if (scr->wsfontcookie != -1)
1530 		wsfont_unlock(scr->wsfontcookie);
1531 
1532 	scr->wsfontcookie = wsfontcookie;
1533 }
1534 
1535 /*
1536  * Try to find the named font and set the screen to use it.
1537  * Check both the fonts we have loaded with load_font and
1538  * fonts from wsfont system.
1539  *
1540  * Returns 0 on success.
1541  */
1542 
1543 static int
1544 amidisplaycc_setnamedfont(struct amidisplaycc_screen *scr, char *fontname)
1545 {
1546 	struct wsdisplay_font  * font;
1547 	int  wsfontcookie;
1548 
1549 	wsfontcookie = -1;
1550 
1551 	if (scr == NULL || scr->device == NULL) {
1552 		dprintf("amidisplaycc_setnamedfont: invalid\n");
1553 		return (EINVAL);
1554 	}
1555 
1556 	/* Try first our dynamically loaded fonts. */
1557 	font = amidisplaycc_findfont(scr->device,
1558 				     fontname,
1559 				     scr->fontwidth,
1560 				     scr->fontheight);
1561 
1562 	if (font == NULL) {
1563 		/*
1564 		 * Ok, no dynamically loaded font found.
1565 		 * Try the wsfont system then.
1566 		 */
1567 		wsfontcookie = wsfont_find(fontname,
1568 					   scr->fontwidth,
1569 					   scr->fontheight,
1570 					   1,
1571 					   WSDISPLAY_FONTORDER_L2R,
1572 					   WSDISPLAY_FONTORDER_L2R);
1573 
1574 		if (wsfontcookie == -1)
1575 			return (EINVAL);
1576 
1577 		/* So, found a suitable font. Now lock it. */
1578 		if (wsfont_lock(wsfontcookie,
1579 				&font))
1580 			return (EINVAL);
1581 
1582 		/* Ok here we have the font successfully. */
1583 	}
1584 
1585 	amidisplaycc_setfont(scr, font, wsfontcookie);
1586 	return (0);
1587 }
1588 
1589 /*
1590  * Load a font. This is used both to load a font and set it to screen.
1591  * The function depends on the parameters.
1592  * If the font has no data we must set a previously loaded
1593  * font with the same name. If it has data, then just load
1594  * the font but don't use it.
1595  */
1596 int
1597 amidisplaycc_load_font(void *dp, void *cookie, struct wsdisplay_font *font)
1598 {
1599 	struct amidisplaycc_softc   * adp;
1600 	struct amidisplaycc_screen  * scr;
1601 	struct wsdisplay_font       * myfont;
1602 
1603 	u_int8_t  * c;
1604 	void      * olddata;
1605 	char      * name;
1606 
1607 	u_int       size;
1608 	u_int       i;
1609 
1610 
1611 	adp = dp;
1612 	scr = cookie;
1613 
1614 	/*
1615 	 * If font has no data it means we have to find the
1616 	 * named font and use it.
1617 	 */
1618 	if (scr && font && font->name && !font->data)
1619 		return amidisplaycc_setnamedfont(scr, font->name);
1620 
1621 
1622 	/* Pre-load the font it is */
1623 
1624 	if (font->stride != 1) {
1625 		dprintf("amidisplaycc_load_font: stride %d != 1\n",
1626 		       font->stride);
1627 		return (EINVAL);
1628 	}
1629 
1630 	if (font->fontwidth != 8) {
1631 		dprintf("amidisplaycc_load_font: width %d not supported\n",
1632 		       font->fontwidth);
1633 		return (EINVAL);
1634 	}
1635 
1636 	/* Size of the font in bytes... Assuming stride==1 */
1637 	size = font->fontheight * font->numchars;
1638 
1639 	/* Check if the same font was loaded before */
1640 	myfont = amidisplaycc_findfont(adp,
1641 				       font->name,
1642 				       font->fontwidth,
1643 				       font->fontheight);
1644 
1645 	olddata = NULL;
1646 	if (myfont) {
1647 		/* Old font found, we will replace */
1648 
1649 		if (myfont->name == NULL || myfont->data == NULL)
1650 			panic("replacing NULL font/data");
1651 
1652 		/*
1653 		 * Store the old data pointer. We'll free it later
1654 		 * when the new one is in place. Reallocation is needed
1655 		 * because the new font may have a different number
1656 		 * of characters in it than the last time it was loaded.
1657 		 */
1658 
1659 		olddata = myfont->data;
1660 
1661 	} else {
1662 		/* Totally brand new font */
1663 
1664 		/* Try to find empty slot for the font */
1665 		myfont = amidisplaycc_findfont(adp, NULL, 0, 0);
1666 
1667 		if (myfont == NULL)
1668 			return (ENOMEM);
1669 
1670 		bzero(myfont, sizeof(struct wsdisplay_font));
1671 
1672 		myfont->fontwidth = font->fontwidth;
1673 		myfont->fontheight = font->fontheight;
1674 		myfont->stride = font->stride;
1675 
1676 		name = malloc(strlen(font->name)+1,
1677 			      M_DEVBUF,
1678 			      M_WAITOK);
1679 		strcpy(name, font->name);
1680 		myfont->name = name;
1681 	}
1682 	myfont->firstchar = font->firstchar;
1683 	myfont->numchars  = font->numchars;
1684 
1685 	myfont->data = malloc(size,
1686 			      M_DEVBUF,
1687 			      M_WAITOK);
1688 
1689 	if (olddata)
1690 		free(olddata, M_DEVBUF);
1691 
1692 
1693 	memcpy(myfont->data, font->data, size);
1694 
1695 	if (font->bitorder == WSDISPLAY_FONTORDER_R2L) {
1696 		/* Reverse the characters. */
1697 		c = myfont->data;
1698 		for (i = 0 ; i < size ; i++) {
1699 			*c = ((*c & 0x0f) << 4) | ((*c & 0xf0) >> 4);
1700 			*c = ((*c & 0x33) << 2) | ((*c & 0xcc) >> 2);
1701 			*c = ((*c & 0x55) << 1) | ((*c & 0xaa) >> 1);
1702 
1703 			c++;
1704 		}
1705 	}
1706 
1707 	/* Yeah, we made it */
1708 	return (0);
1709 }
1710 
1711 /*
1712  * Set display on/off.
1713  */
1714 static int
1715 amidisplaycc_setvideo(struct amidisplaycc_softc *adp, int mode)
1716 {
1717         view_t * view;
1718 
1719 	if (adp == NULL) {
1720 		dprintf("amidisplaycc_setvideo: adp==NULL\n");
1721 		return (EINVAL);
1722 	}
1723 	if (adp->currentscreen == NULL) {
1724 		dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
1725 		return (EINVAL);
1726 	}
1727 
1728 	/* select graphics or emulation screen */
1729 	if (adp->gfxon && adp->gfxview)
1730 		view = adp->gfxview;
1731 	else
1732 		view = adp->currentscreen->view;
1733 
1734 	if (mode) {
1735 		/* on */
1736 
1737 		grf_display_view(view);
1738 		dprintf("amidisplaycc: video is now on\n");
1739 		adp->ison = 1;
1740 
1741 	} else {
1742 		/* off */
1743 
1744 		grf_remove_view(view);
1745 		dprintf("amidisplaycc: video is now off\n");
1746 		adp->ison = 0;
1747 	}
1748 
1749 	return (0);
1750 }
1751 
1752 /*
1753  * Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
1754  * Just handle the copying of data to/from userspace and
1755  * let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
1756  * do the real work.
1757  */
1758 
1759 static int
1760 amidisplaycc_cmapioctl(view_t *view, u_long cmd, struct wsdisplay_cmap *cmap)
1761 {
1762 	struct wsdisplay_cmap  tmpcmap;
1763 	u_char                 cmred[MAXCOLORS];
1764 	u_char                 cmgrn[MAXCOLORS];
1765 	u_char                 cmblu[MAXCOLORS];
1766 
1767 	int                    err;
1768 
1769 	if (cmap->index >= MAXCOLORS ||
1770 	    cmap->count > MAXCOLORS ||
1771 	    cmap->index + cmap->count > MAXCOLORS)
1772 		return (EINVAL);
1773 
1774 	if (cmap->count == 0)
1775 		return (0);
1776 
1777 	tmpcmap.index = cmap->index;
1778 	tmpcmap.count = cmap->count;
1779 	tmpcmap.red   = cmred;
1780 	tmpcmap.green = cmgrn;
1781 	tmpcmap.blue  = cmblu;
1782 
1783 	if (cmd == WSDISPLAYIO_PUTCMAP) {
1784 		/* copy the color data to kernel space */
1785 
1786 		err = copyin(cmap->red, cmred, cmap->count);
1787 		if (err)
1788 			return (err);
1789 
1790 		err = copyin(cmap->green, cmgrn, cmap->count);
1791 		if (err)
1792 			return (err);
1793 
1794 		err = copyin(cmap->blue, cmblu, cmap->count);
1795 		if (err)
1796 			return (err);
1797 
1798 		return amidisplaycc_setcmap(view, &tmpcmap);
1799 
1800 	} else if (cmd == WSDISPLAYIO_GETCMAP) {
1801 
1802 		err = amidisplaycc_getcmap(view, &tmpcmap);
1803 		if (err)
1804 			return (err);
1805 
1806 		/* copy data to user space */
1807 
1808 		err = copyout(cmred, cmap->red, cmap->count);
1809 		if (err)
1810 			return (err);
1811 
1812 		err = copyout(cmgrn, cmap->green, cmap->count);
1813 		if (err)
1814 			return (err);
1815 
1816 		err = copyout(cmblu, cmap->blue, cmap->count);
1817 		if (err)
1818 			return (err);
1819 
1820 		return (0);
1821 
1822 	} else
1823 		return (EPASSTHROUGH);
1824 }
1825 
1826 /*
1827  * Set the palette of a emulation screen.
1828  * Here we do only color remapping and then call
1829  * amidisplaycc_setcmap to do the work.
1830  */
1831 
1832 static int
1833 amidisplaycc_setemulcmap(struct amidisplaycc_screen *scr,
1834 			 struct wsdisplay_cmap *cmap)
1835 {
1836 	struct wsdisplay_cmap  tmpcmap;
1837 
1838 	u_char                 red [MAXCOLORS];
1839 	u_char                 grn [MAXCOLORS];
1840 	u_char                 blu [MAXCOLORS];
1841 
1842 	int                    rc;
1843 	int                    i;
1844 
1845 	/*
1846 	 * Get old palette first.
1847 	 * Because of the color mapping going on in the emulation
1848 	 * screen the color range may not be contiguous in the real
1849 	 * palette.
1850 	 * So get the whole palette, insert the new colors
1851 	 * at the appropriate places and then set the whole
1852 	 * palette back.
1853 	 */
1854 
1855 	tmpcmap.index = 0;
1856 	tmpcmap.count = 1 << scr->depth;
1857 	tmpcmap.red   = red;
1858 	tmpcmap.green = grn;
1859 	tmpcmap.blue  = blu;
1860 
1861 	rc = amidisplaycc_getcmap(scr->view, &tmpcmap);
1862 	if (rc)
1863 		return (rc);
1864 
1865 	for (i = cmap->index ; i < cmap->index + cmap->count ; i++) {
1866 
1867 		tmpcmap.red   [ scr->colormap[ i ] ] = cmap->red   [ i ];
1868 		tmpcmap.green [ scr->colormap[ i ] ] = cmap->green [ i ];
1869 		tmpcmap.blue  [ scr->colormap[ i ] ] = cmap->blue  [ i ];
1870 	}
1871 
1872 	rc = amidisplaycc_setcmap(scr->view, &tmpcmap);
1873 	if (rc)
1874 		return (rc);
1875 
1876 	return (0);
1877 }
1878 
1879 
1880 /*
1881  * Set the colormap for the given screen.
1882  */
1883 
1884 static int
1885 amidisplaycc_setcmap(view_t *view, struct wsdisplay_cmap *cmap)
1886 {
1887 	u_long      cmentries [MAXCOLORS];
1888 
1889 	int         green_div;
1890 	int         blue_div;
1891 	int         grey_div;
1892 	int         red_div;
1893 	u_int       colors;
1894 	int         index;
1895 	int         count;
1896 	int         err;
1897 	colormap_t  cm;
1898 	int         c;
1899 
1900 	if (view == NULL)
1901 		return (EINVAL);
1902 
1903 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue) {
1904 		dprintf("amidisplaycc_setcmap: other==NULL\n");
1905 		return (EINVAL);
1906 	}
1907 
1908 	index  = cmap->index;
1909 	count  = cmap->count;
1910 	colors = (1 << view->bitmap->depth);
1911 
1912 	if (count > colors || index >= colors || index + count > colors)
1913 		return (EINVAL);
1914 
1915 	if (count == 0)
1916 		return (0);
1917 
1918 	cm.entry = cmentries;
1919 	cm.first = index;
1920 	cm.size  = count;
1921 
1922 	/*
1923 	 * Get the old colormap. We need to do this at least to know
1924 	 * how many bits to use with the color values.
1925 	 */
1926 
1927 	err = grf_get_colormap(view, &cm);
1928 	if (err)
1929 		return (err);
1930 
1931 	/*
1932 	 * The palette entries from wscons contain 8 bits per gun.
1933 	 * We need to convert them to the number of bits the view
1934 	 * expects. That is typically 4 or 8. Here we calculate the
1935 	 * conversion constants with which we divide the color values.
1936 	 */
1937 
1938 	if (cm.type == CM_COLOR) {
1939 		red_div = 256 / (cm.red_mask + 1);
1940 		green_div = 256 / (cm.green_mask + 1);
1941 		blue_div = 256 / (cm.blue_mask + 1);
1942 	} else if (cm.type == CM_GREYSCALE)
1943 		grey_div = 256 / (cm.grey_mask + 1);
1944 	else
1945 		return (EINVAL); /* Hmhh */
1946 
1947 	/* Copy our new values to the current colormap */
1948 
1949 	for (c = 0 ; c < count ; c++) {
1950 
1951 		if (cm.type == CM_COLOR) {
1952 
1953 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
1954 				cmap->red[c] / red_div,
1955 				cmap->green[c] / green_div,
1956 				cmap->blue[c] / blue_div);
1957 
1958 		} else if (cm.type == CM_GREYSCALE) {
1959 
1960 			/* Generate grey from average of r-g-b (?) */
1961 
1962 			cm.entry[c + index] = MAKE_COLOR_ENTRY(
1963 				0,
1964 				0,
1965 				(cmap->red[c] +
1966 				 cmap->green[c] +
1967 				 cmap->blue[c]) / 3 / grey_div);
1968 		}
1969 	}
1970 
1971 
1972 	/*
1973 	 * Now we have a new colormap that contains all the entries. Set
1974 	 * it to the view.
1975 	 */
1976 
1977 	err = grf_use_colormap(view, &cm);
1978 	if (err)
1979 		return err;
1980 
1981 	return (0);
1982 }
1983 
1984 /*
1985  * Return the colormap of the given screen.
1986  */
1987 
1988 static int
1989 amidisplaycc_getcmap(view_t *view, struct wsdisplay_cmap *cmap)
1990 {
1991 	u_long      cmentries [MAXCOLORS];
1992 
1993 	int         green_mul;
1994 	int         blue_mul;
1995 	int         grey_mul;
1996 	int         red_mul;
1997 	u_int       colors;
1998 	int         index;
1999 	int         count;
2000 	int         err;
2001 	colormap_t  cm;
2002 	int         c;
2003 
2004 	if (view == NULL)
2005 		return (EINVAL);
2006 
2007 	if (!cmap || !cmap->red || !cmap->green || !cmap->blue)
2008 		return (EINVAL);
2009 
2010 	index  = cmap->index;
2011 	count  = cmap->count;
2012 	colors = (1 << view->bitmap->depth);
2013 
2014 	if (count > colors || index >= colors || index + count > colors)
2015 		return (EINVAL);
2016 
2017 	if (count == 0)
2018 		return (0);
2019 
2020 	cm.entry = cmentries;
2021 	cm.first = index;
2022 	cm.size  = count;
2023 
2024 
2025 	err = grf_get_colormap(view, &cm);
2026 	if (err)
2027 		return (err);
2028 
2029 	if (cm.type == CM_COLOR) {
2030 		red_mul   = 256 / (cm.red_mask + 1);
2031 		green_mul = 256 / (cm.green_mask + 1);
2032 		blue_mul  = 256 / (cm.blue_mask + 1);
2033 	} else if (cm.type == CM_GREYSCALE) {
2034 		grey_mul  = 256 / (cm.grey_mask + 1);
2035 	} else
2036 		return (EINVAL);
2037 
2038 	/*
2039 	 * Copy color data to wscons-style structure. Translate to
2040 	 * 8 bits/gun from whatever resolution the color natively is.
2041 	 */
2042 
2043 	for (c = 0 ; c < count ; c++) {
2044 
2045 		if (cm.type == CM_COLOR) {
2046 
2047 			cmap->red[c]   = CM_GET_RED(cm.entry[index+c]);
2048 			cmap->green[c] = CM_GET_GREEN(cm.entry[index+c]);
2049 			cmap->blue[c]  = CM_GET_BLUE(cm.entry[index+c]);
2050 
2051 			cmap->red[c]   *= red_mul;
2052 			cmap->green[c] *= green_mul;
2053 			cmap->blue[c]  *= blue_mul;
2054 
2055 		} else if (cm.type == CM_GREYSCALE) {
2056 			cmap->red[c]   = CM_GET_GREY(cm.entry[index+c]);
2057 			cmap->red[c]  *= grey_mul;
2058 
2059 			cmap->green[c] = cmap->red[c];
2060 			cmap->blue[c]  = cmap->red[c];
2061 		}
2062 	}
2063 
2064 	return (0);
2065 }
2066 
2067 /* ARGSUSED */
2068 void
2069 amidisplaycc_pollc(void *cookie, int on)
2070 {
2071 }
2072 
2073 /*
2074  * These dummy functions are here just so that we can compete of
2075  * the console at init.
2076  * If we win the console then the wscons system will provide the
2077  * real ones which in turn will call the apropriate wskbd device.
2078  * These should never be called.
2079  */
2080 
2081 /* ARGSUSED */
2082 void
2083 amidisplaycc_cnputc(dev_t cd, int ch)
2084 {
2085 }
2086 
2087 /* ARGSUSED */
2088 int
2089 amidisplaycc_cngetc(dev_t cd)
2090 {
2091 	return (0);
2092 }
2093 
2094 /* ARGSUSED */
2095 void
2096 amidisplaycc_cnpollc(dev_t cd, int on)
2097 {
2098 }
2099 
2100 
2101 /*
2102  * Prints stuff if DEBUG is turned on.
2103  */
2104 
2105 /* ARGSUSED */
2106 static void
2107 dprintf(const char *fmt, ...)
2108 {
2109 #ifdef DEBUG
2110 	va_list ap;
2111 
2112 	va_start(ap, fmt);
2113 	vprintf(fmt, ap);
2114 	va_end(ap);
2115 #endif
2116 }
2117 
2118 #endif /* AMIDISPLAYCC */
2119