backlight: Fix external uses of backlight internal semaphore
authorRichard Purdie <rpurdie@rpsys.net>
Thu, 8 Feb 2007 22:25:09 +0000 (22:25 +0000)
committerRichard Purdie <rpurdie@rpsys.net>
Tue, 20 Feb 2007 08:38:45 +0000 (08:38 +0000)
backlight_device->sem has a very specific use as documented in the
header file. The external users of this are using it for a different
reason, to serialise access to the update_status() method.

backlight users were supposed to implement their own internal
serialisation of update_status() if needed but everyone is doing
things differently and incorrectly. Therefore add a global mutex to
take care of serialisation for everyone, once and for all.

Locking for get_brightness remains optional since most users don't
need it.

Also update the lcd class in a similar way.

Signed-off-by: Richard Purdie <rpurdie@rpsys.net>
15 files changed:
arch/powerpc/kernel/traps.c
arch/powerpc/platforms/powermac/backlight.c
drivers/macintosh/via-pmu-backlight.c
drivers/misc/asus-laptop.c
drivers/usb/misc/appledisplay.c
drivers/video/aty/aty128fb.c
drivers/video/aty/atyfb_base.c
drivers/video/aty/radeon_backlight.c
drivers/video/backlight/backlight.c
drivers/video/backlight/lcd.c
drivers/video/chipsfb.c
drivers/video/nvidia/nv_backlight.c
drivers/video/riva/fbdev.c
include/linux/backlight.h
include/linux/lcd.h

index dcc6f159fd945070050ece10f77f64df90b70edb..35ce07b6a5bc3b340d90528f415ed7448d7422a5 100644 (file)
@@ -107,12 +107,10 @@ int die(const char *str, struct pt_regs *regs, long err)
        if (machine_is(powermac) && pmac_backlight) {
                struct backlight_properties *props;
 
-               down(&pmac_backlight->sem);
                props = pmac_backlight->props;
                props->brightness = props->max_brightness;
                props->power = FB_BLANK_UNBLANK;
-               props->update_status(pmac_backlight);
-               up(&pmac_backlight->sem);
+               backlight_update_status(pmac_backlight);
        }
        mutex_unlock(&pmac_backlight_mutex);
 #endif
index c3a89414ddc099d1baf91fe28234fe87b75b02b5..1be358c1448ac44f8eb9c78c34e33146c60926f1 100644 (file)
@@ -37,7 +37,9 @@ static int pmac_backlight_set_legacy_queued;
  */
 static atomic_t kernel_backlight_disabled = ATOMIC_INIT(0);
 
-/* Protect the pmac_backlight variable */
+/* Protect the pmac_backlight variable below.
+   You should hold this lock when using the pmac_backlight pointer to
+   prevent its potential removal. */
 DEFINE_MUTEX(pmac_backlight_mutex);
 
 /* Main backlight storage
@@ -49,9 +51,6 @@ DEFINE_MUTEX(pmac_backlight_mutex);
  * internal display, it doesn't matter. Other backlight drivers can be used
  * independently.
  *
- * Lock ordering:
- * pmac_backlight_mutex (global, main backlight)
- *   pmac_backlight->sem (backlight class)
  */
 struct backlight_device *pmac_backlight;
 
@@ -104,7 +103,6 @@ static void pmac_backlight_key_worker(struct work_struct *work)
                struct backlight_properties *props;
                int brightness;
 
-               down(&pmac_backlight->sem);
                props = pmac_backlight->props;
 
                brightness = props->brightness +
@@ -117,9 +115,7 @@ static void pmac_backlight_key_worker(struct work_struct *work)
                        brightness = props->max_brightness;
 
                props->brightness = brightness;
-               props->update_status(pmac_backlight);
-
-               up(&pmac_backlight->sem);
+               backlight_update_status(pmac_backlight);
        }
        mutex_unlock(&pmac_backlight_mutex);
 }
@@ -145,7 +141,6 @@ static int __pmac_backlight_set_legacy_brightness(int brightness)
        if (pmac_backlight) {
                struct backlight_properties *props;
 
-               down(&pmac_backlight->sem);
                props = pmac_backlight->props;
                props->brightness = brightness *
                        (props->max_brightness + 1) /
@@ -156,8 +151,7 @@ static int __pmac_backlight_set_legacy_brightness(int brightness)
                else if (props->brightness < 0)
                        props->brightness = 0;
 
-               props->update_status(pmac_backlight);
-               up(&pmac_backlight->sem);
+               backlight_update_status(pmac_backlight);
 
                error = 0;
        }
