fbdev: move FBIO_WAITFORVSYNC to linux/fb.h
[safe/jmp/linux-2.6] / drivers / video / intelfb / intelfbdrv.c
index 9e83664..5ba3999 100644 (file)
@@ -2,9 +2,9 @@
  * intelfb
  *
  * Linux framebuffer driver for Intel(R) 830M/845G/852GM/855GM/865G/915G/915GM/
- * 945G/945GM integrated graphics chips.
+ * 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
  *
  *              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/pci.h>
 #include <linux/vmalloc.h>
 #include <linux/pagemap.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_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);
@@ -181,10 +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, }
 };
 
@@ -194,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,
@@ -218,8 +225,8 @@ 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);
 
@@ -264,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;
@@ -286,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;
@@ -299,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;
@@ -320,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;
 
@@ -351,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))
                        ;
@@ -372,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;
@@ -397,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);
@@ -424,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
@@ -438,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);
 
@@ -467,6 +471,11 @@ 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)
@@ -488,8 +497,8 @@ 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;
@@ -499,8 +508,8 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        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");
@@ -529,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 */
@@ -551,7 +559,11 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        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_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;
        }
@@ -627,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))) {
@@ -652,18 +663,15 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
                offset = ROUND_UP_TO_PAGE(MB(voffset))/GTT_PAGE_SIZE;
 
        /* set the mem offsets - set them after the already used pages */
-       if (dinfo->accel) {
+       if (dinfo->accel)
                dinfo->ring.offset = offset + gtt_info.current_memory;
-       }
-       if (dinfo->hwcursor) {
+       if (dinfo->hwcursor)
                dinfo->cursor.offset = offset +
                        + gtt_info.current_memory + (dinfo->ring.size >> 12);
-       }
-       if (dinfo->fbmem_gart) {
+       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 */
@@ -679,7 +687,7 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
 
        dinfo->mmio_base =
                (u8 __iomem *)ioremap_nocache(dinfo->mmio_base_phys,
-                                              INTEL_REG_SIZE);
+                                             INTEL_REG_SIZE);
        if (!dinfo->mmio_base) {
                ERR_MSG("Cannot remap MMIO region.\n");
                cleanup(dinfo);
@@ -706,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
@@ -765,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, "
@@ -810,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;
@@ -821,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);
@@ -844,6 +851,11 @@ intelfb_pci_register(struct pci_dev *pdev, const struct pci_device_id *ent)
        if (bailearly == 5)
                bailout(dinfo);
 
+#ifdef CONFIG_FB_INTEL_I2C
+       /* register I2C bus */
+       intelfb_create_i2c_busses(dinfo);
+#endif
+
        if (bailearly == 6)
                bailout(dinfo);
 
@@ -862,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);
@@ -888,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;
 
@@ -917,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);
@@ -934,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;
@@ -949,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;
@@ -1017,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;
@@ -1031,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) {
@@ -1074,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;
 
@@ -1111,8 +1153,8 @@ intelfb_set_fbinfo(struct intelfb_info *dinfo)
 }
 
 /* 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");
 
@@ -1141,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;
@@ -1157,33 +1202,74 @@ update_dinfo(struct intelfb_info *dinfo, struct fb_var_screeninfo *var)
  *                       fbdev interface                       *
  ***************************************************************/
 
-static int
-intelfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
+static int intelfb_open(struct fb_info *info, int user)
+{
+       struct intelfb_info *dinfo = GET_DINFO(info);
+
+       if (user)
+               dinfo->open++;
+
+       return 0;
+}
+
+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)
 {
        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) ||
@@ -1265,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)) {
@@ -1276,27 +1361,32 @@ 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 (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);
@@ -1312,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:
@@ -1322,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);
 
@@ -1367,31 +1457,43 @@ intelfb_setcolreg(unsigned regno, unsigned red, unsigned green,
        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 fb_info *info, unsigned int cmd, unsigned long arg)
+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;
@@ -1400,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)
@@ -1419,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);
 
@@ -1428,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;
@@ -1447,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];
@@ -1461,12 +1569,13 @@ 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;
@@ -1580,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);