fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / intelfb / intelfbdrv.c
index 549e229..5ba3999 100644 (file)
@@ -1,11 +1,12 @@
 /*
  * intelfb
  *
- * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G/915G
- * integrated graphics chips.
+ * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G/915G/915GM/
+ * 945G/945GM/945GME/965G/965GM integrated graphics chips.
  *
- * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
+ * Copyright Â© 2002, 2003 David Dawes <dawes@xfree86.org>
  *                   2004 Sylvain Meyer
+ *                   2006 David Airlie
  *
  * This driver consists of two parts.  The first part (intelfbdrv.c) provides
  * the basic fbdev interfaces, is derived in part from the radeonfb and
  *              Fix intelfbhw_pan_display typo
  *              Add __initdata annotations
  *
- * TODO:
- *
- *
- * Wish List:
- *
+ *    04/2008 - Version 0.9.5
+ *              Add support for 965G/965GM. (Maik Broemme <mbroemme@plusserver.de>)
  *
+ *    08/2008 - Version 0.9.6
+ *              Add support for 945GME. (Phil Endecott <spam_from_intelfb@chezphil.org>)
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <linux/mm.h>
-#include <linux/tty.h>
 #include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/fb.h>
-#include <linux/console.h>
-#include <linux/selection.h>
 #include <linux/ioport.h>
 #include <linux/init.h>
 #include <linux/pci.h>
 #include <linux/vmalloc.h>
-#include <linux/kd.h>
-#include <linux/vt_kern.h>
 #include <linux/pagemap.h>
-#include <linux/version.h>
+#include <linux/screen_info.h>
 
 #include <asm/io.h>
 
 
 #include "intelfb.h"
 #include "intelfbhw.h"
+#include "../edid.h"
 
 static void __devinit get_initial_mode(struct intelfb_info *dinfo);
 static void update_dinfo(struct intelfb_info *dinfo,
                         struct fb_var_screeninfo *var);
-static int intelfb_get_fix(struct fb_fix_screeninfo *fix,
-                          struct fb_info *info);
-
+static int intelfb_open(struct fb_info *info, int user);
+static int intelfb_release(struct fb_info *info, int user);
 static int intelfb_check_var(struct fb_var_screeninfo *var,
                             struct fb_info *info);
 static int intelfb_set_par(struct fb_info *info);
@@ -165,9 +159,8 @@ static int intelfb_cursor(struct fb_info *info,
 
 static int intelfb_sync(struct fb_info *info);
 
-static int intelfb_ioctl(struct inode *inode, struct file *file,
-                        unsigned int cmd, unsigned long arg,
-                        struct fb_info *info);
+static int intelfb_ioctl(struct fb_info *info,
+                        unsigned int cmd, unsigned long arg);
 
 static int __devinit intelfb_pci_register(struct pci_dev *pdev,
                                          const struct pci_device_id *ent);
@@ -189,7 +182,14 @@ static struct pci_device_id intelfb_pci_table[] __devinitdata = {
        { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_845G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_845G },
        { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_85XGM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_85XGM },
        { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_865G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_865G },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_854, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_854 },
        { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_915G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_915G },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_915GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_915GM },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945G },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945GM },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_945GME, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_945GME },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_965G, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_965G },
+       { PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_965GM, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8, INTELFB_CLASS_MASK, INTEL_965GM },
        { 0, }
 };
 
@@ -199,6 +199,8 @@ static int num_registered = 0;
 /* fb ops */
 static struct fb_ops intel_fb_ops = {
        .owner =                THIS_MODULE,
+       .fb_open =              intelfb_open,
+       .fb_release =           intelfb_release,
        .fb_check_var =         intelfb_check_var,
        .fb_set_par =           intelfb_set_par,
        .fb_setcolreg =         intelfb_setcolreg,
@@ -214,7 +216,7 @@ static struct fb_ops intel_fb_ops = {
 
 /* PCI driver module table */
 static struct pci_driver intelfb_driver = {
-       .name =         "Intel(R) " SUPPORTED_CHIPSETS " Framebuffer Driver",
+       .name =         "intelfb",
        .id_table =     intelfb_pci_table,
        .probe =        intelfb_pci_register,
        .remove =       __devexit_p(intelfb_pci_unregister)
@@ -223,27 +225,30 @@ static struct pci_driver intelfb_driver = {
 /* Module description/parameters */
 MODULE_AUTHOR("David Dawes <dawes@tungstengraphics.com>, "
              "Sylvain Meyer <sylvain.meyer@worldonline.fr>");
-MODULE_DESCRIPTION(
-       "Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS " chipsets");
+MODULE_DESCRIPTION("Framebuffer driver for Intel(R) " SUPPORTED_CHIPSETS
+                  " chipsets");
 MODULE_LICENSE("Dual BSD/GPL");
 MODULE_DEVICE_TABLE(pci, intelfb_pci_table);
 
-static int accel        __initdata = 1;
-static int vram         __initdata = 4;
-static int hwcursor     __initdata = 1;
-static int mtrr         __initdata = 1;
-static int fixed        __initdata = 0;
-static int noinit       __initdata = 0;
-static int noregister   __initdata = 0;
-static int probeonly    __initdata = 0;
-static int idonly       __initdata = 0;
-static int bailearly    __initdata = 0;
-static char *mode       __initdata = NULL;
+static int accel        = 1;
+static int vram         = 4;
+static int hwcursor     = 0;
+static int mtrr         = 1;
+static int fixed        = 0;
+static int noinit       = 0;
+static int noregister   = 0;
+static int probeonly    = 0;
+static int idonly       = 0;
+static int bailearly    = 0;
+static int voffset     = 48;
+static char *mode       = NULL;
 
 module_param(accel, bool, S_IRUGO);
-MODULE_PARM_DESC(accel, "Enable console acceleration");
+MODULE_PARM_DESC(accel, "Enable hardware acceleration");
 module_param(vram, int, S_IRUGO);
 MODULE_PARM_DESC(vram, "System RAM to allocate to framebuffer in MiB");
+module_param(voffset, int, S_IRUGO);
+MODULE_PARM_DESC(voffset, "Offset of framebuffer in MiB");
 module_param(hwcursor, bool, S_IRUGO);
 MODULE_PARM_DESC(hwcursor, "Enable HW cursor");
 module_param(mtrr, bool, S_IRUGO);
@@ -266,11 +271,10 @@ MODULE_PARM_DESC(mode,
 
 #ifndef MODULE
 #define OPT_EQUAL(opt, name) (!strncmp(opt, name, strlen(name)))
-#define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name), NULL, 0)
+#define OPT_INTVAL(opt, name) simple_strtoul(opt + strlen(name) + 1, NULL, 0)
 #define OPT_STRVAL(opt, name) (opt + strlen(name))
 
-static __inline__ char *
-get_opt_string(const char *this_opt, const char *name)
+static __inline__ char * get_opt_string(const char *this_opt, const char *name)
 {
        const char *p;
        int i;
@@ -288,8 +292,8 @@ get_opt_string(const char *this_opt, const char *name)
        return ret;
 }
 
-static __inline__ int
-get_opt_int(const char *this_opt, const char *name, int *ret)
+static __inline__ int get_opt_int(const char *this_opt, const char *name,
+                                 int *ret)
 {
        if (!ret)
                return 0;
@@ -301,8 +305,8 @@ get_opt_int(const char *this_opt, const char *name, int *ret)
        return 1;
 }
 
-static __inline__ int
-get_opt_bool(const char *this_opt, const char *name, int *ret)
+static __inline__ int get_opt_bool(const char *this_opt, const char *name,
+                                  int *ret)
 {
        if (!ret)
                return 0;
@@ -322,8 +326,7 @@ get_opt_bool(const char *this_opt, const char *name, int *ret)
        return 1;
 }
 
-static int __init
-intelfb_setup(char *options)
+static int __init intelfb_setup(char *options)
 {
        char *this_opt;
 
@@ -353,7 +356,7 @@ intelfb_setup(char *options)
                        continue;
                if (get_opt_bool(this_opt, "accel", &accel))
                        ;
-               else if (get_opt_int(this_opt, "vram", &vram))
+               else if (get_opt_int(this_opt, "vram", &vram))
                        ;
                else if (get_opt_bool(this_opt, "hwcursor", &hwcursor))
                        ;
@@ -374,8 +377,7 @@ intelfb_setup(char *options)
 
 #endif
 
-static int __init
-intelfb_init(void)
+static int __init intelfb_init(void)
 {
 #ifndef MODULE
        char *option = NULL;
@@ -399,8 +401,7 @@ intelfb_init(void)
        return pci_register_driver(&intelfb_driver);
 }
 
-static void __exit
-intelfb_exit(void)
+static void __exit intelfb_exit(void)
 {
        DBG_MSG("intelfb_exit\n");
        pci_unregister_driver(&intelfb_driver);
@@ -426,8 +427,8 @@ static inline void __devinit set_mtrr(struct intelfb_info *dinfo)
 }
 static inline void unset_mtrr(struct intelfb_info *dinfo)
 {
-       if (dinfo->has_mtrr)
-               mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
+       if (dinfo->has_mtrr)
+               mtrr_del(dinfo->mtrr_reg, dinfo->aperture.physical,
                         dinfo->aperture.size);
 }
 #else
@@ -440,14 +441,15 @@ static inline void unset_mtrr(struct intelfb_info *dinfo)
  *                        driver init / cleanup                *
  ***************************************************************/
 
-static void
-cleanup(struct intelfb_info *dinfo)
+static void cleanup(struct intelfb_info *dinfo)
 {
        DBG_MSG("cleanup\n");
 
        if (!dinfo)
                return;
 
+       intelfbhw_disable_irq(dinfo);
+
        fb_dealloc_cmap(&dinfo->info->cmap);
        kfree(dinfo->info->pixmap.addr);
 
@@ -469,14 +471,19 @@ cleanup(struct intelfb_info *dinfo)
                agp_free_memory(dinfo->gtt_ring_mem);
        }
 
+#ifdef CONFIG_FB_INTEL_I2C
+       /* un-register I2C bus */
+       intelfb_delete_i2c_busses(dinfo);
+#endif
+
        if (dinfo->mmio_base)
                iounmap((void __iomem *)dinfo->mmio_base);
        if (dinfo->aperture.virtual)
                iounmap((void __iomem *)dinfo->aperture.virtual);
 
-       if (dinfo->mmio_base_phys)
+       if (dinfo->flag & INTELFB_MMIO_ACQUIRED)
                release_mem_region(dinfo->mmio_base_phys, INTEL_REG_SIZE);
-       if (dinfo->aperture.physical)
+       if (dinfo->flag & INTELFB_FB_ACQUIRED)
                release_mem_region(dinfo->aperture.physical,
                                   dinfo->aperture.size);
        framebuffer_release(dinfo->info);
@@ -490,19 +497,20 @@ cleanup(struct intelfb_info *dinfo)
 } while (0)
 
 
-static int __devinit
-intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
+static int __devinit intelfb_pci_register(struct pci_dev *pdev,
+                                         const struct pci_device_id *ent)
 {
        struct fb_info *info;
        struct intelfb_info *dinfo;
-       int i, j, err, dvo;
+       int i, err, dvo;
        int aperture_size, stolen_size;
        struct agp_kern_info gtt_info;
        int agp_memtype;
        const char *s;
        struct agp_bridge_data *bridge;
-       int aperture_bar = 0;
-       int mmio_bar = 1;
+       int aperture_bar = 0;
+       int mmio_bar = 1;
+       int offset;
 
        DBG_MSG("intelfb_pci_register\n");
 
@@ -530,12 +538,11 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        dinfo->pdev  = pdev;
 
        /* Reserve pixmap space. */
-       info->pixmap.addr = kmalloc(64 * 1024, GFP_KERNEL);
+       info->pixmap.addr = kzalloc(64 * 1024, GFP_KERNEL);
        if (info->pixmap.addr == NULL) {
                ERR_MSG("Cannot reserve pixmap memory.\n");
                goto err_out_pixmap;
        }
-       memset(info->pixmap.addr, 0, 64 * 1024);
 
        /* set early this option because it could be changed by tv encoder
           driver */
@@ -549,11 +556,16 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        }
 
        /* Set base addresses. */
-       if (ent->device == PCI_DEVICE_ID_INTEL_915G) {
+       if ((ent->device == PCI_DEVICE_ID_INTEL_915G) ||
+           (ent->device == PCI_DEVICE_ID_INTEL_915GM) ||
+           (ent->device == PCI_DEVICE_ID_INTEL_945G)  ||
+           (ent->device == PCI_DEVICE_ID_INTEL_945GM) ||
+           (ent->device == PCI_DEVICE_ID_INTEL_945GME) ||
+           (ent->device == PCI_DEVICE_ID_INTEL_965G) ||
+           (ent->device == PCI_DEVICE_ID_INTEL_965GM)) {
+
                aperture_bar = 2;
                mmio_bar = 0;
-               /* Disable HW cursor on 915G (not implemented yet) */
-               hwcursor = 0;
        }
        dinfo->aperture.physical = pci_resource_start(pdev, aperture_bar);
        dinfo->aperture.size     = pci_resource_len(pdev, aperture_bar);
@@ -571,6 +583,9 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
                cleanup(dinfo);
                return -ENODEV;
        }
+
+       dinfo->flag |= INTELFB_FB_ACQUIRED;
+
        if (!request_mem_region(dinfo->mmio_base_phys,
                                INTEL_REG_SIZE,
                                INTELFB_MODULE_NAME)) {
@@ -579,28 +594,12 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
                return -ENODEV;
        }
 
-       /* Map the fb and MMIO regions */
-       dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
-               (dinfo->aperture.physical, dinfo->aperture.size);
-       if (!dinfo->aperture.virtual) {
-               ERR_MSG("Cannot remap FB region.\n");
-               cleanup(dinfo);
-               return -ENODEV;
-       }
-       dinfo->mmio_base =
-               (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
-                                              INTEL_REG_SIZE);
-       if (!dinfo->mmio_base) {
-               ERR_MSG("Cannot remap MMIO region.\n");
-               cleanup(dinfo);
-               return -ENODEV;
-       }
+       dinfo->flag |= INTELFB_MMIO_ACQUIRED;
 
        /* Get the chipset info. */
        dinfo->pci_chipset = pdev->device;
 
-       if (intelfbhw_get_chipset(pdev, &dinfo->name, &dinfo->chipset,
-                                 &dinfo->mobile)) {
+       if (intelfbhw_get_chipset(pdev, dinfo)) {
                cleanup(dinfo);
                return -ENODEV;
        }
@@ -640,9 +639,8 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
                dinfo->ring.size = RINGBUFFER_SIZE;
                dinfo->ring_tail_mask = dinfo->ring.size - 1;
        }
-       if (dinfo->hwcursor) {
+       if (dinfo->hwcursor)
                dinfo->cursor.size = HW_CURSOR_SIZE;
-       }
 
        /* Use agpgart to manage the GATT */
        if (!(bridge = agp_backend_acquire(pdev))) {
@@ -659,22 +657,43 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
                return -ENODEV;
        }
 
+       if (MB(voffset) < stolen_size)
+               offset = (stolen_size >> 12);
+       else
+               offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
+
        /* set the mem offsets - set them after the already used pages */
-       if (dinfo->accel) {
-               dinfo->ring.offset = (stolen_size >> 12)
-                       + gtt_info.current_memory;
-       }
-       if (dinfo->hwcursor) {
-               dinfo->cursor.offset = (stolen_size >> 12) +
+       if (dinfo->accel)
+               dinfo->ring.offset = offset + gtt_info.current_memory;
+       if (dinfo->hwcursor)
+               dinfo->cursor.offset = offset +
                        + gtt_info.current_memory + (dinfo->ring.size >> 12);
-       }
-       if (dinfo->fbmem_gart) {
-               dinfo->fb.offset = (stolen_size >> 12) +
+       if (dinfo->fbmem_gart)
+               dinfo->fb.offset = offset +
                        + gtt_info.current_memory + (dinfo->ring.size >> 12)
                        + (dinfo->cursor.size >> 12);
-       }
 
        /* Allocate memories (which aren't stolen) */
+       /* Map the fb and MMIO regions */
+       /* ioremap only up to the end of used aperture */
+       dinfo->aperture.virtual = (u8 __iomem *)ioremap_nocache
+               (dinfo->aperture.physical, ((offset + dinfo->fb.offset) << 12)
+                + dinfo->fb.size);
+       if (!dinfo->aperture.virtual) {
+               ERR_MSG("Cannot remap FB region.\n");
+               cleanup(dinfo);
+               return -ENODEV;
+       }
+
+       dinfo->mmio_base =
+               (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
+                                             INTEL_REG_SIZE);
+       if (!dinfo->mmio_base) {
+               ERR_MSG("Cannot remap MMIO region.\n");
+               cleanup(dinfo);
+               return -ENODEV;
+       }
+
        if (dinfo->accel) {
                if (!(dinfo->gtt_ring_mem =
                      agp_allocate_memory(bridge, dinfo->ring.size >> 12,
@@ -695,7 +714,7 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
                        + (dinfo->ring.offset << 12);
                dinfo->ring.virtual  = dinfo->aperture.virtual
                        + (dinfo->ring.offset << 12);
-               dinfo->ring_head = dinfo->ring.virtual;
+               dinfo->ring_head = 0;
        }
        if (dinfo->hwcursor) {
                agp_memtype = dinfo->mobile ? AGP_PHYSICAL_MEMORY
@@ -754,18 +773,18 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (mtrr)
                set_mtrr(dinfo);
 
-       DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%x)\n",
+       DBG_MSG("fb: 0x%x(+ 0x%x)/0x%x (0x%p)\n",
                dinfo->fb.physical, dinfo->fb.offset, dinfo->fb.size,
-               (u32 __iomem ) dinfo->fb.virtual);
-       DBG_MSG("MMIO: 0x%x/0x%x (0x%x)\n",
+               dinfo->fb.virtual);
+       DBG_MSG("MMIO: 0x%x/0x%x (0x%p)\n",
                dinfo->mmio_base_phys, INTEL_REG_SIZE,
-               (u32 __iomem) dinfo->mmio_base);
-       DBG_MSG("ring buffer: 0x%x/0x%x (0x%x)\n",
+               dinfo->mmio_base);
+       DBG_MSG("ring buffer: 0x%x/0x%x (0x%p)\n",
                dinfo->ring.physical, dinfo->ring.size,
-               (u32 __iomem ) dinfo->ring.virtual);
-       DBG_MSG("HW cursor: 0x%x/0x%x (0x%x) (offset 0x%x) (phys 0x%x)\n",
+               dinfo->ring.virtual);
+       DBG_MSG("HW cursor: 0x%x/0x%x (0x%p) (offset 0x%x) (phys 0x%x)\n",
                dinfo->cursor.physical, dinfo->cursor.size,
-               (u32 __iomem ) dinfo->cursor.virtual, dinfo->cursor.offset,
+               dinfo->cursor.virtual, dinfo->cursor.offset,
                dinfo->cursor.physical);
 
        DBG_MSG("options: vram = %d, accel = %d, hwcursor = %d, fixed = %d, "
@@ -799,7 +818,8 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (bailearly == 1)
                bailout(dinfo);
 
-       if (FIXED_MODE(dinfo) && ORIG_VIDEO_ISVGA != VIDEO_TYPE_VLFB) {
+       if (FIXED_MODE(dinfo) &&
+           screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB) {
                ERR_MSG("Video mode must be programmed at boot time.\n");
                cleanup(dinfo);
                return -ENODEV;
@@ -810,16 +830,14 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        /* Initialise dinfo and related data. */
        /* If an initial mode was programmed at boot time, get its details. */
-       if (ORIG_VIDEO_ISVGA == VIDEO_TYPE_VLFB)
+       if (screen_info.orig_video_isVGA == VIDEO_TYPE_VLFB)
                get_initial_mode(dinfo);
 
        if (bailearly == 3)
                bailout(dinfo);
 
-       if (FIXED_MODE(dinfo)) {
-               /* remap fb address */
+       if (FIXED_MODE(dinfo))  /* remap fb address */
                update_dinfo(dinfo, &dinfo->initial_var);
-       }
 
        if (bailearly == 4)
                bailout(dinfo);
@@ -833,12 +851,10 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (bailearly == 5)
                bailout(dinfo);
 
-       for (i = 0; i < 16; i++) {
-               j = color_table[i];
-               dinfo->palette[i].red = default_red[j];
-               dinfo->palette[i].green = default_grn[j];
-               dinfo->palette[i].blue = default_blu[j];
-       }
+#ifdef CONFIG_FB_INTEL_I2C
+       /* register I2C bus */
+       intelfb_create_i2c_busses(dinfo);
+#endif
 
        if (bailearly == 6)
                bailout(dinfo);
@@ -858,6 +874,9 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (bailearly == 18)
                bailout(dinfo);
 
+       /* read active pipe */
+       dinfo->pipe = intelfbhw_active_pipe(&dinfo->save_state);
+
        /* Cursor initialisation */
        if (dinfo->hwcursor) {
                intelfbhw_cursor_init(dinfo);
@@ -884,6 +903,13 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        }
 
        dinfo->registered = 1;
+       dinfo->open = 0;
+
+       init_waitqueue_head(&dinfo->vsync.wait);
+       spin_lock_init(&dinfo->int_lock);
+       dinfo->irq_flags = 0;
+       dinfo->vsync.pan_display = 0;
+       dinfo->vsync.pan_offset = 0;
 
        return 0;
 
@@ -913,8 +939,7 @@ intelfb_pci_unregister(struct pci_dev *pdev)
  *                       helper functions                      *
  ***************************************************************/
 
-int __inline__
-intelfb_var_to_depth(const struct fb_var_screeninfo *var)
+int __inline__ intelfb_var_to_depth(const struct fb_var_screeninfo *var)
 {
        DBG_MSG("intelfb_var_to_depth: bpp: %d, green.length is %d\n",
                var->bits_per_pixel, var->green.length);
@@ -930,8 +955,7 @@ intelfb_var_to_depth(const struct fb_var_screeninfo *var)
 }
 
 
-static __inline__ int
-var_to_refresh(const struct fb_var_screeninfo *var)
+static __inline__ int var_to_refresh(const struct fb_var_screeninfo *var)
 {
        int xtot = var->xres + var->left_margin + var->right_margin +
                   var->hsync_len;
@@ -945,8 +969,7 @@ var_to_refresh(const struct fb_var_screeninfo *var)
  *                Various intialisation functions              *
  ***************************************************************/
 
-static void __devinit
-get_initial_mode(struct intelfb_info *dinfo)
+static void __devinit get_initial_mode(struct intelfb_info *dinfo)
 {
        struct fb_var_screeninfo *var;
        int xtot, ytot;
@@ -1013,8 +1036,7 @@ get_initial_mode(struct intelfb_info *dinfo)
        }
 }
 
-static int __devinit
-intelfb_init_var(struct intelfb_info *dinfo)
+static int __devinit intelfb_init_var(struct intelfb_info *dinfo)
 {
        struct fb_var_screeninfo *var;
        int msrc = 0;
@@ -1027,18 +1049,43 @@ intelfb_init_var(struct intelfb_info *dinfo)
                       sizeof(struct fb_var_screeninfo));
                msrc = 5;
        } else {
+               const u8 *edid_s = fb_firmware_edid(&dinfo->pdev->dev);
+               u8 *edid_d = NULL;
+
+               if (edid_s) {
+                       edid_d = kmemdup(edid_s, EDID_LENGTH, GFP_KERNEL);
+
+                       if (edid_d) {
+                               fb_edid_to_monspecs(edid_d,
+                                                   &dinfo->info->monspecs);
+                               kfree(edid_d);
+                       }
+               }
+
                if (mode) {
+                       printk("intelfb: Looking for mode in private "
+                              "database\n");
                        msrc = fb_find_mode(var, dinfo->info, mode,
-                                           vesa_modes, VESA_MODEDB_SIZE,
+                                           dinfo->info->monspecs.modedb,
+                                           dinfo->info->monspecs.modedb_len,
                                            NULL, 0);
-                       if (msrc)
-                               msrc |= 8;
+
+                       if (msrc && msrc > 1) {
+                               printk("intelfb: No mode in private database, "
+                                      "intelfb: looking for mode in global "
+                                      "database ");
+                               msrc = fb_find_mode(var, dinfo->info, mode,
+                                                   NULL, 0, NULL, 0);
+
+                               if (msrc)
+                                       msrc |= 8;
+                       }
+
                }
-               if (!msrc) {
+
+               if (!msrc)
                        msrc = fb_find_mode(var, dinfo->info, PREFERRED_MODE,
-                                           vesa_modes, VESA_MODEDB_SIZE,
-                                           NULL, 0);
-               }
+                                           NULL, 0, NULL, 0);
        }
 
        if (!msrc) {
@@ -1070,8 +1117,7 @@ intelfb_init_var(struct intelfb_info *dinfo)
        return 0;
 }
 
-static int __devinit
-intelfb_set_fbinfo(struct intelfb_info *dinfo)
+static int __devinit intelfb_set_fbinfo(struct intelfb_info *dinfo)
 {
        struct fb_info *info = dinfo->info;
 
@@ -1083,21 +1129,32 @@ intelfb_set_fbinfo(struct intelfb_info *dinfo)
 
        info->pixmap.size = 64*1024;
        info->pixmap.buf_align = 8;
+       info->pixmap.access_align = 32;
        info->pixmap.flags = FB_PIXMAP_SYSTEM;
 
        if (intelfb_init_var(dinfo))
                return 1;
 
        info->pixmap.scan_align = 1;
-
+       strcpy(info->fix.id, dinfo->name);
+       info->fix.smem_start = dinfo->fb.physical;
+       info->fix.smem_len = dinfo->fb.size;
+       info->fix.type = FB_TYPE_PACKED_PIXELS;
+       info->fix.type_aux = 0;
+       info->fix.xpanstep = 8;
+       info->fix.ypanstep = 1;
+       info->fix.ywrapstep = 0;
+       info->fix.mmio_start = dinfo->mmio_base_phys;
+       info->fix.mmio_len = INTEL_REG_SIZE;
+       info->fix.accel = FB_ACCEL_I830;
        update_dinfo(dinfo, &info->var);
 
        return 0;
 }
 
 /* Update dinfo to match the active video mode. */
-static void
-update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
+static void update_dinfo(struct intelfb_info *dinfo,
+                        struct fb_var_screeninfo *var)
 {
        DBG_MSG("update_dinfo\n");
 
@@ -1107,7 +1164,8 @@ update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
        dinfo->yres = var->xres;
        dinfo->pixclock = var->pixclock;
 
-       intelfb_get_fix(&dinfo->info->fix, dinfo->info);
+       dinfo->info->fix.visual = dinfo->visual;
+       dinfo->info->fix.line_length = dinfo->pitch;
 
        switch (dinfo->bpp) {
        case 8:
@@ -1125,7 +1183,10 @@ update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
        }
 
        /* Make sure the line length is a aligned correctly. */
-       dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
+       if (IS_I9XX(dinfo))
+               dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT_I9XX);
+       else
+               dinfo->pitch = ROUND_UP_TO(dinfo->pitch, STRIDE_ALIGNMENT);
 
        if (FIXED_MODE(dinfo))
                dinfo->pitch = dinfo->initial_pitch;
@@ -1137,61 +1198,78 @@ update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
 
 /* fbops functions */
 
-static int
-intelfb_get_fix(struct fb_fix_screeninfo *fix, struct fb_info *info)
+/***************************************************************
+ *                       fbdev interface                       *
+ ***************************************************************/
+
+static int intelfb_open(struct fb_info *info, int user)
 {
        struct intelfb_info *dinfo = GET_DINFO(info);
 
-       DBG_MSG("intelfb_get_fix\n");
-
-       memset(fix, 0, sizeof(*fix));
-       strcpy(fix->id, dinfo->name);
-       fix->smem_start = dinfo->fb.physical;
-       fix->smem_len = dinfo->fb.size;
-       fix->type = FB_TYPE_PACKED_PIXELS;
-       fix->type_aux = 0;
-       fix->visual = dinfo->visual;
-       fix->xpanstep = 8;
-       fix->ypanstep = 1;
-       fix->ywrapstep = 0;
-       fix->line_length = dinfo->pitch;
-       fix->mmio_start = dinfo->mmio_base_phys;
-       fix->mmio_len = INTEL_REG_SIZE;
-       fix->accel = FB_ACCEL_I830;
+       if (user)
+               dinfo->open++;
+
        return 0;
 }
 
-/***************************************************************
- *                       fbdev interface                       *
- ***************************************************************/
+static int intelfb_release(struct fb_info *info, int user)
+{
+       struct intelfb_info *dinfo = GET_DINFO(info);
+
+       if (user) {
+               dinfo->open--;
+               msleep(1);
+               if (!dinfo->open)
+                       intelfbhw_disable_irq(dinfo);
+       }
+
+       return 0;
+}
 
-static int
-intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
+static int intelfb_check_var(struct fb_var_screeninfo *var,
+                            struct fb_info *info)
 {
        int change_var = 0;
        struct fb_var_screeninfo v;
        struct intelfb_info *dinfo;
        static int first = 1;
+       int i;
+       /* Good pitches to allow tiling.  Don't care about pitches < 1024. */
+       static const int pitches[] = {
+               128 * 8,
+               128 * 16,
+               128 * 32,
+               128 * 64,
+               0
+       };
 
        DBG_MSG("intelfb_check_var: accel_flags is %d\n", var->accel_flags);
 
        dinfo = GET_DINFO(info);
 
+       /* update the pitch */
        if (intelfbhw_validate_mode(dinfo, var) != 0)
                return -EINVAL;
 
        v = *var;
 
+       for (i = 0; pitches[i] != 0; i++) {
+               if (pitches[i] >= v.xres_virtual) {
+                       v.xres_virtual = pitches[i];
+                       break;
+               }
+       }
+
        /* Check for a supported bpp. */
-       if (v.bits_per_pixel <= 8) {
+       if (v.bits_per_pixel <= 8)
                v.bits_per_pixel = 8;
-       else if (v.bits_per_pixel <= 16) {
+       else if (v.bits_per_pixel <= 16) {
                if (v.bits_per_pixel == 16)
                        v.green.length = 6;
                v.bits_per_pixel = 16;
-       } else if (v.bits_per_pixel <= 32) {
+       } else if (v.bits_per_pixel <= 32)
                v.bits_per_pixel = 32;
-       else
+       else
                return -EINVAL;
 
        change_var = ((info->var.xres != var->xres) ||
@@ -1273,10 +1351,9 @@ intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        return 0;
 }
 
-static int
-intelfb_set_par(struct fb_info *info)
+static int intelfb_set_par(struct fb_info *info)
 {
-       struct intelfb_hwstate *hw;
+       struct intelfb_hwstate *hw;
         struct intelfb_info *dinfo = GET_DINFO(info);
 
        if (FIXED_MODE(dinfo)) {
@@ -1284,32 +1361,37 @@ intelfb_set_par(struct fb_info *info)
                return -EINVAL;
        }
 
-       hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
-       if (!hw)
-               return -ENOMEM;
+       hw = kmalloc(sizeof(*hw), GFP_ATOMIC);
+       if (!hw)
+               return -ENOMEM;
 
        DBG_MSG("intelfb_set_par (%dx%d-%d)\n", info->var.xres,
                info->var.yres, info->var.bits_per_pixel);
 
+       /*
+        * Disable VCO prior to timing register change.
+        */
+       OUTREG(DPLL_A, INREG(DPLL_A) & ~DPLL_VCO_ENABLE);
+
        intelfb_blank(FB_BLANK_POWERDOWN, info);
 
-       if (dinfo->accel)
+       if (ACCEL(dinfo, info))
                intelfbhw_2d_stop(dinfo);
 
-       memcpy(hw, &dinfo->save_state, sizeof(*hw));
-       if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
-               goto invalid_mode;
-       if (intelfbhw_program_mode(dinfo, hw, 0))
-               goto invalid_mode;
+       memcpy(hw, &dinfo->save_state, sizeof(*hw));
+       if (intelfbhw_mode_to_hw(dinfo, hw, &info->var))
+               goto invalid_mode;
+       if (intelfbhw_program_mode(dinfo, hw, 0))
+               goto invalid_mode;
 
 #if REGDUMP > 0
-       intelfbhw_read_hw_state(dinfo, hw, 0);
-       intelfbhw_print_hw_state(dinfo, hw);
+       intelfbhw_read_hw_state(dinfo, hw, 0);
+       intelfbhw_print_hw_state(dinfo, hw);
 #endif
 
        update_dinfo(dinfo, &info->var);
 
-       if (dinfo->accel)
+       if (ACCEL(dinfo, info))
                intelfbhw_2d_start(dinfo);
 
        intelfb_pan_display(&info->var, info);
@@ -1320,9 +1402,9 @@ intelfb_set_par(struct fb_info *info)
                info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
                FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT |
                FBINFO_HWACCEL_IMAGEBLIT;
-       } else {
+       } else
                info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
-       }
+
        kfree(hw);
        return 0;
 invalid_mode:
@@ -1330,9 +1412,9 @@ invalid_mode:
        return -EINVAL;
 }
 
-static int
-intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
-                 unsigned blue, unsigned transp, struct fb_info *info)
+static int intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
+                            unsigned blue, unsigned transp,
+                            struct fb_info *info)
 {
        struct intelfb_info *dinfo = GET_DINFO(info);
 
@@ -1343,66 +1425,75 @@ intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
        if (regno > 255)
                return 1;
 
-       switch (dinfo->depth) {
-       case 8:
-               {
-                       red >>= 8;
-                       green >>= 8;
-                       blue >>= 8;
+       if (dinfo->depth == 8) {
+               red >>= 8;
+               green >>= 8;
+               blue >>= 8;
 
-                       dinfo->palette[regno].red = red;
-                       dinfo->palette[regno].green = green;
-                       dinfo->palette[regno].blue = blue;
+               intelfbhw_setcolreg(dinfo, regno, red, green, blue,
+                                   transp);
+       }
 
-                       intelfbhw_setcolreg(dinfo, regno, red, green, blue,
-                                           transp);
+       if (regno < 16) {
+               switch (dinfo->depth) {
+               case 15:
+                       dinfo->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
+                               ((green & 0xf800) >>  6) |
+                               ((blue & 0xf800) >> 11);
+                       break;
+               case 16:
+                       dinfo->pseudo_palette[regno] = (red & 0xf800) |
+                               ((green & 0xfc00) >>  5) |
+                               ((blue  & 0xf800) >> 11);
+                       break;
+               case 24:
+                       dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
+                               (green & 0xff00) |
+                               ((blue  & 0xff00) >> 8);
+                       break;
                }
-               break;
-       case 15:
-               dinfo->pseudo_palette[regno] = ((red & 0xf800) >>  1) |
-                                              ((green & 0xf800) >>  6) |
-                                              ((blue & 0xf800) >> 11);
-               break;
-       case 16:
-               dinfo->pseudo_palette[regno] = (red & 0xf800) |
-                                              ((green & 0xfc00) >>  5) |
-                                              ((blue  & 0xf800) >> 11);
-               break;
-       case 24:
-               dinfo->pseudo_palette[regno] = ((red & 0xff00) << 8) |
-                                              (green & 0xff00) |
-                                              ((blue  & 0xff00) >> 8);
-               break;
        }
+
        return 0;
 }
 
-static int
-intelfb_blank(int blank, struct fb_info *info)
+static int intelfb_blank(int blank, struct fb_info *info)
 {
        intelfbhw_do_blank(blank, info);
        return 0;
 }
 
-static int
-intelfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
+static int intelfb_pan_display(struct fb_var_screeninfo *var,
+                              struct fb_info *info)
 {
        intelfbhw_pan_display(var, info);
        return 0;
 }
 
 /* When/if we have our own ioctls. */
-static int
-intelfb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
-             unsigned long arg, struct fb_info *info)
+static int intelfb_ioctl(struct fb_info *info, unsigned int cmd,
+                        unsigned long arg)
 {
        int retval = 0;
+       struct intelfb_info *dinfo = GET_DINFO(info);
+       u32 pipe = 0;
+
+       switch (cmd) {
+               case FBIO_WAITFORVSYNC:
+                       if (get_user(pipe, (__u32 __user *)arg))
+                               return -EFAULT;
+
+                       retval = intelfbhw_wait_for_vsync(dinfo, pipe);
+                       break;
+               default:
+                       break;
+       }
 
        return retval;
 }
 
-static void
-intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
+static void intelfb_fillrect (struct fb_info *info,
+                             const struct fb_fillrect *rect)
 {
         struct intelfb_info *dinfo = GET_DINFO(info);
        u32 rop, color;
@@ -1411,12 +1502,14 @@ intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
        DBG_MSG("intelfb_fillrect\n");
 #endif
 
-       if (!ACCEL(dinfo, info) || dinfo->depth == 4)
-               return cfb_fillrect(info, rect);
+       if (!ACCEL(dinfo, info) || dinfo->depth == 4) {
+               cfb_fillrect(info, rect);
+               return;
+       }
 
        if (rect->rop == ROP_COPY)
                rop = PAT_ROP_GXCOPY;
-       else // ROP_XOR
+       else /* ROP_XOR */
                rop = PAT_ROP_GXXOR;
 
        if (dinfo->depth != 8)
@@ -1430,8 +1523,8 @@ intelfb_fillrect (struct fb_info *info, const struct fb_fillrect *rect)
                              rop);
 }
 
-static void
-intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
+static void intelfb_copyarea(struct fb_info *info,
+                            const struct fb_copyarea *region)
 {
         struct intelfb_info *dinfo = GET_DINFO(info);
 
@@ -1439,16 +1532,18 @@ intelfb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
        DBG_MSG("intelfb_copyarea\n");
 #endif
 
-       if (!ACCEL(dinfo, info) || dinfo->depth == 4)
-               return cfb_copyarea(info, region);
+       if (!ACCEL(dinfo, info) || dinfo->depth == 4) {
+               cfb_copyarea(info, region);
+               return;
+       }
 
        intelfbhw_do_bitblt(dinfo, region->sx, region->sy, region->dx,
                            region->dy, region->width, region->height,
                            dinfo->pitch, info->var.bits_per_pixel);
 }
 
-static void
-intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
+static void intelfb_imageblit(struct fb_info *info,
+                             const struct fb_image *image)
 {
         struct intelfb_info *dinfo = GET_DINFO(info);
        u32 fgcolor, bgcolor;
@@ -1458,8 +1553,10 @@ intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
 #endif
 
        if (!ACCEL(dinfo, info) || dinfo->depth == 4
-           || image->depth != 1)
-               return cfb_imageblit(info, image);
+           || image->depth != 1) {
+               cfb_imageblit(info, image);
+               return;
+       }
 
        if (dinfo->depth != 8) {
                fgcolor = dinfo->pseudo_palette[image->fg_color];
@@ -1472,26 +1569,30 @@ intelfb_imageblit(struct fb_info *info, const struct fb_image *image)
        if (!intelfbhw_do_drawglyph(dinfo, fgcolor, bgcolor, image->width,
                                    image->height, image->data,
                                    image->dx, image->dy,
-                                   dinfo->pitch, info->var.bits_per_pixel))
-               return cfb_imageblit(info, image);
+                                   dinfo->pitch, info->var.bits_per_pixel)) {
+               cfb_imageblit(info, image);
+               return;
+       }
 }
 
-static int
-intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
+static int intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
 {
         struct intelfb_info *dinfo = GET_DINFO(info);
-
+       u32 physical;
 #if VERBOSE > 0
        DBG_MSG("intelfb_cursor\n");
 #endif
 
        if (!dinfo->hwcursor)
-               return soft_cursor(info, cursor);
+               return -ENODEV;
 
        intelfbhw_cursor_hide(dinfo);
 
        /* If XFree killed the cursor - restore it */
-       if (INREG(CURSOR_A_BASEADDR) != dinfo->cursor.offset << 12) {
+       physical = (dinfo->mobile || IS_I9XX(dinfo)) ? dinfo->cursor.physical :
+                  (dinfo->cursor.offset << 12);
+
+       if (INREG(CURSOR_A_BASEADDR) != physical) {
                u32 fg, bg;
 
                DBG_MSG("the cursor was killed - restore it !!\n");
@@ -1588,8 +1689,7 @@ intelfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
        return 0;
 }
 
-static int
-intelfb_sync(struct fb_info *info)
+static int intelfb_sync(struct fb_info *info)
 {
         struct intelfb_info *dinfo = GET_DINFO(info);