@@ -196,14 +190,11 @@ int pmac_backlight_get_legacy_brightness()
        if (pmac_backlight) {
                struct backlight_properties *props;
 
-               down(&pmac_backlight->sem);
                props = pmac_backlight->props;
 
                result = props->brightness *
                        (OLD_BACKLIGHT_MAX + 1) /
                        (props->max_brightness + 1);
-
-               up(&pmac_backlight->sem);
        }
        mutex_unlock(&pmac_backlight_mutex);
 
index db8bcc35bc03f5bfe039dbc72966f21f0a65dadf..7ba2042b2dd0ea354d6764af5d7a0d496f4fa026 100644 (file)
@@ -166,11 +166,9 @@ void __init pmu_backlight_init()
                                pmu_backlight_data.max_brightness / 15);
        }
 
-       down(&bd->sem);
        bd->props->brightness = level;
        bd->props->power = FB_BLANK_UNBLANK;
-       bd->props->update_status(bd);
-       up(&bd->sem);
+       backlight_update_status(bd);
 
        mutex_lock(&pmac_backlight_mutex);
        if (!pmac_backlight)
index d15ee5e3420138481dd2a8e803eb86931ef2db13..7ace5b9a3d28659a2c2616ac9ab22b2ed6494668 100644 (file)
@@ -348,13 +348,8 @@ static void lcd_blank(int blank)
        struct backlight_device *bd = asus_backlight_device;
 
        if (bd) {
-               down(&bd->sem);
-               if (likely(bd->props)) {
-                       bd->props->power = blank;
-                       if (likely(bd->props->update_status))
-                               bd->props->update_status(bd);
-               }
-               up(&bd->sem);
+               bd->props->power = blank;
+               backlight_update_status(bd);
        }
 }
 
@@ -1028,14 +1023,9 @@ static int asus_backlight_init(struct device *dev)
 
                asus_backlight_device = bd;
 
-               down(&bd->sem);
-               if (likely(bd->props)) {
-                       bd->props->brightness = read_brightness(NULL);
-                       bd->props->power = FB_BLANK_UNBLANK;
-                       if (likely(bd->props->update_status))
-                               bd->props->update_status(bd);
-               }
-               up(&bd->sem);
+               bd->props->brightness = read_brightness(NULL);
+               bd->props->power = FB_BLANK_UNBLANK;
+               backlight_update_status(bd);
        }
        return 0;
 }
index 416cde5893af83585a94bb3b787f6fc6b56bc93b..cd2c5574cf933be4950d48b5f3202bec3f6be40f 100644 (file)
@@ -189,11 +189,9 @@ static void appledisplay_work(struct work_struct *work)
                container_of(work, struct appledisplay, work.work);
        int retval;
 
-       up(&pdata->bd->sem);
        retval = appledisplay_bl_get_brightness(pdata->bd);
        if (retval >= 0)
                pdata->bd->props->brightness = retval;
-       down(&pdata->bd->sem);
 
        /* Poll again in about 125ms if there's still a button pressed */
        if (pdata->button_pressed)
@@ -288,9 +286,7 @@ static int appledisplay_probe(struct usb_interface *iface,
        }
 
        /* Try to get brightness */
-       up(&pdata->bd->sem);
        brightness = appledisplay_bl_get_brightness(pdata->bd);
-       down(&pdata->bd->sem);
 
        if (brightness < 0) {
                retval = brightness;
@@ -299,9 +295,7 @@ static int appledisplay_probe(struct usb_interface *iface,
        }
 
        /* Set brightness in backlight device */
-       up(&pdata->bd->sem);
        pdata->bd->props->brightness = brightness;
-       down(&pdata->bd->sem);
 
        /* save our data pointer in the interface device */
        usb_set_intfdata(iface, pdata);
index 1fa211c0d7bba45efbb8e1e8660117ddaa73a5fd..5d8f73b2b66f4febc53baedf5d713d3921a0c233 100644 (file)
@@ -1807,10 +1807,8 @@ static void aty128_bl_set_power(struct fb_info *info, int power)
        mutex_lock(&info->bl_mutex);
 
        if (info->bl_dev) {
-               down(&info->bl_dev->sem);
                info->bl_dev->props->power = power;
                __aty128_bl_update_status(info->bl_dev);
-               up(&info->bl_dev->sem);
        }
 
        mutex_unlock(&info->bl_mutex);
@@ -1847,11 +1845,9 @@ static void aty128_bl_init(struct aty128fb_par *par)
                219 * FB_BACKLIGHT_MAX / MAX_LEVEL);
        mutex_unlock(&info->bl_mutex);
 
