Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[safe/jmp/linux-2.6] / drivers / rtc / rtc-dev.c
index 0114a78..f118252 100644 (file)
@@ -13,7 +13,6 @@
 
 #include <linux/module.h>
 #include <linux/rtc.h>
-#include <linux/smp_lock.h>
 #include "rtc-core.h"
 
 static dev_t rtc_devt;
@@ -27,11 +26,8 @@ static int rtc_dev_open(struct inode *inode, struct file *file)
                                        struct rtc_device, char_dev);
        const struct rtc_class_ops *ops = rtc->ops;
 
-       lock_kernel();
-       if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags)) {
-               err = -EBUSY;
-               goto out;
-       }
+       if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
+               return -EBUSY;
 
        file->private_data = rtc;
 
@@ -41,13 +37,11 @@ static int rtc_dev_open(struct inode *inode, struct file *file)
                rtc->irq_data = 0;
                spin_unlock_irq(&rtc->irq_lock);
 
-               goto out;
+               return 0;
        }
 
        /* something has gone wrong */
        clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
-out:
-       unlock_kernel();
        return err;
 }
 
@@ -209,7 +203,7 @@ static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
        return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
 }
 
-static int rtc_dev_ioctl(struct inode *inode, struct file *file,
+static long rtc_dev_ioctl(struct file *file,
                unsigned int cmd, unsigned long arg)
 {
        int err = 0;
@@ -219,6 +213,10 @@ static int rtc_dev_ioctl(struct inode *inode, struct file *file,
        struct rtc_wkalrm alarm;
        void __user *uarg = (void __user *) arg;
 
+       err = mutex_lock_interruptible(&rtc->ops_lock);
+       if (err)
+               return err;
+
        /* check that the calling task has appropriate permissions
         * for certain ioctls. doing this check here is useful
         * to avoid duplicate code in each driver.
@@ -227,26 +225,31 @@ static int rtc_dev_ioctl(struct inode *inode, struct file *file,
        case RTC_EPOCH_SET:
        case RTC_SET_TIME:
                if (!capable(CAP_SYS_TIME))
-                       return -EACCES;
+                       err = -EACCES;
                break;
 
        case RTC_IRQP_SET:
                if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
-                       return -EACCES;
+                       err = -EACCES;
                break;
 
        case RTC_PIE_ON:
                if (rtc->irq_freq > rtc->max_user_freq &&
                                !capable(CAP_SYS_RESOURCE))
-                       return -EACCES;
+                       err = -EACCES;
                break;
        }
 
+       if (err)
+               goto done;
+
        /* try the driver's ioctl interface */
        if (ops->ioctl) {
                err = ops->ioctl(rtc->dev.parent, cmd, arg);
-               if (err != -ENOIOCTLCMD)
+               if (err != -ENOIOCTLCMD) {
+                       mutex_unlock(&rtc->ops_lock);
                        return err;
+               }
        }
 
        /* if the driver does not provide the ioctl interface
@@ -265,15 +268,19 @@ static int rtc_dev_ioctl(struct inode *inode, struct file *file,
 
        switch (cmd) {
        case RTC_ALM_READ:
+               mutex_unlock(&rtc->ops_lock);
+
                err = rtc_read_alarm(rtc, &alarm);
                if (err < 0)
                        return err;
 
                if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
-                       return -EFAULT;
-               break;
+                       err = -EFAULT;
+               return err;
 
        case RTC_ALM_SET:
+               mutex_unlock(&rtc->ops_lock);
+
                if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
                        return -EFAULT;
 
@@ -321,24 +328,26 @@ static int rtc_dev_ioctl(struct inode *inode, struct file *file,
                        }
                }
 
-               err = rtc_set_alarm(rtc, &alarm);
-               break;
+               return rtc_set_alarm(rtc, &alarm);
 
        case RTC_RD_TIME:
+               mutex_unlock(&rtc->ops_lock);
+
                err = rtc_read_time(rtc, &tm);
                if (err < 0)
                        return err;
 
                if (copy_to_user(uarg, &tm, sizeof(tm)))
-                       return -EFAULT;
-               break;
+                       err = -EFAULT;
+               return err;
 
        case RTC_SET_TIME:
+               mutex_unlock(&rtc->ops_lock);
+
                if (copy_from_user(&tm, uarg, sizeof(tm)))
                        return -EFAULT;
 
-               err = rtc_set_time(rtc, &tm);
-               break;
+               return rtc_set_time(rtc, &tm);
 
        case RTC_PIE_ON:
                err = rtc_irq_set_state(rtc, NULL, 1);
@@ -376,34 +385,40 @@ static int rtc_dev_ioctl(struct inode *inode, struct file *file,
                break;
 #endif
        case RTC_WKALM_SET:
+               mutex_unlock(&rtc->ops_lock);
                if (copy_from_user(&alarm, uarg, sizeof(alarm)))
                        return -EFAULT;
 
-               err = rtc_set_alarm(rtc, &alarm);
-               break;
+               return rtc_set_alarm(rtc, &alarm);
 
        case RTC_WKALM_RD:
+               mutex_unlock(&rtc->ops_lock);
                err = rtc_read_alarm(rtc, &alarm);
                if (err < 0)
                        return err;
 
                if (copy_to_user(uarg, &alarm, sizeof(alarm)))
-                       return -EFAULT;
-               break;
+                       err = -EFAULT;
+               return err;
 
 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
        case RTC_UIE_OFF:
+               mutex_unlock(&rtc->ops_lock);
                clear_uie(rtc);
                return 0;
 
        case RTC_UIE_ON:
-               return set_uie(rtc);
+               mutex_unlock(&rtc->ops_lock);
+               err = set_uie(rtc);
+               return err;
 #endif
        default:
                err = -ENOTTY;
                break;
        }
 
+done:
+       mutex_unlock(&rtc->ops_lock);
        return err;
 }
 
@@ -414,6 +429,8 @@ static int rtc_dev_release(struct inode *inode, struct file *file)
 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
        clear_uie(rtc);
 #endif
+       rtc_irq_set_state(rtc, NULL, 0);
+
        if (rtc->ops->release)
                rtc->ops->release(rtc->dev.parent);
 
@@ -432,7 +449,7 @@ static const struct file_operations rtc_dev_fops = {
        .llseek         = no_llseek,
        .read           = rtc_dev_read,
        .poll           = rtc_dev_poll,
-       .ioctl          = rtc_dev_ioctl,
+       .unlocked_ioctl = rtc_dev_ioctl,
        .open           = rtc_dev_open,
        .release        = rtc_dev_release,
        .fasync         = rtc_dev_fasync,