Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[safe/jmp/linux-2.6] / drivers / video / via / viafbdev.c
index a17e138..1082541 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 1998-2008 VIA Technologies, Inc. All Rights Reserved.
+ * Copyright 1998-2009 VIA Technologies, Inc. All Rights Reserved.
  * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
 
  * This program is free software; you can redistribute it and/or
  */
 
 #include <linux/module.h>
-#define _MASTER_FILE
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/stat.h>
+#include <linux/via-core.h>
 
+#define _MASTER_FILE
 #include "global.h"
 
-static int MAX_CURS = 32;
-static struct fb_var_screeninfo default_var;
 static char *viafb_name = "Via";
 static u32 pseudo_pal[17];
 
 /* video mode */
-static char *viafb_mode = "640x480";
-static char *viafb_mode1 = "640x480";
+static char *viafb_mode;
+static char *viafb_mode1;
+static int viafb_bpp = 32;
+static int viafb_bpp1 = 32;
+
+static unsigned int viafb_second_offset;
+static int viafb_second_size;
+
+static int viafb_accel = 1;
 
 /* Added for specifying active devices.*/
-char *viafb_active_dev = "";
+char *viafb_active_dev;
 
 /*Added for specify lcd output port*/
 char *viafb_lcd_port = "";
@@ -47,18 +56,78 @@ static void apply_second_mode_setting(struct fb_var_screeninfo
        *sec_var);
 static void retrieve_device_setting(struct viafb_ioctl_setting
        *setting_info);
+static int viafb_pan_display(struct fb_var_screeninfo *var,
+       struct fb_info *info);
 
 static struct fb_ops viafb_ops;
 
 
+static void viafb_fill_var_color_info(struct fb_var_screeninfo *var, u8 depth)
+{
+       var->grayscale = 0;
+       var->red.msb_right = 0;
+       var->green.msb_right = 0;
+       var->blue.msb_right = 0;
+       var->transp.offset = 0;
+       var->transp.length = 0;
+       var->transp.msb_right = 0;
+       var->nonstd = 0;
+       switch (depth) {
+       case 8:
+               var->bits_per_pixel = 8;
+               var->red.offset = 0;
+               var->green.offset = 0;
+               var->blue.offset = 0;
+               var->red.length = 8;
+               var->green.length = 8;
+               var->blue.length = 8;
+               break;
+       case 15:
+               var->bits_per_pixel = 16;
+               var->red.offset = 10;
+               var->green.offset = 5;
+               var->blue.offset = 0;
+               var->red.length = 5;
+               var->green.length = 5;
+               var->blue.length = 5;
+               break;
+       case 16:
+               var->bits_per_pixel = 16;
+               var->red.offset = 11;
+               var->green.offset = 5;
+               var->blue.offset = 0;
+               var->red.length = 5;
+               var->green.length = 6;
+               var->blue.length = 5;
+               break;
+       case 24:
+               var->bits_per_pixel = 32;
+               var->red.offset = 16;
+               var->green.offset = 8;
+               var->blue.offset = 0;
+               var->red.length = 8;
+               var->green.length = 8;
+               var->blue.length = 8;
+               break;
+       case 30:
+               var->bits_per_pixel = 32;
+               var->red.offset = 20;
+               var->green.offset = 10;
+               var->blue.offset = 0;
+               var->red.length = 10;
+               var->green.length = 10;
+               var->blue.length = 10;
+               break;
+       }
+}
+
 static void viafb_update_fix(struct fb_info *info)
 {
        u32 bpp = info->var.bits_per_pixel;
 
        info->fix.visual =
                bpp == 8 ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
-       info->fix.line_length =
-               ((info->var.xres_virtual + 7) & ~7) * bpp / 8;
+       info->fix.line_length = (info->var.xres_virtual * bpp / 8 + 7) & ~7;
 }
 
 static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