-       down(&bd->sem);
        bd->props->brightness = aty128_bl_data.max_brightness;
        bd->props->power = FB_BLANK_UNBLANK;
-       bd->props->update_status(bd);
-       up(&bd->sem);
+       backlight_update_status(bd);
 
 #ifdef CONFIG_PMAC_BACKLIGHT
        mutex_lock(&pmac_backlight_mutex);
index 5ea5a00e58a339d82da1e32760debfebe377697c..23deb3566020f30280fd96fe302241c866d251d2 100644 (file)
@@ -2188,10 +2188,8 @@ static void aty_bl_set_power(struct fb_info *info, int power)
        mutex_lock(&info->bl_mutex);
 
        if (info->bl_dev) {
-               down(&info->bl_dev->sem);
                info->bl_dev->props->power = power;
                __aty_bl_update_status(info->bl_dev);
-               up(&info->bl_dev->sem);
        }
 
        mutex_unlock(&info->bl_mutex);
@@ -2224,11 +2222,9 @@ static void aty_bl_init(struct atyfb_par *par)
                0xFF * FB_BACKLIGHT_MAX / MAX_LEVEL);
        mutex_unlock(&info->bl_mutex);
 
-       down(&bd->sem);
        bd->props->brightness = aty_bl_data.max_brightness;
        bd->props->power = FB_BLANK_UNBLANK;
-       bd->props->update_status(bd);
-       up(&bd->sem);
+       backlight_update_status(bd);
 
 #ifdef CONFIG_PMAC_BACKLIGHT
        mutex_lock(&pmac_backlight_mutex);
index 8b66df6f199abd67a0927ed37c10be36c72a7850..9cfcecac8b5e9c4b84623d8b2908d74c2c565e74 100644 (file)
@@ -194,11 +194,9 @@ void radeonfb_bl_init(struct radeonfb_info *rinfo)
                217 * FB_BACKLIGHT_MAX / MAX_RADEON_LEVEL);
        mutex_unlock(&rinfo->info->bl_mutex);
 
-       down(&bd->sem);
        bd->props->brightness = radeon_bl_data.max_brightness;
        bd->props->power = FB_BLANK_UNBLANK;
-       bd->props->update_status(bd);
-       up(&bd->sem);
+       backlight_update_status(bd);
 
 #ifdef CONFIG_PMAC_BACKLIGHT
        mutex_lock(&pmac_backlight_mutex);
