neofb: Fix pseudo_palette array overrun in neofb_setcolreg
[safe/jmp/linux-2.6] / drivers / video / neofb.c
index e18c9f9..731d7a5 100644 (file)
  *
  */
 
-#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>
@@ -68,7 +66,6 @@
 #include <linux/init.h>
 #ifdef CONFIG_TOSHIBA
 #include <linux/toshiba.h>
-extern int tosh_smm(SMMRegisters *regs);
 #endif
 
 #include <asm/io.h>
@@ -165,20 +162,20 @@ static int neoFindMode(int xres, int yres, int depth)
 
        switch (depth) {
        case 8:
-               size = sizeof(bios8) / sizeof(biosMode);
+               size = ARRAY_SIZE(bios8);
                mode = bios8;
                break;
        case 16:
-               size = sizeof(bios16) / sizeof(biosMode);
+               size = ARRAY_SIZE(bios16);
                mode = bios16;
                break;
        case 24:
-               size = sizeof(bios24) / sizeof(biosMode);
+               size = ARRAY_SIZE(bios24);
                mode = bios24;
                break;
 #ifdef NO_32BIT_SUPPORT_YET
        case 32:
-               size = sizeof(bios32) / sizeof(biosMode);
+               size = ARRAY_SIZE(bios32);
                mode = bios32;
                break;
 #endif
@@ -486,10 +483,8 @@ static void vgaHWRestore(const struct fb_info *info,
 static inline int neo2200_sync(struct fb_info *info)
 {
        struct neofb_par *par = info->par;
-       int waitcycles;
 
-       while (readl(&par->neo2200->bltStat) & 1)
-               waitcycles++;
+       while (readl(&par->neo2200->bltStat) & 1);
        return 0;
 }
 
@@ -561,14 +556,16 @@ static int
 neofb_open(struct fb_info *info, int user)
 {
        struct neofb_par *par = info->par;
-       int cnt = atomic_read(&par->ref_count);
 
-       if (!cnt) {
+       mutex_lock(&par->open_lock);
+       if (!par->ref_count) {
                memset(&par->state, 0, sizeof(struct vgastate));
                par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS;
                save_vga(&par->state);
        }
-       atomic_inc(&par->ref_count);
+       par->ref_count++;
+       mutex_unlock(&par->open_lock);
+
        return 0;
 }
 
@@ -576,14 +573,18 @@ static int
 neofb_release(struct fb_info *info, int user)
 {
        struct neofb_par *par = info->par;
-       int cnt = atomic_read(&par->ref_count);
 
-       if (!cnt)
+       mutex_lock(&par->open_lock);
+       if (!par->ref_count) {
+               mutex_unlock(&par->open_lock);
                return -EINVAL;
-       if (cnt == 1) {
+       }
+       if (par->ref_count == 1) {
                restore_vga(&par->state);
        }
-       atomic_dec(&par->ref_count);
+       par->ref_count--;
+       mutex_unlock(&par->open_lock);
+
        return 0;
 }
 
@@ -664,6 +665,7 @@ neofb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
        var->red.msb_right = 0;
        var->green.msb_right = 0;
        var->blue.msb_right = 0;
+       var->transp.msb_right = 0;
 
        switch (var->bits_per_pixel) {
        case 8:         /* PSEUDOCOLOUR, 256 */
@@ -843,6 +845,9 @@ static int neofb_set_par(struct fb_info *info)
 
        par->SysIfaceCntl2 = 0xc0;      /* VESA Bios sets this to 0x80! */
 
+       /* Initialize: by default, we want display config register to be read */
+       par->PanelDispCntlRegRead = 1;
+
        /* Enable any user specified display devices. */
        par->PanelDispCntlReg1 = 0x00;
        if (par->internal_display)
@@ -853,7 +858,7 @@ static int neofb_set_par(struct fb_info *info)
        /* If the user did not specify any display devices, then... */
        if (par->PanelDispCntlReg1 == 0x00) {
                /* Default to internal (i.e., LCD) only. */
-               par->PanelDispCntlReg1 |= 0x02;
+               par->PanelDispCntlReg1 = vga_rgfx(NULL, 0x20) & 0x03;
        }
 
        /* If we are using a fixed mode, then tell the chip we are. */
@@ -1281,34 +1286,36 @@ static int neofb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
        if (regno >= fb->cmap.len || regno > 255)
                return -EINVAL;
 
-       switch (fb->var.bits_per_pixel) {
-       case 8:
+       if (fb->var.bits_per_pixel <= 8) {
                outb(regno, 0x3c8);
 
                outb(red >> 10, 0x3c9);
                outb(green >> 10, 0x3c9);
                outb(blue >> 10, 0x3c9);
-               break;
-       case 16:
-               ((u32 *) fb->pseudo_palette)[regno] =
+       } else if (regno < 16) {
+               switch (fb->var.bits_per_pixel) {
+               case 16:
+                       ((u32 *) fb->pseudo_palette)[regno] =
                                ((red & 0xf800)) | ((green & 0xfc00) >> 5) |
                                ((blue & 0xf800) >> 11);
-               break;
-       case 24:
-               ((u32 *) fb->pseudo_palette)[regno] =
+                       break;
+               case 24:
+                       ((u32 *) fb->pseudo_palette)[regno] =
                                ((red & 0xff00) << 8) | ((green & 0xff00)) |
                                ((blue & 0xff00) >> 8);
-               break;
+                       break;
 #ifdef NO_32BIT_SUPPORT_YET
-       case 32:
-               ((u32 *) fb->pseudo_palette)[regno] =
+               case 32:
+                       ((u32 *) fb->pseudo_palette)[regno] =
                                ((transp & 0xff00) << 16) | ((red & 0xff00) << 8) |
                                ((green & 0xff00)) | ((blue & 0xff00) >> 8);
-               break;
+                       break;
 #endif
-       default:
-               return 1;
+               default:
+                       return 1;
+               }
        }
+
        return 0;
 }
 
@@ -1332,7 +1339,24 @@ static int neofb_blank(int blank_mode, struct fb_info *info)
         *  run "setterm -powersave powerdown" to take advantage
         */
        struct neofb_par *par = info->par;
-       int seqflags, lcdflags, dpmsflags, reg;
+       int seqflags, lcdflags, dpmsflags, reg, tmpdisp;
+
+       /*
+        * Read back the register bits related to display configuration. They might
+        * have been changed underneath the driver via Fn key stroke.
+        */
+       neoUnlock();
+       tmpdisp = vga_rgfx(NULL, 0x20) & 0x03;
+       neoLock(&par->state);
+
+       /* In case we blank the screen, we want to store the possibly new
+        * configuration in the driver. During un-blank, we re-apply this setting,
+        * since the LCD bit will be cleared in order to switch off the backlight.
+        */
+       if (par->PanelDispCntlRegRead) {
+               par->PanelDispCntlReg1 = tmpdisp;
+       }
+       par->PanelDispCntlRegRead = !blank_mode;
 
        switch (blank_mode) {
        case FB_BLANK_POWERDOWN:        /* powerdown - both sync lines down */
@@ -1365,12 +1389,21 @@ static int neofb_blank(int blank_mode, struct fb_info *info)
                break;
        case FB_BLANK_NORMAL:           /* just blank screen (backlight stays on) */
                seqflags = VGA_SR01_SCREEN_OFF; /* Disable sequencer */
-               lcdflags = par->PanelDispCntlReg1 & 0x02; /* LCD normal */
-               dpmsflags = 0;                  /* no hsync/vsync suppression */
+               /*
+                * During a blank operation with the LID shut, we might store "LCD off"
+                * by mistake. Due to timing issues, the BIOS may switch the lights
+                * back on, and we turn it back off once we "unblank".
+                *
+                * So here is an attempt to implement ">=" - if we are in the process
+                * of unblanking, and the LCD bit is unset in the driver but set in the
+                * register, we must keep it.
+                */
+               lcdflags = ((par->PanelDispCntlReg1 | tmpdisp) & 0x02); /* LCD normal */
+               dpmsflags = 0x00;       /* no hsync/vsync suppression */
                break;
        case FB_BLANK_UNBLANK:          /* unblank */
                seqflags = 0;                   /* Enable sequencer */
-               lcdflags = par->PanelDispCntlReg1 & 0x02; /* LCD normal */
+               lcdflags = ((par->PanelDispCntlReg1 | tmpdisp) & 0x02); /* LCD normal */
                dpmsflags = 0x00;       /* no hsync/vsync suppression */
 #ifdef CONFIG_TOSHIBA
                /* Do we still need this ? */
@@ -1907,7 +1940,7 @@ static int __devinit neo_init_hw(struct fb_info *info)
        printk(KERN_DEBUG "--- Neo extended register dump ---\n");
        for (int w = 0; w < 0x85; w++)
                printk(KERN_DEBUG "CR %p: %p\n", (void *) w,
-                      (void *) vga_rcrt(NULL, w);
+                      (void *) vga_rcrt(NULL, w));
        for (int w = 0; w < 0xC7; w++)
                printk(KERN_DEBUG "GR %p: %p\n", (void *) w,
                       (void *) vga_rgfx(NULL, w));
@@ -2023,6 +2056,7 @@ static struct fb_info *__devinit neo_alloc_fb_info(struct pci_dev *dev, const st
 
        info->fix.accel = id->driver_data;
 
+       mutex_init(&par->open_lock);
        par->pci_burst = !nopciburst;
        par->lcd_stretch = !nostretch;
        par->libretto = libretto;