@@ -72,6 +141,7 @@ static void viafb_setup_fixinfo(struct fb_fix_screeninfo *fix,
 
        fix->type = FB_TYPE_PACKED_PIXELS;
        fix->type_aux = 0;
+       fix->visual = FB_VISUAL_TRUECOLOR;
 
        fix->xpanstep = fix->ywrapstep = 0;
        fix->ypanstep = 1;
@@ -94,12 +164,10 @@ static int viafb_release(struct fb_info *info, int user)
 static int viafb_check_var(struct fb_var_screeninfo *var,
        struct fb_info *info)
 {
-       int vmode_index, htotal, vtotal;
-       struct viafb_par *ppar;
-       u32 long_refresh;
-       struct viafb_par *p_viafb_par;
-       ppar = info->par;
-
+       int htotal, vtotal, depth;
+       struct VideoModeTable *vmode_entry;
+       struct viafb_par *ppar = info->par;
+       u32 long_refresh, line;
 
        DEBUG_MSG(KERN_INFO "viafb_check_var!\n");
        /* Sanity check */
@@ -107,26 +175,36 @@ static int viafb_check_var(struct fb_var_screeninfo *var,
        if (var->vmode & FB_VMODE_INTERLACED || var->vmode & FB_VMODE_DOUBLE)
                return -EINVAL;
 
-       vmode_index = viafb_get_mode_index(var->xres, var->yres);
-       if (vmode_index == VIA_RES_INVALID) {
+       vmode_entry = viafb_get_mode(var->xres, var->yres);
+       if (!vmode_entry) {
                DEBUG_MSG(KERN_INFO
                          "viafb: Mode %dx%dx%d not supported!!\n",
                          var->xres, var->yres, var->bits_per_pixel);
                return -EINVAL;
        }
 
-       if (24 == var->bits_per_pixel)
-               var->bits_per_pixel = 32;
+       depth = fb_get_color_depth(var, &info->fix);
+       if (!depth)
+               depth = var->bits_per_pixel;
 
-       if (var->bits_per_pixel != 8 && var->bits_per_pixel != 16 &&
-               var->bits_per_pixel != 32)
+       if (depth < 0 || depth > 32)
                return -EINVAL;
+       else if (!depth)
+               depth = 24;
+       else if (depth == 15 && viafb_dual_fb && ppar->iga_path == IGA1)
+               depth = 15;
+       else if (depth == 30)
+               depth = 30;
+       else if (depth <= 8)
+               depth = 8;
+       else if (depth <= 16)
+               depth = 16;
+       else
+               depth = 24;
 
-       if ((var->xres_virtual * (var->bits_per_pixel >> 3)) & 0x1F)
-               /*32 pixel alignment */
-               var->xres_virtual = (var->xres_virtual + 31) & ~31;
-       if (var->xres_virtual * var->yres_virtual * var->bits_per_pixel / 8 >
-               ppar->memsize)
+       viafb_fill_var_color_info(var, depth);
+       line = (var->xres_virtual * var->bits_per_pixel / 8 + 7) & ~7;
+       if (line * var->yres_virtual > ppar->memsize)
                return -EINVAL;
 
        /* Based on var passed in to calculate the refresh,
@@ -142,57 +220,56 @@ static int viafb_check_var(struct fb_var_screeninfo *var,
        viafb_refresh = viafb_get_refresh(var->xres, var->yres, long_refresh);
 
        /* Adjust var according to our driver's own table */
-       viafb_fill_var_timing_info(var, viafb_refresh, vmode_index);
-
-       /* This is indeed a patch for VT3353 */
-       if (!info->par)
-               return -1;
-       p_viafb_par = (struct viafb_par *)info->par;
-       if (p_viafb_par->chip_info->gfx_chip_name == UNICHROME_VX800)
-               var->accel_flags = 0;
+       viafb_fill_var_timing_info(var, viafb_refresh, vmode_entry);
+       if (info->var.accel_flags & FB_ACCELF_TEXT &&
+               !ppar->shared->vdev->engine_mmio)
+               info->var.accel_flags = 0;
 
        return 0;
 }
 
 static int viafb_set_par(struct fb_info *info)
 {
-       int vmode_index;
-       int vmode_index1 = 0;
+       struct viafb_par *viapar = info->par;
+       struct VideoModeTable *vmode_entry, *vmode_entry1 = NULL;
        DEBUG_MSG(KERN_INFO "viafb_set_par!\n");
 
-       viafb_update_device_setting(info->var.xres, info->var.yres,
-                             info->var.bits_per_pixel, viafb_refresh, 0);
-
-       vmode_index = viafb_get_mode_index(info->var.xres, info->var.yres);
+       viapar->depth = fb_get_color_depth(&info->var, &info->fix);
+       viafb_update_device_setting(viafbinfo->var.xres, viafbinfo->var.yres,
+               viafbinfo->var.bits_per_pixel, viafb_refresh, 0);
 
-       if (viafb_SAMM_ON == 1) {
+       vmode_entry = viafb_get_mode(viafbinfo->var.xres, viafbinfo->var.yres);
+       if (viafb_dual_fb) {
+               vmode_entry1 = viafb_get_mode(viafbinfo1->var.xres,
+                       viafbinfo1->var.yres);
+               viafb_update_device_setting(viafbinfo1->var.xres,
+                       viafbinfo1->var.yres, viafbinfo1->var.bits_per_pixel,
+                       viafb_refresh1, 1);
+       } else if (viafb_SAMM_ON == 1) {
                DEBUG_MSG(KERN_INFO
                "viafb_second_xres = %d, viafb_second_yres = %d, bpp = %d\n",
                          viafb_second_xres, viafb_second_yres, viafb_bpp1);
-               vmode_index1 = viafb_get_mode_index(viafb_second_xres,
+               vmode_entry1 = viafb_get_mode(viafb_second_xres,
                        viafb_second_yres);
-               DEBUG_MSG(KERN_INFO "->viafb_SAMM_ON: index=%d\n",
-                       vmode_index1);
 
                viafb_update_device_setting(viafb_second_xres,
                        viafb_second_yres, viafb_bpp1, viafb_refresh1, 1);
        }
 
-       if (vmode_index != VIA_RES_INVALID) {
-               viafb_setmode(vmode_index, info->var.xres, info->var.yres,
-                       info->var.bits_per_pixel, vmode_index1,
-                       viafb_second_xres, viafb_second_yres, viafb_bpp1);
-
-               /*We should set memory offset according virtual_x */
-               /*Fix me:put this function into viafb_setmode */
-               viafb_memory_pitch_patch(info);
+       if (vmode_entry) {
                viafb_update_fix(info);
-               viafb_bpp = info->var.bits_per_pixel;
-               /* Update viafb_accel, it is necessary to our 2D accelerate */
-               viafb_accel = info->var.accel_flags;
-
-               if (viafb_accel)
-                       viafb_set_2d_color_depth(info->var.bits_per_pixel);
+               if (viafb_dual_fb && viapar->iga_path == IGA2)
+                       viafb_bpp1 = info->var.bits_per_pixel;
+               else
+                       viafb_bpp = info->var.bits_per_pixel;
+
+               if (info->var.accel_flags & FB_ACCELF_TEXT)
+                       info->flags &= ~FBINFO_HWACCEL_DISABLED;
+               else
+                       info->flags |= FBINFO_HWACCEL_DISABLED;
+               viafb_setmode(vmode_entry, info->var.bits_per_pixel,
+                       vmode_entry1, viafb_bpp1);
+               viafb_pan_display(&info->var, info);
        }
 
        return 0;
@@ -202,234 +279,52 @@ static int viafb_set_par(struct fb_info *info)
 static int viafb_setcolreg(unsigned regno, unsigned red, unsigned green,
 unsigned blue, unsigned transp, struct fb_info *info)
 {
-       u8 sr1a, sr1b, cr67, cr6a, rev = 0, shift = 10;
-       unsigned cmap_entries = (info->var.bits_per_pixel == 8) ? 256 : 16;
-       DEBUG_MSG(KERN_INFO "viafb_setcolreg!\n");
-       if (regno >= cmap_entries)
-               return 1;
-       if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name) {
-               /*
-                * Read PCI bus 0,dev 0,function 0,index 0xF6 to get chip rev.
-                */
-               outl(0x80000000 | (0xf6 & ~3), (unsigned long)0xCF8);
-               rev = (inl((unsigned long)0xCFC) >> ((0xf6 & 3) * 8)) & 0xff;
-       }
-       switch (info->var.bits_per_pixel) {
-       case 8:
-               outb(0x1A, 0x3C4);
-               sr1a = inb(0x3C5);
-               outb(0x1B, 0x3C4);
-               sr1b = inb(0x3C5);
-               outb(0x67, 0x3D4);
-               cr67 = inb(0x3D5);
-               outb(0x6A, 0x3D4);
-               cr6a = inb(0x3D5);
-
-               /* Map the 3C6/7/8/9 to the IGA2 */
-               outb(0x1A, 0x3C4);
-               outb(sr1a | 0x01, 0x3C5);
-               /* Second Display Engine colck always on */
-               outb(0x1B, 0x3C4);
-               outb(sr1b | 0x80, 0x3C5);
-               /* Second Display Color Depth 8 */
-               outb(0x67, 0x3D4);
-               outb(cr67 & 0x3F, 0x3D5);
-               outb(0x6A, 0x3D4);
-               /* Second Display Channel Reset CR6A[6]) */
-               outb(cr6a & 0xBF, 0x3D5);
-               /* Second Display Channel Enable CR6A[7] */
-               outb(cr6a | 0x80, 0x3D5);
-               /* Second Display Channel stop reset) */
-               outb(cr6a | 0x40, 0x3D5);
-
-               /* Bit mask of palette */
-               outb(0xFF, 0x3c6);
-               /* Write one register of IGA2 */
-               outb(regno, 0x3C8);
-               if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name &&
-                       rev >= 15) {
-                       shift = 8;
-                       viafb_write_reg_mask(CR6A, VIACR, BIT5, BIT5);
-                       viafb_write_reg_mask(SR15, VIASR, BIT7, BIT7);
-               } else {
-                       shift = 10;
-                       viafb_write_reg_mask(CR6A, VIACR, 0, BIT5);
-                       viafb_write_reg_mask(SR15, VIASR, 0, BIT7);
-               }
-               outb(red >> shift, 0x3C9);
-               outb(green >> shift, 0x3C9);
-               outb(blue >> shift, 0x3C9);
-
-               /* Map the 3C6/7/8/9 to the IGA1 */
-               outb(0x1A, 0x3C4);
-               outb(sr1a & 0xFE, 0x3C5);
-               /* Bit mask of palette */
-               outb(0xFF, 0x3c6);
-               /* Write one register of IGA1 */
-               outb(regno, 0x3C8);
-               outb(red >> shift, 0x3C9);
-               outb(green >> shift, 0x3C9);
-               outb(blue >> shift, 0x3C9);
-
-               outb(0x1A, 0x3C4);
-               outb(sr1a, 0x3C5);
-               outb(0x1B, 0x3C4);
-               outb(sr1b, 0x3C5);
-               outb(0x67, 0x3D4);
-               outb(cr67, 0x3D5);
-               outb(0x6A, 0x3D4);
-               outb(cr6a, 0x3D5);
-               break;
-       case 16:
-               ((u32 *) info->pseudo_palette)[regno] = (red & 0xF800) |
-                   ((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
-               break;
-       case 32:
-               ((u32 *) info->pseudo_palette)[regno] =
-                   ((transp & 0xFF00) << 16) |
-                   ((red & 0xFF00) << 8) |
-                   ((green & 0xFF00)) | ((blue & 0xFF00) >> 8);
-               break;
-       }
-
-       return 0;
+       struct viafb_par *viapar = info->par;
+       u32 r, g, b;
 
-}
+       if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
+               if (regno > 255)
+                       return -EINVAL;
 
-/*CALLED BY: fb_set_cmap */
-/*           fb_set_var, pass 256 colors */
-/*CALLED BY: fb_set_cmap */
-/*           fbcon_set_palette, pass 16 colors */
-static int viafb_setcmap(struct fb_cmap *cmap, struct fb_info *info)
-{
-       u32 len = cmap->len;
-       u32 i;
-       u16 *pred = cmap->red;
-       u16 *pgreen = cmap->green;
-       u16 *pblue = cmap->blue;
-       u16 *ptransp = cmap->transp;
-       u8 sr1a, sr1b, cr67, cr6a, rev = 0, shift = 10;
-       if (len > 256)
-               return 1;
-       if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name) {
-               /*
-                * Read PCI bus 0, dev 0, function 0, index 0xF6 to get chip
-                * rev.
-                */
-               outl(0x80000000 | (0xf6 & ~3), (unsigned long)0xCF8);
-               rev = (inl((unsigned long)0xCFC) >> ((0xf6 & 3) * 8)) & 0xff;
-       }
-       switch (info->var.bits_per_pixel) {
-       case 8:
-               outb(0x1A, 0x3C4);
-               sr1a = inb(0x3C5);
-               outb(0x1B, 0x3C4);
-               sr1b = inb(0x3C5);
-               outb(0x67, 0x3D4);
-               cr67 = inb(0x3D5);
-               outb(0x6A, 0x3D4);
-               cr6a = inb(0x3D5);
-               /* Map the 3C6/7/8/9 to the IGA2 */
-               outb(0x1A, 0x3C4);
-               outb(sr1a | 0x01, 0x3C5);
-               outb(0x1B, 0x3C4);
-               /* Second Display Engine colck always on */
-               outb(sr1b | 0x80, 0x3C5);
-               outb(0x67, 0x3D4);
-               /* Second Display Color Depth 8 */
-               outb(cr67 & 0x3F, 0x3D5);
-               outb(0x6A, 0x3D4);
-               /* Second Display Channel Reset CR6A[6]) */
-               outb(cr6a & 0xBF, 0x3D5);
-               /* Second Display Channel Enable CR6A[7] */
-               outb(cr6a | 0x80, 0x3D5);
-               /* Second Display Channel stop reset) */
-               outb(cr6a | 0xC0, 0x3D5);
-
-               /* Bit mask of palette */
-               outb(0xFF, 0x3c6);
-               outb(0x00, 0x3C8);
-               if (UNICHROME_CLE266 == viaparinfo->chip_info->gfx_chip_name &&
-                       rev >= 15) {
-                       shift = 8;
-                       viafb_write_reg_mask(CR6A, VIACR, BIT5, BIT5);
-                       viafb_write_reg_mask(SR15, VIASR, BIT7, BIT7);
-               } else {
-                       shift = 10;
-                       viafb_write_reg_mask(CR6A, VIACR, 0, BIT5);
-                       viafb_write_reg_mask(SR15, VIASR, 0, BIT7);
-               }
-               for (i = 0; i < len; i++) {
-                       outb((*(pred + i)) >> shift, 0x3C9);
-                       outb((*(pgreen + i)) >> shift, 0x3C9);
-                       outb((*(pblue + i)) >> shift, 0x3C9);
-               }
+               if (!viafb_dual_fb || viapar->iga_path == IGA1)
+                       viafb_set_primary_color_register(regno, red >> 8,
+                               green >> 8, blue >> 8);
 
-               outb(0x1A, 0x3C4);
-               /* Map the 3C6/7/8/9 to the IGA1 */
-               outb(sr1a & 0xFE, 0x3C5);
-               /* Bit mask of palette */
-               outb(0xFF, 0x3c6);
-               outb(0x00, 0x3C8);
-               for (i = 0; i < len; i++) {
-                       outb((*(pred + i)) >> shift, 0x3C9);
-                       outb((*(pgreen + i)) >> shift, 0x3C9);
-                       outb((*(pblue + i)) >> shift, 0x3C9);
-               }
+               if (!viafb_dual_fb || viapar->iga_path == IGA2)
+                       viafb_set_secondary_color_register(regno, red >> 8,
+                               green >> 8, blue >> 8);
+       } else {
+               if (regno > 15)
+                       return -EINVAL;
 
-               outb(0x1A, 0x3C4);
-               outb(sr1a, 0x3C5);
-               outb(0x1B, 0x3C4);
-               outb(sr1b, 0x3C5);
-               outb(0x67, 0x3D4);
-               outb(cr67, 0x3D5);
-               outb(0x6A, 0x3D4);
-               outb(cr6a, 0x3D5);
-               break;
-       case 16:
-               if (len > 17)
-                       return 0;       /* Because static u32 pseudo_pal[17]; */
-               for (i = 0; i < len; i++)
-                       ((u32 *) info->pseudo_palette)[i] =
-                           (*(pred + i) & 0xF800) |
-                           ((*(pgreen + i) & 0xFC00) >> 5) |
-                           ((*(pblue + i) & 0xF800) >> 11);
-               break;
-       case 32:
-               if (len > 17)
-                       return 0;
-               if (ptransp) {
-                       for (i = 0; i < len; i++)
-                               ((u32 *) info->pseudo_palette)[i] =
-                                   ((*(ptransp + i) & 0xFF00) << 16) |
-                                   ((*(pred + i) & 0xFF00) << 8) |
-                                   ((*(pgreen + i) & 0xFF00)) |
-                                   ((*(pblue + i) & 0xFF00) >> 8);
-               } else {
-                       for (i = 0; i < len; i++)
-                               ((u32 *) info->pseudo_palette)[i] =
-                                   0x00000000 |
-                                   ((*(pred + i) & 0xFF00) << 8) |
-                                   ((*(pgreen + i) & 0xFF00)) |
-                                   ((*(pblue + i) & 0xFF00) >> 8);
-               }
-               break;
+               r = (red >> (16 - info->var.red.length))
+                       << info->var.red.offset;
+               b = (blue >> (16 - info->var.blue.length))
+                       << info->var.blue.offset;
+               g = (green >> (16 - info->var.green.length))
+                       << info->var.green.offset;
+               ((u32 *) info->pseudo_palette)[regno] = r | g | b;
        }
+
        return 0;
 }
 
 static int viafb_pan_display(struct fb_var_screeninfo *var,
        struct fb_info *info)
 {
-       unsigned int offset;
-
-       DEBUG_MSG(KERN_INFO "viafb_pan_display!\n");
-
-       offset = (var->xoffset + (var->yoffset * var->xres_virtual)) *
-           var->bits_per_pixel / 16;
+       struct viafb_par *viapar = info->par;
+       u32 vram_addr = (var->yoffset * var->xres_virtual + var->xoffset)
+               * (var->bits_per_pixel / 8) + viapar->vram_addr;
+
+       DEBUG_MSG(KERN_DEBUG "viafb_pan_display, address = %d\n", vram_addr);
+       if (!viafb_dual_fb) {
+               via_set_primary_address(vram_addr);
+               via_set_secondary_address(vram_addr);
+       } else if (viapar->iga_path == IGA1)
+               via_set_primary_address(vram_addr);
+       else
+               via_set_secondary_address(vram_addr);
 
-       DEBUG_MSG(KERN_INFO "\nviafb_pan_display,offset =%d ", offset);
-       viafb_set_primary_address(offset);
        return 0;
 }
 
@@ -483,6 +378,7 @@ static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
        u32 gpu32;
 
        DEBUG_MSG(KERN_INFO "viafb_ioctl: 0x%X !!\n", cmd);
+       printk(KERN_WARNING "viafb_ioctl: Please avoid this interface as it is unstable and might change or vanish at any time!\n");
        memset(&u, 0, sizeof(u));
 
        switch (cmd) {
@@ -682,14 +578,9 @@ static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
                break;
 
        case VIAFB_SET_GAMMA_LUT:
-               viafb_gamma_table = kmalloc(256 * sizeof(u32), GFP_KERNEL);
-               if (!viafb_gamma_table)
-                       return -ENOMEM;
-               if (copy_from_user(viafb_gamma_table, argp,
-                               sizeof(viafb_gamma_table))) {
-                       kfree(viafb_gamma_table);
-                       return -EFAULT;
-               }
+               viafb_gamma_table = memdup_user(argp, 256 * sizeof(u32));
+               if (IS_ERR(viafb_gamma_table))
+                       return PTR_ERR(viafb_gamma_table);
                viafb_set_gamma_table(viafb_bpp, viafb_gamma_table);
                kfree(viafb_gamma_table);
                break;
@@ -700,7 +591,7 @@ static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
                        return -ENOMEM;
                viafb_get_gamma_table(viafb_gamma_table);
                if (copy_to_user(argp, viafb_gamma_table,
-                       sizeof(viafb_gamma_table))) {
+                       256 * sizeof(u32))) {
                        kfree(viafb_gamma_table);
                        return -EFAULT;
                }
@@ -777,10 +668,12 @@ static int viafb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
 static void viafb_fillrect(struct fb_info *info,
        const struct fb_fillrect *rect)
 {
-       u32 col = 0, rop = 0;
-       int pitch;
+       struct viafb_par *viapar = info->par;
+       struct viafb_shared *shared = viapar->shared;
+       u32 fg_color;
+       u8 rop;
 
-       if (!viafb_accel) {
+       if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt) {
                cfb_fillrect(info, rect);
                return;
        }
@@ -788,67 +681,31 @@ static void viafb_fillrect(struct fb_info *info,
        if (!rect->width || !rect->height)
                return;
 
-       switch (rect->rop) {
-       case ROP_XOR:
+       if (info->fix.visual == FB_VISUAL_TRUECOLOR)
+               fg_color = ((u32 *)info->pseudo_palette)[rect->color];
+       else
+               fg_color = rect->color;
+
+       if (rect->rop == ROP_XOR)
                rop = 0x5A;
-               break;
-       case ROP_COPY:
-       default:
+       else
                rop = 0xF0;
-               break;
-       }
-
-       switch (info->var.bits_per_pixel) {
-       case 8:
-               col = rect->color;
-               break;
-       case 16:
-               col = ((u32 *) (info->pseudo_palette))[rect->color];
-               break;
-       case 32:
-               col = ((u32 *) (info->pseudo_palette))[rect->color];
-               break;
-       }
-
-       /* BitBlt Source Address */
-       writel(0x0, viaparinfo->io_virt + VIA_REG_SRCPOS);
-       /* Source Base Address */
-       writel(0x0, viaparinfo->io_virt + VIA_REG_SRCBASE);
-       /* Destination Base Address */
-       writel((info->fix.smem_start - viafbinfo->fix.smem_start) >> 3,
-                  viaparinfo->io_virt + VIA_REG_DSTBASE);
-       /* Pitch */
-       pitch = (info->var.xres_virtual + 7) & ~7;
-       writel(VIA_PITCH_ENABLE |
-                  (((pitch *
-                     info->var.bits_per_pixel >> 3) >> 3) |
-                     (((pitch * info->
-                     var.bits_per_pixel >> 3) >> 3) << 16)),
-                     viaparinfo->io_virt + VIA_REG_PITCH);
-       /* BitBlt Destination Address */
-       writel(((rect->dy << 16) | rect->dx),
-               viaparinfo->io_virt + VIA_REG_DSTPOS);
-       /* Dimension: width & height */
-       writel((((rect->height - 1) << 16) | (rect->width - 1)),
-               viaparinfo->io_virt + VIA_REG_DIMENSION);
-       /* Forground color or Destination color */
-       writel(col, viaparinfo->io_virt + VIA_REG_FGCOLOR);
-       /* GE Command */
-       writel((0x01 | 0x2000 | (rop << 24)),
-               viaparinfo->io_virt + VIA_REG_GECMD);
 
+       DEBUG_MSG(KERN_DEBUG "viafb 2D engine: fillrect\n");
+       if (shared->hw_bitblt(shared->vdev->engine_mmio, VIA_BITBLT_FILL,
+               rect->width, rect->height, info->var.bits_per_pixel,
+               viapar->vram_addr, info->fix.line_length, rect->dx, rect->dy,
+               NULL, 0, 0, 0, 0, fg_color, 0, rop))
+               cfb_fillrect(info, rect);
 }
 
 static void viafb_copyarea(struct fb_info *info,
        const struct fb_copyarea *area)
 {
-       u32 dy = area->dy, sy = area->sy, direction = 0x0;
-       u32 sx = area->sx, dx = area->dx, width = area->width;
-       int pitch;
+       struct viafb_par *viapar = info->par;
+       struct viafb_shared *shared = viapar->shared;
 
-       DEBUG_MSG(KERN_INFO "viafb_copyarea!!\n");
-
-       if (!viafb_accel) {
+       if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt) {
                cfb_copyarea(info, area);
                return;
        }
@@ -856,261 +713,150 @@ static void viafb_copyarea(struct fb_info *info,
        if (!area->width || !area->height)
                return;
 
-       if (sy < dy) {
-               dy += area->height - 1;
-               sy += area->height - 1;
-               direction |= 0x4000;
-       }
-
-       if (sx < dx) {
-               dx += width - 1;
-               sx += width - 1;
-               direction |= 0x8000;
-       }
-
-       /* Source Base Address */
-       writel((info->fix.smem_start - viafbinfo->fix.smem_start) >> 3,
-                  viaparinfo->io_virt + VIA_REG_SRCBASE);
-       /* Destination Base Address */
-       writel((info->fix.smem_start - viafbinfo->fix.smem_start) >> 3,
-                  viaparinfo->io_virt + VIA_REG_DSTBASE);
-       /* Pitch */
-       pitch = (info->var.xres_virtual + 7) & ~7;
-       /* VIA_PITCH_ENABLE can be omitted now. */
-       writel(VIA_PITCH_ENABLE |
-                  (((pitch *
-                     info->var.bits_per_pixel >> 3) >> 3) | (((pitch *
-                                                               info->var.
-                                                               bits_per_pixel
-                                                               >> 3) >> 3)
-                                                             << 16)),
-                               viaparinfo->io_virt + VIA_REG_PITCH);
-       /* BitBlt Source Address */
-       writel(((sy << 16) | sx), viaparinfo->io_virt + VIA_REG_SRCPOS);
-       /* BitBlt Destination Address */
-       writel(((dy << 16) | dx), viaparinfo->io_virt + VIA_REG_DSTPOS);
-       /* Dimension: width & height */
-       writel((((area->height - 1) << 16) | (area->width - 1)),
-                  viaparinfo->io_virt + VIA_REG_DIMENSION);
-       /* GE Command */
-       writel((0x01 | direction | (0xCC << 24)),
-               viaparinfo->io_virt + VIA_REG_GECMD);
-
+       DEBUG_MSG(KERN_DEBUG "viafb 2D engine: copyarea\n");
+       if (shared->hw_bitblt(shared->vdev->engine_mmio, VIA_BITBLT_COLOR,
+               area->width, area->height, info->var.bits_per_pixel,
+               viapar->vram_addr, info->fix.line_length, area->dx, area->dy,
+               NULL, viapar->vram_addr, info->fix.line_length,
+               area->sx, area->sy, 0, 0, 0))
+               cfb_copyarea(info, area);
 }
 
 static void viafb_imageblit(struct fb_info *info,
        const struct fb_image *image)
 {
-       u32 size, bg_col = 0, fg_col = 0, *udata;
-       int i;
-       int pitch;
+       struct viafb_par *viapar = info->par;
+       struct viafb_shared *shared = viapar->shared;
+       u32 fg_color = 0, bg_color = 0;
+       u8 op;
 
-       if (!viafb_accel) {
+       if (info->flags & FBINFO_HWACCEL_DISABLED || !shared->hw_bitblt ||
+               (image->depth != 1 && image->depth != viapar->depth)) {
                cfb_imageblit(info, image);
                return;
        }
 
-       udata = (u32 *) image->data;
-
-       switch (info->var.bits_per_pixel) {
-       case 8:
-               bg_col = image->bg_color;
-               fg_col = image->fg_color;
-               break;
-       case 16:
-               bg_col = ((u32 *) (info->pseudo_palette))[image->bg_color];
-               fg_col = ((u32 *) (info->pseudo_palette))[image->fg_color];
-               break;
-       case 32:
-               bg_col = ((u32 *) (info->pseudo_palette))[image->bg_color];
-               fg_col = ((u32 *) (info->pseudo_palette))[image->fg_color];
-               break;
-       }
-       size = image->width * image->height;
-
-       /* Source Base Address */
-       writel(0x0, viaparinfo->io_virt + VIA_REG_SRCBASE);
-       /* Destination Base Address */
-       writel((info->fix.smem_start - viafbinfo->fix.smem_start) >> 3,
-                  viaparinfo->io_virt + VIA_REG_DSTBASE);
-       /* Pitch */
-       pitch = (info->var.xres_virtual + 7) & ~7;
-       writel(VIA_PITCH_ENABLE |
-                  (((pitch *
-                     info->var.bits_per_pixel >> 3) >> 3) | (((pitch *
-                                                               info->var.
-                                                               bits_per_pixel
-                                                               >> 3) >> 3)
-                                                             << 16)),
-                               viaparinfo->io_virt + VIA_REG_PITCH);
-       /* BitBlt Source Address */
-       writel(0x0, viaparinfo->io_virt + VIA_REG_SRCPOS);
-       /* BitBlt Destination Address */
-       writel(((image->dy << 16) | image->dx),
-               viaparinfo->io_virt + VIA_REG_DSTPOS);
-       /* Dimension: width & height */
-       writel((((image->height - 1) << 16) | (image->width - 1)),
-                  viaparinfo->io_virt + VIA_REG_DIMENSION);
-       /* fb color */
-       writel(fg_col, viaparinfo->io_virt + VIA_REG_FGCOLOR);
-       /* bg color */
-       writel(bg_col, viaparinfo->io_virt + VIA_REG_BGCOLOR);
-       /* GE Command */
-       writel(0xCC020142, viaparinfo->io_virt + VIA_REG_GECMD);
-
-       for (i = 0; i < size / 4; i++) {
-               writel(*udata, viaparinfo->io_virt + VIA_MMIO_BLTBASE);
-               udata++;
-       }
+       if (image->depth == 1) {
+               op = VIA_BITBLT_MONO;
+               if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
+                       fg_color =
+                               ((u32 *)info->pseudo_palette)[image->fg_color];
+                       bg_color =
+                               ((u32 *)info->pseudo_palette)[image->bg_color];
+               } else {
+                       fg_color = image->fg_color;
+                       bg_color = image->bg_color;
+               }
+       } else
+               op = VIA_BITBLT_COLOR;
 
+       DEBUG_MSG(KERN_DEBUG "viafb 2D engine: imageblit\n");
+       if (shared->hw_bitblt(shared->vdev->engine_mmio, op,
+               image->width, image->height, info->var.bits_per_pixel,
+               viapar->vram_addr, info->fix.line_length, image->dx, image->dy,
+               (u32 *)image->data, 0, 0, 0, 0, fg_color, bg_color, 0))
+               cfb_imageblit(info, image);
 }
 
 static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
 {
-       u32 temp, xx, yy, bg_col = 0, fg_col = 0;
-       int i, j = 0;
-       static int hw_cursor;
-       struct viafb_par *p_viafb_par;
-
-       if (viafb_accel)
-               hw_cursor = 1;
-
-       if (!viafb_accel) {
-               if (hw_cursor) {
-                       viafb_show_hw_cursor(info, HW_Cursor_OFF);
-                       hw_cursor = 0;
-               }
-               return -ENODEV;
-       }
+       struct viafb_par *viapar = info->par;
+       void __iomem *engine = viapar->shared->vdev->engine_mmio;
+       u32 temp, xx, yy, bg_color = 0, fg_color = 0,
+               chip_name = viapar->shared->chip_info.gfx_chip_name;
+       int i, j = 0, cur_size = 64;
 
-       if ((((struct viafb_par *)(info->par))->iga_path == IGA2)
-           && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
+       if (info->flags & FBINFO_HWACCEL_DISABLED || info != viafbinfo)
                return -ENODEV;
 
-       /* When duoview and using lcd , use soft cursor */
-       if (viafb_LCD_ON || (!viafb_SAMM_ON &&
-               viafb_LCD2_ON + viafb_DVI_ON + viafb_CRT_ON == 2))
+       /* LCD ouput does not support hw cursors (at least on VN896) */
+       if ((chip_name == UNICHROME_CLE266 && viapar->iga_path == IGA2) ||
+               viafb_LCD_ON)
                return -ENODEV;
 
        viafb_show_hw_cursor(info, HW_Cursor_OFF);
-       viacursor = *cursor;
 
        if (cursor->set & FB_CUR_SETHOT) {
-               viacursor.hot = cursor->hot;
-               temp = ((viacursor.hot.x) << 16) + viacursor.hot.y;
-               writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_ORG);
+               temp = (cursor->hot.x << 16) + cursor->hot.y;
+               writel(temp, engine + VIA_REG_CURSOR_ORG);
        }
 
        if (cursor->set & FB_CUR_SETPOS) {
-               viacursor.image.dx = cursor->image.dx;
-               viacursor.image.dy = cursor->image.dy;
                yy = cursor->image.dy - info->var.yoffset;
                xx = cursor->image.dx - info->var.xoffset;
                temp = yy & 0xFFFF;
                temp |= (xx << 16);
-               writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_POS);
+               writel(temp, engine + VIA_REG_CURSOR_POS);
        }
 
-       if (cursor->set & FB_CUR_SETSIZE) {
-               temp = readl(viaparinfo->io_virt + VIA_REG_CURSOR_MODE);
+       if (cursor->image.width <= 32 && cursor->image.height <= 32)
+               cur_size = 32;
+       else if (cursor->image.width <= 64 && cursor->image.height <= 64)
+               cur_size = 64;
+       else {
+               printk(KERN_WARNING "viafb_cursor: The cursor is too large "
+                       "%dx%d", cursor->image.width, cursor->image.height);
+               return -ENXIO;
+       }
 
-               if ((cursor->image.width <= 32)
-                   && (cursor->image.height <= 32)) {
-                       MAX_CURS = 32;
+       if (cursor->set & FB_CUR_SETSIZE) {
+               temp = readl(engine + VIA_REG_CURSOR_MODE);
+               if (cur_size == 32)
                        temp |= 0x2;
-               } else if ((cursor->image.width <= 64)
-                          && (cursor->image.height <= 64)) {
-                       MAX_CURS = 64;
-                       temp &= 0xFFFFFFFD;
-               } else {
-                       DEBUG_MSG(KERN_INFO
-                       "The cursor image is biger than 64x64 bits...\n");
-                       return -ENXIO;
-               }
-               writel(temp, viaparinfo->io_virt + VIA_REG_CURSOR_MODE);
+               else
+                       temp &= ~0x2;
 
-               viacursor.image.height = cursor->image.height;
-               viacursor.image.width = cursor->image.width;
+               writel(temp, engine + VIA_REG_CURSOR_MODE);
        }
 
        if (cursor->set & FB_CUR_SETCMAP) {
-               viacursor.image.fg_color = cursor->image.fg_color;
-               viacursor.image.bg_color = cursor->image.bg_color;
-
-               switch (info->var.bits_per_pixel) {
-               case 8:
-               case 16:
-               case 32:
-                       bg_col =
-                           (0xFF << 24) |
-                           (((info->cmap.red)[viacursor.image.bg_color] &
-                           0xFF00) << 8) |
-                           ((info->cmap.green)[viacursor.image.bg_color] &
-                           0xFF00) |
-                           (((info->cmap.blue)[viacursor.image.bg_color] &
-                           0xFF00) >> 8);
-                       fg_col =
-                           (0xFF << 24) |
-                           (((info->cmap.red)[viacursor.image.fg_color] &
-                           0xFF00) << 8) |
-                           ((info->cmap.green)[viacursor.image.fg_color] &
-                           0xFF00) |
-                           (((info->cmap.blue)[viacursor.image.fg_color] &
-                           0xFF00) >> 8);
-                       break;
-               default:
-                       return 0;
-               }
-
-               /* This is indeed a patch for VT3324/VT3353 */
-               if (!info->par)
-                       return 0;
-               p_viafb_par = (struct viafb_par *)info->par;
-
-               if ((p_viafb_par->chip_info->gfx_chip_name ==
-                       UNICHROME_CX700) ||
-                       ((p_viafb_par->chip_info->gfx_chip_name ==
-                       UNICHROME_VX800))) {
-                       bg_col =
-                           (((info->cmap.red)[viacursor.image.bg_color] &
-                           0xFFC0) << 14) |
-                           (((info->cmap.green)[viacursor.image.bg_color] &
-                           0xFFC0) << 4) |
-                           (((info->cmap.blue)[viacursor.image.bg_color] &
-                           0xFFC0) >> 6);
-                       fg_col =
-                           (((info->cmap.red)[viacursor.image.fg_color] &
-                           0xFFC0) << 14) |
-                           (((info->cmap.green)[viacursor.image.fg_color] &
-                           0xFFC0) << 4) |
-                           (((info->cmap.blue)[viacursor.image.fg_color] &
-                           0xFFC0) >> 6);
+               fg_color = cursor->image.fg_color;
+               bg_color = cursor->image.bg_color;
+               if (chip_name == UNICHROME_CX700 ||
+                       chip_name == UNICHROME_VX800 ||
+                       chip_name == UNICHROME_VX855) {
+                       fg_color =
+                               ((info->cmap.red[fg_color] & 0xFFC0) << 14) |
+                               ((info->cmap.green[fg_color] & 0xFFC0) << 4) |
+                               ((info->cmap.blue[fg_color] & 0xFFC0) >> 6);
+                       bg_color =
+                               ((info->cmap.red[bg_color] & 0xFFC0) << 14) |
+                               ((info->cmap.green[bg_color] & 0xFFC0) << 4) |
+                               ((info->cmap.blue[bg_color] & 0xFFC0) >> 6);
+               } else {
+                       fg_color =
+                               ((info->cmap.red[fg_color] & 0xFF00) << 8) |
+                               (info->cmap.green[fg_color] & 0xFF00) |
+                               ((info->cmap.blue[fg_color] & 0xFF00) >> 8);
+                       bg_color =
+                               ((info->cmap.red[bg_color] & 0xFF00) << 8) |
+                               (info->cmap.green[bg_color] & 0xFF00) |
+                               ((info->cmap.blue[bg_color] & 0xFF00) >> 8);
                }
 
-               writel(bg_col, viaparinfo->io_virt + VIA_REG_CURSOR_BG);
-               writel(fg_col, viaparinfo->io_virt + VIA_REG_CURSOR_FG);
+               writel(bg_color, engine + VIA_REG_CURSOR_BG);
+               writel(fg_color, engine + VIA_REG_CURSOR_FG);
        }
 
        if (cursor->set & FB_CUR_SETSHAPE) {
                struct {
-                       u8 data[CURSOR_SIZE / 8];
-                       u32 bak[CURSOR_SIZE / 32];
+                       u8 data[CURSOR_SIZE];
+                       u32 bak[CURSOR_SIZE / 4];
                } *cr_data = kzalloc(sizeof(*cr_data), GFP_ATOMIC);
-               int size =
-                   ((viacursor.image.width + 7) >> 3) *
-                   viacursor.image.height;
+               int size = ((cursor->image.width + 7) >> 3) *
+                       cursor->image.height;
 
-               if (cr_data == NULL)
-                       goto out;
+               if (!cr_data)
+                       return -ENOMEM;
 
-               if (MAX_CURS == 32) {
-                       for (i = 0; i < (CURSOR_SIZE / 32); i++) {
+               if (cur_size == 32) {
+                       for (i = 0; i < (CURSOR_SIZE / 4); i++) {
                                cr_data->bak[i] = 0x0;
                                cr_data->bak[i + 1] = 0xFFFFFFFF;
                                i += 1;
                        }
-               } else if (MAX_CURS == 64) {
-                       for (i = 0; i < (CURSOR_SIZE / 32); i++) {
+               } else {
+                       for (i = 0; i < (CURSOR_SIZE / 4); i++) {
                                cr_data->bak[i] = 0x0;
                                cr_data->bak[i + 1] = 0x0;
                                cr_data->bak[i + 2] = 0xFFFFFFFF;
@@ -1119,27 +865,27 @@ static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
                        }
                }
 
-               switch (viacursor.rop) {
+               switch (cursor->rop) {
                case ROP_XOR:
                        for (i = 0; i < size; i++)
-                               cr_data->data[i] = viacursor.mask[i];
+                               cr_data->data[i] = cursor->mask[i];
                        break;
                case ROP_COPY:
 
                        for (i = 0; i < size; i++)
-                               cr_data->data[i] = viacursor.mask[i];
+                               cr_data->data[i] = cursor->mask[i];
                        break;
                default:
                        break;
                }
 
-               if (MAX_CURS == 32) {
+               if (cur_size == 32) {
                        for (i = 0; i < size; i++) {
                                cr_data->bak[j] = (u32) cr_data->data[i];
                                cr_data->bak[j + 1] = ~cr_data->bak[j];
                                j += 2;
                        }
-               } else if (MAX_CURS == 64) {
+               } else {
                        for (i = 0; i < size; i++) {
                                cr_data->bak[j] = (u32) cr_data->data[i];
                                cr_data->bak[j + 1] = 0x0;
@@ -1149,14 +895,12 @@ static int viafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
                        }
                }
 
-               memcpy(viafbinfo->screen_base +
-                      ((struct viafb_par *)(info->par))->cursor_start,
-                      cr_data->bak, CURSOR_SIZE);
-out:
+               memcpy_toio(viafbinfo->screen_base + viapar->shared->
+                       cursor_vram_addr, cr_data->bak, CURSOR_SIZE);
                kfree(cr_data);
        }
 
-       if (viacursor.enable)
+       if (cursor->enable)
                viafb_show_hw_cursor(info, HW_Cursor_ON);
 
        return 0;
@@ -1164,28 +908,11 @@ out:
 
 static int viafb_sync(struct fb_info *info)
 {
-       if (viafb_accel)
-               viafb_wait_engine_idle();
+       if (!(info->flags & FBINFO_HWACCEL_DISABLED))
+               viafb_wait_engine_idle(info);
        return 0;
 }
 
-int viafb_get_mode_index(int hres, int vres)
-{
-       u32 i;
-       DEBUG_MSG(KERN_INFO "viafb_get_mode_index!\n");
-
-       for (i = 0; i < NUM_TOTAL_MODETABLE; i++)
-               if (CLE266Modes[i].mode_array &&
-                       CLE266Modes[i].crtc[0].crtc.hor_addr == hres &&
-                       CLE266Modes[i].crtc[0].crtc.ver_addr == vres)
-                       break;
-
-       if (i == NUM_TOTAL_MODETABLE)
-               return VIA_RES_INVALID;
-
-       return CLE266Modes[i].ModeIndex;
-}
-
 static void check_available_device_to_enable(int device_id)
 {
        int device_num = 0;
@@ -1287,8 +1014,8 @@ static void viafb_set_device(struct device_t active_dev)
                viafb_SAMM_ON = active_dev.samm;
        viafb_primary_dev = active_dev.primary_dev;
 
-       viafb_set_primary_address(0);
-       viafb_set_secondary_address(viafb_SAMM_ON ? viafb_second_offset : 0);
+       via_set_primary_address(0);
+       via_set_secondary_address(viafb_SAMM_ON ? viafb_second_offset : 0);
        viafb_set_iga_path();
 }
 
@@ -1434,8 +1161,9 @@ static int apply_device_setting(struct viafb_ioctl_setting setting_info,
                        if (viafb_SAMM_ON)
                                viafb_primary_dev = setting_info.primary_device;
 
-                       viafb_set_primary_address(0);
-                       viafb_set_secondary_address(viafb_SAMM_ON ? viafb_second_offset : 0);
+                       via_set_primary_address(0);
+                       via_set_secondary_address(viafb_SAMM_ON ?
+                               viafb_second_offset : 0);
                        viafb_set_iga_path();
                }
                need_set_mode = 1;
@@ -1484,7 +1212,7 @@ static void retrieve_device_setting(struct viafb_ioctl_setting
        setting_info->lcd_attributes.lcd_mode = viafb_lcd_mode;
 }
 
-static void parse_active_dev(void)
+static int parse_active_dev(void)
 {
        viafb_CRT_ON = STATE_OFF;
        viafb_DVI_ON = STATE_OFF;
@@ -1495,60 +1223,63 @@ static void parse_active_dev(void)
           IGA path to devices in SAMM case. */
        /*    Note: The previous of active_dev is primary device,
           and the following is secondary device. */
-       if (!strncmp(viafb_active_dev, "CRT+DVI", 7)) {
+       if (!viafb_active_dev) {
+               viafb_CRT_ON = STATE_ON;
+               viafb_SAMM_ON = STATE_OFF;
+       } else if (!strcmp(viafb_active_dev, "CRT+DVI")) {
                /* CRT+DVI */
                viafb_CRT_ON = STATE_ON;
                viafb_DVI_ON = STATE_ON;
                viafb_primary_dev = CRT_Device;
-       } else if (!strncmp(viafb_active_dev, "DVI+CRT", 7)) {
+       } else if (!strcmp(viafb_active_dev, "DVI+CRT")) {
                /* DVI+CRT */
                viafb_CRT_ON = STATE_ON;
                viafb_DVI_ON = STATE_ON;
                viafb_primary_dev = DVI_Device;
-       } else if (!strncmp(viafb_active_dev, "CRT+LCD", 7)) {
+       } else if (!strcmp(viafb_active_dev, "CRT+LCD")) {
                /* CRT+LCD */
                viafb_CRT_ON = STATE_ON;
                viafb_LCD_ON = STATE_ON;
                viafb_primary_dev = CRT_Device;
-       } else if (!strncmp(viafb_active_dev, "LCD+CRT", 7)) {
+       } else if (!strcmp(viafb_active_dev, "LCD+CRT")) {
                /* LCD+CRT */
                viafb_CRT_ON = STATE_ON;
                viafb_LCD_ON = STATE_ON;
                viafb_primary_dev = LCD_Device;
-       } else if (!strncmp(viafb_active_dev, "DVI+LCD", 7)) {
+       } else if (!strcmp(viafb_active_dev, "DVI+LCD")) {
                /* DVI+LCD */
                viafb_DVI_ON = STATE_ON;
                viafb_LCD_ON = STATE_ON;
                viafb_primary_dev = DVI_Device;
-       } else if (!strncmp(viafb_active_dev, "LCD+DVI", 7)) {
+       } else if (!strcmp(viafb_active_dev, "LCD+DVI")) {
                /* LCD+DVI */
                viafb_DVI_ON = STATE_ON;
                viafb_LCD_ON = STATE_ON;
                viafb_primary_dev = LCD_Device;
-       } else if (!strncmp(viafb_active_dev, "LCD+LCD2", 8)) {
+       } else if (!strcmp(viafb_active_dev, "LCD+LCD2")) {
                viafb_LCD_ON = STATE_ON;
                viafb_LCD2_ON = STATE_ON;
                viafb_primary_dev = LCD_Device;
-       } else if (!strncmp(viafb_active_dev, "LCD2+LCD", 8)) {
+       } else if (!strcmp(viafb_active_dev, "LCD2+LCD")) {
                viafb_LCD_ON = STATE_ON;
                viafb_LCD2_ON = STATE_ON;
                viafb_primary_dev = LCD2_Device;
-       } else if (!strncmp(viafb_active_dev, "CRT", 3)) {
+       } else if (!strcmp(viafb_active_dev, "CRT")) {
                /* CRT only */
                viafb_CRT_ON = STATE_ON;
                viafb_SAMM_ON = STATE_OFF;
-       } else if (!strncmp(viafb_active_dev, "DVI", 3)) {
+       } else if (!strcmp(viafb_active_dev, "DVI")) {
                /* DVI only */
                viafb_DVI_ON = STATE_ON;
                viafb_SAMM_ON = STATE_OFF;
-       } else if (!strncmp(viafb_active_dev, "LCD", 3)) {
+       } else if (!strcmp(viafb_active_dev, "LCD")) {
                /* LCD only */
                viafb_LCD_ON = STATE_ON;
                viafb_SAMM_ON = STATE_OFF;
-       } else {
-               viafb_CRT_ON = STATE_ON;
-               viafb_SAMM_ON = STATE_OFF;
-       }
+       } else
+               return -EINVAL;
+
+       return 0;
 }
 
 static int parse_port(char *opt_str, int *output_interface)
@@ -1591,16 +1322,16 @@ static void parse_dvi_port(void)
                  output_interface);
 }
 
+#ifdef CONFIG_FB_VIA_DIRECT_PROCFS
+
 /*
  * The proc filesystem read/write function, a simple proc implement to
  * get/set the value of DPA  DVP0,   DVP0DataDriving,  DVP0ClockDriving, DVP1,
  * DVP1Driving, DFPHigh, DFPLow CR96,   SR2A[5], SR1B[1], SR2A[4], SR1E[2],
  * CR9B,    SR65,    CR97,    CR99
  */
-static int viafb_dvp0_proc_read(char *buf, char **start, off_t offset,
-int count, int *eof, void *data)
+static int viafb_dvp0_proc_show(struct seq_file *m, void *v)
 {
-       int len = 0;
        u8 dvp0_data_dri = 0, dvp0_clk_dri = 0, dvp0 = 0;
        dvp0_data_dri =
            (viafb_read_reg(VIASR, SR2A) & BIT5) >> 4 |
@@ -1609,13 +1340,17 @@ int count, int *eof, void *data)
            (viafb_read_reg(VIASR, SR2A) & BIT4) >> 3 |
            (viafb_read_reg(VIASR, SR1E) & BIT2) >> 2;
        dvp0 = viafb_read_reg(VIACR, CR96) & 0x0f;
-       len +=
-           sprintf(buf + len, "%x %x %x\n", dvp0, dvp0_data_dri, dvp0_clk_dri);
-       *eof = 1;               /*Inform kernel end of data */
-       return len;
+       seq_printf(m, "%x %x %x\n", dvp0, dvp0_data_dri, dvp0_clk_dri);
+       return 0;
 }
-static int viafb_dvp0_proc_write(struct file *file,
-       const char __user *buffer, unsigned long count, void *data)
+
+static int viafb_dvp0_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, viafb_dvp0_proc_show, NULL);
+}
+
+static ssize_t viafb_dvp0_proc_write(struct file *file,
+       const char __user *buffer, size_t count, loff_t *pos)
 {
        char buf[20], *value, *pbuf;
        u8 reg_val = 0;
@@ -1659,21 +1394,33 @@ static int viafb_dvp0_proc_write(struct file *file,
        }
        return count;
 }
-static int viafb_dvp1_proc_read(char *buf, char **start, off_t offset,
-       int count, int *eof, void *data)
+
+static const struct file_operations viafb_dvp0_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = viafb_dvp0_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = viafb_dvp0_proc_write,
+};
+
+static int viafb_dvp1_proc_show(struct seq_file *m, void *v)
 {
-       int len = 0;
        u8 dvp1 = 0, dvp1_data_dri = 0, dvp1_clk_dri = 0;
        dvp1 = viafb_read_reg(VIACR, CR9B) & 0x0f;
        dvp1_data_dri = (viafb_read_reg(VIASR, SR65) & 0x0c) >> 2;
        dvp1_clk_dri = viafb_read_reg(VIASR, SR65) & 0x03;
-       len +=
-           sprintf(buf + len, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
-       *eof = 1;               /*Inform kernel end of data */
-       return len;
+       seq_printf(m, "%x %x %x\n", dvp1, dvp1_data_dri, dvp1_clk_dri);
+       return 0;
 }
-static int viafb_dvp1_proc_write(struct file *file,
-       const char __user *buffer, unsigned long count, void *data)
+
+static int viafb_dvp1_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, viafb_dvp1_proc_show, NULL);
+}
+
+static ssize_t viafb_dvp1_proc_write(struct file *file,
+       const char __user *buffer, size_t count, loff_t *pos)
 {
        char buf[20], *value, *pbuf;
        u8 reg_val = 0;
@@ -1712,18 +1459,30 @@ static int viafb_dvp1_proc_write(struct file *file,
        return count;
 }
 
-static int viafb_dfph_proc_read(char *buf, char **start, off_t offset,
-       int count, int *eof, void *data)
+static const struct file_operations viafb_dvp1_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = viafb_dvp1_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = viafb_dvp1_proc_write,
+};
+
+static int viafb_dfph_proc_show(struct seq_file *m, void *v)
 {
-       int len = 0;
        u8 dfp_high = 0;
        dfp_high = viafb_read_reg(VIACR, CR97) & 0x0f;
-       len += sprintf(buf + len, "%x\n", dfp_high);
-       *eof = 1;               /*Inform kernel end of data */
-       return len;
+       seq_printf(m, "%x\n", dfp_high);
+       return 0;
+}
+
+static int viafb_dfph_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, viafb_dfph_proc_show, NULL);
 }
-static int viafb_dfph_proc_write(struct file *file,
-       const char __user *buffer, unsigned long count, void *data)
+
+static ssize_t viafb_dfph_proc_write(struct file *file,
+       const char __user *buffer, size_t count, loff_t *pos)
 {
        char buf[20];
        u8 reg_val = 0;
@@ -1738,18 +1497,31 @@ static int viafb_dfph_proc_write(struct file *file,
        viafb_write_reg_mask(CR97, VIACR, reg_val, 0x0f);
        return count;
 }
-static int viafb_dfpl_proc_read(char *buf, char **start, off_t offset,
-       int count, int *eof, void *data)
+
+static const struct file_operations viafb_dfph_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = viafb_dfph_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = viafb_dfph_proc_write,
+};
+
+static int viafb_dfpl_proc_show(struct seq_file *m, void *v)
 {
-       int len = 0;
        u8 dfp_low = 0;
        dfp_low = viafb_read_reg(VIACR, CR99) & 0x0f;
-       len += sprintf(buf + len, "%x\n", dfp_low);
-       *eof = 1;               /*Inform kernel end of data */
-       return len;
+       seq_printf(m, "%x\n", dfp_low);
+       return 0;
+}
+
+static int viafb_dfpl_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, viafb_dfpl_proc_show, NULL);
 }
-static int viafb_dfpl_proc_write(struct file *file,
-       const char __user *buffer, unsigned long count, void *data)
+
+static ssize_t viafb_dfpl_proc_write(struct file *file,
+       const char __user *buffer, size_t count, loff_t *pos)
 {
        char buf[20];
        u8 reg_val = 0;
@@ -1764,10 +1536,18 @@ static int viafb_dfpl_proc_write(struct file *file,
        viafb_write_reg_mask(CR99, VIACR, reg_val, 0x0f);
        return count;
 }
-static int viafb_vt1636_proc_read(char *buf, char **start,
-       off_t offset, int count, int *eof, void *data)
+
+static const struct file_operations viafb_dfpl_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = viafb_dfpl_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = viafb_dfpl_proc_write,
+};
+
+static int viafb_vt1636_proc_show(struct seq_file *m, void *v)
 {
-       int len = 0;
        u8 vt1636_08 = 0, vt1636_09 = 0;
        switch (viaparinfo->chip_info->lvds_chip_info.lvds_chip_name) {
        case VT1636_LVDS:
@@ -1777,7 +1557,7 @@ static int viafb_vt1636_proc_read(char *buf, char **start,
                vt1636_09 =
                    viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info,
                    &viaparinfo->chip_info->lvds_chip_info, 0x09) & 0x1f;
-               len += sprintf(buf + len, "%x %x\n", vt1636_08, vt1636_09);
+               seq_printf(m, "%x %x\n", vt1636_08, vt1636_09);
                break;
        default:
                break;
@@ -1790,16 +1570,21 @@ static int viafb_vt1636_proc_read(char *buf, char **start,
                vt1636_09 =
                    viafb_gpio_i2c_read_lvds(viaparinfo->lvds_setting_info2,
                        &viaparinfo->chip_info->lvds_chip_info2, 0x09) & 0x1f;
-               len += sprintf(buf + len, " %x %x\n", vt1636_08, vt1636_09);
+               seq_printf(m, " %x %x\n", vt1636_08, vt1636_09);
                break;
        default:
                break;
        }
-       *eof = 1;               /*Inform kernel end of data */
-       return len;
+       return 0;
 }
-static int viafb_vt1636_proc_write(struct file *file,
-       const char __user *buffer, unsigned long count, void *data)
+
+static int viafb_vt1636_proc_open(struct inode *inode, struct file *file)
+{
+       return single_open(file, viafb_vt1636_proc_show, NULL);
+}
+
+static ssize_t viafb_vt1636_proc_write(struct file *file,
+       const char __user *buffer, size_t count, loff_t *pos)
 {
        char buf[30], *value, *pbuf;
        struct IODATA reg_val;
@@ -1888,99 +1673,106 @@ static int viafb_vt1636_proc_write(struct file *file,
        return count;
 }
 
+static const struct file_operations viafb_vt1636_proc_fops = {
+       .owner          = THIS_MODULE,
+       .open           = viafb_vt1636_proc_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = single_release,
+       .write          = viafb_vt1636_proc_write,
+};
+
 static void viafb_init_proc(struct proc_dir_entry **viafb_entry)
 {
-       struct proc_dir_entry *entry;
        *viafb_entry = proc_mkdir("viafb", NULL);
-       if (viafb_entry) {
-               entry = create_proc_entry("dvp0", 0, *viafb_entry);
-               if (entry) {
-                       entry->read_proc = viafb_dvp0_proc_read;
-                       entry->write_proc = viafb_dvp0_proc_write;
-               }
-               entry = create_proc_entry("dvp1", 0, *viafb_entry);
-               if (entry) {
-                       entry->read_proc = viafb_dvp1_proc_read;
-                       entry->write_proc = viafb_dvp1_proc_write;
-               }
-               entry = create_proc_entry("dfph", 0, *viafb_entry);
-               if (entry) {
-                       entry->read_proc = viafb_dfph_proc_read;
-                       entry->write_proc = viafb_dfph_proc_write;
-               }
-               entry = create_proc_entry("dfpl", 0, *viafb_entry);
-               if (entry) {
-                       entry->read_proc = viafb_dfpl_proc_read;
-                       entry->write_proc = viafb_dfpl_proc_write;
-               }
+       if (*viafb_entry) {
+               proc_create("dvp0", 0, *viafb_entry, &viafb_dvp0_proc_fops);
+               proc_create("dvp1", 0, *viafb_entry, &viafb_dvp1_proc_fops);
+               proc_create("dfph", 0, *viafb_entry, &viafb_dfph_proc_fops);
+               proc_create("dfpl", 0, *viafb_entry, &viafb_dfpl_proc_fops);
                if (VT1636_LVDS == viaparinfo->chip_info->lvds_chip_info.
                        lvds_chip_name || VT1636_LVDS ==
                    viaparinfo->chip_info->lvds_chip_info2.lvds_chip_name) {
-                       entry = create_proc_entry("vt1636", 0, *viafb_entry);
-                       if (entry) {
-                               entry->read_proc = viafb_vt1636_proc_read;
-                               entry->write_proc = viafb_vt1636_proc_write;
-                       }
+                       proc_create("vt1636", 0, *viafb_entry, &viafb_vt1636_proc_fops);
                }
 
        }
 }
 static void viafb_remove_proc(struct proc_dir_entry *viafb_entry)
 {
-       /* no problem if it was not registered */
+       struct chip_information *chip_info = &viaparinfo->shared->chip_info;
+
        remove_proc_entry("dvp0", viafb_entry);/* parent dir */
        remove_proc_entry("dvp1", viafb_entry);
        remove_proc_entry("dfph", viafb_entry);
        remove_proc_entry("dfpl", viafb_entry);
-       remove_proc_entry("vt1636", viafb_entry);
-       remove_proc_entry("vt1625", viafb_entry);
+       if (chip_info->lvds_chip_info.lvds_chip_name == VT1636_LVDS
+               || chip_info->lvds_chip_info2.lvds_chip_name == VT1636_LVDS)
+               remove_proc_entry("vt1636", viafb_entry);
+
        remove_proc_entry("viafb", NULL);
 }
 
-static int __devinit via_pci_probe(void)
+#endif /* CONFIG_FB_VIA_DIRECT_PROCFS */
+
+static int parse_mode(const char *str, u32 *xres, u32 *yres)
 {
-       unsigned long default_xres, default_yres;
-       char *tmpc, *tmpm;
-       char *tmpc_sec, *tmpm_sec;
-       int vmode_index;
-       u32 tmds_length, lvds_length, crt_length, chip_length, viafb_par_length;
+       char *ptr;
 
-       DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
+       if (!str) {
+               *xres = 640;
+               *yres = 480;
+               return 0;
+       }
 
+       *xres = simple_strtoul(str, &ptr, 10);
+       if (ptr[0] != 'x')
+               return -EINVAL;
+
+       *yres = simple_strtoul(&ptr[1], &ptr, 10);
+       if (ptr[0])
+               return -EINVAL;
+
+       return 0;
+}
+
+
+int __devinit via_fb_pci_probe(struct viafb_dev *vdev)
+{
+       u32 default_xres, default_yres;
+       struct VideoModeTable *vmode_entry;
+       struct fb_var_screeninfo default_var;
+       int rc;
+       u32 viafb_par_length;
+
+       DEBUG_MSG(KERN_INFO "VIAFB PCI Probe!!\n");
+       memset(&default_var, 0, sizeof(default_var));
        viafb_par_length = ALIGN(sizeof(struct viafb_par), BITS_PER_LONG/8);
-       tmds_length = ALIGN(sizeof(struct tmds_setting_information),
-               BITS_PER_LONG/8);
-       lvds_length = ALIGN(sizeof(struct lvds_setting_information),
-               BITS_PER_LONG/8);
-       crt_length = ALIGN(sizeof(struct lvds_setting_information),
-               BITS_PER_LONG/8);
-       chip_length = ALIGN(sizeof(struct chip_information), BITS_PER_LONG/8);
 
        /* Allocate fb_info and ***_par here, also including some other needed
         * variables
        */
-       viafbinfo = framebuffer_alloc(viafb_par_length + 2 * lvds_length +
-       tmds_length + crt_length + chip_length, NULL);
+       viafbinfo = framebuffer_alloc(viafb_par_length +
+               ALIGN(sizeof(struct viafb_shared), BITS_PER_LONG/8),
+               &vdev->pdev->dev);
        if (!viafbinfo) {
                printk(KERN_ERR"Could not allocate memory for viafb_info.\n");
-               return -ENODEV;
+               return -ENOMEM;
        }
 
        viaparinfo = (struct viafb_par *)viafbinfo->par;
-       viaparinfo->tmds_setting_info = (struct tmds_setting_information *)
-               ((unsigned long)viaparinfo + viafb_par_length);
-       viaparinfo->lvds_setting_info = (struct lvds_setting_information *)
-               ((unsigned long)viaparinfo->tmds_setting_info + tmds_length);
-       viaparinfo->lvds_setting_info2 = (struct lvds_setting_information *)
-               ((unsigned long)viaparinfo->lvds_setting_info + lvds_length);
-       viaparinfo->crt_setting_info = (struct crt_setting_information *)
-               ((unsigned long)viaparinfo->lvds_setting_info2 + lvds_length);
-       viaparinfo->chip_info = (struct chip_information *)
-               ((unsigned long)viaparinfo->crt_setting_info + crt_length);
+       viaparinfo->shared = viafbinfo->par + viafb_par_length;
+       viaparinfo->shared->vdev = vdev;
+       viaparinfo->vram_addr = 0;
+       viaparinfo->tmds_setting_info = &viaparinfo->shared->tmds_setting_info;
+       viaparinfo->lvds_setting_info = &viaparinfo->shared->lvds_setting_info;
+       viaparinfo->lvds_setting_info2 =
+               &viaparinfo->shared->lvds_setting_info2;
+       viaparinfo->crt_setting_info = &viaparinfo->shared->crt_setting_info;
+       viaparinfo->chip_info = &viaparinfo->shared->chip_info;
 
        if (viafb_dual_fb)
                viafb_SAMM_ON = 1;
-       parse_active_dev();
        parse_lcd_port();
        parse_dvi_port();
 
@@ -1988,38 +1780,32 @@ static int __devinit via_pci_probe(void)
        if (!viafb_SAMM_ON)
                viafb_dual_fb = 0;
 
-       /* Set up I2C bus stuff */
-       viafb_create_i2c_bus(viaparinfo);
-
-       viafb_init_chip_info();
-       viafb_get_fb_info(&viaparinfo->fbmem, &viaparinfo->memsize);
+       viafb_init_chip_info(vdev->chip_type);
+       /*
+        * The framebuffer will have been successfully mapped by
+        * the core (or we'd not be here), but we still need to
+        * set up our own accounting.
+        */
+       viaparinfo->fbmem = vdev->fbmem_start;
+       viaparinfo->memsize = vdev->fbmem_len;
        viaparinfo->fbmem_free = viaparinfo->memsize;
        viaparinfo->fbmem_used = 0;
-       viafbinfo->screen_base = ioremap_nocache(viaparinfo->fbmem,
-               viaparinfo->memsize);
-       if (!viafbinfo->screen_base) {
-               printk(KERN_INFO "ioremap failed\n");
-               return -ENOMEM;
-       }
-
-       viafb_get_mmio_info(&viafbinfo->fix.mmio_start,
-               &viafbinfo->fix.mmio_len);
-       viaparinfo->io_virt = ioremap_nocache(viafbinfo->fix.mmio_start,
-               viafbinfo->fix.mmio_len);
-       if (!viaparinfo->io_virt) {
-               printk(KERN_WARNING "ioremap failed: hardware acceleration disabled\n");
-               viafb_accel = 0;
-       }
+       viafbinfo->screen_base = vdev->fbmem;
 
+       viafbinfo->fix.mmio_start = vdev->engine_start;
+       viafbinfo->fix.mmio_len = vdev->engine_len;
        viafbinfo->node = 0;
        viafbinfo->fbops = &viafb_ops;
        viafbinfo->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
 
        viafbinfo->pseudo_palette = pseudo_pal;
-       if (viafb_accel) {
-               viafb_init_accel();
-               viafb_init_2d_engine();
-               viafb_hw_cursor_init();
+       if (viafb_accel && !viafb_init_engine(viafbinfo)) {
+               viafbinfo->flags |= FBINFO_HWACCEL_COPYAREA |
+                       FBINFO_HWACCEL_FILLRECT |  FBINFO_HWACCEL_IMAGEBLIT;
+               default_var.accel_flags = FB_ACCELF_TEXT;
+       } else {
+               viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
+               default_var.accel_flags = 0;
        }
 
        if (viafb_second_size && (viafb_second_size < 8)) {
@@ -2031,26 +1817,12 @@ static int __devinit via_pci_probe(void)
                        viafb_second_size * 1024 * 1024;
        }
 
-       tmpm = viafb_mode;
-       tmpc = strsep(&tmpm, "x");
-       strict_strtoul(tmpc, 0, &default_xres);
-       strict_strtoul(tmpm, 0, &default_yres);
-
-       vmode_index = viafb_get_mode_index(default_xres, default_yres);
-       DEBUG_MSG(KERN_INFO "0->index=%d\n", vmode_index);
-
+       parse_mode(viafb_mode, &default_xres, &default_yres);
+       vmode_entry = viafb_get_mode(default_xres, default_yres);
        if (viafb_SAMM_ON == 1) {
-               if (strcmp(viafb_mode, viafb_mode1)) {
-                       tmpm_sec = viafb_mode1;
-                       tmpc_sec = strsep(&tmpm_sec, "x");
-                       strict_strtoul(tmpc_sec, 0,
-                               (unsigned long *)&viafb_second_xres);
-                       strict_strtoul(tmpm_sec, 0,
-                               (unsigned long *)&viafb_second_yres);
-               } else {
-                       viafb_second_xres = default_xres;
-                       viafb_second_yres = default_yres;
-               }
+               parse_mode(viafb_mode1, &viafb_second_xres,
+                       &viafb_second_yres);
+
                if (0 == viafb_second_virtual_xres) {
                        switch (viafb_second_xres) {
                        case 1400:
@@ -2065,19 +1837,6 @@ static int __devinit via_pci_probe(void)
                        viafb_second_virtual_yres = viafb_second_yres;
        }
 
-       switch (viafb_bpp) {
-       case 0 ... 8:
-               viafb_bpp = 8;
-               break;
-       case 9 ... 16:
-               viafb_bpp = 16;
-               break;
-       case 17 ... 32:
-               viafb_bpp = 32;
-               break;
-       default:
-               viafb_bpp = 8;
-       }
        default_var.xres = default_xres;
        default_var.yres = default_yres;
        switch (default_xres) {
@@ -2090,8 +1849,6 @@ static int __devinit via_pci_probe(void)
        }
        default_var.yres_virtual = default_yres;
        default_var.bits_per_pixel = viafb_bpp;
-       if (default_var.bits_per_pixel == 15)
-               default_var.bits_per_pixel = 16;
        default_var.pixclock =
            viafb_get_pixclock(default_xres, default_yres, viafb_refresh);
        default_var.left_margin = (default_xres >> 3) & 0xf8;
@@ -2100,26 +1857,21 @@ static int __devinit via_pci_probe(void)
        default_var.lower_margin = 4;
        default_var.hsync_len = default_var.left_margin;
        default_var.vsync_len = 4;
-       default_var.accel_flags = 0;
-
-       if (viafb_accel) {
-               viafbinfo->flags |=
-                   (FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
-                    FBINFO_HWACCEL_IMAGEBLIT);
-               default_var.accel_flags |= FB_ACCELF_TEXT;
-       } else
-               viafbinfo->flags |= FBINFO_HWACCEL_DISABLED;
+       viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
+       viafbinfo->var = default_var;
 
        if (viafb_dual_fb) {
-               viafbinfo1 = framebuffer_alloc(viafb_par_length, NULL);
+               viafbinfo1 = framebuffer_alloc(viafb_par_length,
+                               &vdev->pdev->dev);
                if (!viafbinfo1) {
                        printk(KERN_ERR
                        "allocate the second framebuffer struct error\n");
-                       framebuffer_release(viafbinfo);
-                       return -ENOMEM;
+                       rc = -ENOMEM;
+                       goto out_fb_release;
                }
                viaparinfo1 = viafbinfo1->par;
                memcpy(viaparinfo1, viaparinfo, viafb_par_length);
+               viaparinfo1->vram_addr = viafb_second_offset;
                viaparinfo1->memsize = viaparinfo->memsize -
                        viafb_second_offset;
                viaparinfo->memsize = viafb_second_offset;
@@ -2130,18 +1882,11 @@ static int __devinit via_pci_probe(void)
                        viaparinfo1->fbmem_used;
                viaparinfo->fbmem_free = viaparinfo->memsize;
                viaparinfo->fbmem_used = 0;
-               if (viafb_accel) {
-                       viaparinfo1->cursor_start =
-                           viaparinfo->cursor_start - viafb_second_offset;
-                       viaparinfo1->VQ_start = viaparinfo->VQ_start -
-                               viafb_second_offset;
-                       viaparinfo1->VQ_end = viaparinfo->VQ_end -
-                               viafb_second_offset;
-               }
 
                viaparinfo->iga_path = IGA1;
                viaparinfo1->iga_path = IGA2;
                memcpy(viafbinfo1, viafbinfo, sizeof(struct fb_info));
+               viafbinfo1->par = viaparinfo1;
                viafbinfo1->screen_base = viafbinfo->screen_base +
                        viafb_second_offset;
 
@@ -2149,8 +1894,6 @@ static int __devinit via_pci_probe(void)
                default_var.yres = viafb_second_yres;
                default_var.xres_virtual = viafb_second_virtual_xres;
                default_var.yres_virtual = viafb_second_virtual_yres;
-               if (viafb_bpp1 != viafb_bpp)
-                       viafb_bpp1 = viafb_bpp;
                default_var.bits_per_pixel = viafb_bpp1;
                default_var.pixclock =
                    viafb_get_pixclock(viafb_second_xres, viafb_second_yres,
@@ -2166,55 +1909,75 @@ static int __devinit via_pci_probe(void)
                viafb_check_var(&default_var, viafbinfo1);
                viafbinfo1->var = default_var;
                viafb_update_fix(viafbinfo1);
+               viaparinfo1->depth = fb_get_color_depth(&viafbinfo1->var,
+                       &viafbinfo1->fix);
        }
 
-       viafb_setup_fixinfo(&viafbinfo->fix, viaparinfo);
-       viafb_check_var(&default_var, viafbinfo);
-       viafbinfo->var = default_var;
+       viafb_check_var(&viafbinfo->var, viafbinfo);
        viafb_update_fix(viafbinfo);
+       viaparinfo->depth = fb_get_color_depth(&viafbinfo->var,
+               &viafbinfo->fix);
        default_var.activate = FB_ACTIVATE_NOW;
-       fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
+       rc = fb_alloc_cmap(&viafbinfo->cmap, 256, 0);
+       if (rc)
+               goto out_fb1_release;
 
        if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
            && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266)) {
-               if (register_framebuffer(viafbinfo1) < 0)
-                       return -EINVAL;
+               rc = register_framebuffer(viafbinfo1);
+               if (rc)
+                       goto out_dealloc_cmap;
        }
-       if (register_framebuffer(viafbinfo) < 0)
-               return -EINVAL;
+       rc = register_framebuffer(viafbinfo);
+       if (rc)
+               goto out_fb1_unreg_lcd_cle266;
 
        if (viafb_dual_fb && ((viafb_primary_dev != LCD_Device)
                        || (viaparinfo->chip_info->gfx_chip_name !=
                        UNICHROME_CLE266))) {
-               if (register_framebuffer(viafbinfo1) < 0)
-                       return -EINVAL;
+               rc = register_framebuffer(viafbinfo1);
+               if (rc)
+                       goto out_fb_unreg;
        }
        DEBUG_MSG(KERN_INFO "fb%d: %s frame buffer device %dx%d-%dbpp\n",
                  viafbinfo->node, viafbinfo->fix.id, default_var.xres,
                  default_var.yres, default_var.bits_per_pixel);
 
-       viafb_init_proc(&viaparinfo->proc_entry);
+#ifdef CONFIG_FB_VIA_DIRECT_PROCFS
+       viafb_init_proc(&viaparinfo->shared->proc_entry);
+#endif
        viafb_init_dac(IGA2);
        return 0;
+
+out_fb_unreg:
+       unregister_framebuffer(viafbinfo);
+out_fb1_unreg_lcd_cle266:
+       if (viafb_dual_fb && (viafb_primary_dev == LCD_Device)
+           && (viaparinfo->chip_info->gfx_chip_name == UNICHROME_CLE266))
+               unregister_framebuffer(viafbinfo1);
+out_dealloc_cmap:
+       fb_dealloc_cmap(&viafbinfo->cmap);
+out_fb1_release:
+       if (viafbinfo1)
+               framebuffer_release(viafbinfo1);
+out_fb_release:
+       framebuffer_release(viafbinfo);
+       return rc;
 }
 
-static void __devexit via_pci_remove(void)
+void __devexit via_fb_pci_remove(struct pci_dev *pdev)
 {
        DEBUG_MSG(KERN_INFO "via_pci_remove!\n");
        fb_dealloc_cmap(&viafbinfo->cmap);
        unregister_framebuffer(viafbinfo);
        if (viafb_dual_fb)
                unregister_framebuffer(viafbinfo1);
-       iounmap((void *)viafbinfo->screen_base);
-       iounmap(viaparinfo->io_virt);
-
-       viafb_delete_i2c_buss(viaparinfo);
-
+#ifdef CONFIG_FB_VIA_DIRECT_PROCFS
+       viafb_remove_proc(viaparinfo->shared->proc_entry);
+#endif
        framebuffer_release(viafbinfo);
        if (viafb_dual_fb)
                framebuffer_release(viafbinfo1);
-
-       viafb_remove_proc(viaparinfo->proc_entry);
 }
 
 #ifndef MODULE
@@ -2290,24 +2053,34 @@ static int __init viafb_setup(char *options)
 }
 #endif
 
-static int __init viafb_init(void)
+/*
+ * These are called out of via-core for now.
+ */
+int __init viafb_init(void)
 {
+       u32 dummy;
 #ifndef MODULE
        char *option = NULL;
        if (fb_get_options("viafb", &option))
                return -ENODEV;
        viafb_setup(option);
 #endif
+       if (parse_mode(viafb_mode, &dummy, &dummy)
+               || parse_mode(viafb_mode1, &dummy, &dummy)
+               || viafb_bpp < 0 || viafb_bpp > 32
+               || viafb_bpp1 < 0 || viafb_bpp1 > 32
+               || parse_active_dev())
+               return -EINVAL;
+
        printk(KERN_INFO
        "VIA Graphics Intergration Chipset framebuffer %d.%d initializing\n",
               VERSION_MAJOR, VERSION_MINOR);
-       return via_pci_probe();
+       return 0;
 }
 
-static void __exit viafb_exit(void)
+void __exit viafb_exit(void)
 {
        DEBUG_MSG(KERN_INFO "viafb_exit!\n");
-       via_pci_remove();
 }
 
 static struct fb_ops viafb_ops = {
@@ -2325,86 +2098,81 @@ static struct fb_ops viafb_ops = {
        .fb_cursor = viafb_cursor,
        .fb_ioctl = viafb_ioctl,
        .fb_sync = viafb_sync,
-       .fb_setcmap = viafb_setcmap,
 };
 
-module_init(viafb_init);
-module_exit(viafb_exit);
 
 #ifdef MODULE
-module_param(viafb_memsize, int, 0);
-
-module_param(viafb_mode, charp, 0);
+module_param(viafb_mode, charp, S_IRUSR);
 MODULE_PARM_DESC(viafb_mode, "Set resolution (default=640x480)");
 
-module_param(viafb_mode1, charp, 0);
+module_param(viafb_mode1, charp, S_IRUSR);
 MODULE_PARM_DESC(viafb_mode1, "Set resolution (default=640x480)");
 
-module_param(viafb_bpp, int, 0);
+module_param(viafb_bpp, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_bpp, "Set color depth (default=32bpp)");
 
-module_param(viafb_bpp1, int, 0);
+module_param(viafb_bpp1, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_bpp1, "Set color depth (default=32bpp)");
 
-module_param(viafb_refresh, int, 0);
+module_param(viafb_refresh, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_refresh,
        "Set CRT viafb_refresh rate (default = 60)");
 
-module_param(viafb_refresh1, int, 0);
+module_param(viafb_refresh1, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_refresh1,
        "Set CRT refresh rate (default = 60)");
 
-module_param(viafb_lcd_panel_id, int, 0);
+module_param(viafb_lcd_panel_id, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_lcd_panel_id,
        "Set Flat Panel type(Default=1024x768)");
 
-module_param(viafb_lcd_dsp_method, int, 0);
+module_param(viafb_lcd_dsp_method, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_lcd_dsp_method,
        "Set Flat Panel display scaling method.(Default=Expandsion)");
 
-module_param(viafb_SAMM_ON, int, 0);
+module_param(viafb_SAMM_ON, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_SAMM_ON,
        "Turn on/off flag of SAMM(Default=OFF)");
 
-module_param(viafb_accel, int, 0);
+module_param(viafb_accel, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_accel,
-       "Set 2D Hardware Acceleration.(Default = OFF)");
+       "Set 2D Hardware Acceleration: 0 = OFF, 1 = ON (default)");
 
-module_param(viafb_active_dev, charp, 0);
+module_param(viafb_active_dev, charp, S_IRUSR);
 MODULE_PARM_DESC(viafb_active_dev, "Specify active devices.");
 
-module_param(viafb_display_hardware_layout, int, 0);
+module_param(viafb_display_hardware_layout, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_display_hardware_layout,
        "Display Hardware Layout (LCD Only, DVI Only...,etc)");
 
-module_param(viafb_second_size, int, 0);
+module_param(viafb_second_size, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_second_size,
        "Set secondary device memory size");
 
-module_param(viafb_dual_fb, int, 0);
+module_param(viafb_dual_fb, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_dual_fb,
        "Turn on/off flag of dual framebuffer devices.(Default = OFF)");
 
-module_param(viafb_platform_epia_dvi, int, 0);
+module_param(viafb_platform_epia_dvi, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_platform_epia_dvi,
        "Turn on/off flag of DVI devices on EPIA board.(Default = OFF)");
 
-module_param(viafb_device_lcd_dualedge, int, 0);
+module_param(viafb_device_lcd_dualedge, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_device_lcd_dualedge,
        "Turn on/off flag of dual edge panel.(Default = OFF)");
 
-module_param(viafb_bus_width, int, 0);
+module_param(viafb_bus_width, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_bus_width,
        "Set bus width of panel.(Default = 12)");
 
-module_param(viafb_lcd_mode, int, 0);
+module_param(viafb_lcd_mode, int, S_IRUSR);
 MODULE_PARM_DESC(viafb_lcd_mode,
        "Set Flat Panel mode(Default=OPENLDI)");
 
-module_param(viafb_lcd_port, charp, 0);
+module_param(viafb_lcd_port, charp, S_IRUSR);
 MODULE_PARM_DESC(viafb_lcd_port, "Specify LCD output port.");
 
-module_param(viafb_dvi_port, charp, 0);
+module_param(viafb_dvi_port, charp, S_IRUSR);
 MODULE_PARM_DESC(viafb_dvi_port, "Specify DVI output port.");
 
 MODULE_LICENSE("GPL");