summaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorarpi_esp <arpi_esp@b3059339-0415-0410-9bf9-f77b7e298cf2>2001-02-24 20:28:24 +0000
committerarpi_esp <arpi_esp@b3059339-0415-0410-9bf9-f77b7e298cf2>2001-02-24 20:28:24 +0000
commitd34041569e71fc9bd772354e94dc9d16061072a5 (patch)
tree8f481cae1c70f32d1756fbe5f39000577b73042d /drivers
parente95a95ece09bac96bdfd37322f96c6f57ef79ebc (diff)
downloadmpv-d34041569e71fc9bd772354e94dc9d16061072a5.tar.bz2
mpv-d34041569e71fc9bd772354e94dc9d16061072a5.tar.xz
Initial revision
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@2 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'drivers')
-rw-r--r--drivers/3dfx.h154
-rw-r--r--drivers/Makefile22
-rw-r--r--drivers/README40
-rw-r--r--drivers/mga_vid.c877
-rw-r--r--drivers/mga_vid.h48
-rw-r--r--drivers/mga_vid_test.c221
-rw-r--r--drivers/sis_vid.c645
-rw-r--r--drivers/sis_vid.h37
8 files changed, 2044 insertions, 0 deletions
diff --git a/drivers/3dfx.h b/drivers/3dfx.h
new file mode 100644
index 0000000000..0097f4f39e
--- /dev/null
+++ b/drivers/3dfx.h
@@ -0,0 +1,154 @@
+/*
+ * 3dfx.h
+ *
+ * Copyright (C) Colin Cross Apr 2000
+ *
+ * mpeg2dec is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * mpeg2dec is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNU Make; see the file COPYING. If not, write to
+ * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+
+#define VOODOO_IO_REG_OFFSET ((unsigned long int)0x0000000)
+#define VOODOO_YUV_REG_OFFSET ((unsigned long int)0x0080100)
+#define VOODOO_AGP_REG_OFFSET ((unsigned long int)0x0080000)
+#define VOODOO_2D_REG_OFFSET ((unsigned long int)0x0100000)
+#define VOODOO_YUV_PLANE_OFFSET ((unsigned long int)0x0C00000)
+
+#define VOODOO_BLT_FORMAT_YUYV (8<<16)
+#define VOODOO_BLT_FORMAT_16 (3<<16)
+
+#define VOODOO_YUV_STRIDE (1024>>2)
+
+struct voodoo_yuv_fb_t {
+ uint32_t Y[0x0040000];
+ uint32_t U[0x0040000];
+ uint32_t V[0x0040000];
+};
+
+struct voodoo_yuv_reg_t {
+ uint32_t yuvBaseAddr;
+ uint32_t yuvStride;
+};
+
+struct voodoo_2d_reg_t {
+ uint32_t status;
+ uint32_t intCtrl;
+ uint32_t clip0Min;
+ uint32_t clip0Max;
+ uint32_t dstBaseAddr;
+ uint32_t dstFormat;
+ uint32_t srcColorkeyMin;
+ uint32_t srcColorkeyMax;
+ uint32_t dstColorkeyMin;
+ uint32_t dstColorkeyMax;
+ signed long bresError0;
+ signed long bresError1;
+ uint32_t rop;
+ uint32_t srcBaseAddr;
+ uint32_t commandExtra;
+ uint32_t lineStipple;
+ uint32_t lineStyle;
+ uint32_t pattern0Alias;
+ uint32_t pattern1Alias;;
+ uint32_t clip1Min;
+ uint32_t clip1Max;
+ uint32_t srcFormat;
+ uint32_t srcSize;
+ uint32_t srcXY;
+ uint32_t colorBack;
+ uint32_t colorFore;
+ uint32_t dstSize;
+ uint32_t dstXY;
+ uint32_t command;
+ uint32_t RESERVED1;
+ uint32_t RESERVED2;
+ uint32_t RESERVED3;
+ uint8_t launchArea[128];
+};
+
+
+struct voodoo_io_reg_t {
+ uint32_t status;
+ uint32_t pciInit0;
+ uint32_t sipMonitor;
+ uint32_t lfbMemoryConfig;
+ uint32_t miscInit0;
+ uint32_t miscInit1;
+ uint32_t dramInit0;
+ uint32_t dramInit1;
+ uint32_t agpInit;
+ uint32_t tmuGbeInit;
+ uint32_t vgaInit0;
+ uint32_t vgaInit1;
+ uint32_t dramCommand;
+ uint32_t dramData;
+ uint32_t RESERVED1;
+ uint32_t RESERVED2;
+
+ uint32_t pllCtrl0;
+ uint32_t pllCtrl1;
+ uint32_t pllCtrl2;
+ uint32_t dacMode;
+ uint32_t dacAddr;
+ uint32_t dacData;
+
+ uint32_t rgbMaxDelta;
+ uint32_t vidProcCfg;
+ uint32_t hwCurPatAddr;
+ uint32_t hwCurLoc;
+ uint32_t hwCurC0;
+ uint32_t hwCurC1;
+ uint32_t vidInFormat;
+ uint32_t vidInStatus;
+ uint32_t vidSerialParallelPort;
+ uint32_t vidInXDecimDeltas;
+ uint32_t vidInDecimInitErrs;
+ uint32_t vidInYDecimDeltas;
+ uint32_t vidPixelBufThold;
+ uint32_t vidChromaMin;
+ uint32_t vidChromaMax;
+ uint32_t vidCurrentLine;
+ uint32_t vidScreenSize;
+ uint32_t vidOverlayStartCoords;
+ uint32_t vidOverlayEndScreenCoord;
+ uint32_t vidOverlayDudx;
+ uint32_t vidOverlayDudxOffsetSrcWidth;
+ uint32_t vidOverlayDvdy;
+
+ uint32_t vga_registers_not_mem_mapped[12];
+ uint32_t vidOverlayDvdyOffset;
+ uint32_t vidDesktopStartAddr;
+ uint32_t vidDesktopOverlayStride;
+ uint32_t vidInAddr0;
+ uint32_t vidInAddr1;
+ uint32_t vidInAddr2;
+ uint32_t vidInStride;
+ uint32_t vidCurrOverlayStartAddr;
+};
+
+
+struct pioData_t {
+ short port;
+ short size;
+ int device;
+ void *value;
+};
+
+typedef struct pioData_t pioData;
+typedef struct voodoo_2d_reg_t voodoo_2d_reg;
+typedef struct voodoo_io_reg_t voodoo_io_reg;
+typedef struct voodoo_yuv_reg_t voodoo_yuv_reg;
+typedef struct voodoo_yuv_fb_t voodoo_yuv_fb;
+
diff --git a/drivers/Makefile b/drivers/Makefile
new file mode 100644
index 0000000000..67deaf85cf
--- /dev/null
+++ b/drivers/Makefile
@@ -0,0 +1,22 @@
+
+CC = gcc
+KERNEL_INCLUDES = /usr/src/linux/include
+INCLUDES = -I$(KERNEL_INCLUDES)
+CFLAGS = -g -O2 -Wall -D__KERNEL__ -DMODULE -include $(KERNEL_INCLUDES)/linux/modversions.h
+
+all: mga_vid.o mga_vid_test sis_vid.o
+
+mga_vid.o: mga_vid.c mga_vid.h
+ $(CC) $(CFLAGS) $(INCLUDES) -c $(basename $@).c
+
+sis_vid.o: sis_vid.c sis_vid.h
+ $(CC) $(CFLAGS) $(INCLUDES) -c $(basename $@).c
+
+mga_vid_test: mga_vid_test.c
+ $(CC) -g -O -Wall $(INCLUDES) -o $@ $@.c
+
+clean:
+ rm -f *.o *~
+
+distclean: clean
+ rm -f mga_vid_test
diff --git a/drivers/README b/drivers/README
new file mode 100644
index 0000000000..bea354f489
--- /dev/null
+++ b/drivers/README
@@ -0,0 +1,40 @@
+
+mga_vid - MGA G200/G400 YUV Overlay kernel module
+
+ Author:
+ Aaron Holtzman <aholtzma@ess.engr.uvic.ca>, Oct 1999
+
+ Contributions by:
+ Fredrik Vraalsen <vraalsen@cs.uiuc.edu>
+ Alan Cox <alan@lxorguk.ukuu.org.uk>
+
+ WARNING ----- WARNING
+
+This code messes with your video card and your xserver. It will probably
+lock up your box, format your hard drive, and cause your brand new g400
+MAX to spout 6 inch flames. You have been warned.
+
+ WARNING ----- WARNING
+
+What does this code do?
+
+ mga_vid is a kernel module that utilitizes the Matrox g200/g400 video
+ scaler/overlay unit to perform YUV->RGB colorspace conversion and
+ arbitrary video scaling.
+
+ mga_vid is also a monster hack.
+
+How does mga_vid work?
+
+ This kernel module sets up the BES (backend scaler) with approriate values
+ based on parameters supplied via ioctl. It also maps a chunk of video
+ memory into userspace via mmap. This memory is stolen from X (which may
+ decide to write to it later). The application can then write image data
+ directly to the frame buffer (if it knows the right padding, etc).
+
+
+How do I know if mga_vid works on my system?
+
+ There are test applications called mga_vid_test_g400 and mga_vid_test_g200.
+ Use the appropriate one for your card. This test code should draw some
+ nice 256x256 images for you if all is working well.
diff --git a/drivers/mga_vid.c b/drivers/mga_vid.c
new file mode 100644
index 0000000000..1a5bf7162e
--- /dev/null
+++ b/drivers/mga_vid.c
@@ -0,0 +1,877 @@
+// YUY2 support (see config.format) added by A'rpi/ESP-team
+
+/*
+ *
+ * mga_vid.c
+ *
+ * Copyright (C) 1999 Aaron Holtzman
+ *
+ * Module skeleton based on gutted agpgart module by Jeff Hartmann
+ * <slicer@ionet.net>
+ *
+ * Matrox MGA G200/G400 YUV Video Interface module Version 0.1.0
+ *
+ * BES == Back End Scaler
+ *
+ * This software has been released under the terms of the GNU Public
+ * license. See http://www.gnu.org/copyleft/gpl.html for details.
+ */
+
+//It's entirely possible this major conflicts with something else
+/* mknod /dev/mga_vid c 178 0 */
+
+#include <linux/config.h>
+#include <linux/version.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/mm.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/malloc.h>
+#include <linux/pci.h>
+#include <linux/init.h>
+#include <linux/videodev.h>
+
+#include "mga_vid.h"
+
+#ifdef CONFIG_MTRR
+#include <asm/mtrr.h>
+#endif
+
+#include <asm/uaccess.h>
+#include <asm/system.h>
+#include <asm/io.h>
+
+#define TRUE 1
+#define FALSE 0
+
+#define MGA_VID_MAJOR 178
+
+#define MGA_VIDMEM_SIZE mga_ram_size
+
+#ifndef PCI_DEVICE_ID_MATROX_G200_PCI
+#define PCI_DEVICE_ID_MATROX_G200_PCI 0x0520
+#endif
+
+#ifndef PCI_DEVICE_ID_MATROX_G200_AGP
+#define PCI_DEVICE_ID_MATROX_G200_AGP 0x0521
+#endif
+
+#ifndef PCI_DEVICE_ID_MATROX_G400
+#define PCI_DEVICE_ID_MATROX_G400 0x0525
+#endif
+
+MODULE_AUTHOR("Aaron Holtzman <aholtzma@engr.uvic.ca>");
+
+
+typedef struct bes_registers_s
+{
+ //BES Control
+ uint32_t besctl;
+ //BES Global control
+ uint32_t besglobctl;
+ //Luma control (brightness and contrast)
+ uint32_t beslumactl;
+ //Line pitch
+ uint32_t bespitch;
+
+ //Buffer A-1 Chroma 3 plane org
+ uint32_t besa1c3org;
+ //Buffer A-1 Chroma org
+ uint32_t besa1corg;
+ //Buffer A-1 Luma org
+ uint32_t besa1org;
+
+ //Buffer A-2 Chroma 3 plane org
+ uint32_t besa2c3org;
+ //Buffer A-2 Chroma org
+ uint32_t besa2corg;
+ //Buffer A-2 Luma org
+ uint32_t besa2org;
+
+ //Buffer B-1 Chroma 3 plane org
+ uint32_t besb1c3org;
+ //Buffer B-1 Chroma org
+ uint32_t besb1corg;
+ //Buffer B-1 Luma org
+ uint32_t besb1org;
+
+ //Buffer B-2 Chroma 3 plane org
+ uint32_t besb2c3org;
+ //Buffer B-2 Chroma org
+ uint32_t besb2corg;
+ //Buffer B-2 Luma org
+ uint32_t besb2org;
+
+ //BES Horizontal coord
+ uint32_t beshcoord;
+ //BES Horizontal inverse scaling [5.14]
+ uint32_t beshiscal;
+ //BES Horizontal source start [10.14] (for scaling)
+ uint32_t beshsrcst;
+ //BES Horizontal source ending [10.14] (for scaling)
+ uint32_t beshsrcend;
+ //BES Horizontal source last
+ uint32_t beshsrclst;
+
+
+ //BES Vertical coord
+ uint32_t besvcoord;
+ //BES Vertical inverse scaling [5.14]
+ uint32_t besviscal;
+ //BES Field 1 vertical source last position
+ uint32_t besv1srclst;
+ //BES Field 1 weight start
+ uint32_t besv1wght;
+ //BES Field 2 vertical source last position
+ uint32_t besv2srclst;
+ //BES Field 2 weight start
+ uint32_t besv2wght;
+
+} bes_registers_t;
+
+static bes_registers_t regs;
+static uint32_t mga_vid_in_use = 0;
+static uint32_t is_g400 = 0;
+static uint32_t vid_src_ready = 0;
+static uint32_t vid_overlay_on = 0;
+
+static uint8_t *mga_mmio_base = 0;
+static uint32_t mga_mem_base = 0;
+static uint32_t mga_src_base = 0;
+
+static uint32_t mga_ram_size = 0;
+
+static struct pci_dev *pci_dev;
+
+static struct video_window mga_win;
+static mga_vid_config_t mga_config;
+
+
+//All register offsets are converted to word aligned offsets (32 bit)
+//because we want all our register accesses to be 32 bits
+#define VCOUNT 0x1e20
+
+#define PALWTADD 0x3c00 // Index register for X_DATAREG port
+#define X_DATAREG 0x3c0a
+
+#define XMULCTRL 0x19
+#define BPP_8 0x00
+#define BPP_15 0x01
+#define BPP_16 0x02
+#define BPP_24 0x03
+#define BPP_32_DIR 0x04
+#define BPP_32_PAL 0x07
+
+#define XCOLMSK 0x40
+#define X_COLKEY 0x42
+#define XKEYOPMODE 0x51
+#define XCOLMSK0RED 0x52
+#define XCOLMSK0GREEN 0x53
+#define XCOLMSK0BLUE 0x54
+#define XCOLKEY0RED 0x55
+#define XCOLKEY0GREEN 0x56
+#define XCOLKEY0BLUE 0x57
+
+// Backend Scaler registers
+#define BESCTL 0x3d20
+#define BESGLOBCTL 0x3dc0
+#define BESLUMACTL 0x3d40
+#define BESPITCH 0x3d24
+#define BESA1C3ORG 0x3d60
+#define BESA1CORG 0x3d10
+#define BESA1ORG 0x3d00
+#define BESA2C3ORG 0x3d64
+#define BESA2CORG 0x3d14
+#define BESA2ORG 0x3d04
+#define BESB1C3ORG 0x3d68
+#define BESB1CORG 0x3d18
+#define BESB1ORG 0x3d08
+#define BESB2C3ORG 0x3d6C
+#define BESB2CORG 0x3d1C
+#define BESB2ORG 0x3d0C
+#define BESHCOORD 0x3d28
+#define BESHISCAL 0x3d30
+#define BESHSRCEND 0x3d3C
+#define BESHSRCLST 0x3d50
+#define BESHSRCST 0x3d38
+#define BESV1WGHT 0x3d48
+#define BESV2WGHT 0x3d4c
+#define BESV1SRCLST 0x3d54
+#define BESV2SRCLST 0x3d58
+#define BESVISCAL 0x3d34
+#define BESVCOORD 0x3d2c
+#define BESSTATUS 0x3dc4
+
+
+static void mga_vid_frame_sel(int frame)
+{
+ //we don't need the vcount protection as we're only hitting
+ //one register (and it doesn't seem to be double buffered)
+ regs.besctl = (regs.besctl & ~0x07000000) + (frame << 25);
+ writel( regs.besctl, mga_mmio_base + BESCTL );
+}
+
+
+static void mga_vid_write_regs(void)
+{
+ //Make sure internal registers don't get updated until we're done
+ writel( (readl(mga_mmio_base + VCOUNT)-1)<<16,
+ mga_mmio_base + BESGLOBCTL);
+
+ // color or coordinate keying
+ writeb( XKEYOPMODE, mga_mmio_base + PALWTADD);
+ writeb( mga_config.colkey_on, mga_mmio_base + X_DATAREG);
+ if ( mga_config.colkey_on )
+ {
+ uint32_t r=0, g=0, b=0;
+
+ writeb( XMULCTRL, mga_mmio_base + PALWTADD);
+ switch (readb (mga_mmio_base + X_DATAREG))
+ {
+ case BPP_8:
+ /* Need to look up the color index, just using
+ color 0 for now. */
+ break;
+
+ case BPP_15:
+ r = mga_config.colkey_red >> 3;
+ g = mga_config.colkey_green >> 3;
+ b = mga_config.colkey_blue >> 3;
+ break;
+
+ case BPP_16:
+ r = mga_config.colkey_red >> 3;
+ g = mga_config.colkey_green >> 2;
+ b = mga_config.colkey_blue >> 3;
+ break;
+
+ case BPP_24:
+ case BPP_32_DIR:
+ case BPP_32_PAL:
+ r = mga_config.colkey_red;
+ g = mga_config.colkey_green;
+ b = mga_config.colkey_blue;
+ break;
+ }
+
+ // Disable color keying on alpha channel
+ writeb( XCOLMSK, mga_mmio_base + PALWTADD);
+ writeb( 0x00, mga_mmio_base + X_DATAREG);
+ writeb( X_COLKEY, mga_mmio_base + PALWTADD);
+ writeb( 0x00, mga_mmio_base + X_DATAREG);
+
+ // Set up color key registers
+ writeb( XCOLKEY0RED, mga_mmio_base + PALWTADD);
+ writeb( r, mga_mmio_base + X_DATAREG);
+ writeb( XCOLKEY0GREEN, mga_mmio_base + PALWTADD);
+ writeb( g, mga_mmio_base + X_DATAREG);
+ writeb( XCOLKEY0BLUE, mga_mmio_base + PALWTADD);
+ writeb( b, mga_mmio_base + X_DATAREG);
+
+ // Set up color key mask registers
+ writeb( XCOLMSK0RED, mga_mmio_base + PALWTADD);
+ writeb( 0xff, mga_mmio_base + X_DATAREG);
+ writeb( XCOLMSK0GREEN, mga_mmio_base + PALWTADD);
+ writeb( 0xff, mga_mmio_base + X_DATAREG);
+ writeb( XCOLMSK0BLUE, mga_mmio_base + PALWTADD);
+ writeb( 0xff, mga_mmio_base + X_DATAREG);
+ }
+
+ // Backend Scaler
+ writel( regs.besctl, mga_mmio_base + BESCTL);
+ if(is_g400)
+ writel( regs.beslumactl, mga_mmio_base + BESLUMACTL);
+ writel( regs.bespitch, mga_mmio_base + BESPITCH);
+
+ writel( regs.besa1org, mga_mmio_base + BESA1ORG);
+ writel( regs.besa1corg, mga_mmio_base + BESA1CORG);
+ writel( regs.besb1org, mga_mmio_base + BESB1ORG);
+ writel( regs.besb1corg, mga_mmio_base + BESB1CORG);
+ if(is_g400)
+ {
+ writel( regs.besa1c3org, mga_mmio_base + BESA1C3ORG);
+ writel( regs.besb1c3org, mga_mmio_base + BESB1C3ORG);
+ }
+
+ writel( regs.beshcoord, mga_mmio_base + BESHCOORD);
+ writel( regs.beshiscal, mga_mmio_base + BESHISCAL);
+ writel( regs.beshsrcst, mga_mmio_base + BESHSRCST);
+ writel( regs.beshsrcend, mga_mmio_base + BESHSRCEND);
+ writel( regs.beshsrclst, mga_mmio_base + BESHSRCLST);
+
+ writel( regs.besvcoord, mga_mmio_base + BESVCOORD);
+ writel( regs.besviscal, mga_mmio_base + BESVISCAL);
+ writel( regs.besv1srclst, mga_mmio_base + BESV1SRCLST);
+ writel( regs.besv1wght, mga_mmio_base + BESV1WGHT);
+
+ //update the registers somewhere between 1 and 2 frames from now.
+ writel( regs.besglobctl + ((readl(mga_mmio_base + VCOUNT)+2)<<16),
+ mga_mmio_base + BESGLOBCTL);
+
+ printk("mga_vid: wrote BES registers\n");
+ printk("mga_vid: BESCTL = 0x%08x\n",
+ readl(mga_mmio_base + BESCTL));
+ printk("mga_vid: BESGLOBCTL = 0x%08x\n",
+ readl(mga_mmio_base + BESGLOBCTL));
+ printk("mga_vid: BESSTATUS= 0x%08x\n",
+ readl(mga_mmio_base + BESSTATUS));
+}
+
+static int mga_vid_set_config(mga_vid_config_t *config)
+{
+ int x, y, sw, sh, dw, dh;
+ int besleft, bestop, ifactor, ofsleft, ofstop, baseadrofs, weight, weights;
+ int frame_size;
+ x = config->x_org;
+ y = config->y_org;
+ sw = config->src_width;
+ sh = config->src_height;
+ dw = config->dest_width;
+ dh = config->dest_height;
+
+ printk("mga_vid: Setting up a %dx%d+%d+%d video window (src %dx%d) format %X\n",
+ dw, dh, x, y, sw, sh, config->format);
+
+ //FIXME check that window is valid and inside desktop
+
+ //FIXME figure out a better way to allocate memory on card
+ //allocate 2 megs
+ //mga_src_base = mga_mem_base + (MGA_VIDMEM_SIZE-2) * 0x100000;
+ mga_src_base = (MGA_VIDMEM_SIZE-2) * 0x100000;
+
+
+ //Setup the BES registers for a three plane 4:2:0 video source
+
+switch(config->format){
+ case MGA_VID_FORMAT_YV12:
+ regs.besctl = 1 // BES enabled
+ + (0<<6) // even start polarity
+ + (1<<10) // x filtering enabled
+ + (1<<11) // y filtering enabled
+ + (1<<16) // chroma upsampling
+ + (1<<17) // 4:2:0 mode
+ + (1<<18); // dither enabled
+
+ if(is_g400)
+ {
+ //zoom disabled, zoom filter disabled, 420 3 plane format, proc amp
+ //disabled, rgb mode disabled
+ regs.besglobctl = (1<<5);
+ }
+ else
+ {
+ //zoom disabled, zoom filter disabled, Cb samples in 0246, Cr
+ //in 1357, BES register update on besvcnt
+ regs.besglobctl = 0;
+ }
+ break;
+
+ case MGA_VID_FORMAT_YUY2:
+ regs.besctl = 1 // BES enabled
+ + (0<<6) // even start polarity
+ + (1<<10) // x filtering enabled
+ + (1<<11) // y filtering enabled
+ + (1<<16) // chroma upsampling
+ + (0<<17) // 4:2:2 mode
+ + (1<<18); // dither enabled
+
+ regs.besglobctl = 0; // YUY2 format selected
+ break;
+ default:
+ return -1;
+}
+
+
+ //Disable contrast and brightness control
+ regs.besglobctl = (1<<5) + (1<<7);
+ regs.beslumactl = (0x7f << 16) + (0x80<<0);
+ regs.beslumactl = 0x80<<0;
+
+ //Setup destination window boundaries
+ besleft = x > 0 ? x : 0;
+ bestop = y > 0 ? y : 0;
+ regs.beshcoord = (besleft<<16) + (x + dw-1);
+ regs.besvcoord = (bestop<<16) + (y + dh-1);
+
+ //Setup source dimensions
+ regs.beshsrclst = (sw - 1) << 16;
+ regs.bespitch = (sw + 31) & ~31 ;
+
+ //Setup horizontal scaling
+ ifactor = ((sw-1)<<14)/(dw-1);
+ ofsleft = besleft - x;
+
+ regs.beshiscal = ifactor<<2;
+ regs.beshsrcst = (ofsleft*ifactor)<<2;
+ regs.beshsrcend = regs.beshsrcst + (((dw - ofsleft - 1) * ifactor) << 2);
+
+ //Setup vertical scaling
+ ifactor = ((sh-1)<<14)/(dh-1);
+ ofstop = bestop - y;
+
+ regs.besviscal = ifactor<<2;
+
+ baseadrofs = ((ofstop*regs.besviscal)>>16)*regs.bespitch;
+ frame_size = ((sw + 31) & ~31) * sh + (((sw + 31) & ~31) * sh) / 2;
+ regs.besa1org = (uint32_t) mga_src_base + baseadrofs;
+ regs.besb1org = (uint32_t) mga_src_base + baseadrofs + frame_size;
+
+ if (is_g400)
+ baseadrofs = (((ofstop*regs.besviscal)/4)>>16)*regs.bespitch;
+ else
+ baseadrofs = (((ofstop*regs.besviscal)/2)>>16)*regs.bespitch;
+
+ regs.besa1corg = (uint32_t) mga_src_base + baseadrofs + regs.bespitch * sh ;
+ regs.besb1corg = (uint32_t) mga_src_base + baseadrofs + frame_size + regs.bespitch * sh;
+ regs.besa1c3org = regs.besa1corg + ((regs.bespitch * sh) / 4);
+ regs.besb1c3org = regs.besb1corg + ((regs.bespitch * sh) / 4);
+
+ weight = ofstop * (regs.besviscal >> 2);
+ weights = weight < 0 ? 1 : 0;
+ regs.besv1wght = (weights << 16) + ((weight & 0x3FFF) << 2);
+ regs.besv1srclst = sh - 1 - (((ofstop * regs.besviscal) >> 16) & 0x03FF);
+
+ mga_vid_write_regs();
+ return 0;
+}
+
+
+static int mga_vid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+ int frame;
+
+ switch(cmd)
+ {
+ case MGA_VID_CONFIG:
+ //FIXME remove
+ printk("vcount = %d\n",readl(mga_mmio_base + VCOUNT));
+ printk("mga_mmio_base = %p\n",mga_mmio_base);
+ printk("mga_mem_base = %08lx\n",mga_mem_base);
+ //FIXME remove
+
+ printk("mga_vid: Received configuration\n");
+
+ if(copy_from_user(&mga_config,(mga_vid_config_t*) arg,sizeof(mga_vid_config_t)))
+ {
+ printk("mga_vid: failed copy from userspace\n");
+ return(-EFAULT);
+ }
+ if (is_g400)
+ mga_config.card_type = MGA_G400;
+ else
+ mga_config.card_type = MGA_G200;
+
+ mga_config.ram_size = mga_ram_size;
+
+ if (copy_to_user((mga_vid_config_t *) arg, &mga_config, sizeof(mga_vid_config_t)))
+ {
+ printk("mga_vid: failed copy to userspace\n");
+ return(-EFAULT);
+ }
+ return mga_vid_set_config(&mga_config);
+ break;
+
+ case MGA_VID_ON:
+ printk("mga_vid: Video ON\n");
+ vid_src_ready = 1;
+ if(vid_overlay_on)
+ {
+ regs.besctl |= 1;
+ mga_vid_write_regs();
+ }
+ break;
+
+ case MGA_VID_OFF:
+ printk("mga_vid: Video OFF\n");
+ vid_src_ready = 0;
+ regs.besctl &= ~1;
+ mga_vid_write_regs();
+ break;
+
+ case MGA_VID_FSEL:
+ if(copy_from_user(&frame,(int *) arg,sizeof(int)))
+ {
+ printk("mga_vid: FSEL failed copy from userspace\n");
+ return(-EFAULT);
+ }
+
+ mga_vid_frame_sel(frame);
+ break;
+
+ default:
+ printk("mga_vid: Invalid ioctl\n");
+ return (-EINVAL);
+ }
+
+ return 0;
+}
+
+
+static int mga_vid_find_card(void)
+{
+ struct pci_dev *dev = NULL;
+ unsigned int card_option, temp;
+
+ if((dev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, NULL)))
+ {
+ is_g400 = 1;
+ printk("mga_vid: Found MGA G400\n");
+ }
+ else if((dev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, NULL)))
+ {
+ is_g400 = 0;
+ printk("mga_vid: Found MGA G200 AGP\n");
+ }
+ else if((dev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_PCI, NULL)))
+ {
+ is_g400 = 0;
+ printk("mga_vid: Found MGA G200 PCI\n");
+ }
+ else
+ {
+ printk("mga_vid: No supported cards found\n");
+ return FALSE;
+ }
+
+ pci_dev = dev;
+
+#if LINUX_VERSION_CODE >= 0x020300
+ mga_mmio_base = ioremap_nocache(dev->resource[1].start,0x4000);
+ mga_mem_base = dev->resource[0].start;
+#else
+ mga_mmio_base = ioremap_nocache(dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK,0x4000);
+ mga_mem_base = dev->base_address[0] & PCI_BASE_ADDRESS_MEM_MASK;
+#endif
+ printk("mga_vid: MMIO at 0x%p\n", mga_mmio_base);
+ printk("mga_vid: Frame Buffer at 0x%08lX\n", mga_mem_base);
+
+ pci_read_config_dword(dev, 0x40, &card_option);
+ printk("OPTION word: 0x%08x\n", card_option);
+
+ temp = (card_option >> 10) & 0x17;
+
+ if (is_g400)
+ {
+ switch(temp)
+ {
+ default:
+ mga_ram_size = 16;
+ }
+ }
+ else
+ {
+ // a g200
+ switch(temp)
+ {
+ default:
+ mga_ram_size = 8;
+ }
+ }
+
+ printk("mga_vid: RAMSIZE seems to be %d MB\n", (unsigned int) mga_ram_size);
+
+ return TRUE;
+}
+
+
+static ssize_t mga_vid_read(struct file *file, char *buf, size_t count, loff_t *ppos)
+{
+ return -EINVAL;
+}
+
+static ssize_t mga_vid_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
+{
+ return -EINVAL;
+}
+
+static int mga_vid_mmap(struct file *file, struct vm_area_struct *vma)
+{
+
+ printk("mga_vid: mapping video memory into userspace\n");
+ if(remap_page_range(vma->vm_start, mga_mem_base + (MGA_VIDMEM_SIZE-2) * 0x100000,
+ vma->vm_end - vma->vm_start, vma->vm_page_prot))
+ {
+ printk("mga_vid: error mapping video memory\n");
+ return(-EAGAIN);
+ }
+
+ return(0);
+}
+
+static int mga_vid_release(struct inode *inode, struct file *file)
+{
+ //Close the window just in case
+ vid_src_ready = 0;
+ regs.besctl &= ~1;
+ mga_vid_write_regs();
+ mga_vid_in_use = 0;
+
+ //FIXME put back in!
+ //MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+static long long mga_vid_lseek(struct file *file, long long offset, int origin)
+{
+ return -ESPIPE;
+}
+
+static int mga_vid_open(struct inode *inode, struct file *file)
+{
+ int minor = MINOR(inode->i_rdev);
+
+ if(minor != 0)
+ return(-ENXIO);
+
+ if(mga_vid_in_use == 1)
+ return(-EBUSY);
+
+ mga_vid_in_use = 1;
+ //FIXME turn me back on!
+ //MOD_INC_USE_COUNT;
+ return(0);
+}
+
+#if LINUX_VERSION_CODE >= 0x020400
+static struct file_operations mga_vid_fops =
+{
+ llseek: mga_vid_lseek,
+ read: mga_vid_read,
+ write: mga_vid_write,
+ ioctl: mga_vid_ioctl,
+ mmap: mga_vid_mmap,
+ open: mga_vid_open,
+ release: mga_vid_release
+};
+#else
+static struct file_operations mga_vid_fops =
+{
+ mga_vid_lseek,
+ mga_vid_read,
+ mga_vid_write,
+ NULL,
+ NULL,
+ mga_vid_ioctl,
+ mga_vid_mmap,
+ mga_vid_open,
+ NULL,
+ mga_vid_release
+};
+#endif
+
+static long mga_v4l_read(struct video_device *v, char *buf, unsigned long count,
+ int noblock)
+{
+ return -EINVAL;
+}
+
+static long mga_v4l_write(struct video_device *v, const char *buf, unsigned long count, int noblock)
+{
+ return -EINVAL;
+}
+
+static int mga_v4l_open(struct video_device *dev, int mode)
+{
+ MOD_INC_USE_COUNT;
+ return 0;
+}
+
+static void mga_v4l_close(struct video_device *dev)
+{
+ regs.besctl &= ~1;
+ mga_vid_write_regs();
+ vid_overlay_on = 0;
+ MOD_DEC_USE_COUNT;
+ return;
+}
+
+static int mga_v4l_init_done(struct video_device *dev)
+{
+ return 0;
+}
+
+static int mga_v4l_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
+{
+ switch(cmd)
+ {
+ case VIDIOCGCAP:
+ {
+ struct video_capability b;
+ strcpy(b.name, "Matrox G200/400");
+ b.type = VID_TYPE_SCALES|VID_TYPE_OVERLAY|VID_TYPE_CHROMAKEY;
+ b.channels = 0;
+ b.audios = 0;
+ b.maxwidth = 1024; /* GUESS ?? */
+ b.maxheight = 768;
+ b.minwidth = 32;
+ b.minheight = 16; /* GUESS ?? */
+ if(copy_to_user(arg,&b,sizeof(b)))
+ return -EFAULT;
+ return 0;
+ }
+ case VIDIOCGPICT:
+ {
+ /*
+ * Default values.. if we can change this we
+ * can add the feature later
+ */
+ struct video_picture vp;
+ vp.brightness = 0x8000;
+ vp.hue = 0x8000;
+ vp.colour = 0x8000;
+ vp.whiteness = 0x8000;
+ vp.depth = 8;
+ /* Format is a guess */
+ vp.palette = VIDEO_PALETTE_YUV420P;
+ if(copy_to_user(arg, &vp, sizeof(vp)))
+ return -EFAULT;
+ return 0;
+ }
+ case VIDIOCSPICT:
+ {
+ return -EINVAL;
+ }
+ case VIDIOCSWIN:
+ {
+ struct video_window vw;
+ if(copy_from_user(&vw, arg, sizeof(vw)))
+ return -EFAULT;
+ if(vw.x <0 || vw.y <0 || vw.width < 32
+ || vw.height < 16)
+ return -EINVAL;
+ memcpy(&mga_win, &vw, sizeof(mga_win));
+
+ mga_config.x_org = vw.x;
+ mga_config.y_org = vw.y;
+ mga_config.dest_width = vw.width;
+ mga_config.dest_height = vw.height;
+
+ /*
+ * May have to add
+ *
+ * #define VIDEO_WINDOW_CHROMAKEY 16
+ *
+ * to <linux/videodev.h>
+ */
+
+ //add it here for now
+ #define VIDEO_WINDOW_CHROMAKEY 16
+
+ if (vw.flags & VIDEO_WINDOW_CHROMAKEY)
+ mga_config.colkey_on = 1;
+ else
+ mga_config.colkey_on = 0;
+
+ mga_config.colkey_red = (vw.chromakey >> 24) & 0xFF;
+ mga_config.colkey_green = (vw.chromakey >> 16) & 0xFF;
+ mga_config.colkey_blue = (vw.chromakey >> 8) & 0xFF;
+ mga_vid_set_config(&mga_config);
+ return 0;
+
+ }
+ case VIDIOCGWIN:
+ {
+ if(copy_to_user(arg, &mga_win, sizeof(mga_win)))
+ return -EFAULT;
+ return 0;
+ }
+ case VIDIOCCAPTURE:
+ {
+ int v;
+ if(copy_from_user(&v, arg, sizeof(v)))
+ return -EFAULT;
+ vid_overlay_on = v;
+ if(vid_overlay_on && vid_src_ready)
+ {
+ regs.besctl |= 1;
+ mga_vid_write_regs();
+ }
+ else
+ {
+ regs.besctl &= ~1;
+ mga_vid_write_regs();
+ }
+ return 0;
+ }
+ default:
+ return -ENOIOCTLCMD;
+ }
+}
+
+static struct video_device mga_v4l_dev =
+{
+ "Matrox G200/G400",
+ VID_TYPE_CAPTURE,
+ VID_HARDWARE_BT848, /* This is a lie for now */
+ mga_v4l_open,
+ mga_v4l_close,
+ mga_v4l_read,
+ mga_v4l_write,
+ NULL,
+ mga_v4l_ioctl,
+ NULL,
+ mga_v4l_init_done,
+ NULL,
+ 0,
+ 0
+};
+
+
+
+/*
+ * Main Initialization Function
+ */
+
+
+static int mga_vid_initialize(void)
+{
+ mga_vid_in_use = 0;
+
+ printk( "Matrox MGA G200/G400 YUV Video interface v0.01 (c) Aaron Holtzman \n");
+ if(register_chrdev(MGA_VID_MAJOR, "mga_vid", &mga_vid_fops))
+ {
+ printk("mga_vid: unable to get major: %d\n", MGA_VID_MAJOR);
+ return -EIO;
+ }
+
+ if (!mga_vid_find_card())
+ {
+ printk("mga_vid: no supported devices found\n");
+ unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
+ return -EINVAL;
+ }
+
+#if 0
+ if (video_register_device(&mga_v4l_dev, VFL_TYPE_GRABBER)<0)
+ {
+ printk("mga_vid: unable to register.\n");
+ unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
+ if(mga_mmio_base)
+ iounmap(mga_mmio_base);
+ mga_mmio_base = 0;
+ return -EINVAL;
+ }
+#endif
+
+ return(0);
+}
+
+int init_module(void)
+{
+ return mga_vid_initialize();
+}
+
+void cleanup_module(void)
+{
+// video_unregister_device(&mga_v4l_dev);
+ if(mga_mmio_base)
+ iounmap(mga_mmio_base);
+
+ //FIXME turn off BES
+ printk("mga_vid: Cleaning up module\n");
+ unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
+}
+
diff --git a/drivers/mga_vid.h b/drivers/mga_vid.h
new file mode 100644
index 0000000000..5f1a0b540d
--- /dev/null
+++ b/drivers/mga_vid.h
@@ -0,0 +1,48 @@
+/*
+ *
+ * mga_vid.h
+ *
+ * Copyright (C) 1999 Aaron Holtzman
+ *
+ * Matrox MGA G200/G400 YUV Video Interface module Version 0.1.0
+ *
+ * BES == Back End Scaler
+ *
+ * This software has been released under the terms of the GNU Public
+ * license. See http://www.gnu.org/copyleft/gpl.html for details.
+ */
+
+#ifndef __LINUX_MGAVID_H
+#define __LINUX_MGAVID_H
+
+#include <inttypes.h>
+
+typedef struct mga_vid_config_s
+{
+uint32_t card_type;
+uint32_t ram_size;
+uint32_t src_width;
+uint32_t src_height;
+uint32_t dest_width;
+uint32_t dest_height;
+uint32_t x_org;
+uint32_t y_org;
+uint8_t colkey_on;
+uint8_t colkey_red;
+uint8_t colkey_green;
+uint8_t colkey_blue;
+uint32_t format;
+} mga_vid_config_t;
+
+#define MGA_VID_FORMAT_YV12 0x32315659
+#define MGA_VID_FORMAT_YUY2 (('Y'<<24)|('U'<<16)|('Y'<<8)|'2')
+
+#define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
+#define MGA_VID_ON _IO ('J', 2)
+#define MGA_VID_OFF _IO ('J', 3)
+#define MGA_VID_FSEL _IOR('J', 4, int)
+
+#define MGA_G200 0x1234
+#define MGA_G400 0x5678
+
+#endif
diff --git a/drivers/mga_vid_test.c b/drivers/mga_vid_test.c
new file mode 100644
index 0000000000..c1057a9cf7
--- /dev/null
+++ b/drivers/mga_vid_test.c
@@ -0,0 +1,221 @@
+/*
+ *
+ * mga_vid_test.c
+ *
+ * Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
+ * Sept 1999
+ *
+ * This software has been released under the terms of the GNU Public
+ * license. See http://www.gnu.org/copyleft/gpl.html for details.
+ */
+
+//#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include "mga_vid.h"
+
+mga_vid_config_t config;
+uint8_t *mga_vid_base;
+uint32_t is_g400;
+
+#define SRC_IMAGE_WIDTH 256
+#define SRC_IMAGE_HEIGHT 256
+
+uint8_t y_image[SRC_IMAGE_WIDTH * SRC_IMAGE_HEIGHT];
+uint8_t cr_image[SRC_IMAGE_WIDTH * SRC_IMAGE_HEIGHT];
+uint8_t cb_image[SRC_IMAGE_WIDTH * SRC_IMAGE_HEIGHT];
+
+
+void
+write_frame_g200(uint8_t *y,uint8_t *cr, uint8_t *cb)
+{
+ uint8_t *dest;
+ uint32_t bespitch,h,w;
+
+ dest = mga_vid_base;
+ bespitch = (config.src_width + 31) & ~31;
+
+ for(h=0; h < config.src_height; h++)
+ {
+ memcpy(dest, y, config.src_width);
+ y += config.src_width;
+ dest += bespitch;
+ }
+
+ for(h=0; h < config.src_height/2; h++)
+ {
+ for(w=0; w < config.src_width/2; w++)
+ {
+ *dest++ = *cb++;
+ *dest++ = *cr++;
+ }
+ dest += bespitch - config.src_width;
+ }
+}
+
+void
+write_frame_g400(uint8_t *y,uint8_t *cr, uint8_t *cb)
+{
+ uint8_t *dest;
+ uint32_t bespitch,h;
+
+ dest = mga_vid_base;
+ bespitch = (config.src_width + 31) & ~31;
+
+ for(h=0; h < config.src_height; h++)
+ {
+ memcpy(dest, y, config.src_width);
+ y += config.src_width;
+ dest += bespitch;
+ }
+
+ for(h=0; h < config.src_height/2; h++)
+ {
+ memcpy(dest, cb, config.src_width/2);
+ cb += config.src_width/2;
+ dest += bespitch/2;
+ }
+
+ for(h=0; h < config.src_height/2; h++)
+ {
+ memcpy(dest, cr, config.src_width/2);
+ cr += config.src_width/2;
+ dest += bespitch/2;
+ }
+}
+
+void write_frame(uint8_t *y,uint8_t *cr, uint8_t *cb)
+{
+ if(is_g400)
+ write_frame_g400(y,cr,cb);
+ else
+ write_frame_g200(y,cr,cb);
+}
+
+void
+draw_cool_pattern(void)
+{
+ int i,x,y;
+
+ i = 0;
+ for (y=0; y<config.src_height; y++) {
+ for (x=0; x<config.src_width; x++) {
+ y_image[i++] = x*x/2 + y*y/2 - 128;
+ }
+ }
+
+ i = 0;
+ for (y=0; y<config.src_height/2; y++)
+ for (x=0; x<config.src_width/2; x++)
+ {
+ cr_image[i++] = x - 128;
+ }
+
+ i = 0;
+ for (y=0; y<config.src_height/2; y++)
+ for (x=0; x<config.src_width/2; x++)
+ {
+ cb_image[i++] = y - 128;
+ }
+}
+
+void
+draw_color_blend(void)
+{
+ int i,x,y;
+
+ i = 0;
+ for (y=0; y<config.src_height; y++) {
+ for (x=0; x<config.src_width; x++) {
+ y_image[i++] = 0;
+ }
+ }
+
+ i = 0;
+ for (y=0; y<config.src_height/2; y++)
+ for (x=0; x<config.src_width/2; x++)
+ {
+ cr_image[i++] = x - 128;
+ }
+
+ i = 0;
+ for (y=0; y<config.src_height/2; y++)
+ for (x=0; x<config.src_width/2; x++)
+ {
+ cb_image[i++] = y - 128;
+ }
+}
+
+
+int
+main(int argc, char *argv[])
+{
+ int f;
+
+ f = open("/dev/mga_vid",O_RDWR);
+
+ if(f == -1)
+ {
+ fprintf(stderr,"Couldn't open driver\n");
+ exit(1);
+ }
+
+ config.src_width = SRC_IMAGE_WIDTH;
+ config.src_height= SRC_IMAGE_HEIGHT;
+ config.dest_width = SRC_IMAGE_WIDTH;
+ config.dest_height = SRC_IMAGE_HEIGHT;
+ config.x_org= 10;
+ config.y_org= 10;
+ config.colkey_on = 0;
+ config.format = MGA_VID_FORMAT_YV12;
+
+ if (ioctl(f,MGA_VID_CONFIG,&config))
+ {
+ perror("Error in config ioctl");
+ }
+
+ if (config.card_type == MGA_G200)
+ {
+ printf("Testing MGA G200 Backend Scaler with %d MB of RAM\n", config.ram_size);
+ is_g400 = 0;
+ }
+ else
+ {
+ printf("Testing MGA G400 Backend Scaler with %d MB of RAM\n", config.ram_size);
+ is_g400 = 1;
+ }
+
+ ioctl(f,MGA_VID_ON,0);
+ mga_vid_base = (uint8_t*)mmap(0,256 * 4096,PROT_WRITE,MAP_SHARED,f,0);
+ printf("mga_vid_base = %8p\n",mga_vid_base);
+
+
+ //memset(y_image,80,256 * 128);
+ //memset(cr_image,80,256/2 * 20);
+ //memset(cb_image,80,256/2 * 20);
+ write_frame(y_image,cr_image,cb_image);
+ printf("(1) There should be a green square, offset by 10 pixels from\n"
+ " the upper left corner displayed\n");
+ sleep(3);
+
+
+ draw_cool_pattern();
+ write_frame(y_image,cr_image,cb_image);
+ printf("(2) There should be a cool mosaic like pattern now.\n");
+ sleep(3);
+
+ draw_color_blend();
+ write_frame(y_image,cr_image,cb_image);
+ printf("(3) There should be a color blend with black, red, purple, blue\n"
+ " corners (starting top left going CW)\n");
+ sleep(3);
+
+ ioctl(f,MGA_VID_OFF,0);
+
+ close(f);
+ return 0;
+}
diff --git a/drivers/sis_vid.c b/drivers/sis_vid.c
new file mode 100644
index 0000000000..5b9eace81a
--- /dev/null
+++ b/drivers/sis_vid.c
@@ -0,0 +1,645 @@
+/*
+ *
+ * sis_vid.c
+ *
+ * Copyright (C) 2000 Aaron Holtzman
+ *
+ * This software has been released under the terms of the GNU Public
+ * license. See http://www.gnu.org/copyleft/gpl.html for details.
+ */
+
+// video4linux interface disabled by A'rpi/ESP-team
+
+
+//It's entirely possible this major conflicts with something else
+/* mknod /dev/mga_vid c 178 0 */
+
+#include <linux/config.h>
+#include <linux/version.h>
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/mm.h>
+#include <linux/string.h>
+#include <linux/errno.h>
+#include <linux/malloc.h>
+#include <linux/pci.h>
+#include <linux/init.h>
+//#include <linux/videodev.h>
+
+#include "sis_vid.h"
+
+#ifdef CONFIG_MTRR
+#include <asm/mtrr.h>
+#endif
+
+#include <asm/uaccess.h>
+#include <asm/system.h>
+#include <asm/io.h>
+
+#define TRUE 1
+#define FALSE 0
+
+#define MGA_VID_MAJOR 178
+
+
+#ifndef PCI_DEVICE_ID_SI_6323
+#define PCI_DEVICE_ID_SI_6323 0x6326
+#endif
+
+
+MODULE_AUTHOR("Aaron Holtzman <aholtzma@engr.uvic.ca>");
+
+
+typedef struct bes_registers_s
+{
+ //base address of yuv framebuffer
+ uint32_t yuv_base;
+ uint32_t u_base;
+ uint32_t v_base;
+ uint32_t fb_end;;
+
+ //frame buffer pitch
+ uint32_t pitch;
+
+ //window boundaries
+ uint32_t left;
+ uint32_t right;
+ uint32_t top;
+ uint32_t bottom;
+
+ //control registers
+ uint32_t misc_0;
+ uint32_t misc_1;
+ uint32_t misc_3;
+ uint32_t misc_4;
+
+ //key overlay mode
+ uint32_t key_mode;
+
+} bes_registers_t;
+
+static bes_registers_t regs;
+static uint32_t mga_vid_in_use = 0;
+static uint32_t vid_src_ready = 0;
+static uint32_t vid_overlay_on = 0;
+
+static uint8_t *mga_mmio_base = 0;
+static uint32_t mga_mem_base = 0;
+static uint32_t mga_src_base = 0;
+
+static uint32_t mga_ram_size = 0;
+
+static struct pci_dev *pci_dev;
+
+//static struct video_window mga_win;
+static mga_vid_config_t mga_config;
+
+
+
+// Backend Scaler registers
+
+#define MISC_0 0x98
+#define MISC_1 0x99
+#define MISC_3 0x9d
+#define MISC_4 0xb6
+
+
+
+
+static void mga_vid_frame_sel(int frame)
+{
+ //we don't need the vcount protection as we're only hitting
+ //one register (and it doesn't seem to be double buffered)
+ //regs.besctl = (regs.besctl & ~0x07000000) + (frame << 25);
+ //writel( regs.besctl, mga_mmio_base + BESCTL );
+}
+
+
+#define WRITE_REG(x,y,z) {outb((y),(x));outb((z),(x+1));}
+#define READ_REG(x,y) (outb((y),(x)),inb(x+1))
+#define VIDEO_ACCEL 0x3d4
+
+static void mga_vid_write_regs(void)
+{
+ uint32_t foo;
+
+ //unlock the video accel registers
+ WRITE_REG(VIDEO_ACCEL,0x80,0x86);
+ foo = READ_REG(VIDEO_ACCEL,0x80);
+
+ if(foo != 0xa1)
+ return; //something bad happened
+
+ //setup the horizontal window bounds
+ WRITE_REG(VIDEO_ACCEL,0x81,regs.left & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x82,regs.right & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x83,(regs.left >> 8) | ((regs.right>>4) & 0x70));
+
+ //setup the vertical window bounds
+ WRITE_REG(VIDEO_ACCEL,0x84,regs.top & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x85,regs.bottom & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x86,(regs.top >> 8) | ((regs.bottom>>4) & 0x70));
+
+ //setup the framebuffer base addresses
+ WRITE_REG(VIDEO_ACCEL,0x8a,regs.yuv_base & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x8b,(regs.yuv_base >> 8) & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x89,(regs.yuv_base >> 12) & 0xf0);
+
+ WRITE_REG(VIDEO_ACCEL,0xb7,regs.u_base & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0xb8,(regs.u_base >> 8) & 0xff);
+
+ WRITE_REG(VIDEO_ACCEL,0xba,regs.v_base & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0xbb,(regs.v_base >> 8) & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0xb9,((regs.v_base >> 12) & 0xf0) + ((regs.u_base >> 16) & 0xf));
+
+ WRITE_REG(VIDEO_ACCEL,0x8d,regs.fb_end);
+
+ //setup framebuffer pitch
+ WRITE_REG(VIDEO_ACCEL,0x8c,regs.pitch & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0x8e,(regs.pitch >> 8) & 0x0f);
+ WRITE_REG(VIDEO_ACCEL,0xbc,(regs.pitch) & 0xff);
+ WRITE_REG(VIDEO_ACCEL,0xbd,((regs.pitch) >> 8) & 0x0f);
+
+
+ //write key overlay register
+ WRITE_REG(VIDEO_ACCEL,0xa9,regs.key_mode);
+
+ WRITE_REG(VIDEO_ACCEL,0x93,0x40);
+ WRITE_REG(VIDEO_ACCEL,0x94,1);
+ WRITE_REG(VIDEO_ACCEL,0x9e,0);
+ WRITE_REG(VIDEO_ACCEL,0x9f,0);
+
+ //write config registers
+ WRITE_REG(VIDEO_ACCEL,MISC_0,regs.misc_0);
+ WRITE_REG(VIDEO_ACCEL,MISC_1,regs.misc_1);
+ WRITE_REG(VIDEO_ACCEL,MISC_3,regs.misc_3);
+ WRITE_REG(VIDEO_ACCEL,MISC_4,regs.misc_4);
+
+ //setup the video line buffer
+ WRITE_REG(VIDEO_ACCEL,0xa0,(regs.right - regs.left)/ 8);
+
+}
+
+static int mga_vid_set_config(mga_vid_config_t *config)
+{
+ uint32_t x, y, frame_size;
+
+ x = config->x_org;
+ y = config->y_org;
+
+ regs.left = x;
+ regs.right= config->src_width + x;
+ regs.top = y;
+ regs.bottom = config->src_height + y;
+
+ printk("mga_vid: Setting up a %dx%d+%d+%d video window (src %dx%d)\n",
+ config->dest_width, config->dest_height, config->x_org, config->y_org,
+ config->src_width, config->src_height);
+
+
+ regs.pitch = ((config->src_width + 31) & ~31) / 4 ;
+
+ //frame size in pixels
+ frame_size = regs.pitch * config->src_height * 4;
+
+ regs.yuv_base = (mga_src_base) >> 2;
+ regs.u_base = (mga_src_base + frame_size) >> 2;
+ regs.v_base = (mga_src_base + frame_size/4) >> 2;
+ regs.fb_end = (mga_src_base + (3*frame_size)/2) >> 14;
+
+ //disable video capture, enable video display, enable graphics display,
+ //select yuv format, odd parity
+ regs.misc_0 = (1 << 1) + (1<<6) + (1<<4);
+
+ //disable dithering, no filtering, no interrupts
+ regs.misc_1 = 0;
+
+ //select 2's complement format YUV for playback
+ regs.misc_3 = (1<<1);
+
+ //select 4:2:0 video format, + yuv4:2:2 cpu writes
+ regs.misc_4 = (1<<2);
+
+ //disable keying
+ regs.key_mode = 0xf;
+
+ mga_vid_write_regs();
+ return 0;
+}
+
+
+static int mga_vid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
+{
+ int frame;
+
+ switch(cmd)
+ {
+ case MGA_VID_CONFIG:
+ //FIXME remove
+ printk("mga_mmio_base = %p\n",mga_mmio_base);
+ printk("mga_mem_base = %08x\n",mga_mem_base);
+ //FIXME remove
+
+ printk("mga_vid: Received configuration\n");
+
+ if(copy_from_user(&mga_config,(mga_vid_config_t*) arg,sizeof(mga_vid_config_t)))
+ {
+ printk("mga_vid: failed copy from userspace\n");
+ return(-EFAULT);
+ }
+
+ mga_config.ram_size = mga_ram_size;
+ //XXX make it look like a g400
+ mga_config.card_type = MGA_G400;;
+
+ if (copy_to_user((mga_vid_config_t *) arg, &mga_config, sizeof(mga_vid_config_t)))
+ {
+ printk("mga_vid: failed copy to userspace\n");
+ return(-EFAULT);
+ }
+ return mga_vid_set_config(&mga_config);
+ break;
+
+ case MGA_VID_ON:
+ printk("mga_vid: Video ON\n");
+ vid_src_ready = 1;
+ if(vid_overlay_on)
+ {
+ //regs.besctl |= 1;
+ mga_vid_write_regs();
+ }
+ break;
+
+ case MGA_VID_OFF:
+ printk("mga_vid: Video OFF\n");
+ vid_src_ready = 0;
+ //regs.besctl &= ~1;
+ mga_vid_write_regs();
+ break;
+
+ case MGA_VID_FSEL:
+ if(copy_from_user(&frame,(int *) arg,sizeof(int)))
+ {
+ printk("mga_vid: FSEL failed copy from userspace\n");
+ return(-EFAULT);
+ }
+
+ mga_vid_frame_sel(frame);
+ break;
+
+ default:
+ printk("mga_vid: Invalid ioctl\n");
+ return (-EINVAL);
+ }
+
+ return 0;
+}
+
+
+static int mga_vid_find_card(void)
+{
+ struct pci_dev *dev = NULL;
+
+ if((dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_6323, NULL)))
+ {
+ printk("sis_vid: Found SiS 6326\n");
+ }
+ else
+ {
+ printk("sis_vid: No supported cards found\n");
+ return FALSE;
+ }
+
+ pci_dev = dev;
+
+#if LINUX_VERSION_CODE >= 0x020300
+ mga_mmio_base = ioremap_nocache(dev->resource[1].start,0x10000);
+ mga_mem_base = dev->resource[0].start;
+#else
+ mga_mmio_base = ioremap_nocache(dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK,0x10000);
+ mga_mem_base = dev->base_address[0] & PCI_BASE_ADDRESS_MEM_MASK;
+#endif
+ printk("mga_vid: MMIO at 0x%p\n", mga_mmio_base);
+ printk("mga_vid: Frame Buffer at 0x%08x\n", mga_mem_base);
+
+ //FIXME set ram size properly
+ mga_ram_size = 4;
+ mga_src_base = (mga_ram_size - 1) * 0x100000;
+
+ //FIXME remove
+ if(1)
+ {
+ mga_vid_config_t config ={0,0,256,256,256,256,10,10,0,0,0,0};
+
+ mga_vid_set_config(&config);
+ mga_vid_write_regs();
+ //regs.misc_0 ^= 2;
+ //mga_vid_write_regs();
+ }
+ //FIXME remove
+
+ return TRUE;
+}
+
+
+static ssize_t mga_vid_read(struct file *file, char *buf, size_t count, loff_t *ppos)
+{
+ return -EINVAL;
+}
+
+static ssize_t mga_vid_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
+{
+ return -EINVAL;
+}
+
+static int mga_vid_mmap(struct file *file, struct vm_area_struct *vma)
+{
+
+ printk("mga_vid: mapping video memory into userspace\n");
+ if(remap_page_range(vma->vm_start, mga_mem_base + mga_src_base,
+ vma->vm_end - vma->vm_start, vma->vm_page_prot))
+ {
+ printk("mga_vid: error mapping video memory\n");
+ return(-EAGAIN);
+ }
+
+ return(0);
+}
+
+static int mga_vid_release(struct inode *inode, struct file *file)
+{
+ //Close the window just in case
+ vid_src_ready = 0;
+ regs.misc_0 &= 0xed;
+ mga_vid_write_regs();
+ mga_vid_in_use = 0;
+
+ //FIXME put back in!
+ //MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+static long long mga_vid_lseek(struct file *file, long long offset, int origin)
+{
+ return -ESPIPE;
+}
+
+static int mga_vid_open(struct inode *inode, struct file *file)
+{
+ int minor = MINOR(inode->i_rdev);
+
+ if(minor != 0)
+ return(-ENXIO);
+
+ if(mga_vid_in_use == 1)
+ return(-EBUSY);
+
+ mga_vid_in_use = 1;
+ //FIXME turn me back on!
+ //MOD_INC_USE_COUNT;
+ return(0);
+}
+
+#if LINUX_VERSION_CODE >= 0x020400
+static struct file_operations mga_vid_fops =
+{
+ llseek: mga_vid_lseek,
+ read: mga_vid_read,
+ write: mga_vid_write,
+ ioctl: mga_vid_ioctl,
+ mmap: mga_vid_mmap,
+ open: mga_vid_open,
+ release: mga_vid_release
+};
+#else
+static struct file_operations mga_vid_fops =
+{
+ mga_vid_lseek,
+ mga_vid_read,
+ mga_vid_write,
+ NULL,
+ NULL,
+ mga_vid_ioctl,
+ mga_vid_mmap,
+ mga_vid_open,
+ NULL,
+ mga_vid_release
+};
+#endif
+
+#if 0
+static long mga_v4l_read(struct video_device *v, char *buf, unsigned long count,
+ int noblock)
+{
+ return -EINVAL;
+}
+
+static long mga_v4l_write(struct video_device *v, const char *buf, unsigned long count, int noblock)
+{
+ return -EINVAL;
+}
+
+static int mga_v4l_open(struct video_device *dev, int mode)
+{
+ MOD_INC_USE_COUNT;
+ return 0;
+}
+
+static void mga_v4l_close(struct video_device *dev)
+{
+ //regs.besctl &= ~1;
+ mga_vid_write_regs();
+ vid_overlay_on = 0;
+ MOD_DEC_USE_COUNT;
+ return;
+}
+
+static int mga_v4l_init_done(struct video_device *dev)
+{
+ return 0;
+}
+
+static int mga_v4l_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
+{
+ switch(cmd)
+ {
+ case VIDIOCGCAP:
+ {
+ struct video_capability b;
+ strcpy(b.name, "Matrox G200/400");
+ b.type = VID_TYPE_SCALES|VID_TYPE_OVERLAY|VID_TYPE_CHROMAKEY;
+ b.channels = 0;
+ b.audios = 0;
+ b.maxwidth = 1024; /* GUESS ?? */
+ b.maxheight = 768;
+ b.minwidth = 32;
+ b.minheight = 16; /* GUESS ?? */
+ if(copy_to_user(arg,&b,sizeof(b)))
+ return -EFAULT;
+ return 0;
+ }
+ case VIDIOCGPICT:
+ {
+ /*
+ * Default values.. if we can change this we
+ * can add the feature later
+ */
+ struct video_picture vp;
+ vp.brightness = 0x8000;
+ vp.hue = 0x8000;
+ vp.colour = 0x8000;
+ vp.whiteness = 0x8000;
+ vp.depth = 8;
+ /* Format is a guess */
+ vp.palette = VIDEO_PALETTE_YUV420P;
+ if(copy_to_user(arg, &vp, sizeof(vp)))
+ return -EFAULT;
+ return 0;
+ }
+ case VIDIOCSPICT:
+ {
+ return -EINVAL;
+ }
+ case VIDIOCSWIN:
+ {
+ struct video_window vw;
+ if(copy_from_user(&vw, arg, sizeof(vw)))
+ return -EFAULT;
+ if(vw.x <0 || vw.y <0 || vw.width < 32
+ || vw.height < 16)
+ return -EINVAL;
+ memcpy(&mga_win, &vw, sizeof(mga_win));
+
+ mga_config.x_org = vw.x;
+ mga_config.y_org = vw.y;
+ mga_config.dest_width = vw.width;
+ mga_config.dest_height = vw.height;
+
+ /*
+ * May have to add
+ *
+ * #define VIDEO_WINDOW_CHROMAKEY 16
+ *
+ * to <linux/videodev.h>
+ */
+
+ //add it here for now
+ #define VIDEO_WINDOW_CHROMAKEY 16
+
+ if (vw.flags & VIDEO_WINDOW_CHROMAKEY)
+ mga_config.colkey_on = 1;
+ else
+ mga_config.colkey_on = 0;
+
+ mga_config.colkey_red = (vw.chromakey >> 24) & 0xFF;
+ mga_config.colkey_green = (vw.chromakey >> 16) & 0xFF;
+ mga_config.colkey_blue = (vw.chromakey >> 8) & 0xFF;
+ mga_vid_set_config(&mga_config);
+ return 0;
+
+ }
+ case VIDIOCGWIN:
+ {
+ if(copy_to_user(arg, &mga_win, sizeof(mga_win)))
+ return -EFAULT;
+ return 0;
+ }
+ case VIDIOCCAPTURE:
+ {
+ int v;
+ if(copy_from_user(&v, arg, sizeof(v)))
+ return -EFAULT;
+ vid_overlay_on = v;
+ if(vid_overlay_on && vid_src_ready)
+ {
+ //regs.besctl |= 1;
+ mga_vid_write_regs();
+ }
+ else
+ {
+ //regs.besctl &= ~1;
+ mga_vid_write_regs();
+ }
+ return 0;
+ }
+ default:
+ return -ENOIOCTLCMD;
+ }
+}
+
+static struct video_device mga_v4l_dev =
+{
+ "Matrox G200/G400",
+ VID_TYPE_CAPTURE,
+ VID_HARDWARE_BT848, /* This is a lie for now */
+ mga_v4l_open,
+ mga_v4l_close,
+ mga_v4l_read,
+ mga_v4l_write,
+ NULL,
+ mga_v4l_ioctl,
+ NULL,
+ mga_v4l_init_done,
+ NULL,
+ 0,
+ 0
+};
+
+#endif
+
+/*
+ * Main Initialization Function
+ */
+
+
+static int mga_vid_initialize(void)
+{
+ mga_vid_in_use = 0;
+
+ printk( "SiS 6326 YUV Video interface v0.01 (c) Aaron Holtzman \n");
+ if(register_chrdev(MGA_VID_MAJOR, "mga_vid", &mga_vid_fops))
+ {
+ printk("sis_vid: unable to get major: %d\n", MGA_VID_MAJOR);
+ return -EIO;
+ }
+
+ if (!mga_vid_find_card())
+ {
+ printk("sis_vid: no supported devices found\n");
+ unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
+ return -EINVAL;
+ }
+
+#if 0
+ if (video_register_device(&mga_v4l_dev, VFL_TYPE_GRABBER)<0)
+ {
+ printk("sis_vid: unable to register.\n");
+ unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
+ if(mga_mmio_base)
+ iounmap(mga_mmio_base);
+ mga_mmio_base = 0;
+ return -EINVAL;
+ }
+#endif
+
+ return(0);
+}
+
+int init_module(void)
+{
+ return mga_vid_initialize();
+}
+
+void cleanup_module(void)
+{
+// video_unregister_device(&mga_v4l_dev);
+ if(mga_mmio_base)
+ iounmap(mga_mmio_base);
+
+ //FIXME turn off BES
+ printk("mga_vid: Cleaning up module\n");
+ unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
+}
+
diff --git a/drivers/sis_vid.h b/drivers/sis_vid.h
new file mode 100644
index 0000000000..a4c42cf901
--- /dev/null
+++ b/drivers/sis_vid.h
@@ -0,0 +1,37 @@
+/*
+ *
+ * sis_vid.h
+ *
+ * Copyright (C) 2000 Aaron Holtzman
+ *
+ * YUV Framebuffer driver for SiS 6326 cards
+ *
+ * This software has been released under the terms of the GNU Public
+ * license. See http://www.gnu.org/copyleft/gpl.html for details.
+ */
+
+#include <inttypes.h>
+
+typedef struct mga_vid_config_s
+{
+uint32_t card_type;
+uint32_t ram_size;
+uint32_t src_width;
+uint32_t src_height;
+uint32_t dest_width;
+uint32_t dest_height;
+uint32_t x_org;
+uint32_t y_org;
+uint8_t colkey_on;
+uint8_t colkey_red;
+uint8_t colkey_green;
+uint8_t colkey_blue;
+} mga_vid_config_t;
+
+#define MGA_VID_CONFIG _IOR('J', 1, mga_vid_config_t)
+#define MGA_VID_ON _IO ('J', 2)
+#define MGA_VID_OFF _IO ('J', 3)
+#define MGA_VID_FSEL _IOR('J', 4, int)
+
+#define MGA_G200 0x1234
+#define MGA_G400 0x5678