include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / video / fbmon.c
index fc7965b..563a98b 100644 (file)
@@ -4,7 +4,7 @@
  * Copyright (C) 2002 James Simmons <jsimmons@users.sf.net>
  *
  * Credits:
- * 
+ *
  * The EDID Parser is a conglomeration from the following sources:
  *
  *   1. SciTech SNAP Graphics Architecture
  *
  *   2. XFree86 4.3.0, interpret_edid.c
  *      Copyright 1998 by Egbert Eich <Egbert.Eich@Physik.TU-Darmstadt.DE>
- * 
- *   3. John Fremlin <vii@users.sourceforge.net> and 
+ *
+ *   3. John Fremlin <vii@users.sourceforge.net> and
  *      Ani Joshi <ajoshi@unixbox.com>
- *  
+ *
  * Generalized Timing Formula is derived from:
  *
- *      GTF Spreadsheet by Andy Morrish (1/5/97) 
+ *      GTF Spreadsheet by Andy Morrish (1/5/97)
  *      available at http://www.vesa.org
  *
  * This file is subject to the terms and conditions of the GNU General Public
  * for more details.
  *
  */
-#include <linux/tty.h>
 #include <linux/fb.h>
 #include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/slab.h>
 #include <video/edid.h>
 #ifdef CONFIG_PPC_OF
-#include <linux/pci.h>
 #include <asm/prom.h>
 #include <asm/pci-bridge.h>
 #endif
 #include "edid.h"
 
-/* 
+/*
  * EDID parser
  */
 
@@ -49,8 +49,9 @@
 #define DPRINTK(fmt, args...)
 #endif
 
