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