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