index 7a85be4d2b0a1751bec89135be24d32108bd7d91..347081daf7a469a5fe271308f6d079dde9232950 100644 (file)
@@ -37,8 +37,7 @@ static int fb_notifier_callback(struct notifier_block *self,
                if (!bd->props->check_fb ||
                    bd->props->check_fb(evdata->info)) {
                        bd->props->fb_blank = *(int *)evdata->data;
-                       if (bd->props && bd->props->update_status)
-                               bd->props->update_status(bd);
+                       backlight_update_status(bd);
                }
        up(&bd->sem);
        return 0;
@@ -97,8 +96,7 @@ static ssize_t backlight_store_power(struct class_device *cdev, const char *buf,
        if (bd->props) {
                pr_debug("backlight: set power to %d\n", power);
                bd->props->power = power;
-               if (bd->props->update_status)
-                       bd->props->update_status(bd);
+               backlight_update_status(bd);
                rc = count;
        }
        up(&bd->sem);
@@ -140,8 +138,7 @@ static ssize_t backlight_store_brightness(struct class_device *cdev, const char
                        pr_debug("backlight: set brightness to %d\n",
                                 brightness);
                        bd->props->brightness = brightness;
-                       if (bd->props->update_status)
-                               bd->props->update_status(bd);
+                       backlight_update_status(bd);
                        rc = count;
                }
        }
@@ -230,6 +227,7 @@ struct backlight_device *backlight_device_register(const char *name,
        if (!new_bd)
                return ERR_PTR(-ENOMEM);
 
+       mutex_init(&new_bd->update_lock);
        init_MUTEX(&new_bd->sem);
        new_bd->props = bp;
        memset(&new_bd->class_dev, 0, sizeof(new_bd->class_dev));
index 959024812abce6ca78d28165bb8b57dae47c977a..1e1e61a4b5da6cc6651f2e2a55fa7a76cd12dbe7 100644 (file)
@@ -198,6 +198,7 @@ struct lcd_device *lcd_device_register(const char *name, void *devdata,
                return ERR_PTR(-ENOMEM);
 
        init_MUTEX(&new_ld->sem);
+       mutex_init(&new_ld->update_lock);
        new_ld->props = lp;
        memset(&new_ld->class_dev, 0, sizeof(new_ld->class_dev));
        new_ld->class_dev.class = &lcd_class;
index 73cb426bf2d7db5428ecb348da4b3fc112d2704c..9a656bc6295c6ef432a511481ab353afba0c8c4d 100644 (file)
@@ -153,13 +153,11 @@ static int chipsfb_blank(int blank, struct fb_info *info)
                 * useful at blank = 1 too (saves battery, extends backlight
                 * life)
                 */
-               down(&pmac_backlight->sem);
                if (blank)
                        pmac_backlight->props->power = FB_BLANK_POWERDOWN;
                else
                        pmac_backlight->props->power = FB_BLANK_UNBLANK;
-               pmac_backlight->props->update_status(pmac_backlight);
-               up(&pmac_backlight->sem);
+               backlight_update_status(pmac_backlight);
        }
 
        mutex_unlock(&pmac_backlight_mutex);
@@ -415,10 +413,8 @@ chipsfb_pci_init(struct pci_dev *dp, const struct pci_device_id *ent)
        /* turn on the backlight */
        mutex_lock(&pmac_backlight_mutex);
        if (pmac_backlight) {
-               down(&pmac_backlight->sem);
                pmac_backlight->props->power = FB_BLANK_UNBLANK;
-               pmac_backlight->props->update_status(pmac_backlight);
-               up(&pmac_backlight->sem);
+               backlight_update_status(pmac_backlight);
        }
        mutex_unlock(&pmac_backlight_mutex);
 #endif /* CONFIG_PMAC_BACKLIGHT */
index 4254c090dc4be2a090e6cd74a24b34425c43c3ba..f5e42d0bd0dae808df211bc4996284900dbaac7f 100644 (file)
@@ -114,10 +114,8 @@ void nvidia_bl_set_power(struct fb_info *info, int power)
        mutex_lock(&info->bl_mutex);
 
        if (info->bl_dev) {
-               down(&info->bl_dev->sem);
                info->bl_dev->props->power = power;
                __nvidia_bl_update_status(info->bl_dev);
-               up(&info->bl_dev->sem);
        }
 
        mutex_unlock(&info->bl_mutex);
@@ -154,11 +152,9 @@ void nvidia_bl_init(struct nvidia_par *par)
                0x534 * FB_BACKLIGHT_MAX / MAX_LEVEL);
        mutex_unlock(&info->bl_mutex);
 
-       down(&bd->sem);
        bd->props->brightness = nvidia_bl_data.max_brightness;
        bd->props->power = FB_BLANK_UNBLANK;
-       bd->props->update_status(bd);
-       up(&bd->sem);
+       backlight_update_status(bd);
 
 #ifdef CONFIG_PMAC_BACKLIGHT
        mutex_lock(&pmac_backlight_mutex);
index fd825711bb638ca104cbeea5dea42ce39bc77d2f..b70d18f7fcd93e21ccdfd3e211fa7748dad83d13 100644 (file)
@@ -357,10 +357,8 @@ static void riva_bl_set_power(struct fb_info *info, int power)
        mutex_lock(&info->bl_mutex);
 
        if (info->bl_dev) {
-               down(&info->bl_dev->sem);
                info->bl_dev->props->power = power;
                __riva_bl_update_status(info->bl_dev);
-               up(&info->bl_dev->sem);
        }
 
        mutex_unlock(&info->bl_mutex);
@@ -397,11 +395,9 @@ static void riva_bl_init(struct riva_par *par)
                FB_BACKLIGHT_MAX);
        mutex_unlock(&info->bl_mutex);
 
