/*-
* Copyright (c) 2000 Jukka Andberg.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* wscons interface to amiga custom chips. Contains the necessary functions
* to render text on bitmapped screens. Uses the functions defined in
* grfabs_reg.h for display creation/destruction and low level setup.
*
* For each virtual terminal a new screen (a grfabs view) is allocated.
* Also one more view is allocated for the mapped screen on demand.
*/
/*
* List of supported screen types.
*
* The first item in list is used for the console screen.
* A suitable screen size is guessed for it by looking
* at the GRF_* options.
*/
struct amidisplaycc_screen_descr amidisplaycc_screentab[] = {
/* name, width, height, depth, fontwidth==8, fontheight */
#ifdef GRF_AGA
extern int aga_enable;
#else
static int aga_enable = 0;
#endif
/*
* This gets called at console init to determine the priority of
* this console device.
*
* Pointers to this and other functions must present
* in constab[] in conf.c for this to work.
*/
void
amidisplaycc_cnprobe(struct consdev *cd)
{
cd->cn_pri = CN_INTERNAL;
/*
* Yeah, real nice. But if we win the console then the wscons system
* does the proper initialization.
*/
cd->cn_dev = NODEV;
}
/*
* This gets called if this device is used as the console.
*/
void
amidisplaycc_cninit(struct consdev * cd)
{
void * cookie;
long attr;
int x;
int y;
/*
* This will do the basic stuff we also need.
*/
grfcc_probe();
#if NVIEW>0
viewprobe();
#endif
/*
* Set up wscons to handle the details.
* It will then call us back when it needs something
* display-specific. It will also set up cn_tab properly,
* something which we failed to do at amidisplaycc_cnprobe().
*/
/*
* The alloc_screen knows to allocate the first screen statically.
*/
amidisplaycc_alloc_screen(NULL, &amidisplaycc_screentab[0].wsdescr,
&cookie, &x, &y, &attr);
wsdisplay_cnattach(&amidisplaycc_screentab[0].wsdescr,
cookie, x, y, attr);
#if NKBD>0
/* tell kbd device it is used as console keyboard */
kbd_cnattach();
#endif
}
/*
* Attach only at real configuration time. Console init is done at
* the amidisplaycc_cninit function above.
*/
if (adp) {
printf(": Amiga custom chip graphics %s",
aga_enable ? "(AGA)" : "");
/*
* Called by wscons to draw/clear the cursor.
* We do this by xorring the block to the screen.
*
* This simple implementation will break if the screen is modified
* under the cursor before clearing it.
*/
void
amidisplaycc_cursor(void *screen, int on, int row, int col)
{
adccscr_t * scr;
scr = screen;
if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
return;
for (i = scr->fontheight ; i > 0 ; i--) {
*dst ^= 255;
dst += scr->linebytes;
}
}
int
amidisplaycc_mapchar(void *screen, int ch, unsigned int *chp)
{
if (ch > 0 && ch < 256) {
*chp = ch;
return 5;
}
*chp = ' ';
return 0;
}
/*
* Write a character to screen with color / bgcolor / hilite(bold) /
* underline / reverse.
* Surely could be made faster but I'm not sure if its worth the
* effort as scrolling is at least a magnitude slower.
*/
void
amidisplaycc_putchar(void *screen, int row, int col, u_int ch, long attr)
{
adccscr_t * scr;
u_char * dst;
u_char * font;
int fontheight;
u_int8_t * fontreal;
int fontlow;
int fonthigh;
int bmapoffset;
int linebytes;
int underline;
int fgcolor;
int bgcolor;
int plane;
int depth;
int mode;
int bold;
u_char f;
int j;
scr = screen;
if (row < 0 || col < 0 || row >= scr->nrows || col >= scr->ncols)
return;
/* Extract the colors from the attribute */
fgcolor = ATTRFG(attr);
bgcolor = ATTRBG(attr);
mode = ATTRMO(attr);
/*
* Copy a number of rows to another location on the screen.
*/
void
amidisplaycc_copyrows(void *screen, int srcrow, int dstrow, int nrows)
{
adccscr_t * scr;
u_char * src;
u_char * dst;
int srcbmapoffset;
int dstbmapoffset;
int widthbytes;
int fontheight;
int linebytes;
u_int copysize;
int rowdelta;
int rowbytes;
int srcmask;
int dstmask;
int bmdelta;
int depth;
int plane;
int i;
int j;
for (j = 0 ; j < nrows ; j++) {
/* Need to copy only planes that have data on src or dst */
srcmask = scr->rowmasks[srcrow];
dstmask = scr->rowmasks[dstrow];
scr->rowmasks[dstrow] = srcmask;
for (plane = 0 ; plane < depth ; plane++) {
if (srcmask & 1) {
/*
* Source row has data on this
* plane, copy it.
*/
if (fillsize > 0) {
/* If the rows are continuous, write them all. */
memset(dst, fill, fillsize);
} else {
for (j = 0 ; j < scr->fontheight * nrows ; j++) {
memset(dst, fill, widthbytes);
dst += linebytes;
}
}
bgcolor >>= 1;
}
amidisplaycc_cursor_draw(scr);
}
/*
* Compose an attribute value from foreground color,
* background color, and flags.
*/
int
amidisplaycc_allocattr(void *screen, int fg, int bg, int flags, long *attrp)
{
adccscr_t * scr;
int maxcolor;
int newfg;
int newbg;
/* Ensure the colors are displayable. */
newfg = fg & maxcolor;
newbg = bg & maxcolor;
#ifdef ADJUSTCOLORS
/*
* Hack for low-color screens, if background color is nonzero
* but would be displayed as one, adjust it.
*/
if (bg > 0 && newbg == 0)
newbg = maxcolor;
/*
* If foreground and background colors are different but would
* display the same fix them by modifying the foreground.
*/
if (fg != bg && newfg == newbg) {
if (newbg > 0)
newfg = 0;
else
newfg = maxcolor;
}
#endif
*attrp = MAKEATTR(newfg, newbg, flags);
return 0;
}
int
amidisplaycc_ioctl(void *dp, void *vs, u_long cmd, void *data, int flag,
struct lwp *l)
{
struct amidisplaycc_softc *adp;
adp = dp;
if (adp == NULL) {
printf("amidisplaycc_ioctl: adp==NULL\n");
return EINVAL;
}
/*
* Initialize (but not display) the view used for graphics.
*/
static void
amidisplaycc_initgfxview(struct amidisplaycc_softc *adp)
{
dimen_t dimension;
if (adp->gfxview == NULL) {
/* First time here, create the screen */
dimension.width = adp->gfxwidth;
dimension.height = adp->gfxheight;
adp->gfxview = grf_alloc_view(NULL,
&dimension,
adp->gfxdepth);
}
}
/*
* Switch to either emulation (text) or mapped (graphics) mode
* We keep an extra screen for mapped mode so it does not
* interfere with emulation screens.
*
* Once the extra screen is created, it never goes away.
*/
static int
amidisplaycc_setgfxview(struct amidisplaycc_softc *adp, int on)
{
dprintf("amidisplaycc: switching to %s mode.\n",
on ? "mapped" : "emul");
/* Current mode same as requested mode? */
if ( (on > 0) == (adp->gfxon > 0) )
return 0;
if (!on) {
/*
* Switch away from mapped mode. If there is
* a emulation screen, switch to it, otherwise
* just try to hide the mapped screen.
*/
adp->gfxon = 0;
if (adp->currentscreen)
grf_display_view(adp->currentscreen->view);
else if (adp->gfxview)
grf_remove_view(adp->gfxview);
return 0;
}
/* switch to mapped mode then */
amidisplaycc_initgfxview(adp);
if (adp->gfxview) {
adp->gfxon = 1;
grf_display_view(adp->gfxview);
} else {
printf("amidisplaycc: failed to make mapped screen\n");
return ENOMEM;
}
return 0;
}
/*
* Map the graphics screen. It must have been created before
* by switching to mapped mode by using an ioctl.
*/
paddr_t
amidisplaycc_mmap(void *dp, void *vs, off_t off, int prot)
{
struct amidisplaycc_softc * adp;
bmap_t * bm;
paddr_t rv;
adp = (struct amidisplaycc_softc*)dp;
/* Check we are in mapped mode */
if (adp->gfxon == 0 || adp->gfxview == NULL) {
dprintf("amidisplaycc_mmap: Not in mapped mode\n");
return (paddr_t)(-1);
}
/*
* Screen reserved for graphics is used to avoid writing
* over the text screens.
*/
bm = adp->gfxview->bitmap;
/* Check that the offset is valid */
if (off < 0 || off >= bm->depth * bm->bytes_per_row * bm->rows) {
dprintf("amidisplaycc_mmap: Offset out of range\n");
return (paddr_t)(-1);
}
rv = (paddr_t)bm->hardware_address;
rv += off;
return MD_BTOP(rv);
}
/*
* Create a new screen.
*
* NULL dp signifies console and then memory is allocated statically
* and the screen is automatically displayed.
*
* A font with suitable size is searched and if not found
* the builtin 8x8 font is used.
*
* There are separate default palettes for 2, 4 and 8+ color
* screens.
*/
int
amidisplaycc_alloc_screen(void *dp, const struct wsscreen_descr *screenp,
void **cookiep, int *curxp, int *curyp,
long *defattrp)
{
const struct amidisplaycc_screen_descr * adccscreenp;
struct amidisplaycc_screen * scr;
struct amidisplaycc_softc * adp;
view_t * view;
dimen_t dimension;
int fontheight;
int fontwidth;
int maxcolor;
int depth;
int i;
int j;
/*
* First screen gets the statically allocated console screen.
* Others are allocated dynamically.
*/
if (adp == NULL) {
scr = &amidisplaycc_consolescreen;
if (scr->isconsole)
panic("more than one console?");
/* Copies of most used values */
scr->width = dimension.width;
scr->height = dimension.height;
scr->depth = depth;
scr->widthbytes = view->bitmap->bytes_per_row;
scr->linebytes = scr->widthbytes + view->bitmap->row_mod;
scr->rowbytes = scr->linebytes * fontheight;
scr->device = adp;
/*
* Try to find a suitable font.
* Avoid everything but the builtin font for console screen.
* Builtin font is used if no other is found, even if it
* has the wrong size.
*/
for (i = 0 ; i < depth ; i++) {
scr->planes[i] = view->bitmap->plane[i];
}
for (i = 0 ; i < MAXROWS ; i++)
scr->rowmasks[i] = 0;
/* Simple one-to-one mapping for most colors */
for (i = 0 ; i < MAXCOLORS ; i++)
scr->colormap[i] = i;
/*
* Arrange the most used pens to quickest colors.
* The default color for given depth is (1<<depth)-1.
* It is assumed it is used most and it is mapped to
* color that can be drawn by writing data to one bitplane
* only.
* So map colors 3->2, 7->4, 15->8 and so on.
*/
for (i = 2 ; i < MAXCOLORS ; i *= 2) {
j = i * 2 - 1;
if (adp == NULL) {
dprintf("amidisplaycc_show_screen: adp==NULL\n");
return EINVAL;
}
if (scr == NULL) {
dprintf("amidisplaycc_show_screen: scr==NULL\n");
return EINVAL;
}
if (adp->gfxon) {
dprintf("amidisplaycc: Screen shift while in gfx mode?");
adp->gfxon = 0;
}
adp->currentscreen = scr;
adp->ison = 1;
grf_display_view(scr->view);
return 0;
}
/*
* Load/set a font.
*
* Only setting is supported, as the wsfont pseudo-device can
* handle the loading of fonts for us.
*/
int
amidisplaycc_load_font(void *dp, void *cookie, struct wsdisplay_font *font)
{
struct amidisplaycc_softc * adp __diagused;
struct amidisplaycc_screen * scr __diagused;
if (font->data)
{
/* request to load the font, not supported */
return EINVAL;
}
else
{
/* request to use the given font on this screen */
return amidisplaycc_setfont(scr, font->name);
}
}
/*
* Set display on/off.
*/
static int
amidisplaycc_setvideo(struct amidisplaycc_softc *adp, int mode)
{
view_t * view;
if (adp == NULL) {
dprintf("amidisplaycc_setvideo: adp==NULL\n");
return EINVAL;
}
if (adp->currentscreen == NULL) {
dprintf("amidisplaycc_setvideo: adp->currentscreen==NULL\n");
return EINVAL;
}
/* select graphics or emulation screen */
if (adp->gfxon && adp->gfxview)
view = adp->gfxview;
else
view = adp->currentscreen->view;
if (mode) {
/* on */
grf_display_view(view);
dprintf("amidisplaycc: video is now on\n");
adp->ison = 1;
} else {
/* off */
grf_remove_view(view);
dprintf("amidisplaycc: video is now off\n");
adp->ison = 0;
}
return 0;
}
/*
* Handle the WSDISPLAY_[PUT/GET]CMAP ioctls.
* Just handle the copying of data to/from userspace and
* let the functions amidisplaycc_setcmap and amidisplaycc_putcmap
* do the real work.
*/
u_char red [MAXCOLORS];
u_char grn [MAXCOLORS];
u_char blu [MAXCOLORS];
int rc;
int i;
/*
* Get old palette first.
* Because of the color mapping going on in the emulation
* screen the color range may not be contiguous in the real
* palette.
* So get the whole palette, insert the new colors
* at the appropriate places and then set the whole
* palette back.
*/
/*
* Get the old colormap. We need to do this at least to know
* how many bits to use with the color values.
*/
err = grf_get_colormap(view, &cm);
if (err)
return err;
/*
* The palette entries from wscons contain 8 bits per gun.
* We need to convert them to the number of bits the view
* expects. That is typically 4 or 8. Here we calculate the
* conversion constants with which we divide the color values.
*/
if (cm.type == CM_COLOR) {
int c, green_div, blue_div, red_div;
err = grf_get_colormap(view, &cm);
if (err)
return err;
/*
* Copy color data to wscons-style structure. Translate to
* 8 bits/gun from whatever resolution the color natively is.
*/
if (cm.type == CM_COLOR) {
int c, red_mul, green_mul, blue_mul;
/*
* Find and set a font for the given screen.
*
* If fontname is given, a font with that name and suitable
* size (determined by the screen) is searched for.
* If fontname is NULL, a font with suitable size is searched.
*
* On success, the found font is assigned to the screen and possible
* old font is freed.
*/
static int
amidisplaycc_setfont(struct amidisplaycc_screen *scr, const char *fontname)
{
struct wsdisplay_font *wsfont;
int wsfontcookie;
/*
* Return a font that is guaranteed to exist.
*/
static const struct wsdisplay_font *
amidisplaycc_getbuiltinfont(void)
{
static struct wsdisplay_font font;
/* these values aren't really used for anything */
font.encoding = WSDISPLAY_FONTENC_ISO;
font.bitorder = WSDISPLAY_FONTORDER_KNOWN;
font.byteorder = WSDISPLAY_FONTORDER_KNOWN;
return &font;
}
/* ARGSUSED */
void
amidisplaycc_pollc(void *cookie, int on)
{
if (amidisplaycc_consolescreen.isconsole)
{
if (on)
{
/* About to use console, so make it visible */
grf_display_view(amidisplaycc_consolescreen.view);
}
if (!on &&
amidisplaycc_consolescreen.isconsole &&
amidisplaycc_consolescreen.device != NULL &&
amidisplaycc_consolescreen.device->currentscreen != NULL)
{
/* Restore the correct view after done with console use */
grf_display_view(amidisplaycc_consolescreen.device->currentscreen->view);
}
}
}
/*
* These dummy functions are here just so that we can compete of
* the console at init.
* If we win the console then the wscons system will provide the
* real ones which in turn will call the appropriate wskbd device.
* These should never be called.
*/
/* ARGSUSED */
void
amidisplaycc_cnputc(dev_t cd, int ch)
{
}
/* ARGSUSED */
int
amidisplaycc_cngetc(dev_t cd)
{
return 0;
}
/* ARGSUSED */
void
amidisplaycc_cnpollc(dev_t cd, int on)
{
}