-#define FBMON_FIX_HEADER 1
-#define FBMON_FIX_INPUT  2
+#define FBMON_FIX_HEADER  1
+#define FBMON_FIX_INPUT   2
+#define FBMON_FIX_TIMINGS 3
 
 #ifdef CONFIG_FB_MODE_HELPERS
 struct broken_edid {
@@ -59,7 +60,7 @@ struct broken_edid {
        u32 fix;
 };
 
-static struct broken_edid brokendb[] = {
+static const struct broken_edid brokendb[] = {
        /* DEC FR-PCXAV-YZ */
        {
                .manufacturer = "DEC",
@@ -72,6 +73,12 @@ static struct broken_edid brokendb[] = {
                .model        = 0x5a44,
                .fix          = FBMON_FIX_INPUT,
        },
+       /* Sharp UXGA? */
+       {
+               .manufacturer = "SHP",
+               .model        = 0x138e,
+               .fix          = FBMON_FIX_TIMINGS,
+       },
 };
 
 static const unsigned char edid_v1_header[] = { 0x00, 0xff, 0xff, 0xff,
@@ -88,6 +95,55 @@ static void copy_string(unsigned char *c, unsigned char *s)
   while (i-- && (*--s == 0x20)) *s = 0;
 }
 
+static int edid_is_serial_block(unsigned char *block)
+{
+       if ((block[0] == 0x00) && (block[1] == 0x00) &&
+           (block[2] == 0x00) && (block[3] == 0xff) &&
+           (block[4] == 0x00))
+               return 1;
+       else
+               return 0;
+}
+
+static int edid_is_ascii_block(unsigned char *block)
+{
+       if ((block[0] == 0x00) && (block[1] == 0x00) &&
+           (block[2] == 0x00) && (block[3] == 0xfe) &&
+           (block[4] == 0x00))
+               return 1;
+       else
+               return 0;
+}
+
+static int edid_is_limits_block(unsigned char *block)
+{
+       if ((block[0] == 0x00) && (block[1] == 0x00) &&
+           (block[2] == 0x00) && (block[3] == 0xfd) &&
+           (block[4] == 0x00))
+               return 1;
+       else
+               return 0;
+}
+
+static int edid_is_monitor_block(unsigned char *block)
+{
+       if ((block[0] == 0x00) && (block[1] == 0x00) &&
+           (block[2] == 0x00) && (block[3] == 0xfc) &&
+           (block[4] == 0x00))
+               return 1;
+       else
+               return 0;
+}
+
+static int edid_is_timing_block(unsigned char *block)
+{
+       if ((block[0] != 0x00) || (block[1] != 0x00) ||
+           (block[2] != 0x00) || (block[4] != 0x00))
+               return 1;
+       else
+               return 0;
+}
+
 static int check_edid(unsigned char *edid)
 {
        unsigned char *block = edid + ID_MANUFACTURER_NAME, manufacturer[4];
@@ -105,19 +161,18 @@ static int check_edid(unsigned char *edid)
        for (i = 0; i < ARRAY_SIZE(brokendb); i++) {
                if (!strncmp(manufacturer, brokendb[i].manufacturer, 4) &&
                        brokendb[i].model == model) {
-                       printk("fbmon: The EDID Block of "
-                              "Manufacturer: %s Model: 0x%x is known to "
-                              "be broken,\n",  manufacturer, model);
-                       fix = brokendb[i].fix;
-                       break;
+                       fix = brokendb[i].fix;
+                       break;
                }
        }
 
        switch (fix) {
        case FBMON_FIX_HEADER:
                for (i = 0; i < 8; i++) {
-                       if (edid[i] != edid_v1_header[i])
+                       if (edid[i] != edid_v1_header[i]) {
                                ret = fix;
+                               break;
+                       }
                }
                break;
        case FBMON_FIX_INPUT:
@@ -127,14 +182,34 @@ static int check_edid(unsigned char *edid)
                if (b[4] & 0x01 && b[0] & 0x80)
                        ret = fix;
                break;
+       case FBMON_FIX_TIMINGS:
+               b = edid + DETAILED_TIMING_DESCRIPTIONS_START;
+               ret = fix;
+
+               for (i = 0; i < 4; i++) {
+                       if (edid_is_limits_block(b)) {
+                               ret = 0;
+                               break;
+                       }
+
+                       b += DETAILED_TIMING_DESCRIPTION_SIZE;
+               }
+
+               break;
        }
 
+       if (ret)
+               printk("fbmon: The EDID Block of "
+                      "Manufacturer: %s Model: 0x%x is known to "
+                      "be broken,\n",  manufacturer, model);
+
        return ret;
 }
 
 static void fix_edid(unsigned char *edid, int fix)
 {
-       unsigned char *b;
+       int i;
+       unsigned char *b, csum = 0;
 
        switch (fix) {
        case FBMON_FIX_HEADER:
@@ -146,13 +221,44 @@ static void fix_edid(unsigned char *edid, int fix)
                b = edid + EDID_STRUCT_DISPLAY;
                b[0] &= ~0x80;
                edid[127] += 0x80;
+               break;
+       case FBMON_FIX_TIMINGS:
+               printk("fbmon: trying to fix monitor timings\n");
+               b = edid + DETAILED_TIMING_DESCRIPTIONS_START;
+               for (i = 0; i < 4; i++) {
+                       if (!(edid_is_serial_block(b) ||
+                             edid_is_ascii_block(b) ||
+                             edid_is_monitor_block(b) ||
+                             edid_is_timing_block(b))) {
+                               b[0] = 0x00;
+                               b[1] = 0x00;
+                               b[2] = 0x00;
+                               b[3] = 0xfd;
+                               b[4] = 0x00;
+                               b[5] = 60;   /* vfmin */
+                               b[6] = 60;   /* vfmax */
+                               b[7] = 30;   /* hfmin */
+                               b[8] = 75;   /* hfmax */
+                               b[9] = 17;   /* pixclock - 170 MHz*/
+                               b[10] = 0;   /* GTF */
+                               break;
+                       }
+
+                       b += DETAILED_TIMING_DESCRIPTION_SIZE;
+               }
+
+               for (i = 0; i < EDID_LENGTH - 1; i++)
+                       csum += edid[i];
+
+               edid[127] = 256 - csum;
+               break;
        }
 }
 
 static int edid_checksum(unsigned char *edid)
 {
-       unsigned char i, csum = 0, all_null = 0;
-       int err = 0, fix = check_edid(edid);
+       unsigned char csum = 0, all_null = 0;
+       int i, err = 0, fix = check_edid(edid);
 
        if (fix)
                fix_edid(edid, fix);
@@ -218,7 +324,7 @@ static void get_dpms_capabilities(unsigned char flags,
               (flags & DPMS_SUSPEND)    ? "yes" : "no",
               (flags & DPMS_STANDBY)    ? "yes" : "no");
 }
-       
+
 static void get_chroma(unsigned char *block, struct fb_monspecs *specs)
 {
        int tmp;
@@ -260,7 +366,7 @@ static void get_chroma(unsigned char *block, struct fb_monspecs *specs)
        tmp += 512;
        specs->chroma.bluey = tmp/1024;
        DPRINTK("BlueY:    0.%03d\n", specs->chroma.bluey);
-       
+
        tmp = ((block[6] & (3 << 2)) >> 2) | (block[0xd] << 2);
        tmp *= 1000;
        tmp += 512;
@@ -274,69 +380,32 @@ static void get_chroma(unsigned char *block, struct fb_monspecs *specs)
        DPRINTK("WhiteY:   0.%03d\n", specs->chroma.whitey);
 }
 
-static int edid_is_serial_block(unsigned char *block)
-{
-       if ((block[0] == 0x00) && (block[1] == 0x00) && 
-           (block[2] == 0x00) && (block[3] == 0xff) &&
-           (block[4] == 0x00))
-               return 1;
-       else
-               return 0;
-}
-
-static int edid_is_ascii_block(unsigned char *block)
-{
-       if ((block[0] == 0x00) && (block[1] == 0x00) && 
-           (block[2] == 0x00) && (block[3] == 0xfe) &&
-           (block[4] == 0x00))
-               return 1;
-       else
-               return 0;
-}
-
-static int edid_is_limits_block(unsigned char *block)
-{
-       if ((block[0] == 0x00) && (block[1] == 0x00) && 
-           (block[2] == 0x00) && (block[3] == 0xfd) &&
-           (block[4] == 0x00))
-               return 1;
-       else
-               return 0;
-}
-
-static int edid_is_monitor_block(unsigned char *block)
-{
-       if ((block[0] == 0x00) && (block[1] == 0x00) && 
-           (block[2] == 0x00) && (block[3] == 0xfc) &&
-           (block[4] == 0x00))
-               return 1;
-       else
-               return 0;
-}
-
 static void calc_mode_timings(int xres, int yres, int refresh,
                              struct fb_videomode *mode)
 {
-       struct fb_var_screeninfo var;
-       struct fb_info info;
-       
-       memset(&var, 0, sizeof(struct fb_var_screeninfo));
-       var.xres = xres;
-       var.yres = yres;
-       fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON, 
-                   refresh, &var, &info);
-       mode->xres = xres;
-       mode->yres = yres;
-       mode->pixclock = var.pixclock;
-       mode->refresh = refresh;
-       mode->left_margin = var.left_margin;
-       mode->right_margin = var.right_margin;
-       mode->upper_margin = var.upper_margin;
-       mode->lower_margin = var.lower_margin;
-       mode->hsync_len = var.hsync_len;
-       mode->vsync_len = var.vsync_len;
-       mode->vmode = 0;
-       mode->sync = 0;
+       struct fb_var_screeninfo *var;
+
+       var = kzalloc(sizeof(struct fb_var_screeninfo), GFP_KERNEL);
+
+       if (var) {
+               var->xres = xres;
+               var->yres = yres;
+               fb_get_mode(FB_VSYNCTIMINGS | FB_IGNOREMON,
+                           refresh, var, NULL);
+               mode->xres = xres;
+               mode->yres = yres;
+               mode->pixclock = var->pixclock;
+               mode->refresh = refresh;
+               mode->left_margin = var->left_margin;
+               mode->right_margin = var->right_margin;
+               mode->upper_margin = var->upper_margin;
+               mode->lower_margin = var->lower_margin;
+               mode->hsync_len = var->hsync_len;
+               mode->vsync_len = var->vsync_len;
+               mode->vmode = 0;
+               mode->sync = 0;
+               kfree(var);
+       }
 }
 
 static int get_est_timing(unsigned char *block, struct fb_videomode *mode)
@@ -383,11 +452,11 @@ static int get_est_timing(unsigned char *block, struct fb_videomode *mode)
 
        c = block[1];
        if (c&0x80) {
-               mode[num++] = vesa_modes[9];
+               mode[num++] = vesa_modes[9];
                DPRINTK("      800x600@72Hz\n");
        }
        if (c&0x40) {
-               mode[num++] = vesa_modes[10];
+               mode[num++] = vesa_modes[10];
                DPRINTK("      800x600@75Hz\n");
        }
        if (c&0x20) {
@@ -427,7 +496,7 @@ static int get_est_timing(unsigned char *block, struct fb_videomode *mode)
 static int get_std_timing(unsigned char *block, struct fb_videomode *mode)
 {
        int xres, yres = 0, refresh, ratio, i;
-       
+
        xres = (block[0] + 31) * 8;
        if (xres <= 256)
                return 0;
@@ -451,7 +520,7 @@ static int get_std_timing(unsigned char *block, struct fb_videomode *mode)
 
        DPRINTK("      %dx%d@%dHz\n", xres, yres, refresh);
        for (i = 0; i < VESA_MODEDB_SIZE; i++) {
-               if (vesa_modes[i].xres == xres && 
+               if (vesa_modes[i].xres == xres &&
                    vesa_modes[i].yres == yres &&
                    vesa_modes[i].refresh == refresh) {
                        *mode = vesa_modes[i];
@@ -468,13 +537,13 @@ static int get_dst_timing(unsigned char *block,
 {
        int j, num = 0;
 
-       for (j = 0; j < 6; j++, block+= STD_TIMING_DESCRIPTION_SIZE) 
+       for (j = 0; j < 6; j++, block += STD_TIMING_DESCRIPTION_SIZE)
                num += get_std_timing(block, &mode[num]);
 
        return num;
 }
 
-static void get_detailed_timing(unsigned char *block, 
+static void get_detailed_timing(unsigned char *block,
                                struct fb_videomode *mode)
 {
        mode->xres = H_ACTIVE;
@@ -485,7 +554,7 @@ static void get_detailed_timing(unsigned char *block,
        mode->right_margin = H_SYNC_OFFSET;
        mode->left_margin = (H_ACTIVE + H_BLANKING) -
                (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
-       mode->upper_margin = V_BLANKING - V_SYNC_OFFSET - 
+       mode->upper_margin = V_BLANKING - V_SYNC_OFFSET -
                V_SYNC_WIDTH;
        mode->lower_margin = V_SYNC_OFFSET;
        mode->hsync_len = H_SYNC_WIDTH;
@@ -496,7 +565,13 @@ static void get_detailed_timing(unsigned char *block,
                mode->sync |= FB_SYNC_VERT_HIGH_ACT;
        mode->refresh = PIXEL_CLOCK/((H_ACTIVE + H_BLANKING) *
                                     (V_ACTIVE + V_BLANKING));
-       mode->vmode = 0;
+       if (INTERLACED) {
+               mode->yres *= 2;
+               mode->upper_margin *= 2;
+               mode->lower_margin *= 2;
+               mode->vsync_len *= 2;
+               mode->vmode |= FB_VMODE_INTERLACED;
+       }
        mode->flag = FB_MODE_IS_DETAILED;
 
        DPRINTK("      %d MHz ",  PIXEL_CLOCK/1000000);
@@ -523,14 +598,13 @@ static struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize)
 {
        struct fb_videomode *mode, *m;
        unsigned char *block;
-       int num = 0, i;
+       int num = 0, i, first = 1;
 
-       mode = kmalloc(50 * sizeof(struct fb_videomode), GFP_KERNEL);
+       mode = kzalloc(50 * sizeof(struct fb_videomode), GFP_KERNEL);
        if (mode == NULL)
                return NULL;
-       memset(mode, 0, 50 * sizeof(struct fb_videomode));
 
-       if (edid == NULL || !edid_checksum(edid) || 
+       if (edid == NULL || !edid_checksum(edid) ||
            !edid_check_header(edid)) {
                kfree(mode);
                return NULL;
@@ -541,8 +615,6 @@ static struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize)
        DPRINTK("   Detailed Timings\n");
        block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
        for (i = 0; i < 4; i++, block+= DETAILED_TIMING_DESCRIPTION_SIZE) {
-               int first = 1;
-
                if (!(block[0] == 0x00 && block[1] == 0x00)) {
                        get_detailed_timing(block, &mode[num]);
                        if (first) {
@@ -567,7 +639,7 @@ static struct fb_videomode *fb_create_modedb(unsigned char *edid, int *dbsize)
                if (block[0] == 0x00 && block[1] == 0x00 && block[3] == 0xfa)
                        num += get_dst_timing(block + 5, &mode[num]);
        }
-       
+
        /* Yikes, EDID data is totally useless */
        if (!num) {
                kfree(mode);
@@ -603,6 +675,7 @@ static int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs)
        block = edid + DETAILED_TIMING_DESCRIPTIONS_START;
 
        DPRINTK("      Monitor Operating Limits: ");
+
        for (i = 0; i < 4; i++, block += DETAILED_TIMING_DESCRIPTION_SIZE) {
                if (edid_is_limits_block(block)) {
                        specs->hfmin = H_MIN_RATE * 1000;
@@ -616,11 +689,12 @@ static int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs)
                        break;
                }
        }
-       
+
        /* estimate monitor limits based on modes supported */
        if (retval) {
-               struct fb_videomode *modes;
-               int num_modes, i, hz, hscan, pixclock;
+               struct fb_videomode *modes, *mode;
+               int num_modes, hz, hscan, pixclock;
+               int vtotal, htotal;
 
                modes = fb_create_modedb(edid, &num_modes);
                if (!modes) {
@@ -630,20 +704,38 @@ static int fb_get_monitor_limits(unsigned char *edid, struct fb_monspecs *specs)
 
                retval = 0;
                for (i = 0; i < num_modes; i++) {
-                       hz = modes[i].refresh;
+                       mode = &modes[i];
                        pixclock = PICOS2KHZ(modes[i].pixclock) * 1000;
-                       hscan = (modes[i].yres * 105 * hz + 5000)/100;
-                       
+                       htotal = mode->xres + mode->right_margin + mode->hsync_len
+                               + mode->left_margin;
+                       vtotal = mode->yres + mode->lower_margin + mode->vsync_len
+                               + mode->upper_margin;
+
+                       if (mode->vmode & FB_VMODE_INTERLACED)
+                               vtotal /= 2;
+
+                       if (mode->vmode & FB_VMODE_DOUBLE)
+                               vtotal *= 2;
+
+                       hscan = (pixclock + htotal / 2) / htotal;
+                       hscan = (hscan + 500) / 1000 * 1000;
+                       hz = (hscan + vtotal / 2) / vtotal;
+
                        if (specs->dclkmax == 0 || specs->dclkmax < pixclock)
                                specs->dclkmax = pixclock;
+
                        if (specs->dclkmin == 0 || specs->dclkmin > pixclock)
                                specs->dclkmin = pixclock;
+
                        if (specs->hfmax == 0 || specs->hfmax < hscan)
                                specs->hfmax = hscan;
+
                        if (specs->hfmin == 0 || specs->hfmin > hscan)
                                specs->hfmin = hscan;
+
                        if (specs->vfmax == 0 || specs->vfmax < hz)
                                specs->vfmax = hz;
+
                        if (specs->vfmin == 0 || specs->vfmin > hz)
                                specs->vfmin = hz;
                }
@@ -774,15 +866,6 @@ static void get_monspecs(unsigned char *edid, struct fb_monspecs *specs)
        }
 }
 
-static int edid_is_timing_block(unsigned char *block)
-{
-       if ((block[0] != 0x00) || (block[1] != 0x00) ||
-           (block[2] != 0x00) || (block[4] != 0x00))
-               return 1;
-       else
-               return 0;
-}
-
 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
 {
        int i;
@@ -803,7 +886,7 @@ int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
                if (edid_is_timing_block(block)) {
                        var->xres = var->xres_virtual = H_ACTIVE;
                        var->yres = var->yres_virtual = V_ACTIVE;
-                       var->height = var->width = -1;
+                       var->height = var->width = 0;
                        var->right_margin = H_SYNC_OFFSET;
                        var->left_margin = (H_ACTIVE + H_BLANKING) -
                                (H_ACTIVE + H_SYNC_OFFSET + H_SYNC_WIDTH);
@@ -890,8 +973,8 @@ void fb_edid_to_monspecs(unsigned char *edid, struct fb_monspecs *specs)
        DPRINTK("========================================\n");
 }
 
-/* 
- * VESA Generalized Timing Formula (GTF) 
+/*
+ * VESA Generalized Timing Formula (GTF)
  */
 
 #define FLYBACK                     550
@@ -920,7 +1003,7 @@ struct __fb_timings {
  * @hfreq: horizontal freq
  *
  * DESCRIPTION:
- * vblank = right_margin + vsync_len + left_margin 
+ * vblank = right_margin + vsync_len + left_margin
  *
  *    given: right_margin = 1 (V_FRONTPORCH)
  *           vsync_len    = 3
@@ -934,12 +1017,12 @@ static u32 fb_get_vblank(u32 hfreq)
 {
        u32 vblank;
 
-       vblank = (hfreq * FLYBACK)/1000; 
+       vblank = (hfreq * FLYBACK)/1000;
        vblank = (vblank + 500)/1000;
        return (vblank + V_FRONTPORCH);
 }
 
-/** 
+/**
  * fb_get_hblank_by_freq - get horizontal blank time given hfreq
  * @hfreq: horizontal freq
  * @xres: horizontal resolution in pixels
@@ -955,7 +1038,7 @@ static u32 fb_get_vblank(u32 hfreq)
  *
  * where: C = ((offset - scale factor) * blank_scale)
  *            -------------------------------------- + scale factor
- *                        256 
+ *                        256
  *        M = blank_scale * gradient
  *
  */
@@ -963,7 +1046,7 @@ static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres)
 {
        u32 c_val, m_val, duty_cycle, hblank;
 
-       c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 + 
+       c_val = (((H_OFFSET - H_SCALEFACTOR) * H_BLANKSCALE)/256 +
                 H_SCALEFACTOR) * 1000;
        m_val = (H_BLANKSCALE * H_GRADIENT)/256;
        m_val = (m_val * 1000000)/hfreq;
@@ -972,7 +1055,7 @@ static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres)
        return (hblank);
 }
 
-/** 
+/**
  * fb_get_hblank_by_dclk - get horizontal blank time given pixelclock
  * @dclk: pixelclock in Hz
  * @xres: horizontal resolution in pixels
@@ -985,7 +1068,7 @@ static u32 fb_get_hblank_by_hfreq(u32 hfreq, u32 xres)
  *
  * duty cycle = percent of htotal assigned to inactive display
  * duty cycle = C - (M * h_period)
- * 
+ *
  * where: h_period = SQRT(100 - C + (0.4 * xres * M)/dclk) + C - 100
  *                   -----------------------------------------------
  *                                    2 * M
@@ -1001,11 +1084,11 @@ static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres)
        h_period = 100 - C_VAL;
        h_period *= h_period;
        h_period += (M_VAL * xres * 2 * 1000)/(5 * dclk);
-       h_period *=10000; 
+       h_period *= 10000;
 
        h_period = int_sqrt(h_period);
        h_period -= (100 - C_VAL) * 100;
-       h_period *= 1000; 
+       h_period *= 1000;
        h_period /= 2 * M_VAL;
 
        duty_cycle = C_VAL * 1000 - (M_VAL * h_period)/100;
@@ -1013,7 +1096,7 @@ static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres)
        hblank &= ~15;
        return (hblank);
 }
-       
+
 /**
  * fb_get_hfreq - estimate hsync
  * @vfreq: vertical refresh rate
@@ -1024,13 +1107,13 @@ static u32 fb_get_hblank_by_dclk(u32 dclk, u32 xres)
  *          (yres + front_port) * vfreq * 1000000
  * hfreq = -------------------------------------
  *          (1000000 - (vfreq * FLYBACK)
- * 
+ *
  */
 
 static u32 fb_get_hfreq(u32 vfreq, u32 yres)
 {
        u32 divisor, hfreq;
-       
+
        divisor = (1000000 - (vfreq * FLYBACK))/1000;
        hfreq = (yres + V_FRONTPORCH) * vfreq  * 1000;
        return (hfreq/divisor);
@@ -1041,7 +1124,7 @@ static void fb_timings_vfreq(struct __fb_timings *timings)
        timings->hfreq = fb_get_hfreq(timings->vfreq, timings->vactive);
        timings->vblank = fb_get_vblank(timings->hfreq);
        timings->vtotal = timings->vactive + timings->vblank;
-       timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq, 
+       timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
                                                 timings->hactive);
        timings->htotal = timings->hactive + timings->hblank;
        timings->dclk = timings->htotal * timings->hfreq;
@@ -1052,7 +1135,7 @@ static void fb_timings_hfreq(struct __fb_timings *timings)
        timings->vblank = fb_get_vblank(timings->hfreq);
        timings->vtotal = timings->vactive + timings->vblank;
        timings->vfreq = timings->hfreq/timings->vtotal;
-       timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq, 
+       timings->hblank = fb_get_hblank_by_hfreq(timings->hfreq,
                                                 timings->hactive);
        timings->htotal = timings->hactive + timings->hblank;
        timings->dclk = timings->htotal * timings->hfreq;
@@ -1060,7 +1143,7 @@ static void fb_timings_hfreq(struct __fb_timings *timings)
 
 static void fb_timings_dclk(struct __fb_timings *timings)
 {
-       timings->hblank = fb_get_hblank_by_dclk(timings->dclk, 
+       timings->hblank = fb_get_hblank_by_dclk(timings->dclk,
                                                timings->hactive);
        timings->htotal = timings->hactive + timings->hblank;
        timings->hfreq = timings->dclk/timings->htotal;
@@ -1080,40 +1163,46 @@ static void fb_timings_dclk(struct __fb_timings *timings)
  * @info: pointer to fb_info
  *
  * DESCRIPTION:
- * Calculates video mode based on monitor specs using VESA GTF. 
- * The GTF is best for VESA GTF compliant monitors but is 
+ * Calculates video mode based on monitor specs using VESA GTF.
+ * The GTF is best for VESA GTF compliant monitors but is
  * specifically formulated to work for older monitors as well.
  *
- * If @flag==0, the function will attempt to maximize the 
+ * If @flag==0, the function will attempt to maximize the
  * refresh rate.  Otherwise, it will calculate timings based on
- * the flag and accompanying value.  
+ * the flag and accompanying value.
  *
- * If FB_IGNOREMON bit is set in @flags, monitor specs will be 
+ * If FB_IGNOREMON bit is set in @flags, monitor specs will be
  * ignored and @var will be filled with the calculated timings.
  *
  * All calculations are based on the VESA GTF Spreadsheet
  * available at VESA's public ftp (http://www.vesa.org).
- * 
+ *
  * NOTES:
  * The timings generated by the GTF will be different from VESA
  * DMT.  It might be a good idea to keep a table of standard
  * VESA modes as well.  The GTF may also not work for some displays,
  * such as, and especially, analog TV.
- *   
+ *
  * REQUIRES:
  * A valid info->monspecs, otherwise 'safe numbers' will be used.
- */ 
+ */
 int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, struct fb_info *info)
 {
-       struct __fb_timings timings;
+       struct __fb_timings *timings;
        u32 interlace = 1, dscan = 1;
-       u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
+       u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax, err = 0;
 
-       /* 
+
+       timings = kzalloc(sizeof(struct __fb_timings), GFP_KERNEL);
+
+       if (!timings)
+               return -ENOMEM;
+
+       /*
         * If monspecs are invalid, use values that are enough
         * for 640x480@60
         */
-       if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
+       if (!info || !info->monspecs.hfmax || !info->monspecs.vfmax ||
            !info->monspecs.dclkmax ||
            info->monspecs.hfmax < info->monspecs.hfmin ||
            info->monspecs.vfmax < info->monspecs.vfmin ||
@@ -1130,65 +1219,66 @@ int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var, struct fb_inf
                dclkmax = info->monspecs.dclkmax;
        }
 
-       memset(&timings, 0, sizeof(struct __fb_timings));
-       timings.hactive = var->xres;
-       timings.vactive = var->yres;
-       if (var->vmode & FB_VMODE_INTERLACED) { 
-               timings.vactive /= 2;
+       timings->hactive = var->xres;
+       timings->vactive = var->yres;
+       if (var->vmode & FB_VMODE_INTERLACED) {
+               timings->vactive /= 2;
                interlace = 2;
        }
        if (var->vmode & FB_VMODE_DOUBLE) {
-               timings.vactive *= 2;
+               timings->vactive *= 2;
                dscan = 2;
        }
 
        switch (flags & ~FB_IGNOREMON) {
        case FB_MAXTIMINGS: /* maximize refresh rate */
-               timings.hfreq = hfmax;
-               fb_timings_hfreq(&timings);
-               if (timings.vfreq > vfmax) {
-                       timings.vfreq = vfmax;
-                       fb_timings_vfreq(&timings);
+               timings->hfreq = hfmax;
+               fb_timings_hfreq(timings);
+               if (timings->vfreq > vfmax) {
+                       timings->vfreq = vfmax;
+                       fb_timings_vfreq(timings);
                }
-               if (timings.dclk > dclkmax) {
-                       timings.dclk = dclkmax;
-                       fb_timings_dclk(&timings);
+               if (timings->dclk > dclkmax) {
+                       timings->dclk = dclkmax;
+                       fb_timings_dclk(timings);
                }
                break;
        case FB_VSYNCTIMINGS: /* vrefresh driven */
-               timings.vfreq = val;
-               fb_timings_vfreq(&timings);
+               timings->vfreq = val;
+               fb_timings_vfreq(timings);
                break;
        case FB_HSYNCTIMINGS: /* hsync driven */
-               timings.hfreq = val;
-               fb_timings_hfreq(&timings);
+               timings->hfreq = val;
+               fb_timings_hfreq(timings);
                break;
        case FB_DCLKTIMINGS: /* pixelclock driven */
-               timings.dclk = PICOS2KHZ(val) * 1000;
-               fb_timings_dclk(&timings);
+               timings->dclk = PICOS2KHZ(val) * 1000;
+               fb_timings_dclk(timings);
                break;
        default:
-               return -EINVAL;
-               
-       } 
-       
-       if (!(flags & FB_IGNOREMON) && 
-           (timings.vfreq < vfmin || timings.vfreq > vfmax || 
-            timings.hfreq < hfmin || timings.hfreq > hfmax ||
-            timings.dclk < dclkmin || timings.dclk > dclkmax))
-               return -EINVAL;
+               err = -EINVAL;
+
+       }
+
+       if (err || (!(flags & FB_IGNOREMON) &&
+           (timings->vfreq < vfmin || timings->vfreq > vfmax ||
+            timings->hfreq < hfmin || timings->hfreq > hfmax ||
+            timings->dclk < dclkmin || timings->dclk > dclkmax))) {
+               err = -EINVAL;
+       } else {
+               var->pixclock = KHZ2PICOS(timings->dclk/1000);
+               var->hsync_len = (timings->htotal * 8)/100;
+               var->right_margin = (timings->hblank/2) - var->hsync_len;
+               var->left_margin = timings->hblank - var->right_margin -
+                       var->hsync_len;
+               var->vsync_len = (3 * interlace)/dscan;
+               var->lower_margin = (1 * interlace)/dscan;
+               var->upper_margin = (timings->vblank * interlace)/dscan -
+                       (var->vsync_len + var->lower_margin);
+       }
 
-       var->pixclock = KHZ2PICOS(timings.dclk/1000);
-       var->hsync_len = (timings.htotal * 8)/100;
-       var->right_margin = (timings.hblank/2) - var->hsync_len;
-       var->left_margin = timings.hblank - var->right_margin - var->hsync_len;
-       
-       var->vsync_len = (3 * interlace)/dscan;
-       var->lower_margin = (1 * interlace)/dscan;
-       var->upper_margin = (timings.vblank * interlace)/dscan - 
-               (var->vsync_len + var->lower_margin);
-       
-       return 0;
+       kfree(timings);
+       return err;
 }
 #else
 int fb_parse_edid(unsigned char *edid, struct fb_var_screeninfo *var)
@@ -1208,7 +1298,7 @@ int fb_get_mode(int flags, u32 val, struct fb_var_screeninfo *var,
        return -EINVAL;
 }
 #endif /* CONFIG_FB_MODE_HELPERS */
-       
+
 /*
  * fb_validate_mode - validates var against monitor capabilities
  * @var: pointer to fb_var_screeninfo
@@ -1226,7 +1316,7 @@ int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info)
        u32 hfreq, vfreq, htotal, vtotal, pixclock;
        u32 hfmin, hfmax, vfmin, vfmax, dclkmin, dclkmax;
 
-       /* 
+       /*
         * If monspecs are invalid, use values that are enough
         * for 640x480@60
         */
@@ -1250,10 +1340,10 @@ int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info)
        if (!var->pixclock)
                return -EINVAL;
        pixclock = PICOS2KHZ(var->pixclock) * 1000;
-          
-       htotal = var->xres + var->right_margin + var->hsync_len + 
+
+       htotal = var->xres + var->right_margin + var->hsync_len +
                var->left_margin;
-       vtotal = var->yres + var->lower_margin + var->vsync_len + 
+       vtotal = var->yres + var->lower_margin + var->vsync_len +
                var->upper_margin;
 
        if (var->vmode & FB_VMODE_INTERLACED)
@@ -1266,14 +1356,13 @@ int fb_validate_mode(const struct fb_var_screeninfo *var, struct fb_info *info)
 
        vfreq = hfreq/vtotal;
 
-       return (vfreq < vfmin || vfreq > vfmax || 
+       return (vfreq < vfmin || vfreq > vfmax ||
                hfreq < hfmin || hfreq > hfmax ||
                pixclock < dclkmin || pixclock > dclkmax) ?
                -EINVAL : 0;
 }
 
-#if defined(__i386__)
-#include <linux/pci.h>
+#if defined(CONFIG_FIRMWARE_EDID) && defined(CONFIG_X86)
 
 /*
  * We need to ensure that the EDID block is only returned for
@@ -1302,11 +1391,11 @@ const unsigned char *fb_firmware_edid(struct device *device)
 {
        return NULL;
 }
-#endif /* _i386_ */
+#endif
+EXPORT_SYMBOL(fb_firmware_edid);
 
 EXPORT_SYMBOL(fb_parse_edid);
 EXPORT_SYMBOL(fb_edid_to_monspecs);
-EXPORT_SYMBOL(fb_firmware_edid);
 EXPORT_SYMBOL(fb_get_mode);
 EXPORT_SYMBOL(fb_validate_mode);
 EXPORT_SYMBOL(fb_destroy_modedb);