-       down(&bd->sem);
        bd->props->brightness = riva_bl_data.max_brightness;
        bd->props->power = FB_BLANK_UNBLANK;
-       bd->props->update_status(bd);
-       up(&bd->sem);
+       backlight_update_status(bd);
 
 #ifdef CONFIG_PMAC_BACKLIGHT
        mutex_lock(&pmac_backlight_mutex);
index 287c62d956f2d80283c62586ca6a7786f3303c77..d1426b852bdfe28001b86b95532cd0c1d62971d8 100644 (file)
@@ -9,8 +9,24 @@
 #define _LINUX_BACKLIGHT_H
 
 #include <linux/device.h>
+#include <linux/mutex.h>
 #include <linux/notifier.h>
 
+/* Notes on locking:
+ *
+ * backlight_device->sem is an internal backlight lock protecting the props
+ * field and no code outside the core should need to touch it.
+ *
+ * Access to update_status() is serialised by the update_lock mutex since
+ * most drivers seem to need this and historically get it wrong.
+ *
+ * Most drivers don't need locking on their get_brightness() method.
+ * If yours does, you need to implement it in the driver. You can use the
+ * update_lock mutex if appropriate.
+ *
+ * Any other use of the locks below is probably wrong.
+ */
+
 struct backlight_device;
 struct fb_info;
 
@@ -44,12 +60,22 @@ struct backlight_device {
        struct semaphore sem;
        /* If this is NULL, the backing module is unloaded */
        struct backlight_properties *props;
+       /* Serialise access to update_status method */
+       struct mutex update_lock;
        /* The framebuffer notifier block */
        struct notifier_block fb_notif;
        /* The class device structure */
        struct class_device class_dev;
 };
 
+static inline void backlight_update_status(struct backlight_device *bd)
+{
+       mutex_lock(&bd->update_lock);
+       if (bd->props && bd->props->update_status)
+               bd->props->update_status(bd);
+       mutex_unlock(&bd->update_lock);
+}
+
 extern struct backlight_device *backlight_device_register(const char *name,
        struct device *dev,void *devdata,struct backlight_properties *bp);
 extern void backlight_device_unregister(struct backlight_device *bd);
index 8a468f168c451a4faa6abaa133e751a1db2a33b4..bfbf6552eb5158e2427032eb2ab6cea4e98073ea 100644 (file)
@@ -9,8 +9,24 @@
 #define _LINUX_LCD_H
 
 #include <linux/device.h>
+#include <linux/mutex.h>
 #include <linux/notifier.h>
 
+/* Notes on locking:
+ *
+ * lcd_device->sem is an internal backlight lock protecting the props
+ * field and no code outside the core should need to touch it.
+ *
+ * Access to set_power() is serialised by the update_lock mutex since
+ * most drivers seem to need this and historically get it wrong.
+ *
+ * Most drivers don't need locking on their get_power() method.
+ * If yours does, you need to implement it in the driver. You can use the
+ * update_lock mutex if appropriate.
+ *
+ * Any other use of the locks below is probably wrong.
+ */
+
 struct lcd_device;
 struct fb_info;
 
@@ -39,12 +55,22 @@ struct lcd_device {
        struct semaphore sem;
        /* If this is NULL, the backing module is unloaded */
        struct lcd_properties *props;
+       /* Serialise access to set_power method */
+       struct mutex update_lock;
        /* The framebuffer notifier block */
        struct notifier_block fb_notif;
        /* The class device structure */
        struct class_device class_dev;
 };
 
+static inline void lcd_set_power(struct lcd_device *ld, int power)
+{
+       mutex_lock(&ld->update_lock);
+       if (ld->props && ld->props->set_power)
+               ld->props->set_power(ld, power);
+       mutex_unlock(&ld->update_lock);
+}
+
 extern struct lcd_device *lcd_device_register(const char *name,
        void *devdata, struct lcd_properties *lp);
 extern void lcd_device_unregister(struct lcd_device *ld);